WIP(broken): Converting base library to use c-linkage symbols only

This commit is contained in:
2024-12-04 11:01:53 -05:00
parent 6081834687
commit 3a55af9ce4
31 changed files with 1283 additions and 1062 deletions

View File

@ -127,8 +127,25 @@ int gen_main()
header.print( basic_types );
header.print( debug );
Array(StrC) to_rename = array_init_reserve(StrC, GlobalAllocator, 128);
to_rename.append(txt("align_forward"));
to_rename.append(txt("pointer_add"));
to_rename.append(txt("allocator_info"));
to_rename.append(txt("size_remaining"));
to_rename.append(txt("free"));
// to_rename.append(txt("clear"));
// to_rename.append(txt("init_sub"));
// to_rename.append(txt("check"));
NeedsSelectors needs_selectors;
needs_selectors.table = hashtable_init_reserve(Array(CodeFn), GlobalAllocator, 1024);
for ( StrC id : to_rename ) {
needs_selectors.set(id, array_init_reserve(CodeFn, GlobalAllocator, 128));
}
CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" );
CodeBody memory = def_body(CT_Global_Body);
for ( Code entry = parsed_memory.begin(); entry != parsed_memory.end(); ++ entry )
{
switch (entry->Type)
@ -145,11 +162,10 @@ int gen_main()
case CT_Function_Fwd:
{
CodeFn fn = cast(CodeFn, entry);
if ( fn->Name.is_equal(txt("free")) )
{
fn->Name = get_cached_string(txt("gen_free_ptr"));
}
memory.append(entry);
// for ( StrC id : to_rename ) if (fn->Name.is_equal(id)) {
// rename_function_to_unique_symbol(fn);
// }
memory.append(fn);
}
break;
case CT_Function:
@ -160,11 +176,11 @@ int gen_main()
log_fmt("Found constexpr: %S\n", entry.to_string());
fn->Specs.append(Spec_Inline);
}
if ( fn->Name.is_equal(txt("free")) )
{
fn->Name = get_cached_string(txt("gen_free_ptr"));
}
memory.append(entry);
// for ( StrC id : to_rename ) if (fn->Name.is_equal(id)) {
// Array(CodeFn) list = * needs_selectors.get(id);
// list.append(rename_function_to_unique_symbol(fn));
// }
memory.append(fn);
}
break;
case CT_Template:
@ -213,6 +229,9 @@ int gen_main()
b32 found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, parsed_memory );
if (found) break;
found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_REFERENCES"), entry, parsed_memory );
if (found) break;
memory.append(entry);
}
break;
@ -226,6 +245,12 @@ int gen_main()
break;
case CT_Preprocess_Pragma:
{
CodePragma pragma = cast(CodePragma, entry);
// if (pragma->Content.starts_with(txt("region Memory"))) {
// memory.append(generic_test);
// break;
// }
b32 found = swap_pragma_region_implementation( txt("FixedArena"), gen_fixed_arenas, entry, memory);
if (found) break;
@ -238,10 +263,15 @@ int gen_main()
break;
}
}
// CodeBody selectors = generate_generic_selectors(needs_selectors);
// memory.append_at(selectors, 0)
// memory.append(fmt_newline);
header.print( dump_to_scratch_and_retireve(memory) );
Code string_ops = scan_file( project_dir "dependencies/string_ops.hpp" );
header.print( string_ops );
// header.print( string_ops );
CodeBody printing_parsed = parse_file( project_dir "dependencies/printing.hpp" );
CodeBody printing = def_body(CT_Global_Body);
@ -273,7 +303,7 @@ int gen_main()
break;
}
}
header.print(dump_to_scratch_and_retireve(printing));
// header.print(dump_to_scratch_and_retireve(printing));
CodeBody containers = def_body(CT_Global_Body);
{
@ -285,10 +315,10 @@ int gen_main()
containers.append( def_pragma(code(endregion Containers)));
}
header.print(fmt_newline);
header.print(dump_to_scratch_and_retireve(containers));
// header.print(dump_to_scratch_and_retireve(containers));
Code hashing = scan_file( project_dir "dependencies/hashing.hpp" );
header.print( hashing );
// header.print( hashing );
CodeBody parsed_strings = parse_file( project_dir "dependencies/strings.hpp" );
CodeBody strings = def_body(CT_Global_Body);
@ -298,7 +328,22 @@ int gen_main()
{
case CT_Preprocess_If:
{
ignore_preprocess_cond_block(txt("! GEN_COMPILER_C"), entry, parsed_strings);
CodePreprocessCond cond = cast(CodePreprocessCond, entry);
if (cond->Content.starts_with(txt("GEN_COMPILER_C || ! GEN_SUPPORT_CPP_MEMBER_FEATURES")))
{
++ entry;
strings.append(entry); // typedef
strings.append(fmt_newline);
for (; entry != end(parsed_strings) && entry->Type != CT_Preprocess_EndIf; ++ entry) {}
++ entry;
break;
}
bool found = ignore_preprocess_cond_block(txt("! GEN_COMPILER_C"), entry, parsed_strings);
if (found) break;
found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_REFERENCES"), entry, parsed_strings);
}
break;
@ -350,7 +395,7 @@ int gen_main()
break;
}
}
header.print(dump_to_scratch_and_retireve(strings));
// header.print(dump_to_scratch_and_retireve(strings));
Code filesystem = scan_file( project_dir "dependencies/filesystem.hpp" );
Code timing = scan_file( project_dir "dependencies/timing.hpp" );

