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/dbg | |
| download | nanowasm-6d9d80362f9932bbc87e162b8ef7df06c73e27e1.tar.gz | |
First commit
Diffstat (limited to 'src/dbg')
| -rw-r--r-- | src/dbg/CMakeLists.txt | 15 | ||||
| -rw-r--r-- | src/dbg/global.c | 47 | ||||
| -rw-r--r-- | src/dbg/init.c | 19 | ||||
| -rw-r--r-- | src/dbg/local.c | 71 | ||||
| -rw-r--r-- | src/dbg/mem_read.c | 47 | ||||
| -rw-r--r-- | src/dbg/param.c | 71 | ||||
| -rw-r--r-- | src/dbg/value.c | 17 |
7 files changed, 287 insertions, 0 deletions
diff --git a/src/dbg/CMakeLists.txt b/src/dbg/CMakeLists.txt new file mode 100644 index 0000000..c68a3c8 --- /dev/null +++ b/src/dbg/CMakeLists.txt @@ -0,0 +1,15 @@ +# 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/. + +target_sources(${PROJECT_NAME} PRIVATE + global.c + init.c + local.c + mem_read.c + param.c + value.c +) diff --git a/src/dbg/global.c b/src/dbg/global.c new file mode 100644 index 0000000..cad17ae --- /dev/null +++ b/src/dbg/global.c @@ -0,0 +1,47 @@ +/* + * 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 <nanowasm/dbg.h> +#include <nw/global.h> +#include <nw/log.h> +#include <nw/types.h> + +static enum nw_state read_global(struct nw_interp *const i) +{ + struct nw_dbg *const d = i->args; + struct nw_dbg_sm_global *const pg = &d->sm.global; + struct nw_dbg_value *const v = &d->v; + const struct nw_global *const gl = &pg->gl; + const enum nw_state n = nwp_global_load(i, &pg->io, pg->index); + + if (n) + return n; + + v->type = gl->type; + v->value = gl->value; + i->next = pg->inext; + return NW_AGAIN; +} + +void nw_dbg_global(struct nw_dbg *const d, const unsigned long index) +{ + const struct nw_dbg_sm_global gl = {0}; + struct nw_dbg_sm_global *const pg = &d->sm.global; + struct nw_sm_io *const io = &pg->io; + struct nw_interp *const i = &d->cfg.inst->interp; + + *pg = gl; + pg->index = index; + pg->inext = i->next; + io->buf = &pg->gl; + io->n = sizeof pg->gl; + i->args = d; + i->next = read_global; +} diff --git a/src/dbg/init.c b/src/dbg/init.c new file mode 100644 index 0000000..6169413 --- /dev/null +++ b/src/dbg/init.c @@ -0,0 +1,19 @@ +/* + * 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 <nanowasm/dbg.h> + +void nw_dbg_init(struct nw_dbg *const pd, const struct nw_dbg_cfg *const cfg) +{ + const struct nw_dbg d = {0}; + + *pd = d; + pd->cfg = *cfg; +} diff --git a/src/dbg/local.c b/src/dbg/local.c new file mode 100644 index 0000000..b48ac92 --- /dev/null +++ b/src/dbg/local.c @@ -0,0 +1,71 @@ +/* + * 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 <nanowasm/dbg.h> +#include <nw/log.h> +#include <nw/routines.h> +#include <nw/stack.h> +#include <nw/types.h> + +static enum nw_state read_local(struct nw_interp *const i) +{ + struct nw_dbg *const d = i->args; + struct nw_dbg_sm_local *const pl = &d->sm.local; + const struct nw_find_local *const fl = &pl->fl; + const enum nw_state n = nwp_stack_read(i, &pl->io, fl->addr); + + if (n) + return n; + + i->next = pl->inext; + return NW_AGAIN; +} + +static enum nw_state done(struct nw_interp *const i) +{ + struct nw_dbg *const d = i->args; + struct nw_dbg_sm_local *const pl = &d->sm.local; + const struct nw_local_meta *const m = &pl->fl.meta; + const enum nw_type t = m->type; + 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 = &d->v.value; + io.n = sz; + pl->io = io; + } + + i->next = read_local; + return NW_AGAIN; +} + +void nw_dbg_local(struct nw_dbg *const d, const unsigned long index) +{ + const struct nw_dbg_sm_local l = {0}; + struct nw_dbg_sm_local *const pl = &d->sm.local; + struct nw_interp *const i = &d->cfg.inst->interp; + + *pl = l; + pl->inext = i->next; + nwp_find_local(i, &pl->fl, index, done, d); +} diff --git a/src/dbg/mem_read.c b/src/dbg/mem_read.c new file mode 100644 index 0000000..d1f419c --- /dev/null +++ b/src/dbg/mem_read.c @@ -0,0 +1,47 @@ +/* + * 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 <nanowasm/dbg.h> +#include <nw/linear.h> +#include <nw/types.h> + +static enum nw_state load_mem(struct nw_interp *const i) +{ + struct nw_dbg *const d = i->args; + struct nw_dbg_sm_mem_load *const pm = &d->sm.mem_load; + const enum nw_state n = nwp_linear_load(i, &pm->io, pm->offset); + + if (n) + return n; + + i->next = pm->inext; + return NW_AGAIN; +} + +void nw_dbg_mem_load(struct nw_dbg *const d, const enum nw_type t, + const unsigned long offset) +{ + const struct nw_dbg_sm_mem_load ml = {0}; + struct nw_dbg_sm_mem_load *const pm = &d->sm.mem_load; + struct nw_sm_io *const io = &pm->io; + struct nw_interp *const i = &d->cfg.inst->interp; + struct nw_dbg_value *const v = &d->v; + size_t sz; + + nwp_type_sz(t, &sz); + *pm = ml; + pm->inext = i->next; + pm->offset = offset; + v->type = t; + io->buf = &v->value; + io->n = sz; + i->args = d; + i->next = load_mem; +} diff --git a/src/dbg/param.c b/src/dbg/param.c new file mode 100644 index 0000000..db0a599 --- /dev/null +++ b/src/dbg/param.c @@ -0,0 +1,71 @@ +/* + * 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 <nanowasm/dbg.h> +#include <nw/log.h> +#include <nw/routines.h> +#include <nw/stack.h> +#include <nw/types.h> + +static enum nw_state read_param(struct nw_interp *const i) +{ + struct nw_dbg *const d = i->args; + struct nw_dbg_sm_param *const p = &d->sm.param; + const struct nw_find_param_out *const out = &p->fp.out; + const enum nw_state n = nwp_stack_read(i, &p->io, out->addr); + + if (n) + return n; + + i->next = p->inext; + return NW_AGAIN; +} + +static enum nw_state done(struct nw_interp *const i) +{ + struct nw_dbg *const d = i->args; + struct nw_dbg_sm_param *const p = &d->sm.param; + const struct nw_find_param_out *const po = &p->fp.out; + const enum nw_type t = po->type; + 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 = &d->v.value; + io.n = sz; + p->io = io; + } + + i->next = read_param; + return NW_AGAIN; +} + +void nw_dbg_param(struct nw_dbg *const d, const unsigned long index) +{ + const struct nw_dbg_sm_param p = {0}; + struct nw_dbg_sm_param *const pp = &d->sm.param; + struct nw_interp *const i = &d->cfg.inst->interp; + + *pp = p; + pp->inext = i->next; + nwp_find_param(i, &pp->fp, index, done, d); +} diff --git a/src/dbg/value.c b/src/dbg/value.c new file mode 100644 index 0000000..633c5f7 --- /dev/null +++ b/src/dbg/value.c @@ -0,0 +1,17 @@ +/* + * 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 <nanowasm/dbg.h> + +int nw_dbg_value(const struct nw_dbg *const d, struct nw_dbg_value *const v) +{ + *v = d->v; + return 0; +} |
