aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-09-07 00:04:38 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-10-05 08:17:21 +0200
commit144b7fe1415ff97497fa4ea3b95e8dd6b95d069e (patch)
tree68391f6ec2dba2879ca8255ee25ce39ebaa7d4ad /src
First commit1st
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt19
-rw-r--r--src/exception.c15
-rw-r--r--src/init.c20
-rw-r--r--src/interp/CMakeLists.txt21
-rw-r--r--src/interp/check_opcode.c59
-rw-r--r--src/interp/global_pop.c34
-rw-r--r--src/interp/globalptr.c17
-rw-r--r--src/interp/initexpr_set.c28
-rw-r--r--src/interp/interp.c411
-rw-r--r--src/interp/ops.c53
-rw-r--r--src/interp/resume.c20
-rw-r--r--src/interp/routines/CMakeLists.txt12
-rw-r--r--src/interp/routines/find_export.c202
-rw-r--r--src/interp/routines/full.c48
-rw-r--r--src/interp/routines/limited.c54
-rw-r--r--src/interp/run.c16
-rw-r--r--src/interp/stack_pop.c31
-rw-r--r--src/interp/stack_push.c34
-rw-r--r--src/interp/stackptr.c16
-rw-r--r--src/interp/start.c26
-rw-r--r--src/io/CMakeLists.txt18
-rw-r--r--src/io/leb128.c47
-rw-r--r--src/io/read.c29
-rw-r--r--src/io/varint1.c27
-rw-r--r--src/io/varint64.c27
-rw-r--r--src/io/varint7.c26
-rw-r--r--src/io/varuint1.c27
-rw-r--r--src/io/varuint32.c27
-rw-r--r--src/io/varuint64.c27
-rw-r--r--src/io/varuint7.c27
-rw-r--r--src/load.c21
-rw-r--r--src/op/CMakeLists.txt16
-rw-r--r--src/op/check/CMakeLists.txt19
-rw-r--r--src/op/check/block.c34
-rw-r--r--src/op/check/br_if.c39
-rw-r--r--src/op/check/call.c39
-rw-r--r--src/op/check/end.c35
-rw-r--r--src/op/check/global_index.c39
-rw-r--r--src/op/check/i32_const.c32
-rw-r--r--src/op/check/i64_const.c32
-rw-r--r--src/op/check/local_index.c43
-rw-r--r--src/op/check/memory_immediate.c47
-rw-r--r--src/op/check/no_immediate.c23
-rw-r--r--src/op/end.c24
-rw-r--r--src/op/i32_const.c39
-rw-r--r--src/op/i32_load.c18
-rw-r--r--src/op/nop.c23
-rw-r--r--src/op/unreachable.c27
-rw-r--r--src/routines/CMakeLists.txt17
-rw-r--r--src/routines/call.c33
-rw-r--r--src/routines/check_magic.c50
-rw-r--r--src/routines/check_version.c50
-rw-r--r--src/routines/find_func_type.c20
-rw-r--r--src/routines/find_function.c21
-rw-r--r--src/routines/get_function_type.c256
-rw-r--r--src/routines/section.c94
-rw-r--r--src/routines/section/CMakeLists.txt23
-rw-r--r--src/routines/section/code.c271
-rw-r--r--src/routines/section/custom.c17
-rw-r--r--src/routines/section/data.c19
-rw-r--r--src/routines/section/element.c19
-rw-r--r--src/routines/section/exit.c36
-rw-r--r--src/routines/section/export.c113
-rw-r--r--src/routines/section/function.c49
-rw-r--r--src/routines/section/global.c144
-rw-r--r--src/routines/section/import.c308
-rw-r--r--src/routines/section/memory.c85
-rw-r--r--src/routines/section/skip.c26
-rw-r--r--src/routines/section/start.c35
-rw-r--r--src/routines/section/table.c111
-rw-r--r--src/routines/section/type.c159
-rw-r--r--src/run.c16
-rw-r--r--src/start.c45
73 files changed, 3985 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 0000000..e58c920
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,19 @@
+# nanowasm, a tiny WebAssembly/Wasm interpreter
+# Copyright (C) 2023-2024 Xavier Del Campo Romero
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at https://mozilla.org/MPL/2.0/.
+
+target_sources(${PROJECT_NAME} PRIVATE
+ exception.c
+ init.c
+ load.c
+ run.c
+ start.c
+)
+
+add_subdirectory(interp)
+add_subdirectory(io)
+add_subdirectory(routines)
+add_subdirectory(op)
diff --git a/src/exception.c b/src/exception.c
new file mode 100644
index 0000000..15820eb
--- /dev/null
+++ b/src/exception.c
@@ -0,0 +1,15 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+
+const char *nw_exception(const struct nw_inst *const i)
+{
+ return i->interp.exception;
+}
diff --git a/src/init.c b/src/init.c
new file mode 100644
index 0000000..6634722
--- /dev/null
+++ b/src/init.c
@@ -0,0 +1,20 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <string.h>
+
+void nw_init(struct nw_mod *const m, const struct nw_mod_cfg *const cfg)
+{
+ *m = (const struct nw_mod){.cfg = *cfg};
+ nwp_check_magic(m);
+}
diff --git a/src/interp/CMakeLists.txt b/src/interp/CMakeLists.txt
new file mode 100644
index 0000000..c3bb726
--- /dev/null
+++ b/src/interp/CMakeLists.txt
@@ -0,0 +1,21 @@
+# nanowasm, a tiny WebAssembly/Wasm interpreter
+# Copyright (C) 2023-2024 Xavier Del Campo Romero
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at https://mozilla.org/MPL/2.0/.
+
+target_sources(${PROJECT_NAME} PRIVATE
+ global_pop.c
+ globalptr.c
+ initexpr_set.c
+ ops.c
+ run.c
+ stack_pop.c
+ stackptr.c
+ stack_push.c
+ resume.c
+ start.c
+)
+
+add_subdirectory(routines)
diff --git a/src/interp/check_opcode.c b/src/interp/check_opcode.c
new file mode 100644
index 0000000..b10b927
--- /dev/null
+++ b/src/interp/check_opcode.c
@@ -0,0 +1,59 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/opcodes.h>
+
+int nwp_interp_check_opcode(const uint8_t op, FILE *const f)
+{
+ static int (*const checks[])(FILE *) =
+ {
+ [OP_UNREACHABLE] = check_unreachable,
+ [OP_NOP] = check_nop,
+ [OP_BLOCK] = check_block,
+ [OP_LOOP] = check_loop,
+ [OP_IF] = check_if,
+ [OP_ELSE] = check_else,
+ [OP_END] = check_end,
+ [OP_BR] = check_br,
+ [OP_BR_IF] = check_br_if,
+ [OP_BR_TABLE] = check_br_table,
+ [OP_RETURN] = check_return,
+ [OP_CALL] = check_call,
+ [OP_CALL_INDIRECT] = check_call_indirect,
+ [OP_GET_LOCAL] = check_get_local,
+ [OP_SET_LOCAL] = check_set_local,
+ [OP_TEE_LOCAL] = check_tee_local,
+ [OP_GET_GLOBAL] = check_get_global,
+ [OP_SET_GLOBAL] = check_set_global,
+ [OP_I32_LOAD] = check_i32_load,
+ [OP_I32_STORE] = check_i32_store,
+ [OP_I32_CONST] = check_i32_const,
+ [OP_I64_CONST] = check_i64_const,
+ [OP_F32_CONST] = check_f32_const,
+ [OP_F64_CONST] = check_f64_const,
+ [OP_I32_SUB] = check_i32_sub
+ };
+
+ if (op >= sizeof checks / sizeof *checks)
+ {
+ LOG("%s: invalid opcode %#" PRIx8 "\n", __func__, op);
+ return 1;
+ }
+ else if (!checks[op])
+ {
+ LOG("%s: unsupported opcode %#" PRIx8 "\n", __func__, op);
+ return 1;
+ }
+
+ return checks[op](f);
+}
diff --git a/src/interp/global_pop.c b/src/interp/global_pop.c
new file mode 100644
index 0000000..4066d63
--- /dev/null
+++ b/src/interp/global_pop.c
@@ -0,0 +1,34 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <nw/types.h>
+#include <stddef.h>
+#include <string.h>
+
+int interp_global_pop(struct nw_interp *const i, void *const dst,
+ const size_t n)
+{
+ const void *const src = (const char *)i->cfg.global.buf + i->global_i;
+
+ if (i->global_i < n)
+ {
+ static const char exc[] = "global memory underflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ i->global_i -= n;
+ memcpy(dst, src, n);
+ return 0;
+}
diff --git a/src/interp/globalptr.c b/src/interp/globalptr.c
new file mode 100644
index 0000000..486f5ba
--- /dev/null
+++ b/src/interp/globalptr.c
@@ -0,0 +1,17 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/types.h>
+
+void *interp_globalptr(const struct nw_interp *const i)
+{
+ return (char *)i->cfg.global.buf + i->global_i;
+}
diff --git a/src/interp/initexpr_set.c b/src/interp/initexpr_set.c
new file mode 100644
index 0000000..57601be
--- /dev/null
+++ b/src/interp/initexpr_set.c
@@ -0,0 +1,28 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/opcodes.h>
+
+static const enum opcode opcodes[] =
+{
+ OP_NOP,
+ OP_END,
+ OP_I32_CONST,
+ OP_I64_CONST,
+ OP_F32_CONST,
+ OP_F64_CONST
+};
+
+const struct nwp_interp_set nwp_interp_initexpr_set =
+{
+ .opcodes = opcodes,
+ .n = sizeof opcodes / sizeof *opcodes
+};
diff --git a/src/interp/interp.c b/src/interp/interp.c
new file mode 100644
index 0000000..dbeb126
--- /dev/null
+++ b/src/interp/interp.c
@@ -0,0 +1,411 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/log.h>
+#include <nw/interp.h>
+#include <nw/opcodes.h>
+#include <nw/ops.h>
+#include <nw/sections.h>
+#include <nw/types.h>
+#include <errno.h>
+#include <limits.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static const enum opcode initexpr_opcodes[] =
+{
+ OP_NOP,
+ OP_END,
+ OP_I32_CONST,
+ OP_I64_CONST,
+ OP_F32_CONST,
+ OP_F64_CONST
+};
+
+const struct interp_set nwp_interp_initexpr_set =
+{
+ .opcodes = initexpr_opcodes,
+ .n = sizeof initexpr_opcodes / sizeof *initexpr_opcodes
+};
+
+static int (*const ops[])(struct nw_interp *) =
+{
+ [OP_UNREACHABLE] = op_unreachable,
+ [OP_NOP] = op_nop,
+ [OP_BLOCK] = op_block,
+ [OP_LOOP] = op_loop,
+ [OP_IF] = op_if,
+ [OP_ELSE] = op_else,
+ [OP_END] = op_end,
+ [OP_BR] = op_br,
+ [OP_BR_IF] = op_br_if,
+ [OP_BR_TABLE] = op_br_table,
+ [OP_RETURN] = op_return,
+ [OP_CALL] = op_call,
+ [OP_CALL_INDIRECT] = op_call_indirect,
+ [OP_GET_LOCAL] = op_get_local,
+ [OP_SET_LOCAL] = op_set_local,
+ [OP_TEE_LOCAL] = op_tee_local,
+ [OP_GET_GLOBAL] = op_get_global,
+ [OP_SET_GLOBAL] = op_set_global,
+ [OP_I32_LOAD] = op_i32_load,
+ [OP_I32_STORE] = op_i32_store,
+ [OP_I32_CONST] = op_i32_const,
+ [OP_I64_CONST] = op_i64_const,
+ [OP_F32_CONST] = op_f32_const,
+ [OP_F64_CONST] = op_f64_const,
+ [OP_I32_SUB] = op_i32_sub
+};
+
+int interp_run(struct nw_interp *const i)
+{
+ uint8_t op;
+
+ if (!fread(&op, sizeof op, 1, i->f))
+ {
+ LOG("%s: fread(3) failed, feof=%d, ferror=%d\n", __func__, feof(i->f),
+ ferror(i->f));
+ i->exception = "I/O error";
+ return -1;
+ }
+ else if (op >= sizeof ops / sizeof *ops)
+ {
+ LOG("%s: invalid opcode %#" PRIx8 "\n", __func__, op);
+ i->exception = "invalid opcode";
+ return -1;
+ }
+ else if (!ops[op])
+ {
+ LOG("%s: unsupported opcode %#" PRIx8 "\n", __func__, op);
+ i->exception = "invalid opcode";
+ return -1;
+ }
+
+ return ops[op](i);
+}
+
+static int run_opcode_limited(struct nw_interp *const in,
+ const struct interp_set *const set)
+{
+ uint8_t op;
+
+ if (!fread(&op, sizeof op, 1, in->f))
+ {
+ LOG("%s: fread(3) failed, feof=%d, ferror=%d\n",
+ __func__, feof(in->f), ferror(in->f));
+ in->exception = "I/O error";
+ return -1;
+ }
+
+ for (size_t i = 0; i < set->n; i++)
+ if (op == set->opcodes[i])
+ return ops[op](in);
+
+ LOG("%s: unexpected opcode %#" PRIx8 "\n", __func__, op);
+ in->exception = "invalid opcode";
+ return -1;
+}
+
+int interp_run_limited(struct nw_interp *const i,
+ const struct interp_set *const set)
+{
+ while (!i->exit)
+ if (run_opcode_limited(i, set))
+ {
+ LOG("%s: run_opcode_limited failed\n", __func__);
+ return -1;
+ }
+
+ return 0;
+}
+
+int interp_check_opcode(const uint8_t op, FILE *const f)
+{
+ static int (*const checks[])(FILE *) =
+ {
+ [OP_UNREACHABLE] = check_unreachable,
+ [OP_NOP] = check_nop,
+ [OP_BLOCK] = check_block,
+ [OP_LOOP] = check_loop,
+ [OP_IF] = check_if,
+ [OP_ELSE] = check_else,
+ [OP_END] = check_end,
+ [OP_BR] = check_br,
+ [OP_BR_IF] = check_br_if,
+ [OP_BR_TABLE] = check_br_table,
+ [OP_RETURN] = check_return,
+ [OP_CALL] = check_call,
+ [OP_CALL_INDIRECT] = check_call_indirect,
+ [OP_GET_LOCAL] = check_get_local,
+ [OP_SET_LOCAL] = check_set_local,
+ [OP_TEE_LOCAL] = check_tee_local,
+ [OP_GET_GLOBAL] = check_get_global,
+ [OP_SET_GLOBAL] = check_set_global,
+ [OP_I32_LOAD] = check_i32_load,
+ [OP_I32_STORE] = check_i32_store,
+ [OP_I32_CONST] = check_i32_const,
+ [OP_I64_CONST] = check_i64_const,
+ [OP_F32_CONST] = check_f32_const,
+ [OP_F64_CONST] = check_f64_const,
+ [OP_I32_SUB] = check_i32_sub
+ };
+
+ if (op >= sizeof checks / sizeof *checks)
+ {
+ LOG("%s: invalid opcode %#" PRIx8 "\n", __func__, op);
+ return 1;
+ }
+ else if (!checks[op])
+ {
+ LOG("%s: unsupported opcode %#" PRIx8 "\n", __func__, op);
+ return 1;
+ }
+
+ return checks[op](f);
+}
+
+int interp_start(const struct nw_interp_cfg *const cfg, FILE *const f,
+ struct nw_interp *const i)
+{
+ *i = (const struct nw_interp)
+ {
+ .f = f,
+ .cfg = *cfg
+ };
+
+ return 0;
+}
+
+void *nwp_interp_stackptr(const struct nw_interp *const i)
+{
+ return (char *)i->cfg.stack.buf + i->stack_i;
+}
+
+struct retval_priv
+{
+ enum value_type type;
+
+ union
+ {
+ int32_t i32;
+ int64_t i64;
+ float f32;
+ double f64;
+ } u;
+};
+
+static int get_i32(struct nw_interp *const i, struct retval_priv *const rp)
+{
+ return interp_stack_pop(i, &rp->u.i32, sizeof rp->u.i32);
+}
+
+static int get_i64(struct nw_interp *const i, struct retval_priv *const rp)
+{
+ return interp_stack_pop(i, &rp->u.i64, sizeof rp->u.i64);
+}
+
+static int get_f32(struct nw_interp *const i, struct retval_priv *const rp)
+{
+ return interp_stack_pop(i, &rp->u.f32, sizeof rp->u.f32);
+}
+
+static int get_f64(struct nw_interp *const i, struct retval_priv *const rp)
+{
+ return interp_stack_pop(i, &rp->u.f64, sizeof rp->u.f64);
+}
+
+static int get_retval(struct nw_interp *const i, struct retval_priv *const rp)
+{
+ const struct retval *const r = &i->fp->retval;
+ const enum value_type v = r->type;
+ static int (*const f[])(struct nw_interp *, struct retval_priv *) =
+ {
+ [VALUE_TYPE_I32] = get_i32,
+ [VALUE_TYPE_I64] = get_i64,
+ [VALUE_TYPE_F32] = get_f32,
+ [VALUE_TYPE_F64] = get_f64
+ };
+
+ if (f[r->type](i, rp))
+ {
+ LOG("%s: callback for type %s failed\n", __func__,
+ value_type_tostr(v));
+ return -1;
+ }
+
+ rp->type = r->type;
+ return 0;
+}
+
+int interp_pop(struct nw_interp *const i)
+{
+ if (!i->fp)
+ {
+ static const char exc[] = "no frame pointer";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ const struct retval *const r = &i->fp->retval;
+ struct retval_priv rp;
+
+ if (r->returns && get_retval(i, &rp))
+ {
+ LOG("%s: get_retval failed\n", __func__);
+ return -1;
+ }
+
+#if 0
+#error TODO: do Wasm modules really have an exit status?
+#endif
+
+ if (!(i->fp = i->fp->prev))
+ {
+ /* Entry point is always assumed to return int, which is
+ * defined as an i32 variable according to Wasm. */
+ if (rp.type != VALUE_TYPE_I32)
+ {
+ LOG("%s: expected i32 return type, got %s\n", __func__,
+ value_type_tostr(rp.type));
+ i->exception = "unexpected return type";
+ return -1;
+ }
+
+ i->retval = rp.u.i32;
+ i->exit = true;
+ }
+
+ return 0;
+}
+
+int interp_stack_push(struct nw_interp *const i, const void *const src,
+ const size_t n)
+{
+ const size_t max = i->cfg.stack.n;
+ void *const dst = nwp_interp_stackptr(i);
+
+ if (max < n || i->stack_i > max - n)
+ {
+ static const char exc[] = "stack overflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ memcpy(dst, src, n);
+ i->stack_i += n;
+ return 0;
+}
+
+int interp_stack_pop(struct nw_interp *const i, void *const dst,
+ const size_t n)
+{
+ if (i->stack_i < n)
+ {
+ static const char exc[] = "stack underflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ i->stack_i -= n;
+ memcpy(dst, nwp_interp_stackptr(i), n);
+ return 0;
+}
+
+int interp_heap_store(struct nw_interp *const i, const size_t addr,
+ const void *const src, const size_t n)
+{
+ if (i->cfg.heap.n < n || addr > i->cfg.heap.n - n)
+ {
+ static const char exc[] = "heap overflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ void *const dst = (char *)i->cfg.heap.buf + addr;
+
+ memcpy(dst, src, n);
+ return 0;
+}
+
+int interp_heap_load(struct nw_interp *const i, const size_t addr,
+ void *const dst, const size_t n)
+{
+ const size_t max = i->cfg.heap.n;
+
+ if (max < n || addr > max - n)
+ {
+ static const char exc[] = "heap out-of-bounds access";
+
+ LOG("%s: %s (%lu, max %zu)\n", __func__, exc, (unsigned long)addr, max);
+ i->exception = exc;
+ return -1;
+ }
+
+ const void *const src = (const char *)i->cfg.heap.buf + addr;
+
+ memcpy(dst, src, n);
+ return 0;
+}
+
+int interp_global_push(struct nw_interp *const i, const void *const src,
+ const size_t n)
+{
+ const size_t max = i->cfg.global.n;
+ void *const dst = (char *)i->cfg.global.buf + i->global_i;
+
+ if (max < n || i->global_i > max - n)
+ {
+ static const char exc[] = "global memory overflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ memcpy(dst, src, n);
+ i->global_i += n;
+ return 0;
+}
+
+int interp_global_pop(struct nw_interp *const i, void *const dst,
+ const size_t n)
+{
+ const void *const src = (const char *)i->cfg.global.buf + i->global_i;
+
+ if (i->global_i < n)
+ {
+ static const char exc[] = "global memory underflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ i->global_i -= n;
+ memcpy(dst, src, n);
+ return 0;
+}
+
+void *interp_globalptr(const struct nw_interp *const i)
+{
+ return (char *)i->cfg.global.buf + i->global_i;
+}
diff --git a/src/interp/ops.c b/src/interp/ops.c
new file mode 100644
index 0000000..ea7c2b9
--- /dev/null
+++ b/src/interp/ops.c
@@ -0,0 +1,53 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/opcodes.h>
+#include <nw/ops.h>
+#include <nw/interp.h>
+
+static void (*const ops[])(struct nw_interp *) =
+{
+ [OP_UNREACHABLE] = nwp_op_unreachable,
+ [OP_NOP] = nwp_op_nop,
+ [OP_I32_LOAD] = nwp_op_i32_load,
+ [OP_I32_CONST] = nwp_op_i32_const,
+ [OP_END] = nwp_op_end,
+#if 0
+ [OP_BLOCK] = nwp_op_block,
+ [OP_LOOP] = nwp_op_loop,
+ [OP_IF] = nwp_op_if,
+ [OP_ELSE] = nwp_op_else,
+
+ [OP_BR] = nwp_op_br,
+ [OP_BR_IF] = nwp_op_br_if,
+ [OP_BR_TABLE] = nwp_op_br_table,
+ [OP_RETURN] = nwp_op_return,
+ [OP_CALL] = nwp_op_call,
+ [OP_CALL_INDIRECT] = nwp_op_call_indirect,
+ [OP_GET_LOCAL] = nwp_op_get_local,
+ [OP_SET_LOCAL] = nwp_op_set_local,
+ [OP_TEE_LOCAL] = nwp_op_tee_local,
+ [OP_GET_GLOBAL] = nwp_op_get_global,
+ [OP_SET_GLOBAL] = nwp_op_set_global,
+
+ [OP_I32_STORE] = nwp_op_i32_store,
+
+ [OP_I64_CONST] = nwp_op_i64_const,
+ [OP_F32_CONST] = nwp_op_f32_const,
+ [OP_F64_CONST] = nwp_op_f64_const,
+ [OP_I32_SUB] = nwp_op_i32_sub
+#endif
+};
+
+const struct nwp_ops nwp_ops =
+{
+ .ops = ops,
+ .n = sizeof ops / sizeof *ops
+};
diff --git a/src/interp/resume.c b/src/interp/resume.c
new file mode 100644
index 0000000..e808889
--- /dev/null
+++ b/src/interp/resume.c
@@ -0,0 +1,20 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+
+void nwp_interp_resume(struct nw_interp *const i)
+{
+ if (i->set)
+ nwp_interp_limited(i);
+ else
+ nwp_interp_full(i);
+}
diff --git a/src/interp/routines/CMakeLists.txt b/src/interp/routines/CMakeLists.txt
new file mode 100644
index 0000000..eb76ed3
--- /dev/null
+++ b/src/interp/routines/CMakeLists.txt
@@ -0,0 +1,12 @@
+# nanowasm, a tiny WebAssembly/Wasm interpreter
+# Copyright (C) 2023-2024 Xavier Del Campo Romero
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at https://mozilla.org/MPL/2.0/.
+
+target_sources(${PROJECT_NAME} PRIVATE
+ find_export.c
+ full.c
+ limited.c
+)
diff --git a/src/interp/routines/find_export.c b/src/interp/routines/find_export.c
new file mode 100644
index 0000000..f82e5f9
--- /dev/null
+++ b/src/interp/routines/find_export.c
@@ -0,0 +1,202 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <inttypes.h>
+#include <string.h>
+
+static enum nw_state entry_loop(struct nw_interp *i);
+
+static enum nw_state get_index(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_exp *const e = &i->sm.export;
+ struct nw_sm_leb128 *const l = &e->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &e->index);
+
+ if (n)
+ return n;
+
+ i->next = e->next;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_kind(struct nw_interp *const i)
+{
+ uint8_t kind;
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_sm_io io = {.buf = &kind, .n = sizeof kind};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+ else if (kind >= NW_KINDS)
+ {
+ static const char exc[] = "invalid export kind";
+
+ i->exception = exc;
+ LOG("%s: %s: %" PRIu8 "\n", __func__, exc, kind);
+ return NW_FATAL;
+ }
+
+ struct nw_i_sm_exp *const e = &i->sm.export;
+
+ e->kind = kind;
+ i->next = get_index;
+ return NW_AGAIN;
+}
+
+static enum nw_state skip_index(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_exp *const e = &i->sm.export;
+ struct nw_sm_leb128 *const l = &e->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &(nw_varuint32){0});
+
+ if (n)
+ return n;
+
+ e->entry_i++;
+ i->next = entry_loop;
+ return NW_AGAIN;
+}
+
+static enum nw_state skip_kind(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_sm_io io = {.buf = &(uint8_t){0}, .n = sizeof (uint8_t)};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+
+ i->next = skip_index;
+ return NW_AGAIN;
+}
+
+static enum nw_state skip_field_str(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ const struct nw_i_sm_exp *const e = &i->sm.export;
+ const long offset = e->len - e->len_i;
+ const enum nw_state n = cfg->seek(offset, NW_SEEK_CUR, cfg->user);
+
+ if (n)
+ return n;
+
+ i->next = skip_kind;
+ return NW_AGAIN;
+}
+
+static enum nw_state compare(struct nw_interp *const i)
+{
+ uint8_t byte;
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_exp *const e = &i->sm.export;
+ struct nw_sm_io io = {.buf = &byte, .n = sizeof byte};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+ else if (byte != e->sym[e->len_i++])
+ i->next = skip_field_str;
+ else if (e->len_i >= e->len)
+ i->next = get_kind;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_len(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_exp *const e = &i->sm.export;
+ struct nw_sm_leb128 *const l = &e->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &e->len);
+
+ if (n)
+ return n;
+ else if (e->len != strlen(e->sym))
+ i->next = skip_field_str;
+ else
+ i->next = compare;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_interp *const i)
+{
+ struct nw_i_sm_exp *const e = &i->sm.export;
+
+ if (e->entry_i >= e->count)
+ {
+ static const char exc[] = "failed to find symbol";
+
+ i->exception = exc;
+ LOG("%s: %s: %s\n", __func__, exc, e->sym);
+ return NW_FATAL;
+ }
+
+ e->len_i = 0;
+ i->next = get_len;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_exp *const e = &i->sm.export;
+ struct nw_sm_leb128 *const l = &e->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &e->count);
+
+ if (n)
+ return n;
+
+ i->next = entry_loop;
+ return NW_AGAIN;
+}
+
+static enum nw_state seek(struct nw_interp *const i)
+{
+ const struct nw_mod *const m = i->cfg.m;
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+
+ if (!m->sections[NW_SECTION_EXPORT])
+ {
+ static const char exc[] = "section not found";
+
+ i->exception = exc;
+ LOG("%s: %s: %s\n", __func__, exc, "export");
+ return NW_FATAL;
+ }
+
+ const enum nw_state n = cfg->seek(m->sections[NW_SECTION_EXPORT],
+ NW_SEEK_SET, cfg->user);
+
+ if (n)
+ return n;
+
+ i->next = get_count;
+ return NW_AGAIN;
+}
+
+void nwp_find_export(struct nw_interp *i, const char *sym,
+ enum nw_state (*const next)(struct nw_interp *))
+{
+ i->next = seek;
+ i->sm.export = (const struct nw_i_sm_exp)
+ {
+ .sym = sym,
+ .next = next
+ };
+}
diff --git a/src/interp/routines/full.c b/src/interp/routines/full.c
new file mode 100644
index 0000000..c60432d
--- /dev/null
+++ b/src/interp/routines/full.c
@@ -0,0 +1,48 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/opcodes.h>
+#include <nw/ops.h>
+#include <nw/routines.h>
+#include <inttypes.h>
+
+static enum nw_state get_bytecode(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_b *const b = &i->sm.bytecode;
+ const enum nw_state n = nwp_io_read(cfg, &b->io);
+
+ if (n)
+ return n;
+ else if (b->op >= nwp_ops.n)
+ {
+ static const char exc[] = "invalid opcode";
+
+ LOG("%s: %s: %#" PRIx8 "\n", __func__, exc, b->op);
+ i->exception = exc;
+ }
+
+ nwp_ops.ops[b->op](i);
+ return NW_AGAIN;
+}
+
+void nwp_interp_full(struct nw_interp *const i)
+{
+ struct nw_i_sm_b *const b = &i->sm.bytecode;
+
+ i->next = get_bytecode;
+ b->io = (const struct nw_sm_io)
+ {
+ .buf = &b->op,
+ .n = sizeof b->op
+ };
+}
diff --git a/src/interp/routines/limited.c b/src/interp/routines/limited.c
new file mode 100644
index 0000000..766b2d3
--- /dev/null
+++ b/src/interp/routines/limited.c
@@ -0,0 +1,54 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <stddef.h>
+#include <inttypes.h>
+
+static enum nw_state get_bytecode(struct nw_interp *const in)
+{
+ const struct nw_io_cfg *const cfg = &in->cfg.io;
+ struct nw_i_sm_b *const b = &in->sm.bytecode;
+ const enum nw_state n = nwp_io_read(cfg, &b->io);
+
+ if (n)
+ return n;
+
+ const struct nwp_interp_set *const set = in->set;
+
+ for (size_t i = 0; i < set->n; i++)
+ if (b->op == set->opcodes[i])
+ {
+ nwp_ops.ops[b->op](in);
+ return NW_AGAIN;
+ }
+
+ static const char exc[] = "invalid opcode";
+
+ LOG("%s: %s: %#" PRIx8 "\n", __func__, exc, b->op);
+ in->exception = exc;
+ return NW_FATAL;
+}
+
+void nwp_interp_limited(struct nw_interp *const i)
+{
+ struct nw_i_sm_b *const b = &i->sm.bytecode;
+
+ i->next = get_bytecode;
+ b->io = (const struct nw_sm_io)
+ {
+ .buf = &b->op,
+ .n = sizeof b->op
+ };
+}
diff --git a/src/interp/run.c b/src/interp/run.c
new file mode 100644
index 0000000..ef7f614
--- /dev/null
+++ b/src/interp/run.c
@@ -0,0 +1,16 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+
+enum nw_state nwp_interp_run(struct nw_interp *const i)
+{
+ return i->next(i);
+}
diff --git a/src/interp/stack_pop.c b/src/interp/stack_pop.c
new file mode 100644
index 0000000..8650be1
--- /dev/null
+++ b/src/interp/stack_pop.c
@@ -0,0 +1,31 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <stddef.h>
+#include <string.h>
+
+int interp_stack_pop(struct nw_interp *const i, void *const dst,
+ const size_t n)
+{
+ if (i->stack_i < n)
+ {
+ static const char exc[] = "stack underflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ i->stack_i -= n;
+ memcpy(dst, nwp_interp_stackptr(i), n);
+ return 0;
+}
diff --git a/src/interp/stack_push.c b/src/interp/stack_push.c
new file mode 100644
index 0000000..2145423
--- /dev/null
+++ b/src/interp/stack_push.c
@@ -0,0 +1,34 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <stddef.h>
+#include <string.h>
+
+int nwp_interp_stack_push(struct nw_interp *const i, const void *const src,
+ const size_t n)
+{
+ const size_t max = i->cfg.stack.n;
+ void *const dst = nwp_interp_stackptr(i);
+
+ if (max < n || i->stack_i > max - n)
+ {
+ static const char exc[] = "stack overflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ memcpy(dst, src, n);
+ i->stack_i += n;
+ return 0;
+}
diff --git a/src/interp/stackptr.c b/src/interp/stackptr.c
new file mode 100644
index 0000000..6b95e1d
--- /dev/null
+++ b/src/interp/stackptr.c
@@ -0,0 +1,16 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+
+void *nwp_interp_stackptr(const struct nw_interp *const i)
+{
+ return (char *)i->cfg.stack.buf + i->stack_i;
+}
diff --git a/src/interp/start.c b/src/interp/start.c
new file mode 100644
index 0000000..9d98ccb
--- /dev/null
+++ b/src/interp/start.c
@@ -0,0 +1,26 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+
+int nwp_interp_start(const struct nw_interp_cfg *const cfg,
+ const struct nwp_interp_set *const set,
+ struct nw_interp *const i)
+{
+ *i = (const struct nw_interp)
+ {
+ .cfg = *cfg,
+ .set = set
+ };
+
+ nwp_interp_resume(i);
+ return 0;
+}
diff --git a/src/io/CMakeLists.txt b/src/io/CMakeLists.txt
new file mode 100644
index 0000000..a9f97ca
--- /dev/null
+++ b/src/io/CMakeLists.txt
@@ -0,0 +1,18 @@
+# nanowasm, a tiny WebAssembly/Wasm interpreter
+# Copyright (C) 2023-2024 Xavier Del Campo Romero
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at https://mozilla.org/MPL/2.0/.
+
+target_sources(${PROJECT_NAME} PRIVATE
+ leb128.c
+ read.c
+ varint1.c
+ varint7.c
+ varint64.c
+ varuint1.c
+ varuint7.c
+ varuint32.c
+ varuint64.c
+)
diff --git a/src/io/leb128.c b/src/io/leb128.c
new file mode 100644
index 0000000..a57d5c7
--- /dev/null
+++ b/src/io/leb128.c
@@ -0,0 +1,47 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+enum nw_state nwp_leb128(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, const unsigned maxbits, const bool sign)
+{
+ uint8_t byte;
+
+ for (;;)
+ {
+ const int n = cfg->read(&byte, sizeof byte, cfg->user);
+
+ if (n < 0)
+ return NW_FATAL;
+ else if (!n)
+ return NW_AGAIN;
+
+ l->result |= (unsigned long long)(byte & 0x7f) << l->shift;
+ l->shift += 7;
+
+ if (!(byte & 0x80))
+ break;
+ else if (++l->bcnt > (maxbits + 7u - 1u) / 7u)
+ {
+ LOG("%s: overflow\n", __func__);
+ return NW_FATAL;
+ }
+ }
+
+ if (sign && (l->shift < maxbits) && (byte & 0x40))
+ l->result |= -1ll << l->shift;
+
+ return NW_OK;
+}
diff --git a/src/io/read.c b/src/io/read.c
new file mode 100644
index 0000000..2d64257
--- /dev/null
+++ b/src/io/read.c
@@ -0,0 +1,29 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <stddef.h>
+#include <stdint.h>
+
+enum nw_state nwp_io_read(const struct nw_io_cfg *const cfg,
+ struct nw_sm_io *const io)
+{
+ void *const buf = (uint8_t *)io->buf + io->read;
+ const size_t rem = io->n - io->read;
+ const int n = cfg->read(buf, rem, cfg->user);
+
+ if (n < 0)
+ return NW_FATAL;
+
+ if ((io->read += n) >= io->n)
+ return NW_OK;
+
+ return NW_AGAIN;
+}
diff --git a/src/io/varint1.c b/src/io/varint1.c
new file mode 100644
index 0000000..74a577f
--- /dev/null
+++ b/src/io/varint1.c
@@ -0,0 +1,27 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varint1(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varint1 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 1, true);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/io/varint64.c b/src/io/varint64.c
new file mode 100644
index 0000000..f0faa5b
--- /dev/null
+++ b/src/io/varint64.c
@@ -0,0 +1,27 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varint64(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varuint32 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 64, true);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/io/varint7.c b/src/io/varint7.c
new file mode 100644
index 0000000..77b9006
--- /dev/null
+++ b/src/io/varint7.c
@@ -0,0 +1,26 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varint7(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varint7 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 7, true);
+
+ if (ret)
+ return ret;
+
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ return ret;
+}
diff --git a/src/io/varuint1.c b/src/io/varuint1.c
new file mode 100644
index 0000000..3760f9d
--- /dev/null
+++ b/src/io/varuint1.c
@@ -0,0 +1,27 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varuint1(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varuint1 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 1, false);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/io/varuint32.c b/src/io/varuint32.c
new file mode 100644
index 0000000..40e2030
--- /dev/null
+++ b/src/io/varuint32.c
@@ -0,0 +1,27 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varuint32(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varuint32 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 32, false);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/io/varuint64.c b/src/io/varuint64.c
new file mode 100644
index 0000000..e01ce69
--- /dev/null
+++ b/src/io/varuint64.c
@@ -0,0 +1,27 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varuint64(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varuint64 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 64, false);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/io/varuint7.c b/src/io/varuint7.c
new file mode 100644
index 0000000..b20c00c
--- /dev/null
+++ b/src/io/varuint7.c
@@ -0,0 +1,27 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varuint7(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varuint7 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 7, false);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/load.c b/src/load.c
new file mode 100644
index 0000000..32d9ff3
--- /dev/null
+++ b/src/load.c
@@ -0,0 +1,21 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/types.h>
+
+enum nw_state nw_load(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+
+ if (cfg->eof(cfg->user))
+ return NW_OK;
+
+ return m->next(m);
+}
diff --git a/src/op/CMakeLists.txt b/src/op/CMakeLists.txt
new file mode 100644
index 0000000..cea137b
--- /dev/null
+++ b/src/op/CMakeLists.txt
@@ -0,0 +1,16 @@
+# nanowasm, a tiny WebAssembly/Wasm interpreter
+# Copyright (C) 2023-2024 Xavier Del Campo Romero
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at https://mozilla.org/MPL/2.0/.
+
+target_sources(${PROJECT_NAME} PRIVATE
+ end.c
+ i32_const.c
+ i32_load.c
+ nop.c
+ unreachable.c
+)
+
+add_subdirectory(check)
diff --git a/src/op/check/CMakeLists.txt b/src/op/check/CMakeLists.txt
new file mode 100644
index 0000000..810d3d6
--- /dev/null
+++ b/src/op/check/CMakeLists.txt
@@ -0,0 +1,19 @@
+# nanowasm, a tiny WebAssembly/Wasm interpreter
+# Copyright (C) 2023-2024 Xavier Del Campo Romero
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at https://mozilla.org/MPL/2.0/.
+
+target_sources(${PROJECT_NAME} PRIVATE
+ block.c
+ br_if.c
+ call.c
+ end.c
+ global_index.c
+ i32_const.c
+ i64_const.c
+ local_index.c
+ memory_immediate.c
+ no_immediate.c
+)
diff --git a/src/op/check/block.c b/src/op/check/block.c
new file mode 100644
index 0000000..c58c0f6
--- /dev/null
+++ b/src/op/check/block.c
@@ -0,0 +1,34 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/ops.h>
+
+static enum nw_state get_type(struct nw_mod *const m)
+{
+ nw_varint7 type;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varint7(cfg, l, &type);
+
+ if (n)
+ return n;
+
+ /* TODO: check block type. */
+ c->fn.blocks++;
+ m->next = m->sm.code.next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_block(struct nw_mod *const m)
+{
+ m->next = get_type;
+}
diff --git a/src/op/check/br_if.c b/src/op/check/br_if.c
new file mode 100644
index 0000000..6e6bd10
--- /dev/null
+++ b/src/op/check/br_if.c
@@ -0,0 +1,39 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state get_relative_depth(struct nw_mod *const m)
+{
+ nw_varuint32 relative_depth;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &relative_depth);
+
+ if (n)
+ return n;
+ else if (relative_depth >= c->fn.blocks)
+ {
+ LOG("%s: invalid relative depth %lu\n", __func__,
+ (unsigned long)relative_depth);
+ return NW_FATAL;
+ }
+
+ m->next = c->next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_br_if(struct nw_mod *const m)
+{
+ m->next = get_relative_depth;
+}
diff --git a/src/op/check/call.c b/src/op/check/call.c
new file mode 100644
index 0000000..eaa826f
--- /dev/null
+++ b/src/op/check/call.c
@@ -0,0 +1,39 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state get_index(struct nw_mod *const m)
+{
+ nw_varuint32 index;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &index);
+
+ if (n)
+ return n;
+ else if (index >= c->count + m->cfg.n_imports)
+ {
+ LOG("%s: invalid function index %lu\n", __func__,
+ (unsigned long)index);
+ return NW_FATAL;
+ }
+
+ m->next = m->sm.code.next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_call(struct nw_mod *const m)
+{
+ m->next = get_index;
+}
diff --git a/src/op/check/end.c b/src/op/check/end.c
new file mode 100644
index 0000000..7c614bb
--- /dev/null
+++ b/src/op/check/end.c
@@ -0,0 +1,35 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state run(struct nw_mod *const m)
+{
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_c_fn *const fn = &c->fn;
+
+ if (!fn->blocks)
+ {
+ LOG("%s: unexpected end in function %lu\n", __func__,
+ (unsigned long)c->entry_i);
+ return NW_FATAL;
+ }
+
+ fn->blocks--;
+ m->next = c->next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_end(struct nw_mod *const m)
+{
+ m->next = run;
+}
diff --git a/src/op/check/global_index.c b/src/op/check/global_index.c
new file mode 100644
index 0000000..5cfceb3
--- /dev/null
+++ b/src/op/check/global_index.c
@@ -0,0 +1,39 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state get_index(struct nw_mod *const m)
+{
+ nw_varuint32 index;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &index);
+
+ if (n)
+ return n;
+ else if (index >= m->global_count)
+ {
+ LOG("%s: invalid global index %lu\n", __func__,
+ (unsigned long)index);
+ return NW_FATAL;
+ }
+
+ m->next = c->next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_global_index(struct nw_mod *const m)
+{
+ m->next = get_index;
+}
diff --git a/src/op/check/i32_const.c b/src/op/check/i32_const.c
new file mode 100644
index 0000000..71a90b3
--- /dev/null
+++ b/src/op/check/i32_const.c
@@ -0,0 +1,32 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/ops.h>
+
+static enum nw_state get_value(struct nw_mod *const m)
+{
+ nw_varuint32 value;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &value);
+
+ if (n)
+ return n;
+
+ m->next = c->next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_i32_const(struct nw_mod *const m)
+{
+ m->next = get_value;
+}
diff --git a/src/op/check/i64_const.c b/src/op/check/i64_const.c
new file mode 100644
index 0000000..4d4d066
--- /dev/null
+++ b/src/op/check/i64_const.c
@@ -0,0 +1,32 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/ops.h>
+
+static enum nw_state get_value(struct nw_mod *const m)
+{
+ nw_varuint64 value;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint64(cfg, l, &value);
+
+ if (n)
+ return n;
+
+ m->next = c->next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_i64_const(struct nw_mod *const m)
+{
+ m->next = get_value;
+}
diff --git a/src/op/check/local_index.c b/src/op/check/local_index.c
new file mode 100644
index 0000000..1b991b1
--- /dev/null
+++ b/src/op/check/local_index.c
@@ -0,0 +1,43 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state get_index(struct nw_mod *const m)
+{
+ nw_varuint32 index;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &index);
+
+ if (n)
+ return n;
+#if 0
+ else if (index >= c->fn.local_total)
+ {
+ LOG("%s: invalid local index %lu in function %lu\n", __func__,
+ (unsigned long)index, (unsigned long)c->entry_i);
+ return NW_FATAL;
+ }
+#else
+#warning TODO
+#endif
+
+ m->next = c->next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_local_index(struct nw_mod *const m)
+{
+ m->next = get_index;
+}
diff --git a/src/op/check/memory_immediate.c b/src/op/check/memory_immediate.c
new file mode 100644
index 0000000..2232370
--- /dev/null
+++ b/src/op/check/memory_immediate.c
@@ -0,0 +1,47 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/ops.h>
+
+static enum nw_state get_offset(struct nw_mod *const m)
+{
+ nw_varuint32 offset;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &offset);
+
+ if (n)
+ return n;
+
+ m->next = m->sm.code.next;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_flags(struct nw_mod *const m)
+{
+ nw_varuint32 flags;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &flags);
+
+ if (n)
+ return n;
+
+ m->next = get_offset;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_memory_immediate(struct nw_mod *const m)
+{
+ m->next = get_flags;
+}
diff --git a/src/op/check/no_immediate.c b/src/op/check/no_immediate.c
new file mode 100644
index 0000000..cf1dd21
--- /dev/null
+++ b/src/op/check/no_immediate.c
@@ -0,0 +1,23 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/ops.h>
+
+static enum nw_state run(struct nw_mod *const m)
+{
+ m->next = m->sm.code.next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_no_immediate(struct nw_mod *const m)
+{
+ m->next = run;
+}
diff --git a/src/op/end.c b/src/op/end.c
new file mode 100644
index 0000000..8ec6e7c
--- /dev/null
+++ b/src/op/end.c
@@ -0,0 +1,24 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state run(struct nw_interp *const i)
+{
+ /* TODO: this is not correct. end can appear anywhere in a function. */
+ return NW_OK;
+}
+
+void nwp_op_end(struct nw_interp *const i)
+{
+ i->next = run;
+}
diff --git a/src/op/i32_const.c b/src/op/i32_const.c
new file mode 100644
index 0000000..dbd0e91
--- /dev/null
+++ b/src/op/i32_const.c
@@ -0,0 +1,39 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state get_value(struct nw_interp *const i)
+{
+ nw_varuint32 value;
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_sm_leb128 *const l = &i->sm.i32_const.leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &value);
+
+ if (n)
+ return n;
+ else if (nwp_interp_stack_push(i, &value, sizeof value))
+ {
+ LOG("%s: nwp_interp_stack_push failed\n", __func__);
+ return NW_FATAL;
+ }
+
+ nwp_interp_resume(i);
+ return NW_AGAIN;
+}
+
+void nwp_op_i32_const(struct nw_interp *const i)
+{
+ i->next = get_value;
+ i->sm.i32_const = (const struct nw_i_sm_i32_const){0};
+}
diff --git a/src/op/i32_load.c b/src/op/i32_load.c
new file mode 100644
index 0000000..615cfc4
--- /dev/null
+++ b/src/op/i32_load.c
@@ -0,0 +1,18 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+void nwp_op_i32_load(struct nw_interp *const i)
+{
+ /* TODO */
+}
diff --git a/src/op/nop.c b/src/op/nop.c
new file mode 100644
index 0000000..6924b9a
--- /dev/null
+++ b/src/op/nop.c
@@ -0,0 +1,23 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/ops.h>
+
+static enum nw_state run(struct nw_interp *const i)
+{
+ nwp_interp_resume(i);
+ return NW_AGAIN;
+}
+
+void nwp_op_nop(struct nw_interp *const i)
+{
+ i->next = run;
+}
diff --git a/src/op/unreachable.c b/src/op/unreachable.c
new file mode 100644
index 0000000..77b711b
--- /dev/null
+++ b/src/op/unreachable.c
@@ -0,0 +1,27 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state run(struct nw_interp *const i)
+{
+ static const char exc[] = "unreachable";
+
+ i->exception = exc;
+ LOG("%s: %s\n", __func__, exc);
+ return NW_FATAL;
+}
+
+void nwp_op_unreachable(struct nw_interp *const i)
+{
+ i->next = run;
+}
diff --git a/src/routines/CMakeLists.txt b/src/routines/CMakeLists.txt
new file mode 100644
index 0000000..3141a4e
--- /dev/null
+++ b/src/routines/CMakeLists.txt
@@ -0,0 +1,17 @@
+# nanowasm, a tiny WebAssembly/Wasm interpreter
+# Copyright (C) 2023-2024 Xavier Del Campo Romero
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at https://mozilla.org/MPL/2.0/.
+
+target_sources(${PROJECT_NAME} PRIVATE
+ call.c
+ check_magic.c
+ check_version.c
+ find_function.c
+ get_function_type.c
+ section.c
+)
+
+add_subdirectory(section)
diff --git a/src/routines/call.c b/src/routines/call.c
new file mode 100644
index 0000000..c04c4f8
--- /dev/null
+++ b/src/routines/call.c
@@ -0,0 +1,33 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state push_locals(struct nw_interp *const i)
+{
+ LOG("%s: TODO\n", __func__);
+ return NW_FATAL;
+}
+
+static enum nw_state find_function(struct nw_interp *const i)
+{
+ const struct nw_i_sm_type *const t = &i->sm.type;
+
+ nwp_find_function(i, t->index, push_locals);
+ return NW_AGAIN;
+}
+
+void nwp_call(struct nw_interp *const i, const nw_varuint32 index)
+{
+ nwp_get_function_type(i, index, find_function);
+}
diff --git a/src/routines/check_magic.c b/src/routines/check_magic.c
new file mode 100644
index 0000000..bb49cff
--- /dev/null
+++ b/src/routines/check_magic.c
@@ -0,0 +1,50 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <string.h>
+
+static enum nw_state run(struct nw_mod *const m)
+{
+ struct nw_sm_cm *const cm = &m->sm.check_magic;
+ const enum nw_state n = nwp_io_read(&m->cfg.io, &cm->io);
+
+ if (n)
+ return n;
+
+ static const uint8_t magic[] = {'\0', 'a', 's', 'm'};
+
+ if (memcmp(cm->buf, magic, sizeof cm->buf))
+ {
+ LOG("%s: wrong magic bytes\n", __func__);
+ return NW_FATAL;
+ }
+
+ nwp_check_version(m);
+ return NW_AGAIN;
+}
+
+void nwp_check_magic(struct nw_mod *const m)
+{
+ struct nw_sm_cm *const cm = &m->sm.check_magic;
+
+ *cm = (const struct nw_sm_cm)
+ {
+ .io =
+ {
+ .buf = &cm->buf,
+ .n = sizeof cm->buf
+ }
+ };
+
+ m->next = run;
+}
diff --git a/src/routines/check_version.c b/src/routines/check_version.c
new file mode 100644
index 0000000..9632054
--- /dev/null
+++ b/src/routines/check_version.c
@@ -0,0 +1,50 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <string.h>
+
+static enum nw_state run(struct nw_mod *const m)
+{
+ struct nw_sm_cv *const cv = &m->sm.check_version;
+ const enum nw_state n = nwp_io_read(&m->cfg.io, &cv->io);
+
+ if (n)
+ return n;
+
+ static const uint8_t version[] = {1, 0, 0, 0};
+
+ if (memcmp(&cv->version, version, sizeof cv->version))
+ {
+ LOG("%s: wrong version bytes\n", __func__);
+ return NW_FATAL;
+ }
+
+ nwp_section(m);
+ return NW_AGAIN;
+}
+
+void nwp_check_version(struct nw_mod *const m)
+{
+ struct nw_sm_cv *const cv = &m->sm.check_version;
+
+ *cv = (const struct nw_sm_cv)
+ {
+ .io =
+ {
+ .buf = &cv->version,
+ .n = sizeof cv->version
+ }
+ };
+
+ m->next = run;
+}
diff --git a/src/routines/find_func_type.c b/src/routines/find_func_type.c
new file mode 100644
index 0000000..aa11a51
--- /dev/null
+++ b/src/routines/find_func_type.c
@@ -0,0 +1,20 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+void nwp_find_func_type(struct nw_mod *const m, nw_varuint32
+ enum nw_state (*const next)(struct nw_interp *))
+{
+
+}
diff --git a/src/routines/find_function.c b/src/routines/find_function.c
new file mode 100644
index 0000000..805de82
--- /dev/null
+++ b/src/routines/find_function.c
@@ -0,0 +1,21 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+void nwp_find_function(struct nw_interp *const i, const nw_varuint32 index,
+ enum nw_state (*const next)(struct nw_interp *))
+{
+ /* TODO */
+}
diff --git a/src/routines/get_function_type.c b/src/routines/get_function_type.c
new file mode 100644
index 0000000..61f90f2
--- /dev/null
+++ b/src/routines/get_function_type.c
@@ -0,0 +1,256 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state get_form(struct nw_interp *);
+
+static enum nw_state push(struct nw_interp *const i)
+{
+ const struct nw_i_sm_type *const t = &i->sm.type;
+ const struct nw_i_sm_type_out *const out = &t->out;
+
+ if (nwp_interp_stack_push(i, out, sizeof *out))
+ {
+ LOG("%s: nwp_interp_stack_push failed\n", __func__);
+ return NW_FATAL;
+ }
+
+ i->next = t->next;
+ return NW_AGAIN;
+}
+
+static enum nw_state skip(struct nw_interp *const i)
+{
+ struct nw_i_sm_type *const t = &i->sm.type;
+
+ t->entry_i++;
+ t->param_i = 0;
+ i->next = get_form;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_return_type(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ nw_varint7 type;
+ const enum nw_state n = nwp_varint7(cfg, l, &type);
+
+ if (n)
+ return n;
+
+ t->out.return_type = type;
+ i->next = t->entry_i == t->type_index ? push : skip;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_return_count(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ nw_varuint1 return_count;
+ const enum nw_state n = nwp_varuint1(cfg, l, &return_count);
+
+ if (n)
+ return n;
+ else if (t->entry_i == t->type_index)
+ i->next = return_count ? get_return_type : push;
+ else
+ i->next = return_count ? get_return_type : skip;
+
+ t->out.return_count = return_count;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_param_type(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ nw_varint7 type;
+ const enum nw_state n = nwp_varint7(cfg, l, &type);
+
+ if (n)
+ return n;
+ else if (t->entry_i == t->type_index
+ && nwp_interp_stack_push(i, &type, sizeof type))
+ {
+ LOG("%s: nwp_interp_stack_push failed\n", __func__);
+ return NW_FATAL;
+ }
+ else if (++t->param_i >= t->out.param_count)
+ i->next = get_return_count;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_param_count(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ nw_varuint32 param_count;
+ const enum nw_state n = nwp_varuint32(cfg, l, &param_count);
+
+ if (n)
+ return n;
+
+ t->out.param_count = param_count;
+ i->next = param_count ? get_param_type : get_return_count;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_form(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ nw_varint7 form;
+ const enum nw_state n = nwp_varint7(cfg, l, &form);
+
+ if (n)
+ return n;
+ else if (t->entry_i == t->type_index && form != 0x60)
+ {
+ static const char exc[] = "type index not a function";
+
+ i->exception = exc;
+ LOG("%s: %s: %lu\n", __func__, exc, (unsigned long)t->entry_i);
+ return NW_FATAL;
+ }
+
+ t->out.form = form;
+ i->next = get_param_count;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_type_count(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &t->count);
+
+ if (n)
+ return n;
+ else if (t->count <= t->type_index)
+ {
+ static const char exc[] = "invalid type index";
+
+ i->exception = exc;
+ LOG("%s: %s: %lu\n", __func__, exc, (unsigned long)t->type_index);
+ return NW_FATAL;
+ }
+
+ t->entry_i = 0;
+ i->next = get_form;
+ return NW_AGAIN;
+}
+
+static enum nw_state seek_type(struct nw_interp *const i)
+{
+ const struct nw_interp_cfg *const icfg = &i->cfg;
+ const struct nw_io_cfg *const cfg = &icfg->io;
+ const long offset = icfg->m->sections[NW_SECTION_TYPE];
+
+ if (!offset)
+ {
+ static const char exc[] = "type section not found";
+
+ LOG("%s: %s\n", __func__, exc);
+ return NW_FATAL;
+ }
+
+ const enum nw_state n = cfg->seek(offset, NW_SEEK_SET, cfg->user);
+
+ if (n)
+ return n;
+
+ i->next = get_type_count;
+ return NW_AGAIN;
+}
+
+static enum nw_state find_entry(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &t->type_index);
+
+ if (n)
+ return n;
+ else if (t->entry_i++ == t->index)
+ i->next = seek_type;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &t->count);
+
+ if (n)
+ return n;
+ else if (t->count <= t->index)
+ {
+ static const char exc[] = "could not find function index";
+
+ i->exception = exc;
+ LOG("%s: %s: %lu\n", __func__, exc, (unsigned long)t->index);
+ return NW_FATAL;
+ }
+
+ i->next = find_entry;
+ return NW_AGAIN;
+}
+
+static enum nw_state seek_fn(struct nw_interp *const i)
+{
+ const struct nw_interp_cfg *const icfg = &i->cfg;
+ const struct nw_io_cfg *const cfg = &icfg->io;
+ const long offset = icfg->m->sections[NW_SECTION_FUNCTION];
+
+ if (!offset)
+ {
+ static const char exc[] = "type section not found";
+
+ LOG("%s: %s\n", __func__, exc);
+ return NW_FATAL;
+ }
+
+ const enum nw_state n = cfg->seek(offset, NW_SEEK_SET, cfg->user);
+
+ if (n)
+ return n;
+
+ i->next = get_count;
+ return NW_AGAIN;
+}
+
+void nwp_get_function_type(struct nw_interp *const i, const nw_varuint32 index,
+ enum nw_state (*const next)(struct nw_interp *))
+{
+ i->next = seek_fn;
+ i->sm.type = (const struct nw_i_sm_type)
+ {
+ .index = index,
+ .next = next
+ };
+}
diff --git a/src/routines/section.c b/src/routines/section.c
new file mode 100644
index 0000000..7ed8899
--- /dev/null
+++ b/src/routines/section.c
@@ -0,0 +1,94 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static void (*const fn[])(struct nw_mod *) =
+{
+ nwp_section_custom,
+ nwp_section_type,
+ nwp_section_import,
+ nwp_section_function,
+ nwp_section_table,
+ nwp_section_memory,
+ nwp_section_global,
+ nwp_section_export,
+ nwp_section_start,
+ nwp_section_element,
+ nwp_section_code,
+ nwp_section_data,
+};
+
+static enum nw_state get_offset(struct nw_mod *const m)
+{
+ struct nw_mod_section *const s = &m->section;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->tell(&s->offset, cfg->user);
+ long *const offset = &m->sections[s->section];
+
+ if (n)
+ return n;
+ /* Custom section can appear more than once. */
+ else if (s->section != NW_SECTION_CUSTOM && *offset)
+ {
+ LOG("%s: ignoring duplicate section %hhu\n", __func__,
+ (unsigned char)s->section);
+ return nwp_section_skip(m);
+ }
+
+ *offset = s->offset;
+ fn[s->section](m);
+ return NW_AGAIN;
+}
+
+static enum nw_state get_length(struct nw_mod *const m)
+{
+ struct nw_mod_section *const s = &m->section;
+ struct nw_sm_leb128 *const l = &s->leb128;
+ const enum nw_state n = nwp_varuint32(&m->cfg.io, l, &s->len);
+
+ if (n)
+ return n;
+
+ m->next = get_offset;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_section(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_mod_section *const s = &m->section;
+ struct nw_sm_leb128 *const l = &s->leb128;
+ const enum nw_state n = nwp_varuint7(cfg, l, &s->section);
+
+ if (n)
+ {
+ if (cfg->eof(cfg->user))
+ return NW_OK;
+
+ return n;
+ }
+ else if (s->section >= sizeof fn / sizeof *fn)
+ {
+ LOG("%s: invalid section %u\n", __func__, (unsigned)s->section);
+ return NW_FATAL;
+ }
+
+ m->next = get_length;
+ return NW_AGAIN;
+}
+
+void nwp_section(struct nw_mod *const m)
+{
+ m->section = (const struct nw_mod_section){0};
+ m->next = get_section;
+}
diff --git a/src/routines/section/CMakeLists.txt b/src/routines/section/CMakeLists.txt
new file mode 100644
index 0000000..7561069
--- /dev/null
+++ b/src/routines/section/CMakeLists.txt
@@ -0,0 +1,23 @@
+# nanowasm, a tiny WebAssembly/Wasm interpreter
+# Copyright (C) 2023-2024 Xavier Del Campo Romero
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at https://mozilla.org/MPL/2.0/.
+
+target_sources(${PROJECT_NAME} PRIVATE
+ code.c
+ custom.c
+ data.c
+ element.c
+ exit.c
+ export.c
+ function.c
+ global.c
+ import.c
+ memory.c
+ skip.c
+ start.c
+ table.c
+ type.c
+)
diff --git a/src/routines/section/code.c b/src/routines/section/code.c
new file mode 100644
index 0000000..dd50c41
--- /dev/null
+++ b/src/routines/section/code.c
@@ -0,0 +1,271 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/opcodes.h>
+#include <nw/ops.h>
+#include <nw/routines.h>
+#include <inttypes.h>
+
+static enum nw_state local_loop(struct nw_mod *);
+static enum nw_state get_opcode(struct nw_mod *);
+static enum nw_state entry_loop(struct nw_mod *);
+
+static void (*const checks[])(struct nw_mod *) =
+{
+ [OP_UNREACHABLE] = nwp_op_check_no_immediate,
+ [OP_NOP] = nwp_op_check_no_immediate,
+ [OP_END] = nwp_op_check_end,
+ [OP_BLOCK] = nwp_op_check_block,
+ [OP_BR_IF] = nwp_op_check_br_if,
+ [OP_RETURN] = nwp_op_check_no_immediate,
+ [OP_CALL] = nwp_op_check_call,
+ [OP_GET_LOCAL] = nwp_op_check_local_index,
+ [OP_SET_LOCAL] = nwp_op_check_local_index,
+ [OP_TEE_LOCAL] = nwp_op_check_local_index,
+ [OP_GET_GLOBAL] = nwp_op_check_global_index,
+ [OP_SET_GLOBAL] = nwp_op_check_global_index,
+ [OP_I32_LOAD] = nwp_op_check_memory_immediate,
+ [OP_I32_STORE] = nwp_op_check_memory_immediate,
+ [OP_I64_STORE] = nwp_op_check_memory_immediate,
+ [OP_F32_STORE] = nwp_op_check_memory_immediate,
+ [OP_F64_STORE] = nwp_op_check_memory_immediate,
+ [OP_I32_CONST] = nwp_op_check_i32_const,
+ [OP_I64_CONST] = nwp_op_check_i64_const,
+ [OP_I32_SUB] = nwp_op_check_no_immediate
+};
+
+static enum nw_state get_post_offset(struct nw_mod *const m)
+{
+ long offset;
+ struct nw_sm_c *const c = &m->sm.code;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->tell(&offset, cfg->user);
+
+ if (n)
+ return n;
+
+ const unsigned long consumed = offset - c->op_off;
+
+ if (consumed && consumed >= c->rem)
+ {
+ LOG("%s: parameters exceed expected body length\n", __func__);
+ return NW_FATAL;
+ }
+
+ c->rem -= consumed;
+ m->next = get_opcode;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_op_offset(struct nw_mod *const m)
+{
+ struct nw_sm_c *const c = &m->sm.code;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->tell(&c->op_off, cfg->user);
+ void (*const fn)(struct nw_mod *) = checks[c->op];
+
+ if (n)
+ return n;
+ else if (!fn)
+ {
+ LOG("%s: invalid opcode %#" PRIx8 "\n", __func__, c->op);
+ return NW_FATAL;
+ }
+
+ c->next = get_post_offset;
+ fn(m);
+ return NW_AGAIN;
+}
+
+static enum nw_state check_exit(struct nw_mod *const m)
+{
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_c_fn *const fn = &c->fn;
+
+ if (fn->blocks)
+ {
+ LOG("%s: mismatched number of blocks in function %lu\n", __func__,
+ (unsigned long)c->entry_i);
+ return NW_FATAL;
+ }
+
+ c->entry_i++;
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_opcode(struct nw_mod *const m)
+{
+ struct nw_sm_c *const c = &m->sm.code;
+
+ if (!c->rem)
+ return check_exit(m);
+
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &c->op, .n = sizeof c->op};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+ else if (c->rem == 1 && c->op != OP_END)
+ {
+ LOG("%s: unexpected opcode %#" PRIx8 " at body end\n",
+ __func__, c->op);
+ return NW_FATAL;
+ }
+ else if (c->op >= sizeof checks / sizeof *checks)
+ {
+ LOG("%s: invalid opcode %#" PRIx8 "\n", __func__, c->op);
+ return NW_FATAL;
+ }
+
+ c->rem--;
+ m->next = get_op_offset;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_rem(struct nw_mod *const m)
+{
+ long offset;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->tell(&offset, cfg->user);
+
+ if (n)
+ return n;
+
+ struct nw_sm_c *const c = &m->sm.code;
+ const unsigned long consumed = offset - c->start;
+
+ if (consumed >= c->body_size)
+ {
+ LOG("%s: exceeded function body size\n", __func__);
+ return NW_FATAL;
+ }
+
+ c->rem = c->body_size - consumed;
+ m->next = get_opcode;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_type(struct nw_mod *const m)
+{
+ nw_varint7 type;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varint7(cfg, l, &type);
+
+ if (n)
+ return n;
+
+ c->fn.local_i++;
+ m->next = local_loop;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_group_count(struct nw_mod *const m)
+{
+ nw_varuint32 group_count;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &group_count);
+
+ if (n)
+ return n;
+
+ c->fn.local_total += group_count;
+ m->next = get_type;
+ return NW_AGAIN;
+}
+
+static enum nw_state local_loop(struct nw_mod *const m)
+{
+ const struct nw_sm_c_fn *const fn = &m->sm.code.fn;
+
+ m->next = fn->local_i < fn->local_count ? get_group_count : get_rem;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_local_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_c_fn *const fn = &c->fn;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &fn->local_count);
+
+ if (n)
+ return n;
+
+ m->next = fn->local_count ? local_loop : get_rem;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_start(struct nw_mod *const m)
+{
+ struct nw_sm_c *const c = &m->sm.code;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->tell(&c->start, cfg->user);
+
+ if (n)
+ return n;
+
+ m->next = get_local_count;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_body_size(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &c->body_size);
+
+ if (n)
+ return n;
+
+ m->next = get_start;
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_mod *const m)
+{
+ struct nw_sm_c *const c = &m->sm.code;
+
+ c->fn = (const struct nw_sm_c_fn){.blocks = 1};
+ m->next = c->entry_i < c->count ? get_body_size : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &c->count);
+
+ if (n)
+ return n;
+
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+void nwp_section_code(struct nw_mod *const m)
+{
+ struct nw_sm_c *const c = &m->sm.code;
+
+ m->next = get_count;
+ *c = (const struct nw_sm_c){0};
+}
diff --git a/src/routines/section/custom.c b/src/routines/section/custom.c
new file mode 100644
index 0000000..22b93b0
--- /dev/null
+++ b/src/routines/section/custom.c
@@ -0,0 +1,17 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/routines.h>
+
+void nwp_section_custom(struct nw_mod *const m)
+{
+ m->next = nwp_section_skip;
+}
diff --git a/src/routines/section/data.c b/src/routines/section/data.c
new file mode 100644
index 0000000..a4aba88
--- /dev/null
+++ b/src/routines/section/data.c
@@ -0,0 +1,19 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+void nwp_section_data(struct nw_mod *const m)
+{
+ /* TODO */
+}
diff --git a/src/routines/section/element.c b/src/routines/section/element.c
new file mode 100644
index 0000000..08265dc
--- /dev/null
+++ b/src/routines/section/element.c
@@ -0,0 +1,19 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+void nwp_section_element(struct nw_mod *const m)
+{
+ /* TODO */
+}
diff --git a/src/routines/section/exit.c b/src/routines/section/exit.c
new file mode 100644
index 0000000..db380ef
--- /dev/null
+++ b/src/routines/section/exit.c
@@ -0,0 +1,36 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+enum nw_state nwp_section_exit(struct nw_mod *const m)
+{
+ long offset;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->tell(&offset, cfg->user);
+
+ if (n)
+ return n;
+
+ const struct nw_mod_section *const s = &m->section;
+ const unsigned long size = offset - s->offset;
+
+ if (size != s->len)
+ {
+ LOG("%s: size exceeded (%lu expected, got %lu)\n",
+ __func__, (unsigned long)s->len, size);
+ return NW_FATAL;
+ }
+
+ nwp_section(m);
+ return NW_AGAIN;
+}
diff --git a/src/routines/section/export.c b/src/routines/section/export.c
new file mode 100644
index 0000000..a497db9
--- /dev/null
+++ b/src/routines/section/export.c
@@ -0,0 +1,113 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state entry_loop(struct nw_mod *m);
+
+static enum nw_state get_index(struct nw_mod *const m)
+{
+ nw_varuint32 index;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_exp *const e = &m->sm.export;
+ struct nw_sm_leb128 *const l = &e->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &index);
+
+ if (n)
+ return n;
+
+ e->entry_i++;
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_kind(struct nw_mod *const m)
+{
+ uint8_t kind;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &kind, .n = sizeof kind};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+
+ /* TODO: check kind */
+ m->next = get_index;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_name(struct nw_mod *const m)
+{
+ uint8_t b;
+ struct nw_sm_exp *const e = &m->sm.export;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &b, .n = sizeof b};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+ else if (++e->len_i >= e->field_len)
+ {
+ e->len_i = 0;
+ m->next = get_kind;
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_len(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_exp *const e = &m->sm.export;
+ struct nw_sm_leb128 *const l = &e->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &e->field_len);
+
+ if (n)
+ return n;
+ else if (!e->field_len)
+ {
+ LOG("%s: invalid field length\n", __func__);
+ return NW_FATAL;
+ }
+
+ m->next = get_name;
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_mod *const m)
+{
+ const struct nw_sm_exp *const e = &m->sm.export;
+
+ m->next = e->entry_i < e->count ? get_len : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_exp *const e = &m->sm.export;
+ struct nw_sm_leb128 *const l = &e->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &e->count);
+
+ if (n)
+ return n;
+
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+void nwp_section_export(struct nw_mod *const m)
+{
+ m->next = get_count;
+ m->sm.export = (const struct nw_sm_exp){0};
+}
diff --git a/src/routines/section/function.c b/src/routines/section/function.c
new file mode 100644
index 0000000..3402df3
--- /dev/null
+++ b/src/routines/section/function.c
@@ -0,0 +1,49 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state get_entry(struct nw_mod *const m)
+{
+ struct nw_sm_fn *const fn = &m->sm.function;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_leb128 *const l = &fn->leb128;
+ nw_varuint32 type;
+ const enum nw_state n = nwp_varuint32(cfg, l, &type);
+
+ if (n)
+ return n;
+ else if (++fn->entry_i >= fn->count)
+ m->next = nwp_section_exit;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_fn *const fn = &m->sm.function;
+ struct nw_sm_leb128 *const l = &fn->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &fn->count);
+
+ if (n)
+ return n;
+
+ m->next = get_entry;
+ return NW_AGAIN;
+}
+
+void nwp_section_function(struct nw_mod *const m)
+{
+ m->next = get_count;
+ m->sm.function = (const struct nw_sm_fn){0};
+}
diff --git a/src/routines/section/global.c b/src/routines/section/global.c
new file mode 100644
index 0000000..788d947
--- /dev/null
+++ b/src/routines/section/global.c
@@ -0,0 +1,144 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state loop(struct nw_mod *const m)
+{
+ struct nw_sm_gl *const gl = &m->sm.global;
+ const enum nw_state n = nwp_interp_run(&gl->interp);
+
+ if (n)
+ return n;
+
+ m->next = nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_global(struct nw_mod *const m)
+{
+ struct nw_sm_gl *const gl = &m->sm.global;
+ const struct nw_interp_cfg cfg =
+ {
+ .stack =
+ {
+ .buf = &gl->value,
+ .n = sizeof gl->value
+ },
+
+ .m = m,
+ .io = m->cfg.io
+ };
+
+ if (nwp_interp_start(&cfg, &nwp_interp_initexpr_set, &gl->interp))
+ {
+ LOG("%s: nw_interp_start failed\n", __func__);
+ return NW_FATAL;
+ }
+
+ m->next = loop;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_mutability(struct nw_mod *const m)
+{
+ nw_varuint1 mutability;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_gl *const gl = &m->sm.global;
+ struct nw_sm_leb128 *const l = &gl->leb128;
+ const enum nw_state n = nwp_varuint1(cfg, l, &mutability);
+
+ if (n)
+ return n;
+
+ m->next = get_global;
+ return NW_AGAIN;
+}
+
+static int get_type(const nw_varint7 content_type, enum nw_type *const out)
+{
+ static const struct type
+ {
+ nw_varint7 key;
+ enum nw_type value;
+ } list[] =
+ {
+ {.key = 0x7f, .value = NW_TYPE_I32},
+ {.key = 0x7e, .value = NW_TYPE_I64},
+ {.key = 0x7d, .value = NW_TYPE_F32},
+ {.key = 0x7c, .value = NW_TYPE_F64}
+ };
+
+ for (size_t i = 0; i < sizeof list / sizeof *list; i++)
+ {
+ const struct type *const t = &list[i];
+
+ if (content_type == t->key)
+ {
+ *out = t->value;
+ return 0;
+ }
+ }
+
+ LOG("%s: invalid type: %#hhx\n", __func__, (char)content_type);
+ return -1;
+}
+
+static enum nw_state get_content_type(struct nw_mod *const m)
+{
+ nw_varint7 content_type;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_gl *const gl = &m->sm.global;
+ struct nw_sm_leb128 *const l = &gl->leb128;
+ const enum nw_state n = nwp_varint7(cfg, l, &content_type);
+
+ if (n)
+ return n;
+ else if (get_type(content_type, &gl->type))
+ {
+ LOG("%s: get_type failed\n", __func__);
+ return NW_FATAL;
+ }
+
+ m->next = get_mutability;
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_mod *const m)
+{
+ const struct nw_sm_gl *const gl = &m->sm.global;
+
+ m->next = gl->entry_i < m->global_count ?
+ get_content_type : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_gl *const gl = &m->sm.global;
+ struct nw_sm_leb128 *const l = &gl->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &m->global_count);
+
+ if (n)
+ return n;
+
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+void nwp_section_global(struct nw_mod *const m)
+{
+ m->next = get_count;
+ m->sm.global = (const struct nw_sm_gl){0};
+}
diff --git a/src/routines/section/import.c b/src/routines/section/import.c
new file mode 100644
index 0000000..1ea54eb
--- /dev/null
+++ b/src/routines/section/import.c
@@ -0,0 +1,308 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <string.h>
+
+static enum nw_state entry_loop(struct nw_mod *);
+static enum nw_state find_import(struct nw_mod *);
+
+static enum nw_state get_function_type(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ struct nw_sm_leb128 *const l = &imp->leb128;
+ nw_varuint32 type;
+ const enum nw_state n = nwp_varuint32(cfg, l, &type);
+
+ if (n)
+ return n;
+
+ m->next = nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_kind(struct nw_mod *const m)
+{
+ uint8_t kind;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &kind, .n = sizeof kind};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
+
+ if (kind != i->kind)
+ {
+ m->next = find_import;
+ imp->imp_i++;
+ }
+ /* TODO: process every import type. */
+ else if (kind != NW_KIND_FUNCTION)
+ m->next = nwp_section_exit;
+ else
+ m->next = get_function_type;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state compare_field(struct nw_mod *const m)
+{
+ uint8_t byte;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &byte, .n = sizeof byte};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
+
+ if (byte != i->field[imp->len_i])
+ {
+ m->next = find_import;
+ imp->imp_i++;
+ imp->len_i = 0;
+ }
+ else if (++imp->len_i >= imp->field_len)
+ {
+ m->next = get_kind;
+ imp->len_i = 0;
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_field_offset(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const enum nw_state n = cfg->tell(&imp->field_off, cfg->user);
+ const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
+
+ if (n)
+ return n;
+ else if (imp->field_len != strlen(i->field))
+ {
+ m->next = find_import;
+ imp->imp_i++;
+ }
+ else
+ m->next = compare_field;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_field_len(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ struct nw_sm_leb128 *const l = &imp->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &imp->field_len);
+
+ if (n)
+ return n;
+ else
+ m->next = get_field_offset;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state compare_name(struct nw_mod *const m)
+{
+ uint8_t byte;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &byte, .n = sizeof byte};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
+
+ if (byte != i->module[imp->len_i])
+ {
+ m->next = find_import;
+ imp->imp_i++;
+ imp->len_i = 0;
+ }
+ else if (++imp->len_i >= imp->mod_len)
+ {
+ m->next = get_field_len;
+ imp->len_i = 0;
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state dump_import_field_name(struct nw_mod *const m)
+{
+ uint8_t byte;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &byte, .n = sizeof byte};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+
+ LOG("%c", (char)byte);
+
+ struct nw_sm_imp *const imp = &m->sm.import;
+
+ if (++imp->len_i >= imp->field_len)
+ {
+ LOG("\n");
+ return NW_FATAL;
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state rewind_to_field_name(struct nw_mod *const m)
+{
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->seek(imp->field_off, NW_SEEK_SET, cfg->user);
+
+ if (n)
+ return n;
+
+ m->next = dump_import_field_name;
+ return NW_AGAIN;
+}
+
+static enum nw_state dump_import_mod_name(struct nw_mod *const m)
+{
+ uint8_t byte;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &byte, .n = sizeof byte};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+
+ LOG("%c", (char)byte);
+
+ struct nw_sm_imp *const imp = &m->sm.import;
+
+ if (++imp->len_i >= imp->mod_len)
+ {
+ imp->len_i = 0;
+
+ if (imp->field_off)
+ {
+ LOG("::");
+ m->next = rewind_to_field_name;
+ }
+ else
+ {
+ LOG("\n");
+ return NW_FATAL;
+ }
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state rewind_to_mod_name(struct nw_mod *const m)
+{
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->seek(imp->mod_off, NW_SEEK_SET, cfg->user);
+
+ if (n)
+ return n;
+
+ m->next = dump_import_mod_name;
+ return NW_AGAIN;
+}
+
+static enum nw_state find_import(struct nw_mod *const m)
+{
+ struct nw_sm_imp *const imp = &m->sm.import;
+
+ if (imp->imp_i >= m->cfg.n_imports)
+ {
+ LOG("%s: required import: ", __func__);
+ m->next = rewind_to_mod_name;
+ return NW_AGAIN;
+ }
+
+ const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
+
+ if (imp->mod_len == strlen(i->module))
+ m->next = compare_name;
+ else
+ imp->imp_i++;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_module_offset(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const enum nw_state n = cfg->tell(&imp->mod_off, cfg->user);
+
+ if (n)
+ return n;
+
+ m->next = find_import;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_module_len(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ struct nw_sm_leb128 *const l = &imp->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &imp->mod_len);
+
+ if (n)
+ return n;
+
+ m->next = get_module_offset;
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_mod *const m)
+{
+ const struct nw_sm_imp *const imp = &m->sm.import;
+
+ m->next = imp->entry_i < imp->count ? get_module_len : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ struct nw_sm_leb128 *const l = &imp->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &imp->count);
+
+ if (n)
+ return n;
+
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+void nwp_section_import(struct nw_mod *const m)
+{
+ m->next = get_count;
+ m->sm.import = (const struct nw_sm_imp){0};
+}
diff --git a/src/routines/section/memory.c b/src/routines/section/memory.c
new file mode 100644
index 0000000..2dd6bbe
--- /dev/null
+++ b/src/routines/section/memory.c
@@ -0,0 +1,85 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state get_maximum(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_mem *const mem = &m->sm.memory;
+ struct nw_sm_leb128 *const l = &mem->leb128;
+ nw_varuint32 maximum;
+ const enum nw_state n = nwp_varuint32(cfg, l, &maximum);
+
+ if (n)
+ return n;
+
+ m->next = nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_initial(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_mem *const mem = &m->sm.memory;
+ struct nw_sm_leb128 *const l = &mem->leb128;
+ nw_varuint32 initial;
+ const enum nw_state n = nwp_varuint32(cfg, l, &initial);
+
+ if (n)
+ return n;
+
+ m->next = mem->flags ? get_maximum : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_flags(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_mem *const mem = &m->sm.memory;
+ struct nw_sm_leb128 *const l = &mem->leb128;
+ const enum nw_state n = nwp_varuint1(cfg, l, &mem->flags);
+
+ if (n)
+ return n;
+
+ m->next = get_initial;
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_mod *const m)
+{
+ const struct nw_sm_mem *const mem = &m->sm.memory;
+
+ m->next = mem->entry_i < mem->count ? get_flags : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_mem *const mem = &m->sm.memory;
+ struct nw_sm_leb128 *const l = &mem->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &mem->count);
+
+ if (n)
+ return n;
+
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+void nwp_section_memory(struct nw_mod *const m)
+{
+ m->next = get_count;
+ m->sm.memory = (const struct nw_sm_mem){0};
+}
diff --git a/src/routines/section/skip.c b/src/routines/section/skip.c
new file mode 100644
index 0000000..37612a6
--- /dev/null
+++ b/src/routines/section/skip.c
@@ -0,0 +1,26 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+enum nw_state nwp_section_skip(struct nw_mod *const m)
+{
+ const struct nw_mod_section *const s = &m->section;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->seek(s->len, NW_SEEK_CUR, cfg->user);
+
+ if (n)
+ return n;
+
+ nwp_section(m);
+ return NW_AGAIN;
+}
diff --git a/src/routines/section/start.c b/src/routines/section/start.c
new file mode 100644
index 0000000..dc50bee
--- /dev/null
+++ b/src/routines/section/start.c
@@ -0,0 +1,35 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state get_index(struct nw_mod *const m)
+{
+ nw_varuint32 index;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_start *const st = &m->sm.start;
+ struct nw_sm_leb128 *const l = &st->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &index);
+
+ if (n)
+ return n;
+
+ m->next = nwp_section_exit;
+ return NW_AGAIN;
+}
+
+void nwp_section_start(struct nw_mod *const m)
+{
+ m->next = get_index;
+ m->sm.start = (const struct nw_sm_start){0};
+}
diff --git a/src/routines/section/table.c b/src/routines/section/table.c
new file mode 100644
index 0000000..3dec836
--- /dev/null
+++ b/src/routines/section/table.c
@@ -0,0 +1,111 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state get_maximum(struct nw_mod *const m)
+{
+ nw_varuint32 maximum;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_tb *const tb = &m->sm.table;
+ struct nw_sm_leb128 *const l = &tb->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &maximum);
+
+ if (n)
+ return n;
+
+ m->next = nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_initial(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_tb *const tb = &m->sm.table;
+ struct nw_sm_leb128 *const l = &tb->leb128;
+ nw_varuint32 initial;
+ const enum nw_state n = nwp_varuint32(cfg, l, &initial);
+
+ if (n)
+ return n;
+
+ m->next = tb->flags ? get_maximum : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_flags(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_tb *const tb = &m->sm.table;
+ struct nw_sm_leb128 *const l = &tb->leb128;
+ const enum nw_state n = nwp_varuint1(cfg, l, &tb->flags);
+
+ if (n)
+ return n;
+
+ m->next = get_initial;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_element_type(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_tb *const tb = &m->sm.table;
+ struct nw_sm_leb128 *const l = &tb->leb128;
+ const enum nw_state n = nwp_varint7(cfg, l, &tb->elem_type);
+
+ if (n)
+ return n;
+ /* TODO: replace with enum? */
+ else if (tb->elem_type != 0x70)
+ {
+ LOG("%s: unexpected elem_type %hhx\n", __func__, (char)tb->elem_type);
+ return NW_FATAL;
+ }
+
+ m->next = get_flags;
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_mod *const m)
+{
+ const struct nw_sm_tb *const tb = &m->sm.table;
+
+ m->next = tb->entry_i < tb->count ? get_element_type : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_tb *const tb = &m->sm.table;
+ struct nw_sm_leb128 *const l = &tb->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &tb->count);
+
+ if (n)
+ return n;
+ else if (tb->count > 1u)
+ {
+ LOG("%s: got %lu tables, but only 1 supported by the MVP\n", __func__,
+ (unsigned long)tb->count);
+ return NW_FATAL;
+ }
+
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+void nwp_section_table(struct nw_mod *const m)
+{
+ m->next = get_count;
+ m->sm.table = (const struct nw_sm_tb){0};
+}
diff --git a/src/routines/section/type.c b/src/routines/section/type.c
new file mode 100644
index 0000000..052586a
--- /dev/null
+++ b/src/routines/section/type.c
@@ -0,0 +1,159 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <stddef.h>
+
+static enum nw_state entry_loop(struct nw_mod *);
+
+static enum nw_state get_return_type(struct nw_mod *const m)
+{
+ struct nw_sm_st *const st = &m->sm.type;
+ struct nw_sm_leb128 *const l = &st->leb128;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ nw_varint7 return_type;
+ const enum nw_state n = nwp_varint7(cfg, l, &return_type);
+
+ if (n)
+ return n;
+
+ m->next = entry_loop;
+ st->entry_i++;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_return_count(struct nw_mod *const m)
+{
+ struct nw_sm_st *const st = &m->sm.type;
+ struct nw_sm_leb128 *const l = &st->leb128;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ nw_varuint1 return_count;
+ const enum nw_state n = nwp_varuint1(cfg, l, &return_count);
+
+ if (n)
+ return n;
+ else if (return_count)
+ m->next = get_return_type;
+ else
+ {
+ m->next = entry_loop;
+ st->entry_i++;
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state param_loop(struct nw_mod *const m)
+{
+ struct nw_sm_st *const st = &m->sm.type;
+
+ if (st->p_i >= st->param_count)
+ {
+ m->next = get_return_count;
+ return NW_AGAIN;
+ }
+
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_leb128 *const l = &st->leb128;
+ nw_varint7 type;
+ const enum nw_state n = nwp_varint7(cfg, l, &type);
+
+ if (n)
+ return n;
+
+ st->p_i++;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_param_count(struct nw_mod *const m)
+{
+ struct nw_sm_st *const st = &m->sm.type;
+ struct nw_sm_leb128 *const l = &st->leb128;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = nwp_varuint32(cfg, l, &st->param_count);
+
+ if (n)
+ return n;
+
+ st->p_i = 0;
+ m->next = param_loop;
+ return NW_AGAIN;
+}
+
+static int check_form(const nw_varint7 form)
+{
+ static const nw_varint7 v[] =
+ {
+ /* TODO: move values to enums? */
+ 0x7f, /* i32 */
+ 0x7e, /* i64 */
+ 0x7d, /* f32 */
+ 0x7c, /* f64 */
+ 0x70, /* anyfunc */
+ 0x60, /* func */
+ 0x40 /* empty block_type */
+ };
+
+ for (size_t i = 0; i < sizeof v / sizeof *v; i++)
+ if (form == v[i])
+ return 0;
+
+ return -1;
+}
+
+static enum nw_state get_form(struct nw_mod *const m)
+{
+ struct nw_sm_st *const st = &m->sm.type;
+ struct nw_sm_leb128 *const l = &st->leb128;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ nw_varint7 form;
+ const enum nw_state n = nwp_varint7(cfg, l, &form);
+
+ if (n)
+ return n;
+ else if (check_form(form))
+ {
+ LOG("%s: invalid form %#hhx\n", __func__, (char)l->result);
+ return NW_FATAL;
+ }
+
+ m->next = get_param_count;
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_mod *const m)
+{
+ struct nw_sm_st *const st = &m->sm.type;
+
+ m->next = st->entry_i < st->count ? get_form : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_st *const st = &m->sm.type;
+ struct nw_sm_leb128 *const l = &st->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &st->count);
+
+ if (n)
+ return n;
+
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+void nwp_section_type(struct nw_mod *const m)
+{
+ m->next = get_count;
+ m->sm.type = (const struct nw_sm_st){0};
+}
diff --git a/src/run.c b/src/run.c
new file mode 100644
index 0000000..f69134b
--- /dev/null
+++ b/src/run.c
@@ -0,0 +1,16 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+
+enum nw_state nw_run(struct nw_inst *const i)
+{
+ return nwp_interp_run(&i->interp);
+}
diff --git a/src/start.c b/src/start.c
new file mode 100644
index 0000000..453c62b
--- /dev/null
+++ b/src/start.c
@@ -0,0 +1,45 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state push(struct nw_interp *const i)
+{
+ const struct nw_i_sm_exp *const e = &i->sm.export;
+
+ if (e->kind != NW_KIND_FUNCTION)
+ {
+ static const char exc[] = "unexpected import kind";
+
+ i->exception = exc;
+ LOG("%s: %s: %d\n", __func__, exc, e->kind);
+ return NW_FATAL;
+ }
+
+ nwp_call(i, e->index);
+ return NW_AGAIN;
+}
+
+int nw_start(const struct nw_inst_cfg *const icfg, struct nw_inst *const i)
+{
+ *i = (const struct nw_inst){0};
+
+ if (nwp_interp_start(&icfg->interp_cfg, NULL, &i->interp))
+ {
+ LOG("%s: nwp_interp_start failed\n", __func__);
+ return -1;
+ }
+
+ nwp_find_export(&i->interp, "_start", push);
+ return 0;
+}