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