aboutsummaryrefslogtreecommitdiff
path: root/src/routines
diff options
context:
space:
mode:
Diffstat (limited to 'src/routines')
-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
23 files changed, 1956 insertions, 0 deletions
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};
+}