summaryrefslogtreecommitdiffstats
path: root/third_party/wasm2c/src/prebuilt/wasm2c.include.c
blob: de6297862902dd78836f748feabc3ca00964669f (plain)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* Generated from 'wasm2c.c.tmpl' by wasm2c_tmpl.py, do not edit! */
const char SECTION_NAME(includes)[] =
"/* Automically generated by wasm2c */\n"
"#include <math.h>\n"
"#include <string.h>\n"
"#include <stdlib.h>\n"
;

const char SECTION_NAME(declarations)[] =
"#if defined(_MSC_VER)\n"
"#  define UNLIKELY(x) (x)\n"
"#  define LIKELY(x) (x)\n"
"#else\n"
"#  define UNLIKELY(x) __builtin_expect(!!(x), 0)\n"
"#  define LIKELY(x) __builtin_expect(!!(x), 1)\n"
"#endif\n"
"\n"
"#define TRAP(x) (wasm_rt_trap(WASM_RT_TRAP_##x), 0)\n"
"\n"
"#ifndef FUNC_PROLOGUE\n"
"#define FUNC_PROLOGUE\n"
"#endif\n"
"\n"
"#ifndef FUNC_EPILOGUE\n"
"#define FUNC_EPILOGUE\n"
"#endif\n"
"\n"
"#ifdef EXTERNAL_CALLBACK_PROLOGUE\n"
"#define EXTERNAL_CALLBACK_PROLOGUE_EXEC(table, x)                        \\\n"
"  if (UNLIKELY(table.data[x].func_class == WASM_RT_EXTERNAL_FUNCTION)) { \\\n"
"    EXTERNAL_CALLBACK_PROLOGUE;                                          \\\n"
"  }\n"
"#else\n"
"#define EXTERNAL_CALLBACK_PROLOGUE_EXEC(table, x)\n"
"#endif\n"
"\n"
"#ifdef EXTERNAL_CALLBACK_EPILOGUE\n"
"#define EXTERNAL_CALLBACK_EPILOGUE_EXEC(table, x)                        \\\n"
"  if (UNLIKELY(table.data[x].func_class == WASM_RT_EXTERNAL_FUNCTION)) { \\\n"
"    EXTERNAL_CALLBACK_EPILOGUE;                                          \\\n"
"  }\n"
"#else\n"
"#define EXTERNAL_CALLBACK_EPILOGUE_EXEC(table, x)\n"
"#endif\n"
"\n"
"#define UNREACHABLE (void) TRAP(UNREACHABLE)\n"
"\n"
"#define CALL_INDIRECT_VOID(table, t, ft, x, func_types, ...)                                         \\\n"
"  if (LIKELY((x) < table.size && table.data[x].func && table.data[x].func_type == func_types[ft])) { \\\n"
"    EXTERNAL_CALLBACK_PROLOGUE_EXEC(table, x);                                                       \\\n"
"    ((t)table.data[x].func)(__VA_ARGS__);                                                            \\\n"
"    EXTERNAL_CALLBACK_EPILOGUE_EXEC(table, x);                                                       \\\n"
"  } else {                                                                                           \\\n"
"    wasm_rt_callback_error_trap(&table, x, func_types[ft]);                                          \\\n"
"  }\n"
"\n"
"#define CALL_INDIRECT_RES(res, table, t, ft, x, func_types, ...)                                     \\\n"
"  if (LIKELY((x) < table.size && table.data[x].func && table.data[x].func_type == func_types[ft])) { \\\n"
"    EXTERNAL_CALLBACK_PROLOGUE_EXEC(table, x);                                                       \\\n"
"    res = ((t)table.data[x].func)(__VA_ARGS__);                                                      \\\n"
"    EXTERNAL_CALLBACK_EPILOGUE_EXEC(table, x);                                                       \\\n"
"  } else {                                                                                           \\\n"
"    wasm_rt_callback_error_trap(&table, x, func_types[ft]);                                          \\\n"
"  }\n"
"\n"
"#if defined(WASM2C_MALLOC_FAIL_CALLBACK)\n"
"void WASM2C_MALLOC_FAIL_CALLBACK(u32 ptr_size);\n"
"# define WASM2C_MALLOC_FAIL_CHECK(ptr, ptr_size)  \\\n"
"  if (!ptr) {                                     \\\n"
"    WASM2C_MALLOC_FAIL_CALLBACK(ptr_size);        \\\n"
"  }\n"
"#else\n"
"# define WASM2C_MALLOC_FAIL_CHECK(ptr, ptr_size)\n"
"#endif\n"
"\n"
"#if defined(WASM_CHECK_SHADOW_MEMORY)\n"
"#  define WASM2C_SHADOW_MEMORY_LOAD(mem, func_name, ptr, ptr_size)  wasm2c_shadow_memory_load(mem, func_name, ptr, ptr_size)\n"
"#  define WASM2C_SHADOW_MEMORY_STORE(mem, func_name, ptr, ptr_size) wasm2c_shadow_memory_store(mem, func_name, ptr, ptr_size)\n"
"#  define WASM2C_SHADOW_MEMORY_RESERVE(mem, ptr, ptr_size)          wasm2c_shadow_memory_reserve(mem, ptr, ptr_size)\n"
"#  define WASM2C_SHADOW_MEMORY_DLMALLOC(mem, ptr, ptr_size)         wasm2c_shadow_memory_dlmalloc(mem, ptr, ptr_size)\n"
"#  define WASM2C_SHADOW_MEMORY_DLFREE(mem, ptr)                     wasm2c_shadow_memory_dlfree(mem, ptr)\n"
"#  define WASM2C_SHADOW_MEMORY_MARK_GLOBALS_HEAP_BOUNDARY(mem, ptr) wasm2c_shadow_memory_mark_globals_heap_boundary(mem, ptr)\n"
"#else\n"
"#  define WASM2C_SHADOW_MEMORY_LOAD(mem, func_name, ptr, ptr_size)\n"
"#  define WASM2C_SHADOW_MEMORY_STORE(mem, func_name, ptr, ptr_size)\n"
"#  define WASM2C_SHADOW_MEMORY_RESERVE(mem, ptr, ptr_size)\n"
"#  define WASM2C_SHADOW_MEMORY_DLMALLOC(mem, ptr, ptr_size)\n"
"#  define WASM2C_SHADOW_MEMORY_DLFREE(mem, ptr)\n"
"#  define WASM2C_SHADOW_MEMORY_MARK_GLOBALS_HEAP_BOUNDARY(mem, ptr)\n"
"#endif\n"
"\n"
"#ifdef WASM_USE_GUARD_PAGES\n"
"#  define MEMCHECK(mem, a, t)\n"
"#else\n"
"#  define MEMCHECK(mem, a, t) if (UNLIKELY((a) + sizeof(t) > mem->size)) { (void) TRAP(OOB); }\n"
"#endif\n"
"\n"
"#if defined(WASM_USE_GUARD_PAGES) && UINTPTR_MAX == 0xffffffff\n"
"// on 32-bit platforms we have to mask memory access into range\n"
"#  define MEM_ACCESS_REF(mem, addr) &mem->data[addr & mem->mem_mask]\n"
"#else\n"
"#  define MEM_ACCESS_REF(mem, addr) &mem->data[addr]\n"
"#endif\n"
"\n"
"#if defined(WASM_USING_GLOBAL_HEAP)\n"
"#  undef MEM_ACCESS_REF\n"
"#  define MEM_ACCESS_REF(mem, addr) (char*) addr\n"
"#endif\n"
"\n"
"#ifdef __GNUC__\n"
"#define wasm_asm __asm__\n"
"#else\n"
"#define wasm_asm(X)\n"
"#endif\n"
"\n"
"#if WABT_BIG_ENDIAN\n"
"static inline void load_data(void *dest, const void *src, size_t n) {\n"
"  size_t i = 0;\n"
"  u8 *dest_chars = dest;\n"
"  memcpy(dest, src, n);\n"
"  for (i = 0; i < (n>>1); i++) {\n"
"    u8 cursor = dest_chars[i];\n"
"    dest_chars[i] = dest_chars[n - i - 1];\n"
"    dest_chars[n - i - 1] = cursor;\n"
"  }\n"
"}\n"
"#define LOAD_DATA(m, o, i, s) { load_data(&(m.data[m.size - o - s]), i, s); \\\n"
"  WASM2C_SHADOW_MEMORY_RESERVE(&m, m.size - o - s, s);                       \\\n"
"  WASM2C_SHADOW_MEMORY_STORE(&m, \"GlobalDataLoad\", m.size - o - s, s);       \\\n"
"}\n"
"\n"
"#define DEFINE_LOAD(name, t1, t2, t3)                                                      \\\n"
"  static inline t3 name(wasm_rt_memory_t* mem, u64 addr, const char* func_name) {          \\\n"
"    MEMCHECK(mem, addr, t1);                                                               \\\n"
"    t1 result;                                                                             \\\n"
"    memcpy(&result, MEM_ACCESS_REF(mem, mem->size - addr - sizeof(t1)), sizeof(t1));       \\\n"
"    WASM2C_SHADOW_MEMORY_LOAD(mem, func_name, mem->size - addr - sizeof(t1), sizeof(t1));  \\\n"
"    wasm_asm(\"\" ::\"r\"(result));                                                            \\\n"
"    return (t3)(t2)result;                                                                 \\\n"
"  }\n"
"\n"
"#define DEFINE_STORE(name, t1, t2)                                                            \\\n"
"  static inline void name(wasm_rt_memory_t* mem, u64 addr, t2 value, const char* func_name) { \\\n"
"    MEMCHECK(mem, addr, t1);                                                                  \\\n"
"    t1 wrapped = (t1)value;                                                                   \\\n"
"    memcpy(MEM_ACCESS_REF(mem, mem->size - addr - sizeof(t1), &wrapped, sizeof(t1));          \\\n"
"    WASM2C_SHADOW_MEMORY_STORE(mem, func_name, mem->size - addr - sizeof(t1)), sizeof(t1));   \\\n"
"  }\n"
"#else\n"
"static inline void load_data(void *dest, const void *src, size_t n) {\n"
"  memcpy(dest, src, n);\n"
"}\n"
"#define LOAD_DATA(m, o, i, s) { load_data(&(m.data[o]), i, s); \\\n"
"  WASM2C_SHADOW_MEMORY_RESERVE(&m, o, s);                       \\\n"
"  WASM2C_SHADOW_MEMORY_STORE(&m, \"GlobalDataLoad\", o, s);       \\\n"
"}\n"
"\n"
"#define DEFINE_LOAD(name, t1, t2, t3)                                               \\\n"
"  static inline t3 name(wasm_rt_memory_t* mem, u64 addr, const char* func_name) {   \\\n"
"    MEMCHECK(mem, addr, t1);                                                        \\\n"
"    t1 result;                                                                      \\\n"
"    memcpy(&result, MEM_ACCESS_REF(mem, addr), sizeof(t1));                         \\\n"
"    WASM2C_SHADOW_MEMORY_LOAD(mem, func_name, addr, sizeof(t1));                    \\\n"
"    wasm_asm(\"\" ::\"r\"(result));                                                     \\\n"
"    return (t3)(t2)result;                                                          \\\n"
"  }\n"
"\n"
"#define DEFINE_STORE(name, t1, t2)                                                            \\\n"
"  static inline void name(wasm_rt_memory_t* mem, u64 addr, t2 value, const char* func_name) { \\\n"
"    MEMCHECK(mem, addr, t1);                                                                  \\\n"
"    t1 wrapped = (t1)value;                                                                   \\\n"
"    memcpy(MEM_ACCESS_REF(mem, addr), &wrapped, sizeof(t1));                                  \\\n"
"    WASM2C_SHADOW_MEMORY_STORE(mem, func_name, addr, sizeof(t1));                             \\\n"
"  }\n"
"#endif\n"
"\n"
"DEFINE_LOAD(i32_load, u32, u32, u32);\n"
"DEFINE_LOAD(i64_load, u64, u64, u64);\n"
"DEFINE_LOAD(f32_load, f32, f32, f32);\n"
"DEFINE_LOAD(f64_load, f64, f64, f64);\n"
"DEFINE_LOAD(i32_load8_s, s8, s32, u32);\n"
"DEFINE_LOAD(i64_load8_s, s8, s64, u64);\n"
"DEFINE_LOAD(i32_load8_u, u8, u32, u32);\n"
"DEFINE_LOAD(i64_load8_u, u8, u64, u64);\n"
"DEFINE_LOAD(i32_load16_s, s16, s32, u32);\n"
"DEFINE_LOAD(i64_load16_s, s16, s64, u64);\n"
"DEFINE_LOAD(i32_load16_u, u16, u32, u32);\n"
"DEFINE_LOAD(i64_load16_u, u16, u64, u64);\n"
"DEFINE_LOAD(i64_load32_s, s32, s64, u64);\n"
"DEFINE_LOAD(i64_load32_u, u32, u64, u64);\n"
"DEFINE_STORE(i32_store, u32, u32);\n"
"DEFINE_STORE(i64_store, u64, u64);\n"
"DEFINE_STORE(f32_store, f32, f32);\n"
"DEFINE_STORE(f64_store, f64, f64);\n"
"DEFINE_STORE(i32_store8, u8, u32);\n"
"DEFINE_STORE(i32_store16, u16, u32);\n"
"DEFINE_STORE(i64_store8, u8, u64);\n"
"DEFINE_STORE(i64_store16, u16, u64);\n"
"DEFINE_STORE(i64_store32, u32, u64);\n"
"\n"
"#if defined(_MSC_VER)\n"
"#include <intrin.h>\n"
"\n"
"// Adapted from https://github.com/nemequ/portable-snippets/blob/master/builtin/builtin.h\n"
"\n"
"static inline int I64_CLZ(unsigned long long v) {\n"
"  unsigned long r = 0;\n"
"#if defined(_M_AMD64) || defined(_M_ARM)\n"
"    if (_BitScanReverse64(&r, v)) {\n"
"      return 63 - r;\n"
"    }\n"
"#else\n"
"    if (_BitScanReverse(&r, (unsigned long) (v >> 32))) {\n"
"      return 31 - r;\n"
"    } else if (_BitScanReverse(&r, (unsigned long) v)) {\n"
"      return 63 - r;\n"
"    }\n"
"#endif\n"
"  return 64;\n"
"}\n"
"\n"
"static inline int I32_CLZ(unsigned long v) {\n"
"  unsigned long r = 0;\n"
"  if (_BitScanReverse(&r, v)) {\n"
"    return 31 - r;\n"
"  }\n"
"  return 32;\n"
"}\n"
"\n"
"static inline int I64_CTZ(unsigned long long v) {\n"
"  if (!v) {\n"
"    return 64;\n"
"  }\n"
"  unsigned long r = 0;\n"
"#if defined(_M_AMD64) || defined(_M_ARM)\n"
"    _BitScanForward64(&r, v);\n"
"    return (int) r;\n"
"#else\n"
"    if (_BitScanForward(&r, (unsigned int) (v))) {\n"
"      return (int) (r);\n"
"    }\n"
"\n"
"    _BitScanForward(&r, (unsigned int) (v >> 32));\n"
"    return (int) (r + 32);\n"
"#endif\n"
"}\n"
"\n"
"static inline int I32_CTZ(unsigned long v) {\n"
"  if (!v) {\n"
"    return 32;\n"
"  }\n"
"  unsigned long r = 0;\n"
"  _BitScanForward(&r, v);\n"
"  return (int) r;\n"
"}\n"
"\n"
"#define POPCOUNT_DEFINE_PORTABLE(f_n, T)                    \\\n"
"  static inline u32 f_n(T x) {                              \\\n"
"    x = x - ((x >> 1) & (T)~(T)0/3);                        \\\n"
"    x = (x & (T)~(T)0/15*3) + ((x >> 2) & (T)~(T)0/15*3);   \\\n"
"    x = (x + (x >> 4)) & (T)~(T)0/255*15;                   \\\n"
"    return (T)(x * ((T)~(T)0/255)) >> (sizeof(T) - 1) * 8;  \\\n"
"  }\n"
"\n"
"POPCOUNT_DEFINE_PORTABLE(I32_POPCNT, u32)\n"
"POPCOUNT_DEFINE_PORTABLE(I64_POPCNT, u64)\n"
"\n"
"#undef POPCOUNT_DEFINE_PORTABLE\n"
"\n"
"#else\n"
"#  define I32_CLZ(x) ((x) ? __builtin_clz(x) : 32)\n"
"#  define I64_CLZ(x) ((x) ? __builtin_clzll(x) : 64)\n"
"#  define I32_CTZ(x) ((x) ? __builtin_ctz(x) : 32)\n"
"#  define I64_CTZ(x) ((x) ? __builtin_ctzll(x) : 64)\n"
"#  define I32_POPCNT(x) (__builtin_popcount(x))\n"
"#  define I64_POPCNT(x) (__builtin_popcountll(x))\n"
"#endif\n"
"\n"
"#define DIV_S(ut, min, x, y)                                 \\\n"
"   ((UNLIKELY((y) == 0)) ?                TRAP(DIV_BY_ZERO)  \\\n"
"  : (UNLIKELY((x) == min && (y) == -1)) ? TRAP(INT_OVERFLOW) \\\n"
"  : (ut)((x) / (y)))\n"
"\n"
"#define REM_S(ut, min, x, y)                                \\\n"
"   ((UNLIKELY((y) == 0)) ?                TRAP(DIV_BY_ZERO) \\\n"
"  : (UNLIKELY((x) == min && (y) == -1)) ? 0                 \\\n"
"  : (ut)((x) % (y)))\n"
"\n"
"#define I32_DIV_S(x, y) DIV_S(u32, INT32_MIN, (s32)x, (s32)y)\n"
"#define I64_DIV_S(x, y) DIV_S(u64, INT64_MIN, (s64)x, (s64)y)\n"
"#define I32_REM_S(x, y) REM_S(u32, INT32_MIN, (s32)x, (s32)y)\n"
"#define I64_REM_S(x, y) REM_S(u64, INT64_MIN, (s64)x, (s64)y)\n"
"\n"
"#define DIVREM_U(op, x, y) \\\n"
"  ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) : ((x) op (y)))\n"
"\n"
"#define DIV_U(x, y) DIVREM_U(/, x, y)\n"
"#define REM_U(x, y) DIVREM_U(%, x, y)\n"
"\n"
"#define ROTL(x, y, mask) \\\n"
"  (((x) << ((y) & (mask))) | ((x) >> (((mask) - (y) + 1) & (mask))))\n"
"#define ROTR(x, y, mask) \\\n"
"  (((x) >> ((y) & (mask))) | ((x) << (((mask) - (y) + 1) & (mask))))\n"
"\n"
"#define I32_ROTL(x, y) ROTL(x, y, 31)\n"
"#define I64_ROTL(x, y) ROTL(x, y, 63)\n"
"#define I32_ROTR(x, y) ROTR(x, y, 31)\n"
"#define I64_ROTR(x, y) ROTR(x, y, 63)\n"
"\n"
"#define FMIN(x, y)                                          \\\n"
"   ((UNLIKELY((x) != (x))) ? NAN                            \\\n"
"  : (UNLIKELY((y) != (y))) ? NAN                            \\\n"
"  : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? x : y) \\\n"
"  : (x < y) ? x : y)\n"
"\n"
"#define FMAX(x, y)                                          \\\n"
"   ((UNLIKELY((x) != (x))) ? NAN                            \\\n"
"  : (UNLIKELY((y) != (y))) ? NAN                            \\\n"
"  : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? y : x) \\\n"
"  : (x > y) ? x : y)\n"
"\n"
"#define TRUNC_S(ut, st, ft, min, minop, max, x)                             \\\n"
"  ((UNLIKELY((x) != (x)))                        ? TRAP(INVALID_CONVERSION) \\\n"
"   : (UNLIKELY(!((x)minop(min) && (x) < (max)))) ? TRAP(INT_OVERFLOW)       \\\n"
"                                                 : (ut)(st)(x))\n"
"\n"
"#define I32_TRUNC_S_F32(x) TRUNC_S(u32, s32, f32, (f32)INT32_MIN, >=, 2147483648.f, x)\n"
"#define I64_TRUNC_S_F32(x) TRUNC_S(u64, s64, f32, (f32)INT64_MIN, >=, (f32)INT64_MAX, x)\n"
"#define I32_TRUNC_S_F64(x) TRUNC_S(u32, s32, f64, -2147483649., >, 2147483648., x)\n"
"#define I64_TRUNC_S_F64(x) TRUNC_S(u64, s64, f64, (f64)INT64_MIN, >=, (f64)INT64_MAX, x)\n"
"\n"
"#define TRUNC_U(ut, ft, max, x)                                            \\\n"
"  ((UNLIKELY((x) != (x)))                       ? TRAP(INVALID_CONVERSION) \\\n"
"   : (UNLIKELY(!((x) > (ft)-1 && (x) < (max)))) ? TRAP(INT_OVERFLOW)       \\\n"
"                                                : (ut)(x))\n"
"\n"
"#define I32_TRUNC_U_F32(x) TRUNC_U(u32, f32, 4294967296.f, x)\n"
"#define I64_TRUNC_U_F32(x) TRUNC_U(u64, f32, (f32)UINT64_MAX, x)\n"
"#define I32_TRUNC_U_F64(x) TRUNC_U(u32, f64, 4294967296.,  x)\n"
"#define I64_TRUNC_U_F64(x) TRUNC_U(u64, f64, (f64)UINT64_MAX, x)\n"
"\n"
"#define TRUNC_SAT_S(ut, st, ft, min, smin, minop, max, smax, x) \\\n"
"  ((UNLIKELY((x) != (x)))         ? 0                           \\\n"
"   : (UNLIKELY(!((x)minop(min)))) ? smin                        \\\n"
"   : (UNLIKELY(!((x) < (max))))   ? smax                        \\\n"
"                                  : (ut)(st)(x))\n"
"\n"
"#define I32_TRUNC_SAT_S_F32(x) TRUNC_SAT_S(u32, s32, f32, (f32)INT32_MIN, INT32_MIN, >=, 2147483648.f, INT32_MAX, x)\n"
"#define I64_TRUNC_SAT_S_F32(x) TRUNC_SAT_S(u64, s64, f32, (f32)INT64_MIN, INT64_MIN, >=, (f32)INT64_MAX, INT64_MAX, x)\n"
"#define I32_TRUNC_SAT_S_F64(x) TRUNC_SAT_S(u32, s32, f64, -2147483649., INT32_MIN, >, 2147483648., INT32_MAX, x)\n"
"#define I64_TRUNC_SAT_S_F64(x) TRUNC_SAT_S(u64, s64, f64, (f64)INT64_MIN, INT64_MIN, >=, (f64)INT64_MAX, INT64_MAX, x)\n"
"\n"
"#define TRUNC_SAT_U(ut, ft, max, smax, x) \\\n"
"  ((UNLIKELY((x) != (x)))        ? 0      \\\n"
"   : (UNLIKELY(!((x) > (ft)-1))) ? 0      \\\n"
"   : (UNLIKELY(!((x) < (max))))  ? smax   \\\n"
"                                 : (ut)(x))\n"
"\n"
"#define I32_TRUNC_SAT_U_F32(x) TRUNC_SAT_U(u32, f32, 4294967296.f, UINT32_MAX, x)\n"
"#define I64_TRUNC_SAT_U_F32(x) TRUNC_SAT_U(u64, f32, (f32)UINT64_MAX, UINT64_MAX, x)\n"
"#define I32_TRUNC_SAT_U_F64(x) TRUNC_SAT_U(u32, f64, 4294967296., UINT32_MAX,  x)\n"
"#define I64_TRUNC_SAT_U_F64(x) TRUNC_SAT_U(u64, f64, (f64)UINT64_MAX, UINT64_MAX, x)\n"
"\n"
"#define DEFINE_REINTERPRET(name, t1, t2)  \\\n"
"  static inline t2 name(t1 x) {           \\\n"
"    t2 result;                            \\\n"
"    memcpy(&result, &x, sizeof(result));  \\\n"
"    return result;                        \\\n"
"  }\n"
"\n"
"DEFINE_REINTERPRET(f32_reinterpret_i32, u32, f32)\n"
"DEFINE_REINTERPRET(i32_reinterpret_f32, f32, u32)\n"
"DEFINE_REINTERPRET(f64_reinterpret_i64, u64, f64)\n"
"DEFINE_REINTERPRET(i64_reinterpret_f64, f64, u64)\n"
;

