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/find_param.c | |
| download | nanowasm-6d9d80362f9932bbc87e162b8ef7df06c73e27e1.tar.gz | |
First commit
Diffstat (limited to 'src/routines/find_param.c')
| -rw-r--r-- | src/routines/find_param.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/routines/find_param.c b/src/routines/find_param.c new file mode 100644 index 0000000..eeae86d --- /dev/null +++ b/src/routines/find_param.c @@ -0,0 +1,104 @@ +/* + * 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/io.h> +#include <nw/log.h> +#include <nw/routines.h> +#include <nw/types.h> + +static enum nw_state seek_pc(struct nw_interp *const i) +{ + struct nw_find_param *const fp = i->state; + const struct nw_io_cfg *const cfg = &i->cfg.io; + const enum nw_state n = cfg->seek(fp->pc, cfg->user); + + if (n) + return n; + + fp->out.addr = i->fr.fr_start - fp->sz; + i->next = fp->next; + return NW_AGAIN; +} + +static enum nw_state get_param_type(struct nw_interp *const i) +{ + nw_varint7 type; + struct nw_find_param *const fp = i->state; + struct nw_sm_leb128 *const l = &fp->leb128; + const struct nw_io_cfg *const cfg = &i->cfg.io; + const enum nw_state n = nwp_varint7(cfg, l, &type, cfg->user); + enum nw_type param_type; + size_t sz; + + if (n) + return n; + else if (nwp_get_type(type, ¶m_type) + || nwp_type_sz(param_type, &sz)) + { + static const char *const exc = "invalid param type"; + + i->exception = exc; +#ifdef NW_LOG + nwp_log("%s: %#x\n", exc, (unsigned)type); +#endif + return NW_FATAL; + } + + if (fp->param_i == fp->index) + fp->out.type = param_type; + + if (fp->param_i >= fp->index) + fp->sz += sz; + + if (++fp->param_i >= i->fr.fn.param_count) + i->next = seek_pc; + + return NW_AGAIN; +} + +static enum nw_state seek_param_types(struct nw_interp *const i) +{ + const struct nw_io_cfg *const cfg = &i->cfg.io; + const long offset = i->fr.fn.param_types; + const enum nw_state n = cfg->seek(offset, cfg->user); + + if (n) + return n; + + i->next = get_param_type; + return NW_AGAIN; +} + +static enum nw_state tell(struct nw_interp *const i) +{ + struct nw_find_param *const fp = i->state; + const struct nw_io_cfg *const cfg = &i->cfg.io; + const enum nw_state n = cfg->tell(&fp->pc, cfg->user); + + if (n) + return n; + + i->next = seek_param_types; + return NW_AGAIN; +} + +void nwp_find_param(struct nw_interp *const i, struct nw_find_param *const f, + const nw_varuint32 index, enum nw_state (*const next)(struct nw_interp *), + void *const args) +{ + const struct nw_find_param fp = {0}; + + *f = fp; + f->index = index; + f->next = next; + i->state = f; + i->next = tell; + i->args = args; +} |
