From: Brendan Hansen Date: Sat, 17 Apr 2021 03:19:17 +0000 (-0500) Subject: even more clean up in code generation X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=64ccc50476aa37b6018ee63f0f85b4628088f00a;p=onyx.git even more clean up in code generation --- diff --git a/bin/onyx b/bin/onyx index 5e002a9d..58a3ef31 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/include/bh.h b/include/bh.h index 2afef54c..69b2becf 100644 --- a/include/bh.h +++ b/include/bh.h @@ -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) diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 2368fafe..7e44ac33 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -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;