#ifndef PW_WASMRT_H #define PW_WASMRT_H #include #include #include #if SDL_HAS_BUILTIN(__builtin_expect) #define LIKELY(x) __builtin_expect(!!(x), 1) #define UNLIKELY(x) __builtin_expect(!!(x), 0) #else #define LIKELY(x) (x) #define UNLIKELY(x) (x) #endif #define WASM_RT_NO_RETURN SDL_NORETURN /** * Use a simple and static configuration for the Web Assembly runtime required * by `wasm2c` instead of using the one provided by `wabt`. This allows us a * few things: * * - Use the same configuration everywhere, and not just enable extra features * on better supported targets (like `amd64`). * - Allows us to back the implementation on SDL3 instead of the standard * library for more consistent behaviour. * * Besides, no platforms with these extra features supported, you are probably * better off using PICO-8 directly anyway. * * Note that we explicitely disable stack depth checks. This _may_ cause stack * overflows on systems with a small stack space. We should consider turning * this on in case we ever observe this issue. That being said, if we do get * into a situation where we observe a stack overflow, the game crashed anyway. * * We also set `WASM_RT_USE_MMAP`, even though we don't `mmap` anything: the * generated code only uses it to decide whether it may cache the memory base * pointer and size in local variables instead of reloading them after every * store. This is safe as long as the memory never moves while an instance is * running (there is no `memory.grow` and we refuse `emscripten_resize_heap`). * Caching makes the PICO-8 run noticeably faster. */ #define WASM_RT_MEMCHECK_GUARD_PAGES 0 #define WASM_RT_MEMCHECK_BOUNDS_CHECK 1 #define WASM_RT_STACK_DEPTH_COUNT 0 #define WASM_RT_USE_SEGUE 0 #define WASM_RT_USE_MMAP 1 #if SDL_BYTEORDER == SDL_LIL_ENDIAN #define WABT_BIG_ENDIAN 0 #else #define WABT_BIG_ENDIAN 1 #endif /** * A Web Assembly trap. */ typedef enum wasm_rt_trap_t { WASM_RT_TRAP_NONE, WASM_RT_TRAP_OOB, WASM_RT_TRAP_INT_OVERFLOW, WASM_RT_TRAP_DIV_BY_ZERO, WASM_RT_TRAP_INVALID_CONVERSION, WASM_RT_TRAP_UNREACHABLE, WASM_RT_TRAP_CALL_INDIRECT, } wasm_rt_trap_t; /** * Web Assembly value types. */ typedef enum wasm_rt_type_t { WASM_RT_I32, WASM_RT_I64, WASM_RT_F32, WASM_RT_F64, } wasm_rt_type_t; /** * A function pointer. */ typedef void (*wasm_rt_function_ptr_t)(void); /** * Tail-call entry points, stubbed as PICO-8 was built without Web Assembly * tail-call support. */ typedef struct wasm_rt_tailcallee_t { void *_unused; } wasm_rt_tailcallee_t; /** * A function type: a 32-byte signature hash. */ typedef const char *wasm_rt_func_type_t; /** * A Web Assembly function reference. */ typedef struct wasm_rt_funcref_t { wasm_rt_func_type_t func_type; wasm_rt_function_ptr_t func; wasm_rt_tailcallee_t func_tailcallee; void *module_instance; } wasm_rt_funcref_t; /** * A Web Assembly external reference. */ typedef struct wasm_rt_externref_t { void *_unused; } wasm_rt_externref_t; /** * Linear Web Assembly memory. */ typedef struct wasm_rt_memory_t { uint8_t *data; uint8_t *data_end; uint64_t size; } wasm_rt_memory_t; /** * A function reference table. */ typedef struct wasm_rt_funcref_table_t { wasm_rt_funcref_t *data; uint32_t size; } wasm_rt_funcref_table_t; /** * An external reference table. */ typedef struct wasm_rt_externref_table_t { wasm_rt_externref_t *data; uint32_t size; } wasm_rt_externref_table_t; /** * A trap handling context. * * This is created by the caller for catching traps during wasm execution. */ typedef struct wasm_rt_trap_handler_t { jmp_buf env; volatile wasm_rt_trap_t code; } wasm_rt_trap_handler_t; /** * Returns whether Web Assembly runtime is initialized. */ bool wasm_rt_is_initialized(void); /** * IT'S A TRAP! */ WASM_RT_NO_RETURN void wasm_rt_trap(enum wasm_rt_trap_t code); /** * Returns a string name for the trap. */ char const *wasm_rt_trap_str(enum wasm_rt_trap_t code); /** * Registers a trap handler for a call, returning false if another handler is * already registered. */ bool wasm_rt_trap_register(struct wasm_rt_trap_handler_t *handler); /** * Unregisters the trap handler. Sets an error if no handler was registered. */ void wasm_rt_trap_unregister(void); /** * Returns whether or not a call trapped. * * It is undefined behaviour to call this macro without first registering the * trap handler. */ #define wasm_rt_trapped(h) setjmp((h)->env) #define wasm_rt_memcpy SDL_memcpy #define wasm_rt_funcref_null_value \ ((wasm_rt_funcref_t){NULL, NULL, {NULL}, NULL}) #define wasm_rt_externref_null_value ((wasm_rt_externref_t){NULL}) /** * Checks whether or not memory `[addr, addr+len)` is a valid memory region. */ void wasm_rt_memory_check(struct wasm_rt_memory_t const *mem, uint32_t addr, uint32_t len); /** * Loads bytes from memory. */ void wasm_rt_memory_load(struct wasm_rt_memory_t const *mem, uint32_t addr, void *dst, uint32_t len); /** * Loads bytes from memory without bounds checking. */ void wasm_rt_memory_load_unchecked(struct wasm_rt_memory_t const *mem, uint32_t addr, void *dst, uint32_t len); /** * Loads a 32-bit word from memory. */ uint32_t wasm_rt_memory_load32(struct wasm_rt_memory_t const *mem, uint32_t addr); /** * Loads a 32-bit word from memory without bounds checks. */ uint32_t wasm_rt_memory_load32_unchecked(struct wasm_rt_memory_t const *mem, uint32_t addr); /** * Loads a 32-bit word from memory, returning `false` if out of bounds. */ bool wasm_rt_memory_load32_checked(struct wasm_rt_memory_t const *mem, uint32_t addr, uint32_t *value); /** * Loads a NUL-terminated string from memory. * * It copies at most `sz - 1` bytes and ensures that `buf` always ends with * the NUL character. Returns the length of the string (excluding NUL) or * `sz` in case the buffer was too small to load the entire string. */ size_t wasm_rt_memory_loada(struct wasm_rt_memory_t const *mem, uint32_t addr, char *buf, size_t sz); /** * Loads a NUL-terminated string from memory, without bounds checking. */ size_t wasm_rt_memory_loada_unchecked(struct wasm_rt_memory_t const *mem, uint32_t addr, char *buf, size_t sz); /** * Loads 16-bit values from memory. */ void wasm_rt_memory_load16v(struct wasm_rt_memory_t const *mem, uint32_t addr, uint16_t *values, uint32_t len); /** * Loads 16-bit values from memory without bounds checks. */ void wasm_rt_memory_load16v_unchecked(struct wasm_rt_memory_t const *mem, uint32_t addr, uint16_t *values, uint32_t len); /** * Stores bytes to memory. */ void wasm_rt_memory_store(struct wasm_rt_memory_t *mem, uint32_t addr, void const *src, uint32_t len); /** * Stores bytes to memory without bounds checking. */ void wasm_rt_memory_store_unchecked(struct wasm_rt_memory_t *mem, uint32_t addr, void const *src, uint32_t len); /** * Stores a byte to memory. */ void wasm_rt_memory_store8(struct wasm_rt_memory_t *mem, uint32_t addr, uint8_t value); /** * Stores a byte to memory without bounds checks. */ void wasm_rt_memory_store8_unchecked(struct wasm_rt_memory_t *mem, uint32_t addr, uint8_t value); /** * Stores a 16-bit value to memory. */ void wasm_rt_memory_store16(struct wasm_rt_memory_t *mem, uint32_t addr, uint16_t value); /** * Stores a 16-bit value to memory without bounds checks. */ void wasm_rt_memory_store16_unchecked(struct wasm_rt_memory_t *mem, uint32_t addr, uint16_t value); /** * Stores a 32-bit word to memory. */ void wasm_rt_memory_store32(struct wasm_rt_memory_t *mem, uint32_t addr, uint32_t value); /** * Stores a 32-bit word to memory without bounds checks. */ void wasm_rt_memory_store32_unchecked(struct wasm_rt_memory_t *mem, uint32_t addr, uint32_t value); /** * Stores a 64-bit value to memory. */ void wasm_rt_memory_store64(struct wasm_rt_memory_t *mem, uint32_t addr, uint64_t value); /** * Stores a 64-bit value to memory without bounds checks. */ void wasm_rt_memory_store64_unchecked(struct wasm_rt_memory_t *mem, uint32_t addr, uint64_t value); /** * Sets all bytes in a memory range to a value. */ void wasm_rt_memory_set(struct wasm_rt_memory_t *mem, uint32_t addr, uint32_t len, uint8_t value); /** * Copies memory from one location to another, allowing overlap. */ void wasm_rt_memory_copy(struct wasm_rt_memory_t *mem, uint32_t dst, uint32_t src, uint32_t len); #endif