diff options
| author | Xavier Del Campo Romero <xavi92@disroot.org> | 2025-11-10 23:47:10 +0100 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi92@disroot.org> | 2025-11-10 23:47:10 +0100 |
| commit | ba623c7a3a5b450b9b492de50efceea93df96039 (patch) | |
| tree | 92b0d70db2fb304bac604338215401e25e8c4aac /src/op | |
| parent | 806824b23c28d2d81ffd588c555be82fa5a14004 (diff) | |
| download | nanowasm-ba623c7a3a5b450b9b492de50efceea93df96039.tar.gz | |
Add if and else operators
Diffstat (limited to 'src/op')
| -rw-r--r-- | src/op/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/op/block.c | 9 | ||||
| -rw-r--r-- | src/op/else.c | 18 | ||||
| -rw-r--r-- | src/op/if.c | 43 | ||||
| -rw-r--r-- | src/op/loop.c | 10 |
5 files changed, 80 insertions, 2 deletions
diff --git a/src/op/CMakeLists.txt b/src/op/CMakeLists.txt index 4b4a531..bea3555 100644 --- a/src/op/CMakeLists.txt +++ b/src/op/CMakeLists.txt @@ -12,6 +12,7 @@ target_sources(${PROJECT_NAME} PRIVATE call.c call_indirect.c drop.c + else.c end.c get_local.c get_global.c @@ -35,6 +36,7 @@ target_sources(${PROJECT_NAME} PRIVATE i32_sub.c i64_const.c i64_store.c + if.c loop.c nop.c return.c diff --git a/src/op/block.c b/src/op/block.c index e35a722..78651da 100644 --- a/src/op/block.c +++ b/src/op/block.c @@ -8,10 +8,17 @@ */ #include <nanowasm/nw.h> +#include <nw/interp.h> #include <nw/ops.h> #include <nw/routines.h> +static enum nw_state resume(struct nw_interp *const i) +{ + nwp_interp_resume(i); + return NW_AGAIN; +} + void nwp_op_block(struct nw_interp *const i) { - nwp_start_block(i); + nwp_start_block(i, &i->sm.start_block, resume); } diff --git a/src/op/else.c b/src/op/else.c new file mode 100644 index 0000000..2c1c6b1 --- /dev/null +++ b/src/op/else.c @@ -0,0 +1,18 @@ +/* + * 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/ops.h> +#include <nw/routines.h> + +void nwp_op_else(struct nw_interp *const i) +{ + nwp_break(i, 0); +} diff --git a/src/op/if.c b/src/op/if.c new file mode 100644 index 0000000..2ef9565 --- /dev/null +++ b/src/op/if.c @@ -0,0 +1,43 @@ +/* + * 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/ops.h> +#include <nw/routines.h> +#include <nw/stack.h> +#include <nw/types.h> + +static enum nw_state pop(struct nw_interp *const i) +{ + struct nw_i_sm_if *const pif = &i->sm.sm_if; + const enum nw_state n = nwp_stack_pop(i, &pif->io); + + if (n) + return n; + else if (pif->condition) + nwp_interp_resume(i); + else + nwp_break(i, 0); + + return NW_AGAIN; +} + +void nwp_op_if(struct nw_interp *const i) +{ + static const struct nw_i_sm_if sm_if; + struct nw_i_sm_if *const pif = &i->sm.sm_if; + struct nw_sm_io *const io = &pif->io; + + *pif = sm_if; + io->buf = &pif->condition; + io->n = sizeof pif->condition; + nwp_start_block(i, &pif->sb, pop); +} diff --git a/src/op/loop.c b/src/op/loop.c index e129610..cb3137b 100644 --- a/src/op/loop.c +++ b/src/op/loop.c @@ -8,9 +8,17 @@ */ #include <nanowasm/nw.h> +#include <nw/interp.h> +#include <nw/ops.h> #include <nw/routines.h> +static enum nw_state resume(struct nw_interp *const i) +{ + nwp_interp_resume(i); + return NW_AGAIN; +} + void nwp_op_loop(struct nw_interp *const i) { - nwp_start_block(i); + nwp_start_block(i, &i->sm.start_block, resume); } |
