diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2024-09-07 00:04:38 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2024-10-05 08:17:21 +0200 |
| commit | 144b7fe1415ff97497fa4ea3b95e8dd6b95d069e (patch) | |
| tree | 68391f6ec2dba2879ca8255ee25ce39ebaa7d4ad /private_include/nw | |
First commit1st
Diffstat (limited to 'private_include/nw')
| -rw-r--r-- | private_include/nw/interp.h | 50 | ||||
| -rw-r--r-- | private_include/nw/io.h | 32 | ||||
| -rw-r--r-- | private_include/nw/log.h | 20 | ||||
| -rw-r--r-- | private_include/nw/opcodes.h | 67 | ||||
| -rw-r--r-- | private_include/nw/ops.h | 67 | ||||
| -rw-r--r-- | private_include/nw/routines.h | 41 | ||||
| -rw-r--r-- | private_include/nw/types.h | 76 |
7 files changed, 353 insertions, 0 deletions
diff --git a/private_include/nw/interp.h b/private_include/nw/interp.h new file mode 100644 index 0000000..6df491f --- /dev/null +++ b/private_include/nw/interp.h @@ -0,0 +1,50 @@ +/* + * 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/. + */ + +#ifndef INTERP_H +#define INTERP_H + +#include <nanowasm/nw.h> +#include <nw/opcodes.h> +#include <stddef.h> +#include <stdint.h> + +struct nwp_interp_set +{ + const enum opcode *opcodes; + size_t n; +}; + +struct nwp_ops +{ + void (*const *ops)(struct nw_interp *); + size_t n; +}; + +extern const struct nwp_interp_set nwp_interp_initexpr_set; +extern const struct nwp_ops nwp_ops; + +int nwp_interp_start(const struct nw_interp_cfg *cfg, + const struct nwp_interp_set *set, struct nw_interp *i); +void nwp_interp_resume(struct nw_interp *i); +void nwp_interp_limited(struct nw_interp *i); +void nwp_interp_full(struct nw_interp *i); +enum nw_state nwp_interp_run(struct nw_interp *i); +int nwp_interp_check_opcode(const struct nw_io_cfg *io, uint8_t op); +int nwp_interp_pop(struct nw_interp *i); +void *nwp_interp_stackptr(const struct nw_interp *i); +int nwp_interp_stack_push(struct nw_interp *i, const void *src, size_t n); +int nwp_interp_stack_pop(struct nw_interp *i, void *dst, size_t n); +int nwp_interp_heap_store(struct nw_interp *i, size_t addr, const void *src, size_t n); +int nwp_interp_heap_load(struct nw_interp *i, size_t addr, void *dst, size_t n); +int nwp_interp_global_push(struct nw_interp *i, const void *src, size_t n); +int nwp_interp_global_pop(struct nw_interp *i, void *dst, size_t n); +void *nwp_interp_globalptr(const struct nw_interp *i); + +#endif diff --git a/private_include/nw/io.h b/private_include/nw/io.h new file mode 100644 index 0000000..a038d9d --- /dev/null +++ b/private_include/nw/io.h @@ -0,0 +1,32 @@ +/* + * 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/. + */ + +#ifndef IO_H +#define IO_H + +#include <nanowasm/nw.h> +#include <stdbool.h> + +enum nw_state nwp_io_read(const struct nw_io_cfg *cfg, struct nw_sm_io *io); +enum nw_state nwp_varint1(const struct nw_io_cfg *cfg, struct nw_sm_leb128 *l, + nw_varint1 *out); +enum nw_state nwp_varint7(const struct nw_io_cfg *cfg, struct nw_sm_leb128 *l, + nw_varint7 *out); +enum nw_state nwp_varuint1(const struct nw_io_cfg *cfg, struct nw_sm_leb128 *l, + nw_varuint1 *out); +enum nw_state nwp_varuint7(const struct nw_io_cfg *cfg, struct nw_sm_leb128 *l, + nw_varuint7 *out); +enum nw_state nwp_varuint32(const struct nw_io_cfg *cfg, struct nw_sm_leb128 *l, + nw_varuint32 *out); +enum nw_state nwp_varuint64(const struct nw_io_cfg *cfg, struct nw_sm_leb128 *l, + nw_varuint64 *out); +enum nw_state nwp_leb128(const struct nw_io_cfg *cfg, struct nw_sm_leb128 *l, + unsigned maxbits, bool sign); + +#endif diff --git a/private_include/nw/log.h b/private_include/nw/log.h new file mode 100644 index 0000000..55d428a --- /dev/null +++ b/private_include/nw/log.h @@ -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/. + */ + +#ifndef LOG_H +#define LOG_H + +#ifdef ENABLE_LOG +#include <stdio.h> +#define LOG(...) fprintf(stderr, __VA_ARGS__) +#else +#define LOG(...) +#endif + +#endif diff --git a/private_include/nw/opcodes.h b/private_include/nw/opcodes.h new file mode 100644 index 0000000..2822b0d --- /dev/null +++ b/private_include/nw/opcodes.h @@ -0,0 +1,67 @@ +/* + * 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/. + */ + +#ifndef OPCODES_H +#define OPCODES_H + +enum opcode +{ + OP_UNREACHABLE, + OP_NOP, + OP_BLOCK, + OP_LOOP, + OP_IF, + OP_ELSE, + OP_END = 0xb, + OP_BR, + OP_BR_IF, + OP_BR_TABLE, + OP_RETURN = 0xf, + OP_CALL, + OP_CALL_INDIRECT, + OP_DROP = 0x1a, + OP_SELECT, + OP_GET_LOCAL = 0x20, + OP_SET_LOCAL, + OP_TEE_LOCAL, + OP_GET_GLOBAL, + OP_SET_GLOBAL, + OP_I32_LOAD = 0x28, + OP_I64_LOAD, + OP_F32_LOAD, + OP_F64_LOAD, + OP_I32_LOAD8_S, + OP_I32_LOAD8_U, + OP_I32_LOAD16_S, + OP_I32_LOAD16_U, + OP_I64_LOAD8_S, + OP_I64_LOAD8_U, + OP_I64_LOAD16_S, + OP_I64_LOAD16_U, + OP_I64_LOAD32_S, + OP_I64_LOAD32_U, + OP_I32_STORE, + OP_I64_STORE, + OP_F32_STORE, + OP_F64_STORE, + OP_I32_STORE8, + OP_I32_STORE16, + OP_I64_STORE8, + OP_I64_STORE16, + OP_I64_STORE32, + OP_CURRENT_MEMORY, + OP_GROW_MEMORY, + OP_I32_CONST, + OP_I64_CONST, + OP_F32_CONST, + OP_F64_CONST, + OP_I32_SUB = 0x6b +}; + +#endif diff --git a/private_include/nw/ops.h b/private_include/nw/ops.h new file mode 100644 index 0000000..4d8bdf0 --- /dev/null +++ b/private_include/nw/ops.h @@ -0,0 +1,67 @@ +/* + * 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/. + */ + +#ifndef OPS_H +#define OPS_H + +#include <nanowasm/nw.h> +#include <nw/types.h> + +void nwp_op_unreachable(struct nw_interp *i); +void nwp_op_nop(struct nw_interp *i); +void nwp_op_block(struct nw_interp *i); +void nwp_op_loop(struct nw_interp *i); +void nwp_op_if(struct nw_interp *i); +void nwp_op_else(struct nw_interp *i); +void nwp_op_end(struct nw_interp *i); +void nwp_op_br(struct nw_interp *i); +void nwp_op_br_if(struct nw_interp *i); +void nwp_op_br_table(struct nw_interp *i); +void nwp_op_return(struct nw_interp *i); +void nwp_op_call(struct nw_interp *i); +void nwp_op_call_indirect(struct nw_interp *i); +void nwp_op_get_local(struct nw_interp *i); +void nwp_op_set_local(struct nw_interp *i); +void nwp_op_tee_local(struct nw_interp *i); +void nwp_op_get_global(struct nw_interp *i); +void nwp_op_set_global(struct nw_interp *i); +void nwp_op_i32_load(struct nw_interp *i); +void nwp_op_i32_store(struct nw_interp *i); +void nwp_op_current_memory(struct nw_interp *i); +void nwp_op_i32_const(struct nw_interp *i); +void nwp_op_i64_const(struct nw_interp *i); +void nwp_op_f32_const(struct nw_interp *i); +void nwp_op_f64_const(struct nw_interp *i); +void nwp_op_i32_sub(struct nw_interp *i); + +void nwp_op_check_no_immediate(struct nw_mod *m); +void nwp_op_check_block(struct nw_mod *m); +void nwp_op_check_loop(struct nw_mod *m); +void nwp_op_check_if(struct nw_mod *m); +void nwp_op_check_else(struct nw_mod *m); +void nwp_op_check_end(struct nw_mod *m); +void nwp_op_check_br(struct nw_mod *m); +void nwp_op_check_br_if(struct nw_mod *m); +void nwp_op_check_br_table(struct nw_mod *m); +void nwp_op_check_call(struct nw_mod *m); +void nwp_op_check_call_indirect(struct nw_mod *m); +void nwp_op_check_local_index(struct nw_mod *m); +void nwp_op_check_local_index(struct nw_mod *m); +void nwp_op_check_global_index(struct nw_mod *m); +void nwp_op_check_memory_immediate(struct nw_mod *m); +void nwp_op_check_i32_const(struct nw_mod *m); +void nwp_op_check_i64_const(struct nw_mod *m); +void nwp_op_check_f32_const(struct nw_mod *m); +void nwp_op_check_f64_const(struct nw_mod *m); +void nwp_op_check_i32_sub(struct nw_mod *m); + +int locals_get(const varuint32 local_index, struct nw_interp *const i, + struct nw_locals **const l, varuint32 *const idx); + +#endif diff --git a/private_include/nw/routines.h b/private_include/nw/routines.h new file mode 100644 index 0000000..1225d64 --- /dev/null +++ b/private_include/nw/routines.h @@ -0,0 +1,41 @@ +/* + * 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/. + */ + +#ifndef ROUTINES_H +#define ROUTINES_H + +#include <nanowasm/nw.h> + +void nwp_check_magic(struct nw_mod *m); +void nwp_check_version(struct nw_mod *m); +void nwp_section(struct nw_mod *m); +void nwp_section_custom(struct nw_mod *m); +void nwp_section_type(struct nw_mod *m); +void nwp_section_import(struct nw_mod *m); +void nwp_section_function(struct nw_mod *m); +void nwp_section_table(struct nw_mod *m); +void nwp_section_memory(struct nw_mod *m); +void nwp_section_global(struct nw_mod *m); +void nwp_section_export(struct nw_mod *m); +void nwp_section_start(struct nw_mod *m); +void nwp_section_element(struct nw_mod *m); +void nwp_section_code(struct nw_mod *m); +void nwp_section_data(struct nw_mod *m); +enum nw_state nwp_section_skip(struct nw_mod *m); +enum nw_state nwp_section_exit(struct nw_mod *m); + +void nwp_find_export(struct nw_interp *i, const char *sym, + enum nw_state (*next)(struct nw_interp *)); +void nwp_call(struct nw_interp *i, nw_varuint32 index); +void nwp_find_function(struct nw_interp *i, nw_varuint32 index, + enum nw_state (*next)(struct nw_interp *)); +void nwp_get_function_type(struct nw_interp *i, nw_varuint32 index, + enum nw_state (*next)(struct nw_interp *)); + +#endif diff --git a/private_include/nw/types.h b/private_include/nw/types.h new file mode 100644 index 0000000..490c55f --- /dev/null +++ b/private_include/nw/types.h @@ -0,0 +1,76 @@ +/* + * 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/. + */ + +#ifndef WASM_TYPES_H +#define WASM_TYPES_H + +#include <nanowasm/nw.h> +#include <stdbool.h> +#include <stdint.h> + +typedef bool varuint1; +typedef signed char varint7; +typedef unsigned char varuint7; +typedef unsigned long varuint32; +typedef long varint32; +typedef unsigned long long varuint64; +typedef long long varint64; + +#define VALUE_TYPES \ + X(VALUE_TYPE_I32) \ + X(VALUE_TYPE_I64) \ + X(VALUE_TYPE_F32) \ + X(VALUE_TYPE_F64) + +enum value_type +{ +#define X(x) x, + VALUE_TYPES +#undef X +}; + +struct retval +{ + bool returns; + enum value_type type; +}; + +struct nw_block +{ + long pc; + struct nw_block *prev; +}; + +struct nw_locals +{ + enum value_type type; + unsigned long n; + struct nw_locals *next; +}; + +struct nw_frame +{ + struct retval retval; + struct nw_locals *locals; + struct nw_block *last_block; + struct nw_frame *prev, *next; +}; + +struct nw_gframe +{ + enum value_type type; + bool mutable; + struct nw_gframe *next; +}; + +int get_value_type(varint7 type, enum value_type *vtype); +size_t get_type_size(enum value_type type); +const char *value_type_tostr(enum value_type v); + +#endif |
