2023-07-30 15:55:57 -07:00
|
|
|
internal void init_parser();
|
|
|
|
internal void deinit_parser();
|
|
|
|
|
2023-07-24 14:45:27 -07:00
|
|
|
internal
|
|
|
|
void* Global_Allocator_Proc( void* allocator_data, AllocType type, sw size, sw alignment, void* old_memory, sw old_size, u64 flags )
|
|
|
|
{
|
|
|
|
Arena* last = & Global_AllocatorBuckets.back();
|
|
|
|
|
|
|
|
switch ( type )
|
|
|
|
{
|
|
|
|
case EAllocation_ALLOC:
|
|
|
|
{
|
|
|
|
if ( ( last->TotalUsed + size ) > last->TotalSize )
|
|
|
|
{
|
|
|
|
Arena bucket = Arena::init_from_allocator( heap(), Global_BucketSize );
|
|
|
|
|
|
|
|
if ( bucket.PhysicalStart == nullptr )
|
2023-08-09 15:47:59 -07:00
|
|
|
GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets");
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
if ( ! Global_AllocatorBuckets.append( bucket ) )
|
2023-08-09 15:47:59 -07:00
|
|
|
GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets");
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
last = & Global_AllocatorBuckets.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
return alloc_align( * last, size, alignment );
|
|
|
|
}
|
|
|
|
case EAllocation_FREE:
|
|
|
|
{
|
|
|
|
// Doesn't recycle.
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EAllocation_FREE_ALL:
|
|
|
|
{
|
|
|
|
// Memory::cleanup instead.
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EAllocation_RESIZE:
|
|
|
|
{
|
|
|
|
if ( last->TotalUsed + size > last->TotalSize )
|
|
|
|
{
|
|
|
|
Arena bucket = Arena::init_from_allocator( heap(), Global_BucketSize );
|
|
|
|
|
|
|
|
if ( bucket.PhysicalStart == nullptr )
|
2023-08-09 15:47:59 -07:00
|
|
|
GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets");
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
if ( ! Global_AllocatorBuckets.append( bucket ) )
|
2023-08-09 15:47:59 -07:00
|
|
|
GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets");
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
last = & Global_AllocatorBuckets.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void* result = alloc_align( last->Backing, size, alignment );
|
|
|
|
|
|
|
|
if ( result != nullptr && old_memory != nullptr )
|
|
|
|
{
|
|
|
|
mem_copy( result, old_memory, old_size );
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-24 17:59:20 -07:00
|
|
|
void define_constants()
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
|
|
|
Code::Global = make_code();
|
2023-08-09 15:47:59 -07:00
|
|
|
Code::Global->Name = get_cached_string( txt("Global Code") );
|
2023-07-24 14:45:27 -07:00
|
|
|
Code::Global->Content = Code::Global->Name;
|
|
|
|
|
|
|
|
Code::Invalid = make_code();
|
|
|
|
Code::Invalid.set_global();
|
|
|
|
|
2023-08-01 13:07:47 -07:00
|
|
|
t_empty = (CodeType) make_code();
|
|
|
|
t_empty->Type = ECode::Typename;
|
2023-08-09 15:47:59 -07:00
|
|
|
t_empty->Name = get_cached_string( txt("") );
|
2023-08-01 13:07:47 -07:00
|
|
|
t_empty.set_global();
|
|
|
|
|
2023-07-29 22:21:04 -07:00
|
|
|
access_private = make_code();
|
|
|
|
access_private->Type = ECode::Access_Private;
|
2023-08-09 15:47:59 -07:00
|
|
|
access_private->Name = get_cached_string( txt("private:") );
|
2023-07-29 22:21:04 -07:00
|
|
|
access_private.set_global();
|
|
|
|
|
|
|
|
access_protected = make_code();
|
|
|
|
access_protected->Type = ECode::Access_Protected;
|
2023-08-09 15:47:59 -07:00
|
|
|
access_protected->Name = get_cached_string( txt("protected:") );
|
2023-07-29 22:21:04 -07:00
|
|
|
access_protected.set_global();
|
|
|
|
|
|
|
|
access_public = make_code();
|
|
|
|
access_public->Type = ECode::Access_Public;
|
2023-08-09 15:47:59 -07:00
|
|
|
access_public->Name = get_cached_string( txt("public:") );
|
2023-07-29 22:21:04 -07:00
|
|
|
access_public.set_global();
|
|
|
|
|
|
|
|
attrib_api_export = def_attributes( code(GEN_API_Export_Code));
|
|
|
|
attrib_api_export.set_global();
|
|
|
|
|
|
|
|
attrib_api_import = def_attributes( code(GEN_API_Import_Code));
|
|
|
|
attrib_api_import.set_global();
|
|
|
|
|
|
|
|
module_global_fragment = make_code();
|
|
|
|
module_global_fragment->Type = ECode::Untyped;
|
2023-08-09 15:47:59 -07:00
|
|
|
module_global_fragment->Name = get_cached_string( txt("module;") );
|
2023-07-29 22:21:04 -07:00
|
|
|
module_global_fragment->Content = module_global_fragment->Name;
|
|
|
|
module_global_fragment.set_global();
|
|
|
|
|
|
|
|
module_private_fragment = make_code();
|
|
|
|
module_private_fragment->Type = ECode::Untyped;
|
2023-08-09 15:47:59 -07:00
|
|
|
module_private_fragment->Name = get_cached_string( txt("module : private;") );
|
2023-07-29 22:21:04 -07:00
|
|
|
module_private_fragment->Content = module_private_fragment->Name;
|
|
|
|
module_private_fragment.set_global();
|
|
|
|
|
2023-08-04 13:12:13 -07:00
|
|
|
fmt_newline = make_code();
|
|
|
|
fmt_newline->Type = ECode::NewLine;
|
|
|
|
fmt_newline.set_global();
|
|
|
|
|
2023-07-29 22:21:04 -07:00
|
|
|
pragma_once = (CodePragma) make_code();
|
2023-08-09 06:50:12 -07:00
|
|
|
pragma_once->Type = ECode::Preprocess_Pragma;
|
2023-08-09 15:47:59 -07:00
|
|
|
pragma_once->Name = get_cached_string( txt("once") );
|
2023-07-29 22:21:04 -07:00
|
|
|
pragma_once->Content = pragma_once->Name;
|
|
|
|
pragma_once.set_global();
|
|
|
|
|
|
|
|
param_varadic = (CodeType) make_code();
|
|
|
|
param_varadic->Type = ECode::Parameters;
|
2023-08-09 15:47:59 -07:00
|
|
|
param_varadic->Name = get_cached_string( txt("...") );
|
2023-07-29 22:21:04 -07:00
|
|
|
param_varadic->ValueType = t_empty;
|
|
|
|
param_varadic.set_global();
|
|
|
|
|
|
|
|
preprocess_else = (CodePreprocessCond) make_code();
|
|
|
|
preprocess_else->Type = ECode::Preprocess_Else;
|
|
|
|
preprocess_else.set_global();
|
|
|
|
|
|
|
|
preprocess_endif = (CodePreprocessCond) make_code();
|
|
|
|
preprocess_endif->Type = ECode::Preprocess_EndIf;
|
|
|
|
preprocess_endif.set_global();
|
|
|
|
|
2023-07-24 17:59:20 -07:00
|
|
|
# define def_constant_code_type( Type_ ) \
|
|
|
|
t_##Type_ = def_type( name(Type_) ); \
|
|
|
|
t_##Type_.set_global();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
def_constant_code_type( auto );
|
|
|
|
def_constant_code_type( void );
|
|
|
|
def_constant_code_type( int );
|
|
|
|
def_constant_code_type( bool );
|
|
|
|
def_constant_code_type( char );
|
|
|
|
def_constant_code_type( wchar_t );
|
|
|
|
def_constant_code_type( class );
|
|
|
|
def_constant_code_type( typename );
|
|
|
|
|
|
|
|
#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
|
|
|
|
t_b32 = def_type( name(b32) );
|
|
|
|
|
|
|
|
def_constant_code_type( s8 );
|
|
|
|
def_constant_code_type( s16 );
|
|
|
|
def_constant_code_type( s32 );
|
|
|
|
def_constant_code_type( s64 );
|
|
|
|
|
|
|
|
def_constant_code_type( u8 );
|
|
|
|
def_constant_code_type( u16 );
|
|
|
|
def_constant_code_type( u32 );
|
|
|
|
def_constant_code_type( u64 );
|
|
|
|
|
|
|
|
def_constant_code_type( sw );
|
|
|
|
def_constant_code_type( uw );
|
|
|
|
|
|
|
|
def_constant_code_type( f32 );
|
|
|
|
def_constant_code_type( f64 );
|
|
|
|
#endif
|
|
|
|
# undef def_constant_code_type
|
|
|
|
|
|
|
|
# pragma push_macro( "global" )
|
|
|
|
# pragma push_macro( "internal" )
|
|
|
|
# pragma push_macro( "local_persist" )
|
2023-08-02 09:39:35 -07:00
|
|
|
# pragma push_macro( "neverinline" )
|
2023-07-24 14:45:27 -07:00
|
|
|
# undef global
|
|
|
|
# undef internal
|
|
|
|
# undef local_persist
|
2023-08-02 09:39:35 -07:00
|
|
|
# undef neverinline
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2023-07-24 17:59:20 -07:00
|
|
|
# define def_constant_spec( Type_, ... ) \
|
|
|
|
spec_##Type_ = def_specifiers( num_args(__VA_ARGS__), __VA_ARGS__); \
|
|
|
|
spec_##Type_.set_global();
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
def_constant_spec( const, ESpecifier::Const );
|
|
|
|
def_constant_spec( consteval, ESpecifier::Consteval );
|
|
|
|
def_constant_spec( constexpr, ESpecifier::Constexpr );
|
|
|
|
def_constant_spec( constinit, ESpecifier::Constinit );
|
|
|
|
def_constant_spec( extern_linkage, ESpecifier::External_Linkage );
|
|
|
|
def_constant_spec( final, ESpecifier::Final );
|
|
|
|
def_constant_spec( global, ESpecifier::Global );
|
|
|
|
def_constant_spec( inline, ESpecifier::Inline );
|
|
|
|
def_constant_spec( internal_linkage, ESpecifier::Internal_Linkage );
|
|
|
|
def_constant_spec( local_persist, ESpecifier::Local_Persist );
|
|
|
|
def_constant_spec( mutable, ESpecifier::Mutable );
|
2023-08-02 09:39:35 -07:00
|
|
|
def_constant_spec( neverinline, ESpecifier::NeverInline );
|
2023-07-24 14:45:27 -07:00
|
|
|
def_constant_spec( override, ESpecifier::Override );
|
|
|
|
def_constant_spec( ptr, ESpecifier::Ptr );
|
2023-08-07 00:10:45 -07:00
|
|
|
def_constant_spec( pure, ESpecifier::Pure )
|
2023-07-24 14:45:27 -07:00
|
|
|
def_constant_spec( ref, ESpecifier::Ref );
|
|
|
|
def_constant_spec( register, ESpecifier::Register );
|
|
|
|
def_constant_spec( rvalue, ESpecifier::RValue );
|
|
|
|
def_constant_spec( static_member, ESpecifier::Static );
|
|
|
|
def_constant_spec( thread_local, ESpecifier::Thread_Local );
|
|
|
|
def_constant_spec( virtual, ESpecifier::Virtual );
|
|
|
|
def_constant_spec( volatile, ESpecifier::Volatile)
|
|
|
|
|
2023-07-24 17:59:20 -07:00
|
|
|
spec_local_persist = def_specifiers( 1, ESpecifier::Local_Persist );
|
2023-07-24 14:45:27 -07:00
|
|
|
spec_local_persist.set_global();
|
|
|
|
|
|
|
|
# pragma pop_macro( "global" )
|
|
|
|
# pragma pop_macro( "internal" )
|
|
|
|
# pragma pop_macro( "local_persist" )
|
2023-08-02 09:39:35 -07:00
|
|
|
# pragma pop_macro( "neverinline" )
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
# undef def_constant_spec
|
|
|
|
}
|
|
|
|
|
|
|
|
void init()
|
|
|
|
{
|
|
|
|
// Setup global allocator
|
|
|
|
{
|
|
|
|
GlobalAllocator = AllocatorInfo { & Global_Allocator_Proc, nullptr };
|
|
|
|
|
|
|
|
Global_AllocatorBuckets = Array<Arena>::init_reserve( heap(), 128 );
|
|
|
|
|
|
|
|
if ( Global_AllocatorBuckets == nullptr )
|
2023-08-09 15:47:59 -07:00
|
|
|
GEN_FATAL( "Failed to reserve memory for Global_AllocatorBuckets");
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
Arena bucket = Arena::init_from_allocator( heap(), Global_BucketSize );
|
|
|
|
|
|
|
|
if ( bucket.PhysicalStart == nullptr )
|
2023-08-09 15:47:59 -07:00
|
|
|
GEN_FATAL( "Failed to create first bucket for Global_AllocatorBuckets");
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
Global_AllocatorBuckets.append( bucket );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the arrays
|
|
|
|
{
|
|
|
|
CodePools = Array<Pool>::init_reserve( Allocator_DataArrays, InitSize_DataArrays );
|
|
|
|
|
|
|
|
if ( CodePools == nullptr )
|
2023-08-09 15:47:59 -07:00
|
|
|
GEN_FATAL( "gen::init: Failed to initialize the CodePools array" );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
StringArenas = Array<Arena>::init_reserve( Allocator_DataArrays, InitSize_DataArrays );
|
|
|
|
|
|
|
|
if ( StringArenas == nullptr )
|
2023-08-09 15:47:59 -07:00
|
|
|
GEN_FATAL( "gen::init: Failed to initialize the StringArenas array" );
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the code pool and code entries arena.
|
|
|
|
{
|
|
|
|
Pool code_pool = Pool::init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) );
|
|
|
|
|
|
|
|
if ( code_pool.PhysicalStart == nullptr )
|
2023-08-09 15:47:59 -07:00
|
|
|
GEN_FATAL( "gen::init: Failed to initialize the code pool" );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
CodePools.append( code_pool );
|
|
|
|
|
|
|
|
LexArena = Arena::init_from_allocator( Allocator_Lexer, LexAllocator_Size );
|
|
|
|
|
|
|
|
Arena string_arena = Arena::init_from_allocator( Allocator_StringArena, SizePer_StringArena );
|
|
|
|
|
|
|
|
if ( string_arena.PhysicalStart == nullptr )
|
2023-08-09 15:47:59 -07:00
|
|
|
GEN_FATAL( "gen::init: Failed to initialize the string arena" );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
StringArenas.append( string_arena );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the hash tables
|
|
|
|
{
|
|
|
|
StringCache = StringTable::init( Allocator_StringTable );
|
|
|
|
|
|
|
|
if ( StringCache.Entries == nullptr )
|
2023-08-09 15:47:59 -07:00
|
|
|
GEN_FATAL( "gen::init: Failed to initialize the StringCache");
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
define_constants();
|
2023-07-30 15:55:57 -07:00
|
|
|
init_parser();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void deinit()
|
|
|
|
{
|
|
|
|
uw index = 0;
|
|
|
|
uw left = CodePools.num();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Pool* code_pool = & CodePools[index];
|
|
|
|
code_pool->free();
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
while ( left--, left );
|
|
|
|
|
|
|
|
index = 0;
|
|
|
|
left = StringArenas.num();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Arena* string_arena = & StringArenas[index];
|
|
|
|
string_arena->free();
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
while ( left--, left );
|
|
|
|
|
|
|
|
StringCache.destroy();
|
|
|
|
|
|
|
|
CodePools.free();
|
|
|
|
StringArenas.free();
|
|
|
|
|
|
|
|
LexArena.free();
|
|
|
|
|
|
|
|
index = 0;
|
|
|
|
left = Global_AllocatorBuckets.num();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Arena* bucket = & Global_AllocatorBuckets[ index ];
|
|
|
|
bucket->free();
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
while ( left--, left );
|
|
|
|
|
|
|
|
Global_AllocatorBuckets.free();
|
2023-07-30 15:55:57 -07:00
|
|
|
deinit_parser();
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
s32 index = 0;
|
|
|
|
s32 left = CodePools.num();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Pool* code_pool = & CodePools[index];
|
|
|
|
code_pool->clear();
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
while ( left--, left );
|
|
|
|
|
|
|
|
index = 0;
|
|
|
|
left = StringArenas.num();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Arena* string_arena = & StringArenas[index];
|
|
|
|
string_arena->TotalUsed = 0;;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
while ( left--, left );
|
|
|
|
|
|
|
|
StringCache.clear();
|
|
|
|
|
|
|
|
define_constants();
|
|
|
|
}
|
|
|
|
|
|
|
|
AllocatorInfo get_string_allocator( s32 str_length )
|
|
|
|
{
|
|
|
|
Arena* last = & StringArenas.back();
|
|
|
|
|
|
|
|
uw size_req = str_length + sizeof(String::Header) + sizeof(char*);
|
|
|
|
|
|
|
|
if ( last->TotalUsed + size_req > last->TotalSize )
|
|
|
|
{
|
|
|
|
Arena new_arena = Arena::init_from_allocator( Allocator_StringArena, SizePer_StringArena );
|
|
|
|
|
|
|
|
if ( ! StringArenas.append( new_arena ) )
|
2023-08-09 15:47:59 -07:00
|
|
|
GEN_FATAL( "gen::get_string_allocator: Failed to allocate a new string arena" );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
last = & StringArenas.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
return * last;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Will either make or retrive a code string.
|
|
|
|
StringCached get_cached_string( StrC str )
|
|
|
|
{
|
|
|
|
s32 hash_length = str.Len > kilobytes(1) ? kilobytes(1) : str.Len;
|
|
|
|
u64 key = crc32( str.Ptr, hash_length );
|
|
|
|
{
|
|
|
|
StringCached* result = StringCache.get( key );
|
|
|
|
|
|
|
|
if ( result )
|
|
|
|
return * result;
|
|
|
|
}
|
|
|
|
|
|
|
|
String result = String::make( get_string_allocator( str.Len ), str );
|
|
|
|
StringCache.set( key, result );
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-03 20:18:33 -07:00
|
|
|
// Used internally to retireve a Code object form the CodePool.
|
2023-07-24 14:45:27 -07:00
|
|
|
Code make_code()
|
|
|
|
{
|
|
|
|
Pool* allocator = & CodePools.back();
|
|
|
|
if ( allocator->FreeList == nullptr )
|
|
|
|
{
|
|
|
|
Pool code_pool = Pool::init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) );
|
|
|
|
|
|
|
|
if ( code_pool.PhysicalStart == nullptr )
|
2023-08-09 15:47:59 -07:00
|
|
|
GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePool allcoator returned nullptr." );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
if ( ! CodePools.append( code_pool ) )
|
2023-08-09 15:47:59 -07:00
|
|
|
GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePools failed to append new pool." );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
allocator = & CodePools.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
Code result { rcast( AST*, alloc( * allocator, sizeof(AST) )) };
|
|
|
|
|
|
|
|
result->Content = { nullptr };
|
|
|
|
result->Prev = { nullptr };
|
|
|
|
result->Next = { nullptr };
|
|
|
|
result->Parent = { nullptr };
|
|
|
|
result->Name = { nullptr };
|
|
|
|
result->Type = ECode::Invalid;
|
|
|
|
result->ModuleFlags = ModuleFlag::Invalid;
|
|
|
|
result->NumEntries = 0;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_allocator_data_arrays( AllocatorInfo allocator )
|
|
|
|
{
|
|
|
|
Allocator_DataArrays = allocator;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_allocator_code_pool( AllocatorInfo allocator )
|
|
|
|
{
|
|
|
|
Allocator_CodePool = allocator;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_allocator_lexer( AllocatorInfo allocator )
|
|
|
|
{
|
|
|
|
Allocator_Lexer = allocator;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_allocator_string_arena( AllocatorInfo allocator )
|
|
|
|
{
|
|
|
|
Allocator_StringArena = allocator;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_allocator_string_table( AllocatorInfo allocator )
|
|
|
|
{
|
|
|
|
Allocator_StringArena = allocator;
|
|
|
|
}
|
2023-08-03 08:01:43 -07:00
|
|
|
|