aboutsummaryrefslogtreecommitdiff
path: root/private_include
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-11-26 22:43:30 +0100
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-04-21 01:51:24 +0200
commitf25b015e5b668028c34974bbb22faa4105c26690 (patch)
tree28f2b08c17b3585d06694ad74004d0617eadb785 /private_include
downloadnanowasm-sync-f25b015e5b668028c34974bbb22faa4105c26690.tar.gz
First commit
Diffstat (limited to 'private_include')
-rw-r--r--private_include/nw/fstring.h18
-rw-r--r--private_include/nw/interp.h39
-rw-r--r--private_include/nw/leb128.h18
-rw-r--r--private_include/nw/log.h21
-rw-r--r--private_include/nw/opcodes.h67
-rw-r--r--private_include/nw/ops.h69
-rw-r--r--private_include/nw/search.h29
-rw-r--r--private_include/nw/sections.h65
-rw-r--r--private_include/nw/types.h81
9 files changed, 407 insertions, 0 deletions
diff --git a/private_include/nw/fstring.h b/private_include/nw/fstring.h
new file mode 100644
index 0000000..a8feda4
--- /dev/null
+++ b/private_include/nw/fstring.h
@@ -0,0 +1,18 @@
+/*
+ * 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 FSTRING_H
+#define FSTRING_H
+
+#include <stdbool.h>
+#include <stdio.h>
+
+int fstrcmp(const char *str, FILE *f, bool abort);
+
+#endif
diff --git a/private_include/nw/interp.h b/private_include/nw/interp.h
new file mode 100644
index 0000000..5fa37bf
--- /dev/null
+++ b/private_include/nw/interp.h
@@ -0,0 +1,39 @@
+/*
+ * 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>
+#include <stdio.h>
+
+struct interp_set
+{
+ const enum opcode *opcodes;
+ size_t n;
+};
+
+extern const struct interp_set interp_initexpr_set;
+
+int interp_start(const struct nw_interp_cfg *cfg, FILE *f, struct nw_interp *i);
+int interp_run(struct nw_interp *i);
+int interp_run_limited(struct nw_interp *i, const struct interp_set *ops);
+int interp_check_opcode(uint8_t op, FILE *f);
+int interp_push(struct nw_interp *i, const struct nw_frame *f);
+int interp_pop(struct nw_interp *i);
+void *interp_stackptr(const struct nw_interp *i);
+int interp_stack_push(struct nw_interp *i, const void *src, size_t n);
+int interp_stack_pop(struct nw_interp *i, void *dst, size_t n);
+int interp_heap_store(struct nw_interp *i, size_t addr, const void *src, size_t n);
+int interp_heap_load(struct nw_interp *i, size_t addr, void *dst, size_t n);
+
+#endif
diff --git a/private_include/nw/leb128.h b/private_include/nw/leb128.h
new file mode 100644
index 0000000..4338255
--- /dev/null
+++ b/private_include/nw/leb128.h
@@ -0,0 +1,18 @@
+/*
+ * 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 LEB128_H
+#define LEB128_H
+
+#include <stdio.h>
+
+int leb128_read_unsigned(FILE *f, unsigned maxbits, unsigned long long *out);
+int leb128_read_signed(FILE *f, unsigned maxbits, long long *out);
+
+#endif
diff --git a/private_include/nw/log.h b/private_include/nw/log.h
new file mode 100644
index 0000000..3eafc88
--- /dev/null
+++ b/private_include/nw/log.h
@@ -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/.
+ */
+
+#ifndef LOG_H
+#define LOG_H
+
+#include <stdio.h>
+
+#ifdef ENABLE_LOG
+#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..02f191e
--- /dev/null
+++ b/private_include/nw/ops.h
@@ -0,0 +1,69 @@
+/*
+ * 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 <stdio.h>
+
+int op_unreachable(struct nw_interp *i);
+int op_nop(struct nw_interp *i);
+int op_block(struct nw_interp *i);
+int op_loop(struct nw_interp *i);
+int op_if(struct nw_interp *i);
+int op_else(struct nw_interp *i);
+int op_end(struct nw_interp *i);
+int op_br(struct nw_interp *i);
+int op_br_if(struct nw_interp *i);
+int op_br_table(struct nw_interp *i);
+int op_return(struct nw_interp *i);
+int op_call(struct nw_interp *i);
+int op_call_indirect(struct nw_interp *i);
+int op_get_local(struct nw_interp *i);
+int op_set_local(struct nw_interp *i);
+int op_tee_local(struct nw_interp *i);
+int op_get_global(struct nw_interp *i);
+int op_set_global(struct nw_interp *i);
+int op_i32_load(struct nw_interp *i);
+int op_i32_store(struct nw_interp *i);
+int op_current_memory(struct nw_interp *i);
+int op_i32_const(struct nw_interp *i);
+int op_i64_const(struct nw_interp *i);
+int op_f32_const(struct nw_interp *i);
+int op_f64_const(struct nw_interp *i);
+int op_i32_sub(struct nw_interp *i);
+
+int check_unreachable(FILE *f);
+int check_nop(FILE *f);
+int check_block(FILE *f);
+int check_loop(FILE *f);
+int check_if(FILE *f);
+int check_else(FILE *f);
+int check_end(FILE *f);
+int check_br(FILE *f);
+int check_br_if(FILE *f);
+int check_br_table(FILE *f);
+int check_return(FILE *f);
+int check_call(FILE *f);
+int check_call_indirect(FILE *f);
+int check_get_local(FILE *f);
+int check_set_local(FILE *f);
+int check_tee_local(FILE *f);
+int check_get_global(FILE *f);
+int check_set_global(FILE *f);
+int check_i32_load(FILE *f);
+int check_i32_store(FILE *f);
+int check_i32_const(FILE *f);
+int check_i64_const(FILE *f);
+int check_f32_const(FILE *f);
+int check_f64_const(FILE *f);
+int check_i32_sub(FILE *f);
+
+#endif
diff --git a/private_include/nw/search.h b/private_include/nw/search.h
new file mode 100644
index 0000000..937e6fd
--- /dev/null
+++ b/private_include/nw/search.h
@@ -0,0 +1,29 @@
+/*
+ * 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 SEARCH_H
+#define SEARCH_H
+
+#include <nanowasm/nw.h>
+#include <nw/types.h>
+#include <stdio.h>
+
+struct search_fn
+{
+ long start;
+ bool returns;
+ varuint32 index;
+};
+
+int search_exported_fn(const char *fn, const struct nw_mod *m, FILE *f,
+ struct search_fn *out);
+int search_fn(varuint32 index, const struct nw_mod *m, FILE *f,
+ struct search_fn *out);
+
+#endif
diff --git a/private_include/nw/sections.h b/private_include/nw/sections.h
new file mode 100644
index 0000000..bef9cd8
--- /dev/null
+++ b/private_include/nw/sections.h
@@ -0,0 +1,65 @@
+/*
+ * 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 SECTIONS_H
+#define SECTIONS_H
+
+#include <nanowasm/nw.h>
+#include <nw/types.h>
+#include <stddef.h>
+#include <stdio.h>
+
+struct section
+{
+ const struct nw_mod_cfg *cfg;
+ FILE *f;
+};
+
+int section_custom_check(const struct section *s, struct nw_mod *m, unsigned long len);
+int section_type_check(const struct section *s, struct nw_mod *m, unsigned long len);
+int section_import_check(const struct section *s, struct nw_mod *m, unsigned long len);
+int section_function_check(const struct section *s, struct nw_mod *m, unsigned long len);
+int section_table_check(const struct section *s, struct nw_mod *m, unsigned long len);
+int section_memory_check(const struct section *s, struct nw_mod *m, unsigned long len);
+int section_global_check(const struct section *s, struct nw_mod *m, unsigned long len);
+int section_export_check(const struct section *s, struct nw_mod *m, unsigned long len);
+int section_start_check(const struct section *s, struct nw_mod *m, unsigned long len);
+int section_element_check(const struct section *s, struct nw_mod *m, unsigned long len);
+int section_code_check(const struct section *s, struct nw_mod *m, unsigned long len);
+int section_data_check(const struct section *s, struct nw_mod *m, unsigned long len);
+
+struct section_function
+{
+ varuint32 type, index, nreturn;
+};
+
+struct section_code
+{
+ long start;
+};
+
+int section_function(const struct section *s, const struct nw_mod *m,
+ varuint32 idx, struct section_function *f);
+int section_code(const struct section *s, const struct nw_mod *m,
+ varuint32 idx, struct section_code *c);
+
+int section_code_push(FILE *f, long pc, struct nw_frame *fr);
+int section_type_push(FILE *f, const struct nw_mod *m, varuint32 idx,
+ struct nw_frame *fr);
+int section_global_push(FILE *f, const struct nw_mod *m, struct nw_interp *i);
+
+struct resizable_limits
+{
+ bool max_available;
+ size_t sz, max;
+};
+
+int check_resizable_limits(FILE *f, struct resizable_limits *r);
+
+#endif
diff --git a/private_include/nw/types.h b/private_include/nw/types.h
new file mode 100644
index 0000000..23a2306
--- /dev/null
+++ b/private_include/nw/types.h
@@ -0,0 +1,81 @@
+/*
+ * 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>
+#include <stdio.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_frame
+{
+ enum value_type local_type;
+ unsigned long n_locals;
+ struct retval retval;
+ struct nw_block *last_block;
+ struct nw_frame *prev, *next;
+};
+
+struct nw_gframe
+{
+ enum value_type type;
+ bool mutable;
+ struct nw_gframe *next;
+};
+
+int varuint1_read(FILE *f, varuint1 *out);
+int varint7_read(FILE *f, varint7 *out);
+int varuint7_read(FILE *f, varuint7 *out);
+int varuint32_read(FILE *f, varuint32 *out);
+int varint32_read(FILE *f, varint32 *out);
+int varuint64_read(FILE *f, varuint64 *out);
+int varint64_read(FILE *f, varint64 *out);
+
+int get_value_type(varint7 type, enum value_type *vtype);
+size_t get_type_size(enum value_type type);
+
+int32_t htoni32(int32_t in);
+const char *value_type_tostr(enum value_type v);
+
+#endif