continuing work on creating the WASM->OVM interface
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 20 Jun 2022 17:52:49 +0000 (12:52 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 20 Jun 2022 17:52:49 +0000 (12:52 -0500)
docs/wasm_wtf.md
include/onyx_wasm.h
src/wasm/type.c
src/wasm/value.c

index be20c4e7bb62bbe2df82c6ca30032776fe7ebf62..59411b76ed3c4ab9279a0065e80f5d3356494a10 100644 (file)
@@ -46,3 +46,13 @@ OVM Engine  <-> WASM Engine
 OVM State   <-> WASM Instance
 
 
+## Concessions made
+
+To made the translation from WASM to OVM easier, some of
+the features granted by WASM are removed. As OVM is mainly
+about creating a local debugging environment for Onyx, I think
+it is unnecessary to implement of all the WASM features that
+Onyx does not use. For this reason, the following WASM features
+are omitted:
+
+
index fa1ddc01437c2465fb1c98fea40e0478a7403bee..a797dd1278d4bd8a74756cc25c150f8a39660397 100644 (file)
@@ -30,35 +30,40 @@ struct wasm_valtype_t {
     wasm_valkind_t kind;
 };
 
-struct wasm_functype_t {
+struct wasm_functype_inner_t {
     wasm_valtype_vec_t params;
     wasm_valtype_vec_t results;
 };
 
-struct wasm_globaltype_t {
+struct wasm_globaltype_inner_t {
     wasm_valtype_t *content;
     wasm_mutability_t mutability; 
 };
 
-struct wasm_tabletype_t {
+struct wasm_tabletype_inner_t {
     wasm_valtype_t *element;
     wasm_limits_t   limits;
 };
 
-struct wasm_memorytype_t {
+struct wasm_memorytype_inner_t {
     wasm_limits_t   limits;
 };
 
 struct wasm_externtype_t {
     wasm_externkind_t kind;
     union {
-        wasm_functype_t   *func;
-        wasm_globaltype_t *global;
-        wasm_tabletype_t  *table;
-        wasm_memorytype_t *memory;
+        struct wasm_functype_inner_t   func;
+        struct wasm_globaltype_inner_t global;
+        struct wasm_tabletype_inner_t  table;
+        struct wasm_memorytype_inner_t memory;
     };
 };
 
+struct wasm_functype_t { wasm_externtype_t type; };
+struct wasm_globaltype_t { wasm_externtype_t type; };
+struct wasm_tabletype_t { wasm_externtype_t type; };
+struct wasm_memorytype_t { wasm_externtype_t type; };
+
 struct wasm_importtype_t {
     wasm_name_t *module_name;
     wasm_name_t *import_name;
@@ -90,18 +95,33 @@ struct wasm_module_t {
 };
 
 struct wasm_func_t {
+    wasm_instance_t *instance;
+    ovm_func_t *func;
 };
 
 struct wasm_global_t {
+    wasm_instance_t *instance;
+    int register_index;
+
+    wasm_globaltype_t *type;
 };
 
 struct wasm_table_t {
+    wasm_tabletype_t *type;
 };
 
 struct wasm_memory_t {
+    wasm_memorytype_t *type;
 };
 
 struct wasm_extern_t {
+    wasm_externtype_t *type;
+    union {
+        wasm_func_t   *func;
+        wasm_global_t *global;
+        wasm_table_t  *table;
+        wasm_memory_t *memory;
+    };
 };
 
 struct wasm_instance_t {
@@ -109,4 +129,33 @@ struct wasm_instance_t {
     ovm_state_t *state;
 };
 
+
+#define WASM_DECLARE_VEC_IMPL(type, ptr_or_none) \
+    void wasm_##type##_vec_new_empty(wasm_##type##_vec_t *out) { \
+        out->size = 0; \
+        out->data = NULL; \
+    } \
+     \
+    void wasm_##type##_vec_new_uninitialized(wasm_##type##_vec_t *out, size_t size) { \
+        out->data = malloc(sizeof(wasm_##type##_t) * size); \
+        out->size = size; \
+    } \
+     \
+    void wasm_##type##_vec_new(wasm_##type##_vec_t *out, size_t size, wasm_##type##_t ptr_or_none const data[]) { \
+        out->data = malloc(sizeof(wasm_##type##_t) * size); \
+        out->size = size; \
+     \
+        fori (i, 0, (i32) size) { \
+            out->data[i] = data[i]; \
+        } \
+    } \
+     \
+    void wasm_##type##_vec_copy(wasm_##type##_vec_t *out, const wasm_##type##_vec_t *in) { \
+        wasm_##type##_vec_new(out, in->size, in->data); \
+    } \
+     \
+    void wasm_##type##_vec_delete(wasm_##type##_vec_t *vec) { \
+        if (vec->data) free(vec->data); \
+    }
+
 #endif
index b4dabdd1633598e4bade03c6902db59aaac5b3b0..2fa307c491ce2aafa84553c3937f8007cdffde51 100644 (file)
@@ -6,32 +6,7 @@
 // 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_DECLARE_VEC_IMPL(byte, )
 
 //
 // wasm_valtype_t
@@ -65,42 +40,16 @@ wasm_valkind_t wasm_valtype_kind(const wasm_valtype_t *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_DECLARE_VEC_IMPL(valtype, *)
 
 
 // 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;
+    functype->type.kind = WASM_EXTERN_FUNC;
+    functype->type.func.params = *params;
+    functype->type.func.results = *results;
 
     return functype;
 }
@@ -110,49 +59,23 @@ void wasm_functype_delete(wasm_functype_t *functype) {
 }
 
 const wasm_valtype_vec_t* wasm_functype_params(const wasm_functype_t *functype) {
-    return &functype->params;
+    return &functype->type.func.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);
+    return &functype->type.func.results;
 }
 
+WASM_DECLARE_VEC_IMPL(functype, *)
 
 
 // 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;
+    globaltype->type.kind = WASM_EXTERN_GLOBAL;
+    globaltype->type.global.content = valtype;
+    globaltype->type.global.mutability = mut;
 
     return globaltype;
 }
@@ -162,48 +85,23 @@ void wasm_globaltype_delete(wasm_globaltype_t *globaltype) {
 }
 
 const wasm_valtype_t* wasm_globaltype_content(const wasm_globaltype_t *globaltype) {
-    return globaltype->content;
+    return globaltype->type.global.content;
 }
 
 wasm_mutability_t wasm_globaltype_mutability(const wasm_globaltype_t *globaltype) {
-    return globaltype->mutability;
+    return globaltype->type.global.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_DECLARE_VEC_IMPL(globaltype, *)
 
 
 // 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;
+    tabletype->type.kind = WASM_EXTERN_TABLE;
+    tabletype->type.table.element = valtype;
+    tabletype->type.table.limits = *limits;
 
     return tabletype;
 }
@@ -213,87 +111,114 @@ void wasm_tabletype_delete(wasm_tabletype_t *tabletype) {
 }
 
 const wasm_valtype_t* wasm_tabletype_element(const wasm_tabletype_t *tabletype) {
-    return tabletype->element;
+    return tabletype->type.table.element;
 }
 
 const wasm_limits_t* wasm_tabletype_limits(const wasm_tabletype_t *tabletype) {
-    return &tabletype->limits;
+    return &tabletype->type.table.limits;
 }
 
+WASM_DECLARE_VEC_IMPL(tabletype, *)
 
-void wasm_tabletype_vec_new_empty(wasm_tabletype_vec_t *out) {
-    out->size = 0;
-    out->data = NULL;
-}
+// wasm_memorytype_t
 
-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;
-}
+wasm_memorytype_t *wasm_memorytype_new(const wasm_limits_t *limits) {
+    wasm_memorytype_t *memorytype = malloc(sizeof(*memorytype));
+    memorytype->type.kind = WASM_EXTERN_TABLE;
+    memorytype->type.memory.limits = *limits;
 
-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;
+    return memorytype;
+}
 
-    fori (i, 0, (i32) size) {
-        out->data[i] = data[i];
-    }
+void wasm_memorytype_delete(wasm_memorytype_t *memorytype) {
+    if (memorytype) free(memorytype);
 }
 
-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);
+const wasm_limits_t* wasm_memorytype_limits(const wasm_memorytype_t *memorytype) {
+    return &memorytype->type.memory.limits;
 }
 
-void wasm_tabletype_vec_delete(wasm_tabletype_vec_t *vec) {
-    if (vec->data) free(vec->data);
+WASM_DECLARE_VEC_IMPL(memorytype, *)
+
+// wasm_externtype_t
+
+const wasm_externkind_t wasm_externtype_kind(const wasm_externtype_t *externtype) {
+    return externtype->kind;
 }
 
+WASM_DECLARE_VEC_IMPL(externtype, *)
 
-// wasm_memorytype_t
+wasm_externtype_t* wasm_functype_as_externtype(wasm_functype_t* ext)     { return (wasm_externtype_t *) ext; }
+wasm_externtype_t* wasm_globaltype_as_externtype(wasm_globaltype_t* ext) { return (wasm_externtype_t *) ext; }
+wasm_externtype_t* wasm_tabletype_as_externtype(wasm_tabletype_t* ext)   { return (wasm_externtype_t *) ext; }
+wasm_externtype_t* wasm_memorytype_as_externtype(wasm_memorytype_t* ext) { return (wasm_externtype_t *) ext; }
 
-wasm_memorytype_t *wasm_memorytype_new(const wasm_limits_t *limits) {
-    wasm_memorytype_t *memorytype = malloc(sizeof(*memorytype));
-    memorytype->limits = *limits;
+wasm_functype_t* wasm_externtype_as_functype(wasm_externtype_t* ext)     { return ext->kind == WASM_EXTERN_FUNC ? (wasm_functype_t *) ext : NULL; }
+wasm_globaltype_t* wasm_externtype_as_globaltype(wasm_externtype_t* ext) { return ext->kind == WASM_EXTERN_GLOBAL ? (wasm_globaltype_t *) ext : NULL; }
+wasm_tabletype_t* wasm_externtype_as_tabletype(wasm_externtype_t* ext)   { return ext->kind == WASM_EXTERN_TABLE ? (wasm_tabletype_t *) ext : NULL; }
+wasm_memorytype_t* wasm_externtype_as_memorytype(wasm_externtype_t* ext) { return ext->kind == WASM_EXTERN_MEMORY ? (wasm_memorytype_t *) ext : NULL; }
 
-    return memorytype;
-}
+const wasm_externtype_t* wasm_functype_as_externtype_const(const wasm_functype_t* ext)     { return (const wasm_externtype_t *) ext; }
+const wasm_externtype_t* wasm_globaltype_as_externtype_const(const wasm_globaltype_t* ext) { return (const wasm_externtype_t *) ext; }
+const wasm_externtype_t* wasm_tabletype_as_externtype_const(const wasm_tabletype_t* ext)   { return (const wasm_externtype_t *) ext; }
+const wasm_externtype_t* wasm_memorytype_as_externtype_const(const wasm_memorytype_t* ext) { return (const wasm_externtype_t *) ext; }
 
-void wasm_memorytype_delete(wasm_memorytype_t *memorytype) {
-    if (memorytype) free(memorytype);
+const wasm_functype_t* wasm_externtype_as_functype_const(const wasm_externtype_t* ext)     { return ext->kind == WASM_EXTERN_FUNC ? (const wasm_functype_t *) ext : NULL; }
+const wasm_globaltype_t* wasm_externtype_as_globaltype_const(const wasm_externtype_t* ext) { return ext->kind == WASM_EXTERN_GLOBAL ? (const wasm_globaltype_t *) ext : NULL; }
+const wasm_tabletype_t* wasm_externtype_as_tabletype_const(const wasm_externtype_t* ext)   { return ext->kind == WASM_EXTERN_TABLE ? (const wasm_tabletype_t *) ext : NULL; }
+const wasm_memorytype_t* wasm_externtype_as_memorytype_const(const wasm_externtype_t* ext) { return ext->kind == WASM_EXTERN_MEMORY ? (const wasm_memorytype_t *) ext : NULL; }
+
+
+
+// wasm_importtype_t
+
+wasm_importtype_t *wasm_importtype_new(wasm_name_t *module, wasm_name_t* name, wasm_externtype_t *ext) {
+    wasm_importtype_t *importtype = malloc(sizeof(*importtype));
+    importtype->module_name = module;
+    importtype->import_name = name;
+    importtype->type = ext;
+
+    return importtype;
 }
 
-const wasm_limits_t* wasm_memorytype_limits(const wasm_memorytype_t *memorytype) {
-    return &memorytype->limits;
+void wasm_importtype_delete(wasm_importtype_t *importtype) {
+    if (importtype) free(importtype);
 }
 
+const wasm_name_t* wasm_importtype_module(const wasm_importtype_t* importtype) {
+    return importtype->module_name;
+}
 
-void wasm_memorytype_vec_new_empty(wasm_memorytype_vec_t *out) {
-    out->size = 0;
-    out->data = NULL;
+const wasm_name_t* wasm_importtype_name(const wasm_importtype_t* importtype) {
+    return importtype->import_name;
 }
 
-void wasm_memorytype_vec_new_uninitialized(wasm_memorytype_vec_t *out, size_t size) {
-    out->data = malloc(sizeof(wasm_memorytype_t) * size);
-    out->size = size;
+const wasm_externtype_t* wasm_importtype_type(const wasm_importtype_t* importtype) {
+    return importtype->type;
 }
 
-void wasm_memorytype_vec_new(wasm_memorytype_vec_t *out, size_t size, wasm_memorytype_t* const data[]) {
-    out->data = malloc(sizeof(wasm_memorytype_t) * size);
-    out->size = size;
+WASM_DECLARE_VEC_IMPL(importtype, *)
 
-    fori (i, 0, (i32) size) {
-        out->data[i] = data[i];
-    }
-}
+// wasm_exporttype_t
+
+wasm_exporttype_t *wasm_exporttype_new(wasm_name_t* name, wasm_externtype_t *ext) {
+    wasm_exporttype_t *exporttype = malloc(sizeof(*exporttype));
+    exporttype->name = name;
+    exporttype->type = ext;
 
-void wasm_memorytype_vec_copy(wasm_memorytype_vec_t *out, const wasm_memorytype_vec_t *in) {
-    wasm_memorytype_vec_new(out, in->size, in->data);
+    return exporttype;
 }
 
-void wasm_memorytype_vec_delete(wasm_memorytype_vec_t *vec) {
-    if (vec->data) free(vec->data);
+void wasm_exporttype_delete(wasm_exporttype_t *exporttype) {
+    if (exporttype) free(exporttype);
 }
 
+const wasm_name_t* wasm_exporttype_name(const wasm_exporttype_t* exporttype) {
+    return exporttype->name;
+}
 
-// wasm_externtype_t
+const wasm_externtype_t* wasm_exporttype_type(const wasm_exporttype_t* exporttype) {
+    return exporttype->type;
+}
 
+WASM_DECLARE_VEC_IMPL(exporttype, *)
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0810fa79aff3823ed2b17f91250275d89e38e367 100644 (file)
@@ -0,0 +1,24 @@
+
+#include "onyx_wasm.h"
+
+void wasm_val_delete(own wasm_val_t* v) {
+    // Apparently this is suppose to do nothing...
+}
+
+void wasm_val_copy(own wasm_val_t* out, const wasm_val_t* in) {
+    out->kind = in->kind;
+    switch (out->kind) {
+        case WASM_I32: out->i32 = in->i32; break;
+        case WASM_I64: out->i64 = in->i64; break;
+        case WASM_F32: out->f32 = in->i32; break;
+        case WASM_F64: out->f64 = in->f64; break;
+        case WASM_ANYREF:
+        case WASM_FUNCREF:
+            out->ref = in->ref;
+            break;
+    }
+}
+
+WASM_DECLARE_VEC_IMPL(val, )
+
+