Introduced the general context struct for gencpp

This commit is contained in:
Edward R. Gonzalez 2024-12-13 19:16:52 -05:00
parent 78bcc21130
commit 76ac3a0f93
30 changed files with 831 additions and 852 deletions

View File

@ -150,7 +150,7 @@ The convention you'll see used throughout the upfront interface of the library i
3. Populate immediate fields (Name, Type, ModuleFlags, etc)
4. Populate sub-entires using `add_entry`. If using the default serialization function `to_strbuilder`, follow the order at which entires are expected to appear (there is a strong ordering expected).
Names or Content fields are interned strings and thus showed be cached using `get_cached_string` if its desired to preserve that behavior.
Names or Content fields are interned strings and thus showed be cached using `cache_str` if its desired to preserve that behavior.
`def_operator` is the most sophisticated upfront constructor as it has multiple permutations of definitions that could be created that are not trivial to determine if valid.

View File

@ -15,7 +15,7 @@ Builder builder_open( char const* path )
return result;
}
result.Buffer = strbuilder_make_reserve( GlobalAllocator, Builder_StrBufferReserve );
result.Buffer = strbuilder_make_reserve( _ctx->Allocator_Temp, _ctx->InitSize_BuilderBuffer );
// log_fmt("$Builder - Opened file: %s\n", result.File.filename );
return result;

View File

