diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-11-26 22:43:30 +0100 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2024-04-21 01:51:24 +0200 |
| commit | f25b015e5b668028c34974bbb22faa4105c26690 (patch) | |
| tree | 28f2b08c17b3585d06694ad74004d0617eadb785 /src/op/control_flow | |
| download | nanowasm-sync-f25b015e5b668028c34974bbb22faa4105c26690.tar.gz | |
First commit
Diffstat (limited to 'src/op/control_flow')
| -rw-r--r-- | src/op/control_flow/CMakeLists.txt | 20 | ||||
| -rw-r--r-- | src/op/control_flow/block.c | 75 | ||||
| -rw-r--r-- | src/op/control_flow/br.c | 38 | ||||
| -rw-r--r-- | src/op/control_flow/br_if.c | 95 | ||||
| -rw-r--r-- | src/op/control_flow/br_table.c | 56 | ||||
| -rw-r--r-- | src/op/control_flow/else.c | 21 | ||||
| -rw-r--r-- | src/op/control_flow/end.c | 24 | ||||
| -rw-r--r-- | src/op/control_flow/if.c | 36 | ||||
| -rw-r--r-- | src/op/control_flow/loop.c | 36 | ||||
| -rw-r--r-- | src/op/control_flow/nop.c | 22 | ||||
| -rw-r--r-- | src/op/control_flow/return.c | 30 | ||||
| -rw-r--r-- | src/op/control_flow/unreachable.c | 25 |
12 files changed, 478 insertions, 0 deletions
diff --git a/src/op/control_flow/CMakeLists.txt b/src/op/control_flow/CMakeLists.txt new file mode 100644 index 0000000..832f09e --- /dev/null +++ b/src/op/control_flow/CMakeLists.txt @@ -0,0 +1,20 @@ +# nanowasm, a tiny WebAssembly/Wasm interpreter +# Copyright (C) 2023-2024 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 + block.c + br.c + br_if.c + br_table.c + else.c + end.c + if.c + loop.c + nop.c + return.c + unreachable.c +) diff --git a/src/op/control_flow/block.c b/src/op/control_flow/block.c new file mode 100644 index 0000000..56f72f1 --- /dev/null +++ b/src/op/control_flow/block.c @@ -0,0 +1,75 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 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 <nw/log.h> +#include <nw/ops.h> +#include <nanowasm/nw.h> +#include <nw/interp.h> +#include <nw/types.h> +#include <errno.h> +#include <stddef.h> +#include <stdio.h> +#include <string.h> + +static int push(struct nw_interp *const i, FILE *const f) +{ + const long pc = ftell(f); + + if (pc < 0) + { + LOG("%s: ftell(3): %s\n", __func__, strerror(errno)); + i->exception = "I/O error"; + return -1; + } + + struct nw_frame *const fp = i->fp; + struct nw_block *const p = interp_stackptr(i); + const struct nw_block b = + { + .pc = pc, + .prev = fp->last_block + }; + + if (interp_stack_push(i, &b, sizeof b)) + { + LOG("%s: interp_stack_push failed\n", __func__); + return -1; + } + + fp->last_block = p; + return 0; +} + +static int op(struct nw_interp *const i, FILE *const f) +{ + varint7 sig; + + if (varint7_read(f, &sig)) + { + LOG("%s: varint7_read failed\n", __func__); + return -1; + } + else if (i && push(i, f)) + { + LOG("%s: push failed\n", __func__); + return -1; + } + + return 0; +} + +int op_block(struct nw_interp *const i) +{ + return op(i, i->f); +} + +int check_block(FILE *const f) +{ + return op(NULL, f); +} diff --git a/src/op/control_flow/br.c b/src/op/control_flow/br.c new file mode 100644 index 0000000..df3aacb --- /dev/null +++ b/src/op/control_flow/br.c @@ -0,0 +1,38 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 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 <nw/log.h> +#include <nw/ops.h> +#include <nanowasm/nw.h> +#include <nw/types.h> +#include <stddef.h> +#include <stdio.h> + +static int op(struct nw_interp *const i, FILE *const f) +{ + varuint32 relative_depth; + + if (varuint32_read(f, &relative_depth)) + { + LOG("%s: varuint32_read failed\n", __func__); + return -1; + } + + return 0; +} + +int op_br(struct nw_interp *const i) +{ + return op(i, i->f); +} + +int check_br(FILE *const f) +{ + return op(NULL, f); +} diff --git a/src/op/control_flow/br_if.c b/src/op/control_flow/br_if.c new file mode 100644 index 0000000..25bfdee --- /dev/null +++ b/src/op/control_flow/br_if.c @@ -0,0 +1,95 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 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 <nw/log.h> +#include <nw/ops.h> +#include <nanowasm/nw.h> +#include <nw/interp.h> +#include <nw/types.h> +#include <errno.h> +#include <stddef.h> +#include <stdio.h> +#include <string.h> + +static int branch(struct nw_interp *const i, const varuint32 relative_depth) +{ + const struct nw_block *b = i->fp->last_block; + static const char exc[] = "relative depth and block count mismatch"; + + for (varuint32 d = 0; b && d < relative_depth; d++) + b = b->prev; + + if (!b) + { + LOG("%s: %s\n", __func__, exc); + i->exception = exc; + return -1; + } + + if (fseek(i->f, b->pc, SEEK_SET)) + { + LOG("%s: fseek(3): %s\n", __func__, strerror(errno)); + i->exception = "I/O error"; + return -1; + } + + return 0; +} + +static bool cond(struct nw_interp *const i) +{ + int32_t value; + + if (interp_stack_pop(i, &value, sizeof value)) + { + LOG("%s: interp_stack_pop failed\n", __func__); + return -1; + } + + return value; +} + +static int br_if(struct nw_interp *const i, const varuint32 relative_depth) +{ + if (cond(i) && branch(i, relative_depth)) + { + LOG("%s: branch failed\n", __func__); + return -1; + } + + return 0; +} + +static int op(struct nw_interp *const i, FILE *const f) +{ + varuint32 relative_depth; + + if (varuint32_read(f, &relative_depth)) + { + LOG("%s: varuint32_read failed\n", __func__); + return 1; + } + else if (i && br_if(i, relative_depth)) + { + LOG("%s: br_if failed\n", __func__); + return -1; + } + + return 0; +} + +int op_br_if(struct nw_interp *const i) +{ + return op(i, i->f); +} + +int check_br_if(FILE *const f) +{ + return op(NULL, f); +} diff --git a/src/op/control_flow/br_table.c b/src/op/control_flow/br_table.c new file mode 100644 index 0000000..837cdcd --- /dev/null +++ b/src/op/control_flow/br_table.c @@ -0,0 +1,56 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 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 <nw/log.h> +#include <nw/ops.h> +#include <nanowasm/nw.h> +#include <nw/types.h> +#include <stddef.h> +#include <stdio.h> + +static int op(struct nw_interp *const i, FILE *const f) +{ + varuint32 target_count, default_target; + + if (varuint32_read(f, &target_count)) + { + LOG("%s: varuint32_read target_count failed\n", __func__); + return -1; + } + + for (varuint32 i = 0; i < target_count; i++) + { + varuint32 target_table; + + if (varuint32_read(f, &target_table)) + { + LOG("%s: varuint32_read target_table failed\n", + __func__); + return -1; + } + } + + if (varuint32_read(f, &default_target)) + { + LOG("%s: varuint32_read default_target failed\n", __func__); + return -1; + } + + return 0; +} + +int op_br_table(struct nw_interp *const i) +{ + return op(i, i->f); +} + +int check_br_table(FILE *const f) +{ + return op(NULL, f); +} diff --git a/src/op/control_flow/else.c b/src/op/control_flow/else.c new file mode 100644 index 0000000..fb839f0 --- /dev/null +++ b/src/op/control_flow/else.c @@ -0,0 +1,21 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 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 <nw/ops.h> +#include <nanowasm/nw.h> + +int op_else(struct nw_interp *const i) +{ + return -1; +} + +int check_else(FILE *const f) +{ + return 0; +} diff --git a/src/op/control_flow/end.c b/src/op/control_flow/end.c new file mode 100644 index 0000000..5f193d9 --- /dev/null +++ b/src/op/control_flow/end.c @@ -0,0 +1,24 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 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 <nw/ops.h> +#include <nanowasm/nw.h> +#include <stdbool.h> + +int op_end(struct nw_interp *const i) +{ + /* TODO: this is not correct. end can appear anywhere in a function. */ + i->exit = true; + return 0; +} + +int check_end(FILE *const f) +{ + return 0; +} diff --git a/src/op/control_flow/if.c b/src/op/control_flow/if.c new file mode 100644 index 0000000..1d5e838 --- /dev/null +++ b/src/op/control_flow/if.c @@ -0,0 +1,36 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 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 <nw/log.h> +#include <nw/ops.h> +#include <nanowasm/nw.h> +#include <nw/types.h> + +static int op(struct nw_interp *const i, FILE *const f) +{ + varint7 sig; + + if (varint7_read(f, &sig)) + { + LOG("%s: varint7_read failed\n", __func__); + return -1; + } + + return 0; +} + +int op_if(struct nw_interp *const i) +{ + return op(i, i->f); +} + +int check_if(FILE *const f) +{ + return op(NULL, f); +} diff --git a/src/op/control_flow/loop.c b/src/op/control_flow/loop.c new file mode 100644 index 0000000..5020f23 --- /dev/null +++ b/src/op/control_flow/loop.c @@ -0,0 +1,36 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 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 <nw/log.h> +#include <nw/ops.h> +#include <nanowasm/nw.h> +#include <nw/types.h> + +static int op(struct nw_interp *const i, FILE *const f) +{ + varint7 sig; + + if (varint7_read(f, &sig)) + { + LOG("%s: varint7_read failed\n", __func__); + return -1; + } + + return 0; +} + +int op_loop(struct nw_interp *const i) +{ + return op(i, i->f); +} + +int check_loop(FILE *const f) +{ + return op(NULL, f); +} diff --git a/src/op/control_flow/nop.c b/src/op/control_flow/nop.c new file mode 100644 index 0000000..356b7b0 --- /dev/null +++ b/src/op/control_flow/nop.c @@ -0,0 +1,22 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 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 <nw/ops.h> +#include <nanowasm/nw.h> +#include <nw/interp.h> + +int op_nop(struct nw_interp *const i) +{ + return 0; +} + +int check_nop(FILE *const f) +{ + return 0; +} diff --git a/src/op/control_flow/return.c b/src/op/control_flow/return.c new file mode 100644 index 0000000..abddea7 --- /dev/null +++ b/src/op/control_flow/return.c @@ -0,0 +1,30 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 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 <nw/log.h> +#include <nw/interp.h> +#include <nw/ops.h> +#include <nanowasm/nw.h> +#include <stdio.h> + +int op_return(struct nw_interp *const i) +{ + if (interp_pop(i)) + { + LOG("%s: interp_pop failed\n", __func__); + return -1; + } + + return 0; +} + +int check_return(FILE *const f) +{ + return 0; +} diff --git a/src/op/control_flow/unreachable.c b/src/op/control_flow/unreachable.c new file mode 100644 index 0000000..8f1982c --- /dev/null +++ b/src/op/control_flow/unreachable.c @@ -0,0 +1,25 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 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 <nw/ops.h> +#include <nanowasm/nw.h> +#include <stdbool.h> +#include <stdio.h> + +int op_unreachable(struct nw_interp *const i) +{ + i->exception = "Unreachable instruction"; + i->exit = true; + return 1; +} + +int check_unreachable(FILE *const f) +{ + return 0; +} |
