outputting a C file.
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 19 Apr 2021 16:24:49 +0000 (11:24 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 19 Apr 2021 16:24:49 +0000 (11:24 -0500)
bin/onyx
build.sh
include/onyxastnodes.h
include/onyxc.h
src/onyx.c
src/onyxc.c
src/onyxc_output.c [new file with mode: 0644]
src/onyxutils.c

index 063e96decd24a57efb8d055814b7c4fb53a4e59b..a4b2ea1475fa7b51c5d4838ee04787fdb56b7d01 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 567e92f843b7228d6b2653a239891a46110b3a20..1f985c7e9ea8c4e701657841241790cbe9088631 100755 (executable)
--- 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'
 
index efbee08158ff3446b363a4cdd9f7e37e038a9e5d..de5ccc808e897060984b8580518446794f540f69 100644 (file)
@@ -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;
 };
index 3ea25584bd3ffb793f65a40679928bc155f08c85..0a33e5268b6e63afccb34fb46fabd58393b498b5 100644 (file)
@@ -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
index beb0a4982b671177201db43ed6cb36133c8095f2..72c0cf2c60b25e67c117f35e0ea95da3e101bd53 100644 (file)
@@ -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<input files>           List of initial files\n"
-    "\t-o <target_file>        Specify the target file (default: out.wasm)\n"
+    "\t-o <target_file>        Specify the target file (default: out.c)\n"
     "\t--runtime, -r <runtime> 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[]) {
index 2f2fcf4e026680503a377438eb52c0b5c10a421a..8179648ea19bf3c9ec4cb6d24af5fe5d41095c85 100644 (file)
@@ -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 (file)
index 0000000..d98f0b7
--- /dev/null
@@ -0,0 +1,24 @@
+#include "onyxc.h"
+
+
+static char* BOILERPLATE_TOP =
+    "#include <stdlib.h>\n"
+    "#include <stdint.h>\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));
+}
index 92290fdd79e4173ebc3494b11985fd6e83db2b52..efa5b12dd84648b360d88f7df225bd5be317a2ac 100644 (file)
@@ -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;
         }