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