Finished first pass reviewing memory.hpp for C lib generation

This commit is contained in:
Edward R. Gonzalez 2024-11-30 23:38:27 -05:00
parent 6d04165b96
commit fbdb870986
15 changed files with 350 additions and 30 deletions

1
.gitignore vendored
View File

@ -31,3 +31,4 @@ project/auxillary/vis_ast/dependencies/temp
test/gen/original
singleheader/gen/scratch.hpp
test/gen/scratch.cpp
gen_c_library/gen

View File

@ -38,7 +38,8 @@
"android_native_app_glue.h": "c",
"raylib.h": "c",
"*.m": "cpp",
"atomic": "cpp"
"atomic": "cpp",
"gen.h": "c"
},
"C_Cpp.intelliSenseEngineFallback": "disabled",
"mesonbuild.configureOnOpen": true,

View File

@ -1,7 +1,7 @@
#define GEN_DEFINE_LIBRARY_CODE_CONSTANTS
#define GEN_ENFORCE_STRONG_CODE_TYPES
#define GEN_EXPOSE_BACKEND
#include "gen.cpp"
#include "../project/gen.cpp"
#include "helpers/push_ignores.inline.hpp"
#include "helpers/helper.hpp"
@ -16,6 +16,9 @@ GEN_NS_END
#include <cstdlib> // for system()
#include "components/fixed_arena.hpp"
#include "components/misc.hpp"
using namespace gen;
constexpr char const* generation_notice =
@ -80,6 +83,13 @@ Code dump_to_scratch_and_retireve( Code code )
return result;
}
CodeBody parse_file( const char* path )
{
FileContents file = file_read_contents( GlobalAllocator, true, path );
CodeBody code = parse_global_body( { file.size, (char const*)file.data } );
return code;
}
int gen_main()
{
#define project_dir "../project/"
@ -87,18 +97,102 @@ int gen_main()
Code push_ignores = scan_file( project_dir "helpers/push_ignores.inline.hpp" );
Code pop_ignores = scan_file( project_dir "helpers/pop_ignores.inline.hpp" );
Code single_header_start = scan_file( "components/header_start.hpp" );
Code c_library_header_start = scan_file( "components/header_start.hpp" );
Builder
header = Builder::open( "gen/gen.hpp" );
header = Builder::open( "gen/gen.h" );
header.print_fmt( generation_notice );
header.print_fmt("#pragma once\n\n");
header.print( push_ignores );
// Headers
{
header.print( c_library_header_start );
Code platform = scan_file( project_dir "dependencies/platform.hpp" );
Code macros = scan_file( project_dir "dependencies/macros.hpp" );
Code basic_types = scan_file( project_dir "dependencies/basic_types.hpp" );
Code debug = scan_file( project_dir "dependencies/debug.hpp" );
CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" );
CodeBody memory = def_body(ECode::Struct_Body);
for ( Code entry = parsed_memory.begin(); entry != parsed_memory.end(); ++ entry )
{
switch (entry->Type)
{
case ECode::Using:
{
log_fmt("REPLACE THIS MANUALLY: %S\n", entry->Name);
CodeUsing using_ver = entry.cast<CodeUsing>();
CodeTypedef typedef_ver = def_typedef(using_ver->Name, using_ver->UnderlyingType);
memory.append(typedef_ver);
}
break;
case ECode::Function:
{
CodeFn fn = entry.cast<CodeFn>();
s32 constexpr_found = fn->Specs.remove( ESpecifier::Constexpr );
if (constexpr_found > -1) {
log_fmt("Found constexpr proc\n");
fn->Specs.append(ESpecifier::Inline);
}
memory.append(entry);
}
break;
case ECode::Class:
case ECode::Struct:
{
CodeBody body = entry->Body->operator CodeBody();
CodeBody new_body = def_body( entry->Body->Type );
for ( Code body_entry = body.begin(); body_entry != body.end(); ++ body_entry ) switch
(body_entry->Type) {
case ECode::Preprocess_If:
{
ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), body_entry, body );
}
break;
default:
new_body.append(body_entry);
break;
}
entry->Body = rcast(AST*, new_body.ast);
memory.append(entry);
}
break;
case ECode::Preprocess_If:
{
ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, memory );
}
break;
case ECode::Preprocess_Pragma:
{
swap_pragma_region_implementation( txt("FixedArena"), gen_fixed_arenas, entry, memory);
}
break;
default: {
memory.append(entry);
}
break;
}
}
header.print_fmt( roll_own_dependencies_guard_start );
header.print( platform );
header.print_fmt( "\nGEN_NS_BEGIN\n" );
header.print( macros );
header.print( basic_types );
header.print( debug );
header.print( memory );
}
header.print( pop_ignores );
header.write();
format_file( "gen/gen.h" );
gen::deinit();
return 0;

