diff options
Diffstat (limited to 'src/op/get_local.c')
| -rw-r--r-- | src/op/get_local.c | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/src/op/get_local.c b/src/op/get_local.c new file mode 100644 index 0000000..6a9a42a --- /dev/null +++ b/src/op/get_local.c @@ -0,0 +1,150 @@ + +/* + * 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 int init_io(struct nw_interp *const i, const enum nw_type t) +{ + struct nw_i_sm_get_local *const gl = &i->sm.get_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 = &gl->value; + io.n = sz; + gl->io = io; + } + + return 0; +} + +static enum nw_state push(struct nw_interp *const i) +{ + struct nw_i_sm_get_local *const gl = &i->sm.get_local; + const enum nw_state n = nwp_stack_push(i, &gl->io); + + if (n) + return n; + + nwp_interp_resume(i); + return NW_AGAIN; +} + +static enum nw_state get_local(struct nw_interp *const i) +{ + struct nw_i_sm_get_local *const gl = &i->sm.get_local; + const struct nw_local_meta *const m = &gl->f.l.meta; + const enum nw_state n = nwp_stack_read(i, &gl->io, gl->f.l.addr); + + if (n) + return n; + else if (init_io(i, m->type)) + return NW_FATAL; + + i->next = push; + i->push_type = m->type; + return NW_AGAIN; +} + +static enum nw_state prepare_local(struct nw_interp *const i) +{ + const struct nw_local_meta *const m = &i->sm.get_local.f.l.meta; + + if (init_io(i, m->type)) + return NW_FATAL; + + i->next = get_local; + return NW_AGAIN; +} + +static enum nw_state get_param(struct nw_interp *const i) +{ + struct nw_i_sm_get_local *const gl = &i->sm.get_local; + const struct nw_find_param_out *const o = &gl->f.p.out; + const enum nw_state n = nwp_stack_read(i, &gl->io, o->addr); + + if (n) + return n; + else if (init_io(i, o->type)) + return NW_FATAL; + + i->next = push; + i->push_type = o->type; + return NW_AGAIN; +} + +static enum nw_state prepare_param(struct nw_interp *const i) +{ + struct nw_i_sm_get_local *const gl = &i->sm.get_local; + const struct nw_find_param_out *const o = &gl->f.p.out; + + if (init_io(i, o->type)) + return NW_FATAL; + + i->next = get_param; + return NW_AGAIN; +} + +static enum nw_state get_index(struct nw_interp *const i) +{ + struct nw_i_sm_get_local *const gl = &i->sm.get_local; + struct nw_sm_leb128 *const l = &gl->leb128; + const struct nw_io_cfg *const cfg = &i->cfg.io; + const struct nw_frame *const fr = &i->fr; + const struct nw_fn *const fn = &fr->fn; + const enum nw_state n = nwp_varuint32(cfg, l, &gl->index, cfg->user); + nw_varuint32 index; + + if (n) + return n; + else if (gl->index < fn->param_count) + nwp_find_param(i, &gl->f.p, gl->index, prepare_param, NULL); + else if ((index = gl->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)gl->index); +#endif + return NW_FATAL; + } + else + nwp_find_local(i, &gl->f.l, index, prepare_local, NULL); + + return NW_AGAIN; +} + +void nwp_op_get_local(struct nw_interp *const i) +{ + static const struct nw_i_sm_get_local gl = {0}; + + i->sm.get_local = gl; + i->next = get_index; +} |