View File

@ -5,22 +5,67 @@
using SwapContentProc = CodeBody(void);
struct NeedsSelectors {
HashTable(Array(CodeFn)) table;
void set(StrC sig, Array(CodeFn) list) {
u32 key = crc32(sig.Ptr, sig.Len);
table.set(key, list);
}
Array(CodeFn)* get(StrC sig) {
u32 key = crc32(sig.Ptr, sig.Len);
return table.get(key);
}
};
CodeBody generate_generic_selectors(Array(Array(CodeFn)) listing)
{
constexpr char const* tmpl_selector =
R"( #define <base_name>(<params>) \
_Generic((
))
);
)";
CodeBody result = def_body(CT_Global_Body);
for (Array(CodeFn) functions : listing)
{
}
return result;
}
b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& body )
{
b32 found = false;
CodePreprocessCond cond = cast(CodePreprocessCond, entry_iter);
if ( cond->Content.contains(cond_sig) )
{
log_fmt("Preprocess cond found: %S\n", cond->Content);
log_fmt("Preprocess cond found: %SC\n", cond->Content);
found = true;
s32 depth = 1;
++ entry_iter; for(b32 continue_for = true; continue_for && entry_iter != body.end(); ) switch
++ entry_iter;
for(b32 continue_for = true; continue_for && entry_iter != body.end(); ) switch
(entry_iter->Type) {
case CT_Preprocess_If:
case CT_Preprocess_IfDef:
case CT_Preprocess_IfNotDef:
depth ++;
++ depth;
++ entry_iter;
break;
case CT_Preprocess_Else:
++ entry_iter;
for(; continue_for && entry_iter != body.end(); ++ entry_iter)
{
if (entry_iter->Type == CT_Preprocess_EndIf)
{
continue_for = false;
break;
}
body.append(entry_iter);
}
break;
case CT_Preprocess_EndIf:
@ -30,6 +75,7 @@ b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& bod
continue_for = false;
break;
}
++ entry_iter;
}
break;
default:
@ -41,6 +87,72 @@ b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& bod
return found;
}
CodeFn rename_function_to_unique_symbol(CodeFn fn, StrC optional_prefix = txt(""))
{
// Get basic components for the name
StrC old_name = fn->Name;
String new_name;
// Add prefix if provided
if (optional_prefix.Len)
new_name = string_fmt_buf(GlobalAllocator, "%SC_%SC_", optional_prefix, old_name);
else
new_name = string_fmt_buf(GlobalAllocator, "%SC_", old_name);
// Add return type to the signature
if (fn->ReturnType)
new_name.append_fmt("_%SC", fn->ReturnType->Name);
// Add parameter types to create a unique signature
bool first_param = true;
for (CodeParam param = fn->Params; param.ast; param = param->Next)
{
if (param->ValueType)
{
// Add separator for readability
if (first_param)
{
new_name.append("_P_");
first_param = false;
}
else
new_name.append("_");
// Add parameter type, handle any specifiers
if (param->ValueType->Specs && param->ValueType->Specs->NumEntries > 0)
{
// Add specifiers (const, volatile, etc)
for (Specifier spec : param->ValueType->Specs)
{
if (spec == Spec_Ptr) {
new_name.append("ptr_");
continue;
}
new_name.append_fmt("%SC_", to_str(spec));
}
}
new_name.append_fmt("%SC", param->ValueType->Name);
}
}
// Handle function specifiers if present
if (fn->Specs && fn->Specs->NumEntries > 0)
{
new_name.append("_S_");
for (Specifier* spec = begin(fn->Specs);
spec != end(fn->Specs);
++spec)
{
new_name.append_fmt("%SC_", to_str(*spec));
}
}
fn->Name = new_name;
return fn;
}
bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body )
{
bool found = false;
@ -54,7 +166,8 @@ bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_
// body.append(possible_region);
body.append(swap_content());
++ entry_iter; for(b32 continue_for = true; continue_for; ++entry_iter) switch
++ entry_iter;
for(b32 continue_for = true; continue_for; ++entry_iter) switch
(entry_iter->Type) {
case CT_Preprocess_Pragma:
{