even more clean up in code generation
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 17 Apr 2021 03:19:17 +0000 (22:19 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 17 Apr 2021 03:19:17 +0000 (22:19 -0500)
bin/onyx
include/bh.h
src/onyxwasm.c

index 5e002a9d88a1e2917fe43a92c604fca0ed34f032..58a3ef31605799b64bc4f284df68e8388f04c70c 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 2afef54c84d71aa553145a616bf7b5ff19561bb8..69b2becfccf1b47becd2a22d77fecf47d2921348 100644 (file)
@@ -528,6 +528,11 @@ typedef struct bh__arr {
     bh__arr_grow(bh_arr_allocator(arr), (void **) &(arr), sizeof(*(arr)), bh_arr_length(arr) + 1), \
     arr[bh__arrhead(arr)->length++] = value)
 
+#define bh_arr_set_at(arr, n, value) ( \
+    bh__arr_grow(bh_arr_allocator(arr), (void **) &(arr), sizeof(*(arr)), (n) + 1), \
+    bh_arr_set_length((arr), bh_max(bh_arr_length(arr), (i32) (n) + 1)), \
+    arr[n] = value)
+
 #define bh_arr_is_empty(arr)          (arr ? bh__arrhead(arr)->length == 0 : 1)
 #define bh_arr_clear(arr)             (arr ? (bh__arrhead(arr)->length = 0) : 0)
 
index 2368fafe3c9c91a2c24aa41bb4b942a2d6aec8ca..7e44ac3368c3eeb09d6ea8ba0f3a775d0a575c7a 100644 (file)
@@ -2792,10 +2792,7 @@ static void emit_function(OnyxWasmModule* mod, AstFunction* fd) {
 
     bh_arr_push(wasm_func.code, ((WasmInstruction){ WI_BLOCK_END, 0x00 }));
 
-    // HACK: This is gross
-    bh_arr_grow(mod->funcs, func_idx - mod->foreign_function_count + 1);
-    mod->funcs[func_idx - mod->foreign_function_count] = wasm_func;
-    bh_arr_set_length(mod->funcs, bh_max((u32) bh_arr_length(mod->funcs), func_idx - mod->foreign_function_count + 1));
+    bh_arr_set_at(mod->funcs, func_idx - mod->foreign_function_count, wasm_func);
 
     // NOTE: Clear the local map on exit of generating this function
     bh_imap_clear(&mod->local_map);
@@ -2852,9 +2849,7 @@ static void emit_global(OnyxWasmModule* module, AstGlobal* global) {
         default: assert(("Invalid global type", 0)); break;
     }
 
-    bh_arr_grow(module->globals, global_idx - module->foreign_global_count + 1);
-    module->globals[global_idx - module->foreign_global_count] = glob;
-    bh_arr_set_length(module->globals, bh_max((u32) bh_arr_length(module->globals), global_idx - module->foreign_global_count + 1));
+    bh_arr_set_at(module->globals, global_idx - module->foreign_global_count, glob);
 
     if (global->flags & Ast_Flag_Global_Stack_Top)
         module->stack_top_ptr = &module->globals[global_idx - module->foreign_global_count].initial_value[0].data.i1;