@ -20,7 +20,7 @@ Code scan_file( char const* path )
GEN_FATAL("scan_file: %s is empty", path );
}
StrBuilder str = strbuilder_make_reserve( GlobalAllocator, fsize );
StrBuilder str = strbuilder_make_reserve( _ctx->Allocator_Temp, fsize );
file_read( & file, str, fsize );
strbuilder_get_header(str)->Length = fsize;
@ -117,7 +117,7 @@ Code scan_file( char const* path )
}
CodeBody parse_file( const char* path ) {
FileContents file = file_read_contents( GlobalAllocator, true, path );
FileContents file = file_read_contents( _ctx->Allocator_Temp, true, path );
Str content = { (char const*)file.data, file.size };
CodeBody code = parse_global_body( content );
log_fmt("\nParsed: %s\n", path);

View File

@ -27,7 +27,8 @@ constexpr char const* generation_notice =
int gen_main()
{
gen::init();
gen::Context ctx {};
gen::init( & ctx);
CodeBody gen_component_header = def_global_body( args(
def_preprocess_cond( PreprocessCond_IfDef, txt("GEN_INTELLISENSE_DIRECTIVES") ),
@ -69,6 +70,6 @@ int gen_main()
builder_print( & header_ast_inlines, format(ast_inlines) );
builder_write( & header_ast_inlines);
gen::deinit();
gen::deinit(& ctx);
return 0;
}

View File

@ -10,7 +10,7 @@ global Code Code_Invalid;
Str code_debug_str(Code self)
{
GEN_ASSERT(self != nullptr);
StrBuilder result_stack = strbuilder_make_reserve( GlobalAllocator, kilobytes(1) );
StrBuilder result_stack = strbuilder_make_reserve( _ctx->Allocator_Temp, kilobytes(1) );
StrBuilder* result = & result_stack;
if ( self->Parent )
@ -372,7 +372,7 @@ Code code_duplicate(Code self)
StrBuilder code_to_strbuilder(Code self)
{
StrBuilder result = strbuilder_make_str( GlobalAllocator, txt("") );
StrBuilder result = strbuilder_make_str( _ctx->Allocator_Temp, txt("") );
code_to_strbuilder_ptr( self, & result );
return result;
}
@ -660,8 +660,8 @@ bool code_is_equal( Code self, Code other )
"so it must be verified by eye for now\n" \
"AST Content:\n%S\n" \
"Other Content:\n%S\n" \
, str_visualize_whitespace(self->content, GlobalAllocator) \
, str_visualize_whitespace(other->content, GlobalAllocator) \
, str_visualize_whitespace(self->content, _ctx->Allocator_Temp) \
, str_visualize_whitespace(other->content, _ctx->Allocator_Temp) \
); \
}

View File

@ -368,7 +368,7 @@ int AST_ArrSpecs_Cap =
(
AST_POD_Size
- sizeof(Code)
- sizeof(StringCached)
- sizeof(StrCached)
- sizeof(Code) * 2
- sizeof(Token*)
- sizeof(Code)
@ -414,13 +414,13 @@ struct AST
Code PostNameMacro; // Only used with parameters for specifically UE_REQUIRES (Thanks Unreal)
};
};
StringCached Content; // Attributes, Comment, Execution, Include
StrCached Content; // Attributes, Comment, Execution, Include
struct {
Specifier ArrSpecs[AST_ArrSpecs_Cap]; // Specifiers
Code NextSpecs; // Specifiers; If ArrSpecs is full, then NextSpecs is used.
};
};
StringCached Name;
StrCached Name;
union {
Code Prev;
Code Front;

View File

@ -31,7 +31,7 @@ struct AST_Body
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
Code Front;
Code Back;
Token* Tok;
@ -46,9 +46,9 @@ struct AST_Attributes
{
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
StringCached Content;
StrCached Content;
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -64,7 +64,7 @@ struct AST_BaseClass
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -79,9 +79,9 @@ struct AST_Comment
{
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
StringCached Content;
StrCached Content;
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -106,7 +106,7 @@ struct AST_Class
char _PAD_PROPERTIES_2_[ sizeof(AST*) ];
};
};
StringCached Name;
StrCached Name;
CodeTypename Prev;
CodeTypename Next;
Token* Tok;
@ -132,7 +132,7 @@ struct AST_Constructor
char _PAD_PROPERTIES_2_ [ sizeof(AST*) * 2 ];
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -146,9 +146,9 @@ struct AST_Define
{
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
StringCached Content;
StrCached Content;
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -172,7 +172,7 @@ struct AST_Destructor
char _PAD_PROPERTIES_3_ [ sizeof(AST*) ];
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -197,7 +197,7 @@ struct AST_Enum
char _PAD_PROPERTIES_2_[ sizeof(AST*) ];
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -212,9 +212,9 @@ struct AST_Exec
{
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
StringCached Content;
StrCached Content;
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -230,7 +230,7 @@ struct AST_Expr
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -245,7 +245,7 @@ struct AST_Expr_Assign
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -260,7 +260,7 @@ struct AST_Expr_Alignof
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -275,7 +275,7 @@ struct AST_Expr_Binary
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -290,7 +290,7 @@ struct AST_Expr_CStyleCast
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -305,7 +305,7 @@ struct AST_Expr_FunctionalCast
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -320,7 +320,7 @@ struct AST_Expr_CppCast
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -335,7 +335,7 @@ struct AST_Expr_ProcCall
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -350,7 +350,7 @@ struct AST_Expr_Decltype
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -365,7 +365,7 @@ struct AST_Expr_Comma
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -380,7 +380,7 @@ struct AST_Expr_AMS
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -395,7 +395,7 @@ struct AST_Expr_Sizeof
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -410,7 +410,7 @@ struct AST_Expr_Subscript
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -425,7 +425,7 @@ struct AST_Expr_Ternary
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -440,7 +440,7 @@ struct AST_Expr_UnaryPrefix
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -455,7 +455,7 @@ struct AST_Expr_UnaryPostfix
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -470,7 +470,7 @@ struct AST_Expr_Element
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -492,7 +492,7 @@ struct AST_Extern
char _PAD_PROPERTIES_2_[ sizeof(AST*) ];
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -506,9 +506,9 @@ struct AST_Include
{
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
StringCached Content;
StrCached Content;
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -530,7 +530,7 @@ struct AST_Friend
char _PAD_PROPERTIES_2_[ sizeof(AST*) ];
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -555,7 +555,7 @@ struct AST_Fn
char _PAD_PROPERTIES_ [ sizeof(AST*) ];
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -571,7 +571,7 @@ struct AST_Module
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -592,7 +592,7 @@ struct AST_NS
char _PAD_PROPERTIES_2_[ sizeof(AST*) ];
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -618,7 +618,7 @@ struct AST_Operator
char _PAD_PROPERTIES_ [ sizeof(AST*) ];
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -644,7 +644,7 @@ struct AST_OpCast
char _PAD_PROPERTIES_3_[ sizeof(AST*) ];
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -668,7 +668,7 @@ struct AST_Params
// char _PAD_PROPERTIES_3_[sizeof( AST* )];
};
};
StringCached Name;
StrCached Name;
CodeParams Last;
CodeParams Next;
Token* Tok;
@ -683,9 +683,9 @@ struct AST_Pragma
{
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
StringCached Content;
StrCached Content;
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -699,9 +699,9 @@ struct AST_PreprocessCond
{
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
StringCached Content;
StrCached Content;
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -714,7 +714,7 @@ static_assert( sizeof(AST_PreprocessCond) == sizeof(AST), "ERROR: AST_Preprocess
struct AST_Specifiers
{
Specifier ArrSpecs[ AST_ArrSpecs_Cap ];
StringCached Name;
StrCached Name;
CodeSpecifiers NextSpecs;
Code Prev;
Code Next;
@ -732,7 +732,7 @@ struct AST_Stmt
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -747,7 +747,7 @@ struct AST_Stmt_Break
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -762,7 +762,7 @@ struct AST_Stmt_Case
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -777,7 +777,7 @@ struct AST_Stmt_Continue
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -792,7 +792,7 @@ struct AST_Stmt_Decl
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -807,7 +807,7 @@ struct AST_Stmt_Do
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -822,7 +822,7 @@ struct AST_Stmt_Expr
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -837,7 +837,7 @@ struct AST_Stmt_Else
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -852,7 +852,7 @@ struct AST_Stmt_If
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -867,7 +867,7 @@ struct AST_Stmt_For
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -882,7 +882,7 @@ struct AST_Stmt_Goto
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -897,7 +897,7 @@ struct AST_Stmt_Label
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -912,7 +912,7 @@ struct AST_Stmt_Switch
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -927,7 +927,7 @@ struct AST_Stmt_While
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
StrCached Name;
CodeExpr Prev;
CodeExpr Next;
Token* Tok;
@ -953,7 +953,7 @@ struct AST_Struct
char _PAD_PROPERTIES_2_[ sizeof(AST*) ];
};
};
StringCached Name;
StrCached Name;
CodeTypename Prev;
CodeTypename Next;
Token* Tok;
@ -976,7 +976,7 @@ struct AST_Template
char _PAD_PROPERTIES_2_[ sizeof(AST*) ];
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -1005,7 +1005,7 @@ struct AST_Type
// CodeSpecifiers SpecsFuncSuffix; // Only used for function signatures
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -1032,7 +1032,7 @@ struct AST_Typename
CodeSpecifiers SpecsFuncSuffix; // Only used for function signatures
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -1058,7 +1058,7 @@ struct AST_Typedef
char _PAD_PROPERTIES_2_[ sizeof(AST*) * 3 ];
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -1082,7 +1082,7 @@ struct AST_Union
char _PAD_PROPERTIES_2_[ sizeof(AST*) ];
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -1106,7 +1106,7 @@ struct AST_Using
char _PAD_PROPERTIES_[ sizeof(AST*) * 3 ];
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -1132,7 +1132,7 @@ struct AST_Var
CodeVar NextVar;
};
};
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;

View File

@ -6,7 +6,7 @@
inline
StrBuilder attributes_to_strbuilder(CodeAttributes attributes) {
GEN_ASSERT(attributes);
char* raw = ccast(char*, str_duplicate( attributes->Content, GlobalAllocator ).Ptr);
char* raw = ccast(char*, str_duplicate( attributes->Content, _ctx->Allocator_Temp ).Ptr);
StrBuilder result = { raw };
return result;
}
@ -21,7 +21,7 @@ void attributes_to_strbuilder_ref(CodeAttributes attributes, StrBuilder* result)
StrBuilder body_to_strbuilder(CodeBody body)
{
GEN_ASSERT(body);
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 128 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 128 );
switch ( body->Type )
{
case CT_Untyped:
@ -82,7 +82,7 @@ void body_to_strbuilder_export( CodeBody body, StrBuilder* result )
inline
StrBuilder comment_to_strbuilder(CodeComment comment) {
GEN_ASSERT(comment);
char* raw = ccast(char*, str_duplicate( comment->Content, GlobalAllocator ).Ptr);
char* raw = ccast(char*, str_duplicate( comment->Content, _ctx->Allocator_Temp ).Ptr);
StrBuilder result = { raw };
return result;
}
@ -96,7 +96,7 @@ void comment_to_strbuilder_ref(CodeComment comment, StrBuilder* result) {
StrBuilder constructor_to_strbuilder(CodeConstructor self)
{
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 128 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 128 );
switch (self->Type)
{
case CT_Constructor:
@ -159,7 +159,7 @@ void constructor_to_strbuilder_fwd(CodeConstructor self, StrBuilder* result )
StrBuilder class_to_strbuilder( CodeClass self )
{
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 512 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 512 );
switch ( self->Type )
{
case CT_Class:
@ -241,7 +241,7 @@ void class_to_strbuilder_fwd( CodeClass self, StrBuilder* result )
StrBuilder define_to_strbuilder(CodeDefine define)
{
return strbuilder_fmt_buf( GlobalAllocator, "#define %S %S", define->Name, define->Content );
return strbuilder_fmt_buf( _ctx->Allocator_Temp, "#define %S %S", define->Name, define->Content );
}
void define_to_strbuilder_ref(CodeDefine define, StrBuilder* result )
@ -251,7 +251,7 @@ void define_to_strbuilder_ref(CodeDefine define, StrBuilder* result )
StrBuilder destructor_to_strbuilder(CodeDestructor self)
{
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 128 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 128 );
switch ( self->Type )
{
case CT_Destructor:
@ -308,7 +308,7 @@ void destructor_to_strbuilder_fwd(CodeDestructor self, StrBuilder* result )
StrBuilder enum_to_strbuilder(CodeEnum self)
{
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 512 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 512 );
switch ( self->Type )
{
case CT_Enum:
@ -443,7 +443,7 @@ void enum_to_strbuilder_class_fwd(CodeEnum self, StrBuilder* result )
StrBuilder exec_to_strbuilder(CodeExec exec)
{
GEN_ASSERT(exec);
char* raw = ccast(char*, str_duplicate( exec->Content, GlobalAllocator ).Ptr);
char* raw = ccast(char*, str_duplicate( exec->Content, _ctx->Allocator_Temp ).Ptr);
StrBuilder result = { raw };
return result;
}
@ -458,7 +458,7 @@ void extern_to_strbuilder(CodeExtern self, StrBuilder* result )
StrBuilder include_to_strbuilder(CodeInclude include)
{
return strbuilder_fmt_buf( GlobalAllocator, "#include %S\n", include->Content );
return strbuilder_fmt_buf( _ctx->Allocator_Temp, "#include %S\n", include->Content );
}
void include_to_strbuilder_ref( CodeInclude include, StrBuilder* result )
@ -468,7 +468,7 @@ void include_to_strbuilder_ref( CodeInclude include, StrBuilder* result )
StrBuilder friend_to_strbuilder(CodeFriend self)
{
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 256 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 256 );
friend_to_strbuilder_ref( self, & result );
return result;
}
@ -490,7 +490,7 @@ void friend_to_strbuilder_ref(CodeFriend self, StrBuilder* result )
StrBuilder fn_to_strbuilder(CodeFn self)
{
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 512 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 512 );
switch ( self->Type )
{
case CT_Function:
@ -621,7 +621,7 @@ void fn_to_strbuilder_fwd(CodeFn self, StrBuilder* result )
StrBuilder module_to_strbuilder(CodeModule self)
{
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 64 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 64 );
module_to_strbuilder_ref( self, & result );
return result;
}
@ -639,7 +639,7 @@ void module_to_strbuilder_ref(CodeModule self, StrBuilder* result )
StrBuilder namespace_to_strbuilder(CodeNS self)
{
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 512 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 512 );
namespace_to_strbuilder_ref( self, & result );
return result;
}
@ -654,7 +654,7 @@ void namespace_to_strbuilder_ref(CodeNS self, StrBuilder* result )
StrBuilder code_op_to_strbuilder(CodeOperator self)
{
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 512 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 512 );
switch ( self->Type )
{
case CT_Operator:
@ -776,7 +776,7 @@ void code_op_to_strbuilder_fwd(CodeOperator self, StrBuilder* result )
StrBuilder opcast_to_strbuilder(CodeOpCast self)
{
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 128 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 128 );
switch ( self->Type )
{
case CT_Operator_Cast:
@ -867,7 +867,7 @@ StrBuilder params_to_strbuilder(CodeParams self)
{
GEN_ASSERT(self);
GEN_ASSERT(self);
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 128 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 128 );
params_to_strbuilder_ref( self, & result );
return result;
}
@ -914,7 +914,7 @@ void params_to_strbuilder_ref( CodeParams self, StrBuilder* result )
StrBuilder preprocess_to_strbuilder(CodePreprocessCond self)
{
GEN_ASSERT(self);
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 256 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 256 );
switch ( self->Type )
{
case CT_Preprocess_If:
@ -978,7 +978,7 @@ void preprocess_to_strbuilder_endif(CodePreprocessCond cond, StrBuilder* result
StrBuilder pragma_to_strbuilder(CodePragma self)
{
GEN_ASSERT(self);
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 256 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 256 );
pragma_to_strbuilder_ref( self, & result );
return result;
}
@ -990,7 +990,7 @@ void pragma_to_strbuilder_ref(CodePragma self, StrBuilder* result )
StrBuilder specifiers_to_strbuilder(CodeSpecifiers self)
{
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 64 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 64 );
specifiers_to_strbuilder_ref( self, & result );
return result;
}
@ -1013,7 +1013,7 @@ StrBuilder struct_to_strbuilder(CodeStruct self)
{
GEN_ASSERT(self);
GEN_ASSERT(self);
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 512 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 512 );
switch ( self->Type )
{
case CT_Struct:
@ -1096,7 +1096,7 @@ void struct_to_strbuilder_fwd( CodeStruct self, StrBuilder* result )
StrBuilder template_to_strbuilder(CodeTemplate self)
{
GEN_ASSERT(self);
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 1024 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 1024 );
template_to_strbuilder_ref( self, & result );
return result;
}
@ -1116,7 +1116,7 @@ void template_to_strbuilder_ref(CodeTemplate self, StrBuilder* result )
StrBuilder typedef_to_strbuilder(CodeTypedef self)
{
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 128 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 128 );
typedef_to_strbuilder_ref( self, & result );
return result;
}
@ -1158,7 +1158,7 @@ void typedef_to_strbuilder_ref(CodeTypedef self, StrBuilder* result )
StrBuilder typename_to_strbuilder(CodeTypename self)
{
StrBuilder result = strbuilder_make_str( GlobalAllocator, txt("") );
StrBuilder result = strbuilder_make_str( _ctx->Allocator_Temp, txt("") );
typename_to_strbuilder_ref( self, & result );
return result;
}
@ -1221,7 +1221,7 @@ void typename_to_strbuilder_ref(CodeTypename self, StrBuilder* result )
StrBuilder union_to_strbuilder(CodeUnion self)
{
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 512 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 512 );
switch ( self->Type )
{
case CT_Union:
@ -1287,7 +1287,7 @@ void union_to_strbuilder_fwd(CodeUnion self, StrBuilder* result )
StrBuilder using_to_strbuilder(CodeUsing self)
{
GEN_ASSERT(self);
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 128 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 128 );
switch ( self->Type )
{
case CT_Using:
@ -1352,7 +1352,7 @@ inline
StrBuilder var_to_strbuilder(CodeVar self)
{
GEN_ASSERT(self);
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 256 );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 256 );
var_to_strbuilder_ref( self, & result );
return result;
}

View File

@ -6,53 +6,6 @@
#pragma region Constants
#ifndef GEN_GLOBAL_BUCKET_SIZE
# define GEN_GLOBAL_BUCKET_SIZE megabytes(8)
#endif
#ifndef GEN_CODEPOOL_NUM_BLOCKS
# define GEN_CODEPOOL_NUM_BLOCKS kilobytes(16)
#endif
#ifndef GEN_SIZE_PER_STRING_ARENA
# define GEN_SIZE_PER_STRING_ARENA megabytes(1)
#endif
#ifndef GEN_MAX_COMMENT_LINE_LENGTH
# define GEN_MAX_COMMENT_LINE_LENGTH 1024
#endif
#ifndef GEN_MAX_NAME_LENGTH
# define GEN_MAX_NAME_LENGTH 128
#endif
#ifndef GEN_MAX_UNTYPED_STR_LENGTH
# define GEN_MAX_UNTYPED_STR_LENGTH megabytes(1)
#endif
#ifndef TokenMap_FixedArena
# define TokenMap_FixedArena FixedArena_8KB
#endif
#ifndef GEN_LEX_ALLOCATOR_SIZE
# define GEN_LEX_ALLOCATOR_SIZE megabytes(4)
#endif
#ifndef GEN_BUILDER_STR_BUFFER_RESERVE
# define GEN_BUILDER_STR_BUFFER_RESERVE megabytes(2)
#endif
// These constexprs are used for allocation behavior of data structures
// or string handling while constructing or serializing.
// Change them to suit your needs.
constexpr s32 InitSize_DataArrays = 16;
// NOTE: This limits the maximum size of an allocation
// If you are generating a string larger than this, increase the size of the bucket here.
constexpr usize Global_BucketSize = GEN_GLOBAL_BUCKET_SIZE;
constexpr s32 CodePool_NumBlocks = GEN_CODEPOOL_NUM_BLOCKS;
constexpr s32 SizePer_StringArena = GEN_SIZE_PER_STRING_ARENA;
constexpr s32 MaxCommentLineLength = GEN_MAX_COMMENT_LINE_LENGTH;
constexpr s32 MaxNameLength = GEN_MAX_NAME_LENGTH;
constexpr s32 MaxUntypedStrLength = GEN_MAX_UNTYPED_STR_LENGTH;
// constexpr s32 TokenFmt_TokenMap_MemSize = GEN_TOKEN_FMT_TOKEN_MAP_MEM_SIZE;
constexpr s32 LexAllocator_Size = GEN_LEX_ALLOCATOR_SIZE;
constexpr s32 Builder_StrBufferReserve = GEN_BUILDER_STR_BUFFER_RESERVE;
extern Str enum_underlying_sig;
extern Code access_public;
@ -111,6 +64,7 @@ extern CodeTypename t_typename;
#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
// Predefined typename codes. Are set to readonly and are setup during gen::init()
extern Context* _ctx;
extern CodeTypename t_b32;
@ -132,28 +86,3 @@ extern CodeTypename t_typename;
#endif
#pragma endregion Constants
// Used by the lexer to persistently treat all these identifiers as preprocessor defines.
// Populate with strings via gen::get_cached_string.
// Functional defines must have format: id( ;at minimum to indicate that the define is only valid with arguments.
extern Array(StringCached) PreprocessorDefines;
#ifdef GEN_EXPOSE_BACKEND
// Global allocator used for data with process lifetime.
extern AllocatorInfo GlobalAllocator;
extern Array(Arena) Global_AllocatorBuckets;
extern Array(Pool) CodePools;
extern Array(Arena) StringArenas;
extern StringTable StringCache;
extern Arena LexArena;
extern AllocatorInfo Allocator_DataArrays;
extern AllocatorInfo Allocator_CodePool;
extern AllocatorInfo Allocator_Lexer;
extern AllocatorInfo Allocator_StringArena;
extern AllocatorInfo Allocator_StringTable;
extern AllocatorInfo Allocator_TypeTable;
#endif

View File

@ -9,9 +9,11 @@ internal void parser_deinit();
GEN_NS_PARSER_END
internal
void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags )
void* fallback_allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags )
{
Arena* last = array_back(Global_AllocatorBuckets);
GEN_ASSERT(_ctx);
GEN_ASSERT(_ctx->Fallback_AllocatorBuckets);
Arena* last = array_back(_ctx->Fallback_AllocatorBuckets);
switch ( type )
{
@ -19,15 +21,15 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s
{
if ( ( last->TotalUsed + size ) > last->TotalSize )
{
Arena bucket = arena_init_from_allocator( heap(), Global_BucketSize );
Arena bucket = arena_init_from_allocator( heap(), _ctx->InitSize_Fallback_Allocator_Bucket_Size );
if ( bucket.PhysicalStart == nullptr )
GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets");
GEN_FATAL( "Failed to create bucket for Fallback_AllocatorBuckets");
if ( ! array_append( Global_AllocatorBuckets, bucket ) )
GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets");
if ( ! array_append( _ctx->Fallback_AllocatorBuckets, bucket ) )
GEN_FATAL( "Failed to append bucket to Fallback_AllocatorBuckets");
last = array_back(Global_AllocatorBuckets);
last = array_back(_ctx->Fallback_AllocatorBuckets);
}
return alloc_align( arena_allocator_info(last), size, alignment );
@ -46,15 +48,15 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s
{
if ( last->TotalUsed + size > last->TotalSize )
{
Arena bucket = arena_init_from_allocator( heap(), Global_BucketSize );
Arena bucket = arena_init_from_allocator( heap(), _ctx->InitSize_Fallback_Allocator_Bucket_Size );
if ( bucket.PhysicalStart == nullptr )
GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets");
GEN_FATAL( "Failed to create bucket for Fallback_AllocatorBuckets");
if ( ! array_append( Global_AllocatorBuckets, bucket ) )
GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets");
if ( ! array_append( _ctx->Fallback_AllocatorBuckets, bucket ) )
GEN_FATAL( "Failed to append bucket to Fallback_AllocatorBuckets");
last = array_back(Global_AllocatorBuckets);
last = array_back( _ctx->Fallback_AllocatorBuckets);
}
void* result = alloc_align( last->Backing, size, alignment );
@ -75,7 +77,7 @@ internal
void define_constants()
{
Code_Global = make_code();
Code_Global->Name = get_cached_string( txt("Global Code") );
Code_Global->Name = cache_str( txt("Global Code") );
Code_Global->Content = Code_Global->Name;
Code_Invalid = make_code();
@ -83,22 +85,22 @@ void define_constants()
t_empty = (CodeTypename) make_code();
t_empty->Type = CT_Typename;
t_empty->Name = get_cached_string( txt("") );
t_empty->Name = cache_str( txt("") );
code_set_global(cast(Code, t_empty));
access_private = make_code();
access_private->Type = CT_Access_Private;
access_private->Name = get_cached_string( txt("private:\n") );
access_private->Name = cache_str( txt("private:\n") );
code_set_global(cast(Code, access_private));
access_protected = make_code();
access_protected->Type = CT_Access_Protected;
access_protected->Name = get_cached_string( txt("protected:\n") );
access_protected->Name = cache_str( txt("protected:\n") );
code_set_global(access_protected);
access_public = make_code();
access_public->Type = CT_Access_Public;
access_public->Name = get_cached_string( txt("public:\n") );
access_public->Name = cache_str( txt("public:\n") );
code_set_global(access_public);
Str api_export_str = code(GEN_API_Export_Code);
@ -111,13 +113,13 @@ void define_constants()
module_global_fragment = make_code();
module_global_fragment->Type = CT_Untyped;
module_global_fragment->Name = get_cached_string( txt("module;") );
module_global_fragment->Name = cache_str( txt("module;") );
module_global_fragment->Content = module_global_fragment->Name;
code_set_global(cast(Code, module_global_fragment));
module_private_fragment = make_code();
module_private_fragment->Type = CT_Untyped;
module_private_fragment->Name = get_cached_string( txt("module : private;") );
module_private_fragment->Name = cache_str( txt("module : private;") );
module_private_fragment->Content = module_private_fragment->Name;
code_set_global(cast(Code, module_private_fragment));
@ -127,13 +129,13 @@ void define_constants()
pragma_once = (CodePragma) make_code();
pragma_once->Type = CT_Preprocess_Pragma;
pragma_once->Name = get_cached_string( txt("once") );
pragma_once->Name = cache_str( txt("once") );
pragma_once->Content = pragma_once->Name;
code_set_global((Code)pragma_once);
param_varadic = (CodeParams) make_code();
param_varadic->Type = CT_Parameters;
param_varadic->Name = get_cached_string( txt("...") );
param_varadic->Name = cache_str( txt("...") );
param_varadic->ValueType = t_empty;
code_set_global((Code)param_varadic);
@ -205,221 +207,249 @@ void define_constants()
if (enum_underlying_sig.Len == 0) {
enum_underlying_sig = txt("enum_underlying(");
}
array_append(PreprocessorDefines, enum_underlying_sig);
array_append( _ctx->PreprocessorDefines, enum_underlying_sig);
# undef def_constant_spec
}
void init()
void init(Context* ctx)
{
// Setup global allocator
AllocatorInfo fallback_allocator = { & fallback_allocator_proc, nullptr };
b32 using_fallback_allocator = false;
if (ctx->Allocator_DyanmicContainers.Proc == nullptr) {
ctx->Allocator_DyanmicContainers = fallback_allocator;
using_fallback_allocator = true;
}
if (ctx->Allocator_Pool.Proc == nullptr ) {
ctx->Allocator_Pool = fallback_allocator;
using_fallback_allocator = true;
}
if (ctx->Allocator_StrCache.Proc == nullptr) {
ctx->Allocator_StrCache = fallback_allocator;
using_fallback_allocator = true;
}
if (ctx->Allocator_Temp.Proc == nullptr) {
ctx->Allocator_Temp = fallback_allocator;
using_fallback_allocator = true;
}
// Setup fallback allocator
if (using_fallback_allocator)
{
AllocatorInfo becasue_C = { & Global_Allocator_Proc, nullptr };
GlobalAllocator = becasue_C;
Global_AllocatorBuckets = array_init_reserve(Arena, heap(), 128 );
if ( Global_AllocatorBuckets == nullptr )
GEN_FATAL( "Failed to reserve memory for Global_AllocatorBuckets");
Arena bucket = arena_init_from_allocator( heap(), Global_BucketSize );
ctx->Fallback_AllocatorBuckets = array_init_reserve(Arena, heap(), 128 );
if ( ctx->Fallback_AllocatorBuckets == nullptr )
GEN_FATAL( "Failed to reserve memory for Fallback_AllocatorBuckets");
Arena bucket = arena_init_from_allocator( heap(), ctx->InitSize_Fallback_Allocator_Bucket_Size );
if ( bucket.PhysicalStart == nullptr )
GEN_FATAL( "Failed to create first bucket for Global_AllocatorBuckets");
GEN_FATAL( "Failed to create first bucket for Fallback_AllocatorBuckets");
array_append( Global_AllocatorBuckets, bucket );
array_append( ctx->Fallback_AllocatorBuckets, bucket );
}
if (Allocator_DataArrays.Proc == nullptr) {
Allocator_DataArrays = GlobalAllocator;
if (ctx->Max_CommentLineLength == 0) {
ctx->Max_CommentLineLength = 1024;
}
if (Allocator_CodePool.Proc == nullptr ) {
Allocator_CodePool = GlobalAllocator;
if (ctx->Max_StrCacheLength == 0) {
ctx->Max_StrCacheLength = kilobytes(512);
}
if (Allocator_Lexer.Proc == nullptr) {
Allocator_Lexer = GlobalAllocator;
if (ctx->InitSize_BuilderBuffer == 0) {
ctx->InitSize_BuilderBuffer = megabytes(2);
}
if (Allocator_StringArena.Proc == nullptr) {
Allocator_StringArena = GlobalAllocator;
if (ctx->InitSize_CodePoolsArray == 0) {
ctx->InitSize_CodePoolsArray = 16;
}
if (Allocator_StringTable.Proc == nullptr) {
Allocator_StringTable = GlobalAllocator;
if (ctx->InitSize_StringArenasArray == 0) {
ctx->InitSize_StringArenasArray = 16;
}
if (Allocator_TypeTable.Proc == nullptr) {
Allocator_TypeTable = GlobalAllocator;
if (ctx->CodePool_NumBlocks == 0) {
ctx->CodePool_NumBlocks = kilobytes(16);
}
if (ctx->InitSize_LexArena == 0 ) {
ctx->InitSize_LexArena = megabytes(4);
}
if (ctx->SizePer_StringArena == 0) {
ctx->SizePer_StringArena = megabytes(1);
}
if (ctx->InitSize_Fallback_Allocator_Bucket_Size == 0) {
ctx->InitSize_Fallback_Allocator_Bucket_Size = megabytes(8);
}
// Override the current context (user has to put it back if unwanted).
_ctx = ctx;
// Setup the arrays
{
CodePools = array_init_reserve(Pool, Allocator_DataArrays, InitSize_DataArrays );
if ( CodePools == nullptr )
ctx->CodePools = array_init_reserve(Pool, ctx->Allocator_DyanmicContainers, ctx->InitSize_CodePoolsArray );
if ( ctx->CodePools == nullptr )
GEN_FATAL( "gen::init: Failed to initialize the CodePools array" );
StringArenas = array_init_reserve(Arena, Allocator_DataArrays, InitSize_DataArrays );
if ( StringArenas == nullptr )
ctx->StringArenas = array_init_reserve(Arena, ctx->Allocator_DyanmicContainers, ctx->InitSize_StringArenasArray );
if ( ctx->StringArenas == nullptr )
GEN_FATAL( "gen::init: Failed to initialize the StringArenas array" );
}
// Setup the code pool and code entries arena.
{
Pool code_pool = pool_init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) );
Pool code_pool = pool_init( ctx->Allocator_Pool, ctx->CodePool_NumBlocks, sizeof(AST) );
if ( code_pool.PhysicalStart == nullptr )
GEN_FATAL( "gen::init: Failed to initialize the code pool" );
array_append( ctx->CodePools, code_pool );
array_append( CodePools, code_pool );
LexArena = arena_init_from_allocator( Allocator_Lexer, LexAllocator_Size );
Arena strbuilder_arena = arena_init_from_allocator( Allocator_StringArena, SizePer_StringArena );
// TODO(Ed): This is going to be phased out most likely.
ctx->LexArena = arena_init_from_allocator( ctx->Allocator_DyanmicContainers, ctx->InitSize_LexArena );
// TODO(Ed): Eventually the string arenas needs to be phased out for a dedicated string slab allocator
Arena strbuilder_arena = arena_init_from_allocator( ctx->Allocator_StrCache, ctx->SizePer_StringArena );
if ( strbuilder_arena.PhysicalStart == nullptr )
GEN_FATAL( "gen::init: Failed to initialize the string arena" );
array_append( StringArenas, strbuilder_arena );
array_append( ctx->StringArenas, strbuilder_arena );
}
// Setup the hash tables
{
StringCache = hashtable_init(StringCached, Allocator_StringTable);
if ( StringCache.Entries == nullptr )
ctx->StrCache = hashtable_init(StrCached, ctx->Allocator_DyanmicContainers);
if ( ctx->StrCache.Entries == nullptr )
GEN_FATAL( "gen::init: Failed to initialize the StringCache");
}
// Preprocessor Defines
PreprocessorDefines = array_init_reserve(StringCached, GlobalAllocator, kilobytes(1) );
ctx->PreprocessorDefines = array_init_reserve(StrCached, ctx->Allocator_DyanmicContainers, kilobytes(1) );
define_constants();
GEN_NS_PARSER parser_init();
}
void deinit()
void deinit(Context* ctx)
{
usize index = 0;
usize left = array_num(CodePools);
usize left = array_num(ctx->CodePools);
do
{
Pool* code_pool = & CodePools[index];
Pool* code_pool = & ctx->CodePools[index];
pool_free(code_pool);
index++;
}
while ( left--, left );
index = 0;
left = array_num(StringArenas);
left = array_num(ctx->StringArenas);
do
{
Arena* strbuilder_arena = & StringArenas[index];
Arena* strbuilder_arena = & ctx->StringArenas[index];
arena_free(strbuilder_arena);
index++;
}
while ( left--, left );
hashtable_destroy(StringCache);
hashtable_destroy(ctx->StrCache);
array_free( CodePools);
array_free( StringArenas);
array_free( ctx->CodePools);
array_free( ctx->StringArenas);
arena_free(& LexArena);
arena_free(& ctx->LexArena);
array_free(PreprocessorDefines);
array_free(ctx->PreprocessorDefines);
index = 0;
left = array_num(Global_AllocatorBuckets);
left = array_num( ctx->Fallback_AllocatorBuckets);
do
{
Arena* bucket = & Global_AllocatorBuckets[ index ];
Arena* bucket = & ctx->Fallback_AllocatorBuckets[ index ];
arena_free(bucket);
index++;
}
while ( left--, left );
array_free(Global_AllocatorBuckets);
array_free( ctx->Fallback_AllocatorBuckets);
GEN_NS_PARSER parser_deinit();
if (_ctx == ctx)
_ctx = nullptr;
}
void reset()
void reset(Context* ctx)
{
s32 index = 0;
s32 left = array_num(CodePools);
s32 left = array_num(ctx->CodePools);
do
{
Pool* code_pool = & CodePools[index];
Pool* code_pool = & ctx->CodePools[index];
pool_clear(code_pool);
index++;
}
while ( left--, left );
index = 0;
left = array_num(StringArenas);
left = array_num(ctx->StringArenas);
do
{
Arena* strbuilder_arena = & StringArenas[index];
Arena* strbuilder_arena = & ctx->StringArenas[index];
strbuilder_arena->TotalUsed = 0;;
index++;
}
while ( left--, left );
hashtable_clear(StringCache);
hashtable_clear(ctx->StrCache);
define_constants();
}
AllocatorInfo get_strbuilder_allocator( s32 c_str_length )
void set_context(Context* new_ctx) {
GEN_ASSERT(new_ctx);
_ctx = new_ctx;
}
AllocatorInfo get_cached_str_allocator( s32 str_length )
{
Arena* last = array_back(StringArenas);
usize size_req = c_str_length + sizeof(StrBuilderHeader) + sizeof(char*);
Arena* last = array_back(_ctx->StringArenas);
usize size_req = str_length + sizeof(StrBuilderHeader) + sizeof(char*);
if ( last->TotalUsed + scast(ssize, size_req) > last->TotalSize )
{
Arena new_arena = arena_init_from_allocator( Allocator_StringArena, SizePer_StringArena );
Arena new_arena = arena_init_from_allocator( _ctx->Allocator_StrCache, _ctx->SizePer_StringArena );
if ( ! array_append( _ctx->StringArenas, new_arena ) )
GEN_FATAL( "gen::get_cached_str_allocator: Failed to allocate a new string arena" );
if ( ! array_append( StringArenas, new_arena ) )
GEN_FATAL( "gen::get_strbuilder_allocator: Failed to allocate a new string arena" );
last = array_back(StringArenas);
last = array_back( _ctx->StringArenas);
}
return arena_allocator_info(last);
}
// Will either make or retrive a code string.
StringCached get_cached_string( Str str )
StrCached cache_str( Str str )
{
s32 hash_length = str.Len > kilobytes(1) ? kilobytes(1) : str.Len;
u64 key = crc32( str.Ptr, hash_length );
{
StringCached* result = hashtable_get(StringCache, key );
if (str.Len > _ctx->Max_StrCacheLength) {
// Do not cache the string, just shove into the arena and and return it.
Str result = strbuilder_to_str( strbuilder_make_str( get_cached_str_allocator( str.Len ), str ));
return result;
}
u64 key = crc32( str.Ptr, str.Len ); {
StrCached* result = hashtable_get( _ctx->StrCache, key );
if ( result )
return * result;
}
Str result = strbuilder_to_str( strbuilder_make_str( get_strbuilder_allocator( str.Len ), str ));
hashtable_set(StringCache, key, result );
Str result = strbuilder_to_str( strbuilder_make_str( get_cached_str_allocator( str.Len ), str ));
hashtable_set( _ctx->StrCache, key, result );
return result;
}
// Used internally to retireve a Code object form the CodePool.
Code make_code()
{
Pool* allocator = array_back( CodePools);
Pool* allocator = array_back( _ctx->CodePools);
if ( allocator->FreeList == nullptr )
{
Pool code_pool = pool_init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) );
Pool code_pool = pool_init( _ctx->Allocator_Pool, _ctx->CodePool_NumBlocks, sizeof(AST) );
if ( code_pool.PhysicalStart == nullptr )
GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePool allcoator returned nullptr." );
if ( ! array_append( CodePools, code_pool ) )
if ( ! array_append( _ctx->CodePools, code_pool ) )
GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePools failed to append new pool." );
allocator = array_back( CodePools);
allocator = array_back( _ctx->CodePools);
}
Code result = { rcast( AST*, alloc( pool_allocator_info(allocator), sizeof(AST) )) };
@ -427,27 +457,10 @@ Code make_code()
return result;
}
void set_allocator_data_arrays( AllocatorInfo allocator )
{
Allocator_DataArrays = allocator;
void set_preprocess_define( Str id, b32 is_functional ) {
StrBuilder builder = strbuilder_make_str( _ctx->Allocator_Temp, id );
if (is_functional) {
strbuilder_append_char( & builder, '(' );
}
void set_allocator_code_pool( AllocatorInfo allocator )
{
Allocator_CodePool = allocator;
}
void set_allocator_lexer( AllocatorInfo allocator )
{
Allocator_Lexer = allocator;
}
void set_allocator_strbuilder_arena( AllocatorInfo allocator )
{
Allocator_StringArena = allocator;
}
void set_allocator_strbuilder_table( AllocatorInfo allocator )
{
Allocator_StringArena = allocator;
array_append( _ctx->PreprocessorDefines, cache_str(builder) );
}

View File

@ -15,20 +15,97 @@
\ \\ \ \\ \ \ \\ \ \ \ \
*/
#if 0
enum LogLevel : u32
{
Info,
Warning,
Panic,
};
struct LogEntry
{
Str msg;
u32 line_num;
void* data;
};
typedef void LoggerCallback(LogEntry entry);
#endif
// Note(Ed): This is subject to heavily change
// with upcoming changes to the library's fallback (default) allocations strategy;
// and major changes to lexer/parser context usage.
struct Context
{
// User Configuration
// Persistent Data Allocation
AllocatorInfo Allocator_DyanmicContainers; // By default will use a genral slab allocator (TODO(Ed): Currently does not)
AllocatorInfo Allocator_Pool; // By default will use the growing vmem reserve (TODO(Ed): Currently does not)
AllocatorInfo Allocator_StrCache; // By default will use a dedicated slab allocator (TODO(Ed): Currently does not)
// Temporary Allocation
AllocatorInfo Allocator_Temp;
// LoggerCallaback* log_callback; // TODO(Ed): Impl user logger callback as an option.
//
u32 Max_CommentLineLength; // Used by def_comment
u32 Max_StrCacheLength; // Any cached string longer than this is always allocated again.
u32 InitSize_BuilderBuffer;
u32 InitSize_CodePoolsArray;
u32 InitSize_StringArenasArray;
u32 CodePool_NumBlocks;
// TODO(Ed): Review these... (No longer needed if using the proper allocation strategy)
u32 InitSize_LexArena;
u32 SizePer_StringArena;
// Parser
// Used by the lexer to persistently treat all these identifiers as preprocessor defines.
// Populate with strings via gen::cache_str.
// Functional defines must have format: id( ;at minimum to indicate that the define is only valid with arguments.
Array(StrCached) PreprocessorDefines;
// Backend
// The fallback allocator is utilized if any fo the three above allocators is not specified by the user.
u32 InitSize_Fallback_Allocator_Bucket_Size;
Array(Arena) Fallback_AllocatorBuckets;
// Array(Token) LexerTokens;
Array(Pool) CodePools;
Array(Arena) StringArenas;
Arena LexArena;
StringTable StrCache;
};
// Initialize the library.
void init();
void init(Context* ctx);
// Currently manually free's the arenas, code for checking for leaks.
// However on Windows at least, it doesn't need to occur as the OS will clean up after the process.
void deinit();
void deinit(Context* ctx);
// Clears the allocations, but doesn't return to the heap, the calls init() again.
// Ease of use.
void reset();
void reset(Context* ctx);
void set_context(Context* ctx);
// Alternative way to add a preprocess define entry for the lexer & parser to utilize
// if the user doesn't want to use def_define
void set_preprocess_define( Str id, b32 is_functional );
// Used internally to retrive or make string allocations.
// Strings are stored in a series of string arenas of fixed size (SizePer_StringArena)
StringCached get_cached_string( Str str );
StrCached cache_str( Str str );
/*
This provides a fresh Code AST.
@ -39,13 +116,6 @@ Code make_code();
// Set these before calling gen's init() procedure.
void set_allocator_data_arrays ( AllocatorInfo data_array_allocator );
void set_allocator_code_pool ( AllocatorInfo pool_allocator );
void set_allocator_lexer ( AllocatorInfo lex_allocator );
void set_allocator_strbuilder_arena( AllocatorInfo strbuilder_allocator );
void set_allocator_strbuilder_table( AllocatorInfo strbuilder_allocator );
void set_allocator_type_table ( AllocatorInfo type_reg_allocator );
#pragma region Upfront
CodeAttributes def_attributes( Str content );

View File

@ -17,10 +17,10 @@ CodeClass parse_class( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
push_scope();
CodeClass result = (CodeClass) parse_class_struct( Tok_Decl_Class, parser_not_inplace_def );
parser_pop(& Context);
parser_pop(& parser_ctx);
return result;
}
@ -59,8 +59,8 @@ CodeConstructor parse_constructor( Str def )
break;
default :
log_failure( "Invalid specifier %s for variable\n%S", spec_to_str( spec ), parser_to_strbuilder(Context) );
parser_pop(& Context);
log_failure( "Invalid specifier %s for variable\n%S", spec_to_str( spec ), parser_to_strbuilder(parser_ctx) );
parser_pop(& parser_ctx);
return InvalidCode;
}
@ -79,7 +79,7 @@ CodeConstructor parse_constructor( Str def )
// <specifiers> ...
}
Context.Tokens = toks;
parser_ctx.Tokens = toks;
CodeConstructor result = parser_parse_constructor( specifiers );
return result;
}
@ -96,7 +96,7 @@ CodeDestructor parse_destructor( Str def )
// TODO(Ed): Destructors can have prefix attributes
// TODO(Ed): Destructors can have virtual
Context.Tokens = toks;
parser_ctx.Tokens = toks;
CodeDestructor result = parser_parse_destructor(NullCode);
return result;
}
@ -109,11 +109,11 @@ CodeEnum parse_enum( Str def )
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
{
parser_pop(& Context);
parser_pop(& parser_ctx);
return InvalidCode;
}
Context.Tokens = toks;
parser_ctx.Tokens = toks;
return parser_parse_enum( parser_not_inplace_def);
}
@ -126,7 +126,7 @@ CodeBody parse_export_body( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
return parser_parse_export_body();
}
@ -139,7 +139,7 @@ CodeExtern parse_extern_link( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
return parser_parse_extern_link();
}
@ -152,7 +152,7 @@ CodeFriend parse_friend( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
return parser_parse_friend();
}
@ -165,7 +165,7 @@ CodeFn parse_function( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
return (CodeFn) parser_parse_function();
}
@ -178,10 +178,10 @@ CodeBody parse_global_body( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
push_scope();
CodeBody result = parse_global_nspace( CT_Global_Body );
parser_pop(& Context);
parser_pop(& parser_ctx);
return result;
}
@ -194,7 +194,7 @@ CodeNS parse_namespace( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
return parser_parse_namespace();
}
@ -207,7 +207,7 @@ CodeOperator parse_operator( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
return (CodeOperator) parser_parse_operator();
}
@ -220,7 +220,7 @@ CodeOpCast parse_operator_cast( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
return parser_parse_operator_cast(NullCode);
}
@ -233,10 +233,10 @@ CodeStruct parse_struct( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
push_scope();
CodeStruct result = (CodeStruct) parse_class_struct( Tok_Decl_Struct, parser_not_inplace_def );
parser_pop(& Context);
parser_pop(& parser_ctx);
return result;
}
@ -249,7 +249,7 @@ CodeTemplate parse_template( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
return parser_parse_template();
}
@ -262,7 +262,7 @@ CodeTypename parse_type( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
return parser_parse_type( parser_not_from_template, nullptr);
}
@ -275,7 +275,7 @@ CodeTypedef parse_typedef( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
return parser_parse_typedef();
}
@ -288,7 +288,7 @@ CodeUnion parse_union( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
return parser_parse_union( parser_not_inplace_def);
}
@ -301,7 +301,7 @@ CodeUsing parse_using( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
return parser_parse_using();
}
@ -314,7 +314,7 @@ CodeVar parse_variable( Str def )
if ( toks.Arr == nullptr )
return InvalidCode;
Context.Tokens = toks;
parser_ctx.Tokens = toks;
return parser_parse_variable();
}

View File

@ -8,15 +8,12 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va )
char const* buf_begin = buf;
ssize remaining = buf_size;
local_persist
TokenMap_FixedArena tok_map_arena;
fixed_arena_init( & tok_map_arena);
local_persist
StringTable tok_map;
local_persist StringTable tok_map;
do_once() {
tok_map = hashtable_init(Str, _ctx->Allocator_DyanmicContainers );
}
// Populate token pairs
{
tok_map = hashtable_init(Str, fixed_arena_allocator_info(& tok_map_arena) );
s32 left = num_tokens - 1;
while ( left-- )
@ -90,12 +87,8 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va )
current = * fmt;
}
}
hashtable_clear(tok_map);
fixed_arena_free(& tok_map_arena);
ssize result = buf_size - remaining;
return result;
}
@ -109,7 +102,7 @@ Code untyped_str( Str content )
Code
result = make_code();
result->Name = get_cached_string( content );
result->Name = cache_str( content );
result->Type = CT_Untyped;
result->Content = result->Name;
@ -137,15 +130,12 @@ Code untyped_fmt( char const* fmt, ...)
va_start(va, fmt);
ssize length = c_str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va);
va_end(va);
Str buf_str = { fmt, c_str_len_capped(fmt, MaxNameLength) };
Str uncapped_str = { buf, length };
Str content = { buf, length };
Code
result = make_code();
result->Name = get_cached_string( buf_str );
result->Type = CT_Untyped;
result->Content = get_cached_string( uncapped_str );
result->Content = cache_str( content );
if ( result->Name.Len == 0 )
{
@ -176,9 +166,8 @@ Code untyped_token_fmt( s32 num_tokens, char const* fmt, ... )
Code
result = make_code();
result->Name = get_cached_string( buf_str );
result->Type = CT_Untyped;
result->Content = result->Name;
result->Content = cache_str( buf_str );
if ( result->Name.Len == 0 )
{

View File

@ -419,7 +419,7 @@ CodeAttributes def_attributes( Str content )
Code
result = make_code();
result->Type = CT_PlatformAttributes;
result->Name = get_cached_string( content );
result->Name = cache_str( content );
result->Content = result->Name;
return (CodeAttributes) result;
}
@ -433,9 +433,7 @@ CodeComment def_comment( Str content )
return InvalidCode;
}
static char line[ MaxCommentLineLength ];
StrBuilder cmt_formatted = strbuilder_make_reserve( GlobalAllocator, kilobytes(1) );
StrBuilder cmt_formatted = strbuilder_make_reserve( _ctx->Allocator_Temp, kilobytes(1) );
char const* end = content.Ptr + content.Len;
char const* scanner = content.Ptr;
s32 curr = 0;
@ -450,10 +448,7 @@ CodeComment def_comment( Str content )
}
length++;
c_str_copy( line, scanner, length );
strbuilder_append_fmt(& cmt_formatted, "//%.*s", length, line );
mem_set( line, 0, MaxCommentLineLength );
strbuilder_append_fmt(& cmt_formatted, "//%.*s", length, scanner );
scanner += length;
}
while ( scanner <= end );
@ -466,7 +461,7 @@ CodeComment def_comment( Str content )
Code
result = make_code();
result->Type = CT_Comment;
result->Name = get_cached_string( name );
result->Name = cache_str( name );
result->Content = result->Name;
strbuilder_free(& cmt_formatted);
@ -531,7 +526,7 @@ CodeClass def_class( Str name, Opts_def_struct p )
CodeClass
result = (CodeClass) make_code();
result->Name = get_cached_string( name );
result->Name = cache_str( name );
result->ModuleFlags = p.mflags;
if ( p.body )
{
@ -574,12 +569,12 @@ CodeDefine def_define( Str name, Str content, Opts_def_define p )
CodeDefine
result = (CodeDefine) make_code();
result->Type = CT_Preprocess_Define;
result->Name = get_cached_string( name );
result->Name = cache_str( name );
if ( content.Len <= 0 || content.Ptr == nullptr )
result->Content = get_cached_string( txt("") );
result->Content = cache_str( txt("") );
else
result->Content = get_cached_string( strbuilder_to_str(strbuilder_fmt_buf(GlobalAllocator, "%S\n", content)) );
result->Content = cache_str( strbuilder_to_str(strbuilder_fmt_buf(_ctx->Allocator_Temp, "%S\n", content)) );
b32 append_preprocess_defines = ! p.dont_append_preprocess_defines;
if ( append_preprocess_defines ) {
@ -590,7 +585,7 @@ CodeDefine def_define( Str name, Str content, Opts_def_define p )
break;
}
Str lex_id = { result->Name.Ptr, lex_id_len };
array_append(PreprocessorDefines, lex_id );
array_append(_ctx->PreprocessorDefines, cache_str(lex_id) );
}
return result;
}
@ -648,7 +643,7 @@ CodeEnum def_enum( Str name, Opts_def_enum p )
CodeEnum
result = (CodeEnum) make_code();
result->Name = get_cached_string( name );
result->Name = cache_str( name );
result->ModuleFlags = p.mflags;
if ( p.body )
{
@ -700,7 +695,7 @@ CodeExec def_execution( Str content )
CodeExec
result = (CodeExec) make_code();
result->Type = CT_Execution;
result->Content = get_cached_string( content );
result->Content = cache_str( content );
return result;
}
@ -718,7 +713,7 @@ CodeExtern def_extern_link( Str name, CodeBody body )
CodeExtern
result = (CodeExtern)make_code();
result->Type = CT_Extern_Linkage;
result->Name = get_cached_string( name );
result->Name = cache_str( name );
result->Body = body;
return result;
}
@ -781,7 +776,7 @@ CodeFn def_function( Str name, Opts_def_function p )
CodeFn
result = (CodeFn) make_code();
result->Name = get_cached_string( name );
result->Name = cache_str( name );
result->ModuleFlags = p.mflags;
if ( p.body )
{
@ -820,13 +815,13 @@ CodeInclude def_include( Str path, Opts_def_include p )
return InvalidCode;
}
StrBuilder content = p.foreign ?
strbuilder_fmt_buf( GlobalAllocator, "<%.*s>", path.Len, path.Ptr )
: strbuilder_fmt_buf( GlobalAllocator, "\"%.*s\"", path.Len, path.Ptr );
strbuilder_fmt_buf( _ctx->Allocator_Temp, "<%.*s>", path.Len, path.Ptr )
: strbuilder_fmt_buf( _ctx->Allocator_Temp, "\"%.*s\"", path.Len, path.Ptr );
CodeInclude
result = (CodeInclude) make_code();
result->Type = CT_Preprocess_Include;
result->Name = get_cached_string( strbuilder_to_str(content) );
result->Name = cache_str( strbuilder_to_str(content) );
result->Content = result->Name;
return result;
}
@ -840,7 +835,7 @@ CodeModule def_module( Str name, Opts_def_module p )
CodeModule
result = (CodeModule) make_code();
result->Type = CT_Module;
result->Name = get_cached_string( name );
result->Name = cache_str( name );
result->ModuleFlags = p.mflags;
return result;
}
@ -863,7 +858,7 @@ CodeNS def_namespace( Str name, CodeBody body, Opts_def_namespace p )
CodeNS
result = (CodeNS) make_code();
result->Type = CT_Namespace;
result->Name = get_cached_string( name );
result->Name = cache_str( name );
result->ModuleFlags = p.mflags;
result->Body = body;
return result;
@ -899,7 +894,7 @@ CodeOperator def_operator( Operator op, Str nspace, Opts_def_operator p )
CodeOperator
result = (CodeOperator) make_code();
result->Name = get_cached_string( name_resolved );
result->Name = cache_str( name_resolved );
result->ModuleFlags = p.mflags;
result->Op = op;
if ( p.body )
@ -986,7 +981,7 @@ CodeParams def_param( CodeTypename type, Str name, Opts_def_param p )
CodeParams
result = (CodeParams) make_code();
result->Type = CT_Parameters;
result->Name = get_cached_string( name );
result->Name = cache_str( name );
result->ValueType = type;
result->Value = p.value;
result->NumEntries++;
@ -1003,7 +998,7 @@ CodePragma def_pragma( Str directive )
CodePragma
result = (CodePragma) make_code();
result->Type = CT_Preprocess_Pragma;
result->Content = get_cached_string( directive );
result->Content = cache_str( directive );
return result;
}
@ -1016,7 +1011,7 @@ CodePreprocessCond def_preprocess_cond( EPreprocessCond type, Str expr )
}
CodePreprocessCond
result = (CodePreprocessCond) make_code();
result->Content = get_cached_string( expr );
result->Content = cache_str( expr );
switch (type)
{
case PreprocessCond_If:
@ -1066,7 +1061,7 @@ CodeStruct def_struct( Str name, Opts_def_struct p )
result = (CodeStruct) make_code();
result->ModuleFlags = p.mflags;
if ( name.Len )
result->Name = get_cached_string( name );
result->Name = cache_str( name );
if ( p.body ) {
result->Type = CT_Struct;
@ -1143,7 +1138,7 @@ CodeTypename def_type( Str name, Opts_def_type p )
}
CodeTypename
result = (CodeTypename) make_code();
result->Name = get_cached_string( name );
result->Name = cache_str( name );
result->Type = CT_Typename;
result->Attributes = p.attributes;
result->Specs = p.specifiers;
@ -1204,12 +1199,12 @@ CodeTypedef def_typedef( Str name, Code type, Opts_def_typedef p )
GEN_DEBUG_TRAP();
return InvalidCode;
}
result->Name = get_cached_string( type->Name );
result->Name = cache_str( type->Name );
result->IsFunction = true;
}
else
{
result->Name = get_cached_string( name );
result->Name = cache_str( name );
result->IsFunction = false;
}
return result;
@ -1238,7 +1233,7 @@ CodeUnion def_union( Str name, CodeBody body, Opts_def_union p )
result->Body = body;
result->Attributes = p.attributes;
if ( name.Ptr )
result->Name = get_cached_string( name );
result->Name = cache_str( name );
return result;
}
@ -1262,7 +1257,7 @@ CodeUsing def_using( Str name, CodeTypename type, Opts_def_using p )
}
CodeUsing
result = (CodeUsing) make_code();
result->Name = get_cached_string( name );
result->Name = cache_str( name );
result->ModuleFlags = p.mflags;
result->Type = CT_Using;
result->UnderlyingType = type;
@ -1278,7 +1273,7 @@ CodeUsing def_using_namespace( Str name )
}
CodeUsing
result = (CodeUsing) make_code();
result->Name = get_cached_string( name );
result->Name = cache_str( name );
result->Type = CT_Using_Namespace;
return result;
}
@ -1311,7 +1306,7 @@ CodeVar def_variable( CodeTypename type, Str name, Opts_def_variable p )
}
CodeVar
result = (CodeVar) make_code();
result->Name = get_cached_string( name );
result->Name = cache_str( name );
result->Type = CT_Variable;
result->ModuleFlags = p.mflags;
result->ValueType = type;

View File

@ -92,7 +92,7 @@ bool tok_is_end_definition(Token tok) {
StrBuilder tok_to_strbuilder(Token tok)
{
StrBuilder result = strbuilder_make_reserve( GlobalAllocator, kilobytes(4) );
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, kilobytes(4) );
Str type_str = toktype_to_str( tok.Type );
strbuilder_append_fmt( & result, "Line: %d Column: %d, Type: %.*s Content: %.*s"
@ -354,7 +354,7 @@ s32 lex_preprocessor_directive( LexContext* ctx )
if ( (* ctx->scanner) != '"' && (* ctx->scanner) != '<' )
{
StrBuilder directive_str = strbuilder_fmt_buf( GlobalAllocator, "%.*s", min( 80, ctx->left + preprocess_content.Text.Len ), ctx->token.Text.Ptr );
StrBuilder directive_str = strbuilder_fmt_buf( _ctx->Allocator_Temp, "%.*s", min( 80, ctx->left + preprocess_content.Text.Len ), ctx->token.Text.Ptr );
log_failure( "gen::Parser::lex: Expected '\"' or '<' after #include, not '%c' (%d, %d)\n%s"
, (* ctx->scanner)
@ -421,8 +421,8 @@ s32 lex_preprocessor_directive( LexContext* ctx )
}
else
{
StrBuilder directive_str = strbuilder_make_length( GlobalAllocator, ctx->token.Text.Ptr, ctx->token.Text.Len );
StrBuilder content_str = strbuilder_fmt_buf( GlobalAllocator, "%.*s", min( 400, ctx->left + preprocess_content.Text.Len ), preprocess_content.Text.Ptr );
StrBuilder directive_str = strbuilder_make_length( _ctx->Allocator_Temp, ctx->token.Text.Ptr, ctx->token.Text.Len );
StrBuilder content_str = strbuilder_fmt_buf( _ctx->Allocator_Temp, "%.*s", min( 400, ctx->left + preprocess_content.Text.Len ), preprocess_content.Text.Ptr );
log_failure( "gen::Parser::lex: Invalid escape sequence '\\%c' (%d, %d)"
" in preprocessor directive '%s' (%d, %d)\n%s"
@ -584,7 +584,7 @@ TokArray lex( Str content )
return null_array;
}
for ( StringCached* entry = array_begin(PreprocessorDefines); entry != array_end(PreprocessorDefines); entry = array_next(PreprocessorDefines, entry))
for ( StrCached* entry = array_begin(_ctx->PreprocessorDefines); entry != array_end(_ctx->PreprocessorDefines); entry = array_next(_ctx->PreprocessorDefines, entry))
{
s32 length = 0;
char const* entry_scanner = (*entry).Ptr;
@ -710,7 +710,7 @@ TokArray lex( Str content )
}
else
{
StrBuilder context_str = strbuilder_fmt_buf( GlobalAllocator, "%s", c.scanner, min( 100, c.left ) );
StrBuilder context_str = strbuilder_fmt_buf( _ctx->Allocator_Temp, "%s", c.scanner, min( 100, c.left ) );
log_failure( "gen::lex: invalid varadic argument, expected '...' got '..%c' (%d, %d)\n%s", (* ctx->scanner), c.line, c.column, context_str );
}
@ -1271,7 +1271,7 @@ TokArray lex( Str content )
);
}
StrBuilder context_str = strbuilder_fmt_buf( GlobalAllocator, "%.*s", min( 100, c.left ), c.scanner );
StrBuilder context_str = strbuilder_fmt_buf( _ctx->Allocator_Temp, "%.*s", min( 100, c.left ), c.scanner );
log_failure( "Failed to lex token '%c' (%d, %d)\n%s", (* ctx->scanner), c.line, c.column, context_str );
// Skip to next whitespace since we can't know if anything else is valid until then.

File diff suppressed because it is too large Load Diff

View File

@ -4,30 +4,9 @@
#endif
#pragma region StaticData
// TODO : Convert global allocation strategy to use a slab allocation strategy.
global AllocatorInfo GlobalAllocator;
global Array( Arena ) Global_AllocatorBuckets;
// TODO(Ed) : Make the code pool a dynamic arena
global Array( Pool ) CodePools = { nullptr };
global Array( Arena ) StringArenas = { nullptr };
global StringTable StringCache;
global Arena LexArena;
global AllocatorInfo Allocator_DataArrays = {0};
global AllocatorInfo Allocator_CodePool = {0};
global AllocatorInfo Allocator_Lexer = {0};
global AllocatorInfo Allocator_StringArena = {0};
global AllocatorInfo Allocator_StringTable = {0};
global AllocatorInfo Allocator_TypeTable = {0};
#pragma endregion StaticData
global Context* _ctx;
#pragma region Constants
global Str enum_underlying_sig;
global Code access_public;
@ -84,8 +63,6 @@ global CodeTypename t_wchar_t;
global CodeTypename t_class;
global CodeTypename t_typename;
global Array(StringCached) PreprocessorDefines;
#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
global CodeTypename t_b32;
@ -107,3 +84,5 @@ global CodeTypename t_f64;
#endif
#pragma endregion Constants
#pragma endregion StaticData

View File

@ -69,7 +69,8 @@
#endif
#ifndef do_once
#define do_once( statement ) for ( local_persist b32 once = true; once; once = false, (statement) )
#define do_once() for ( local_persist b32 once = true; once; once = false )
#define do_once_defer( statement ) for ( local_persist b32 once = true; once; once = false, (statement) )
#define do_once_start \
do \

View File

@ -737,8 +737,8 @@ Str str_visualize_whitespace(Str str, AllocatorInfo allocator)
// Represents strings cached with the string table.
// Should never be modified, if changed string is desired, cache_string( str ) another.
typedef Str StringCached;
typedef Str StrCached;
// Implements basic string interning. Data structure is based off the ZPL Hashtable.
typedef HashTable(StringCached) StringTable;
typedef HashTable(StrCached) StringTable;
#pragma endregion Strings

View File

@ -13,9 +13,9 @@ CodeBody gen_ecode( char const* path, bool use_c_definition = false )
AllocatorInfo scratch_info = fixed_arena_allocator_info(& scratch);
CSV_Columns2 csv_enum = parse_csv_two_columns( scratch_info, path );
StrBuilder enum_entries = strbuilder_make_reserve( GlobalAllocator, kilobytes(1) );
StrBuilder to_c_str_entries = strbuilder_make_reserve( GlobalAllocator, kilobytes(1) );
StrBuilder to_keyword_c_str_entries = strbuilder_make_reserve( GlobalAllocator, kilobytes(1) );
StrBuilder enum_entries = strbuilder_make_reserve( _ctx->Allocator_Temp, kilobytes(1) );
StrBuilder to_c_str_entries = strbuilder_make_reserve( _ctx->Allocator_Temp, kilobytes(1) );
StrBuilder to_keyword_c_str_entries = strbuilder_make_reserve( _ctx->Allocator_Temp, kilobytes(1) );
for ( ssize idx = 0; idx < array_num(csv_enum.Col_1); ++ idx ) {
char const* code = csv_enum.Col_1[idx].string;
@ -40,7 +40,7 @@ CodeBody gen_ecode( char const* path, bool use_c_definition = false )
#pragma push_macro("local_persist")
#undef local_persist
Str lookup_size = strbuilder_to_str(strbuilder_fmt_buf(GlobalAllocator, "%d", array_num(csv_enum.Col_1) ));
Str lookup_size = strbuilder_to_str(strbuilder_fmt_buf(_ctx->Allocator_Temp, "%d", array_num(csv_enum.Col_1) ));
CodeBody to_c_str_fns = parse_global_body( token_fmt(
"entries", strbuilder_to_str(to_c_str_entries)
, "keywords", strbuilder_to_str(to_keyword_c_str_entries)
@ -97,8 +97,8 @@ CodeBody gen_eoperator( char const* path, bool use_c_definition = false )
AllocatorInfo scratch_info = fixed_arena_allocator_info(& scratch);
CSV_Columns2 csv_enum = parse_csv_two_columns( scratch_info, path );
StrBuilder enum_entries = strbuilder_make_reserve( GlobalAllocator, 32 );
StrBuilder to_c_str_entries = strbuilder_make_reserve( GlobalAllocator, 32 );
StrBuilder enum_entries = strbuilder_make_reserve( _ctx->Allocator_Temp, 32 );
StrBuilder to_c_str_entries = strbuilder_make_reserve( _ctx->Allocator_Temp, 32 );
for (usize idx = 0; idx < array_num(csv_enum.Col_1); idx++) {
char const* enum_str = csv_enum.Col_1[idx].string;
@ -136,7 +136,7 @@ CodeBody gen_eoperator( char const* path, bool use_c_definition = false )
#pragma push_macro("local_persist")
#undef local_persist
Str lookup_size = strbuilder_to_str(strbuilder_fmt_buf(GlobalAllocator, "%d", array_num(csv_enum.Col_1) ));
Str lookup_size = strbuilder_to_str(strbuilder_fmt_buf(_ctx->Allocator_Temp, "%d", array_num(csv_enum.Col_1) ));
CodeFn to_str = parse_function(token_fmt(
"entries", strbuilder_to_str(to_c_str_entries)
, "num", lookup_size
@ -237,7 +237,7 @@ CodeBody gen_especifier( char const* path, bool use_c_definition = false )
#undef do_once_end
#undef forceinline
#undef neverinline
Str lookup_size = strbuilder_to_str(strbuilder_fmt_buf(GlobalAllocator, "%d", array_num(csv_enum.Col_1) ));
Str lookup_size = strbuilder_to_str(strbuilder_fmt_buf(_ctx->Allocator_Temp, "%d", array_num(csv_enum.Col_1) ));
CodeFn to_str = parse_function(token_fmt(
"entries", strbuilder_to_str(to_c_str_entries)
, "num", lookup_size

View File

@ -21,10 +21,10 @@ using namespace gen;
void clang_format_file( char const* path, char const* style_path )
{
GEN_ASSERT_NOT_NULL(path);
StrBuilder resolved_path = strbuilder_make_str(GlobalAllocator, to_str_from_c_str(path));
StrBuilder resolved_path = strbuilder_make_str(_ctx->Allocator_Temp, to_str_from_c_str(path));
StrBuilder style_arg;
if (style_path) {
style_arg = strbuilder_make_str(GlobalAllocator, txt("-style=file:"));
style_arg = strbuilder_make_str(_ctx->Allocator_Temp, txt("-style=file:"));
strbuilder_append_fmt( & style_arg, "%s ", style_path );
}
@ -32,7 +32,7 @@ void clang_format_file( char const* path, char const* style_path )
Str cf_format_inplace = txt("-i ");
Str cf_verbose = txt("-verbose ");
StrBuilder command = strbuilder_make_str( GlobalAllocator, clang_format );
StrBuilder command = strbuilder_make_str( _ctx->Allocator_Temp, clang_format );
strbuilder_append_str( & command, cf_format_inplace );
strbuilder_append_str( & command, cf_verbose );
strbuilder_append_string( & command, style_arg );
@ -48,7 +48,7 @@ void refactor_file( char const* path, char const* refactor_script )
GEN_ASSERT_NOT_NULL(path);
GEN_ASSERT_NOT_NULL(refactor_script);
StrBuilder command = strbuilder_make_str(GlobalAllocator, txt("refactor "));
StrBuilder command = strbuilder_make_str(_ctx->Allocator_Temp, txt("refactor "));
// strbuilder_append_str( & command, txt("-debug ") );
strbuilder_append_str( & command, txt("-num=1 ") );
strbuilder_append_fmt( & command, "-src=%s ", path );

View File

@ -25,7 +25,7 @@ These are containers representing a scope body of a definition that can be of th
Fields:
```cpp
StringCached Name;
StrCached Name;
Code Front;
Code Back;
Token* Tok;
@ -56,8 +56,8 @@ Represent standard or vendor specific C/C++ attributes.
Fields:
```cpp
StringCached Content;
StringCached Name;
StrCached Content;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -80,8 +80,8 @@ Stores a comment.
Fields:
```cpp
StringCached Content;
StringCached Name;
StrCached Content;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -109,7 +109,7 @@ CodeComment InlineCmt; // Only supported by forward declarations
CodeAttributes Attributes;
CodeType ParentType;
CodeBody Body;
StringCached Name;
StrCached Name;
CodeType Prev;
CodeType Next;
Token* Tok;
@ -143,7 +143,7 @@ CodeComment InlineCmt; // Only supported by forward declarations
Code InitializerList;
CodeParams Params;
Code Body;
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -178,8 +178,8 @@ Represents a preprocessor define
Fields:
```cpp
StringCached Content;
StringCached Name;
StrCached Content;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -201,7 +201,7 @@ Fields:
CodeComment InlineCmt;
CodeSpecifiers Specs;
Code Body;
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -242,7 +242,7 @@ Code Prev;
Code Next;
Token* Tok;
Code Parent;
StringCached Name;
StrCached Name;
CodeT Type;
ModuleFlag ModuleFlags;
```
@ -271,8 +271,8 @@ Will be obsolute when function body parsing is implemented.
Fields:
```cpp
StringCached Content;
StringCached Name;
StrCached Content;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -292,7 +292,7 @@ Fields:
```cpp
CodeBody Body;
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -314,8 +314,8 @@ extern "<Name>"
Fields:
```cpp
StringCached Content;
StringCached Name;
StrCached Content;
StrCached Name;
Code Prev;
Code Next;
Code Parent;
@ -338,7 +338,7 @@ Fields:
```cpp
CodeComment InlineCmt;
Code Declaration;
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -363,7 +363,7 @@ CodeSpecifiers Specs;
CodeType ReturnType;
CodeParams Params;
CodeBody Body;
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -390,7 +390,7 @@ Serialization:
Fields:
```cpp
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -411,7 +411,7 @@ Fields:
```cpp
CodeBody Body;
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -440,7 +440,7 @@ CodeSpecifiers Specs;
CodeType ReturnType;
CodeParams Params;
CodeBody Body;
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -472,7 +472,7 @@ CodeComment InlineCmt;
CodeSpecifiers Specs;
CodeType ValueType;
CodeBody Body;
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -502,7 +502,7 @@ CodeType ValueType;
Code Macro;
Code Value;
Code PostNameMacro;
StringCached Name;
StrCached Name;
CodeParams Last;
CodeParams Next;
Token* Tok;
@ -524,8 +524,8 @@ Serialization:
Fields:
```cpp
StringCached Content;
StringCached Name;
StrCached Content;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -544,8 +544,8 @@ Serialization:
Fields:
```cpp
StringCached Content;
StringCached Name;
StrCached Content;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -566,7 +566,7 @@ Fields:
```cpp
SpecifierT ArrSpecs[ AST_ArrSpecs_Cap ];
CodeSpecifiers NextSpecs;
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -588,7 +588,7 @@ Fields:
```cpp
CodeParams Params;
Code Declaration;
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -621,7 +621,7 @@ Code Prev;
Code Next;
Token* Tok;
Code Parent;
StringCached Name;
StrCached Name;
CodeT Type;
b32 IsParamPack;
ETypenameTag TypeTag;
@ -649,7 +649,7 @@ Fields:
```cpp
CodeComment InlineCmt;
Code UnderlyingType;
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok
@ -682,7 +682,7 @@ Fields:
```cpp
CodeAttributes Attributes;
CodeBody Body;
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -708,7 +708,7 @@ Fields:
CodeComment InlineCmt;
CodeAttributes Attributes;
CodeType UnderlyingType;
StringCached Name;
StrCached Name;
Code Prev;
Code Next;
Token* Tok;
@ -740,7 +740,7 @@ CodeSpecifiers Specs;
CodeType ValueType;
Code BitfieldSize;
Code Value;
StringCached Name;
StrCached Name;
CodeVar NextVar;
Code Prev;
Code Next;

View File

@ -63,7 +63,7 @@ int AST_ArrSpecs_Cap =
(
AST_POD_Size
- sizeof(Code)
- sizeof(StringCached)
- sizeof(StrCached)
- sizeof(Code) * 2
- sizeof(Token*)
- sizeof(Code)
@ -79,7 +79,7 @@ int AST_ArrSpecs_Cap =
Data Notes:
* The allocator definitions used are exposed to the user incase they want to dictate memory usage
* You'll find the memory handling in `init`, `deinit`, `reset`, `gen_strbuilder_allocator`, `get_cached_string`, `make_code`.
* You'll find the memory handling in `init`, `deinit`, `reset`, `gen_strbuilder_allocator`, `cache_str`, `make_code`.
* Allocators are defined with the `AllocatorInfo` structure found in [`memory.hpp`](../base/dependencies/memory.hpp)
* Most of the work is just defining the allocation procedure:
@ -93,7 +93,7 @@ Data Notes:
* Cached Strings are stored in their own set of arenas. AST constructors use cached strings for names, and content.
* `StringArenas`, `StringCache`, `Allocator_StringArena`, and `Allocator_StringTable` are the associated containers or allocators.
* Strings used for serialization and file buffers are not contained by those used for cached strings.
* They are currently using `GlobalAllocator`, which are tracked array of arenas that grows as needed (adds buckets when one runs out).
* They are currently using `FallbackAllocator`, which are tracked array of arenas that grows as needed (adds buckets when one runs out).
* Memory within the buckets is not reused, so its inherently wasteful.
* I will be augmenting the default allocator with virtual memory & a slab allocator in the [future](https://github.com/Ed94/gencpp/issues/12)
* Intrusive linked lists used children nodes on bodies, and parameters.

View File

@ -589,7 +589,7 @@ do \
}
// Used to track which functions need generic selectors.
Array(CodeFn) code_c_interface = array_init_reserve<CodeFn>(GlobalAllocator, 16);
Array(CodeFn) code_c_interface = array_init_reserve<CodeFn>(FallbackAllocator, 16);
CodeBody parsed_ast = parse_file( path_base "components/ast.hpp" );
CodeBody ast = def_body(CT_Global_Body);
@ -647,9 +647,9 @@ do \
{
Str old_prefix = txt("code_");
Str actual_name = { fn->Name.Ptr + old_prefix.Len, fn->Name.Len - old_prefix.Len };
StrBuilder new_name = StrBuilder::fmt_buf(GlobalAllocator, "code__%S", actual_name );
StrBuilder new_name = StrBuilder::fmt_buf(FallbackAllocator, "code__%S", actual_name );
fn->Name = get_cached_string(new_name);
fn->Name = cache_str(new_name);
code_c_interface.append(fn);
}
ast.append(entry);
@ -702,7 +702,7 @@ R"(#define AST_ArrSpecs_Cap \
( \
AST_POD_Size \
- sizeof(Code) \
- sizeof(StringCached) \
- sizeof(StrCached) \
- sizeof(Code) * 2 \
- sizeof(Token*) \
- sizeof(Code) \
@ -791,17 +791,17 @@ R"(#define AST_ArrSpecs_Cap \
default: gen_generic_selection (Fail case) \
) GEN_RESOLVED_FUNCTION_CALL( code, ... ) \
*/
StrBuilder generic_selector = StrBuilder::make_reserve(GlobalAllocator, kilobytes(2));
StrBuilder generic_selector = StrBuilder::make_reserve(FallbackAllocator, kilobytes(2));
for ( CodeFn fn : code_c_interface )
{
generic_selector.clear();
Str private_prefix = txt("code__");
Str actual_name = { fn->Name.Ptr + private_prefix.Len, fn->Name.Len - private_prefix.Len };
StrBuilder interface_name = StrBuilder::fmt_buf(GlobalAllocator, "code_%S", actual_name );
StrBuilder interface_name = StrBuilder::fmt_buf(FallbackAllocator, "code_%S", actual_name );
// Resolve generic's arguments
b32 has_args = fn->Params->NumEntries > 1;
StrBuilder params_str = StrBuilder::make_reserve(GlobalAllocator, 32);
StrBuilder params_str = StrBuilder::make_reserve(FallbackAllocator, 32);
for (CodeParams param = fn->Params->Next; param != fn->Params.end(); ++ param) {
// We skip the first parameter as its always going to be the code for selection
if (param->Next == nullptr) {
@ -931,8 +931,8 @@ R"(#define <interface_name>( code ) _Generic( (code), \
if (prev && prev->Name.is_equal(entry->Name)) {
// rename second definition so there isn't a symbol conflict
StrBuilder postfix_arr = StrBuilder::fmt_buf(GlobalAllocator, "%S_arr", entry->Name);
entry->Name = get_cached_string(postfix_arr.to_str());
StrBuilder postfix_arr = StrBuilder::fmt_buf(FallbackAllocator, "%S_arr", entry->Name);
entry->Name = cache_str(postfix_arr.to_str());
postfix_arr.free();
}
@ -942,11 +942,11 @@ R"(#define <interface_name>( code ) _Generic( (code), \
// Convert the definition to use a default struct: https://vxtwitter.com/vkrajacic/status/1749816169736073295
Str prefix = txt("def_");
Str actual_name = { fn->Name.Ptr + prefix.Len, fn->Name.Len - prefix.Len };
Str new_name = StrBuilder::fmt_buf(GlobalAllocator, "def__%S", actual_name ).to_str();
Str new_name = StrBuilder::fmt_buf(FallbackAllocator, "def__%S", actual_name ).to_str();
// Resolve define's arguments
b32 has_args = fn->Params->NumEntries > 1;
StrBuilder params_str = StrBuilder::make_reserve(GlobalAllocator, 32);
StrBuilder params_str = StrBuilder::make_reserve(FallbackAllocator, 32);
for (CodeParams other_param = fn->Params; other_param != opt_param; ++ other_param) {
if ( other_param == opt_param ) {
params_str.append_fmt( "%S", other_param->Name );
@ -970,7 +970,7 @@ R"(#define <interface_name>( code ) _Generic( (code), \
, tmpl_fn_macro
));
fn->Name = get_cached_string(new_name);
fn->Name = cache_str(new_name);
interface.append(fn);
interface.append(fn_macro);
if (entry->Next && entry->Next->Type != CT_NewLine) {
@ -1021,9 +1021,9 @@ R"(#define <interface_name>( code ) _Generic( (code), \
{
Str old_prefix = txt("code_");
Str actual_name = { fn->Name.Ptr + old_prefix.Len, fn->Name.Len - old_prefix.Len };
StrBuilder new_name = StrBuilder::fmt_buf(GlobalAllocator, "code__%S", actual_name );
StrBuilder new_name = StrBuilder::fmt_buf(FallbackAllocator, "code__%S", actual_name );
fn->Name = get_cached_string(new_name);
fn->Name = cache_str(new_name);
}
inlines.append(entry);
}
@ -1176,9 +1176,9 @@ R"(#define <interface_name>( code ) _Generic( (code), \
{
Str old_prefix = txt("code_");
Str actual_name = { fn->Name.Ptr + old_prefix.Len, fn->Name.Len - old_prefix.Len };
StrBuilder new_name = StrBuilder::fmt_buf(GlobalAllocator, "code__%S", actual_name );
StrBuilder new_name = StrBuilder::fmt_buf(FallbackAllocator, "code__%S", actual_name );
fn->Name = get_cached_string(new_name);
fn->Name = cache_str(new_name);
}
src_ast.append(entry);
}
@ -1219,8 +1219,8 @@ R"(#define <interface_name>( code ) _Generic( (code), \
)
{
// rename second definition so there isn't a symbol conflict
StrBuilder postfix_arr = StrBuilder::fmt_buf(GlobalAllocator, "%S_arr", fn->Name);
fn->Name = get_cached_string(postfix_arr.to_str());
StrBuilder postfix_arr = StrBuilder::fmt_buf(FallbackAllocator, "%S_arr", fn->Name);
fn->Name = cache_str(postfix_arr.to_str());
postfix_arr.free();
}
@ -1228,9 +1228,9 @@ R"(#define <interface_name>( code ) _Generic( (code), \
{
Str prefix = txt("def_");
Str actual_name = { fn->Name.Ptr + prefix.Len, fn->Name.Len - prefix.Len };
Str new_name = StrBuilder::fmt_buf(GlobalAllocator, "def__%S", actual_name ).to_str();
Str new_name = StrBuilder::fmt_buf(FallbackAllocator, "def__%S", actual_name ).to_str();
fn->Name = get_cached_string(new_name);
fn->Name = cache_str(new_name);
}
src_upfront.append(fn);

View File

@ -224,7 +224,7 @@ word StrBuilder, gen_StrBuilder
namespace strbuilder_, gen_strbuilder_
word StringCached, gen_StringCached
word StrCached, gen_StringCached
word StringTable, gen_StringTable
@ -401,7 +401,7 @@ word init, gen_init
word deinit, gen_deinit
word reset, gen_reset
word get_cached_string, gen_get_cached_string
word cache_str, gen_get_cached_string
word make_code, gen_make_code
@ -450,8 +450,8 @@ word PreprocessorDefines, gen_PreprocessorDefines
// Backend
word GlobalAllocator, gen_GlobalAllocator
word Global_AllocatorBuckets, gen_Global_AllocatorBuckets
word FallbackAllocator, gen_GlobalAllocator
word Fallback_AllocatorBuckets, gen_Global_AllocatorBuckets
word CodePools, gen_CodePools
word StringArenas, gen_StringArenas
word StringCache, gen_StringCache
@ -518,7 +518,7 @@ word _adt_get_field, gen__adt_get_field
word _csv_write_record, gen__csv_write_record
word _csv_write_header, gen__csv_write_header
word Global_Allocator_Proc, gen_Global_Allocator_Proc
word fallback_allocator_proc, gen_Global_Allocator_Proc
word define_constants, gen_define_constants
word operator__validate, gen_operator__validate

View File

@ -42,8 +42,8 @@ CodeBody gen_array_base()
CodeBody gen_array( Str type, Str array_name )
{
StrBuilder array_type = StrBuilder::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr );
StrBuilder fn = StrBuilder::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr );
StrBuilder array_type = StrBuilder::fmt_buf( FallbackAllocator, "%.*s", array_name.Len, array_name.Ptr );
StrBuilder fn = StrBuilder::fmt_buf( FallbackAllocator, "%.*s", array_name.Len, array_name.Ptr );
// c_str_to_lower(fn.Data);
#pragma push_macro( "GEN_ASSERT" )
@ -375,7 +375,7 @@ CodeBody gen_array( Str type, Str array_name )
#pragma pop_macro( "forceinline" )
++ Array_DefinitionCounter;
Str slot_str = StrBuilder::fmt_buf(GlobalAllocator, "%d", Array_DefinitionCounter).to_str();
Str slot_str = StrBuilder::fmt_buf(FallbackAllocator, "%d", Array_DefinitionCounter).to_str();
Code generic_interface_slot = untyped_str(token_fmt( "type", type, "array_type", (Str)array_type, "slot", (Str)slot_str,
R"(#define GENERIC_SLOT_<slot>__array_init <type>, <array_type>_init
@ -399,13 +399,13 @@ R"(#define GENERIC_SLOT_<slot>__array_init <type>, <array_type>_i
));
return def_global_body( args(
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( GlobalAllocator, "region %SB", array_type ))),
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( FallbackAllocator, "region %SB", array_type ))),
fmt_newline,
generic_interface_slot,
fmt_newline,
result,
fmt_newline,
def_pragma( strbuilder_to_str(strbuilder_fmt_buf( GlobalAllocator, "endregion %SB", array_type ))),
def_pragma( strbuilder_to_str(strbuilder_fmt_buf( FallbackAllocator, "endregion %SB", array_type ))),
fmt_newline
));
};

View File

@ -30,16 +30,16 @@ R"(#define HashTable(_type) struct _type
CodeBody gen_hashtable( Str type, Str hashtable_name )
{
StrBuilder tbl_type = {(char*) hashtable_name.duplicate(GlobalAllocator).Ptr};
StrBuilder fn = tbl_type.duplicate(GlobalAllocator);
StrBuilder tbl_type = {(char*) hashtable_name.duplicate(FallbackAllocator).Ptr};
StrBuilder fn = tbl_type.duplicate(FallbackAllocator);
// c_str_to_lower(fn.Data);
StrBuilder name_lower = StrBuilder::make( GlobalAllocator, hashtable_name );
StrBuilder name_lower = StrBuilder::make( FallbackAllocator, hashtable_name );
// c_str_to_lower( name_lower.Data );
StrBuilder hashtable_entry = StrBuilder::fmt_buf( GlobalAllocator, "HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
StrBuilder entry_array_name = StrBuilder::fmt_buf( GlobalAllocator, "Arr_HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
StrBuilder entry_array_fn_ns = StrBuilder::fmt_buf( GlobalAllocator, "arr_hte_%.*s", name_lower.length(), name_lower.Data );
StrBuilder hashtable_entry = StrBuilder::fmt_buf( FallbackAllocator, "HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
StrBuilder entry_array_name = StrBuilder::fmt_buf( FallbackAllocator, "Arr_HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
StrBuilder entry_array_fn_ns = StrBuilder::fmt_buf( FallbackAllocator, "arr_hte_%.*s", name_lower.length(), name_lower.Data );
CodeBody hashtable_types = parse_global_body( token_fmt(
"type", (Str) type,
@ -372,7 +372,7 @@ CodeBody gen_hashtable( Str type, Str hashtable_name )
#pragma pop_macro( "forceinline" )
++ HashTable_DefinitionCounter;
Str slot_str = StrBuilder::fmt_buf(GlobalAllocator, "%d", HashTable_DefinitionCounter).to_str();
Str slot_str = StrBuilder::fmt_buf(FallbackAllocator, "%d", HashTable_DefinitionCounter).to_str();
Code generic_interface_slot = untyped_str(token_fmt( "type", type, "tbl_type", (Str)tbl_type, "slot", (Str)slot_str,
R"(#define GENERIC_SLOT_<slot>__hashtable_init <type>, <tbl_type>_init
@ -400,7 +400,7 @@ R"(#define GENERIC_SLOT_<slot>__hashtable_init <type>, <tbl_type>_
, type.Len, type.Ptr );
return def_global_body(args(
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( GlobalAllocator, "region %SB", tbl_type ))),
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( FallbackAllocator, "region %SB", tbl_type ))),
fmt_newline,
generic_interface_slot,
fmt_newline,
@ -409,7 +409,7 @@ R"(#define GENERIC_SLOT_<slot>__hashtable_init <type>, <tbl_type>_
entry_array,
hashtable_def,
fmt_newline,
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( GlobalAllocator, "endregion %SB", tbl_type ))),
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( FallbackAllocator, "endregion %SB", tbl_type ))),
fmt_newline
));
}

View File

@ -84,7 +84,7 @@ Code gen_generic_selection_function_macro( s32 num_slots, Str macro_name, Generi
) GEN_RESOLVED_FUNCTION_CALL( selector_arg )
*/
local_persist
StrBuilder define_builder = StrBuilder::make_reserve(GlobalAllocator, kilobytes(64));
StrBuilder define_builder = StrBuilder::make_reserve(FallbackAllocator, kilobytes(64));
define_builder.clear();
Str macro_begin;
@ -104,7 +104,7 @@ R"(#define <macro_name>(selector_arg, ...) _Generic( (selector_arg), \
for ( s32 slot = 1; slot <= num_slots; ++ slot )
{
Str slot_str = StrBuilder::fmt_buf(GlobalAllocator, "%d", slot).to_str();
Str slot_str = StrBuilder::fmt_buf(FallbackAllocator, "%d", slot).to_str();
define_builder.append( token_fmt( "macro_name", macro_name, "slot", slot_str,
R"(GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_<slot>__<macro_name> ) \
)"
@ -147,9 +147,9 @@ CodeFn rename_function_to_unique_symbol(CodeFn fn, Str optional_prefix = txt("")
// Add prefix if provided
if (optional_prefix.Len)
new_name = strbuilder_fmt_buf(GlobalAllocator, "%S_%S_", optional_prefix, old_name);
new_name = strbuilder_fmt_buf(FallbackAllocator, "%S_%S_", optional_prefix, old_name);
else
new_name = strbuilder_fmt_buf(GlobalAllocator, "%S_", old_name);
new_name = strbuilder_fmt_buf(FallbackAllocator, "%S_", old_name);
// Add return type to the signature
if (fn->ReturnType)
@ -211,8 +211,8 @@ bool swap_pragma_region_implementation( Str region_name, SwapContentProc* swap_c
bool found = false;
CodePragma possible_region = cast(CodePragma, entry_iter);
StrBuilder region_sig = strbuilder_fmt_buf(GlobalAllocator, "region %s", region_name.Ptr);
StrBuilder endregion_sig = strbuilder_fmt_buf(GlobalAllocator, "endregion %s", region_name.Ptr);
StrBuilder region_sig = strbuilder_fmt_buf(FallbackAllocator, "region %s", region_name.Ptr);
StrBuilder endregion_sig = strbuilder_fmt_buf(FallbackAllocator, "endregion %s", region_name.Ptr);
if ( possible_region->Content.contains(region_sig))
{
found = true;

View File

@ -32,7 +32,10 @@ Code format( Code code ) {
int gen_main()
{
gen::init();
Context ctx {
};
gen::init(& ctx);
Code push_ignores = scan_file( (path_base "helpers/push_ignores.inline.hpp") );
Code pop_ignores = scan_file( (path_base "helpers/pop_ignores.inline.hpp") );
@ -282,6 +285,6 @@ int gen_main()
builder_write( & src);
}
gen::deinit();
gen::deinit( & ctx);
return 0;
}

View File

@ -67,7 +67,7 @@ int gen_main()
{
CodeBody macros = def_body( CT_Global_Body );
{
FileContents content = file_read_contents( GlobalAllocator, true, path_base "dependencies/macros.hpp" );
FileContents content = file_read_contents( FallbackAllocator, true, path_base "dependencies/macros.hpp" );
CodeBody ori_macros = parse_global_body( Str { (char const*)content.data, content.size });
for (Code code = ori_macros.begin();