View File

@ -0,0 +1,119 @@
// #pragma once
// #include "../project/gen.hpp"
// using namespace gen;
CodeBody gen_fixed_arenas()
{
CodeBody result = def_body(ECode::Global_Body);
char const* template_struct = stringize(
struct FixedArena_<Name>
{
char memory[<Size>];
Arena arena;
};
);
char const* template_interface = stringize(
inline
void fixed_arena_init_<Name>(FixedArena_<Name>* result) {
zero_size(& result.memory[0], <Size>);
result.arena = arena_init_from_memory(& result.memory[0], <Size>);
}
inline
ssize fixed_arena_size_remaining_<Name>(FixedArena_<Name>* fixed_arena, ssize alignment) {
return size_remaining(fixed_arena.arena, alignment);
}
);
CodeStruct arena_struct_1kb = parse_struct( token_fmt_impl( 3, "Name", txt("1KB"), "Size", txt("kilobytes(1)"), template_struct ));
CodeStruct arena_struct_4kb = parse_struct( token_fmt_impl( 3, "Name", txt("4KB"), "Size", txt("kilobytes(4)"), template_struct ));
CodeStruct arena_struct_8kb = parse_struct( token_fmt_impl( 3, "Name", txt("8KB"), "Size", txt("kilobytes(8)"), template_struct ));
CodeStruct arena_struct_16kb = parse_struct( token_fmt_impl( 3, "Name", txt("16KB"), "Size", txt("kilobytes(16)"), template_struct ));
CodeStruct arena_struct_32kb = parse_struct( token_fmt_impl( 3, "Name", txt("32KB"), "Size", txt("kilobytes(32)"), template_struct ));
CodeStruct arena_struct_64kb = parse_struct( token_fmt_impl( 3, "Name", txt("64KB"), "Size", txt("kilobytes(64)"), template_struct ));
CodeStruct arena_struct_128kb = parse_struct( token_fmt_impl( 3, "Name", txt("128KB"), "Size", txt("kilobytes(128)"), template_struct ));
CodeStruct arena_struct_256kb = parse_struct( token_fmt_impl( 3, "Name", txt("256KB"), "Size", txt("kilobytes(256)"), template_struct ));
CodeStruct arena_struct_512kb = parse_struct( token_fmt_impl( 3, "Name", txt("512KB"), "Size", txt("kilobytes(512)"), template_struct ));
CodeStruct arena_struct_1mb = parse_struct( token_fmt_impl( 3, "Name", txt("1MB"), "Size", txt("megabytes(1)"), template_struct ));
CodeStruct arena_struct_2mb = parse_struct( token_fmt_impl( 3, "Name", txt("2MB"), "Size", txt("megabytes(2)"), template_struct ));
CodeStruct arena_struct_4mb = parse_struct( token_fmt_impl( 3, "Name", txt("4MB"), "Size", txt("megabytes(4)"), template_struct ));
CodeBody arena_interface_1kb = parse_global_body( token_fmt_impl( 3, "Name", txt("1KB"), "Size", txt("kilobytes(1)"), template_interface ));
CodeBody arena_interface_4kb = parse_global_body( token_fmt_impl( 3, "Name", txt("4KB"), "Size", txt("kilobytes(4)"), template_interface ));
CodeBody arena_interface_8kb = parse_global_body( token_fmt_impl( 3, "Name", txt("8KB"), "Size", txt("kilobytes(8)"), template_interface ));
CodeBody arena_interface_16kb = parse_global_body( token_fmt_impl( 3, "Name", txt("16KB"), "Size", txt("kilobytes(16)"), template_interface ));
CodeBody arena_interface_32kb = parse_global_body( token_fmt_impl( 3, "Name", txt("32KB"), "Size", txt("kilobytes(32)"), template_interface ));
CodeBody arena_interface_64kb = parse_global_body( token_fmt_impl( 3, "Name", txt("64KB"), "Size", txt("kilobytes(64)"), template_interface ));
CodeBody arena_interface_128kb = parse_global_body( token_fmt_impl( 3, "Name", txt("128KB"), "Size", txt("kilobytes(128)"), template_interface ));
CodeBody arena_interface_256kb = parse_global_body( token_fmt_impl( 3, "Name", txt("256KB"), "Size", txt("kilobytes(256)"), template_interface ));
CodeBody arena_interface_512kb = parse_global_body( token_fmt_impl( 3, "Name", txt("512KB"), "Size", txt("kilobytes(512)"), template_interface ));
CodeBody arena_interface_1mb = parse_global_body( token_fmt_impl( 3, "Name", txt("1MB"), "Size", txt("megabytes(1)"), template_interface ));
CodeBody arena_interface_2mb = parse_global_body( token_fmt_impl( 3, "Name", txt("2MB"), "Size", txt("megabytes(2)"), template_interface ));
CodeBody arena_interface_4mb = parse_global_body( token_fmt_impl( 3, "Name", txt("4MB"), "Size", txt("megabytes(4)"), template_interface ));
result.append(arena_struct_1kb);
result.append(arena_struct_4kb);
result.append(arena_struct_8kb);
result.append(arena_struct_16kb);
result.append(arena_struct_32kb);
result.append(arena_struct_128kb);
result.append(arena_struct_256kb);
result.append(arena_struct_512kb);
result.append(arena_struct_1mb);
result.append(arena_struct_2mb);
result.append(arena_struct_4mb);
result.append(arena_interface_1kb);
result.append(arena_interface_4kb);
result.append(arena_interface_8kb);
result.append(arena_interface_16kb);
result.append(arena_interface_32kb);
result.append(arena_interface_128kb);
result.append(arena_interface_256kb);
result.append(arena_interface_512kb);
result.append(arena_interface_1mb);
result.append(arena_interface_2mb);
result.append(arena_interface_4mb);
CodeDefine def = def_define(txt("fixed_arena_allocator_info(fixed_arena)"), code({ arena_allocator_proc, & fixed_arena.arena }) );
result.append(def);
result.append(parse_global_body(txt(R"(
#define fixed_arena_init(expr) _Generic((expr), \
FixedArena_1KB* : fixed_arena_init_1KB, \
FixedArena_4KB* : fixed_arena_init_4KB, \
FixedArena_8KB* : fixed_arena_init_8KB, \
FixedArena_16KB* : fixed_arena_init_16KB, \
FixedArena_32KB* : fixed_arena_init_32KB, \
FixedArena_64KB* : fixed_arena_init_64KB, \
FixedArena_128KB* : fixed_arena_init_128KB, \
FixedArena_256KB* : fixed_arena_init_256KB, \
FixedArena_512KB* : fixed_arena_init_512KB, \
FixedArena_1MB* : fixed_arena_init_1MB, \
FixedArena_2MB* : fixed_arena_init_2MB, \
FixedArena_4MB* : fixed_arena_init_4MB \
)(expr)
#define fixed_arena_size_remaining(expr, alignment) _Generic((expr), \
FixedArena_1KB* : fixed_arena_size_remaining_1KB, \
FixedArena_4KB* : fixed_arena_size_remaining_4KB, \
FixedArena_8KB* : fixed_arena_size_remaining_8KB, \
FixedArena_16KB* : fixed_arena_size_remaining_16KB, \
FixedArena_32KB* : fixed_arena_size_remaining_32KB, \
FixedArena_64KB* : fixed_arena_size_remaining_64KB, \
FixedArena_128KB* : fixed_arena_size_remaining_128KB, \
FixedArena_256KB* : fixed_arena_size_remaining_256KB, \
FixedArena_512KB* : fixed_arena_size_remaining_512KB, \
FixedArena_1MB* : fixed_arena_size_remaining_1MB, \
FixedArena_2MB* : fixed_arena_size_remaining_2MB, \
FixedArena_4MB* : fixed_arena_size_remaining_4MB \
)(expr, alignment)
)"
)));
return result;
}