const char SECTION_NAME(sandboxapis)[] =
"//test\n"
"\n"
"static u32 add_wasm2c_callback(void* sbx_ptr, u32 func_type_idx, void* func_ptr, wasm_rt_elem_target_class_t func_class) {\n"
"  wasm_rt_table_t* table = get_wasm2c_callback_table(sbx_ptr);\n"
"  for (u32 i = 1; i < table->max_size; i++) {\n"
"    if (i >= table->size) {\n"
"      wasm_rt_expand_table(table);\n"
"    }\n"
"    if (table->data[i].func == 0) {\n"
"      table->data[i] = (wasm_rt_elem_t){ func_class, func_type_idx, (wasm_rt_anyfunc_t) func_ptr };\n"
"      return i;\n"
"    }\n"
"  }\n"
"  (void) TRAP(CALL_INDIRECT_TABLE_EXPANSION);\n"
"}\n"
"\n"
"static void remove_wasm2c_callback(void* sbx_ptr, u32 callback_idx) {\n"
"  wasm_rt_table_t* table = get_wasm2c_callback_table(sbx_ptr);\n"
"  table->data[callback_idx].func = 0;\n"
"}\n"
"\n"
"static u32 lookup_wasm2c_func_index(void* sbx_ptr, u32 param_count, u32 result_count, wasm_rt_type_t* types) {\n"
"  wasm2c_sandbox_t* const sbx = (wasm2c_sandbox_t* const) sbx_ptr;\n"
"  return wasm_rt_register_func_type(&sbx->func_type_structs, &sbx->func_type_count, param_count, result_count, types);\n"
"}\n"
"\n"
"static void* create_wasm2c_sandbox(uint32_t max_wasm_pages) {\n"
"  wasm2c_sandbox_t* const sbx = (wasm2c_sandbox_t* const) calloc(sizeof(wasm2c_sandbox_t), 1);\n"
"  if (!init_memory(sbx, max_wasm_pages)) {\n"
"    free(sbx);\n"
"    return 0;\n"
"  }\n"
"  init_func_types(sbx);\n"
"  init_globals(sbx);\n"
"  init_table(sbx);\n"
"  wasm_rt_init_wasi(&(sbx->wasi_data));\n"
"  init_module_starts();\n"
"  // w2c___wasm_call_ctors(sbx);\n"
"  return sbx;\n"
"}\n"
"\n"
"static void destroy_wasm2c_sandbox(void* aSbx) {\n"
"  wasm2c_sandbox_t* const sbx = (wasm2c_sandbox_t* const) aSbx;\n"
"  cleanup_memory(sbx);\n"
"  cleanup_func_types(sbx);\n"
"  cleanup_table(sbx);\n"
"  wasm_rt_cleanup_wasi(&(sbx->wasi_data));\n"
"  free(sbx);\n"
"}\n"
"\n"
"FUNC_EXPORT wasm2c_sandbox_funcs_t WASM_CURR_ADD_PREFIX(get_wasm2c_sandbox_info)() {\n"
"  wasm2c_sandbox_funcs_t ret;\n"
"  ret.wasm_rt_sys_init = &wasm_rt_sys_init;\n"
"  ret.create_wasm2c_sandbox = &create_wasm2c_sandbox;\n"
"  ret.destroy_wasm2c_sandbox = &destroy_wasm2c_sandbox;\n"
"  ret.lookup_wasm2c_nonfunc_export = &lookup_wasm2c_nonfunc_export;\n"
"  ret.lookup_wasm2c_func_index = &lookup_wasm2c_func_index;\n"
"  ret.add_wasm2c_callback = &add_wasm2c_callback;\n"
"  ret.remove_wasm2c_callback = &remove_wasm2c_callback;\n"
"  return ret;\n"
"}\n"
;