From: Brendan Hansen Date: Mon, 19 Apr 2021 20:09:44 +0000 (-0500) Subject: string literals get compiled. X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=269b74994f5405ec65891dc6db38645b97b07353;p=onyx.git string literals get compiled. --- diff --git a/include/onyxc.h b/include/onyxc.h index 0a33e526..b82e450f 100644 --- a/include/onyxc.h +++ b/include/onyxc.h @@ -7,7 +7,7 @@ typedef struct CMemoryReservation { i32 number; i32 size; - char* initial_value; + void* initial_value; } CMemoryReservation; typedef struct CStringLiteral { @@ -19,12 +19,15 @@ typedef struct CStringLiteral { typedef struct OnyxCFile { bh_arr(CMemoryReservation) memory_reservations; - bh_arr(CStringLiteral) string_literals; + + u32 next_string_literal_idx; + bh_table(CStringLiteral) string_literals; + } OnyxCFile; -void emit_c_entity(Entity* ent); +OnyxCFile onyx_c_file_create(bh_allocator alloc); void onyx_output_c_file(OnyxCFile* cfile, bh_file file); #endif diff --git a/src/onyx.c b/src/onyx.c index 72c0cf2c..9b44da58 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -180,6 +180,7 @@ static void context_init(CompileOptions* opts) { // context.wasm_module = bh_alloc_item(global_heap_allocator, OnyxWasmModule); // *context.wasm_module = onyx_wasm_module_create(global_heap_allocator); context.c_file = bh_alloc_item(global_heap_allocator, OnyxCFile); + *context.c_file = onyx_c_file_create(global_heap_allocator); entity_heap_init(&context.entities); diff --git a/src/onyxc.c b/src/onyxc.c index 8179648e..b24d1c27 100644 --- a/src/onyxc.c +++ b/src/onyxc.c @@ -1,10 +1,59 @@ #include "onyxc.h" +#include "onyxutils.h" +static void emit_string_literal(OnyxCFile* c_file, AstStrLit* strlit) { + + i8* strdata = bh_alloc_array(global_heap_allocator, i8, strlit->token->length + 1); + i32 length = string_process_escape_seqs(strdata, strlit->token->text, strlit->token->length); + + if (bh_table_has(CStringLiteral, c_file->string_literals, (char *) strdata)) { + CStringLiteral sti = bh_table_get(CStringLiteral, c_file->string_literals, (char *) strdata); + strlit->addr = sti.number; + strlit->length = sti.size; + + bh_free(global_heap_allocator, strdata); + return; + } + + CStringLiteral csl = { + .number = c_file->next_string_literal_idx, + .size = length, + .data = strdata, + }; + + strlit->addr = csl.number; + strlit->length = length; + c_file->next_string_literal_idx += 1; + + bh_table_put(CStringLiteral, c_file->string_literals, (char *) strdata, csl); +} void emit_c_entity(Entity *ent) { + OnyxCFile* c_file = context.c_file; + + switch (ent->type) { + case Entity_Type_String_Literal: { + emit_string_literal(c_file, ent->strlit); + break; + } + } ent->state = Entity_State_Finalized; } + +OnyxCFile onyx_c_file_create(bh_allocator alloc) { + OnyxCFile cfile; + + cfile.memory_reservations = NULL; + bh_arr_new(alloc, cfile.memory_reservations, 4); + + cfile.next_string_literal_idx = 0; + cfile.string_literals = NULL; + bh_table_init(alloc, cfile.string_literals, 4); + + return cfile; +} + #include "onyxc_output.c" diff --git a/src/onyxc_output.c b/src/onyxc_output.c index 945a77d5..cb1d3575 100644 --- a/src/onyxc_output.c +++ b/src/onyxc_output.c @@ -29,10 +29,29 @@ static char* REGISTER_DEFINITION = " f32 f32;\n" " f64 f64;\n" " rawptr rawptr;\n" - "} Register;"; + "} Register;\n"; +static char* ENTRYPOINT = + "int main(int argc, char* argv[]) {\n" + " puts(\"Nothing so far!\");\n" + " return 0;\n" + "}\n"; -void onyx_output_c_file(OnyxCFile* cfile, bh_file file) { +static void output_string_literals(OnyxCFile* c_file, bh_file file) { + bh_table_each_start(CStringLiteral, c_file->string_literals); + bh_fprintf(&file, "static const char __string%d[] = {", value.number); + fori (i, 0, value.size) { + bh_fprintf(&file, "%d,", (u32) value.data[i]); + } + bh_fprintf(&file, "0};\n"); + bh_table_each_end; +} + +void onyx_output_c_file(OnyxCFile* c_file, bh_file file) { bh_file_write(&file, BOILERPLATE_TOP, strlen(BOILERPLATE_TOP)); bh_file_write(&file, REGISTER_DEFINITION, strlen(REGISTER_DEFINITION)); + + output_string_literals(c_file, file); + + bh_file_write(&file, ENTRYPOINT, strlen(ENTRYPOINT)); }