From 820f8b499acf038fa3c885ea30d4d19712d1432f Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Fri, 6 Sep 2024 15:32:29 +0300 Subject: [PATCH] Add WASM_RT_OPTIMIZE --- Makefile | 1 + runtime/libc.c | 12 ++++++------ runtime/runtime.c | 4 ++++ runtime/wasm-rt-impl.c | 2 ++ runtime/wasm-rt.h | 6 +++++- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index c724eac..3f50e87 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,7 @@ WASM ?= test/$(APP).wasm include $(MPY_DIR)/py/dynruntime.mk +#-DWASM_RT_OPTIMIZE -Ofast CFLAGS += -Os -Iruntime -I$(BUILD) -Wno-unused-value -Wno-unused-function \ -Wno-unused-variable -Wno-unused-but-set-variable diff --git a/runtime/libc.c b/runtime/libc.c index 78eeb84..4ca3648 100644 --- a/runtime/libc.c +++ b/runtime/libc.c @@ -48,10 +48,10 @@ size_t strlen(const char *str) { } int strncmp(const char *_l, const char *_r, size_t n) { - const unsigned char *l=(void *)_l, *r=(void *)_r; - if (!n--) return 0; - for (; *l && *r && n && *l == *r ; l++, r++, n--); - return *l - *r; + const unsigned char *l=(void *)_l, *r=(void *)_r; + if (!n--) return 0; + for (; *l && *r && n && *l == *r ; l++, r++, n--); + return *l - *r; } void abort() { @@ -61,10 +61,10 @@ void abort() { #if defined(__ARM_EABI__) int __aeabi_idiv0(int ret) { - return ret; + return ret; } long long __aeabi_ldiv0(long long ret) { - return ret; + return ret; } #endif diff --git a/runtime/runtime.c b/runtime/runtime.c index 4f41d55..aa7e366 100644 --- a/runtime/runtime.c +++ b/runtime/runtime.c @@ -91,7 +91,11 @@ void os_print_last_error(const char* msg) { } void wasm_rt_trap_handler(wasm_rt_trap_t code) { +#if WASM_RT_OPTIMIZE + mp_printf(&mp_plat_print, "Trap: %d\n", code); +#else mp_printf(&mp_plat_print, "Trap: %s\n", wasm_rt_strerror(code)); +#endif abort(); } diff --git a/runtime/wasm-rt-impl.c b/runtime/wasm-rt-impl.c index bd4cd7e..b5018b2 100644 --- a/runtime/wasm-rt-impl.c +++ b/runtime/wasm-rt-impl.c @@ -261,6 +261,7 @@ void wasm_rt_free_thread(void) { #include "wasm-rt-impl-tableops.inc" #undef WASM_RT_TABLE_OPS_EXTERNREF +#if !WASM_RT_OPTIMIZE const char* wasm_rt_strerror(wasm_rt_trap_t trap) { switch (trap) { case WASM_RT_TRAP_NONE: @@ -291,3 +292,4 @@ const char* wasm_rt_strerror(wasm_rt_trap_t trap) { } return "invalid trap code"; } +#endif diff --git a/runtime/wasm-rt.h b/runtime/wasm-rt.h index eeca6bf..6a0d749 100644 --- a/runtime/wasm-rt.h +++ b/runtime/wasm-rt.h @@ -89,6 +89,10 @@ extern "C" { #define WASM_RT_THREAD_LOCAL #endif +#ifndef WASM_RT_OPTIMIZE +#define WASM_RT_OPTIMIZE 0 +#endif + /** * If enabled, perform additional sanity checks in the generated wasm2c code and * wasm2c runtime. This is useful to enable on debug builds. @@ -206,7 +210,7 @@ extern "C" { * Wasm's specification, and may compromise security. Use with caution. */ #ifndef WASM_RT_NONCONFORMING_UNCHECKED_STACK_EXHAUSTION -#define WASM_RT_NONCONFORMING_UNCHECKED_STACK_EXHAUSTION 0 +#define WASM_RT_NONCONFORMING_UNCHECKED_STACK_EXHAUSTION WASM_RT_OPTIMIZE #endif /**