aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--CMakeLists.txt47
-rw-r--r--LICENSE373
-rw-r--r--README.md44
-rw-r--r--include/nanowasm/leb128.h34
-rw-r--r--include/nanowasm/nw.h31
-rw-r--r--include/nanowasm/types.h333
-rw-r--r--private_include/nw/interp.h50
-rw-r--r--private_include/nw/io.h32
-rw-r--r--private_include/nw/log.h20
-rw-r--r--private_include/nw/opcodes.h67
-rw-r--r--private_include/nw/ops.h67
-rw-r--r--private_include/nw/routines.h41
-rw-r--r--private_include/nw/types.h76
-rw-r--r--src/CMakeLists.txt19
-rw-r--r--src/exception.c15
-rw-r--r--src/init.c20
-rw-r--r--src/interp/CMakeLists.txt21
-rw-r--r--src/interp/check_opcode.c59
-rw-r--r--src/interp/global_pop.c34
-rw-r--r--src/interp/globalptr.c17
-rw-r--r--src/interp/initexpr_set.c28
-rw-r--r--src/interp/interp.c411
-rw-r--r--src/interp/ops.c53
-rw-r--r--src/interp/resume.c20
-rw-r--r--src/interp/routines/CMakeLists.txt12
-rw-r--r--src/interp/routines/find_export.c202
-rw-r--r--src/interp/routines/full.c48
-rw-r--r--src/interp/routines/limited.c54
-rw-r--r--src/interp/run.c16
-rw-r--r--src/interp/stack_pop.c31
-rw-r--r--src/interp/stack_push.c34
-rw-r--r--src/interp/stackptr.c16
-rw-r--r--src/interp/start.c26
-rw-r--r--src/io/CMakeLists.txt18
-rw-r--r--src/io/leb128.c47
-rw-r--r--src/io/read.c29
-rw-r--r--src/io/varint1.c27
-rw-r--r--src/io/varint64.c27
-rw-r--r--src/io/varint7.c26
-rw-r--r--src/io/varuint1.c27
-rw-r--r--src/io/varuint32.c27
-rw-r--r--src/io/varuint64.c27
-rw-r--r--src/io/varuint7.c27
-rw-r--r--src/load.c21
-rw-r--r--src/op/CMakeLists.txt16
-rw-r--r--src/op/check/CMakeLists.txt19
-rw-r--r--src/op/check/block.c34
-rw-r--r--src/op/check/br_if.c39
-rw-r--r--src/op/check/call.c39
-rw-r--r--src/op/check/end.c35
-rw-r--r--src/op/check/global_index.c39
-rw-r--r--src/op/check/i32_const.c32
-rw-r--r--src/op/check/i64_const.c32
-rw-r--r--src/op/check/local_index.c43
-rw-r--r--src/op/check/memory_immediate.c47
-rw-r--r--src/op/check/no_immediate.c23
-rw-r--r--src/op/end.c24
-rw-r--r--src/op/i32_const.c39
-rw-r--r--src/op/i32_load.c18
-rw-r--r--src/op/nop.c23
-rw-r--r--src/op/unreachable.c27
-rw-r--r--src/routines/CMakeLists.txt17
-rw-r--r--src/routines/call.c33
-rw-r--r--src/routines/check_magic.c50
-rw-r--r--src/routines/check_version.c50
-rw-r--r--src/routines/find_func_type.c20
-rw-r--r--src/routines/find_function.c21
-rw-r--r--src/routines/get_function_type.c256
-rw-r--r--src/routines/section.c94
-rw-r--r--src/routines/section/CMakeLists.txt23
-rw-r--r--src/routines/section/code.c271
-rw-r--r--src/routines/section/custom.c17
-rw-r--r--src/routines/section/data.c19
-rw-r--r--src/routines/section/element.c19
-rw-r--r--src/routines/section/exit.c36
-rw-r--r--src/routines/section/export.c113
-rw-r--r--src/routines/section/function.c49
-rw-r--r--src/routines/section/global.c144
-rw-r--r--src/routines/section/import.c308
-rw-r--r--src/routines/section/memory.c85
-rw-r--r--src/routines/section/skip.c26
-rw-r--r--src/routines/section/start.c35
-rw-r--r--src/routines/section/table.c111
-rw-r--r--src/routines/section/type.c159
-rw-r--r--src/run.c16
-rw-r--r--src/start.c45
-rw-r--r--test/CMakeLists.txt2
-rwxr-xr-xtest/example.wasmbin0 -> 901 bytes
-rw-r--r--test/main.c183
90 files changed, 5387 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..bdcf7af
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.cache/
+*build*/
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..9c9d97b
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,47 @@
+# 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/.
+
+cmake_minimum_required(VERSION 3.19)
+project(nanowasm C)
+option(NW_LOG "Enables logging to stderr" ON)
+add_library(${PROJECT_NAME})
+add_subdirectory(test)
+target_include_directories(${PROJECT_NAME} PUBLIC include
+ PRIVATE private_include)
+
+set(cflags_np
+ -ffunction-sections
+ -fdata-sections
+ -pedantic
+ -Wall
+)
+
+if(CMAKE_BUILD_TYPE STREQUAL "Debug")
+ set(cflags_np ${cflags_np} -Og)
+elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
+ set(cflags_np ${cflags_np} -Os)
+endif()
+
+include(CheckCompilerFlag)
+
+foreach(f ${cflags_np})
+ string(REPLACE "-" "_" var supported_${f})
+ check_compiler_flag(C ${f} ${var})
+
+ if(${var})
+ target_compile_options(${PROJECT_NAME} PRIVATE ${f})
+ target_compile_options(test PRIVATE ${f})
+ endif()
+endforeach()
+
+if(NW_LOG)
+ target_compile_definitions(${PROJECT_NAME} PRIVATE ENABLE_LOG)
+endif()
+
+set_target_properties(${PROJECT_NAME} PROPERTIES C_STANDARD 99 C_EXTENSIONS OFF)
+add_subdirectory(src)
+target_compile_options(${PROJECT_NAME} PRIVATE -g)
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..d0a1fa1
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,373 @@
+Mozilla Public License Version 2.0
+==================================
+
+1. Definitions
+--------------
+
+1.1. "Contributor"
+ means each individual or legal entity that creates, contributes to
+ the creation of, or owns Covered Software.
+
+1.2. "Contributor Version"
+ means the combination of the Contributions of others (if any) used
+ by a Contributor and that particular Contributor's Contribution.
+
+1.3. "Contribution"
+ means Covered Software of a particular Contributor.
+
+1.4. "Covered Software"
+ means Source Code Form to which the initial Contributor has attached
+ the notice in Exhibit A, the Executable Form of such Source Code
+ Form, and Modifications of such Source Code Form, in each case
+ including portions thereof.
+
+1.5. "Incompatible With Secondary Licenses"
+ means
+
+ (a) that the initial Contributor has attached the notice described
+ in Exhibit B to the Covered Software; or
+
+ (b) that the Covered Software was made available under the terms of
+ version 1.1 or earlier of the License, but not also under the
+ terms of a Secondary License.
+
+1.6. "Executable Form"
+ means any form of the work other than Source Code Form.
+
+1.7. "Larger Work"
+ means a work that combines Covered Software with other material, in
+ a separate file or files, that is not Covered Software.
+
+1.8. "License"
+ means this document.
+
+1.9. "Licensable"
+ means having the right to grant, to the maximum extent possible,
+ whether at the time of the initial grant or subsequently, any and
+ all of the rights conveyed by this License.
+
+1.10. "Modifications"
+ means any of the following:
+
+ (a) any file in Source Code Form that results from an addition to,
+ deletion from, or modification of the contents of Covered
+ Software; or
+
+ (b) any new file in Source Code Form that contains any Covered
+ Software.
+
+1.11. "Patent Claims" of a Contributor
+ means any patent claim(s), including without limitation, method,
+ process, and apparatus claims, in any patent Licensable by such
+ Contributor that would be infringed, but for the grant of the
+ License, by the making, using, selling, offering for sale, having
+ made, import, or transfer of either its Contributions or its
+ Contributor Version.
+
+1.12. "Secondary License"
+ means either the GNU General Public License, Version 2.0, the GNU
+ Lesser General Public License, Version 2.1, the GNU Affero General
+ Public License, Version 3.0, or any later versions of those
+ licenses.
+
+1.13. "Source Code Form"
+ means the form of the work preferred for making modifications.
+
+1.14. "You" (or "Your")
+ means an individual or a legal entity exercising rights under this
+ License. For legal entities, "You" includes any entity that
+ controls, is controlled by, or is under common control with You. For
+ purposes of this definition, "control" means (a) the power, direct
+ or indirect, to cause the direction or management of such entity,
+ whether by contract or otherwise, or (b) ownership of more than
+ fifty percent (50%) of the outstanding shares or beneficial
+ ownership of such entity.
+
+2. License Grants and Conditions
+--------------------------------
+
+2.1. Grants
+
+Each Contributor hereby grants You a world-wide, royalty-free,
+non-exclusive license:
+
+(a) under intellectual property rights (other than patent or trademark)
+ Licensable by such Contributor to use, reproduce, make available,
+ modify, display, perform, distribute, and otherwise exploit its
+ Contributions, either on an unmodified basis, with Modifications, or
+ as part of a Larger Work; and
+
+(b) under Patent Claims of such Contributor to make, use, sell, offer
+ for sale, have made, import, and otherwise transfer either its
+ Contributions or its Contributor Version.
+
+2.2. Effective Date
+
+The licenses granted in Section 2.1 with respect to any Contribution
+become effective for each Contribution on the date the Contributor first
+distributes such Contribution.
+
+2.3. Limitations on Grant Scope
+
+The licenses granted in this Section 2 are the only rights granted under
+this License. No additional rights or licenses will be implied from the
+distribution or licensing of Covered Software under this License.
+Notwithstanding Section 2.1(b) above, no patent license is granted by a
+Contributor:
+
+(a) for any code that a Contributor has removed from Covered Software;
+ or
+
+(b) for infringements caused by: (i) Your and any other third party's
+ modifications of Covered Software, or (ii) the combination of its
+ Contributions with other software (except as part of its Contributor
+ Version); or
+
+(c) under Patent Claims infringed by Covered Software in the absence of
+ its Contributions.
+
+This License does not grant any rights in the trademarks, service marks,
+or logos of any Contributor (except as may be necessary to comply with
+the notice requirements in Section 3.4).
+
+2.4. Subsequent Licenses
+
+No Contributor makes additional grants as a result of Your choice to
+distribute the Covered Software under a subsequent version of this
+License (see Section 10.2) or under the terms of a Secondary License (if
+permitted under the terms of Section 3.3).
+
+2.5. Representation
+
+Each Contributor represents that the Contributor believes its
+Contributions are its original creation(s) or it has sufficient rights
+to grant the rights to its Contributions conveyed by this License.
+
+2.6. Fair Use
+
+This License is not intended to limit any rights You have under
+applicable copyright doctrines of fair use, fair dealing, or other
+equivalents.
+
+2.7. Conditions
+
+Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
+in Section 2.1.
+
+3. Responsibilities
+-------------------
+
+3.1. Distribution of Source Form
+
+All distribution of Covered Software in Source Code Form, including any
+Modifications that You create or to which You contribute, must be under
+the terms of this License. You must inform recipients that the Source
+Code Form of the Covered Software is governed by the terms of this
+License, and how they can obtain a copy of this License. You may not
+attempt to alter or restrict the recipients' rights in the Source Code
+Form.
+
+3.2. Distribution of Executable Form
+
+If You distribute Covered Software in Executable Form then:
+
+(a) such Covered Software must also be made available in Source Code
+ Form, as described in Section 3.1, and You must inform recipients of
+ the Executable Form how they can obtain a copy of such Source Code
+ Form by reasonable means in a timely manner, at a charge no more
+ than the cost of distribution to the recipient; and
+
+(b) You may distribute such Executable Form under the terms of this
+ License, or sublicense it under different terms, provided that the
+ license for the Executable Form does not attempt to limit or alter
+ the recipients' rights in the Source Code Form under this License.
+
+3.3. Distribution of a Larger Work
+
+You may create and distribute a Larger Work under terms of Your choice,
+provided that You also comply with the requirements of this License for
+the Covered Software. If the Larger Work is a combination of Covered
+Software with a work governed by one or more Secondary Licenses, and the
+Covered Software is not Incompatible With Secondary Licenses, this
+License permits You to additionally distribute such Covered Software
+under the terms of such Secondary License(s), so that the recipient of
+the Larger Work may, at their option, further distribute the Covered
+Software under the terms of either this License or such Secondary
+License(s).
+
+3.4. Notices
+
+You may not remove or alter the substance of any license notices
+(including copyright notices, patent notices, disclaimers of warranty,
+or limitations of liability) contained within the Source Code Form of
+the Covered Software, except that You may alter any license notices to
+the extent required to remedy known factual inaccuracies.
+
+3.5. Application of Additional Terms
+
+You may choose to offer, and to charge a fee for, warranty, support,
+indemnity or liability obligations to one or more recipients of Covered
+Software. However, You may do so only on Your own behalf, and not on
+behalf of any Contributor. You must make it absolutely clear that any
+such warranty, support, indemnity, or liability obligation is offered by
+You alone, and You hereby agree to indemnify every Contributor for any
+liability incurred by such Contributor as a result of warranty, support,
+indemnity or liability terms You offer. You may include additional
+disclaimers of warranty and limitations of liability specific to any
+jurisdiction.
+
+4. Inability to Comply Due to Statute or Regulation
+---------------------------------------------------
+
+If it is impossible for You to comply with any of the terms of this
+License with respect to some or all of the Covered Software due to
+statute, judicial order, or regulation then You must: (a) comply with
+the terms of this License to the maximum extent possible; and (b)
+describe the limitations and the code they affect. Such description must
+be placed in a text file included with all distributions of the Covered
+Software under this License. Except to the extent prohibited by statute
+or regulation, such description must be sufficiently detailed for a
+recipient of ordinary skill to be able to understand it.
+
+5. Termination
+--------------
+
+5.1. The rights granted under this License will terminate automatically
+if You fail to comply with any of its terms. However, if You become
+compliant, then the rights granted under this License from a particular
+Contributor are reinstated (a) provisionally, unless and until such
+Contributor explicitly and finally terminates Your grants, and (b) on an
+ongoing basis, if such Contributor fails to notify You of the
+non-compliance by some reasonable means prior to 60 days after You have
+come back into compliance. Moreover, Your grants from a particular
+Contributor are reinstated on an ongoing basis if such Contributor
+notifies You of the non-compliance by some reasonable means, this is the
+first time You have received notice of non-compliance with this License
+from such Contributor, and You become compliant prior to 30 days after
+Your receipt of the notice.
+
+5.2. If You initiate litigation against any entity by asserting a patent
+infringement claim (excluding declaratory judgment actions,
+counter-claims, and cross-claims) alleging that a Contributor Version
+directly or indirectly infringes any patent, then the rights granted to
+You by any and all Contributors for the Covered Software under Section
+2.1 of this License shall terminate.
+
+5.3. In the event of termination under Sections 5.1 or 5.2 above, all
+end user license agreements (excluding distributors and resellers) which
+have been validly granted by You or Your distributors under this License
+prior to termination shall survive termination.
+
+************************************************************************
+* *
+* 6. Disclaimer of Warranty *
+* ------------------------- *
+* *
+* Covered Software is provided under this License on an "as is" *
+* basis, without warranty of any kind, either expressed, implied, or *
+* statutory, including, without limitation, warranties that the *
+* Covered Software is free of defects, merchantable, fit for a *
+* particular purpose or non-infringing. The entire risk as to the *
+* quality and performance of the Covered Software is with You. *
+* Should any Covered Software prove defective in any respect, You *
+* (not any Contributor) assume the cost of any necessary servicing, *
+* repair, or correction. This disclaimer of warranty constitutes an *
+* essential part of this License. No use of any Covered Software is *
+* authorized under this License except under this disclaimer. *
+* *
+************************************************************************
+
+************************************************************************
+* *
+* 7. Limitation of Liability *
+* -------------------------- *
+* *
+* Under no circumstances and under no legal theory, whether tort *
+* (including negligence), contract, or otherwise, shall any *
+* Contributor, or anyone who distributes Covered Software as *
+* permitted above, be liable to You for any direct, indirect, *
+* special, incidental, or consequential damages of any character *
+* including, without limitation, damages for lost profits, loss of *
+* goodwill, work stoppage, computer failure or malfunction, or any *
+* and all other commercial damages or losses, even if such party *
+* shall have been informed of the possibility of such damages. This *
+* limitation of liability shall not apply to liability for death or *
+* personal injury resulting from such party's negligence to the *
+* extent applicable law prohibits such limitation. Some *
+* jurisdictions do not allow the exclusion or limitation of *
+* incidental or consequential damages, so this exclusion and *
+* limitation may not apply to You. *
+* *
+************************************************************************
+
+8. Litigation
+-------------
+
+Any litigation relating to this License may be brought only in the
+courts of a jurisdiction where the defendant maintains its principal
+place of business and such litigation shall be governed by laws of that
+jurisdiction, without reference to its conflict-of-law provisions.
+Nothing in this Section shall prevent a party's ability to bring
+cross-claims or counter-claims.
+
+9. Miscellaneous
+----------------
+
+This License represents the complete agreement concerning the subject
+matter hereof. If any provision of this License is held to be
+unenforceable, such provision shall be reformed only to the extent
+necessary to make it enforceable. Any law or regulation which provides
+that the language of a contract shall be construed against the drafter
+shall not be used to construe this License against a Contributor.
+
+10. Versions of the License
+---------------------------
+
+10.1. New Versions
+
+Mozilla Foundation is the license steward. Except as provided in Section
+10.3, no one other than the license steward has the right to modify or
+publish new versions of this License. Each version will be given a
+distinguishing version number.
+
+10.2. Effect of New Versions
+
+You may distribute the Covered Software under the terms of the version
+of the License under which You originally received the Covered Software,
+or under the terms of any subsequent version published by the license
+steward.
+
+10.3. Modified Versions
+
+If you create software not governed by this License, and you want to
+create a new license for such software, you may create and use a
+modified version of this License if you rename the license and remove
+any references to the name of the license steward (except to note that
+such modified license differs from this License).
+
+10.4. Distributing Source Code Form that is Incompatible With Secondary
+Licenses
+
+If You choose to distribute Source Code Form that is Incompatible With
+Secondary Licenses under the terms of this version of the License, the
+notice described in Exhibit B of this License must be attached.
+
+Exhibit A - Source Code Form License Notice
+-------------------------------------------
+
+ 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/.
+
+If it is not possible or desirable to put the notice in a particular
+file, then You may include the notice in a location (such as a LICENSE
+file in a relevant directory) where a recipient would be likely to look
+for such a notice.
+
+You may add additional accurate notices of copyright ownership.
+
+Exhibit B - "Incompatible With Secondary Licenses" Notice
+---------------------------------------------------------
+
+ This Source Code Form is "Incompatible With Secondary Licenses", as
+ defined by the Mozilla Public License, v. 2.0.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..d0caafb
--- /dev/null
+++ b/README.md
@@ -0,0 +1,44 @@
+# `nanowasm`, a tiny WebAssembly/Wasm interpreter
+
+**This project is still unfinished and is not meant for production use.**
+
+This project aims to provide the most minimal Wasm interpreter possible in
+strictly portable ISO C99. It can run one or more Wasm applications
+concurrently, without threading.
+
+## Features
+
+- Portable, ISO C99 implementation.
+- Asynchronous interface, allows to run multiple applications concurrently.
+- Suitable for resource-constrained devices.
+- Does not require dynamic memory allocations.
+
+## What `nanowasm` is not
+
+As opposed to other interpreters, `nanowasm` prefers lower memory usage
+rather than run-time performance. Therefore, it should not be unfairly
+compared _performance_-wise against other interpreters.
+
+## How to build
+
+Use the conventional process in CMake projects:
+
+```
+cmake -B <dir>
+cmake --build <dir>
+```
+
+## Examples
+
+A minimal example is provided on [`test/main.c`](./test/main.c).
+
+## License
+
+```
+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/.
+```
diff --git a/include/nanowasm/leb128.h b/include/nanowasm/leb128.h
new file mode 100644
index 0000000..8213789
--- /dev/null
+++ b/include/nanowasm/leb128.h
@@ -0,0 +1,34 @@
+/*
+ * 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_LEB128_H
+#define WASM_LEB128_H
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+typedef bool nw_varint1;
+typedef bool nw_varuint1;
+typedef int8_t nw_varint7;
+typedef uint8_t nw_varuint7;
+typedef uint32_t nw_varuint32;
+typedef int32_t nw_varint32;
+typedef uint64_t nw_varuint64;
+typedef int64_t nw_varint64;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/include/nanowasm/nw.h b/include/nanowasm/nw.h
new file mode 100644
index 0000000..ede5442
--- /dev/null
+++ b/include/nanowasm/nw.h
@@ -0,0 +1,31 @@
+/*
+ * 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 NANOWASM_H
+#define NANOWASM_H
+
+#include <nanowasm/types.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+void nw_init(struct nw_mod *m, const struct nw_mod_cfg *cfg);
+enum nw_state nw_load(struct nw_mod *m);
+int nw_start(const struct nw_inst_cfg *icfg, struct nw_inst *i);
+enum nw_state nw_run(struct nw_inst *i);
+const char *nw_exception(const struct nw_inst *i);
+int nw_stop(struct nw_inst *i);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/include/nanowasm/types.h b/include/nanowasm/types.h
new file mode 100644
index 0000000..c839f17
--- /dev/null
+++ b/include/nanowasm/types.h
@@ -0,0 +1,333 @@
+/*
+ * 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 NANOWASM_TYPES_H
+#define NANOWASM_TYPES_H
+
+#include <nanowasm/leb128.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+struct nw_interp;
+
+union nw_value
+{
+ long i32;
+ long long i64;
+ float f32;
+ double f64;
+ /* TODO: define {de}serialization between Wasm and host. */
+ void *p;
+};
+
+enum nw_type
+{
+ NW_TYPE_I32,
+ NW_TYPE_I64,
+ NW_TYPE_F32,
+ NW_TYPE_F64
+};
+
+enum nw_kind
+{
+ NW_KIND_FUNCTION,
+ NW_KIND_TABLE,
+ NW_KIND_MEMORY,
+ NW_KIND_GLOBAL,
+
+ NW_KINDS
+};
+
+struct nw_args
+{
+ const union nw_value *args;
+ size_t n;
+};
+
+struct nw_buf
+{
+ void *buf;
+ size_t n;
+};
+
+enum nw_state
+{
+ NW_OK,
+ NW_AGAIN,
+ NW_FATAL
+};
+
+enum nw_whence
+{
+ NW_SEEK_SET,
+ NW_SEEK_CUR,
+ NW_SEEK_END
+};
+
+struct nw_io_cfg
+{
+ int (*read)(void *buf, size_t n, void *user);
+ int (*eof)(void *user);
+ enum nw_state (*seek)(long offset, enum nw_whence whence, void *user);
+ enum nw_state (*tell)(long *offset, void *user);
+ void *user;
+};
+
+struct nw_sm_io
+{
+ void *buf;
+ size_t n, read;
+};
+
+struct nw_sm_leb128
+{
+ unsigned long long result;
+ unsigned shift, bcnt;
+};
+
+struct nw_interp_cfg
+{
+ struct nw_buf stack, heap, global;
+ struct nw_io_cfg io;
+ const struct nw_mod *m;
+};
+
+union nw_interp_sm
+{
+ struct nw_i_sm_b
+ {
+ uint8_t op;
+ struct nw_sm_io io;
+ } bytecode;
+
+ struct nw_i_sm_i32_const
+ {
+ struct nw_sm_leb128 leb128;
+ } i32_const;
+
+ struct nw_i_sm_exp
+ {
+ struct nw_sm_leb128 leb128;
+ nw_varuint32 count, entry_i, len, len_i;
+ const char *sym;
+ enum nw_kind kind;
+ nw_varuint32 index;
+ enum nw_state (*next)(struct nw_interp *);
+ } export;
+
+ struct nw_i_sm_type
+ {
+ struct nw_sm_leb128 leb128;
+ nw_varuint32 index, count, entry_i, type_index, param_i;
+ enum nw_state (*next)(struct nw_interp *);
+
+ struct nw_i_sm_type_out
+ {
+ nw_varuint32 count, param_count;
+ nw_varint7 form, return_type;
+ nw_varuint1 return_count;
+ } out;
+ } type;
+};
+
+struct nw_interp
+{
+ const void *set;
+ const char *exception;
+ bool exit;
+ int retval;
+ struct nw_interp_cfg cfg;
+ size_t stack_i, global_i;
+ struct nw_frame *fp;
+ struct nw_gframe *gfp;
+ enum nw_state (*next)(struct nw_interp *);
+ union nw_interp_sm sm;
+};
+
+union nw_sm
+{
+ struct nw_sm_cm
+ {
+ struct nw_sm_io io;
+ uint8_t buf[sizeof "\0asm" - 1];
+ } check_magic;
+
+ struct nw_sm_cv
+ {
+ struct nw_sm_io io;
+ uint32_t version;
+ } check_version;
+
+ struct nw_sm_st
+ {
+ struct nw_sm_leb128 leb128;
+ nw_varuint32 count, entry_i, param_count, p_i;
+ } type;
+
+ struct nw_sm_imp
+ {
+ struct nw_sm_leb128 leb128;
+ long mod_off, field_off;
+ nw_varuint32 count, entry_i, mod_len, field_len, len_i, imp_i;
+ } import;
+
+ struct nw_sm_fn
+ {
+ struct nw_sm_leb128 leb128;
+ nw_varuint32 count, entry_i;
+ } function;
+
+ struct nw_sm_tb
+ {
+ struct nw_sm_leb128 leb128;
+ nw_varuint32 count, entry_i;
+ nw_varint7 elem_type;
+ nw_varuint1 flags;
+ } table;
+
+ struct nw_sm_mem
+ {
+ struct nw_sm_leb128 leb128;
+ nw_varuint32 count, entry_i;
+ nw_varuint1 flags;
+ } memory;
+
+ struct nw_sm_gl
+ {
+ struct nw_sm_leb128 leb128;
+ nw_varuint32 entry_i;
+ struct nw_interp interp;
+ bool mutable;
+ enum nw_type type;
+
+ union
+ {
+ int32_t i32;
+ int64_t i64;
+ float f32;
+ double f64;
+ } value;
+ } global;
+
+ struct nw_sm_exp
+ {
+ struct nw_sm_io io;
+ struct nw_sm_leb128 leb128;
+ nw_varuint32 count, entry_i, field_len, len_i;
+ } export;
+
+ struct nw_sm_start
+ {
+ struct nw_sm_leb128 leb128;
+ } start;
+
+ struct nw_sm_c
+ {
+ struct nw_sm_leb128 leb128;
+ nw_varuint32 count, entry_i, body_size;
+ long start, op_off;
+ unsigned long rem;
+ enum nw_state (*next)(struct nw_mod *);
+ uint8_t op;
+
+ struct nw_sm_c_fn
+ {
+ nw_varuint32 local_count, local_i, local_total;
+ unsigned blocks;
+ } fn;
+
+ struct nw_sm_c_fn_type
+ {
+ enum nw_state (*next)(struct nw_mod *);
+ } func_type;
+ } code;
+};
+
+struct nw_mod_cfg
+{
+ const struct nw_import
+ {
+ enum nw_kind kind;
+ const char *module, *field;
+
+ union
+ {
+ struct nw_import_fn
+ {
+ const char *signature;
+ int (*fn)(const struct nw_args *params,
+ union nw_value *ret, void *user);
+ } function;
+ } u;
+ } *imports;
+
+ struct nw_io_cfg io;
+ size_t n_imports;
+ void *user;
+};
+
+struct nw_inst_cfg
+{
+ struct nw_interp_cfg interp_cfg;
+};
+
+enum
+{
+ NW_SECTION_CUSTOM,
+ NW_SECTION_TYPE,
+ NW_SECTION_IMPORT,
+ NW_SECTION_FUNCTION,
+ NW_SECTION_TABLE,
+ NW_SECTION_MEMORY,
+ NW_SECTION_GLOBAL,
+ NW_SECTION_EXPORT,
+ NW_SECTION_START,
+ NW_SECTION_ELEMENT,
+ NW_SECTION_CODE,
+ NW_SECTION_DATA,
+
+ NW_SECTIONS
+};
+
+struct nw_mod
+{
+ long sections[NW_SECTIONS];
+ size_t data_len, heap_len;
+ enum nw_state (*next)(struct nw_mod *);
+ struct nw_mod_cfg cfg;
+ union nw_sm sm;
+ nw_varuint32 global_count;
+
+ struct nw_mod_section
+ {
+ struct nw_sm_leb128 leb128;
+ nw_varuint7 section;
+ nw_varuint32 len;
+ long offset;
+ } section;
+};
+
+struct nw_inst
+{
+ struct nw_interp interp;
+};
+
+#define NW_BUF(sz) (const struct nw_buf){.buf = (char [sz]){0}, .n = (sz)}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
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
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 0000000..e58c920
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,19 @@
+# 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
+ exception.c
+ init.c
+ load.c
+ run.c
+ start.c
+)
+
+add_subdirectory(interp)
+add_subdirectory(io)
+add_subdirectory(routines)
+add_subdirectory(op)
diff --git a/src/exception.c b/src/exception.c
new file mode 100644
index 0000000..15820eb
--- /dev/null
+++ b/src/exception.c
@@ -0,0 +1,15 @@
+/*
+ * 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 <nanowasm/nw.h>
+
+const char *nw_exception(const struct nw_inst *const i)
+{
+ return i->interp.exception;
+}
diff --git a/src/init.c b/src/init.c
new file mode 100644
index 0000000..6634722
--- /dev/null
+++ b/src/init.c
@@ -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/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <string.h>
+
+void nw_init(struct nw_mod *const m, const struct nw_mod_cfg *const cfg)
+{
+ *m = (const struct nw_mod){.cfg = *cfg};
+ nwp_check_magic(m);
+}
diff --git a/src/interp/CMakeLists.txt b/src/interp/CMakeLists.txt
new file mode 100644
index 0000000..c3bb726
--- /dev/null
+++ b/src/interp/CMakeLists.txt
@@ -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/.
+
+target_sources(${PROJECT_NAME} PRIVATE
+ global_pop.c
+ globalptr.c
+ initexpr_set.c
+ ops.c
+ run.c
+ stack_pop.c
+ stackptr.c
+ stack_push.c
+ resume.c
+ start.c
+)
+
+add_subdirectory(routines)
diff --git a/src/interp/check_opcode.c b/src/interp/check_opcode.c
new file mode 100644
index 0000000..b10b927
--- /dev/null
+++ b/src/interp/check_opcode.c
@@ -0,0 +1,59 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/opcodes.h>
+
+int nwp_interp_check_opcode(const uint8_t op, FILE *const f)
+{
+ static int (*const checks[])(FILE *) =
+ {
+ [OP_UNREACHABLE] = check_unreachable,
+ [OP_NOP] = check_nop,
+ [OP_BLOCK] = check_block,
+ [OP_LOOP] = check_loop,
+ [OP_IF] = check_if,
+ [OP_ELSE] = check_else,
+ [OP_END] = check_end,
+ [OP_BR] = check_br,
+ [OP_BR_IF] = check_br_if,
+ [OP_BR_TABLE] = check_br_table,
+ [OP_RETURN] = check_return,
+ [OP_CALL] = check_call,
+ [OP_CALL_INDIRECT] = check_call_indirect,
+ [OP_GET_LOCAL] = check_get_local,
+ [OP_SET_LOCAL] = check_set_local,
+ [OP_TEE_LOCAL] = check_tee_local,
+ [OP_GET_GLOBAL] = check_get_global,
+ [OP_SET_GLOBAL] = check_set_global,
+ [OP_I32_LOAD] = check_i32_load,
+ [OP_I32_STORE] = check_i32_store,
+ [OP_I32_CONST] = check_i32_const,
+ [OP_I64_CONST] = check_i64_const,
+ [OP_F32_CONST] = check_f32_const,
+ [OP_F64_CONST] = check_f64_const,
+ [OP_I32_SUB] = check_i32_sub
+ };
+
+ if (op >= sizeof checks / sizeof *checks)
+ {
+ LOG("%s: invalid opcode %#" PRIx8 "\n", __func__, op);
+ return 1;
+ }
+ else if (!checks[op])
+ {
+ LOG("%s: unsupported opcode %#" PRIx8 "\n", __func__, op);
+ return 1;
+ }
+
+ return checks[op](f);
+}
diff --git a/src/interp/global_pop.c b/src/interp/global_pop.c
new file mode 100644
index 0000000..4066d63
--- /dev/null
+++ b/src/interp/global_pop.c
@@ -0,0 +1,34 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <nw/types.h>
+#include <stddef.h>
+#include <string.h>
+
+int interp_global_pop(struct nw_interp *const i, void *const dst,
+ const size_t n)
+{
+ const void *const src = (const char *)i->cfg.global.buf + i->global_i;
+
+ if (i->global_i < n)
+ {
+ static const char exc[] = "global memory underflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ i->global_i -= n;
+ memcpy(dst, src, n);
+ return 0;
+}
diff --git a/src/interp/globalptr.c b/src/interp/globalptr.c
new file mode 100644
index 0000000..486f5ba
--- /dev/null
+++ b/src/interp/globalptr.c
@@ -0,0 +1,17 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/types.h>
+
+void *interp_globalptr(const struct nw_interp *const i)
+{
+ return (char *)i->cfg.global.buf + i->global_i;
+}
diff --git a/src/interp/initexpr_set.c b/src/interp/initexpr_set.c
new file mode 100644
index 0000000..57601be
--- /dev/null
+++ b/src/interp/initexpr_set.c
@@ -0,0 +1,28 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/opcodes.h>
+
+static const enum opcode opcodes[] =
+{
+ OP_NOP,
+ OP_END,
+ OP_I32_CONST,
+ OP_I64_CONST,
+ OP_F32_CONST,
+ OP_F64_CONST
+};
+
+const struct nwp_interp_set nwp_interp_initexpr_set =
+{
+ .opcodes = opcodes,
+ .n = sizeof opcodes / sizeof *opcodes
+};
diff --git a/src/interp/interp.c b/src/interp/interp.c
new file mode 100644
index 0000000..dbeb126
--- /dev/null
+++ b/src/interp/interp.c
@@ -0,0 +1,411 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/log.h>
+#include <nw/interp.h>
+#include <nw/opcodes.h>
+#include <nw/ops.h>
+#include <nw/sections.h>
+#include <nw/types.h>
+#include <errno.h>
+#include <limits.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static const enum opcode initexpr_opcodes[] =
+{
+ OP_NOP,
+ OP_END,
+ OP_I32_CONST,
+ OP_I64_CONST,
+ OP_F32_CONST,
+ OP_F64_CONST
+};
+
+const struct interp_set nwp_interp_initexpr_set =
+{
+ .opcodes = initexpr_opcodes,
+ .n = sizeof initexpr_opcodes / sizeof *initexpr_opcodes
+};
+
+static int (*const ops[])(struct nw_interp *) =
+{
+ [OP_UNREACHABLE] = op_unreachable,
+ [OP_NOP] = op_nop,
+ [OP_BLOCK] = op_block,
+ [OP_LOOP] = op_loop,
+ [OP_IF] = op_if,
+ [OP_ELSE] = op_else,
+ [OP_END] = op_end,
+ [OP_BR] = op_br,
+ [OP_BR_IF] = op_br_if,
+ [OP_BR_TABLE] = op_br_table,
+ [OP_RETURN] = op_return,
+ [OP_CALL] = op_call,
+ [OP_CALL_INDIRECT] = op_call_indirect,
+ [OP_GET_LOCAL] = op_get_local,
+ [OP_SET_LOCAL] = op_set_local,
+ [OP_TEE_LOCAL] = op_tee_local,
+ [OP_GET_GLOBAL] = op_get_global,
+ [OP_SET_GLOBAL] = op_set_global,
+ [OP_I32_LOAD] = op_i32_load,
+ [OP_I32_STORE] = op_i32_store,
+ [OP_I32_CONST] = op_i32_const,
+ [OP_I64_CONST] = op_i64_const,
+ [OP_F32_CONST] = op_f32_const,
+ [OP_F64_CONST] = op_f64_const,
+ [OP_I32_SUB] = op_i32_sub
+};
+
+int interp_run(struct nw_interp *const i)
+{
+ uint8_t op;
+
+ if (!fread(&op, sizeof op, 1, i->f))
+ {
+ LOG("%s: fread(3) failed, feof=%d, ferror=%d\n", __func__, feof(i->f),
+ ferror(i->f));
+ i->exception = "I/O error";
+ return -1;
+ }
+ else if (op >= sizeof ops / sizeof *ops)
+ {
+ LOG("%s: invalid opcode %#" PRIx8 "\n", __func__, op);
+ i->exception = "invalid opcode";
+ return -1;
+ }
+ else if (!ops[op])
+ {
+ LOG("%s: unsupported opcode %#" PRIx8 "\n", __func__, op);
+ i->exception = "invalid opcode";
+ return -1;
+ }
+
+ return ops[op](i);
+}
+
+static int run_opcode_limited(struct nw_interp *const in,
+ const struct interp_set *const set)
+{
+ uint8_t op;
+
+ if (!fread(&op, sizeof op, 1, in->f))
+ {
+ LOG("%s: fread(3) failed, feof=%d, ferror=%d\n",
+ __func__, feof(in->f), ferror(in->f));
+ in->exception = "I/O error";
+ return -1;
+ }
+
+ for (size_t i = 0; i < set->n; i++)
+ if (op == set->opcodes[i])
+ return ops[op](in);
+
+ LOG("%s: unexpected opcode %#" PRIx8 "\n", __func__, op);
+ in->exception = "invalid opcode";
+ return -1;
+}
+
+int interp_run_limited(struct nw_interp *const i,
+ const struct interp_set *const set)
+{
+ while (!i->exit)
+ if (run_opcode_limited(i, set))
+ {
+ LOG("%s: run_opcode_limited failed\n", __func__);
+ return -1;
+ }
+
+ return 0;
+}
+
+int interp_check_opcode(const uint8_t op, FILE *const f)
+{
+ static int (*const checks[])(FILE *) =
+ {
+ [OP_UNREACHABLE] = check_unreachable,
+ [OP_NOP] = check_nop,
+ [OP_BLOCK] = check_block,
+ [OP_LOOP] = check_loop,
+ [OP_IF] = check_if,
+ [OP_ELSE] = check_else,
+ [OP_END] = check_end,
+ [OP_BR] = check_br,
+ [OP_BR_IF] = check_br_if,
+ [OP_BR_TABLE] = check_br_table,
+ [OP_RETURN] = check_return,
+ [OP_CALL] = check_call,
+ [OP_CALL_INDIRECT] = check_call_indirect,
+ [OP_GET_LOCAL] = check_get_local,
+ [OP_SET_LOCAL] = check_set_local,
+ [OP_TEE_LOCAL] = check_tee_local,
+ [OP_GET_GLOBAL] = check_get_global,
+ [OP_SET_GLOBAL] = check_set_global,
+ [OP_I32_LOAD] = check_i32_load,
+ [OP_I32_STORE] = check_i32_store,
+ [OP_I32_CONST] = check_i32_const,
+ [OP_I64_CONST] = check_i64_const,
+ [OP_F32_CONST] = check_f32_const,
+ [OP_F64_CONST] = check_f64_const,
+ [OP_I32_SUB] = check_i32_sub
+ };
+
+ if (op >= sizeof checks / sizeof *checks)
+ {
+ LOG("%s: invalid opcode %#" PRIx8 "\n", __func__, op);
+ return 1;
+ }
+ else if (!checks[op])
+ {
+ LOG("%s: unsupported opcode %#" PRIx8 "\n", __func__, op);
+ return 1;
+ }
+
+ return checks[op](f);
+}
+
+int interp_start(const struct nw_interp_cfg *const cfg, FILE *const f,
+ struct nw_interp *const i)
+{
+ *i = (const struct nw_interp)
+ {
+ .f = f,
+ .cfg = *cfg
+ };
+
+ return 0;
+}
+
+void *nwp_interp_stackptr(const struct nw_interp *const i)
+{
+ return (char *)i->cfg.stack.buf + i->stack_i;
+}
+
+struct retval_priv
+{
+ enum value_type type;
+
+ union
+ {
+ int32_t i32;
+ int64_t i64;
+ float f32;
+ double f64;
+ } u;
+};
+
+static int get_i32(struct nw_interp *const i, struct retval_priv *const rp)
+{
+ return interp_stack_pop(i, &rp->u.i32, sizeof rp->u.i32);
+}
+
+static int get_i64(struct nw_interp *const i, struct retval_priv *const rp)
+{
+ return interp_stack_pop(i, &rp->u.i64, sizeof rp->u.i64);
+}
+
+static int get_f32(struct nw_interp *const i, struct retval_priv *const rp)
+{
+ return interp_stack_pop(i, &rp->u.f32, sizeof rp->u.f32);
+}
+
+static int get_f64(struct nw_interp *const i, struct retval_priv *const rp)
+{
+ return interp_stack_pop(i, &rp->u.f64, sizeof rp->u.f64);
+}
+
+static int get_retval(struct nw_interp *const i, struct retval_priv *const rp)
+{
+ const struct retval *const r = &i->fp->retval;
+ const enum value_type v = r->type;
+ static int (*const f[])(struct nw_interp *, struct retval_priv *) =
+ {
+ [VALUE_TYPE_I32] = get_i32,
+ [VALUE_TYPE_I64] = get_i64,
+ [VALUE_TYPE_F32] = get_f32,
+ [VALUE_TYPE_F64] = get_f64
+ };
+
+ if (f[r->type](i, rp))
+ {
+ LOG("%s: callback for type %s failed\n", __func__,
+ value_type_tostr(v));
+ return -1;
+ }
+
+ rp->type = r->type;
+ return 0;
+}
+
+int interp_pop(struct nw_interp *const i)
+{
+ if (!i->fp)
+ {
+ static const char exc[] = "no frame pointer";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ const struct retval *const r = &i->fp->retval;
+ struct retval_priv rp;
+
+ if (r->returns && get_retval(i, &rp))
+ {
+ LOG("%s: get_retval failed\n", __func__);
+ return -1;
+ }
+
+#if 0
+#error TODO: do Wasm modules really have an exit status?
+#endif
+
+ if (!(i->fp = i->fp->prev))
+ {
+ /* Entry point is always assumed to return int, which is
+ * defined as an i32 variable according to Wasm. */
+ if (rp.type != VALUE_TYPE_I32)
+ {
+ LOG("%s: expected i32 return type, got %s\n", __func__,
+ value_type_tostr(rp.type));
+ i->exception = "unexpected return type";
+ return -1;
+ }
+
+ i->retval = rp.u.i32;
+ i->exit = true;
+ }
+
+ return 0;
+}
+
+int interp_stack_push(struct nw_interp *const i, const void *const src,
+ const size_t n)
+{
+ const size_t max = i->cfg.stack.n;
+ void *const dst = nwp_interp_stackptr(i);
+
+ if (max < n || i->stack_i > max - n)
+ {
+ static const char exc[] = "stack overflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ memcpy(dst, src, n);
+ i->stack_i += n;
+ return 0;
+}
+
+int interp_stack_pop(struct nw_interp *const i, void *const dst,
+ const size_t n)
+{
+ if (i->stack_i < n)
+ {
+ static const char exc[] = "stack underflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ i->stack_i -= n;
+ memcpy(dst, nwp_interp_stackptr(i), n);
+ return 0;
+}
+
+int interp_heap_store(struct nw_interp *const i, const size_t addr,
+ const void *const src, const size_t n)
+{
+ if (i->cfg.heap.n < n || addr > i->cfg.heap.n - n)
+ {
+ static const char exc[] = "heap overflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ void *const dst = (char *)i->cfg.heap.buf + addr;
+
+ memcpy(dst, src, n);
+ return 0;
+}
+
+int interp_heap_load(struct nw_interp *const i, const size_t addr,
+ void *const dst, const size_t n)
+{
+ const size_t max = i->cfg.heap.n;
+
+ if (max < n || addr > max - n)
+ {
+ static const char exc[] = "heap out-of-bounds access";
+
+ LOG("%s: %s (%lu, max %zu)\n", __func__, exc, (unsigned long)addr, max);
+ i->exception = exc;
+ return -1;
+ }
+
+ const void *const src = (const char *)i->cfg.heap.buf + addr;
+
+ memcpy(dst, src, n);
+ return 0;
+}
+
+int interp_global_push(struct nw_interp *const i, const void *const src,
+ const size_t n)
+{
+ const size_t max = i->cfg.global.n;
+ void *const dst = (char *)i->cfg.global.buf + i->global_i;
+
+ if (max < n || i->global_i > max - n)
+ {
+ static const char exc[] = "global memory overflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ memcpy(dst, src, n);
+ i->global_i += n;
+ return 0;
+}
+
+int interp_global_pop(struct nw_interp *const i, void *const dst,
+ const size_t n)
+{
+ const void *const src = (const char *)i->cfg.global.buf + i->global_i;
+
+ if (i->global_i < n)
+ {
+ static const char exc[] = "global memory underflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ i->global_i -= n;
+ memcpy(dst, src, n);
+ return 0;
+}
+
+void *interp_globalptr(const struct nw_interp *const i)
+{
+ return (char *)i->cfg.global.buf + i->global_i;
+}
diff --git a/src/interp/ops.c b/src/interp/ops.c
new file mode 100644
index 0000000..ea7c2b9
--- /dev/null
+++ b/src/interp/ops.c
@@ -0,0 +1,53 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/opcodes.h>
+#include <nw/ops.h>
+#include <nw/interp.h>
+
+static void (*const ops[])(struct nw_interp *) =
+{
+ [OP_UNREACHABLE] = nwp_op_unreachable,
+ [OP_NOP] = nwp_op_nop,
+ [OP_I32_LOAD] = nwp_op_i32_load,
+ [OP_I32_CONST] = nwp_op_i32_const,
+ [OP_END] = nwp_op_end,
+#if 0
+ [OP_BLOCK] = nwp_op_block,
+ [OP_LOOP] = nwp_op_loop,
+ [OP_IF] = nwp_op_if,
+ [OP_ELSE] = nwp_op_else,
+
+ [OP_BR] = nwp_op_br,
+ [OP_BR_IF] = nwp_op_br_if,
+ [OP_BR_TABLE] = nwp_op_br_table,
+ [OP_RETURN] = nwp_op_return,
+ [OP_CALL] = nwp_op_call,
+ [OP_CALL_INDIRECT] = nwp_op_call_indirect,
+ [OP_GET_LOCAL] = nwp_op_get_local,
+ [OP_SET_LOCAL] = nwp_op_set_local,
+ [OP_TEE_LOCAL] = nwp_op_tee_local,
+ [OP_GET_GLOBAL] = nwp_op_get_global,
+ [OP_SET_GLOBAL] = nwp_op_set_global,
+
+ [OP_I32_STORE] = nwp_op_i32_store,
+
+ [OP_I64_CONST] = nwp_op_i64_const,
+ [OP_F32_CONST] = nwp_op_f32_const,
+ [OP_F64_CONST] = nwp_op_f64_const,
+ [OP_I32_SUB] = nwp_op_i32_sub
+#endif
+};
+
+const struct nwp_ops nwp_ops =
+{
+ .ops = ops,
+ .n = sizeof ops / sizeof *ops
+};
diff --git a/src/interp/resume.c b/src/interp/resume.c
new file mode 100644
index 0000000..e808889
--- /dev/null
+++ b/src/interp/resume.c
@@ -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/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+
+void nwp_interp_resume(struct nw_interp *const i)
+{
+ if (i->set)
+ nwp_interp_limited(i);
+ else
+ nwp_interp_full(i);
+}
diff --git a/src/interp/routines/CMakeLists.txt b/src/interp/routines/CMakeLists.txt
new file mode 100644
index 0000000..eb76ed3
--- /dev/null
+++ b/src/interp/routines/CMakeLists.txt
@@ -0,0 +1,12 @@
+# 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
+ find_export.c
+ full.c
+ limited.c
+)
diff --git a/src/interp/routines/find_export.c b/src/interp/routines/find_export.c
new file mode 100644
index 0000000..f82e5f9
--- /dev/null
+++ b/src/interp/routines/find_export.c
@@ -0,0 +1,202 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <inttypes.h>
+#include <string.h>
+
+static enum nw_state entry_loop(struct nw_interp *i);
+
+static enum nw_state get_index(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_exp *const e = &i->sm.export;
+ struct nw_sm_leb128 *const l = &e->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &e->index);
+
+ if (n)
+ return n;
+
+ i->next = e->next;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_kind(struct nw_interp *const i)
+{
+ uint8_t kind;
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_sm_io io = {.buf = &kind, .n = sizeof kind};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+ else if (kind >= NW_KINDS)
+ {
+ static const char exc[] = "invalid export kind";
+
+ i->exception = exc;
+ LOG("%s: %s: %" PRIu8 "\n", __func__, exc, kind);
+ return NW_FATAL;
+ }
+
+ struct nw_i_sm_exp *const e = &i->sm.export;
+
+ e->kind = kind;
+ i->next = get_index;
+ return NW_AGAIN;
+}
+
+static enum nw_state skip_index(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_exp *const e = &i->sm.export;
+ struct nw_sm_leb128 *const l = &e->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &(nw_varuint32){0});
+
+ if (n)
+ return n;
+
+ e->entry_i++;
+ i->next = entry_loop;
+ return NW_AGAIN;
+}
+
+static enum nw_state skip_kind(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_sm_io io = {.buf = &(uint8_t){0}, .n = sizeof (uint8_t)};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+
+ i->next = skip_index;
+ return NW_AGAIN;
+}
+
+static enum nw_state skip_field_str(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ const struct nw_i_sm_exp *const e = &i->sm.export;
+ const long offset = e->len - e->len_i;
+ const enum nw_state n = cfg->seek(offset, NW_SEEK_CUR, cfg->user);
+
+ if (n)
+ return n;
+
+ i->next = skip_kind;
+ return NW_AGAIN;
+}
+
+static enum nw_state compare(struct nw_interp *const i)
+{
+ uint8_t byte;
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_exp *const e = &i->sm.export;
+ struct nw_sm_io io = {.buf = &byte, .n = sizeof byte};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+ else if (byte != e->sym[e->len_i++])
+ i->next = skip_field_str;
+ else if (e->len_i >= e->len)
+ i->next = get_kind;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_len(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_exp *const e = &i->sm.export;
+ struct nw_sm_leb128 *const l = &e->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &e->len);
+
+ if (n)
+ return n;
+ else if (e->len != strlen(e->sym))
+ i->next = skip_field_str;
+ else
+ i->next = compare;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_interp *const i)
+{
+ struct nw_i_sm_exp *const e = &i->sm.export;
+
+ if (e->entry_i >= e->count)
+ {
+ static const char exc[] = "failed to find symbol";
+
+ i->exception = exc;
+ LOG("%s: %s: %s\n", __func__, exc, e->sym);
+ return NW_FATAL;
+ }
+
+ e->len_i = 0;
+ i->next = get_len;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_exp *const e = &i->sm.export;
+ struct nw_sm_leb128 *const l = &e->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &e->count);
+
+ if (n)
+ return n;
+
+ i->next = entry_loop;
+ return NW_AGAIN;
+}
+
+static enum nw_state seek(struct nw_interp *const i)
+{
+ const struct nw_mod *const m = i->cfg.m;
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+
+ if (!m->sections[NW_SECTION_EXPORT])
+ {
+ static const char exc[] = "section not found";
+
+ i->exception = exc;
+ LOG("%s: %s: %s\n", __func__, exc, "export");
+ return NW_FATAL;
+ }
+
+ const enum nw_state n = cfg->seek(m->sections[NW_SECTION_EXPORT],
+ NW_SEEK_SET, cfg->user);
+
+ if (n)
+ return n;
+
+ i->next = get_count;
+ return NW_AGAIN;
+}
+
+void nwp_find_export(struct nw_interp *i, const char *sym,
+ enum nw_state (*const next)(struct nw_interp *))
+{
+ i->next = seek;
+ i->sm.export = (const struct nw_i_sm_exp)
+ {
+ .sym = sym,
+ .next = next
+ };
+}
diff --git a/src/interp/routines/full.c b/src/interp/routines/full.c
new file mode 100644
index 0000000..c60432d
--- /dev/null
+++ b/src/interp/routines/full.c
@@ -0,0 +1,48 @@
+/*
+ * 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/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/opcodes.h>
+#include <nw/ops.h>
+#include <nw/routines.h>
+#include <inttypes.h>
+
+static enum nw_state get_bytecode(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_b *const b = &i->sm.bytecode;
+ const enum nw_state n = nwp_io_read(cfg, &b->io);
+
+ if (n)
+ return n;
+ else if (b->op >= nwp_ops.n)
+ {
+ static const char exc[] = "invalid opcode";
+
+ LOG("%s: %s: %#" PRIx8 "\n", __func__, exc, b->op);
+ i->exception = exc;
+ }
+
+ nwp_ops.ops[b->op](i);
+ return NW_AGAIN;
+}
+
+void nwp_interp_full(struct nw_interp *const i)
+{
+ struct nw_i_sm_b *const b = &i->sm.bytecode;
+
+ i->next = get_bytecode;
+ b->io = (const struct nw_sm_io)
+ {
+ .buf = &b->op,
+ .n = sizeof b->op
+ };
+}
diff --git a/src/interp/routines/limited.c b/src/interp/routines/limited.c
new file mode 100644
index 0000000..766b2d3
--- /dev/null
+++ b/src/interp/routines/limited.c
@@ -0,0 +1,54 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <stddef.h>
+#include <inttypes.h>
+
+static enum nw_state get_bytecode(struct nw_interp *const in)
+{
+ const struct nw_io_cfg *const cfg = &in->cfg.io;
+ struct nw_i_sm_b *const b = &in->sm.bytecode;
+ const enum nw_state n = nwp_io_read(cfg, &b->io);
+
+ if (n)
+ return n;
+
+ const struct nwp_interp_set *const set = in->set;
+
+ for (size_t i = 0; i < set->n; i++)
+ if (b->op == set->opcodes[i])
+ {
+ nwp_ops.ops[b->op](in);
+ return NW_AGAIN;
+ }
+
+ static const char exc[] = "invalid opcode";
+
+ LOG("%s: %s: %#" PRIx8 "\n", __func__, exc, b->op);
+ in->exception = exc;
+ return NW_FATAL;
+}
+
+void nwp_interp_limited(struct nw_interp *const i)
+{
+ struct nw_i_sm_b *const b = &i->sm.bytecode;
+
+ i->next = get_bytecode;
+ b->io = (const struct nw_sm_io)
+ {
+ .buf = &b->op,
+ .n = sizeof b->op
+ };
+}
diff --git a/src/interp/run.c b/src/interp/run.c
new file mode 100644
index 0000000..ef7f614
--- /dev/null
+++ b/src/interp/run.c
@@ -0,0 +1,16 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+
+enum nw_state nwp_interp_run(struct nw_interp *const i)
+{
+ return i->next(i);
+}
diff --git a/src/interp/stack_pop.c b/src/interp/stack_pop.c
new file mode 100644
index 0000000..8650be1
--- /dev/null
+++ b/src/interp/stack_pop.c
@@ -0,0 +1,31 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <stddef.h>
+#include <string.h>
+
+int interp_stack_pop(struct nw_interp *const i, void *const dst,
+ const size_t n)
+{
+ if (i->stack_i < n)
+ {
+ static const char exc[] = "stack underflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ i->stack_i -= n;
+ memcpy(dst, nwp_interp_stackptr(i), n);
+ return 0;
+}
diff --git a/src/interp/stack_push.c b/src/interp/stack_push.c
new file mode 100644
index 0000000..2145423
--- /dev/null
+++ b/src/interp/stack_push.c
@@ -0,0 +1,34 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <stddef.h>
+#include <string.h>
+
+int nwp_interp_stack_push(struct nw_interp *const i, const void *const src,
+ const size_t n)
+{
+ const size_t max = i->cfg.stack.n;
+ void *const dst = nwp_interp_stackptr(i);
+
+ if (max < n || i->stack_i > max - n)
+ {
+ static const char exc[] = "stack overflow";
+
+ LOG("%s: %s\n", __func__, exc);
+ i->exception = exc;
+ return -1;
+ }
+
+ memcpy(dst, src, n);
+ i->stack_i += n;
+ return 0;
+}
diff --git a/src/interp/stackptr.c b/src/interp/stackptr.c
new file mode 100644
index 0000000..6b95e1d
--- /dev/null
+++ b/src/interp/stackptr.c
@@ -0,0 +1,16 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+
+void *nwp_interp_stackptr(const struct nw_interp *const i)
+{
+ return (char *)i->cfg.stack.buf + i->stack_i;
+}
diff --git a/src/interp/start.c b/src/interp/start.c
new file mode 100644
index 0000000..9d98ccb
--- /dev/null
+++ b/src/interp/start.c
@@ -0,0 +1,26 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+
+int nwp_interp_start(const struct nw_interp_cfg *const cfg,
+ const struct nwp_interp_set *const set,
+ struct nw_interp *const i)
+{
+ *i = (const struct nw_interp)
+ {
+ .cfg = *cfg,
+ .set = set
+ };
+
+ nwp_interp_resume(i);
+ return 0;
+}
diff --git a/src/io/CMakeLists.txt b/src/io/CMakeLists.txt
new file mode 100644
index 0000000..a9f97ca
--- /dev/null
+++ b/src/io/CMakeLists.txt
@@ -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/.
+
+target_sources(${PROJECT_NAME} PRIVATE
+ leb128.c
+ read.c
+ varint1.c
+ varint7.c
+ varint64.c
+ varuint1.c
+ varuint7.c
+ varuint32.c
+ varuint64.c
+)
diff --git a/src/io/leb128.c b/src/io/leb128.c
new file mode 100644
index 0000000..a57d5c7
--- /dev/null
+++ b/src/io/leb128.c
@@ -0,0 +1,47 @@
+/*
+ * 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 <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+enum nw_state nwp_leb128(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, const unsigned maxbits, const bool sign)
+{
+ uint8_t byte;
+
+ for (;;)
+ {
+ const int n = cfg->read(&byte, sizeof byte, cfg->user);
+
+ if (n < 0)
+ return NW_FATAL;
+ else if (!n)
+ return NW_AGAIN;
+
+ l->result |= (unsigned long long)(byte & 0x7f) << l->shift;
+ l->shift += 7;
+
+ if (!(byte & 0x80))
+ break;
+ else if (++l->bcnt > (maxbits + 7u - 1u) / 7u)
+ {
+ LOG("%s: overflow\n", __func__);
+ return NW_FATAL;
+ }
+ }
+
+ if (sign && (l->shift < maxbits) && (byte & 0x40))
+ l->result |= -1ll << l->shift;
+
+ return NW_OK;
+}
diff --git a/src/io/read.c b/src/io/read.c
new file mode 100644
index 0000000..2d64257
--- /dev/null
+++ b/src/io/read.c
@@ -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/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <stddef.h>
+#include <stdint.h>
+
+enum nw_state nwp_io_read(const struct nw_io_cfg *const cfg,
+ struct nw_sm_io *const io)
+{
+ void *const buf = (uint8_t *)io->buf + io->read;
+ const size_t rem = io->n - io->read;
+ const int n = cfg->read(buf, rem, cfg->user);
+
+ if (n < 0)
+ return NW_FATAL;
+
+ if ((io->read += n) >= io->n)
+ return NW_OK;
+
+ return NW_AGAIN;
+}
diff --git a/src/io/varint1.c b/src/io/varint1.c
new file mode 100644
index 0000000..74a577f
--- /dev/null
+++ b/src/io/varint1.c
@@ -0,0 +1,27 @@
+/*
+ * 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 <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varint1(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varint1 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 1, true);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/io/varint64.c b/src/io/varint64.c
new file mode 100644
index 0000000..f0faa5b
--- /dev/null
+++ b/src/io/varint64.c
@@ -0,0 +1,27 @@
+/*
+ * 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 <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varint64(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varuint32 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 64, true);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/io/varint7.c b/src/io/varint7.c
new file mode 100644
index 0000000..77b9006
--- /dev/null
+++ b/src/io/varint7.c
@@ -0,0 +1,26 @@
+/*
+ * 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 <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varint7(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varint7 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 7, true);
+
+ if (ret)
+ return ret;
+
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ return ret;
+}
diff --git a/src/io/varuint1.c b/src/io/varuint1.c
new file mode 100644
index 0000000..3760f9d
--- /dev/null
+++ b/src/io/varuint1.c
@@ -0,0 +1,27 @@
+/*
+ * 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 <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varuint1(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varuint1 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 1, false);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/io/varuint32.c b/src/io/varuint32.c
new file mode 100644
index 0000000..40e2030
--- /dev/null
+++ b/src/io/varuint32.c
@@ -0,0 +1,27 @@
+/*
+ * 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 <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varuint32(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varuint32 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 32, false);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/io/varuint64.c b/src/io/varuint64.c
new file mode 100644
index 0000000..e01ce69
--- /dev/null
+++ b/src/io/varuint64.c
@@ -0,0 +1,27 @@
+/*
+ * 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 <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varuint64(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varuint64 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 64, false);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/io/varuint7.c b/src/io/varuint7.c
new file mode 100644
index 0000000..b20c00c
--- /dev/null
+++ b/src/io/varuint7.c
@@ -0,0 +1,27 @@
+/*
+ * 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 <nanowasm/types.h>
+#include <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varuint7(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varuint7 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 7, false);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/load.c b/src/load.c
new file mode 100644
index 0000000..32d9ff3
--- /dev/null
+++ b/src/load.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 <nanowasm/nw.h>
+#include <nw/types.h>
+
+enum nw_state nw_load(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+
+ if (cfg->eof(cfg->user))
+ return NW_OK;
+
+ return m->next(m);
+}
diff --git a/src/op/CMakeLists.txt b/src/op/CMakeLists.txt
new file mode 100644
index 0000000..cea137b
--- /dev/null
+++ b/src/op/CMakeLists.txt
@@ -0,0 +1,16 @@
+# 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
+ end.c
+ i32_const.c
+ i32_load.c
+ nop.c
+ unreachable.c
+)
+
+add_subdirectory(check)
diff --git a/src/op/check/CMakeLists.txt b/src/op/check/CMakeLists.txt
new file mode 100644
index 0000000..810d3d6
--- /dev/null
+++ b/src/op/check/CMakeLists.txt
@@ -0,0 +1,19 @@
+# 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_if.c
+ call.c
+ end.c
+ global_index.c
+ i32_const.c
+ i64_const.c
+ local_index.c
+ memory_immediate.c
+ no_immediate.c
+)
diff --git a/src/op/check/block.c b/src/op/check/block.c
new file mode 100644
index 0000000..c58c0f6
--- /dev/null
+++ b/src/op/check/block.c
@@ -0,0 +1,34 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/ops.h>
+
+static enum nw_state get_type(struct nw_mod *const m)
+{
+ nw_varint7 type;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varint7(cfg, l, &type);
+
+ if (n)
+ return n;
+
+ /* TODO: check block type. */
+ c->fn.blocks++;
+ m->next = m->sm.code.next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_block(struct nw_mod *const m)
+{
+ m->next = get_type;
+}
diff --git a/src/op/check/br_if.c b/src/op/check/br_if.c
new file mode 100644
index 0000000..6e6bd10
--- /dev/null
+++ b/src/op/check/br_if.c
@@ -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/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state get_relative_depth(struct nw_mod *const m)
+{
+ nw_varuint32 relative_depth;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &relative_depth);
+
+ if (n)
+ return n;
+ else if (relative_depth >= c->fn.blocks)
+ {
+ LOG("%s: invalid relative depth %lu\n", __func__,
+ (unsigned long)relative_depth);
+ return NW_FATAL;
+ }
+
+ m->next = c->next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_br_if(struct nw_mod *const m)
+{
+ m->next = get_relative_depth;
+}
diff --git a/src/op/check/call.c b/src/op/check/call.c
new file mode 100644
index 0000000..eaa826f
--- /dev/null
+++ b/src/op/check/call.c
@@ -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/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state get_index(struct nw_mod *const m)
+{
+ nw_varuint32 index;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &index);
+
+ if (n)
+ return n;
+ else if (index >= c->count + m->cfg.n_imports)
+ {
+ LOG("%s: invalid function index %lu\n", __func__,
+ (unsigned long)index);
+ return NW_FATAL;
+ }
+
+ m->next = m->sm.code.next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_call(struct nw_mod *const m)
+{
+ m->next = get_index;
+}
diff --git a/src/op/check/end.c b/src/op/check/end.c
new file mode 100644
index 0000000..7c614bb
--- /dev/null
+++ b/src/op/check/end.c
@@ -0,0 +1,35 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state run(struct nw_mod *const m)
+{
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_c_fn *const fn = &c->fn;
+
+ if (!fn->blocks)
+ {
+ LOG("%s: unexpected end in function %lu\n", __func__,
+ (unsigned long)c->entry_i);
+ return NW_FATAL;
+ }
+
+ fn->blocks--;
+ m->next = c->next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_end(struct nw_mod *const m)
+{
+ m->next = run;
+}
diff --git a/src/op/check/global_index.c b/src/op/check/global_index.c
new file mode 100644
index 0000000..5cfceb3
--- /dev/null
+++ b/src/op/check/global_index.c
@@ -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/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state get_index(struct nw_mod *const m)
+{
+ nw_varuint32 index;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &index);
+
+ if (n)
+ return n;
+ else if (index >= m->global_count)
+ {
+ LOG("%s: invalid global index %lu\n", __func__,
+ (unsigned long)index);
+ return NW_FATAL;
+ }
+
+ m->next = c->next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_global_index(struct nw_mod *const m)
+{
+ m->next = get_index;
+}
diff --git a/src/op/check/i32_const.c b/src/op/check/i32_const.c
new file mode 100644
index 0000000..71a90b3
--- /dev/null
+++ b/src/op/check/i32_const.c
@@ -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/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/ops.h>
+
+static enum nw_state get_value(struct nw_mod *const m)
+{
+ nw_varuint32 value;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &value);
+
+ if (n)
+ return n;
+
+ m->next = c->next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_i32_const(struct nw_mod *const m)
+{
+ m->next = get_value;
+}
diff --git a/src/op/check/i64_const.c b/src/op/check/i64_const.c
new file mode 100644
index 0000000..4d4d066
--- /dev/null
+++ b/src/op/check/i64_const.c
@@ -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/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/ops.h>
+
+static enum nw_state get_value(struct nw_mod *const m)
+{
+ nw_varuint64 value;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint64(cfg, l, &value);
+
+ if (n)
+ return n;
+
+ m->next = c->next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_i64_const(struct nw_mod *const m)
+{
+ m->next = get_value;
+}
diff --git a/src/op/check/local_index.c b/src/op/check/local_index.c
new file mode 100644
index 0000000..1b991b1
--- /dev/null
+++ b/src/op/check/local_index.c
@@ -0,0 +1,43 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state get_index(struct nw_mod *const m)
+{
+ nw_varuint32 index;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &index);
+
+ if (n)
+ return n;
+#if 0
+ else if (index >= c->fn.local_total)
+ {
+ LOG("%s: invalid local index %lu in function %lu\n", __func__,
+ (unsigned long)index, (unsigned long)c->entry_i);
+ return NW_FATAL;
+ }
+#else
+#warning TODO
+#endif
+
+ m->next = c->next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_local_index(struct nw_mod *const m)
+{
+ m->next = get_index;
+}
diff --git a/src/op/check/memory_immediate.c b/src/op/check/memory_immediate.c
new file mode 100644
index 0000000..2232370
--- /dev/null
+++ b/src/op/check/memory_immediate.c
@@ -0,0 +1,47 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/ops.h>
+
+static enum nw_state get_offset(struct nw_mod *const m)
+{
+ nw_varuint32 offset;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &offset);
+
+ if (n)
+ return n;
+
+ m->next = m->sm.code.next;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_flags(struct nw_mod *const m)
+{
+ nw_varuint32 flags;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &flags);
+
+ if (n)
+ return n;
+
+ m->next = get_offset;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_memory_immediate(struct nw_mod *const m)
+{
+ m->next = get_flags;
+}
diff --git a/src/op/check/no_immediate.c b/src/op/check/no_immediate.c
new file mode 100644
index 0000000..cf1dd21
--- /dev/null
+++ b/src/op/check/no_immediate.c
@@ -0,0 +1,23 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/ops.h>
+
+static enum nw_state run(struct nw_mod *const m)
+{
+ m->next = m->sm.code.next;
+ return NW_AGAIN;
+}
+
+void nwp_op_check_no_immediate(struct nw_mod *const m)
+{
+ m->next = run;
+}
diff --git a/src/op/end.c b/src/op/end.c
new file mode 100644
index 0000000..8ec6e7c
--- /dev/null
+++ b/src/op/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 <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state run(struct nw_interp *const i)
+{
+ /* TODO: this is not correct. end can appear anywhere in a function. */
+ return NW_OK;
+}
+
+void nwp_op_end(struct nw_interp *const i)
+{
+ i->next = run;
+}
diff --git a/src/op/i32_const.c b/src/op/i32_const.c
new file mode 100644
index 0000000..dbd0e91
--- /dev/null
+++ b/src/op/i32_const.c
@@ -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/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state get_value(struct nw_interp *const i)
+{
+ nw_varuint32 value;
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_sm_leb128 *const l = &i->sm.i32_const.leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &value);
+
+ if (n)
+ return n;
+ else if (nwp_interp_stack_push(i, &value, sizeof value))
+ {
+ LOG("%s: nwp_interp_stack_push failed\n", __func__);
+ return NW_FATAL;
+ }
+
+ nwp_interp_resume(i);
+ return NW_AGAIN;
+}
+
+void nwp_op_i32_const(struct nw_interp *const i)
+{
+ i->next = get_value;
+ i->sm.i32_const = (const struct nw_i_sm_i32_const){0};
+}
diff --git a/src/op/i32_load.c b/src/op/i32_load.c
new file mode 100644
index 0000000..615cfc4
--- /dev/null
+++ b/src/op/i32_load.c
@@ -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/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+void nwp_op_i32_load(struct nw_interp *const i)
+{
+ /* TODO */
+}
diff --git a/src/op/nop.c b/src/op/nop.c
new file mode 100644
index 0000000..6924b9a
--- /dev/null
+++ b/src/op/nop.c
@@ -0,0 +1,23 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/ops.h>
+
+static enum nw_state run(struct nw_interp *const i)
+{
+ nwp_interp_resume(i);
+ return NW_AGAIN;
+}
+
+void nwp_op_nop(struct nw_interp *const i)
+{
+ i->next = run;
+}
diff --git a/src/op/unreachable.c b/src/op/unreachable.c
new file mode 100644
index 0000000..77b711b
--- /dev/null
+++ b/src/op/unreachable.c
@@ -0,0 +1,27 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <nw/ops.h>
+
+static enum nw_state run(struct nw_interp *const i)
+{
+ static const char exc[] = "unreachable";
+
+ i->exception = exc;
+ LOG("%s: %s\n", __func__, exc);
+ return NW_FATAL;
+}
+
+void nwp_op_unreachable(struct nw_interp *const i)
+{
+ i->next = run;
+}
diff --git a/src/routines/CMakeLists.txt b/src/routines/CMakeLists.txt
new file mode 100644
index 0000000..3141a4e
--- /dev/null
+++ b/src/routines/CMakeLists.txt
@@ -0,0 +1,17 @@
+# 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
+ call.c
+ check_magic.c
+ check_version.c
+ find_function.c
+ get_function_type.c
+ section.c
+)
+
+add_subdirectory(section)
diff --git a/src/routines/call.c b/src/routines/call.c
new file mode 100644
index 0000000..c04c4f8
--- /dev/null
+++ b/src/routines/call.c
@@ -0,0 +1,33 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state push_locals(struct nw_interp *const i)
+{
+ LOG("%s: TODO\n", __func__);
+ return NW_FATAL;
+}
+
+static enum nw_state find_function(struct nw_interp *const i)
+{
+ const struct nw_i_sm_type *const t = &i->sm.type;
+
+ nwp_find_function(i, t->index, push_locals);
+ return NW_AGAIN;
+}
+
+void nwp_call(struct nw_interp *const i, const nw_varuint32 index)
+{
+ nwp_get_function_type(i, index, find_function);
+}
diff --git a/src/routines/check_magic.c b/src/routines/check_magic.c
new file mode 100644
index 0000000..bb49cff
--- /dev/null
+++ b/src/routines/check_magic.c
@@ -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/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <string.h>
+
+static enum nw_state run(struct nw_mod *const m)
+{
+ struct nw_sm_cm *const cm = &m->sm.check_magic;
+ const enum nw_state n = nwp_io_read(&m->cfg.io, &cm->io);
+
+ if (n)
+ return n;
+
+ static const uint8_t magic[] = {'\0', 'a', 's', 'm'};
+
+ if (memcmp(cm->buf, magic, sizeof cm->buf))
+ {
+ LOG("%s: wrong magic bytes\n", __func__);
+ return NW_FATAL;
+ }
+
+ nwp_check_version(m);
+ return NW_AGAIN;
+}
+
+void nwp_check_magic(struct nw_mod *const m)
+{
+ struct nw_sm_cm *const cm = &m->sm.check_magic;
+
+ *cm = (const struct nw_sm_cm)
+ {
+ .io =
+ {
+ .buf = &cm->buf,
+ .n = sizeof cm->buf
+ }
+ };
+
+ m->next = run;
+}
diff --git a/src/routines/check_version.c b/src/routines/check_version.c
new file mode 100644
index 0000000..9632054
--- /dev/null
+++ b/src/routines/check_version.c
@@ -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/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <string.h>
+
+static enum nw_state run(struct nw_mod *const m)
+{
+ struct nw_sm_cv *const cv = &m->sm.check_version;
+ const enum nw_state n = nwp_io_read(&m->cfg.io, &cv->io);
+
+ if (n)
+ return n;
+
+ static const uint8_t version[] = {1, 0, 0, 0};
+
+ if (memcmp(&cv->version, version, sizeof cv->version))
+ {
+ LOG("%s: wrong version bytes\n", __func__);
+ return NW_FATAL;
+ }
+
+ nwp_section(m);
+ return NW_AGAIN;
+}
+
+void nwp_check_version(struct nw_mod *const m)
+{
+ struct nw_sm_cv *const cv = &m->sm.check_version;
+
+ *cv = (const struct nw_sm_cv)
+ {
+ .io =
+ {
+ .buf = &cv->version,
+ .n = sizeof cv->version
+ }
+ };
+
+ m->next = run;
+}
diff --git a/src/routines/find_func_type.c b/src/routines/find_func_type.c
new file mode 100644
index 0000000..aa11a51
--- /dev/null
+++ b/src/routines/find_func_type.c
@@ -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/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+void nwp_find_func_type(struct nw_mod *const m, nw_varuint32
+ enum nw_state (*const next)(struct nw_interp *))
+{
+
+}
diff --git a/src/routines/find_function.c b/src/routines/find_function.c
new file mode 100644
index 0000000..805de82
--- /dev/null
+++ b/src/routines/find_function.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 <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+void nwp_find_function(struct nw_interp *const i, const nw_varuint32 index,
+ enum nw_state (*const next)(struct nw_interp *))
+{
+ /* TODO */
+}
diff --git a/src/routines/get_function_type.c b/src/routines/get_function_type.c
new file mode 100644
index 0000000..61f90f2
--- /dev/null
+++ b/src/routines/get_function_type.c
@@ -0,0 +1,256 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state get_form(struct nw_interp *);
+
+static enum nw_state push(struct nw_interp *const i)
+{
+ const struct nw_i_sm_type *const t = &i->sm.type;
+ const struct nw_i_sm_type_out *const out = &t->out;
+
+ if (nwp_interp_stack_push(i, out, sizeof *out))
+ {
+ LOG("%s: nwp_interp_stack_push failed\n", __func__);
+ return NW_FATAL;
+ }
+
+ i->next = t->next;
+ return NW_AGAIN;
+}
+
+static enum nw_state skip(struct nw_interp *const i)
+{
+ struct nw_i_sm_type *const t = &i->sm.type;
+
+ t->entry_i++;
+ t->param_i = 0;
+ i->next = get_form;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_return_type(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ nw_varint7 type;
+ const enum nw_state n = nwp_varint7(cfg, l, &type);
+
+ if (n)
+ return n;
+
+ t->out.return_type = type;
+ i->next = t->entry_i == t->type_index ? push : skip;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_return_count(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ nw_varuint1 return_count;
+ const enum nw_state n = nwp_varuint1(cfg, l, &return_count);
+
+ if (n)
+ return n;
+ else if (t->entry_i == t->type_index)
+ i->next = return_count ? get_return_type : push;
+ else
+ i->next = return_count ? get_return_type : skip;
+
+ t->out.return_count = return_count;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_param_type(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ nw_varint7 type;
+ const enum nw_state n = nwp_varint7(cfg, l, &type);
+
+ if (n)
+ return n;
+ else if (t->entry_i == t->type_index
+ && nwp_interp_stack_push(i, &type, sizeof type))
+ {
+ LOG("%s: nwp_interp_stack_push failed\n", __func__);
+ return NW_FATAL;
+ }
+ else if (++t->param_i >= t->out.param_count)
+ i->next = get_return_count;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_param_count(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ nw_varuint32 param_count;
+ const enum nw_state n = nwp_varuint32(cfg, l, &param_count);
+
+ if (n)
+ return n;
+
+ t->out.param_count = param_count;
+ i->next = param_count ? get_param_type : get_return_count;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_form(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ nw_varint7 form;
+ const enum nw_state n = nwp_varint7(cfg, l, &form);
+
+ if (n)
+ return n;
+ else if (t->entry_i == t->type_index && form != 0x60)
+ {
+ static const char exc[] = "type index not a function";
+
+ i->exception = exc;
+ LOG("%s: %s: %lu\n", __func__, exc, (unsigned long)t->entry_i);
+ return NW_FATAL;
+ }
+
+ t->out.form = form;
+ i->next = get_param_count;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_type_count(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &t->count);
+
+ if (n)
+ return n;
+ else if (t->count <= t->type_index)
+ {
+ static const char exc[] = "invalid type index";
+
+ i->exception = exc;
+ LOG("%s: %s: %lu\n", __func__, exc, (unsigned long)t->type_index);
+ return NW_FATAL;
+ }
+
+ t->entry_i = 0;
+ i->next = get_form;
+ return NW_AGAIN;
+}
+
+static enum nw_state seek_type(struct nw_interp *const i)
+{
+ const struct nw_interp_cfg *const icfg = &i->cfg;
+ const struct nw_io_cfg *const cfg = &icfg->io;
+ const long offset = icfg->m->sections[NW_SECTION_TYPE];
+
+ if (!offset)
+ {
+ static const char exc[] = "type section not found";
+
+ LOG("%s: %s\n", __func__, exc);
+ return NW_FATAL;
+ }
+
+ const enum nw_state n = cfg->seek(offset, NW_SEEK_SET, cfg->user);
+
+ if (n)
+ return n;
+
+ i->next = get_type_count;
+ return NW_AGAIN;
+}
+
+static enum nw_state find_entry(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &t->type_index);
+
+ if (n)
+ return n;
+ else if (t->entry_i++ == t->index)
+ i->next = seek_type;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_interp *const i)
+{
+ const struct nw_io_cfg *const cfg = &i->cfg.io;
+ struct nw_i_sm_type *const t = &i->sm.type;
+ struct nw_sm_leb128 *const l = &t->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &t->count);
+
+ if (n)
+ return n;
+ else if (t->count <= t->index)
+ {
+ static const char exc[] = "could not find function index";
+
+ i->exception = exc;
+ LOG("%s: %s: %lu\n", __func__, exc, (unsigned long)t->index);
+ return NW_FATAL;
+ }
+
+ i->next = find_entry;
+ return NW_AGAIN;
+}
+
+static enum nw_state seek_fn(struct nw_interp *const i)
+{
+ const struct nw_interp_cfg *const icfg = &i->cfg;
+ const struct nw_io_cfg *const cfg = &icfg->io;
+ const long offset = icfg->m->sections[NW_SECTION_FUNCTION];
+
+ if (!offset)
+ {
+ static const char exc[] = "type section not found";
+
+ LOG("%s: %s\n", __func__, exc);
+ return NW_FATAL;
+ }
+
+ const enum nw_state n = cfg->seek(offset, NW_SEEK_SET, cfg->user);
+
+ if (n)
+ return n;
+
+ i->next = get_count;
+ return NW_AGAIN;
+}
+
+void nwp_get_function_type(struct nw_interp *const i, const nw_varuint32 index,
+ enum nw_state (*const next)(struct nw_interp *))
+{
+ i->next = seek_fn;
+ i->sm.type = (const struct nw_i_sm_type)
+ {
+ .index = index,
+ .next = next
+ };
+}
diff --git a/src/routines/section.c b/src/routines/section.c
new file mode 100644
index 0000000..7ed8899
--- /dev/null
+++ b/src/routines/section.c
@@ -0,0 +1,94 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static void (*const fn[])(struct nw_mod *) =
+{
+ nwp_section_custom,
+ nwp_section_type,
+ nwp_section_import,
+ nwp_section_function,
+ nwp_section_table,
+ nwp_section_memory,
+ nwp_section_global,
+ nwp_section_export,
+ nwp_section_start,
+ nwp_section_element,
+ nwp_section_code,
+ nwp_section_data,
+};
+
+static enum nw_state get_offset(struct nw_mod *const m)
+{
+ struct nw_mod_section *const s = &m->section;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->tell(&s->offset, cfg->user);
+ long *const offset = &m->sections[s->section];
+
+ if (n)
+ return n;
+ /* Custom section can appear more than once. */
+ else if (s->section != NW_SECTION_CUSTOM && *offset)
+ {
+ LOG("%s: ignoring duplicate section %hhu\n", __func__,
+ (unsigned char)s->section);
+ return nwp_section_skip(m);
+ }
+
+ *offset = s->offset;
+ fn[s->section](m);
+ return NW_AGAIN;
+}
+
+static enum nw_state get_length(struct nw_mod *const m)
+{
+ struct nw_mod_section *const s = &m->section;
+ struct nw_sm_leb128 *const l = &s->leb128;
+ const enum nw_state n = nwp_varuint32(&m->cfg.io, l, &s->len);
+
+ if (n)
+ return n;
+
+ m->next = get_offset;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_section(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_mod_section *const s = &m->section;
+ struct nw_sm_leb128 *const l = &s->leb128;
+ const enum nw_state n = nwp_varuint7(cfg, l, &s->section);
+
+ if (n)
+ {
+ if (cfg->eof(cfg->user))
+ return NW_OK;
+
+ return n;
+ }
+ else if (s->section >= sizeof fn / sizeof *fn)
+ {
+ LOG("%s: invalid section %u\n", __func__, (unsigned)s->section);
+ return NW_FATAL;
+ }
+
+ m->next = get_length;
+ return NW_AGAIN;
+}
+
+void nwp_section(struct nw_mod *const m)
+{
+ m->section = (const struct nw_mod_section){0};
+ m->next = get_section;
+}
diff --git a/src/routines/section/CMakeLists.txt b/src/routines/section/CMakeLists.txt
new file mode 100644
index 0000000..7561069
--- /dev/null
+++ b/src/routines/section/CMakeLists.txt
@@ -0,0 +1,23 @@
+# 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
+ code.c
+ custom.c
+ data.c
+ element.c
+ exit.c
+ export.c
+ function.c
+ global.c
+ import.c
+ memory.c
+ skip.c
+ start.c
+ table.c
+ type.c
+)
diff --git a/src/routines/section/code.c b/src/routines/section/code.c
new file mode 100644
index 0000000..dd50c41
--- /dev/null
+++ b/src/routines/section/code.c
@@ -0,0 +1,271 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/opcodes.h>
+#include <nw/ops.h>
+#include <nw/routines.h>
+#include <inttypes.h>
+
+static enum nw_state local_loop(struct nw_mod *);
+static enum nw_state get_opcode(struct nw_mod *);
+static enum nw_state entry_loop(struct nw_mod *);
+
+static void (*const checks[])(struct nw_mod *) =
+{
+ [OP_UNREACHABLE] = nwp_op_check_no_immediate,
+ [OP_NOP] = nwp_op_check_no_immediate,
+ [OP_END] = nwp_op_check_end,
+ [OP_BLOCK] = nwp_op_check_block,
+ [OP_BR_IF] = nwp_op_check_br_if,
+ [OP_RETURN] = nwp_op_check_no_immediate,
+ [OP_CALL] = nwp_op_check_call,
+ [OP_GET_LOCAL] = nwp_op_check_local_index,
+ [OP_SET_LOCAL] = nwp_op_check_local_index,
+ [OP_TEE_LOCAL] = nwp_op_check_local_index,
+ [OP_GET_GLOBAL] = nwp_op_check_global_index,
+ [OP_SET_GLOBAL] = nwp_op_check_global_index,
+ [OP_I32_LOAD] = nwp_op_check_memory_immediate,
+ [OP_I32_STORE] = nwp_op_check_memory_immediate,
+ [OP_I64_STORE] = nwp_op_check_memory_immediate,
+ [OP_F32_STORE] = nwp_op_check_memory_immediate,
+ [OP_F64_STORE] = nwp_op_check_memory_immediate,
+ [OP_I32_CONST] = nwp_op_check_i32_const,
+ [OP_I64_CONST] = nwp_op_check_i64_const,
+ [OP_I32_SUB] = nwp_op_check_no_immediate
+};
+
+static enum nw_state get_post_offset(struct nw_mod *const m)
+{
+ long offset;
+ struct nw_sm_c *const c = &m->sm.code;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->tell(&offset, cfg->user);
+
+ if (n)
+ return n;
+
+ const unsigned long consumed = offset - c->op_off;
+
+ if (consumed && consumed >= c->rem)
+ {
+ LOG("%s: parameters exceed expected body length\n", __func__);
+ return NW_FATAL;
+ }
+
+ c->rem -= consumed;
+ m->next = get_opcode;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_op_offset(struct nw_mod *const m)
+{
+ struct nw_sm_c *const c = &m->sm.code;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->tell(&c->op_off, cfg->user);
+ void (*const fn)(struct nw_mod *) = checks[c->op];
+
+ if (n)
+ return n;
+ else if (!fn)
+ {
+ LOG("%s: invalid opcode %#" PRIx8 "\n", __func__, c->op);
+ return NW_FATAL;
+ }
+
+ c->next = get_post_offset;
+ fn(m);
+ return NW_AGAIN;
+}
+
+static enum nw_state check_exit(struct nw_mod *const m)
+{
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_c_fn *const fn = &c->fn;
+
+ if (fn->blocks)
+ {
+ LOG("%s: mismatched number of blocks in function %lu\n", __func__,
+ (unsigned long)c->entry_i);
+ return NW_FATAL;
+ }
+
+ c->entry_i++;
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_opcode(struct nw_mod *const m)
+{
+ struct nw_sm_c *const c = &m->sm.code;
+
+ if (!c->rem)
+ return check_exit(m);
+
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &c->op, .n = sizeof c->op};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+ else if (c->rem == 1 && c->op != OP_END)
+ {
+ LOG("%s: unexpected opcode %#" PRIx8 " at body end\n",
+ __func__, c->op);
+ return NW_FATAL;
+ }
+ else if (c->op >= sizeof checks / sizeof *checks)
+ {
+ LOG("%s: invalid opcode %#" PRIx8 "\n", __func__, c->op);
+ return NW_FATAL;
+ }
+
+ c->rem--;
+ m->next = get_op_offset;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_rem(struct nw_mod *const m)
+{
+ long offset;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->tell(&offset, cfg->user);
+
+ if (n)
+ return n;
+
+ struct nw_sm_c *const c = &m->sm.code;
+ const unsigned long consumed = offset - c->start;
+
+ if (consumed >= c->body_size)
+ {
+ LOG("%s: exceeded function body size\n", __func__);
+ return NW_FATAL;
+ }
+
+ c->rem = c->body_size - consumed;
+ m->next = get_opcode;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_type(struct nw_mod *const m)
+{
+ nw_varint7 type;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varint7(cfg, l, &type);
+
+ if (n)
+ return n;
+
+ c->fn.local_i++;
+ m->next = local_loop;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_group_count(struct nw_mod *const m)
+{
+ nw_varuint32 group_count;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &group_count);
+
+ if (n)
+ return n;
+
+ c->fn.local_total += group_count;
+ m->next = get_type;
+ return NW_AGAIN;
+}
+
+static enum nw_state local_loop(struct nw_mod *const m)
+{
+ const struct nw_sm_c_fn *const fn = &m->sm.code.fn;
+
+ m->next = fn->local_i < fn->local_count ? get_group_count : get_rem;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_local_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_c_fn *const fn = &c->fn;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &fn->local_count);
+
+ if (n)
+ return n;
+
+ m->next = fn->local_count ? local_loop : get_rem;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_start(struct nw_mod *const m)
+{
+ struct nw_sm_c *const c = &m->sm.code;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->tell(&c->start, cfg->user);
+
+ if (n)
+ return n;
+
+ m->next = get_local_count;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_body_size(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &c->body_size);
+
+ if (n)
+ return n;
+
+ m->next = get_start;
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_mod *const m)
+{
+ struct nw_sm_c *const c = &m->sm.code;
+
+ c->fn = (const struct nw_sm_c_fn){.blocks = 1};
+ m->next = c->entry_i < c->count ? get_body_size : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_c *const c = &m->sm.code;
+ struct nw_sm_leb128 *const l = &c->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &c->count);
+
+ if (n)
+ return n;
+
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+void nwp_section_code(struct nw_mod *const m)
+{
+ struct nw_sm_c *const c = &m->sm.code;
+
+ m->next = get_count;
+ *c = (const struct nw_sm_c){0};
+}
diff --git a/src/routines/section/custom.c b/src/routines/section/custom.c
new file mode 100644
index 0000000..22b93b0
--- /dev/null
+++ b/src/routines/section/custom.c
@@ -0,0 +1,17 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/routines.h>
+
+void nwp_section_custom(struct nw_mod *const m)
+{
+ m->next = nwp_section_skip;
+}
diff --git a/src/routines/section/data.c b/src/routines/section/data.c
new file mode 100644
index 0000000..a4aba88
--- /dev/null
+++ b/src/routines/section/data.c
@@ -0,0 +1,19 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+void nwp_section_data(struct nw_mod *const m)
+{
+ /* TODO */
+}
diff --git a/src/routines/section/element.c b/src/routines/section/element.c
new file mode 100644
index 0000000..08265dc
--- /dev/null
+++ b/src/routines/section/element.c
@@ -0,0 +1,19 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+void nwp_section_element(struct nw_mod *const m)
+{
+ /* TODO */
+}
diff --git a/src/routines/section/exit.c b/src/routines/section/exit.c
new file mode 100644
index 0000000..db380ef
--- /dev/null
+++ b/src/routines/section/exit.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 <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+enum nw_state nwp_section_exit(struct nw_mod *const m)
+{
+ long offset;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->tell(&offset, cfg->user);
+
+ if (n)
+ return n;
+
+ const struct nw_mod_section *const s = &m->section;
+ const unsigned long size = offset - s->offset;
+
+ if (size != s->len)
+ {
+ LOG("%s: size exceeded (%lu expected, got %lu)\n",
+ __func__, (unsigned long)s->len, size);
+ return NW_FATAL;
+ }
+
+ nwp_section(m);
+ return NW_AGAIN;
+}
diff --git a/src/routines/section/export.c b/src/routines/section/export.c
new file mode 100644
index 0000000..a497db9
--- /dev/null
+++ b/src/routines/section/export.c
@@ -0,0 +1,113 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state entry_loop(struct nw_mod *m);
+
+static enum nw_state get_index(struct nw_mod *const m)
+{
+ nw_varuint32 index;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_exp *const e = &m->sm.export;
+ struct nw_sm_leb128 *const l = &e->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &index);
+
+ if (n)
+ return n;
+
+ e->entry_i++;
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_kind(struct nw_mod *const m)
+{
+ uint8_t kind;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &kind, .n = sizeof kind};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+
+ /* TODO: check kind */
+ m->next = get_index;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_name(struct nw_mod *const m)
+{
+ uint8_t b;
+ struct nw_sm_exp *const e = &m->sm.export;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &b, .n = sizeof b};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+ else if (++e->len_i >= e->field_len)
+ {
+ e->len_i = 0;
+ m->next = get_kind;
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_len(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_exp *const e = &m->sm.export;
+ struct nw_sm_leb128 *const l = &e->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &e->field_len);
+
+ if (n)
+ return n;
+ else if (!e->field_len)
+ {
+ LOG("%s: invalid field length\n", __func__);
+ return NW_FATAL;
+ }
+
+ m->next = get_name;
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_mod *const m)
+{
+ const struct nw_sm_exp *const e = &m->sm.export;
+
+ m->next = e->entry_i < e->count ? get_len : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_exp *const e = &m->sm.export;
+ struct nw_sm_leb128 *const l = &e->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &e->count);
+
+ if (n)
+ return n;
+
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+void nwp_section_export(struct nw_mod *const m)
+{
+ m->next = get_count;
+ m->sm.export = (const struct nw_sm_exp){0};
+}
diff --git a/src/routines/section/function.c b/src/routines/section/function.c
new file mode 100644
index 0000000..3402df3
--- /dev/null
+++ b/src/routines/section/function.c
@@ -0,0 +1,49 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state get_entry(struct nw_mod *const m)
+{
+ struct nw_sm_fn *const fn = &m->sm.function;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_leb128 *const l = &fn->leb128;
+ nw_varuint32 type;
+ const enum nw_state n = nwp_varuint32(cfg, l, &type);
+
+ if (n)
+ return n;
+ else if (++fn->entry_i >= fn->count)
+ m->next = nwp_section_exit;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_fn *const fn = &m->sm.function;
+ struct nw_sm_leb128 *const l = &fn->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &fn->count);
+
+ if (n)
+ return n;
+
+ m->next = get_entry;
+ return NW_AGAIN;
+}
+
+void nwp_section_function(struct nw_mod *const m)
+{
+ m->next = get_count;
+ m->sm.function = (const struct nw_sm_fn){0};
+}
diff --git a/src/routines/section/global.c b/src/routines/section/global.c
new file mode 100644
index 0000000..788d947
--- /dev/null
+++ b/src/routines/section/global.c
@@ -0,0 +1,144 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state loop(struct nw_mod *const m)
+{
+ struct nw_sm_gl *const gl = &m->sm.global;
+ const enum nw_state n = nwp_interp_run(&gl->interp);
+
+ if (n)
+ return n;
+
+ m->next = nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_global(struct nw_mod *const m)
+{
+ struct nw_sm_gl *const gl = &m->sm.global;
+ const struct nw_interp_cfg cfg =
+ {
+ .stack =
+ {
+ .buf = &gl->value,
+ .n = sizeof gl->value
+ },
+
+ .m = m,
+ .io = m->cfg.io
+ };
+
+ if (nwp_interp_start(&cfg, &nwp_interp_initexpr_set, &gl->interp))
+ {
+ LOG("%s: nw_interp_start failed\n", __func__);
+ return NW_FATAL;
+ }
+
+ m->next = loop;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_mutability(struct nw_mod *const m)
+{
+ nw_varuint1 mutability;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_gl *const gl = &m->sm.global;
+ struct nw_sm_leb128 *const l = &gl->leb128;
+ const enum nw_state n = nwp_varuint1(cfg, l, &mutability);
+
+ if (n)
+ return n;
+
+ m->next = get_global;
+ return NW_AGAIN;
+}
+
+static int get_type(const nw_varint7 content_type, enum nw_type *const out)
+{
+ static const struct type
+ {
+ nw_varint7 key;
+ enum nw_type value;
+ } list[] =
+ {
+ {.key = 0x7f, .value = NW_TYPE_I32},
+ {.key = 0x7e, .value = NW_TYPE_I64},
+ {.key = 0x7d, .value = NW_TYPE_F32},
+ {.key = 0x7c, .value = NW_TYPE_F64}
+ };
+
+ for (size_t i = 0; i < sizeof list / sizeof *list; i++)
+ {
+ const struct type *const t = &list[i];
+
+ if (content_type == t->key)
+ {
+ *out = t->value;
+ return 0;
+ }
+ }
+
+ LOG("%s: invalid type: %#hhx\n", __func__, (char)content_type);
+ return -1;
+}
+
+static enum nw_state get_content_type(struct nw_mod *const m)
+{
+ nw_varint7 content_type;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_gl *const gl = &m->sm.global;
+ struct nw_sm_leb128 *const l = &gl->leb128;
+ const enum nw_state n = nwp_varint7(cfg, l, &content_type);
+
+ if (n)
+ return n;
+ else if (get_type(content_type, &gl->type))
+ {
+ LOG("%s: get_type failed\n", __func__);
+ return NW_FATAL;
+ }
+
+ m->next = get_mutability;
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_mod *const m)
+{
+ const struct nw_sm_gl *const gl = &m->sm.global;
+
+ m->next = gl->entry_i < m->global_count ?
+ get_content_type : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_gl *const gl = &m->sm.global;
+ struct nw_sm_leb128 *const l = &gl->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &m->global_count);
+
+ if (n)
+ return n;
+
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+void nwp_section_global(struct nw_mod *const m)
+{
+ m->next = get_count;
+ m->sm.global = (const struct nw_sm_gl){0};
+}
diff --git a/src/routines/section/import.c b/src/routines/section/import.c
new file mode 100644
index 0000000..1ea54eb
--- /dev/null
+++ b/src/routines/section/import.c
@@ -0,0 +1,308 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <string.h>
+
+static enum nw_state entry_loop(struct nw_mod *);
+static enum nw_state find_import(struct nw_mod *);
+
+static enum nw_state get_function_type(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ struct nw_sm_leb128 *const l = &imp->leb128;
+ nw_varuint32 type;
+ const enum nw_state n = nwp_varuint32(cfg, l, &type);
+
+ if (n)
+ return n;
+
+ m->next = nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_kind(struct nw_mod *const m)
+{
+ uint8_t kind;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &kind, .n = sizeof kind};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
+
+ if (kind != i->kind)
+ {
+ m->next = find_import;
+ imp->imp_i++;
+ }
+ /* TODO: process every import type. */
+ else if (kind != NW_KIND_FUNCTION)
+ m->next = nwp_section_exit;
+ else
+ m->next = get_function_type;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state compare_field(struct nw_mod *const m)
+{
+ uint8_t byte;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &byte, .n = sizeof byte};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
+
+ if (byte != i->field[imp->len_i])
+ {
+ m->next = find_import;
+ imp->imp_i++;
+ imp->len_i = 0;
+ }
+ else if (++imp->len_i >= imp->field_len)
+ {
+ m->next = get_kind;
+ imp->len_i = 0;
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_field_offset(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const enum nw_state n = cfg->tell(&imp->field_off, cfg->user);
+ const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
+
+ if (n)
+ return n;
+ else if (imp->field_len != strlen(i->field))
+ {
+ m->next = find_import;
+ imp->imp_i++;
+ }
+ else
+ m->next = compare_field;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_field_len(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ struct nw_sm_leb128 *const l = &imp->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &imp->field_len);
+
+ if (n)
+ return n;
+ else
+ m->next = get_field_offset;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state compare_name(struct nw_mod *const m)
+{
+ uint8_t byte;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &byte, .n = sizeof byte};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
+
+ if (byte != i->module[imp->len_i])
+ {
+ m->next = find_import;
+ imp->imp_i++;
+ imp->len_i = 0;
+ }
+ else if (++imp->len_i >= imp->mod_len)
+ {
+ m->next = get_field_len;
+ imp->len_i = 0;
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state dump_import_field_name(struct nw_mod *const m)
+{
+ uint8_t byte;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &byte, .n = sizeof byte};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+
+ LOG("%c", (char)byte);
+
+ struct nw_sm_imp *const imp = &m->sm.import;
+
+ if (++imp->len_i >= imp->field_len)
+ {
+ LOG("\n");
+ return NW_FATAL;
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state rewind_to_field_name(struct nw_mod *const m)
+{
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->seek(imp->field_off, NW_SEEK_SET, cfg->user);
+
+ if (n)
+ return n;
+
+ m->next = dump_import_field_name;
+ return NW_AGAIN;
+}
+
+static enum nw_state dump_import_mod_name(struct nw_mod *const m)
+{
+ uint8_t byte;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {.buf = &byte, .n = sizeof byte};
+ const enum nw_state n = nwp_io_read(cfg, &io);
+
+ if (n)
+ return n;
+
+ LOG("%c", (char)byte);
+
+ struct nw_sm_imp *const imp = &m->sm.import;
+
+ if (++imp->len_i >= imp->mod_len)
+ {
+ imp->len_i = 0;
+
+ if (imp->field_off)
+ {
+ LOG("::");
+ m->next = rewind_to_field_name;
+ }
+ else
+ {
+ LOG("\n");
+ return NW_FATAL;
+ }
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state rewind_to_mod_name(struct nw_mod *const m)
+{
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->seek(imp->mod_off, NW_SEEK_SET, cfg->user);
+
+ if (n)
+ return n;
+
+ m->next = dump_import_mod_name;
+ return NW_AGAIN;
+}
+
+static enum nw_state find_import(struct nw_mod *const m)
+{
+ struct nw_sm_imp *const imp = &m->sm.import;
+
+ if (imp->imp_i >= m->cfg.n_imports)
+ {
+ LOG("%s: required import: ", __func__);
+ m->next = rewind_to_mod_name;
+ return NW_AGAIN;
+ }
+
+ const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
+
+ if (imp->mod_len == strlen(i->module))
+ m->next = compare_name;
+ else
+ imp->imp_i++;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_module_offset(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const enum nw_state n = cfg->tell(&imp->mod_off, cfg->user);
+
+ if (n)
+ return n;
+
+ m->next = find_import;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_module_len(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ struct nw_sm_leb128 *const l = &imp->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &imp->mod_len);
+
+ if (n)
+ return n;
+
+ m->next = get_module_offset;
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_mod *const m)
+{
+ const struct nw_sm_imp *const imp = &m->sm.import;
+
+ m->next = imp->entry_i < imp->count ? get_module_len : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ struct nw_sm_leb128 *const l = &imp->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &imp->count);
+
+ if (n)
+ return n;
+
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+void nwp_section_import(struct nw_mod *const m)
+{
+ m->next = get_count;
+ m->sm.import = (const struct nw_sm_imp){0};
+}
diff --git a/src/routines/section/memory.c b/src/routines/section/memory.c
new file mode 100644
index 0000000..2dd6bbe
--- /dev/null
+++ b/src/routines/section/memory.c
@@ -0,0 +1,85 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state get_maximum(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_mem *const mem = &m->sm.memory;
+ struct nw_sm_leb128 *const l = &mem->leb128;
+ nw_varuint32 maximum;
+ const enum nw_state n = nwp_varuint32(cfg, l, &maximum);
+
+ if (n)
+ return n;
+
+ m->next = nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_initial(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_mem *const mem = &m->sm.memory;
+ struct nw_sm_leb128 *const l = &mem->leb128;
+ nw_varuint32 initial;
+ const enum nw_state n = nwp_varuint32(cfg, l, &initial);
+
+ if (n)
+ return n;
+
+ m->next = mem->flags ? get_maximum : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_flags(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_mem *const mem = &m->sm.memory;
+ struct nw_sm_leb128 *const l = &mem->leb128;
+ const enum nw_state n = nwp_varuint1(cfg, l, &mem->flags);
+
+ if (n)
+ return n;
+
+ m->next = get_initial;
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_mod *const m)
+{
+ const struct nw_sm_mem *const mem = &m->sm.memory;
+
+ m->next = mem->entry_i < mem->count ? get_flags : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_mem *const mem = &m->sm.memory;
+ struct nw_sm_leb128 *const l = &mem->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &mem->count);
+
+ if (n)
+ return n;
+
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+void nwp_section_memory(struct nw_mod *const m)
+{
+ m->next = get_count;
+ m->sm.memory = (const struct nw_sm_mem){0};
+}
diff --git a/src/routines/section/skip.c b/src/routines/section/skip.c
new file mode 100644
index 0000000..37612a6
--- /dev/null
+++ b/src/routines/section/skip.c
@@ -0,0 +1,26 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+enum nw_state nwp_section_skip(struct nw_mod *const m)
+{
+ const struct nw_mod_section *const s = &m->section;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->seek(s->len, NW_SEEK_CUR, cfg->user);
+
+ if (n)
+ return n;
+
+ nwp_section(m);
+ return NW_AGAIN;
+}
diff --git a/src/routines/section/start.c b/src/routines/section/start.c
new file mode 100644
index 0000000..dc50bee
--- /dev/null
+++ b/src/routines/section/start.c
@@ -0,0 +1,35 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state get_index(struct nw_mod *const m)
+{
+ nw_varuint32 index;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_start *const st = &m->sm.start;
+ struct nw_sm_leb128 *const l = &st->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &index);
+
+ if (n)
+ return n;
+
+ m->next = nwp_section_exit;
+ return NW_AGAIN;
+}
+
+void nwp_section_start(struct nw_mod *const m)
+{
+ m->next = get_index;
+ m->sm.start = (const struct nw_sm_start){0};
+}
diff --git a/src/routines/section/table.c b/src/routines/section/table.c
new file mode 100644
index 0000000..3dec836
--- /dev/null
+++ b/src/routines/section/table.c
@@ -0,0 +1,111 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state get_maximum(struct nw_mod *const m)
+{
+ nw_varuint32 maximum;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_tb *const tb = &m->sm.table;
+ struct nw_sm_leb128 *const l = &tb->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &maximum);
+
+ if (n)
+ return n;
+
+ m->next = nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_initial(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_tb *const tb = &m->sm.table;
+ struct nw_sm_leb128 *const l = &tb->leb128;
+ nw_varuint32 initial;
+ const enum nw_state n = nwp_varuint32(cfg, l, &initial);
+
+ if (n)
+ return n;
+
+ m->next = tb->flags ? get_maximum : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_flags(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_tb *const tb = &m->sm.table;
+ struct nw_sm_leb128 *const l = &tb->leb128;
+ const enum nw_state n = nwp_varuint1(cfg, l, &tb->flags);
+
+ if (n)
+ return n;
+
+ m->next = get_initial;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_element_type(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_tb *const tb = &m->sm.table;
+ struct nw_sm_leb128 *const l = &tb->leb128;
+ const enum nw_state n = nwp_varint7(cfg, l, &tb->elem_type);
+
+ if (n)
+ return n;
+ /* TODO: replace with enum? */
+ else if (tb->elem_type != 0x70)
+ {
+ LOG("%s: unexpected elem_type %hhx\n", __func__, (char)tb->elem_type);
+ return NW_FATAL;
+ }
+
+ m->next = get_flags;
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_mod *const m)
+{
+ const struct nw_sm_tb *const tb = &m->sm.table;
+
+ m->next = tb->entry_i < tb->count ? get_element_type : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_tb *const tb = &m->sm.table;
+ struct nw_sm_leb128 *const l = &tb->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &tb->count);
+
+ if (n)
+ return n;
+ else if (tb->count > 1u)
+ {
+ LOG("%s: got %lu tables, but only 1 supported by the MVP\n", __func__,
+ (unsigned long)tb->count);
+ return NW_FATAL;
+ }
+
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+void nwp_section_table(struct nw_mod *const m)
+{
+ m->next = get_count;
+ m->sm.table = (const struct nw_sm_tb){0};
+}
diff --git a/src/routines/section/type.c b/src/routines/section/type.c
new file mode 100644
index 0000000..052586a
--- /dev/null
+++ b/src/routines/section/type.c
@@ -0,0 +1,159 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <stddef.h>
+
+static enum nw_state entry_loop(struct nw_mod *);
+
+static enum nw_state get_return_type(struct nw_mod *const m)
+{
+ struct nw_sm_st *const st = &m->sm.type;
+ struct nw_sm_leb128 *const l = &st->leb128;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ nw_varint7 return_type;
+ const enum nw_state n = nwp_varint7(cfg, l, &return_type);
+
+ if (n)
+ return n;
+
+ m->next = entry_loop;
+ st->entry_i++;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_return_count(struct nw_mod *const m)
+{
+ struct nw_sm_st *const st = &m->sm.type;
+ struct nw_sm_leb128 *const l = &st->leb128;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ nw_varuint1 return_count;
+ const enum nw_state n = nwp_varuint1(cfg, l, &return_count);
+
+ if (n)
+ return n;
+ else if (return_count)
+ m->next = get_return_type;
+ else
+ {
+ m->next = entry_loop;
+ st->entry_i++;
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state param_loop(struct nw_mod *const m)
+{
+ struct nw_sm_st *const st = &m->sm.type;
+
+ if (st->p_i >= st->param_count)
+ {
+ m->next = get_return_count;
+ return NW_AGAIN;
+ }
+
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_leb128 *const l = &st->leb128;
+ nw_varint7 type;
+ const enum nw_state n = nwp_varint7(cfg, l, &type);
+
+ if (n)
+ return n;
+
+ st->p_i++;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_param_count(struct nw_mod *const m)
+{
+ struct nw_sm_st *const st = &m->sm.type;
+ struct nw_sm_leb128 *const l = &st->leb128;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = nwp_varuint32(cfg, l, &st->param_count);
+
+ if (n)
+ return n;
+
+ st->p_i = 0;
+ m->next = param_loop;
+ return NW_AGAIN;
+}
+
+static int check_form(const nw_varint7 form)
+{
+ static const nw_varint7 v[] =
+ {
+ /* TODO: move values to enums? */
+ 0x7f, /* i32 */
+ 0x7e, /* i64 */
+ 0x7d, /* f32 */
+ 0x7c, /* f64 */
+ 0x70, /* anyfunc */
+ 0x60, /* func */
+ 0x40 /* empty block_type */
+ };
+
+ for (size_t i = 0; i < sizeof v / sizeof *v; i++)
+ if (form == v[i])
+ return 0;
+
+ return -1;
+}
+
+static enum nw_state get_form(struct nw_mod *const m)
+{
+ struct nw_sm_st *const st = &m->sm.type;
+ struct nw_sm_leb128 *const l = &st->leb128;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ nw_varint7 form;
+ const enum nw_state n = nwp_varint7(cfg, l, &form);
+
+ if (n)
+ return n;
+ else if (check_form(form))
+ {
+ LOG("%s: invalid form %#hhx\n", __func__, (char)l->result);
+ return NW_FATAL;
+ }
+
+ m->next = get_param_count;
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_mod *const m)
+{
+ struct nw_sm_st *const st = &m->sm.type;
+
+ m->next = st->entry_i < st->count ? get_form : nwp_section_exit;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_count(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_st *const st = &m->sm.type;
+ struct nw_sm_leb128 *const l = &st->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &st->count);
+
+ if (n)
+ return n;
+
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+void nwp_section_type(struct nw_mod *const m)
+{
+ m->next = get_count;
+ m->sm.type = (const struct nw_sm_st){0};
+}
diff --git a/src/run.c b/src/run.c
new file mode 100644
index 0000000..f69134b
--- /dev/null
+++ b/src/run.c
@@ -0,0 +1,16 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nw/interp.h>
+
+enum nw_state nw_run(struct nw_inst *const i)
+{
+ return nwp_interp_run(&i->interp);
+}
diff --git a/src/start.c b/src/start.c
new file mode 100644
index 0000000..453c62b
--- /dev/null
+++ b/src/start.c
@@ -0,0 +1,45 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <nanowasm/types.h>
+#include <nw/interp.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+
+static enum nw_state push(struct nw_interp *const i)
+{
+ const struct nw_i_sm_exp *const e = &i->sm.export;
+
+ if (e->kind != NW_KIND_FUNCTION)
+ {
+ static const char exc[] = "unexpected import kind";
+
+ i->exception = exc;
+ LOG("%s: %s: %d\n", __func__, exc, e->kind);
+ return NW_FATAL;
+ }
+
+ nwp_call(i, e->index);
+ return NW_AGAIN;
+}
+
+int nw_start(const struct nw_inst_cfg *const icfg, struct nw_inst *const i)
+{
+ *i = (const struct nw_inst){0};
+
+ if (nwp_interp_start(&icfg->interp_cfg, NULL, &i->interp))
+ {
+ LOG("%s: nwp_interp_start failed\n", __func__);
+ return -1;
+ }
+
+ nwp_find_export(&i->interp, "_start", push);
+ return 0;
+}
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100644
index 0000000..6913862
--- /dev/null
+++ b/test/CMakeLists.txt
@@ -0,0 +1,2 @@
+add_executable(test main.c)
+target_link_libraries(test PRIVATE ${PROJECT_NAME})
diff --git a/test/example.wasm b/test/example.wasm
new file mode 100755
index 0000000..2c7ad1c
--- /dev/null
+++ b/test/example.wasm
Binary files differ
diff --git a/test/main.c b/test/main.c
new file mode 100644
index 0000000..3c4e663
--- /dev/null
+++ b/test/main.c
@@ -0,0 +1,183 @@
+/*
+ * 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 <nanowasm/nw.h>
+#include <errno.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static int io_read(void *const buf, const size_t n, void *const user)
+{
+ FILE *const f = user;
+ const size_t r = fread(buf, 1, 1, f);
+
+ if (!r && ferror(f))
+ {
+ fprintf(stderr, "%s: ferror\n", __func__);
+ return -1;
+ }
+
+ return r;
+}
+
+static enum nw_state io_seek(const long offset, const enum nw_whence whence,
+ void *const user)
+{
+ static const int trans[] =
+ {
+ [NW_SEEK_END] = SEEK_END,
+ [NW_SEEK_CUR] = SEEK_CUR,
+ [NW_SEEK_SET] = SEEK_SET
+ };
+
+ if (fseek(user, offset, trans[whence]))
+ {
+ fprintf(stderr, "%s: fseek(3): %s\n", __func__, strerror(errno));
+ return NW_FATAL;
+ }
+
+ return NW_OK;
+}
+
+static enum nw_state io_tell(long *const out, void *const user)
+{
+ const long offset = ftell(user);
+
+ if (offset < 0)
+ {
+ fprintf(stderr, "%s: ftell(3): %s\n", __func__, strerror(errno));
+ return NW_FATAL;
+ }
+
+ *out = offset;
+ return NW_OK;
+}
+
+static int io_eof(void *const user)
+{
+ FILE *const f = user;
+
+ return feof(f);
+}
+
+int main(int argc, char *argv[])
+{
+ int ret = EXIT_FAILURE;
+ FILE *f = NULL;
+
+ if (argc != 2)
+ {
+ fprintf(stderr, "%s <wasm>\n", *argv);
+ goto end;
+ }
+
+ const char *const path = argv[1];
+
+ if (!(f = fopen(path, "rb")))
+ {
+ fprintf(stderr, "%s: fopen(3) %s: %s\n", __func__, path,
+ strerror(errno));
+ goto end;
+ }
+
+ static const struct nw_import imports[] =
+ {
+ {
+ .kind = NW_KIND_FUNCTION,
+ .module = "wasi_snapshot_preview1",
+ .field = "proc_exit"
+ }
+ };
+
+ const struct nw_io_cfg io =
+ {
+ .read = io_read,
+ .seek = io_seek,
+ .tell = io_tell,
+ .eof = io_eof,
+ .user = f
+ };
+
+ const struct nw_mod_cfg cfg =
+ {
+ .imports = imports,
+ .n_imports = sizeof imports / sizeof *imports,
+ .io = io
+ };
+
+ struct nw_mod m;
+
+ nw_init(&m, &cfg);
+
+again:
+
+ switch (nw_load(&m))
+ {
+ case NW_OK:
+ break;
+
+ case NW_AGAIN:
+ goto again;
+
+ case NW_FATAL:
+ fprintf(stderr, "%s: nw_load failed\n", __func__);
+ goto end;
+ }
+
+ const struct nw_inst_cfg icfg =
+ {
+ .interp_cfg =
+ {
+ .global = NW_BUF(1),
+ .heap = NW_BUF(1),
+ .stack = NW_BUF(128),
+ .io = io,
+ .m = &m
+ }
+ };
+
+ struct nw_inst inst;
+
+ if (nw_start(&icfg, &inst))
+ {
+ fprintf(stderr, "%s: nw_start failed\n", __func__);
+ goto end;
+ }
+
+again2:
+
+ switch (nw_run(&inst))
+ {
+ case NW_OK:
+ break;
+
+ case NW_AGAIN:
+ goto again2;
+
+ case NW_FATAL:
+ fprintf(stderr, "%s: nw_run failed: %s\n", __func__,
+ nw_exception(&inst));
+ goto end;
+ }
+
+ ret = EXIT_SUCCESS;
+
+end:
+
+ if (f && fclose(f))
+ {
+ fprintf(stderr, "%s: fclose(3) %s: %s\n", __func__, path,
+ strerror(errno));
+ goto end;
+ }
+
+ return ret;
+}