From: Brendan Hansen Date: Mon, 19 Apr 2021 16:24:49 +0000 (-0500) Subject: outputting a C file. X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=a70c444775413d19709f381ea63cbf3eebf7646c;p=onyx.git outputting a C file. --- diff --git a/bin/onyx b/bin/onyx index 063e96de..a4b2ea14 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/build.sh b/build.sh index 567e92f8..1f985c7e 100755 --- a/build.sh +++ b/build.sh @@ -7,7 +7,7 @@ sudo cp -r ./core/ "$CORE_DIR" [ "$1" = "libs_only" ] && exit 0 -C_FILES="onyx onyxastnodes onyxbuiltins onyxchecker onyxclone onyxdoc onyxentities onyxerrors onyxlex onyxparser onyxsymres onyxtypes onyxutils onyxwasm onyxc" +C_FILES="onyx onyxastnodes onyxbuiltins onyxchecker onyxclone onyxdoc onyxentities onyxerrors onyxlex onyxparser onyxsymres onyxtypes onyxutils onyxc" TARGET='./bin/onyx' CC='gcc' diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index efbee081..de5ccc80 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -1011,7 +1011,8 @@ void add_entities_for_node(bh_arr(Entity *)* target_arr, AstNode* node, Scope* s void entity_bring_to_state(Entity* ent, EntityState state); void symres_entity(Entity* ent); void check_entity(Entity* ent); -void emit_entity(Entity* ent); +void emit_c_entity(Entity* ent); +// void emit_wasm_entity(Entity* ent); struct Package { char *name; @@ -1077,7 +1078,10 @@ struct Context { bh_arr(bh_file_contents) loaded_files; // NOTE: This is defined in onyxwasm.h - struct OnyxWasmModule* wasm_module; + // struct OnyxWasmModule* wasm_module; + + // NOTE: This is defined in onyxc.h + struct OnyxCFile* c_file; b32 cycle_detected : 1; }; diff --git a/include/onyxc.h b/include/onyxc.h index 3ea25584..0a33e526 100644 --- a/include/onyxc.h +++ b/include/onyxc.h @@ -16,12 +16,15 @@ typedef struct CStringLiteral { char* data; } CStringLiteral; -typedef struct CFile { +typedef struct OnyxCFile { bh_arr(CMemoryReservation) memory_reservations; bh_arr(CStringLiteral) string_literals; -} CFile; +} OnyxCFile; +void emit_c_entity(Entity* ent); +void onyx_output_c_file(OnyxCFile* cfile, bh_file file); + #endif diff --git a/src/onyx.c b/src/onyx.c index beb0a498..72c0cf2c 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -6,7 +6,7 @@ #include "onyxerrors.h" #include "onyxparser.h" #include "onyxutils.h" -#include "onyxwasm.h" +#include "onyxc.h" #include "onyxdoc.h" #define VERSION "v0.1.0-beta" @@ -37,7 +37,7 @@ static const char* docstring = "Onyx compiler version " VERSION "\n" "\n" "Flags:\n" "\t List of initial files\n" - "\t-o Specify the target file (default: out.wasm)\n" + "\t-o Specify the target file (default: out.c)\n" "\t--runtime, -r Specifies a runtime. Can be: wasi, js, custom.\n" "\t--verbose, -V Verbose output\n" "\t -VV Very verbose output\n" @@ -63,7 +63,7 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg .runtime = Runtime_Wasi, .files = NULL, - .target_file = "out.wasm", + .target_file = "out.c", }; bh_arr_new(alloc, options.files, 2); @@ -177,8 +177,9 @@ static void context_init(CompileOptions* opts) { bh_arena_init(&context.ast_arena, global_heap_allocator, 16 * 1024 * 1024); // 16MB context.ast_alloc = bh_arena_allocator(&context.ast_arena); - context.wasm_module = bh_alloc_item(global_heap_allocator, OnyxWasmModule); - *context.wasm_module = onyx_wasm_module_create(global_heap_allocator); + // 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); entity_heap_init(&context.entities); @@ -357,7 +358,7 @@ static b32 process_entity(Entity* ent) { case Entity_State_Comptime_Check_Types: case Entity_State_Check_Types: check_entity(ent); break; - case Entity_State_Code_Gen: emit_entity(ent); break; + case Entity_State_Code_Gen: emit_c_entity(ent); break; } return ent->state != before_state; @@ -456,6 +457,13 @@ static i32 onyx_compile() { if (bh_file_create(&output_file, context.options->target_file) != BH_FILE_ERROR_NONE) return ONYX_COMPILER_PROGRESS_FAILED_OUTPUT; + if (context.options->verbose_output) + bh_printf("Outputting to C file: %s\n", output_file.filename); + + onyx_output_c_file(context.c_file, output_file); + + return ONYX_COMPILER_PROGRESS_SUCCESS; +#if 0 if (context.options->verbose_output) bh_printf("Outputting to WASM file: %s\n", output_file.filename); @@ -473,6 +481,7 @@ static i32 onyx_compile() { } return ONYX_COMPILER_PROGRESS_SUCCESS; +#endif } int main(int argc, char *argv[]) { diff --git a/src/onyxc.c b/src/onyxc.c index 2f2fcf4e..8179648e 100644 --- a/src/onyxc.c +++ b/src/onyxc.c @@ -1,3 +1,10 @@ #include "onyxc.h" +void emit_c_entity(Entity *ent) { + + ent->state = Entity_State_Finalized; +} + +#include "onyxc_output.c" + diff --git a/src/onyxc_output.c b/src/onyxc_output.c new file mode 100644 index 00000000..d98f0b7d --- /dev/null +++ b/src/onyxc_output.c @@ -0,0 +1,24 @@ +#include "onyxc.h" + + +static char* BOILERPLATE_TOP = + "#include \n" + "#include \n" + "typedef uint8_t u8;\n" + "typedef uint16_t u16;\n" + "typedef uint32_t u32;\n" + "typedef uint64_t u64;\n" + "typedef int8_t i8;\n" + "typedef int16_t i16;\n" + "typedef int32_t i32;\n" + "typedef int64_t i64;\n" + "typedef float f32;\n" + "typedef double f64;\n" + "typedef void *rawptr;\n"; + + + + +void onyx_output_c_file(OnyxCFile* cfile, bh_file file) { + bh_file_write(&file, BOILERPLATE_TOP, strlen(BOILERPLATE_TOP)); +} diff --git a/src/onyxutils.c b/src/onyxutils.c index 92290fdd..efa5b12d 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -1264,7 +1264,7 @@ void entity_bring_to_state(Entity* ent, EntityState state) { switch (ent->state) { case Entity_State_Resolve_Symbols: symres_entity(ent); break; case Entity_State_Check_Types: check_entity(ent); break; - case Entity_State_Code_Gen: emit_entity(ent); break; + case Entity_State_Code_Gen: emit_c_entity(ent); break; default: return; }