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/section/global.c | |
| download | nanowasm-6d9d80362f9932bbc87e162b8ef7df06c73e27e1.tar.gz | |
First commit
Diffstat (limited to 'src/routines/section/global.c')
| -rw-r--r-- | src/routines/section/global.c | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/src/routines/section/global.c b/src/routines/section/global.c new file mode 100644 index 0000000..6223fc9 --- /dev/null +++ b/src/routines/section/global.c @@ -0,0 +1,148 @@ +/* + * 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 <nw/interp.h> +#include <nw/io.h> +#include <nw/log.h> +#include <nw/routines.h> +#include <nw/types.h> +#include <limits.h> +#include <string.h> + +static enum nw_state get_content_type(struct nw_mod *); + +static int push(const void *const src, const size_t n, void *const user) +{ + struct nw_mod *const m = user; + struct nw_sm_gl *const gl = &m->sm.global; + + if (n > sizeof gl->value) + { +#ifdef NW_LOG + nwp_log("stack overflow\n"); +#endif + return -1; + } + + memcpy(&gl->value, src, n); + return n; +} + +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 = ++gl->entry_i >= m->global_count ? + nwp_section_exit : get_content_type; + return NW_AGAIN; +} + +static enum nw_state get_global(struct nw_mod *const m) +{ + struct nw_sm_gl *const gl = &m->sm.global; + struct nw_interp_cfg cfg = {0}; + + cfg.m = m; + cfg.io = m->cfg.io; + cfg.user = m; + cfg.stack.push = push; + + if (nwp_interp_start(&gl->interp, &cfg, &nwp_interp_initexpr_set)) + { +#ifdef NW_LOG + nwp_log("nw_interp_start failed\n"); +#endif + 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, cfg->user); + + if (n) + return n; + + m->next = get_global; + return NW_AGAIN; +} + +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, cfg->user); + enum nw_type type; + + /* TODO: do not ignore type */ + if (n) + return n; + else if (nwp_get_type(content_type, &type)) + { +#ifdef NW_LOG + nwp_log("nwp_get_type failed\n"); +#endif + return NW_FATAL; + } + + m->next = get_mutability; + 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; + enum nw_state n; + + if (!m->sections[NW_SECTION_IMPORT]) + { +#ifdef NW_LOG + nwp_log("global section found before import section\n"); +#endif + return NW_FATAL; + } + else if ((n = nwp_varuint32(cfg, l, &m->global_count, cfg->user))) + return n; + else if (m->global_count > ULONG_MAX / sizeof (struct nw_global)) + { +#ifdef NW_LOG + nwp_log("global_count too large: %lu\n", + (unsigned long)m->global_count); +#endif + return NW_FATAL; + } + + m->out.global = m->global_count * sizeof (struct nw_global); + m->next = m->global_count ? get_content_type : nwp_section_exit; + return NW_AGAIN; +} + +void nwp_section_global(struct nw_mod *const m) +{ + const struct nw_sm_gl g = {0}; + + m->sm.global = g; + m->next = get_count; +} |
