starting to fill out the wasm->ovm layer
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 17 Jun 2022 03:11:48 +0000 (22:11 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 17 Jun 2022 03:11:48 +0000 (22:11 -0500)
include/onyx_wasm.h
src/cli.c
src/wasm/config.c
src/wasm/engine.c
src/wasm/store.c
src/wasm/type.c

index bea6cb950759105f727a6d1fb59c160c897d77f8..e388dea66f4dab3cbc9ec095ca1d981b6c057ed5 100644 (file)
@@ -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 {
index cc8856d783ace227ed71b79ebe75875b5a9d979e..449ce3ef26a69b92594e9957d9ec408f4eea1933 100644 (file)
--- 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);
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..2ffcab400d46922b769f3af0f7a3d9b404bf7c96 100644 (file)
@@ -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;
+}
+
+
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..036e731b2fd113bf576bca5760e83f9e3f45e002 100644 (file)
@@ -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);
+}
+
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..1fc7eaf0b9d260b2b08a12b895c2652241e97b95 100644 (file)
@@ -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);
+}
+
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..05f18819ecf0c1ff70c90157fe5af51501f18497 100644 (file)
@@ -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);
+}
+
+