2023-07-18 21:13:12 -07:00
|
|
|
#if GEN_TIME
|
2023-07-15 23:26:55 -07:00
|
|
|
#define GEN_FEATURE_PARSING
|
|
|
|
#define GEN_DEFINE_LIBRARY_CODE_CONSTANTS
|
|
|
|
#define GEN_ENFORCE_STRONG_CODE_TYPES
|
2023-07-17 20:40:28 -07:00
|
|
|
#define GEN_EXPOSE_BACKEND
|
|
|
|
#define GEN_BENCHMARK
|
2023-07-11 15:29:45 -07:00
|
|
|
#include "gen.hpp"
|
|
|
|
using namespace gen;
|
|
|
|
|
2023-07-13 20:01:20 -07:00
|
|
|
Code gen_SOA( CodeStruct struct_def, s32 num_entries = 0 )
|
2023-07-11 15:29:45 -07:00
|
|
|
{
|
2023-07-12 12:59:47 -07:00
|
|
|
StringCached name = get_cached_string( token_fmt( "name", (StrC)struct_def->Name,
|
|
|
|
stringize( SOA_<name> )
|
|
|
|
));
|
2023-07-11 15:29:45 -07:00
|
|
|
|
|
|
|
Code
|
2023-07-15 23:26:55 -07:00
|
|
|
soa_entry = { struct_def.duplicate() };
|
2023-07-11 15:29:45 -07:00
|
|
|
soa_entry->Name = get_cached_string( name(Entry) );
|
|
|
|
|
2023-07-12 12:59:47 -07:00
|
|
|
constexpr s32 Num_Vars_Cap = 128;
|
2023-07-11 15:29:45 -07:00
|
|
|
|
2023-07-12 12:59:47 -07:00
|
|
|
local_persist Code var_memory[Num_Vars_Cap];
|
|
|
|
local_persist Arena var_arena;
|
|
|
|
do_once_start
|
|
|
|
var_arena = Arena::init_from_memory( var_memory, kilobytes(Num_Vars_Cap) );
|
|
|
|
do_once_end
|
|
|
|
|
2023-07-13 20:01:20 -07:00
|
|
|
Array<CodeVar> vars = Array<CodeVar>::init( var_arena );;
|
2023-07-12 12:59:47 -07:00
|
|
|
|
2023-07-15 20:07:30 -07:00
|
|
|
CodeStruct soa = def_struct( name, def_struct_body( args( soa_entry ) ));
|
2023-07-11 15:29:45 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
for ( Code struct_mem : struct_def->Body )
|
2023-07-11 15:29:45 -07:00
|
|
|
{
|
|
|
|
if ( struct_mem->Type == ECode::Variable )
|
|
|
|
{
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeType var_type = struct_mem.cast<CodeVar>()->ValueType;
|
2023-07-13 20:01:20 -07:00
|
|
|
StrC num_entries_str = to_StrC( str_fmt_buf( "%d", num_entries ) );
|
2023-07-12 12:59:47 -07:00
|
|
|
|
2023-07-13 20:01:20 -07:00
|
|
|
CodeVar entry_arr = { nullptr };
|
2023-07-12 12:59:47 -07:00
|
|
|
if ( ! num_entries)
|
2023-07-11 15:29:45 -07:00
|
|
|
{
|
2023-07-11 22:33:11 -07:00
|
|
|
entry_arr = parse_variable( token_fmt( "type", (StrC)var_type->Name, "name", (StrC)struct_mem->Name,
|
|
|
|
stringize( Array<<type>> <name>; )
|
|
|
|
));
|
2023-07-11 15:29:45 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-12 12:59:47 -07:00
|
|
|
entry_arr = parse_variable( token_fmt( "type", (StrC)var_type->Name, "name", (StrC)struct_mem->Name, "num", num_entries_str,
|
|
|
|
stringize( <type> <name>[<num>]; )
|
2023-07-11 22:33:11 -07:00
|
|
|
));
|
2023-07-11 15:29:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
vars.append( entry_arr );
|
2023-07-15 12:20:55 -07:00
|
|
|
soa->Body.append( entry_arr );
|
2023-07-11 15:29:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 20:01:20 -07:00
|
|
|
CodeFn make;
|
2023-07-11 15:29:45 -07:00
|
|
|
{
|
2023-07-12 12:59:47 -07:00
|
|
|
make = parse_function( token_fmt("SOA_Type", (StrC)name,
|
2023-07-11 22:33:11 -07:00
|
|
|
stringize(
|
|
|
|
static
|
|
|
|
<SOA_Type> make( AllocatorInfo allocator )
|
|
|
|
{
|
|
|
|
<SOA_Type> soa = {};
|
|
|
|
}
|
|
|
|
)
|
2023-07-11 15:29:45 -07:00
|
|
|
));
|
|
|
|
|
2023-07-12 12:59:47 -07:00
|
|
|
if ( ! num_entries )
|
2023-07-11 15:29:45 -07:00
|
|
|
{
|
2023-07-13 20:01:20 -07:00
|
|
|
for ( CodeVar member : vars )
|
2023-07-11 15:29:45 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
Code arr_init = def_execution( token_fmt( "var_name", (StrC)member->Name, "var_type", (StrC)member->ValueType->Name,
|
2023-07-11 22:33:11 -07:00
|
|
|
stringize( soa.<var_name> = <var_type>::init( allocator ); )
|
2023-07-11 15:29:45 -07:00
|
|
|
));
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
make->Body.append( arr_init );
|
2023-07-11 15:29:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
make->Body.append( def_execution( code( return soa; ) ));
|
2023-07-11 15:29:45 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 20:01:20 -07:00
|
|
|
CodeFn get;
|
2023-07-11 15:29:45 -07:00
|
|
|
{
|
|
|
|
get = parse_function( code(
|
|
|
|
Entry get( s32 idx )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
2023-07-17 20:40:28 -07:00
|
|
|
String content = String::make( GlobalAllocator, "return\n{\n" );
|
2023-07-11 15:29:45 -07:00
|
|
|
|
2023-07-13 20:01:20 -07:00
|
|
|
for ( CodeVar member : vars )
|
2023-07-11 15:29:45 -07:00
|
|
|
{
|
2023-07-11 22:33:11 -07:00
|
|
|
content.append_fmt( token_fmt( "var_name", (StrC)member->Name,
|
|
|
|
"<var_name>[idx],"
|
2023-07-11 15:29:45 -07:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
content.append( "};" );
|
|
|
|
|
2023-07-13 20:01:20 -07:00
|
|
|
CodeExec ret = def_execution( content );
|
2023-07-11 15:29:45 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
get->Body.append( ret );
|
2023-07-11 15:29:45 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
soa->Body.append( make );
|
|
|
|
soa->Body.append( get );
|
|
|
|
soa->Body.raw()->validate_body();
|
2023-07-12 12:59:47 -07:00
|
|
|
vars.free();
|
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
return soa;
|
|
|
|
}
|
2023-07-17 20:40:28 -07:00
|
|
|
|
|
|
|
void check_SOA()
|
|
|
|
{
|
|
|
|
gen::init();
|
|
|
|
Builder soa_test; soa_test.open( "SOA.gen.hpp" );
|
|
|
|
|
|
|
|
soa_test.print( parse_using( code(
|
|
|
|
using u16 = unsigned short;
|
|
|
|
)));
|
|
|
|
soa_test.print( def_include( txt_StrC("gen.hpp")));
|
|
|
|
soa_test.print( def_using_namespace( name(gen) ) );
|
|
|
|
|
|
|
|
soa_test.print( gen_SOA(
|
|
|
|
parse_struct( code(
|
|
|
|
struct TestStruct
|
|
|
|
{
|
|
|
|
u8 A;
|
|
|
|
u16 B;
|
|
|
|
u32 C;
|
|
|
|
u64 D;
|
|
|
|
};
|
|
|
|
))
|
|
|
|
));
|
|
|
|
|
|
|
|
soa_test.write();
|
|
|
|
gen::deinit();
|
|
|
|
}
|
2023-07-11 15:29:45 -07:00
|
|
|
#endif
|