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/set_local.c | |
| download | nanowasm-6d9d80362f9932bbc87e162b8ef7df06c73e27e1.tar.gz | |
First commit
Diffstat (limited to 'src/routines/set_local.c')
| -rw-r--r-- | src/routines/set_local.c | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/src/routines/set_local.c b/src/routines/set_local.c new file mode 100644 index 0000000..84c7c26 --- /dev/null +++ b/src/routines/set_local.c @@ -0,0 +1,162 @@ +/* + * 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/ops.h> +#include <nw/routines.h> +#include <nw/stack.h> +#include <nw/types.h> + +static enum nw_state set_local(struct nw_interp *const i) +{ + struct nw_i_sm_set_local *const sl = &i->sm.set_local; + const enum nw_state n = nwp_stack_write(i, &sl->io, sl->f.l.addr); + + if (n) + return n; + + i->next = sl->next; + return NW_AGAIN; +} + +static int init_io(struct nw_interp *const i, const enum nw_type t) +{ + struct nw_i_sm_set_local *const sl = &i->sm.set_local; + size_t sz; + + if (nwp_type_sz(t, &sz)) + { + static const char *const exc = "invalid type"; + + i->exception = exc; +#ifdef NW_LOG + nwp_log("%s: %#x\n", exc, (unsigned)t); +#endif + return -1; + } + else + { + struct nw_sm_io io = {0}; + + io.buf = &sl->out.value; + io.n = sz; + sl->io = io; + } + + return 0; +} + +static enum nw_state pop(struct nw_interp *const i, const enum nw_type t, + enum nw_state (*const next)(struct nw_interp *)) +{ + struct nw_i_sm_set_local *const sl = &i->sm.set_local; + const enum nw_state n = nwp_stack_pop(i, &sl->io); + + if (n) + return n; + else if (init_io(i, t)) + return NW_FATAL; + + sl->out.type = t; + i->next = next; + return NW_AGAIN; +} + +static enum nw_state pop_local(struct nw_interp *const i) +{ + const struct nw_local_meta *const m = &i->sm.set_local.f.l.meta; + + return pop(i, m->type, set_local); +} + +static enum nw_state prepare_local(struct nw_interp *const i) +{ + const struct nw_local_meta *const m = &i->sm.set_local.f.l.meta; + + if (init_io(i, m->type)) + return NW_FATAL; + + i->next = pop_local; + return NW_AGAIN; +} + +static enum nw_state set_param(struct nw_interp *const i) +{ + struct nw_i_sm_set_local *const sl = &i->sm.set_local; + const enum nw_state n = nwp_stack_write(i, &sl->io, sl->f.p.out.addr); + + if (n) + return n; + + nwp_interp_resume(i); + return NW_AGAIN; +} + +static enum nw_state pop_param(struct nw_interp *const i) +{ + const struct nw_find_param_out *const o = &i->sm.set_local.f.p.out; + + return pop(i, o->type, set_param); +} + +static enum nw_state prepare_param(struct nw_interp *const i) +{ + struct nw_i_sm_set_local *const sl = &i->sm.set_local; + const struct nw_find_param_out *const o = &sl->f.p.out; + + if (init_io(i, o->type)) + return NW_FATAL; + + i->next = pop_param; + return NW_AGAIN; +} + +static enum nw_state get_index(struct nw_interp *const i) +{ + struct nw_i_sm_set_local *const sl = &i->sm.set_local; + struct nw_sm_leb128 *const l = &sl->leb128; + const struct nw_frame *const fr = &i->fr; + const struct nw_fn *const fn = &fr->fn; + const struct nw_io_cfg *const cfg = &i->cfg.io; + const enum nw_state n = nwp_varuint32(cfg, l, &sl->index, cfg->user); + nw_varuint32 index; + + if (n) + return n; + else if (sl->index < fn->param_count) + nwp_find_param(i, &sl->f.p, sl->index, prepare_param, NULL); + else if ((index = sl->index - fn->param_count) >= fr->local_count) + { + static const char *const exc = "invalid local index"; + + i->exception = exc; +#ifdef NW_LOG + nwp_log("%s: %lu\n", exc, (unsigned long)sl->index); +#endif + return NW_FATAL; + } + else + nwp_find_local(i, &sl->f.l, index, prepare_local, NULL); + + return NW_AGAIN; +} + +void nwp_set_local(struct nw_interp *const i, + enum nw_state (*const next)(struct nw_interp *)) +{ + const struct nw_i_sm_set_local sl = {0}; + struct nw_i_sm_set_local *const p = &i->sm.set_local; + + *p = sl; + p->next = next; + i->next = get_index; +} |
