/* * 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 #include #include #include #include #include #include #include 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; i->next = sl->next; 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; }