blob: ec7c5e713d89b8542b52713c19fa7ce56249c7df [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
Ben Murdochc5610432016-08-08 18:44:38 +01009#include "src/base/platform/elapsed-timer.h"
10
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011#include "src/wasm/wasm-macro-gen.h"
12
13#include "test/cctest/cctest.h"
14#include "test/cctest/compiler/value-helper.h"
15#include "test/cctest/wasm/test-signatures.h"
16#include "test/cctest/wasm/wasm-run-utils.h"
17
18using namespace v8::base;
19using namespace v8::internal;
20using namespace v8::internal::compiler;
21using namespace v8::internal::wasm;
22
Ben Murdochda12d292016-06-02 14:46:10 +010023// for even shorter tests.
Ben Murdochc5610432016-08-08 18:44:38 +010024#define B2(a, b) kExprBlock, a, b, kExprEnd
25#define B1(a) kExprBlock, a, kExprEnd
26#define RET(x) x, kExprReturn, 1
27#define RET_I8(x) kExprI8Const, x, kExprReturn, 1
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028
Ben Murdochc5610432016-08-08 18:44:38 +010029WASM_EXEC_TEST(Int8Const) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010030 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000031 const byte kExpectedValue = 121;
32 // return(kExpectedValue)
33 BUILD(r, WASM_I8(kExpectedValue));
34 CHECK_EQ(kExpectedValue, r.Call());
35}
36
Ben Murdochc5610432016-08-08 18:44:38 +010037WASM_EXEC_TEST(Int8Const_fallthru1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010038 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000039 const byte kExpectedValue = 122;
40 // kExpectedValue
41 BUILD(r, WASM_I8(kExpectedValue));
42 CHECK_EQ(kExpectedValue, r.Call());
43}
44
Ben Murdochc5610432016-08-08 18:44:38 +010045WASM_EXEC_TEST(Int8Const_fallthru2) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010046 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047 const byte kExpectedValue = 123;
48 // -99 kExpectedValue
49 BUILD(r, WASM_I8(-99), WASM_I8(kExpectedValue));
50 CHECK_EQ(kExpectedValue, r.Call());
51}
52
Ben Murdochc5610432016-08-08 18:44:38 +010053WASM_EXEC_TEST(Int8Const_all) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010054 for (int value = -128; value <= 127; ++value) {
55 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056 // return(value)
57 BUILD(r, WASM_I8(value));
Ben Murdoch097c5b22016-05-18 11:27:45 +010058 int32_t result = r.Call();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000059 CHECK_EQ(value, result);
60 }
61}
62
Ben Murdochc5610432016-08-08 18:44:38 +010063WASM_EXEC_TEST(Int32Const) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010064 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000065 const int32_t kExpectedValue = 0x11223344;
66 // return(kExpectedValue)
Ben Murdochda12d292016-06-02 14:46:10 +010067 BUILD(r, WASM_I32V_5(kExpectedValue));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068 CHECK_EQ(kExpectedValue, r.Call());
69}
70
Ben Murdochc5610432016-08-08 18:44:38 +010071WASM_EXEC_TEST(Int32Const_many) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072 FOR_INT32_INPUTS(i) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010073 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074 const int32_t kExpectedValue = *i;
75 // return(kExpectedValue)
Ben Murdochda12d292016-06-02 14:46:10 +010076 BUILD(r, WASM_I32V(kExpectedValue));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000077 CHECK_EQ(kExpectedValue, r.Call());
78 }
79}
80
Ben Murdochc5610432016-08-08 18:44:38 +010081WASM_EXEC_TEST(MemorySize) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010082 TestingModule module(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +010083 WasmRunner<int32_t> r(&module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000084 module.AddMemory(1024);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000085 BUILD(r, kExprMemorySize);
86 CHECK_EQ(1024, r.Call());
87}
88
Ben Murdochc5610432016-08-08 18:44:38 +010089WASM_EXEC_TEST(Int32Param0) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010090 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000091 // return(local[0])
92 BUILD(r, WASM_GET_LOCAL(0));
93 FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); }
94}
95
Ben Murdochc5610432016-08-08 18:44:38 +010096WASM_EXEC_TEST(Int32Param0_fallthru) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010097 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000098 // local[0]
99 BUILD(r, WASM_GET_LOCAL(0));
100 FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); }
101}
102
Ben Murdochc5610432016-08-08 18:44:38 +0100103WASM_EXEC_TEST(Int32Param1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100104 WasmRunner<int32_t> r(execution_mode, MachineType::Int32(),
105 MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000106 // local[1]
107 BUILD(r, WASM_GET_LOCAL(1));
108 FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(-111, *i)); }
109}
110
Ben Murdochc5610432016-08-08 18:44:38 +0100111WASM_EXEC_TEST(Int32Add) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100112 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113 // 11 + 44
114 BUILD(r, WASM_I32_ADD(WASM_I8(11), WASM_I8(44)));
115 CHECK_EQ(55, r.Call());
116}
117
Ben Murdochc5610432016-08-08 18:44:38 +0100118WASM_EXEC_TEST(Int32Add_P) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100119 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000120 // p0 + 13
121 BUILD(r, WASM_I32_ADD(WASM_I8(13), WASM_GET_LOCAL(0)));
122 FOR_INT32_INPUTS(i) { CHECK_EQ(*i + 13, r.Call(*i)); }
123}
124
Ben Murdochc5610432016-08-08 18:44:38 +0100125WASM_EXEC_TEST(Int32Add_P_fallthru) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100126 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000127 // p0 + 13
128 BUILD(r, WASM_I32_ADD(WASM_I8(13), WASM_GET_LOCAL(0)));
129 FOR_INT32_INPUTS(i) { CHECK_EQ(*i + 13, r.Call(*i)); }
130}
131
Ben Murdochc5610432016-08-08 18:44:38 +0100132WASM_EXEC_TEST(Int32Add_P2) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100133 WasmRunner<int32_t> r(execution_mode, MachineType::Int32(),
134 MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000135 // p0 + p1
136 BUILD(r, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
137 FOR_INT32_INPUTS(i) {
138 FOR_INT32_INPUTS(j) {
139 int32_t expected = static_cast<int32_t>(static_cast<uint32_t>(*i) +
140 static_cast<uint32_t>(*j));
141 CHECK_EQ(expected, r.Call(*i, *j));
142 }
143 }
144}
145
Ben Murdochc5610432016-08-08 18:44:38 +0100146WASM_EXEC_TEST(Float32Add) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100147 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000148 // int(11.5f + 44.5f)
149 BUILD(r,
150 WASM_I32_SCONVERT_F32(WASM_F32_ADD(WASM_F32(11.5f), WASM_F32(44.5f))));
151 CHECK_EQ(56, r.Call());
152}
153
Ben Murdochc5610432016-08-08 18:44:38 +0100154WASM_EXEC_TEST(Float64Add) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100155 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000156 // return int(13.5d + 43.5d)
157 BUILD(r, WASM_I32_SCONVERT_F64(WASM_F64_ADD(WASM_F64(13.5), WASM_F64(43.5))));
158 CHECK_EQ(57, r.Call());
159}
160
Ben Murdoch61f157c2016-09-16 13:49:30 +0100161void TestInt32Binop(WasmExecutionMode execution_mode, WasmOpcode opcode,
162 int32_t expected, int32_t a, int32_t b) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000163 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100164 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000165 // K op K
Ben Murdochda12d292016-06-02 14:46:10 +0100166 BUILD(r, WASM_BINOP(opcode, WASM_I32V(a), WASM_I32V(b)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000167 CHECK_EQ(expected, r.Call());
168 }
169 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100170 WasmRunner<int32_t> r(execution_mode, MachineType::Int32(),
171 MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000172 // a op b
173 BUILD(r, WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
174 CHECK_EQ(expected, r.Call(a, b));
175 }
176}
177
Ben Murdochc5610432016-08-08 18:44:38 +0100178WASM_EXEC_TEST(Int32Binops) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100179 TestInt32Binop(execution_mode, kExprI32Add, 88888888, 33333333, 55555555);
180 TestInt32Binop(execution_mode, kExprI32Sub, -1111111, 7777777, 8888888);
181 TestInt32Binop(execution_mode, kExprI32Mul, 65130756, 88734, 734);
182 TestInt32Binop(execution_mode, kExprI32DivS, -66, -4777344, 72384);
183 TestInt32Binop(execution_mode, kExprI32DivU, 805306368, 0xF0000000, 5);
184 TestInt32Binop(execution_mode, kExprI32RemS, -3, -3003, 1000);
185 TestInt32Binop(execution_mode, kExprI32RemU, 4, 4004, 1000);
186 TestInt32Binop(execution_mode, kExprI32And, 0xEE, 0xFFEE, 0xFF0000FF);
187 TestInt32Binop(execution_mode, kExprI32Ior, 0xF0FF00FF, 0xF0F000EE,
188 0x000F0011);
189 TestInt32Binop(execution_mode, kExprI32Xor, 0xABCDEF01, 0xABCDEFFF, 0xFE);
190 TestInt32Binop(execution_mode, kExprI32Shl, 0xA0000000, 0xA, 28);
191 TestInt32Binop(execution_mode, kExprI32ShrU, 0x07000010, 0x70000100, 4);
192 TestInt32Binop(execution_mode, kExprI32ShrS, 0xFF000000, 0x80000000, 7);
193 TestInt32Binop(execution_mode, kExprI32Ror, 0x01000000, 0x80000000, 7);
194 TestInt32Binop(execution_mode, kExprI32Ror, 0x01000000, 0x80000000, 39);
195 TestInt32Binop(execution_mode, kExprI32Rol, 0x00000040, 0x80000000, 7);
196 TestInt32Binop(execution_mode, kExprI32Rol, 0x00000040, 0x80000000, 39);
197 TestInt32Binop(execution_mode, kExprI32Eq, 1, -99, -99);
198 TestInt32Binop(execution_mode, kExprI32Ne, 0, -97, -97);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000199
Ben Murdoch61f157c2016-09-16 13:49:30 +0100200 TestInt32Binop(execution_mode, kExprI32LtS, 1, -4, 4);
201 TestInt32Binop(execution_mode, kExprI32LeS, 0, -2, -3);
202 TestInt32Binop(execution_mode, kExprI32LtU, 1, 0, -6);
203 TestInt32Binop(execution_mode, kExprI32LeU, 1, 98978, 0xF0000000);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000204
Ben Murdoch61f157c2016-09-16 13:49:30 +0100205 TestInt32Binop(execution_mode, kExprI32GtS, 1, 4, -4);
206 TestInt32Binop(execution_mode, kExprI32GeS, 0, -3, -2);
207 TestInt32Binop(execution_mode, kExprI32GtU, 1, -6, 0);
208 TestInt32Binop(execution_mode, kExprI32GeU, 1, 0xF0000000, 98978);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000209}
210
Ben Murdoch61f157c2016-09-16 13:49:30 +0100211void TestInt32Unop(WasmExecutionMode execution_mode, WasmOpcode opcode,
212 int32_t expected, int32_t a) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000213 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100214 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000215 // return op K
Ben Murdochda12d292016-06-02 14:46:10 +0100216 BUILD(r, WASM_UNOP(opcode, WASM_I32V(a)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000217 CHECK_EQ(expected, r.Call());
218 }
219 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100220 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000221 // return op a
222 BUILD(r, WASM_UNOP(opcode, WASM_GET_LOCAL(0)));
223 CHECK_EQ(expected, r.Call(a));
224 }
225}
226
Ben Murdochc5610432016-08-08 18:44:38 +0100227WASM_EXEC_TEST(Int32Clz) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100228 TestInt32Unop(execution_mode, kExprI32Clz, 0, 0x80001000);
229 TestInt32Unop(execution_mode, kExprI32Clz, 1, 0x40000500);
230 TestInt32Unop(execution_mode, kExprI32Clz, 2, 0x20000300);
231 TestInt32Unop(execution_mode, kExprI32Clz, 3, 0x10000003);
232 TestInt32Unop(execution_mode, kExprI32Clz, 4, 0x08050000);
233 TestInt32Unop(execution_mode, kExprI32Clz, 5, 0x04006000);
234 TestInt32Unop(execution_mode, kExprI32Clz, 6, 0x02000000);
235 TestInt32Unop(execution_mode, kExprI32Clz, 7, 0x010000a0);
236 TestInt32Unop(execution_mode, kExprI32Clz, 8, 0x00800c00);
237 TestInt32Unop(execution_mode, kExprI32Clz, 9, 0x00400000);
238 TestInt32Unop(execution_mode, kExprI32Clz, 10, 0x0020000d);
239 TestInt32Unop(execution_mode, kExprI32Clz, 11, 0x00100f00);
240 TestInt32Unop(execution_mode, kExprI32Clz, 12, 0x00080000);
241 TestInt32Unop(execution_mode, kExprI32Clz, 13, 0x00041000);
242 TestInt32Unop(execution_mode, kExprI32Clz, 14, 0x00020020);
243 TestInt32Unop(execution_mode, kExprI32Clz, 15, 0x00010300);
244 TestInt32Unop(execution_mode, kExprI32Clz, 16, 0x00008040);
245 TestInt32Unop(execution_mode, kExprI32Clz, 17, 0x00004005);
246 TestInt32Unop(execution_mode, kExprI32Clz, 18, 0x00002050);
247 TestInt32Unop(execution_mode, kExprI32Clz, 19, 0x00001700);
248 TestInt32Unop(execution_mode, kExprI32Clz, 20, 0x00000870);
249 TestInt32Unop(execution_mode, kExprI32Clz, 21, 0x00000405);
250 TestInt32Unop(execution_mode, kExprI32Clz, 22, 0x00000203);
251 TestInt32Unop(execution_mode, kExprI32Clz, 23, 0x00000101);
252 TestInt32Unop(execution_mode, kExprI32Clz, 24, 0x00000089);
253 TestInt32Unop(execution_mode, kExprI32Clz, 25, 0x00000041);
254 TestInt32Unop(execution_mode, kExprI32Clz, 26, 0x00000022);
255 TestInt32Unop(execution_mode, kExprI32Clz, 27, 0x00000013);
256 TestInt32Unop(execution_mode, kExprI32Clz, 28, 0x00000008);
257 TestInt32Unop(execution_mode, kExprI32Clz, 29, 0x00000004);
258 TestInt32Unop(execution_mode, kExprI32Clz, 30, 0x00000002);
259 TestInt32Unop(execution_mode, kExprI32Clz, 31, 0x00000001);
260 TestInt32Unop(execution_mode, kExprI32Clz, 32, 0x00000000);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000261}
262
Ben Murdochc5610432016-08-08 18:44:38 +0100263WASM_EXEC_TEST(Int32Ctz) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100264 TestInt32Unop(execution_mode, kExprI32Ctz, 32, 0x00000000);
265 TestInt32Unop(execution_mode, kExprI32Ctz, 31, 0x80000000);
266 TestInt32Unop(execution_mode, kExprI32Ctz, 30, 0x40000000);
267 TestInt32Unop(execution_mode, kExprI32Ctz, 29, 0x20000000);
268 TestInt32Unop(execution_mode, kExprI32Ctz, 28, 0x10000000);
269 TestInt32Unop(execution_mode, kExprI32Ctz, 27, 0xa8000000);
270 TestInt32Unop(execution_mode, kExprI32Ctz, 26, 0xf4000000);
271 TestInt32Unop(execution_mode, kExprI32Ctz, 25, 0x62000000);
272 TestInt32Unop(execution_mode, kExprI32Ctz, 24, 0x91000000);
273 TestInt32Unop(execution_mode, kExprI32Ctz, 23, 0xcd800000);
274 TestInt32Unop(execution_mode, kExprI32Ctz, 22, 0x09400000);
275 TestInt32Unop(execution_mode, kExprI32Ctz, 21, 0xaf200000);
276 TestInt32Unop(execution_mode, kExprI32Ctz, 20, 0xac100000);
277 TestInt32Unop(execution_mode, kExprI32Ctz, 19, 0xe0b80000);
278 TestInt32Unop(execution_mode, kExprI32Ctz, 18, 0x9ce40000);
279 TestInt32Unop(execution_mode, kExprI32Ctz, 17, 0xc7920000);
280 TestInt32Unop(execution_mode, kExprI32Ctz, 16, 0xb8f10000);
281 TestInt32Unop(execution_mode, kExprI32Ctz, 15, 0x3b9f8000);
282 TestInt32Unop(execution_mode, kExprI32Ctz, 14, 0xdb4c4000);
283 TestInt32Unop(execution_mode, kExprI32Ctz, 13, 0xe9a32000);
284 TestInt32Unop(execution_mode, kExprI32Ctz, 12, 0xfca61000);
285 TestInt32Unop(execution_mode, kExprI32Ctz, 11, 0x6c8a7800);
286 TestInt32Unop(execution_mode, kExprI32Ctz, 10, 0x8ce5a400);
287 TestInt32Unop(execution_mode, kExprI32Ctz, 9, 0xcb7d0200);
288 TestInt32Unop(execution_mode, kExprI32Ctz, 8, 0xcb4dc100);
289 TestInt32Unop(execution_mode, kExprI32Ctz, 7, 0xdfbec580);
290 TestInt32Unop(execution_mode, kExprI32Ctz, 6, 0x27a9db40);
291 TestInt32Unop(execution_mode, kExprI32Ctz, 5, 0xde3bcb20);
292 TestInt32Unop(execution_mode, kExprI32Ctz, 4, 0xd7e8a610);
293 TestInt32Unop(execution_mode, kExprI32Ctz, 3, 0x9afdbc88);
294 TestInt32Unop(execution_mode, kExprI32Ctz, 2, 0x9afdbc84);
295 TestInt32Unop(execution_mode, kExprI32Ctz, 1, 0x9afdbc82);
296 TestInt32Unop(execution_mode, kExprI32Ctz, 0, 0x9afdbc81);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000297}
298
Ben Murdochc5610432016-08-08 18:44:38 +0100299WASM_EXEC_TEST(Int32Popcnt) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100300 TestInt32Unop(execution_mode, kExprI32Popcnt, 32, 0xffffffff);
301 TestInt32Unop(execution_mode, kExprI32Popcnt, 0, 0x00000000);
302 TestInt32Unop(execution_mode, kExprI32Popcnt, 1, 0x00008000);
303 TestInt32Unop(execution_mode, kExprI32Popcnt, 13, 0x12345678);
304 TestInt32Unop(execution_mode, kExprI32Popcnt, 19, 0xfedcba09);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000305}
306
Ben Murdochc5610432016-08-08 18:44:38 +0100307WASM_EXEC_TEST(I32Eqz) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100308 TestInt32Unop(execution_mode, kExprI32Eqz, 0, 1);
309 TestInt32Unop(execution_mode, kExprI32Eqz, 0, -1);
310 TestInt32Unop(execution_mode, kExprI32Eqz, 0, -827343);
311 TestInt32Unop(execution_mode, kExprI32Eqz, 0, 8888888);
312 TestInt32Unop(execution_mode, kExprI32Eqz, 1, 0);
Ben Murdochda12d292016-06-02 14:46:10 +0100313}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000314
Ben Murdochc5610432016-08-08 18:44:38 +0100315WASM_EXEC_TEST(I32Shl) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100316 WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(),
317 MachineType::Uint32());
Ben Murdochda12d292016-06-02 14:46:10 +0100318 BUILD(r, WASM_I32_SHL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
319
320 FOR_UINT32_INPUTS(i) {
321 FOR_UINT32_INPUTS(j) {
322 uint32_t expected = (*i) << (*j & 0x1f);
323 CHECK_EQ(expected, r.Call(*i, *j));
324 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000325 }
326}
327
Ben Murdochc5610432016-08-08 18:44:38 +0100328WASM_EXEC_TEST(I32Shr) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100329 WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(),
330 MachineType::Uint32());
Ben Murdochda12d292016-06-02 14:46:10 +0100331 BUILD(r, WASM_I32_SHR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000332
Ben Murdochda12d292016-06-02 14:46:10 +0100333 FOR_UINT32_INPUTS(i) {
334 FOR_UINT32_INPUTS(j) {
335 uint32_t expected = (*i) >> (*j & 0x1f);
336 CHECK_EQ(expected, r.Call(*i, *j));
337 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000338 }
339}
340
Ben Murdochc5610432016-08-08 18:44:38 +0100341WASM_EXEC_TEST(I32Sar) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100342 WasmRunner<int32_t> r(execution_mode, MachineType::Int32(),
343 MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +0100344 BUILD(r, WASM_I32_SAR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000345
Ben Murdochda12d292016-06-02 14:46:10 +0100346 FOR_INT32_INPUTS(i) {
347 FOR_INT32_INPUTS(j) {
348 int32_t expected = (*i) >> (*j & 0x1f);
349 CHECK_EQ(expected, r.Call(*i, *j));
350 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000351 }
352}
353
Ben Murdochc5610432016-08-08 18:44:38 +0100354WASM_EXEC_TEST(Int32DivS_trap) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100355 WasmRunner<int32_t> r(execution_mode, MachineType::Int32(),
356 MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000357 BUILD(r, WASM_I32_DIVS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
Ben Murdochda12d292016-06-02 14:46:10 +0100358 const int32_t kMin = std::numeric_limits<int32_t>::min();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000359 CHECK_EQ(0, r.Call(0, 100));
360 CHECK_TRAP(r.Call(100, 0));
361 CHECK_TRAP(r.Call(-1001, 0));
Ben Murdochda12d292016-06-02 14:46:10 +0100362 CHECK_TRAP(r.Call(kMin, -1));
363 CHECK_TRAP(r.Call(kMin, 0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000364}
365
Ben Murdochc5610432016-08-08 18:44:38 +0100366WASM_EXEC_TEST(Int32RemS_trap) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100367 WasmRunner<int32_t> r(execution_mode, MachineType::Int32(),
368 MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000369 BUILD(r, WASM_I32_REMS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
Ben Murdochda12d292016-06-02 14:46:10 +0100370 const int32_t kMin = std::numeric_limits<int32_t>::min();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000371 CHECK_EQ(33, r.Call(133, 100));
Ben Murdochda12d292016-06-02 14:46:10 +0100372 CHECK_EQ(0, r.Call(kMin, -1));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000373 CHECK_TRAP(r.Call(100, 0));
374 CHECK_TRAP(r.Call(-1001, 0));
Ben Murdochda12d292016-06-02 14:46:10 +0100375 CHECK_TRAP(r.Call(kMin, 0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000376}
377
Ben Murdochc5610432016-08-08 18:44:38 +0100378WASM_EXEC_TEST(Int32DivU_trap) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100379 WasmRunner<int32_t> r(execution_mode, MachineType::Int32(),
380 MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000381 BUILD(r, WASM_I32_DIVU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
Ben Murdochda12d292016-06-02 14:46:10 +0100382 const int32_t kMin = std::numeric_limits<int32_t>::min();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000383 CHECK_EQ(0, r.Call(0, 100));
Ben Murdochda12d292016-06-02 14:46:10 +0100384 CHECK_EQ(0, r.Call(kMin, -1));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000385 CHECK_TRAP(r.Call(100, 0));
386 CHECK_TRAP(r.Call(-1001, 0));
Ben Murdochda12d292016-06-02 14:46:10 +0100387 CHECK_TRAP(r.Call(kMin, 0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000388}
389
Ben Murdochc5610432016-08-08 18:44:38 +0100390WASM_EXEC_TEST(Int32RemU_trap) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100391 WasmRunner<int32_t> r(execution_mode, MachineType::Int32(),
392 MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000393 BUILD(r, WASM_I32_REMU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
394 CHECK_EQ(17, r.Call(217, 100));
Ben Murdochda12d292016-06-02 14:46:10 +0100395 const int32_t kMin = std::numeric_limits<int32_t>::min();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000396 CHECK_TRAP(r.Call(100, 0));
397 CHECK_TRAP(r.Call(-1001, 0));
Ben Murdochda12d292016-06-02 14:46:10 +0100398 CHECK_TRAP(r.Call(kMin, 0));
399 CHECK_EQ(kMin, r.Call(kMin, -1));
400}
401
Ben Murdochc5610432016-08-08 18:44:38 +0100402WASM_EXEC_TEST(Int32DivS_byzero_const) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100403 for (int8_t denom = -2; denom < 8; ++denom) {
404 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000405 BUILD(r, WASM_I32_DIVS(WASM_GET_LOCAL(0), WASM_I8(denom)));
Ben Murdoch61f157c2016-09-16 13:49:30 +0100406 for (int32_t val = -7; val < 8; ++val) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000407 if (denom == 0) {
408 CHECK_TRAP(r.Call(val));
409 } else {
410 CHECK_EQ(val / denom, r.Call(val));
411 }
412 }
413 }
414}
415
Ben Murdochc5610432016-08-08 18:44:38 +0100416WASM_EXEC_TEST(Int32DivU_byzero_const) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100417 for (uint32_t denom = 0xfffffffe; denom < 8; ++denom) {
418 WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32());
Ben Murdochda12d292016-06-02 14:46:10 +0100419 BUILD(r, WASM_I32_DIVU(WASM_GET_LOCAL(0), WASM_I32V_1(denom)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000420
Ben Murdoch61f157c2016-09-16 13:49:30 +0100421 for (uint32_t val = 0xfffffff0; val < 8; ++val) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000422 if (denom == 0) {
423 CHECK_TRAP(r.Call(val));
424 } else {
425 CHECK_EQ(val / denom, r.Call(val));
426 }
427 }
428 }
429}
430
Ben Murdochc5610432016-08-08 18:44:38 +0100431WASM_EXEC_TEST(Int32DivS_trap_effect) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100432 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000433 module.AddMemoryElems<int32_t>(8);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100434 WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000435
436 BUILD(r,
437 WASM_IF_ELSE(WASM_GET_LOCAL(0),
438 WASM_I32_DIVS(WASM_STORE_MEM(MachineType::Int8(),
439 WASM_ZERO, WASM_GET_LOCAL(0)),
440 WASM_GET_LOCAL(1)),
441 WASM_I32_DIVS(WASM_STORE_MEM(MachineType::Int8(),
442 WASM_ZERO, WASM_GET_LOCAL(0)),
443 WASM_GET_LOCAL(1))));
444 CHECK_EQ(0, r.Call(0, 100));
445 CHECK_TRAP(r.Call(8, 0));
446 CHECK_TRAP(r.Call(4, 0));
447 CHECK_TRAP(r.Call(0, 0));
448}
449
Ben Murdoch61f157c2016-09-16 13:49:30 +0100450void TestFloat32Binop(WasmExecutionMode execution_mode, WasmOpcode opcode,
451 int32_t expected, float a, float b) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000452 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100453 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000454 // return K op K
455 BUILD(r, WASM_BINOP(opcode, WASM_F32(a), WASM_F32(b)));
456 CHECK_EQ(expected, r.Call());
457 }
458 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100459 WasmRunner<int32_t> r(execution_mode, MachineType::Float32(),
460 MachineType::Float32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000461 // return a op b
462 BUILD(r, WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
463 CHECK_EQ(expected, r.Call(a, b));
464 }
465}
466
Ben Murdoch61f157c2016-09-16 13:49:30 +0100467void TestFloat32BinopWithConvert(WasmExecutionMode execution_mode,
468 WasmOpcode opcode, int32_t expected, float a,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000469 float b) {
470 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100471 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000472 // return int(K op K)
473 BUILD(r,
474 WASM_I32_SCONVERT_F32(WASM_BINOP(opcode, WASM_F32(a), WASM_F32(b))));
475 CHECK_EQ(expected, r.Call());
476 }
477 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100478 WasmRunner<int32_t> r(execution_mode, MachineType::Float32(),
479 MachineType::Float32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000480 // return int(a op b)
481 BUILD(r, WASM_I32_SCONVERT_F32(
482 WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))));
483 CHECK_EQ(expected, r.Call(a, b));
484 }
485}
486
Ben Murdoch61f157c2016-09-16 13:49:30 +0100487void TestFloat32UnopWithConvert(WasmExecutionMode execution_mode,
488 WasmOpcode opcode, int32_t expected, float a) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000489 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100490 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000491 // return int(op(K))
492 BUILD(r, WASM_I32_SCONVERT_F32(WASM_UNOP(opcode, WASM_F32(a))));
493 CHECK_EQ(expected, r.Call());
494 }
495 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100496 WasmRunner<int32_t> r(execution_mode, MachineType::Float32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000497 // return int(op(a))
498 BUILD(r, WASM_I32_SCONVERT_F32(WASM_UNOP(opcode, WASM_GET_LOCAL(0))));
499 CHECK_EQ(expected, r.Call(a));
500 }
501}
502
Ben Murdoch61f157c2016-09-16 13:49:30 +0100503void TestFloat64Binop(WasmExecutionMode execution_mode, WasmOpcode opcode,
504 int32_t expected, double a, double b) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000505 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100506 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000507 // return K op K
508 BUILD(r, WASM_BINOP(opcode, WASM_F64(a), WASM_F64(b)));
509 CHECK_EQ(expected, r.Call());
510 }
511 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100512 WasmRunner<int32_t> r(execution_mode, MachineType::Float64(),
513 MachineType::Float64());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000514 // return a op b
515 BUILD(r, WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
516 CHECK_EQ(expected, r.Call(a, b));
517 }
518}
519
Ben Murdoch61f157c2016-09-16 13:49:30 +0100520void TestFloat64BinopWithConvert(WasmExecutionMode execution_mode,
521 WasmOpcode opcode, int32_t expected, double a,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000522 double b) {
523 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100524 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000525 // return int(K op K)
526 BUILD(r,
527 WASM_I32_SCONVERT_F64(WASM_BINOP(opcode, WASM_F64(a), WASM_F64(b))));
528 CHECK_EQ(expected, r.Call());
529 }
530 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100531 WasmRunner<int32_t> r(execution_mode, MachineType::Float64(),
532 MachineType::Float64());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000533 BUILD(r, WASM_I32_SCONVERT_F64(
534 WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))));
535 CHECK_EQ(expected, r.Call(a, b));
536 }
537}
538
Ben Murdoch61f157c2016-09-16 13:49:30 +0100539void TestFloat64UnopWithConvert(WasmExecutionMode execution_mode,
540 WasmOpcode opcode, int32_t expected, double a) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000541 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100542 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000543 // return int(op(K))
544 BUILD(r, WASM_I32_SCONVERT_F64(WASM_UNOP(opcode, WASM_F64(a))));
545 CHECK_EQ(expected, r.Call());
546 }
547 {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100548 WasmRunner<int32_t> r(execution_mode, MachineType::Float64());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000549 // return int(op(a))
550 BUILD(r, WASM_I32_SCONVERT_F64(WASM_UNOP(opcode, WASM_GET_LOCAL(0))));
551 CHECK_EQ(expected, r.Call(a));
552 }
553}
554
Ben Murdochc5610432016-08-08 18:44:38 +0100555WASM_EXEC_TEST(Float32Binops) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100556 TestFloat32Binop(execution_mode, kExprF32Eq, 1, 8.125f, 8.125f);
557 TestFloat32Binop(execution_mode, kExprF32Ne, 1, 8.125f, 8.127f);
558 TestFloat32Binop(execution_mode, kExprF32Lt, 1, -9.5f, -9.0f);
559 TestFloat32Binop(execution_mode, kExprF32Le, 1, -1111.0f, -1111.0f);
560 TestFloat32Binop(execution_mode, kExprF32Gt, 1, -9.0f, -9.5f);
561 TestFloat32Binop(execution_mode, kExprF32Ge, 1, -1111.0f, -1111.0f);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000562
Ben Murdoch61f157c2016-09-16 13:49:30 +0100563 TestFloat32BinopWithConvert(execution_mode, kExprF32Add, 10, 3.5f, 6.5f);
564 TestFloat32BinopWithConvert(execution_mode, kExprF32Sub, 2, 44.5f, 42.5f);
565 TestFloat32BinopWithConvert(execution_mode, kExprF32Mul, -66, -132.1f, 0.5f);
566 TestFloat32BinopWithConvert(execution_mode, kExprF32Div, 11, 22.1f, 2.0f);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000567}
568
Ben Murdochc5610432016-08-08 18:44:38 +0100569WASM_EXEC_TEST(Float32Unops) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100570 TestFloat32UnopWithConvert(execution_mode, kExprF32Abs, 8, 8.125f);
571 TestFloat32UnopWithConvert(execution_mode, kExprF32Abs, 9, -9.125f);
572 TestFloat32UnopWithConvert(execution_mode, kExprF32Neg, -213, 213.125f);
573 TestFloat32UnopWithConvert(execution_mode, kExprF32Sqrt, 12, 144.4f);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000574}
575
Ben Murdochc5610432016-08-08 18:44:38 +0100576WASM_EXEC_TEST(Float64Binops) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100577 TestFloat64Binop(execution_mode, kExprF64Eq, 1, 16.25, 16.25);
578 TestFloat64Binop(execution_mode, kExprF64Ne, 1, 16.25, 16.15);
579 TestFloat64Binop(execution_mode, kExprF64Lt, 1, -32.4, 11.7);
580 TestFloat64Binop(execution_mode, kExprF64Le, 1, -88.9, -88.9);
581 TestFloat64Binop(execution_mode, kExprF64Gt, 1, 11.7, -32.4);
582 TestFloat64Binop(execution_mode, kExprF64Ge, 1, -88.9, -88.9);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000583
Ben Murdoch61f157c2016-09-16 13:49:30 +0100584 TestFloat64BinopWithConvert(execution_mode, kExprF64Add, 100, 43.5, 56.5);
585 TestFloat64BinopWithConvert(execution_mode, kExprF64Sub, 200, 12200.1,
586 12000.1);
587 TestFloat64BinopWithConvert(execution_mode, kExprF64Mul, -33, 134, -0.25);
588 TestFloat64BinopWithConvert(execution_mode, kExprF64Div, -1111, -2222.3, 2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000589}
590
Ben Murdochc5610432016-08-08 18:44:38 +0100591WASM_EXEC_TEST(Float64Unops) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100592 TestFloat64UnopWithConvert(execution_mode, kExprF64Abs, 108, 108.125);
593 TestFloat64UnopWithConvert(execution_mode, kExprF64Abs, 209, -209.125);
594 TestFloat64UnopWithConvert(execution_mode, kExprF64Neg, -209, 209.125);
595 TestFloat64UnopWithConvert(execution_mode, kExprF64Sqrt, 13, 169.4);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000596}
597
Ben Murdochc5610432016-08-08 18:44:38 +0100598WASM_EXEC_TEST(Float32Neg) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100599 WasmRunner<float> r(execution_mode, MachineType::Float32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000600 BUILD(r, WASM_F32_NEG(WASM_GET_LOCAL(0)));
601
602 FOR_FLOAT32_INPUTS(i) {
603 CHECK_EQ(0x80000000,
604 bit_cast<uint32_t>(*i) ^ bit_cast<uint32_t>(r.Call(*i)));
605 }
606}
607
Ben Murdochc5610432016-08-08 18:44:38 +0100608WASM_EXEC_TEST(Float32SubMinusZero) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100609 WasmRunner<float> r(execution_mode, MachineType::Float32());
Ben Murdochc5610432016-08-08 18:44:38 +0100610 BUILD(r, WASM_F32_SUB(WASM_F32(-0.0), WASM_GET_LOCAL(0)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000611
Ben Murdoch61f157c2016-09-16 13:49:30 +0100612 uint32_t sNanValue =
613 bit_cast<uint32_t>(std::numeric_limits<float>::signaling_NaN());
614 uint32_t qNanValue =
615 bit_cast<uint32_t>(std::numeric_limits<float>::quiet_NaN());
616 uint32_t payload = 0x00200000;
617
618 uint32_t expected = (qNanValue & 0xffc00000) | payload;
619 uint32_t operand = (sNanValue & 0xffc00000) | payload;
620 CHECK_EQ(expected, bit_cast<uint32_t>(r.Call(bit_cast<float>(operand))));
621
622 // Change the sign of the NaN.
623 expected |= 0x80000000;
624 operand |= 0x80000000;
625 CHECK_EQ(expected, bit_cast<uint32_t>(r.Call(bit_cast<float>(operand))));
Ben Murdochc5610432016-08-08 18:44:38 +0100626}
627
628WASM_EXEC_TEST(Float64SubMinusZero) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100629 WasmRunner<double> r(execution_mode, MachineType::Float64());
Ben Murdochc5610432016-08-08 18:44:38 +0100630 BUILD(r, WASM_F64_SUB(WASM_F64(-0.0), WASM_GET_LOCAL(0)));
631
Ben Murdoch61f157c2016-09-16 13:49:30 +0100632 uint64_t sNanValue =
633 bit_cast<uint64_t>(std::numeric_limits<double>::signaling_NaN());
634 uint64_t qNanValue =
635 bit_cast<uint64_t>(std::numeric_limits<double>::quiet_NaN());
636 uint64_t payload = 0x0000123456789abc;
637
638 uint64_t expected = (qNanValue & 0xfff8000000000000) | payload;
639 uint64_t operand = (sNanValue & 0xfff8000000000000) | payload;
640 CHECK_EQ(expected, bit_cast<uint64_t>(r.Call(bit_cast<double>(operand))));
641
642 // Change the sign of the NaN.
643 expected |= 0x8000000000000000;
644 operand |= 0x8000000000000000;
645 CHECK_EQ(expected, bit_cast<uint64_t>(r.Call(bit_cast<double>(operand))));
Ben Murdochc5610432016-08-08 18:44:38 +0100646}
647
648WASM_EXEC_TEST(Float64Neg) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100649 WasmRunner<double> r(execution_mode, MachineType::Float64());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000650 BUILD(r, WASM_F64_NEG(WASM_GET_LOCAL(0)));
651
652 FOR_FLOAT64_INPUTS(i) {
653 CHECK_EQ(0x8000000000000000,
654 bit_cast<uint64_t>(*i) ^ bit_cast<uint64_t>(r.Call(*i)));
655 }
656}
657
Ben Murdochc5610432016-08-08 18:44:38 +0100658WASM_EXEC_TEST(IfElse_P) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100659 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000660 // if (p0) return 11; else return 22;
661 BUILD(r, WASM_IF_ELSE(WASM_GET_LOCAL(0), // --
662 WASM_I8(11), // --
663 WASM_I8(22))); // --
664 FOR_INT32_INPUTS(i) {
665 int32_t expected = *i ? 11 : 22;
666 CHECK_EQ(expected, r.Call(*i));
667 }
668}
669
Ben Murdochc5610432016-08-08 18:44:38 +0100670WASM_EXEC_TEST(If_empty1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100671 WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(),
672 MachineType::Uint32());
Ben Murdochc5610432016-08-08 18:44:38 +0100673 BUILD(r, WASM_GET_LOCAL(0), kExprIf, kExprEnd, WASM_GET_LOCAL(1));
674 FOR_UINT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i - 9, *i)); }
675}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000676
Ben Murdochc5610432016-08-08 18:44:38 +0100677WASM_EXEC_TEST(IfElse_empty1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100678 WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(),
679 MachineType::Uint32());
Ben Murdochc5610432016-08-08 18:44:38 +0100680 BUILD(r, WASM_GET_LOCAL(0), kExprIf, kExprElse, kExprEnd, WASM_GET_LOCAL(1));
681 FOR_UINT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i - 8, *i)); }
682}
683
684WASM_EXEC_TEST(IfElse_empty2) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100685 WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(),
686 MachineType::Uint32());
Ben Murdochc5610432016-08-08 18:44:38 +0100687 BUILD(r, WASM_GET_LOCAL(0), kExprIf, WASM_ZERO, kExprElse, kExprEnd,
688 WASM_GET_LOCAL(1));
689 FOR_UINT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i - 7, *i)); }
690}
691
692WASM_EXEC_TEST(IfElse_empty3) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100693 WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(),
694 MachineType::Uint32());
Ben Murdochc5610432016-08-08 18:44:38 +0100695 BUILD(r, WASM_GET_LOCAL(0), kExprIf, kExprElse, WASM_ZERO, kExprEnd,
696 WASM_GET_LOCAL(1));
697 FOR_UINT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i - 6, *i)); }
698}
699
700WASM_EXEC_TEST(If_chain) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100701 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +0100702 // if (p0) 13; if (p0) 14; 15
703 BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_I8(13)),
704 WASM_IF(WASM_GET_LOCAL(0), WASM_I8(14)), WASM_I8(15));
705 FOR_INT32_INPUTS(i) { CHECK_EQ(15, r.Call(*i)); }
706}
707
708WASM_EXEC_TEST(If_chain_set) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100709 WasmRunner<int32_t> r(execution_mode, MachineType::Int32(),
710 MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +0100711 // if (p0) p1 = 73; if (p0) p1 = 74; p1
712 BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(1, WASM_I8(73))),
713 WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(1, WASM_I8(74))),
714 WASM_GET_LOCAL(1));
715 FOR_INT32_INPUTS(i) {
716 int32_t expected = *i ? 74 : *i;
717 CHECK_EQ(expected, r.Call(*i, *i));
718 }
719}
720
721WASM_EXEC_TEST(IfElse_Unreachable1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100722 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000723 // if (0) unreachable; else return 22;
724 BUILD(r, WASM_IF_ELSE(WASM_ZERO, // --
725 WASM_UNREACHABLE, // --
726 WASM_I8(27))); // --
727 CHECK_EQ(27, r.Call());
728}
729
Ben Murdochc5610432016-08-08 18:44:38 +0100730WASM_EXEC_TEST(Return12) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100731 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000732
Ben Murdochda12d292016-06-02 14:46:10 +0100733 BUILD(r, RET_I8(12));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000734 CHECK_EQ(12, r.Call());
735}
736
Ben Murdochc5610432016-08-08 18:44:38 +0100737WASM_EXEC_TEST(Return17) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100738 WasmRunner<int32_t> r(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000739
Ben Murdochda12d292016-06-02 14:46:10 +0100740 BUILD(r, B1(RET_I8(17)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000741 CHECK_EQ(17, r.Call());
742}
743
Ben Murdochc5610432016-08-08 18:44:38 +0100744WASM_EXEC_TEST(Return_I32) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100745 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000746
Ben Murdochda12d292016-06-02 14:46:10 +0100747 BUILD(r, RET(WASM_GET_LOCAL(0)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000748
749 FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); }
750}
751
Ben Murdochc5610432016-08-08 18:44:38 +0100752WASM_EXEC_TEST(Return_F32) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100753 WasmRunner<float> r(execution_mode, MachineType::Float32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000754
Ben Murdochda12d292016-06-02 14:46:10 +0100755 BUILD(r, RET(WASM_GET_LOCAL(0)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000756
757 FOR_FLOAT32_INPUTS(i) {
758 float expect = *i;
759 float result = r.Call(expect);
760 if (std::isnan(expect)) {
761 CHECK(std::isnan(result));
762 } else {
763 CHECK_EQ(expect, result);
764 }
765 }
766}
767
Ben Murdochc5610432016-08-08 18:44:38 +0100768WASM_EXEC_TEST(Return_F64) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100769 WasmRunner<double> r(execution_mode, MachineType::Float64());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000770
Ben Murdochda12d292016-06-02 14:46:10 +0100771 BUILD(r, RET(WASM_GET_LOCAL(0)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000772
773 FOR_FLOAT64_INPUTS(i) {
774 double expect = *i;
775 double result = r.Call(expect);
776 if (std::isnan(expect)) {
777 CHECK(std::isnan(result));
778 } else {
779 CHECK_EQ(expect, result);
780 }
781 }
782}
783
Ben Murdochc5610432016-08-08 18:44:38 +0100784WASM_EXEC_TEST(Select) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100785 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100786 // return select(11, 22, a);
787 BUILD(r, WASM_SELECT(WASM_I8(11), WASM_I8(22), WASM_GET_LOCAL(0)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000788 FOR_INT32_INPUTS(i) {
789 int32_t expected = *i ? 11 : 22;
790 CHECK_EQ(expected, r.Call(*i));
791 }
792}
793
Ben Murdochc5610432016-08-08 18:44:38 +0100794WASM_EXEC_TEST(Select_strict1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100795 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100796 // select(a=0, a=1, a=2); return a
Ben Murdochda12d292016-06-02 14:46:10 +0100797 BUILD(r, B2(WASM_SELECT(WASM_SET_LOCAL(0, WASM_I8(0)),
798 WASM_SET_LOCAL(0, WASM_I8(1)),
799 WASM_SET_LOCAL(0, WASM_I8(2))),
800 WASM_GET_LOCAL(0)));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100801 FOR_INT32_INPUTS(i) { CHECK_EQ(2, r.Call(*i)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000802}
803
Ben Murdochc5610432016-08-08 18:44:38 +0100804WASM_EXEC_TEST(Select_strict2) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100805 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +0100806 r.AllocateLocal(kAstI32);
807 r.AllocateLocal(kAstI32);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100808 // select(b=5, c=6, a)
809 BUILD(r, WASM_SELECT(WASM_SET_LOCAL(1, WASM_I8(5)),
810 WASM_SET_LOCAL(2, WASM_I8(6)), WASM_GET_LOCAL(0)));
811 FOR_INT32_INPUTS(i) {
812 int32_t expected = *i ? 5 : 6;
813 CHECK_EQ(expected, r.Call(*i));
814 }
815}
816
Ben Murdochc5610432016-08-08 18:44:38 +0100817WASM_EXEC_TEST(Select_strict3) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100818 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +0100819 r.AllocateLocal(kAstI32);
820 r.AllocateLocal(kAstI32);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100821 // select(b=5, c=6, a=b)
822 BUILD(r, WASM_SELECT(WASM_SET_LOCAL(1, WASM_I8(5)),
823 WASM_SET_LOCAL(2, WASM_I8(6)),
824 WASM_SET_LOCAL(0, WASM_GET_LOCAL(1))));
825 FOR_INT32_INPUTS(i) {
826 int32_t expected = 5;
827 CHECK_EQ(expected, r.Call(*i));
828 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000829}
830
Ben Murdochc5610432016-08-08 18:44:38 +0100831WASM_EXEC_TEST(BrIf_strict) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100832 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +0100833 BUILD(
834 r,
835 B2(B1(WASM_BRV_IF(0, WASM_GET_LOCAL(0), WASM_SET_LOCAL(0, WASM_I8(99)))),
836 WASM_GET_LOCAL(0)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000837
838 FOR_INT32_INPUTS(i) { CHECK_EQ(99, r.Call(*i)); }
839}
840
Ben Murdochc5610432016-08-08 18:44:38 +0100841WASM_EXEC_TEST(BrTable0a) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100842 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +0100843 BUILD(r,
844 B2(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(0))), WASM_I8(91)));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100845 FOR_INT32_INPUTS(i) { CHECK_EQ(91, r.Call(*i)); }
846}
847
Ben Murdochc5610432016-08-08 18:44:38 +0100848WASM_EXEC_TEST(BrTable0b) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100849 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +0100850 BUILD(r,
851 B2(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 1, BR_TARGET(0), BR_TARGET(0))),
852 WASM_I8(92)));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100853 FOR_INT32_INPUTS(i) { CHECK_EQ(92, r.Call(*i)); }
854}
855
Ben Murdochc5610432016-08-08 18:44:38 +0100856WASM_EXEC_TEST(BrTable0c) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100857 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +0100858 BUILD(
859 r,
860 B2(B2(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 1, BR_TARGET(0), BR_TARGET(1))),
861 RET_I8(76)),
862 WASM_I8(77)));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100863 FOR_INT32_INPUTS(i) {
864 int32_t expected = *i == 0 ? 76 : 77;
865 CHECK_EQ(expected, r.Call(*i));
866 }
867}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000868
Ben Murdochc5610432016-08-08 18:44:38 +0100869WASM_EXEC_TEST(BrTable1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100870 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +0100871 BUILD(r, B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(0))), RET_I8(93));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000872 FOR_INT32_INPUTS(i) { CHECK_EQ(93, r.Call(*i)); }
873}
874
Ben Murdochc5610432016-08-08 18:44:38 +0100875WASM_EXEC_TEST(BrTable_loop) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100876 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +0100877 BUILD(r,
878 B2(WASM_LOOP(1, WASM_BR_TABLE(WASM_INC_LOCAL_BY(0, 1), 2, BR_TARGET(2),
879 BR_TARGET(1), BR_TARGET(0))),
880 RET_I8(99)),
881 WASM_I8(98));
882 CHECK_EQ(99, r.Call(0));
883 CHECK_EQ(98, r.Call(-1));
884 CHECK_EQ(98, r.Call(-2));
885 CHECK_EQ(98, r.Call(-3));
886 CHECK_EQ(98, r.Call(-100));
887}
888
Ben Murdochc5610432016-08-08 18:44:38 +0100889WASM_EXEC_TEST(BrTable_br) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100890 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +0100891 BUILD(r,
892 B2(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 1, BR_TARGET(1), BR_TARGET(0))),
893 RET_I8(91)),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000894 WASM_I8(99));
895 CHECK_EQ(99, r.Call(0));
896 CHECK_EQ(91, r.Call(1));
897 CHECK_EQ(91, r.Call(2));
898 CHECK_EQ(91, r.Call(3));
899}
900
Ben Murdochc5610432016-08-08 18:44:38 +0100901WASM_EXEC_TEST(BrTable_br2) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100902 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +0100903
904 BUILD(r, B2(B2(B2(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 3, BR_TARGET(1),
905 BR_TARGET(2), BR_TARGET(3), BR_TARGET(0))),
906 RET_I8(85)),
907 RET_I8(86)),
908 RET_I8(87)),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000909 WASM_I8(88));
910 CHECK_EQ(86, r.Call(0));
911 CHECK_EQ(87, r.Call(1));
912 CHECK_EQ(88, r.Call(2));
913 CHECK_EQ(85, r.Call(3));
914 CHECK_EQ(85, r.Call(4));
915 CHECK_EQ(85, r.Call(5));
916}
917
Ben Murdochc5610432016-08-08 18:44:38 +0100918WASM_EXEC_TEST(BrTable4) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100919 for (int i = 0; i < 4; ++i) {
920 for (int t = 0; t < 4; ++t) {
Ben Murdochda12d292016-06-02 14:46:10 +0100921 uint32_t cases[] = {0, 1, 2, 3};
922 cases[i] = t;
923 byte code[] = {B2(B2(B2(B2(B1(WASM_BR_TABLE(
924 WASM_GET_LOCAL(0), 3, BR_TARGET(cases[0]),
925 BR_TARGET(cases[1]), BR_TARGET(cases[2]),
926 BR_TARGET(cases[3]))),
927 RET_I8(70)),
928 RET_I8(71)),
929 RET_I8(72)),
930 RET_I8(73)),
931 WASM_I8(75)};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000932
Ben Murdoch61f157c2016-09-16 13:49:30 +0100933 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +0100934 r.Build(code, code + arraysize(code));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000935
Ben Murdoch61f157c2016-09-16 13:49:30 +0100936 for (int x = -3; x < 50; ++x) {
Ben Murdochda12d292016-06-02 14:46:10 +0100937 int index = (x > 3 || x < 0) ? 3 : x;
938 int32_t expected = 70 + cases[index];
939 CHECK_EQ(expected, r.Call(x));
940 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000941 }
942 }
943}
944
Ben Murdochc5610432016-08-08 18:44:38 +0100945WASM_EXEC_TEST(BrTable4x4) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100946 for (byte a = 0; a < 4; ++a) {
947 for (byte b = 0; b < 4; ++b) {
948 for (byte c = 0; c < 4; ++c) {
949 for (byte d = 0; d < 4; ++d) {
950 for (int i = 0; i < 4; ++i) {
Ben Murdochda12d292016-06-02 14:46:10 +0100951 uint32_t cases[] = {a, b, c, d};
952 byte code[] = {
953 B2(B2(B2(B2(B1(WASM_BR_TABLE(
954 WASM_GET_LOCAL(0), 3, BR_TARGET(cases[0]),
955 BR_TARGET(cases[1]), BR_TARGET(cases[2]),
956 BR_TARGET(cases[3]))),
957 RET_I8(50)),
958 RET_I8(51)),
959 RET_I8(52)),
960 RET_I8(53)),
961 WASM_I8(55)};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000962
Ben Murdoch61f157c2016-09-16 13:49:30 +0100963 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +0100964 r.Build(code, code + arraysize(code));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000965
Ben Murdoch61f157c2016-09-16 13:49:30 +0100966 for (int x = -6; x < 47; ++x) {
Ben Murdochda12d292016-06-02 14:46:10 +0100967 int index = (x > 3 || x < 0) ? 3 : x;
968 int32_t expected = 50 + cases[index];
969 CHECK_EQ(expected, r.Call(x));
970 }
971 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000972 }
973 }
974 }
975 }
976}
977
Ben Murdochc5610432016-08-08 18:44:38 +0100978WASM_EXEC_TEST(BrTable4_fallthru) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000979 byte code[] = {
Ben Murdochda12d292016-06-02 14:46:10 +0100980 B2(B2(B2(B2(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 3, BR_TARGET(0),
981 BR_TARGET(1), BR_TARGET(2), BR_TARGET(3))),
982 WASM_INC_LOCAL_BY(1, 1)),
983 WASM_INC_LOCAL_BY(1, 2)),
984 WASM_INC_LOCAL_BY(1, 4)),
985 WASM_INC_LOCAL_BY(1, 8)),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000986 WASM_GET_LOCAL(1)};
987
Ben Murdoch61f157c2016-09-16 13:49:30 +0100988 WasmRunner<int32_t> r(execution_mode, MachineType::Int32(),
989 MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000990 r.Build(code, code + arraysize(code));
991
992 CHECK_EQ(15, r.Call(0, 0));
993 CHECK_EQ(14, r.Call(1, 0));
994 CHECK_EQ(12, r.Call(2, 0));
995 CHECK_EQ(8, r.Call(3, 0));
996 CHECK_EQ(8, r.Call(4, 0));
997
998 CHECK_EQ(115, r.Call(0, 100));
999 CHECK_EQ(114, r.Call(1, 100));
1000 CHECK_EQ(112, r.Call(2, 100));
1001 CHECK_EQ(108, r.Call(3, 100));
1002 CHECK_EQ(108, r.Call(4, 100));
1003}
1004
Ben Murdochc5610432016-08-08 18:44:38 +01001005WASM_EXEC_TEST(F32ReinterpretI32) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001006 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001007 int32_t* memory = module.AddMemoryElems<int32_t>(8);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001008 WasmRunner<int32_t> r(&module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001009
1010 BUILD(r, WASM_I32_REINTERPRET_F32(
1011 WASM_LOAD_MEM(MachineType::Float32(), WASM_ZERO)));
1012
1013 FOR_INT32_INPUTS(i) {
1014 int32_t expected = *i;
1015 memory[0] = expected;
1016 CHECK_EQ(expected, r.Call());
1017 }
1018}
1019
Ben Murdochc5610432016-08-08 18:44:38 +01001020WASM_EXEC_TEST(I32ReinterpretF32) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001021 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001022 int32_t* memory = module.AddMemoryElems<int32_t>(8);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001023 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001024
1025 BUILD(r, WASM_BLOCK(
1026 2, WASM_STORE_MEM(MachineType::Float32(), WASM_ZERO,
1027 WASM_F32_REINTERPRET_I32(WASM_GET_LOCAL(0))),
1028 WASM_I8(107)));
1029
1030 FOR_INT32_INPUTS(i) {
1031 int32_t expected = *i;
1032 CHECK_EQ(107, r.Call(expected));
1033 CHECK_EQ(expected, memory[0]);
1034 }
1035}
1036
Ben Murdochc5610432016-08-08 18:44:38 +01001037WASM_EXEC_TEST(ReturnStore) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001038 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001039 int32_t* memory = module.AddMemoryElems<int32_t>(8);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001040 WasmRunner<int32_t> r(&module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001041
1042 BUILD(r, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO,
1043 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)));
1044
1045 FOR_INT32_INPUTS(i) {
1046 int32_t expected = *i;
1047 memory[0] = expected;
1048 CHECK_EQ(expected, r.Call());
1049 }
1050}
1051
Ben Murdochc5610432016-08-08 18:44:38 +01001052WASM_EXEC_TEST(VoidReturn1) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001053 // We use a wrapper function because WasmRunner<void> does not exist.
1054
1055 // Build the test function.
1056 TestSignatures sigs;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001057 TestingModule module(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001058 WasmFunctionCompiler t(sigs.v_v(), &module);
1059 BUILD(t, kExprNop);
1060 uint32_t index = t.CompileAndAdd();
1061
1062 const int32_t kExpected = -414444;
1063 // Build the calling function.
Ben Murdochda12d292016-06-02 14:46:10 +01001064 WasmRunner<int32_t> r(&module);
1065 BUILD(r, B2(WASM_CALL_FUNCTION0(index), WASM_I32V_3(kExpected)));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001066
1067 int32_t result = r.Call();
1068 CHECK_EQ(kExpected, result);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001069}
1070
Ben Murdochc5610432016-08-08 18:44:38 +01001071WASM_EXEC_TEST(VoidReturn2) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001072 // We use a wrapper function because WasmRunner<void> does not exist.
1073 // Build the test function.
1074 TestSignatures sigs;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001075 TestingModule module(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001076 WasmFunctionCompiler t(sigs.v_v(), &module);
1077 BUILD(t, WASM_RETURN0);
1078 uint32_t index = t.CompileAndAdd();
1079
1080 const int32_t kExpected = -414444;
1081 // Build the calling function.
Ben Murdochda12d292016-06-02 14:46:10 +01001082 WasmRunner<int32_t> r(&module);
1083 BUILD(r, B2(WASM_CALL_FUNCTION0(index), WASM_I32V_3(kExpected)));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001084
1085 int32_t result = r.Call();
1086 CHECK_EQ(kExpected, result);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001087}
1088
Ben Murdochc5610432016-08-08 18:44:38 +01001089WASM_EXEC_TEST(Block_empty) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001090 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01001091 BUILD(r, kExprBlock, kExprEnd, WASM_GET_LOCAL(0));
1092 FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); }
1093}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001094
Ben Murdochc5610432016-08-08 18:44:38 +01001095WASM_EXEC_TEST(Block_empty_br1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001096 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01001097 BUILD(r, B1(WASM_BR(0)), WASM_GET_LOCAL(0));
1098 FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); }
1099}
1100
1101WASM_EXEC_TEST(Block_empty_brif1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001102 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01001103 BUILD(r, B1(WASM_BR_IF(0, WASM_ZERO)), WASM_GET_LOCAL(0));
1104 FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); }
1105}
1106
1107WASM_EXEC_TEST(Block_empty_brif2) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001108 WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(),
1109 MachineType::Uint32());
Ben Murdochc5610432016-08-08 18:44:38 +01001110 BUILD(r, B1(WASM_BR_IF(0, WASM_GET_LOCAL(1))), WASM_GET_LOCAL(0));
1111 FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i, *i + 1)); }
1112}
1113
1114WASM_EXEC_TEST(Block_br2) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001115 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01001116 BUILD(r, B1(WASM_BRV(0, WASM_GET_LOCAL(0))));
1117 FOR_UINT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); }
1118}
1119
1120WASM_EXEC_TEST(Block_If_P) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001121 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001122 // { if (p0) return 51; return 52; }
Ben Murdochda12d292016-06-02 14:46:10 +01001123 BUILD(r, B2( // --
1124 WASM_IF(WASM_GET_LOCAL(0), // --
Ben Murdochc5610432016-08-08 18:44:38 +01001125 WASM_BRV(1, WASM_I8(51))), // --
Ben Murdochda12d292016-06-02 14:46:10 +01001126 WASM_I8(52))); // --
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001127 FOR_INT32_INPUTS(i) {
1128 int32_t expected = *i ? 51 : 52;
1129 CHECK_EQ(expected, r.Call(*i));
1130 }
1131}
1132
Ben Murdochc5610432016-08-08 18:44:38 +01001133WASM_EXEC_TEST(Loop_empty) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001134 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01001135 BUILD(r, kExprLoop, kExprEnd, WASM_GET_LOCAL(0));
1136 FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); }
1137}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001138
Ben Murdochc5610432016-08-08 18:44:38 +01001139WASM_EXEC_TEST(Loop_empty_br1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001140 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01001141 BUILD(r, WASM_LOOP(1, WASM_BR(1)), WASM_GET_LOCAL(0));
1142 FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); }
1143}
1144
1145WASM_EXEC_TEST(Loop_empty_brif1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001146 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01001147 BUILD(r, WASM_LOOP(1, WASM_BR_IF(1, WASM_ZERO)), WASM_GET_LOCAL(0));
1148 FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); }
1149}
1150
1151WASM_EXEC_TEST(Loop_empty_brif2) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001152 WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(),
1153 MachineType::Uint32());
Ben Murdochc5610432016-08-08 18:44:38 +01001154 BUILD(r, WASM_LOOP(1, WASM_BR_IF(1, WASM_GET_LOCAL(1))), WASM_GET_LOCAL(0));
1155 FOR_UINT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i, *i + 1)); }
1156}
1157
1158WASM_EXEC_TEST(Block_BrIf_P) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001159 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +01001160 BUILD(r, B2(WASM_BRV_IF(0, WASM_I8(51), WASM_GET_LOCAL(0)), WASM_I8(52)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001161 FOR_INT32_INPUTS(i) {
1162 int32_t expected = *i ? 51 : 52;
1163 CHECK_EQ(expected, r.Call(*i));
1164 }
1165}
1166
Ben Murdochc5610432016-08-08 18:44:38 +01001167WASM_EXEC_TEST(Block_IfElse_P_assign) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001168 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001169 // { if (p0) p0 = 71; else p0 = 72; return p0; }
Ben Murdochda12d292016-06-02 14:46:10 +01001170 BUILD(r, B2( // --
1171 WASM_IF_ELSE(WASM_GET_LOCAL(0), // --
1172 WASM_SET_LOCAL(0, WASM_I8(71)), // --
1173 WASM_SET_LOCAL(0, WASM_I8(72))), // --
1174 WASM_GET_LOCAL(0)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001175 FOR_INT32_INPUTS(i) {
1176 int32_t expected = *i ? 71 : 72;
1177 CHECK_EQ(expected, r.Call(*i));
1178 }
1179}
1180
Ben Murdochc5610432016-08-08 18:44:38 +01001181WASM_EXEC_TEST(Block_IfElse_P_return) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001182 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001183 // if (p0) return 81; else return 82;
Ben Murdochda12d292016-06-02 14:46:10 +01001184 BUILD(r, // --
1185 WASM_IF_ELSE(WASM_GET_LOCAL(0), // --
1186 RET_I8(81), // --
1187 RET_I8(82))); // --
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001188 FOR_INT32_INPUTS(i) {
1189 int32_t expected = *i ? 81 : 82;
1190 CHECK_EQ(expected, r.Call(*i));
1191 }
1192}
1193
Ben Murdochc5610432016-08-08 18:44:38 +01001194WASM_EXEC_TEST(Block_If_P_assign) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001195 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001196 // { if (p0) p0 = 61; p0; }
1197 BUILD(r, WASM_BLOCK(
1198 2, WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(0, WASM_I8(61))),
1199 WASM_GET_LOCAL(0)));
1200 FOR_INT32_INPUTS(i) {
1201 int32_t expected = *i ? 61 : *i;
1202 CHECK_EQ(expected, r.Call(*i));
1203 }
1204}
1205
Ben Murdochc5610432016-08-08 18:44:38 +01001206WASM_EXEC_TEST(DanglingAssign) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001207 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001208 // { return 0; p0 = 0; }
Ben Murdochda12d292016-06-02 14:46:10 +01001209 BUILD(r, B2(RET_I8(99), WASM_SET_LOCAL(0, WASM_ZERO)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001210 CHECK_EQ(99, r.Call(1));
1211}
1212
Ben Murdochc5610432016-08-08 18:44:38 +01001213WASM_EXEC_TEST(ExprIf_P) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001214 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001215 // p0 ? 11 : 22;
1216 BUILD(r, WASM_IF_ELSE(WASM_GET_LOCAL(0), // --
1217 WASM_I8(11), // --
1218 WASM_I8(22))); // --
1219 FOR_INT32_INPUTS(i) {
1220 int32_t expected = *i ? 11 : 22;
1221 CHECK_EQ(expected, r.Call(*i));
1222 }
1223}
1224
Ben Murdochc5610432016-08-08 18:44:38 +01001225WASM_EXEC_TEST(ExprIf_P_fallthru) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001226 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001227 // p0 ? 11 : 22;
1228 BUILD(r, WASM_IF_ELSE(WASM_GET_LOCAL(0), // --
1229 WASM_I8(11), // --
1230 WASM_I8(22))); // --
1231 FOR_INT32_INPUTS(i) {
1232 int32_t expected = *i ? 11 : 22;
1233 CHECK_EQ(expected, r.Call(*i));
1234 }
1235}
1236
Ben Murdochc5610432016-08-08 18:44:38 +01001237WASM_EXEC_TEST(CountDown) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001238 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001239 BUILD(r,
1240 WASM_BLOCK(
1241 2, WASM_LOOP(
1242 1, WASM_IF(WASM_GET_LOCAL(0),
Ben Murdochc5610432016-08-08 18:44:38 +01001243 WASM_BRV(1, WASM_SET_LOCAL(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001244 0, WASM_I32_SUB(WASM_GET_LOCAL(0),
1245 WASM_I8(1)))))),
1246 WASM_GET_LOCAL(0)));
1247 CHECK_EQ(0, r.Call(1));
1248 CHECK_EQ(0, r.Call(10));
1249 CHECK_EQ(0, r.Call(100));
1250}
1251
Ben Murdochc5610432016-08-08 18:44:38 +01001252WASM_EXEC_TEST(CountDown_fallthru) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001253 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001254 BUILD(r,
1255 WASM_BLOCK(
Ben Murdochc5610432016-08-08 18:44:38 +01001256 2, WASM_LOOP(3, WASM_IF(WASM_NOT(WASM_GET_LOCAL(0)), WASM_BREAK(1)),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001257 WASM_SET_LOCAL(
1258 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(1))),
1259 WASM_CONTINUE(0)),
1260 WASM_GET_LOCAL(0)));
1261 CHECK_EQ(0, r.Call(1));
1262 CHECK_EQ(0, r.Call(10));
1263 CHECK_EQ(0, r.Call(100));
1264}
1265
Ben Murdochc5610432016-08-08 18:44:38 +01001266WASM_EXEC_TEST(WhileCountDown) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001267 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001268 BUILD(r, WASM_BLOCK(
1269 2, WASM_WHILE(WASM_GET_LOCAL(0),
1270 WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0),
1271 WASM_I8(1)))),
1272 WASM_GET_LOCAL(0)));
1273 CHECK_EQ(0, r.Call(1));
1274 CHECK_EQ(0, r.Call(10));
1275 CHECK_EQ(0, r.Call(100));
1276}
1277
Ben Murdochc5610432016-08-08 18:44:38 +01001278WASM_EXEC_TEST(Loop_if_break1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001279 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01001280 BUILD(r, B2(WASM_LOOP(2, WASM_IF(WASM_GET_LOCAL(0), WASM_BREAK(1)),
Ben Murdochda12d292016-06-02 14:46:10 +01001281 WASM_SET_LOCAL(0, WASM_I8(99))),
1282 WASM_GET_LOCAL(0)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001283 CHECK_EQ(99, r.Call(0));
1284 CHECK_EQ(3, r.Call(3));
1285 CHECK_EQ(10000, r.Call(10000));
1286 CHECK_EQ(-29, r.Call(-29));
1287}
1288
Ben Murdochc5610432016-08-08 18:44:38 +01001289WASM_EXEC_TEST(Loop_if_break2) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001290 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +01001291 BUILD(r, B2(WASM_LOOP(2, WASM_BR_IF(1, WASM_GET_LOCAL(0)),
1292 WASM_SET_LOCAL(0, WASM_I8(99))),
1293 WASM_GET_LOCAL(0)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001294 CHECK_EQ(99, r.Call(0));
1295 CHECK_EQ(3, r.Call(3));
1296 CHECK_EQ(10000, r.Call(10000));
1297 CHECK_EQ(-29, r.Call(-29));
1298}
1299
Ben Murdochc5610432016-08-08 18:44:38 +01001300WASM_EXEC_TEST(Loop_if_break_fallthru) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001301 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +01001302 BUILD(r, B1(WASM_LOOP(2, WASM_IF(WASM_GET_LOCAL(0), WASM_BREAK(1)),
1303 WASM_SET_LOCAL(0, WASM_I8(93)))),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001304 WASM_GET_LOCAL(0));
1305 CHECK_EQ(93, r.Call(0));
1306 CHECK_EQ(3, r.Call(3));
1307 CHECK_EQ(10001, r.Call(10001));
1308 CHECK_EQ(-22, r.Call(-22));
1309}
1310
Ben Murdochc5610432016-08-08 18:44:38 +01001311WASM_EXEC_TEST(IfBreak1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001312 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01001313 BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SEQ(WASM_BR(0), WASM_UNREACHABLE)),
1314 WASM_I8(91));
1315 CHECK_EQ(91, r.Call(0));
1316 CHECK_EQ(91, r.Call(1));
1317 CHECK_EQ(91, r.Call(-8734));
1318}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001319
Ben Murdochc5610432016-08-08 18:44:38 +01001320WASM_EXEC_TEST(IfBreak2) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001321 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01001322 BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SEQ(WASM_BR(0), RET_I8(77))),
1323 WASM_I8(81));
1324 CHECK_EQ(81, r.Call(0));
1325 CHECK_EQ(81, r.Call(1));
1326 CHECK_EQ(81, r.Call(-8734));
1327}
1328
1329WASM_EXEC_TEST(LoadMemI32) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001330 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001331 int32_t* memory = module.AddMemoryElems<int32_t>(8);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001332 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001333 module.RandomizeMemory(1111);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001334
1335 BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(0)));
1336
1337 memory[0] = 99999999;
1338 CHECK_EQ(99999999, r.Call(0));
1339
1340 memory[0] = 88888888;
1341 CHECK_EQ(88888888, r.Call(0));
1342
1343 memory[0] = 77777777;
1344 CHECK_EQ(77777777, r.Call(0));
1345}
1346
Ben Murdoch61f157c2016-09-16 13:49:30 +01001347WASM_EXEC_TEST(LoadMemI32_alignment) {
1348 TestingModule module(execution_mode);
1349 int32_t* memory = module.AddMemoryElems<int32_t>(8);
1350 for (byte alignment = 0; alignment <= 2; ++alignment) {
1351 WasmRunner<int32_t> r(&module, MachineType::Int32());
1352 module.RandomizeMemory(1111);
1353
1354 BUILD(r,
1355 WASM_LOAD_MEM_ALIGNMENT(MachineType::Int32(), WASM_I8(0), alignment));
1356
1357 memory[0] = 0x1a2b3c4d;
1358 CHECK_EQ(0x1a2b3c4d, r.Call(0));
1359
1360 memory[0] = 0x5e6f7a8b;
1361 CHECK_EQ(0x5e6f7a8b, r.Call(0));
1362
1363 memory[0] = 0x9ca0b1c2;
1364 CHECK_EQ(0x9ca0b1c2, r.Call(0));
1365 }
1366}
1367
Ben Murdochc5610432016-08-08 18:44:38 +01001368WASM_EXEC_TEST(LoadMemI32_oob) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001369 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001370 int32_t* memory = module.AddMemoryElems<int32_t>(8);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001371 WasmRunner<int32_t> r(&module, MachineType::Uint32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001372 module.RandomizeMemory(1111);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001373
1374 BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0)));
1375
1376 memory[0] = 88888888;
1377 CHECK_EQ(88888888, r.Call(0u));
Ben Murdoch61f157c2016-09-16 13:49:30 +01001378 for (uint32_t offset = 29; offset < 40; ++offset) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001379 CHECK_TRAP(r.Call(offset));
1380 }
1381
Ben Murdoch61f157c2016-09-16 13:49:30 +01001382 for (uint32_t offset = 0x80000000; offset < 0x80000010; ++offset) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001383 CHECK_TRAP(r.Call(offset));
1384 }
1385}
1386
Ben Murdochc5610432016-08-08 18:44:38 +01001387WASM_EXEC_TEST(LoadMem_offset_oob) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001388 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001389 module.AddMemoryElems<int32_t>(8);
1390
1391 static const MachineType machineTypes[] = {
1392 MachineType::Int8(), MachineType::Uint8(), MachineType::Int16(),
1393 MachineType::Uint16(), MachineType::Int32(), MachineType::Uint32(),
1394 MachineType::Int64(), MachineType::Uint64(), MachineType::Float32(),
1395 MachineType::Float64()};
1396
Ben Murdoch61f157c2016-09-16 13:49:30 +01001397 for (size_t m = 0; m < arraysize(machineTypes); ++m) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001398 module.RandomizeMemory(1116 + static_cast<int>(m));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001399 WasmRunner<int32_t> r(&module, MachineType::Uint32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001400 uint32_t boundary = 24 - WasmOpcodes::MemSize(machineTypes[m]);
1401
1402 BUILD(r, WASM_LOAD_MEM_OFFSET(machineTypes[m], 8, WASM_GET_LOCAL(0)),
1403 WASM_ZERO);
1404
1405 CHECK_EQ(0, r.Call(boundary)); // in bounds.
1406
Ben Murdoch61f157c2016-09-16 13:49:30 +01001407 for (uint32_t offset = boundary + 1; offset < boundary + 19; ++offset) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001408 CHECK_TRAP(r.Call(offset)); // out of bounds.
1409 }
1410 }
1411}
1412
Ben Murdochc5610432016-08-08 18:44:38 +01001413WASM_EXEC_TEST(LoadMemI32_offset) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001414 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001415 int32_t* memory = module.AddMemoryElems<int32_t>(4);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001416 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001417 module.RandomizeMemory(1111);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001418
1419 BUILD(r, WASM_LOAD_MEM_OFFSET(MachineType::Int32(), 4, WASM_GET_LOCAL(0)));
1420
1421 memory[0] = 66666666;
1422 memory[1] = 77777777;
1423 memory[2] = 88888888;
1424 memory[3] = 99999999;
1425 CHECK_EQ(77777777, r.Call(0));
1426 CHECK_EQ(88888888, r.Call(4));
1427 CHECK_EQ(99999999, r.Call(8));
1428
1429 memory[0] = 11111111;
1430 memory[1] = 22222222;
1431 memory[2] = 33333333;
1432 memory[3] = 44444444;
1433 CHECK_EQ(22222222, r.Call(0));
1434 CHECK_EQ(33333333, r.Call(4));
1435 CHECK_EQ(44444444, r.Call(8));
1436}
1437
Ben Murdochc5610432016-08-08 18:44:38 +01001438WASM_EXEC_TEST(LoadMemI32_const_oob_misaligned) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001439 const int kMemSize = 12;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001440 // TODO(titzer): Fix misaligned accesses on MIPS and re-enable.
Ben Murdoch61f157c2016-09-16 13:49:30 +01001441 for (int offset = 0; offset < kMemSize + 5; ++offset) {
1442 for (int index = 0; index < kMemSize + 5; ++index) {
1443 TestingModule module(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001444 module.AddMemoryElems<byte>(kMemSize);
1445
1446 WasmRunner<int32_t> r(&module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001447 module.RandomizeMemory();
1448
1449 BUILD(r,
1450 WASM_LOAD_MEM_OFFSET(MachineType::Int32(), offset, WASM_I8(index)));
1451
1452 if ((offset + index) <= (kMemSize - sizeof(int32_t))) {
1453 CHECK_EQ(module.raw_val_at<int32_t>(offset + index), r.Call());
1454 } else {
1455 CHECK_TRAP(r.Call());
1456 }
1457 }
1458 }
1459}
1460
Ben Murdochc5610432016-08-08 18:44:38 +01001461WASM_EXEC_TEST(LoadMemI32_const_oob) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001462 const int kMemSize = 24;
1463 for (int offset = 0; offset < kMemSize + 5; offset += 4) {
1464 for (int index = 0; index < kMemSize + 5; index += 4) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001465 TestingModule module(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001466 module.AddMemoryElems<byte>(kMemSize);
1467
1468 WasmRunner<int32_t> r(&module);
1469 module.RandomizeMemory();
1470
1471 BUILD(r,
1472 WASM_LOAD_MEM_OFFSET(MachineType::Int32(), offset, WASM_I8(index)));
1473
1474 if ((offset + index) <= (kMemSize - sizeof(int32_t))) {
1475 CHECK_EQ(module.raw_val_at<int32_t>(offset + index), r.Call());
1476 } else {
1477 CHECK_TRAP(r.Call());
1478 }
1479 }
1480 }
1481}
1482
Ben Murdoch61f157c2016-09-16 13:49:30 +01001483WASM_EXEC_TEST(StoreMemI32_alignment) {
1484 TestingModule module(execution_mode);
1485 int32_t* memory = module.AddMemoryElems<int32_t>(4);
1486 const int32_t kWritten = 0x12345678;
1487
1488 for (byte i = 0; i <= 2; ++i) {
1489 WasmRunner<int32_t> r(&module, MachineType::Int32());
1490 BUILD(r, WASM_STORE_MEM_ALIGNMENT(MachineType::Int32(), WASM_ZERO, i,
1491 WASM_GET_LOCAL(0)));
1492 module.RandomizeMemory(1111);
1493 memory[0] = 0;
1494
1495 CHECK_EQ(kWritten, r.Call(kWritten));
1496 CHECK_EQ(kWritten, memory[0]);
1497 }
1498}
1499
Ben Murdochc5610432016-08-08 18:44:38 +01001500WASM_EXEC_TEST(StoreMemI32_offset) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001501 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001502 int32_t* memory = module.AddMemoryElems<int32_t>(4);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001503 WasmRunner<int32_t> r(&module, MachineType::Int32());
1504 const int32_t kWritten = 0xaabbccdd;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001505
1506 BUILD(r, WASM_STORE_MEM_OFFSET(MachineType::Int32(), 4, WASM_GET_LOCAL(0),
Ben Murdochda12d292016-06-02 14:46:10 +01001507 WASM_I32V_5(kWritten)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001508
Ben Murdoch61f157c2016-09-16 13:49:30 +01001509 for (int i = 0; i < 2; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001510 module.RandomizeMemory(1111);
1511 memory[0] = 66666666;
1512 memory[1] = 77777777;
1513 memory[2] = 88888888;
1514 memory[3] = 99999999;
1515 CHECK_EQ(kWritten, r.Call(i * 4));
1516 CHECK_EQ(66666666, memory[0]);
1517 CHECK_EQ(i == 0 ? kWritten : 77777777, memory[1]);
1518 CHECK_EQ(i == 1 ? kWritten : 88888888, memory[2]);
1519 CHECK_EQ(i == 2 ? kWritten : 99999999, memory[3]);
1520 }
1521}
1522
Ben Murdochc5610432016-08-08 18:44:38 +01001523WASM_EXEC_TEST(StoreMem_offset_oob) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001524 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001525 byte* memory = module.AddMemoryElems<byte>(32);
1526
Ben Murdoch61f157c2016-09-16 13:49:30 +01001527 // 64-bit cases are handled in test-run-wasm-64.cc
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001528 static const MachineType machineTypes[] = {
1529 MachineType::Int8(), MachineType::Uint8(), MachineType::Int16(),
1530 MachineType::Uint16(), MachineType::Int32(), MachineType::Uint32(),
1531 MachineType::Float32(), MachineType::Float64()};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001532
Ben Murdoch61f157c2016-09-16 13:49:30 +01001533 for (size_t m = 0; m < arraysize(machineTypes); ++m) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001534 module.RandomizeMemory(1119 + static_cast<int>(m));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001535 WasmRunner<int32_t> r(&module, MachineType::Uint32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001536
1537 BUILD(r, WASM_STORE_MEM_OFFSET(machineTypes[m], 8, WASM_GET_LOCAL(0),
1538 WASM_LOAD_MEM(machineTypes[m], WASM_ZERO)),
1539 WASM_ZERO);
1540
1541 byte memsize = WasmOpcodes::MemSize(machineTypes[m]);
1542 uint32_t boundary = 24 - memsize;
1543 CHECK_EQ(0, r.Call(boundary)); // in bounds.
1544 CHECK_EQ(0, memcmp(&memory[0], &memory[8 + boundary], memsize));
1545
Ben Murdoch61f157c2016-09-16 13:49:30 +01001546 for (uint32_t offset = boundary + 1; offset < boundary + 19; ++offset) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001547 CHECK_TRAP(r.Call(offset)); // out of bounds.
1548 }
1549 }
1550}
1551
Ben Murdochc5610432016-08-08 18:44:38 +01001552WASM_EXEC_TEST(LoadMemI32_P) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001553 const int kNumElems = 8;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001554 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001555 int32_t* memory = module.AddMemoryElems<int32_t>(kNumElems);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001556 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001557 module.RandomizeMemory(2222);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001558
1559 BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0)));
1560
Ben Murdoch61f157c2016-09-16 13:49:30 +01001561 for (int i = 0; i < kNumElems; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001562 CHECK_EQ(memory[i], r.Call(i * 4));
1563 }
1564}
1565
Ben Murdochc5610432016-08-08 18:44:38 +01001566WASM_EXEC_TEST(MemI32_Sum) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001567 const int kNumElems = 20;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001568 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001569 uint32_t* memory = module.AddMemoryElems<uint32_t>(kNumElems);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001570 WasmRunner<uint32_t> r(&module, MachineType::Int32());
1571 const byte kSum = r.AllocateLocal(kAstI32);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001572
1573 BUILD(r, WASM_BLOCK(
1574 2, WASM_WHILE(
1575 WASM_GET_LOCAL(0),
1576 WASM_BLOCK(
1577 2, WASM_SET_LOCAL(
1578 kSum, WASM_I32_ADD(
1579 WASM_GET_LOCAL(kSum),
1580 WASM_LOAD_MEM(MachineType::Int32(),
1581 WASM_GET_LOCAL(0)))),
1582 WASM_SET_LOCAL(
1583 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(4))))),
1584 WASM_GET_LOCAL(1)));
1585
1586 // Run 4 trials.
Ben Murdoch61f157c2016-09-16 13:49:30 +01001587 for (int i = 0; i < 3; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001588 module.RandomizeMemory(i * 33);
1589 uint32_t expected = 0;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001590 for (size_t j = kNumElems - 1; j > 0; --j) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001591 expected += memory[j];
1592 }
Ben Murdoch61f157c2016-09-16 13:49:30 +01001593 uint32_t result = r.Call(4 * (kNumElems - 1));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001594 CHECK_EQ(expected, result);
1595 }
1596}
1597
Ben Murdochc5610432016-08-08 18:44:38 +01001598WASM_EXEC_TEST(CheckMachIntsZero) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001599 const int kNumElems = 55;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001600 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001601 module.AddMemoryElems<uint32_t>(kNumElems);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001602 WasmRunner<uint32_t> r(&module, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001603
Ben Murdochc5610432016-08-08 18:44:38 +01001604 BUILD(r, kExprLoop, kExprGetLocal, 0, kExprIf, kExprGetLocal, 0,
1605 kExprI32LoadMem, 0, 0, kExprIf, kExprI8Const, 255, kExprReturn, ARITY_1,
1606 kExprEnd, kExprGetLocal, 0, kExprI8Const, 4, kExprI32Sub, kExprSetLocal,
1607 0, kExprBr, ARITY_1, DEPTH_0, kExprEnd, kExprEnd, kExprI8Const, 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001608
1609 module.BlankMemory();
1610 CHECK_EQ(0, r.Call((kNumElems - 1) * 4));
1611}
1612
Ben Murdochc5610432016-08-08 18:44:38 +01001613WASM_EXEC_TEST(MemF32_Sum) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001614 const int kSize = 5;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001615 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001616 module.AddMemoryElems<float>(kSize);
1617 float* buffer = module.raw_mem_start<float>();
1618 buffer[0] = -99.25;
1619 buffer[1] = -888.25;
1620 buffer[2] = -77.25;
1621 buffer[3] = 66666.25;
1622 buffer[4] = 5555.25;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001623 WasmRunner<int32_t> r(&module, MachineType::Int32());
1624 const byte kSum = r.AllocateLocal(kAstF32);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001625
1626 BUILD(r, WASM_BLOCK(
1627 3, WASM_WHILE(
1628 WASM_GET_LOCAL(0),
1629 WASM_BLOCK(
1630 2, WASM_SET_LOCAL(
1631 kSum, WASM_F32_ADD(
1632 WASM_GET_LOCAL(kSum),
1633 WASM_LOAD_MEM(MachineType::Float32(),
1634 WASM_GET_LOCAL(0)))),
1635 WASM_SET_LOCAL(
1636 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(4))))),
1637 WASM_STORE_MEM(MachineType::Float32(), WASM_ZERO,
1638 WASM_GET_LOCAL(kSum)),
1639 WASM_GET_LOCAL(0)));
1640
1641 CHECK_EQ(0, r.Call(4 * (kSize - 1)));
1642 CHECK_NE(-99.25, buffer[0]);
1643 CHECK_EQ(71256.0f, buffer[0]);
1644}
1645
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001646template <typename T>
Ben Murdoch61f157c2016-09-16 13:49:30 +01001647T GenerateAndRunFold(WasmExecutionMode execution_mode, WasmOpcode binop,
1648 T* buffer, uint32_t size, LocalType astType,
1649 MachineType memType) {
1650 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001651 module.AddMemoryElems<T>(size);
Ben Murdoch61f157c2016-09-16 13:49:30 +01001652 for (uint32_t i = 0; i < size; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001653 module.raw_mem_start<T>()[i] = buffer[i];
1654 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001655 WasmRunner<int32_t> r(&module, MachineType::Int32());
1656 const byte kAccum = r.AllocateLocal(astType);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001657
1658 BUILD(
1659 r,
1660 WASM_BLOCK(
1661 4, WASM_SET_LOCAL(kAccum, WASM_LOAD_MEM(memType, WASM_ZERO)),
1662 WASM_WHILE(
1663 WASM_GET_LOCAL(0),
1664 WASM_BLOCK(
1665 2, WASM_SET_LOCAL(
1666 kAccum,
1667 WASM_BINOP(binop, WASM_GET_LOCAL(kAccum),
1668 WASM_LOAD_MEM(memType, WASM_GET_LOCAL(0)))),
1669 WASM_SET_LOCAL(
1670 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(sizeof(T)))))),
1671 WASM_STORE_MEM(memType, WASM_ZERO, WASM_GET_LOCAL(kAccum)),
1672 WASM_GET_LOCAL(0)));
1673 r.Call(static_cast<int>(sizeof(T) * (size - 1)));
1674 return module.raw_mem_at<double>(0);
1675}
1676
Ben Murdochc5610432016-08-08 18:44:38 +01001677WASM_EXEC_TEST(MemF64_Mul) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001678 const size_t kSize = 6;
1679 double buffer[kSize] = {1, 2, 2, 2, 2, 2};
Ben Murdoch61f157c2016-09-16 13:49:30 +01001680 double result =
1681 GenerateAndRunFold<double>(execution_mode, kExprF64Mul, buffer, kSize,
1682 kAstF64, MachineType::Float64());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001683 CHECK_EQ(32, result);
1684}
1685
Ben Murdoch61f157c2016-09-16 13:49:30 +01001686WASM_EXEC_TEST(Build_Wasm_Infinite_Loop) {
1687 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001688 // Only build the graph and compile, don't run.
1689 BUILD(r, WASM_INFINITE_LOOP);
1690}
1691
Ben Murdoch61f157c2016-09-16 13:49:30 +01001692WASM_EXEC_TEST(Build_Wasm_Infinite_Loop_effect) {
1693 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001694 module.AddMemoryElems<int8_t>(16);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001695 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001696
1697 // Only build the graph and compile, don't run.
1698 BUILD(r, WASM_LOOP(1, WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)));
1699}
1700
Ben Murdochc5610432016-08-08 18:44:38 +01001701WASM_EXEC_TEST(Unreachable0a) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001702 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +01001703 BUILD(r, B2(WASM_BRV(0, WASM_I8(9)), RET(WASM_GET_LOCAL(0))));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001704 CHECK_EQ(9, r.Call(0));
1705 CHECK_EQ(9, r.Call(1));
1706}
1707
Ben Murdochc5610432016-08-08 18:44:38 +01001708WASM_EXEC_TEST(Unreachable0b) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001709 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +01001710 BUILD(r, B2(WASM_BRV(0, WASM_I8(7)), WASM_UNREACHABLE));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001711 CHECK_EQ(7, r.Call(0));
1712 CHECK_EQ(7, r.Call(1));
1713}
1714
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001715TEST(Build_Wasm_Unreachable1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001716 WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001717 BUILD(r, WASM_UNREACHABLE);
1718}
1719
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001720TEST(Build_Wasm_Unreachable2) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001721 WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001722 BUILD(r, WASM_UNREACHABLE, WASM_UNREACHABLE);
1723}
1724
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001725TEST(Build_Wasm_Unreachable3) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001726 WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001727 BUILD(r, WASM_UNREACHABLE, WASM_UNREACHABLE, WASM_UNREACHABLE);
1728}
1729
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001730TEST(Build_Wasm_UnreachableIf1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001731 WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001732 BUILD(r, WASM_UNREACHABLE, WASM_IF(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
1733}
1734
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001735TEST(Build_Wasm_UnreachableIf2) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001736 WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001737 BUILD(r, WASM_UNREACHABLE,
1738 WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_UNREACHABLE));
1739}
1740
Ben Murdochc5610432016-08-08 18:44:38 +01001741WASM_EXEC_TEST(Unreachable_Load) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001742 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +01001743 BUILD(r, B2(WASM_BRV(0, WASM_GET_LOCAL(0)),
1744 WASM_LOAD_MEM(MachineType::Int8(), WASM_GET_LOCAL(0))));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001745 CHECK_EQ(11, r.Call(11));
1746 CHECK_EQ(21, r.Call(21));
1747}
1748
Ben Murdochc5610432016-08-08 18:44:38 +01001749WASM_EXEC_TEST(Infinite_Loop_not_taken1) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001750 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +01001751 BUILD(r, B2(WASM_IF(WASM_GET_LOCAL(0), WASM_INFINITE_LOOP), WASM_I8(45)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001752 // Run the code, but don't go into the infinite loop.
1753 CHECK_EQ(45, r.Call(0));
1754}
1755
Ben Murdochc5610432016-08-08 18:44:38 +01001756WASM_EXEC_TEST(Infinite_Loop_not_taken2) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001757 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01001758 BUILD(r, B1(WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_I8(45)),
Ben Murdochda12d292016-06-02 14:46:10 +01001759 WASM_INFINITE_LOOP)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001760 // Run the code, but don't go into the infinite loop.
1761 CHECK_EQ(45, r.Call(1));
1762}
1763
Ben Murdochc5610432016-08-08 18:44:38 +01001764WASM_EXEC_TEST(Infinite_Loop_not_taken2_brif) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001765 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +01001766 BUILD(r,
1767 B2(WASM_BRV_IF(0, WASM_I8(45), WASM_GET_LOCAL(0)), WASM_INFINITE_LOOP));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001768 // Run the code, but don't go into the infinite loop.
1769 CHECK_EQ(45, r.Call(1));
1770}
1771
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001772static void TestBuildGraphForSimpleExpression(WasmOpcode opcode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001773 Isolate* isolate = CcTest::InitIsolateOnce();
Ben Murdochda12d292016-06-02 14:46:10 +01001774 Zone zone(isolate->allocator());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001775 HandleScope scope(isolate);
1776 // Enable all optional operators.
1777 CommonOperatorBuilder common(&zone);
1778 MachineOperatorBuilder machine(&zone, MachineType::PointerRepresentation(),
1779 MachineOperatorBuilder::kAllOptionalOps);
1780 Graph graph(&zone);
1781 JSGraph jsgraph(isolate, &graph, &common, nullptr, nullptr, &machine);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001782 FunctionSig* sig = WasmOpcodes::Signature(opcode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001783
1784 if (sig->parameter_count() == 1) {
Ben Murdochc5610432016-08-08 18:44:38 +01001785 byte code[] = {WASM_NO_LOCALS, kExprGetLocal, 0, static_cast<byte>(opcode)};
1786 TestBuildingGraph(&zone, &jsgraph, nullptr, sig, nullptr, code,
Ben Murdochda12d292016-06-02 14:46:10 +01001787 code + arraysize(code));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001788 } else {
1789 CHECK_EQ(2, sig->parameter_count());
Ben Murdochc5610432016-08-08 18:44:38 +01001790 byte code[] = {WASM_NO_LOCALS, kExprGetLocal, 0, kExprGetLocal, 1,
1791 static_cast<byte>(opcode)};
1792 TestBuildingGraph(&zone, &jsgraph, nullptr, sig, nullptr, code,
Ben Murdochda12d292016-06-02 14:46:10 +01001793 code + arraysize(code));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001794 }
1795}
1796
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001797TEST(Build_Wasm_SimpleExprs) {
1798// Test that the decoder can build a graph for all supported simple expressions.
1799#define GRAPH_BUILD_TEST(name, opcode, sig) \
1800 TestBuildGraphForSimpleExpression(kExpr##name);
1801
1802 FOREACH_SIMPLE_OPCODE(GRAPH_BUILD_TEST);
1803
1804#undef GRAPH_BUILD_TEST
1805}
1806
Ben Murdochc5610432016-08-08 18:44:38 +01001807WASM_EXEC_TEST(Int32LoadInt8_signext) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001808 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001809 const int kNumElems = 16;
1810 int8_t* memory = module.AddMemoryElems<int8_t>(kNumElems);
1811 module.RandomizeMemory();
1812 memory[0] = -1;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001813 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001814 BUILD(r, WASM_LOAD_MEM(MachineType::Int8(), WASM_GET_LOCAL(0)));
1815
Ben Murdoch61f157c2016-09-16 13:49:30 +01001816 for (int i = 0; i < kNumElems; ++i) {
1817 CHECK_EQ(memory[i], r.Call(i));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001818 }
1819}
1820
Ben Murdochc5610432016-08-08 18:44:38 +01001821WASM_EXEC_TEST(Int32LoadInt8_zeroext) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001822 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001823 const int kNumElems = 16;
1824 byte* memory = module.AddMemory(kNumElems);
1825 module.RandomizeMemory(77);
1826 memory[0] = 255;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001827 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001828 BUILD(r, WASM_LOAD_MEM(MachineType::Uint8(), WASM_GET_LOCAL(0)));
1829
Ben Murdoch61f157c2016-09-16 13:49:30 +01001830 for (int i = 0; i < kNumElems; ++i) {
1831 CHECK_EQ(memory[i], r.Call(i));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001832 }
1833}
1834
Ben Murdochc5610432016-08-08 18:44:38 +01001835WASM_EXEC_TEST(Int32LoadInt16_signext) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001836 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001837 const int kNumBytes = 16;
1838 byte* memory = module.AddMemory(kNumBytes);
1839 module.RandomizeMemory(888);
1840 memory[1] = 200;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001841 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001842 BUILD(r, WASM_LOAD_MEM(MachineType::Int16(), WASM_GET_LOCAL(0)));
1843
Ben Murdoch61f157c2016-09-16 13:49:30 +01001844 for (int i = 0; i < kNumBytes; i += 2) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001845 int32_t expected = memory[i] | (static_cast<int8_t>(memory[i + 1]) << 8);
Ben Murdoch61f157c2016-09-16 13:49:30 +01001846 CHECK_EQ(expected, r.Call(i));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001847 }
1848}
1849
Ben Murdochc5610432016-08-08 18:44:38 +01001850WASM_EXEC_TEST(Int32LoadInt16_zeroext) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001851 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001852 const int kNumBytes = 16;
1853 byte* memory = module.AddMemory(kNumBytes);
1854 module.RandomizeMemory(9999);
1855 memory[1] = 204;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001856 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001857 BUILD(r, WASM_LOAD_MEM(MachineType::Uint16(), WASM_GET_LOCAL(0)));
1858
Ben Murdoch61f157c2016-09-16 13:49:30 +01001859 for (int i = 0; i < kNumBytes; i += 2) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001860 int32_t expected = memory[i] | (memory[i + 1] << 8);
Ben Murdoch61f157c2016-09-16 13:49:30 +01001861 CHECK_EQ(expected, r.Call(i));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001862 }
1863}
1864
Ben Murdochc5610432016-08-08 18:44:38 +01001865WASM_EXEC_TEST(Int32Global) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001866 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001867 int32_t* global = module.AddGlobal<int32_t>(MachineType::Int32());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001868 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001869 // global = global + p0
1870 BUILD(r, WASM_STORE_GLOBAL(
1871 0, WASM_I32_ADD(WASM_LOAD_GLOBAL(0), WASM_GET_LOCAL(0))));
1872
1873 *global = 116;
1874 for (int i = 9; i < 444444; i += 111111) {
1875 int32_t expected = *global + i;
1876 r.Call(i);
1877 CHECK_EQ(expected, *global);
1878 }
1879}
1880
Ben Murdochc5610432016-08-08 18:44:38 +01001881WASM_EXEC_TEST(Int32Globals_DontAlias) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001882 const int kNumGlobals = 3;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001883 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001884 int32_t* globals[] = {module.AddGlobal<int32_t>(MachineType::Int32()),
1885 module.AddGlobal<int32_t>(MachineType::Int32()),
1886 module.AddGlobal<int32_t>(MachineType::Int32())};
1887
Ben Murdoch61f157c2016-09-16 13:49:30 +01001888 for (int g = 0; g < kNumGlobals; ++g) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001889 // global = global + p0
Ben Murdoch097c5b22016-05-18 11:27:45 +01001890 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001891 BUILD(r, WASM_STORE_GLOBAL(
1892 g, WASM_I32_ADD(WASM_LOAD_GLOBAL(g), WASM_GET_LOCAL(0))));
1893
1894 // Check that reading/writing global number {g} doesn't alter the others.
1895 *globals[g] = 116 * g;
1896 int32_t before[kNumGlobals];
1897 for (int i = 9; i < 444444; i += 111113) {
1898 int32_t sum = *globals[g] + i;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001899 for (int j = 0; j < kNumGlobals; ++j) before[j] = *globals[j];
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001900 r.Call(i);
Ben Murdoch61f157c2016-09-16 13:49:30 +01001901 for (int j = 0; j < kNumGlobals; ++j) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001902 int32_t expected = j == g ? sum : before[j];
1903 CHECK_EQ(expected, *globals[j]);
1904 }
1905 }
1906 }
1907}
1908
Ben Murdochc5610432016-08-08 18:44:38 +01001909WASM_EXEC_TEST(Float32Global) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001910 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001911 float* global = module.AddGlobal<float>(MachineType::Float32());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001912 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001913 // global = global + p0
Ben Murdochda12d292016-06-02 14:46:10 +01001914 BUILD(r, B2(WASM_STORE_GLOBAL(
1915 0, WASM_F32_ADD(WASM_LOAD_GLOBAL(0),
1916 WASM_F32_SCONVERT_I32(WASM_GET_LOCAL(0)))),
1917 WASM_ZERO));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001918
1919 *global = 1.25;
1920 for (int i = 9; i < 4444; i += 1111) {
1921 volatile float expected = *global + i;
1922 r.Call(i);
1923 CHECK_EQ(expected, *global);
1924 }
1925}
1926
Ben Murdochc5610432016-08-08 18:44:38 +01001927WASM_EXEC_TEST(Float64Global) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001928 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001929 double* global = module.AddGlobal<double>(MachineType::Float64());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001930 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001931 // global = global + p0
Ben Murdochda12d292016-06-02 14:46:10 +01001932 BUILD(r, B2(WASM_STORE_GLOBAL(
1933 0, WASM_F64_ADD(WASM_LOAD_GLOBAL(0),
1934 WASM_F64_SCONVERT_I32(WASM_GET_LOCAL(0)))),
1935 WASM_ZERO));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001936
1937 *global = 1.25;
1938 for (int i = 9; i < 4444; i += 1111) {
1939 volatile double expected = *global + i;
1940 r.Call(i);
1941 CHECK_EQ(expected, *global);
1942 }
1943}
1944
Ben Murdochc5610432016-08-08 18:44:38 +01001945WASM_EXEC_TEST(MixedGlobals) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001946 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001947 int32_t* unused = module.AddGlobal<int32_t>(MachineType::Int32());
1948 byte* memory = module.AddMemory(32);
1949
1950 int8_t* var_int8 = module.AddGlobal<int8_t>(MachineType::Int8());
1951 uint8_t* var_uint8 = module.AddGlobal<uint8_t>(MachineType::Uint8());
1952 int16_t* var_int16 = module.AddGlobal<int16_t>(MachineType::Int16());
1953 uint16_t* var_uint16 = module.AddGlobal<uint16_t>(MachineType::Uint16());
1954 int32_t* var_int32 = module.AddGlobal<int32_t>(MachineType::Int32());
1955 uint32_t* var_uint32 = module.AddGlobal<uint32_t>(MachineType::Uint32());
1956 float* var_float = module.AddGlobal<float>(MachineType::Float32());
1957 double* var_double = module.AddGlobal<double>(MachineType::Float64());
1958
Ben Murdoch097c5b22016-05-18 11:27:45 +01001959 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001960
1961 BUILD(
1962 r,
1963 WASM_BLOCK(
1964 9,
1965 WASM_STORE_GLOBAL(1, WASM_LOAD_MEM(MachineType::Int8(), WASM_ZERO)),
1966 WASM_STORE_GLOBAL(2, WASM_LOAD_MEM(MachineType::Uint8(), WASM_ZERO)),
1967 WASM_STORE_GLOBAL(3, WASM_LOAD_MEM(MachineType::Int16(), WASM_ZERO)),
1968 WASM_STORE_GLOBAL(4, WASM_LOAD_MEM(MachineType::Uint16(), WASM_ZERO)),
1969 WASM_STORE_GLOBAL(5, WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)),
1970 WASM_STORE_GLOBAL(6, WASM_LOAD_MEM(MachineType::Uint32(), WASM_ZERO)),
1971 WASM_STORE_GLOBAL(7,
1972 WASM_LOAD_MEM(MachineType::Float32(), WASM_ZERO)),
1973 WASM_STORE_GLOBAL(8,
1974 WASM_LOAD_MEM(MachineType::Float64(), WASM_ZERO)),
1975 WASM_ZERO));
1976
1977 memory[0] = 0xaa;
1978 memory[1] = 0xcc;
1979 memory[2] = 0x55;
1980 memory[3] = 0xee;
1981 memory[4] = 0x33;
1982 memory[5] = 0x22;
1983 memory[6] = 0x11;
1984 memory[7] = 0x99;
1985 r.Call(1);
1986
1987 CHECK(static_cast<int8_t>(0xaa) == *var_int8);
1988 CHECK(static_cast<uint8_t>(0xaa) == *var_uint8);
1989 CHECK(static_cast<int16_t>(0xccaa) == *var_int16);
1990 CHECK(static_cast<uint16_t>(0xccaa) == *var_uint16);
1991 CHECK(static_cast<int32_t>(0xee55ccaa) == *var_int32);
1992 CHECK(static_cast<uint32_t>(0xee55ccaa) == *var_uint32);
1993 CHECK(bit_cast<float>(0xee55ccaa) == *var_float);
1994 CHECK(bit_cast<double>(0x99112233ee55ccaaULL) == *var_double);
1995
1996 USE(unused);
1997}
1998
Ben Murdochc5610432016-08-08 18:44:38 +01001999WASM_EXEC_TEST(CallEmpty) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002000 const int32_t kExpected = -414444;
2001 // Build the target function.
2002 TestSignatures sigs;
Ben Murdoch61f157c2016-09-16 13:49:30 +01002003 TestingModule module(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002004 WasmFunctionCompiler t(sigs.i_v(), &module);
Ben Murdochda12d292016-06-02 14:46:10 +01002005 BUILD(t, WASM_I32V_3(kExpected));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002006 uint32_t index = t.CompileAndAdd();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002007
2008 // Build the calling function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002009 WasmRunner<int32_t> r(&module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002010 BUILD(r, WASM_CALL_FUNCTION0(index));
2011
2012 int32_t result = r.Call();
2013 CHECK_EQ(kExpected, result);
2014}
2015
Ben Murdochc5610432016-08-08 18:44:38 +01002016WASM_EXEC_TEST(CallF32StackParameter) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002017 // Build the target function.
2018 LocalType param_types[20];
Ben Murdoch61f157c2016-09-16 13:49:30 +01002019 for (int i = 0; i < 20; ++i) param_types[i] = kAstF32;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002020 FunctionSig sig(1, 19, param_types);
Ben Murdoch61f157c2016-09-16 13:49:30 +01002021 TestingModule module(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002022 WasmFunctionCompiler t(&sig, &module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002023 BUILD(t, WASM_GET_LOCAL(17));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002024 uint32_t index = t.CompileAndAdd();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002025
2026 // Build the calling function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002027 WasmRunner<float> r(&module);
Ben Murdochc5610432016-08-08 18:44:38 +01002028 BUILD(r, WASM_CALL_FUNCTIONN(
2029 19, index, WASM_F32(1.0f), WASM_F32(2.0f), WASM_F32(4.0f),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002030 WASM_F32(8.0f), WASM_F32(16.0f), WASM_F32(32.0f),
2031 WASM_F32(64.0f), WASM_F32(128.0f), WASM_F32(256.0f),
2032 WASM_F32(1.5f), WASM_F32(2.5f), WASM_F32(4.5f), WASM_F32(8.5f),
2033 WASM_F32(16.5f), WASM_F32(32.5f), WASM_F32(64.5f),
2034 WASM_F32(128.5f), WASM_F32(256.5f), WASM_F32(512.5f)));
2035
2036 float result = r.Call();
2037 CHECK_EQ(256.5f, result);
2038}
2039
Ben Murdochc5610432016-08-08 18:44:38 +01002040WASM_EXEC_TEST(CallF64StackParameter) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002041 // Build the target function.
2042 LocalType param_types[20];
Ben Murdoch61f157c2016-09-16 13:49:30 +01002043 for (int i = 0; i < 20; ++i) param_types[i] = kAstF64;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002044 FunctionSig sig(1, 19, param_types);
Ben Murdoch61f157c2016-09-16 13:49:30 +01002045 TestingModule module(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002046 WasmFunctionCompiler t(&sig, &module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002047 BUILD(t, WASM_GET_LOCAL(17));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002048 uint32_t index = t.CompileAndAdd();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002049
2050 // Build the calling function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002051 WasmRunner<double> r(&module);
Ben Murdochc5610432016-08-08 18:44:38 +01002052 BUILD(r, WASM_CALL_FUNCTIONN(19, index, WASM_F64(1.0), WASM_F64(2.0),
2053 WASM_F64(4.0), WASM_F64(8.0), WASM_F64(16.0),
2054 WASM_F64(32.0), WASM_F64(64.0), WASM_F64(128.0),
2055 WASM_F64(256.0), WASM_F64(1.5), WASM_F64(2.5),
2056 WASM_F64(4.5), WASM_F64(8.5), WASM_F64(16.5),
2057 WASM_F64(32.5), WASM_F64(64.5), WASM_F64(128.5),
2058 WASM_F64(256.5), WASM_F64(512.5)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002059
2060 float result = r.Call();
2061 CHECK_EQ(256.5, result);
2062}
2063
Ben Murdochc5610432016-08-08 18:44:38 +01002064WASM_EXEC_TEST(CallVoid) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002065 const byte kMemOffset = 8;
2066 const int32_t kElemNum = kMemOffset / sizeof(int32_t);
2067 const int32_t kExpected = -414444;
2068 // Build the target function.
2069 TestSignatures sigs;
Ben Murdoch61f157c2016-09-16 13:49:30 +01002070 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002071 module.AddMemory(16);
2072 module.RandomizeMemory();
Ben Murdoch097c5b22016-05-18 11:27:45 +01002073 WasmFunctionCompiler t(sigs.v_v(), &module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002074 BUILD(t, WASM_STORE_MEM(MachineType::Int32(), WASM_I8(kMemOffset),
Ben Murdochda12d292016-06-02 14:46:10 +01002075 WASM_I32V_3(kExpected)));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002076 uint32_t index = t.CompileAndAdd();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002077
2078 // Build the calling function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002079 WasmRunner<int32_t> r(&module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002080 BUILD(r, WASM_CALL_FUNCTION0(index),
2081 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kMemOffset)));
2082
2083 int32_t result = r.Call();
2084 CHECK_EQ(kExpected, result);
2085 CHECK_EQ(kExpected, module.raw_mem_start<int32_t>()[kElemNum]);
2086}
2087
Ben Murdochc5610432016-08-08 18:44:38 +01002088WASM_EXEC_TEST(Call_Int32Add) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002089 // Build the target function.
2090 TestSignatures sigs;
Ben Murdoch61f157c2016-09-16 13:49:30 +01002091 TestingModule module(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002092 WasmFunctionCompiler t(sigs.i_ii(), &module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002093 BUILD(t, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002094 uint32_t index = t.CompileAndAdd();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002095
2096 // Build the caller function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002097 WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01002098 BUILD(r, WASM_CALL_FUNCTION2(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002099
2100 FOR_INT32_INPUTS(i) {
2101 FOR_INT32_INPUTS(j) {
2102 int32_t expected = static_cast<int32_t>(static_cast<uint32_t>(*i) +
2103 static_cast<uint32_t>(*j));
2104 CHECK_EQ(expected, r.Call(*i, *j));
2105 }
2106 }
2107}
2108
Ben Murdochc5610432016-08-08 18:44:38 +01002109WASM_EXEC_TEST(Call_Float32Sub) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002110 TestSignatures sigs;
Ben Murdoch61f157c2016-09-16 13:49:30 +01002111 TestingModule module(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002112 WasmFunctionCompiler t(sigs.f_ff(), &module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002113
2114 // Build the target function.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002115 BUILD(t, WASM_F32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002116 uint32_t index = t.CompileAndAdd();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002117
2118 // Builder the caller function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002119 WasmRunner<float> r(&module, MachineType::Float32(), MachineType::Float32());
Ben Murdochc5610432016-08-08 18:44:38 +01002120 BUILD(r, WASM_CALL_FUNCTION2(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002121
2122 FOR_FLOAT32_INPUTS(i) {
Ben Murdochda12d292016-06-02 14:46:10 +01002123 FOR_FLOAT32_INPUTS(j) { CHECK_FLOAT_EQ(*i - *j, r.Call(*i, *j)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002124 }
2125}
2126
Ben Murdochc5610432016-08-08 18:44:38 +01002127WASM_EXEC_TEST(Call_Float64Sub) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002128 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002129 double* memory = module.AddMemoryElems<double>(16);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002130 WasmRunner<int32_t> r(&module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002131
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002132 BUILD(r, WASM_BLOCK(
2133 2, WASM_STORE_MEM(
2134 MachineType::Float64(), WASM_ZERO,
2135 WASM_F64_SUB(
2136 WASM_LOAD_MEM(MachineType::Float64(), WASM_ZERO),
2137 WASM_LOAD_MEM(MachineType::Float64(), WASM_I8(8)))),
2138 WASM_I8(107)));
2139
2140 FOR_FLOAT64_INPUTS(i) {
2141 FOR_FLOAT64_INPUTS(j) {
2142 memory[0] = *i;
2143 memory[1] = *j;
2144 double expected = *i - *j;
2145 CHECK_EQ(107, r.Call());
2146 if (expected != expected) {
2147 CHECK(memory[0] != memory[0]);
2148 } else {
2149 CHECK_EQ(expected, memory[0]);
2150 }
2151 }
2152 }
2153}
2154
2155#define ADD_CODE(vec, ...) \
2156 do { \
2157 byte __buf[] = {__VA_ARGS__}; \
Ben Murdoch61f157c2016-09-16 13:49:30 +01002158 for (size_t i = 0; i < sizeof(__buf); ++i) vec.push_back(__buf[i]); \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002159 } while (false)
2160
Ben Murdoch61f157c2016-09-16 13:49:30 +01002161static void Run_WasmMixedCall_N(WasmExecutionMode execution_mode, int start) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002162 const int kExpected = 6333;
2163 const int kElemSize = 8;
2164 TestSignatures sigs;
2165
Ben Murdoch61f157c2016-09-16 13:49:30 +01002166 // 64-bit cases handled in test-run-wasm-64.cc.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002167 static MachineType mixed[] = {
2168 MachineType::Int32(), MachineType::Float32(), MachineType::Float64(),
2169 MachineType::Float32(), MachineType::Int32(), MachineType::Float64(),
2170 MachineType::Float32(), MachineType::Float64(), MachineType::Int32(),
2171 MachineType::Int32(), MachineType::Int32()};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002172
2173 int num_params = static_cast<int>(arraysize(mixed)) - start;
Ben Murdoch61f157c2016-09-16 13:49:30 +01002174 for (int which = 0; which < num_params; ++which) {
Ben Murdochda12d292016-06-02 14:46:10 +01002175 v8::base::AccountingAllocator allocator;
2176 Zone zone(&allocator);
Ben Murdoch61f157c2016-09-16 13:49:30 +01002177 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002178 module.AddMemory(1024);
2179 MachineType* memtypes = &mixed[start];
2180 MachineType result = memtypes[which];
2181
2182 // =========================================================================
2183 // Build the selector function.
2184 // =========================================================================
2185 uint32_t index;
2186 FunctionSig::Builder b(&zone, 1, num_params);
2187 b.AddReturn(WasmOpcodes::LocalTypeFor(result));
Ben Murdoch61f157c2016-09-16 13:49:30 +01002188 for (int i = 0; i < num_params; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002189 b.AddParam(WasmOpcodes::LocalTypeFor(memtypes[i]));
2190 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01002191 WasmFunctionCompiler t(b.Build(), &module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002192 BUILD(t, WASM_GET_LOCAL(which));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002193 index = t.CompileAndAdd();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002194
2195 // =========================================================================
2196 // Build the calling function.
2197 // =========================================================================
Ben Murdoch097c5b22016-05-18 11:27:45 +01002198 WasmRunner<int32_t> r(&module);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002199 std::vector<byte> code;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002200
Ben Murdochc5610432016-08-08 18:44:38 +01002201 // Load the offset for the store.
2202 ADD_CODE(code, WASM_ZERO);
2203
2204 // Load the arguments.
Ben Murdoch61f157c2016-09-16 13:49:30 +01002205 for (int i = 0; i < num_params; ++i) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002206 int offset = (i + 1) * kElemSize;
2207 ADD_CODE(code, WASM_LOAD_MEM(memtypes[i], WASM_I8(offset)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002208 }
2209
Ben Murdochc5610432016-08-08 18:44:38 +01002210 // Call the selector function.
2211 ADD_CODE(code, kExprCallFunction, static_cast<byte>(num_params),
2212 static_cast<byte>(index));
2213
2214 // Store the result in memory.
2215 ADD_CODE(code,
2216 static_cast<byte>(WasmOpcodes::LoadStoreOpcodeOf(result, true)),
2217 ZERO_ALIGNMENT, ZERO_OFFSET);
2218
2219 // Return the expected value.
Ben Murdochda12d292016-06-02 14:46:10 +01002220 ADD_CODE(code, WASM_I32V_2(kExpected));
Ben Murdochc5610432016-08-08 18:44:38 +01002221
2222 r.Build(&code[0], &code[0] + code.size());
Ben Murdoch097c5b22016-05-18 11:27:45 +01002223
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002224 // Run the code.
Ben Murdoch61f157c2016-09-16 13:49:30 +01002225 for (int t = 0; t < 10; ++t) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002226 module.RandomizeMemory();
2227 CHECK_EQ(kExpected, r.Call());
2228
2229 int size = WasmOpcodes::MemSize(result);
Ben Murdoch61f157c2016-09-16 13:49:30 +01002230 for (int i = 0; i < size; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002231 int base = (which + 1) * kElemSize;
2232 byte expected = module.raw_mem_at<byte>(base + i);
2233 byte result = module.raw_mem_at<byte>(i);
2234 CHECK_EQ(expected, result);
2235 }
2236 }
2237 }
2238}
2239
Ben Murdoch61f157c2016-09-16 13:49:30 +01002240WASM_EXEC_TEST(MixedCall_0) { Run_WasmMixedCall_N(execution_mode, 0); }
2241WASM_EXEC_TEST(MixedCall_1) { Run_WasmMixedCall_N(execution_mode, 1); }
2242WASM_EXEC_TEST(MixedCall_2) { Run_WasmMixedCall_N(execution_mode, 2); }
2243WASM_EXEC_TEST(MixedCall_3) { Run_WasmMixedCall_N(execution_mode, 3); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002244
Ben Murdochc5610432016-08-08 18:44:38 +01002245WASM_EXEC_TEST(AddCall) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002246 TestSignatures sigs;
Ben Murdoch61f157c2016-09-16 13:49:30 +01002247 TestingModule module(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002248 WasmFunctionCompiler t1(sigs.i_ii(), &module);
2249 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2250 t1.CompileAndAdd();
2251
2252 WasmRunner<int32_t> r(&module, MachineType::Int32());
2253 byte local = r.AllocateLocal(kAstI32);
Ben Murdochda12d292016-06-02 14:46:10 +01002254 BUILD(r, B2(WASM_SET_LOCAL(local, WASM_I8(99)),
2255 WASM_I32_ADD(
Ben Murdoch61f157c2016-09-16 13:49:30 +01002256 WASM_CALL_FUNCTION2(t1.function_index(), WASM_GET_LOCAL(0),
Ben Murdochc5610432016-08-08 18:44:38 +01002257 WASM_GET_LOCAL(0)),
Ben Murdoch61f157c2016-09-16 13:49:30 +01002258 WASM_CALL_FUNCTION2(t1.function_index(), WASM_GET_LOCAL(1),
Ben Murdochc5610432016-08-08 18:44:38 +01002259 WASM_GET_LOCAL(local)))));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002260
2261 CHECK_EQ(198, r.Call(0));
2262 CHECK_EQ(200, r.Call(1));
2263 CHECK_EQ(100, r.Call(-49));
2264}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002265
Ben Murdochc5610432016-08-08 18:44:38 +01002266WASM_EXEC_TEST(CountDown_expr) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002267 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002268 BUILD(r, WASM_LOOP(
2269 3, WASM_IF(WASM_NOT(WASM_GET_LOCAL(0)),
Ben Murdochc5610432016-08-08 18:44:38 +01002270 WASM_BREAKV(1, WASM_GET_LOCAL(0))),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002271 WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(1))),
2272 WASM_CONTINUE(0)));
2273 CHECK_EQ(0, r.Call(1));
2274 CHECK_EQ(0, r.Call(10));
2275 CHECK_EQ(0, r.Call(100));
2276}
2277
Ben Murdochc5610432016-08-08 18:44:38 +01002278WASM_EXEC_TEST(ExprBlock2a) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002279 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01002280 BUILD(r, B2(WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_I8(1))), WASM_I8(1)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002281 CHECK_EQ(1, r.Call(0));
2282 CHECK_EQ(1, r.Call(1));
2283}
2284
Ben Murdochc5610432016-08-08 18:44:38 +01002285WASM_EXEC_TEST(ExprBlock2b) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002286 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01002287 BUILD(r, B2(WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_I8(1))), WASM_I8(2)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002288 CHECK_EQ(2, r.Call(0));
2289 CHECK_EQ(1, r.Call(1));
2290}
2291
Ben Murdochc5610432016-08-08 18:44:38 +01002292WASM_EXEC_TEST(ExprBlock2c) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002293 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +01002294 BUILD(r, B2(WASM_BRV_IF(0, WASM_I8(1), WASM_GET_LOCAL(0)), WASM_I8(1)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002295 CHECK_EQ(1, r.Call(0));
2296 CHECK_EQ(1, r.Call(1));
2297}
2298
Ben Murdochc5610432016-08-08 18:44:38 +01002299WASM_EXEC_TEST(ExprBlock2d) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002300 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +01002301 BUILD(r, B2(WASM_BRV_IF(0, WASM_I8(1), WASM_GET_LOCAL(0)), WASM_I8(2)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002302 CHECK_EQ(2, r.Call(0));
2303 CHECK_EQ(1, r.Call(1));
2304}
2305
Ben Murdochc5610432016-08-08 18:44:38 +01002306WASM_EXEC_TEST(ExprBlock_ManualSwitch) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002307 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002308 BUILD(r, WASM_BLOCK(6, WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(1)),
Ben Murdochc5610432016-08-08 18:44:38 +01002309 WASM_BRV(1, WASM_I8(11))),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002310 WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(2)),
Ben Murdochc5610432016-08-08 18:44:38 +01002311 WASM_BRV(1, WASM_I8(12))),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002312 WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(3)),
Ben Murdochc5610432016-08-08 18:44:38 +01002313 WASM_BRV(1, WASM_I8(13))),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002314 WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(4)),
Ben Murdochc5610432016-08-08 18:44:38 +01002315 WASM_BRV(1, WASM_I8(14))),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002316 WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(5)),
Ben Murdochc5610432016-08-08 18:44:38 +01002317 WASM_BRV(1, WASM_I8(15))),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002318 WASM_I8(99)));
2319 CHECK_EQ(99, r.Call(0));
2320 CHECK_EQ(11, r.Call(1));
2321 CHECK_EQ(12, r.Call(2));
2322 CHECK_EQ(13, r.Call(3));
2323 CHECK_EQ(14, r.Call(4));
2324 CHECK_EQ(15, r.Call(5));
2325 CHECK_EQ(99, r.Call(6));
2326}
2327
Ben Murdochc5610432016-08-08 18:44:38 +01002328WASM_EXEC_TEST(ExprBlock_ManualSwitch_brif) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002329 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002330 BUILD(r,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002331 WASM_BLOCK(6, WASM_BRV_IF(0, WASM_I8(11),
2332 WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(1))),
2333 WASM_BRV_IF(0, WASM_I8(12),
2334 WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(2))),
2335 WASM_BRV_IF(0, WASM_I8(13),
2336 WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(3))),
2337 WASM_BRV_IF(0, WASM_I8(14),
2338 WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(4))),
2339 WASM_BRV_IF(0, WASM_I8(15),
2340 WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(5))),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002341 WASM_I8(99)));
2342 CHECK_EQ(99, r.Call(0));
2343 CHECK_EQ(11, r.Call(1));
2344 CHECK_EQ(12, r.Call(2));
2345 CHECK_EQ(13, r.Call(3));
2346 CHECK_EQ(14, r.Call(4));
2347 CHECK_EQ(15, r.Call(5));
2348 CHECK_EQ(99, r.Call(6));
2349}
2350
Ben Murdochc5610432016-08-08 18:44:38 +01002351WASM_EXEC_TEST(nested_ifs) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002352 WasmRunner<int32_t> r(execution_mode, MachineType::Int32(),
2353 MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002354
2355 BUILD(r, WASM_IF_ELSE(
2356 WASM_GET_LOCAL(0),
2357 WASM_IF_ELSE(WASM_GET_LOCAL(1), WASM_I8(11), WASM_I8(12)),
2358 WASM_IF_ELSE(WASM_GET_LOCAL(1), WASM_I8(13), WASM_I8(14))));
2359
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002360 CHECK_EQ(11, r.Call(1, 1));
2361 CHECK_EQ(12, r.Call(1, 0));
2362 CHECK_EQ(13, r.Call(0, 1));
2363 CHECK_EQ(14, r.Call(0, 0));
2364}
2365
Ben Murdochc5610432016-08-08 18:44:38 +01002366WASM_EXEC_TEST(ExprBlock_if) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002367 WasmRunner<int32_t> r(execution_mode, MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002368
Ben Murdochda12d292016-06-02 14:46:10 +01002369 BUILD(r, B1(WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_BRV(0, WASM_I8(11)),
Ben Murdochc5610432016-08-08 18:44:38 +01002370 WASM_BRV(1, WASM_I8(14)))));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002371
2372 CHECK_EQ(11, r.Call(1));
2373 CHECK_EQ(14, r.Call(0));
2374}
2375
Ben Murdochc5610432016-08-08 18:44:38 +01002376WASM_EXEC_TEST(ExprBlock_nested_ifs) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002377 WasmRunner<int32_t> r(execution_mode, MachineType::Int32(),
2378 MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002379
2380 BUILD(r, WASM_BLOCK(
2381 1, WASM_IF_ELSE(
2382 WASM_GET_LOCAL(0),
2383 WASM_IF_ELSE(WASM_GET_LOCAL(1), WASM_BRV(0, WASM_I8(11)),
Ben Murdochc5610432016-08-08 18:44:38 +01002384 WASM_BRV(1, WASM_I8(12))),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002385 WASM_IF_ELSE(WASM_GET_LOCAL(1), WASM_BRV(0, WASM_I8(13)),
Ben Murdochc5610432016-08-08 18:44:38 +01002386 WASM_BRV(1, WASM_I8(14))))));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002387
2388 CHECK_EQ(11, r.Call(1, 1));
2389 CHECK_EQ(12, r.Call(1, 0));
2390 CHECK_EQ(13, r.Call(0, 1));
2391 CHECK_EQ(14, r.Call(0, 0));
2392}
2393
Ben Murdochc5610432016-08-08 18:44:38 +01002394WASM_EXEC_TEST(ExprLoop_nested_ifs) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002395 WasmRunner<int32_t> r(execution_mode, MachineType::Int32(),
2396 MachineType::Int32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002397
2398 BUILD(r, WASM_LOOP(
2399 1, WASM_IF_ELSE(
2400 WASM_GET_LOCAL(0),
2401 WASM_IF_ELSE(WASM_GET_LOCAL(1), WASM_BRV(1, WASM_I8(11)),
Ben Murdochc5610432016-08-08 18:44:38 +01002402 WASM_BRV(3, WASM_I8(12))),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002403 WASM_IF_ELSE(WASM_GET_LOCAL(1), WASM_BRV(1, WASM_I8(13)),
Ben Murdochc5610432016-08-08 18:44:38 +01002404 WASM_BRV(3, WASM_I8(14))))));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002405
2406 CHECK_EQ(11, r.Call(1, 1));
2407 CHECK_EQ(12, r.Call(1, 0));
2408 CHECK_EQ(13, r.Call(0, 1));
2409 CHECK_EQ(14, r.Call(0, 0));
2410}
2411
Ben Murdochc5610432016-08-08 18:44:38 +01002412WASM_EXEC_TEST(SimpleCallIndirect) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002413 TestSignatures sigs;
Ben Murdoch61f157c2016-09-16 13:49:30 +01002414 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002415
Ben Murdoch097c5b22016-05-18 11:27:45 +01002416 WasmFunctionCompiler t1(sigs.i_ii(), &module);
2417 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2418 t1.CompileAndAdd(/*sig_index*/ 1);
2419
2420 WasmFunctionCompiler t2(sigs.i_ii(), &module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002421 BUILD(t2, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002422 t2.CompileAndAdd(/*sig_index*/ 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002423
2424 // Signature table.
2425 module.AddSignature(sigs.f_ff());
2426 module.AddSignature(sigs.i_ii());
2427 module.AddSignature(sigs.d_dd());
2428
2429 // Function table.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002430 int table[] = {0, 1};
2431 module.AddIndirectFunctionTable(table, 2);
2432 module.PopulateIndirectFunctionTable();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002433
2434 // Builder the caller function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002435 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01002436 BUILD(r, WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_I8(66), WASM_I8(22)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002437
2438 CHECK_EQ(88, r.Call(0));
2439 CHECK_EQ(44, r.Call(1));
2440 CHECK_TRAP(r.Call(2));
2441}
2442
Ben Murdochc5610432016-08-08 18:44:38 +01002443WASM_EXEC_TEST(MultipleCallIndirect) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002444 TestSignatures sigs;
Ben Murdoch61f157c2016-09-16 13:49:30 +01002445 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002446
Ben Murdoch097c5b22016-05-18 11:27:45 +01002447 WasmFunctionCompiler t1(sigs.i_ii(), &module);
2448 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2449 t1.CompileAndAdd(/*sig_index*/ 1);
2450
2451 WasmFunctionCompiler t2(sigs.i_ii(), &module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002452 BUILD(t2, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002453 t2.CompileAndAdd(/*sig_index*/ 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002454
2455 // Signature table.
2456 module.AddSignature(sigs.f_ff());
2457 module.AddSignature(sigs.i_ii());
2458 module.AddSignature(sigs.d_dd());
2459
2460 // Function table.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002461 int table[] = {0, 1};
2462 module.AddIndirectFunctionTable(table, 2);
2463 module.PopulateIndirectFunctionTable();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002464
2465 // Builder the caller function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002466 WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32(),
2467 MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01002468 BUILD(r, WASM_I32_ADD(
2469 WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
2470 WASM_GET_LOCAL(2)),
2471 WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(1), WASM_GET_LOCAL(2),
2472 WASM_GET_LOCAL(0))));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002473
2474 CHECK_EQ(5, r.Call(0, 1, 2));
2475 CHECK_EQ(19, r.Call(0, 1, 9));
2476 CHECK_EQ(1, r.Call(1, 0, 2));
2477 CHECK_EQ(1, r.Call(1, 0, 9));
2478
2479 CHECK_TRAP(r.Call(0, 2, 1));
2480 CHECK_TRAP(r.Call(1, 2, 0));
2481 CHECK_TRAP(r.Call(2, 0, 1));
2482 CHECK_TRAP(r.Call(2, 1, 0));
2483}
2484
Ben Murdochc5610432016-08-08 18:44:38 +01002485WASM_EXEC_TEST(CallIndirect_NoTable) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002486 TestSignatures sigs;
Ben Murdoch61f157c2016-09-16 13:49:30 +01002487 TestingModule module(execution_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002488
Ben Murdoch097c5b22016-05-18 11:27:45 +01002489 // One function.
2490 WasmFunctionCompiler t1(sigs.i_ii(), &module);
2491 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2492 t1.CompileAndAdd(/*sig_index*/ 1);
2493
2494 // Signature table.
2495 module.AddSignature(sigs.f_ff());
2496 module.AddSignature(sigs.i_ii());
2497
2498 // Builder the caller function.
2499 WasmRunner<int32_t> r(&module, MachineType::Int32());
Ben Murdochc5610432016-08-08 18:44:38 +01002500 BUILD(r, WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_I8(66), WASM_I8(22)));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002501
2502 CHECK_TRAP(r.Call(0));
2503 CHECK_TRAP(r.Call(1));
2504 CHECK_TRAP(r.Call(2));
2505}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002506
Ben Murdochc5610432016-08-08 18:44:38 +01002507WASM_EXEC_TEST(F32Floor) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002508 WasmRunner<float> r(execution_mode, MachineType::Float32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002509 BUILD(r, WASM_F32_FLOOR(WASM_GET_LOCAL(0)));
2510
Ben Murdochda12d292016-06-02 14:46:10 +01002511 FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(floorf(*i), r.Call(*i)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002512}
2513
Ben Murdochc5610432016-08-08 18:44:38 +01002514WASM_EXEC_TEST(F32Ceil) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002515 WasmRunner<float> r(execution_mode, MachineType::Float32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002516 BUILD(r, WASM_F32_CEIL(WASM_GET_LOCAL(0)));
2517
Ben Murdochda12d292016-06-02 14:46:10 +01002518 FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(ceilf(*i), r.Call(*i)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002519}
2520
Ben Murdochc5610432016-08-08 18:44:38 +01002521WASM_EXEC_TEST(F32Trunc) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002522 WasmRunner<float> r(execution_mode, MachineType::Float32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002523 BUILD(r, WASM_F32_TRUNC(WASM_GET_LOCAL(0)));
2524
Ben Murdochda12d292016-06-02 14:46:10 +01002525 FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(truncf(*i), r.Call(*i)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002526}
2527
Ben Murdochc5610432016-08-08 18:44:38 +01002528WASM_EXEC_TEST(F32NearestInt) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002529 WasmRunner<float> r(execution_mode, MachineType::Float32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002530 BUILD(r, WASM_F32_NEARESTINT(WASM_GET_LOCAL(0)));
2531
Ben Murdochda12d292016-06-02 14:46:10 +01002532 FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(nearbyintf(*i), r.Call(*i)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002533}
2534
Ben Murdochc5610432016-08-08 18:44:38 +01002535WASM_EXEC_TEST(F64Floor) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002536 WasmRunner<double> r(execution_mode, MachineType::Float64());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002537 BUILD(r, WASM_F64_FLOOR(WASM_GET_LOCAL(0)));
2538
Ben Murdochda12d292016-06-02 14:46:10 +01002539 FOR_FLOAT64_INPUTS(i) { CHECK_DOUBLE_EQ(floor(*i), r.Call(*i)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002540}
2541
Ben Murdochc5610432016-08-08 18:44:38 +01002542WASM_EXEC_TEST(F64Ceil) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002543 WasmRunner<double> r(execution_mode, MachineType::Float64());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002544 BUILD(r, WASM_F64_CEIL(WASM_GET_LOCAL(0)));
2545
Ben Murdochda12d292016-06-02 14:46:10 +01002546 FOR_FLOAT64_INPUTS(i) { CHECK_DOUBLE_EQ(ceil(*i), r.Call(*i)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002547}
2548
Ben Murdochc5610432016-08-08 18:44:38 +01002549WASM_EXEC_TEST(F64Trunc) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002550 WasmRunner<double> r(execution_mode, MachineType::Float64());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002551 BUILD(r, WASM_F64_TRUNC(WASM_GET_LOCAL(0)));
2552
Ben Murdochda12d292016-06-02 14:46:10 +01002553 FOR_FLOAT64_INPUTS(i) { CHECK_DOUBLE_EQ(trunc(*i), r.Call(*i)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002554}
2555
Ben Murdochc5610432016-08-08 18:44:38 +01002556WASM_EXEC_TEST(F64NearestInt) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002557 WasmRunner<double> r(execution_mode, MachineType::Float64());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002558 BUILD(r, WASM_F64_NEARESTINT(WASM_GET_LOCAL(0)));
2559
Ben Murdochda12d292016-06-02 14:46:10 +01002560 FOR_FLOAT64_INPUTS(i) { CHECK_DOUBLE_EQ(nearbyint(*i), r.Call(*i)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002561}
2562
Ben Murdochc5610432016-08-08 18:44:38 +01002563WASM_EXEC_TEST(F32Min) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002564 WasmRunner<float> r(execution_mode, MachineType::Float32(),
2565 MachineType::Float32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002566 BUILD(r, WASM_F32_MIN(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2567
2568 FOR_FLOAT32_INPUTS(i) {
2569 FOR_FLOAT32_INPUTS(j) {
2570 float expected;
2571 if (*i < *j) {
2572 expected = *i;
2573 } else if (*j < *i) {
2574 expected = *j;
2575 } else if (*i != *i) {
2576 // If *i or *j is NaN, then the result is NaN.
2577 expected = *i;
2578 } else {
2579 expected = *j;
2580 }
2581
Ben Murdochda12d292016-06-02 14:46:10 +01002582 CHECK_FLOAT_EQ(expected, r.Call(*i, *j));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002583 }
2584 }
2585}
2586
Ben Murdochc5610432016-08-08 18:44:38 +01002587WASM_EXEC_TEST(F64Min) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002588 WasmRunner<double> r(execution_mode, MachineType::Float64(),
2589 MachineType::Float64());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002590 BUILD(r, WASM_F64_MIN(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2591
2592 FOR_FLOAT64_INPUTS(i) {
2593 FOR_FLOAT64_INPUTS(j) {
2594 double expected;
2595 if (*i < *j) {
2596 expected = *i;
2597 } else if (*j < *i) {
2598 expected = *j;
2599 } else if (*i != *i) {
2600 // If *i or *j is NaN, then the result is NaN.
2601 expected = *i;
2602 } else {
2603 expected = *j;
2604 }
2605
Ben Murdochda12d292016-06-02 14:46:10 +01002606 CHECK_DOUBLE_EQ(expected, r.Call(*i, *j));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002607 }
2608 }
2609}
2610
Ben Murdochc5610432016-08-08 18:44:38 +01002611WASM_EXEC_TEST(F32Max) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002612 WasmRunner<float> r(execution_mode, MachineType::Float32(),
2613 MachineType::Float32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002614 BUILD(r, WASM_F32_MAX(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2615
2616 FOR_FLOAT32_INPUTS(i) {
2617 FOR_FLOAT32_INPUTS(j) {
2618 float expected;
2619 if (*i > *j) {
2620 expected = *i;
2621 } else if (*j > *i) {
2622 expected = *j;
2623 } else if (*i != *i) {
2624 // If *i or *j is NaN, then the result is NaN.
2625 expected = *i;
2626 } else {
2627 expected = *j;
2628 }
2629
Ben Murdochda12d292016-06-02 14:46:10 +01002630 CHECK_FLOAT_EQ(expected, r.Call(*i, *j));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002631 }
2632 }
2633}
2634
Ben Murdochc5610432016-08-08 18:44:38 +01002635WASM_EXEC_TEST(F64Max) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002636 WasmRunner<double> r(execution_mode, MachineType::Float64(),
2637 MachineType::Float64());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002638 BUILD(r, WASM_F64_MAX(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2639
2640 FOR_FLOAT64_INPUTS(i) {
2641 FOR_FLOAT64_INPUTS(j) {
2642 double expected;
2643 if (*i > *j) {
2644 expected = *i;
2645 } else if (*j > *i) {
2646 expected = *j;
2647 } else if (*i != *i) {
2648 // If *i or *j is NaN, then the result is NaN.
2649 expected = *i;
2650 } else {
2651 expected = *j;
2652 }
2653
Ben Murdochda12d292016-06-02 14:46:10 +01002654 CHECK_DOUBLE_EQ(expected, r.Call(*i, *j));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002655 }
2656 }
2657}
2658
Ben Murdochc5610432016-08-08 18:44:38 +01002659// TODO(ahaas): Fix on mips and reenable.
2660#if !V8_TARGET_ARCH_MIPS && !V8_TARGET_ARCH_MIPS64
Ben Murdoch097c5b22016-05-18 11:27:45 +01002661
Ben Murdochc5610432016-08-08 18:44:38 +01002662WASM_EXEC_TEST(F32Min_Snan) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002663 // Test that the instruction does not return a signalling NaN.
2664 {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002665 WasmRunner<float> r(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002666 BUILD(r,
2667 WASM_F32_MIN(WASM_F32(bit_cast<float>(0xff80f1e2)), WASM_F32(57.67)));
2668 CHECK_EQ(0xffc0f1e2, bit_cast<uint32_t>(r.Call()));
2669 }
2670 {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002671 WasmRunner<float> r(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002672 BUILD(r,
2673 WASM_F32_MIN(WASM_F32(45.73), WASM_F32(bit_cast<float>(0x7f80f1e2))));
2674 CHECK_EQ(0x7fc0f1e2, bit_cast<uint32_t>(r.Call()));
2675 }
2676}
2677
Ben Murdochc5610432016-08-08 18:44:38 +01002678WASM_EXEC_TEST(F32Max_Snan) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002679 // Test that the instruction does not return a signalling NaN.
2680 {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002681 WasmRunner<float> r(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002682 BUILD(r,
2683 WASM_F32_MAX(WASM_F32(bit_cast<float>(0xff80f1e2)), WASM_F32(57.67)));
2684 CHECK_EQ(0xffc0f1e2, bit_cast<uint32_t>(r.Call()));
2685 }
2686 {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002687 WasmRunner<float> r(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002688 BUILD(r,
2689 WASM_F32_MAX(WASM_F32(45.73), WASM_F32(bit_cast<float>(0x7f80f1e2))));
2690 CHECK_EQ(0x7fc0f1e2, bit_cast<uint32_t>(r.Call()));
2691 }
2692}
2693
Ben Murdochc5610432016-08-08 18:44:38 +01002694WASM_EXEC_TEST(F64Min_Snan) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002695 // Test that the instruction does not return a signalling NaN.
2696 {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002697 WasmRunner<double> r(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002698 BUILD(r, WASM_F64_MIN(WASM_F64(bit_cast<double>(0xfff000000000f1e2)),
2699 WASM_F64(57.67)));
2700 CHECK_EQ(0xfff800000000f1e2, bit_cast<uint64_t>(r.Call()));
2701 }
2702 {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002703 WasmRunner<double> r(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002704 BUILD(r, WASM_F64_MIN(WASM_F64(45.73),
2705 WASM_F64(bit_cast<double>(0x7ff000000000f1e2))));
2706 CHECK_EQ(0x7ff800000000f1e2, bit_cast<uint64_t>(r.Call()));
2707 }
2708}
2709
Ben Murdochc5610432016-08-08 18:44:38 +01002710WASM_EXEC_TEST(F64Max_Snan) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002711 // Test that the instruction does not return a signalling NaN.
2712 {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002713 WasmRunner<double> r(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002714 BUILD(r, WASM_F64_MAX(WASM_F64(bit_cast<double>(0xfff000000000f1e2)),
2715 WASM_F64(57.67)));
2716 CHECK_EQ(0xfff800000000f1e2, bit_cast<uint64_t>(r.Call()));
2717 }
2718 {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002719 WasmRunner<double> r(execution_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002720 BUILD(r, WASM_F64_MAX(WASM_F64(45.73),
2721 WASM_F64(bit_cast<double>(0x7ff000000000f1e2))));
2722 CHECK_EQ(0x7ff800000000f1e2, bit_cast<uint64_t>(r.Call()));
2723 }
2724}
2725
2726#endif
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002727
Ben Murdochc5610432016-08-08 18:44:38 +01002728WASM_EXEC_TEST(I32SConvertF32) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002729 WasmRunner<int32_t> r(execution_mode, MachineType::Float32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002730 BUILD(r, WASM_I32_SCONVERT_F32(WASM_GET_LOCAL(0)));
2731
2732 FOR_FLOAT32_INPUTS(i) {
2733 if (*i < static_cast<float>(INT32_MAX) &&
2734 *i >= static_cast<float>(INT32_MIN)) {
2735 CHECK_EQ(static_cast<int32_t>(*i), r.Call(*i));
2736 } else {
2737 CHECK_TRAP32(r.Call(*i));
2738 }
2739 }
2740}
2741
Ben Murdochc5610432016-08-08 18:44:38 +01002742WASM_EXEC_TEST(I32SConvertF64) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002743 WasmRunner<int32_t> r(execution_mode, MachineType::Float64());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002744 BUILD(r, WASM_I32_SCONVERT_F64(WASM_GET_LOCAL(0)));
2745
2746 FOR_FLOAT64_INPUTS(i) {
Ben Murdochda12d292016-06-02 14:46:10 +01002747 if (*i < (static_cast<double>(INT32_MAX) + 1.0) &&
2748 *i > (static_cast<double>(INT32_MIN) - 1.0)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002749 CHECK_EQ(static_cast<int64_t>(*i), r.Call(*i));
2750 } else {
2751 CHECK_TRAP32(r.Call(*i));
2752 }
2753 }
2754}
2755
Ben Murdochc5610432016-08-08 18:44:38 +01002756WASM_EXEC_TEST(I32UConvertF32) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002757 WasmRunner<uint32_t> r(execution_mode, MachineType::Float32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002758 BUILD(r, WASM_I32_UCONVERT_F32(WASM_GET_LOCAL(0)));
2759
2760 FOR_FLOAT32_INPUTS(i) {
Ben Murdochda12d292016-06-02 14:46:10 +01002761 if (*i < (static_cast<float>(UINT32_MAX) + 1.0) && *i > -1) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002762 CHECK_EQ(static_cast<uint32_t>(*i), r.Call(*i));
2763 } else {
2764 CHECK_TRAP32(r.Call(*i));
2765 }
2766 }
2767}
2768
Ben Murdochc5610432016-08-08 18:44:38 +01002769WASM_EXEC_TEST(I32UConvertF64) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002770 WasmRunner<uint32_t> r(execution_mode, MachineType::Float64());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002771 BUILD(r, WASM_I32_UCONVERT_F64(WASM_GET_LOCAL(0)));
2772
2773 FOR_FLOAT64_INPUTS(i) {
Ben Murdochda12d292016-06-02 14:46:10 +01002774 if (*i < (static_cast<float>(UINT32_MAX) + 1.0) && *i > -1) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002775 CHECK_EQ(static_cast<uint32_t>(*i), r.Call(*i));
2776 } else {
2777 CHECK_TRAP32(r.Call(*i));
2778 }
2779 }
2780}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002781
Ben Murdochc5610432016-08-08 18:44:38 +01002782WASM_EXEC_TEST(F64CopySign) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002783 WasmRunner<double> r(execution_mode, MachineType::Float64(),
2784 MachineType::Float64());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002785 BUILD(r, WASM_F64_COPYSIGN(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2786
2787 FOR_FLOAT64_INPUTS(i) {
Ben Murdochda12d292016-06-02 14:46:10 +01002788 FOR_FLOAT64_INPUTS(j) { CHECK_DOUBLE_EQ(copysign(*i, *j), r.Call(*i, *j)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002789 }
2790}
2791
Ben Murdochc5610432016-08-08 18:44:38 +01002792WASM_EXEC_TEST(F32CopySign) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002793 WasmRunner<float> r(execution_mode, MachineType::Float32(),
2794 MachineType::Float32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002795 BUILD(r, WASM_F32_COPYSIGN(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2796
2797 FOR_FLOAT32_INPUTS(i) {
Ben Murdochda12d292016-06-02 14:46:10 +01002798 FOR_FLOAT32_INPUTS(j) { CHECK_FLOAT_EQ(copysignf(*i, *j), r.Call(*i, *j)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002799 }
2800}
2801
Ben Murdoch61f157c2016-09-16 13:49:30 +01002802static void CompileCallIndirectMany(LocalType param) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002803 // Make sure we don't run out of registers when compiling indirect calls
2804 // with many many parameters.
2805 TestSignatures sigs;
Ben Murdoch61f157c2016-09-16 13:49:30 +01002806 for (byte num_params = 0; num_params < 40; ++num_params) {
Ben Murdochda12d292016-06-02 14:46:10 +01002807 v8::base::AccountingAllocator allocator;
2808 Zone zone(&allocator);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002809 HandleScope scope(CcTest::InitIsolateOnce());
Ben Murdoch61f157c2016-09-16 13:49:30 +01002810 TestingModule module(kExecuteCompiled);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002811 FunctionSig* sig = sigs.many(&zone, kAstStmt, param, num_params);
2812
2813 module.AddSignature(sig);
2814 module.AddSignature(sig);
2815 module.AddIndirectFunctionTable(nullptr, 0);
2816
2817 WasmFunctionCompiler t(sig, &module);
2818
2819 std::vector<byte> code;
Ben Murdoch097c5b22016-05-18 11:27:45 +01002820 ADD_CODE(code, kExprI8Const, 0);
Ben Murdoch61f157c2016-09-16 13:49:30 +01002821 for (byte p = 0; p < num_params; ++p) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002822 ADD_CODE(code, kExprGetLocal, p);
2823 }
Ben Murdochc5610432016-08-08 18:44:38 +01002824 ADD_CODE(code, kExprCallIndirect, static_cast<byte>(num_params), 1);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002825
2826 t.Build(&code[0], &code[0] + code.size());
2827 t.Compile();
2828 }
2829}
2830
Ben Murdoch097c5b22016-05-18 11:27:45 +01002831TEST(Compile_Wasm_CallIndirect_Many_i32) { CompileCallIndirectMany(kAstI32); }
2832
Ben Murdoch097c5b22016-05-18 11:27:45 +01002833TEST(Compile_Wasm_CallIndirect_Many_f32) { CompileCallIndirectMany(kAstF32); }
2834
Ben Murdoch097c5b22016-05-18 11:27:45 +01002835TEST(Compile_Wasm_CallIndirect_Many_f64) { CompileCallIndirectMany(kAstF64); }
Ben Murdochda12d292016-06-02 14:46:10 +01002836
Ben Murdochc5610432016-08-08 18:44:38 +01002837WASM_EXEC_TEST(Int32RemS_dead) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01002838 WasmRunner<int32_t> r(execution_mode, MachineType::Int32(),
2839 MachineType::Int32());
Ben Murdochda12d292016-06-02 14:46:10 +01002840 BUILD(r, WASM_I32_REMS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)), WASM_ZERO);
2841 const int32_t kMin = std::numeric_limits<int32_t>::min();
2842 CHECK_EQ(0, r.Call(133, 100));
2843 CHECK_EQ(0, r.Call(kMin, -1));
2844 CHECK_EQ(0, r.Call(0, 1));
2845 CHECK_TRAP(r.Call(100, 0));
2846 CHECK_TRAP(r.Call(-1001, 0));
2847 CHECK_TRAP(r.Call(kMin, 0));
2848}