Line data Source code
1 : // Copyright 2015 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include <stdint.h>
6 : #include <stdlib.h>
7 : #include <string.h>
8 :
9 : #include "src/assembler-inl.h"
10 : #include "src/base/overflowing-math.h"
11 : #include "src/base/platform/elapsed-timer.h"
12 : #include "src/utils.h"
13 : #include "test/cctest/cctest.h"
14 : #include "test/cctest/compiler/value-helper.h"
15 : #include "test/cctest/wasm/wasm-run-utils.h"
16 : #include "test/common/wasm/test-signatures.h"
17 : #include "test/common/wasm/wasm-macro-gen.h"
18 :
19 : namespace v8 {
20 : namespace internal {
21 : namespace wasm {
22 : namespace test_run_wasm {
23 :
24 : // for even shorter tests.
25 : #define B1(a) WASM_BLOCK(a)
26 : #define B2(a, b) WASM_BLOCK(a, b)
27 : #define RET(x) x, kExprReturn
28 : #define RET_I8(x) WASM_I32V_2(x), kExprReturn
29 :
30 25899 : WASM_EXEC_TEST(Int32Const) {
31 12 : WasmRunner<int32_t> r(execution_tier);
32 : const int32_t kExpectedValue = 0x11223344;
33 : // return(kExpectedValue)
34 12 : BUILD(r, WASM_I32V_5(kExpectedValue));
35 12 : CHECK_EQ(kExpectedValue, r.Call());
36 12 : }
37 :
38 25899 : WASM_EXEC_TEST(Int32Const_many) {
39 708 : FOR_INT32_INPUTS(i) {
40 696 : WasmRunner<int32_t> r(execution_tier);
41 : const int32_t kExpectedValue = i;
42 : // return(kExpectedValue)
43 696 : BUILD(r, WASM_I32V(kExpectedValue));
44 696 : CHECK_EQ(kExpectedValue, r.Call());
45 696 : }
46 12 : }
47 :
48 25899 : WASM_EXEC_TEST(GraphTrimming) {
49 : // This WebAssembly code requires graph trimming in the TurboFan compiler.
50 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
51 12 : BUILD(r, kExprGetLocal, 0, kExprGetLocal, 0, kExprGetLocal, 0, kExprI32RemS,
52 : kExprI32Eq, kExprGetLocal, 0, kExprI32DivS, kExprUnreachable);
53 12 : r.Call(1);
54 12 : }
55 :
56 25899 : WASM_EXEC_TEST(Int32Param0) {
57 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
58 : // return(local[0])
59 12 : BUILD(r, WASM_GET_LOCAL(0));
60 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(i, r.Call(i)); }
61 12 : }
62 :
63 25899 : WASM_EXEC_TEST(Int32Param0_fallthru) {
64 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
65 : // local[0]
66 12 : BUILD(r, WASM_GET_LOCAL(0));
67 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(i, r.Call(i)); }
68 12 : }
69 :
70 25899 : WASM_EXEC_TEST(Int32Param1) {
71 12 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
72 : // local[1]
73 12 : BUILD(r, WASM_GET_LOCAL(1));
74 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(i, r.Call(-111, i)); }
75 12 : }
76 :
77 25899 : WASM_EXEC_TEST(Int32Add) {
78 12 : WasmRunner<int32_t> r(execution_tier);
79 : // 11 + 44
80 12 : BUILD(r, WASM_I32_ADD(WASM_I32V_1(11), WASM_I32V_1(44)));
81 12 : CHECK_EQ(55, r.Call());
82 12 : }
83 :
84 25899 : WASM_EXEC_TEST(Int32Add_P) {
85 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
86 : // p0 + 13
87 12 : BUILD(r, WASM_I32_ADD(WASM_I32V_1(13), WASM_GET_LOCAL(0)));
88 708 : FOR_INT32_INPUTS(i) { CHECK_EQ(base::AddWithWraparound(i, 13), r.Call(i)); }
89 12 : }
90 :
91 25899 : WASM_EXEC_TEST(Int32Add_P_fallthru) {
92 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
93 : // p0 + 13
94 12 : BUILD(r, WASM_I32_ADD(WASM_I32V_1(13), WASM_GET_LOCAL(0)));
95 708 : FOR_INT32_INPUTS(i) { CHECK_EQ(base::AddWithWraparound(i, 13), r.Call(i)); }
96 12 : }
97 :
98 48 : static void RunInt32AddTest(ExecutionTier execution_tier, const byte* code,
99 : size_t size) {
100 48 : TestSignatures sigs;
101 48 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
102 48 : r.builder().AddSignature(sigs.ii_v());
103 48 : r.builder().AddSignature(sigs.iii_v());
104 48 : r.Build(code, code + size);
105 2832 : FOR_INT32_INPUTS(i) {
106 164256 : FOR_INT32_INPUTS(j) {
107 161472 : int32_t expected = static_cast<int32_t>(static_cast<uint32_t>(i) +
108 161472 : static_cast<uint32_t>(j));
109 161472 : CHECK_EQ(expected, r.Call(i, j));
110 : }
111 48 : }
112 48 : }
113 :
114 25899 : WASM_EXEC_TEST(Int32Add_P2) {
115 : EXPERIMENTAL_FLAG_SCOPE(mv);
116 : static const byte code[] = {
117 : WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))};
118 12 : RunInt32AddTest(execution_tier, code, sizeof(code));
119 0 : }
120 :
121 25899 : WASM_EXEC_TEST(Int32Add_block1) {
122 : EXPERIMENTAL_FLAG_SCOPE(mv);
123 : static const byte code[] = {
124 : WASM_BLOCK_X(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)),
125 : kExprI32Add};
126 12 : RunInt32AddTest(execution_tier, code, sizeof(code));
127 0 : }
128 :
129 25899 : WASM_EXEC_TEST(Int32Add_block2) {
130 : EXPERIMENTAL_FLAG_SCOPE(mv);
131 : static const byte code[] = {
132 : WASM_BLOCK_X(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), kExprBr, DEPTH_0),
133 : kExprI32Add};
134 12 : RunInt32AddTest(execution_tier, code, sizeof(code));
135 0 : }
136 :
137 25899 : WASM_EXEC_TEST(Int32Add_multi_if) {
138 : EXPERIMENTAL_FLAG_SCOPE(mv);
139 : static const byte code[] = {
140 : WASM_IF_ELSE_X(0, WASM_GET_LOCAL(0),
141 : WASM_SEQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)),
142 : WASM_SEQ(WASM_GET_LOCAL(1), WASM_GET_LOCAL(0))),
143 : kExprI32Add};
144 12 : RunInt32AddTest(execution_tier, code, sizeof(code));
145 0 : }
146 :
147 25899 : WASM_EXEC_TEST(Float32Add) {
148 12 : WasmRunner<int32_t> r(execution_tier);
149 : // int(11.5f + 44.5f)
150 12 : BUILD(r,
151 : WASM_I32_SCONVERT_F32(WASM_F32_ADD(WASM_F32(11.5f), WASM_F32(44.5f))));
152 12 : CHECK_EQ(56, r.Call());
153 12 : }
154 :
155 25899 : WASM_EXEC_TEST(Float64Add) {
156 12 : WasmRunner<int32_t> r(execution_tier);
157 : // return int(13.5d + 43.5d)
158 12 : BUILD(r, WASM_I32_SCONVERT_F64(WASM_F64_ADD(WASM_F64(13.5), WASM_F64(43.5))));
159 12 : CHECK_EQ(57, r.Call());
160 12 : }
161 :
162 : // clang-format messes up the FOR_INT32_INPUTS macros.
163 : // clang-format off
164 : template<typename ctype>
165 300 : static void TestInt32Binop(ExecutionTier execution_tier, WasmOpcode opcode,
166 : ctype(*expected)(ctype, ctype)) {
167 17700 : FOR_INT32_INPUTS(i) {
168 1026600 : FOR_INT32_INPUTS(j) {
169 1009200 : WasmRunner<ctype> r(execution_tier);
170 : // Apply {opcode} on two constants.
171 1009200 : BUILD(r, WASM_BINOP(opcode, WASM_I32V(i), WASM_I32V(j)));
172 1009200 : CHECK_EQ(expected(i, j), r.Call());
173 : }
174 : }
175 : {
176 300 : WasmRunner<ctype, ctype, ctype> r(execution_tier);
177 : // Apply {opcode} on two parameters.
178 300 : BUILD(r, WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
179 17700 : FOR_INT32_INPUTS(i) {
180 1026600 : FOR_INT32_INPUTS(j) {
181 1009200 : CHECK_EQ(expected(i, j), r.Call(i, j));
182 : }
183 300 : }
184 : }
185 300 : }
186 : // clang-format on
187 :
188 : #define WASM_I32_BINOP_TEST(expr, ctype, expected) \
189 : WASM_EXEC_TEST(I32Binop_##expr) { \
190 : TestInt32Binop<ctype>(execution_tier, kExprI32##expr, \
191 : [](ctype a, ctype b) -> ctype { return expected; }); \
192 : }
193 :
194 187359 : WASM_I32_BINOP_TEST(Add, int32_t, base::AddWithWraparound(a, b))
195 187359 : WASM_I32_BINOP_TEST(Sub, int32_t, base::SubWithWraparound(a, b))
196 187359 : WASM_I32_BINOP_TEST(Mul, int32_t, base::MulWithWraparound(a, b))
197 106623 : WASM_I32_BINOP_TEST(DivS, int32_t,
198 : (a == kMinInt && b == -1) || b == 0
199 : ? static_cast<int32_t>(0xDEADBEEF)
200 : : a / b)
201 106623 : WASM_I32_BINOP_TEST(DivU, uint32_t, b == 0 ? 0xDEADBEEF : a / b)
202 106623 : WASM_I32_BINOP_TEST(RemS, int32_t, b == 0 ? 0xDEADBEEF : b == -1 ? 0 : a % b)
203 106623 : WASM_I32_BINOP_TEST(RemU, uint32_t, b == 0 ? 0xDEADBEEF : a % b)
204 106623 : WASM_I32_BINOP_TEST(And, int32_t, a& b)
205 106623 : WASM_I32_BINOP_TEST(Ior, int32_t, a | b)
206 106623 : WASM_I32_BINOP_TEST(Xor, int32_t, a ^ b)
207 187359 : WASM_I32_BINOP_TEST(Shl, int32_t, base::ShlWithWraparound(a, b))
208 106623 : WASM_I32_BINOP_TEST(ShrU, uint32_t, a >> (b & 0x1F))
209 106623 : WASM_I32_BINOP_TEST(ShrS, int32_t, a >> (b & 0x1F))
210 106623 : WASM_I32_BINOP_TEST(Ror, uint32_t, (a >> (b & 0x1F)) | (a << ((32 - b) & 0x1F)))
211 106623 : WASM_I32_BINOP_TEST(Rol, uint32_t, (a << (b & 0x1F)) | (a >> ((32 - b) & 0x1F)))
212 106623 : WASM_I32_BINOP_TEST(Eq, int32_t, a == b)
213 106623 : WASM_I32_BINOP_TEST(Ne, int32_t, a != b)
214 106623 : WASM_I32_BINOP_TEST(LtS, int32_t, a < b)
215 106623 : WASM_I32_BINOP_TEST(LeS, int32_t, a <= b)
216 106623 : WASM_I32_BINOP_TEST(LtU, uint32_t, a < b)
217 106623 : WASM_I32_BINOP_TEST(LeU, uint32_t, a <= b)
218 106623 : WASM_I32_BINOP_TEST(GtS, int32_t, a > b)
219 106623 : WASM_I32_BINOP_TEST(GeS, int32_t, a >= b)
220 106623 : WASM_I32_BINOP_TEST(GtU, uint32_t, a > b)
221 106623 : WASM_I32_BINOP_TEST(GeU, uint32_t, a >= b)
222 :
223 : #undef WASM_I32_BINOP_TEST
224 :
225 912 : void TestInt32Unop(ExecutionTier execution_tier, WasmOpcode opcode,
226 : int32_t expected, int32_t a) {
227 : {
228 912 : WasmRunner<int32_t> r(execution_tier);
229 : // return op K
230 912 : BUILD(r, WASM_UNOP(opcode, WASM_I32V(a)));
231 912 : CHECK_EQ(expected, r.Call());
232 : }
233 : {
234 912 : WasmRunner<int32_t, int32_t> r(execution_tier);
235 : // return op a
236 912 : BUILD(r, WASM_UNOP(opcode, WASM_GET_LOCAL(0)));
237 912 : CHECK_EQ(expected, r.Call(a));
238 : }
239 912 : }
240 :
241 25899 : WASM_EXEC_TEST(Int32Clz) {
242 12 : TestInt32Unop(execution_tier, kExprI32Clz, 0, 0x80001000);
243 12 : TestInt32Unop(execution_tier, kExprI32Clz, 1, 0x40000500);
244 12 : TestInt32Unop(execution_tier, kExprI32Clz, 2, 0x20000300);
245 12 : TestInt32Unop(execution_tier, kExprI32Clz, 3, 0x10000003);
246 12 : TestInt32Unop(execution_tier, kExprI32Clz, 4, 0x08050000);
247 12 : TestInt32Unop(execution_tier, kExprI32Clz, 5, 0x04006000);
248 12 : TestInt32Unop(execution_tier, kExprI32Clz, 6, 0x02000000);
249 12 : TestInt32Unop(execution_tier, kExprI32Clz, 7, 0x010000A0);
250 12 : TestInt32Unop(execution_tier, kExprI32Clz, 8, 0x00800C00);
251 12 : TestInt32Unop(execution_tier, kExprI32Clz, 9, 0x00400000);
252 12 : TestInt32Unop(execution_tier, kExprI32Clz, 10, 0x0020000D);
253 12 : TestInt32Unop(execution_tier, kExprI32Clz, 11, 0x00100F00);
254 12 : TestInt32Unop(execution_tier, kExprI32Clz, 12, 0x00080000);
255 12 : TestInt32Unop(execution_tier, kExprI32Clz, 13, 0x00041000);
256 12 : TestInt32Unop(execution_tier, kExprI32Clz, 14, 0x00020020);
257 12 : TestInt32Unop(execution_tier, kExprI32Clz, 15, 0x00010300);
258 12 : TestInt32Unop(execution_tier, kExprI32Clz, 16, 0x00008040);
259 12 : TestInt32Unop(execution_tier, kExprI32Clz, 17, 0x00004005);
260 12 : TestInt32Unop(execution_tier, kExprI32Clz, 18, 0x00002050);
261 12 : TestInt32Unop(execution_tier, kExprI32Clz, 19, 0x00001700);
262 12 : TestInt32Unop(execution_tier, kExprI32Clz, 20, 0x00000870);
263 12 : TestInt32Unop(execution_tier, kExprI32Clz, 21, 0x00000405);
264 12 : TestInt32Unop(execution_tier, kExprI32Clz, 22, 0x00000203);
265 12 : TestInt32Unop(execution_tier, kExprI32Clz, 23, 0x00000101);
266 12 : TestInt32Unop(execution_tier, kExprI32Clz, 24, 0x00000089);
267 12 : TestInt32Unop(execution_tier, kExprI32Clz, 25, 0x00000041);
268 12 : TestInt32Unop(execution_tier, kExprI32Clz, 26, 0x00000022);
269 12 : TestInt32Unop(execution_tier, kExprI32Clz, 27, 0x00000013);
270 12 : TestInt32Unop(execution_tier, kExprI32Clz, 28, 0x00000008);
271 12 : TestInt32Unop(execution_tier, kExprI32Clz, 29, 0x00000004);
272 12 : TestInt32Unop(execution_tier, kExprI32Clz, 30, 0x00000002);
273 12 : TestInt32Unop(execution_tier, kExprI32Clz, 31, 0x00000001);
274 12 : TestInt32Unop(execution_tier, kExprI32Clz, 32, 0x00000000);
275 12 : }
276 :
277 25899 : WASM_EXEC_TEST(Int32Ctz) {
278 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 32, 0x00000000);
279 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 31, 0x80000000);
280 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 30, 0x40000000);
281 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 29, 0x20000000);
282 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 28, 0x10000000);
283 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 27, 0xA8000000);
284 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 26, 0xF4000000);
285 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 25, 0x62000000);
286 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 24, 0x91000000);
287 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 23, 0xCD800000);
288 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 22, 0x09400000);
289 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 21, 0xAF200000);
290 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 20, 0xAC100000);
291 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 19, 0xE0B80000);
292 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 18, 0x9CE40000);
293 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 17, 0xC7920000);
294 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 16, 0xB8F10000);
295 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 15, 0x3B9F8000);
296 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 14, 0xDB4C4000);
297 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 13, 0xE9A32000);
298 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 12, 0xFCA61000);
299 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 11, 0x6C8A7800);
300 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 10, 0x8CE5A400);
301 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 9, 0xCB7D0200);
302 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 8, 0xCB4DC100);
303 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 7, 0xDFBEC580);
304 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 6, 0x27A9DB40);
305 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 5, 0xDE3BCB20);
306 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 4, 0xD7E8A610);
307 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 3, 0x9AFDBC88);
308 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 2, 0x9AFDBC84);
309 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 1, 0x9AFDBC82);
310 12 : TestInt32Unop(execution_tier, kExprI32Ctz, 0, 0x9AFDBC81);
311 12 : }
312 :
313 25899 : WASM_EXEC_TEST(Int32Popcnt) {
314 12 : TestInt32Unop(execution_tier, kExprI32Popcnt, 32, 0xFFFFFFFF);
315 12 : TestInt32Unop(execution_tier, kExprI32Popcnt, 0, 0x00000000);
316 12 : TestInt32Unop(execution_tier, kExprI32Popcnt, 1, 0x00008000);
317 12 : TestInt32Unop(execution_tier, kExprI32Popcnt, 13, 0x12345678);
318 12 : TestInt32Unop(execution_tier, kExprI32Popcnt, 19, 0xFEDCBA09);
319 12 : }
320 :
321 25899 : WASM_EXEC_TEST(I32Eqz) {
322 12 : TestInt32Unop(execution_tier, kExprI32Eqz, 0, 1);
323 12 : TestInt32Unop(execution_tier, kExprI32Eqz, 0, -1);
324 12 : TestInt32Unop(execution_tier, kExprI32Eqz, 0, -827343);
325 12 : TestInt32Unop(execution_tier, kExprI32Eqz, 0, 8888888);
326 12 : TestInt32Unop(execution_tier, kExprI32Eqz, 1, 0);
327 12 : }
328 :
329 :
330 25899 : WASM_EXEC_TEST(Int32DivS_trap) {
331 12 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
332 12 : BUILD(r, WASM_I32_DIVS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
333 : const int32_t kMin = std::numeric_limits<int32_t>::min();
334 12 : CHECK_EQ(0, r.Call(0, 100));
335 24 : CHECK_TRAP(r.Call(100, 0));
336 24 : CHECK_TRAP(r.Call(-1001, 0));
337 24 : CHECK_TRAP(r.Call(kMin, -1));
338 24 : CHECK_TRAP(r.Call(kMin, 0));
339 12 : }
340 :
341 25899 : WASM_EXEC_TEST(Int32RemS_trap) {
342 12 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
343 12 : BUILD(r, WASM_I32_REMS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
344 : const int32_t kMin = std::numeric_limits<int32_t>::min();
345 12 : CHECK_EQ(33, r.Call(133, 100));
346 12 : CHECK_EQ(0, r.Call(kMin, -1));
347 24 : CHECK_TRAP(r.Call(100, 0));
348 24 : CHECK_TRAP(r.Call(-1001, 0));
349 24 : CHECK_TRAP(r.Call(kMin, 0));
350 12 : }
351 :
352 25899 : WASM_EXEC_TEST(Int32DivU_trap) {
353 12 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
354 12 : BUILD(r, WASM_I32_DIVU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
355 : const int32_t kMin = std::numeric_limits<int32_t>::min();
356 12 : CHECK_EQ(0, r.Call(0, 100));
357 12 : CHECK_EQ(0, r.Call(kMin, -1));
358 24 : CHECK_TRAP(r.Call(100, 0));
359 24 : CHECK_TRAP(r.Call(-1001, 0));
360 24 : CHECK_TRAP(r.Call(kMin, 0));
361 12 : }
362 :
363 25899 : WASM_EXEC_TEST(Int32RemU_trap) {
364 12 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
365 12 : BUILD(r, WASM_I32_REMU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
366 12 : CHECK_EQ(17, r.Call(217, 100));
367 : const int32_t kMin = std::numeric_limits<int32_t>::min();
368 24 : CHECK_TRAP(r.Call(100, 0));
369 24 : CHECK_TRAP(r.Call(-1001, 0));
370 24 : CHECK_TRAP(r.Call(kMin, 0));
371 12 : CHECK_EQ(kMin, r.Call(kMin, -1));
372 12 : }
373 :
374 25899 : WASM_EXEC_TEST(Int32DivS_byzero_const) {
375 132 : for (int8_t denom = -2; denom < 8; ++denom) {
376 120 : WasmRunner<int32_t, int32_t> r(execution_tier);
377 120 : BUILD(r, WASM_I32_DIVS(WASM_GET_LOCAL(0), WASM_I32V_1(denom)));
378 1920 : for (int32_t val = -7; val < 8; ++val) {
379 1800 : if (denom == 0) {
380 360 : CHECK_TRAP(r.Call(val));
381 : } else {
382 1620 : CHECK_EQ(val / denom, r.Call(val));
383 : }
384 : }
385 120 : }
386 12 : }
387 :
388 25899 : WASM_EXEC_TEST(Int32AsmjsDivS_byzero_const) {
389 132 : for (int8_t denom = -2; denom < 8; ++denom) {
390 120 : WasmRunner<int32_t, int32_t> r(execution_tier);
391 : r.builder().ChangeOriginToAsmjs();
392 120 : BUILD(r, WASM_I32_ASMJS_DIVS(WASM_GET_LOCAL(0), WASM_I32V_1(denom)));
393 7080 : FOR_INT32_INPUTS(i) {
394 6960 : if (denom == 0) {
395 696 : CHECK_EQ(0, r.Call(i));
396 6264 : } else if (denom == -1 && i == std::numeric_limits<int32_t>::min()) {
397 12 : CHECK_EQ(std::numeric_limits<int32_t>::min(), r.Call(i));
398 : } else {
399 6252 : CHECK_EQ(i / denom, r.Call(i));
400 : }
401 : }
402 120 : }
403 12 : }
404 :
405 25899 : WASM_EXEC_TEST(Int32AsmjsRemS_byzero_const) {
406 132 : for (int8_t denom = -2; denom < 8; ++denom) {
407 120 : WasmRunner<int32_t, int32_t> r(execution_tier);
408 : r.builder().ChangeOriginToAsmjs();
409 120 : BUILD(r, WASM_I32_ASMJS_REMS(WASM_GET_LOCAL(0), WASM_I32V_1(denom)));
410 7080 : FOR_INT32_INPUTS(i) {
411 6960 : if (denom == 0) {
412 696 : CHECK_EQ(0, r.Call(i));
413 6264 : } else if (denom == -1 && i == std::numeric_limits<int32_t>::min()) {
414 12 : CHECK_EQ(0, r.Call(i));
415 : } else {
416 6252 : CHECK_EQ(i % denom, r.Call(i));
417 : }
418 : }
419 120 : }
420 12 : }
421 :
422 25887 : WASM_EXEC_TEST(Int32DivU_byzero_const) {
423 : for (uint32_t denom = 0xFFFFFFFE; denom < 8; ++denom) {
424 : WasmRunner<uint32_t, uint32_t> r(execution_tier);
425 : BUILD(r, WASM_I32_DIVU(WASM_GET_LOCAL(0), WASM_I32V_1(denom)));
426 :
427 : for (uint32_t val = 0xFFFFFFF0; val < 8; ++val) {
428 : if (denom == 0) {
429 : CHECK_TRAP(r.Call(val));
430 : } else {
431 : CHECK_EQ(val / denom, r.Call(val));
432 : }
433 : }
434 : }
435 0 : }
436 :
437 25899 : WASM_EXEC_TEST(Int32DivS_trap_effect) {
438 12 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
439 12 : r.builder().AddMemory(kWasmPageSize);
440 :
441 12 : BUILD(r, WASM_IF_ELSE_I(
442 : WASM_GET_LOCAL(0),
443 : WASM_I32_DIVS(
444 : WASM_BLOCK_I(WASM_STORE_MEM(MachineType::Int8(), WASM_ZERO,
445 : WASM_GET_LOCAL(0)),
446 : WASM_GET_LOCAL(0)),
447 : WASM_GET_LOCAL(1)),
448 : WASM_I32_DIVS(
449 : WASM_BLOCK_I(WASM_STORE_MEM(MachineType::Int8(), WASM_ZERO,
450 : WASM_GET_LOCAL(0)),
451 : WASM_GET_LOCAL(0)),
452 : WASM_GET_LOCAL(1))));
453 12 : CHECK_EQ(0, r.Call(0, 100));
454 24 : CHECK_TRAP(r.Call(8, 0));
455 24 : CHECK_TRAP(r.Call(4, 0));
456 24 : CHECK_TRAP(r.Call(0, 0));
457 12 : }
458 :
459 72 : void TestFloat32Binop(ExecutionTier execution_tier, WasmOpcode opcode,
460 : int32_t expected, float a, float b) {
461 : {
462 72 : WasmRunner<int32_t> r(execution_tier);
463 : // return K op K
464 360 : BUILD(r, WASM_BINOP(opcode, WASM_F32(a), WASM_F32(b)));
465 72 : CHECK_EQ(expected, r.Call());
466 : }
467 : {
468 72 : WasmRunner<int32_t, float, float> r(execution_tier);
469 : // return a op b
470 72 : BUILD(r, WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
471 72 : CHECK_EQ(expected, r.Call(a, b));
472 : }
473 72 : }
474 :
475 48 : void TestFloat32BinopWithConvert(ExecutionTier execution_tier,
476 : WasmOpcode opcode, int32_t expected, float a,
477 : float b) {
478 : {
479 48 : WasmRunner<int32_t> r(execution_tier);
480 : // return int(K op K)
481 240 : BUILD(r,
482 : WASM_I32_SCONVERT_F32(WASM_BINOP(opcode, WASM_F32(a), WASM_F32(b))));
483 48 : CHECK_EQ(expected, r.Call());
484 : }
485 : {
486 48 : WasmRunner<int32_t, float, float> r(execution_tier);
487 : // return int(a op b)
488 48 : BUILD(r, WASM_I32_SCONVERT_F32(
489 : WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))));
490 48 : CHECK_EQ(expected, r.Call(a, b));
491 : }
492 48 : }
493 :
494 48 : void TestFloat32UnopWithConvert(ExecutionTier execution_tier, WasmOpcode opcode,
495 : int32_t expected, float a) {
496 : {
497 48 : WasmRunner<int32_t> r(execution_tier);
498 : // return int(op(K))
499 144 : BUILD(r, WASM_I32_SCONVERT_F32(WASM_UNOP(opcode, WASM_F32(a))));
500 48 : CHECK_EQ(expected, r.Call());
501 : }
502 : {
503 48 : WasmRunner<int32_t, float> r(execution_tier);
504 : // return int(op(a))
505 48 : BUILD(r, WASM_I32_SCONVERT_F32(WASM_UNOP(opcode, WASM_GET_LOCAL(0))));
506 48 : CHECK_EQ(expected, r.Call(a));
507 : }
508 48 : }
509 :
510 72 : void TestFloat64Binop(ExecutionTier execution_tier, WasmOpcode opcode,
511 : int32_t expected, double a, double b) {
512 : {
513 72 : WasmRunner<int32_t> r(execution_tier);
514 : // return K op K
515 216 : BUILD(r, WASM_BINOP(opcode, WASM_F64(a), WASM_F64(b)));
516 72 : CHECK_EQ(expected, r.Call());
517 : }
518 : {
519 72 : WasmRunner<int32_t, double, double> r(execution_tier);
520 : // return a op b
521 72 : BUILD(r, WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
522 72 : CHECK_EQ(expected, r.Call(a, b));
523 : }
524 72 : }
525 :
526 48 : void TestFloat64BinopWithConvert(ExecutionTier execution_tier,
527 : WasmOpcode opcode, int32_t expected, double a,
528 : double b) {
529 : {
530 48 : WasmRunner<int32_t> r(execution_tier);
531 : // return int(K op K)
532 144 : BUILD(r,
533 : WASM_I32_SCONVERT_F64(WASM_BINOP(opcode, WASM_F64(a), WASM_F64(b))));
534 48 : CHECK_EQ(expected, r.Call());
535 : }
536 : {
537 48 : WasmRunner<int32_t, double, double> r(execution_tier);
538 48 : BUILD(r, WASM_I32_SCONVERT_F64(
539 : WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))));
540 48 : CHECK_EQ(expected, r.Call(a, b));
541 : }
542 48 : }
543 :
544 48 : void TestFloat64UnopWithConvert(ExecutionTier execution_tier, WasmOpcode opcode,
545 : int32_t expected, double a) {
546 : {
547 48 : WasmRunner<int32_t> r(execution_tier);
548 : // return int(op(K))
549 96 : BUILD(r, WASM_I32_SCONVERT_F64(WASM_UNOP(opcode, WASM_F64(a))));
550 48 : CHECK_EQ(expected, r.Call());
551 : }
552 : {
553 48 : WasmRunner<int32_t, double> r(execution_tier);
554 : // return int(op(a))
555 48 : BUILD(r, WASM_I32_SCONVERT_F64(WASM_UNOP(opcode, WASM_GET_LOCAL(0))));
556 48 : CHECK_EQ(expected, r.Call(a));
557 : }
558 48 : }
559 :
560 25899 : WASM_EXEC_TEST(Float32Binops) {
561 12 : TestFloat32Binop(execution_tier, kExprF32Eq, 1, 8.125f, 8.125f);
562 12 : TestFloat32Binop(execution_tier, kExprF32Ne, 1, 8.125f, 8.127f);
563 12 : TestFloat32Binop(execution_tier, kExprF32Lt, 1, -9.5f, -9.0f);
564 12 : TestFloat32Binop(execution_tier, kExprF32Le, 1, -1111.0f, -1111.0f);
565 12 : TestFloat32Binop(execution_tier, kExprF32Gt, 1, -9.0f, -9.5f);
566 12 : TestFloat32Binop(execution_tier, kExprF32Ge, 1, -1111.0f, -1111.0f);
567 :
568 12 : TestFloat32BinopWithConvert(execution_tier, kExprF32Add, 10, 3.5f, 6.5f);
569 12 : TestFloat32BinopWithConvert(execution_tier, kExprF32Sub, 2, 44.5f, 42.5f);
570 12 : TestFloat32BinopWithConvert(execution_tier, kExprF32Mul, -66, -132.1f, 0.5f);
571 12 : TestFloat32BinopWithConvert(execution_tier, kExprF32Div, 11, 22.1f, 2.0f);
572 12 : }
573 :
574 25899 : WASM_EXEC_TEST(Float32Unops) {
575 12 : TestFloat32UnopWithConvert(execution_tier, kExprF32Abs, 8, 8.125f);
576 12 : TestFloat32UnopWithConvert(execution_tier, kExprF32Abs, 9, -9.125f);
577 12 : TestFloat32UnopWithConvert(execution_tier, kExprF32Neg, -213, 213.125f);
578 12 : TestFloat32UnopWithConvert(execution_tier, kExprF32Sqrt, 12, 144.4f);
579 12 : }
580 :
581 25899 : WASM_EXEC_TEST(Float64Binops) {
582 12 : TestFloat64Binop(execution_tier, kExprF64Eq, 1, 16.25, 16.25);
583 12 : TestFloat64Binop(execution_tier, kExprF64Ne, 1, 16.25, 16.15);
584 12 : TestFloat64Binop(execution_tier, kExprF64Lt, 1, -32.4, 11.7);
585 12 : TestFloat64Binop(execution_tier, kExprF64Le, 1, -88.9, -88.9);
586 12 : TestFloat64Binop(execution_tier, kExprF64Gt, 1, 11.7, -32.4);
587 12 : TestFloat64Binop(execution_tier, kExprF64Ge, 1, -88.9, -88.9);
588 :
589 12 : TestFloat64BinopWithConvert(execution_tier, kExprF64Add, 100, 43.5, 56.5);
590 : TestFloat64BinopWithConvert(execution_tier, kExprF64Sub, 200, 12200.1,
591 12 : 12000.1);
592 12 : TestFloat64BinopWithConvert(execution_tier, kExprF64Mul, -33, 134, -0.25);
593 12 : TestFloat64BinopWithConvert(execution_tier, kExprF64Div, -1111, -2222.3, 2);
594 12 : }
595 :
596 25899 : WASM_EXEC_TEST(Float64Unops) {
597 12 : TestFloat64UnopWithConvert(execution_tier, kExprF64Abs, 108, 108.125);
598 12 : TestFloat64UnopWithConvert(execution_tier, kExprF64Abs, 209, -209.125);
599 12 : TestFloat64UnopWithConvert(execution_tier, kExprF64Neg, -209, 209.125);
600 12 : TestFloat64UnopWithConvert(execution_tier, kExprF64Sqrt, 13, 169.4);
601 12 : }
602 :
603 25899 : WASM_EXEC_TEST(Float32Neg) {
604 12 : WasmRunner<float, float> r(execution_tier);
605 12 : BUILD(r, WASM_F32_NEG(WASM_GET_LOCAL(0)));
606 :
607 1392 : FOR_FLOAT32_INPUTS(i) {
608 2760 : CHECK_EQ(0x80000000, bit_cast<uint32_t>(i) ^ bit_cast<uint32_t>(r.Call(i)));
609 12 : }
610 12 : }
611 :
612 25899 : WASM_EXEC_TEST(Float64Neg) {
613 12 : WasmRunner<double, double> r(execution_tier);
614 12 : BUILD(r, WASM_F64_NEG(WASM_GET_LOCAL(0)));
615 :
616 600 : FOR_FLOAT64_INPUTS(i) {
617 1176 : CHECK_EQ(0x8000000000000000,
618 : bit_cast<uint64_t>(i) ^ bit_cast<uint64_t>(r.Call(i)));
619 12 : }
620 12 : }
621 :
622 25899 : WASM_EXEC_TEST(IfElse_P) {
623 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
624 : // if (p0) return 11; else return 22;
625 12 : BUILD(r, WASM_IF_ELSE_I(WASM_GET_LOCAL(0), // --
626 : WASM_I32V_1(11), // --
627 : WASM_I32V_1(22))); // --
628 708 : FOR_INT32_INPUTS(i) {
629 696 : int32_t expected = i ? 11 : 22;
630 696 : CHECK_EQ(expected, r.Call(i));
631 12 : }
632 12 : }
633 :
634 25899 : WASM_EXEC_TEST(If_empty1) {
635 12 : WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_tier);
636 12 : BUILD(r, WASM_GET_LOCAL(0), kExprIf, kLocalVoid, kExprEnd, WASM_GET_LOCAL(1));
637 12 : FOR_UINT32_INPUTS(i) { CHECK_EQ(i, r.Call(i - 9, i)); }
638 12 : }
639 :
640 25899 : WASM_EXEC_TEST(IfElse_empty1) {
641 12 : WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_tier);
642 12 : BUILD(r, WASM_GET_LOCAL(0), kExprIf, kLocalVoid, kExprElse, kExprEnd,
643 : WASM_GET_LOCAL(1));
644 12 : FOR_UINT32_INPUTS(i) { CHECK_EQ(i, r.Call(i - 8, i)); }
645 12 : }
646 :
647 25899 : WASM_EXEC_TEST(IfElse_empty2) {
648 12 : WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_tier);
649 12 : BUILD(r, WASM_GET_LOCAL(0), kExprIf, kLocalVoid, WASM_NOP, kExprElse,
650 : kExprEnd, WASM_GET_LOCAL(1));
651 12 : FOR_UINT32_INPUTS(i) { CHECK_EQ(i, r.Call(i - 7, i)); }
652 12 : }
653 :
654 25899 : WASM_EXEC_TEST(IfElse_empty3) {
655 12 : WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_tier);
656 12 : BUILD(r, WASM_GET_LOCAL(0), kExprIf, kLocalVoid, kExprElse, WASM_NOP,
657 : kExprEnd, WASM_GET_LOCAL(1));
658 12 : FOR_UINT32_INPUTS(i) { CHECK_EQ(i, r.Call(i - 6, i)); }
659 12 : }
660 :
661 25899 : WASM_EXEC_TEST(If_chain1) {
662 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
663 : // if (p0) 13; if (p0) 14; 15
664 12 : BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_NOP),
665 : WASM_IF(WASM_GET_LOCAL(0), WASM_NOP), WASM_I32V_1(15));
666 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(15, r.Call(i)); }
667 12 : }
668 :
669 25899 : WASM_EXEC_TEST(If_chain_set) {
670 12 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
671 : // if (p0) p1 = 73; if (p0) p1 = 74; p1
672 12 : BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(1, WASM_I32V_2(73))),
673 : WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(1, WASM_I32V_2(74))),
674 : WASM_GET_LOCAL(1));
675 708 : FOR_INT32_INPUTS(i) {
676 696 : int32_t expected = i ? 74 : i;
677 696 : CHECK_EQ(expected, r.Call(i, i));
678 12 : }
679 12 : }
680 :
681 25899 : WASM_EXEC_TEST(IfElse_Unreachable1) {
682 12 : WasmRunner<int32_t> r(execution_tier);
683 : // 0 ? unreachable : 27
684 12 : BUILD(r, WASM_IF_ELSE_I(WASM_ZERO, // --
685 : WASM_UNREACHABLE, // --
686 : WASM_I32V_1(27))); // --
687 12 : CHECK_EQ(27, r.Call());
688 12 : }
689 :
690 25899 : WASM_EXEC_TEST(IfElse_Unreachable2) {
691 12 : WasmRunner<int32_t> r(execution_tier);
692 : // 1 ? 28 : unreachable
693 12 : BUILD(r, WASM_IF_ELSE_I(WASM_I32V_1(1), // --
694 : WASM_I32V_1(28), // --
695 : WASM_UNREACHABLE)); // --
696 12 : CHECK_EQ(28, r.Call());
697 12 : }
698 :
699 25899 : WASM_EXEC_TEST(Return12) {
700 12 : WasmRunner<int32_t> r(execution_tier);
701 :
702 12 : BUILD(r, RET_I8(12));
703 12 : CHECK_EQ(12, r.Call());
704 12 : }
705 :
706 25899 : WASM_EXEC_TEST(Return17) {
707 12 : WasmRunner<int32_t> r(execution_tier);
708 :
709 12 : BUILD(r, WASM_BLOCK(RET_I8(17)), WASM_ZERO);
710 12 : CHECK_EQ(17, r.Call());
711 12 : }
712 :
713 25899 : WASM_EXEC_TEST(Return_I32) {
714 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
715 :
716 12 : BUILD(r, RET(WASM_GET_LOCAL(0)));
717 :
718 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(i, r.Call(i)); }
719 12 : }
720 :
721 25899 : WASM_EXEC_TEST(Return_F32) {
722 12 : WasmRunner<float, float> r(execution_tier);
723 :
724 12 : BUILD(r, RET(WASM_GET_LOCAL(0)));
725 :
726 1392 : FOR_FLOAT32_INPUTS(i) {
727 : float expect = i;
728 1380 : float result = r.Call(expect);
729 1380 : if (std::isnan(expect)) {
730 24 : CHECK(std::isnan(result));
731 : } else {
732 1356 : CHECK_EQ(expect, result);
733 : }
734 12 : }
735 12 : }
736 :
737 25899 : WASM_EXEC_TEST(Return_F64) {
738 12 : WasmRunner<double, double> r(execution_tier);
739 :
740 12 : BUILD(r, RET(WASM_GET_LOCAL(0)));
741 :
742 600 : FOR_FLOAT64_INPUTS(i) {
743 : double expect = i;
744 588 : double result = r.Call(expect);
745 588 : if (std::isnan(expect)) {
746 24 : CHECK(std::isnan(result));
747 : } else {
748 564 : CHECK_EQ(expect, result);
749 : }
750 12 : }
751 12 : }
752 :
753 25899 : WASM_EXEC_TEST(Select_float_parameters) {
754 12 : WasmRunner<float, float, float, int32_t> r(execution_tier);
755 : // return select(11, 22, a);
756 12 : BUILD(r,
757 : WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)));
758 24 : CHECK_FLOAT_EQ(2.0f, r.Call(2.0f, 1.0f, 1));
759 12 : }
760 :
761 25899 : WASM_EXEC_TEST(Select) {
762 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
763 : // return select(11, 22, a);
764 12 : BUILD(r, WASM_SELECT(WASM_I32V_1(11), WASM_I32V_1(22), WASM_GET_LOCAL(0)));
765 708 : FOR_INT32_INPUTS(i) {
766 696 : int32_t expected = i ? 11 : 22;
767 696 : CHECK_EQ(expected, r.Call(i));
768 12 : }
769 12 : }
770 :
771 25899 : WASM_EXEC_TEST(Select_strict1) {
772 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
773 : // select(a=0, a=1, a=2); return a
774 12 : BUILD(r, WASM_SELECT(WASM_TEE_LOCAL(0, WASM_ZERO),
775 : WASM_TEE_LOCAL(0, WASM_I32V_1(1)),
776 : WASM_TEE_LOCAL(0, WASM_I32V_1(2))),
777 : WASM_DROP, WASM_GET_LOCAL(0));
778 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(2, r.Call(i)); }
779 12 : }
780 :
781 25899 : WASM_EXEC_TEST(Select_strict2) {
782 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
783 : r.AllocateLocal(kWasmI32);
784 : r.AllocateLocal(kWasmI32);
785 : // select(b=5, c=6, a)
786 12 : BUILD(r, WASM_SELECT(WASM_TEE_LOCAL(1, WASM_I32V_1(5)),
787 : WASM_TEE_LOCAL(2, WASM_I32V_1(6)), WASM_GET_LOCAL(0)));
788 708 : FOR_INT32_INPUTS(i) {
789 696 : int32_t expected = i ? 5 : 6;
790 696 : CHECK_EQ(expected, r.Call(i));
791 12 : }
792 12 : }
793 :
794 25899 : WASM_EXEC_TEST(Select_strict3) {
795 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
796 : r.AllocateLocal(kWasmI32);
797 : r.AllocateLocal(kWasmI32);
798 : // select(b=5, c=6, a=b)
799 12 : BUILD(r, WASM_SELECT(WASM_TEE_LOCAL(1, WASM_I32V_1(5)),
800 : WASM_TEE_LOCAL(2, WASM_I32V_1(6)),
801 : WASM_TEE_LOCAL(0, WASM_GET_LOCAL(1))));
802 708 : FOR_INT32_INPUTS(i) {
803 : int32_t expected = 5;
804 696 : CHECK_EQ(expected, r.Call(i));
805 12 : }
806 12 : }
807 :
808 25899 : WASM_EXEC_TEST(BrIf_strict) {
809 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
810 12 : BUILD(r, WASM_BLOCK_I(WASM_BRV_IF(0, WASM_GET_LOCAL(0),
811 : WASM_TEE_LOCAL(0, WASM_I32V_2(99)))));
812 :
813 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(i, r.Call(i)); }
814 12 : }
815 :
816 25899 : WASM_EXEC_TEST(Br_height) {
817 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
818 12 : BUILD(r, WASM_BLOCK_I(
819 : WASM_BLOCK(WASM_BRV_IFD(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)),
820 : WASM_RETURN1(WASM_I32V_1(9))),
821 : WASM_BRV(0, WASM_I32V_1(8))));
822 :
823 72 : for (int32_t i = 0; i < 5; i++) {
824 60 : int32_t expected = i != 0 ? 8 : 9;
825 60 : CHECK_EQ(expected, r.Call(i));
826 12 : }
827 12 : }
828 :
829 25899 : WASM_EXEC_TEST(Regression_660262) {
830 12 : WasmRunner<int32_t> r(execution_tier);
831 12 : r.builder().AddMemory(kWasmPageSize);
832 12 : BUILD(r, kExprI32Const, 0x00, kExprI32Const, 0x00, kExprI32LoadMem, 0x00,
833 : 0x0F, kExprBrTable, 0x00, 0x80, 0x00); // entries=0
834 12 : r.Call();
835 12 : }
836 :
837 25899 : WASM_EXEC_TEST(BrTable0a) {
838 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
839 12 : BUILD(r, B1(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(0)))),
840 : WASM_I32V_2(91));
841 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(91, r.Call(i)); }
842 12 : }
843 :
844 25899 : WASM_EXEC_TEST(BrTable0b) {
845 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
846 12 : BUILD(r,
847 : B1(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 1, BR_TARGET(0), BR_TARGET(0)))),
848 : WASM_I32V_2(92));
849 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(92, r.Call(i)); }
850 12 : }
851 :
852 25899 : WASM_EXEC_TEST(BrTable0c) {
853 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
854 12 : BUILD(
855 : r,
856 : B1(B2(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 1, BR_TARGET(0), BR_TARGET(1))),
857 : RET_I8(76))),
858 : WASM_I32V_2(77));
859 708 : FOR_INT32_INPUTS(i) {
860 696 : int32_t expected = i == 0 ? 76 : 77;
861 696 : CHECK_EQ(expected, r.Call(i));
862 12 : }
863 12 : }
864 :
865 25899 : WASM_EXEC_TEST(BrTable1) {
866 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
867 12 : BUILD(r, B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(0))), RET_I8(93));
868 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(93, r.Call(i)); }
869 12 : }
870 :
871 25899 : WASM_EXEC_TEST(BrTable_loop) {
872 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
873 12 : BUILD(r,
874 : B2(B1(WASM_LOOP(WASM_BR_TABLE(WASM_INC_LOCAL_BYV(0, 1), 2, BR_TARGET(2),
875 : BR_TARGET(1), BR_TARGET(0)))),
876 : RET_I8(99)),
877 : WASM_I32V_2(98));
878 12 : CHECK_EQ(99, r.Call(0));
879 12 : CHECK_EQ(98, r.Call(-1));
880 12 : CHECK_EQ(98, r.Call(-2));
881 12 : CHECK_EQ(98, r.Call(-3));
882 12 : CHECK_EQ(98, r.Call(-100));
883 12 : }
884 :
885 25899 : WASM_EXEC_TEST(BrTable_br) {
886 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
887 12 : BUILD(r,
888 : B2(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 1, BR_TARGET(1), BR_TARGET(0))),
889 : RET_I8(91)),
890 : WASM_I32V_2(99));
891 12 : CHECK_EQ(99, r.Call(0));
892 12 : CHECK_EQ(91, r.Call(1));
893 12 : CHECK_EQ(91, r.Call(2));
894 12 : CHECK_EQ(91, r.Call(3));
895 12 : }
896 :
897 25899 : WASM_EXEC_TEST(BrTable_br2) {
898 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
899 :
900 12 : BUILD(r, B2(B2(B2(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 3, BR_TARGET(1),
901 : BR_TARGET(2), BR_TARGET(3), BR_TARGET(0))),
902 : RET_I8(85)),
903 : RET_I8(86)),
904 : RET_I8(87)),
905 : WASM_I32V_2(88));
906 12 : CHECK_EQ(86, r.Call(0));
907 12 : CHECK_EQ(87, r.Call(1));
908 12 : CHECK_EQ(88, r.Call(2));
909 12 : CHECK_EQ(85, r.Call(3));
910 12 : CHECK_EQ(85, r.Call(4));
911 12 : CHECK_EQ(85, r.Call(5));
912 12 : }
913 :
914 25899 : WASM_EXEC_TEST(BrTable4) {
915 60 : for (int i = 0; i < 4; ++i) {
916 192 : for (int t = 0; t < 4; ++t) {
917 192 : uint32_t cases[] = {0, 1, 2, 3};
918 192 : cases[i] = t;
919 768 : byte code[] = {B2(B2(B2(B2(B1(WASM_BR_TABLE(
920 : WASM_GET_LOCAL(0), 3, BR_TARGET(cases[0]),
921 : BR_TARGET(cases[1]), BR_TARGET(cases[2]),
922 : BR_TARGET(cases[3]))),
923 : RET_I8(70)),
924 : RET_I8(71)),
925 : RET_I8(72)),
926 : RET_I8(73)),
927 960 : WASM_I32V_2(75)};
928 :
929 192 : WasmRunner<int32_t, int32_t> r(execution_tier);
930 192 : r.Build(code, code + arraysize(code));
931 :
932 10368 : for (int x = -3; x < 50; ++x) {
933 10176 : int index = (x > 3 || x < 0) ? 3 : x;
934 10176 : int32_t expected = 70 + cases[index];
935 10176 : CHECK_EQ(expected, r.Call(x));
936 : }
937 192 : }
938 : }
939 12 : }
940 :
941 25899 : WASM_EXEC_TEST(BrTable4x4) {
942 60 : for (byte a = 0; a < 4; ++a) {
943 192 : for (byte b = 0; b < 4; ++b) {
944 768 : for (byte c = 0; c < 4; ++c) {
945 3072 : for (byte d = 0; d < 4; ++d) {
946 12288 : for (int i = 0; i < 4; ++i) {
947 12288 : uint32_t cases[] = {a, b, c, d};
948 : byte code[] = {
949 : B2(B2(B2(B2(B1(WASM_BR_TABLE(
950 : WASM_GET_LOCAL(0), 3, BR_TARGET(cases[0]),
951 : BR_TARGET(cases[1]), BR_TARGET(cases[2]),
952 : BR_TARGET(cases[3]))),
953 : RET_I8(50)),
954 : RET_I8(51)),
955 : RET_I8(52)),
956 : RET_I8(53)),
957 12288 : WASM_I32V_2(55)};
958 :
959 12288 : WasmRunner<int32_t, int32_t> r(execution_tier);
960 12288 : r.Build(code, code + arraysize(code));
961 :
962 663552 : for (int x = -6; x < 47; ++x) {
963 651264 : int index = (x > 3 || x < 0) ? 3 : x;
964 651264 : int32_t expected = 50 + cases[index];
965 651264 : CHECK_EQ(expected, r.Call(x));
966 : }
967 12288 : }
968 : }
969 : }
970 : }
971 : }
972 12 : }
973 :
974 25899 : WASM_EXEC_TEST(BrTable4_fallthru) {
975 : byte code[] = {
976 : B2(B2(B2(B2(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 3, BR_TARGET(0),
977 : BR_TARGET(1), BR_TARGET(2), BR_TARGET(3))),
978 : WASM_INC_LOCAL_BY(1, 1)),
979 : WASM_INC_LOCAL_BY(1, 2)),
980 : WASM_INC_LOCAL_BY(1, 4)),
981 : WASM_INC_LOCAL_BY(1, 8)),
982 12 : WASM_GET_LOCAL(1)};
983 :
984 12 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
985 12 : r.Build(code, code + arraysize(code));
986 :
987 12 : CHECK_EQ(15, r.Call(0, 0));
988 12 : CHECK_EQ(14, r.Call(1, 0));
989 12 : CHECK_EQ(12, r.Call(2, 0));
990 12 : CHECK_EQ(8, r.Call(3, 0));
991 12 : CHECK_EQ(8, r.Call(4, 0));
992 :
993 12 : CHECK_EQ(115, r.Call(0, 100));
994 12 : CHECK_EQ(114, r.Call(1, 100));
995 12 : CHECK_EQ(112, r.Call(2, 100));
996 12 : CHECK_EQ(108, r.Call(3, 100));
997 12 : CHECK_EQ(108, r.Call(4, 100));
998 12 : }
999 :
1000 25899 : WASM_EXEC_TEST(BrTable_loop_target) {
1001 : byte code[] = {
1002 : WASM_LOOP_I(
1003 : WASM_BLOCK(
1004 : WASM_BR_TABLE(WASM_GET_LOCAL(0), 2,
1005 : BR_TARGET(0), BR_TARGET(1), BR_TARGET(1))),
1006 12 : WASM_ONE)};
1007 :
1008 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1009 12 : r.Build(code, code + arraysize(code));
1010 :
1011 12 : CHECK_EQ(1, r.Call(0));
1012 12 : }
1013 :
1014 25899 : WASM_EXEC_TEST(F32ReinterpretI32) {
1015 12 : WasmRunner<int32_t> r(execution_tier);
1016 : int32_t* memory =
1017 : r.builder().AddMemoryElems<int32_t>(kWasmPageSize / sizeof(int32_t));
1018 :
1019 12 : BUILD(r, WASM_I32_REINTERPRET_F32(
1020 : WASM_LOAD_MEM(MachineType::Float32(), WASM_ZERO)));
1021 :
1022 708 : FOR_INT32_INPUTS(i) {
1023 : int32_t expected = i;
1024 : r.builder().WriteMemory(&memory[0], expected);
1025 696 : CHECK_EQ(expected, r.Call());
1026 12 : }
1027 12 : }
1028 :
1029 25899 : WASM_EXEC_TEST(I32ReinterpretF32) {
1030 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1031 696 : int32_t* memory =
1032 : r.builder().AddMemoryElems<int32_t>(kWasmPageSize / sizeof(int32_t));
1033 :
1034 12 : BUILD(r, WASM_STORE_MEM(MachineType::Float32(), WASM_ZERO,
1035 : WASM_F32_REINTERPRET_I32(WASM_GET_LOCAL(0))),
1036 : WASM_I32V_2(107));
1037 :
1038 708 : FOR_INT32_INPUTS(i) {
1039 : int32_t expected = i;
1040 696 : CHECK_EQ(107, r.Call(expected));
1041 696 : CHECK_EQ(expected, r.builder().ReadMemory(&memory[0]));
1042 12 : }
1043 12 : }
1044 :
1045 : // Do not run this test in a simulator because of signalling NaN issues on ia32.
1046 : #ifndef USE_SIMULATOR
1047 :
1048 25899 : WASM_EXEC_TEST(SignallingNanSurvivesI32ReinterpretF32) {
1049 12 : WasmRunner<int32_t> r(execution_tier);
1050 :
1051 12 : BUILD(r, WASM_I32_REINTERPRET_F32(
1052 : WASM_SEQ(kExprF32Const, 0x00, 0x00, 0xA0, 0x7F)));
1053 :
1054 : // This is a signalling nan.
1055 12 : CHECK_EQ(0x7FA00000, r.Call());
1056 12 : }
1057 :
1058 : #endif
1059 :
1060 25899 : WASM_EXEC_TEST(LoadMaxUint32Offset) {
1061 12 : WasmRunner<int32_t> r(execution_tier);
1062 12 : r.builder().AddMemory(kWasmPageSize);
1063 :
1064 12 : BUILD(r, WASM_LOAD_MEM_OFFSET(MachineType::Int32(), // type
1065 : U32V_5(0xFFFFFFFF), // offset
1066 : WASM_ZERO)); // index
1067 :
1068 24 : CHECK_TRAP32(r.Call());
1069 12 : }
1070 :
1071 25899 : WASM_EXEC_TEST(LoadStoreLoad) {
1072 12 : WasmRunner<int32_t> r(execution_tier);
1073 : int32_t* memory =
1074 : r.builder().AddMemoryElems<int32_t>(kWasmPageSize / sizeof(int32_t));
1075 :
1076 12 : BUILD(r, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO,
1077 : WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)),
1078 : WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO));
1079 :
1080 708 : FOR_INT32_INPUTS(i) {
1081 : int32_t expected = i;
1082 : r.builder().WriteMemory(&memory[0], expected);
1083 696 : CHECK_EQ(expected, r.Call());
1084 12 : }
1085 12 : }
1086 :
1087 25899 : WASM_EXEC_TEST(UnalignedFloat32Load) {
1088 12 : WasmRunner<float> r(execution_tier);
1089 12 : r.builder().AddMemory(kWasmPageSize);
1090 12 : BUILD(r, WASM_LOAD_MEM_ALIGNMENT(MachineType::Float32(), WASM_ONE, 2));
1091 12 : r.Call();
1092 12 : }
1093 :
1094 25899 : WASM_EXEC_TEST(UnalignedFloat64Load) {
1095 12 : WasmRunner<double> r(execution_tier);
1096 12 : r.builder().AddMemory(kWasmPageSize);
1097 12 : BUILD(r, WASM_LOAD_MEM_ALIGNMENT(MachineType::Float64(), WASM_ONE, 3));
1098 12 : r.Call();
1099 12 : }
1100 :
1101 25899 : WASM_EXEC_TEST(UnalignedInt32Load) {
1102 12 : WasmRunner<uint32_t> r(execution_tier);
1103 12 : r.builder().AddMemory(kWasmPageSize);
1104 12 : BUILD(r, WASM_LOAD_MEM_ALIGNMENT(MachineType::Int32(), WASM_ONE, 2));
1105 12 : r.Call();
1106 12 : }
1107 :
1108 25899 : WASM_EXEC_TEST(UnalignedInt32Store) {
1109 12 : WasmRunner<int32_t> r(execution_tier);
1110 12 : r.builder().AddMemory(kWasmPageSize);
1111 12 : BUILD(r, WASM_SEQ(WASM_STORE_MEM_ALIGNMENT(MachineType::Int32(), WASM_ONE, 2,
1112 : WASM_I32V_1(1)),
1113 : WASM_I32V_1(12)));
1114 12 : r.Call();
1115 12 : }
1116 :
1117 25899 : WASM_EXEC_TEST(UnalignedFloat32Store) {
1118 12 : WasmRunner<int32_t> r(execution_tier);
1119 12 : r.builder().AddMemory(kWasmPageSize);
1120 12 : BUILD(r, WASM_SEQ(WASM_STORE_MEM_ALIGNMENT(MachineType::Float32(), WASM_ONE,
1121 : 2, WASM_F32(1.0)),
1122 : WASM_I32V_1(12)));
1123 12 : r.Call();
1124 12 : }
1125 :
1126 25899 : WASM_EXEC_TEST(UnalignedFloat64Store) {
1127 12 : WasmRunner<int32_t> r(execution_tier);
1128 12 : r.builder().AddMemory(kWasmPageSize);
1129 12 : BUILD(r, WASM_SEQ(WASM_STORE_MEM_ALIGNMENT(MachineType::Float64(), WASM_ONE,
1130 : 3, WASM_F64(1.0)),
1131 : WASM_I32V_1(12)));
1132 12 : r.Call();
1133 12 : }
1134 :
1135 25899 : WASM_EXEC_TEST(VoidReturn1) {
1136 : const int32_t kExpected = -414444;
1137 12 : WasmRunner<int32_t> r(execution_tier);
1138 :
1139 : // Build the test function.
1140 24 : WasmFunctionCompiler& test_func = r.NewFunction<void>();
1141 12 : BUILD(test_func, kExprNop);
1142 :
1143 : // Build the calling function.
1144 24 : BUILD(r, WASM_CALL_FUNCTION0(test_func.function_index()),
1145 : WASM_I32V_3(kExpected));
1146 :
1147 : // Call and check.
1148 12 : int32_t result = r.Call();
1149 12 : CHECK_EQ(kExpected, result);
1150 12 : }
1151 :
1152 25899 : WASM_EXEC_TEST(VoidReturn2) {
1153 : const int32_t kExpected = -414444;
1154 12 : WasmRunner<int32_t> r(execution_tier);
1155 :
1156 : // Build the test function.
1157 24 : WasmFunctionCompiler& test_func = r.NewFunction<void>();
1158 12 : BUILD(test_func, WASM_RETURN0);
1159 :
1160 : // Build the calling function.
1161 24 : BUILD(r, WASM_CALL_FUNCTION0(test_func.function_index()),
1162 : WASM_I32V_3(kExpected));
1163 :
1164 : // Call and check.
1165 12 : int32_t result = r.Call();
1166 12 : CHECK_EQ(kExpected, result);
1167 12 : }
1168 :
1169 25899 : WASM_EXEC_TEST(BrEmpty) {
1170 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1171 12 : BUILD(r, WASM_BRV(0, WASM_GET_LOCAL(0)));
1172 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(i, r.Call(i)); }
1173 12 : }
1174 :
1175 25899 : WASM_EXEC_TEST(BrIfEmpty) {
1176 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1177 12 : BUILD(r, WASM_BRV_IF(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
1178 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(i, r.Call(i)); }
1179 12 : }
1180 :
1181 25899 : WASM_EXEC_TEST(Block_empty) {
1182 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1183 12 : BUILD(r, kExprBlock, kLocalVoid, kExprEnd, WASM_GET_LOCAL(0));
1184 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(i, r.Call(i)); }
1185 12 : }
1186 :
1187 25899 : WASM_EXEC_TEST(Block_empty_br1) {
1188 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1189 12 : BUILD(r, B1(WASM_BR(0)), WASM_GET_LOCAL(0));
1190 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(i, r.Call(i)); }
1191 12 : }
1192 :
1193 25899 : WASM_EXEC_TEST(Block_empty_brif1) {
1194 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1195 12 : BUILD(r, WASM_BLOCK(WASM_BR_IF(0, WASM_ZERO)), WASM_GET_LOCAL(0));
1196 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(i, r.Call(i)); }
1197 12 : }
1198 :
1199 25899 : WASM_EXEC_TEST(Block_empty_brif2) {
1200 12 : WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_tier);
1201 12 : BUILD(r, WASM_BLOCK(WASM_BR_IF(0, WASM_GET_LOCAL(1))), WASM_GET_LOCAL(0));
1202 12 : FOR_UINT32_INPUTS(i) { CHECK_EQ(i, r.Call(i, i + 1)); }
1203 12 : }
1204 :
1205 25899 : WASM_EXEC_TEST(Block_i) {
1206 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1207 12 : BUILD(r, WASM_BLOCK_I(WASM_GET_LOCAL(0)));
1208 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(i, r.Call(i)); }
1209 12 : }
1210 :
1211 25899 : WASM_EXEC_TEST(Block_f) {
1212 12 : WasmRunner<float, float> r(execution_tier);
1213 12 : BUILD(r, WASM_BLOCK_F(WASM_GET_LOCAL(0)));
1214 1392 : FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(i, r.Call(i)); }
1215 12 : }
1216 :
1217 25899 : WASM_EXEC_TEST(Block_d) {
1218 12 : WasmRunner<double, double> r(execution_tier);
1219 12 : BUILD(r, WASM_BLOCK_D(WASM_GET_LOCAL(0)));
1220 600 : FOR_FLOAT64_INPUTS(i) { CHECK_DOUBLE_EQ(i, r.Call(i)); }
1221 12 : }
1222 :
1223 25899 : WASM_EXEC_TEST(Block_br2) {
1224 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1225 12 : BUILD(r, WASM_BLOCK_I(WASM_BRV(0, WASM_GET_LOCAL(0))));
1226 12 : FOR_UINT32_INPUTS(i) { CHECK_EQ(i, static_cast<uint32_t>(r.Call(i))); }
1227 12 : }
1228 :
1229 25899 : WASM_EXEC_TEST(Block_If_P) {
1230 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1231 : // block { if (p0) break 51; 52; }
1232 12 : BUILD(r, WASM_BLOCK_I( // --
1233 : WASM_IF(WASM_GET_LOCAL(0), // --
1234 : WASM_BRV(1, WASM_I32V_1(51))), // --
1235 : WASM_I32V_1(52))); // --
1236 708 : FOR_INT32_INPUTS(i) {
1237 696 : int32_t expected = i ? 51 : 52;
1238 696 : CHECK_EQ(expected, r.Call(i));
1239 12 : }
1240 12 : }
1241 :
1242 25899 : WASM_EXEC_TEST(Loop_empty) {
1243 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1244 12 : BUILD(r, kExprLoop, kLocalVoid, kExprEnd, WASM_GET_LOCAL(0));
1245 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(i, r.Call(i)); }
1246 12 : }
1247 :
1248 25899 : WASM_EXEC_TEST(Loop_i) {
1249 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1250 12 : BUILD(r, WASM_LOOP_I(WASM_GET_LOCAL(0)));
1251 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(i, r.Call(i)); }
1252 12 : }
1253 :
1254 25899 : WASM_EXEC_TEST(Loop_f) {
1255 12 : WasmRunner<float, float> r(execution_tier);
1256 12 : BUILD(r, WASM_LOOP_F(WASM_GET_LOCAL(0)));
1257 1392 : FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(i, r.Call(i)); }
1258 12 : }
1259 :
1260 25899 : WASM_EXEC_TEST(Loop_d) {
1261 12 : WasmRunner<double, double> r(execution_tier);
1262 12 : BUILD(r, WASM_LOOP_D(WASM_GET_LOCAL(0)));
1263 600 : FOR_FLOAT64_INPUTS(i) { CHECK_DOUBLE_EQ(i, r.Call(i)); }
1264 12 : }
1265 :
1266 25899 : WASM_EXEC_TEST(Loop_empty_br1) {
1267 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1268 12 : BUILD(r, B1(WASM_LOOP(WASM_BR(1))), WASM_GET_LOCAL(0));
1269 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(i, r.Call(i)); }
1270 12 : }
1271 :
1272 25899 : WASM_EXEC_TEST(Loop_empty_brif1) {
1273 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1274 12 : BUILD(r, B1(WASM_LOOP(WASM_BR_IF(1, WASM_ZERO))), WASM_GET_LOCAL(0));
1275 12 : FOR_INT32_INPUTS(i) { CHECK_EQ(i, r.Call(i)); }
1276 12 : }
1277 :
1278 25899 : WASM_EXEC_TEST(Loop_empty_brif2) {
1279 12 : WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_tier);
1280 12 : BUILD(r, WASM_LOOP_I(WASM_BRV_IF(1, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))));
1281 12 : FOR_UINT32_INPUTS(i) { CHECK_EQ(i, r.Call(i, i + 1)); }
1282 12 : }
1283 :
1284 25899 : WASM_EXEC_TEST(Loop_empty_brif3) {
1285 12 : WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
1286 12 : BUILD(r, WASM_LOOP(WASM_BRV_IFD(1, WASM_GET_LOCAL(2), WASM_GET_LOCAL(0))),
1287 : WASM_GET_LOCAL(1));
1288 708 : FOR_UINT32_INPUTS(i) {
1289 41064 : FOR_UINT32_INPUTS(j) {
1290 40368 : CHECK_EQ(i, r.Call(0, i, j));
1291 40368 : CHECK_EQ(j, r.Call(1, i, j));
1292 : }
1293 12 : }
1294 12 : }
1295 :
1296 25899 : WASM_EXEC_TEST(Block_BrIf_P) {
1297 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1298 12 : BUILD(r, WASM_BLOCK_I(WASM_BRV_IFD(0, WASM_I32V_1(51), WASM_GET_LOCAL(0)),
1299 : WASM_I32V_1(52)));
1300 708 : FOR_INT32_INPUTS(i) {
1301 696 : int32_t expected = i ? 51 : 52;
1302 696 : CHECK_EQ(expected, r.Call(i));
1303 12 : }
1304 12 : }
1305 :
1306 25899 : WASM_EXEC_TEST(Block_IfElse_P_assign) {
1307 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1308 : // { if (p0) p0 = 71; else p0 = 72; return p0; }
1309 12 : BUILD(r, // --
1310 : WASM_IF_ELSE(WASM_GET_LOCAL(0), // --
1311 : WASM_SET_LOCAL(0, WASM_I32V_2(71)), // --
1312 : WASM_SET_LOCAL(0, WASM_I32V_2(72))), // --
1313 : WASM_GET_LOCAL(0));
1314 708 : FOR_INT32_INPUTS(i) {
1315 696 : int32_t expected = i ? 71 : 72;
1316 696 : CHECK_EQ(expected, r.Call(i));
1317 12 : }
1318 12 : }
1319 :
1320 25899 : WASM_EXEC_TEST(Block_IfElse_P_return) {
1321 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1322 : // if (p0) return 81; else return 82;
1323 12 : BUILD(r, // --
1324 : WASM_IF_ELSE(WASM_GET_LOCAL(0), // --
1325 : RET_I8(81), // --
1326 : RET_I8(82)), // --
1327 : WASM_ZERO); // --
1328 708 : FOR_INT32_INPUTS(i) {
1329 696 : int32_t expected = i ? 81 : 82;
1330 696 : CHECK_EQ(expected, r.Call(i));
1331 12 : }
1332 12 : }
1333 :
1334 25899 : WASM_EXEC_TEST(Block_If_P_assign) {
1335 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1336 : // { if (p0) p0 = 61; p0; }
1337 12 : BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(0, WASM_I32V_1(61))),
1338 : WASM_GET_LOCAL(0));
1339 708 : FOR_INT32_INPUTS(i) {
1340 696 : int32_t expected = i ? 61 : i;
1341 696 : CHECK_EQ(expected, r.Call(i));
1342 12 : }
1343 12 : }
1344 :
1345 25899 : WASM_EXEC_TEST(DanglingAssign) {
1346 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1347 : // { return 0; p0 = 0; }
1348 12 : BUILD(r, WASM_BLOCK_I(RET_I8(99), WASM_TEE_LOCAL(0, WASM_ZERO)));
1349 12 : CHECK_EQ(99, r.Call(1));
1350 12 : }
1351 :
1352 25899 : WASM_EXEC_TEST(ExprIf_P) {
1353 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1354 : // p0 ? 11 : 22;
1355 12 : BUILD(r, WASM_IF_ELSE_I(WASM_GET_LOCAL(0), // --
1356 : WASM_I32V_1(11), // --
1357 : WASM_I32V_1(22))); // --
1358 708 : FOR_INT32_INPUTS(i) {
1359 696 : int32_t expected = i ? 11 : 22;
1360 696 : CHECK_EQ(expected, r.Call(i));
1361 12 : }
1362 12 : }
1363 :
1364 25899 : WASM_EXEC_TEST(CountDown) {
1365 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1366 12 : BUILD(r, WASM_LOOP(WASM_IFB(WASM_GET_LOCAL(0),
1367 : WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0),
1368 : WASM_I32V_1(1))),
1369 : WASM_BR(1))),
1370 : WASM_GET_LOCAL(0));
1371 12 : CHECK_EQ(0, r.Call(1));
1372 12 : CHECK_EQ(0, r.Call(10));
1373 12 : CHECK_EQ(0, r.Call(100));
1374 12 : }
1375 :
1376 25899 : WASM_EXEC_TEST(CountDown_fallthru) {
1377 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1378 12 : BUILD(
1379 : r,
1380 : WASM_LOOP(
1381 : WASM_IF(WASM_NOT(WASM_GET_LOCAL(0)), WASM_BRV(2, WASM_GET_LOCAL(0))),
1382 : WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I32V_1(1))),
1383 : WASM_CONTINUE(0)),
1384 : WASM_GET_LOCAL(0));
1385 12 : CHECK_EQ(0, r.Call(1));
1386 12 : CHECK_EQ(0, r.Call(10));
1387 12 : CHECK_EQ(0, r.Call(100));
1388 12 : }
1389 :
1390 25899 : WASM_EXEC_TEST(WhileCountDown) {
1391 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1392 12 : BUILD(r, WASM_WHILE(WASM_GET_LOCAL(0),
1393 : WASM_SET_LOCAL(
1394 : 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I32V_1(1)))),
1395 : WASM_GET_LOCAL(0));
1396 12 : CHECK_EQ(0, r.Call(1));
1397 12 : CHECK_EQ(0, r.Call(10));
1398 12 : CHECK_EQ(0, r.Call(100));
1399 12 : }
1400 :
1401 25899 : WASM_EXEC_TEST(Loop_if_break1) {
1402 12 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
1403 12 : BUILD(r, WASM_LOOP(WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(2, WASM_GET_LOCAL(1))),
1404 : WASM_SET_LOCAL(0, WASM_I32V_2(99))),
1405 : WASM_GET_LOCAL(0));
1406 12 : CHECK_EQ(99, r.Call(0, 11));
1407 12 : CHECK_EQ(65, r.Call(3, 65));
1408 12 : CHECK_EQ(10001, r.Call(10000, 10001));
1409 12 : CHECK_EQ(-29, r.Call(-28, -29));
1410 12 : }
1411 :
1412 25899 : WASM_EXEC_TEST(Loop_if_break2) {
1413 12 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
1414 12 : BUILD(r, WASM_LOOP(WASM_BRV_IF(1, WASM_GET_LOCAL(1), WASM_GET_LOCAL(0)),
1415 : WASM_DROP, WASM_SET_LOCAL(0, WASM_I32V_2(99))),
1416 : WASM_GET_LOCAL(0));
1417 12 : CHECK_EQ(99, r.Call(0, 33));
1418 12 : CHECK_EQ(3, r.Call(1, 3));
1419 12 : CHECK_EQ(10000, r.Call(99, 10000));
1420 12 : CHECK_EQ(-29, r.Call(-11, -29));
1421 12 : }
1422 :
1423 25899 : WASM_EXEC_TEST(Loop_if_break_fallthru) {
1424 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1425 12 : BUILD(r, B1(WASM_LOOP(WASM_IF(WASM_GET_LOCAL(0), WASM_BR(2)),
1426 : WASM_SET_LOCAL(0, WASM_I32V_2(93)))),
1427 : WASM_GET_LOCAL(0));
1428 12 : CHECK_EQ(93, r.Call(0));
1429 12 : CHECK_EQ(3, r.Call(3));
1430 12 : CHECK_EQ(10001, r.Call(10001));
1431 12 : CHECK_EQ(-22, r.Call(-22));
1432 12 : }
1433 :
1434 25899 : WASM_EXEC_TEST(Loop_if_break_fallthru2) {
1435 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1436 12 : BUILD(r, B1(B1(WASM_LOOP(WASM_IF(WASM_GET_LOCAL(0), WASM_BR(2)),
1437 : WASM_SET_LOCAL(0, WASM_I32V_2(93))))),
1438 : WASM_GET_LOCAL(0));
1439 12 : CHECK_EQ(93, r.Call(0));
1440 12 : CHECK_EQ(3, r.Call(3));
1441 12 : CHECK_EQ(10001, r.Call(10001));
1442 12 : CHECK_EQ(-22, r.Call(-22));
1443 12 : }
1444 :
1445 25899 : WASM_EXEC_TEST(IfBreak1) {
1446 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1447 12 : BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SEQ(WASM_BR(0), WASM_UNREACHABLE)),
1448 : WASM_I32V_2(91));
1449 12 : CHECK_EQ(91, r.Call(0));
1450 12 : CHECK_EQ(91, r.Call(1));
1451 12 : CHECK_EQ(91, r.Call(-8734));
1452 12 : }
1453 :
1454 25899 : WASM_EXEC_TEST(IfBreak2) {
1455 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1456 12 : BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SEQ(WASM_BR(0), RET_I8(77))),
1457 : WASM_I32V_2(81));
1458 12 : CHECK_EQ(81, r.Call(0));
1459 12 : CHECK_EQ(81, r.Call(1));
1460 12 : CHECK_EQ(81, r.Call(-8734));
1461 12 : }
1462 :
1463 25899 : WASM_EXEC_TEST(LoadMemI32) {
1464 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1465 : int32_t* memory =
1466 : r.builder().AddMemoryElems<int32_t>(kWasmPageSize / sizeof(int32_t));
1467 12 : r.builder().RandomizeMemory(1111);
1468 :
1469 12 : BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO));
1470 :
1471 : r.builder().WriteMemory(&memory[0], 99999999);
1472 12 : CHECK_EQ(99999999, r.Call(0));
1473 :
1474 : r.builder().WriteMemory(&memory[0], 88888888);
1475 12 : CHECK_EQ(88888888, r.Call(0));
1476 :
1477 : r.builder().WriteMemory(&memory[0], 77777777);
1478 12 : CHECK_EQ(77777777, r.Call(0));
1479 12 : }
1480 :
1481 25899 : WASM_EXEC_TEST(LoadMemI32_alignment) {
1482 48 : for (byte alignment = 0; alignment <= 2; ++alignment) {
1483 36 : WasmRunner<int32_t, int32_t> r(execution_tier);
1484 : int32_t* memory =
1485 : r.builder().AddMemoryElems<int32_t>(kWasmPageSize / sizeof(int32_t));
1486 36 : r.builder().RandomizeMemory(1111);
1487 :
1488 36 : BUILD(r,
1489 : WASM_LOAD_MEM_ALIGNMENT(MachineType::Int32(), WASM_ZERO, alignment));
1490 :
1491 : r.builder().WriteMemory(&memory[0], 0x1A2B3C4D);
1492 36 : CHECK_EQ(0x1A2B3C4D, r.Call(0));
1493 :
1494 : r.builder().WriteMemory(&memory[0], 0x5E6F7A8B);
1495 36 : CHECK_EQ(0x5E6F7A8B, r.Call(0));
1496 :
1497 : r.builder().WriteMemory(&memory[0], 0x7CA0B1C2);
1498 36 : CHECK_EQ(0x7CA0B1C2, r.Call(0));
1499 36 : }
1500 12 : }
1501 :
1502 25899 : WASM_EXEC_TEST(LoadMemI32_oob) {
1503 12 : WasmRunner<int32_t, uint32_t> r(execution_tier);
1504 : int32_t* memory =
1505 : r.builder().AddMemoryElems<int32_t>(kWasmPageSize / sizeof(int32_t));
1506 12 : r.builder().RandomizeMemory(1111);
1507 :
1508 12 : BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0)));
1509 :
1510 : r.builder().WriteMemory(&memory[0], 88888888);
1511 12 : CHECK_EQ(88888888, r.Call(0u));
1512 516 : for (uint32_t offset = kWasmPageSize - 3; offset < kWasmPageSize + 40;
1513 : ++offset) {
1514 1032 : CHECK_TRAP(r.Call(offset));
1515 : }
1516 :
1517 192 : for (uint32_t offset = 0x80000000; offset < 0x80000010; ++offset) {
1518 384 : CHECK_TRAP(r.Call(offset));
1519 12 : }
1520 12 : }
1521 :
1522 25899 : WASM_EXEC_TEST(LoadMem_offset_oob) {
1523 : static const MachineType machineTypes[] = {
1524 : MachineType::Int8(), MachineType::Uint8(), MachineType::Int16(),
1525 : MachineType::Uint16(), MachineType::Int32(), MachineType::Uint32(),
1526 : MachineType::Int64(), MachineType::Uint64(), MachineType::Float32(),
1527 : MachineType::Float64()};
1528 :
1529 : constexpr size_t num_bytes = kWasmPageSize;
1530 :
1531 132 : for (size_t m = 0; m < arraysize(machineTypes); ++m) {
1532 120 : WasmRunner<int32_t, uint32_t> r(execution_tier);
1533 : r.builder().AddMemoryElems<byte>(num_bytes);
1534 120 : r.builder().RandomizeMemory(1116 + static_cast<int>(m));
1535 :
1536 : constexpr byte offset = 8;
1537 : uint32_t boundary =
1538 240 : num_bytes - offset - ValueTypes::MemSize(machineTypes[m]);
1539 :
1540 120 : BUILD(r, WASM_LOAD_MEM_OFFSET(machineTypes[m], offset, WASM_GET_LOCAL(0)),
1541 : WASM_DROP, WASM_ZERO);
1542 :
1543 120 : CHECK_EQ(0, r.Call(boundary)); // in bounds.
1544 :
1545 2280 : for (uint32_t offset = boundary + 1; offset < boundary + 19; ++offset) {
1546 4320 : CHECK_TRAP(r.Call(offset)); // out of bounds.
1547 : }
1548 120 : }
1549 12 : }
1550 :
1551 25899 : WASM_EXEC_TEST(LoadMemI32_offset) {
1552 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1553 : int32_t* memory =
1554 : r.builder().AddMemoryElems<int32_t>(kWasmPageSize / sizeof(int32_t));
1555 12 : r.builder().RandomizeMemory(1111);
1556 :
1557 12 : BUILD(r, WASM_LOAD_MEM_OFFSET(MachineType::Int32(), 4, WASM_GET_LOCAL(0)));
1558 :
1559 : r.builder().WriteMemory(&memory[0], 66666666);
1560 12 : r.builder().WriteMemory(&memory[1], 77777777);
1561 12 : r.builder().WriteMemory(&memory[2], 88888888);
1562 12 : r.builder().WriteMemory(&memory[3], 99999999);
1563 12 : CHECK_EQ(77777777, r.Call(0));
1564 12 : CHECK_EQ(88888888, r.Call(4));
1565 12 : CHECK_EQ(99999999, r.Call(8));
1566 :
1567 : r.builder().WriteMemory(&memory[0], 11111111);
1568 : r.builder().WriteMemory(&memory[1], 22222222);
1569 : r.builder().WriteMemory(&memory[2], 33333333);
1570 : r.builder().WriteMemory(&memory[3], 44444444);
1571 12 : CHECK_EQ(22222222, r.Call(0));
1572 12 : CHECK_EQ(33333333, r.Call(4));
1573 12 : CHECK_EQ(44444444, r.Call(8));
1574 12 : }
1575 :
1576 25899 : WASM_EXEC_TEST(LoadMemI32_const_oob_misaligned) {
1577 : // This test accesses memory starting at kRunwayLength bytes before the end of
1578 : // the memory until a few bytes beyond.
1579 : constexpr byte kRunwayLength = 12;
1580 : // TODO(titzer): Fix misaligned accesses on MIPS and re-enable.
1581 216 : for (byte offset = 0; offset < kRunwayLength + 5; ++offset) {
1582 3468 : for (uint32_t index = kWasmPageSize - kRunwayLength;
1583 : index < kWasmPageSize + 5; ++index) {
1584 3468 : WasmRunner<int32_t> r(execution_tier);
1585 : r.builder().AddMemoryElems<byte>(kWasmPageSize);
1586 3468 : r.builder().RandomizeMemory();
1587 :
1588 3468 : BUILD(r, WASM_LOAD_MEM_OFFSET(MachineType::Int32(), offset,
1589 : WASM_I32V_3(index)));
1590 :
1591 3468 : if (offset + index + sizeof(int32_t) <= kWasmPageSize) {
1592 1080 : CHECK_EQ(r.builder().raw_val_at<int32_t>(offset + index), r.Call());
1593 : } else {
1594 5856 : CHECK_TRAP(r.Call());
1595 : }
1596 3468 : }
1597 : }
1598 12 : }
1599 :
1600 25899 : WASM_EXEC_TEST(LoadMemI32_const_oob) {
1601 : // This test accesses memory starting at kRunwayLength bytes before the end of
1602 : // the memory until a few bytes beyond.
1603 : constexpr byte kRunwayLength = 24;
1604 108 : for (byte offset = 0; offset < kRunwayLength + 5; offset += 4) {
1605 768 : for (uint32_t index = kWasmPageSize - kRunwayLength;
1606 : index < kWasmPageSize + 5; index += 4) {
1607 768 : WasmRunner<int32_t> r(execution_tier);
1608 : r.builder().AddMemoryElems<byte>(kWasmPageSize);
1609 768 : r.builder().RandomizeMemory();
1610 :
1611 768 : BUILD(r, WASM_LOAD_MEM_OFFSET(MachineType::Int32(), offset,
1612 : WASM_I32V_3(index)));
1613 :
1614 768 : if (offset + index + sizeof(int32_t) <= kWasmPageSize) {
1615 504 : CHECK_EQ(r.builder().raw_val_at<int32_t>(offset + index), r.Call());
1616 : } else {
1617 1032 : CHECK_TRAP(r.Call());
1618 : }
1619 768 : }
1620 : }
1621 12 : }
1622 :
1623 25899 : WASM_EXEC_TEST(StoreMemI32_alignment) {
1624 : const int32_t kWritten = 0x12345678;
1625 :
1626 48 : for (byte i = 0; i <= 2; ++i) {
1627 36 : WasmRunner<int32_t, int32_t> r(execution_tier);
1628 36 : int32_t* memory =
1629 : r.builder().AddMemoryElems<int32_t>(kWasmPageSize / sizeof(int32_t));
1630 36 : BUILD(r, WASM_STORE_MEM_ALIGNMENT(MachineType::Int32(), WASM_ZERO, i,
1631 : WASM_GET_LOCAL(0)),
1632 : WASM_GET_LOCAL(0));
1633 36 : r.builder().RandomizeMemory(1111);
1634 36 : memory[0] = 0;
1635 :
1636 36 : CHECK_EQ(kWritten, r.Call(kWritten));
1637 36 : CHECK_EQ(kWritten, r.builder().ReadMemory(&memory[0]));
1638 36 : }
1639 12 : }
1640 :
1641 25899 : WASM_EXEC_TEST(StoreMemI32_offset) {
1642 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1643 24 : int32_t* memory =
1644 : r.builder().AddMemoryElems<int32_t>(kWasmPageSize / sizeof(int32_t));
1645 : const int32_t kWritten = 0xAABBCCDD;
1646 :
1647 12 : BUILD(r, WASM_STORE_MEM_OFFSET(MachineType::Int32(), 4, WASM_GET_LOCAL(0),
1648 : WASM_I32V_5(kWritten)),
1649 : WASM_I32V_5(kWritten));
1650 :
1651 36 : for (int i = 0; i < 2; ++i) {
1652 24 : r.builder().RandomizeMemory(1111);
1653 : r.builder().WriteMemory(&memory[0], 66666666);
1654 48 : r.builder().WriteMemory(&memory[1], 77777777);
1655 48 : r.builder().WriteMemory(&memory[2], 88888888);
1656 48 : r.builder().WriteMemory(&memory[3], 99999999);
1657 24 : CHECK_EQ(kWritten, r.Call(i * 4));
1658 24 : CHECK_EQ(66666666, r.builder().ReadMemory(&memory[0]));
1659 24 : CHECK_EQ(i == 0 ? kWritten : 77777777, r.builder().ReadMemory(&memory[1]));
1660 24 : CHECK_EQ(i == 1 ? kWritten : 88888888, r.builder().ReadMemory(&memory[2]));
1661 24 : CHECK_EQ(i == 2 ? kWritten : 99999999, r.builder().ReadMemory(&memory[3]));
1662 12 : }
1663 12 : }
1664 :
1665 25899 : WASM_EXEC_TEST(StoreMem_offset_oob) {
1666 : // 64-bit cases are handled in test-run-wasm-64.cc
1667 : static const MachineType machineTypes[] = {
1668 : MachineType::Int8(), MachineType::Uint8(), MachineType::Int16(),
1669 : MachineType::Uint16(), MachineType::Int32(), MachineType::Uint32(),
1670 : MachineType::Float32(), MachineType::Float64()};
1671 :
1672 : constexpr size_t num_bytes = kWasmPageSize;
1673 :
1674 108 : for (size_t m = 0; m < arraysize(machineTypes); ++m) {
1675 96 : WasmRunner<int32_t, uint32_t> r(execution_tier);
1676 : byte* memory = r.builder().AddMemoryElems<byte>(num_bytes);
1677 :
1678 96 : r.builder().RandomizeMemory(1119 + static_cast<int>(m));
1679 :
1680 96 : BUILD(r, WASM_STORE_MEM_OFFSET(machineTypes[m], 8, WASM_GET_LOCAL(0),
1681 : WASM_LOAD_MEM(machineTypes[m], WASM_ZERO)),
1682 : WASM_ZERO);
1683 :
1684 96 : byte memsize = ValueTypes::MemSize(machineTypes[m]);
1685 96 : uint32_t boundary = num_bytes - 8 - memsize;
1686 96 : CHECK_EQ(0, r.Call(boundary)); // in bounds.
1687 96 : CHECK_EQ(0, memcmp(&memory[0], &memory[8 + boundary], memsize));
1688 :
1689 1824 : for (uint32_t offset = boundary + 1; offset < boundary + 19; ++offset) {
1690 3456 : CHECK_TRAP(r.Call(offset)); // out of bounds.
1691 : }
1692 96 : }
1693 12 : }
1694 :
1695 25899 : WASM_EXEC_TEST(Store_i32_narrowed) {
1696 : constexpr byte kOpcodes[] = {kExprI32StoreMem8, kExprI32StoreMem16,
1697 12 : kExprI32StoreMem};
1698 : int stored_size_in_bytes = 0;
1699 48 : for (auto opcode : kOpcodes) {
1700 72 : stored_size_in_bytes = std::max(1, stored_size_in_bytes * 2);
1701 : constexpr int kBytes = 24;
1702 36 : uint8_t expected_memory[kBytes] = {0};
1703 36 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
1704 : uint8_t* memory = r.builder().AddMemoryElems<uint8_t>(kWasmPageSize);
1705 : constexpr uint32_t kPattern = 0x12345678;
1706 :
1707 36 : BUILD(r, WASM_GET_LOCAL(0), // index
1708 : WASM_GET_LOCAL(1), // value
1709 : opcode, ZERO_ALIGNMENT, ZERO_OFFSET, // store
1710 : WASM_ZERO); // return value
1711 :
1712 852 : for (int i = 0; i <= kBytes - stored_size_in_bytes; ++i) {
1713 816 : uint32_t pattern = base::bits::RotateLeft32(kPattern, i % 32);
1714 816 : r.Call(i, pattern);
1715 2664 : for (int b = 0; b < stored_size_in_bytes; ++b) {
1716 1848 : expected_memory[i + b] = static_cast<uint8_t>(pattern >> (b * 8));
1717 : }
1718 19584 : for (int w = 0; w < kBytes; ++w) {
1719 19584 : CHECK_EQ(expected_memory[w], memory[w]);
1720 : }
1721 : }
1722 36 : }
1723 12 : }
1724 :
1725 25899 : WASM_EXEC_TEST(LoadMemI32_P) {
1726 : const int kNumElems = 8;
1727 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1728 : int32_t* memory =
1729 : r.builder().AddMemoryElems<int32_t>(kWasmPageSize / sizeof(int32_t));
1730 12 : r.builder().RandomizeMemory(2222);
1731 :
1732 12 : BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0)));
1733 :
1734 108 : for (int i = 0; i < kNumElems; ++i) {
1735 96 : CHECK_EQ(r.builder().ReadMemory(&memory[i]), r.Call(i * 4));
1736 12 : }
1737 12 : }
1738 :
1739 25899 : WASM_EXEC_TEST(MemI32_Sum) {
1740 : const int kNumElems = 20;
1741 12 : WasmRunner<uint32_t, int32_t> r(execution_tier);
1742 : uint32_t* memory =
1743 : r.builder().AddMemoryElems<uint32_t>(kWasmPageSize / sizeof(int32_t));
1744 : const byte kSum = r.AllocateLocal(kWasmI32);
1745 :
1746 12 : BUILD(r, WASM_WHILE(
1747 : WASM_GET_LOCAL(0),
1748 : WASM_BLOCK(
1749 : WASM_SET_LOCAL(
1750 : kSum, WASM_I32_ADD(WASM_GET_LOCAL(kSum),
1751 : WASM_LOAD_MEM(MachineType::Int32(),
1752 : WASM_GET_LOCAL(0)))),
1753 : WASM_SET_LOCAL(
1754 : 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I32V_1(4))))),
1755 : WASM_GET_LOCAL(1));
1756 :
1757 : // Run 4 trials.
1758 48 : for (int i = 0; i < 3; ++i) {
1759 36 : r.builder().RandomizeMemory(i * 33);
1760 : uint32_t expected = 0;
1761 720 : for (size_t j = kNumElems - 1; j > 0; --j) {
1762 684 : expected += r.builder().ReadMemory(&memory[j]);
1763 : }
1764 36 : uint32_t result = r.Call(4 * (kNumElems - 1));
1765 36 : CHECK_EQ(expected, result);
1766 12 : }
1767 12 : }
1768 :
1769 25899 : WASM_EXEC_TEST(CheckMachIntsZero) {
1770 : const int kNumElems = 55;
1771 12 : WasmRunner<uint32_t, int32_t> r(execution_tier);
1772 : r.builder().AddMemoryElems<uint32_t>(kWasmPageSize / sizeof(uint32_t));
1773 :
1774 12 : BUILD(r, // --
1775 : /**/ kExprLoop, kLocalVoid, // --
1776 : /* */ kExprGetLocal, 0, // --
1777 : /* */ kExprIf, kLocalVoid, // --
1778 : /* */ kExprGetLocal, 0, // --
1779 : /* */ kExprI32LoadMem, 0, 0, // --
1780 : /* */ kExprIf, kLocalVoid, // --
1781 : /* */ kExprI32Const, 127, // --
1782 : /* */ kExprReturn, // --
1783 : /* */ kExprEnd, // --
1784 : /* */ kExprGetLocal, 0, // --
1785 : /* */ kExprI32Const, 4, // --
1786 : /* */ kExprI32Sub, // --
1787 : /* */ kExprTeeLocal, 0, // --
1788 : /* */ kExprBr, DEPTH_0, // --
1789 : /* */ kExprEnd, // --
1790 : /**/ kExprEnd, // --
1791 : /**/ kExprI32Const, 0); // --
1792 :
1793 : r.builder().BlankMemory();
1794 12 : CHECK_EQ(0, r.Call((kNumElems - 1) * 4));
1795 12 : }
1796 :
1797 25899 : WASM_EXEC_TEST(MemF32_Sum) {
1798 : const int kSize = 5;
1799 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1800 : r.builder().AddMemoryElems<float>(kWasmPageSize / sizeof(float));
1801 24 : float* buffer = r.builder().raw_mem_start<float>();
1802 : r.builder().WriteMemory(&buffer[0], -99.25f);
1803 12 : r.builder().WriteMemory(&buffer[1], -888.25f);
1804 12 : r.builder().WriteMemory(&buffer[2], -77.25f);
1805 12 : r.builder().WriteMemory(&buffer[3], 66666.25f);
1806 12 : r.builder().WriteMemory(&buffer[4], 5555.25f);
1807 : const byte kSum = r.AllocateLocal(kWasmF32);
1808 :
1809 12 : BUILD(r, WASM_WHILE(
1810 : WASM_GET_LOCAL(0),
1811 : WASM_BLOCK(
1812 : WASM_SET_LOCAL(
1813 : kSum, WASM_F32_ADD(WASM_GET_LOCAL(kSum),
1814 : WASM_LOAD_MEM(MachineType::Float32(),
1815 : WASM_GET_LOCAL(0)))),
1816 : WASM_SET_LOCAL(
1817 : 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I32V_1(4))))),
1818 : WASM_STORE_MEM(MachineType::Float32(), WASM_ZERO, WASM_GET_LOCAL(kSum)),
1819 : WASM_GET_LOCAL(0));
1820 :
1821 12 : CHECK_EQ(0, r.Call(4 * (kSize - 1)));
1822 12 : CHECK_NE(-99.25f, r.builder().ReadMemory(&buffer[0]));
1823 12 : CHECK_EQ(71256.0f, r.builder().ReadMemory(&buffer[0]));
1824 12 : }
1825 :
1826 : template <typename T>
1827 12 : T GenerateAndRunFold(ExecutionTier execution_tier, WasmOpcode binop, T* buffer,
1828 : uint32_t size, ValueType astType, MachineType memType) {
1829 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1830 12 : T* memory = r.builder().AddMemoryElems<T>(static_cast<uint32_t>(
1831 24 : RoundUp(size * sizeof(T), kWasmPageSize) / sizeof(sizeof(T))));
1832 84 : for (uint32_t i = 0; i < size; ++i) {
1833 72 : r.builder().WriteMemory(&memory[i], buffer[i]);
1834 : }
1835 : const byte kAccum = r.AllocateLocal(astType);
1836 :
1837 12 : BUILD(
1838 : r, WASM_SET_LOCAL(kAccum, WASM_LOAD_MEM(memType, WASM_ZERO)),
1839 : WASM_WHILE(
1840 : WASM_GET_LOCAL(0),
1841 : WASM_BLOCK(WASM_SET_LOCAL(
1842 : kAccum,
1843 : WASM_BINOP(binop, WASM_GET_LOCAL(kAccum),
1844 : WASM_LOAD_MEM(memType, WASM_GET_LOCAL(0)))),
1845 : WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0),
1846 : WASM_I32V_1(sizeof(T)))))),
1847 : WASM_STORE_MEM(memType, WASM_ZERO, WASM_GET_LOCAL(kAccum)),
1848 : WASM_GET_LOCAL(0));
1849 12 : r.Call(static_cast<int>(sizeof(T) * (size - 1)));
1850 12 : return r.builder().ReadMemory(&memory[0]);
1851 : }
1852 :
1853 25899 : WASM_EXEC_TEST(MemF64_Mul) {
1854 : const size_t kSize = 6;
1855 12 : double buffer[kSize] = {1, 2, 2, 2, 2, 2};
1856 : double result =
1857 : GenerateAndRunFold<double>(execution_tier, kExprF64Mul, buffer, kSize,
1858 12 : kWasmF64, MachineType::Float64());
1859 12 : CHECK_EQ(32, result);
1860 12 : }
1861 :
1862 25899 : WASM_EXEC_TEST(Build_Wasm_Infinite_Loop) {
1863 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1864 : // Only build the graph and compile, don't run.
1865 12 : BUILD(r, WASM_INFINITE_LOOP, WASM_ZERO);
1866 12 : }
1867 :
1868 25899 : WASM_EXEC_TEST(Build_Wasm_Infinite_Loop_effect) {
1869 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1870 12 : r.builder().AddMemory(kWasmPageSize);
1871 :
1872 : // Only build the graph and compile, don't run.
1873 12 : BUILD(r, WASM_LOOP(WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO), WASM_DROP),
1874 12 : WASM_ZERO);
1875 12 : }
1876 :
1877 25899 : WASM_EXEC_TEST(Unreachable0a) {
1878 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1879 12 : BUILD(r, WASM_BLOCK_I(WASM_BRV(0, WASM_I32V_1(9)), RET(WASM_GET_LOCAL(0))));
1880 12 : CHECK_EQ(9, r.Call(0));
1881 12 : CHECK_EQ(9, r.Call(1));
1882 12 : }
1883 :
1884 25899 : WASM_EXEC_TEST(Unreachable0b) {
1885 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1886 12 : BUILD(r, WASM_BLOCK_I(WASM_BRV(0, WASM_I32V_1(7)), WASM_UNREACHABLE));
1887 12 : CHECK_EQ(7, r.Call(0));
1888 12 : CHECK_EQ(7, r.Call(1));
1889 12 : }
1890 :
1891 25891 : WASM_COMPILED_EXEC_TEST(Build_Wasm_Unreachable1) {
1892 8 : WasmRunner<int32_t, int32_t> r(execution_tier);
1893 8 : BUILD(r, WASM_UNREACHABLE);
1894 8 : }
1895 :
1896 25891 : WASM_COMPILED_EXEC_TEST(Build_Wasm_Unreachable2) {
1897 8 : WasmRunner<int32_t, int32_t> r(execution_tier);
1898 8 : BUILD(r, WASM_UNREACHABLE, WASM_UNREACHABLE);
1899 8 : }
1900 :
1901 25891 : WASM_COMPILED_EXEC_TEST(Build_Wasm_Unreachable3) {
1902 8 : WasmRunner<int32_t, int32_t> r(execution_tier);
1903 8 : BUILD(r, WASM_UNREACHABLE, WASM_UNREACHABLE, WASM_UNREACHABLE);
1904 8 : }
1905 :
1906 25891 : WASM_COMPILED_EXEC_TEST(Build_Wasm_UnreachableIf1) {
1907 8 : WasmRunner<int32_t, int32_t> r(execution_tier);
1908 8 : BUILD(r, WASM_UNREACHABLE,
1909 : WASM_IF(WASM_GET_LOCAL(0), WASM_SEQ(WASM_GET_LOCAL(0), WASM_DROP)),
1910 8 : WASM_ZERO);
1911 8 : }
1912 :
1913 25891 : WASM_COMPILED_EXEC_TEST(Build_Wasm_UnreachableIf2) {
1914 8 : WasmRunner<int32_t, int32_t> r(execution_tier);
1915 8 : BUILD(r, WASM_UNREACHABLE,
1916 8 : WASM_IF_ELSE_I(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_UNREACHABLE));
1917 8 : }
1918 :
1919 25899 : WASM_EXEC_TEST(Unreachable_Load) {
1920 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1921 12 : r.builder().AddMemory(kWasmPageSize);
1922 12 : BUILD(r, WASM_BLOCK_I(WASM_BRV(0, WASM_GET_LOCAL(0)),
1923 : WASM_LOAD_MEM(MachineType::Int8(), WASM_GET_LOCAL(0))));
1924 12 : CHECK_EQ(11, r.Call(11));
1925 12 : CHECK_EQ(21, r.Call(21));
1926 12 : }
1927 :
1928 25899 : WASM_EXEC_TEST(BrV_Fallthrough) {
1929 12 : WasmRunner<int32_t> r(execution_tier);
1930 12 : BUILD(r, WASM_BLOCK_I(WASM_BLOCK(WASM_BRV(1, WASM_I32V_1(42))),
1931 : WASM_I32V_1(22)));
1932 12 : CHECK_EQ(42, r.Call());
1933 12 : }
1934 :
1935 25899 : WASM_EXEC_TEST(Infinite_Loop_not_taken1) {
1936 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1937 12 : BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_INFINITE_LOOP), WASM_I32V_1(45));
1938 : // Run the code, but don't go into the infinite loop.
1939 12 : CHECK_EQ(45, r.Call(0));
1940 12 : }
1941 :
1942 25899 : WASM_EXEC_TEST(Infinite_Loop_not_taken2) {
1943 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1944 12 : BUILD(r, WASM_BLOCK_I(
1945 : WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_I32V_1(45)),
1946 : WASM_INFINITE_LOOP),
1947 : WASM_ZERO));
1948 : // Run the code, but don't go into the infinite loop.
1949 12 : CHECK_EQ(45, r.Call(1));
1950 12 : }
1951 :
1952 25899 : WASM_EXEC_TEST(Infinite_Loop_not_taken2_brif) {
1953 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
1954 12 : BUILD(r, WASM_BLOCK_I(WASM_BRV_IF(0, WASM_I32V_1(45), WASM_GET_LOCAL(0)),
1955 : WASM_INFINITE_LOOP));
1956 : // Run the code, but don't go into the infinite loop.
1957 12 : CHECK_EQ(45, r.Call(1));
1958 12 : }
1959 :
1960 492 : static void TestBuildGraphForSimpleExpression(WasmOpcode opcode) {
1961 492 : Isolate* isolate = CcTest::InitIsolateOnce();
1962 492 : Zone zone(isolate->allocator(), ZONE_NAME);
1963 : HandleScope scope(isolate);
1964 : // TODO(ahaas): Enable this test for anyref opcodes when code generation for
1965 : // them is implemented.
1966 984 : if (WasmOpcodes::IsAnyRefOpcode(opcode)) return;
1967 : // Enable all optional operators.
1968 492 : compiler::CommonOperatorBuilder common(&zone);
1969 : compiler::MachineOperatorBuilder machine(
1970 : &zone, MachineType::PointerRepresentation(),
1971 492 : compiler::MachineOperatorBuilder::kAllOptionalOps);
1972 492 : compiler::Graph graph(&zone);
1973 : compiler::JSGraph jsgraph(isolate, &graph, &common, nullptr, nullptr,
1974 492 : &machine);
1975 492 : FunctionSig* sig = WasmOpcodes::Signature(opcode);
1976 :
1977 492 : if (sig->parameter_count() == 1) {
1978 : byte code[] = {WASM_NO_LOCALS, kExprGetLocal, 0, static_cast<byte>(opcode),
1979 188 : WASM_END};
1980 : TestBuildingGraph(&zone, &jsgraph, nullptr, sig, nullptr, code,
1981 188 : code + arraysize(code));
1982 : } else {
1983 304 : CHECK_EQ(2, sig->parameter_count());
1984 : byte code[] = {WASM_NO_LOCALS,
1985 : kExprGetLocal,
1986 : 0,
1987 : kExprGetLocal,
1988 : 1,
1989 : static_cast<byte>(opcode),
1990 304 : WASM_END};
1991 : TestBuildingGraph(&zone, &jsgraph, nullptr, sig, nullptr, code,
1992 304 : code + arraysize(code));
1993 492 : }
1994 : }
1995 :
1996 25879 : TEST(Build_Wasm_SimpleExprs) {
1997 : // Test that the decoder can build a graph for all supported simple expressions.
1998 : #define GRAPH_BUILD_TEST(name, opcode, sig) \
1999 : TestBuildGraphForSimpleExpression(kExpr##name);
2000 :
2001 4 : FOREACH_SIMPLE_OPCODE(GRAPH_BUILD_TEST);
2002 :
2003 : #undef GRAPH_BUILD_TEST
2004 4 : }
2005 :
2006 25899 : WASM_EXEC_TEST(Int32LoadInt8_signext) {
2007 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2008 : const int kNumElems = kWasmPageSize;
2009 : int8_t* memory = r.builder().AddMemoryElems<int8_t>(kNumElems);
2010 12 : r.builder().RandomizeMemory();
2011 12 : memory[0] = -1;
2012 12 : BUILD(r, WASM_LOAD_MEM(MachineType::Int8(), WASM_GET_LOCAL(0)));
2013 :
2014 786444 : for (int i = 0; i < kNumElems; ++i) {
2015 1572864 : CHECK_EQ(memory[i], r.Call(i));
2016 12 : }
2017 12 : }
2018 :
2019 25899 : WASM_EXEC_TEST(Int32LoadInt8_zeroext) {
2020 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2021 : const int kNumElems = kWasmPageSize;
2022 12 : byte* memory = r.builder().AddMemory(kNumElems);
2023 12 : r.builder().RandomizeMemory(77);
2024 12 : memory[0] = 255;
2025 12 : BUILD(r, WASM_LOAD_MEM(MachineType::Uint8(), WASM_GET_LOCAL(0)));
2026 :
2027 786444 : for (int i = 0; i < kNumElems; ++i) {
2028 1572864 : CHECK_EQ(memory[i], r.Call(i));
2029 12 : }
2030 12 : }
2031 :
2032 25899 : WASM_EXEC_TEST(Int32LoadInt16_signext) {
2033 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2034 : const int kNumBytes = kWasmPageSize;
2035 12 : byte* memory = r.builder().AddMemory(kNumBytes);
2036 12 : r.builder().RandomizeMemory(888);
2037 12 : memory[1] = 200;
2038 12 : BUILD(r, WASM_LOAD_MEM(MachineType::Int16(), WASM_GET_LOCAL(0)));
2039 :
2040 393228 : for (int i = 0; i < kNumBytes; i += 2) {
2041 393216 : int32_t expected = static_cast<int16_t>(memory[i] | (memory[i + 1] << 8));
2042 393216 : CHECK_EQ(expected, r.Call(i));
2043 12 : }
2044 12 : }
2045 :
2046 25899 : WASM_EXEC_TEST(Int32LoadInt16_zeroext) {
2047 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2048 : const int kNumBytes = kWasmPageSize;
2049 12 : byte* memory = r.builder().AddMemory(kNumBytes);
2050 12 : r.builder().RandomizeMemory(9999);
2051 12 : memory[1] = 204;
2052 12 : BUILD(r, WASM_LOAD_MEM(MachineType::Uint16(), WASM_GET_LOCAL(0)));
2053 :
2054 393228 : for (int i = 0; i < kNumBytes; i += 2) {
2055 393216 : int32_t expected = memory[i] | (memory[i + 1] << 8);
2056 393216 : CHECK_EQ(expected, r.Call(i));
2057 12 : }
2058 12 : }
2059 :
2060 25899 : WASM_EXEC_TEST(Int32Global) {
2061 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2062 108 : int32_t* global = r.builder().AddGlobal<int32_t>();
2063 : // global = global + p0
2064 12 : BUILD(r,
2065 : WASM_SET_GLOBAL(0, WASM_I32_ADD(WASM_GET_GLOBAL(0), WASM_GET_LOCAL(0))),
2066 : WASM_ZERO);
2067 :
2068 : WriteLittleEndianValue<int32_t>(global, 116);
2069 60 : for (int i = 9; i < 444444; i += 111111) {
2070 48 : int32_t expected = ReadLittleEndianValue<int32_t>(global) + i;
2071 48 : r.Call(i);
2072 48 : CHECK_EQ(expected, ReadLittleEndianValue<int32_t>(global));
2073 12 : }
2074 12 : }
2075 :
2076 25899 : WASM_EXEC_TEST(Int32Globals_DontAlias) {
2077 : const int kNumGlobals = 3;
2078 48 : for (int g = 0; g < kNumGlobals; ++g) {
2079 : // global = global + p0
2080 36 : WasmRunner<int32_t, int32_t> r(execution_tier);
2081 36 : int32_t* globals[] = {r.builder().AddGlobal<int32_t>(),
2082 36 : r.builder().AddGlobal<int32_t>(),
2083 108 : r.builder().AddGlobal<int32_t>()};
2084 :
2085 36 : BUILD(r, WASM_SET_GLOBAL(
2086 : g, WASM_I32_ADD(WASM_GET_GLOBAL(g), WASM_GET_LOCAL(0))),
2087 : WASM_GET_GLOBAL(g));
2088 :
2089 : // Check that reading/writing global number {g} doesn't alter the others.
2090 180 : WriteLittleEndianValue<int32_t>(globals[g], 116 * g);
2091 : int32_t before[kNumGlobals];
2092 180 : for (int i = 9; i < 444444; i += 111113) {
2093 144 : int32_t sum = ReadLittleEndianValue<int32_t>(globals[g]) + i;
2094 576 : for (int j = 0; j < kNumGlobals; ++j)
2095 432 : before[j] = ReadLittleEndianValue<int32_t>(globals[j]);
2096 144 : int32_t result = r.Call(i);
2097 144 : CHECK_EQ(sum, result);
2098 432 : for (int j = 0; j < kNumGlobals; ++j) {
2099 432 : int32_t expected = j == g ? sum : before[j];
2100 432 : CHECK_EQ(expected, ReadLittleEndianValue<int32_t>(globals[j]));
2101 : }
2102 : }
2103 36 : }
2104 12 : }
2105 :
2106 25899 : WASM_EXEC_TEST(Float32Global) {
2107 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2108 108 : float* global = r.builder().AddGlobal<float>();
2109 : // global = global + p0
2110 12 : BUILD(r, WASM_SET_GLOBAL(
2111 : 0, WASM_F32_ADD(WASM_GET_GLOBAL(0),
2112 : WASM_F32_SCONVERT_I32(WASM_GET_LOCAL(0)))),
2113 : WASM_ZERO);
2114 :
2115 : WriteLittleEndianValue<float>(global, 1.25);
2116 60 : for (int i = 9; i < 4444; i += 1111) {
2117 48 : volatile float expected = ReadLittleEndianValue<float>(global) + i;
2118 48 : r.Call(i);
2119 48 : CHECK_EQ(expected, ReadLittleEndianValue<float>(global));
2120 12 : }
2121 12 : }
2122 :
2123 25899 : WASM_EXEC_TEST(Float64Global) {
2124 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2125 108 : double* global = r.builder().AddGlobal<double>();
2126 : // global = global + p0
2127 12 : BUILD(r, WASM_SET_GLOBAL(
2128 : 0, WASM_F64_ADD(WASM_GET_GLOBAL(0),
2129 : WASM_F64_SCONVERT_I32(WASM_GET_LOCAL(0)))),
2130 : WASM_ZERO);
2131 :
2132 : WriteLittleEndianValue<double>(global, 1.25);
2133 60 : for (int i = 9; i < 4444; i += 1111) {
2134 48 : volatile double expected = ReadLittleEndianValue<double>(global) + i;
2135 48 : r.Call(i);
2136 48 : CHECK_EQ(expected, ReadLittleEndianValue<double>(global));
2137 12 : }
2138 12 : }
2139 :
2140 25899 : WASM_EXEC_TEST(MixedGlobals) {
2141 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2142 :
2143 : int32_t* unused = r.builder().AddGlobal<int32_t>();
2144 12 : byte* memory = r.builder().AddMemory(kWasmPageSize);
2145 :
2146 24 : int32_t* var_int32 = r.builder().AddGlobal<int32_t>();
2147 24 : uint32_t* var_uint32 = r.builder().AddGlobal<uint32_t>();
2148 24 : float* var_float = r.builder().AddGlobal<float>();
2149 24 : double* var_double = r.builder().AddGlobal<double>();
2150 :
2151 12 : BUILD(r, WASM_SET_GLOBAL(1, WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)),
2152 : WASM_SET_GLOBAL(2, WASM_LOAD_MEM(MachineType::Uint32(), WASM_ZERO)),
2153 : WASM_SET_GLOBAL(3, WASM_LOAD_MEM(MachineType::Float32(), WASM_ZERO)),
2154 : WASM_SET_GLOBAL(4, WASM_LOAD_MEM(MachineType::Float64(), WASM_ZERO)),
2155 : WASM_ZERO);
2156 :
2157 12 : memory[0] = 0xAA;
2158 12 : memory[1] = 0xCC;
2159 12 : memory[2] = 0x55;
2160 12 : memory[3] = 0xEE;
2161 12 : memory[4] = 0x33;
2162 12 : memory[5] = 0x22;
2163 12 : memory[6] = 0x11;
2164 12 : memory[7] = 0x99;
2165 12 : r.Call(1);
2166 :
2167 12 : CHECK(static_cast<int32_t>(0xEE55CCAA) ==
2168 : ReadLittleEndianValue<int32_t>(var_int32));
2169 12 : CHECK(static_cast<uint32_t>(0xEE55CCAA) ==
2170 : ReadLittleEndianValue<uint32_t>(var_uint32));
2171 12 : CHECK(bit_cast<float>(0xEE55CCAA) == ReadLittleEndianValue<float>(var_float));
2172 12 : CHECK(bit_cast<double>(0x99112233EE55CCAAULL) ==
2173 : ReadLittleEndianValue<double>(var_double));
2174 :
2175 12 : USE(unused);
2176 12 : }
2177 :
2178 25899 : WASM_EXEC_TEST(CallEmpty) {
2179 : const int32_t kExpected = -414444;
2180 12 : WasmRunner<int32_t> r(execution_tier);
2181 :
2182 : // Build the target function.
2183 24 : WasmFunctionCompiler& target_func = r.NewFunction<int>();
2184 12 : BUILD(target_func, WASM_I32V_3(kExpected));
2185 :
2186 : // Build the calling function.
2187 24 : BUILD(r, WASM_CALL_FUNCTION0(target_func.function_index()));
2188 :
2189 12 : int32_t result = r.Call();
2190 12 : CHECK_EQ(kExpected, result);
2191 12 : }
2192 :
2193 25899 : WASM_EXEC_TEST(CallF32StackParameter) {
2194 12 : WasmRunner<float> r(execution_tier);
2195 :
2196 : // Build the target function.
2197 : ValueType param_types[20];
2198 12 : for (int i = 0; i < 20; ++i) param_types[i] = kWasmF32;
2199 : FunctionSig sig(1, 19, param_types);
2200 24 : WasmFunctionCompiler& t = r.NewFunction(&sig);
2201 12 : BUILD(t, WASM_GET_LOCAL(17));
2202 :
2203 : // Build the calling function.
2204 24 : BUILD(r, WASM_CALL_FUNCTION(
2205 : t.function_index(), WASM_F32(1.0f), WASM_F32(2.0f),
2206 : WASM_F32(4.0f), WASM_F32(8.0f), WASM_F32(16.0f), WASM_F32(32.0f),
2207 : WASM_F32(64.0f), WASM_F32(128.0f), WASM_F32(256.0f),
2208 : WASM_F32(1.5f), WASM_F32(2.5f), WASM_F32(4.5f), WASM_F32(8.5f),
2209 : WASM_F32(16.5f), WASM_F32(32.5f), WASM_F32(64.5f),
2210 : WASM_F32(128.5f), WASM_F32(256.5f), WASM_F32(512.5f)));
2211 :
2212 12 : float result = r.Call();
2213 12 : CHECK_EQ(256.5f, result);
2214 12 : }
2215 :
2216 25899 : WASM_EXEC_TEST(CallF64StackParameter) {
2217 12 : WasmRunner<double> r(execution_tier);
2218 :
2219 : // Build the target function.
2220 : ValueType param_types[20];
2221 12 : for (int i = 0; i < 20; ++i) param_types[i] = kWasmF64;
2222 : FunctionSig sig(1, 19, param_types);
2223 24 : WasmFunctionCompiler& t = r.NewFunction(&sig);
2224 12 : BUILD(t, WASM_GET_LOCAL(17));
2225 :
2226 : // Build the calling function.
2227 24 : BUILD(r, WASM_CALL_FUNCTION(t.function_index(), WASM_F64(1.0), WASM_F64(2.0),
2228 : WASM_F64(4.0), WASM_F64(8.0), WASM_F64(16.0),
2229 : WASM_F64(32.0), WASM_F64(64.0), WASM_F64(128.0),
2230 : WASM_F64(256.0), WASM_F64(1.5), WASM_F64(2.5),
2231 : WASM_F64(4.5), WASM_F64(8.5), WASM_F64(16.5),
2232 : WASM_F64(32.5), WASM_F64(64.5), WASM_F64(128.5),
2233 : WASM_F64(256.5), WASM_F64(512.5)));
2234 :
2235 12 : float result = r.Call();
2236 12 : CHECK_EQ(256.5, result);
2237 12 : }
2238 :
2239 25899 : WASM_EXEC_TEST(CallVoid) {
2240 12 : WasmRunner<int32_t> r(execution_tier);
2241 :
2242 : const byte kMemOffset = 8;
2243 : const int32_t kElemNum = kMemOffset / sizeof(int32_t);
2244 : const int32_t kExpected = 414444;
2245 : // Build the target function.
2246 12 : TestSignatures sigs;
2247 : int32_t* memory =
2248 : r.builder().AddMemoryElems<int32_t>(kWasmPageSize / sizeof(int32_t));
2249 12 : r.builder().RandomizeMemory();
2250 24 : WasmFunctionCompiler& t = r.NewFunction(sigs.v_v());
2251 12 : BUILD(t, WASM_STORE_MEM(MachineType::Int32(), WASM_I32V_1(kMemOffset),
2252 : WASM_I32V_3(kExpected)));
2253 :
2254 : // Build the calling function.
2255 24 : BUILD(r, WASM_CALL_FUNCTION0(t.function_index()),
2256 : WASM_LOAD_MEM(MachineType::Int32(), WASM_I32V_1(kMemOffset)));
2257 :
2258 12 : int32_t result = r.Call();
2259 12 : CHECK_EQ(kExpected, result);
2260 12 : CHECK_EQ(static_cast<int64_t>(kExpected),
2261 12 : static_cast<int64_t>(r.builder().ReadMemory(&memory[kElemNum])));
2262 12 : }
2263 :
2264 25899 : WASM_EXEC_TEST(Call_Int32Add) {
2265 12 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
2266 :
2267 : // Build the target function.
2268 24 : WasmFunctionCompiler& t = r.NewFunction<int32_t, int32_t, int32_t>();
2269 12 : BUILD(t, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2270 :
2271 : // Build the caller function.
2272 24 : BUILD(r, WASM_CALL_FUNCTION(t.function_index(), WASM_GET_LOCAL(0),
2273 : WASM_GET_LOCAL(1)));
2274 :
2275 708 : FOR_INT32_INPUTS(i) {
2276 41064 : FOR_INT32_INPUTS(j) {
2277 40368 : int32_t expected = static_cast<int32_t>(static_cast<uint32_t>(i) +
2278 40368 : static_cast<uint32_t>(j));
2279 40368 : CHECK_EQ(expected, r.Call(i, j));
2280 : }
2281 12 : }
2282 12 : }
2283 :
2284 25899 : WASM_EXEC_TEST(Call_Float32Sub) {
2285 12 : WasmRunner<float, float, float> r(execution_tier);
2286 :
2287 : // Build the target function.
2288 24 : WasmFunctionCompiler& target_func = r.NewFunction<float, float, float>();
2289 12 : BUILD(target_func, WASM_F32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2290 :
2291 : // Build the caller function.
2292 24 : BUILD(r, WASM_CALL_FUNCTION(target_func.function_index(), WASM_GET_LOCAL(0),
2293 : WASM_GET_LOCAL(1)));
2294 :
2295 1392 : FOR_FLOAT32_INPUTS(i) {
2296 318780 : FOR_FLOAT32_INPUTS(j) { CHECK_FLOAT_EQ(i - j, r.Call(i, j)); }
2297 12 : }
2298 12 : }
2299 :
2300 25899 : WASM_EXEC_TEST(Call_Float64Sub) {
2301 12 : WasmRunner<int32_t> r(execution_tier);
2302 28812 : double* memory =
2303 : r.builder().AddMemoryElems<double>(kWasmPageSize / sizeof(double));
2304 :
2305 12 : BUILD(r, WASM_STORE_MEM(
2306 : MachineType::Float64(), WASM_ZERO,
2307 : WASM_F64_SUB(
2308 : WASM_LOAD_MEM(MachineType::Float64(), WASM_ZERO),
2309 : WASM_LOAD_MEM(MachineType::Float64(), WASM_I32V_1(8)))),
2310 : WASM_I32V_2(107));
2311 :
2312 600 : FOR_FLOAT64_INPUTS(i) {
2313 29400 : FOR_FLOAT64_INPUTS(j) {
2314 : r.builder().WriteMemory(&memory[0], i);
2315 28812 : r.builder().WriteMemory(&memory[1], j);
2316 28812 : double expected = i - j;
2317 28812 : CHECK_EQ(107, r.Call());
2318 :
2319 28812 : if (expected != expected) {
2320 2328 : CHECK(r.builder().ReadMemory(&memory[0]) !=
2321 : r.builder().ReadMemory(&memory[0]));
2322 : } else {
2323 26484 : CHECK_EQ(expected, r.builder().ReadMemory(&memory[0]));
2324 : }
2325 : }
2326 12 : }
2327 12 : }
2328 :
2329 : #define ADD_CODE(vec, ...) \
2330 : do { \
2331 : byte __buf[] = {__VA_ARGS__}; \
2332 : for (size_t i = 0; i < sizeof(__buf); ++i) vec.push_back(__buf[i]); \
2333 : } while (false)
2334 :
2335 48 : static void Run_WasmMixedCall_N(ExecutionTier execution_tier, int start) {
2336 : const int kExpected = 6333;
2337 : const int kElemSize = 8;
2338 48 : TestSignatures sigs;
2339 :
2340 : // 64-bit cases handled in test-run-wasm-64.cc.
2341 : static MachineType mixed[] = {
2342 : MachineType::Int32(), MachineType::Float32(), MachineType::Float64(),
2343 : MachineType::Float32(), MachineType::Int32(), MachineType::Float64(),
2344 : MachineType::Float32(), MachineType::Float64(), MachineType::Int32(),
2345 : MachineType::Int32(), MachineType::Int32()};
2346 :
2347 48 : int num_params = static_cast<int>(arraysize(mixed)) - start;
2348 504 : for (int which = 0; which < num_params; ++which) {
2349 456 : v8::internal::AccountingAllocator allocator;
2350 912 : Zone zone(&allocator, ZONE_NAME);
2351 912 : WasmRunner<int32_t> r(execution_tier);
2352 456 : r.builder().AddMemory(kWasmPageSize);
2353 456 : MachineType* memtypes = &mixed[start];
2354 456 : MachineType result = memtypes[which];
2355 :
2356 : // =========================================================================
2357 : // Build the selector function.
2358 : // =========================================================================
2359 456 : FunctionSig::Builder b(&zone, 1, num_params);
2360 456 : b.AddReturn(ValueTypes::ValueTypeFor(result));
2361 4848 : for (int i = 0; i < num_params; ++i) {
2362 4392 : b.AddParam(ValueTypes::ValueTypeFor(memtypes[i]));
2363 : }
2364 912 : WasmFunctionCompiler& t = r.NewFunction(b.Build());
2365 456 : BUILD(t, WASM_GET_LOCAL(which));
2366 :
2367 : // =========================================================================
2368 : // Build the calling function.
2369 : // =========================================================================
2370 : std::vector<byte> code;
2371 :
2372 : // Load the arguments.
2373 5304 : for (int i = 0; i < num_params; ++i) {
2374 4392 : int offset = (i + 1) * kElemSize;
2375 4392 : ADD_CODE(code, WASM_LOAD_MEM(memtypes[i], WASM_I32V_2(offset)));
2376 : }
2377 :
2378 : // Call the selector function.
2379 912 : ADD_CODE(code, WASM_CALL_FUNCTION0(t.function_index()));
2380 :
2381 : // Store the result in a local.
2382 456 : byte local_index = r.AllocateLocal(ValueTypes::ValueTypeFor(result));
2383 456 : ADD_CODE(code, kExprSetLocal, local_index);
2384 :
2385 : // Store the result in memory.
2386 456 : ADD_CODE(code,
2387 : WASM_STORE_MEM(result, WASM_ZERO, WASM_GET_LOCAL(local_index)));
2388 :
2389 : // Return the expected value.
2390 456 : ADD_CODE(code, WASM_I32V_2(kExpected));
2391 :
2392 912 : r.Build(&code[0], &code[0] + code.size());
2393 :
2394 : // Run the code.
2395 5016 : for (int t = 0; t < 10; ++t) {
2396 4560 : r.builder().RandomizeMemory();
2397 4560 : CHECK_EQ(kExpected, r.Call());
2398 :
2399 4560 : int size = ValueTypes::MemSize(result);
2400 28080 : for (int i = 0; i < size; ++i) {
2401 23520 : int base = (which + 1) * kElemSize;
2402 23520 : byte expected = r.builder().raw_mem_at<byte>(base + i);
2403 : byte result = r.builder().raw_mem_at<byte>(i);
2404 23520 : CHECK_EQ(expected, result);
2405 : }
2406 : }
2407 456 : }
2408 48 : }
2409 :
2410 25887 : WASM_EXEC_TEST(MixedCall_0) { Run_WasmMixedCall_N(execution_tier, 0); }
2411 25887 : WASM_EXEC_TEST(MixedCall_1) { Run_WasmMixedCall_N(execution_tier, 1); }
2412 25887 : WASM_EXEC_TEST(MixedCall_2) { Run_WasmMixedCall_N(execution_tier, 2); }
2413 25887 : WASM_EXEC_TEST(MixedCall_3) { Run_WasmMixedCall_N(execution_tier, 3); }
2414 :
2415 25899 : WASM_EXEC_TEST(AddCall) {
2416 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2417 24 : WasmFunctionCompiler& t1 = r.NewFunction<int32_t, int32_t, int32_t>();
2418 12 : BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2419 :
2420 : byte local = r.AllocateLocal(kWasmI32);
2421 36 : BUILD(r, WASM_SET_LOCAL(local, WASM_I32V_2(99)),
2422 : WASM_I32_ADD(
2423 : WASM_CALL_FUNCTION(t1.function_index(), WASM_GET_LOCAL(0),
2424 : WASM_GET_LOCAL(0)),
2425 : WASM_CALL_FUNCTION(t1.function_index(), WASM_GET_LOCAL(local),
2426 : WASM_GET_LOCAL(local))));
2427 :
2428 12 : CHECK_EQ(198, r.Call(0));
2429 12 : CHECK_EQ(200, r.Call(1));
2430 12 : CHECK_EQ(100, r.Call(-49));
2431 12 : }
2432 :
2433 25899 : WASM_EXEC_TEST(MultiReturnSub) {
2434 : EXPERIMENTAL_FLAG_SCOPE(mv);
2435 24 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
2436 :
2437 12 : ValueType storage[] = {kWasmI32, kWasmI32, kWasmI32, kWasmI32};
2438 : FunctionSig sig_ii_ii(2, 2, storage);
2439 24 : WasmFunctionCompiler& t1 = r.NewFunction(&sig_ii_ii);
2440 12 : BUILD(t1, WASM_GET_LOCAL(1), WASM_GET_LOCAL(0));
2441 :
2442 24 : BUILD(r, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
2443 : WASM_CALL_FUNCTION0(t1.function_index()), kExprI32Sub);
2444 :
2445 708 : FOR_INT32_INPUTS(i) {
2446 41064 : FOR_INT32_INPUTS(j) {
2447 40368 : int32_t expected = static_cast<int32_t>(static_cast<uint32_t>(j) -
2448 40368 : static_cast<uint32_t>(i));
2449 40368 : CHECK_EQ(expected, r.Call(i, j));
2450 : }
2451 : }
2452 12 : }
2453 :
2454 : template <typename T>
2455 48 : void RunMultiReturnSelect(ExecutionTier execution_tier, const T* inputs) {
2456 : EXPERIMENTAL_FLAG_SCOPE(mv);
2457 48 : ValueType type = ValueTypes::ValueTypeFor(MachineTypeForC<T>());
2458 48 : ValueType storage[] = {type, type, type, type, type, type};
2459 : const size_t kNumReturns = 2;
2460 : const size_t kNumParams = arraysize(storage) - kNumReturns;
2461 : FunctionSig sig(kNumReturns, kNumParams, storage);
2462 :
2463 240 : for (size_t i = 0; i < kNumParams; i++) {
2464 768 : for (size_t j = 0; j < kNumParams; j++) {
2465 1536 : for (int k = 0; k < 2; k++) {
2466 1536 : WasmRunner<T, T, T, T, T> r(execution_tier);
2467 3072 : WasmFunctionCompiler& r1 = r.NewFunction(&sig);
2468 :
2469 1536 : BUILD(r1, WASM_GET_LOCAL(i), WASM_GET_LOCAL(j));
2470 :
2471 1536 : if (k == 0) {
2472 1536 : BUILD(r, WASM_CALL_FUNCTION(r1.function_index(), WASM_GET_LOCAL(0),
2473 : WASM_GET_LOCAL(1), WASM_GET_LOCAL(2),
2474 : WASM_GET_LOCAL(3)),
2475 : WASM_DROP);
2476 : } else {
2477 1536 : BUILD(r, WASM_CALL_FUNCTION(r1.function_index(), WASM_GET_LOCAL(0),
2478 : WASM_GET_LOCAL(1), WASM_GET_LOCAL(2),
2479 : WASM_GET_LOCAL(3)),
2480 : kExprSetLocal, 0, WASM_DROP, WASM_GET_LOCAL(0));
2481 : }
2482 :
2483 1536 : T expected = inputs[k == 0 ? i : j];
2484 1536 : CHECK_EQ(expected, r.Call(inputs[0], inputs[1], inputs[2], inputs[3]));
2485 : }
2486 : }
2487 : }
2488 48 : }
2489 :
2490 25899 : WASM_EXEC_TEST(MultiReturnSelect_i32) {
2491 : static const int32_t inputs[] = {3333333, 4444444, -55555555, -7777777};
2492 12 : RunMultiReturnSelect<int32_t>(execution_tier, inputs);
2493 0 : }
2494 :
2495 25899 : WASM_EXEC_TEST(MultiReturnSelect_f32) {
2496 : static const float inputs[] = {33.33333f, 444.4444f, -55555.555f, -77777.77f};
2497 12 : RunMultiReturnSelect<float>(execution_tier, inputs);
2498 0 : }
2499 :
2500 25899 : WASM_EXEC_TEST(MultiReturnSelect_i64) {
2501 : #if !V8_TARGET_ARCH_32_BIT || V8_TARGET_ARCH_X64
2502 : // TODO(titzer): implement int64-lowering for multiple return values
2503 : static const int64_t inputs[] = {33333338888, 44444446666, -555555553333,
2504 : -77777771111};
2505 12 : RunMultiReturnSelect<int64_t>(execution_tier, inputs);
2506 : #endif
2507 0 : }
2508 :
2509 25899 : WASM_EXEC_TEST(MultiReturnSelect_f64) {
2510 : static const double inputs[] = {3.333333, 44444.44, -55.555555, -7777.777};
2511 12 : RunMultiReturnSelect<double>(execution_tier, inputs);
2512 0 : }
2513 :
2514 25899 : WASM_EXEC_TEST(ExprBlock2a) {
2515 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2516 12 : BUILD(r, WASM_BLOCK_I(WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_I32V_1(1))),
2517 : WASM_I32V_1(1)));
2518 12 : CHECK_EQ(1, r.Call(0));
2519 12 : CHECK_EQ(1, r.Call(1));
2520 12 : }
2521 :
2522 25899 : WASM_EXEC_TEST(ExprBlock2b) {
2523 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2524 12 : BUILD(r, WASM_BLOCK_I(WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_I32V_1(1))),
2525 : WASM_I32V_1(2)));
2526 12 : CHECK_EQ(2, r.Call(0));
2527 12 : CHECK_EQ(1, r.Call(1));
2528 12 : }
2529 :
2530 25899 : WASM_EXEC_TEST(ExprBlock2c) {
2531 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2532 12 : BUILD(r, WASM_BLOCK_I(WASM_BRV_IFD(0, WASM_I32V_1(1), WASM_GET_LOCAL(0)),
2533 : WASM_I32V_1(1)));
2534 12 : CHECK_EQ(1, r.Call(0));
2535 12 : CHECK_EQ(1, r.Call(1));
2536 12 : }
2537 :
2538 25899 : WASM_EXEC_TEST(ExprBlock2d) {
2539 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2540 12 : BUILD(r, WASM_BLOCK_I(WASM_BRV_IFD(0, WASM_I32V_1(1), WASM_GET_LOCAL(0)),
2541 : WASM_I32V_1(2)));
2542 12 : CHECK_EQ(2, r.Call(0));
2543 12 : CHECK_EQ(1, r.Call(1));
2544 12 : }
2545 :
2546 25899 : WASM_EXEC_TEST(ExprBlock_ManualSwitch) {
2547 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2548 12 : BUILD(r, WASM_BLOCK_I(WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(1)),
2549 : WASM_BRV(1, WASM_I32V_1(11))),
2550 : WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(2)),
2551 : WASM_BRV(1, WASM_I32V_1(12))),
2552 : WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(3)),
2553 : WASM_BRV(1, WASM_I32V_1(13))),
2554 : WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(4)),
2555 : WASM_BRV(1, WASM_I32V_1(14))),
2556 : WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(5)),
2557 : WASM_BRV(1, WASM_I32V_1(15))),
2558 : WASM_I32V_2(99)));
2559 12 : CHECK_EQ(99, r.Call(0));
2560 12 : CHECK_EQ(11, r.Call(1));
2561 12 : CHECK_EQ(12, r.Call(2));
2562 12 : CHECK_EQ(13, r.Call(3));
2563 12 : CHECK_EQ(14, r.Call(4));
2564 12 : CHECK_EQ(15, r.Call(5));
2565 12 : CHECK_EQ(99, r.Call(6));
2566 12 : }
2567 :
2568 25899 : WASM_EXEC_TEST(ExprBlock_ManualSwitch_brif) {
2569 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2570 12 : BUILD(r, WASM_BLOCK_I(
2571 : WASM_BRV_IFD(0, WASM_I32V_1(11),
2572 : WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(1))),
2573 : WASM_BRV_IFD(0, WASM_I32V_1(12),
2574 : WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(2))),
2575 : WASM_BRV_IFD(0, WASM_I32V_1(13),
2576 : WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(3))),
2577 : WASM_BRV_IFD(0, WASM_I32V_1(14),
2578 : WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(4))),
2579 : WASM_BRV_IFD(0, WASM_I32V_1(15),
2580 : WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(5))),
2581 : WASM_I32V_2(99)));
2582 12 : CHECK_EQ(99, r.Call(0));
2583 12 : CHECK_EQ(11, r.Call(1));
2584 12 : CHECK_EQ(12, r.Call(2));
2585 12 : CHECK_EQ(13, r.Call(3));
2586 12 : CHECK_EQ(14, r.Call(4));
2587 12 : CHECK_EQ(15, r.Call(5));
2588 12 : CHECK_EQ(99, r.Call(6));
2589 12 : }
2590 :
2591 25899 : WASM_EXEC_TEST(If_nested) {
2592 12 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
2593 :
2594 12 : BUILD(
2595 : r,
2596 : WASM_IF_ELSE_I(
2597 : WASM_GET_LOCAL(0),
2598 : WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_I32V_1(11), WASM_I32V_1(12)),
2599 : WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_I32V_1(13), WASM_I32V_1(14))));
2600 :
2601 12 : CHECK_EQ(11, r.Call(1, 1));
2602 12 : CHECK_EQ(12, r.Call(1, 0));
2603 12 : CHECK_EQ(13, r.Call(0, 1));
2604 12 : CHECK_EQ(14, r.Call(0, 0));
2605 12 : }
2606 :
2607 25899 : WASM_EXEC_TEST(ExprBlock_if) {
2608 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2609 :
2610 12 : BUILD(r, WASM_BLOCK_I(WASM_IF_ELSE_I(WASM_GET_LOCAL(0),
2611 : WASM_BRV(0, WASM_I32V_1(11)),
2612 : WASM_BRV(1, WASM_I32V_1(14)))));
2613 :
2614 12 : CHECK_EQ(11, r.Call(1));
2615 12 : CHECK_EQ(14, r.Call(0));
2616 12 : }
2617 :
2618 25899 : WASM_EXEC_TEST(ExprBlock_nested_ifs) {
2619 12 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
2620 :
2621 12 : BUILD(r, WASM_BLOCK_I(WASM_IF_ELSE_I(
2622 : WASM_GET_LOCAL(0),
2623 : WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_BRV(0, WASM_I32V_1(11)),
2624 : WASM_BRV(1, WASM_I32V_1(12))),
2625 : WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_BRV(0, WASM_I32V_1(13)),
2626 : WASM_BRV(1, WASM_I32V_1(14))))));
2627 :
2628 12 : CHECK_EQ(11, r.Call(1, 1));
2629 12 : CHECK_EQ(12, r.Call(1, 0));
2630 12 : CHECK_EQ(13, r.Call(0, 1));
2631 12 : CHECK_EQ(14, r.Call(0, 0));
2632 12 : }
2633 :
2634 25899 : WASM_EXEC_TEST(SimpleCallIndirect) {
2635 12 : TestSignatures sigs;
2636 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2637 :
2638 36 : WasmFunctionCompiler& t1 = r.NewFunction(sigs.i_ii());
2639 12 : BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2640 : t1.SetSigIndex(1);
2641 :
2642 36 : WasmFunctionCompiler& t2 = r.NewFunction(sigs.i_ii());
2643 12 : BUILD(t2, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2644 : t2.SetSigIndex(1);
2645 :
2646 : // Signature table.
2647 12 : r.builder().AddSignature(sigs.f_ff());
2648 12 : r.builder().AddSignature(sigs.i_ii());
2649 12 : r.builder().AddSignature(sigs.d_dd());
2650 :
2651 : // Function table.
2652 : uint16_t indirect_function_table[] = {
2653 : static_cast<uint16_t>(t1.function_index()),
2654 24 : static_cast<uint16_t>(t2.function_index())};
2655 : r.builder().AddIndirectFunctionTable(indirect_function_table,
2656 12 : arraysize(indirect_function_table));
2657 12 : r.builder().PopulateIndirectFunctionTable();
2658 :
2659 : // Build the caller function.
2660 12 : BUILD(r, WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_I32V_2(66),
2661 : WASM_I32V_1(22)));
2662 :
2663 12 : CHECK_EQ(88, r.Call(0));
2664 12 : CHECK_EQ(44, r.Call(1));
2665 24 : CHECK_TRAP(r.Call(2));
2666 12 : }
2667 :
2668 25899 : WASM_EXEC_TEST(MultipleCallIndirect) {
2669 12 : TestSignatures sigs;
2670 12 : WasmRunner<int32_t, int32_t, int32_t, int32_t> r(execution_tier);
2671 :
2672 36 : WasmFunctionCompiler& t1 = r.NewFunction(sigs.i_ii());
2673 12 : BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2674 : t1.SetSigIndex(1);
2675 :
2676 36 : WasmFunctionCompiler& t2 = r.NewFunction(sigs.i_ii());
2677 12 : BUILD(t2, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2678 : t2.SetSigIndex(1);
2679 :
2680 : // Signature table.
2681 12 : r.builder().AddSignature(sigs.f_ff());
2682 12 : r.builder().AddSignature(sigs.i_ii());
2683 12 : r.builder().AddSignature(sigs.d_dd());
2684 :
2685 : // Function table.
2686 : uint16_t indirect_function_table[] = {
2687 : static_cast<uint16_t>(t1.function_index()),
2688 24 : static_cast<uint16_t>(t2.function_index())};
2689 : r.builder().AddIndirectFunctionTable(indirect_function_table,
2690 12 : arraysize(indirect_function_table));
2691 12 : r.builder().PopulateIndirectFunctionTable();
2692 :
2693 : // Build the caller function.
2694 12 : BUILD(r, WASM_I32_ADD(
2695 : WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
2696 : WASM_GET_LOCAL(2)),
2697 : WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(1), WASM_GET_LOCAL(2),
2698 : WASM_GET_LOCAL(0))));
2699 :
2700 12 : CHECK_EQ(5, r.Call(0, 1, 2));
2701 12 : CHECK_EQ(19, r.Call(0, 1, 9));
2702 12 : CHECK_EQ(1, r.Call(1, 0, 2));
2703 12 : CHECK_EQ(1, r.Call(1, 0, 9));
2704 :
2705 24 : CHECK_TRAP(r.Call(0, 2, 1));
2706 24 : CHECK_TRAP(r.Call(1, 2, 0));
2707 24 : CHECK_TRAP(r.Call(2, 0, 1));
2708 24 : CHECK_TRAP(r.Call(2, 1, 0));
2709 12 : }
2710 :
2711 25899 : WASM_EXEC_TEST(CallIndirect_EmptyTable) {
2712 12 : TestSignatures sigs;
2713 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2714 :
2715 : // One function.
2716 24 : WasmFunctionCompiler& t1 = r.NewFunction(sigs.i_ii());
2717 12 : BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2718 : t1.SetSigIndex(1);
2719 :
2720 : // Signature table.
2721 12 : r.builder().AddSignature(sigs.f_ff());
2722 12 : r.builder().AddSignature(sigs.i_ii());
2723 12 : r.builder().AddIndirectFunctionTable(nullptr, 0);
2724 :
2725 : // Build the caller function.
2726 12 : BUILD(r, WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_I32V_2(66),
2727 : WASM_I32V_1(22)));
2728 :
2729 24 : CHECK_TRAP(r.Call(0));
2730 24 : CHECK_TRAP(r.Call(1));
2731 24 : CHECK_TRAP(r.Call(2));
2732 12 : }
2733 :
2734 25899 : WASM_EXEC_TEST(CallIndirect_canonical) {
2735 12 : TestSignatures sigs;
2736 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
2737 :
2738 36 : WasmFunctionCompiler& t1 = r.NewFunction(sigs.i_ii());
2739 12 : BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2740 : t1.SetSigIndex(0);
2741 :
2742 36 : WasmFunctionCompiler& t2 = r.NewFunction(sigs.i_ii());
2743 12 : BUILD(t2, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2744 : t2.SetSigIndex(1);
2745 :
2746 36 : WasmFunctionCompiler& t3 = r.NewFunction(sigs.f_ff());
2747 12 : BUILD(t3, WASM_F32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2748 : t3.SetSigIndex(2);
2749 :
2750 : // Signature table.
2751 12 : r.builder().AddSignature(sigs.i_ii());
2752 12 : r.builder().AddSignature(sigs.i_ii());
2753 12 : r.builder().AddSignature(sigs.f_ff());
2754 :
2755 : // Function table.
2756 12 : uint16_t i1 = static_cast<uint16_t>(t1.function_index());
2757 12 : uint16_t i2 = static_cast<uint16_t>(t2.function_index());
2758 12 : uint16_t i3 = static_cast<uint16_t>(t3.function_index());
2759 12 : uint16_t indirect_function_table[] = {i1, i2, i3, i1, i2};
2760 :
2761 : r.builder().AddIndirectFunctionTable(indirect_function_table,
2762 12 : arraysize(indirect_function_table));
2763 12 : r.builder().PopulateIndirectFunctionTable();
2764 :
2765 : // Build the caller function.
2766 12 : BUILD(r, WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_I32V_2(77),
2767 : WASM_I32V_1(11)));
2768 :
2769 12 : CHECK_EQ(88, r.Call(0));
2770 12 : CHECK_EQ(66, r.Call(1));
2771 24 : CHECK_TRAP(r.Call(2));
2772 12 : CHECK_EQ(88, r.Call(3));
2773 12 : CHECK_EQ(66, r.Call(4));
2774 24 : CHECK_TRAP(r.Call(5));
2775 12 : }
2776 :
2777 25899 : WASM_EXEC_TEST(F32Floor) {
2778 12 : WasmRunner<float, float> r(execution_tier);
2779 12 : BUILD(r, WASM_F32_FLOOR(WASM_GET_LOCAL(0)));
2780 :
2781 2772 : FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(floorf(i), r.Call(i)); }
2782 12 : }
2783 :
2784 25899 : WASM_EXEC_TEST(F32Ceil) {
2785 12 : WasmRunner<float, float> r(execution_tier);
2786 12 : BUILD(r, WASM_F32_CEIL(WASM_GET_LOCAL(0)));
2787 :
2788 2772 : FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(ceilf(i), r.Call(i)); }
2789 12 : }
2790 :
2791 25899 : WASM_EXEC_TEST(F32Trunc) {
2792 12 : WasmRunner<float, float> r(execution_tier);
2793 12 : BUILD(r, WASM_F32_TRUNC(WASM_GET_LOCAL(0)));
2794 :
2795 2772 : FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(truncf(i), r.Call(i)); }
2796 12 : }
2797 :
2798 25899 : WASM_EXEC_TEST(F32NearestInt) {
2799 12 : WasmRunner<float, float> r(execution_tier);
2800 12 : BUILD(r, WASM_F32_NEARESTINT(WASM_GET_LOCAL(0)));
2801 :
2802 2772 : FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(nearbyintf(i), r.Call(i)); }
2803 12 : }
2804 :
2805 25899 : WASM_EXEC_TEST(F64Floor) {
2806 12 : WasmRunner<double, double> r(execution_tier);
2807 12 : BUILD(r, WASM_F64_FLOOR(WASM_GET_LOCAL(0)));
2808 :
2809 1188 : FOR_FLOAT64_INPUTS(i) { CHECK_DOUBLE_EQ(floor(i), r.Call(i)); }
2810 12 : }
2811 :
2812 25899 : WASM_EXEC_TEST(F64Ceil) {
2813 12 : WasmRunner<double, double> r(execution_tier);
2814 12 : BUILD(r, WASM_F64_CEIL(WASM_GET_LOCAL(0)));
2815 :
2816 1188 : FOR_FLOAT64_INPUTS(i) { CHECK_DOUBLE_EQ(ceil(i), r.Call(i)); }
2817 12 : }
2818 :
2819 25899 : WASM_EXEC_TEST(F64Trunc) {
2820 12 : WasmRunner<double, double> r(execution_tier);
2821 12 : BUILD(r, WASM_F64_TRUNC(WASM_GET_LOCAL(0)));
2822 :
2823 1188 : FOR_FLOAT64_INPUTS(i) { CHECK_DOUBLE_EQ(trunc(i), r.Call(i)); }
2824 12 : }
2825 :
2826 25899 : WASM_EXEC_TEST(F64NearestInt) {
2827 12 : WasmRunner<double, double> r(execution_tier);
2828 12 : BUILD(r, WASM_F64_NEARESTINT(WASM_GET_LOCAL(0)));
2829 :
2830 1188 : FOR_FLOAT64_INPUTS(i) { CHECK_DOUBLE_EQ(nearbyint(i), r.Call(i)); }
2831 12 : }
2832 :
2833 25899 : WASM_EXEC_TEST(F32Min) {
2834 12 : WasmRunner<float, float, float> r(execution_tier);
2835 12 : BUILD(r, WASM_F32_MIN(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2836 :
2837 1392 : FOR_FLOAT32_INPUTS(i) {
2838 318780 : FOR_FLOAT32_INPUTS(j) { CHECK_DOUBLE_EQ(JSMin(i, j), r.Call(i, j)); }
2839 12 : }
2840 12 : }
2841 :
2842 25899 : WASM_EXEC_TEST(F32MinSameValue) {
2843 12 : WasmRunner<float, float> r(execution_tier);
2844 12 : BUILD(r, WASM_F32_MIN(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
2845 12 : float result = r.Call(5.0f);
2846 12 : CHECK_FLOAT_EQ(5.0f, result);
2847 12 : }
2848 :
2849 25899 : WASM_EXEC_TEST(F64Min) {
2850 12 : WasmRunner<double, double, double> r(execution_tier);
2851 12 : BUILD(r, WASM_F64_MIN(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2852 :
2853 600 : FOR_FLOAT64_INPUTS(i) {
2854 58212 : FOR_FLOAT64_INPUTS(j) { CHECK_DOUBLE_EQ(JSMin(i, j), r.Call(i, j)); }
2855 12 : }
2856 12 : }
2857 :
2858 25899 : WASM_EXEC_TEST(F64MinSameValue) {
2859 12 : WasmRunner<double, double> r(execution_tier);
2860 12 : BUILD(r, WASM_F64_MIN(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
2861 12 : double result = r.Call(5.0);
2862 12 : CHECK_DOUBLE_EQ(5.0, result);
2863 12 : }
2864 :
2865 25899 : WASM_EXEC_TEST(F32Max) {
2866 12 : WasmRunner<float, float, float> r(execution_tier);
2867 12 : BUILD(r, WASM_F32_MAX(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2868 :
2869 1392 : FOR_FLOAT32_INPUTS(i) {
2870 318780 : FOR_FLOAT32_INPUTS(j) { CHECK_FLOAT_EQ(JSMax(i, j), r.Call(i, j)); }
2871 12 : }
2872 12 : }
2873 :
2874 25899 : WASM_EXEC_TEST(F32MaxSameValue) {
2875 12 : WasmRunner<float, float> r(execution_tier);
2876 12 : BUILD(r, WASM_F32_MAX(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
2877 12 : float result = r.Call(5.0f);
2878 12 : CHECK_FLOAT_EQ(5.0f, result);
2879 12 : }
2880 :
2881 25899 : WASM_EXEC_TEST(F64Max) {
2882 12 : WasmRunner<double, double, double> r(execution_tier);
2883 12 : BUILD(r, WASM_F64_MAX(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2884 :
2885 600 : FOR_FLOAT64_INPUTS(i) {
2886 29400 : FOR_FLOAT64_INPUTS(j) {
2887 28812 : double result = r.Call(i, j);
2888 57624 : CHECK_DOUBLE_EQ(JSMax(i, j), result);
2889 : }
2890 12 : }
2891 12 : }
2892 :
2893 25899 : WASM_EXEC_TEST(F64MaxSameValue) {
2894 12 : WasmRunner<double, double> r(execution_tier);
2895 12 : BUILD(r, WASM_F64_MAX(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
2896 12 : double result = r.Call(5.0);
2897 12 : CHECK_DOUBLE_EQ(5.0, result);
2898 12 : }
2899 :
2900 25899 : WASM_EXEC_TEST(I32SConvertF32) {
2901 12 : WasmRunner<int32_t, float> r(execution_tier);
2902 12 : BUILD(r, WASM_I32_SCONVERT_F32(WASM_GET_LOCAL(0)));
2903 :
2904 1392 : FOR_FLOAT32_INPUTS(i) {
2905 1380 : if (is_inbounds<int32_t>(i)) {
2906 852 : CHECK_EQ(static_cast<int32_t>(i), r.Call(i));
2907 : } else {
2908 1056 : CHECK_TRAP32(r.Call(i));
2909 : }
2910 12 : }
2911 12 : }
2912 :
2913 25899 : WASM_EXEC_TEST(I32SConvertSatF32) {
2914 : EXPERIMENTAL_FLAG_SCOPE(sat_f2i_conversions);
2915 24 : WasmRunner<int32_t, float> r(execution_tier);
2916 12 : BUILD(r, WASM_I32_SCONVERT_SAT_F32(WASM_GET_LOCAL(0)));
2917 :
2918 1392 : FOR_FLOAT32_INPUTS(i) {
2919 : int32_t expected =
2920 : is_inbounds<int32_t>(i)
2921 : ? static_cast<int32_t>(i)
2922 : : std::isnan(i) ? 0
2923 : : i < 0.0 ? std::numeric_limits<int32_t>::min()
2924 1380 : : std::numeric_limits<int32_t>::max();
2925 1380 : int32_t found = r.Call(i);
2926 1380 : CHECK_EQ(expected, found);
2927 : }
2928 12 : }
2929 :
2930 25899 : WASM_EXEC_TEST(I32SConvertF64) {
2931 12 : WasmRunner<int32_t, double> r(execution_tier);
2932 12 : BUILD(r, WASM_I32_SCONVERT_F64(WASM_GET_LOCAL(0)));
2933 :
2934 600 : FOR_FLOAT64_INPUTS(i) {
2935 588 : if (is_inbounds<int32_t>(i)) {
2936 396 : CHECK_EQ(static_cast<int32_t>(i), r.Call(i));
2937 : } else {
2938 384 : CHECK_TRAP32(r.Call(i));
2939 : }
2940 12 : }
2941 12 : }
2942 :
2943 25899 : WASM_EXEC_TEST(I32SConvertSatF64) {
2944 : EXPERIMENTAL_FLAG_SCOPE(sat_f2i_conversions);
2945 24 : WasmRunner<int32_t, double> r(execution_tier);
2946 12 : BUILD(r, WASM_I32_SCONVERT_SAT_F64(WASM_GET_LOCAL(0)));
2947 600 : FOR_FLOAT64_INPUTS(i) {
2948 : int32_t expected =
2949 : is_inbounds<int32_t>(i)
2950 : ? static_cast<int32_t>(i)
2951 : : std::isnan(i) ? 0
2952 : : i < 0.0 ? std::numeric_limits<int32_t>::min()
2953 588 : : std::numeric_limits<int32_t>::max();
2954 588 : int32_t found = r.Call(i);
2955 588 : CHECK_EQ(expected, found);
2956 : }
2957 12 : }
2958 :
2959 25899 : WASM_EXEC_TEST(I32UConvertF32) {
2960 12 : WasmRunner<uint32_t, float> r(execution_tier);
2961 12 : BUILD(r, WASM_I32_UCONVERT_F32(WASM_GET_LOCAL(0)));
2962 1392 : FOR_FLOAT32_INPUTS(i) {
2963 1380 : if (is_inbounds<uint32_t>(i)) {
2964 552 : CHECK_EQ(static_cast<uint32_t>(i), r.Call(i));
2965 : } else {
2966 828 : CHECK_TRAP32(r.Call(i));
2967 : }
2968 12 : }
2969 12 : }
2970 :
2971 25899 : WASM_EXEC_TEST(I32UConvertSatF32) {
2972 : EXPERIMENTAL_FLAG_SCOPE(sat_f2i_conversions);
2973 24 : WasmRunner<uint32_t, float> r(execution_tier);
2974 12 : BUILD(r, WASM_I32_UCONVERT_SAT_F32(WASM_GET_LOCAL(0)));
2975 1392 : FOR_FLOAT32_INPUTS(i) {
2976 : int32_t expected =
2977 : is_inbounds<uint32_t>(i)
2978 : ? static_cast<uint32_t>(i)
2979 : : std::isnan(i) ? 0
2980 : : i < 0.0 ? std::numeric_limits<uint32_t>::min()
2981 1380 : : std::numeric_limits<uint32_t>::max();
2982 1380 : int32_t found = r.Call(i);
2983 1380 : CHECK_EQ(expected, found);
2984 : }
2985 12 : }
2986 :
2987 25899 : WASM_EXEC_TEST(I32UConvertF64) {
2988 12 : WasmRunner<uint32_t, double> r(execution_tier);
2989 12 : BUILD(r, WASM_I32_UCONVERT_F64(WASM_GET_LOCAL(0)));
2990 600 : FOR_FLOAT64_INPUTS(i) {
2991 588 : if (is_inbounds<uint32_t>(i)) {
2992 324 : CHECK_EQ(static_cast<uint32_t>(i), r.Call(i));
2993 : } else {
2994 264 : CHECK_TRAP32(r.Call(i));
2995 : }
2996 12 : }
2997 12 : }
2998 :
2999 25899 : WASM_EXEC_TEST(I32UConvertSatF64) {
3000 : EXPERIMENTAL_FLAG_SCOPE(sat_f2i_conversions);
3001 24 : WasmRunner<uint32_t, double> r(execution_tier);
3002 12 : BUILD(r, WASM_I32_UCONVERT_SAT_F64(WASM_GET_LOCAL(0)));
3003 600 : FOR_FLOAT64_INPUTS(i) {
3004 : int32_t expected =
3005 : is_inbounds<uint32_t>(i)
3006 : ? static_cast<uint32_t>(i)
3007 : : std::isnan(i) ? 0
3008 : : i < 0.0 ? std::numeric_limits<uint32_t>::min()
3009 588 : : std::numeric_limits<uint32_t>::max();
3010 588 : int32_t found = r.Call(i);
3011 588 : CHECK_EQ(expected, found);
3012 : }
3013 12 : }
3014 :
3015 25899 : WASM_EXEC_TEST(F64CopySign) {
3016 12 : WasmRunner<double, double, double> r(execution_tier);
3017 12 : BUILD(r, WASM_F64_COPYSIGN(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
3018 :
3019 600 : FOR_FLOAT64_INPUTS(i) {
3020 58212 : FOR_FLOAT64_INPUTS(j) { CHECK_DOUBLE_EQ(copysign(i, j), r.Call(i, j)); }
3021 12 : }
3022 12 : }
3023 :
3024 25899 : WASM_EXEC_TEST(F32CopySign) {
3025 12 : WasmRunner<float, float, float> r(execution_tier);
3026 12 : BUILD(r, WASM_F32_COPYSIGN(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
3027 :
3028 1392 : FOR_FLOAT32_INPUTS(i) {
3029 318780 : FOR_FLOAT32_INPUTS(j) { CHECK_FLOAT_EQ(copysignf(i, j), r.Call(i, j)); }
3030 12 : }
3031 12 : }
3032 :
3033 24 : static void CompileCallIndirectMany(ExecutionTier tier, ValueType param) {
3034 : // Make sure we don't run out of registers when compiling indirect calls
3035 : // with many many parameters.
3036 24 : TestSignatures sigs;
3037 984 : for (byte num_params = 0; num_params < 40; ++num_params) {
3038 960 : WasmRunner<void> r(tier);
3039 960 : FunctionSig* sig = sigs.many(r.zone(), kWasmStmt, param, num_params);
3040 :
3041 960 : r.builder().AddSignature(sig);
3042 960 : r.builder().AddSignature(sig);
3043 960 : r.builder().AddIndirectFunctionTable(nullptr, 0);
3044 :
3045 960 : WasmFunctionCompiler& t = r.NewFunction(sig);
3046 :
3047 : std::vector<byte> code;
3048 19680 : for (byte p = 0; p < num_params; ++p) {
3049 18720 : ADD_CODE(code, kExprGetLocal, p);
3050 : }
3051 960 : ADD_CODE(code, kExprI32Const, 0);
3052 960 : ADD_CODE(code, kExprCallIndirect, 1, TABLE_ZERO);
3053 :
3054 1920 : t.Build(&code[0], &code[0] + code.size());
3055 960 : }
3056 24 : }
3057 :
3058 25891 : WASM_COMPILED_EXEC_TEST(Compile_Wasm_CallIndirect_Many_i32) {
3059 8 : CompileCallIndirectMany(execution_tier, kWasmI32);
3060 0 : }
3061 :
3062 25891 : WASM_COMPILED_EXEC_TEST(Compile_Wasm_CallIndirect_Many_f32) {
3063 8 : CompileCallIndirectMany(execution_tier, kWasmF32);
3064 0 : }
3065 :
3066 25891 : WASM_COMPILED_EXEC_TEST(Compile_Wasm_CallIndirect_Many_f64) {
3067 8 : CompileCallIndirectMany(execution_tier, kWasmF64);
3068 0 : }
3069 :
3070 25899 : WASM_EXEC_TEST(Int32RemS_dead) {
3071 12 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
3072 12 : BUILD(r, WASM_I32_REMS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)), WASM_DROP,
3073 : WASM_ZERO);
3074 : const int32_t kMin = std::numeric_limits<int32_t>::min();
3075 12 : CHECK_EQ(0, r.Call(133, 100));
3076 12 : CHECK_EQ(0, r.Call(kMin, -1));
3077 12 : CHECK_EQ(0, r.Call(0, 1));
3078 24 : CHECK_TRAP(r.Call(100, 0));
3079 24 : CHECK_TRAP(r.Call(-1001, 0));
3080 24 : CHECK_TRAP(r.Call(kMin, 0));
3081 12 : }
3082 :
3083 25899 : WASM_EXEC_TEST(BrToLoopWithValue) {
3084 12 : WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
3085 : // Subtracts <1> times 3 from <0> and returns the result.
3086 12 : BUILD(r,
3087 : // loop i32
3088 : kExprLoop, kLocalI32,
3089 : // decrement <0> by 3.
3090 : WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I32V_1(3))),
3091 : // decrement <1> by 1.
3092 : WASM_SET_LOCAL(1, WASM_I32_SUB(WASM_GET_LOCAL(1), WASM_ONE)),
3093 : // load return value <0>, br_if will drop if if the branch is taken.
3094 : WASM_GET_LOCAL(0),
3095 : // continue loop if <1> is != 0.
3096 : WASM_BR_IF(0, WASM_GET_LOCAL(1)),
3097 : // end of loop, value loaded above is the return value.
3098 : kExprEnd);
3099 12 : CHECK_EQ(12, r.Call(27, 5));
3100 12 : }
3101 :
3102 25899 : WASM_EXEC_TEST(BrToLoopWithoutValue) {
3103 : // This was broken in the interpreter, see http://crbug.com/715454
3104 12 : WasmRunner<int32_t, int32_t> r(execution_tier);
3105 12 : BUILD(
3106 : r, kExprLoop, kLocalI32, // loop i32
3107 : WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_ONE)), // dec <0>
3108 : WASM_BR_IF(0, WASM_GET_LOCAL(0)), // br_if <0> != 0
3109 : kExprUnreachable, // unreachable
3110 : kExprEnd); // end
3111 24 : CHECK_TRAP32(r.Call(2));
3112 12 : }
3113 :
3114 25899 : WASM_EXEC_TEST(LoopsWithValues) {
3115 12 : WasmRunner<int32_t> r(execution_tier);
3116 12 : BUILD(r, WASM_LOOP_I(WASM_LOOP_I(WASM_ONE), WASM_ONE, kExprI32Add));
3117 12 : CHECK_EQ(2, r.Call());
3118 12 : }
3119 :
3120 25899 : WASM_EXEC_TEST(InvalidStackAfterUnreachable) {
3121 12 : WasmRunner<int32_t> r(execution_tier);
3122 12 : BUILD(r, kExprUnreachable, kExprI32Add);
3123 24 : CHECK_TRAP32(r.Call());
3124 12 : }
3125 :
3126 25899 : WASM_EXEC_TEST(InvalidStackAfterBr) {
3127 12 : WasmRunner<int32_t> r(execution_tier);
3128 12 : BUILD(r, WASM_BRV(0, WASM_I32V_1(27)), kExprI32Add);
3129 12 : CHECK_EQ(27, r.Call());
3130 12 : }
3131 :
3132 25899 : WASM_EXEC_TEST(InvalidStackAfterReturn) {
3133 12 : WasmRunner<int32_t> r(execution_tier);
3134 12 : BUILD(r, WASM_RETURN1(WASM_I32V_1(17)), kExprI32Add);
3135 12 : CHECK_EQ(17, r.Call());
3136 12 : }
3137 :
3138 25899 : WASM_EXEC_TEST(BranchOverUnreachableCode) {
3139 12 : WasmRunner<int32_t> r(execution_tier);
3140 12 : BUILD(r,
3141 : // Start a block which breaks in the middle (hence unreachable code
3142 : // afterwards) and continue execution after this block.
3143 : WASM_BLOCK_I(WASM_BRV(0, WASM_I32V_1(17)), kExprI32Add),
3144 : // Add one to the 17 returned from the block.
3145 : WASM_ONE, kExprI32Add);
3146 12 : CHECK_EQ(18, r.Call());
3147 12 : }
3148 :
3149 25899 : WASM_EXEC_TEST(BranchOverUnreachableCodeInLoop0) {
3150 12 : WasmRunner<int32_t> r(execution_tier);
3151 12 : BUILD(r,
3152 : WASM_BLOCK_I(
3153 : // Start a loop which breaks in the middle (hence unreachable code
3154 : // afterwards) and continue execution after this loop.
3155 : // This should validate even though there is no value on the stack
3156 : // at the end of the loop.
3157 : WASM_LOOP_I(WASM_BRV(1, WASM_I32V_1(17)))),
3158 : // Add one to the 17 returned from the block.
3159 : WASM_ONE, kExprI32Add);
3160 12 : CHECK_EQ(18, r.Call());
3161 12 : }
3162 :
3163 25899 : WASM_EXEC_TEST(BranchOverUnreachableCodeInLoop1) {
3164 12 : WasmRunner<int32_t> r(execution_tier);
3165 12 : BUILD(r,
3166 : WASM_BLOCK_I(
3167 : // Start a loop which breaks in the middle (hence unreachable code
3168 : // afterwards) and continue execution after this loop.
3169 : // Even though unreachable, the loop leaves one value on the stack.
3170 : WASM_LOOP_I(WASM_BRV(1, WASM_I32V_1(17)), WASM_ONE)),
3171 : // Add one to the 17 returned from the block.
3172 : WASM_ONE, kExprI32Add);
3173 12 : CHECK_EQ(18, r.Call());
3174 12 : }
3175 :
3176 25899 : WASM_EXEC_TEST(BranchOverUnreachableCodeInLoop2) {
3177 12 : WasmRunner<int32_t> r(execution_tier);
3178 12 : BUILD(r,
3179 : WASM_BLOCK_I(
3180 : // Start a loop which breaks in the middle (hence unreachable code
3181 : // afterwards) and continue execution after this loop.
3182 : // The unreachable code is allowed to pop non-existing values off
3183 : // the stack and push back the result.
3184 : WASM_LOOP_I(WASM_BRV(1, WASM_I32V_1(17)), kExprI32Add)),
3185 : // Add one to the 17 returned from the block.
3186 : WASM_ONE, kExprI32Add);
3187 12 : CHECK_EQ(18, r.Call());
3188 12 : }
3189 :
3190 25899 : WASM_EXEC_TEST(BlockInsideUnreachable) {
3191 12 : WasmRunner<int32_t> r(execution_tier);
3192 12 : BUILD(r, WASM_RETURN1(WASM_I32V_1(17)), WASM_BLOCK(WASM_BR(0)));
3193 12 : CHECK_EQ(17, r.Call());
3194 12 : }
3195 :
3196 25899 : WASM_EXEC_TEST(IfInsideUnreachable) {
3197 12 : WasmRunner<int32_t> r(execution_tier);
3198 12 : BUILD(
3199 : r, WASM_RETURN1(WASM_I32V_1(17)),
3200 : WASM_IF_ELSE_I(WASM_ONE, WASM_BRV(0, WASM_ONE), WASM_RETURN1(WASM_ONE)));
3201 12 : CHECK_EQ(17, r.Call());
3202 12 : }
3203 :
3204 : // This test targets binops in Liftoff.
3205 : // Initialize a number of local variables to force them into different
3206 : // registers, then perform a binary operation on two of the locals.
3207 : // Afterwards, write back all locals to memory, to check that their value was
3208 : // not overwritten.
3209 : template <typename ctype>
3210 240 : void BinOpOnDifferentRegisters(
3211 : ExecutionTier execution_tier, ValueType type, Vector<const ctype> inputs,
3212 : WasmOpcode opcode, std::function<ctype(ctype, ctype, bool*)> expect_fn) {
3213 : static constexpr int kMaxNumLocals = 8;
3214 1920 : for (int num_locals = 1; num_locals < kMaxNumLocals; ++num_locals) {
3215 : // {init_locals_code} is shared by all code generated in the loop below.
3216 : std::vector<byte> init_locals_code;
3217 : // Load from memory into the locals.
3218 8400 : for (int i = 0; i < num_locals; ++i) {
3219 6720 : ADD_CODE(
3220 : init_locals_code,
3221 : WASM_SET_LOCAL(i, WASM_LOAD_MEM(ValueTypes::MachineTypeFor(type),
3222 : WASM_I32V_2(sizeof(ctype) * i))));
3223 : }
3224 : // {write_locals_code} is shared by all code generated in the loop below.
3225 : std::vector<byte> write_locals_code;
3226 : // Write locals back into memory, shifted by one element to the right.
3227 10080 : for (int i = 0; i < num_locals; ++i) {
3228 6720 : ADD_CODE(write_locals_code,
3229 : WASM_STORE_MEM(ValueTypes::MachineTypeFor(type),
3230 : WASM_I32V_2(sizeof(ctype) * (i + 1)),
3231 : WASM_GET_LOCAL(i)));
3232 : }
3233 6720 : for (int lhs = 0; lhs < num_locals; ++lhs) {
3234 33600 : for (int rhs = 0; rhs < num_locals; ++rhs) {
3235 33600 : WasmRunner<int32_t> r(execution_tier);
3236 1162560 : ctype* memory =
3237 : r.builder().AddMemoryElems<ctype>(kWasmPageSize / sizeof(ctype));
3238 221760 : for (int i = 0; i < num_locals; ++i) {
3239 : r.AllocateLocal(type);
3240 : }
3241 33600 : std::vector<byte> code(init_locals_code);
3242 33600 : ADD_CODE(code,
3243 : // Store the result of the binary operation at memory[0].
3244 : WASM_STORE_MEM(ValueTypes::MachineTypeFor(type), WASM_ZERO,
3245 : WASM_BINOP(opcode, WASM_GET_LOCAL(lhs),
3246 : WASM_GET_LOCAL(rhs))),
3247 : // Return 0.
3248 : WASM_ZERO);
3249 33600 : code.insert(code.end(), write_locals_code.begin(),
3250 : write_locals_code.end());
3251 67200 : r.Build(code.data(), code.data() + code.size());
3252 235200 : for (ctype lhs_value : inputs) {
3253 1444800 : for (ctype rhs_value : inputs) {
3254 1243200 : if (lhs == rhs) lhs_value = rhs_value;
3255 8205120 : for (int i = 0; i < num_locals; ++i) {
3256 : ctype value =
3257 : i == lhs ? lhs_value
3258 6961920 : : i == rhs ? rhs_value : static_cast<ctype>(i + 47);
3259 6961920 : WriteLittleEndianValue<ctype>(&memory[i], value);
3260 : }
3261 1243200 : bool trap = false;
3262 1243200 : int64_t expect = expect_fn(lhs_value, rhs_value, &trap);
3263 1243200 : if (trap) {
3264 161280 : CHECK_TRAP(r.Call());
3265 80640 : continue;
3266 : }
3267 1162560 : CHECK_EQ(0, r.Call());
3268 1162560 : CHECK_EQ(expect, ReadLittleEndianValue<ctype>(&memory[0]));
3269 6510336 : for (int i = 0; i < num_locals; ++i) {
3270 : ctype value =
3271 : i == lhs ? lhs_value
3272 6510336 : : i == rhs ? rhs_value : static_cast<ctype>(i + 47);
3273 6510336 : CHECK_EQ(value, ReadLittleEndianValue<ctype>(&memory[i + 1]));
3274 : }
3275 : }
3276 : }
3277 : }
3278 : }
3279 : }
3280 240 : }
3281 :
3282 : // Keep this list small, the BinOpOnDifferentRegisters test is running long
3283 : // enough already.
3284 : static constexpr int32_t kSome32BitInputs[] = {0, 1, -1, 31, 0xff112233};
3285 : static constexpr int64_t kSome64BitInputs[] = {
3286 : 0, 1, -1, 31, 63, 0x100000000, 0xff11223344556677};
3287 :
3288 25899 : WASM_EXEC_TEST(I32AddOnDifferentRegisters) {
3289 : BinOpOnDifferentRegisters<int32_t>(
3290 : execution_tier, kWasmI32, ArrayVector(kSome32BitInputs), kExprI32Add,
3291 42024 : [](int32_t lhs, int32_t rhs, bool* trap) { return lhs + rhs; });
3292 12 : }
3293 :
3294 25899 : WASM_EXEC_TEST(I32SubOnDifferentRegisters) {
3295 : BinOpOnDifferentRegisters<int32_t>(
3296 : execution_tier, kWasmI32, ArrayVector(kSome32BitInputs), kExprI32Sub,
3297 42024 : [](int32_t lhs, int32_t rhs, bool* trap) { return lhs - rhs; });
3298 12 : }
3299 :
3300 25899 : WASM_EXEC_TEST(I32MulOnDifferentRegisters) {
3301 : BinOpOnDifferentRegisters<int32_t>(execution_tier, kWasmI32,
3302 : ArrayVector(kSome32BitInputs), kExprI32Mul,
3303 : [](int32_t lhs, int32_t rhs, bool* trap) {
3304 : return base::MulWithWraparound(lhs, rhs);
3305 24 : });
3306 12 : }
3307 :
3308 25899 : WASM_EXEC_TEST(I32ShlOnDifferentRegisters) {
3309 : BinOpOnDifferentRegisters<int32_t>(execution_tier, kWasmI32,
3310 : ArrayVector(kSome32BitInputs), kExprI32Shl,
3311 : [](int32_t lhs, int32_t rhs, bool* trap) {
3312 : return base::ShlWithWraparound(lhs, rhs);
3313 24 : });
3314 12 : }
3315 :
3316 25899 : WASM_EXEC_TEST(I32ShrSOnDifferentRegisters) {
3317 : BinOpOnDifferentRegisters<int32_t>(
3318 : execution_tier, kWasmI32, ArrayVector(kSome32BitInputs), kExprI32ShrS,
3319 42024 : [](int32_t lhs, int32_t rhs, bool* trap) { return lhs >> (rhs & 31); });
3320 12 : }
3321 :
3322 25899 : WASM_EXEC_TEST(I32ShrUOnDifferentRegisters) {
3323 : BinOpOnDifferentRegisters<int32_t>(
3324 : execution_tier, kWasmI32, ArrayVector(kSome32BitInputs), kExprI32ShrU,
3325 : [](int32_t lhs, int32_t rhs, bool* trap) {
3326 42000 : return static_cast<uint32_t>(lhs) >> (rhs & 31);
3327 42024 : });
3328 12 : }
3329 :
3330 25899 : WASM_EXEC_TEST(I32DivSOnDifferentRegisters) {
3331 : BinOpOnDifferentRegisters<int32_t>(
3332 : execution_tier, kWasmI32, ArrayVector(kSome32BitInputs), kExprI32DivS,
3333 : [](int32_t lhs, int32_t rhs, bool* trap) {
3334 42000 : *trap = rhs == 0;
3335 42000 : return *trap ? 0 : lhs / rhs;
3336 24 : });
3337 12 : }
3338 :
3339 25899 : WASM_EXEC_TEST(I32DivUOnDifferentRegisters) {
3340 : BinOpOnDifferentRegisters<int32_t>(
3341 : execution_tier, kWasmI32, ArrayVector(kSome32BitInputs), kExprI32DivU,
3342 : [](uint32_t lhs, uint32_t rhs, bool* trap) {
3343 42000 : *trap = rhs == 0;
3344 42000 : return *trap ? 0 : lhs / rhs;
3345 24 : });
3346 12 : }
3347 :
3348 25899 : WASM_EXEC_TEST(I32RemSOnDifferentRegisters) {
3349 : BinOpOnDifferentRegisters<int32_t>(
3350 : execution_tier, kWasmI32, ArrayVector(kSome32BitInputs), kExprI32RemS,
3351 : [](int32_t lhs, int32_t rhs, bool* trap) {
3352 42000 : *trap = rhs == 0;
3353 42000 : return *trap || rhs == -1 ? 0 : lhs % rhs;
3354 24 : });
3355 12 : }
3356 :
3357 25899 : WASM_EXEC_TEST(I32RemUOnDifferentRegisters) {
3358 : BinOpOnDifferentRegisters<int32_t>(
3359 : execution_tier, kWasmI32, ArrayVector(kSome32BitInputs), kExprI32RemU,
3360 : [](uint32_t lhs, uint32_t rhs, bool* trap) {
3361 42000 : *trap = rhs == 0;
3362 42000 : return *trap ? 0 : lhs % rhs;
3363 24 : });
3364 12 : }
3365 :
3366 25899 : WASM_EXEC_TEST(I64AddOnDifferentRegisters) {
3367 : BinOpOnDifferentRegisters<int64_t>(
3368 : execution_tier, kWasmI64, ArrayVector(kSome64BitInputs), kExprI64Add,
3369 82344 : [](int64_t lhs, int64_t rhs, bool* trap) { return lhs + rhs; });
3370 12 : }
3371 :
3372 25899 : WASM_EXEC_TEST(I64SubOnDifferentRegisters) {
3373 : BinOpOnDifferentRegisters<int64_t>(
3374 : execution_tier, kWasmI64, ArrayVector(kSome64BitInputs), kExprI64Sub,
3375 82344 : [](int64_t lhs, int64_t rhs, bool* trap) { return lhs - rhs; });
3376 12 : }
3377 :
3378 25899 : WASM_EXEC_TEST(I64MulOnDifferentRegisters) {
3379 : BinOpOnDifferentRegisters<int64_t>(execution_tier, kWasmI64,
3380 : ArrayVector(kSome64BitInputs), kExprI64Mul,
3381 : [](int64_t lhs, int64_t rhs, bool* trap) {
3382 : return base::MulWithWraparound(lhs, rhs);
3383 24 : });
3384 12 : }
3385 :
3386 25899 : WASM_EXEC_TEST(I64ShlOnDifferentRegisters) {
3387 : BinOpOnDifferentRegisters<int64_t>(execution_tier, kWasmI64,
3388 : ArrayVector(kSome64BitInputs), kExprI64Shl,
3389 : [](int64_t lhs, int64_t rhs, bool* trap) {
3390 : return base::ShlWithWraparound(lhs, rhs);
3391 24 : });
3392 12 : }
3393 :
3394 25899 : WASM_EXEC_TEST(I64ShrSOnDifferentRegisters) {
3395 : BinOpOnDifferentRegisters<int64_t>(
3396 : execution_tier, kWasmI64, ArrayVector(kSome64BitInputs), kExprI64ShrS,
3397 82344 : [](int64_t lhs, int64_t rhs, bool* trap) { return lhs >> (rhs & 63); });
3398 12 : }
3399 :
3400 25899 : WASM_EXEC_TEST(I64ShrUOnDifferentRegisters) {
3401 : BinOpOnDifferentRegisters<int64_t>(
3402 : execution_tier, kWasmI64, ArrayVector(kSome64BitInputs), kExprI64ShrU,
3403 : [](int64_t lhs, int64_t rhs, bool* trap) {
3404 82320 : return static_cast<uint64_t>(lhs) >> (rhs & 63);
3405 82344 : });
3406 12 : }
3407 :
3408 25899 : WASM_EXEC_TEST(I64DivSOnDifferentRegisters) {
3409 : BinOpOnDifferentRegisters<int64_t>(
3410 : execution_tier, kWasmI64, ArrayVector(kSome64BitInputs), kExprI64DivS,
3411 : [](int64_t lhs, int64_t rhs, bool* trap) {
3412 82320 : *trap = rhs == 0 ||
3413 11760 : (rhs == -1 && lhs == std::numeric_limits<int64_t>::min());
3414 82320 : return *trap ? 0 : lhs / rhs;
3415 24 : });
3416 12 : }
3417 :
3418 25899 : WASM_EXEC_TEST(I64DivUOnDifferentRegisters) {
3419 : BinOpOnDifferentRegisters<int64_t>(
3420 : execution_tier, kWasmI64, ArrayVector(kSome64BitInputs), kExprI64DivU,
3421 : [](uint64_t lhs, uint64_t rhs, bool* trap) {
3422 82320 : *trap = rhs == 0;
3423 82320 : return *trap ? 0 : lhs / rhs;
3424 24 : });
3425 12 : }
3426 :
3427 25899 : WASM_EXEC_TEST(I64RemSOnDifferentRegisters) {
3428 : BinOpOnDifferentRegisters<int64_t>(
3429 : execution_tier, kWasmI64, ArrayVector(kSome64BitInputs), kExprI64RemS,
3430 : [](int64_t lhs, int64_t rhs, bool* trap) {
3431 82320 : *trap = rhs == 0;
3432 82320 : return *trap || rhs == -1 ? 0 : lhs % rhs;
3433 24 : });
3434 12 : }
3435 :
3436 25899 : WASM_EXEC_TEST(I64RemUOnDifferentRegisters) {
3437 : BinOpOnDifferentRegisters<int64_t>(
3438 : execution_tier, kWasmI64, ArrayVector(kSome64BitInputs), kExprI64RemU,
3439 : [](uint64_t lhs, uint64_t rhs, bool* trap) {
3440 82320 : *trap = rhs == 0;
3441 82320 : return *trap ? 0 : lhs % rhs;
3442 24 : });
3443 12 : }
3444 :
3445 25879 : TEST(Liftoff_tier_up) {
3446 4 : WasmRunner<int32_t, int32_t, int32_t> r(ExecutionTier::kBaseline);
3447 :
3448 16 : WasmFunctionCompiler& add = r.NewFunction<int32_t, int32_t, int32_t>("add");
3449 4 : BUILD(add, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
3450 :
3451 8 : WasmFunctionCompiler& sub = r.NewFunction<int32_t, int32_t, int32_t>("sub");
3452 4 : BUILD(sub, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
3453 :
3454 : // Create the main function, which shall call {add}.
3455 8 : BUILD(r, WASM_CALL_FUNCTION(add.function_index(), WASM_GET_LOCAL(0),
3456 : WASM_GET_LOCAL(1)));
3457 :
3458 : NativeModule* native_module =
3459 4 : r.builder().instance_object()->module_object()->native_module();
3460 :
3461 : // This test only works if we managed to compile with Liftoff.
3462 4 : if (native_module->code(add.function_index())->is_liftoff()) {
3463 : // First run should execute {add}.
3464 4 : CHECK_EQ(18, r.Call(11, 7));
3465 :
3466 : // Now make a copy of the {sub} function, and add it to the native module at
3467 : // the index of {add}.
3468 4 : CodeDesc desc;
3469 : memset(&desc, 0, sizeof(CodeDesc));
3470 : WasmCode* sub_code = native_module->code(sub.function_index());
3471 : size_t sub_size = sub_code->instructions().size();
3472 4 : std::unique_ptr<byte[]> buffer(new byte[sub_code->instructions().size()]);
3473 : memcpy(buffer.get(), sub_code->instructions().start(), sub_size);
3474 4 : desc.buffer = buffer.get();
3475 4 : desc.instr_size = static_cast<int>(sub_size);
3476 : WasmCode* code = native_module->AddCode(
3477 : add.function_index(), desc, 0, 0, {}, OwnedVector<byte>(),
3478 16 : WasmCode::kFunction, WasmCode::kOther);
3479 4 : native_module->PublishCode(code);
3480 :
3481 : // Second run should now execute {sub}.
3482 4 : CHECK_EQ(4, r.Call(11, 7));
3483 4 : }
3484 4 : }
3485 :
3486 : #undef B1
3487 : #undef B2
3488 : #undef RET
3489 : #undef RET_I8
3490 : #undef ADD_CODE
3491 :
3492 : } // namespace test_run_wasm
3493 : } // namespace wasm
3494 : } // namespace internal
3495 77625 : } // namespace v8
|