/* * 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 #include static enum nw_state get_index(struct nw_inst *); static enum nw_state get_byte(struct nw_inst *); static enum nw_state store_byte(struct nw_inst *const inst) { struct nw_inst_sm_d *const d = &inst->sm.data; struct nw_interp *const i = &inst->interp; const unsigned long offset = d->offset + d->bytes_i; const enum nw_state n = nwp_linear_store(i, &d->io, offset); if (n) return n; else if (++d->bytes_i >= d->size) inst->next = ++d->entry_i >= d->count ? d->next : get_index; else inst->next = get_byte; return NW_AGAIN; } static enum nw_state get_byte(struct nw_inst *const inst) { struct nw_inst_sm_d *const d = &inst->sm.data; struct nw_interp *const i = &inst->interp; const struct nw_io_cfg *const cfg = &i->cfg.io; struct nw_sm_io io = {0}; enum nw_state n; io.buf = &d->b; io.n = sizeof d->b; if ((n = nwp_io_read(cfg, &io, cfg->user))) return n; else { struct nw_sm_io io = {0}; io.buf = &d->b; io.n = sizeof d->b; d->io = io; } inst->next = store_byte; return NW_AGAIN; } static enum nw_state get_size(struct nw_inst *const inst) { struct nw_inst_sm_d *const d = &inst->sm.data; struct nw_sm_leb128 *const l = &d->leb128; struct nw_interp *const i = &inst->interp; const struct nw_io_cfg *const cfg = &i->cfg.io; const enum nw_state n = nwp_varuint32(cfg, l, &d->size, cfg->user); if (n) return n; d->bytes_i = 0; inst->next = get_byte; return NW_AGAIN; } static enum nw_state pop(struct nw_inst *const inst) { struct nw_inst_sm_d *const d = &inst->sm.data; const enum nw_state n = nwp_stack_pop(&inst->interp, &d->io); if (n) return n; inst->next = get_size; return NW_AGAIN; } static enum nw_state loop(struct nw_inst *const inst) { struct nw_inst_sm_d *const d = &inst->sm.data; const enum nw_state n = nwp_interp_run(&inst->interp); struct nw_sm_io io = {0}; if (n) return n; io.buf = &d->offset; io.n = sizeof d->offset; d->io = io; inst->next = pop; return NW_AGAIN; } static enum nw_state setup_initexpr(struct nw_inst *const inst) { struct nw_interp *const i = &inst->interp; const struct nw_interp_cfg icfg = i->cfg; if (nwp_interp_start(i, &icfg, &nwp_interp_data_set)) { #ifdef NW_LOG nwp_log("nw_interp_start failed\n"); #endif return NW_FATAL; } inst->next = loop; return NW_AGAIN; } static enum nw_state get_index(struct nw_inst *const inst) { struct nw_inst_sm_d *const d = &inst->sm.data; struct nw_sm_leb128 *const l = &d->leb128; struct nw_interp *const i = &inst->interp; const struct nw_io_cfg *const cfg = &i->cfg.io; const enum nw_state n = nwp_varuint32(cfg, l, &d->index, cfg->user); if (n) return n; return setup_initexpr(inst); } static enum nw_state get_count(struct nw_inst *const i) { struct nw_inst_sm_d *const d = &i->sm.data; struct nw_sm_leb128 *const l = &d->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->data_count; const enum nw_state n = nwp_varuint32(cfg, l, &d->count, cfg->user); if (n) return n; else if (d->count != expected) { #ifdef NW_LOG static const char *const exc = "data count mismatch"; nwp_log("%s, expected %lu, got %lu\n", exc, (unsigned long)expected, (unsigned long)d->count); #endif return NW_FATAL; } i->next = d->count ? get_index : d->next; return NW_AGAIN; } static enum nw_state seek_data(struct nw_inst *const i) { struct nw_inst_sm_gl *const d = &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_DATA]; enum nw_state n; if (!offset) { i->next = d->next; return NW_AGAIN; } else if ((n = cfg->seek(offset, cfg->user))) return n; i->next = get_count; return NW_AGAIN; } void nwp_init_data(struct nw_inst *const i, enum nw_state (*const next)(struct nw_inst *)) { const struct nw_inst_sm_d d = {0}; struct nw_inst_sm_d *const p = &i->sm.data; *p = d; p->next = next; i->next = seek_data; }