View File

@ -0,0 +1,14 @@
/*
gencpp: An attempt at "simple" staged metaprogramming for c/c++.
See Readme.md for more information from the project repository.
Public Address:
https://github.com/Ed94/gencpp
This is a single header C-Library variant.
Define GEN_IMPLEMENTATION before including this file in a single compilation unit.
*/
#if ! defined(GEN_DONT_ENFORCE_GEN_TIME_GUARD) && ! defined(GEN_TIME)
# error Gen.hpp : GEN_TIME not defined
#endif

View File

@ -0,0 +1,61 @@
// #pragma once
// #include "../project/gen.hpp"
// using namespace gen;
using SwapContentProc = CodeBody(void);
b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& body )
{
CodePreprocessCond cond = entry_iter.cast<CodePreprocessCond>();
if ( cond->Content.contains(cond_sig) )
{
s32 depth = 1;
++ entry_iter; for(b32 continue_for = true; continue_for && entry_iter != body.end(); ++ entry_iter) switch
(entry_iter->Type) {
case ECode::Preprocess_If:
case ECode::Preprocess_IfDef:
case ECode::Preprocess_IfNotDef:
depth ++;
break;
case ECode::Preprocess_EndIf:
{
depth --;
if (depth == 0) {
continue_for = false;
}
}
break;
}
}
return entry_iter != body.end();
}
void swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body )
{
CodePragma possible_region = entry_iter.cast<CodePragma>();
String region_sig = string_fmt_buf(GlobalAllocator, "region %s", region_name.Ptr);
String endregion_sig = string_fmt_buf(GlobalAllocator, "endregion %s", region_name.Ptr);
if ( possible_region->Content.contains(region_sig))
{
body.append(possible_region);
body.append(swap_content());
++ entry_iter; for(b32 continue_for = true; continue_for; ++entry_iter) switch
(entry_iter->Type) {
case ECode::Preprocess_Pragma:
{
CodePragma possible_end_region = entry_iter.cast<CodePragma>();
if ( possible_end_region->Content.contains(endregion_sig) ) {
body.append(possible_end_region);
continue_for = false;
}
}
break;
}
}
body.append(entry_iter);
}

