diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2025-04-20 10:16:42 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2025-04-20 10:16:42 +0200 |
| commit | 00330b81483399020ec542c2bc7cdd2b452b5d2b (patch) | |
| tree | 09b097ae6f0d50758927969e3f0575854393c161 | |
| parent | 4a23ae3522503fe9497ea8736bf5e8832b959951 (diff) | |
Fibonacci Test Passes
We can now run the fibonacci program, including a native function
import. This is a great first step, and now we need to start
implementing PICO-8 library functions (basically, `emcc` library, but
one that is both more stubbed and supports the `setjmp` and `longjmp`
which does not appear to be the case for the included implementation
in WAMR).
But before, a small build system refactoring detour to make building
WAMR and PICO-w easier for both SDL platforms and Vita platform.
Additionally, the platform stubs that we use in the WAMR test might
need to change a bit, in order to allow PICO-w SDL and Vita platforms
to implement certain details differently.
| -rw-r--r-- | doc/journal.md | 12 | ||||
| -rw-r--r-- | lib/wamr/Makefile | 6 | ||||
| -rw-r--r-- | lib/wamr/core/iwasm/common/arch/invokeNative_general.c | 123 | ||||
| -rw-r--r-- | lib/wamr/core/shared/platform/stub/stubs.c | 4 | ||||
| -rw-r--r-- | lib/wamr/test/main.c | 5 |
5 files changed, 18 insertions, 132 deletions
diff --git a/doc/journal.md b/doc/journal.md index 05e8b10..697c27c 100644 --- a/doc/journal.md +++ b/doc/journal.md @@ -1,16 +1,20 @@ -# 14.4.2025 +# 20.04.2025 + +After some painful debugging, I figured out that `invokeNative` implementation in WAMR relied on platform and ABI specific semantics, and that `invokeNative_general.c` implementation was not portable. Instead, there is a `_raw` variant for native functions where the arguments and returns need to be manually setup and extracted, but this is portable and doesn't rely on platform specifics - so I will use this instead. Note that I stubbed out the `invokeNative` implementation so that you get runtime errors if you try and use it. + +# 14.04.2025 Nothing too exciting in the way of PICO-8 development, but we did settle on a PNG parsing library (`stb`) and now support extracting PICO-8 32kB roms from `.p8.png` files. It was mostly painless and not very interesting. -# 12.4.2025 +# 12.04.2025 Started looking into Wasm runtimes to use, in particular ones that are portable C. The leading candidate right now is WebAssembly Micro Runtime (WAMR). Note that it even has Emscripten library support, so we have way fewer imports to implement. -# 10.4.2025 +# 10.04.2025 Beutifying the exported JavaScript file yields some great results. In particular, it wasn't minified and instead just stripped of whitespace, making it legible. I was able to wrap imported and exported functions of the Wasm blob with some basic logging, making the process of reverse engineering what each of the imported functions are supposed to do. -# 8.4.2025 +# 8.04.2025 First thing that I tested was exporting different carts and seeing what changed in the output (including exporting multi-carts). What I discovered was: diff --git a/lib/wamr/Makefile b/lib/wamr/Makefile index 2d4a1ff..e24dc9d 100644 --- a/lib/wamr/Makefile +++ b/lib/wamr/Makefile @@ -1,5 +1,4 @@ -SRC = core/iwasm/common/arch/invokeNative_general.c \ - core/iwasm/common/wasm_application.c \ +SRC = core/iwasm/common/wasm_application.c \ core/iwasm/common/wasm_blocking_op.c \ core/iwasm/common/wasm_c_api.c \ core/iwasm/common/wasm_exec_env.c \ @@ -28,7 +27,8 @@ SRC = core/iwasm/common/arch/invokeNative_general.c \ core/shared/utils/bh_vector.c \ core/shared/utils/runtime_timer.c -CFLAGS = -DBH_FREE=wasm_runtime_free \ +CFLAGS = -DBH_DEBUG=1 \ + -DBH_FREE=wasm_runtime_free \ -DBH_MALLOC=wasm_runtime_malloc \ -DWASM_ENABLE_BULK_MEMORY=1 \ -DWASM_ENABLE_FAST_INTERP=1 \ diff --git a/lib/wamr/core/iwasm/common/arch/invokeNative_general.c b/lib/wamr/core/iwasm/common/arch/invokeNative_general.c deleted file mode 100644 index 52ffee5..0000000 --- a/lib/wamr/core/iwasm/common/arch/invokeNative_general.c +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "../wasm_runtime_common.h" -#include "../wasm_exec_env.h" - -#if defined(__clang__) -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-non-prototype" -#endif - -void -invokeNative(void (*native_code)(), uint32 argv[], uint32 argc) -{ - bh_assert(argc >= sizeof(WASMExecEnv *) / sizeof(uint32)); - - switch (argc) { - case 0: - native_code(); - break; - case 1: - native_code(argv[0]); - break; - case 2: - native_code(argv[0], argv[1]); - break; - case 3: - native_code(argv[0], argv[1], argv[2]); - break; - case 4: - native_code(argv[0], argv[1], argv[2], argv[3]); - break; - case 5: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4]); - break; - case 6: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]); - break; - case 7: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], - argv[6]); - break; - case 8: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], - argv[6], argv[7]); - break; - case 9: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], - argv[6], argv[7], argv[8]); - break; - case 10: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], - argv[6], argv[7], argv[8], argv[9]); - break; - case 11: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], - argv[6], argv[7], argv[8], argv[9], argv[10]); - break; - case 12: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], - argv[6], argv[7], argv[8], argv[9], argv[10], argv[11]); - break; - case 13: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], - argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], - argv[12]); - break; - case 14: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], - argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], - argv[12], argv[13]); - break; - case 15: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], - argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], - argv[12], argv[13], argv[14]); - break; - case 16: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], - argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], - argv[12], argv[13], argv[14], argv[15]); - break; - case 17: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], - argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], - argv[12], argv[13], argv[14], argv[15], argv[16]); - break; - case 18: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], - argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], - argv[12], argv[13], argv[14], argv[15], argv[16], - argv[17]); - break; - case 19: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], - argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], - argv[12], argv[13], argv[14], argv[15], argv[16], - argv[17], argv[18]); - break; - case 20: - native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], - argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], - argv[12], argv[13], argv[14], argv[15], argv[16], - argv[17], argv[18], argv[19]); - break; - default: - { - /* FIXME: If this happen, add more cases. */ - WASMExecEnv *exec_env = *(WASMExecEnv **)argv; - WASMModuleInstanceCommon *module_inst = exec_env->module_inst; - wasm_runtime_set_exception( - module_inst, - "the argument number of native function exceeds maximum"); - return; - } - } -} - -#if defined(__clang__) -#pragma clang diagnostic pop -#endif diff --git a/lib/wamr/core/shared/platform/stub/stubs.c b/lib/wamr/core/shared/platform/stub/stubs.c index f1ea4f2..14e9528 100644 --- a/lib/wamr/core/shared/platform/stub/stubs.c +++ b/lib/wamr/core/shared/platform/stub/stubs.c @@ -140,3 +140,7 @@ uint32_t os_getpagesize(void) { return 4096; // TODO: NOT_IMPLEMENTED(); } + +void invokeNative(void (*native_code)(), uint32_t argv[], uint32_t argc) { + NOT_IMPLEMENTED(); +} diff --git a/lib/wamr/test/main.c b/lib/wamr/test/main.c index 1fe6686..b0bd004 100644 --- a/lib/wamr/test/main.c +++ b/lib/wamr/test/main.c @@ -19,7 +19,8 @@ #define LEN_OF(x) \ ((sizeof (x)) / (sizeof (x)[0])) -static void lognumber(wasm_exec_env_t exec, int32_t n) { +static void lognumber(wasm_exec_env_t exec, uint64_t *args) { + native_raw_get_arg(int32_t, n, args); printf("-- %d\n", n); } @@ -52,7 +53,7 @@ int main(int argc, char *argv[argc + 1]) { struct NativeSymbol env[] = { {"lognumber", lognumber, "(i)"}, }; - WAMR_CHECK(wasm_runtime_register_natives("env", env, LEN_OF(env)), + WAMR_CHECK(wasm_runtime_register_natives_raw("env", env, LEN_OF(env)), "failed to register natives"); wasm_module_t module = wasm_runtime_load(wasm, wasmsz, err, sizeof err); |
