Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1005)

Side by Side Diff: test/unittests/wasm/ast-decoder-unittest.cc

Issue 1830663002: [wasm] Binary 11: AST changes. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Renumber opcodes and remove tests added elsewhere Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "test/unittests/test-utils.h" 5 #include "test/unittests/test-utils.h"
6 6
7 #include "src/v8.h" 7 #include "src/v8.h"
8 8
9 #include "test/cctest/wasm/test-signatures.h" 9 #include "test/cctest/wasm/test-signatures.h"
10 10
11 #include "src/objects.h" 11 #include "src/objects.h"
12 12
13 #include "src/wasm/ast-decoder.h" 13 #include "src/wasm/ast-decoder.h"
14 #include "src/wasm/wasm-macro-gen.h" 14 #include "src/wasm/wasm-macro-gen.h"
15 #include "src/wasm/wasm-module.h" 15 #include "src/wasm/wasm-module.h"
16 16
17 namespace v8 { 17 namespace v8 {
18 namespace internal { 18 namespace internal {
19 namespace wasm { 19 namespace wasm {
20 20
21 #define B1(a) kExprBlock, a, kExprEnd
22 #define B2(a, b) kExprBlock, a, b, kExprEnd
23 #define B3(a, b, c) kExprBlock, a, b, c, kExprEnd
24
21 static const byte kCodeGetLocal0[] = {kExprGetLocal, 0}; 25 static const byte kCodeGetLocal0[] = {kExprGetLocal, 0};
22 static const byte kCodeGetLocal1[] = {kExprGetLocal, 1}; 26 static const byte kCodeGetLocal1[] = {kExprGetLocal, 1};
23 static const byte kCodeSetLocal0[] = {kExprSetLocal, 0, kExprI8Const, 0}; 27 static const byte kCodeSetLocal0[] = {WASM_SET_LOCAL(0, WASM_ZERO)};
24 28
25 static const LocalType kLocalTypes[] = {kAstI32, kAstI64, kAstF32, kAstF64}; 29 static const LocalType kLocalTypes[] = {kAstI32, kAstI64, kAstF32, kAstF64};
26 static const MachineType machineTypes[] = { 30 static const MachineType machineTypes[] = {
27 MachineType::Int8(), MachineType::Uint8(), MachineType::Int16(), 31 MachineType::Int8(), MachineType::Uint8(), MachineType::Int16(),
28 MachineType::Uint16(), MachineType::Int32(), MachineType::Uint32(), 32 MachineType::Uint16(), MachineType::Int32(), MachineType::Uint32(),
29 MachineType::Int64(), MachineType::Uint64(), MachineType::Float32(), 33 MachineType::Int64(), MachineType::Uint64(), MachineType::Float32(),
30 MachineType::Float64()}; 34 MachineType::Float64()};
31 35
32 static const WasmOpcode kInt32BinopOpcodes[] = { 36 static const WasmOpcode kInt32BinopOpcodes[] = {
33 kExprI32Add, kExprI32Sub, kExprI32Mul, kExprI32DivS, kExprI32DivU, 37 kExprI32Add, kExprI32Sub, kExprI32Mul, kExprI32DivS, kExprI32DivU,
34 kExprI32RemS, kExprI32RemU, kExprI32And, kExprI32Ior, kExprI32Xor, 38 kExprI32RemS, kExprI32RemU, kExprI32And, kExprI32Ior, kExprI32Xor,
35 kExprI32Shl, kExprI32ShrU, kExprI32ShrS, kExprI32Eq, kExprI32LtS, 39 kExprI32Shl, kExprI32ShrU, kExprI32ShrS, kExprI32Eq, kExprI32LtS,
36 kExprI32LeS, kExprI32LtU, kExprI32LeU}; 40 kExprI32LeS, kExprI32LtU, kExprI32LeU};
37 41
38 #define WASM_BRV_IF_ZERO(depth, val) \ 42 #define WASM_BRV_IF_ZERO(depth, val) \
39 kExprBrIf, static_cast<byte>(depth), val, WASM_ZERO 43 val, WASM_ZERO, kExprBrIf, static_cast<byte>(depth)
40 44
41 #define EXPECT_VERIFIES(env, x) Verify(kSuccess, env, x, x + arraysize(x)) 45 #define EXPECT_VERIFIES(env, x) Verify(kSuccess, env, x, x + arraysize(x))
42 46
43 #define EXPECT_FAILURE(env, x) Verify(kError, env, x, x + arraysize(x)) 47 #define EXPECT_FAILURE(env, x) Verify(kError, env, x, x + arraysize(x))
44 48
45 #define EXPECT_VERIFIES_INLINE(env, ...) \ 49 #define EXPECT_VERIFIES_INLINE(env, ...) \
46 do { \ 50 do { \
47 static byte code[] = {__VA_ARGS__}; \ 51 static byte code[] = {__VA_ARGS__}; \
48 Verify(kSuccess, env, code, code + arraysize(code)); \ 52 Verify(kSuccess, env, code, code + arraysize(code)); \
49 } while (false) 53 } while (false)
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 } 103 }
100 } 104 }
101 FATAL(str.str().c_str()); 105 FATAL(str.str().c_str());
102 } 106 }
103 107
104 delete[] start; // local_decls.Prepend() allocated a new buffer. 108 delete[] start; // local_decls.Prepend() allocated a new buffer.
105 } 109 }
106 110
107 void TestBinop(WasmOpcode opcode, FunctionSig* success) { 111 void TestBinop(WasmOpcode opcode, FunctionSig* success) {
108 // op(local[0], local[1]) 112 // op(local[0], local[1])
109 byte code[] = {static_cast<byte>(opcode), kExprGetLocal, 0, kExprGetLocal, 113 byte code[] = {WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))};
110 1};
111 EXPECT_VERIFIES(success, code); 114 EXPECT_VERIFIES(success, code);
112 115
113 // Try all combinations of return and parameter types. 116 // Try all combinations of return and parameter types.
114 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 117 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
115 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { 118 for (size_t j = 0; j < arraysize(kLocalTypes); j++) {
116 for (size_t k = 0; k < arraysize(kLocalTypes); k++) { 119 for (size_t k = 0; k < arraysize(kLocalTypes); k++) {
117 LocalType types[] = {kLocalTypes[i], kLocalTypes[j], kLocalTypes[k]}; 120 LocalType types[] = {kLocalTypes[i], kLocalTypes[j], kLocalTypes[k]};
118 if (types[0] != success->GetReturn(0) || 121 if (types[0] != success->GetReturn(0) ||
119 types[1] != success->GetParam(0) || 122 types[1] != success->GetParam(0) ||
120 types[2] != success->GetParam(1)) { 123 types[2] != success->GetParam(1)) {
121 // Test signature mismatch. 124 // Test signature mismatch.
122 FunctionSig sig(1, 2, types); 125 FunctionSig sig(1, 2, types);
123 EXPECT_FAILURE(&sig, code); 126 EXPECT_FAILURE(&sig, code);
124 } 127 }
125 } 128 }
126 } 129 }
127 } 130 }
128 } 131 }
129 132
130 void TestUnop(WasmOpcode opcode, FunctionSig* success) { 133 void TestUnop(WasmOpcode opcode, FunctionSig* success) {
131 TestUnop(opcode, success->GetReturn(), success->GetParam(0)); 134 TestUnop(opcode, success->GetReturn(), success->GetParam(0));
132 } 135 }
133 136
134 void TestUnop(WasmOpcode opcode, LocalType ret_type, LocalType param_type) { 137 void TestUnop(WasmOpcode opcode, LocalType ret_type, LocalType param_type) {
135 // Return(op(local[0])) 138 // Return(op(local[0]))
136 byte code[] = {static_cast<byte>(opcode), kExprGetLocal, 0}; 139 byte code[] = {WASM_UNOP(opcode, WASM_GET_LOCAL(0))};
137 { 140 {
138 LocalType types[] = {ret_type, param_type}; 141 LocalType types[] = {ret_type, param_type};
139 FunctionSig sig(1, 1, types); 142 FunctionSig sig(1, 1, types);
140 EXPECT_VERIFIES(&sig, code); 143 EXPECT_VERIFIES(&sig, code);
141 } 144 }
142 145
143 // Try all combinations of return and parameter types. 146 // Try all combinations of return and parameter types.
144 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 147 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
145 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { 148 for (size_t j = 0; j < arraysize(kLocalTypes); j++) {
146 LocalType types[] = {kLocalTypes[i], kLocalTypes[j]}; 149 LocalType types[] = {kLocalTypes[i], kLocalTypes[j]};
147 if (types[0] != ret_type || types[1] != param_type) { 150 if (types[0] != ret_type || types[1] != param_type) {
148 // Test signature mismatch. 151 // Test signature mismatch.
149 FunctionSig sig(1, 1, types); 152 FunctionSig sig(1, 1, types);
150 EXPECT_FAILURE(&sig, code); 153 EXPECT_FAILURE(&sig, code);
151 } 154 }
152 } 155 }
153 } 156 }
154 } 157 }
155 }; 158 };
156 159
157
158 TEST_F(AstDecoderTest, Int8Const) { 160 TEST_F(AstDecoderTest, Int8Const) {
159 byte code[] = {kExprI8Const, 0}; 161 byte code[] = {kExprI8Const, 0};
160 for (int i = -128; i < 128; i++) { 162 for (int i = -128; i < 128; i++) {
161 code[1] = static_cast<byte>(i); 163 code[1] = static_cast<byte>(i);
162 EXPECT_VERIFIES(sigs.i_i(), code); 164 EXPECT_VERIFIES(sigs.i_i(), code);
163 } 165 }
164 } 166 }
165 167
166 TEST_F(AstDecoderTest, EmptyFunction) { 168 TEST_F(AstDecoderTest, EmptyFunction) {
167 byte code[] = {0}; 169 byte code[] = {0};
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 EXPECT_FAILURE_INLINE(sigs.i_v(), kExprGetLocal, U32V_4(kMaxLocals + 1)); 297 EXPECT_FAILURE_INLINE(sigs.i_v(), kExprGetLocal, U32V_4(kMaxLocals + 1));
296 } 298 }
297 299
298 TEST_F(AstDecoderTest, Binops_off_end) { 300 TEST_F(AstDecoderTest, Binops_off_end) {
299 byte code1[] = {0}; // [opcode] 301 byte code1[] = {0}; // [opcode]
300 for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) { 302 for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) {
301 code1[0] = kInt32BinopOpcodes[i]; 303 code1[0] = kInt32BinopOpcodes[i];
302 EXPECT_FAILURE(sigs.i_i(), code1); 304 EXPECT_FAILURE(sigs.i_i(), code1);
303 } 305 }
304 306
305 byte code3[] = {0, kExprGetLocal, 0}; // [opcode] [expr] 307 byte code3[] = {kExprGetLocal, 0, 0}; // [expr] [opcode]
306 for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) { 308 for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) {
307 code3[0] = kInt32BinopOpcodes[i]; 309 code3[2] = kInt32BinopOpcodes[i];
308 EXPECT_FAILURE(sigs.i_i(), code3); 310 EXPECT_FAILURE(sigs.i_i(), code3);
309 } 311 }
310 312
311 byte code4[] = {0, kExprGetLocal, 0, 0}; // [opcode] [expr] [opcode] 313 byte code4[] = {kExprGetLocal, 0, 0, 0}; // [expr] [opcode] [opcode]
312 for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) { 314 for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) {
313 code4[0] = kInt32BinopOpcodes[i]; 315 code4[1] = kInt32BinopOpcodes[i];
314 code4[3] = kInt32BinopOpcodes[i]; 316 code4[3] = kInt32BinopOpcodes[i];
315 EXPECT_FAILURE(sigs.i_i(), code4); 317 EXPECT_FAILURE(sigs.i_i(), code4);
316 } 318 }
317 } 319 }
318 320
319 321
320 //=================================================================== 322 //===================================================================
321 //== Statements 323 //== Statements
322 //=================================================================== 324 //===================================================================
323 TEST_F(AstDecoderTest, Nop) { 325 TEST_F(AstDecoderTest, Nop) {
324 static const byte code[] = {kExprNop}; 326 static const byte code[] = {kExprNop};
325 EXPECT_VERIFIES(sigs.v_v(), code); 327 EXPECT_VERIFIES(sigs.v_v(), code);
326 } 328 }
327 329
328 TEST_F(AstDecoderTest, SetLocal0_param) { 330 TEST_F(AstDecoderTest, SetLocal0_param) {
329 static const byte code[] = {kExprSetLocal, 0, kExprI8Const, 0}; 331 EXPECT_VERIFIES(sigs.i_i(), kCodeSetLocal0);
330 EXPECT_VERIFIES(sigs.i_i(), code); 332 EXPECT_FAILURE(sigs.f_ff(), kCodeSetLocal0);
333 EXPECT_FAILURE(sigs.d_dd(), kCodeSetLocal0);
331 } 334 }
332 335
333 TEST_F(AstDecoderTest, SetLocal0_local) { 336 TEST_F(AstDecoderTest, SetLocal0_local) {
334 byte code[] = {kExprSetLocal, 0, kExprI8Const, 0}; 337 EXPECT_FAILURE(sigs.i_v(), kCodeSetLocal0);
335 AddLocals(kAstI32, 1); 338 AddLocals(kAstI32, 1);
336 EXPECT_VERIFIES(sigs.i_v(), code); 339 EXPECT_VERIFIES(sigs.i_v(), kCodeSetLocal0);
337 } 340 }
338 341
339 TEST_F(AstDecoderTest, SetLocalN_local) { 342 TEST_F(AstDecoderTest, SetLocalN_local) {
340 for (byte i = 1; i < 8; i++) { 343 for (byte i = 1; i < 8; i++) {
341 AddLocals(kAstI32, 1); 344 AddLocals(kAstI32, 1);
342 for (byte j = 0; j < i; j++) { 345 for (byte j = 0; j < i; j++) {
343 byte code[] = {kExprSetLocal, j, kExprI8Const, i}; 346 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_SET_LOCAL(j, WASM_I8(i)));
344 EXPECT_VERIFIES(sigs.v_v(), code);
345 } 347 }
346 } 348 }
347 } 349 }
348 350
351 TEST_F(AstDecoderTest, BlockN) {
352 const int kMaxSize = 200;
353 byte buffer[kMaxSize + 2];
354
355 for (int i = 0; i <= kMaxSize; i++) {
356 memset(buffer, kExprNop, sizeof(buffer));
357 buffer[0] = kExprBlock;
358 buffer[i + 1] = kExprEnd;
359 Verify(kSuccess, sigs.v_i(), buffer, buffer + i + 2);
360 }
361 }
362
349 TEST_F(AstDecoderTest, Block0) { 363 TEST_F(AstDecoderTest, Block0) {
350 static const byte code[] = {kExprBlock, 0}; 364 static const byte code[] = {kExprBlock, kExprEnd};
351 EXPECT_VERIFIES(sigs.v_v(), code); 365 EXPECT_VERIFIES(sigs.v_v(), code);
366 EXPECT_FAILURE(sigs.i_i(), code);
352 } 367 }
353 368
354 TEST_F(AstDecoderTest, Block0_fallthru1) { 369 TEST_F(AstDecoderTest, Block0_fallthru1) {
355 static const byte code[] = {kExprBlock, 0, kExprBlock, 0}; 370 static const byte code[] = {kExprBlock, kExprBlock, kExprEnd, kExprEnd};
356 EXPECT_VERIFIES(sigs.v_v(), code); 371 EXPECT_VERIFIES(sigs.v_v(), code);
372 EXPECT_FAILURE(sigs.i_i(), code);
373 }
374
375 TEST_F(AstDecoderTest, Block0Block0) {
376 static const byte code[] = {kExprBlock, kExprEnd, kExprBlock, kExprEnd};
377 EXPECT_VERIFIES(sigs.v_v(), code);
378 EXPECT_FAILURE(sigs.i_i(), code);
379 }
380
381 TEST_F(AstDecoderTest, Block0_end_end) {
382 static const byte code[] = {kExprBlock, kExprEnd, kExprEnd};
383 EXPECT_FAILURE(sigs.v_v(), code);
357 } 384 }
358 385
359 TEST_F(AstDecoderTest, Block1) { 386 TEST_F(AstDecoderTest, Block1) {
360 static const byte code[] = {kExprBlock, 1, kExprSetLocal, 0, kExprI8Const, 0}; 387 byte code[] = {B1(WASM_SET_LOCAL(0, WASM_ZERO))};
361 EXPECT_VERIFIES(sigs.i_i(), code); 388 EXPECT_VERIFIES(sigs.i_i(), code);
362 } 389 EXPECT_VERIFIES(sigs.v_i(), code);
363 390 EXPECT_FAILURE(sigs.d_dd(), code);
364 TEST_F(AstDecoderTest, Block0_fallthru2) { 391 }
365 static const byte code[] = {kExprBlock, 0, kExprSetLocal, 0, kExprI8Const, 0}; 392
393 TEST_F(AstDecoderTest, Block1_i) {
394 byte code[] = {B1(WASM_ZERO)};
366 EXPECT_VERIFIES(sigs.i_i(), code); 395 EXPECT_VERIFIES(sigs.i_i(), code);
396 EXPECT_FAILURE(sigs.f_ff(), code);
397 EXPECT_FAILURE(sigs.d_dd(), code);
398 EXPECT_FAILURE(sigs.l_ll(), code);
399 }
400
401 TEST_F(AstDecoderTest, Block1_f) {
402 byte code[] = {B1(WASM_F32(0))};
403 EXPECT_FAILURE(sigs.i_i(), code);
404 EXPECT_VERIFIES(sigs.f_ff(), code);
405 EXPECT_FAILURE(sigs.d_dd(), code);
406 EXPECT_FAILURE(sigs.l_ll(), code);
407 }
408
409 TEST_F(AstDecoderTest, Block1_continue) {
410 EXPECT_VERIFIES_INLINE(sigs.v_v(), B1(WASM_BR(0)));
411 EXPECT_FAILURE_INLINE(sigs.v_v(), B1(WASM_BR(1)));
412 EXPECT_FAILURE_INLINE(sigs.v_v(), B1(WASM_BR(2)));
413 EXPECT_FAILURE_INLINE(sigs.v_v(), B1(WASM_BR(3)));
414 }
415
416 TEST_F(AstDecoderTest, Block1_br) {
417 EXPECT_FAILURE_INLINE(sigs.v_v(), kExprBlock, kExprBr, 0, kExprEnd);
418 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprBlock, kExprNop, kExprBr, 0,
419 kExprEnd);
420 }
421
422 TEST_F(AstDecoderTest, Block2_br) {
423 EXPECT_VERIFIES_INLINE(sigs.v_v(), B2(WASM_NOP, WASM_BR(0)));
424 EXPECT_VERIFIES_INLINE(sigs.v_v(), B2(WASM_BR(0), WASM_NOP));
425 EXPECT_VERIFIES_INLINE(sigs.v_v(), B2(WASM_BR(0), WASM_BR(0)));
367 } 426 }
368 427
369 TEST_F(AstDecoderTest, Block2) { 428 TEST_F(AstDecoderTest, Block2) {
370 static const byte code[] = {kExprBlock, 2, // -- 429 EXPECT_VERIFIES_INLINE(sigs.i_i(),
371 kExprSetLocal, 0, kExprI8Const, 0, // -- 430 B2(WASM_NOP, WASM_SET_LOCAL(0, WASM_ZERO)));
372 kExprSetLocal, 0, kExprI8Const, 0}; // -- 431 EXPECT_FAILURE_INLINE(sigs.i_i(), B2(WASM_SET_LOCAL(0, WASM_ZERO), WASM_NOP));
432 EXPECT_VERIFIES_INLINE(sigs.i_i(), B2(WASM_SET_LOCAL(0, WASM_ZERO),
433 WASM_SET_LOCAL(0, WASM_ZERO)));
434 }
435
436 TEST_F(AstDecoderTest, Block2b) {
437 byte code[] = {B2(WASM_SET_LOCAL(0, WASM_ZERO), WASM_ZERO)};
373 EXPECT_VERIFIES(sigs.i_i(), code); 438 EXPECT_VERIFIES(sigs.i_i(), code);
439 EXPECT_FAILURE(sigs.v_v(), code);
440 EXPECT_FAILURE(sigs.f_ff(), code);
374 } 441 }
375 442
376 TEST_F(AstDecoderTest, Block2_fallthru) { 443 TEST_F(AstDecoderTest, Block2_fallthru) {
377 static const byte code[] = {kExprBlock, 2, // -- 444 EXPECT_VERIFIES_INLINE(sigs.i_i(), B2(WASM_SET_LOCAL(0, WASM_ZERO),
378 kExprSetLocal, 0, kExprI8Const, 0, // -- 445 WASM_SET_LOCAL(0, WASM_ZERO)),
379 kExprSetLocal, 0, kExprI8Const, 0, // -- 446 WASM_I8(23));
380 kExprI8Const, 11}; // -- 447 }
381 EXPECT_VERIFIES(sigs.i_i(), code); 448
382 } 449 TEST_F(AstDecoderTest, Block3) {
383 450 EXPECT_VERIFIES_INLINE(
384 TEST_F(AstDecoderTest, BlockN) { 451 sigs.i_i(), B3(WASM_SET_LOCAL(0, WASM_ZERO), WASM_SET_LOCAL(0, WASM_ZERO),
385 byte block[] = {kExprBlock, 2}; 452 WASM_I8(11)));
386 453 }
387 for (size_t i = 0; i < 10; i++) { 454
388 size_t total = sizeof(block) + sizeof(kCodeSetLocal0) * i; 455 TEST_F(AstDecoderTest, Block5) {
389 byte* code = reinterpret_cast<byte*>(malloc(total)); 456 EXPECT_VERIFIES_INLINE(sigs.v_i(), B1(WASM_GET_LOCAL(0)));
390 memcpy(code, block, sizeof(block)); 457
391 code[1] = static_cast<byte>(i); 458 EXPECT_VERIFIES_INLINE(sigs.v_i(), B2(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
392 for (size_t j = 0; j < i; j++) { 459
393 memcpy(code + sizeof(block) + j * sizeof(kCodeSetLocal0), kCodeSetLocal0, 460 EXPECT_VERIFIES_INLINE(
394 sizeof(kCodeSetLocal0)); 461 sigs.v_i(), B3(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
395 } 462
396 Verify(kSuccess, sigs.v_i(), code, code + total); 463 EXPECT_VERIFIES_INLINE(sigs.v_i(),
397 free(code); 464 WASM_BLOCK(4, WASM_GET_LOCAL(0), WASM_GET_LOCAL(0),
465 WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
466
467 EXPECT_VERIFIES_INLINE(
468 sigs.v_i(),
469 WASM_BLOCK(5, WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0),
470 WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
471 }
472
473 TEST_F(AstDecoderTest, BlockF32) {
474 static const byte code[] = {kExprBlock, kExprF32Const, 0, 0, 0, 0, kExprEnd};
475 EXPECT_VERIFIES(sigs.f_ff(), code);
476 EXPECT_FAILURE(sigs.i_i(), code);
477 EXPECT_FAILURE(sigs.d_dd(), code);
478 }
479
480 TEST_F(AstDecoderTest, BlockN_off_end) {
481 byte code[] = {kExprBlock, kExprNop, kExprNop, kExprNop, kExprNop, kExprEnd};
482 EXPECT_VERIFIES(sigs.v_v(), code);
483 for (size_t i = 1; i < arraysize(code); i++) {
484 Verify(kError, sigs.v_v(), code, code + i);
398 } 485 }
399 } 486 }
400 487
401 TEST_F(AstDecoderTest, BlockN_off_end) {
402 for (byte i = 2; i < 10; i++) {
403 byte code[] = {kExprBlock, i, kExprNop};
404 EXPECT_FAILURE(sigs.v_v(), code);
405 }
406 }
407
408 TEST_F(AstDecoderTest, Block1_break) {
409 static const byte code[] = {kExprBlock, 1, kExprBr, 0, kExprNop};
410 EXPECT_VERIFIES(sigs.v_v(), code);
411 }
412
413 TEST_F(AstDecoderTest, Block2_break) {
414 static const byte code[] = {kExprBlock, 2, kExprNop, kExprBr, 0, kExprNop};
415 EXPECT_VERIFIES(sigs.v_v(), code);
416 }
417
418 TEST_F(AstDecoderTest, Block1_continue) {
419 static const byte code[] = {kExprBlock, 1, kExprBr, 1, kExprNop};
420 EXPECT_FAILURE(sigs.v_v(), code);
421 }
422
423 TEST_F(AstDecoderTest, Block2_continue) { 488 TEST_F(AstDecoderTest, Block2_continue) {
424 static const byte code[] = {kExprBlock, 2, kExprNop, kExprBr, 1, kExprNop}; 489 static const byte code[] = {kExprBlock, kExprNop, kExprBr,
425 EXPECT_FAILURE(sigs.v_v(), code); 490 1, kExprNop, kExprEnd};
426 } 491 EXPECT_FAILURE(sigs.v_v(), code);
427 492 }
428 TEST_F(AstDecoderTest, ExprBlock0) { 493
429 static const byte code[] = {kExprBlock, 0}; 494 TEST_F(AstDecoderTest, NestedBlock_return) {
430 EXPECT_VERIFIES(sigs.v_v(), code); 495 EXPECT_VERIFIES_INLINE(sigs.i_i(), B1(B1(WASM_RETURN(WASM_ZERO))));
431 } 496 }
432 497
433 TEST_F(AstDecoderTest, ExprBlock1a) { 498 TEST_F(AstDecoderTest, BlockBinop) {
434 static const byte code[] = {kExprBlock, 1, kExprI8Const, 0}; 499 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_I32_AND(B1(WASM_I8(1)), WASM_I8(2)));
435 EXPECT_VERIFIES(sigs.i_i(), code); 500 }
436 } 501
437 502 TEST_F(AstDecoderTest, BlockBrBinop) {
438 TEST_F(AstDecoderTest, ExprBlock1b) { 503 EXPECT_VERIFIES_INLINE(sigs.i_i(),
439 static const byte code[] = {kExprBlock, 1, kExprI8Const, 0}; 504 WASM_I32_AND(B1(WASM_BRV(0, WASM_I8(1))), WASM_I8(2)));
440 EXPECT_FAILURE(sigs.f_ff(), code); 505 }
441 } 506
442 507 TEST_F(AstDecoderTest, If_empty_stack) {
443 TEST_F(AstDecoderTest, ExprBlock1c) { 508 byte code[] = {kExprIf};
444 static const byte code[] = {kExprBlock, 1, kExprF32Const, 0, 0, 0, 0}; 509 EXPECT_FAILURE(sigs.v_v(), code);
445 EXPECT_VERIFIES(sigs.f_ff(), code); 510 EXPECT_FAILURE(sigs.i_i(), code);
511 }
512
513 TEST_F(AstDecoderTest, If_incomplete1) {
514 byte code[] = {kExprI8Const, 0, kExprIf};
515 EXPECT_FAILURE(sigs.v_v(), code);
516 EXPECT_FAILURE(sigs.i_i(), code);
517 }
518
519 TEST_F(AstDecoderTest, If_incomplete2) {
520 byte code[] = {kExprI8Const, 0, kExprIf, kExprNop};
521 EXPECT_FAILURE(sigs.v_v(), code);
522 EXPECT_FAILURE(sigs.i_i(), code);
523 }
524
525 TEST_F(AstDecoderTest, If_else_else) {
526 byte code[] = {kExprI8Const, 0, kExprIf, kExprElse, kExprElse, kExprEnd};
527 EXPECT_FAILURE(sigs.v_v(), code);
528 EXPECT_FAILURE(sigs.i_i(), code);
446 } 529 }
447 530
448 TEST_F(AstDecoderTest, IfEmpty) { 531 TEST_F(AstDecoderTest, IfEmpty) {
449 static const byte code[] = {kExprIf, kExprGetLocal, 0, kExprNop}; 532 EXPECT_VERIFIES_INLINE(sigs.v_i(), kExprGetLocal, 0, kExprIf, kExprEnd);
450 EXPECT_VERIFIES(sigs.v_i(), code);
451 } 533 }
452 534
453 TEST_F(AstDecoderTest, IfSet) { 535 TEST_F(AstDecoderTest, IfSet) {
454 static const byte code[] = {kExprIfElse, kExprGetLocal, 0, kExprSetLocal, 536 EXPECT_VERIFIES_INLINE(
455 0, kExprI8Const, 0, kExprNop}; 537 sigs.v_i(), WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(0, WASM_ZERO)));
456 EXPECT_VERIFIES(sigs.v_i(), code); 538 EXPECT_VERIFIES_INLINE(
457 } 539 sigs.v_i(),
458 540 WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_SET_LOCAL(0, WASM_ZERO), WASM_NOP));
459 TEST_F(AstDecoderTest, IfBlock1) {
460 static const byte code[] = {kExprIfElse, kExprGetLocal, 0, kExprBlock,
461 1, kExprSetLocal, 0, kExprI8Const,
462 0, kExprNop};
463 EXPECT_VERIFIES(sigs.v_i(), code);
464 }
465
466 TEST_F(AstDecoderTest, IfBlock2) {
467 static const byte code[] = {kExprIf, kExprGetLocal, 0, kExprBlock,
468 2, kExprSetLocal, 0, kExprI8Const,
469 0, kExprSetLocal, 0, kExprI8Const,
470 0};
471 EXPECT_VERIFIES(sigs.v_i(), code);
472 } 541 }
473 542
474 TEST_F(AstDecoderTest, IfElseEmpty) { 543 TEST_F(AstDecoderTest, IfElseEmpty) {
475 static const byte code[] = {kExprIfElse, kExprGetLocal, 0, kExprNop, 544 EXPECT_VERIFIES_INLINE(sigs.v_i(), WASM_GET_LOCAL(0), kExprIf, kExprElse,
476 kExprNop}; 545 kExprEnd);
477 EXPECT_VERIFIES(sigs.v_i(), code); 546 EXPECT_VERIFIES_INLINE(sigs.v_i(),
478 } 547 WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP));
479 548 }
480 TEST_F(AstDecoderTest, IfElseSet) { 549
481 static const byte code[] = {kExprIfElse, 550 TEST_F(AstDecoderTest, IfElseUnreachable1) {
482 kExprGetLocal, 551 EXPECT_VERIFIES_INLINE(
483 0, // -- 552 sigs.i_i(),
484 kExprSetLocal, 553 WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_UNREACHABLE, WASM_GET_LOCAL(0)));
485 0, 554 EXPECT_VERIFIES_INLINE(
486 kExprI8Const, 555 sigs.i_i(),
487 0, // -- 556 WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_UNREACHABLE));
488 kExprSetLocal, 557 }
489 0, 558
490 kExprI8Const, 559 TEST_F(AstDecoderTest, IfElseUnreachable2) {
491 1}; // -- 560 static const byte code[] = {
492 EXPECT_VERIFIES(sigs.v_i(), code); 561 WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_UNREACHABLE, WASM_GET_LOCAL(0))};
493 }
494
495 TEST_F(AstDecoderTest, IfElseUnreachable) {
496 static const byte code[] = {kExprIfElse, kExprI8Const, 0,
497 kExprUnreachable, kExprGetLocal, 0};
498 562
499 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 563 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
500 LocalType types[] = {kAstI32, kLocalTypes[i]}; 564 LocalType types[] = {kAstI32, kLocalTypes[i]};
501 FunctionSig sig(1, 1, types); 565 FunctionSig sig(1, 1, types);
502 566
503 if (kLocalTypes[i] == kAstI32) { 567 if (kLocalTypes[i] == kAstI32) {
504 EXPECT_VERIFIES(&sig, code); 568 EXPECT_VERIFIES(&sig, code);
505 } else { 569 } else {
506 EXPECT_FAILURE(&sig, code); 570 EXPECT_FAILURE(&sig, code);
507 } 571 }
508 } 572 }
509 } 573 }
510 574
575 TEST_F(AstDecoderTest, Block_else) {
576 byte code[] = {kExprI8Const, 0, kExprBlock, kExprElse, kExprEnd};
577 EXPECT_FAILURE(sigs.v_v(), code);
578 EXPECT_FAILURE(sigs.i_i(), code);
579 }
580
581 TEST_F(AstDecoderTest, IfNop) {
582 EXPECT_VERIFIES_INLINE(sigs.v_i(), WASM_IF(WASM_GET_LOCAL(0), WASM_NOP));
583 }
584
585 TEST_F(AstDecoderTest, IfNopElseNop) {
586 EXPECT_VERIFIES_INLINE(sigs.v_i(),
587 WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP));
588 }
589
590 TEST_F(AstDecoderTest, If_end_end) {
591 static const byte code[] = {kExprGetLocal, 0, kExprIf, kExprEnd, kExprEnd};
592 EXPECT_FAILURE(sigs.v_i(), code);
593 }
594
595 TEST_F(AstDecoderTest, If_falloff) {
596 static const byte code[] = {kExprGetLocal, 0, kExprIf};
597 EXPECT_FAILURE(sigs.v_i(), code);
598 }
599
600 TEST_F(AstDecoderTest, IfElse_falloff) {
601 static const byte code[] = {kExprGetLocal, 0, kExprIf, kExprNop, kExprElse};
602 EXPECT_FAILURE(sigs.v_i(), code);
603 }
604
605 TEST_F(AstDecoderTest, IfElseNop) {
606 EXPECT_VERIFIES_INLINE(
607 sigs.v_i(),
608 WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_SET_LOCAL(0, WASM_ZERO), WASM_NOP));
609 }
610
611 TEST_F(AstDecoderTest, IfBlock1) {
612 EXPECT_VERIFIES_INLINE(
613 sigs.v_i(), WASM_IF_ELSE(WASM_GET_LOCAL(0),
614 B1(WASM_SET_LOCAL(0, WASM_ZERO)), WASM_NOP));
615 }
616
617 TEST_F(AstDecoderTest, IfBlock1b) {
618 EXPECT_VERIFIES_INLINE(
619 sigs.v_i(), WASM_IF(WASM_GET_LOCAL(0), B1(WASM_SET_LOCAL(0, WASM_ZERO))));
620 }
621
622 TEST_F(AstDecoderTest, IfBlock2a) {
623 EXPECT_VERIFIES_INLINE(
624 sigs.v_i(), WASM_IF(WASM_GET_LOCAL(0), B2(WASM_SET_LOCAL(0, WASM_ZERO),
625 WASM_SET_LOCAL(0, WASM_ZERO))));
626 }
627
628 TEST_F(AstDecoderTest, IfBlock2b) {
629 EXPECT_VERIFIES_INLINE(
630 sigs.v_i(),
631 WASM_IF_ELSE(WASM_GET_LOCAL(0), B2(WASM_SET_LOCAL(0, WASM_ZERO),
632 WASM_SET_LOCAL(0, WASM_ZERO)),
633 WASM_NOP));
634 }
635
636 TEST_F(AstDecoderTest, IfElseSet) {
637 EXPECT_VERIFIES_INLINE(
638 sigs.v_i(), WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_SET_LOCAL(0, WASM_ZERO),
639 WASM_SET_LOCAL(0, WASM_I8(1))));
640 }
641
511 TEST_F(AstDecoderTest, Loop0) { 642 TEST_F(AstDecoderTest, Loop0) {
512 static const byte code[] = {kExprLoop, 0}; 643 static const byte code[] = {kExprLoop, kExprEnd};
513 EXPECT_VERIFIES(sigs.v_v(), code); 644 EXPECT_VERIFIES(sigs.v_v(), code);
514 } 645 }
515 646
516 TEST_F(AstDecoderTest, Loop1) { 647 TEST_F(AstDecoderTest, Loop1) {
517 static const byte code[] = {kExprLoop, 1, kExprSetLocal, 0, kExprI8Const, 0}; 648 static const byte code[] = {WASM_LOOP(1, WASM_SET_LOCAL(0, WASM_ZERO))};
518 EXPECT_VERIFIES(sigs.v_i(), code); 649 EXPECT_VERIFIES(sigs.v_i(), code);
650 EXPECT_FAILURE(sigs.v_v(), code);
651 EXPECT_FAILURE(sigs.f_ff(), code);
519 } 652 }
520 653
521 TEST_F(AstDecoderTest, Loop2) { 654 TEST_F(AstDecoderTest, Loop2) {
522 static const byte code[] = {kExprLoop, 2, // -- 655 EXPECT_VERIFIES_INLINE(sigs.v_i(), WASM_LOOP(2, WASM_SET_LOCAL(0, WASM_ZERO),
523 kExprSetLocal, 0, kExprI8Const, 0, // -- 656 WASM_SET_LOCAL(0, WASM_ZERO)));
524 kExprSetLocal, 0, kExprI8Const, 0}; // --
525 EXPECT_VERIFIES(sigs.v_i(), code);
526 } 657 }
527 658
528 TEST_F(AstDecoderTest, Loop1_continue) { 659 TEST_F(AstDecoderTest, Loop1_continue) {
529 static const byte code[] = {kExprLoop, 1, kExprBr, 0, kExprNop}; 660 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BR(0)));
530 EXPECT_VERIFIES(sigs.v_v(), code);
531 } 661 }
532 662
533 TEST_F(AstDecoderTest, Loop1_break) { 663 TEST_F(AstDecoderTest, Loop1_break) {
534 static const byte code[] = {kExprLoop, 1, kExprBr, 1, kExprNop}; 664 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BR(1)));
535 EXPECT_VERIFIES(sigs.v_v(), code);
536 } 665 }
537 666
538 TEST_F(AstDecoderTest, Loop2_continue) { 667 TEST_F(AstDecoderTest, Loop2_continue) {
539 static const byte code[] = {kExprLoop, 2, // -- 668 EXPECT_VERIFIES_INLINE(
540 kExprSetLocal, 0, kExprI8Const, 0, // -- 669 sigs.v_i(), WASM_LOOP(2, WASM_SET_LOCAL(0, WASM_ZERO), WASM_BR(0)));
541 kExprBr, 0, kExprNop}; // --
542 EXPECT_VERIFIES(sigs.v_i(), code);
543 } 670 }
544 671
545 TEST_F(AstDecoderTest, Loop2_break) { 672 TEST_F(AstDecoderTest, Loop2_break) {
546 static const byte code[] = {kExprLoop, 2, // -- 673 EXPECT_VERIFIES_INLINE(
547 kExprSetLocal, 0, kExprI8Const, 0, // -- 674 sigs.v_i(), WASM_LOOP(2, WASM_SET_LOCAL(0, WASM_ZERO), WASM_BR(1)));
548 kExprBr, 1, kExprNop}; // --
549 EXPECT_VERIFIES(sigs.v_i(), code);
550 } 675 }
551 676
552 TEST_F(AstDecoderTest, ExprLoop0) { 677 TEST_F(AstDecoderTest, ExprLoop0) {
553 static const byte code[] = {kExprLoop, 0}; 678 static const byte code[] = {kExprLoop, kExprEnd};
554 EXPECT_VERIFIES(sigs.v_v(), code); 679 EXPECT_VERIFIES(sigs.v_v(), code);
555 } 680 }
556 681
557 TEST_F(AstDecoderTest, ExprLoop1a) { 682 TEST_F(AstDecoderTest, ExprLoop1a) {
558 static const byte code[] = {kExprLoop, 1, kExprBr, 0, kExprI8Const, 0}; 683 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_LOOP(1, WASM_BRV(0, WASM_ZERO)));
559 EXPECT_VERIFIES(sigs.i_i(), code);
560 } 684 }
561 685
562 TEST_F(AstDecoderTest, ExprLoop1b) { 686 TEST_F(AstDecoderTest, ExprLoop1b) {
563 static const byte code[] = {kExprLoop, 1, kExprBr, 0, kExprI8Const, 0}; 687 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_LOOP(1, WASM_BRV(1, WASM_ZERO)));
564 EXPECT_VERIFIES(sigs.i_i(), code); 688 EXPECT_FAILURE_INLINE(sigs.f_ff(), WASM_LOOP(1, WASM_BRV(1, WASM_ZERO)));
565 } 689 }
566 690
567 TEST_F(AstDecoderTest, ExprLoop2_unreachable) { 691 TEST_F(AstDecoderTest, ExprLoop2_unreachable) {
568 static const byte code[] = {kExprLoop, 2, kExprBr, 0, 692 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_LOOP(2, WASM_BR(0), WASM_NOP));
569 kExprI8Const, 0, kExprNop};
570 EXPECT_VERIFIES(sigs.i_i(), code);
571 } 693 }
572 694
573 TEST_F(AstDecoderTest, ReturnVoid1) { 695 TEST_F(AstDecoderTest, ReturnVoid1) {
574 static const byte code[] = {kExprNop}; 696 static const byte code[] = {kExprNop};
575 EXPECT_VERIFIES(sigs.v_v(), code); 697 EXPECT_VERIFIES(sigs.v_v(), code);
576 EXPECT_FAILURE(sigs.i_i(), code); 698 EXPECT_FAILURE(sigs.i_i(), code);
577 EXPECT_FAILURE(sigs.i_f(), code); 699 EXPECT_FAILURE(sigs.i_f(), code);
578 } 700 }
579 701
580 TEST_F(AstDecoderTest, ReturnVoid2) { 702 TEST_F(AstDecoderTest, ReturnVoid2) {
581 static const byte code[] = {kExprBlock, 1, kExprBr, 0, kExprNop}; 703 static const byte code[] = {kExprBlock, kExprNop, kExprBr, 0, kExprEnd};
582 EXPECT_VERIFIES(sigs.v_v(), code); 704 EXPECT_VERIFIES(sigs.v_v(), code);
583 EXPECT_FAILURE(sigs.i_i(), code); 705 EXPECT_FAILURE(sigs.i_i(), code);
584 EXPECT_FAILURE(sigs.i_f(), code); 706 EXPECT_FAILURE(sigs.i_f(), code);
585 } 707 }
586 708
587 TEST_F(AstDecoderTest, ReturnVoid3) { 709 TEST_F(AstDecoderTest, ReturnVoid3) {
588 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprI8Const, 0); 710 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprI8Const, 0);
589 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprI32Const, 0, 0, 0, 0); 711 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprI32Const, 0, 0, 0, 0);
590 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprI64Const, 0, 0, 0, 0, 0, 0, 0, 0); 712 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprI64Const, 0, 0, 0, 0, 0, 0, 0, 0);
591 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprF32Const, 0, 0, 0, 0); 713 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprF32Const, 0, 0, 0, 0);
592 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprF64Const, 0, 0, 0, 0, 0, 0, 0, 0); 714 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprF64Const, 0, 0, 0, 0, 0, 0, 0, 0);
593 715
594 EXPECT_VERIFIES_INLINE(sigs.v_i(), kExprGetLocal, 0); 716 EXPECT_VERIFIES_INLINE(sigs.v_i(), kExprGetLocal, 0);
595 } 717 }
596 718
597 TEST_F(AstDecoderTest, Unreachable1) { 719 TEST_F(AstDecoderTest, Unreachable1) {
598 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprUnreachable); 720 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprUnreachable);
599 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprUnreachable, kExprUnreachable); 721 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprUnreachable, kExprUnreachable);
600 EXPECT_VERIFIES_INLINE(sigs.v_v(), 722 EXPECT_VERIFIES_INLINE(sigs.v_v(), B2(WASM_UNREACHABLE, WASM_ZERO));
601 WASM_BLOCK(2, WASM_UNREACHABLE, WASM_ZERO)); 723 EXPECT_VERIFIES_INLINE(sigs.v_v(), B2(WASM_BR(0), WASM_ZERO));
602 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_BLOCK(2, WASM_BR(0), WASM_ZERO));
603 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(2, WASM_UNREACHABLE, WASM_ZERO)); 724 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(2, WASM_UNREACHABLE, WASM_ZERO));
604 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(2, WASM_BR(0), WASM_ZERO)); 725 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(2, WASM_BR(0), WASM_ZERO));
605 } 726 }
606 727
607 TEST_F(AstDecoderTest, Codeiness) { 728 TEST_F(AstDecoderTest, Unreachable_binop) {
608 VERIFY(kExprLoop, 2, // -- 729 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_I32_AND(WASM_ZERO, WASM_UNREACHABLE));
609 kExprSetLocal, 0, kExprI8Const, 0, // -- 730 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_I32_AND(WASM_UNREACHABLE, WASM_ZERO));
610 kExprBr, 0, kExprNop); // --
611 } 731 }
612 732
613 TEST_F(AstDecoderTest, ExprIf1) { 733 TEST_F(AstDecoderTest, Unreachable_select) {
614 VERIFY(kExprIf, kExprGetLocal, 0, kExprI8Const, 0, kExprI8Const, 1); 734 EXPECT_VERIFIES_INLINE(sigs.i_i(),
615 VERIFY(kExprIf, kExprGetLocal, 0, kExprGetLocal, 0, kExprGetLocal, 0); 735 WASM_SELECT(WASM_UNREACHABLE, WASM_ZERO, WASM_ZERO));
616 VERIFY(kExprIf, kExprGetLocal, 0, kExprI32Add, kExprGetLocal, 0, 736 EXPECT_VERIFIES_INLINE(sigs.i_i(),
617 kExprGetLocal, 0, kExprI8Const, 1); 737 WASM_SELECT(WASM_ZERO, WASM_UNREACHABLE, WASM_ZERO));
738 EXPECT_VERIFIES_INLINE(sigs.i_i(),
739 WASM_SELECT(WASM_ZERO, WASM_ZERO, WASM_UNREACHABLE));
618 } 740 }
619 741
620 TEST_F(AstDecoderTest, ExprIf_off_end) { 742 TEST_F(AstDecoderTest, If1) {
621 static const byte kCode[] = {kExprIf, kExprGetLocal, 0, kExprGetLocal, 743 EXPECT_VERIFIES_INLINE(
622 0, kExprGetLocal, 0}; 744 sigs.i_i(), WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_I8(9), WASM_I8(8)));
623 for (size_t len = 1; len < arraysize(kCode); len++) { 745 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_I8(9),
746 WASM_GET_LOCAL(0)));
747 EXPECT_VERIFIES_INLINE(
748 sigs.i_i(),
749 WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_I8(8)));
750 }
751
752 TEST_F(AstDecoderTest, If_off_end) {
753 static const byte kCode[] = {
754 WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0))};
755 for (size_t len = 3; len < arraysize(kCode); len++) {
624 Verify(kError, sigs.i_i(), kCode, kCode + len); 756 Verify(kError, sigs.i_i(), kCode, kCode + len);
625 } 757 }
626 } 758 }
627 759
628 TEST_F(AstDecoderTest, ExprIf_type) { 760 TEST_F(AstDecoderTest, If_type1) {
629 { 761 // float|double ? 1 : 2
630 // float|double ? 1 : 2 762 static const byte kCode[] = {
631 static const byte kCode[] = {kExprIfElse, kExprGetLocal, 0, kExprI8Const, 763 WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_I8(0), WASM_I8(2))};
632 1, kExprI8Const, 2}; 764 EXPECT_VERIFIES(sigs.i_i(), kCode);
633 EXPECT_FAILURE(sigs.i_f(), kCode); 765 EXPECT_FAILURE(sigs.i_f(), kCode);
634 EXPECT_FAILURE(sigs.i_d(), kCode); 766 EXPECT_FAILURE(sigs.i_d(), kCode);
635 } 767 }
636 { 768
637 // 1 ? float|double : 2 769 TEST_F(AstDecoderTest, If_type2) {
638 static const byte kCode[] = {kExprIfElse, kExprI8Const, 1, kExprGetLocal, 770 // 1 ? float|double : 2
639 0, kExprI8Const, 2}; 771 static const byte kCode[] = {
640 EXPECT_FAILURE(sigs.i_f(), kCode); 772 WASM_IF_ELSE(WASM_I8(1), WASM_GET_LOCAL(0), WASM_I8(1))};
641 EXPECT_FAILURE(sigs.i_d(), kCode); 773 EXPECT_VERIFIES(sigs.i_i(), kCode);
642 } 774 EXPECT_FAILURE(sigs.i_f(), kCode);
643 { 775 EXPECT_FAILURE(sigs.i_d(), kCode);
644 // stmt ? 0 : 1 776 }
645 static const byte kCode[] = {kExprIfElse, kExprNop, kExprI8Const, 777
646 0, kExprI8Const, 1}; 778 TEST_F(AstDecoderTest, If_type3) {
647 EXPECT_FAILURE(sigs.i_i(), kCode); 779 // stmt ? 0 : 1
648 } 780 static const byte kCode[] = {WASM_IF_ELSE(WASM_NOP, WASM_I8(0), WASM_I8(1))};
649 { 781 EXPECT_FAILURE(sigs.i_i(), kCode);
650 // 0 ? stmt : 1 782 EXPECT_FAILURE(sigs.i_f(), kCode);
651 static const byte kCode[] = {kExprIfElse, kExprI8Const, 0, 783 EXPECT_FAILURE(sigs.i_d(), kCode);
652 kExprNop, kExprI8Const, 1}; 784 }
653 EXPECT_FAILURE(sigs.i_i(), kCode); 785
654 } 786 TEST_F(AstDecoderTest, If_type4) {
655 { 787 // 0 ? stmt : 1
656 // 0 ? 1 : stmt 788 static const byte kCode[] = {
657 static const byte kCode[] = {kExprIfElse, kExprI8Const, 0, kExprI8Const, 1, 789 WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_I8(1))};
658 0, kExprBlock}; 790 EXPECT_FAILURE(sigs.i_i(), kCode);
659 EXPECT_FAILURE(sigs.i_i(), kCode); 791 EXPECT_FAILURE(sigs.i_f(), kCode);
660 } 792 EXPECT_FAILURE(sigs.i_d(), kCode);
793 }
794
795 TEST_F(AstDecoderTest, If_type5) {
796 // 0 ? 1 : stmt
797 static const byte kCode[] = {WASM_IF_ELSE(WASM_ZERO, WASM_I8(1), WASM_NOP)};
798 EXPECT_FAILURE(sigs.i_i(), kCode);
799 EXPECT_FAILURE(sigs.i_f(), kCode);
800 EXPECT_FAILURE(sigs.i_d(), kCode);
661 } 801 }
662 802
663 TEST_F(AstDecoderTest, Int64Local_param) { 803 TEST_F(AstDecoderTest, Int64Local_param) {
664 EXPECT_VERIFIES(sigs.l_l(), kCodeGetLocal0); 804 EXPECT_VERIFIES(sigs.l_l(), kCodeGetLocal0);
665 } 805 }
666 806
667 TEST_F(AstDecoderTest, Int64Locals) { 807 TEST_F(AstDecoderTest, Int64Locals) {
668 for (byte i = 1; i < 8; i++) { 808 for (byte i = 1; i < 8; i++) {
669 AddLocals(kAstI64, 1); 809 AddLocals(kAstI64, 1);
670 for (byte j = 0; j < i; j++) { 810 for (byte j = 0; j < i; j++) {
671 byte code[] = {kExprGetLocal, j}; 811 EXPECT_VERIFIES_INLINE(sigs.l_v(), WASM_GET_LOCAL(j));
672 EXPECT_VERIFIES(sigs.l_v(), code);
673 } 812 }
674 } 813 }
675 } 814 }
676 815
677 TEST_F(AstDecoderTest, Int32Binops) { 816 TEST_F(AstDecoderTest, Int32Binops) {
678 TestBinop(kExprI32Add, sigs.i_ii()); 817 TestBinop(kExprI32Add, sigs.i_ii());
679 TestBinop(kExprI32Sub, sigs.i_ii()); 818 TestBinop(kExprI32Sub, sigs.i_ii());
680 TestBinop(kExprI32Mul, sigs.i_ii()); 819 TestBinop(kExprI32Mul, sigs.i_ii());
681 TestBinop(kExprI32DivS, sigs.i_ii()); 820 TestBinop(kExprI32DivS, sigs.i_ii());
682 TestBinop(kExprI32DivU, sigs.i_ii()); 821 TestBinop(kExprI32DivU, sigs.i_ii());
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
729 TestUnop(kExprF32UConvertI32, kAstF32, kAstI32); 868 TestUnop(kExprF32UConvertI32, kAstF32, kAstI32);
730 TestUnop(kExprF32ConvertF64, kAstF32, kAstF64); 869 TestUnop(kExprF32ConvertF64, kAstF32, kAstF64);
731 } 870 }
732 871
733 TEST_F(AstDecoderTest, MacrosStmt) { 872 TEST_F(AstDecoderTest, MacrosStmt) {
734 VERIFY(WASM_SET_LOCAL(0, WASM_I32V_3(87348))); 873 VERIFY(WASM_SET_LOCAL(0, WASM_I32V_3(87348)));
735 VERIFY(WASM_STORE_MEM(MachineType::Int32(), WASM_I8(24), WASM_I8(40))); 874 VERIFY(WASM_STORE_MEM(MachineType::Int32(), WASM_I8(24), WASM_I8(40)));
736 VERIFY(WASM_IF(WASM_GET_LOCAL(0), WASM_NOP)); 875 VERIFY(WASM_IF(WASM_GET_LOCAL(0), WASM_NOP));
737 VERIFY(WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP)); 876 VERIFY(WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP));
738 VERIFY(WASM_NOP); 877 VERIFY(WASM_NOP);
739 VERIFY(WASM_BLOCK(1, WASM_NOP)); 878 VERIFY(B1(WASM_NOP));
740 VERIFY(WASM_LOOP(1, WASM_NOP)); 879 VERIFY(WASM_LOOP(1, WASM_NOP));
741 VERIFY(WASM_LOOP(1, WASM_BREAK(0))); 880 VERIFY(WASM_LOOP(1, WASM_BREAK(0)));
742 VERIFY(WASM_LOOP(1, WASM_CONTINUE(0))); 881 VERIFY(WASM_LOOP(1, WASM_CONTINUE(0)));
743 } 882 }
744 883
745 TEST_F(AstDecoderTest, MacrosBreak) { 884 TEST_F(AstDecoderTest, MacrosBreak) {
746 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BREAK(0))); 885 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BREAK(0)));
747 886
748 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_LOOP(1, WASM_BREAKV(0, WASM_ZERO))); 887 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_LOOP(1, WASM_BREAKV(0, WASM_ZERO)));
749 EXPECT_VERIFIES_INLINE(sigs.l_l(), 888 EXPECT_VERIFIES_INLINE(sigs.l_l(),
750 WASM_LOOP(1, WASM_BREAKV(0, WASM_I64V_1(0)))); 889 WASM_LOOP(1, WASM_BREAKV(0, WASM_I64V_1(0))));
751 EXPECT_VERIFIES_INLINE(sigs.f_ff(), 890 EXPECT_VERIFIES_INLINE(sigs.f_ff(),
752 WASM_LOOP(1, WASM_BREAKV(0, WASM_F32(0.0)))); 891 WASM_LOOP(1, WASM_BREAKV(0, WASM_F32(0.0))));
753 EXPECT_VERIFIES_INLINE(sigs.d_dd(), 892 EXPECT_VERIFIES_INLINE(sigs.d_dd(),
754 WASM_LOOP(1, WASM_BREAKV(0, WASM_F64(0.0)))); 893 WASM_LOOP(1, WASM_BREAKV(0, WASM_F64(0.0))));
755 } 894 }
756 895
757 TEST_F(AstDecoderTest, MacrosContinue) { 896 TEST_F(AstDecoderTest, MacrosContinue) {
758 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_CONTINUE(0))); 897 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_CONTINUE(0)));
759 } 898 }
760 899
761 TEST_F(AstDecoderTest, MacrosVariadic) { 900 TEST_F(AstDecoderTest, MacrosVariadic) {
762 VERIFY(WASM_BLOCK(2, WASM_NOP, WASM_NOP)); 901 VERIFY(B2(WASM_NOP, WASM_NOP));
763 VERIFY(WASM_BLOCK(3, WASM_NOP, WASM_NOP, WASM_NOP)); 902 VERIFY(B3(WASM_NOP, WASM_NOP, WASM_NOP));
764 VERIFY(WASM_LOOP(2, WASM_NOP, WASM_NOP)); 903 VERIFY(WASM_LOOP(2, WASM_NOP, WASM_NOP));
765 VERIFY(WASM_LOOP(3, WASM_NOP, WASM_NOP, WASM_NOP)); 904 VERIFY(WASM_LOOP(3, WASM_NOP, WASM_NOP, WASM_NOP));
766 } 905 }
767 906
768 TEST_F(AstDecoderTest, MacrosNestedBlocks) { 907 TEST_F(AstDecoderTest, MacrosNestedBlocks) {
769 VERIFY(WASM_BLOCK(2, WASM_NOP, WASM_BLOCK(2, WASM_NOP, WASM_NOP))); 908 VERIFY(B2(WASM_NOP, B2(WASM_NOP, WASM_NOP)));
770 VERIFY(WASM_BLOCK(3, WASM_NOP, // -- 909 VERIFY(B3(WASM_NOP, // --
771 WASM_BLOCK(2, WASM_NOP, WASM_NOP), // -- 910 B2(WASM_NOP, WASM_NOP), // --
772 WASM_BLOCK(2, WASM_NOP, WASM_NOP))); // -- 911 B2(WASM_NOP, WASM_NOP))); // --
773 VERIFY(WASM_BLOCK(1, WASM_BLOCK(1, WASM_BLOCK(2, WASM_NOP, WASM_NOP)))); 912 VERIFY(B1(B1(B2(WASM_NOP, WASM_NOP))));
774 } 913 }
775 914
776 TEST_F(AstDecoderTest, MultipleReturn) { 915 TEST_F(AstDecoderTest, MultipleReturn) {
777 static LocalType kIntTypes5[] = {kAstI32, kAstI32, kAstI32, kAstI32, kAstI32}; 916 static LocalType kIntTypes5[] = {kAstI32, kAstI32, kAstI32, kAstI32, kAstI32};
778 FunctionSig sig_ii_v(2, 0, kIntTypes5); 917 FunctionSig sig_ii_v(2, 0, kIntTypes5);
779 EXPECT_VERIFIES_INLINE(&sig_ii_v, WASM_RETURN(WASM_ZERO, WASM_ONE)); 918 EXPECT_VERIFIES_INLINE(&sig_ii_v, WASM_RETURN(WASM_ZERO, WASM_ONE));
780 EXPECT_FAILURE_INLINE(&sig_ii_v, WASM_RETURN(WASM_ZERO)); 919 EXPECT_FAILURE_INLINE(&sig_ii_v, WASM_RETURN(WASM_ZERO));
781 920
782 FunctionSig sig_iii_v(3, 0, kIntTypes5); 921 FunctionSig sig_iii_v(3, 0, kIntTypes5);
783 EXPECT_VERIFIES_INLINE(&sig_iii_v, 922 EXPECT_VERIFIES_INLINE(&sig_iii_v,
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
879 #undef DECODE_TEST 1018 #undef DECODE_TEST
880 } 1019 }
881 1020
882 TEST_F(AstDecoderTest, MemorySize) { 1021 TEST_F(AstDecoderTest, MemorySize) {
883 byte code[] = {kExprMemorySize}; 1022 byte code[] = {kExprMemorySize};
884 EXPECT_VERIFIES(sigs.i_i(), code); 1023 EXPECT_VERIFIES(sigs.i_i(), code);
885 EXPECT_FAILURE(sigs.f_ff(), code); 1024 EXPECT_FAILURE(sigs.f_ff(), code);
886 } 1025 }
887 1026
888 TEST_F(AstDecoderTest, GrowMemory) { 1027 TEST_F(AstDecoderTest, GrowMemory) {
889 byte code[] = {kExprGrowMemory, kExprGetLocal, 0}; 1028 byte code[] = {WASM_UNOP(kExprGrowMemory, WASM_GET_LOCAL(0))};
890 EXPECT_VERIFIES(sigs.i_i(), code); 1029 EXPECT_VERIFIES(sigs.i_i(), code);
891 EXPECT_FAILURE(sigs.i_d(), code); 1030 EXPECT_FAILURE(sigs.i_d(), code);
892 } 1031 }
893 1032
894 TEST_F(AstDecoderTest, LoadMemOffset) { 1033 TEST_F(AstDecoderTest, LoadMemOffset) {
895 for (int offset = 0; offset < 128; offset += 7) { 1034 for (int offset = 0; offset < 128; offset += 7) {
896 byte code[] = {kExprI32LoadMem, ZERO_ALIGNMENT, static_cast<byte>(offset), 1035 byte code[] = {kExprI8Const, 0, kExprI32LoadMem, ZERO_ALIGNMENT,
897 kExprI8Const, 0}; 1036 static_cast<byte>(offset)};
898 EXPECT_VERIFIES(sigs.i_i(), code); 1037 EXPECT_VERIFIES(sigs.i_i(), code);
899 } 1038 }
900 } 1039 }
901 1040
902 TEST_F(AstDecoderTest, StoreMemOffset) { 1041 TEST_F(AstDecoderTest, StoreMemOffset) {
903 for (int offset = 0; offset < 128; offset += 7) { 1042 for (int offset = 0; offset < 128; offset += 7) {
904 byte code[] = { 1043 byte code[] = {WASM_STORE_MEM_OFFSET(MachineType::Int32(), offset,
905 kExprI32StoreMem, 0, static_cast<byte>(offset), kExprI8Const, 0, 1044 WASM_ZERO, WASM_ZERO)};
906 kExprI8Const, 0};
907 EXPECT_VERIFIES(sigs.i_i(), code); 1045 EXPECT_VERIFIES(sigs.i_i(), code);
908 } 1046 }
909 } 1047 }
910 1048
1049 #define BYTE0(x) ((x)&0x7F)
1050 #define BYTE1(x) ((x >> 7) & 0x7F)
1051 #define BYTE2(x) ((x >> 14) & 0x7F)
1052 #define BYTE3(x) ((x >> 21) & 0x7F)
1053
1054 #define VARINT1(x) BYTE0(x)
1055 #define VARINT2(x) BYTE0(x) | 0x80, BYTE1(x)
1056 #define VARINT3(x) BYTE0(x) | 0x80, BYTE1(x) | 0x80, BYTE2(x)
1057 #define VARINT4(x) BYTE0(x) | 0x80, BYTE1(x) | 0x80, BYTE2(x) | 0x80, BYTE3(x)
1058
911 TEST_F(AstDecoderTest, LoadMemOffset_varint) { 1059 TEST_F(AstDecoderTest, LoadMemOffset_varint) {
912 byte code1[] = {kExprI32LoadMem, ZERO_ALIGNMENT, ZERO_OFFSET, kExprI8Const, 1060 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_ZERO, kExprI32LoadMem, ZERO_ALIGNMENT,
913 0}; 1061 VARINT1(0x45));
914 byte code2[] = {kExprI32LoadMem, ZERO_ALIGNMENT, 0x80, 1, kExprI8Const, 0}; 1062 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_ZERO, kExprI32LoadMem, ZERO_ALIGNMENT,
915 byte code3[] = { 1063 VARINT2(0x3999));
916 kExprI32LoadMem, ZERO_ALIGNMENT, 0x81, 0x82, 5, kExprI8Const, 0}; 1064 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_ZERO, kExprI32LoadMem, ZERO_ALIGNMENT,
917 byte code4[] = { 1065 VARINT3(0x344445));
918 kExprI32LoadMem, ZERO_ALIGNMENT, 0x83, 0x84, 0x85, 7, kExprI8Const, 0}; 1066 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_ZERO, kExprI32LoadMem, ZERO_ALIGNMENT,
919 1067 VARINT4(0x36666667));
920 EXPECT_VERIFIES(sigs.i_i(), code1);
921 EXPECT_VERIFIES(sigs.i_i(), code2);
922 EXPECT_VERIFIES(sigs.i_i(), code3);
923 EXPECT_VERIFIES(sigs.i_i(), code4);
924 } 1068 }
925 1069
926 TEST_F(AstDecoderTest, StoreMemOffset_varint) { 1070 TEST_F(AstDecoderTest, StoreMemOffset_varint) {
927 byte code1[] = { 1071 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_ZERO, WASM_ZERO, kExprI32StoreMem,
928 kExprI32StoreMem, ZERO_ALIGNMENT, 0, kExprI8Const, 0, kExprI8Const, 0}; 1072 ZERO_ALIGNMENT, VARINT1(0x33));
929 byte code2[] = {kExprI32StoreMem, 1073 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_ZERO, WASM_ZERO, kExprI32StoreMem,
930 ZERO_ALIGNMENT, 1074 ZERO_ALIGNMENT, VARINT2(0x1111));
931 0x80, 1075 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_ZERO, WASM_ZERO, kExprI32StoreMem,
932 1, 1076 ZERO_ALIGNMENT, VARINT3(0x222222));
933 kExprI8Const, 1077 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_ZERO, WASM_ZERO, kExprI32StoreMem,
934 0, 1078 ZERO_ALIGNMENT, VARINT4(0x44444444));
935 kExprI8Const,
936 0};
937 byte code3[] = {kExprI32StoreMem,
938 ZERO_ALIGNMENT,
939 0x81,
940 0x82,
941 5,
942 kExprI8Const,
943 0,
944 kExprI8Const,
945 0};
946 byte code4[] = {kExprI32StoreMem,
947 ZERO_ALIGNMENT,
948 0x83,
949 0x84,
950 0x85,
951 7,
952 kExprI8Const,
953 0,
954 kExprI8Const,
955 0};
956
957 EXPECT_VERIFIES(sigs.i_i(), code1);
958 EXPECT_VERIFIES(sigs.i_i(), code2);
959 EXPECT_VERIFIES(sigs.i_i(), code3);
960 EXPECT_VERIFIES(sigs.i_i(), code4);
961 } 1079 }
962 1080
963 TEST_F(AstDecoderTest, AllLoadMemCombinations) { 1081 TEST_F(AstDecoderTest, AllLoadMemCombinations) {
964 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 1082 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
965 LocalType local_type = kLocalTypes[i]; 1083 LocalType local_type = kLocalTypes[i];
966 for (size_t j = 0; j < arraysize(machineTypes); j++) { 1084 for (size_t j = 0; j < arraysize(machineTypes); j++) {
967 MachineType mem_type = machineTypes[j]; 1085 MachineType mem_type = machineTypes[j];
968 byte code[] = { 1086 byte code[] = {WASM_LOAD_MEM(mem_type, WASM_ZERO)};
969 static_cast<byte>(WasmOpcodes::LoadStoreOpcodeOf(mem_type, false)),
970 ZERO_ALIGNMENT, ZERO_OFFSET, kExprI8Const, 0};
971 FunctionSig sig(1, 0, &local_type); 1087 FunctionSig sig(1, 0, &local_type);
972 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { 1088 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) {
973 EXPECT_VERIFIES(&sig, code); 1089 EXPECT_VERIFIES(&sig, code);
974 } else { 1090 } else {
975 EXPECT_FAILURE(&sig, code); 1091 EXPECT_FAILURE(&sig, code);
976 } 1092 }
977 } 1093 }
978 } 1094 }
979 } 1095 }
980 1096
981 TEST_F(AstDecoderTest, AllStoreMemCombinations) { 1097 TEST_F(AstDecoderTest, AllStoreMemCombinations) {
982 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 1098 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
983 LocalType local_type = kLocalTypes[i]; 1099 LocalType local_type = kLocalTypes[i];
984 for (size_t j = 0; j < arraysize(machineTypes); j++) { 1100 for (size_t j = 0; j < arraysize(machineTypes); j++) {
985 MachineType mem_type = machineTypes[j]; 1101 MachineType mem_type = machineTypes[j];
986 byte code[] = { 1102 byte code[] = {WASM_STORE_MEM(mem_type, WASM_ZERO, WASM_GET_LOCAL(0))};
987 static_cast<byte>(WasmOpcodes::LoadStoreOpcodeOf(mem_type, true)),
988 ZERO_ALIGNMENT,
989 ZERO_OFFSET,
990 kExprI8Const,
991 0,
992 kExprGetLocal,
993 0};
994 FunctionSig sig(0, 1, &local_type); 1103 FunctionSig sig(0, 1, &local_type);
995 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { 1104 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) {
996 EXPECT_VERIFIES(&sig, code); 1105 EXPECT_VERIFIES(&sig, code);
997 } else { 1106 } else {
998 EXPECT_FAILURE(&sig, code); 1107 EXPECT_FAILURE(&sig, code);
999 } 1108 }
1000 } 1109 }
1001 } 1110 }
1002 } 1111 }
1003 1112
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1040 1149
1041 TEST_F(AstDecoderTest, SimpleCalls) { 1150 TEST_F(AstDecoderTest, SimpleCalls) {
1042 FunctionSig* sig = sigs.i_i(); 1151 FunctionSig* sig = sigs.i_i();
1043 TestModuleEnv module_env; 1152 TestModuleEnv module_env;
1044 module = &module_env; 1153 module = &module_env;
1045 1154
1046 module_env.AddFunction(sigs.i_v()); 1155 module_env.AddFunction(sigs.i_v());
1047 module_env.AddFunction(sigs.i_i()); 1156 module_env.AddFunction(sigs.i_i());
1048 module_env.AddFunction(sigs.i_ii()); 1157 module_env.AddFunction(sigs.i_ii());
1049 1158
1050 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_FUNCTION(0)); 1159 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_FUNCTION0(0));
1051 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_FUNCTION(1, WASM_I8(27))); 1160 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_FUNCTION(1, WASM_I8(27)));
1052 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_FUNCTION(2, WASM_I8(37), WASM_I8(77))); 1161 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_FUNCTION(2, WASM_I8(37), WASM_I8(77)));
1053 } 1162 }
1054 1163
1055 TEST_F(AstDecoderTest, CallsWithTooFewArguments) { 1164 TEST_F(AstDecoderTest, CallsWithTooFewArguments) {
1056 FunctionSig* sig = sigs.i_i(); 1165 FunctionSig* sig = sigs.i_i();
1057 TestModuleEnv module_env; 1166 TestModuleEnv module_env;
1058 module = &module_env; 1167 module = &module_env;
1059 1168
1060 module_env.AddFunction(sigs.i_i()); 1169 module_env.AddFunction(sigs.i_i());
1061 module_env.AddFunction(sigs.i_ii()); 1170 module_env.AddFunction(sigs.i_ii());
1062 module_env.AddFunction(sigs.f_ff()); 1171 module_env.AddFunction(sigs.f_ff());
1063 1172
1064 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION0(0)); 1173 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION0(0));
1065 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(1, WASM_ZERO)); 1174 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(1, WASM_ZERO));
1066 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(2, WASM_GET_LOCAL(0))); 1175 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(2, WASM_GET_LOCAL(0)));
1067 } 1176 }
1068 1177
1069 TEST_F(AstDecoderTest, CallsWithSpilloverArgs) { 1178 TEST_F(AstDecoderTest, CallsWithSpilloverArgs) {
1070 static LocalType a_i_ff[] = {kAstI32, kAstF32, kAstF32}; 1179 static LocalType a_i_ff[] = {kAstI32, kAstF32, kAstF32};
1071 FunctionSig sig_i_ff(1, 2, a_i_ff); 1180 FunctionSig sig_i_ff(1, 2, a_i_ff);
1072 1181
1073 TestModuleEnv module_env; 1182 TestModuleEnv module_env;
1074 module = &module_env; 1183 module = &module_env;
1075 1184
1076 module_env.AddFunction(&sig_i_ff); 1185 module_env.AddFunction(&sig_i_ff);
1077 1186
1078 EXPECT_VERIFIES_INLINE(sigs.i_i(), 1187 {
1079 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))); 1188 byte code[] = {WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))};
1189 EXPECT_VERIFIES(sigs.i_i(), code);
1190 EXPECT_VERIFIES(sigs.i_ff(), code);
1191 EXPECT_FAILURE(sigs.f_ff(), code);
1192 }
1080 1193
1081 EXPECT_VERIFIES_INLINE(sigs.i_ff(), 1194 {
1082 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))); 1195 byte code[] = {
1083 1196 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1), WASM_F32(0.2))};
1084 EXPECT_FAILURE_INLINE(sigs.f_ff(), 1197 EXPECT_VERIFIES(sigs.i_i(), code);
1085 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))); 1198 EXPECT_FAILURE(sigs.f_ff(), code);
1086 1199 }
1087 EXPECT_FAILURE_INLINE(
1088 sigs.i_i(),
1089 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1), WASM_F32(0.2)));
1090
1091 EXPECT_VERIFIES_INLINE(
1092 sigs.f_ff(),
1093 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1), WASM_F32(11)));
1094 } 1200 }
1095 1201
1096 TEST_F(AstDecoderTest, CallsWithMismatchedSigs2) { 1202 TEST_F(AstDecoderTest, CallsWithMismatchedSigs2) {
1097 FunctionSig* sig = sigs.i_i(); 1203 FunctionSig* sig = sigs.i_i();
1098 TestModuleEnv module_env; 1204 TestModuleEnv module_env;
1099 module = &module_env; 1205 module = &module_env;
1100 1206
1101 module_env.AddFunction(sigs.i_i()); 1207 module_env.AddFunction(sigs.i_i());
1102 1208
1103 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(0, WASM_I64V_1(17))); 1209 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(0, WASM_I64V_1(17)));
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
1326 module_env.AddGlobal(mem_type); 1432 module_env.AddGlobal(mem_type);
1327 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { 1433 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) {
1328 EXPECT_VERIFIES_INLINE(&sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); 1434 EXPECT_VERIFIES_INLINE(&sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0)));
1329 } else { 1435 } else {
1330 EXPECT_FAILURE_INLINE(&sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); 1436 EXPECT_FAILURE_INLINE(&sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0)));
1331 } 1437 }
1332 } 1438 }
1333 } 1439 }
1334 } 1440 }
1335 1441
1442 TEST_F(AstDecoderTest, BreakEnd) {
1443 EXPECT_VERIFIES_INLINE(sigs.i_i(),
1444 B1(WASM_I32_ADD(WASM_BRV(0, WASM_ZERO), WASM_ZERO)));
1445 EXPECT_VERIFIES_INLINE(sigs.i_i(),
1446 B1(WASM_I32_ADD(WASM_ZERO, WASM_BRV(0, WASM_ZERO))));
1447 }
1448
1449 TEST_F(AstDecoderTest, BreakIfBinop) {
1450 EXPECT_FAILURE_INLINE(
1451 sigs.i_i(),
1452 WASM_BLOCK(
1453 1, WASM_I32_ADD(WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO), WASM_ZERO)));
1454 EXPECT_FAILURE_INLINE(
1455 sigs.i_i(),
1456 WASM_BLOCK(
1457 1, WASM_I32_ADD(WASM_ZERO, WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO))));
1458 }
1459
1336 TEST_F(AstDecoderTest, BreakNesting1) { 1460 TEST_F(AstDecoderTest, BreakNesting1) {
1337 for (int i = 0; i < 5; i++) { 1461 for (int i = 0; i < 5; i++) {
1338 // (block[2] (loop[2] (if (get p) break[N]) (set p 1)) p) 1462 // (block[2] (loop[2] (if (get p) break[N]) (set p 1)) p)
1339 byte code[] = {WASM_BLOCK( 1463 byte code[] = {WASM_BLOCK(
1340 2, WASM_LOOP(2, WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(i, WASM_ZERO)), 1464 2, WASM_LOOP(2, WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(i, WASM_ZERO)),
1341 WASM_SET_LOCAL(0, WASM_I8(1))), 1465 WASM_SET_LOCAL(0, WASM_I8(1))),
1342 WASM_GET_LOCAL(0))}; 1466 WASM_GET_LOCAL(0))};
1343 if (i < 3) { 1467 if (i < 3) {
1344 EXPECT_VERIFIES(sigs.i_i(), code); 1468 EXPECT_VERIFIES(sigs.i_i(), code);
1345 } else { 1469 } else {
1346 EXPECT_FAILURE(sigs.i_i(), code); 1470 EXPECT_FAILURE(sigs.i_i(), code);
1347 } 1471 }
1348 } 1472 }
1349 } 1473 }
1350 1474
1351 TEST_F(AstDecoderTest, BreakNesting2) { 1475 TEST_F(AstDecoderTest, BreakNesting2) {
1352 AddLocals(kAstI32, 1); 1476 AddLocals(kAstI32, 1);
1353 for (int i = 0; i < 5; i++) { 1477 for (int i = 0; i < 5; i++) {
1354 // (block[2] (loop[2] (if 0 break[N]) (set p 1)) (return p)) (11) 1478 // (block[2] (loop[2] (if 0 break[N]) (set p 1)) (return p)) (11)
1355 byte code[] = {WASM_BLOCK(1, WASM_LOOP(2, WASM_IF(WASM_ZERO, WASM_BREAK(i)), 1479 byte code[] = {B1(WASM_LOOP(2, WASM_IF(WASM_ZERO, WASM_BREAK(i)),
1356 WASM_SET_LOCAL(0, WASM_I8(1)))), 1480 WASM_SET_LOCAL(0, WASM_I8(1)))),
1357 WASM_I8(11)}; 1481 WASM_I8(11)};
1358 if (i < 2) { 1482 if (i < 2) {
1359 EXPECT_VERIFIES(sigs.v_v(), code); 1483 EXPECT_VERIFIES(sigs.v_v(), code);
1360 } else { 1484 } else {
1361 EXPECT_FAILURE(sigs.v_v(), code); 1485 EXPECT_FAILURE(sigs.v_v(), code);
1362 } 1486 }
1363 } 1487 }
1364 } 1488 }
1365 1489
1366 TEST_F(AstDecoderTest, BreakNesting3) { 1490 TEST_F(AstDecoderTest, BreakNesting3) {
1367 for (int i = 0; i < 5; i++) { 1491 for (int i = 0; i < 5; i++) {
1368 // (block[1] (loop[1] (block[1] (if 0 break[N]) 1492 // (block[1] (loop[1] (block[1] (if 0 break[N])
1369 byte code[] = {WASM_BLOCK( 1493 byte code[] = {
1370 1, WASM_LOOP(1, WASM_BLOCK(1, WASM_IF(WASM_ZERO, WASM_BREAK(i)))))}; 1494 WASM_BLOCK(1, WASM_LOOP(1, B1(WASM_IF(WASM_ZERO, WASM_BREAK(i)))))};
1371 if (i < 3) { 1495 if (i < 3) {
1372 EXPECT_VERIFIES(sigs.v_v(), code); 1496 EXPECT_VERIFIES(sigs.v_v(), code);
1373 } else { 1497 } else {
1374 EXPECT_FAILURE(sigs.v_v(), code); 1498 EXPECT_FAILURE(sigs.v_v(), code);
1375 } 1499 }
1376 } 1500 }
1377 } 1501 }
1378 1502
1379 TEST_F(AstDecoderTest, BreaksWithMultipleTypes) { 1503 TEST_F(AstDecoderTest, BreaksWithMultipleTypes) {
1380 EXPECT_FAILURE_INLINE( 1504 EXPECT_FAILURE_INLINE(sigs.i_i(),
1381 sigs.i_i(), 1505 B2(WASM_BRV_IF_ZERO(0, WASM_I8(7)), WASM_F32(7.7)));
1382 WASM_BLOCK(2, WASM_BRV_IF_ZERO(0, WASM_I8(7)), WASM_F32(7.7)));
1383 1506
1384 EXPECT_FAILURE_INLINE(sigs.i_i(), 1507 EXPECT_FAILURE_INLINE(sigs.i_i(), B2(WASM_BRV_IF_ZERO(0, WASM_I8(7)),
1385 WASM_BLOCK(2, WASM_BRV_IF_ZERO(0, WASM_I8(7)), 1508 WASM_BRV_IF_ZERO(0, WASM_F32(7.7))));
1386 WASM_BRV_IF_ZERO(0, WASM_F32(7.7)))); 1509 EXPECT_FAILURE_INLINE(sigs.i_i(), B3(WASM_BRV_IF_ZERO(0, WASM_I8(8)),
1387 EXPECT_FAILURE_INLINE(sigs.i_i(), 1510 WASM_BRV_IF_ZERO(0, WASM_I8(0)),
1388 WASM_BLOCK(3, WASM_BRV_IF_ZERO(0, WASM_I8(8)), 1511 WASM_BRV_IF_ZERO(0, WASM_F32(7.7))));
1389 WASM_BRV_IF_ZERO(0, WASM_I8(0)), 1512 EXPECT_FAILURE_INLINE(sigs.i_i(), B3(WASM_BRV_IF_ZERO(0, WASM_I8(9)),
1390 WASM_BRV_IF_ZERO(0, WASM_F32(7.7)))); 1513 WASM_BRV_IF_ZERO(0, WASM_F32(7.7)),
1391 EXPECT_FAILURE_INLINE(sigs.i_i(), 1514 WASM_BRV_IF_ZERO(0, WASM_I8(11))));
1392 WASM_BLOCK(3, WASM_BRV_IF_ZERO(0, WASM_I8(9)),
1393 WASM_BRV_IF_ZERO(0, WASM_F32(7.7)),
1394 WASM_BRV_IF_ZERO(0, WASM_I8(11))));
1395 } 1515 }
1396 1516
1397 TEST_F(AstDecoderTest, BreakNesting_6_levels) { 1517 TEST_F(AstDecoderTest, BreakNesting_6_levels) {
1398 for (int mask = 0; mask < 64; mask++) { 1518 for (int mask = 0; mask < 64; mask++) {
1399 for (int i = 0; i < 14; i++) { 1519 for (int i = 0; i < 14; i++) {
1400 byte code[] = { 1520 byte code[] = {
1401 kExprBlock, 1, // -- 1521 kExprBlock, // --
1402 kExprBlock, 1, // -- 1522 kExprBlock, // --
1403 kExprBlock, 1, // -- 1523 kExprBlock, // --
1404 kExprBlock, 1, // -- 1524 kExprBlock, // --
1405 kExprBlock, 1, // -- 1525 kExprBlock, // --
1406 kExprBlock, 1, // -- 1526 kExprBlock, // --
1527 kExprNop, // --
1407 kExprBr, static_cast<byte>(i), 1528 kExprBr, static_cast<byte>(i),
1408 kExprNop // -- 1529 kExprEnd, // --
1530 kExprEnd, // --
1531 kExprEnd, // --
1532 kExprEnd, // --
1533 kExprEnd, // --
1534 kExprEnd // --
1409 }; 1535 };
1410 1536
1411 int depth = 6; 1537 int depth = 6;
1412 for (int l = 0; l < 6; l++) { 1538 for (int l = 0; l < 6; l++) {
1413 if (mask & (1 << l)) { 1539 if (mask & (1 << l)) {
1414 code[l * 2] = kExprLoop; 1540 code[l] = kExprLoop;
1415 depth++; 1541 depth++;
1416 } 1542 }
1417 } 1543 }
1418 1544
1419 if (i < depth) { 1545 if (i < depth) {
1420 EXPECT_VERIFIES(sigs.v_v(), code); 1546 EXPECT_VERIFIES(sigs.v_v(), code);
1421 } else { 1547 } else {
1422 EXPECT_FAILURE(sigs.v_v(), code); 1548 EXPECT_FAILURE(sigs.v_v(), code);
1423 } 1549 }
1424 } 1550 }
1425 } 1551 }
1426 } 1552 }
1427 1553
1428 TEST_F(AstDecoderTest, ExprBreak_TypeCheck) { 1554 TEST_F(AstDecoderTest, ExprBreak_TypeCheck) {
1429 FunctionSig* sigarray[] = {sigs.i_i(), sigs.l_l(), sigs.f_ff(), sigs.d_dd()}; 1555 FunctionSig* sigarray[] = {sigs.i_i(), sigs.l_l(), sigs.f_ff(), sigs.d_dd()};
1430 for (size_t i = 0; i < arraysize(sigarray); i++) { 1556 for (size_t i = 0; i < arraysize(sigarray); i++) {
1431 FunctionSig* sig = sigarray[i]; 1557 FunctionSig* sig = sigarray[i];
1432 // unify X and X => OK 1558 // unify X and X => OK
1433 EXPECT_VERIFIES_INLINE( 1559 EXPECT_VERIFIES_INLINE(
1434 sig, WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), 1560 sig, B2(WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))),
1435 WASM_GET_LOCAL(0))); 1561 WASM_GET_LOCAL(0)));
1436 } 1562 }
1437 1563
1438 // unify i32 and f32 => fail 1564 // unify i32 and f32 => fail
1439 EXPECT_FAILURE_INLINE( 1565 EXPECT_FAILURE_INLINE(
1440 sigs.i_i(), 1566 sigs.i_i(),
1441 WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_ZERO)), WASM_F32(1.2))); 1567 B2(WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_ZERO)), WASM_F32(1.2)));
1442 1568
1443 // unify f64 and f64 => OK 1569 // unify f64 and f64 => OK
1444 EXPECT_VERIFIES_INLINE( 1570 EXPECT_VERIFIES_INLINE(
1445 sigs.d_dd(), 1571 sigs.d_dd(),
1446 WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), 1572 B2(WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), WASM_F64(1.2)));
1447 WASM_F64(1.2)));
1448 } 1573 }
1449 1574
1450 TEST_F(AstDecoderTest, ExprBreak_TypeCheckAll) { 1575 TEST_F(AstDecoderTest, ExprBreak_TypeCheckAll) {
1451 byte code1[] = {WASM_BLOCK(2, 1576 byte code1[] = {WASM_BLOCK(2,
1452 WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), 1577 WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))),
1453 WASM_GET_LOCAL(1))}; 1578 WASM_GET_LOCAL(1))};
1454 byte code2[] = { 1579 byte code2[] = {B2(WASM_IF(WASM_ZERO, WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(0))),
1455 WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(0))), 1580 WASM_GET_LOCAL(1))};
1456 WASM_GET_LOCAL(1))};
1457 1581
1458 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 1582 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
1459 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { 1583 for (size_t j = 0; j < arraysize(kLocalTypes); j++) {
1460 LocalType storage[] = {kLocalTypes[i], kLocalTypes[i], kLocalTypes[j]}; 1584 LocalType storage[] = {kLocalTypes[i], kLocalTypes[i], kLocalTypes[j]};
1461 FunctionSig sig(1, 2, storage); 1585 FunctionSig sig(1, 2, storage);
1462 1586
1463 if (i == j) { 1587 if (i == j) {
1464 EXPECT_VERIFIES(&sig, code1); 1588 EXPECT_VERIFIES(&sig, code1);
1465 EXPECT_VERIFIES(&sig, code2); 1589 EXPECT_VERIFIES(&sig, code2);
1466 } else { 1590 } else {
1467 EXPECT_FAILURE(&sig, code1); 1591 EXPECT_FAILURE(&sig, code1);
1468 EXPECT_FAILURE(&sig, code2); 1592 EXPECT_FAILURE(&sig, code2);
1469 } 1593 }
1470 } 1594 }
1471 } 1595 }
1472 } 1596 }
1473 1597
1474 TEST_F(AstDecoderTest, ExprBr_Unify) { 1598 TEST_F(AstDecoderTest, ExprBr_Unify) {
1475 for (int which = 0; which < 2; which++) { 1599 for (int which = 0; which < 2; which++) {
1476 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 1600 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
1477 LocalType type = kLocalTypes[i]; 1601 LocalType type = kLocalTypes[i];
1478 LocalType storage[] = {kAstI32, kAstI32, type}; 1602 LocalType storage[] = {kAstI32, kAstI32, type};
1479 FunctionSig sig(1, 2, storage); 1603 FunctionSig sig(1, 2, storage);
1480 1604
1481 byte code1[] = { 1605 byte code1[] = {B2(WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(which))),
1482 WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(which))), 1606 WASM_GET_LOCAL(which ^ 1))};
1483 WASM_GET_LOCAL(which ^ 1))};
1484 byte code2[] = { 1607 byte code2[] = {
1485 WASM_LOOP(2, WASM_IF(WASM_ZERO, WASM_BRV(1, WASM_GET_LOCAL(which))), 1608 WASM_LOOP(2, WASM_IF(WASM_ZERO, WASM_BRV(1, WASM_GET_LOCAL(which))),
1486 WASM_GET_LOCAL(which ^ 1))}; 1609 WASM_GET_LOCAL(which ^ 1))};
1487 1610
1488 1611
1489 if (type == kAstI32) { 1612 if (type == kAstI32) {
1490 EXPECT_VERIFIES(&sig, code1); 1613 EXPECT_VERIFIES(&sig, code1);
1491 EXPECT_VERIFIES(&sig, code2); 1614 EXPECT_VERIFIES(&sig, code2);
1492 } else { 1615 } else {
1493 EXPECT_FAILURE(&sig, code1); 1616 EXPECT_FAILURE(&sig, code1);
1494 EXPECT_FAILURE(&sig, code2); 1617 EXPECT_FAILURE(&sig, code2);
1495 } 1618 }
1496 } 1619 }
1497 } 1620 }
1498 } 1621 }
1499 1622
1500 TEST_F(AstDecoderTest, ExprBrIf_cond_type) { 1623 TEST_F(AstDecoderTest, ExprBrIf_cond_type) {
1501 byte code[] = { 1624 byte code[] = {B1(WASM_BRV_IF(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)))};
1502 WASM_BLOCK(1, WASM_BRV_IF(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)))};
1503 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 1625 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
1504 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { 1626 for (size_t j = 0; j < arraysize(kLocalTypes); j++) {
1505 LocalType types[] = {kLocalTypes[i], kLocalTypes[j]}; 1627 LocalType types[] = {kLocalTypes[i], kLocalTypes[j]};
1506 FunctionSig sig(0, 2, types); 1628 FunctionSig sig(0, 2, types);
1507 1629
1508 if (types[1] == kAstI32) { 1630 if (types[1] == kAstI32) {
1509 EXPECT_VERIFIES(&sig, code); 1631 EXPECT_VERIFIES(&sig, code);
1510 } else { 1632 } else {
1511 EXPECT_FAILURE(&sig, code); 1633 EXPECT_FAILURE(&sig, code);
1512 } 1634 }
1513 } 1635 }
1514 } 1636 }
1515 } 1637 }
1516 1638
1517 TEST_F(AstDecoderTest, ExprBrIf_val_type) { 1639 TEST_F(AstDecoderTest, ExprBrIf_val_type) {
1518 byte code[] = { 1640 byte code[] = {B2(WASM_BRV_IF(0, WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)),
1519 WASM_BLOCK(2, WASM_BRV_IF(0, WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)), 1641 WASM_GET_LOCAL(0))};
1520 WASM_GET_LOCAL(0))};
1521 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 1642 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
1522 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { 1643 for (size_t j = 0; j < arraysize(kLocalTypes); j++) {
1523 LocalType types[] = {kLocalTypes[i], kLocalTypes[i], kLocalTypes[j], 1644 LocalType types[] = {kLocalTypes[i], kLocalTypes[i], kLocalTypes[j],
1524 kAstI32}; 1645 kAstI32};
1525 FunctionSig sig(1, 3, types); 1646 FunctionSig sig(1, 3, types);
1526 1647
1527 if (i == j) { 1648 if (i == j) {
1528 EXPECT_VERIFIES(&sig, code); 1649 EXPECT_VERIFIES(&sig, code);
1529 } else { 1650 } else {
1530 EXPECT_FAILURE(&sig, code); 1651 EXPECT_FAILURE(&sig, code);
1531 } 1652 }
1532 } 1653 }
1533 } 1654 }
1534 } 1655 }
1535 1656
1536 TEST_F(AstDecoderTest, ExprBrIf_Unify) { 1657 TEST_F(AstDecoderTest, ExprBrIf_Unify) {
1537 for (int which = 0; which < 2; which++) { 1658 for (int which = 0; which < 2; which++) {
1538 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 1659 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
1539 LocalType type = kLocalTypes[i]; 1660 LocalType type = kLocalTypes[i];
1540 LocalType storage[] = {kAstI32, kAstI32, type}; 1661 LocalType storage[] = {kAstI32, kAstI32, type};
1541 FunctionSig sig(1, 2, storage); 1662 FunctionSig sig(1, 2, storage);
1542 1663
1543 byte code1[] = {WASM_BLOCK(2, WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(which)), 1664 byte code1[] = {B2(WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(which)),
1544 WASM_GET_LOCAL(which ^ 1))}; 1665 WASM_GET_LOCAL(which ^ 1))};
1545 byte code2[] = {WASM_LOOP(2, WASM_BRV_IF_ZERO(1, WASM_GET_LOCAL(which)), 1666 byte code2[] = {WASM_LOOP(2, WASM_BRV_IF_ZERO(1, WASM_GET_LOCAL(which)),
1546 WASM_GET_LOCAL(which ^ 1))}; 1667 WASM_GET_LOCAL(which ^ 1))};
1547 1668
1548 if (type == kAstI32) { 1669 if (type == kAstI32) {
1549 EXPECT_VERIFIES(&sig, code1); 1670 EXPECT_VERIFIES(&sig, code1);
1550 EXPECT_VERIFIES(&sig, code2); 1671 EXPECT_VERIFIES(&sig, code2);
1551 } else { 1672 } else {
1552 EXPECT_FAILURE(&sig, code1); 1673 EXPECT_FAILURE(&sig, code1);
1553 EXPECT_FAILURE(&sig, code2); 1674 EXPECT_FAILURE(&sig, code2);
1554 } 1675 }
(...skipping 12 matching lines...) Expand all
1567 EXPECT_FAILURE(sigs.i_i(), code); 1688 EXPECT_FAILURE(sigs.i_i(), code);
1568 } 1689 }
1569 1690
1570 TEST_F(AstDecoderTest, BrTable0c) { 1691 TEST_F(AstDecoderTest, BrTable0c) {
1571 static byte code[] = {kExprBrTable, 0, 1, 0, 0, kExprI32Const, 11}; 1692 static byte code[] = {kExprBrTable, 0, 1, 0, 0, kExprI32Const, 11};
1572 EXPECT_FAILURE(sigs.v_v(), code); 1693 EXPECT_FAILURE(sigs.v_v(), code);
1573 EXPECT_FAILURE(sigs.i_i(), code); 1694 EXPECT_FAILURE(sigs.i_i(), code);
1574 } 1695 }
1575 1696
1576 TEST_F(AstDecoderTest, BrTable1a) { 1697 TEST_F(AstDecoderTest, BrTable1a) {
1577 static byte code[] = { 1698 static byte code[] = {B1(WASM_BR_TABLE(WASM_I8(67), 0, BR_TARGET(0)))};
1578 WASM_BLOCK(1, WASM_BR_TABLE(WASM_I8(67), 0, BR_TARGET(0)))};
1579 EXPECT_VERIFIES(sigs.v_v(), code); 1699 EXPECT_VERIFIES(sigs.v_v(), code);
1580 } 1700 }
1581 1701
1582 TEST_F(AstDecoderTest, BrTable1b) { 1702 TEST_F(AstDecoderTest, BrTable1b) {
1583 static byte code[] = { 1703 static byte code[] = {B1(WASM_BR_TABLE(WASM_ZERO, 0, BR_TARGET(0)))};
1584 WASM_BLOCK(1, WASM_BR_TABLE(WASM_ZERO, 0, BR_TARGET(0)))};
1585 EXPECT_VERIFIES(sigs.v_v(), code); 1704 EXPECT_VERIFIES(sigs.v_v(), code);
1586 EXPECT_FAILURE(sigs.i_i(), code); 1705 EXPECT_FAILURE(sigs.i_i(), code);
1587 EXPECT_FAILURE(sigs.f_ff(), code); 1706 EXPECT_FAILURE(sigs.f_ff(), code);
1588 EXPECT_FAILURE(sigs.d_dd(), code); 1707 EXPECT_FAILURE(sigs.d_dd(), code);
1589 } 1708 }
1590 1709
1591 TEST_F(AstDecoderTest, BrTable2a) { 1710 TEST_F(AstDecoderTest, BrTable2a) {
1592 static byte code[] = { 1711 static byte code[] = {
1593 WASM_BLOCK(1, WASM_BR_TABLE(WASM_I8(67), 1, BR_TARGET(0), BR_TARGET(0)))}; 1712 B1(WASM_BR_TABLE(WASM_I8(67), 1, BR_TARGET(0), BR_TARGET(0)))};
1594 EXPECT_VERIFIES(sigs.v_v(), code); 1713 EXPECT_VERIFIES(sigs.v_v(), code);
1595 } 1714 }
1596 1715
1597 TEST_F(AstDecoderTest, BrTable2b) { 1716 TEST_F(AstDecoderTest, BrTable2b) {
1598 static byte code[] = {WASM_BLOCK( 1717 static byte code[] = {WASM_BLOCK(
1599 1, WASM_BLOCK( 1718 1, WASM_BLOCK(
1600 1, WASM_BR_TABLE(WASM_I8(67), 1, BR_TARGET(0), BR_TARGET(1))))}; 1719 1, WASM_BR_TABLE(WASM_I8(67), 1, BR_TARGET(0), BR_TARGET(1))))};
1601 EXPECT_VERIFIES(sigs.v_v(), code); 1720 EXPECT_VERIFIES(sigs.v_v(), code);
1602 } 1721 }
1603 1722
1604 TEST_F(AstDecoderTest, BrTable_off_end) { 1723 TEST_F(AstDecoderTest, BrTable_off_end) {
1605 static byte code[] = { 1724 static byte code[] = {B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(0)))};
1606 WASM_BLOCK(1, WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(0)))};
1607 for (size_t len = 1; len < sizeof(code); len++) { 1725 for (size_t len = 1; len < sizeof(code); len++) {
1608 Verify(kError, sigs.i_i(), code, code + len); 1726 Verify(kError, sigs.i_i(), code, code + len);
1609 } 1727 }
1610 } 1728 }
1611 1729
1612 TEST_F(AstDecoderTest, BrTable_invalid_br1) { 1730 TEST_F(AstDecoderTest, BrTable_invalid_br1) {
1613 for (int depth = 0; depth < 4; depth++) { 1731 for (int depth = 0; depth < 4; depth++) {
1614 byte code[] = { 1732 byte code[] = {B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(depth)))};
1615 WASM_BLOCK(1, WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(depth)))};
1616 if (depth == 0) { 1733 if (depth == 0) {
1617 EXPECT_VERIFIES(sigs.v_i(), code); 1734 EXPECT_VERIFIES(sigs.v_i(), code);
1618 } else { 1735 } else {
1619 EXPECT_FAILURE(sigs.v_i(), code); 1736 EXPECT_FAILURE(sigs.v_i(), code);
1620 } 1737 }
1621 } 1738 }
1622 } 1739 }
1623 1740
1624 TEST_F(AstDecoderTest, BrTable_invalid_br2) { 1741 TEST_F(AstDecoderTest, BrTable_invalid_br2) {
1625 for (int depth = 0; depth < 4; depth++) { 1742 for (int depth = 0; depth < 4; depth++) {
1626 byte code[] = { 1743 byte code[] = {
1627 WASM_LOOP(1, WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(depth)))}; 1744 WASM_LOOP(1, WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(depth)))};
1628 if (depth <= 1) { 1745 if (depth <= 1) {
1629 EXPECT_VERIFIES(sigs.v_i(), code); 1746 EXPECT_VERIFIES(sigs.v_i(), code);
1630 } else { 1747 } else {
1631 EXPECT_FAILURE(sigs.v_i(), code); 1748 EXPECT_FAILURE(sigs.v_i(), code);
1632 } 1749 }
1633 } 1750 }
1634 } 1751 }
1635 1752
1636 TEST_F(AstDecoderTest, ExprBreakNesting1) { 1753 TEST_F(AstDecoderTest, ExprBreakNesting1) {
1637 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_BLOCK(1, WASM_BRV(0, WASM_ZERO))); 1754 EXPECT_VERIFIES_INLINE(sigs.v_v(), B1(WASM_BRV(0, WASM_ZERO)));
1638 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_BLOCK(1, WASM_BR(0))); 1755 EXPECT_VERIFIES_INLINE(sigs.v_v(), B1(WASM_BR(0)));
1639 EXPECT_VERIFIES_INLINE(sigs.v_v(), 1756 EXPECT_VERIFIES_INLINE(sigs.v_v(), B1(WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO)));
1640 WASM_BLOCK(1, WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO))); 1757 EXPECT_VERIFIES_INLINE(sigs.v_v(), B1(WASM_BR_IF(0, WASM_ZERO)));
1641 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_BLOCK(1, WASM_BR_IF(0, WASM_ZERO)));
1642 1758
1643 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BRV(0, WASM_ZERO))); 1759 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BRV(0, WASM_ZERO)));
1644 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BR(0))); 1760 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BR(0)));
1645 EXPECT_VERIFIES_INLINE(sigs.v_v(), 1761 EXPECT_VERIFIES_INLINE(sigs.v_v(),
1646 WASM_LOOP(1, WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO))); 1762 WASM_LOOP(1, WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO)));
1647 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BR_IF(0, WASM_ZERO))); 1763 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BR_IF(0, WASM_ZERO)));
1648 1764
1649 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BRV(1, WASM_ZERO))); 1765 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BRV(1, WASM_ZERO)));
1650 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BR(1))); 1766 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BR(1)));
1651 } 1767 }
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
1726 } 1842 }
1727 1843
1728 #define EXPECT_LENGTH_N(expected, ...) \ 1844 #define EXPECT_LENGTH_N(expected, ...) \
1729 { \ 1845 { \
1730 static const byte code[] = {__VA_ARGS__}; \ 1846 static const byte code[] = {__VA_ARGS__}; \
1731 EXPECT_EQ(expected, OpcodeLength(code, code + sizeof(code))); \ 1847 EXPECT_EQ(expected, OpcodeLength(code, code + sizeof(code))); \
1732 } 1848 }
1733 1849
1734 TEST_F(WasmOpcodeLengthTest, Statements) { 1850 TEST_F(WasmOpcodeLengthTest, Statements) {
1735 EXPECT_LENGTH(1, kExprNop); 1851 EXPECT_LENGTH(1, kExprNop);
1736 EXPECT_LENGTH(2, kExprBlock); 1852 EXPECT_LENGTH(1, kExprBlock);
1737 EXPECT_LENGTH(2, kExprLoop); 1853 EXPECT_LENGTH(1, kExprLoop);
1738 EXPECT_LENGTH(1, kExprIf); 1854 EXPECT_LENGTH(1, kExprIf);
1739 EXPECT_LENGTH(1, kExprIfElse); 1855 EXPECT_LENGTH(1, kExprElse);
1856 EXPECT_LENGTH(1, kExprEnd);
1740 EXPECT_LENGTH(1, kExprSelect); 1857 EXPECT_LENGTH(1, kExprSelect);
1741 EXPECT_LENGTH(2, kExprBr); 1858 EXPECT_LENGTH(2, kExprBr);
1742 EXPECT_LENGTH(2, kExprBrIf); 1859 EXPECT_LENGTH(2, kExprBrIf);
1743 } 1860 }
1744 1861
1745 1862
1746 TEST_F(WasmOpcodeLengthTest, MiscExpressions) { 1863 TEST_F(WasmOpcodeLengthTest, MiscExpressions) {
1747 EXPECT_LENGTH(2, kExprI8Const); 1864 EXPECT_LENGTH(2, kExprI8Const);
1748 EXPECT_LENGTH(5, kExprF32Const); 1865 EXPECT_LENGTH(5, kExprF32Const);
1749 EXPECT_LENGTH(9, kExprF64Const); 1866 EXPECT_LENGTH(9, kExprF64Const);
1750 EXPECT_LENGTH(2, kExprGetLocal); 1867 EXPECT_LENGTH(2, kExprGetLocal);
1751 EXPECT_LENGTH(2, kExprSetLocal); 1868 EXPECT_LENGTH(2, kExprSetLocal);
1752 EXPECT_LENGTH(2, kExprLoadGlobal); 1869 EXPECT_LENGTH(2, kExprLoadGlobal);
1753 EXPECT_LENGTH(2, kExprStoreGlobal); 1870 EXPECT_LENGTH(2, kExprStoreGlobal);
1754 EXPECT_LENGTH(2, kExprCallFunction); 1871 EXPECT_LENGTH(2, kExprCallFunction);
1755 EXPECT_LENGTH(2, kExprCallImport); 1872 EXPECT_LENGTH(2, kExprCallImport);
1756 EXPECT_LENGTH(2, kExprCallIndirect); 1873 EXPECT_LENGTH(2, kExprCallIndirect);
1757 EXPECT_LENGTH(1, kExprIf); 1874 EXPECT_LENGTH(1, kExprIf);
1758 EXPECT_LENGTH(1, kExprIfElse); 1875 EXPECT_LENGTH(1, kExprBlock);
1759 EXPECT_LENGTH(2, kExprBlock); 1876 EXPECT_LENGTH(1, kExprLoop);
1760 EXPECT_LENGTH(2, kExprLoop);
1761 EXPECT_LENGTH(2, kExprBr); 1877 EXPECT_LENGTH(2, kExprBr);
1762 EXPECT_LENGTH(2, kExprBrIf); 1878 EXPECT_LENGTH(2, kExprBrIf);
1763 } 1879 }
1764 1880
1765 TEST_F(WasmOpcodeLengthTest, I32Const) { 1881 TEST_F(WasmOpcodeLengthTest, I32Const) {
1766 EXPECT_LENGTH_N(2, kExprI32Const, U32V_1(1)); 1882 EXPECT_LENGTH_N(2, kExprI32Const, U32V_1(1));
1767 EXPECT_LENGTH_N(3, kExprI32Const, U32V_2(999)); 1883 EXPECT_LENGTH_N(3, kExprI32Const, U32V_2(999));
1768 EXPECT_LENGTH_N(4, kExprI32Const, U32V_3(9999)); 1884 EXPECT_LENGTH_N(4, kExprI32Const, U32V_3(9999));
1769 EXPECT_LENGTH_N(5, kExprI32Const, U32V_4(999999)); 1885 EXPECT_LENGTH_N(5, kExprI32Const, U32V_4(999999));
1770 EXPECT_LENGTH_N(6, kExprI32Const, U32V_5(99999999)); 1886 EXPECT_LENGTH_N(6, kExprI32Const, U32V_5(99999999));
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
1968 EXPECT_ARITY(2, kExprBlock, 2); 2084 EXPECT_ARITY(2, kExprBlock, 2);
1969 EXPECT_ARITY(5, kExprBlock, 5); 2085 EXPECT_ARITY(5, kExprBlock, 5);
1970 EXPECT_ARITY(10, kExprBlock, 10); 2086 EXPECT_ARITY(10, kExprBlock, 10);
1971 2087
1972 EXPECT_ARITY(0, kExprLoop, 0); 2088 EXPECT_ARITY(0, kExprLoop, 0);
1973 EXPECT_ARITY(1, kExprLoop, 1); 2089 EXPECT_ARITY(1, kExprLoop, 1);
1974 EXPECT_ARITY(2, kExprLoop, 2); 2090 EXPECT_ARITY(2, kExprLoop, 2);
1975 EXPECT_ARITY(7, kExprLoop, 7); 2091 EXPECT_ARITY(7, kExprLoop, 7);
1976 EXPECT_ARITY(11, kExprLoop, 11); 2092 EXPECT_ARITY(11, kExprLoop, 11);
1977 2093
1978 EXPECT_ARITY(2, kExprIf);
1979 EXPECT_ARITY(3, kExprIfElse);
1980 EXPECT_ARITY(3, kExprSelect); 2094 EXPECT_ARITY(3, kExprSelect);
1981 2095
1982 EXPECT_ARITY(1, kExprBr); 2096 EXPECT_ARITY(1, kExprBr);
1983 EXPECT_ARITY(2, kExprBrIf); 2097 EXPECT_ARITY(2, kExprBrIf);
1984 2098
1985 { 2099 {
1986 sig = sigs.v_v(); 2100 sig = sigs.v_v();
1987 EXPECT_ARITY(0, kExprReturn); 2101 EXPECT_ARITY(0, kExprReturn);
1988 sig = sigs.i_i(); 2102 sig = sigs.i_i();
1989 EXPECT_ARITY(1, kExprReturn); 2103 EXPECT_ARITY(1, kExprReturn);
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
2280 pos = ExpectRun(map, pos, kAstI32, 1337); 2394 pos = ExpectRun(map, pos, kAstI32, 1337);
2281 pos = ExpectRun(map, pos, kAstI64, 212); 2395 pos = ExpectRun(map, pos, kAstI64, 212);
2282 2396
2283 if (map) delete map; 2397 if (map) delete map;
2284 delete[] data; 2398 delete[] data;
2285 } 2399 }
2286 2400
2287 } // namespace wasm 2401 } // namespace wasm
2288 } // namespace internal 2402 } // namespace internal
2289 } // namespace v8 2403 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698