From 1868d5726a5949d83c860060e79dd7299e2f5c38 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 21 Jul 2022 08:03:52 -0500 Subject: [PATCH] renamed header file --- build.sh | 20 ++++++++-------- include/{onyx_wasm.h => ovm_wasm.h} | 16 +++++++++---- include/vm.h | 16 ++++++------- src/wasm.c | 2 +- src/wasm/config.c | 2 +- src/wasm/engine.c | 2 +- src/wasm/extern.c | 2 +- src/wasm/frame.c | 37 ++++++++++++++++++++++++++++- src/wasm/func.c | 2 +- src/wasm/global.c | 2 +- src/wasm/instance.c | 11 ++++++++- src/wasm/memory.c | 2 +- src/wasm/module.c | 2 +- src/wasm/store.c | 3 ++- src/wasm/table.c | 2 +- src/wasm/trap.c | 23 ++++++++++++++++-- src/wasm/type.c | 2 +- src/wasm/value.c | 2 +- src/wasm_cli_test.c | 2 +- 19 files changed, 111 insertions(+), 39 deletions(-) rename include/{onyx_wasm.h => ovm_wasm.h} (95%) diff --git a/build.sh b/build.sh index ad33538..05f7bdf 100755 --- a/build.sh +++ b/build.sh @@ -3,10 +3,10 @@ CC="gcc" WARNINGS='-Wimplicit -Wmisleading-indentation -Wparentheses -Wsequence-point -Wreturn-type -Wshift-negative-value -Wunused-but-set-parameter -Wunused-but-set-variable -Wunused-function -Wunused-label -Wmaybe-uninitialized -Wsign-compare -Wstrict-overflow -Wduplicated-branches -Wduplicated-cond -Wtrigraphs -Waddress -Wlogical-op' # FLAGS="-g3 -DOVM_VERBOSE=1" -FLAGS="-O3" +FLAGS="-Ofast" LIBS="-pthread" INCLUDES="-I include" -TARGET="libonyx_embedder.so" +TARGET="libovmwasm.so" C_FILES="src/wasm.c src/vm/*.c src/wasm/*.c" $CC $FLAGS $INCLUDES -shared -fPIC -o $TARGET $C_FILES $LIBS $WARNINGS @@ -24,12 +24,12 @@ $CC $FLAGS $INCLUDES -shared -fPIC -o $TARGET $C_FILES $LIBS $WARNINGS # LIBS="-L$(pwd) -lonyx_embedder -lm -Wl,-rpath=./" # # $CC $FLAGS $INCLUDES -o $TARGET $C_FILES $LIBS $WARNINGS - - -flex -o src/tools/lex.yy.c src/tools/asm.l - -TARGET="bin/assembler" -C_FILES="src/tools/assembler.c src/tools/lex.yy.c" - -$CC $FLAGS $INCLUDES -o $TARGET $C_FILES $LIBS $WARNINGS +# +# +# flex -o src/tools/lex.yy.c src/tools/asm.l +# +# TARGET="bin/assembler" +# C_FILES="src/tools/assembler.c src/tools/lex.yy.c" +# +# $CC $FLAGS $INCLUDES -o $TARGET $C_FILES $LIBS $WARNINGS diff --git a/include/onyx_wasm.h b/include/ovm_wasm.h similarity index 95% rename from include/onyx_wasm.h rename to include/ovm_wasm.h index a775dc7..c258ddd 100644 --- a/include/onyx_wasm.h +++ b/include/ovm_wasm.h @@ -1,5 +1,5 @@ -#ifndef _ONYX_WASM_H -#define _ONYX_WASM_H +#ifndef _OVM_WASM_H +#define _OVM_WASM_H #include "wasm.h" #include "vm.h" @@ -20,7 +20,8 @@ struct wasm_engine_t { }; struct wasm_store_t { - wasm_engine_t *engine; + wasm_engine_t *engine; + wasm_instance_t *instance; }; @@ -94,10 +95,17 @@ struct wasm_ref_t { }; struct wasm_frame_t { + wasm_instance_t *instance; + int func_idx; + size_t func_offset; + size_t module_offset; }; struct wasm_trap_t { wasm_message_t msg; + wasm_frame_vec_t frames; + + wasm_store_t *store; }; struct wasm_foreign_t { @@ -161,7 +169,7 @@ struct wasm_table_inner_t { ovm_program_t *program; ovm_engine_t *engine; - i32 static_arr; + int static_arr; const wasm_tabletype_t *type; }; diff --git a/include/vm.h b/include/vm.h index 7583735..e737868 100644 --- a/include/vm.h +++ b/include/vm.h @@ -256,14 +256,14 @@ struct ovm_instr_t { #define OVMI_SAR 0x0D // %r = %a >>> %b #define OVMI_IMM 0x10 // %r = i/l/f/d -#define OVMI_MOV 0x10 // %r = %a -#define OVMI_LOAD 0x11 // %r = mem[%a + %b] -#define OVMI_STORE 0x12 // mem[%r + %b] = %a -#define OVMI_COPY 0x13 // memcpy(%r, %a, %b) -#define OVMI_FILL 0x14 // memset(%r, %a, %b) -#define OVMI_REG_GET 0x15 // %r = #a -#define OVMI_REG_SET 0x16 // #r = %a -#define OVMI_IDX_ARR 0x17 // %r = (a)[%b] +#define OVMI_MOV 0x11 // %r = %a +#define OVMI_LOAD 0x12 // %r = mem[%a + %b] +#define OVMI_STORE 0x13 // mem[%r + %b] = %a +#define OVMI_COPY 0x14 // memcpy(%r, %a, %b) +#define OVMI_FILL 0x15 // memset(%r, %a, %b) +#define OVMI_REG_GET 0x16 // %r = #a +#define OVMI_REG_SET 0x17 // #r = %a +#define OVMI_IDX_ARR 0x18 // %r = (a)[%b] #define OVMI_LT 0x20 // %r = %a < %b #define OVMI_LT_S 0x21 // %r = %a < %b diff --git a/src/wasm.c b/src/wasm.c index 83fed31..6b0da56 100644 --- a/src/wasm.c +++ b/src/wasm.c @@ -5,4 +5,4 @@ #include "bh.h" #include "stb_ds.h" -#include "onyx_wasm.h" +#include "ovm_wasm.h" diff --git a/src/wasm/config.c b/src/wasm/config.c index 2ffcab4..07ea474 100644 --- a/src/wasm/config.c +++ b/src/wasm/config.c @@ -1,5 +1,5 @@ -#include "onyx_wasm.h" +#include "ovm_wasm.h" wasm_config_t *wasm_config_new() { wasm_config_t *config = malloc(sizeof(*config)); diff --git a/src/wasm/engine.c b/src/wasm/engine.c index 036e731..8f2162d 100644 --- a/src/wasm/engine.c +++ b/src/wasm/engine.c @@ -1,5 +1,5 @@ -#include "onyx_wasm.h" +#include "ovm_wasm.h" #include "vm.h" wasm_engine_t *wasm_engine_new() { diff --git a/src/wasm/extern.c b/src/wasm/extern.c index 22bc6e9..9cda46c 100644 --- a/src/wasm/extern.c +++ b/src/wasm/extern.c @@ -1,6 +1,6 @@ -#include "onyx_wasm.h" +#include "ovm_wasm.h" WASM_DECLARE_VEC_IMPL(extern, *) diff --git a/src/wasm/frame.c b/src/wasm/frame.c index 0ade447..26bfb04 100644 --- a/src/wasm/frame.c +++ b/src/wasm/frame.c @@ -1,2 +1,37 @@ -// TODO +#include "ovm_wasm.h" + + +void wasm_frame_delete(wasm_frame_t *frame) { + // Don't have to free the frame because it was allocated on the + // arena allocator. +} + +WASM_DECLARE_VEC_IMPL(frame, *) + +wasm_frame_t *wasm_frame_copy(const wasm_frame_t* frame) { + if (!frame) return NULL; + assert(frame->instance); + + wasm_frame_t *new_frame = bh_alloc_item(frame->instance->store->engine->store->arena_allocator, wasm_frame_t); + memcpy(new_frame, frame, sizeof(*frame)); + + return new_frame; +} + +wasm_instance_t* wasm_frame_instance(const wasm_frame_t* frame) { + return frame->instance; +} + +u32 wasm_frame_func_index(const wasm_frame_t *frame) { + return frame->func_idx; +} + +size_t wasm_frame_func_offset(const wasm_frame_t *frame) { + return frame->func_offset; +} + +size_t wasm_frame_module_offset(const wasm_frame_t *frame) { + return frame->module_offset; +} + diff --git a/src/wasm/func.c b/src/wasm/func.c index bd5f6b1..bd3fad8 100644 --- a/src/wasm/func.c +++ b/src/wasm/func.c @@ -1,5 +1,5 @@ -#include "onyx_wasm.h" +#include "ovm_wasm.h" #include "vm.h" wasm_func_t *wasm_func_new(wasm_store_t *store, const wasm_functype_t *type, wasm_func_callback_t callback) { diff --git a/src/wasm/global.c b/src/wasm/global.c index 1dee022..81370eb 100644 --- a/src/wasm/global.c +++ b/src/wasm/global.c @@ -1,5 +1,5 @@ -#include "onyx_wasm.h" +#include "ovm_wasm.h" #include "vm.h" wasm_global_t *wasm_global_new(wasm_store_t *store, const wasm_globaltype_t *type, const wasm_val_t *initial) { diff --git a/src/wasm/instance.c b/src/wasm/instance.c index 916c29b..57875dc 100644 --- a/src/wasm/instance.c +++ b/src/wasm/instance.c @@ -1,9 +1,11 @@ -#include "onyx_wasm.h" +#include "ovm_wasm.h" #include "vm.h" #include +static_assert(sizeof(ovm_value_t) == sizeof(wasm_val_t)); + typedef struct wasm_ovm_binding wasm_ovm_binding; struct wasm_ovm_binding { int func_idx; @@ -304,6 +306,13 @@ wasm_instance_t *wasm_instance_new(wasm_store_t *store, const wasm_module_t *mod instance->store = store; instance->module = module; + if (store->instance) { + bh_printf("A WASM store should only be used for a single instance!\n"); + return NULL; + } + + store->instance = instance; + instance->funcs = NULL; instance->memories = NULL; instance->tables = NULL; diff --git a/src/wasm/memory.c b/src/wasm/memory.c index 04722e6..3844579 100644 --- a/src/wasm/memory.c +++ b/src/wasm/memory.c @@ -1,5 +1,5 @@ -#include "onyx_wasm.h" +#include "ovm_wasm.h" #include "vm.h" wasm_memory_t *wasm_memory_new(wasm_store_t *store, const wasm_memorytype_t *type) { diff --git a/src/wasm/module.c b/src/wasm/module.c index f68e87c..a1786aa 100644 --- a/src/wasm/module.c +++ b/src/wasm/module.c @@ -1,6 +1,6 @@ -#include "onyx_wasm.h" +#include "ovm_wasm.h" #include "vm_codebuilder.h" #include "./module_parsing.h" diff --git a/src/wasm/store.c b/src/wasm/store.c index 0f805b7..5c06791 100644 --- a/src/wasm/store.c +++ b/src/wasm/store.c @@ -1,10 +1,11 @@ -#include "onyx_wasm.h" +#include "ovm_wasm.h" #include "vm.h" wasm_store_t *wasm_store_new(wasm_engine_t *engine) { wasm_store_t *store = bh_alloc(engine->store->arena_allocator, sizeof(wasm_store_t)); store->engine = engine; + store->instance = NULL; return store; } diff --git a/src/wasm/table.c b/src/wasm/table.c index 6110df2..b878eba 100644 --- a/src/wasm/table.c +++ b/src/wasm/table.c @@ -1,5 +1,5 @@ -#include "onyx_wasm.h" +#include "ovm_wasm.h" #include "vm.h" wasm_table_t *wasm_table_new(wasm_store_t *store, const wasm_tabletype_t *type, wasm_ref_t *init) { diff --git a/src/wasm/trap.c b/src/wasm/trap.c index 7909867..cb4ba74 100644 --- a/src/wasm/trap.c +++ b/src/wasm/trap.c @@ -1,11 +1,30 @@ -#include "onyx_wasm.h" +#include "ovm_wasm.h" #include "vm.h" wasm_trap_t *wasm_trap_new(wasm_store_t *store, const wasm_message_t *msg) { wasm_trap_t *trap = bh_alloc(store->engine->store->arena_allocator, sizeof(*trap)); + trap->store = store; trap->msg = *msg; + // + // Generate frames + bh_arr(ovm_stack_frame_t) ovm_frames = store->instance->state->stack_frames; + int frame_count = bh_arr_length(ovm_frames); + + wasm_frame_vec_new_uninitialized(&trap->frames, frame_count); + + fori (i, 0, frame_count) { + ovm_stack_frame_t *ovm_frame = &ovm_frames[frame_count - 1 - i]; + wasm_frame_t *frame = bh_alloc(store->engine->store->arena_allocator, sizeof(*frame)); + frame->instance = store->instance; + frame->func_idx = ovm_frame->func->id; + frame->func_offset = 0; + frame->module_offset = 0; + + trap->frames.data[i] = frame; + } + return trap; } @@ -14,7 +33,7 @@ void wasm_trap_message(const wasm_trap_t *trap, wasm_message_t *out) { } wasm_frame_t *wasm_trap_origin(const wasm_trap_t *trap) { - return NULL; + return trap->frames.data[0]; } void wasm_trap_trace(const wasm_trap_t *trap, wasm_frame_vec_t *frames) { diff --git a/src/wasm/type.c b/src/wasm/type.c index ba34497..8bc6298 100644 --- a/src/wasm/type.c +++ b/src/wasm/type.c @@ -1,5 +1,5 @@ -#include "onyx_wasm.h" +#include "ovm_wasm.h" #include "vm.h" // diff --git a/src/wasm/value.c b/src/wasm/value.c index f81dea9..a533c94 100644 --- a/src/wasm/value.c +++ b/src/wasm/value.c @@ -1,5 +1,5 @@ -#include "onyx_wasm.h" +#include "ovm_wasm.h" void wasm_val_delete(wasm_val_t* v) { // Apparently this is suppose to do nothing... diff --git a/src/wasm_cli_test.c b/src/wasm_cli_test.c index 5f06ea9..8903f27 100644 --- a/src/wasm_cli_test.c +++ b/src/wasm_cli_test.c @@ -2,7 +2,7 @@ #include "bh.h" #include "wasm.h" -#include "onyx_wasm.h" +#include "ovm_wasm.h" #include "vm.h" -- 2.25.1