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

Side by Side Diff: test/cctest/wasm/test-run-wasm.cc

Issue 1707403002: [wasm] Refactor WASM test usage of TestingModule. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 10 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
« no previous file with comments | « no previous file | test/cctest/wasm/test-run-wasm-js.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <stdint.h> 5 #include <stdint.h>
6 #include <stdlib.h> 6 #include <stdlib.h>
7 #include <string.h> 7 #include <string.h>
8 8
9 #include "src/wasm/wasm-macro-gen.h" 9 #include "src/wasm/wasm-macro-gen.h"
10 10
11 #include "test/cctest/cctest.h" 11 #include "test/cctest/cctest.h"
12 #include "test/cctest/compiler/value-helper.h" 12 #include "test/cctest/compiler/value-helper.h"
13 #include "test/cctest/wasm/test-signatures.h" 13 #include "test/cctest/wasm/test-signatures.h"
14 #include "test/cctest/wasm/wasm-run-utils.h" 14 #include "test/cctest/wasm/wasm-run-utils.h"
15 15
16 using namespace v8::base; 16 using namespace v8::base;
17 using namespace v8::internal; 17 using namespace v8::internal;
18 using namespace v8::internal::compiler; 18 using namespace v8::internal::compiler;
19 using namespace v8::internal::wasm; 19 using namespace v8::internal::wasm;
20 20
21 #define BUILD(r, ...) \ 21 #define BUILD(r, ...) \
22 do { \ 22 do { \
23 byte code[] = {__VA_ARGS__}; \ 23 byte code[] = {__VA_ARGS__}; \
24 r.Build(code, code + arraysize(code)); \ 24 r.Build(code, code + arraysize(code)); \
25 } while (false) 25 } while (false)
26 26
27 27
28 TEST(Run_WasmInt8Const) { 28 TEST(Run_WasmInt8Const) {
29 WasmRunner<int8_t> r; 29 WasmRunner<int32_t> r;
30 const byte kExpectedValue = 121; 30 const byte kExpectedValue = 121;
31 // return(kExpectedValue) 31 // return(kExpectedValue)
32 BUILD(r, WASM_I8(kExpectedValue)); 32 BUILD(r, WASM_I8(kExpectedValue));
33 CHECK_EQ(kExpectedValue, r.Call()); 33 CHECK_EQ(kExpectedValue, r.Call());
34 } 34 }
35 35
36 36
37 TEST(Run_WasmInt8Const_fallthru1) { 37 TEST(Run_WasmInt8Const_fallthru1) {
38 WasmRunner<int8_t> r; 38 WasmRunner<int32_t> r;
39 const byte kExpectedValue = 122; 39 const byte kExpectedValue = 122;
40 // kExpectedValue 40 // kExpectedValue
41 BUILD(r, WASM_I8(kExpectedValue)); 41 BUILD(r, WASM_I8(kExpectedValue));
42 CHECK_EQ(kExpectedValue, r.Call()); 42 CHECK_EQ(kExpectedValue, r.Call());
43 } 43 }
44 44
45 45
46 TEST(Run_WasmInt8Const_fallthru2) { 46 TEST(Run_WasmInt8Const_fallthru2) {
47 WasmRunner<int8_t> r; 47 WasmRunner<int32_t> r;
48 const byte kExpectedValue = 123; 48 const byte kExpectedValue = 123;
49 // -99 kExpectedValue 49 // -99 kExpectedValue
50 BUILD(r, WASM_I8(-99), WASM_I8(kExpectedValue)); 50 BUILD(r, WASM_I8(-99), WASM_I8(kExpectedValue));
51 CHECK_EQ(kExpectedValue, r.Call()); 51 CHECK_EQ(kExpectedValue, r.Call());
52 } 52 }
53 53
54 54
55 TEST(Run_WasmInt8Const_all) { 55 TEST(Run_WasmInt8Const_all) {
56 for (int value = -128; value <= 127; value++) { 56 for (int value = -128; value <= 127; value++) {
57 WasmRunner<int8_t> r; 57 WasmRunner<int32_t> r;
58 // return(value) 58 // return(value)
59 BUILD(r, WASM_I8(value)); 59 BUILD(r, WASM_I8(value));
60 int8_t result = r.Call(); 60 int32_t result = r.Call();
61 CHECK_EQ(value, result); 61 CHECK_EQ(value, result);
62 } 62 }
63 } 63 }
64 64
65 65
66 TEST(Run_WasmInt32Const) { 66 TEST(Run_WasmInt32Const) {
67 WasmRunner<int32_t> r; 67 WasmRunner<int32_t> r;
68 const int32_t kExpectedValue = 0x11223344; 68 const int32_t kExpectedValue = 0x11223344;
69 // return(kExpectedValue) 69 // return(kExpectedValue)
70 BUILD(r, WASM_I32(kExpectedValue)); 70 BUILD(r, WASM_I32(kExpectedValue));
71 CHECK_EQ(kExpectedValue, r.Call()); 71 CHECK_EQ(kExpectedValue, r.Call());
72 } 72 }
73 73
74 74
75 TEST(Run_WasmInt32Const_many) { 75 TEST(Run_WasmInt32Const_many) {
76 FOR_INT32_INPUTS(i) { 76 FOR_INT32_INPUTS(i) {
77 WasmRunner<int32_t> r; 77 WasmRunner<int32_t> r;
78 const int32_t kExpectedValue = *i; 78 const int32_t kExpectedValue = *i;
79 // return(kExpectedValue) 79 // return(kExpectedValue)
80 BUILD(r, WASM_I32(kExpectedValue)); 80 BUILD(r, WASM_I32(kExpectedValue));
81 CHECK_EQ(kExpectedValue, r.Call()); 81 CHECK_EQ(kExpectedValue, r.Call());
82 } 82 }
83 } 83 }
84 84
85 85
86 TEST(Run_WasmMemorySize) { 86 TEST(Run_WasmMemorySize) {
87 WasmRunner<int32_t> r;
88 TestingModule module; 87 TestingModule module;
ahaas 2016/02/18 14:17:46 Why do you declare the TestingModule in the test?
titzer 2016/02/18 14:59:36 Many tests don't use need module, and there is alr
ahaas 2016/02/18 15:02:47 I don't agree, but that's only a personal preferen
88 WasmRunner<int32_t> r(&module);
89 module.AddMemory(1024); 89 module.AddMemory(1024);
90 r.env()->module = &module;
91 BUILD(r, kExprMemorySize); 90 BUILD(r, kExprMemorySize);
92 CHECK_EQ(1024, r.Call()); 91 CHECK_EQ(1024, r.Call());
93 } 92 }
94 93
95 94
96 #if WASM_64 95 #if WASM_64
97 TEST(Run_WasmInt64Const) { 96 TEST(Run_WasmInt64Const) {
98 WasmRunner<int64_t> r; 97 WasmRunner<int64_t> r;
99 const int64_t kExpectedValue = 0x1122334455667788LL; 98 const int64_t kExpectedValue = 0x1122334455667788LL;
100 // return(kExpectedValue) 99 // return(kExpectedValue)
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 CHECK_TRAP(r.Call(val)); 597 CHECK_TRAP(r.Call(val));
599 } else { 598 } else {
600 CHECK_EQ(val / denom, r.Call(val)); 599 CHECK_EQ(val / denom, r.Call(val));
601 } 600 }
602 } 601 }
603 } 602 }
604 } 603 }
605 604
606 605
607 TEST(Run_WASM_Int32DivS_trap_effect) { 606 TEST(Run_WASM_Int32DivS_trap_effect) {
608 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32());
609 TestingModule module; 607 TestingModule module;
610 module.AddMemoryElems<int32_t>(8); 608 module.AddMemoryElems<int32_t>(8);
611 r.env()->module = &module; 609 WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32());
612 610
613 BUILD(r, 611 BUILD(r,
614 WASM_IF_ELSE(WASM_GET_LOCAL(0), 612 WASM_IF_ELSE(WASM_GET_LOCAL(0),
615 WASM_I32_DIVS(WASM_STORE_MEM(MachineType::Int8(), 613 WASM_I32_DIVS(WASM_STORE_MEM(MachineType::Int8(),
616 WASM_ZERO, WASM_GET_LOCAL(0)), 614 WASM_ZERO, WASM_GET_LOCAL(0)),
617 WASM_GET_LOCAL(1)), 615 WASM_GET_LOCAL(1)),
618 WASM_I32_DIVS(WASM_STORE_MEM(MachineType::Int8(), 616 WASM_I32_DIVS(WASM_STORE_MEM(MachineType::Int8(),
619 WASM_ZERO, WASM_GET_LOCAL(0)), 617 WASM_ZERO, WASM_GET_LOCAL(0)),
620 WASM_GET_LOCAL(1)))); 618 WASM_GET_LOCAL(1))));
621 CHECK_EQ(0, r.Call(0, 100)); 619 CHECK_EQ(0, r.Call(0, 100));
(...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after
1216 1214
1217 CHECK_EQ(203, r.Call(0, 200)); 1215 CHECK_EQ(203, r.Call(0, 200));
1218 CHECK_EQ(202, r.Call(1, 200)); 1216 CHECK_EQ(202, r.Call(1, 200));
1219 CHECK_EQ(212, r.Call(2, 200)); 1217 CHECK_EQ(212, r.Call(2, 200));
1220 CHECK_EQ(208, r.Call(3, 200)); 1218 CHECK_EQ(208, r.Call(3, 200));
1221 CHECK_EQ(208, r.Call(4, 200)); 1219 CHECK_EQ(208, r.Call(4, 200));
1222 } 1220 }
1223 1221
1224 1222
1225 TEST(Run_Wasm_F32ReinterpretI32) { 1223 TEST(Run_Wasm_F32ReinterpretI32) {
1226 WasmRunner<int32_t> r;
1227 TestingModule module; 1224 TestingModule module;
1228 int32_t* memory = module.AddMemoryElems<int32_t>(8); 1225 int32_t* memory = module.AddMemoryElems<int32_t>(8);
1229 r.env()->module = &module; 1226 WasmRunner<int32_t> r(&module);
1230 1227
1231 BUILD(r, WASM_I32_REINTERPRET_F32( 1228 BUILD(r, WASM_I32_REINTERPRET_F32(
1232 WASM_LOAD_MEM(MachineType::Float32(), WASM_ZERO))); 1229 WASM_LOAD_MEM(MachineType::Float32(), WASM_ZERO)));
1233 1230
1234 FOR_INT32_INPUTS(i) { 1231 FOR_INT32_INPUTS(i) {
1235 int32_t expected = *i; 1232 int32_t expected = *i;
1236 memory[0] = expected; 1233 memory[0] = expected;
1237 CHECK_EQ(expected, r.Call()); 1234 CHECK_EQ(expected, r.Call());
1238 } 1235 }
1239 } 1236 }
1240 1237
1241 1238
1242 TEST(Run_Wasm_I32ReinterpretF32) { 1239 TEST(Run_Wasm_I32ReinterpretF32) {
1243 WasmRunner<int32_t> r(MachineType::Int32());
1244 TestingModule module; 1240 TestingModule module;
1245 int32_t* memory = module.AddMemoryElems<int32_t>(8); 1241 int32_t* memory = module.AddMemoryElems<int32_t>(8);
1246 r.env()->module = &module; 1242 WasmRunner<int32_t> r(&module, MachineType::Int32());
1247 1243
1248 BUILD(r, WASM_BLOCK( 1244 BUILD(r, WASM_BLOCK(
1249 2, WASM_STORE_MEM(MachineType::Float32(), WASM_ZERO, 1245 2, WASM_STORE_MEM(MachineType::Float32(), WASM_ZERO,
1250 WASM_F32_REINTERPRET_I32(WASM_GET_LOCAL(0))), 1246 WASM_F32_REINTERPRET_I32(WASM_GET_LOCAL(0))),
1251 WASM_I8(107))); 1247 WASM_I8(107)));
1252 1248
1253 FOR_INT32_INPUTS(i) { 1249 FOR_INT32_INPUTS(i) {
1254 int32_t expected = *i; 1250 int32_t expected = *i;
1255 CHECK_EQ(107, r.Call(expected)); 1251 CHECK_EQ(107, r.Call(expected));
1256 CHECK_EQ(expected, memory[0]); 1252 CHECK_EQ(expected, memory[0]);
1257 } 1253 }
1258 } 1254 }
1259 1255
1260 1256
1261 TEST(Run_Wasm_ReturnStore) { 1257 TEST(Run_Wasm_ReturnStore) {
1262 WasmRunner<int32_t> r;
1263 TestingModule module; 1258 TestingModule module;
1264 int32_t* memory = module.AddMemoryElems<int32_t>(8); 1259 int32_t* memory = module.AddMemoryElems<int32_t>(8);
1265 r.env()->module = &module; 1260 WasmRunner<int32_t> r(&module);
1266 1261
1267 BUILD(r, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, 1262 BUILD(r, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO,
1268 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO))); 1263 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)));
1269 1264
1270 FOR_INT32_INPUTS(i) { 1265 FOR_INT32_INPUTS(i) {
1271 int32_t expected = *i; 1266 int32_t expected = *i;
1272 memory[0] = expected; 1267 memory[0] = expected;
1273 CHECK_EQ(expected, r.Call()); 1268 CHECK_EQ(expected, r.Call());
1274 } 1269 }
1275 } 1270 }
1276 1271
1277 1272
1278 TEST(Run_Wasm_VoidReturn1) { 1273 TEST(Run_Wasm_VoidReturn1) {
1279 // We use a wrapper function because WasmRunner<void> does not exist. 1274 // We use a wrapper function because WasmRunner<void> does not exist.
1280 1275
1281 // Build the test function. 1276 // Build the test function.
1282 TestSignatures sigs; 1277 TestSignatures sigs;
1283 TestingModule module; 1278 TestingModule module;
1284 WasmFunctionCompiler t(sigs.v_v()); 1279 WasmFunctionCompiler t(sigs.v_v(), &module);
1285 BUILD(t, kExprNop); 1280 BUILD(t, kExprNop);
1286 uint32_t index = t.CompileAndAdd(&module); 1281 uint32_t index = t.CompileAndAdd();
1287 1282
1288 const int32_t kExpected = -414444; 1283 const int32_t kExpected = -414444;
1289 // Build the calling function. 1284 // Build the calling function.
1290 WasmRunner<int32_t> r; 1285 WasmRunner<int32_t> r;
1291 r.env()->module = &module; 1286 r.env()->module = &module;
1292 BUILD(r, WASM_BLOCK(2, WASM_CALL_FUNCTION0(index), WASM_I32(kExpected))); 1287 BUILD(r, WASM_BLOCK(2, WASM_CALL_FUNCTION0(index), WASM_I32(kExpected)));
1293 1288
1294 int32_t result = r.Call(); 1289 int32_t result = r.Call();
1295 CHECK_EQ(kExpected, result); 1290 CHECK_EQ(kExpected, result);
1296 } 1291 }
1297 1292
1298 1293
1299 TEST(Run_Wasm_VoidReturn2) { 1294 TEST(Run_Wasm_VoidReturn2) {
1300 // We use a wrapper function because WasmRunner<void> does not exist. 1295 // We use a wrapper function because WasmRunner<void> does not exist.
1301 // Build the test function. 1296 // Build the test function.
1302 TestSignatures sigs; 1297 TestSignatures sigs;
1303 TestingModule module; 1298 TestingModule module;
1304 WasmFunctionCompiler t(sigs.v_v()); 1299 WasmFunctionCompiler t(sigs.v_v(), &module);
1305 BUILD(t, WASM_RETURN0); 1300 BUILD(t, WASM_RETURN0);
1306 uint32_t index = t.CompileAndAdd(&module); 1301 uint32_t index = t.CompileAndAdd();
1307 1302
1308 const int32_t kExpected = -414444; 1303 const int32_t kExpected = -414444;
1309 // Build the calling function. 1304 // Build the calling function.
1310 WasmRunner<int32_t> r; 1305 WasmRunner<int32_t> r;
1311 r.env()->module = &module; 1306 r.env()->module = &module;
1312 BUILD(r, WASM_BLOCK(2, WASM_CALL_FUNCTION0(index), WASM_I32(kExpected))); 1307 BUILD(r, WASM_BLOCK(2, WASM_CALL_FUNCTION0(index), WASM_I32(kExpected)));
1313 1308
1314 int32_t result = r.Call(); 1309 int32_t result = r.Call();
1315 CHECK_EQ(kExpected, result); 1310 CHECK_EQ(kExpected, result);
1316 } 1311 }
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
1492 WASM_SET_LOCAL(0, WASM_I8(93)))), 1487 WASM_SET_LOCAL(0, WASM_I8(93)))),
1493 WASM_GET_LOCAL(0)); 1488 WASM_GET_LOCAL(0));
1494 CHECK_EQ(93, r.Call(0)); 1489 CHECK_EQ(93, r.Call(0));
1495 CHECK_EQ(3, r.Call(3)); 1490 CHECK_EQ(3, r.Call(3));
1496 CHECK_EQ(10001, r.Call(10001)); 1491 CHECK_EQ(10001, r.Call(10001));
1497 CHECK_EQ(-22, r.Call(-22)); 1492 CHECK_EQ(-22, r.Call(-22));
1498 } 1493 }
1499 1494
1500 1495
1501 TEST(Run_Wasm_LoadMemI32) { 1496 TEST(Run_Wasm_LoadMemI32) {
1502 WasmRunner<int32_t> r(MachineType::Int32());
1503 TestingModule module; 1497 TestingModule module;
1504 int32_t* memory = module.AddMemoryElems<int32_t>(8); 1498 int32_t* memory = module.AddMemoryElems<int32_t>(8);
1499 WasmRunner<int32_t> r(&module, MachineType::Int32());
1505 module.RandomizeMemory(1111); 1500 module.RandomizeMemory(1111);
1506 r.env()->module = &module;
1507 1501
1508 BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(0))); 1502 BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(0)));
1509 1503
1510 memory[0] = 99999999; 1504 memory[0] = 99999999;
1511 CHECK_EQ(99999999, r.Call(0)); 1505 CHECK_EQ(99999999, r.Call(0));
1512 1506
1513 memory[0] = 88888888; 1507 memory[0] = 88888888;
1514 CHECK_EQ(88888888, r.Call(0)); 1508 CHECK_EQ(88888888, r.Call(0));
1515 1509
1516 memory[0] = 77777777; 1510 memory[0] = 77777777;
1517 CHECK_EQ(77777777, r.Call(0)); 1511 CHECK_EQ(77777777, r.Call(0));
1518 } 1512 }
1519 1513
1520 1514
1521 TEST(Run_Wasm_LoadMemI32_oob) { 1515 TEST(Run_Wasm_LoadMemI32_oob) {
1522 WasmRunner<int32_t> r(MachineType::Uint32());
1523 TestingModule module; 1516 TestingModule module;
1524 int32_t* memory = module.AddMemoryElems<int32_t>(8); 1517 int32_t* memory = module.AddMemoryElems<int32_t>(8);
1518 WasmRunner<int32_t> r(&module, MachineType::Uint32());
1525 module.RandomizeMemory(1111); 1519 module.RandomizeMemory(1111);
1526 r.env()->module = &module;
1527 1520
1528 BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0))); 1521 BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0)));
1529 1522
1530 memory[0] = 88888888; 1523 memory[0] = 88888888;
1531 CHECK_EQ(88888888, r.Call(0u)); 1524 CHECK_EQ(88888888, r.Call(0u));
1532 for (uint32_t offset = 29; offset < 40; offset++) { 1525 for (uint32_t offset = 29; offset < 40; offset++) {
1533 CHECK_TRAP(r.Call(offset)); 1526 CHECK_TRAP(r.Call(offset));
1534 } 1527 }
1535 1528
1536 for (uint32_t offset = 0x80000000; offset < 0x80000010; offset++) { 1529 for (uint32_t offset = 0x80000000; offset < 0x80000010; offset++) {
1537 CHECK_TRAP(r.Call(offset)); 1530 CHECK_TRAP(r.Call(offset));
1538 } 1531 }
1539 } 1532 }
1540 1533
1541 1534
1542 TEST(Run_Wasm_LoadMemI32_oob_asm) { 1535 TEST(Run_Wasm_LoadMemI32_oob_asm) {
1543 WasmRunner<int32_t> r(MachineType::Uint32());
1544 TestingModule module; 1536 TestingModule module;
1545 module.asm_js = true; 1537 module.asm_js = true;
1546 int32_t* memory = module.AddMemoryElems<int32_t>(8); 1538 int32_t* memory = module.AddMemoryElems<int32_t>(8);
1539 WasmRunner<int32_t> r(&module, MachineType::Uint32());
1547 module.RandomizeMemory(1112); 1540 module.RandomizeMemory(1112);
1548 r.env()->module = &module;
1549 1541
1550 BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0))); 1542 BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0)));
1551 1543
1552 memory[0] = 999999; 1544 memory[0] = 999999;
1553 CHECK_EQ(999999, r.Call(0u)); 1545 CHECK_EQ(999999, r.Call(0u));
1554 // TODO(titzer): offset 29-31 should also be OOB. 1546 // TODO(titzer): offset 29-31 should also be OOB.
1555 for (uint32_t offset = 32; offset < 40; offset++) { 1547 for (uint32_t offset = 32; offset < 40; offset++) {
1556 CHECK_EQ(0, r.Call(offset)); 1548 CHECK_EQ(0, r.Call(offset));
1557 } 1549 }
1558 1550
1559 for (uint32_t offset = 0x80000000; offset < 0x80000010; offset++) { 1551 for (uint32_t offset = 0x80000000; offset < 0x80000010; offset++) {
1560 CHECK_EQ(0, r.Call(offset)); 1552 CHECK_EQ(0, r.Call(offset));
1561 } 1553 }
1562 } 1554 }
1563 1555
1564 1556
1565 TEST(Run_Wasm_LoadMem_offset_oob) { 1557 TEST(Run_Wasm_LoadMem_offset_oob) {
1566 TestingModule module; 1558 TestingModule module;
1567 module.AddMemoryElems<int32_t>(8); 1559 module.AddMemoryElems<int32_t>(8);
1568 1560
1569 static const MachineType machineTypes[] = { 1561 static const MachineType machineTypes[] = {
1570 MachineType::Int8(), MachineType::Uint8(), MachineType::Int16(), 1562 MachineType::Int8(), MachineType::Uint8(), MachineType::Int16(),
1571 MachineType::Uint16(), MachineType::Int32(), MachineType::Uint32(), 1563 MachineType::Uint16(), MachineType::Int32(), MachineType::Uint32(),
1572 MachineType::Int64(), MachineType::Uint64(), MachineType::Float32(), 1564 MachineType::Int64(), MachineType::Uint64(), MachineType::Float32(),
1573 MachineType::Float64()}; 1565 MachineType::Float64()};
1574 1566
1575 for (size_t m = 0; m < arraysize(machineTypes); m++) { 1567 for (size_t m = 0; m < arraysize(machineTypes); m++) {
1576 module.RandomizeMemory(1116 + static_cast<int>(m)); 1568 module.RandomizeMemory(1116 + static_cast<int>(m));
1577 WasmRunner<int32_t> r(MachineType::Uint32()); 1569 WasmRunner<int32_t> r(&module, MachineType::Uint32());
1578 r.env()->module = &module;
1579 uint32_t boundary = 24 - WasmOpcodes::MemSize(machineTypes[m]); 1570 uint32_t boundary = 24 - WasmOpcodes::MemSize(machineTypes[m]);
1580 1571
1581 BUILD(r, WASM_LOAD_MEM_OFFSET(machineTypes[m], 8, WASM_GET_LOCAL(0)), 1572 BUILD(r, WASM_LOAD_MEM_OFFSET(machineTypes[m], 8, WASM_GET_LOCAL(0)),
1582 WASM_ZERO); 1573 WASM_ZERO);
1583 1574
1584 CHECK_EQ(0, r.Call(boundary)); // in bounds. 1575 CHECK_EQ(0, r.Call(boundary)); // in bounds.
1585 1576
1586 for (uint32_t offset = boundary + 1; offset < boundary + 19; offset++) { 1577 for (uint32_t offset = boundary + 1; offset < boundary + 19; offset++) {
1587 CHECK_TRAP(r.Call(offset)); // out of bounds. 1578 CHECK_TRAP(r.Call(offset)); // out of bounds.
1588 } 1579 }
1589 } 1580 }
1590 } 1581 }
1591 1582
1592 1583
1593 TEST(Run_Wasm_LoadMemI32_offset) { 1584 TEST(Run_Wasm_LoadMemI32_offset) {
1594 WasmRunner<int32_t> r(MachineType::Int32());
1595 TestingModule module; 1585 TestingModule module;
1596 int32_t* memory = module.AddMemoryElems<int32_t>(4); 1586 int32_t* memory = module.AddMemoryElems<int32_t>(4);
1587 WasmRunner<int32_t> r(&module, MachineType::Int32());
1597 module.RandomizeMemory(1111); 1588 module.RandomizeMemory(1111);
1598 r.env()->module = &module;
1599 1589
1600 BUILD(r, WASM_LOAD_MEM_OFFSET(MachineType::Int32(), 4, WASM_GET_LOCAL(0))); 1590 BUILD(r, WASM_LOAD_MEM_OFFSET(MachineType::Int32(), 4, WASM_GET_LOCAL(0)));
1601 1591
1602 memory[0] = 66666666; 1592 memory[0] = 66666666;
1603 memory[1] = 77777777; 1593 memory[1] = 77777777;
1604 memory[2] = 88888888; 1594 memory[2] = 88888888;
1605 memory[3] = 99999999; 1595 memory[3] = 99999999;
1606 CHECK_EQ(77777777, r.Call(0)); 1596 CHECK_EQ(77777777, r.Call(0));
1607 CHECK_EQ(88888888, r.Call(4)); 1597 CHECK_EQ(88888888, r.Call(4));
1608 CHECK_EQ(99999999, r.Call(8)); 1598 CHECK_EQ(99999999, r.Call(8));
1609 1599
1610 memory[0] = 11111111; 1600 memory[0] = 11111111;
1611 memory[1] = 22222222; 1601 memory[1] = 22222222;
1612 memory[2] = 33333333; 1602 memory[2] = 33333333;
1613 memory[3] = 44444444; 1603 memory[3] = 44444444;
1614 CHECK_EQ(22222222, r.Call(0)); 1604 CHECK_EQ(22222222, r.Call(0));
1615 CHECK_EQ(33333333, r.Call(4)); 1605 CHECK_EQ(33333333, r.Call(4));
1616 CHECK_EQ(44444444, r.Call(8)); 1606 CHECK_EQ(44444444, r.Call(8));
1617 } 1607 }
1618 1608
1619 1609
1620 #if !V8_TARGET_ARCH_MIPS && !V8_TARGET_ARCH_MIPS64 1610 #if !V8_TARGET_ARCH_MIPS && !V8_TARGET_ARCH_MIPS64
1621 1611
1622 TEST(Run_Wasm_LoadMemI32_const_oob_misaligned) { 1612 TEST(Run_Wasm_LoadMemI32_const_oob_misaligned) {
1613 const int kMemSize = 12;
1623 // TODO(titzer): Fix misaligned accesses on MIPS and re-enable. 1614 // TODO(titzer): Fix misaligned accesses on MIPS and re-enable.
1624 TestingModule module;
1625 const int kMemSize = 12;
1626 module.AddMemoryElems<byte>(kMemSize);
1627
1628 for (int offset = 0; offset < kMemSize + 5; offset++) { 1615 for (int offset = 0; offset < kMemSize + 5; offset++) {
1629 for (int index = 0; index < kMemSize + 5; index++) { 1616 for (int index = 0; index < kMemSize + 5; index++) {
1630 WasmRunner<int32_t> r; 1617 TestingModule module;
1631 r.env()->module = &module; 1618 module.AddMemoryElems<byte>(kMemSize);
1619
1620 WasmRunner<int32_t> r(&module);
1632 module.RandomizeMemory(); 1621 module.RandomizeMemory();
1633 1622
1634 BUILD(r, 1623 BUILD(r,
1635 WASM_LOAD_MEM_OFFSET(MachineType::Int32(), offset, WASM_I8(index))); 1624 WASM_LOAD_MEM_OFFSET(MachineType::Int32(), offset, WASM_I8(index)));
1636 1625
1637 if ((offset + index) <= (kMemSize - sizeof(int32_t))) { 1626 if ((offset + index) <= (kMemSize - sizeof(int32_t))) {
1638 CHECK_EQ(module.raw_val_at<int32_t>(offset + index), r.Call()); 1627 CHECK_EQ(module.raw_val_at<int32_t>(offset + index), r.Call());
1639 } else { 1628 } else {
1640 CHECK_TRAP(r.Call()); 1629 CHECK_TRAP(r.Call());
1641 } 1630 }
1642 } 1631 }
1643 } 1632 }
1644 } 1633 }
1645 1634
1646 #endif 1635 #endif
1647 1636
1648 1637
1649 TEST(Run_Wasm_LoadMemI32_const_oob) { 1638 TEST(Run_Wasm_LoadMemI32_const_oob) {
1650 TestingModule module;
1651 const int kMemSize = 24; 1639 const int kMemSize = 24;
1652 module.AddMemoryElems<byte>(kMemSize);
1653
1654 for (int offset = 0; offset < kMemSize + 5; offset += 4) { 1640 for (int offset = 0; offset < kMemSize + 5; offset += 4) {
1655 for (int index = 0; index < kMemSize + 5; index += 4) { 1641 for (int index = 0; index < kMemSize + 5; index += 4) {
1656 WasmRunner<int32_t> r; 1642 TestingModule module;
1657 r.env()->module = &module; 1643 module.AddMemoryElems<byte>(kMemSize);
1644
1645 WasmRunner<int32_t> r(&module);
1658 module.RandomizeMemory(); 1646 module.RandomizeMemory();
1659 1647
1660 BUILD(r, 1648 BUILD(r,
1661 WASM_LOAD_MEM_OFFSET(MachineType::Int32(), offset, WASM_I8(index))); 1649 WASM_LOAD_MEM_OFFSET(MachineType::Int32(), offset, WASM_I8(index)));
1662 1650
1663 if ((offset + index) <= (kMemSize - sizeof(int32_t))) { 1651 if ((offset + index) <= (kMemSize - sizeof(int32_t))) {
1664 CHECK_EQ(module.raw_val_at<int32_t>(offset + index), r.Call()); 1652 CHECK_EQ(module.raw_val_at<int32_t>(offset + index), r.Call());
1665 } else { 1653 } else {
1666 CHECK_TRAP(r.Call()); 1654 CHECK_TRAP(r.Call());
1667 } 1655 }
1668 } 1656 }
1669 } 1657 }
1670 } 1658 }
1671 1659
1672 1660
1673 TEST(Run_Wasm_StoreMemI32_offset) { 1661 TEST(Run_Wasm_StoreMemI32_offset) {
1674 WasmRunner<int32_t> r(MachineType::Int32());
1675 const int32_t kWritten = 0xaabbccdd;
1676 TestingModule module; 1662 TestingModule module;
1677 int32_t* memory = module.AddMemoryElems<int32_t>(4); 1663 int32_t* memory = module.AddMemoryElems<int32_t>(4);
1678 r.env()->module = &module; 1664 WasmRunner<int32_t> r(&module, MachineType::Int32());
1665 const int32_t kWritten = 0xaabbccdd;
1679 1666
1680 BUILD(r, WASM_STORE_MEM_OFFSET(MachineType::Int32(), 4, WASM_GET_LOCAL(0), 1667 BUILD(r, WASM_STORE_MEM_OFFSET(MachineType::Int32(), 4, WASM_GET_LOCAL(0),
1681 WASM_I32(kWritten))); 1668 WASM_I32(kWritten)));
1682 1669
1683 for (int i = 0; i < 2; i++) { 1670 for (int i = 0; i < 2; i++) {
1684 module.RandomizeMemory(1111); 1671 module.RandomizeMemory(1111);
1685 memory[0] = 66666666; 1672 memory[0] = 66666666;
1686 memory[1] = 77777777; 1673 memory[1] = 77777777;
1687 memory[2] = 88888888; 1674 memory[2] = 88888888;
1688 memory[3] = 99999999; 1675 memory[3] = 99999999;
(...skipping 18 matching lines...) Expand all
1707 MachineType::Float64()}; 1694 MachineType::Float64()};
1708 #else 1695 #else
1709 static const MachineType machineTypes[] = { 1696 static const MachineType machineTypes[] = {
1710 MachineType::Int8(), MachineType::Uint8(), MachineType::Int16(), 1697 MachineType::Int8(), MachineType::Uint8(), MachineType::Int16(),
1711 MachineType::Uint16(), MachineType::Int32(), MachineType::Uint32(), 1698 MachineType::Uint16(), MachineType::Int32(), MachineType::Uint32(),
1712 MachineType::Float32(), MachineType::Float64()}; 1699 MachineType::Float32(), MachineType::Float64()};
1713 #endif 1700 #endif
1714 1701
1715 for (size_t m = 0; m < arraysize(machineTypes); m++) { 1702 for (size_t m = 0; m < arraysize(machineTypes); m++) {
1716 module.RandomizeMemory(1119 + static_cast<int>(m)); 1703 module.RandomizeMemory(1119 + static_cast<int>(m));
1717 WasmRunner<int32_t> r(MachineType::Uint32()); 1704 WasmRunner<int32_t> r(&module, MachineType::Uint32());
1718 r.env()->module = &module;
1719 1705
1720 BUILD(r, WASM_STORE_MEM_OFFSET(machineTypes[m], 8, WASM_GET_LOCAL(0), 1706 BUILD(r, WASM_STORE_MEM_OFFSET(machineTypes[m], 8, WASM_GET_LOCAL(0),
1721 WASM_LOAD_MEM(machineTypes[m], WASM_ZERO)), 1707 WASM_LOAD_MEM(machineTypes[m], WASM_ZERO)),
1722 WASM_ZERO); 1708 WASM_ZERO);
1723 1709
1724 byte memsize = WasmOpcodes::MemSize(machineTypes[m]); 1710 byte memsize = WasmOpcodes::MemSize(machineTypes[m]);
1725 uint32_t boundary = 24 - memsize; 1711 uint32_t boundary = 24 - memsize;
1726 CHECK_EQ(0, r.Call(boundary)); // in bounds. 1712 CHECK_EQ(0, r.Call(boundary)); // in bounds.
1727 CHECK_EQ(0, memcmp(&memory[0], &memory[8 + boundary], memsize)); 1713 CHECK_EQ(0, memcmp(&memory[0], &memory[8 + boundary], memsize));
1728 1714
1729 for (uint32_t offset = boundary + 1; offset < boundary + 19; offset++) { 1715 for (uint32_t offset = boundary + 1; offset < boundary + 19; offset++) {
1730 CHECK_TRAP(r.Call(offset)); // out of bounds. 1716 CHECK_TRAP(r.Call(offset)); // out of bounds.
1731 } 1717 }
1732 } 1718 }
1733 } 1719 }
1734 1720
1735 1721
1736 #if WASM_64 1722 #if WASM_64
1737 TEST(Run_Wasm_F64ReinterpretI64) { 1723 TEST(Run_Wasm_F64ReinterpretI64) {
1738 WasmRunner<int64_t> r;
1739 TestingModule module; 1724 TestingModule module;
1740 int64_t* memory = module.AddMemoryElems<int64_t>(8); 1725 int64_t* memory = module.AddMemoryElems<int64_t>(8);
1741 r.env()->module = &module; 1726 WasmRunner<int64_t> r(&module);
1742 1727
1743 BUILD(r, WASM_I64_REINTERPRET_F64( 1728 BUILD(r, WASM_I64_REINTERPRET_F64(
1744 WASM_LOAD_MEM(MachineType::Float64(), WASM_ZERO))); 1729 WASM_LOAD_MEM(MachineType::Float64(), WASM_ZERO)));
1745 1730
1746 FOR_INT32_INPUTS(i) { 1731 FOR_INT32_INPUTS(i) {
1747 int64_t expected = static_cast<int64_t>(*i) * 0x300010001; 1732 int64_t expected = static_cast<int64_t>(*i) * 0x300010001;
1748 memory[0] = expected; 1733 memory[0] = expected;
1749 CHECK_EQ(expected, r.Call()); 1734 CHECK_EQ(expected, r.Call());
1750 } 1735 }
1751 } 1736 }
1752 1737
1753 1738
1754 TEST(Run_Wasm_I64ReinterpretF64) { 1739 TEST(Run_Wasm_I64ReinterpretF64) {
1755 WasmRunner<int64_t> r(MachineType::Int64());
1756 TestingModule module; 1740 TestingModule module;
1757 int64_t* memory = module.AddMemoryElems<int64_t>(8); 1741 int64_t* memory = module.AddMemoryElems<int64_t>(8);
1758 r.env()->module = &module; 1742 WasmRunner<int64_t> r(&module, MachineType::Int64());
1759 1743
1760 BUILD(r, WASM_BLOCK( 1744 BUILD(r, WASM_BLOCK(
1761 2, WASM_STORE_MEM(MachineType::Float64(), WASM_ZERO, 1745 2, WASM_STORE_MEM(MachineType::Float64(), WASM_ZERO,
1762 WASM_F64_REINTERPRET_I64(WASM_GET_LOCAL(0))), 1746 WASM_F64_REINTERPRET_I64(WASM_GET_LOCAL(0))),
1763 WASM_GET_LOCAL(0))); 1747 WASM_GET_LOCAL(0)));
1764 1748
1765 FOR_INT32_INPUTS(i) { 1749 FOR_INT32_INPUTS(i) {
1766 int64_t expected = static_cast<int64_t>(*i) * 0x300010001; 1750 int64_t expected = static_cast<int64_t>(*i) * 0x300010001;
1767 CHECK_EQ(expected, r.Call(expected)); 1751 CHECK_EQ(expected, r.Call(expected));
1768 CHECK_EQ(expected, memory[0]); 1752 CHECK_EQ(expected, memory[0]);
1769 } 1753 }
1770 } 1754 }
1771 1755
1772 1756
1773 TEST(Run_Wasm_LoadMemI64) { 1757 TEST(Run_Wasm_LoadMemI64) {
1774 WasmRunner<int64_t> r;
1775 TestingModule module; 1758 TestingModule module;
1776 int64_t* memory = module.AddMemoryElems<int64_t>(8); 1759 int64_t* memory = module.AddMemoryElems<int64_t>(8);
1777 module.RandomizeMemory(1111); 1760 module.RandomizeMemory(1111);
1778 r.env()->module = &module; 1761 WasmRunner<int64_t> r(&module);
1779 1762
1780 BUILD(r, WASM_LOAD_MEM(MachineType::Int64(), WASM_I8(0))); 1763 BUILD(r, WASM_LOAD_MEM(MachineType::Int64(), WASM_I8(0)));
1781 1764
1782 memory[0] = 0xaabbccdd00112233LL; 1765 memory[0] = 0xaabbccdd00112233LL;
1783 CHECK_EQ(0xaabbccdd00112233LL, r.Call()); 1766 CHECK_EQ(0xaabbccdd00112233LL, r.Call());
1784 1767
1785 memory[0] = 0x33aabbccdd001122LL; 1768 memory[0] = 0x33aabbccdd001122LL;
1786 CHECK_EQ(0x33aabbccdd001122LL, r.Call()); 1769 CHECK_EQ(0x33aabbccdd001122LL, r.Call());
1787 1770
1788 memory[0] = 77777777; 1771 memory[0] = 77777777;
1789 CHECK_EQ(77777777, r.Call()); 1772 CHECK_EQ(77777777, r.Call());
1790 } 1773 }
1791 #endif 1774 #endif
1792 1775
1793 1776
1794 TEST(Run_Wasm_LoadMemI32_P) { 1777 TEST(Run_Wasm_LoadMemI32_P) {
1795 const int kNumElems = 8; 1778 const int kNumElems = 8;
1796 WasmRunner<int32_t> r(MachineType::Int32());
1797 TestingModule module; 1779 TestingModule module;
1798 int32_t* memory = module.AddMemoryElems<int32_t>(kNumElems); 1780 int32_t* memory = module.AddMemoryElems<int32_t>(kNumElems);
1781 WasmRunner<int32_t> r(&module, MachineType::Int32());
1799 module.RandomizeMemory(2222); 1782 module.RandomizeMemory(2222);
1800 r.env()->module = &module;
1801 1783
1802 BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0))); 1784 BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0)));
1803 1785
1804 for (int i = 0; i < kNumElems; i++) { 1786 for (int i = 0; i < kNumElems; i++) {
1805 CHECK_EQ(memory[i], r.Call(i * 4)); 1787 CHECK_EQ(memory[i], r.Call(i * 4));
1806 } 1788 }
1807 } 1789 }
1808 1790
1809 1791
1810 TEST(Run_Wasm_MemI32_Sum) { 1792 TEST(Run_Wasm_MemI32_Sum) {
1811 WasmRunner<uint32_t> r(MachineType::Int32());
1812 const int kNumElems = 20; 1793 const int kNumElems = 20;
1813 const byte kSum = r.AllocateLocal(kAstI32);
1814 TestingModule module; 1794 TestingModule module;
1815 uint32_t* memory = module.AddMemoryElems<uint32_t>(kNumElems); 1795 uint32_t* memory = module.AddMemoryElems<uint32_t>(kNumElems);
1816 r.env()->module = &module; 1796 WasmRunner<uint32_t> r(&module, MachineType::Int32());
1797 const byte kSum = r.AllocateLocal(kAstI32);
1817 1798
1818 BUILD(r, WASM_BLOCK( 1799 BUILD(r, WASM_BLOCK(
1819 2, WASM_WHILE( 1800 2, WASM_WHILE(
1820 WASM_GET_LOCAL(0), 1801 WASM_GET_LOCAL(0),
1821 WASM_BLOCK( 1802 WASM_BLOCK(
1822 2, WASM_SET_LOCAL( 1803 2, WASM_SET_LOCAL(
1823 kSum, WASM_I32_ADD( 1804 kSum, WASM_I32_ADD(
1824 WASM_GET_LOCAL(kSum), 1805 WASM_GET_LOCAL(kSum),
1825 WASM_LOAD_MEM(MachineType::Int32(), 1806 WASM_LOAD_MEM(MachineType::Int32(),
1826 WASM_GET_LOCAL(0)))), 1807 WASM_GET_LOCAL(0)))),
1827 WASM_SET_LOCAL( 1808 WASM_SET_LOCAL(
1828 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(4))))), 1809 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(4))))),
1829 WASM_GET_LOCAL(1))); 1810 WASM_GET_LOCAL(1)));
1830 1811
1831 // Run 4 trials. 1812 // Run 4 trials.
1832 for (int i = 0; i < 3; i++) { 1813 for (int i = 0; i < 3; i++) {
1833 module.RandomizeMemory(i * 33); 1814 module.RandomizeMemory(i * 33);
1834 uint32_t expected = 0; 1815 uint32_t expected = 0;
1835 for (size_t j = kNumElems - 1; j > 0; j--) { 1816 for (size_t j = kNumElems - 1; j > 0; j--) {
1836 expected += memory[j]; 1817 expected += memory[j];
1837 } 1818 }
1838 uint32_t result = r.Call(static_cast<int>(4 * (kNumElems - 1))); 1819 uint32_t result = r.Call(static_cast<int>(4 * (kNumElems - 1)));
1839 CHECK_EQ(expected, result); 1820 CHECK_EQ(expected, result);
1840 } 1821 }
1841 } 1822 }
1842 1823
1843 1824
1844 TEST(Run_Wasm_CheckMachIntsZero) { 1825 TEST(Run_Wasm_CheckMachIntsZero) {
1845 WasmRunner<uint32_t> r(MachineType::Int32());
1846 const int kNumElems = 55; 1826 const int kNumElems = 55;
1847 TestingModule module; 1827 TestingModule module;
1848 module.AddMemoryElems<uint32_t>(kNumElems); 1828 module.AddMemoryElems<uint32_t>(kNumElems);
1849 r.env()->module = &module; 1829 WasmRunner<uint32_t> r(&module, MachineType::Int32());
1850 1830
1851 BUILD(r, kExprBlock, 2, kExprLoop, 1, kExprIf, kExprGetLocal, 0, kExprBr, 0, 1831 BUILD(r, kExprBlock, 2, kExprLoop, 1, kExprIf, kExprGetLocal, 0, kExprBr, 0,
1852 kExprIfElse, kExprI32LoadMem, 0, kExprGetLocal, 0, kExprBr, 2, 1832 kExprIfElse, kExprI32LoadMem, 0, kExprGetLocal, 0, kExprBr, 2,
1853 kExprI8Const, 255, kExprSetLocal, 0, kExprI32Sub, kExprGetLocal, 0, 1833 kExprI8Const, 255, kExprSetLocal, 0, kExprI32Sub, kExprGetLocal, 0,
1854 kExprI8Const, 4, kExprI8Const, 0); 1834 kExprI8Const, 4, kExprI8Const, 0);
1855 1835
1856 module.BlankMemory(); 1836 module.BlankMemory();
1857 CHECK_EQ(0, r.Call((kNumElems - 1) * 4)); 1837 CHECK_EQ(0, r.Call((kNumElems - 1) * 4));
1858 } 1838 }
1859 1839
1860 1840
1861 TEST(Run_Wasm_MemF32_Sum) { 1841 TEST(Run_Wasm_MemF32_Sum) {
1862 WasmRunner<int32_t> r(MachineType::Int32());
1863 const byte kSum = r.AllocateLocal(kAstF32);
1864 const int kSize = 5; 1842 const int kSize = 5;
1865 TestingModule module; 1843 TestingModule module;
1866 module.AddMemoryElems<float>(kSize); 1844 module.AddMemoryElems<float>(kSize);
1867 float* buffer = module.raw_mem_start<float>(); 1845 float* buffer = module.raw_mem_start<float>();
1868 buffer[0] = -99.25; 1846 buffer[0] = -99.25;
1869 buffer[1] = -888.25; 1847 buffer[1] = -888.25;
1870 buffer[2] = -77.25; 1848 buffer[2] = -77.25;
1871 buffer[3] = 66666.25; 1849 buffer[3] = 66666.25;
1872 buffer[4] = 5555.25; 1850 buffer[4] = 5555.25;
1873 r.env()->module = &module; 1851 WasmRunner<int32_t> r(&module, MachineType::Int32());
1852 const byte kSum = r.AllocateLocal(kAstF32);
1874 1853
1875 BUILD(r, WASM_BLOCK( 1854 BUILD(r, WASM_BLOCK(
1876 3, WASM_WHILE( 1855 3, WASM_WHILE(
1877 WASM_GET_LOCAL(0), 1856 WASM_GET_LOCAL(0),
1878 WASM_BLOCK( 1857 WASM_BLOCK(
1879 2, WASM_SET_LOCAL( 1858 2, WASM_SET_LOCAL(
1880 kSum, WASM_F32_ADD( 1859 kSum, WASM_F32_ADD(
1881 WASM_GET_LOCAL(kSum), 1860 WASM_GET_LOCAL(kSum),
1882 WASM_LOAD_MEM(MachineType::Float32(), 1861 WASM_LOAD_MEM(MachineType::Float32(),
1883 WASM_GET_LOCAL(0)))), 1862 WASM_GET_LOCAL(0)))),
1884 WASM_SET_LOCAL( 1863 WASM_SET_LOCAL(
1885 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(4))))), 1864 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(4))))),
1886 WASM_STORE_MEM(MachineType::Float32(), WASM_ZERO, 1865 WASM_STORE_MEM(MachineType::Float32(), WASM_ZERO,
1887 WASM_GET_LOCAL(kSum)), 1866 WASM_GET_LOCAL(kSum)),
1888 WASM_GET_LOCAL(0))); 1867 WASM_GET_LOCAL(0)));
1889 1868
1890 CHECK_EQ(0, r.Call(4 * (kSize - 1))); 1869 CHECK_EQ(0, r.Call(4 * (kSize - 1)));
1891 CHECK_NE(-99.25, buffer[0]); 1870 CHECK_NE(-99.25, buffer[0]);
1892 CHECK_EQ(71256.0f, buffer[0]); 1871 CHECK_EQ(71256.0f, buffer[0]);
1893 } 1872 }
1894 1873
1895 1874
1896 #if WASM_64 1875 #if WASM_64
1897 TEST(Run_Wasm_MemI64_Sum) { 1876 TEST(Run_Wasm_MemI64_Sum) {
1898 WasmRunner<uint64_t> r(MachineType::Int32());
1899 const int kNumElems = 20; 1877 const int kNumElems = 20;
1900 const byte kSum = r.AllocateLocal(kAstI64);
1901 TestingModule module; 1878 TestingModule module;
1902 uint64_t* memory = module.AddMemoryElems<uint64_t>(kNumElems); 1879 uint64_t* memory = module.AddMemoryElems<uint64_t>(kNumElems);
1903 r.env()->module = &module; 1880 WasmRunner<uint64_t> r(&module, MachineType::Int32());
1881 const byte kSum = r.AllocateLocal(kAstI64);
1904 1882
1905 BUILD(r, WASM_BLOCK( 1883 BUILD(r, WASM_BLOCK(
1906 2, WASM_WHILE( 1884 2, WASM_WHILE(
1907 WASM_GET_LOCAL(0), 1885 WASM_GET_LOCAL(0),
1908 WASM_BLOCK( 1886 WASM_BLOCK(
1909 2, WASM_SET_LOCAL( 1887 2, WASM_SET_LOCAL(
1910 kSum, WASM_I64_ADD( 1888 kSum, WASM_I64_ADD(
1911 WASM_GET_LOCAL(kSum), 1889 WASM_GET_LOCAL(kSum),
1912 WASM_LOAD_MEM(MachineType::Int64(), 1890 WASM_LOAD_MEM(MachineType::Int64(),
1913 WASM_GET_LOCAL(0)))), 1891 WASM_GET_LOCAL(0)))),
(...skipping 11 matching lines...) Expand all
1925 uint64_t result = r.Call(8 * (kNumElems - 1)); 1903 uint64_t result = r.Call(8 * (kNumElems - 1));
1926 CHECK_EQ(expected, result); 1904 CHECK_EQ(expected, result);
1927 } 1905 }
1928 } 1906 }
1929 #endif 1907 #endif
1930 1908
1931 1909
1932 template <typename T> 1910 template <typename T>
1933 T GenerateAndRunFold(WasmOpcode binop, T* buffer, size_t size, 1911 T GenerateAndRunFold(WasmOpcode binop, T* buffer, size_t size,
1934 LocalType astType, MachineType memType) { 1912 LocalType astType, MachineType memType) {
1935 WasmRunner<int32_t> r(MachineType::Int32());
1936 const byte kAccum = r.AllocateLocal(astType);
1937 TestingModule module; 1913 TestingModule module;
1938 module.AddMemoryElems<T>(size); 1914 module.AddMemoryElems<T>(size);
1939 for (size_t i = 0; i < size; i++) { 1915 for (size_t i = 0; i < size; i++) {
1940 module.raw_mem_start<T>()[i] = buffer[i]; 1916 module.raw_mem_start<T>()[i] = buffer[i];
1941 } 1917 }
1942 r.env()->module = &module; 1918 WasmRunner<int32_t> r(&module, MachineType::Int32());
1919 const byte kAccum = r.AllocateLocal(astType);
1943 1920
1944 BUILD( 1921 BUILD(
1945 r, 1922 r,
1946 WASM_BLOCK( 1923 WASM_BLOCK(
1947 4, WASM_SET_LOCAL(kAccum, WASM_LOAD_MEM(memType, WASM_ZERO)), 1924 4, WASM_SET_LOCAL(kAccum, WASM_LOAD_MEM(memType, WASM_ZERO)),
1948 WASM_WHILE( 1925 WASM_WHILE(
1949 WASM_GET_LOCAL(0), 1926 WASM_GET_LOCAL(0),
1950 WASM_BLOCK( 1927 WASM_BLOCK(
1951 2, WASM_SET_LOCAL( 1928 2, WASM_SET_LOCAL(
1952 kAccum, 1929 kAccum,
(...skipping 18 matching lines...) Expand all
1971 1948
1972 1949
1973 TEST(Build_Wasm_Infinite_Loop) { 1950 TEST(Build_Wasm_Infinite_Loop) {
1974 WasmRunner<int32_t> r(MachineType::Int32()); 1951 WasmRunner<int32_t> r(MachineType::Int32());
1975 // Only build the graph and compile, don't run. 1952 // Only build the graph and compile, don't run.
1976 BUILD(r, WASM_INFINITE_LOOP); 1953 BUILD(r, WASM_INFINITE_LOOP);
1977 } 1954 }
1978 1955
1979 1956
1980 TEST(Build_Wasm_Infinite_Loop_effect) { 1957 TEST(Build_Wasm_Infinite_Loop_effect) {
1981 WasmRunner<int32_t> r(MachineType::Int32());
1982 TestingModule module; 1958 TestingModule module;
1983 module.AddMemoryElems<int8_t>(16); 1959 module.AddMemoryElems<int8_t>(16);
1984 r.env()->module = &module; 1960 WasmRunner<int32_t> r(&module, MachineType::Int32());
1985 1961
1986 // Only build the graph and compile, don't run. 1962 // Only build the graph and compile, don't run.
1987 BUILD(r, WASM_LOOP(1, WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO))); 1963 BUILD(r, WASM_LOOP(1, WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)));
1988 } 1964 }
1989 1965
1990 1966
1991 TEST(Run_Wasm_Unreachable0a) { 1967 TEST(Run_Wasm_Unreachable0a) {
1992 WasmRunner<int32_t> r(MachineType::Int32()); 1968 WasmRunner<int32_t> r(MachineType::Int32());
1993 BUILD(r, 1969 BUILD(r,
1994 WASM_BLOCK(2, WASM_BRV(0, WASM_I8(9)), WASM_RETURN(WASM_GET_LOCAL(0)))); 1970 WASM_BLOCK(2, WASM_BRV(0, WASM_I8(9)), WASM_RETURN(WASM_GET_LOCAL(0))));
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
2111 #undef GRAPH_BUILD_TEST 2087 #undef GRAPH_BUILD_TEST
2112 } 2088 }
2113 2089
2114 2090
2115 TEST(Run_Wasm_Int32LoadInt8_signext) { 2091 TEST(Run_Wasm_Int32LoadInt8_signext) {
2116 TestingModule module; 2092 TestingModule module;
2117 const int kNumElems = 16; 2093 const int kNumElems = 16;
2118 int8_t* memory = module.AddMemoryElems<int8_t>(kNumElems); 2094 int8_t* memory = module.AddMemoryElems<int8_t>(kNumElems);
2119 module.RandomizeMemory(); 2095 module.RandomizeMemory();
2120 memory[0] = -1; 2096 memory[0] = -1;
2121 WasmRunner<int32_t> r(MachineType::Int32()); 2097 WasmRunner<int32_t> r(&module, MachineType::Int32());
2122 r.env()->module = &module;
2123 BUILD(r, WASM_LOAD_MEM(MachineType::Int8(), WASM_GET_LOCAL(0))); 2098 BUILD(r, WASM_LOAD_MEM(MachineType::Int8(), WASM_GET_LOCAL(0)));
2124 2099
2125 for (size_t i = 0; i < kNumElems; i++) { 2100 for (size_t i = 0; i < kNumElems; i++) {
2126 CHECK_EQ(memory[i], r.Call(static_cast<int>(i))); 2101 CHECK_EQ(memory[i], r.Call(static_cast<int>(i)));
2127 } 2102 }
2128 } 2103 }
2129 2104
2130 2105
2131 TEST(Run_Wasm_Int32LoadInt8_zeroext) { 2106 TEST(Run_Wasm_Int32LoadInt8_zeroext) {
2132 TestingModule module; 2107 TestingModule module;
2133 const int kNumElems = 16; 2108 const int kNumElems = 16;
2134 byte* memory = module.AddMemory(kNumElems); 2109 byte* memory = module.AddMemory(kNumElems);
2135 module.RandomizeMemory(77); 2110 module.RandomizeMemory(77);
2136 memory[0] = 255; 2111 memory[0] = 255;
2137 WasmRunner<int32_t> r(MachineType::Int32()); 2112 WasmRunner<int32_t> r(&module, MachineType::Int32());
2138 r.env()->module = &module;
2139 BUILD(r, WASM_LOAD_MEM(MachineType::Uint8(), WASM_GET_LOCAL(0))); 2113 BUILD(r, WASM_LOAD_MEM(MachineType::Uint8(), WASM_GET_LOCAL(0)));
2140 2114
2141 for (size_t i = 0; i < kNumElems; i++) { 2115 for (size_t i = 0; i < kNumElems; i++) {
2142 CHECK_EQ(memory[i], r.Call(static_cast<int>(i))); 2116 CHECK_EQ(memory[i], r.Call(static_cast<int>(i)));
2143 } 2117 }
2144 } 2118 }
2145 2119
2146 2120
2147 TEST(Run_Wasm_Int32LoadInt16_signext) { 2121 TEST(Run_Wasm_Int32LoadInt16_signext) {
2148 TestingModule module; 2122 TestingModule module;
2149 const int kNumBytes = 16; 2123 const int kNumBytes = 16;
2150 byte* memory = module.AddMemory(kNumBytes); 2124 byte* memory = module.AddMemory(kNumBytes);
2151 module.RandomizeMemory(888); 2125 module.RandomizeMemory(888);
2152 memory[1] = 200; 2126 memory[1] = 200;
2153 WasmRunner<int32_t> r(MachineType::Int32()); 2127 WasmRunner<int32_t> r(&module, MachineType::Int32());
2154 r.env()->module = &module;
2155 BUILD(r, WASM_LOAD_MEM(MachineType::Int16(), WASM_GET_LOCAL(0))); 2128 BUILD(r, WASM_LOAD_MEM(MachineType::Int16(), WASM_GET_LOCAL(0)));
2156 2129
2157 for (size_t i = 0; i < kNumBytes; i += 2) { 2130 for (size_t i = 0; i < kNumBytes; i += 2) {
2158 int32_t expected = memory[i] | (static_cast<int8_t>(memory[i + 1]) << 8); 2131 int32_t expected = memory[i] | (static_cast<int8_t>(memory[i + 1]) << 8);
2159 CHECK_EQ(expected, r.Call(static_cast<int>(i))); 2132 CHECK_EQ(expected, r.Call(static_cast<int>(i)));
2160 } 2133 }
2161 } 2134 }
2162 2135
2163 2136
2164 TEST(Run_Wasm_Int32LoadInt16_zeroext) { 2137 TEST(Run_Wasm_Int32LoadInt16_zeroext) {
2165 TestingModule module; 2138 TestingModule module;
2166 const int kNumBytes = 16; 2139 const int kNumBytes = 16;
2167 byte* memory = module.AddMemory(kNumBytes); 2140 byte* memory = module.AddMemory(kNumBytes);
2168 module.RandomizeMemory(9999); 2141 module.RandomizeMemory(9999);
2169 memory[1] = 204; 2142 memory[1] = 204;
2170 WasmRunner<int32_t> r(MachineType::Int32()); 2143 WasmRunner<int32_t> r(&module, MachineType::Int32());
2171 r.env()->module = &module;
2172 BUILD(r, WASM_LOAD_MEM(MachineType::Uint16(), WASM_GET_LOCAL(0))); 2144 BUILD(r, WASM_LOAD_MEM(MachineType::Uint16(), WASM_GET_LOCAL(0)));
2173 2145
2174 for (size_t i = 0; i < kNumBytes; i += 2) { 2146 for (size_t i = 0; i < kNumBytes; i += 2) {
2175 int32_t expected = memory[i] | (memory[i + 1] << 8); 2147 int32_t expected = memory[i] | (memory[i + 1] << 8);
2176 CHECK_EQ(expected, r.Call(static_cast<int>(i))); 2148 CHECK_EQ(expected, r.Call(static_cast<int>(i)));
2177 } 2149 }
2178 } 2150 }
2179 2151
2180 2152
2181 TEST(Run_WasmInt32Global) { 2153 TEST(Run_WasmInt32Global) {
2182 TestingModule module; 2154 TestingModule module;
2183 int32_t* global = module.AddGlobal<int32_t>(MachineType::Int32()); 2155 int32_t* global = module.AddGlobal<int32_t>(MachineType::Int32());
2184 WasmRunner<int32_t> r(MachineType::Int32()); 2156 WasmRunner<int32_t> r(&module, MachineType::Int32());
2185 r.env()->module = &module;
2186 // global = global + p0 2157 // global = global + p0
2187 BUILD(r, WASM_STORE_GLOBAL( 2158 BUILD(r, WASM_STORE_GLOBAL(
2188 0, WASM_I32_ADD(WASM_LOAD_GLOBAL(0), WASM_GET_LOCAL(0)))); 2159 0, WASM_I32_ADD(WASM_LOAD_GLOBAL(0), WASM_GET_LOCAL(0))));
2189 2160
2190 *global = 116; 2161 *global = 116;
2191 for (int i = 9; i < 444444; i += 111111) { 2162 for (int i = 9; i < 444444; i += 111111) {
2192 int32_t expected = *global + i; 2163 int32_t expected = *global + i;
2193 r.Call(i); 2164 r.Call(i);
2194 CHECK_EQ(expected, *global); 2165 CHECK_EQ(expected, *global);
2195 } 2166 }
2196 } 2167 }
2197 2168
2198 2169
2199 TEST(Run_WasmInt32Globals_DontAlias) { 2170 TEST(Run_WasmInt32Globals_DontAlias) {
2200 const int kNumGlobals = 3; 2171 const int kNumGlobals = 3;
2201 TestingModule module; 2172 TestingModule module;
2202 int32_t* globals[] = {module.AddGlobal<int32_t>(MachineType::Int32()), 2173 int32_t* globals[] = {module.AddGlobal<int32_t>(MachineType::Int32()),
2203 module.AddGlobal<int32_t>(MachineType::Int32()), 2174 module.AddGlobal<int32_t>(MachineType::Int32()),
2204 module.AddGlobal<int32_t>(MachineType::Int32())}; 2175 module.AddGlobal<int32_t>(MachineType::Int32())};
2205 2176
2206 for (int g = 0; g < kNumGlobals; g++) { 2177 for (int g = 0; g < kNumGlobals; g++) {
2207 // global = global + p0 2178 // global = global + p0
2208 WasmRunner<int32_t> r(MachineType::Int32()); 2179 WasmRunner<int32_t> r(&module, MachineType::Int32());
2209 r.env()->module = &module;
2210 BUILD(r, WASM_STORE_GLOBAL( 2180 BUILD(r, WASM_STORE_GLOBAL(
2211 g, WASM_I32_ADD(WASM_LOAD_GLOBAL(g), WASM_GET_LOCAL(0)))); 2181 g, WASM_I32_ADD(WASM_LOAD_GLOBAL(g), WASM_GET_LOCAL(0))));
2212 2182
2213 // Check that reading/writing global number {g} doesn't alter the others. 2183 // Check that reading/writing global number {g} doesn't alter the others.
2214 *globals[g] = 116 * g; 2184 *globals[g] = 116 * g;
2215 int32_t before[kNumGlobals]; 2185 int32_t before[kNumGlobals];
2216 for (int i = 9; i < 444444; i += 111113) { 2186 for (int i = 9; i < 444444; i += 111113) {
2217 int32_t sum = *globals[g] + i; 2187 int32_t sum = *globals[g] + i;
2218 for (int j = 0; j < kNumGlobals; j++) before[j] = *globals[j]; 2188 for (int j = 0; j < kNumGlobals; j++) before[j] = *globals[j];
2219 r.Call(i); 2189 r.Call(i);
2220 for (int j = 0; j < kNumGlobals; j++) { 2190 for (int j = 0; j < kNumGlobals; j++) {
2221 int32_t expected = j == g ? sum : before[j]; 2191 int32_t expected = j == g ? sum : before[j];
2222 CHECK_EQ(expected, *globals[j]); 2192 CHECK_EQ(expected, *globals[j]);
2223 } 2193 }
2224 } 2194 }
2225 } 2195 }
2226 } 2196 }
2227 2197
2228 2198
2229 #if WASM_64 2199 #if WASM_64
2230 TEST(Run_WasmInt64Global) { 2200 TEST(Run_WasmInt64Global) {
2231 TestingModule module; 2201 TestingModule module;
2232 int64_t* global = module.AddGlobal<int64_t>(MachineType::Int64()); 2202 int64_t* global = module.AddGlobal<int64_t>(MachineType::Int64());
2233 WasmRunner<int32_t> r(MachineType::Int32()); 2203 WasmRunner<int32_t> r(&module, MachineType::Int32());
2234 r.env()->module = &module;
2235 // global = global + p0 2204 // global = global + p0
2236 BUILD(r, WASM_BLOCK(2, WASM_STORE_GLOBAL( 2205 BUILD(r, WASM_BLOCK(2, WASM_STORE_GLOBAL(
2237 0, WASM_I64_ADD( 2206 0, WASM_I64_ADD(
2238 WASM_LOAD_GLOBAL(0), 2207 WASM_LOAD_GLOBAL(0),
2239 WASM_I64_SCONVERT_I32(WASM_GET_LOCAL(0)))), 2208 WASM_I64_SCONVERT_I32(WASM_GET_LOCAL(0)))),
2240 WASM_ZERO)); 2209 WASM_ZERO));
2241 2210
2242 *global = 0xFFFFFFFFFFFFFFFFLL; 2211 *global = 0xFFFFFFFFFFFFFFFFLL;
2243 for (int i = 9; i < 444444; i += 111111) { 2212 for (int i = 9; i < 444444; i += 111111) {
2244 int64_t expected = *global + i; 2213 int64_t expected = *global + i;
2245 r.Call(i); 2214 r.Call(i);
2246 CHECK_EQ(expected, *global); 2215 CHECK_EQ(expected, *global);
2247 } 2216 }
2248 } 2217 }
2249 #endif 2218 #endif
2250 2219
2251 2220
2252 TEST(Run_WasmFloat32Global) { 2221 TEST(Run_WasmFloat32Global) {
2253 TestingModule module; 2222 TestingModule module;
2254 float* global = module.AddGlobal<float>(MachineType::Float32()); 2223 float* global = module.AddGlobal<float>(MachineType::Float32());
2255 WasmRunner<int32_t> r(MachineType::Int32()); 2224 WasmRunner<int32_t> r(&module, MachineType::Int32());
2256 r.env()->module = &module;
2257 // global = global + p0 2225 // global = global + p0
2258 BUILD(r, WASM_BLOCK(2, WASM_STORE_GLOBAL( 2226 BUILD(r, WASM_BLOCK(2, WASM_STORE_GLOBAL(
2259 0, WASM_F32_ADD( 2227 0, WASM_F32_ADD(
2260 WASM_LOAD_GLOBAL(0), 2228 WASM_LOAD_GLOBAL(0),
2261 WASM_F32_SCONVERT_I32(WASM_GET_LOCAL(0)))), 2229 WASM_F32_SCONVERT_I32(WASM_GET_LOCAL(0)))),
2262 WASM_ZERO)); 2230 WASM_ZERO));
2263 2231
2264 *global = 1.25; 2232 *global = 1.25;
2265 for (int i = 9; i < 4444; i += 1111) { 2233 for (int i = 9; i < 4444; i += 1111) {
2266 volatile float expected = *global + i; 2234 volatile float expected = *global + i;
2267 r.Call(i); 2235 r.Call(i);
2268 CHECK_EQ(expected, *global); 2236 CHECK_EQ(expected, *global);
2269 } 2237 }
2270 } 2238 }
2271 2239
2272 2240
2273 TEST(Run_WasmFloat64Global) { 2241 TEST(Run_WasmFloat64Global) {
2274 TestingModule module; 2242 TestingModule module;
2275 double* global = module.AddGlobal<double>(MachineType::Float64()); 2243 double* global = module.AddGlobal<double>(MachineType::Float64());
2276 WasmRunner<int32_t> r(MachineType::Int32()); 2244 WasmRunner<int32_t> r(&module, MachineType::Int32());
2277 r.env()->module = &module;
2278 // global = global + p0 2245 // global = global + p0
2279 BUILD(r, WASM_BLOCK(2, WASM_STORE_GLOBAL( 2246 BUILD(r, WASM_BLOCK(2, WASM_STORE_GLOBAL(
2280 0, WASM_F64_ADD( 2247 0, WASM_F64_ADD(
2281 WASM_LOAD_GLOBAL(0), 2248 WASM_LOAD_GLOBAL(0),
2282 WASM_F64_SCONVERT_I32(WASM_GET_LOCAL(0)))), 2249 WASM_F64_SCONVERT_I32(WASM_GET_LOCAL(0)))),
2283 WASM_ZERO)); 2250 WASM_ZERO));
2284 2251
2285 *global = 1.25; 2252 *global = 1.25;
2286 for (int i = 9; i < 4444; i += 1111) { 2253 for (int i = 9; i < 4444; i += 1111) {
2287 volatile double expected = *global + i; 2254 volatile double expected = *global + i;
(...skipping 10 matching lines...) Expand all
2298 2265
2299 int8_t* var_int8 = module.AddGlobal<int8_t>(MachineType::Int8()); 2266 int8_t* var_int8 = module.AddGlobal<int8_t>(MachineType::Int8());
2300 uint8_t* var_uint8 = module.AddGlobal<uint8_t>(MachineType::Uint8()); 2267 uint8_t* var_uint8 = module.AddGlobal<uint8_t>(MachineType::Uint8());
2301 int16_t* var_int16 = module.AddGlobal<int16_t>(MachineType::Int16()); 2268 int16_t* var_int16 = module.AddGlobal<int16_t>(MachineType::Int16());
2302 uint16_t* var_uint16 = module.AddGlobal<uint16_t>(MachineType::Uint16()); 2269 uint16_t* var_uint16 = module.AddGlobal<uint16_t>(MachineType::Uint16());
2303 int32_t* var_int32 = module.AddGlobal<int32_t>(MachineType::Int32()); 2270 int32_t* var_int32 = module.AddGlobal<int32_t>(MachineType::Int32());
2304 uint32_t* var_uint32 = module.AddGlobal<uint32_t>(MachineType::Uint32()); 2271 uint32_t* var_uint32 = module.AddGlobal<uint32_t>(MachineType::Uint32());
2305 float* var_float = module.AddGlobal<float>(MachineType::Float32()); 2272 float* var_float = module.AddGlobal<float>(MachineType::Float32());
2306 double* var_double = module.AddGlobal<double>(MachineType::Float64()); 2273 double* var_double = module.AddGlobal<double>(MachineType::Float64());
2307 2274
2308 WasmRunner<int32_t> r(MachineType::Int32()); 2275 WasmRunner<int32_t> r(&module, MachineType::Int32());
2309 r.env()->module = &module;
2310 2276
2311 BUILD( 2277 BUILD(
2312 r, 2278 r,
2313 WASM_BLOCK( 2279 WASM_BLOCK(
2314 9, 2280 9,
2315 WASM_STORE_GLOBAL(1, WASM_LOAD_MEM(MachineType::Int8(), WASM_ZERO)), 2281 WASM_STORE_GLOBAL(1, WASM_LOAD_MEM(MachineType::Int8(), WASM_ZERO)),
2316 WASM_STORE_GLOBAL(2, WASM_LOAD_MEM(MachineType::Uint8(), WASM_ZERO)), 2282 WASM_STORE_GLOBAL(2, WASM_LOAD_MEM(MachineType::Uint8(), WASM_ZERO)),
2317 WASM_STORE_GLOBAL(3, WASM_LOAD_MEM(MachineType::Int16(), WASM_ZERO)), 2283 WASM_STORE_GLOBAL(3, WASM_LOAD_MEM(MachineType::Int16(), WASM_ZERO)),
2318 WASM_STORE_GLOBAL(4, WASM_LOAD_MEM(MachineType::Uint16(), WASM_ZERO)), 2284 WASM_STORE_GLOBAL(4, WASM_LOAD_MEM(MachineType::Uint16(), WASM_ZERO)),
2319 WASM_STORE_GLOBAL(5, WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), 2285 WASM_STORE_GLOBAL(5, WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)),
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
2401 } 2367 }
2402 } 2368 }
2403 #endif 2369 #endif
2404 2370
2405 2371
2406 TEST(Run_WasmCallEmpty) { 2372 TEST(Run_WasmCallEmpty) {
2407 const int32_t kExpected = -414444; 2373 const int32_t kExpected = -414444;
2408 // Build the target function. 2374 // Build the target function.
2409 TestSignatures sigs; 2375 TestSignatures sigs;
2410 TestingModule module; 2376 TestingModule module;
2411 WasmFunctionCompiler t(sigs.i_v()); 2377 WasmFunctionCompiler t(sigs.i_v(), &module);
2412 BUILD(t, WASM_I32(kExpected)); 2378 BUILD(t, WASM_I32(kExpected));
2413 uint32_t index = t.CompileAndAdd(&module); 2379 uint32_t index = t.CompileAndAdd();
2414 2380
2415 // Build the calling function. 2381 // Build the calling function.
2416 WasmRunner<int32_t> r; 2382 WasmRunner<int32_t> r(&module);
2417 r.env()->module = &module;
2418 BUILD(r, WASM_CALL_FUNCTION0(index)); 2383 BUILD(r, WASM_CALL_FUNCTION0(index));
2419 2384
2420 int32_t result = r.Call(); 2385 int32_t result = r.Call();
2421 CHECK_EQ(kExpected, result); 2386 CHECK_EQ(kExpected, result);
2422 } 2387 }
2423 2388
2424 2389
2425 TEST(Run_WasmCallF32StackParameter) { 2390 TEST(Run_WasmCallF32StackParameter) {
2426 // Build the target function. 2391 // Build the target function.
2427 LocalType param_types[20]; 2392 LocalType param_types[20];
2428 for (int i = 0; i < 20; i++) param_types[i] = kAstF32; 2393 for (int i = 0; i < 20; i++) param_types[i] = kAstF32;
2429 FunctionSig sig(1, 19, param_types); 2394 FunctionSig sig(1, 19, param_types);
2430 TestingModule module; 2395 TestingModule module;
2431 WasmFunctionCompiler t(&sig); 2396 WasmFunctionCompiler t(&sig, &module);
2432 BUILD(t, WASM_GET_LOCAL(17)); 2397 BUILD(t, WASM_GET_LOCAL(17));
2433 uint32_t index = t.CompileAndAdd(&module); 2398 uint32_t index = t.CompileAndAdd();
2434 2399
2435 // Build the calling function. 2400 // Build the calling function.
2436 WasmRunner<float> r; 2401 WasmRunner<float> r(&module);
2437 r.env()->module = &module;
2438 BUILD(r, WASM_CALL_FUNCTION( 2402 BUILD(r, WASM_CALL_FUNCTION(
2439 index, WASM_F32(1.0f), WASM_F32(2.0f), WASM_F32(4.0f), 2403 index, WASM_F32(1.0f), WASM_F32(2.0f), WASM_F32(4.0f),
2440 WASM_F32(8.0f), WASM_F32(16.0f), WASM_F32(32.0f), 2404 WASM_F32(8.0f), WASM_F32(16.0f), WASM_F32(32.0f),
2441 WASM_F32(64.0f), WASM_F32(128.0f), WASM_F32(256.0f), 2405 WASM_F32(64.0f), WASM_F32(128.0f), WASM_F32(256.0f),
2442 WASM_F32(1.5f), WASM_F32(2.5f), WASM_F32(4.5f), WASM_F32(8.5f), 2406 WASM_F32(1.5f), WASM_F32(2.5f), WASM_F32(4.5f), WASM_F32(8.5f),
2443 WASM_F32(16.5f), WASM_F32(32.5f), WASM_F32(64.5f), 2407 WASM_F32(16.5f), WASM_F32(32.5f), WASM_F32(64.5f),
2444 WASM_F32(128.5f), WASM_F32(256.5f), WASM_F32(512.5f))); 2408 WASM_F32(128.5f), WASM_F32(256.5f), WASM_F32(512.5f)));
2445 2409
2446 float result = r.Call(); 2410 float result = r.Call();
2447 CHECK_EQ(256.5f, result); 2411 CHECK_EQ(256.5f, result);
2448 } 2412 }
2449 2413
2450 2414
2451 TEST(Run_WasmCallF64StackParameter) { 2415 TEST(Run_WasmCallF64StackParameter) {
2452 // Build the target function. 2416 // Build the target function.
2453 LocalType param_types[20]; 2417 LocalType param_types[20];
2454 for (int i = 0; i < 20; i++) param_types[i] = kAstF64; 2418 for (int i = 0; i < 20; i++) param_types[i] = kAstF64;
2455 FunctionSig sig(1, 19, param_types); 2419 FunctionSig sig(1, 19, param_types);
2456 TestingModule module; 2420 TestingModule module;
2457 WasmFunctionCompiler t(&sig); 2421 WasmFunctionCompiler t(&sig, &module);
2458 BUILD(t, WASM_GET_LOCAL(17)); 2422 BUILD(t, WASM_GET_LOCAL(17));
2459 uint32_t index = t.CompileAndAdd(&module); 2423 uint32_t index = t.CompileAndAdd();
2460 2424
2461 // Build the calling function. 2425 // Build the calling function.
2462 WasmRunner<double> r; 2426 WasmRunner<double> r(&module);
2463 r.env()->module = &module;
2464 BUILD(r, WASM_CALL_FUNCTION(index, WASM_F64(1.0), WASM_F64(2.0), 2427 BUILD(r, WASM_CALL_FUNCTION(index, WASM_F64(1.0), WASM_F64(2.0),
2465 WASM_F64(4.0), WASM_F64(8.0), WASM_F64(16.0), 2428 WASM_F64(4.0), WASM_F64(8.0), WASM_F64(16.0),
2466 WASM_F64(32.0), WASM_F64(64.0), WASM_F64(128.0), 2429 WASM_F64(32.0), WASM_F64(64.0), WASM_F64(128.0),
2467 WASM_F64(256.0), WASM_F64(1.5), WASM_F64(2.5), 2430 WASM_F64(256.0), WASM_F64(1.5), WASM_F64(2.5),
2468 WASM_F64(4.5), WASM_F64(8.5), WASM_F64(16.5), 2431 WASM_F64(4.5), WASM_F64(8.5), WASM_F64(16.5),
2469 WASM_F64(32.5), WASM_F64(64.5), WASM_F64(128.5), 2432 WASM_F64(32.5), WASM_F64(64.5), WASM_F64(128.5),
2470 WASM_F64(256.5), WASM_F64(512.5))); 2433 WASM_F64(256.5), WASM_F64(512.5)));
2471 2434
2472 float result = r.Call(); 2435 float result = r.Call();
2473 CHECK_EQ(256.5, result); 2436 CHECK_EQ(256.5, result);
2474 } 2437 }
2475 2438
2476 2439
2477 TEST(Run_WasmCallVoid) { 2440 TEST(Run_WasmCallVoid) {
2478 const byte kMemOffset = 8; 2441 const byte kMemOffset = 8;
2479 const int32_t kElemNum = kMemOffset / sizeof(int32_t); 2442 const int32_t kElemNum = kMemOffset / sizeof(int32_t);
2480 const int32_t kExpected = -414444; 2443 const int32_t kExpected = -414444;
2481 // Build the target function. 2444 // Build the target function.
2482 TestSignatures sigs; 2445 TestSignatures sigs;
2483 TestingModule module; 2446 TestingModule module;
2484 module.AddMemory(16); 2447 module.AddMemory(16);
2485 module.RandomizeMemory(); 2448 module.RandomizeMemory();
2486 WasmFunctionCompiler t(sigs.v_v()); 2449 WasmFunctionCompiler t(sigs.v_v(), &module);
2487 t.env.module = &module;
2488 BUILD(t, WASM_STORE_MEM(MachineType::Int32(), WASM_I8(kMemOffset), 2450 BUILD(t, WASM_STORE_MEM(MachineType::Int32(), WASM_I8(kMemOffset),
2489 WASM_I32(kExpected))); 2451 WASM_I32(kExpected)));
2490 uint32_t index = t.CompileAndAdd(&module); 2452 uint32_t index = t.CompileAndAdd();
2491 2453
2492 // Build the calling function. 2454 // Build the calling function.
2493 WasmRunner<int32_t> r; 2455 WasmRunner<int32_t> r(&module);
2494 r.env()->module = &module;
2495 BUILD(r, WASM_CALL_FUNCTION0(index), 2456 BUILD(r, WASM_CALL_FUNCTION0(index),
2496 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kMemOffset))); 2457 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kMemOffset)));
2497 2458
2498 int32_t result = r.Call(); 2459 int32_t result = r.Call();
2499 CHECK_EQ(kExpected, result); 2460 CHECK_EQ(kExpected, result);
2500 CHECK_EQ(kExpected, module.raw_mem_start<int32_t>()[kElemNum]); 2461 CHECK_EQ(kExpected, module.raw_mem_start<int32_t>()[kElemNum]);
2501 } 2462 }
2502 2463
2503 2464
2504 TEST(Run_WasmCall_Int32Add) { 2465 TEST(Run_WasmCall_Int32Add) {
2505 // Build the target function. 2466 // Build the target function.
2506 TestSignatures sigs; 2467 TestSignatures sigs;
2507 TestingModule module; 2468 TestingModule module;
2508 WasmFunctionCompiler t(sigs.i_ii()); 2469 WasmFunctionCompiler t(sigs.i_ii(), &module);
2509 BUILD(t, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); 2470 BUILD(t, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2510 uint32_t index = t.CompileAndAdd(&module); 2471 uint32_t index = t.CompileAndAdd();
2511 2472
2512 // Build the caller function. 2473 // Build the caller function.
2513 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); 2474 WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32());
2514 r.env()->module = &module;
2515 BUILD(r, WASM_CALL_FUNCTION(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); 2475 BUILD(r, WASM_CALL_FUNCTION(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2516 2476
2517 FOR_INT32_INPUTS(i) { 2477 FOR_INT32_INPUTS(i) {
2518 FOR_INT32_INPUTS(j) { 2478 FOR_INT32_INPUTS(j) {
2519 int32_t expected = static_cast<int32_t>(static_cast<uint32_t>(*i) + 2479 int32_t expected = static_cast<int32_t>(static_cast<uint32_t>(*i) +
2520 static_cast<uint32_t>(*j)); 2480 static_cast<uint32_t>(*j));
2521 CHECK_EQ(expected, r.Call(*i, *j)); 2481 CHECK_EQ(expected, r.Call(*i, *j));
2522 } 2482 }
2523 } 2483 }
2524 } 2484 }
2525 2485
2526 2486
2527 #if WASM_64 2487 #if WASM_64
2528 TEST(Run_WasmCall_Int64Sub) { 2488 TEST(Run_WasmCall_Int64Sub) {
2529 // Build the target function. 2489 // Build the target function.
2530 TestSignatures sigs; 2490 TestSignatures sigs;
2531 TestingModule module; 2491 TestingModule module;
2532 WasmFunctionCompiler t(sigs.l_ll()); 2492 WasmFunctionCompiler t(sigs.l_ll(), &module);
2533 BUILD(t, WASM_I64_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); 2493 BUILD(t, WASM_I64_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2534 uint32_t index = t.CompileAndAdd(&module); 2494 uint32_t index = t.CompileAndAdd();
2535 2495
2536 // Build the caller function. 2496 // Build the caller function.
2537 WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64()); 2497 WasmRunner<int64_t> r(&module, MachineType::Int64(), MachineType::Int64());
2538 r.env()->module = &module;
2539 BUILD(r, WASM_CALL_FUNCTION(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); 2498 BUILD(r, WASM_CALL_FUNCTION(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2540 2499
2541 FOR_INT32_INPUTS(i) { 2500 FOR_INT32_INPUTS(i) {
2542 FOR_INT32_INPUTS(j) { 2501 FOR_INT32_INPUTS(j) {
2543 int64_t a = static_cast<int64_t>(*i) << 32 | 2502 int64_t a = static_cast<int64_t>(*i) << 32 |
2544 (static_cast<int64_t>(*j) | 0xFFFFFFFF); 2503 (static_cast<int64_t>(*j) | 0xFFFFFFFF);
2545 int64_t b = static_cast<int64_t>(*j) << 32 | 2504 int64_t b = static_cast<int64_t>(*j) << 32 |
2546 (static_cast<int64_t>(*i) | 0xFFFFFFFF); 2505 (static_cast<int64_t>(*i) | 0xFFFFFFFF);
2547 2506
2548 int64_t expected = static_cast<int64_t>(static_cast<uint64_t>(a) - 2507 int64_t expected = static_cast<int64_t>(static_cast<uint64_t>(a) -
2549 static_cast<uint64_t>(b)); 2508 static_cast<uint64_t>(b));
2550 CHECK_EQ(expected, r.Call(a, b)); 2509 CHECK_EQ(expected, r.Call(a, b));
2551 } 2510 }
2552 } 2511 }
2553 } 2512 }
2554 #endif 2513 #endif
2555 2514
2556 2515
2557 TEST(Run_WasmCall_Float32Sub) { 2516 TEST(Run_WasmCall_Float32Sub) {
2558 TestSignatures sigs; 2517 TestSignatures sigs;
2559 WasmFunctionCompiler t(sigs.f_ff()); 2518 TestingModule module;
2519 WasmFunctionCompiler t(sigs.f_ff(), &module);
2560 2520
2561 // Build the target function. 2521 // Build the target function.
2562 TestingModule module;
2563 BUILD(t, WASM_F32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); 2522 BUILD(t, WASM_F32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2564 uint32_t index = t.CompileAndAdd(&module); 2523 uint32_t index = t.CompileAndAdd();
2565 2524
2566 // Builder the caller function. 2525 // Builder the caller function.
2567 WasmRunner<float> r(MachineType::Float32(), MachineType::Float32()); 2526 WasmRunner<float> r(&module, MachineType::Float32(), MachineType::Float32());
2568 r.env()->module = &module;
2569 BUILD(r, WASM_CALL_FUNCTION(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); 2527 BUILD(r, WASM_CALL_FUNCTION(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2570 2528
2571 FOR_FLOAT32_INPUTS(i) { 2529 FOR_FLOAT32_INPUTS(i) {
2572 FOR_FLOAT32_INPUTS(j) { 2530 FOR_FLOAT32_INPUTS(j) {
2573 volatile float expected = *i - *j; 2531 volatile float expected = *i - *j;
2574 CheckFloatEq(expected, r.Call(*i, *j)); 2532 CheckFloatEq(expected, r.Call(*i, *j));
2575 } 2533 }
2576 } 2534 }
2577 } 2535 }
2578 2536
2579 2537
2580 TEST(Run_WasmCall_Float64Sub) { 2538 TEST(Run_WasmCall_Float64Sub) {
2581 WasmRunner<int32_t> r;
2582 TestingModule module; 2539 TestingModule module;
2583 double* memory = module.AddMemoryElems<double>(16); 2540 double* memory = module.AddMemoryElems<double>(16);
2584 r.env()->module = &module; 2541 WasmRunner<int32_t> r(&module);
2585 2542
2586 // TODO(titzer): convert to a binop test. 2543 // TODO(titzer): convert to a binop test.
2587 BUILD(r, WASM_BLOCK( 2544 BUILD(r, WASM_BLOCK(
2588 2, WASM_STORE_MEM( 2545 2, WASM_STORE_MEM(
2589 MachineType::Float64(), WASM_ZERO, 2546 MachineType::Float64(), WASM_ZERO,
2590 WASM_F64_SUB( 2547 WASM_F64_SUB(
2591 WASM_LOAD_MEM(MachineType::Float64(), WASM_ZERO), 2548 WASM_LOAD_MEM(MachineType::Float64(), WASM_ZERO),
2592 WASM_LOAD_MEM(MachineType::Float64(), WASM_I8(8)))), 2549 WASM_LOAD_MEM(MachineType::Float64(), WASM_I8(8)))),
2593 WASM_I8(107))); 2550 WASM_I8(107)));
2594 2551
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
2644 2601
2645 // ========================================================================= 2602 // =========================================================================
2646 // Build the selector function. 2603 // Build the selector function.
2647 // ========================================================================= 2604 // =========================================================================
2648 uint32_t index; 2605 uint32_t index;
2649 FunctionSig::Builder b(&zone, 1, num_params); 2606 FunctionSig::Builder b(&zone, 1, num_params);
2650 b.AddReturn(WasmOpcodes::LocalTypeFor(result)); 2607 b.AddReturn(WasmOpcodes::LocalTypeFor(result));
2651 for (int i = 0; i < num_params; i++) { 2608 for (int i = 0; i < num_params; i++) {
2652 b.AddParam(WasmOpcodes::LocalTypeFor(memtypes[i])); 2609 b.AddParam(WasmOpcodes::LocalTypeFor(memtypes[i]));
2653 } 2610 }
2654 WasmFunctionCompiler t(b.Build()); 2611 WasmFunctionCompiler t(b.Build(), &module);
2655 t.env.module = &module;
2656 BUILD(t, WASM_GET_LOCAL(which)); 2612 BUILD(t, WASM_GET_LOCAL(which));
2657 index = t.CompileAndAdd(&module); 2613 index = t.CompileAndAdd();
2658 2614
2659 // ========================================================================= 2615 // =========================================================================
2660 // Build the calling function. 2616 // Build the calling function.
2661 // ========================================================================= 2617 // =========================================================================
2662 WasmRunner<int32_t> r; 2618 WasmRunner<int32_t> r(&module);
2663 r.env()->module = &module;
2664 2619
2665 { 2620 std::vector<byte> code;
2666 std::vector<byte> code; 2621 ADD_CODE(code,
2667 ADD_CODE(code, 2622 static_cast<byte>(WasmOpcodes::LoadStoreOpcodeOf(result, true)),
2668 static_cast<byte>(WasmOpcodes::LoadStoreOpcodeOf(result, true)), 2623 WasmOpcodes::LoadStoreAccessOf(false));
2669 WasmOpcodes::LoadStoreAccessOf(false)); 2624 ADD_CODE(code, WASM_ZERO);
2670 ADD_CODE(code, WASM_ZERO); 2625 ADD_CODE(code, kExprCallFunction, static_cast<byte>(index));
2671 ADD_CODE(code, kExprCallFunction, static_cast<byte>(index));
2672 2626
2673 for (int i = 0; i < num_params; i++) { 2627 for (int i = 0; i < num_params; i++) {
2674 int offset = (i + 1) * kElemSize; 2628 int offset = (i + 1) * kElemSize;
2675 ADD_CODE(code, WASM_LOAD_MEM(memtypes[i], WASM_I8(offset))); 2629 ADD_CODE(code, WASM_LOAD_MEM(memtypes[i], WASM_I8(offset)));
2676 } 2630 }
2677 2631
2678 ADD_CODE(code, WASM_I32(kExpected)); 2632 ADD_CODE(code, WASM_I32(kExpected));
2679 size_t end = code.size(); 2633 size_t end = code.size();
2680 code.push_back(0); 2634 code.push_back(0);
2681 r.Build(&code[0], &code[end]); 2635 r.Build(&code[0], &code[end]);
2682 }
2683 2636
2684 // Run the code. 2637 // Run the code.
2685 for (int t = 0; t < 10; t++) { 2638 for (int t = 0; t < 10; t++) {
2686 module.RandomizeMemory(); 2639 module.RandomizeMemory();
2687 CHECK_EQ(kExpected, r.Call()); 2640 CHECK_EQ(kExpected, r.Call());
2688 2641
2689 int size = WasmOpcodes::MemSize(result); 2642 int size = WasmOpcodes::MemSize(result);
2690 for (int i = 0; i < size; i++) { 2643 for (int i = 0; i < size; i++) {
2691 int base = (which + 1) * kElemSize; 2644 int base = (which + 1) * kElemSize;
2692 byte expected = module.raw_mem_at<byte>(base + i); 2645 byte expected = module.raw_mem_at<byte>(base + i);
2693 byte result = module.raw_mem_at<byte>(i); 2646 byte result = module.raw_mem_at<byte>(i);
2694 CHECK_EQ(expected, result); 2647 CHECK_EQ(expected, result);
2695 } 2648 }
2696 } 2649 }
2697 } 2650 }
2698 } 2651 }
2699 2652
2700 2653
2701 TEST(Run_WasmMixedCall_0) { Run_WasmMixedCall_N(0); } 2654 TEST(Run_WasmMixedCall_0) { Run_WasmMixedCall_N(0); }
2702 TEST(Run_WasmMixedCall_1) { Run_WasmMixedCall_N(1); } 2655 TEST(Run_WasmMixedCall_1) { Run_WasmMixedCall_N(1); }
2703 TEST(Run_WasmMixedCall_2) { Run_WasmMixedCall_N(2); } 2656 TEST(Run_WasmMixedCall_2) { Run_WasmMixedCall_N(2); }
2704 TEST(Run_WasmMixedCall_3) { Run_WasmMixedCall_N(3); } 2657 TEST(Run_WasmMixedCall_3) { Run_WasmMixedCall_N(3); }
2705 2658
2659 TEST(Run_Wasm_AddCall) {
2660 TestSignatures sigs;
2661 TestingModule module;
2662 WasmFunctionCompiler t1(sigs.i_ii(), &module);
2663 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
ahaas 2016/02/18 14:17:46 Why do you pass two parameters here? Just say WASM
titzer 2016/02/18 14:59:36 Checking two is better than checking one :-)
2664 t1.CompileAndAdd();
2665
2666 WasmRunner<int32_t> r(&module, MachineType::Int32());
2667 byte local = r.AllocateLocal(kAstI32);
2668 BUILD(r,
2669 WASM_BLOCK(2, WASM_SET_LOCAL(local, WASM_I8(99)),
2670 WASM_I32_ADD(
2671 WASM_CALL_FUNCTION(t1.function_index_, WASM_GET_LOCAL(0),
2672 WASM_GET_LOCAL(0)),
2673 WASM_CALL_FUNCTION(t1.function_index_, WASM_GET_LOCAL(1),
ahaas 2016/02/18 14:17:46 Why don't you use WASM_GET_LOCAL(local) here?
titzer 2016/02/18 14:59:36 Good catch. Done.
2674 WASM_GET_LOCAL(1)))));
2675
2676 CHECK_EQ(198, r.Call(0));
2677 CHECK_EQ(200, r.Call(1));
2678 CHECK_EQ(100, r.Call(-49));
2679 }
2706 2680
2707 TEST(Run_Wasm_CountDown_expr) { 2681 TEST(Run_Wasm_CountDown_expr) {
2708 WasmRunner<int32_t> r(MachineType::Int32()); 2682 WasmRunner<int32_t> r(MachineType::Int32());
2709 BUILD(r, WASM_LOOP( 2683 BUILD(r, WASM_LOOP(
2710 3, WASM_IF(WASM_NOT(WASM_GET_LOCAL(0)), 2684 3, WASM_IF(WASM_NOT(WASM_GET_LOCAL(0)),
2711 WASM_BREAKV(0, WASM_GET_LOCAL(0))), 2685 WASM_BREAKV(0, WASM_GET_LOCAL(0))),
2712 WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(1))), 2686 WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(1))),
2713 WASM_CONTINUE(0))); 2687 WASM_CONTINUE(0)));
2714 CHECK_EQ(0, r.Call(1)); 2688 CHECK_EQ(0, r.Call(1));
2715 CHECK_EQ(0, r.Call(10)); 2689 CHECK_EQ(0, r.Call(10));
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
2865 CHECK_EQ(14, r.Call(0, 0)); 2839 CHECK_EQ(14, r.Call(0, 0));
2866 } 2840 }
2867 2841
2868 2842
2869 #if WASM_64 2843 #if WASM_64
2870 TEST(Run_Wasm_LoadStoreI64_sx) { 2844 TEST(Run_Wasm_LoadStoreI64_sx) {
2871 byte loads[] = {kExprI64LoadMem8S, kExprI64LoadMem16S, kExprI64LoadMem32S, 2845 byte loads[] = {kExprI64LoadMem8S, kExprI64LoadMem16S, kExprI64LoadMem32S,
2872 kExprI64LoadMem}; 2846 kExprI64LoadMem};
2873 2847
2874 for (size_t m = 0; m < arraysize(loads); m++) { 2848 for (size_t m = 0; m < arraysize(loads); m++) {
2875 WasmRunner<int64_t> r;
2876 TestingModule module; 2849 TestingModule module;
2877 byte* memory = module.AddMemoryElems<byte>(16); 2850 byte* memory = module.AddMemoryElems<byte>(16);
2878 r.env()->module = &module; 2851 WasmRunner<int64_t> r(&module);
2879 2852
2880 byte code[] = {kExprI64StoreMem, 0, kExprI8Const, 8, 2853 byte code[] = {kExprI64StoreMem, 0, kExprI8Const, 8,
2881 loads[m], 0, kExprI8Const, 0}; 2854 loads[m], 0, kExprI8Const, 0};
2882 2855
2883 r.Build(code, code + arraysize(code)); 2856 r.Build(code, code + arraysize(code));
2884 2857
2885 // Try a bunch of different negative values. 2858 // Try a bunch of different negative values.
2886 for (int i = -1; i >= -128; i -= 11) { 2859 for (int i = -1; i >= -128; i -= 11) {
2887 int size = 1 << m; 2860 int size = 1 << m;
2888 module.BlankMemory(); 2861 module.BlankMemory();
2889 memory[size - 1] = static_cast<byte>(i); // set the high order byte. 2862 memory[size - 1] = static_cast<byte>(i); // set the high order byte.
2890 2863
2891 int64_t expected = static_cast<int64_t>(i) << ((size - 1) * 8); 2864 int64_t expected = static_cast<int64_t>(i) << ((size - 1) * 8);
2892 2865
2893 CHECK_EQ(expected, r.Call()); 2866 CHECK_EQ(expected, r.Call());
2894 CHECK_EQ(static_cast<byte>(i), memory[8 + size - 1]); 2867 CHECK_EQ(static_cast<byte>(i), memory[8 + size - 1]);
2895 for (int j = size; j < 8; j++) { 2868 for (int j = size; j < 8; j++) {
2896 CHECK_EQ(255, memory[8 + j]); 2869 CHECK_EQ(255, memory[8 + j]);
2897 } 2870 }
2898 } 2871 }
2899 } 2872 }
2900 } 2873 }
2901 2874
2902 2875
2903 #endif 2876 #endif
2904 2877
2905 2878
2906 TEST(Run_Wasm_SimpleCallIndirect) { 2879 TEST(Run_Wasm_SimpleCallIndirect) {
2907 WasmRunner<int32_t> r(MachineType::Int32());
2908 TestSignatures sigs; 2880 TestSignatures sigs;
2909 TestingModule module; 2881 TestingModule module;
2910 r.env()->module = &module; 2882
2911 WasmFunctionCompiler t1(sigs.i_ii()); 2883 WasmFunctionCompiler t1(sigs.i_ii(), &module);
2912 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); 2884 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2913 t1.CompileAndAdd(&module, /*sig_index*/ 1); 2885 t1.CompileAndAdd(/*sig_index*/ 1);
2914 2886
2915 WasmFunctionCompiler t2(sigs.i_ii()); 2887 WasmFunctionCompiler t2(sigs.i_ii(), &module);
2916 BUILD(t2, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); 2888 BUILD(t2, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2917 t2.CompileAndAdd(&module, /*sig_index*/ 1); 2889 t2.CompileAndAdd(/*sig_index*/ 1);
2918 2890
2919 // Signature table. 2891 // Signature table.
2920 module.AddSignature(sigs.f_ff()); 2892 module.AddSignature(sigs.f_ff());
2921 module.AddSignature(sigs.i_ii()); 2893 module.AddSignature(sigs.i_ii());
2922 module.AddSignature(sigs.d_dd()); 2894 module.AddSignature(sigs.d_dd());
2923 2895
2924 // Function table. 2896 // Function table.
2925 int table[] = {0, 1}; 2897 int table[] = {0, 1};
2926 module.AddIndirectFunctionTable(table, 2); 2898 module.AddIndirectFunctionTable(table, 2);
2927 module.PopulateIndirectFunctionTable(); 2899 module.PopulateIndirectFunctionTable();
2928 2900
2929 // Builder the caller function. 2901 // Builder the caller function.
2902 WasmRunner<int32_t> r(&module, MachineType::Int32());
2930 BUILD(r, WASM_CALL_INDIRECT(1, WASM_GET_LOCAL(0), WASM_I8(66), WASM_I8(22))); 2903 BUILD(r, WASM_CALL_INDIRECT(1, WASM_GET_LOCAL(0), WASM_I8(66), WASM_I8(22)));
2931 2904
2932 CHECK_EQ(88, r.Call(0)); 2905 CHECK_EQ(88, r.Call(0));
2933 CHECK_EQ(44, r.Call(1)); 2906 CHECK_EQ(44, r.Call(1));
2934 CHECK_TRAP(r.Call(2)); 2907 CHECK_TRAP(r.Call(2));
2935 } 2908 }
2936 2909
2937 2910
2938 TEST(Run_Wasm_MultipleCallIndirect) { 2911 TEST(Run_Wasm_MultipleCallIndirect) {
2939 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32(),
2940 MachineType::Int32());
2941 TestSignatures sigs; 2912 TestSignatures sigs;
2942 TestingModule module; 2913 TestingModule module;
2943 r.env()->module = &module; 2914
2944 WasmFunctionCompiler t1(sigs.i_ii()); 2915 WasmFunctionCompiler t1(sigs.i_ii(), &module);
2945 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); 2916 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2946 t1.CompileAndAdd(&module, /*sig_index*/ 1); 2917 t1.CompileAndAdd(/*sig_index*/ 1);
2947 2918
2948 WasmFunctionCompiler t2(sigs.i_ii()); 2919 WasmFunctionCompiler t2(sigs.i_ii(), &module);
2949 BUILD(t2, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); 2920 BUILD(t2, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2950 t2.CompileAndAdd(&module, /*sig_index*/ 1); 2921 t2.CompileAndAdd(/*sig_index*/ 1);
2951 2922
2952 // Signature table. 2923 // Signature table.
2953 module.AddSignature(sigs.f_ff()); 2924 module.AddSignature(sigs.f_ff());
2954 module.AddSignature(sigs.i_ii()); 2925 module.AddSignature(sigs.i_ii());
2955 module.AddSignature(sigs.d_dd()); 2926 module.AddSignature(sigs.d_dd());
2956 2927
2957 // Function table. 2928 // Function table.
2958 int table[] = {0, 1}; 2929 int table[] = {0, 1};
2959 module.AddIndirectFunctionTable(table, 2); 2930 module.AddIndirectFunctionTable(table, 2);
2960 module.PopulateIndirectFunctionTable(); 2931 module.PopulateIndirectFunctionTable();
2961 2932
2962 // Builder the caller function. 2933 // Builder the caller function.
2934 WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32(),
2935 MachineType::Int32());
2963 BUILD(r, 2936 BUILD(r,
2964 WASM_I32_ADD(WASM_CALL_INDIRECT(1, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), 2937 WASM_I32_ADD(WASM_CALL_INDIRECT(1, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
2965 WASM_GET_LOCAL(2)), 2938 WASM_GET_LOCAL(2)),
2966 WASM_CALL_INDIRECT(1, WASM_GET_LOCAL(1), WASM_GET_LOCAL(2), 2939 WASM_CALL_INDIRECT(1, WASM_GET_LOCAL(1), WASM_GET_LOCAL(2),
2967 WASM_GET_LOCAL(0)))); 2940 WASM_GET_LOCAL(0))));
2968 2941
2969 CHECK_EQ(5, r.Call(0, 1, 2)); 2942 CHECK_EQ(5, r.Call(0, 1, 2));
2970 CHECK_EQ(19, r.Call(0, 1, 9)); 2943 CHECK_EQ(19, r.Call(0, 1, 9));
2971 CHECK_EQ(1, r.Call(1, 0, 2)); 2944 CHECK_EQ(1, r.Call(1, 0, 2));
2972 CHECK_EQ(1, r.Call(1, 0, 9)); 2945 CHECK_EQ(1, r.Call(1, 0, 9));
2973 2946
2974 CHECK_TRAP(r.Call(0, 2, 1)); 2947 CHECK_TRAP(r.Call(0, 2, 1));
2975 CHECK_TRAP(r.Call(1, 2, 0)); 2948 CHECK_TRAP(r.Call(1, 2, 0));
2976 CHECK_TRAP(r.Call(2, 0, 1)); 2949 CHECK_TRAP(r.Call(2, 0, 1));
2977 CHECK_TRAP(r.Call(2, 1, 0)); 2950 CHECK_TRAP(r.Call(2, 1, 0));
2978 } 2951 }
2979 2952
2980 TEST(Run_Wasm_CallIndirect_NoTable) { 2953 TEST(Run_Wasm_CallIndirect_NoTable) {
2981 WasmRunner<int32_t> r(MachineType::Int32());
2982 TestSignatures sigs; 2954 TestSignatures sigs;
2983 TestingModule module; 2955 TestingModule module;
2984 r.env()->module = &module; 2956
2985 // One function. 2957 // One function.
2986 WasmFunctionCompiler t1(sigs.i_ii()); 2958 WasmFunctionCompiler t1(sigs.i_ii(), &module);
2987 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); 2959 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
2988 t1.CompileAndAdd(&module, /*sig_index*/ 1); 2960 t1.CompileAndAdd(/*sig_index*/ 1);
2989 2961
2990 // Signature table. 2962 // Signature table.
2991 module.AddSignature(sigs.f_ff()); 2963 module.AddSignature(sigs.f_ff());
2992 module.AddSignature(sigs.i_ii()); 2964 module.AddSignature(sigs.i_ii());
2993 2965
2994 // Builder the caller function. 2966 // Builder the caller function.
2967 WasmRunner<int32_t> r(&module, MachineType::Int32());
2995 BUILD(r, WASM_CALL_INDIRECT(1, WASM_GET_LOCAL(0), WASM_I8(66), WASM_I8(22))); 2968 BUILD(r, WASM_CALL_INDIRECT(1, WASM_GET_LOCAL(0), WASM_I8(66), WASM_I8(22)));
2996 2969
2997 CHECK_TRAP(r.Call(0)); 2970 CHECK_TRAP(r.Call(0));
2998 CHECK_TRAP(r.Call(1)); 2971 CHECK_TRAP(r.Call(1));
2999 CHECK_TRAP(r.Call(2)); 2972 CHECK_TRAP(r.Call(2));
3000 } 2973 }
3001 2974
3002 TEST(Run_Wasm_F32Floor) { 2975 TEST(Run_Wasm_F32Floor) {
3003 WasmRunner<float> r(MachineType::Float32()); 2976 WasmRunner<float> r(MachineType::Float32());
3004 BUILD(r, WASM_F32_FLOOR(WASM_GET_LOCAL(0))); 2977 BUILD(r, WASM_F32_FLOOR(WASM_GET_LOCAL(0)));
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
3407 for (byte num_params = 0; num_params < 40; num_params++) { 3380 for (byte num_params = 0; num_params < 40; num_params++) {
3408 Zone zone; 3381 Zone zone;
3409 HandleScope scope(CcTest::InitIsolateOnce()); 3382 HandleScope scope(CcTest::InitIsolateOnce());
3410 TestingModule module; 3383 TestingModule module;
3411 FunctionSig* sig = sigs.many(&zone, kAstStmt, param, num_params); 3384 FunctionSig* sig = sigs.many(&zone, kAstStmt, param, num_params);
3412 3385
3413 module.AddSignature(sig); 3386 module.AddSignature(sig);
3414 module.AddSignature(sig); 3387 module.AddSignature(sig);
3415 module.AddIndirectFunctionTable(nullptr, 0); 3388 module.AddIndirectFunctionTable(nullptr, 0);
3416 3389
3417 WasmFunctionCompiler t(sig); 3390 WasmFunctionCompiler t(sig, &module);
3418 t.env.module = &module;
3419 3391
3420 std::vector<byte> code; 3392 std::vector<byte> code;
3421 ADD_CODE(code, kExprCallIndirect, 1); 3393 ADD_CODE(code, kExprCallIndirect, 1);
3422 ADD_CODE(code, kExprI8Const, 0); 3394 ADD_CODE(code, kExprI8Const, 0);
3423 for (byte p = 0; p < num_params; p++) { 3395 for (byte p = 0; p < num_params; p++) {
3424 ADD_CODE(code, kExprGetLocal, p); 3396 ADD_CODE(code, kExprGetLocal, p);
3425 } 3397 }
3426 3398
3427 t.Build(&code[0], &code[0] + code.size()); 3399 t.Build(&code[0], &code[0] + code.size());
3428 t.Compile(&module); 3400 t.Compile();
3429 } 3401 }
3430 } 3402 }
3431 3403
3432 3404
3433 TEST(Compile_Wasm_CallIndirect_Many_i32) { CompileCallIndirectMany(kAstI32); } 3405 TEST(Compile_Wasm_CallIndirect_Many_i32) { CompileCallIndirectMany(kAstI32); }
3434 3406
3435 3407
3436 #if WASM_64 3408 #if WASM_64
3437 TEST(Compile_Wasm_CallIndirect_Many_i64) { CompileCallIndirectMany(kAstI64); } 3409 TEST(Compile_Wasm_CallIndirect_Many_i64) { CompileCallIndirectMany(kAstI64); }
3438 #endif 3410 #endif
3439 3411
3440 3412
3441 TEST(Compile_Wasm_CallIndirect_Many_f32) { CompileCallIndirectMany(kAstF32); } 3413 TEST(Compile_Wasm_CallIndirect_Many_f32) { CompileCallIndirectMany(kAstF32); }
3442 3414
3443 3415
3444 TEST(Compile_Wasm_CallIndirect_Many_f64) { CompileCallIndirectMany(kAstF64); } 3416 TEST(Compile_Wasm_CallIndirect_Many_f64) { CompileCallIndirectMany(kAstF64); }
OLDNEW
« no previous file with comments | « no previous file | test/cctest/wasm/test-run-wasm-js.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698