typedef struct CMemoryReservation {
i32 number;
i32 size;
- char* initial_value;
+ void* initial_value;
} CMemoryReservation;
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
// 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);
#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"
" 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));
}