View File

@ -12,12 +12,3 @@
#if ! defined(GEN_DONT_ENFORCE_GEN_TIME_GUARD) && ! defined(GEN_TIME)
# error Gen.hpp : GEN_TIME not defined
#endif
#ifdef GEN_DONT_USE_NAMESPACE
# define GEN_NS_BEGIN
# define GEN_NS_END
#else
# define GEN_NS_BEGIN namespace gen {
# define GEN_NS_END }
#endif

View File

@ -165,6 +165,46 @@ struct CodeSpecifiers
return -1;
}
s32 remove( SpecifierT to_remove )
{
if ( ast == nullptr )
{
log_failure("CodeSpecifiers: Attempted to append to a null specifiers AST!");
return -1;
}
if ( raw()->NumEntries == AST::ArrSpecs_Cap )
{
log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST::ArrSpecs_Cap );
return -1;
}
s32 result = -1;
s32 curr = 0;
s32 next = 0;
for(; next < raw()->NumEntries; ++ curr, ++ next)
{
SpecifierT spec = raw()->ArrSpecs[next];
if (spec == to_remove)
{
result = next;
next ++;
if (next >= raw()->NumEntries)
break;
spec = raw()->ArrSpecs[next];
}
raw()->ArrSpecs[ curr ] = spec;
}
if (result > -1) {
raw()->NumEntries --;
}
return result;
}
void to_string( String& result );
AST* raw()
{

View File

@ -133,6 +133,7 @@ extern CodeType t_typename;
#pragma region Macros
#ifndef token_fmt
# define gen_main main
# define __ NoCode
@ -151,6 +152,7 @@ extern CodeType t_typename;
// Takes a format string (char const*) and a list of tokens (StrC) and returns a StrC of the formatted string.
# define token_fmt( ... ) GEN_NS token_fmt_impl( (num_args( __VA_ARGS__ ) + 1) / 2, __VA_ARGS__ )
#endif
#pragma endregion Macros

View File

@ -714,7 +714,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false )
char interface_arr_mem[ kilobytes(4) ] {0};
Array<CodeType> interfaces; {
Arena arena = arena_init_from_memory( interface_arr_mem, kilobytes(4) );
array_init_reserve<CodeType>( allocator_info(arena), 4 );
interfaces = array_init_reserve<CodeType>( allocator_info(arena), 4 );
}
// TODO(Ed) : Make an AST_DerivedType, we'll store any arbitary derived type into there as a linear linked list of them.

