diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2024-09-07 00:04:38 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi92@disroot.org> | 2025-11-06 14:38:40 +0100 |
| commit | 6d9d80362f9932bbc87e162b8ef7df06c73e27e1 (patch) | |
| tree | e3e228c63fe26f07503f226de7fb5086b3dc2286 /src/routines/init_globals.c | |
| download | nanowasm-6d9d80362f9932bbc87e162b8ef7df06c73e27e1.tar.gz | |
First commit
Diffstat (limited to 'src/routines/init_globals.c')
| -rw-r--r-- | src/routines/init_globals.c | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/src/routines/init_globals.c b/src/routines/init_globals.c new file mode 100644 index 0000000..6d1e5e4 --- /dev/null +++ b/src/routines/init_globals.c @@ -0,0 +1,181 @@ +/* + * 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/global.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_content_type(struct nw_inst *const i); + +static enum nw_state store(struct nw_inst *const inst) +{ + struct nw_inst_sm_gl *const g = &inst->sm.global; + struct nw_interp *const i = &inst->interp; + const enum nw_state n = nwp_global_store(i, &g->io, g->entry_i); + + if (n) + return n; + + inst->next = ++g->entry_i >= g->count ? g->next : get_content_type; + return NW_AGAIN; +} + +static enum nw_state pop(struct nw_inst *const inst) +{ + struct nw_inst_sm_gl *const g = &inst->sm.global; + struct nw_interp *const i = &inst->interp; + const enum nw_state n = nwp_stack_pop(i, &g->io); + + if (n) + return n; + else + { + struct nw_sm_io io = {0}; + + io.buf = &g->out; + io.n = sizeof g->out; + g->io = io; + inst->next = store; + } + + return NW_AGAIN; +} + +static enum nw_state loop(struct nw_inst *const i) +{ + struct nw_inst_sm_gl *const g = &i->sm.global; + const enum nw_state n = nwp_interp_run(&i->interp); + size_t sz; + + if (n) + return n; + else if (nwp_type_sz(g->out.type, &sz)) + { +#ifdef NW_LOG + static const char *const exc = "invalid type"; + + nwp_log("%s: %#x\n", exc, (unsigned)g->out.type); +#endif + return NW_FATAL; + } + else + { + struct nw_sm_io io = {0}; + + io.buf = &g->out.value; + io.n = sz; + g->io = io; + i->next = pop; + } + + return NW_AGAIN; +} + +static enum nw_state get_mutability(struct nw_inst *const inst) +{ + struct nw_inst_sm_gl *const g = &inst->sm.global; + struct nw_interp *const i = &inst->interp; + const struct nw_io_cfg *const cfg = &i->cfg.io; + struct nw_sm_leb128 *const l = &g->leb128; + const enum nw_state n = nwp_varuint1(cfg, l, &g->out.mutability, cfg->user); + + if (n) + return n; + + nwp_interp_resume(i); + inst->next = loop; + return NW_AGAIN; +} + +static enum nw_state get_content_type(struct nw_inst *const i) +{ + nw_varint7 content_type; + struct nw_inst_sm_gl *const g = &i->sm.global; + const struct nw_io_cfg *const cfg = &i->interp.cfg.io; + struct nw_sm_leb128 *const l = &g->leb128; + const enum nw_state n = nwp_varint7(cfg, l, &content_type, cfg->user); + + if (n) + return n; + else if (nwp_get_type(content_type, &g->out.type)) + { +#ifdef NW_LOG + static const char *const exc = "invalid type"; + + nwp_log("%s: %#x\n", exc, (unsigned)content_type); +#endif + return NW_FATAL; + } + + i->next = get_mutability; + return NW_AGAIN; +} + +static enum nw_state get_count(struct nw_inst *const i) +{ + struct nw_inst_sm_gl *const g = &i->sm.global; + struct nw_sm_leb128 *const l = &g->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->global_count; + const enum nw_state n = nwp_varuint32(cfg, l, &g->count, cfg->user); + + if (n) + return n; + else if (g->count != expected) + { +#ifdef NW_LOG + static const char *const exc = "global count mismatch"; + + nwp_log("%s, expected %lu, got %lu\n", exc, + (unsigned long)expected, (unsigned long)g->count); +#endif + return NW_FATAL; + } + + i->next = g->count ? get_content_type : g->next; + return NW_AGAIN; +} + +static enum nw_state seek_global(struct nw_inst *const i) +{ + struct nw_inst_sm_gl *const g = &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_GLOBAL]; + enum nw_state n; + + if (!offset) + { + i->next = g->next; + return NW_AGAIN; + } + else if ((n = cfg->seek(offset, cfg->user))) + return n; + + i->next = get_count; + return NW_AGAIN; +} + +void nwp_init_globals(struct nw_inst *const i, + enum nw_state (*const next)(struct nw_inst *)) +{ + const struct nw_inst_sm_gl gl = {0}; + struct nw_inst_sm_gl *const p = &i->sm.global; + + *p = gl; + p->next = next; + i->next = seek_global; +} |
