1 #include <assert.h>
2 #include <math.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "fac.h"
7 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
8 #define LIKELY(x) __builtin_expect(!!(x), 1)
9
10 #define TRAP(x) (wasm_rt_trap(WASM_RT_TRAP_##x), 0)
11
12 #define FUNC_PROLOGUE \
13 if (++wasm_rt_call_stack_depth > WASM_RT_MAX_CALL_STACK_DEPTH) \
14 TRAP(EXHAUSTION)
15
16 #define FUNC_EPILOGUE --wasm_rt_call_stack_depth
17
18 #define UNREACHABLE TRAP(UNREACHABLE)
19
20 #define CALL_INDIRECT(table, t, ft, x, ...) \
21 (LIKELY((x) < table.size && table.data[x].func && \
22 table.data[x].func_type == func_types[ft]) \
23 ? ((t)table.data[x].func)(__VA_ARGS__) \
24 : TRAP(CALL_INDIRECT))
25
26 #define MEMCHECK(mem, a, t) \
27 if (UNLIKELY((a) + sizeof(t) > mem->size)) TRAP(OOB)
28
29 #define DEFINE_LOAD(name, t1, t2, t3) \
30 static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \
31 MEMCHECK(mem, addr, t1); \
32 t1 result; \
33 memcpy(&result, &mem->data[addr], sizeof(t1)); \
34 return (t3)(t2)result; \
35 }
36
37 #define DEFINE_STORE(name, t1, t2) \
38 static inline void name(wasm_rt_memory_t* mem, u64 addr, t2 value) { \
39 MEMCHECK(mem, addr, t1); \
40 t1 wrapped = (t1)value; \
41 memcpy(&mem->data[addr], &wrapped, sizeof(t1)); \
42 }
43
44 DEFINE_LOAD(i32_load, u32, u32, u32);
45 DEFINE_LOAD(i64_load, u64, u64, u64);
46 DEFINE_LOAD(f32_load, f32, f32, f32);
47 DEFINE_LOAD(f64_load, f64, f64, f64);
48 DEFINE_LOAD(i32_load8_s, s8, s32, u32);
49 DEFINE_LOAD(i64_load8_s, s8, s64, u64);
50 DEFINE_LOAD(i32_load8_u, u8, u32, u32);
51 DEFINE_LOAD(i64_load8_u, u8, u64, u64);
52 DEFINE_LOAD(i32_load16_s, s16, s32, u32);
53 DEFINE_LOAD(i64_load16_s, s16, s64, u64);
54 DEFINE_LOAD(i32_load16_u, u16, u32, u32);
55 DEFINE_LOAD(i64_load16_u, u16, u64, u64);
56 DEFINE_LOAD(i64_load32_s, s32, s64, u64);
57 DEFINE_LOAD(i64_load32_u, u32, u64, u64);
58 DEFINE_STORE(i32_store, u32, u32);
59 DEFINE_STORE(i64_store, u64, u64);
60 DEFINE_STORE(f32_store, f32, f32);
61 DEFINE_STORE(f64_store, f64, f64);
62 DEFINE_STORE(i32_store8, u8, u32);
63 DEFINE_STORE(i32_store16, u16, u32);
64 DEFINE_STORE(i64_store8, u8, u64);
65 DEFINE_STORE(i64_store16, u16, u64);
66 DEFINE_STORE(i64_store32, u32, u64);
67
68 #define I32_CLZ(x) ((x) ? __builtin_clz(x) : 32)
69 #define I64_CLZ(x) ((x) ? __builtin_clzll(x) : 64)
70 #define I32_CTZ(x) ((x) ? __builtin_ctz(x) : 32)
71 #define I64_CTZ(x) ((x) ? __builtin_ctzll(x) : 64)
72 #define I32_POPCNT(x) (__builtin_popcount(x))
73 #define I64_POPCNT(x) (__builtin_popcountll(x))
74
75 #define DIV_S(ut, min, x, y) \
76 ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) \
77 : (UNLIKELY((x) == min && (y) == -1)) ? TRAP(INT_OVERFLOW) \
78 : (ut)((x) / (y)))
79
80 #define REM_S(ut, min, x, y) \
81 ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) \
82 : (UNLIKELY((x) == min && (y) == -1)) ? 0 \
83 : (ut)((x) % (y)))
84
85 #define I32_DIV_S(x, y) DIV_S(u32, INT32_MIN, (s32)x, (s32)y)
86 #define I64_DIV_S(x, y) DIV_S(u64, INT64_MIN, (s64)x, (s64)y)
87 #define I32_REM_S(x, y) REM_S(u32, INT32_MIN, (s32)x, (s32)y)
88 #define I64_REM_S(x, y) REM_S(u64, INT64_MIN, (s64)x, (s64)y)
89
90 #define DIVREM_U(op, x, y) \
91 ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) : ((x) op (y)))
92
93 #define DIV_U(x, y) DIVREM_U(/, x, y)
94 #define REM_U(x, y) DIVREM_U(%, x, y)
95
96 #define ROTL(x, y, mask) \
97 (((x) << ((y) & (mask))) | ((x) >> (((mask) - (y) + 1) & (mask))))
98 #define ROTR(x, y, mask) \
99 (((x) >> ((y) & (mask))) | ((x) << (((mask) - (y) + 1) & (mask))))
100
101 #define I32_ROTL(x, y) ROTL(x, y, 31)
102 #define I64_ROTL(x, y) ROTL(x, y, 63)
103 #define I32_ROTR(x, y) ROTR(x, y, 31)
104 #define I64_ROTR(x, y) ROTR(x, y, 63)
105
106 #define FMIN(x, y) \
107 ((UNLIKELY((x) != (x))) ? NAN \
108 : (UNLIKELY((y) != (y))) ? NAN \
109 : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? x : y) \
110 : (x < y) ? x : y)
111
112 #define FMAX(x, y) \
113 ((UNLIKELY((x) != (x))) ? NAN \
114 : (UNLIKELY((y) != (y))) ? NAN \
115 : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? y : x) \
116 : (x > y) ? x : y)
117
118 #define TRUNC_S(ut, st, ft, min, max, maxop, x) \
119 ((UNLIKELY((x) != (x))) ? TRAP(INVALID_CONVERSION) \
120 : (UNLIKELY((x) < (ft)(min) || (x) maxop (ft)(max))) ? TRAP(INT_OVERFLOW) \
121 : (ut)(st)(x))
122
123 #define I32_TRUNC_S_F32(x) TRUNC_S(u32, s32, f32, INT32_MIN, INT32_MAX, >=, x)
124 #define I64_TRUNC_S_F32(x) TRUNC_S(u64, s64, f32, INT64_MIN, INT64_MAX, >=, x)
125 #define I32_TRUNC_S_F64(x) TRUNC_S(u32, s32, f64, INT32_MIN, INT32_MAX, >, x)
126 #define I64_TRUNC_S_F64(x) TRUNC_S(u64, s64, f64, INT64_MIN, INT64_MAX, >=, x)
127
128 #define TRUNC_U(ut, ft, max, maxop, x) \
129 ((UNLIKELY((x) != (x))) ? TRAP(INVALID_CONVERSION) \
130 : (UNLIKELY((x) <= (ft)-1 || (x) maxop (ft)(max))) ? TRAP(INT_OVERFLOW) \
131 : (ut)(x))
132
133 #define I32_TRUNC_U_F32(x) TRUNC_U(u32, f32, UINT32_MAX, >=, x)
134 #define I64_TRUNC_U_F32(x) TRUNC_U(u64, f32, UINT64_MAX, >=, x)
135 #define I32_TRUNC_U_F64(x) TRUNC_U(u32, f64, UINT32_MAX, >, x)
136 #define I64_TRUNC_U_F64(x) TRUNC_U(u64, f64, UINT64_MAX, >=, x)
137
138 #define DEFINE_REINTERPRET(name, t1, t2) \
139 static inline t2 name(t1 x) { \
140 t2 result; \
141 memcpy(&result, &x, sizeof(result)); \
142 return result; \
143 }
144
145 DEFINE_REINTERPRET(f32_reinterpret_i32, u32, f32)
146 DEFINE_REINTERPRET(i32_reinterpret_f32, f32, u32)
147 DEFINE_REINTERPRET(f64_reinterpret_i64, u64, f64)
148 DEFINE_REINTERPRET(i64_reinterpret_f64, f64, u64)
149
150
151 static u32 func_types[1];
152
init_func_types(void)153 static void init_func_types(void) {
154 func_types[0] = wasm_rt_register_func_type(1, 1, WASM_RT_I32, WASM_RT_I32);
155 }
156
157 static u32 fac(u32);
158
init_globals(void)159 static void init_globals(void) {
160 }
161
fac(u32 p0)162 static u32 fac(u32 p0) {
163 FUNC_PROLOGUE;
164 u32 i0, i1, i2;
165 i0 = p0;
166 i1 = 0u;
167 i0 = i0 == i1;
168 if (i0) {
169 i0 = 1u;
170 } else {
171 i0 = p0;
172 i1 = p0;
173 i2 = 1u;
174 i1 -= i2;
175 i1 = fac(i1);
176 i0 *= i1;
177 }
178 FUNC_EPILOGUE;
179 return i0;
180 }
181
init_memory(void)182 static void init_memory(void) {
183 }
184
init_table(void)185 static void init_table(void) {
186 uint32_t offset;
187 }
188
189 /* export: 'fac' */
190 u32 (*WASM_RT_ADD_PREFIX(Z_facZ_ii))(u32);
191
init_exports(void)192 static void init_exports(void) {
193 /* export: 'fac' */
194 WASM_RT_ADD_PREFIX(Z_facZ_ii) = (&fac);
195 }
196
WASM_RT_ADD_PREFIX(init)197 void WASM_RT_ADD_PREFIX(init)(void) {
198 init_func_types();
199 init_globals();
200 init_memory();
201 init_table();
202 init_exports();
203 }
204