View File

@ -123,8 +123,8 @@ typedef s8 b8;
typedef s16 b16;
typedef s32 b32;
using mem_ptr = void*;
using mem_ptr_const = void const*;
typedef void* mem_ptr;
typedef void const* mem_ptr_const ;
#if ! GEN_COMPILER_C
template<typename Type> uptr to_uptr( Type* ptr ) { return (uptr)ptr; }

View File

@ -54,7 +54,7 @@ template<class Type> ArrayHeader* get_header(Array<Type>& array);
template<class Type> forceinline Type* begin(Array<Type>& array) { return array; }
template<class Type> forceinline Type* end(Array<Type>& array) { return array + get_header(array)->Num; }
template<class Type> forceinline Type* next(Type* entry) { return entry + 1; }
template<class Type> forceinline Type* next(Array<Type>& array, Type* entry) { return entry + 1; }
struct ArrayHeader {
AllocatorInfo Allocator;
@ -250,7 +250,7 @@ bool fill(Array<Type>& array, usize begin, usize end, Type value)
template<class Type> inline
void free(Array<Type>& array) {
ArrayHeader* header = get_header(array);
gen::free(header->Allocator, header);
GEN_NS free(header->Allocator, header);
Type*& Data = rcast(Type*&, array);
Data = nullptr;
}

View File

@ -200,7 +200,7 @@
// This is intended to only really be used internally or with the C-library variant
// C++ users can just use the for-range directly.
#if GEN_COMPILER_C
# define foreach(Type, entry_id, iterable) for ( Type entry_id = begin(iterable); entry_id != end(iterable); entry_id = next(entry_id) )
# define foreach(Type, entry_id, iterable) for ( Type entry_id = begin(iterable); entry_id != end(iterable); entry_id = next(iterable, entry_id) )
#else
# define foreach(Type, entry_id, iterable) for ( Type entry_id : iterable )
#endif

View File

@ -80,10 +80,7 @@ enum AllocType : u8
EAllocation_RESIZE,
};
using AllocatorProc = void* ( void* allocator_data, AllocType type
, ssize size, ssize alignment
, void* old_memory, ssize old_size
, u64 flags );
typedef void*(AllocatorProc)( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags );
struct AllocatorInfo
{

View File

@ -90,7 +90,7 @@ else {
$optimize = $true
}
if ( $bootstrap -eq $false -and $singleheader -eq $false -and $unreal -eq $false -and $test -eq $false ) {
if ( $bootstrap -eq $false -and $singleheader -eq $false -and $c_library -eq $false -and $unreal -eq $false -and $test -eq $false ) {
throw "No build target specified. One must be specified, this script will not assume one"
}
@ -215,7 +215,7 @@ if ( $c_library )
build-simple $path_build $includes $compiler_args $linker_args $unit $executable
Push-Location $path_singleheader
Push-Location $path_c_library
if ( Test-Path( $executable ) ) {
write-host "`nRunning c_library generator"
$time_taken = Measure-Command { & $executable