OLD | NEW |
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/base/platform/elapsed-timer.h" |
| 10 |
9 #include "src/wasm/wasm-macro-gen.h" | 11 #include "src/wasm/wasm-macro-gen.h" |
10 | 12 |
11 #include "test/cctest/cctest.h" | 13 #include "test/cctest/cctest.h" |
12 #include "test/cctest/compiler/value-helper.h" | 14 #include "test/cctest/compiler/value-helper.h" |
13 #include "test/cctest/wasm/test-signatures.h" | 15 #include "test/cctest/wasm/test-signatures.h" |
14 #include "test/cctest/wasm/wasm-run-utils.h" | 16 #include "test/cctest/wasm/wasm-run-utils.h" |
15 | 17 |
16 using namespace v8::base; | 18 using namespace v8::base; |
17 using namespace v8::internal; | 19 using namespace v8::internal; |
18 using namespace v8::internal::compiler; | 20 using namespace v8::internal::compiler; |
19 using namespace v8::internal::wasm; | 21 using namespace v8::internal::wasm; |
20 | 22 |
21 // for even shorter tests. | 23 // for even shorter tests. |
22 #define B2(a, b) kExprBlock, 2, a, b | 24 #define B2(a, b) kExprBlock, a, b, kExprEnd |
23 #define B1(a) kExprBlock, 1, a | 25 #define B1(a) kExprBlock, a, kExprEnd |
24 #define RET(x) kExprReturn, x | 26 #define RET(x) x, kExprReturn, 1 |
25 #define RET_I8(x) kExprReturn, kExprI8Const, x | 27 #define RET_I8(x) kExprI8Const, x, kExprReturn, 1 |
26 | 28 |
27 TEST(Run_WasmInt8Const) { | 29 TEST(Run_WasmInt8Const) { |
28 WasmRunner<int32_t> r; | 30 WasmRunner<int32_t> r; |
29 const byte kExpectedValue = 121; | 31 const byte kExpectedValue = 121; |
30 // return(kExpectedValue) | 32 // return(kExpectedValue) |
31 BUILD(r, WASM_I8(kExpectedValue)); | 33 BUILD(r, WASM_I8(kExpectedValue)); |
32 CHECK_EQ(kExpectedValue, r.Call()); | 34 CHECK_EQ(kExpectedValue, r.Call()); |
33 } | 35 } |
34 | 36 |
35 | 37 |
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 // if (p0) return 11; else return 22; | 681 // if (p0) return 11; else return 22; |
680 BUILD(r, WASM_IF_ELSE(WASM_GET_LOCAL(0), // -- | 682 BUILD(r, WASM_IF_ELSE(WASM_GET_LOCAL(0), // -- |
681 WASM_I8(11), // -- | 683 WASM_I8(11), // -- |
682 WASM_I8(22))); // -- | 684 WASM_I8(22))); // -- |
683 FOR_INT32_INPUTS(i) { | 685 FOR_INT32_INPUTS(i) { |
684 int32_t expected = *i ? 11 : 22; | 686 int32_t expected = *i ? 11 : 22; |
685 CHECK_EQ(expected, r.Call(*i)); | 687 CHECK_EQ(expected, r.Call(*i)); |
686 } | 688 } |
687 } | 689 } |
688 | 690 |
| 691 TEST(Run_Wasm_If_chain) { |
| 692 WasmRunner<int32_t> r(MachineType::Int32()); |
| 693 // if (p0) 13; if (p0) 14; 15 |
| 694 BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_I8(13)), |
| 695 WASM_IF(WASM_GET_LOCAL(0), WASM_I8(14)), WASM_I8(15)); |
| 696 FOR_INT32_INPUTS(i) { CHECK_EQ(15, r.Call(*i)); } |
| 697 } |
| 698 |
| 699 TEST(Run_Wasm_If_chain_set) { |
| 700 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); |
| 701 // if (p0) p1 = 73; if (p0) p1 = 74; p1 |
| 702 BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(1, WASM_I8(73))), |
| 703 WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(1, WASM_I8(74))), |
| 704 WASM_GET_LOCAL(1)); |
| 705 FOR_INT32_INPUTS(i) { |
| 706 int32_t expected = *i ? 74 : *i; |
| 707 CHECK_EQ(expected, r.Call(*i, *i)); |
| 708 } |
| 709 } |
689 | 710 |
690 TEST(Run_Wasm_IfElse_Unreachable1) { | 711 TEST(Run_Wasm_IfElse_Unreachable1) { |
691 WasmRunner<int32_t> r; | 712 WasmRunner<int32_t> r; |
692 // if (0) unreachable; else return 22; | 713 // if (0) unreachable; else return 22; |
693 BUILD(r, WASM_IF_ELSE(WASM_ZERO, // -- | 714 BUILD(r, WASM_IF_ELSE(WASM_ZERO, // -- |
694 WASM_UNREACHABLE, // -- | 715 WASM_UNREACHABLE, // -- |
695 WASM_I8(27))); // -- | 716 WASM_I8(27))); // -- |
696 CHECK_EQ(27, r.Call()); | 717 CHECK_EQ(27, r.Call()); |
697 } | 718 } |
698 | 719 |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1066 int32_t result = r.Call(); | 1087 int32_t result = r.Call(); |
1067 CHECK_EQ(kExpected, result); | 1088 CHECK_EQ(kExpected, result); |
1068 } | 1089 } |
1069 | 1090 |
1070 | 1091 |
1071 TEST(Run_Wasm_Block_If_P) { | 1092 TEST(Run_Wasm_Block_If_P) { |
1072 WasmRunner<int32_t> r(MachineType::Int32()); | 1093 WasmRunner<int32_t> r(MachineType::Int32()); |
1073 // { if (p0) return 51; return 52; } | 1094 // { if (p0) return 51; return 52; } |
1074 BUILD(r, B2( // -- | 1095 BUILD(r, B2( // -- |
1075 WASM_IF(WASM_GET_LOCAL(0), // -- | 1096 WASM_IF(WASM_GET_LOCAL(0), // -- |
1076 WASM_BRV(0, WASM_I8(51))), // -- | 1097 WASM_BRV(1, WASM_I8(51))), // -- |
1077 WASM_I8(52))); // -- | 1098 WASM_I8(52))); // -- |
1078 FOR_INT32_INPUTS(i) { | 1099 FOR_INT32_INPUTS(i) { |
1079 int32_t expected = *i ? 51 : 52; | 1100 int32_t expected = *i ? 51 : 52; |
1080 CHECK_EQ(expected, r.Call(*i)); | 1101 CHECK_EQ(expected, r.Call(*i)); |
1081 } | 1102 } |
1082 } | 1103 } |
1083 | 1104 |
1084 | 1105 |
1085 TEST(Run_Wasm_Block_BrIf_P) { | 1106 TEST(Run_Wasm_Block_BrIf_P) { |
1086 WasmRunner<int32_t> r(MachineType::Int32()); | 1107 WasmRunner<int32_t> r(MachineType::Int32()); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1167 } | 1188 } |
1168 } | 1189 } |
1169 | 1190 |
1170 | 1191 |
1171 TEST(Run_Wasm_CountDown) { | 1192 TEST(Run_Wasm_CountDown) { |
1172 WasmRunner<int32_t> r(MachineType::Int32()); | 1193 WasmRunner<int32_t> r(MachineType::Int32()); |
1173 BUILD(r, | 1194 BUILD(r, |
1174 WASM_BLOCK( | 1195 WASM_BLOCK( |
1175 2, WASM_LOOP( | 1196 2, WASM_LOOP( |
1176 1, WASM_IF(WASM_GET_LOCAL(0), | 1197 1, WASM_IF(WASM_GET_LOCAL(0), |
1177 WASM_BRV(0, WASM_SET_LOCAL( | 1198 WASM_BRV(1, WASM_SET_LOCAL( |
1178 0, WASM_I32_SUB(WASM_GET_LOCAL(0), | 1199 0, WASM_I32_SUB(WASM_GET_LOCAL(0), |
1179 WASM_I8(1)))))), | 1200 WASM_I8(1)))))), |
1180 WASM_GET_LOCAL(0))); | 1201 WASM_GET_LOCAL(0))); |
1181 CHECK_EQ(0, r.Call(1)); | 1202 CHECK_EQ(0, r.Call(1)); |
1182 CHECK_EQ(0, r.Call(10)); | 1203 CHECK_EQ(0, r.Call(10)); |
1183 CHECK_EQ(0, r.Call(100)); | 1204 CHECK_EQ(0, r.Call(100)); |
1184 } | 1205 } |
1185 | 1206 |
1186 | 1207 |
1187 TEST(Run_Wasm_CountDown_fallthru) { | 1208 TEST(Run_Wasm_CountDown_fallthru) { |
1188 WasmRunner<int32_t> r(MachineType::Int32()); | 1209 WasmRunner<int32_t> r(MachineType::Int32()); |
1189 BUILD(r, | 1210 BUILD(r, |
1190 WASM_BLOCK( | 1211 WASM_BLOCK( |
1191 2, WASM_LOOP(3, WASM_IF(WASM_NOT(WASM_GET_LOCAL(0)), WASM_BREAK(0)), | 1212 2, WASM_LOOP(3, WASM_IF(WASM_NOT(WASM_GET_LOCAL(0)), WASM_BREAK(1)), |
1192 WASM_SET_LOCAL( | 1213 WASM_SET_LOCAL( |
1193 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(1))), | 1214 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(1))), |
1194 WASM_CONTINUE(0)), | 1215 WASM_CONTINUE(0)), |
1195 WASM_GET_LOCAL(0))); | 1216 WASM_GET_LOCAL(0))); |
1196 CHECK_EQ(0, r.Call(1)); | 1217 CHECK_EQ(0, r.Call(1)); |
1197 CHECK_EQ(0, r.Call(10)); | 1218 CHECK_EQ(0, r.Call(10)); |
1198 CHECK_EQ(0, r.Call(100)); | 1219 CHECK_EQ(0, r.Call(100)); |
1199 } | 1220 } |
1200 | 1221 |
1201 | 1222 |
1202 TEST(Run_Wasm_WhileCountDown) { | 1223 TEST(Run_Wasm_WhileCountDown) { |
1203 WasmRunner<int32_t> r(MachineType::Int32()); | 1224 WasmRunner<int32_t> r(MachineType::Int32()); |
1204 BUILD(r, WASM_BLOCK( | 1225 BUILD(r, WASM_BLOCK( |
1205 2, WASM_WHILE(WASM_GET_LOCAL(0), | 1226 2, WASM_WHILE(WASM_GET_LOCAL(0), |
1206 WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), | 1227 WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), |
1207 WASM_I8(1)))), | 1228 WASM_I8(1)))), |
1208 WASM_GET_LOCAL(0))); | 1229 WASM_GET_LOCAL(0))); |
1209 CHECK_EQ(0, r.Call(1)); | 1230 CHECK_EQ(0, r.Call(1)); |
1210 CHECK_EQ(0, r.Call(10)); | 1231 CHECK_EQ(0, r.Call(10)); |
1211 CHECK_EQ(0, r.Call(100)); | 1232 CHECK_EQ(0, r.Call(100)); |
1212 } | 1233 } |
1213 | 1234 |
1214 | 1235 |
1215 TEST(Run_Wasm_Loop_if_break1) { | 1236 TEST(Run_Wasm_Loop_if_break1) { |
1216 WasmRunner<int32_t> r(MachineType::Int32()); | 1237 WasmRunner<int32_t> r(MachineType::Int32()); |
1217 BUILD(r, B2(WASM_LOOP(2, WASM_IF(WASM_GET_LOCAL(0), WASM_BREAK(0)), | 1238 BUILD(r, B2(WASM_LOOP(2, WASM_IF(WASM_GET_LOCAL(0), WASM_BREAK(1)), |
1218 WASM_SET_LOCAL(0, WASM_I8(99))), | 1239 WASM_SET_LOCAL(0, WASM_I8(99))), |
1219 WASM_GET_LOCAL(0))); | 1240 WASM_GET_LOCAL(0))); |
1220 CHECK_EQ(99, r.Call(0)); | 1241 CHECK_EQ(99, r.Call(0)); |
1221 CHECK_EQ(3, r.Call(3)); | 1242 CHECK_EQ(3, r.Call(3)); |
1222 CHECK_EQ(10000, r.Call(10000)); | 1243 CHECK_EQ(10000, r.Call(10000)); |
1223 CHECK_EQ(-29, r.Call(-29)); | 1244 CHECK_EQ(-29, r.Call(-29)); |
1224 } | 1245 } |
1225 | 1246 |
1226 | 1247 |
1227 TEST(Run_Wasm_Loop_if_break2) { | 1248 TEST(Run_Wasm_Loop_if_break2) { |
(...skipping 12 matching lines...) Expand all Loading... |
1240 WasmRunner<int32_t> r(MachineType::Int32()); | 1261 WasmRunner<int32_t> r(MachineType::Int32()); |
1241 BUILD(r, B1(WASM_LOOP(2, WASM_IF(WASM_GET_LOCAL(0), WASM_BREAK(1)), | 1262 BUILD(r, B1(WASM_LOOP(2, WASM_IF(WASM_GET_LOCAL(0), WASM_BREAK(1)), |
1242 WASM_SET_LOCAL(0, WASM_I8(93)))), | 1263 WASM_SET_LOCAL(0, WASM_I8(93)))), |
1243 WASM_GET_LOCAL(0)); | 1264 WASM_GET_LOCAL(0)); |
1244 CHECK_EQ(93, r.Call(0)); | 1265 CHECK_EQ(93, r.Call(0)); |
1245 CHECK_EQ(3, r.Call(3)); | 1266 CHECK_EQ(3, r.Call(3)); |
1246 CHECK_EQ(10001, r.Call(10001)); | 1267 CHECK_EQ(10001, r.Call(10001)); |
1247 CHECK_EQ(-22, r.Call(-22)); | 1268 CHECK_EQ(-22, r.Call(-22)); |
1248 } | 1269 } |
1249 | 1270 |
| 1271 TEST(Run_Wasm_IfBreak1) { |
| 1272 WasmRunner<int32_t> r(MachineType::Int32()); |
| 1273 BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SEQ(WASM_BR(0), WASM_UNREACHABLE)), |
| 1274 WASM_I8(91)); |
| 1275 CHECK_EQ(91, r.Call(0)); |
| 1276 CHECK_EQ(91, r.Call(1)); |
| 1277 CHECK_EQ(91, r.Call(-8734)); |
| 1278 } |
| 1279 |
| 1280 TEST(Run_Wasm_IfBreak2) { |
| 1281 WasmRunner<int32_t> r(MachineType::Int32()); |
| 1282 BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SEQ(WASM_BR(0), RET_I8(77))), |
| 1283 WASM_I8(81)); |
| 1284 CHECK_EQ(81, r.Call(0)); |
| 1285 CHECK_EQ(81, r.Call(1)); |
| 1286 CHECK_EQ(81, r.Call(-8734)); |
| 1287 } |
1250 | 1288 |
1251 TEST(Run_Wasm_LoadMemI32) { | 1289 TEST(Run_Wasm_LoadMemI32) { |
1252 TestingModule module; | 1290 TestingModule module; |
1253 int32_t* memory = module.AddMemoryElems<int32_t>(8); | 1291 int32_t* memory = module.AddMemoryElems<int32_t>(8); |
1254 WasmRunner<int32_t> r(&module, MachineType::Int32()); | 1292 WasmRunner<int32_t> r(&module, MachineType::Int32()); |
1255 module.RandomizeMemory(1111); | 1293 module.RandomizeMemory(1111); |
1256 | 1294 |
1257 BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(0))); | 1295 BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(0))); |
1258 | 1296 |
1259 memory[0] = 99999999; | 1297 memory[0] = 99999999; |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1521 } | 1559 } |
1522 } | 1560 } |
1523 | 1561 |
1524 | 1562 |
1525 TEST(Run_Wasm_CheckMachIntsZero) { | 1563 TEST(Run_Wasm_CheckMachIntsZero) { |
1526 const int kNumElems = 55; | 1564 const int kNumElems = 55; |
1527 TestingModule module; | 1565 TestingModule module; |
1528 module.AddMemoryElems<uint32_t>(kNumElems); | 1566 module.AddMemoryElems<uint32_t>(kNumElems); |
1529 WasmRunner<uint32_t> r(&module, MachineType::Int32()); | 1567 WasmRunner<uint32_t> r(&module, MachineType::Int32()); |
1530 | 1568 |
1531 BUILD(r, kExprBlock, 2, kExprLoop, 1, kExprIf, kExprGetLocal, 0, kExprBr, 0, | 1569 BUILD(r, kExprLoop, kExprGetLocal, 0, kExprIf, kExprGetLocal, 0, |
1532 kExprIfElse, kExprI32LoadMem, ZERO_ALIGNMENT, ZERO_OFFSET, | 1570 kExprI32LoadMem, 0, 0, kExprIf, kExprI8Const, 255, kExprReturn, ARITY_1, |
1533 kExprGetLocal, 0, kExprBr, 2, kExprI8Const, 255, kExprSetLocal, 0, | 1571 kExprEnd, kExprGetLocal, 0, kExprI8Const, 4, kExprI32Sub, kExprSetLocal, |
1534 kExprI32Sub, kExprGetLocal, 0, kExprI8Const, 4, kExprI8Const, 0); | 1572 0, kExprBr, ARITY_1, DEPTH_0, kExprEnd, kExprEnd, kExprI8Const, 0); |
1535 | 1573 |
1536 module.BlankMemory(); | 1574 module.BlankMemory(); |
1537 CHECK_EQ(0, r.Call((kNumElems - 1) * 4)); | 1575 CHECK_EQ(0, r.Call((kNumElems - 1) * 4)); |
1538 } | 1576 } |
1539 | 1577 |
1540 | 1578 |
1541 TEST(Run_Wasm_MemF32_Sum) { | 1579 TEST(Run_Wasm_MemF32_Sum) { |
1542 const int kSize = 5; | 1580 const int kSize = 5; |
1543 TestingModule module; | 1581 TestingModule module; |
1544 module.AddMemoryElems<float>(kSize); | 1582 module.AddMemoryElems<float>(kSize); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1688 TEST(Run_Wasm_Infinite_Loop_not_taken1) { | 1726 TEST(Run_Wasm_Infinite_Loop_not_taken1) { |
1689 WasmRunner<int32_t> r(MachineType::Int32()); | 1727 WasmRunner<int32_t> r(MachineType::Int32()); |
1690 BUILD(r, B2(WASM_IF(WASM_GET_LOCAL(0), WASM_INFINITE_LOOP), WASM_I8(45))); | 1728 BUILD(r, B2(WASM_IF(WASM_GET_LOCAL(0), WASM_INFINITE_LOOP), WASM_I8(45))); |
1691 // Run the code, but don't go into the infinite loop. | 1729 // Run the code, but don't go into the infinite loop. |
1692 CHECK_EQ(45, r.Call(0)); | 1730 CHECK_EQ(45, r.Call(0)); |
1693 } | 1731 } |
1694 | 1732 |
1695 | 1733 |
1696 TEST(Run_Wasm_Infinite_Loop_not_taken2) { | 1734 TEST(Run_Wasm_Infinite_Loop_not_taken2) { |
1697 WasmRunner<int32_t> r(MachineType::Int32()); | 1735 WasmRunner<int32_t> r(MachineType::Int32()); |
1698 BUILD(r, B1(WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_BRV(0, WASM_I8(45)), | 1736 BUILD(r, B1(WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_I8(45)), |
1699 WASM_INFINITE_LOOP))); | 1737 WASM_INFINITE_LOOP))); |
1700 // Run the code, but don't go into the infinite loop. | 1738 // Run the code, but don't go into the infinite loop. |
1701 CHECK_EQ(45, r.Call(1)); | 1739 CHECK_EQ(45, r.Call(1)); |
1702 } | 1740 } |
1703 | 1741 |
1704 | 1742 |
1705 TEST(Run_Wasm_Infinite_Loop_not_taken2_brif) { | 1743 TEST(Run_Wasm_Infinite_Loop_not_taken2_brif) { |
1706 WasmRunner<int32_t> r(MachineType::Int32()); | 1744 WasmRunner<int32_t> r(MachineType::Int32()); |
1707 BUILD(r, | 1745 BUILD(r, |
1708 B2(WASM_BRV_IF(0, WASM_I8(45), WASM_GET_LOCAL(0)), WASM_INFINITE_LOOP)); | 1746 B2(WASM_BRV_IF(0, WASM_I8(45), WASM_GET_LOCAL(0)), WASM_INFINITE_LOOP)); |
(...skipping 10 matching lines...) Expand all Loading... |
1719 HandleScope scope(isolate); | 1757 HandleScope scope(isolate); |
1720 // Enable all optional operators. | 1758 // Enable all optional operators. |
1721 CommonOperatorBuilder common(&zone); | 1759 CommonOperatorBuilder common(&zone); |
1722 MachineOperatorBuilder machine(&zone, MachineType::PointerRepresentation(), | 1760 MachineOperatorBuilder machine(&zone, MachineType::PointerRepresentation(), |
1723 MachineOperatorBuilder::kAllOptionalOps); | 1761 MachineOperatorBuilder::kAllOptionalOps); |
1724 Graph graph(&zone); | 1762 Graph graph(&zone); |
1725 JSGraph jsgraph(isolate, &graph, &common, nullptr, nullptr, &machine); | 1763 JSGraph jsgraph(isolate, &graph, &common, nullptr, nullptr, &machine); |
1726 FunctionSig* sig = WasmOpcodes::Signature(opcode); | 1764 FunctionSig* sig = WasmOpcodes::Signature(opcode); |
1727 | 1765 |
1728 if (sig->parameter_count() == 1) { | 1766 if (sig->parameter_count() == 1) { |
1729 byte code[] = {WASM_NO_LOCALS, static_cast<byte>(opcode), kExprGetLocal, 0}; | 1767 byte code[] = {WASM_NO_LOCALS, kExprGetLocal, 0, static_cast<byte>(opcode)}; |
1730 TestBuildingGraph(&zone, &jsgraph, nullptr, sig, nullptr, code, | 1768 TestBuildingGraph(&zone, &jsgraph, nullptr, sig, nullptr, code, |
1731 code + arraysize(code)); | 1769 code + arraysize(code)); |
1732 } else { | 1770 } else { |
1733 CHECK_EQ(2, sig->parameter_count()); | 1771 CHECK_EQ(2, sig->parameter_count()); |
1734 byte code[] = {WASM_NO_LOCALS, static_cast<byte>(opcode), | 1772 byte code[] = {WASM_NO_LOCALS, kExprGetLocal, 0, kExprGetLocal, 1, |
1735 kExprGetLocal, 0, | 1773 static_cast<byte>(opcode)}; |
1736 kExprGetLocal, 1}; | |
1737 TestBuildingGraph(&zone, &jsgraph, nullptr, sig, nullptr, code, | 1774 TestBuildingGraph(&zone, &jsgraph, nullptr, sig, nullptr, code, |
1738 code + arraysize(code)); | 1775 code + arraysize(code)); |
1739 } | 1776 } |
1740 } | 1777 } |
1741 | 1778 |
1742 | 1779 |
1743 TEST(Build_Wasm_SimpleExprs) { | 1780 TEST(Build_Wasm_SimpleExprs) { |
1744 // Test that the decoder can build a graph for all supported simple expressions. | 1781 // Test that the decoder can build a graph for all supported simple expressions. |
1745 #define GRAPH_BUILD_TEST(name, opcode, sig) \ | 1782 #define GRAPH_BUILD_TEST(name, opcode, sig) \ |
1746 TestBuildGraphForSimpleExpression(kExpr##name); | 1783 TestBuildGraphForSimpleExpression(kExpr##name); |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1975 LocalType param_types[20]; | 2012 LocalType param_types[20]; |
1976 for (int i = 0; i < 20; i++) param_types[i] = kAstF32; | 2013 for (int i = 0; i < 20; i++) param_types[i] = kAstF32; |
1977 FunctionSig sig(1, 19, param_types); | 2014 FunctionSig sig(1, 19, param_types); |
1978 TestingModule module; | 2015 TestingModule module; |
1979 WasmFunctionCompiler t(&sig, &module); | 2016 WasmFunctionCompiler t(&sig, &module); |
1980 BUILD(t, WASM_GET_LOCAL(17)); | 2017 BUILD(t, WASM_GET_LOCAL(17)); |
1981 uint32_t index = t.CompileAndAdd(); | 2018 uint32_t index = t.CompileAndAdd(); |
1982 | 2019 |
1983 // Build the calling function. | 2020 // Build the calling function. |
1984 WasmRunner<float> r(&module); | 2021 WasmRunner<float> r(&module); |
1985 BUILD(r, WASM_CALL_FUNCTION( | 2022 BUILD(r, WASM_CALL_FUNCTIONN( |
1986 index, WASM_F32(1.0f), WASM_F32(2.0f), WASM_F32(4.0f), | 2023 19, index, WASM_F32(1.0f), WASM_F32(2.0f), WASM_F32(4.0f), |
1987 WASM_F32(8.0f), WASM_F32(16.0f), WASM_F32(32.0f), | 2024 WASM_F32(8.0f), WASM_F32(16.0f), WASM_F32(32.0f), |
1988 WASM_F32(64.0f), WASM_F32(128.0f), WASM_F32(256.0f), | 2025 WASM_F32(64.0f), WASM_F32(128.0f), WASM_F32(256.0f), |
1989 WASM_F32(1.5f), WASM_F32(2.5f), WASM_F32(4.5f), WASM_F32(8.5f), | 2026 WASM_F32(1.5f), WASM_F32(2.5f), WASM_F32(4.5f), WASM_F32(8.5f), |
1990 WASM_F32(16.5f), WASM_F32(32.5f), WASM_F32(64.5f), | 2027 WASM_F32(16.5f), WASM_F32(32.5f), WASM_F32(64.5f), |
1991 WASM_F32(128.5f), WASM_F32(256.5f), WASM_F32(512.5f))); | 2028 WASM_F32(128.5f), WASM_F32(256.5f), WASM_F32(512.5f))); |
1992 | 2029 |
1993 float result = r.Call(); | 2030 float result = r.Call(); |
1994 CHECK_EQ(256.5f, result); | 2031 CHECK_EQ(256.5f, result); |
1995 } | 2032 } |
1996 | 2033 |
1997 | 2034 |
1998 TEST(Run_WasmCallF64StackParameter) { | 2035 TEST(Run_WasmCallF64StackParameter) { |
1999 // Build the target function. | 2036 // Build the target function. |
2000 LocalType param_types[20]; | 2037 LocalType param_types[20]; |
2001 for (int i = 0; i < 20; i++) param_types[i] = kAstF64; | 2038 for (int i = 0; i < 20; i++) param_types[i] = kAstF64; |
2002 FunctionSig sig(1, 19, param_types); | 2039 FunctionSig sig(1, 19, param_types); |
2003 TestingModule module; | 2040 TestingModule module; |
2004 WasmFunctionCompiler t(&sig, &module); | 2041 WasmFunctionCompiler t(&sig, &module); |
2005 BUILD(t, WASM_GET_LOCAL(17)); | 2042 BUILD(t, WASM_GET_LOCAL(17)); |
2006 uint32_t index = t.CompileAndAdd(); | 2043 uint32_t index = t.CompileAndAdd(); |
2007 | 2044 |
2008 // Build the calling function. | 2045 // Build the calling function. |
2009 WasmRunner<double> r(&module); | 2046 WasmRunner<double> r(&module); |
2010 BUILD(r, WASM_CALL_FUNCTION(index, WASM_F64(1.0), WASM_F64(2.0), | 2047 BUILD(r, WASM_CALL_FUNCTIONN(19, index, WASM_F64(1.0), WASM_F64(2.0), |
2011 WASM_F64(4.0), WASM_F64(8.0), WASM_F64(16.0), | 2048 WASM_F64(4.0), WASM_F64(8.0), WASM_F64(16.0), |
2012 WASM_F64(32.0), WASM_F64(64.0), WASM_F64(128.0), | 2049 WASM_F64(32.0), WASM_F64(64.0), WASM_F64(128.0), |
2013 WASM_F64(256.0), WASM_F64(1.5), WASM_F64(2.5), | 2050 WASM_F64(256.0), WASM_F64(1.5), WASM_F64(2.5), |
2014 WASM_F64(4.5), WASM_F64(8.5), WASM_F64(16.5), | 2051 WASM_F64(4.5), WASM_F64(8.5), WASM_F64(16.5), |
2015 WASM_F64(32.5), WASM_F64(64.5), WASM_F64(128.5), | 2052 WASM_F64(32.5), WASM_F64(64.5), WASM_F64(128.5), |
2016 WASM_F64(256.5), WASM_F64(512.5))); | 2053 WASM_F64(256.5), WASM_F64(512.5))); |
2017 | 2054 |
2018 float result = r.Call(); | 2055 float result = r.Call(); |
2019 CHECK_EQ(256.5, result); | 2056 CHECK_EQ(256.5, result); |
2020 } | 2057 } |
2021 | 2058 |
2022 TEST(Run_WasmCallVoid) { | 2059 TEST(Run_WasmCallVoid) { |
2023 const byte kMemOffset = 8; | 2060 const byte kMemOffset = 8; |
2024 const int32_t kElemNum = kMemOffset / sizeof(int32_t); | 2061 const int32_t kElemNum = kMemOffset / sizeof(int32_t); |
2025 const int32_t kExpected = -414444; | 2062 const int32_t kExpected = -414444; |
2026 // Build the target function. | 2063 // Build the target function. |
(...skipping 20 matching lines...) Expand all Loading... |
2047 TEST(Run_WasmCall_Int32Add) { | 2084 TEST(Run_WasmCall_Int32Add) { |
2048 // Build the target function. | 2085 // Build the target function. |
2049 TestSignatures sigs; | 2086 TestSignatures sigs; |
2050 TestingModule module; | 2087 TestingModule module; |
2051 WasmFunctionCompiler t(sigs.i_ii(), &module); | 2088 WasmFunctionCompiler t(sigs.i_ii(), &module); |
2052 BUILD(t, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); | 2089 BUILD(t, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); |
2053 uint32_t index = t.CompileAndAdd(); | 2090 uint32_t index = t.CompileAndAdd(); |
2054 | 2091 |
2055 // Build the caller function. | 2092 // Build the caller function. |
2056 WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32()); | 2093 WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32()); |
2057 BUILD(r, WASM_CALL_FUNCTION(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); | 2094 BUILD(r, WASM_CALL_FUNCTION2(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); |
2058 | 2095 |
2059 FOR_INT32_INPUTS(i) { | 2096 FOR_INT32_INPUTS(i) { |
2060 FOR_INT32_INPUTS(j) { | 2097 FOR_INT32_INPUTS(j) { |
2061 int32_t expected = static_cast<int32_t>(static_cast<uint32_t>(*i) + | 2098 int32_t expected = static_cast<int32_t>(static_cast<uint32_t>(*i) + |
2062 static_cast<uint32_t>(*j)); | 2099 static_cast<uint32_t>(*j)); |
2063 CHECK_EQ(expected, r.Call(*i, *j)); | 2100 CHECK_EQ(expected, r.Call(*i, *j)); |
2064 } | 2101 } |
2065 } | 2102 } |
2066 } | 2103 } |
2067 | 2104 |
2068 TEST(Run_WasmCall_Float32Sub) { | 2105 TEST(Run_WasmCall_Float32Sub) { |
2069 TestSignatures sigs; | 2106 TestSignatures sigs; |
2070 TestingModule module; | 2107 TestingModule module; |
2071 WasmFunctionCompiler t(sigs.f_ff(), &module); | 2108 WasmFunctionCompiler t(sigs.f_ff(), &module); |
2072 | 2109 |
2073 // Build the target function. | 2110 // Build the target function. |
2074 BUILD(t, WASM_F32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); | 2111 BUILD(t, WASM_F32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); |
2075 uint32_t index = t.CompileAndAdd(); | 2112 uint32_t index = t.CompileAndAdd(); |
2076 | 2113 |
2077 // Builder the caller function. | 2114 // Builder the caller function. |
2078 WasmRunner<float> r(&module, MachineType::Float32(), MachineType::Float32()); | 2115 WasmRunner<float> r(&module, MachineType::Float32(), MachineType::Float32()); |
2079 BUILD(r, WASM_CALL_FUNCTION(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); | 2116 BUILD(r, WASM_CALL_FUNCTION2(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); |
2080 | 2117 |
2081 FOR_FLOAT32_INPUTS(i) { | 2118 FOR_FLOAT32_INPUTS(i) { |
2082 FOR_FLOAT32_INPUTS(j) { CHECK_FLOAT_EQ(*i - *j, r.Call(*i, *j)); } | 2119 FOR_FLOAT32_INPUTS(j) { CHECK_FLOAT_EQ(*i - *j, r.Call(*i, *j)); } |
2083 } | 2120 } |
2084 } | 2121 } |
2085 | 2122 |
2086 | 2123 |
2087 TEST(Run_WasmCall_Float64Sub) { | 2124 TEST(Run_WasmCall_Float64Sub) { |
2088 TestingModule module; | 2125 TestingModule module; |
2089 double* memory = module.AddMemoryElems<double>(16); | 2126 double* memory = module.AddMemoryElems<double>(16); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2158 b.AddParam(WasmOpcodes::LocalTypeFor(memtypes[i])); | 2195 b.AddParam(WasmOpcodes::LocalTypeFor(memtypes[i])); |
2159 } | 2196 } |
2160 WasmFunctionCompiler t(b.Build(), &module); | 2197 WasmFunctionCompiler t(b.Build(), &module); |
2161 BUILD(t, WASM_GET_LOCAL(which)); | 2198 BUILD(t, WASM_GET_LOCAL(which)); |
2162 index = t.CompileAndAdd(); | 2199 index = t.CompileAndAdd(); |
2163 | 2200 |
2164 // ========================================================================= | 2201 // ========================================================================= |
2165 // Build the calling function. | 2202 // Build the calling function. |
2166 // ========================================================================= | 2203 // ========================================================================= |
2167 WasmRunner<int32_t> r(&module); | 2204 WasmRunner<int32_t> r(&module); |
| 2205 std::vector<byte> code; |
2168 | 2206 |
2169 std::vector<byte> code; | 2207 // Load the offset for the store. |
2170 ADD_CODE(code, | |
2171 static_cast<byte>(WasmOpcodes::LoadStoreOpcodeOf(result, true)), | |
2172 ZERO_ALIGNMENT, ZERO_OFFSET); | |
2173 ADD_CODE(code, WASM_ZERO); | 2208 ADD_CODE(code, WASM_ZERO); |
2174 ADD_CODE(code, kExprCallFunction, static_cast<byte>(index)); | |
2175 | 2209 |
| 2210 // Load the arguments. |
2176 for (int i = 0; i < num_params; i++) { | 2211 for (int i = 0; i < num_params; i++) { |
2177 int offset = (i + 1) * kElemSize; | 2212 int offset = (i + 1) * kElemSize; |
2178 ADD_CODE(code, WASM_LOAD_MEM(memtypes[i], WASM_I8(offset))); | 2213 ADD_CODE(code, WASM_LOAD_MEM(memtypes[i], WASM_I8(offset))); |
2179 } | 2214 } |
2180 | 2215 |
| 2216 // Call the selector function. |
| 2217 ADD_CODE(code, kExprCallFunction, static_cast<byte>(num_params), |
| 2218 static_cast<byte>(index)); |
| 2219 |
| 2220 // Store the result in memory. |
| 2221 ADD_CODE(code, |
| 2222 static_cast<byte>(WasmOpcodes::LoadStoreOpcodeOf(result, true)), |
| 2223 ZERO_ALIGNMENT, ZERO_OFFSET); |
| 2224 |
| 2225 // Return the expected value. |
2181 ADD_CODE(code, WASM_I32V_2(kExpected)); | 2226 ADD_CODE(code, WASM_I32V_2(kExpected)); |
2182 size_t end = code.size(); | 2227 |
2183 code.push_back(0); | 2228 r.Build(&code[0], &code[0] + code.size()); |
2184 r.Build(&code[0], &code[end]); | |
2185 | 2229 |
2186 // Run the code. | 2230 // Run the code. |
2187 for (int t = 0; t < 10; t++) { | 2231 for (int t = 0; t < 10; t++) { |
2188 module.RandomizeMemory(); | 2232 module.RandomizeMemory(); |
2189 CHECK_EQ(kExpected, r.Call()); | 2233 CHECK_EQ(kExpected, r.Call()); |
2190 | 2234 |
2191 int size = WasmOpcodes::MemSize(result); | 2235 int size = WasmOpcodes::MemSize(result); |
2192 for (int i = 0; i < size; i++) { | 2236 for (int i = 0; i < size; i++) { |
2193 int base = (which + 1) * kElemSize; | 2237 int base = (which + 1) * kElemSize; |
2194 byte expected = module.raw_mem_at<byte>(base + i); | 2238 byte expected = module.raw_mem_at<byte>(base + i); |
(...skipping 14 matching lines...) Expand all Loading... |
2209 TestSignatures sigs; | 2253 TestSignatures sigs; |
2210 TestingModule module; | 2254 TestingModule module; |
2211 WasmFunctionCompiler t1(sigs.i_ii(), &module); | 2255 WasmFunctionCompiler t1(sigs.i_ii(), &module); |
2212 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); | 2256 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); |
2213 t1.CompileAndAdd(); | 2257 t1.CompileAndAdd(); |
2214 | 2258 |
2215 WasmRunner<int32_t> r(&module, MachineType::Int32()); | 2259 WasmRunner<int32_t> r(&module, MachineType::Int32()); |
2216 byte local = r.AllocateLocal(kAstI32); | 2260 byte local = r.AllocateLocal(kAstI32); |
2217 BUILD(r, B2(WASM_SET_LOCAL(local, WASM_I8(99)), | 2261 BUILD(r, B2(WASM_SET_LOCAL(local, WASM_I8(99)), |
2218 WASM_I32_ADD( | 2262 WASM_I32_ADD( |
2219 WASM_CALL_FUNCTION(t1.function_index_, WASM_GET_LOCAL(0), | 2263 WASM_CALL_FUNCTION2(t1.function_index_, WASM_GET_LOCAL(0), |
2220 WASM_GET_LOCAL(0)), | 2264 WASM_GET_LOCAL(0)), |
2221 WASM_CALL_FUNCTION(t1.function_index_, WASM_GET_LOCAL(1), | 2265 WASM_CALL_FUNCTION2(t1.function_index_, WASM_GET_LOCAL(1), |
2222 WASM_GET_LOCAL(local))))); | 2266 WASM_GET_LOCAL(local))))); |
2223 | 2267 |
2224 CHECK_EQ(198, r.Call(0)); | 2268 CHECK_EQ(198, r.Call(0)); |
2225 CHECK_EQ(200, r.Call(1)); | 2269 CHECK_EQ(200, r.Call(1)); |
2226 CHECK_EQ(100, r.Call(-49)); | 2270 CHECK_EQ(100, r.Call(-49)); |
2227 } | 2271 } |
2228 | 2272 |
2229 TEST(Run_Wasm_CountDown_expr) { | 2273 TEST(Run_Wasm_CountDown_expr) { |
2230 WasmRunner<int32_t> r(MachineType::Int32()); | 2274 WasmRunner<int32_t> r(MachineType::Int32()); |
2231 BUILD(r, WASM_LOOP( | 2275 BUILD(r, WASM_LOOP( |
2232 3, WASM_IF(WASM_NOT(WASM_GET_LOCAL(0)), | 2276 3, WASM_IF(WASM_NOT(WASM_GET_LOCAL(0)), |
2233 WASM_BREAKV(0, WASM_GET_LOCAL(0))), | 2277 WASM_BREAKV(1, WASM_GET_LOCAL(0))), |
2234 WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(1))), | 2278 WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(1))), |
2235 WASM_CONTINUE(0))); | 2279 WASM_CONTINUE(0))); |
2236 CHECK_EQ(0, r.Call(1)); | 2280 CHECK_EQ(0, r.Call(1)); |
2237 CHECK_EQ(0, r.Call(10)); | 2281 CHECK_EQ(0, r.Call(10)); |
2238 CHECK_EQ(0, r.Call(100)); | 2282 CHECK_EQ(0, r.Call(100)); |
2239 } | 2283 } |
2240 | 2284 |
2241 | 2285 |
2242 TEST(Run_Wasm_ExprBlock2a) { | 2286 TEST(Run_Wasm_ExprBlock2a) { |
2243 WasmRunner<int32_t> r(MachineType::Int32()); | 2287 WasmRunner<int32_t> r(MachineType::Int32()); |
2244 BUILD(r, B2(WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(0, WASM_I8(1))), WASM_I8(1))); | 2288 BUILD(r, B2(WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_I8(1))), WASM_I8(1))); |
2245 CHECK_EQ(1, r.Call(0)); | 2289 CHECK_EQ(1, r.Call(0)); |
2246 CHECK_EQ(1, r.Call(1)); | 2290 CHECK_EQ(1, r.Call(1)); |
2247 } | 2291 } |
2248 | 2292 |
2249 | 2293 |
2250 TEST(Run_Wasm_ExprBlock2b) { | 2294 TEST(Run_Wasm_ExprBlock2b) { |
2251 WasmRunner<int32_t> r(MachineType::Int32()); | 2295 WasmRunner<int32_t> r(MachineType::Int32()); |
2252 BUILD(r, B2(WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(0, WASM_I8(1))), WASM_I8(2))); | 2296 BUILD(r, B2(WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_I8(1))), WASM_I8(2))); |
2253 CHECK_EQ(2, r.Call(0)); | 2297 CHECK_EQ(2, r.Call(0)); |
2254 CHECK_EQ(1, r.Call(1)); | 2298 CHECK_EQ(1, r.Call(1)); |
2255 } | 2299 } |
2256 | 2300 |
2257 | 2301 |
2258 TEST(Run_Wasm_ExprBlock2c) { | 2302 TEST(Run_Wasm_ExprBlock2c) { |
2259 WasmRunner<int32_t> r(MachineType::Int32()); | 2303 WasmRunner<int32_t> r(MachineType::Int32()); |
2260 BUILD(r, B2(WASM_BRV_IF(0, WASM_I8(1), WASM_GET_LOCAL(0)), WASM_I8(1))); | 2304 BUILD(r, B2(WASM_BRV_IF(0, WASM_I8(1), WASM_GET_LOCAL(0)), WASM_I8(1))); |
2261 CHECK_EQ(1, r.Call(0)); | 2305 CHECK_EQ(1, r.Call(0)); |
2262 CHECK_EQ(1, r.Call(1)); | 2306 CHECK_EQ(1, r.Call(1)); |
2263 } | 2307 } |
2264 | 2308 |
2265 | 2309 |
2266 TEST(Run_Wasm_ExprBlock2d) { | 2310 TEST(Run_Wasm_ExprBlock2d) { |
2267 WasmRunner<int32_t> r(MachineType::Int32()); | 2311 WasmRunner<int32_t> r(MachineType::Int32()); |
2268 BUILD(r, B2(WASM_BRV_IF(0, WASM_I8(1), WASM_GET_LOCAL(0)), WASM_I8(2))); | 2312 BUILD(r, B2(WASM_BRV_IF(0, WASM_I8(1), WASM_GET_LOCAL(0)), WASM_I8(2))); |
2269 CHECK_EQ(2, r.Call(0)); | 2313 CHECK_EQ(2, r.Call(0)); |
2270 CHECK_EQ(1, r.Call(1)); | 2314 CHECK_EQ(1, r.Call(1)); |
2271 } | 2315 } |
2272 | 2316 |
2273 | 2317 |
2274 TEST(Run_Wasm_ExprBlock_ManualSwitch) { | 2318 TEST(Run_Wasm_ExprBlock_ManualSwitch) { |
2275 WasmRunner<int32_t> r(MachineType::Int32()); | 2319 WasmRunner<int32_t> r(MachineType::Int32()); |
2276 BUILD(r, WASM_BLOCK(6, WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(1)), | 2320 BUILD(r, WASM_BLOCK(6, WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(1)), |
2277 WASM_BRV(0, WASM_I8(11))), | 2321 WASM_BRV(1, WASM_I8(11))), |
2278 WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(2)), | 2322 WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(2)), |
2279 WASM_BRV(0, WASM_I8(12))), | 2323 WASM_BRV(1, WASM_I8(12))), |
2280 WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(3)), | 2324 WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(3)), |
2281 WASM_BRV(0, WASM_I8(13))), | 2325 WASM_BRV(1, WASM_I8(13))), |
2282 WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(4)), | 2326 WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(4)), |
2283 WASM_BRV(0, WASM_I8(14))), | 2327 WASM_BRV(1, WASM_I8(14))), |
2284 WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(5)), | 2328 WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(5)), |
2285 WASM_BRV(0, WASM_I8(15))), | 2329 WASM_BRV(1, WASM_I8(15))), |
2286 WASM_I8(99))); | 2330 WASM_I8(99))); |
2287 CHECK_EQ(99, r.Call(0)); | 2331 CHECK_EQ(99, r.Call(0)); |
2288 CHECK_EQ(11, r.Call(1)); | 2332 CHECK_EQ(11, r.Call(1)); |
2289 CHECK_EQ(12, r.Call(2)); | 2333 CHECK_EQ(12, r.Call(2)); |
2290 CHECK_EQ(13, r.Call(3)); | 2334 CHECK_EQ(13, r.Call(3)); |
2291 CHECK_EQ(14, r.Call(4)); | 2335 CHECK_EQ(14, r.Call(4)); |
2292 CHECK_EQ(15, r.Call(5)); | 2336 CHECK_EQ(15, r.Call(5)); |
2293 CHECK_EQ(99, r.Call(6)); | 2337 CHECK_EQ(99, r.Call(6)); |
2294 } | 2338 } |
2295 | 2339 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2331 CHECK_EQ(12, r.Call(1, 0)); | 2375 CHECK_EQ(12, r.Call(1, 0)); |
2332 CHECK_EQ(13, r.Call(0, 1)); | 2376 CHECK_EQ(13, r.Call(0, 1)); |
2333 CHECK_EQ(14, r.Call(0, 0)); | 2377 CHECK_EQ(14, r.Call(0, 0)); |
2334 } | 2378 } |
2335 | 2379 |
2336 | 2380 |
2337 TEST(Run_Wasm_ExprBlock_if) { | 2381 TEST(Run_Wasm_ExprBlock_if) { |
2338 WasmRunner<int32_t> r(MachineType::Int32()); | 2382 WasmRunner<int32_t> r(MachineType::Int32()); |
2339 | 2383 |
2340 BUILD(r, B1(WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_BRV(0, WASM_I8(11)), | 2384 BUILD(r, B1(WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_BRV(0, WASM_I8(11)), |
2341 WASM_BRV(0, WASM_I8(14))))); | 2385 WASM_BRV(1, WASM_I8(14))))); |
2342 | 2386 |
2343 CHECK_EQ(11, r.Call(1)); | 2387 CHECK_EQ(11, r.Call(1)); |
2344 CHECK_EQ(14, r.Call(0)); | 2388 CHECK_EQ(14, r.Call(0)); |
2345 } | 2389 } |
2346 | 2390 |
2347 | 2391 |
2348 TEST(Run_Wasm_ExprBlock_nested_ifs) { | 2392 TEST(Run_Wasm_ExprBlock_nested_ifs) { |
2349 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); | 2393 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); |
2350 | 2394 |
2351 BUILD(r, WASM_BLOCK( | 2395 BUILD(r, WASM_BLOCK( |
2352 1, WASM_IF_ELSE( | 2396 1, WASM_IF_ELSE( |
2353 WASM_GET_LOCAL(0), | 2397 WASM_GET_LOCAL(0), |
2354 WASM_IF_ELSE(WASM_GET_LOCAL(1), WASM_BRV(0, WASM_I8(11)), | 2398 WASM_IF_ELSE(WASM_GET_LOCAL(1), WASM_BRV(0, WASM_I8(11)), |
2355 WASM_BRV(0, WASM_I8(12))), | 2399 WASM_BRV(1, WASM_I8(12))), |
2356 WASM_IF_ELSE(WASM_GET_LOCAL(1), WASM_BRV(0, WASM_I8(13)), | 2400 WASM_IF_ELSE(WASM_GET_LOCAL(1), WASM_BRV(0, WASM_I8(13)), |
2357 WASM_BRV(0, WASM_I8(14)))))); | 2401 WASM_BRV(1, WASM_I8(14)))))); |
2358 | |
2359 | 2402 |
2360 CHECK_EQ(11, r.Call(1, 1)); | 2403 CHECK_EQ(11, r.Call(1, 1)); |
2361 CHECK_EQ(12, r.Call(1, 0)); | 2404 CHECK_EQ(12, r.Call(1, 0)); |
2362 CHECK_EQ(13, r.Call(0, 1)); | 2405 CHECK_EQ(13, r.Call(0, 1)); |
2363 CHECK_EQ(14, r.Call(0, 0)); | 2406 CHECK_EQ(14, r.Call(0, 0)); |
2364 } | 2407 } |
2365 | 2408 |
2366 | 2409 |
2367 TEST(Run_Wasm_ExprLoop_nested_ifs) { | 2410 TEST(Run_Wasm_ExprLoop_nested_ifs) { |
2368 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); | 2411 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); |
2369 | 2412 |
2370 BUILD(r, WASM_LOOP( | 2413 BUILD(r, WASM_LOOP( |
2371 1, WASM_IF_ELSE( | 2414 1, WASM_IF_ELSE( |
2372 WASM_GET_LOCAL(0), | 2415 WASM_GET_LOCAL(0), |
2373 WASM_IF_ELSE(WASM_GET_LOCAL(1), WASM_BRV(1, WASM_I8(11)), | 2416 WASM_IF_ELSE(WASM_GET_LOCAL(1), WASM_BRV(1, WASM_I8(11)), |
2374 WASM_BRV(1, WASM_I8(12))), | 2417 WASM_BRV(3, WASM_I8(12))), |
2375 WASM_IF_ELSE(WASM_GET_LOCAL(1), WASM_BRV(1, WASM_I8(13)), | 2418 WASM_IF_ELSE(WASM_GET_LOCAL(1), WASM_BRV(1, WASM_I8(13)), |
2376 WASM_BRV(1, WASM_I8(14)))))); | 2419 WASM_BRV(3, WASM_I8(14)))))); |
2377 | |
2378 | 2420 |
2379 CHECK_EQ(11, r.Call(1, 1)); | 2421 CHECK_EQ(11, r.Call(1, 1)); |
2380 CHECK_EQ(12, r.Call(1, 0)); | 2422 CHECK_EQ(12, r.Call(1, 0)); |
2381 CHECK_EQ(13, r.Call(0, 1)); | 2423 CHECK_EQ(13, r.Call(0, 1)); |
2382 CHECK_EQ(14, r.Call(0, 0)); | 2424 CHECK_EQ(14, r.Call(0, 0)); |
2383 } | 2425 } |
2384 | 2426 |
2385 | |
2386 TEST(Run_Wasm_SimpleCallIndirect) { | 2427 TEST(Run_Wasm_SimpleCallIndirect) { |
2387 TestSignatures sigs; | 2428 TestSignatures sigs; |
2388 TestingModule module; | 2429 TestingModule module; |
2389 | 2430 |
2390 WasmFunctionCompiler t1(sigs.i_ii(), &module); | 2431 WasmFunctionCompiler t1(sigs.i_ii(), &module); |
2391 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); | 2432 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); |
2392 t1.CompileAndAdd(/*sig_index*/ 1); | 2433 t1.CompileAndAdd(/*sig_index*/ 1); |
2393 | 2434 |
2394 WasmFunctionCompiler t2(sigs.i_ii(), &module); | 2435 WasmFunctionCompiler t2(sigs.i_ii(), &module); |
2395 BUILD(t2, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); | 2436 BUILD(t2, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); |
2396 t2.CompileAndAdd(/*sig_index*/ 1); | 2437 t2.CompileAndAdd(/*sig_index*/ 1); |
2397 | 2438 |
2398 // Signature table. | 2439 // Signature table. |
2399 module.AddSignature(sigs.f_ff()); | 2440 module.AddSignature(sigs.f_ff()); |
2400 module.AddSignature(sigs.i_ii()); | 2441 module.AddSignature(sigs.i_ii()); |
2401 module.AddSignature(sigs.d_dd()); | 2442 module.AddSignature(sigs.d_dd()); |
2402 | 2443 |
2403 // Function table. | 2444 // Function table. |
2404 int table[] = {0, 1}; | 2445 int table[] = {0, 1}; |
2405 module.AddIndirectFunctionTable(table, 2); | 2446 module.AddIndirectFunctionTable(table, 2); |
2406 module.PopulateIndirectFunctionTable(); | 2447 module.PopulateIndirectFunctionTable(); |
2407 | 2448 |
2408 // Builder the caller function. | 2449 // Builder the caller function. |
2409 WasmRunner<int32_t> r(&module, MachineType::Int32()); | 2450 WasmRunner<int32_t> r(&module, MachineType::Int32()); |
2410 BUILD(r, WASM_CALL_INDIRECT(1, WASM_GET_LOCAL(0), WASM_I8(66), WASM_I8(22))); | 2451 BUILD(r, WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_I8(66), WASM_I8(22))); |
2411 | 2452 |
2412 CHECK_EQ(88, r.Call(0)); | 2453 CHECK_EQ(88, r.Call(0)); |
2413 CHECK_EQ(44, r.Call(1)); | 2454 CHECK_EQ(44, r.Call(1)); |
2414 CHECK_TRAP(r.Call(2)); | 2455 CHECK_TRAP(r.Call(2)); |
2415 } | 2456 } |
2416 | 2457 |
2417 | 2458 |
2418 TEST(Run_Wasm_MultipleCallIndirect) { | 2459 TEST(Run_Wasm_MultipleCallIndirect) { |
2419 TestSignatures sigs; | 2460 TestSignatures sigs; |
2420 TestingModule module; | 2461 TestingModule module; |
(...skipping 12 matching lines...) Expand all Loading... |
2433 module.AddSignature(sigs.d_dd()); | 2474 module.AddSignature(sigs.d_dd()); |
2434 | 2475 |
2435 // Function table. | 2476 // Function table. |
2436 int table[] = {0, 1}; | 2477 int table[] = {0, 1}; |
2437 module.AddIndirectFunctionTable(table, 2); | 2478 module.AddIndirectFunctionTable(table, 2); |
2438 module.PopulateIndirectFunctionTable(); | 2479 module.PopulateIndirectFunctionTable(); |
2439 | 2480 |
2440 // Builder the caller function. | 2481 // Builder the caller function. |
2441 WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32(), | 2482 WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32(), |
2442 MachineType::Int32()); | 2483 MachineType::Int32()); |
2443 BUILD(r, | 2484 BUILD(r, WASM_I32_ADD( |
2444 WASM_I32_ADD(WASM_CALL_INDIRECT(1, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), | 2485 WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), |
2445 WASM_GET_LOCAL(2)), | 2486 WASM_GET_LOCAL(2)), |
2446 WASM_CALL_INDIRECT(1, WASM_GET_LOCAL(1), WASM_GET_LOCAL(2), | 2487 WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(1), WASM_GET_LOCAL(2), |
2447 WASM_GET_LOCAL(0)))); | 2488 WASM_GET_LOCAL(0)))); |
2448 | 2489 |
2449 CHECK_EQ(5, r.Call(0, 1, 2)); | 2490 CHECK_EQ(5, r.Call(0, 1, 2)); |
2450 CHECK_EQ(19, r.Call(0, 1, 9)); | 2491 CHECK_EQ(19, r.Call(0, 1, 9)); |
2451 CHECK_EQ(1, r.Call(1, 0, 2)); | 2492 CHECK_EQ(1, r.Call(1, 0, 2)); |
2452 CHECK_EQ(1, r.Call(1, 0, 9)); | 2493 CHECK_EQ(1, r.Call(1, 0, 9)); |
2453 | 2494 |
2454 CHECK_TRAP(r.Call(0, 2, 1)); | 2495 CHECK_TRAP(r.Call(0, 2, 1)); |
2455 CHECK_TRAP(r.Call(1, 2, 0)); | 2496 CHECK_TRAP(r.Call(1, 2, 0)); |
2456 CHECK_TRAP(r.Call(2, 0, 1)); | 2497 CHECK_TRAP(r.Call(2, 0, 1)); |
2457 CHECK_TRAP(r.Call(2, 1, 0)); | 2498 CHECK_TRAP(r.Call(2, 1, 0)); |
2458 } | 2499 } |
2459 | 2500 |
2460 TEST(Run_Wasm_CallIndirect_NoTable) { | 2501 TEST(Run_Wasm_CallIndirect_NoTable) { |
2461 TestSignatures sigs; | 2502 TestSignatures sigs; |
2462 TestingModule module; | 2503 TestingModule module; |
2463 | 2504 |
2464 // One function. | 2505 // One function. |
2465 WasmFunctionCompiler t1(sigs.i_ii(), &module); | 2506 WasmFunctionCompiler t1(sigs.i_ii(), &module); |
2466 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); | 2507 BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); |
2467 t1.CompileAndAdd(/*sig_index*/ 1); | 2508 t1.CompileAndAdd(/*sig_index*/ 1); |
2468 | 2509 |
2469 // Signature table. | 2510 // Signature table. |
2470 module.AddSignature(sigs.f_ff()); | 2511 module.AddSignature(sigs.f_ff()); |
2471 module.AddSignature(sigs.i_ii()); | 2512 module.AddSignature(sigs.i_ii()); |
2472 | 2513 |
2473 // Builder the caller function. | 2514 // Builder the caller function. |
2474 WasmRunner<int32_t> r(&module, MachineType::Int32()); | 2515 WasmRunner<int32_t> r(&module, MachineType::Int32()); |
2475 BUILD(r, WASM_CALL_INDIRECT(1, WASM_GET_LOCAL(0), WASM_I8(66), WASM_I8(22))); | 2516 BUILD(r, WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_I8(66), WASM_I8(22))); |
2476 | 2517 |
2477 CHECK_TRAP(r.Call(0)); | 2518 CHECK_TRAP(r.Call(0)); |
2478 CHECK_TRAP(r.Call(1)); | 2519 CHECK_TRAP(r.Call(1)); |
2479 CHECK_TRAP(r.Call(2)); | 2520 CHECK_TRAP(r.Call(2)); |
2480 } | 2521 } |
2481 | 2522 |
2482 TEST(Run_Wasm_F32Floor) { | 2523 TEST(Run_Wasm_F32Floor) { |
2483 WasmRunner<float> r(MachineType::Float32()); | 2524 WasmRunner<float> r(MachineType::Float32()); |
2484 BUILD(r, WASM_F32_FLOOR(WASM_GET_LOCAL(0))); | 2525 BUILD(r, WASM_F32_FLOOR(WASM_GET_LOCAL(0))); |
2485 | 2526 |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2786 TestingModule module; | 2827 TestingModule module; |
2787 FunctionSig* sig = sigs.many(&zone, kAstStmt, param, num_params); | 2828 FunctionSig* sig = sigs.many(&zone, kAstStmt, param, num_params); |
2788 | 2829 |
2789 module.AddSignature(sig); | 2830 module.AddSignature(sig); |
2790 module.AddSignature(sig); | 2831 module.AddSignature(sig); |
2791 module.AddIndirectFunctionTable(nullptr, 0); | 2832 module.AddIndirectFunctionTable(nullptr, 0); |
2792 | 2833 |
2793 WasmFunctionCompiler t(sig, &module); | 2834 WasmFunctionCompiler t(sig, &module); |
2794 | 2835 |
2795 std::vector<byte> code; | 2836 std::vector<byte> code; |
2796 ADD_CODE(code, kExprCallIndirect, 1); | |
2797 ADD_CODE(code, kExprI8Const, 0); | 2837 ADD_CODE(code, kExprI8Const, 0); |
2798 for (byte p = 0; p < num_params; p++) { | 2838 for (byte p = 0; p < num_params; p++) { |
2799 ADD_CODE(code, kExprGetLocal, p); | 2839 ADD_CODE(code, kExprGetLocal, p); |
2800 } | 2840 } |
| 2841 ADD_CODE(code, kExprCallIndirect, static_cast<byte>(num_params), 1); |
2801 | 2842 |
2802 t.Build(&code[0], &code[0] + code.size()); | 2843 t.Build(&code[0], &code[0] + code.size()); |
2803 t.Compile(); | 2844 t.Compile(); |
2804 } | 2845 } |
2805 } | 2846 } |
2806 | 2847 |
2807 | 2848 |
2808 TEST(Compile_Wasm_CallIndirect_Many_i32) { CompileCallIndirectMany(kAstI32); } | 2849 TEST(Compile_Wasm_CallIndirect_Many_i32) { CompileCallIndirectMany(kAstI32); } |
2809 | 2850 |
2810 | 2851 |
(...skipping 11 matching lines...) Expand all Loading... |
2822 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); | 2863 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); |
2823 BUILD(r, WASM_I32_REMS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)), WASM_ZERO); | 2864 BUILD(r, WASM_I32_REMS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)), WASM_ZERO); |
2824 const int32_t kMin = std::numeric_limits<int32_t>::min(); | 2865 const int32_t kMin = std::numeric_limits<int32_t>::min(); |
2825 CHECK_EQ(0, r.Call(133, 100)); | 2866 CHECK_EQ(0, r.Call(133, 100)); |
2826 CHECK_EQ(0, r.Call(kMin, -1)); | 2867 CHECK_EQ(0, r.Call(kMin, -1)); |
2827 CHECK_EQ(0, r.Call(0, 1)); | 2868 CHECK_EQ(0, r.Call(0, 1)); |
2828 CHECK_TRAP(r.Call(100, 0)); | 2869 CHECK_TRAP(r.Call(100, 0)); |
2829 CHECK_TRAP(r.Call(-1001, 0)); | 2870 CHECK_TRAP(r.Call(-1001, 0)); |
2830 CHECK_TRAP(r.Call(kMin, 0)); | 2871 CHECK_TRAP(r.Call(kMin, 0)); |
2831 } | 2872 } |
OLD | NEW |