1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
#ifndef PW_WASMRT_H
#define PW_WASMRT_H
#include <setjmp.h>
#include <SDL3/SDL_endian.h>
#include <SDL3/SDL_stdinc.h>
#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
|