From: Brendan Hansen Date: Fri, 17 Jun 2022 03:11:48 +0000 (-0500) Subject: starting to fill out the wasm->ovm layer X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=ca49507747008023c3c2e4df299539bee76c2b22;p=onyx-embedder.git starting to fill out the wasm->ovm layer --- diff --git a/include/onyx_wasm.h b/include/onyx_wasm.h index bea6cb9..e388dea 100644 --- a/include/onyx_wasm.h +++ b/include/onyx_wasm.h @@ -2,19 +2,23 @@ #define _ONYX_WASM_H #include "wasm.h" +#include "vm.h" // Core Utils struct wasm_config_t { - + bool debug_enabled; }; struct wasm_engine_t { + wasm_config_t *config; + ovm_store_t *store; + ovm_engine_t *engine; }; struct wasm_store_t { - + i32 nothing; }; @@ -25,8 +29,8 @@ struct wasm_valtype_t { }; struct wasm_functype_t { - wasm_valtype_vec_t *params; - wasm_valtype_vec_t *results; + wasm_valtype_vec_t params; + wasm_valtype_vec_t results; }; struct wasm_globaltype_t { diff --git a/src/cli.c b/src/cli.c index cc8856d..449ce3e 100644 --- a/src/cli.c +++ b/src/cli.c @@ -39,7 +39,7 @@ int main(int argc, char *argv[]) { { .type = OVM_TYPE_I32, .i32 = 1 }, { .type = OVM_TYPE_I32, .i32 = 2 }, }; - ovm_value_t result = ovm_func_call(engine, state, prog, 0, 2, values); + ovm_value_t result = ovm_func_call(engine, state, prog, 2, 0, values); printf("%d %d\n", result.type, result.i32); ovm_state_delete(state); diff --git a/src/wasm/config.c b/src/wasm/config.c index e69de29..2ffcab4 100644 --- a/src/wasm/config.c +++ b/src/wasm/config.c @@ -0,0 +1,18 @@ + +#include "onyx_wasm.h" + +wasm_config_t *wasm_config_new() { + wasm_config_t *config = malloc(sizeof(*config)); + config->debug_enabled = false; + return config; +} + +void wasm_config_delete(wasm_config_t *config) { + free(config); +} + +void wasm_config_enable_debug(wasm_config_t *config, bool enabled) { + config->debug_enabled = enabled; +} + + diff --git a/src/wasm/engine.c b/src/wasm/engine.c index e69de29..036e731 100644 --- a/src/wasm/engine.c +++ b/src/wasm/engine.c @@ -0,0 +1,29 @@ + +#include "onyx_wasm.h" +#include "vm.h" + +wasm_engine_t *wasm_engine_new() { + ovm_store_t *store = ovm_store_new(); + + wasm_engine_t *engine = bh_alloc_item(store->heap_allocator, wasm_engine_t); + engine->config = NULL; + engine->store = store; + + ovm_engine_t *ovm_engine = ovm_engine_new(store); + engine->engine = ovm_engine; + return engine; +} + +wasm_engine_t *wasm_engine_new_with_config(wasm_config_t *config) { + wasm_engine_t *engine = wasm_engine_new(); + engine->config = config; + return engine; +} + +void wasm_engine_delete(wasm_engine_t *engine) { + ovm_store_t *store = engine->store; + ovm_engine_delete(engine->engine); + bh_free(store->heap_allocator, engine); + ovm_store_delete(store); +} + diff --git a/src/wasm/store.c b/src/wasm/store.c index e69de29..1fc7eaf 100644 --- a/src/wasm/store.c +++ b/src/wasm/store.c @@ -0,0 +1,12 @@ + +#include "onyx_wasm.h" +#include "vm.h" + +wasm_store_t *wasm_store_new(wasm_engine_t *engine) { + return malloc(sizeof(wasm_store_t)); +} + +void wasm_store_delete(wasm_store_t *store) { + free(store); +} + diff --git a/src/wasm/type.c b/src/wasm/type.c index e69de29..05f1881 100644 --- a/src/wasm/type.c +++ b/src/wasm/type.c @@ -0,0 +1,251 @@ + +#include "onyx_wasm.h" +#include "vm.h" + +// +// wasm_byte_t +// + +void wasm_byte_vec_new_empty(wasm_byte_vec_t *out) { + out->size = 0; + out->data = NULL; +} + +void wasm_byte_vec_new_uninitialized(wasm_byte_vec_t *out, size_t size) { + out->data = malloc(sizeof(wasm_byte_t) * size); + out->size = size; +} + +void wasm_byte_vec_new(wasm_byte_vec_t *out, size_t size, const wasm_byte_t data[]) { + out->data = malloc(sizeof(wasm_byte_t) * size); + out->size = size; + + fori (i, 0, (i32) size) { + out->data[i] = data[i]; + } +} + +void wasm_byte_vec_copy(wasm_byte_vec_t *out, const wasm_byte_vec_t *in) { + wasm_byte_vec_new(out, in->size, in->data); +} + +void wasm_byte_vec_delete(wasm_byte_vec_t *vec) { + if (vec->data) free(vec->data); +} + +// +// wasm_valtype_t +// + +static wasm_valtype_t + valtype_i32 = { WASM_I32 }, + valtype_i64 = { WASM_I64 }, + valtype_f32 = { WASM_F32 }, + valtype_f64 = { WASM_F64 }, + valtype_anyref = { WASM_ANYREF }, + valtype_funcref = { WASM_FUNCREF }; + + +wasm_valtype_t *wasm_valtype_new(wasm_valkind_t kind) { + switch (kind) { + case WASM_I32: return &valtype_i32; + case WASM_I64: return &valtype_i64; + case WASM_F32: return &valtype_f32; + case WASM_F64: return &valtype_f64; + case WASM_ANYREF: return &valtype_anyref; + case WASM_FUNCREF: return &valtype_funcref; + default: assert(0); + } +} + +void wasm_valtype_delete(wasm_valtype_t *type) {} + +wasm_valkind_t wasm_valtype_kind(const wasm_valtype_t *type) { + assert(type); + return type->kind; +} + + +void wasm_valtype_vec_new_empty(wasm_valtype_vec_t *out) { + out->size = 0; + out->data = NULL; +} + +void wasm_valtype_vec_new_uninitialized(wasm_valtype_vec_t *out, size_t size) { + out->data = malloc(sizeof(wasm_valtype_t) * size); + out->size = size; +} + +void wasm_valtype_vec_new(wasm_valtype_vec_t *out, size_t size, wasm_valtype_t* const data[]) { + out->data = malloc(sizeof(wasm_valtype_t) * size); + out->size = size; + + fori (i, 0, (i32) size) { + out->data[i] = data[i]; + } +} + +void wasm_valtype_vec_copy(wasm_valtype_vec_t *out, const wasm_valtype_vec_t *in) { + wasm_valtype_vec_new(out, in->size, in->data); +} + +void wasm_valtype_vec_delete(wasm_valtype_vec_t *vec) { + if (vec->data) free(vec->data); +} + + + +// wasm_functype_t + +wasm_functype_t *wasm_functype_new(wasm_valtype_vec_t *params, wasm_valtype_vec_t *results) { + wasm_functype_t *functype = malloc(sizeof(*functype)); + functype->params = *params; + functype->results = *results; + + return functype; +} + +void wasm_functype_delete(wasm_functype_t *functype) { + if (functype) free(functype); +} + +const wasm_valtype_vec_t* wasm_functype_params(const wasm_functype_t *functype) { + return &functype->params; +} + +const wasm_valtype_vec_t* wasm_functype_results(const wasm_functype_t *functype) { + return &functype->results; +} + + +void wasm_functype_vec_new_empty(wasm_functype_vec_t *out) { + out->size = 0; + out->data = NULL; +} + +void wasm_functype_vec_new_uninitialized(wasm_functype_vec_t *out, size_t size) { + out->data = malloc(sizeof(wasm_functype_t) * size); + out->size = size; +} + +void wasm_functype_vec_new(wasm_functype_vec_t *out, size_t size, wasm_functype_t* const data[]) { + out->data = malloc(sizeof(wasm_functype_t) * size); + out->size = size; + + fori (i, 0, (i32) size) { + out->data[i] = data[i]; + } +} + +void wasm_functype_vec_copy(wasm_functype_vec_t *out, const wasm_functype_vec_t *in) { + wasm_functype_vec_new(out, in->size, in->data); +} + +void wasm_functype_vec_delete(wasm_functype_vec_t *vec) { + if (vec->data) free(vec->data); +} + + + +// wasm_globaltype_t + +wasm_globaltype_t *wasm_globaltype_new(wasm_valtype_t *valtype, wasm_mutability_t mut) { + wasm_globaltype_t *globaltype = malloc(sizeof(*globaltype)); + globaltype->content = valtype; + globaltype->mutability = mut; + + return globaltype; +} + +void wasm_globaltype_delete(wasm_globaltype_t *globaltype) { + if (globaltype) free(globaltype); +} + +const wasm_valtype_t* wasm_globaltype_content(const wasm_globaltype_t *globaltype) { + return globaltype->content; +} + +wasm_mutability_t wasm_globaltype_mutability(const wasm_globaltype_t *globaltype) { + return globaltype->mutability; +} + + +void wasm_globaltype_vec_new_empty(wasm_globaltype_vec_t *out) { + out->size = 0; + out->data = NULL; +} + +void wasm_globaltype_vec_new_uninitialized(wasm_globaltype_vec_t *out, size_t size) { + out->data = malloc(sizeof(wasm_globaltype_t) * size); + out->size = size; +} + +void wasm_globaltype_vec_new(wasm_globaltype_vec_t *out, size_t size, wasm_globaltype_t* const data[]) { + out->data = malloc(sizeof(wasm_globaltype_t) * size); + out->size = size; + + fori (i, 0, (i32) size) { + out->data[i] = data[i]; + } +} + +void wasm_globaltype_vec_copy(wasm_globaltype_vec_t *out, const wasm_globaltype_vec_t *in) { + wasm_globaltype_vec_new(out, in->size, in->data); +} + +void wasm_globaltype_vec_delete(wasm_globaltype_vec_t *vec) { + if (vec->data) free(vec->data); +} + + +// wasm_tabletype_t + +wasm_tabletype_t *wasm_tabletype_new(wasm_valtype_t *valtype, const wasm_limits_t *limits) { + wasm_tabletype_t *tabletype = malloc(sizeof(*tabletype)); + tabletype->element = valtype; + tabletype->limits = *limits; + + return tabletype; +} + +void wasm_tabletype_delete(wasm_tabletype_t *tabletype) { + if (tabletype) free(tabletype); +} + +const wasm_valtype_t* wasm_tabletype_element(const wasm_tabletype_t *tabletype) { + return tabletype->element; +} + +const wasm_limits_t* wasm_tabletype_limits(const wasm_tabletype_t *tabletype) { + return &tabletype->limits; +} + + +void wasm_tabletype_vec_new_empty(wasm_tabletype_vec_t *out) { + out->size = 0; + out->data = NULL; +} + +void wasm_tabletype_vec_new_uninitialized(wasm_tabletype_vec_t *out, size_t size) { + out->data = malloc(sizeof(wasm_tabletype_t) * size); + out->size = size; +} + +void wasm_tabletype_vec_new(wasm_tabletype_vec_t *out, size_t size, wasm_tabletype_t* const data[]) { + out->data = malloc(sizeof(wasm_tabletype_t) * size); + out->size = size; + + fori (i, 0, (i32) size) { + out->data[i] = data[i]; + } +} + +void wasm_tabletype_vec_copy(wasm_tabletype_vec_t *out, const wasm_tabletype_vec_t *in) { + wasm_tabletype_vec_new(out, in->size, in->data); +} + +void wasm_tabletype_vec_delete(wasm_tabletype_vec_t *vec) { + if (vec->data) free(vec->data); +} + +