aboutsummaryrefslogtreecommitdiff
path: root/src/routines/init_data.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-09-07 00:04:38 +0200
committerXavier Del Campo Romero <xavi92@disroot.org>2025-11-06 14:38:40 +0100
commit6d9d80362f9932bbc87e162b8ef7df06c73e27e1 (patch)
treee3e228c63fe26f07503f226de7fb5086b3dc2286 /src/routines/init_data.c
downloadnanowasm-6d9d80362f9932bbc87e162b8ef7df06c73e27e1.tar.gz
First commit
Diffstat (limited to 'src/routines/init_data.c')
-rw-r--r--src/routines/init_data.c196
1 files changed, 196 insertions, 0 deletions
diff --git a/src/routines/init_data.c b/src/routines/init_data.c
new file mode 100644
index 0000000..c9482a0
--- /dev/null
+++ b/src/routines/init_data.c
@@ -0,0 +1,196 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2025 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/linear.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <nw/stack.h>
+#include <nw/types.h>
+
+static enum nw_state get_index(struct nw_inst *);
+static enum nw_state get_byte(struct nw_inst *);
+
+static enum nw_state store_byte(struct nw_inst *const inst)
+{
+ struct nw_inst_sm_d *const d = &inst->sm.data;
+ struct nw_interp *const i = &inst->interp;
+ const unsigned long offset = d->offset + d->bytes_i;
+ const enum nw_state n = nwp_linear_store(i, &d->io, offset);
+
+ if (n)
+ return n;
+ else if (++d->bytes_i >= d->size)
+ inst->next = ++d->entry_i >= d->count ? d->next : get_index;
+ else
+ inst->next = get_byte;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_byte(struct nw_inst *const inst)
+{
+ struct nw_inst_sm_d *const d = &inst->sm.data;
+ struct nw_interp *const i = &inst->interp;
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_sm_io io = {0};
+ enum nw_state n;
+
+ io.buf = &d->b;
+ io.n = sizeof d->b;
+
+ if ((n = nwp_io_read(cfg, &io, cfg->user)))
+ return n;
+ else
+ {
+ struct nw_sm_io io = {0};
+
+ io.buf = &d->b;
+ io.n = sizeof d->b;
+ d->io = io;
+ }
+
+ inst->next = store_byte;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_size(struct nw_inst *const inst)
+{
+ struct nw_inst_sm_d *const d = &inst->sm.data;
+ struct nw_sm_leb128 *const l = &d->leb128;
+ struct nw_interp *const i = &inst->interp;
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ const enum nw_state n = nwp_varuint32(cfg, l, &d->size, cfg->user);
+
+ if (n)
+ return n;
+
+ d->bytes_i = 0;
+ inst->next = get_byte;
+ return NW_AGAIN;
+}
+
+static enum nw_state pop(struct nw_inst *const inst)
+{
+ struct nw_inst_sm_d *const d = &inst->sm.data;
+ const enum nw_state n = nwp_stack_pop(&inst->interp, &d->io);
+
+ if (n)
+ return n;
+
+ inst->next = get_size;
+ return NW_AGAIN;
+}
+
+static enum nw_state loop(struct nw_inst *const inst)
+{
+ struct nw_inst_sm_d *const d = &inst->sm.data;
+ const enum nw_state n = nwp_interp_run(&inst->interp);
+ struct nw_sm_io io = {0};
+
+ if (n)
+ return n;
+
+ io.buf = &d->offset;
+ io.n = sizeof d->offset;
+ d->io = io;
+ inst->next = pop;
+ return NW_AGAIN;
+}
+
+static enum nw_state setup_initexpr(struct nw_inst *const inst)
+{
+ struct nw_interp *const i = &inst->interp;
+ const struct nw_interp_cfg icfg = i->cfg;
+
+ if (nwp_interp_start(i, &icfg, &nwp_interp_data_set))
+ {
+#ifdef NW_LOG
+ nwp_log("nw_interp_start failed\n");
+#endif
+ return NW_FATAL;
+ }
+
+ inst->next = loop;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_index(struct nw_inst *const inst)
+{
+ struct nw_inst_sm_d *const d = &inst->sm.data;
+ struct nw_sm_leb128 *const l = &d->leb128;
+ struct nw_interp *const i = &inst->interp;
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ const enum nw_state n = nwp_varuint32(cfg, l, &d->index, cfg->user);
+
+ if (n)
+ return n;
+
+ return setup_initexpr(inst);
+}
+
+static enum nw_state get_count(struct nw_inst *const i)
+{
+ struct nw_inst_sm_d *const d = &i->sm.data;
+ struct nw_sm_leb128 *const l = &d->leb128;
+ const struct nw_interp_cfg *const icfg = &i->interp.cfg;
+ const struct nw_io_cfg *const cfg = &icfg->io;
+ const nw_varuint32 expected = icfg->m->data_count;
+ const enum nw_state n = nwp_varuint32(cfg, l, &d->count, cfg->user);
+
+ if (n)
+ return n;
+ else if (d->count != expected)
+ {
+#ifdef NW_LOG
+ static const char *const exc = "data count mismatch";
+
+ nwp_log("%s, expected %lu, got %lu\n", exc,
+ (unsigned long)expected, (unsigned long)d->count);
+#endif
+ return NW_FATAL;
+ }
+
+ i->next = d->count ? get_index : d->next;
+ return NW_AGAIN;
+}
+
+static enum nw_state seek_data(struct nw_inst *const i)
+{
+ struct nw_inst_sm_gl *const d = &i->sm.global;
+ const struct nw_interp_cfg *const icfg = &i->interp.cfg;
+ const struct nw_io_cfg *const cfg = &icfg->io;
+ const long offset = icfg->m->sections[NW_SECTION_DATA];
+ enum nw_state n;
+
+ if (!offset)
+ {
+ i->next = d->next;
+ return NW_AGAIN;
+ }
+ else if ((n = cfg->seek(offset, cfg->user)))
+ return n;
+
+ i->next = get_count;
+ return NW_AGAIN;
+}
+
+void nwp_init_data(struct nw_inst *const i,
+ enum nw_state (*const next)(struct nw_inst *))
+{
+ const struct nw_inst_sm_d d = {0};
+ struct nw_inst_sm_d *const p = &i->sm.data;
+
+ *p = d;
+ p->next = next;
+ i->next = seek_data;
+}