string literals get compiled.
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 19 Apr 2021 20:09:44 +0000 (15:09 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 19 Apr 2021 20:09:44 +0000 (15:09 -0500)
include/onyxc.h
src/onyx.c
src/onyxc.c
src/onyxc_output.c

index 0a33e5268b6e63afccb34fb46fabd58393b498b5..b82e450f1de0f6b5f1154f031c7ad188e2b8adb2 100644 (file)
@@ -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
index 72c0cf2c60b25e67c117f35e0ea95da3e101bd53..9b44da58b1d29c8e3750087be0162d10300613e4 100644 (file)
@@ -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);
 
index 8179648ea19bf3c9ec4cb6d24af5fe5d41095c85..b24d1c27703ed9cfa75924d8fe0c65a07731f472 100644 (file)
@@ -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"
 
index 945a77d5d384bd46122b536348dc94679db5ca96..cb1d357584612cf6be8520f09921c1b822bbc0e3 100644 (file)
@@ -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));
 }