1%%includes 2#include <math.h> 3#include <string.h> 4%%declarations 5#define UNLIKELY(x) __builtin_expect(!!(x), 0) 6#define LIKELY(x) __builtin_expect(!!(x), 1) 7 8#define TRAP(x) (wasm_rt_trap(WASM_RT_TRAP_##x), 0) 9 10#define FUNC_PROLOGUE \ 11 if (++wasm_rt_call_stack_depth > WASM_RT_MAX_CALL_STACK_DEPTH) \ 12 TRAP(EXHAUSTION) 13 14#define FUNC_EPILOGUE --wasm_rt_call_stack_depth 15 16#define UNREACHABLE TRAP(UNREACHABLE) 17 18#define CALL_INDIRECT(table, t, ft, x, ...) \ 19 (LIKELY((x) < table.size && table.data[x].func && \ 20 table.data[x].func_type == func_types[ft]) \ 21 ? ((t)table.data[x].func)(__VA_ARGS__) \ 22 : TRAP(CALL_INDIRECT)) 23 24#define MEMCHECK(mem, a, t) \ 25 if (UNLIKELY((a) + sizeof(t) > mem->size)) TRAP(OOB) 26 27#define DEFINE_LOAD(name, t1, t2, t3) \ 28 static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \ 29 MEMCHECK(mem, addr, t1); \ 30 t1 result; \ 31 memcpy(&result, &mem->data[addr], sizeof(t1)); \ 32 return (t3)(t2)result; \ 33 } 34 35#define DEFINE_STORE(name, t1, t2) \ 36 static inline void name(wasm_rt_memory_t* mem, u64 addr, t2 value) { \ 37 MEMCHECK(mem, addr, t1); \ 38 t1 wrapped = (t1)value; \ 39 memcpy(&mem->data[addr], &wrapped, sizeof(t1)); \ 40 } 41 42DEFINE_LOAD(i32_load, u32, u32, u32); 43DEFINE_LOAD(i64_load, u64, u64, u64); 44DEFINE_LOAD(f32_load, f32, f32, f32); 45DEFINE_LOAD(f64_load, f64, f64, f64); 46DEFINE_LOAD(i32_load8_s, s8, s32, u32); 47DEFINE_LOAD(i64_load8_s, s8, s64, u64); 48DEFINE_LOAD(i32_load8_u, u8, u32, u32); 49DEFINE_LOAD(i64_load8_u, u8, u64, u64); 50DEFINE_LOAD(i32_load16_s, s16, s32, u32); 51DEFINE_LOAD(i64_load16_s, s16, s64, u64); 52DEFINE_LOAD(i32_load16_u, u16, u32, u32); 53DEFINE_LOAD(i64_load16_u, u16, u64, u64); 54DEFINE_LOAD(i64_load32_s, s32, s64, u64); 55DEFINE_LOAD(i64_load32_u, u32, u64, u64); 56DEFINE_STORE(i32_store, u32, u32); 57DEFINE_STORE(i64_store, u64, u64); 58DEFINE_STORE(f32_store, f32, f32); 59DEFINE_STORE(f64_store, f64, f64); 60DEFINE_STORE(i32_store8, u8, u32); 61DEFINE_STORE(i32_store16, u16, u32); 62DEFINE_STORE(i64_store8, u8, u64); 63DEFINE_STORE(i64_store16, u16, u64); 64DEFINE_STORE(i64_store32, u32, u64); 65 66#define I32_CLZ(x) ((x) ? __builtin_clz(x) : 32) 67#define I64_CLZ(x) ((x) ? __builtin_clzll(x) : 64) 68#define I32_CTZ(x) ((x) ? __builtin_ctz(x) : 32) 69#define I64_CTZ(x) ((x) ? __builtin_ctzll(x) : 64) 70#define I32_POPCNT(x) (__builtin_popcount(x)) 71#define I64_POPCNT(x) (__builtin_popcountll(x)) 72 73#define DIV_S(ut, min, x, y) \ 74 ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) \ 75 : (UNLIKELY((x) == min && (y) == -1)) ? TRAP(INT_OVERFLOW) \ 76 : (ut)((x) / (y))) 77 78#define REM_S(ut, min, x, y) \ 79 ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) \ 80 : (UNLIKELY((x) == min && (y) == -1)) ? 0 \ 81 : (ut)((x) % (y))) 82 83#define I32_DIV_S(x, y) DIV_S(u32, INT32_MIN, (s32)x, (s32)y) 84#define I64_DIV_S(x, y) DIV_S(u64, INT64_MIN, (s64)x, (s64)y) 85#define I32_REM_S(x, y) REM_S(u32, INT32_MIN, (s32)x, (s32)y) 86#define I64_REM_S(x, y) REM_S(u64, INT64_MIN, (s64)x, (s64)y) 87 88#define DIVREM_U(op, x, y) \ 89 ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) : ((x) op (y))) 90 91#define DIV_U(x, y) DIVREM_U(/, x, y) 92#define REM_U(x, y) DIVREM_U(%, x, y) 93 94#define ROTL(x, y, mask) \ 95 (((x) << ((y) & (mask))) | ((x) >> (((mask) - (y) + 1) & (mask)))) 96#define ROTR(x, y, mask) \ 97 (((x) >> ((y) & (mask))) | ((x) << (((mask) - (y) + 1) & (mask)))) 98 99#define I32_ROTL(x, y) ROTL(x, y, 31) 100#define I64_ROTL(x, y) ROTL(x, y, 63) 101#define I32_ROTR(x, y) ROTR(x, y, 31) 102#define I64_ROTR(x, y) ROTR(x, y, 63) 103 104#define FMIN(x, y) \ 105 ((UNLIKELY((x) != (x))) ? NAN \ 106 : (UNLIKELY((y) != (y))) ? NAN \ 107 : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? x : y) \ 108 : (x < y) ? x : y) 109 110#define FMAX(x, y) \ 111 ((UNLIKELY((x) != (x))) ? NAN \ 112 : (UNLIKELY((y) != (y))) ? NAN \ 113 : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? y : x) \ 114 : (x > y) ? x : y) 115 116#define TRUNC_S(ut, st, ft, min, max, maxop, x) \ 117 ((UNLIKELY((x) != (x))) ? TRAP(INVALID_CONVERSION) \ 118 : (UNLIKELY((x) < (ft)(min) || (x) maxop (ft)(max))) ? TRAP(INT_OVERFLOW) \ 119 : (ut)(st)(x)) 120 121#define I32_TRUNC_S_F32(x) TRUNC_S(u32, s32, f32, INT32_MIN, INT32_MAX, >=, x) 122#define I64_TRUNC_S_F32(x) TRUNC_S(u64, s64, f32, INT64_MIN, INT64_MAX, >=, x) 123#define I32_TRUNC_S_F64(x) TRUNC_S(u32, s32, f64, INT32_MIN, INT32_MAX, >, x) 124#define I64_TRUNC_S_F64(x) TRUNC_S(u64, s64, f64, INT64_MIN, INT64_MAX, >=, x) 125 126#define TRUNC_U(ut, ft, max, maxop, x) \ 127 ((UNLIKELY((x) != (x))) ? TRAP(INVALID_CONVERSION) \ 128 : (UNLIKELY((x) <= (ft)-1 || (x) maxop (ft)(max))) ? TRAP(INT_OVERFLOW) \ 129 : (ut)(x)) 130 131#define I32_TRUNC_U_F32(x) TRUNC_U(u32, f32, UINT32_MAX, >=, x) 132#define I64_TRUNC_U_F32(x) TRUNC_U(u64, f32, UINT64_MAX, >=, x) 133#define I32_TRUNC_U_F64(x) TRUNC_U(u32, f64, UINT32_MAX, >, x) 134#define I64_TRUNC_U_F64(x) TRUNC_U(u64, f64, UINT64_MAX, >=, x) 135 136#define DEFINE_REINTERPRET(name, t1, t2) \ 137 static inline t2 name(t1 x) { \ 138 t2 result; \ 139 memcpy(&result, &x, sizeof(result)); \ 140 return result; \ 141 } 142 143DEFINE_REINTERPRET(f32_reinterpret_i32, u32, f32) 144DEFINE_REINTERPRET(i32_reinterpret_f32, f32, u32) 145DEFINE_REINTERPRET(f64_reinterpret_i64, u64, f64) 146DEFINE_REINTERPRET(i64_reinterpret_f64, f64, u64) 147 148