diff options
Diffstat (limited to 'deps/v8/test/cctest/wasm')
-rw-r--r-- | deps/v8/test/cctest/wasm/OWNERS | 1 | ||||
-rw-r--r-- | deps/v8/test/cctest/wasm/test-run-wasm-64.cc | 473 | ||||
-rw-r--r-- | deps/v8/test/cctest/wasm/test-run-wasm-asmjs.cc | 177 | ||||
-rw-r--r-- | deps/v8/test/cctest/wasm/test-run-wasm-interpreter.cc | 102 | ||||
-rw-r--r-- | deps/v8/test/cctest/wasm/test-run-wasm-js.cc | 98 | ||||
-rw-r--r-- | deps/v8/test/cctest/wasm/test-run-wasm-module.cc | 168 | ||||
-rw-r--r-- | deps/v8/test/cctest/wasm/test-run-wasm-relocation.cc | 28 | ||||
-rw-r--r-- | deps/v8/test/cctest/wasm/test-run-wasm-simd-lowering.cc | 262 | ||||
-rw-r--r-- | deps/v8/test/cctest/wasm/test-run-wasm-simd.cc | 400 | ||||
-rw-r--r-- | deps/v8/test/cctest/wasm/test-run-wasm.cc | 1431 | ||||
-rw-r--r-- | deps/v8/test/cctest/wasm/test-wasm-breakpoints.cc | 70 | ||||
-rw-r--r-- | deps/v8/test/cctest/wasm/test-wasm-stack.cc | 62 | ||||
-rw-r--r-- | deps/v8/test/cctest/wasm/test-wasm-trap-position.cc | 54 | ||||
-rw-r--r-- | deps/v8/test/cctest/wasm/wasm-run-utils.h | 675 |
14 files changed, 2279 insertions, 1722 deletions
diff --git a/deps/v8/test/cctest/wasm/OWNERS b/deps/v8/test/cctest/wasm/OWNERS index eda8deabfd..59b1e7dc7a 100644 --- a/deps/v8/test/cctest/wasm/OWNERS +++ b/deps/v8/test/cctest/wasm/OWNERS @@ -1,5 +1,6 @@ ahaas@chromium.org bradnelson@chromium.org +clemensh@chromium.org mtrofin@chromium.org rossberg@chromium.org titzer@chromium.org diff --git a/deps/v8/test/cctest/wasm/test-run-wasm-64.cc b/deps/v8/test/cctest/wasm/test-run-wasm-64.cc index e9a2d2da47..4571364980 100644 --- a/deps/v8/test/cctest/wasm/test-run-wasm-64.cc +++ b/deps/v8/test/cctest/wasm/test-run-wasm-64.cc @@ -120,7 +120,7 @@ WASM_EXEC_TEST(I64Const_many) { WASM_EXEC_TEST(Return_I64) { REQUIRE(I64Return); - WasmRunner<int64_t> r(execution_mode, MachineType::Int64()); + WasmRunner<int64_t, int64_t> r(execution_mode); BUILD(r, WASM_RETURN1(WASM_GET_LOCAL(0))); @@ -129,29 +129,51 @@ WASM_EXEC_TEST(Return_I64) { WASM_EXEC_TEST(I64Add) { REQUIRE(I64Add); - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT64_INPUTS(i) { FOR_INT64_INPUTS(j) { CHECK_EQ(*i + *j, r.Call(*i, *j)); } } } +// The i64 add and subtract regression tests need a 64-bit value with a non-zero +// upper half. This upper half was clobbering eax, leading to the function +// returning 1 rather than 0. +const int64_t kHasBit33On = 0x100000000; + +WASM_EXEC_TEST(Regress5800_Add) { + REQUIRE(I64Add); + WasmRunner<int32_t> r(execution_mode); + BUILD(r, WASM_BLOCK(WASM_BR_IF(0, WASM_I64_EQZ(WASM_I64_ADD( + WASM_I64V(0), WASM_I64V(kHasBit33On)))), + WASM_RETURN1(WASM_I32V(0))), + WASM_I32V(0)); + CHECK_EQ(0, r.Call()); +} + WASM_EXEC_TEST(I64Sub) { REQUIRE(I64Sub); - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT64_INPUTS(i) { FOR_INT64_INPUTS(j) { CHECK_EQ(*i - *j, r.Call(*i, *j)); } } } +WASM_EXEC_TEST(Regress5800_Sub) { + REQUIRE(I64Sub); + WasmRunner<int32_t> r(execution_mode); + BUILD(r, WASM_BLOCK(WASM_BR_IF(0, WASM_I64_EQZ(WASM_I64_SUB( + WASM_I64V(0), WASM_I64V(kHasBit33On)))), + WASM_RETURN1(WASM_I32V(0))), + WASM_I32V(0)); + CHECK_EQ(0, r.Call()); +} + WASM_EXEC_TEST(I64AddUseOnlyLowWord) { REQUIRE(I64Add); REQUIRE(I32ConvertI64); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I32_CONVERT_I64( WASM_I64_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)))); FOR_INT64_INPUTS(i) { @@ -164,8 +186,7 @@ WASM_EXEC_TEST(I64AddUseOnlyLowWord) { WASM_EXEC_TEST(I64SubUseOnlyLowWord) { REQUIRE(I64Sub); REQUIRE(I32ConvertI64); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I32_CONVERT_I64( WASM_I64_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)))); FOR_INT64_INPUTS(i) { @@ -178,8 +199,7 @@ WASM_EXEC_TEST(I64SubUseOnlyLowWord) { WASM_EXEC_TEST(I64MulUseOnlyLowWord) { REQUIRE(I64Mul); REQUIRE(I32ConvertI64); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I32_CONVERT_I64( WASM_I64_MUL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)))); FOR_INT64_INPUTS(i) { @@ -192,8 +212,7 @@ WASM_EXEC_TEST(I64MulUseOnlyLowWord) { WASM_EXEC_TEST(I64ShlUseOnlyLowWord) { REQUIRE(I64Shl); REQUIRE(I32ConvertI64); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I32_CONVERT_I64( WASM_I64_SHL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)))); FOR_INT64_INPUTS(i) { @@ -207,8 +226,7 @@ WASM_EXEC_TEST(I64ShlUseOnlyLowWord) { WASM_EXEC_TEST(I64ShrUseOnlyLowWord) { REQUIRE(I64ShrU); REQUIRE(I32ConvertI64); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I32_CONVERT_I64( WASM_I64_SHR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)))); FOR_UINT64_INPUTS(i) { @@ -222,8 +240,7 @@ WASM_EXEC_TEST(I64ShrUseOnlyLowWord) { WASM_EXEC_TEST(I64SarUseOnlyLowWord) { REQUIRE(I64ShrS); REQUIRE(I32ConvertI64); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I32_CONVERT_I64( WASM_I64_SAR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)))); FOR_INT64_INPUTS(i) { @@ -234,10 +251,9 @@ WASM_EXEC_TEST(I64SarUseOnlyLowWord) { } } -WASM_EXEC_TEST(I64DivS) { +WASM_EXEC_TEST_WITH_TRAP(I64DivS) { REQUIRE(I64DivS); - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_DIVS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT64_INPUTS(i) { FOR_INT64_INPUTS(j) { @@ -252,10 +268,9 @@ WASM_EXEC_TEST(I64DivS) { } } -WASM_EXEC_TEST(I64DivS_Trap) { +WASM_EXEC_TEST_WITH_TRAP(I64DivS_Trap) { REQUIRE(I64DivS); - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_DIVS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); CHECK_EQ(0, r.Call(asi64(0), asi64(100))); CHECK_TRAP64(r.Call(asi64(100), asi64(0))); @@ -264,10 +279,10 @@ WASM_EXEC_TEST(I64DivS_Trap) { CHECK_TRAP64(r.Call(std::numeric_limits<int64_t>::min(), asi64(0))); } -WASM_EXEC_TEST(I64DivS_Byzero_Const) { +WASM_EXEC_TEST_WITH_TRAP(I64DivS_Byzero_Const) { REQUIRE(I64DivS); for (int8_t denom = -2; denom < 8; denom++) { - WasmRunner<int64_t> r(execution_mode, MachineType::Int64()); + WasmRunner<int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_DIVS(WASM_GET_LOCAL(0), WASM_I64V_1(denom))); for (int64_t val = -7; val < 8; val++) { if (denom == 0) { @@ -279,10 +294,9 @@ WASM_EXEC_TEST(I64DivS_Byzero_Const) { } } -WASM_EXEC_TEST(I64DivU) { +WASM_EXEC_TEST_WITH_TRAP(I64DivU) { REQUIRE(I64DivU); - WasmRunner<uint64_t> r(execution_mode, MachineType::Uint64(), - MachineType::Uint64()); + WasmRunner<uint64_t, uint64_t, uint64_t> r(execution_mode); BUILD(r, WASM_I64_DIVU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_UINT64_INPUTS(i) { FOR_UINT64_INPUTS(j) { @@ -295,21 +309,20 @@ WASM_EXEC_TEST(I64DivU) { } } -WASM_EXEC_TEST(I64DivU_Trap) { +WASM_EXEC_TEST_WITH_TRAP(I64DivU_Trap) { REQUIRE(I64DivU); - WasmRunner<uint64_t> r(execution_mode, MachineType::Uint64(), - MachineType::Uint64()); + WasmRunner<uint64_t, uint64_t, uint64_t> r(execution_mode); BUILD(r, WASM_I64_DIVU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - CHECK_EQ(0u, r.Call(asu64(0), asu64(100))); + CHECK_EQ(0, r.Call(asu64(0), asu64(100))); CHECK_TRAP64(r.Call(asu64(100), asu64(0))); CHECK_TRAP64(r.Call(asu64(1001), asu64(0))); CHECK_TRAP64(r.Call(std::numeric_limits<uint64_t>::max(), asu64(0))); } -WASM_EXEC_TEST(I64DivU_Byzero_Const) { +WASM_EXEC_TEST_WITH_TRAP(I64DivU_Byzero_Const) { REQUIRE(I64DivU); for (uint64_t denom = 0xfffffffffffffffe; denom < 8; denom++) { - WasmRunner<uint64_t> r(execution_mode, MachineType::Uint64()); + WasmRunner<uint64_t, uint64_t> r(execution_mode); BUILD(r, WASM_I64_DIVU(WASM_GET_LOCAL(0), WASM_I64V_1(denom))); for (uint64_t val = 0xfffffffffffffff0; val < 8; val++) { @@ -322,10 +335,9 @@ WASM_EXEC_TEST(I64DivU_Byzero_Const) { } } -WASM_EXEC_TEST(I64RemS) { +WASM_EXEC_TEST_WITH_TRAP(I64RemS) { REQUIRE(I64RemS); - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_REMS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT64_INPUTS(i) { FOR_INT64_INPUTS(j) { @@ -338,10 +350,9 @@ WASM_EXEC_TEST(I64RemS) { } } -WASM_EXEC_TEST(I64RemS_Trap) { +WASM_EXEC_TEST_WITH_TRAP(I64RemS_Trap) { REQUIRE(I64RemS); - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_REMS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); CHECK_EQ(33, r.Call(asi64(133), asi64(100))); CHECK_EQ(0, r.Call(std::numeric_limits<int64_t>::min(), asi64(-1))); @@ -350,10 +361,9 @@ WASM_EXEC_TEST(I64RemS_Trap) { CHECK_TRAP64(r.Call(std::numeric_limits<int64_t>::min(), asi64(0))); } -WASM_EXEC_TEST(I64RemU) { +WASM_EXEC_TEST_WITH_TRAP(I64RemU) { REQUIRE(I64RemU); - WasmRunner<uint64_t> r(execution_mode, MachineType::Uint64(), - MachineType::Uint64()); + WasmRunner<uint64_t, uint64_t, uint64_t> r(execution_mode); BUILD(r, WASM_I64_REMU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_UINT64_INPUTS(i) { FOR_UINT64_INPUTS(j) { @@ -366,12 +376,11 @@ WASM_EXEC_TEST(I64RemU) { } } -WASM_EXEC_TEST(I64RemU_Trap) { +WASM_EXEC_TEST_WITH_TRAP(I64RemU_Trap) { REQUIRE(I64RemU); - WasmRunner<uint64_t> r(execution_mode, MachineType::Uint64(), - MachineType::Uint64()); + WasmRunner<uint64_t, uint64_t, uint64_t> r(execution_mode); BUILD(r, WASM_I64_REMU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - CHECK_EQ(17u, r.Call(asu64(217), asu64(100))); + CHECK_EQ(17, r.Call(asu64(217), asu64(100))); CHECK_TRAP64(r.Call(asu64(100), asu64(0))); CHECK_TRAP64(r.Call(asu64(1001), asu64(0))); CHECK_TRAP64(r.Call(std::numeric_limits<uint64_t>::max(), asu64(0))); @@ -379,8 +388,7 @@ WASM_EXEC_TEST(I64RemU_Trap) { WASM_EXEC_TEST(I64And) { REQUIRE(I64And); - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_AND(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT64_INPUTS(i) { FOR_INT64_INPUTS(j) { CHECK_EQ((*i) & (*j), r.Call(*i, *j)); } @@ -389,8 +397,7 @@ WASM_EXEC_TEST(I64And) { WASM_EXEC_TEST(I64Ior) { REQUIRE(I64Ior); - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_IOR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT64_INPUTS(i) { FOR_INT64_INPUTS(j) { CHECK_EQ((*i) | (*j), r.Call(*i, *j)); } @@ -399,8 +406,7 @@ WASM_EXEC_TEST(I64Ior) { WASM_EXEC_TEST(I64Xor) { REQUIRE(I64Xor); - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_XOR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT64_INPUTS(i) { FOR_INT64_INPUTS(j) { CHECK_EQ((*i) ^ (*j), r.Call(*i, *j)); } @@ -410,8 +416,7 @@ WASM_EXEC_TEST(I64Xor) { WASM_EXEC_TEST(I64Shl) { REQUIRE(I64Shl); { - WasmRunner<uint64_t> r(execution_mode, MachineType::Uint64(), - MachineType::Uint64()); + WasmRunner<uint64_t, uint64_t, uint64_t> r(execution_mode); BUILD(r, WASM_I64_SHL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_UINT64_INPUTS(i) { @@ -422,22 +427,22 @@ WASM_EXEC_TEST(I64Shl) { } } { - WasmRunner<uint64_t> r(execution_mode, MachineType::Int64()); + WasmRunner<uint64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_SHL(WASM_GET_LOCAL(0), WASM_I64V_1(0))); FOR_UINT64_INPUTS(i) { CHECK_EQ(*i << 0, r.Call(*i)); } } { - WasmRunner<uint64_t> r(execution_mode, MachineType::Int64()); + WasmRunner<uint64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_SHL(WASM_GET_LOCAL(0), WASM_I64V_1(32))); FOR_UINT64_INPUTS(i) { CHECK_EQ(*i << 32, r.Call(*i)); } } { - WasmRunner<uint64_t> r(execution_mode, MachineType::Int64()); + WasmRunner<uint64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_SHL(WASM_GET_LOCAL(0), WASM_I64V_1(20))); FOR_UINT64_INPUTS(i) { CHECK_EQ(*i << 20, r.Call(*i)); } } { - WasmRunner<uint64_t> r(execution_mode, MachineType::Int64()); + WasmRunner<uint64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_SHL(WASM_GET_LOCAL(0), WASM_I64V_1(40))); FOR_UINT64_INPUTS(i) { CHECK_EQ(*i << 40, r.Call(*i)); } } @@ -446,8 +451,7 @@ WASM_EXEC_TEST(I64Shl) { WASM_EXEC_TEST(I64ShrU) { REQUIRE(I64ShrU); { - WasmRunner<uint64_t> r(execution_mode, MachineType::Uint64(), - MachineType::Uint64()); + WasmRunner<uint64_t, uint64_t, uint64_t> r(execution_mode); BUILD(r, WASM_I64_SHR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_UINT64_INPUTS(i) { @@ -458,22 +462,22 @@ WASM_EXEC_TEST(I64ShrU) { } } { - WasmRunner<uint64_t> r(execution_mode, MachineType::Int64()); + WasmRunner<uint64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_SHR(WASM_GET_LOCAL(0), WASM_I64V_1(0))); FOR_UINT64_INPUTS(i) { CHECK_EQ(*i >> 0, r.Call(*i)); } } { - WasmRunner<uint64_t> r(execution_mode, MachineType::Int64()); + WasmRunner<uint64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_SHR(WASM_GET_LOCAL(0), WASM_I64V_1(32))); FOR_UINT64_INPUTS(i) { CHECK_EQ(*i >> 32, r.Call(*i)); } } { - WasmRunner<uint64_t> r(execution_mode, MachineType::Int64()); + WasmRunner<uint64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_SHR(WASM_GET_LOCAL(0), WASM_I64V_1(20))); FOR_UINT64_INPUTS(i) { CHECK_EQ(*i >> 20, r.Call(*i)); } } { - WasmRunner<uint64_t> r(execution_mode, MachineType::Int64()); + WasmRunner<uint64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_SHR(WASM_GET_LOCAL(0), WASM_I64V_1(40))); FOR_UINT64_INPUTS(i) { CHECK_EQ(*i >> 40, r.Call(*i)); } } @@ -482,8 +486,7 @@ WASM_EXEC_TEST(I64ShrU) { WASM_EXEC_TEST(I64ShrS) { REQUIRE(I64ShrS); { - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_SAR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT64_INPUTS(i) { @@ -494,22 +497,22 @@ WASM_EXEC_TEST(I64ShrS) { } } { - WasmRunner<int64_t> r(execution_mode, MachineType::Int64()); + WasmRunner<int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_SAR(WASM_GET_LOCAL(0), WASM_I64V_1(0))); FOR_INT64_INPUTS(i) { CHECK_EQ(*i >> 0, r.Call(*i)); } } { - WasmRunner<int64_t> r(execution_mode, MachineType::Int64()); + WasmRunner<int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_SAR(WASM_GET_LOCAL(0), WASM_I64V_1(32))); FOR_INT64_INPUTS(i) { CHECK_EQ(*i >> 32, r.Call(*i)); } } { - WasmRunner<int64_t> r(execution_mode, MachineType::Int64()); + WasmRunner<int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_SAR(WASM_GET_LOCAL(0), WASM_I64V_1(20))); FOR_INT64_INPUTS(i) { CHECK_EQ(*i >> 20, r.Call(*i)); } } { - WasmRunner<int64_t> r(execution_mode, MachineType::Int64()); + WasmRunner<int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_SAR(WASM_GET_LOCAL(0), WASM_I64V_1(40))); FOR_INT64_INPUTS(i) { CHECK_EQ(*i >> 40, r.Call(*i)); } } @@ -517,8 +520,7 @@ WASM_EXEC_TEST(I64ShrS) { WASM_EXEC_TEST(I64Eq) { REQUIRE(I64Eq); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_EQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT64_INPUTS(i) { FOR_INT64_INPUTS(j) { CHECK_EQ(*i == *j ? 1 : 0, r.Call(*i, *j)); } @@ -527,8 +529,7 @@ WASM_EXEC_TEST(I64Eq) { WASM_EXEC_TEST(I64Ne) { REQUIRE(I64Ne); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_NE(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT64_INPUTS(i) { FOR_INT64_INPUTS(j) { CHECK_EQ(*i != *j ? 1 : 0, r.Call(*i, *j)); } @@ -537,8 +538,7 @@ WASM_EXEC_TEST(I64Ne) { WASM_EXEC_TEST(I64LtS) { REQUIRE(I64LtS); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_LTS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT64_INPUTS(i) { FOR_INT64_INPUTS(j) { CHECK_EQ(*i < *j ? 1 : 0, r.Call(*i, *j)); } @@ -547,8 +547,7 @@ WASM_EXEC_TEST(I64LtS) { WASM_EXEC_TEST(I64LeS) { REQUIRE(I64LeS); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_LES(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT64_INPUTS(i) { FOR_INT64_INPUTS(j) { CHECK_EQ(*i <= *j ? 1 : 0, r.Call(*i, *j)); } @@ -557,8 +556,7 @@ WASM_EXEC_TEST(I64LeS) { WASM_EXEC_TEST(I64LtU) { REQUIRE(I64LtU); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_LTU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_UINT64_INPUTS(i) { FOR_UINT64_INPUTS(j) { CHECK_EQ(*i < *j ? 1 : 0, r.Call(*i, *j)); } @@ -567,8 +565,7 @@ WASM_EXEC_TEST(I64LtU) { WASM_EXEC_TEST(I64LeU) { REQUIRE(I64LeU); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_LEU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_UINT64_INPUTS(i) { FOR_UINT64_INPUTS(j) { CHECK_EQ(*i <= *j ? 1 : 0, r.Call(*i, *j)); } @@ -577,8 +574,7 @@ WASM_EXEC_TEST(I64LeU) { WASM_EXEC_TEST(I64GtS) { REQUIRE(I64GtS); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_GTS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT64_INPUTS(i) { FOR_INT64_INPUTS(j) { CHECK_EQ(*i > *j ? 1 : 0, r.Call(*i, *j)); } @@ -587,8 +583,7 @@ WASM_EXEC_TEST(I64GtS) { WASM_EXEC_TEST(I64GeS) { REQUIRE(I64GeS); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_GES(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT64_INPUTS(i) { FOR_INT64_INPUTS(j) { CHECK_EQ(*i >= *j ? 1 : 0, r.Call(*i, *j)); } @@ -597,8 +592,7 @@ WASM_EXEC_TEST(I64GeS) { WASM_EXEC_TEST(I64GtU) { REQUIRE(I64GtU); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_GTU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_UINT64_INPUTS(i) { FOR_UINT64_INPUTS(j) { CHECK_EQ(*i > *j ? 1 : 0, r.Call(*i, *j)); } @@ -607,8 +601,7 @@ WASM_EXEC_TEST(I64GtU) { WASM_EXEC_TEST(I64GeU) { REQUIRE(I64GeU); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_GEU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_UINT64_INPUTS(i) { FOR_UINT64_INPUTS(j) { CHECK_EQ(*i >= *j ? 1 : 0, r.Call(*i, *j)); } @@ -626,14 +619,14 @@ WASM_EXEC_TEST(I32ConvertI64) { WASM_EXEC_TEST(I64SConvertI32) { REQUIRE(I64SConvertI32); - WasmRunner<int64_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int64_t, int32_t> r(execution_mode); BUILD(r, WASM_I64_SCONVERT_I32(WASM_GET_LOCAL(0))); FOR_INT32_INPUTS(i) { CHECK_EQ(static_cast<int64_t>(*i), r.Call(*i)); } } WASM_EXEC_TEST(I64UConvertI32) { REQUIRE(I64UConvertI32); - WasmRunner<int64_t> r(execution_mode, MachineType::Uint32()); + WasmRunner<int64_t, uint32_t> r(execution_mode); BUILD(r, WASM_I64_UCONVERT_I32(WASM_GET_LOCAL(0))); FOR_UINT32_INPUTS(i) { CHECK_EQ(static_cast<int64_t>(*i), r.Call(*i)); } } @@ -648,7 +641,7 @@ WASM_EXEC_TEST(I64Popcnt) { {26, 0x1123456782345678}, {38, 0xffedcba09edcba09}}; - WasmRunner<int64_t> r(execution_mode, MachineType::Uint64()); + WasmRunner<int64_t, uint64_t> r(execution_mode); BUILD(r, WASM_I64_POPCNT(WASM_GET_LOCAL(0))); for (size_t i = 0; i < arraysize(values); i++) { CHECK_EQ(values[i].expected, r.Call(values[i].input)); @@ -657,7 +650,7 @@ WASM_EXEC_TEST(I64Popcnt) { WASM_EXEC_TEST(F32SConvertI64) { REQUIRE(F32SConvertI64); - WasmRunner<float> r(execution_mode, MachineType::Int64()); + WasmRunner<float, int64_t> r(execution_mode); BUILD(r, WASM_F32_SCONVERT_I64(WASM_GET_LOCAL(0))); FOR_INT64_INPUTS(i) { CHECK_FLOAT_EQ(static_cast<float>(*i), r.Call(*i)); } } @@ -743,7 +736,7 @@ WASM_EXEC_TEST(F32UConvertI64) { {0x8000008000000001, 0x5f000001}, {0x8000000000000400, 0x5f000000}, {0x8000000000000401, 0x5f000000}}; - WasmRunner<float> r(execution_mode, MachineType::Uint64()); + WasmRunner<float, uint64_t> r(execution_mode); BUILD(r, WASM_F32_UCONVERT_I64(WASM_GET_LOCAL(0))); for (size_t i = 0; i < arraysize(values); i++) { CHECK_EQ(bit_cast<float>(values[i].expected), r.Call(values[i].input)); @@ -752,7 +745,7 @@ WASM_EXEC_TEST(F32UConvertI64) { WASM_EXEC_TEST(F64SConvertI64) { REQUIRE(F64SConvertI64); - WasmRunner<double> r(execution_mode, MachineType::Int64()); + WasmRunner<double, int64_t> r(execution_mode); BUILD(r, WASM_F64_SCONVERT_I64(WASM_GET_LOCAL(0))); FOR_INT64_INPUTS(i) { CHECK_DOUBLE_EQ(static_cast<double>(*i), r.Call(*i)); } } @@ -837,15 +830,15 @@ WASM_EXEC_TEST(F64UConvertI64) { {0x8000008000000001, 0x43e0000010000000}, {0x8000000000000400, 0x43e0000000000000}, {0x8000000000000401, 0x43e0000000000001}}; - WasmRunner<double> r(execution_mode, MachineType::Uint64()); + WasmRunner<double, uint64_t> r(execution_mode); BUILD(r, WASM_F64_UCONVERT_I64(WASM_GET_LOCAL(0))); for (size_t i = 0; i < arraysize(values); i++) { CHECK_EQ(bit_cast<double>(values[i].expected), r.Call(values[i].input)); } } -WASM_EXEC_TEST(I64SConvertF32a) { - WasmRunner<int64_t> r(execution_mode, MachineType::Float32()); +WASM_EXEC_TEST_WITH_TRAP(I64SConvertF32a) { + WasmRunner<int64_t, float> r(execution_mode); BUILD(r, WASM_I64_SCONVERT_F32(WASM_GET_LOCAL(0))); FOR_FLOAT32_INPUTS(i) { @@ -858,8 +851,8 @@ WASM_EXEC_TEST(I64SConvertF32a) { } } -WASM_EXEC_TEST(I64SConvertF64a) { - WasmRunner<int64_t> r(execution_mode, MachineType::Float64()); +WASM_EXEC_TEST_WITH_TRAP(I64SConvertF64a) { + WasmRunner<int64_t, double> r(execution_mode); BUILD(r, WASM_I64_SCONVERT_F64(WASM_GET_LOCAL(0))); FOR_FLOAT64_INPUTS(i) { @@ -872,8 +865,8 @@ WASM_EXEC_TEST(I64SConvertF64a) { } } -WASM_EXEC_TEST(I64UConvertF32a) { - WasmRunner<uint64_t> r(execution_mode, MachineType::Float32()); +WASM_EXEC_TEST_WITH_TRAP(I64UConvertF32a) { + WasmRunner<uint64_t, float> r(execution_mode); BUILD(r, WASM_I64_UCONVERT_F32(WASM_GET_LOCAL(0))); FOR_FLOAT32_INPUTS(i) { @@ -886,8 +879,8 @@ WASM_EXEC_TEST(I64UConvertF32a) { } } -WASM_EXEC_TEST(I64UConvertF64a) { - WasmRunner<uint64_t> r(execution_mode, MachineType::Float64()); +WASM_EXEC_TEST_WITH_TRAP(I64UConvertF64a) { + WasmRunner<uint64_t, double> r(execution_mode); BUILD(r, WASM_I64_UCONVERT_F64(WASM_GET_LOCAL(0))); FOR_FLOAT64_INPUTS(i) { @@ -901,28 +894,23 @@ WASM_EXEC_TEST(I64UConvertF64a) { } WASM_EXEC_TEST(CallI64Parameter) { - // Build the target function. - LocalType param_types[20]; - for (int i = 0; i < 20; i++) param_types[i] = kAstI64; - param_types[3] = kAstI32; - param_types[4] = kAstI32; + ValueType param_types[20]; + for (int i = 0; i < 20; i++) param_types[i] = kWasmI64; + param_types[3] = kWasmI32; + param_types[4] = kWasmI32; FunctionSig sig(1, 19, param_types); for (int i = 0; i < 19; i++) { - TestingModule module(execution_mode); - WasmFunctionCompiler t(&sig, &module); - if (i == 2 || i == 3) { - continue; - } else { - BUILD(t, WASM_GET_LOCAL(i)); - } - uint32_t index = t.CompileAndAdd(); + if (i == 2 || i == 3) continue; + WasmRunner<int32_t> r(execution_mode); + // Build the target function. + WasmFunctionCompiler& t = r.NewFunction(&sig); + BUILD(t, WASM_GET_LOCAL(i)); // Build the calling function. - WasmRunner<int32_t> r(&module); BUILD( r, WASM_I32_CONVERT_I64(WASM_CALL_FUNCTION( - index, WASM_I64V_9(0xbcd12340000000b), + t.function_index(), WASM_I64V_9(0xbcd12340000000b), WASM_I64V_9(0xbcd12340000000c), WASM_I32V_1(0xd), WASM_I32_CONVERT_I64(WASM_I64V_9(0xbcd12340000000e)), WASM_I64V_9(0xbcd12340000000f), WASM_I64V_10(0xbcd1234000000010), @@ -947,8 +935,7 @@ void TestI64Binop(WasmExecutionMode execution_mode, WasmOpcode opcode, CHECK_EQ(expected, r.Call()); } { - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t> r(execution_mode); // return a op b BUILD(r, WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); CHECK_EQ(expected, r.Call(a, b)); @@ -964,8 +951,7 @@ void TestI64Cmp(WasmExecutionMode execution_mode, WasmOpcode opcode, CHECK_EQ(expected, r.Call()); } { - WasmRunner<int32_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int32_t, int64_t, int64_t> r(execution_mode); // return a op b BUILD(r, WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); CHECK_EQ(expected, r.Call(a, b)); @@ -1068,7 +1054,7 @@ WASM_EXEC_TEST(I64Clz) { {62, 0x0000000000000002}, {63, 0x0000000000000001}, {64, 0x0000000000000000}}; - WasmRunner<int64_t> r(execution_mode, MachineType::Uint64()); + WasmRunner<int64_t, uint64_t> r(execution_mode); BUILD(r, WASM_I64_CLZ(WASM_GET_LOCAL(0))); for (size_t i = 0; i < arraysize(values); i++) { CHECK_EQ(values[i].expected, r.Call(values[i].input)); @@ -1114,7 +1100,7 @@ WASM_EXEC_TEST(I64Ctz) { {2, 0x000000009afdbc84}, {1, 0x000000009afdbc82}, {0, 0x000000009afdbc81}}; - WasmRunner<int64_t> r(execution_mode, MachineType::Uint64()); + WasmRunner<int64_t, uint64_t> r(execution_mode); BUILD(r, WASM_I64_CTZ(WASM_GET_LOCAL(0))); for (size_t i = 0; i < arraysize(values); i++) { CHECK_EQ(values[i].expected, r.Call(values[i].input)); @@ -1132,7 +1118,7 @@ WASM_EXEC_TEST(I64Popcnt2) { {26, 0x1123456782345678}, {38, 0xffedcba09edcba09}}; - WasmRunner<int64_t> r(execution_mode, MachineType::Uint64()); + WasmRunner<int64_t, uint64_t> r(execution_mode); BUILD(r, WASM_I64_POPCNT(WASM_GET_LOCAL(0))); for (size_t i = 0; i < arraysize(values); i++) { CHECK_EQ(values[i].expected, r.Call(values[i].input)); @@ -1150,21 +1136,19 @@ WASM_EXEC_TEST(I64WasmRunner) { } } { - WasmRunner<int64_t> r(execution_mode, MachineType::Int64()); + WasmRunner<int64_t, int64_t> r(execution_mode); BUILD(r, WASM_GET_LOCAL(0)); FOR_INT64_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); } } { - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_XOR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT64_INPUTS(i) { FOR_INT64_INPUTS(j) { CHECK_EQ(*i ^ *j, r.Call(*i, *j)); } } } { - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64(), MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_XOR(WASM_GET_LOCAL(0), WASM_I64_XOR(WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)))); FOR_INT64_INPUTS(i) { @@ -1176,9 +1160,7 @@ WASM_EXEC_TEST(I64WasmRunner) { } } { - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64(), MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_XOR(WASM_GET_LOCAL(0), WASM_I64_XOR(WASM_GET_LOCAL(1), WASM_I64_XOR(WASM_GET_LOCAL(2), @@ -1196,16 +1178,15 @@ WASM_EXEC_TEST(I64WasmRunner) { WASM_EXEC_TEST(Call_Int64Sub) { REQUIRE(I64Sub); + WasmRunner<int64_t, int64_t, int64_t> r(execution_mode); // Build the target function. TestSignatures sigs; - TestingModule module(execution_mode); - WasmFunctionCompiler t(sigs.l_ll(), &module); + WasmFunctionCompiler& t = r.NewFunction(sigs.l_ll()); BUILD(t, WASM_I64_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - uint32_t index = t.CompileAndAdd(); // Build the caller function. - WasmRunner<int64_t> r(&module, MachineType::Int64(), MachineType::Int64()); - BUILD(r, WASM_CALL_FUNCTION(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); + BUILD(r, WASM_CALL_FUNCTION(t.function_index(), WASM_GET_LOCAL(0), + WASM_GET_LOCAL(1))); FOR_INT32_INPUTS(i) { FOR_INT32_INPUTS(j) { @@ -1228,20 +1209,19 @@ WASM_EXEC_TEST(LoadStoreI64_sx) { kExprI64LoadMem}; for (size_t m = 0; m < arraysize(loads); m++) { - TestingModule module(execution_mode); - byte* memory = module.AddMemoryElems<byte>(16); - WasmRunner<int64_t> r(&module); + WasmRunner<int64_t> r(execution_mode); + byte* memory = r.module().AddMemoryElems<byte>(16); byte code[] = { - kExprI8Const, 8, // -- - kExprI8Const, 0, // -- + kExprI32Const, 8, // -- + kExprI32Const, 0, // -- loads[m], // -- ZERO_ALIGNMENT, // -- ZERO_OFFSET, // -- kExprI64StoreMem, // -- ZERO_ALIGNMENT, // -- ZERO_OFFSET, // -- - kExprI8Const, 0, // -- + kExprI32Const, 0, // -- loads[m], // -- ZERO_ALIGNMENT, // -- ZERO_OFFSET, // -- @@ -1252,7 +1232,7 @@ WASM_EXEC_TEST(LoadStoreI64_sx) { // Try a bunch of different negative values. for (int i = -1; i >= -128; i -= 11) { int size = 1 << m; - module.BlankMemory(); + r.module().BlankMemory(); memory[size - 1] = static_cast<byte>(i); // set the high order byte. int64_t expected = static_cast<int64_t>(i) << ((size - 1) * 8); @@ -1266,9 +1246,9 @@ WASM_EXEC_TEST(LoadStoreI64_sx) { } } -WASM_EXEC_TEST(I64SConvertF32b) { +WASM_EXEC_TEST_WITH_TRAP(I64SConvertF32b) { REQUIRE(I64SConvertF32); - WasmRunner<int64_t> r(execution_mode, MachineType::Float32()); + WasmRunner<int64_t, float> r(execution_mode); BUILD(r, WASM_I64_SCONVERT_F32(WASM_GET_LOCAL(0))); FOR_FLOAT32_INPUTS(i) { @@ -1281,9 +1261,9 @@ WASM_EXEC_TEST(I64SConvertF32b) { } } -WASM_EXEC_TEST(I64SConvertF64b) { +WASM_EXEC_TEST_WITH_TRAP(I64SConvertF64b) { REQUIRE(I64SConvertF64); - WasmRunner<int64_t> r(execution_mode, MachineType::Float64()); + WasmRunner<int64_t, double> r(execution_mode); BUILD(r, WASM_I64_SCONVERT_F64(WASM_GET_LOCAL(0))); FOR_FLOAT64_INPUTS(i) { @@ -1296,9 +1276,9 @@ WASM_EXEC_TEST(I64SConvertF64b) { } } -WASM_EXEC_TEST(I64UConvertF32b) { +WASM_EXEC_TEST_WITH_TRAP(I64UConvertF32b) { REQUIRE(I64UConvertF32); - WasmRunner<uint64_t> r(execution_mode, MachineType::Float32()); + WasmRunner<uint64_t, float> r(execution_mode); BUILD(r, WASM_I64_UCONVERT_F32(WASM_GET_LOCAL(0))); FOR_FLOAT32_INPUTS(i) { @@ -1310,9 +1290,9 @@ WASM_EXEC_TEST(I64UConvertF32b) { } } -WASM_EXEC_TEST(I64UConvertF64b) { +WASM_EXEC_TEST_WITH_TRAP(I64UConvertF64b) { REQUIRE(I64UConvertF64); - WasmRunner<uint64_t> r(execution_mode, MachineType::Float64()); + WasmRunner<uint64_t, double> r(execution_mode); BUILD(r, WASM_I64_UCONVERT_F64(WASM_GET_LOCAL(0))); FOR_FLOAT64_INPUTS(i) { @@ -1326,25 +1306,33 @@ WASM_EXEC_TEST(I64UConvertF64b) { WASM_EXEC_TEST(I64ReinterpretF64) { REQUIRE(I64ReinterpretF64); - TestingModule module(execution_mode); - int64_t* memory = module.AddMemoryElems<int64_t>(8); - WasmRunner<int64_t> r(&module); + WasmRunner<int64_t> r(execution_mode); + int64_t* memory = r.module().AddMemoryElems<int64_t>(8); BUILD(r, WASM_I64_REINTERPRET_F64( WASM_LOAD_MEM(MachineType::Float64(), WASM_ZERO))); FOR_INT32_INPUTS(i) { int64_t expected = static_cast<int64_t>(*i) * 0x300010001; - module.WriteMemory(&memory[0], expected); + r.module().WriteMemory(&memory[0], expected); CHECK_EQ(expected, r.Call()); } } +WASM_EXEC_TEST(SignallingNanSurvivesI64ReinterpretF64) { + REQUIRE(I64ReinterpretF64); + WasmRunner<int64_t> r(execution_mode); + BUILD(r, WASM_I64_REINTERPRET_F64(WASM_SEQ(kExprF64Const, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xf4, 0x7f))); + + // This is a signalling nan. + CHECK_EQ(0x7ff4000000000000, r.Call()); +} + WASM_EXEC_TEST(F64ReinterpretI64) { REQUIRE(F64ReinterpretI64); - TestingModule module(execution_mode); - int64_t* memory = module.AddMemoryElems<int64_t>(8); - WasmRunner<int64_t> r(&module, MachineType::Int64()); + WasmRunner<int64_t, int64_t> r(execution_mode); + int64_t* memory = r.module().AddMemoryElems<int64_t>(8); BUILD(r, WASM_STORE_MEM(MachineType::Float64(), WASM_ZERO, WASM_F64_REINTERPRET_I64(WASM_GET_LOCAL(0))), @@ -1353,47 +1341,45 @@ WASM_EXEC_TEST(F64ReinterpretI64) { FOR_INT32_INPUTS(i) { int64_t expected = static_cast<int64_t>(*i) * 0x300010001; CHECK_EQ(expected, r.Call(expected)); - CHECK_EQ(expected, module.ReadMemory<int64_t>(&memory[0])); + CHECK_EQ(expected, r.module().ReadMemory<int64_t>(&memory[0])); } } WASM_EXEC_TEST(LoadMemI64) { REQUIRE(I64LoadStore); - TestingModule module(execution_mode); - int64_t* memory = module.AddMemoryElems<int64_t>(8); - module.RandomizeMemory(1111); - WasmRunner<int64_t> r(&module); + WasmRunner<int64_t> r(execution_mode); + int64_t* memory = r.module().AddMemoryElems<int64_t>(8); + r.module().RandomizeMemory(1111); - BUILD(r, WASM_LOAD_MEM(MachineType::Int64(), WASM_I8(0))); + BUILD(r, WASM_LOAD_MEM(MachineType::Int64(), WASM_ZERO)); - module.WriteMemory<int64_t>(&memory[0], 0x1abbccdd00112233LL); + r.module().WriteMemory<int64_t>(&memory[0], 0x1abbccdd00112233LL); CHECK_EQ(0x1abbccdd00112233LL, r.Call()); - module.WriteMemory<int64_t>(&memory[0], 0x33aabbccdd001122LL); + r.module().WriteMemory<int64_t>(&memory[0], 0x33aabbccdd001122LL); CHECK_EQ(0x33aabbccdd001122LL, r.Call()); - module.WriteMemory<int64_t>(&memory[0], 77777777); + r.module().WriteMemory<int64_t>(&memory[0], 77777777); CHECK_EQ(77777777, r.Call()); } WASM_EXEC_TEST(LoadMemI64_alignment) { REQUIRE(I64LoadStore); - TestingModule module(execution_mode); - int64_t* memory = module.AddMemoryElems<int64_t>(8); for (byte alignment = 0; alignment <= 3; alignment++) { - module.RandomizeMemory(1111); - WasmRunner<int64_t> r(&module); + WasmRunner<int64_t> r(execution_mode); + int64_t* memory = r.module().AddMemoryElems<int64_t>(8); + r.module().RandomizeMemory(1111); BUILD(r, - WASM_LOAD_MEM_ALIGNMENT(MachineType::Int64(), WASM_I8(0), alignment)); + WASM_LOAD_MEM_ALIGNMENT(MachineType::Int64(), WASM_ZERO, alignment)); - module.WriteMemory<int64_t>(&memory[0], 0x1abbccdd00112233LL); + r.module().WriteMemory<int64_t>(&memory[0], 0x1abbccdd00112233LL); CHECK_EQ(0x1abbccdd00112233LL, r.Call()); - module.WriteMemory<int64_t>(&memory[0], 0x33aabbccdd001122LL); + r.module().WriteMemory<int64_t>(&memory[0], 0x33aabbccdd001122LL); CHECK_EQ(0x33aabbccdd001122LL, r.Call()); - module.WriteMemory<int64_t>(&memory[0], 77777777); + r.module().WriteMemory<int64_t>(&memory[0], 77777777); CHECK_EQ(77777777, r.Call()); } } @@ -1404,29 +1390,27 @@ WASM_EXEC_TEST(MemI64_Sum) { REQUIRE(I64Sub); REQUIRE(I64Phi); const int kNumElems = 20; - TestingModule module(execution_mode); - uint64_t* memory = module.AddMemoryElems<uint64_t>(kNumElems); - WasmRunner<uint64_t> r(&module, MachineType::Int32()); - const byte kSum = r.AllocateLocal(kAstI64); - - BUILD( - r, - WASM_WHILE( - WASM_GET_LOCAL(0), - WASM_BLOCK( - WASM_SET_LOCAL(kSum, - WASM_I64_ADD(WASM_GET_LOCAL(kSum), + WasmRunner<uint64_t, int32_t> r(execution_mode); + uint64_t* memory = r.module().AddMemoryElems<uint64_t>(kNumElems); + const byte kSum = r.AllocateLocal(kWasmI64); + + BUILD(r, WASM_WHILE( + WASM_GET_LOCAL(0), + WASM_BLOCK( + WASM_SET_LOCAL( + kSum, WASM_I64_ADD(WASM_GET_LOCAL(kSum), WASM_LOAD_MEM(MachineType::Int64(), WASM_GET_LOCAL(0)))), - WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(8))))), - WASM_GET_LOCAL(1)); + WASM_SET_LOCAL( + 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I32V_1(8))))), + WASM_GET_LOCAL(1)); // Run 4 trials. for (int i = 0; i < 3; i++) { - module.RandomizeMemory(i * 33); + r.module().RandomizeMemory(i * 33); uint64_t expected = 0; for (size_t j = kNumElems - 1; j > 0; j--) { - expected += module.ReadMemory(&memory[j]); + expected += r.module().ReadMemory(&memory[j]); } uint64_t result = r.Call(8 * (kNumElems - 1)); CHECK_EQ(expected, result); @@ -1434,20 +1418,19 @@ WASM_EXEC_TEST(MemI64_Sum) { } WASM_EXEC_TEST(StoreMemI64_alignment) { - TestingModule module(execution_mode); - int64_t* memory = module.AddMemoryElems<int64_t>(4); const int64_t kWritten = 0x12345678abcd0011ll; for (byte i = 0; i <= 3; i++) { - WasmRunner<int64_t> r(&module, MachineType::Int64()); + WasmRunner<int64_t, int64_t> r(execution_mode); + int64_t* memory = r.module().AddMemoryElems<int64_t>(4); BUILD(r, WASM_STORE_MEM_ALIGNMENT(MachineType::Int64(), WASM_ZERO, i, WASM_GET_LOCAL(0)), WASM_GET_LOCAL(0)); - module.RandomizeMemory(1111); - module.WriteMemory<int64_t>(&memory[0], 0); + r.module().RandomizeMemory(1111); + r.module().WriteMemory<int64_t>(&memory[0], 0); CHECK_EQ(kWritten, r.Call(kWritten)); - CHECK_EQ(kWritten, module.ReadMemory(&memory[0])); + CHECK_EQ(kWritten, r.module().ReadMemory(&memory[0])); } } @@ -1456,16 +1439,15 @@ WASM_EXEC_TEST(I64Global) { REQUIRE(I64SConvertI32); REQUIRE(I64And); REQUIRE(DepthFirst); - TestingModule module(execution_mode); - int64_t* global = module.AddGlobal<int64_t>(kAstI64); - WasmRunner<int32_t> r(&module, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); + int64_t* global = r.module().AddGlobal<int64_t>(); // global = global + p0 BUILD(r, WASM_SET_GLOBAL( 0, WASM_I64_AND(WASM_GET_GLOBAL(0), WASM_I64_SCONVERT_I32(WASM_GET_LOCAL(0)))), WASM_ZERO); - module.WriteMemory<int64_t>(global, 0xFFFFFFFFFFFFFFFFLL); + r.module().WriteMemory<int64_t>(global, 0xFFFFFFFFFFFFFFFFLL); for (int i = 9; i < 444444; i += 111111) { int64_t expected = *global & i; r.Call(i); @@ -1476,7 +1458,7 @@ WASM_EXEC_TEST(I64Global) { WASM_EXEC_TEST(I64Eqz) { REQUIRE(I64Eq); - WasmRunner<int32_t> r(execution_mode, MachineType::Int64()); + WasmRunner<int32_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_EQZ(WASM_GET_LOCAL(0))); FOR_INT64_INPUTS(i) { @@ -1487,8 +1469,7 @@ WASM_EXEC_TEST(I64Eqz) { WASM_EXEC_TEST(I64Ror) { REQUIRE(I64Ror); - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_ROR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_UINT64_INPUTS(i) { @@ -1501,8 +1482,7 @@ WASM_EXEC_TEST(I64Ror) { WASM_EXEC_TEST(I64Rol) { REQUIRE(I64Rol); - WasmRunner<int64_t> r(execution_mode, MachineType::Int64(), - MachineType::Int64()); + WasmRunner<int64_t, int64_t, int64_t> r(execution_mode); BUILD(r, WASM_I64_ROL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_UINT64_INPUTS(i) { @@ -1513,10 +1493,7 @@ WASM_EXEC_TEST(I64Rol) { } } -WASM_EXEC_TEST(StoreMem_offset_oob_i64) { - TestingModule module(execution_mode); - byte* memory = module.AddMemoryElems<byte>(32); - +WASM_EXEC_TEST_WITH_TRAP(StoreMem_offset_oob_i64) { static const MachineType machineTypes[] = { MachineType::Int8(), MachineType::Uint8(), MachineType::Int16(), MachineType::Uint16(), MachineType::Int32(), MachineType::Uint32(), @@ -1524,8 +1501,9 @@ WASM_EXEC_TEST(StoreMem_offset_oob_i64) { MachineType::Float64()}; for (size_t m = 0; m < arraysize(machineTypes); m++) { - module.RandomizeMemory(1119 + static_cast<int>(m)); - WasmRunner<int32_t> r(&module, MachineType::Uint32()); + WasmRunner<int32_t, uint32_t> r(execution_mode); + byte* memory = r.module().AddMemoryElems<byte>(32); + r.module().RandomizeMemory(1119 + static_cast<int>(m)); BUILD(r, WASM_STORE_MEM_OFFSET(machineTypes[m], 8, WASM_GET_LOCAL(0), WASM_LOAD_MEM(machineTypes[m], WASM_ZERO)), @@ -1548,36 +1526,32 @@ WASM_EXEC_TEST(StoreMem_offset_oob_i64) { for (size_t i = 0; i < sizeof(__buf); i++) vec.push_back(__buf[i]); \ } while (false) -static void CompileCallIndirectMany(LocalType param) { +static void CompileCallIndirectMany(ValueType param) { // Make sure we don't run out of registers when compiling indirect calls // with many many parameters. TestSignatures sigs; for (byte num_params = 0; num_params < 40; num_params++) { - v8::internal::AccountingAllocator allocator; - Zone zone(&allocator, ZONE_NAME); - HandleScope scope(CcTest::InitIsolateOnce()); - TestingModule module(kExecuteCompiled); - FunctionSig* sig = sigs.many(&zone, kAstStmt, param, num_params); + WasmRunner<void> r(kExecuteCompiled); + FunctionSig* sig = sigs.many(r.zone(), kWasmStmt, param, num_params); - module.AddSignature(sig); - module.AddSignature(sig); - module.AddIndirectFunctionTable(nullptr, 0); + r.module().AddSignature(sig); + r.module().AddSignature(sig); + r.module().AddIndirectFunctionTable(nullptr, 0); - WasmFunctionCompiler t(sig, &module); + WasmFunctionCompiler& t = r.NewFunction(sig); std::vector<byte> code; for (byte p = 0; p < num_params; p++) { ADD_CODE(code, kExprGetLocal, p); } - ADD_CODE(code, kExprI8Const, 0); + ADD_CODE(code, kExprI32Const, 0); ADD_CODE(code, kExprCallIndirect, 1, TABLE_ZERO); t.Build(&code[0], &code[0] + code.size()); - t.Compile(); } } -TEST(Compile_Wasm_CallIndirect_Many_i64) { CompileCallIndirectMany(kAstI64); } +TEST(Compile_Wasm_CallIndirect_Many_i64) { CompileCallIndirectMany(kWasmI64); } static void Run_WasmMixedCall_N(WasmExecutionMode execution_mode, int start) { const int kExpected = 6333; @@ -1595,28 +1569,25 @@ static void Run_WasmMixedCall_N(WasmExecutionMode execution_mode, int start) { for (int which = 0; which < num_params; which++) { v8::internal::AccountingAllocator allocator; Zone zone(&allocator, ZONE_NAME); - TestingModule module(execution_mode); - module.AddMemory(1024); + WasmRunner<int32_t> r(execution_mode); + r.module().AddMemory(1024); MachineType* memtypes = &mixed[start]; MachineType result = memtypes[which]; // ========================================================================= // Build the selector function. // ========================================================================= - uint32_t index; FunctionSig::Builder b(&zone, 1, num_params); - b.AddReturn(WasmOpcodes::LocalTypeFor(result)); + b.AddReturn(WasmOpcodes::ValueTypeFor(result)); for (int i = 0; i < num_params; i++) { - b.AddParam(WasmOpcodes::LocalTypeFor(memtypes[i])); + b.AddParam(WasmOpcodes::ValueTypeFor(memtypes[i])); } - WasmFunctionCompiler t(b.Build(), &module); + WasmFunctionCompiler& t = r.NewFunction(b.Build()); BUILD(t, WASM_GET_LOCAL(which)); - index = t.CompileAndAdd(); // ========================================================================= // Build the calling function. // ========================================================================= - WasmRunner<int32_t> r(&module); std::vector<byte> code; // Load the offset for the store. @@ -1625,11 +1596,11 @@ static void Run_WasmMixedCall_N(WasmExecutionMode execution_mode, int start) { // Load the arguments. for (int i = 0; i < num_params; i++) { int offset = (i + 1) * kElemSize; - ADD_CODE(code, WASM_LOAD_MEM(memtypes[i], WASM_I8(offset))); + ADD_CODE(code, WASM_LOAD_MEM(memtypes[i], WASM_I32V_2(offset))); } // Call the selector function. - ADD_CODE(code, kExprCallFunction, static_cast<byte>(index)); + ADD_CODE(code, WASM_CALL_FUNCTION0(t.function_index())); // Store the result in memory. ADD_CODE(code, @@ -1643,14 +1614,14 @@ static void Run_WasmMixedCall_N(WasmExecutionMode execution_mode, int start) { // Run the code. for (int t = 0; t < 10; t++) { - module.RandomizeMemory(); + r.module().RandomizeMemory(); CHECK_EQ(kExpected, r.Call()); int size = WasmOpcodes::MemSize(result); for (int i = 0; i < size; i++) { int base = (which + 1) * kElemSize; - byte expected = module.raw_mem_at<byte>(base + i); - byte result = module.raw_mem_at<byte>(i); + byte expected = r.module().raw_mem_at<byte>(base + i); + byte result = r.module().raw_mem_at<byte>(i); CHECK_EQ(expected, result); } } @@ -1661,3 +1632,19 @@ WASM_EXEC_TEST(MixedCall_i64_0) { Run_WasmMixedCall_N(execution_mode, 0); } WASM_EXEC_TEST(MixedCall_i64_1) { Run_WasmMixedCall_N(execution_mode, 1); } WASM_EXEC_TEST(MixedCall_i64_2) { Run_WasmMixedCall_N(execution_mode, 2); } WASM_EXEC_TEST(MixedCall_i64_3) { Run_WasmMixedCall_N(execution_mode, 3); } + +WASM_EXEC_TEST(Regress5874) { + REQUIRE(I32ConvertI64); + REQUIRE(I64LoadStore); + REQUIRE(I64Const); + WasmRunner<int32_t> r(execution_mode); + r.module().AddMemoryElems<int64_t>(8); + + BUILD(r, kExprI64Const, 0x00, // -- + kExprI32ConvertI64, // -- + kExprI64Const, 0x00, // -- + kExprI64StoreMem, 0x03, 0x00, // -- + kExprI32Const, 0x00); // -- + + r.Call(); +} diff --git a/deps/v8/test/cctest/wasm/test-run-wasm-asmjs.cc b/deps/v8/test/cctest/wasm/test-run-wasm-asmjs.cc index bd80e28f9f..38430f292c 100644 --- a/deps/v8/test/cctest/wasm/test-run-wasm-asmjs.cc +++ b/deps/v8/test/cctest/wasm/test-run-wasm-asmjs.cc @@ -38,9 +38,8 @@ uint32_t GetMatchingRelocInfoCount(Handle<Code> code, RelocInfo::Mode rmode) { } WASM_EXEC_TEST(Int32AsmjsDivS) { - TestingModule module(execution_mode); - module.origin = kAsmJsOrigin; - WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32()); + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); + r.module().ChangeOriginToAsmjs(); BUILD(r, WASM_BINOP(kExprI32AsmjsDivS, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); const int32_t kMin = std::numeric_limits<int32_t>::min(); CHECK_EQ(0, r.Call(0, 100)); @@ -51,9 +50,8 @@ WASM_EXEC_TEST(Int32AsmjsDivS) { } WASM_EXEC_TEST(Int32AsmjsRemS) { - TestingModule module(execution_mode); - module.origin = kAsmJsOrigin; - WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32()); + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); + r.module().ChangeOriginToAsmjs(); BUILD(r, WASM_BINOP(kExprI32AsmjsRemS, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); const int32_t kMin = std::numeric_limits<int32_t>::min(); CHECK_EQ(33, r.Call(133, 100)); @@ -64,9 +62,8 @@ WASM_EXEC_TEST(Int32AsmjsRemS) { } WASM_EXEC_TEST(Int32AsmjsDivU) { - TestingModule module(execution_mode); - module.origin = kAsmJsOrigin; - WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32()); + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); + r.module().ChangeOriginToAsmjs(); BUILD(r, WASM_BINOP(kExprI32AsmjsDivU, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); const int32_t kMin = std::numeric_limits<int32_t>::min(); CHECK_EQ(0, r.Call(0, 100)); @@ -77,9 +74,8 @@ WASM_EXEC_TEST(Int32AsmjsDivU) { } WASM_EXEC_TEST(Int32AsmjsRemU) { - TestingModule module(execution_mode); - module.origin = kAsmJsOrigin; - WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32()); + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); + r.module().ChangeOriginToAsmjs(); BUILD(r, WASM_BINOP(kExprI32AsmjsRemU, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); const int32_t kMin = std::numeric_limits<int32_t>::min(); CHECK_EQ(17, r.Call(217, 100)); @@ -90,9 +86,8 @@ WASM_EXEC_TEST(Int32AsmjsRemU) { } WASM_EXEC_TEST(I32AsmjsSConvertF32) { - TestingModule module(execution_mode); - module.origin = kAsmJsOrigin; - WasmRunner<int32_t> r(&module, MachineType::Float32()); + WasmRunner<int32_t, float> r(execution_mode); + r.module().ChangeOriginToAsmjs(); BUILD(r, WASM_UNOP(kExprI32AsmjsSConvertF32, WASM_GET_LOCAL(0))); FOR_FLOAT32_INPUTS(i) { @@ -102,9 +97,8 @@ WASM_EXEC_TEST(I32AsmjsSConvertF32) { } WASM_EXEC_TEST(I32AsmjsSConvertF64) { - TestingModule module(execution_mode); - module.origin = kAsmJsOrigin; - WasmRunner<int32_t> r(&module, MachineType::Float64()); + WasmRunner<int32_t, double> r(execution_mode); + r.module().ChangeOriginToAsmjs(); BUILD(r, WASM_UNOP(kExprI32AsmjsSConvertF64, WASM_GET_LOCAL(0))); FOR_FLOAT64_INPUTS(i) { @@ -114,9 +108,8 @@ WASM_EXEC_TEST(I32AsmjsSConvertF64) { } WASM_EXEC_TEST(I32AsmjsUConvertF32) { - TestingModule module(execution_mode); - module.origin = kAsmJsOrigin; - WasmRunner<uint32_t> r(&module, MachineType::Float32()); + WasmRunner<uint32_t, float> r(execution_mode); + r.module().ChangeOriginToAsmjs(); BUILD(r, WASM_UNOP(kExprI32AsmjsUConvertF32, WASM_GET_LOCAL(0))); FOR_FLOAT32_INPUTS(i) { @@ -126,9 +119,8 @@ WASM_EXEC_TEST(I32AsmjsUConvertF32) { } WASM_EXEC_TEST(I32AsmjsUConvertF64) { - TestingModule module(execution_mode); - module.origin = kAsmJsOrigin; - WasmRunner<uint32_t> r(&module, MachineType::Float64()); + WasmRunner<uint32_t, double> r(execution_mode); + r.module().ChangeOriginToAsmjs(); BUILD(r, WASM_UNOP(kExprI32AsmjsUConvertF64, WASM_GET_LOCAL(0))); FOR_FLOAT64_INPUTS(i) { @@ -138,11 +130,10 @@ WASM_EXEC_TEST(I32AsmjsUConvertF64) { } WASM_EXEC_TEST(LoadMemI32_oob_asm) { - TestingModule module(execution_mode); - module.origin = kAsmJsOrigin; - int32_t* memory = module.AddMemoryElems<int32_t>(8); - WasmRunner<int32_t> r(&module, MachineType::Uint32()); - module.RandomizeMemory(1112); + WasmRunner<int32_t, uint32_t> r(execution_mode); + r.module().ChangeOriginToAsmjs(); + int32_t* memory = r.module().AddMemoryElems<int32_t>(8); + r.module().RandomizeMemory(1112); BUILD(r, WASM_UNOP(kExprI32AsmjsLoadMem, WASM_GET_LOCAL(0))); @@ -159,11 +150,10 @@ WASM_EXEC_TEST(LoadMemI32_oob_asm) { } WASM_EXEC_TEST(LoadMemF32_oob_asm) { - TestingModule module(execution_mode); - module.origin = kAsmJsOrigin; - float* memory = module.AddMemoryElems<float>(8); - WasmRunner<float> r(&module, MachineType::Uint32()); - module.RandomizeMemory(1112); + WasmRunner<float, uint32_t> r(execution_mode); + r.module().ChangeOriginToAsmjs(); + float* memory = r.module().AddMemoryElems<float>(8); + r.module().RandomizeMemory(1112); BUILD(r, WASM_UNOP(kExprF32AsmjsLoadMem, WASM_GET_LOCAL(0))); @@ -180,11 +170,10 @@ WASM_EXEC_TEST(LoadMemF32_oob_asm) { } WASM_EXEC_TEST(LoadMemF64_oob_asm) { - TestingModule module(execution_mode); - module.origin = kAsmJsOrigin; - double* memory = module.AddMemoryElems<double>(8); - WasmRunner<double> r(&module, MachineType::Uint32()); - module.RandomizeMemory(1112); + WasmRunner<double, uint32_t> r(execution_mode); + r.module().ChangeOriginToAsmjs(); + double* memory = r.module().AddMemoryElems<double>(8); + r.module().RandomizeMemory(1112); BUILD(r, WASM_UNOP(kExprF64AsmjsLoadMem, WASM_GET_LOCAL(0))); @@ -203,11 +192,10 @@ WASM_EXEC_TEST(LoadMemF64_oob_asm) { } WASM_EXEC_TEST(StoreMemI32_oob_asm) { - TestingModule module(execution_mode); - module.origin = kAsmJsOrigin; - int32_t* memory = module.AddMemoryElems<int32_t>(8); - WasmRunner<int32_t> r(&module, MachineType::Uint32(), MachineType::Uint32()); - module.RandomizeMemory(1112); + WasmRunner<int32_t, uint32_t, uint32_t> r(execution_mode); + r.module().ChangeOriginToAsmjs(); + int32_t* memory = r.module().AddMemoryElems<int32_t>(8); + r.module().RandomizeMemory(1112); BUILD(r, WASM_BINOP(kExprI32AsmjsStoreMem, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); @@ -237,87 +225,78 @@ WASM_EXEC_TEST(StoreMemI32_oob_asm) { TEST_BODY(kExprI32AsmjsStoreMem16) \ TEST_BODY(kExprI32AsmjsStoreMem) -#define INT_LOAD_TEST(OP_TYPE) \ - TEST(RunWasm_AsmCheckedRelocInfo##OP_TYPE) { \ - TestingModule module(kExecuteCompiled); \ - module.origin = kAsmJsOrigin; \ - WasmRunner<int32_t> r(&module, MachineType::Uint32()); \ - BUILD(r, WASM_UNOP(OP_TYPE, WASM_GET_LOCAL(0))); \ - CHECK_EQ(1u, GetMatchingRelocInfoCount(module.instance->function_code[0], \ - RelocInfo::WASM_MEMORY_REFERENCE)); \ - CHECK_NE( \ - 0u, GetMatchingRelocInfoCount(module.instance->function_code[0], \ - RelocInfo::WASM_MEMORY_SIZE_REFERENCE)); \ +#define INT_LOAD_TEST(OP_TYPE) \ + TEST(RunWasm_AsmCheckedRelocInfo##OP_TYPE) { \ + WasmRunner<int32_t, uint32_t> r(kExecuteCompiled); \ + r.module().ChangeOriginToAsmjs(); \ + BUILD(r, WASM_UNOP(OP_TYPE, WASM_GET_LOCAL(0))); \ + CHECK_EQ(1, \ + GetMatchingRelocInfoCount(r.module().instance->function_code[0], \ + RelocInfo::WASM_MEMORY_REFERENCE)); \ + CHECK_NE( \ + 0, GetMatchingRelocInfoCount(r.module().instance->function_code[0], \ + RelocInfo::WASM_MEMORY_SIZE_REFERENCE)); \ } FOREACH_INT_CHECKED_LOAD_OP(INT_LOAD_TEST) -#define INT_STORE_TEST(OP_TYPE) \ - TEST(RunWasm_AsmCheckedRelocInfo##OP_TYPE) { \ - TestingModule module(kExecuteCompiled); \ - module.origin = kAsmJsOrigin; \ - WasmRunner<int32_t> r(&module, MachineType::Uint32(), \ - MachineType::Uint32()); \ - BUILD(r, WASM_BINOP(OP_TYPE, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); \ - CHECK_EQ(1u, GetMatchingRelocInfoCount(module.instance->function_code[0], \ - RelocInfo::WASM_MEMORY_REFERENCE)); \ - CHECK_NE( \ - 0u, GetMatchingRelocInfoCount(module.instance->function_code[0], \ - RelocInfo::WASM_MEMORY_SIZE_REFERENCE)); \ +#define INT_STORE_TEST(OP_TYPE) \ + TEST(RunWasm_AsmCheckedRelocInfo##OP_TYPE) { \ + WasmRunner<int32_t, uint32_t, uint32_t> r(kExecuteCompiled); \ + r.module().ChangeOriginToAsmjs(); \ + BUILD(r, WASM_BINOP(OP_TYPE, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); \ + CHECK_EQ(1, \ + GetMatchingRelocInfoCount(r.module().instance->function_code[0], \ + RelocInfo::WASM_MEMORY_REFERENCE)); \ + CHECK_NE( \ + 0, GetMatchingRelocInfoCount(r.module().instance->function_code[0], \ + RelocInfo::WASM_MEMORY_SIZE_REFERENCE)); \ } FOREACH_INT_CHECKED_STORE_OP(INT_STORE_TEST) TEST(RunWasm_AsmCheckedLoadFloat32RelocInfo) { - TestingModule module(kExecuteCompiled); - module.origin = kAsmJsOrigin; - WasmRunner<float> r(&module, MachineType::Uint32()); + WasmRunner<float, uint32_t> r(kExecuteCompiled); + r.module().ChangeOriginToAsmjs(); BUILD(r, WASM_UNOP(kExprF32AsmjsLoadMem, WASM_GET_LOCAL(0))); - CHECK_EQ(1u, GetMatchingRelocInfoCount(module.instance->function_code[0], - RelocInfo::WASM_MEMORY_REFERENCE)); - CHECK_NE(0u, - GetMatchingRelocInfoCount(module.instance->function_code[0], - RelocInfo::WASM_MEMORY_SIZE_REFERENCE)); + CHECK_EQ(1, GetMatchingRelocInfoCount(r.module().instance->function_code[0], + RelocInfo::WASM_MEMORY_REFERENCE)); + CHECK_NE(0, GetMatchingRelocInfoCount(r.module().instance->function_code[0], + RelocInfo::WASM_MEMORY_SIZE_REFERENCE)); } TEST(RunWasm_AsmCheckedStoreFloat32RelocInfo) { - TestingModule module(kExecuteCompiled); - module.origin = kAsmJsOrigin; - WasmRunner<float> r(&module, MachineType::Uint32(), MachineType::Float32()); + WasmRunner<float, uint32_t, float> r(kExecuteCompiled); + r.module().ChangeOriginToAsmjs(); BUILD(r, WASM_BINOP(kExprF32AsmjsStoreMem, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - CHECK_EQ(1u, GetMatchingRelocInfoCount(module.instance->function_code[0], - RelocInfo::WASM_MEMORY_REFERENCE)); - CHECK_NE(0u, - GetMatchingRelocInfoCount(module.instance->function_code[0], - RelocInfo::WASM_MEMORY_SIZE_REFERENCE)); + CHECK_EQ(1, GetMatchingRelocInfoCount(r.module().instance->function_code[0], + RelocInfo::WASM_MEMORY_REFERENCE)); + CHECK_NE(0, GetMatchingRelocInfoCount(r.module().instance->function_code[0], + RelocInfo::WASM_MEMORY_SIZE_REFERENCE)); } TEST(RunWasm_AsmCheckedLoadFloat64RelocInfo) { - TestingModule module(kExecuteCompiled); - module.origin = kAsmJsOrigin; - WasmRunner<double> r(&module, MachineType::Uint32()); + WasmRunner<double, uint32_t> r(kExecuteCompiled); + r.module().ChangeOriginToAsmjs(); BUILD(r, WASM_UNOP(kExprF64AsmjsLoadMem, WASM_GET_LOCAL(0))); - CHECK_EQ(1u, GetMatchingRelocInfoCount(module.instance->function_code[0], - RelocInfo::WASM_MEMORY_REFERENCE)); - CHECK_NE(0u, - GetMatchingRelocInfoCount(module.instance->function_code[0], - RelocInfo::WASM_MEMORY_SIZE_REFERENCE)); + CHECK_EQ(1, GetMatchingRelocInfoCount(r.module().instance->function_code[0], + RelocInfo::WASM_MEMORY_REFERENCE)); + CHECK_NE(0, GetMatchingRelocInfoCount(r.module().instance->function_code[0], + RelocInfo::WASM_MEMORY_SIZE_REFERENCE)); } TEST(RunWasm_AsmCheckedStoreFloat64RelocInfo) { - TestingModule module(kExecuteCompiled); - module.origin = kAsmJsOrigin; - WasmRunner<double> r(&module, MachineType::Uint32(), MachineType::Float64()); + WasmRunner<double, uint32_t, double> r(kExecuteCompiled); + r.module().ChangeOriginToAsmjs(); BUILD(r, WASM_BINOP(kExprF64AsmjsStoreMem, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - CHECK_EQ(1u, GetMatchingRelocInfoCount(module.instance->function_code[0], - RelocInfo::WASM_MEMORY_REFERENCE)); - CHECK_NE(0u, - GetMatchingRelocInfoCount(module.instance->function_code[0], - RelocInfo::WASM_MEMORY_SIZE_REFERENCE)); + CHECK_EQ(1, GetMatchingRelocInfoCount(r.module().instance->function_code[0], + RelocInfo::WASM_MEMORY_REFERENCE)); + CHECK_NE(0, GetMatchingRelocInfoCount(r.module().instance->function_code[0], + RelocInfo::WASM_MEMORY_SIZE_REFERENCE)); } diff --git a/deps/v8/test/cctest/wasm/test-run-wasm-interpreter.cc b/deps/v8/test/cctest/wasm/test-run-wasm-interpreter.cc index 47d97f4e48..e355b68d19 100644 --- a/deps/v8/test/cctest/wasm/test-run-wasm-interpreter.cc +++ b/deps/v8/test/cctest/wasm/test-run-wasm-interpreter.cc @@ -30,20 +30,21 @@ TEST(Run_WasmInt8Const_i) { WasmRunner<int32_t> r(kExecuteInterpreted); const byte kExpectedValue = 109; // return(kExpectedValue) - BUILD(r, WASM_I8(kExpectedValue)); + BUILD(r, WASM_I32V_2(kExpectedValue)); CHECK_EQ(kExpectedValue, r.Call()); } TEST(Run_WasmIfElse) { - WasmRunner<int32_t> r(kExecuteInterpreted, MachineType::Int32()); - BUILD(r, WASM_IF_ELSE_I(WASM_GET_LOCAL(0), WASM_I8(9), WASM_I8(10))); + WasmRunner<int32_t, int32_t> r(kExecuteInterpreted); + BUILD(r, WASM_IF_ELSE_I(WASM_GET_LOCAL(0), WASM_I32V_1(9), WASM_I32V_1(10))); CHECK_EQ(10, r.Call(0)); CHECK_EQ(9, r.Call(1)); } TEST(Run_WasmIfReturn) { - WasmRunner<int32_t> r(kExecuteInterpreted, MachineType::Int32()); - BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_RETURN1(WASM_I8(77))), WASM_I8(65)); + WasmRunner<int32_t, int32_t> r(kExecuteInterpreted); + BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_RETURN1(WASM_I32V_2(77))), + WASM_I32V_2(65)); CHECK_EQ(65, r.Call(0)); CHECK_EQ(77, r.Call(1)); } @@ -54,7 +55,7 @@ TEST(Run_WasmNopsN) { for (int nops = 0; nops < kMaxNops; nops++) { byte expected = static_cast<byte>(20 + nops); memset(code, kExprNop, sizeof(code)); - code[nops] = kExprI8Const; + code[nops] = kExprI32Const; code[nops + 1] = expected; WasmRunner<int32_t> r(kExecuteInterpreted); @@ -64,13 +65,13 @@ TEST(Run_WasmNopsN) { } TEST(Run_WasmConstsN) { - const int kMaxConsts = 10; + const int kMaxConsts = 5; byte code[kMaxConsts * 3]; int32_t expected = 0; for (int count = 1; count < kMaxConsts; count++) { for (int i = 0; i < count; i++) { byte val = static_cast<byte>(count * 10 + i); - code[i * 3] = kExprI8Const; + code[i * 3] = kExprI32Const; code[i * 3 + 1] = val; if (i == (count - 1)) { code[i * 3 + 2] = kExprNop; @@ -95,7 +96,7 @@ TEST(Run_WasmBlocksN) { memset(code, kExprNop, sizeof(code)); code[0] = kExprBlock; code[1] = kLocalI32; - code[2 + nops] = kExprI8Const; + code[2 + nops] = kExprI32Const; code[2 + nops + 1] = expected; code[2 + nops + 2] = kExprEnd; @@ -108,6 +109,7 @@ TEST(Run_WasmBlocksN) { TEST(Run_WasmBlockBreakN) { const int kMaxNops = 10; const int kExtra = 6; + int run = 0; byte code[kMaxNops + kExtra]; for (int nops = 0; nops < kMaxNops; nops++) { // Place the break anywhere within the block. @@ -117,8 +119,8 @@ TEST(Run_WasmBlockBreakN) { code[1] = kLocalI32; code[sizeof(code) - 1] = kExprEnd; - int expected = nops * 11 + index; - code[2 + index + 0] = kExprI8Const; + int expected = run++; + code[2 + index + 0] = kExprI32Const; code[2 + index + 1] = static_cast<byte>(expected); code[2 + index + 2] = kExprBr; code[2 + index + 3] = 0; @@ -131,13 +133,14 @@ TEST(Run_WasmBlockBreakN) { } TEST(Run_Wasm_nested_ifs_i) { - WasmRunner<int32_t> r(kExecuteInterpreted, MachineType::Int32(), - MachineType::Int32()); + WasmRunner<int32_t, int32_t, int32_t> r(kExecuteInterpreted); - BUILD(r, WASM_IF_ELSE_I( - WASM_GET_LOCAL(0), - WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_I8(11), WASM_I8(12)), - WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_I8(13), WASM_I8(14)))); + BUILD( + r, + WASM_IF_ELSE_I( + WASM_GET_LOCAL(0), + WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_I32V_1(11), WASM_I32V_1(12)), + WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_I32V_1(13), WASM_I32V_1(14)))); CHECK_EQ(11, r.Call(1, 1)); CHECK_EQ(12, r.Call(1, 0)); @@ -180,8 +183,7 @@ TEST(Breakpoint_I32Add) { Find(code, sizeof(code), kNumBreakpoints, kExprGetLocal, kExprGetLocal, kExprI32Add); - WasmRunner<int32_t> r(kExecuteInterpreted, MachineType::Uint32(), - MachineType::Uint32()); + WasmRunner<int32_t, uint32_t, uint32_t> r(kExecuteInterpreted); r.Build(code, code + arraysize(code)); @@ -217,11 +219,10 @@ TEST(Breakpoint_I32Add) { } TEST(Step_I32Mul) { - static const int kTraceLength = 4; + static const int kTraceLength = 5; byte code[] = {WASM_I32_MUL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))}; - WasmRunner<int32_t> r(kExecuteInterpreted, MachineType::Uint32(), - MachineType::Uint32()); + WasmRunner<int32_t, uint32_t, uint32_t> r(kExecuteInterpreted); r.Build(code, code + arraysize(code)); @@ -259,8 +260,7 @@ TEST(Breakpoint_I32And_disable) { std::unique_ptr<int[]> offsets = Find(code, sizeof(code), kNumBreakpoints, kExprI32And); - WasmRunner<int32_t> r(kExecuteInterpreted, MachineType::Uint32(), - MachineType::Uint32()); + WasmRunner<int32_t, uint32_t, uint32_t> r(kExecuteInterpreted); r.Build(code, code + arraysize(code)); @@ -297,19 +297,27 @@ TEST(Breakpoint_I32And_disable) { } TEST(GrowMemory) { - TestingModule module(kExecuteInterpreted); - WasmRunner<int32_t> r(&module, MachineType::Uint32()); - module.AddMemory(WasmModule::kPageSize); - BUILD(r, WASM_GROW_MEMORY(WASM_GET_LOCAL(0))); - CHECK_EQ(1, r.Call(1)); + { + WasmRunner<int32_t, uint32_t> r(kExecuteInterpreted); + r.module().AddMemory(WasmModule::kPageSize); + r.module().SetMaxMemPages(10); + BUILD(r, WASM_GROW_MEMORY(WASM_GET_LOCAL(0))); + CHECK_EQ(1, r.Call(1)); + } + { + WasmRunner<int32_t, uint32_t> r(kExecuteInterpreted); + r.module().AddMemory(WasmModule::kPageSize); + r.module().SetMaxMemPages(10); + BUILD(r, WASM_GROW_MEMORY(WASM_GET_LOCAL(0))); + CHECK_EQ(-1, r.Call(11)); + } } TEST(GrowMemoryPreservesData) { int32_t index = 16; int32_t value = 2335; - TestingModule module(kExecuteInterpreted); - WasmRunner<int32_t> r(&module, MachineType::Uint32()); - module.AddMemory(WasmModule::kPageSize); + WasmRunner<int32_t, uint32_t> r(kExecuteInterpreted); + r.module().AddMemory(WasmModule::kPageSize); BUILD(r, WASM_STORE_MEM(MachineType::Int32(), WASM_I32V(index), WASM_I32V(value)), WASM_GROW_MEMORY(WASM_GET_LOCAL(0)), WASM_DROP, @@ -320,16 +328,14 @@ TEST(GrowMemoryPreservesData) { TEST(GrowMemoryInvalidSize) { { // Grow memory by an invalid amount without initial memory. - TestingModule module(kExecuteInterpreted); - WasmRunner<int32_t> r(&module, MachineType::Uint32()); + WasmRunner<int32_t, uint32_t> r(kExecuteInterpreted); BUILD(r, WASM_GROW_MEMORY(WASM_GET_LOCAL(0))); CHECK_EQ(-1, r.Call(1048575)); } { // Grow memory by an invalid amount without initial memory. - TestingModule module(kExecuteInterpreted); - WasmRunner<int32_t> r(&module, MachineType::Uint32()); - module.AddMemory(WasmModule::kPageSize); + WasmRunner<int32_t, uint32_t> r(kExecuteInterpreted); + r.module().AddMemory(WasmModule::kPageSize); BUILD(r, WASM_GROW_MEMORY(WASM_GET_LOCAL(0))); CHECK_EQ(-1, r.Call(1048575)); } @@ -338,9 +344,7 @@ TEST(GrowMemoryInvalidSize) { TEST(TestPossibleNondeterminism) { { // F32Div may produced NaN - TestingModule module(kExecuteInterpreted); - WasmRunner<float> r(&module, MachineType::Float32(), - MachineType::Float32()); + WasmRunner<float, float, float> r(kExecuteInterpreted); BUILD(r, WASM_F32_DIV(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); r.Call(1048575.5f, 2.5f); CHECK(!r.possible_nondeterminism()); @@ -349,8 +353,7 @@ TEST(TestPossibleNondeterminism) { } { // F32Sqrt may produced NaN - TestingModule module(kExecuteInterpreted); - WasmRunner<float> r(&module, MachineType::Float32()); + WasmRunner<float, float> r(kExecuteInterpreted); BUILD(r, WASM_F32_SQRT(WASM_GET_LOCAL(0))); r.Call(16.0f); CHECK(!r.possible_nondeterminism()); @@ -359,9 +362,7 @@ TEST(TestPossibleNondeterminism) { } { // F32Mul may produced NaN - TestingModule module(kExecuteInterpreted); - WasmRunner<float> r(&module, MachineType::Float32(), - MachineType::Float32()); + WasmRunner<float, float, float> r(kExecuteInterpreted); BUILD(r, WASM_F32_MUL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); r.Call(1048575.5f, 2.5f); CHECK(!r.possible_nondeterminism()); @@ -370,9 +371,7 @@ TEST(TestPossibleNondeterminism) { } { // F64Div may produced NaN - TestingModule module(kExecuteInterpreted); - WasmRunner<double> r(&module, MachineType::Float64(), - MachineType::Float64()); + WasmRunner<double, double, double> r(kExecuteInterpreted); BUILD(r, WASM_F64_DIV(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); r.Call(1048575.5, 2.5); CHECK(!r.possible_nondeterminism()); @@ -381,8 +380,7 @@ TEST(TestPossibleNondeterminism) { } { // F64Sqrt may produced NaN - TestingModule module(kExecuteInterpreted); - WasmRunner<double> r(&module, MachineType::Float64()); + WasmRunner<double, double> r(kExecuteInterpreted); BUILD(r, WASM_F64_SQRT(WASM_GET_LOCAL(0))); r.Call(1048575.5); CHECK(!r.possible_nondeterminism()); @@ -391,9 +389,7 @@ TEST(TestPossibleNondeterminism) { } { // F64Mul may produced NaN - TestingModule module(kExecuteInterpreted); - WasmRunner<double> r(&module, MachineType::Float64(), - MachineType::Float64()); + WasmRunner<double, double, double> r(kExecuteInterpreted); BUILD(r, WASM_F64_MUL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); r.Call(1048575.5, 2.5); CHECK(!r.possible_nondeterminism()); diff --git a/deps/v8/test/cctest/wasm/test-run-wasm-js.cc b/deps/v8/test/cctest/wasm/test-run-wasm-js.cc index 4a74128f50..ee6b066282 100644 --- a/deps/v8/test/cctest/wasm/test-run-wasm-js.cc +++ b/deps/v8/test/cctest/wasm/test-run-wasm-js.cc @@ -97,48 +97,36 @@ void EXPECT_CALL(double expected, Handle<JSFunction> jsfunc, double a, } // namespace TEST(Run_Int32Sub_jswrapped) { - CcTest::InitializeVM(); - TestSignatures sigs; - TestingModule module; - WasmFunctionCompiler t(sigs.i_ii(), &module); - BUILD(t, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - Handle<JSFunction> jsfunc = module.WrapCode(t.CompileAndAdd()); + WasmRunner<int, int, int> r(kExecuteCompiled); + BUILD(r, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); + Handle<JSFunction> jsfunc = r.module().WrapCode(r.function()->func_index); EXPECT_CALL(33, jsfunc, 44, 11); EXPECT_CALL(-8723487, jsfunc, -8000000, 723487); } TEST(Run_Float32Div_jswrapped) { - CcTest::InitializeVM(); - TestSignatures sigs; - TestingModule module; - WasmFunctionCompiler t(sigs.f_ff(), &module); - BUILD(t, WASM_F32_DIV(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - Handle<JSFunction> jsfunc = module.WrapCode(t.CompileAndAdd()); + WasmRunner<float, float, float> r(kExecuteCompiled); + BUILD(r, WASM_F32_DIV(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); + Handle<JSFunction> jsfunc = r.module().WrapCode(r.function()->func_index); EXPECT_CALL(92, jsfunc, 46, 0.5); EXPECT_CALL(64, jsfunc, -16, -0.25); } TEST(Run_Float64Add_jswrapped) { - CcTest::InitializeVM(); - TestSignatures sigs; - TestingModule module; - WasmFunctionCompiler t(sigs.d_dd(), &module); - BUILD(t, WASM_F64_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - Handle<JSFunction> jsfunc = module.WrapCode(t.CompileAndAdd()); + WasmRunner<double, double, double> r(kExecuteCompiled); + BUILD(r, WASM_F64_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); + Handle<JSFunction> jsfunc = r.module().WrapCode(r.function()->func_index); EXPECT_CALL(3, jsfunc, 2, 1); EXPECT_CALL(-5.5, jsfunc, -5.25, -0.25); } TEST(Run_I32Popcount_jswrapped) { - CcTest::InitializeVM(); - TestSignatures sigs; - TestingModule module; - WasmFunctionCompiler t(sigs.i_i(), &module); - BUILD(t, WASM_I32_POPCNT(WASM_GET_LOCAL(0))); - Handle<JSFunction> jsfunc = module.WrapCode(t.CompileAndAdd()); + WasmRunner<int, int> r(kExecuteCompiled); + BUILD(r, WASM_I32_POPCNT(WASM_GET_LOCAL(0))); + Handle<JSFunction> jsfunc = r.module().WrapCode(r.function()->func_index); EXPECT_CALL(2, jsfunc, 9, 0); EXPECT_CALL(3, jsfunc, 11, 0); @@ -146,15 +134,13 @@ TEST(Run_I32Popcount_jswrapped) { } TEST(Run_CallJS_Add_jswrapped) { - CcTest::InitializeVM(); + WasmRunner<int, int> r(kExecuteCompiled); TestSignatures sigs; - TestingModule module; - WasmFunctionCompiler t(sigs.i_i(), &module); uint32_t js_index = - module.AddJsFunction(sigs.i_i(), "(function(a) { return a + 99; })"); - BUILD(t, WASM_CALL_FUNCTION(js_index, WASM_GET_LOCAL(0))); + r.module().AddJsFunction(sigs.i_i(), "(function(a) { return a + 99; })"); + BUILD(r, WASM_CALL_FUNCTION(js_index, WASM_GET_LOCAL(0))); - Handle<JSFunction> jsfunc = module.WrapCode(t.CompileAndAdd()); + Handle<JSFunction> jsfunc = r.module().WrapCode(r.function()->func_index); EXPECT_CALL(101, jsfunc, 2, -8); EXPECT_CALL(199, jsfunc, 100, -1); @@ -164,16 +150,16 @@ TEST(Run_CallJS_Add_jswrapped) { void RunJSSelectTest(int which) { const int kMaxParams = 8; PredictableInputValues inputs(0x100); - LocalType type = kAstF64; - LocalType types[kMaxParams + 1] = {type, type, type, type, type, + ValueType type = kWasmF64; + ValueType types[kMaxParams + 1] = {type, type, type, type, type, type, type, type, type}; for (int num_params = which + 1; num_params < kMaxParams; num_params++) { HandleScope scope(CcTest::InitIsolateOnce()); FunctionSig sig(1, num_params, types); - TestingModule module; - uint32_t js_index = AddJSSelector(&module, &sig, which); - WasmFunctionCompiler t(&sig, &module); + WasmRunner<void> r(kExecuteCompiled); + uint32_t js_index = AddJSSelector(&r.module(), &sig, which); + WasmFunctionCompiler& t = r.NewFunction(&sig); { std::vector<byte> code; @@ -189,7 +175,7 @@ void RunJSSelectTest(int which) { t.Build(&code[0], &code[end]); } - Handle<JSFunction> jsfunc = module.WrapCode(t.CompileAndAdd()); + Handle<JSFunction> jsfunc = r.module().WrapCode(t.function_index()); double expected = inputs.arg_d(which); EXPECT_CALL(expected, jsfunc, 0.0, 0.0); } @@ -240,15 +226,15 @@ void RunWASMSelectTest(int which) { Isolate* isolate = CcTest::InitIsolateOnce(); const int kMaxParams = 8; for (int num_params = which + 1; num_params < kMaxParams; num_params++) { - LocalType type = kAstF64; - LocalType types[kMaxParams + 1] = {type, type, type, type, type, + ValueType type = kWasmF64; + ValueType types[kMaxParams + 1] = {type, type, type, type, type, type, type, type, type}; FunctionSig sig(1, num_params, types); - TestingModule module; - WasmFunctionCompiler t(&sig, &module); + WasmRunner<void> r(kExecuteCompiled); + WasmFunctionCompiler& t = r.NewFunction(&sig); BUILD(t, WASM_GET_LOCAL(which)); - Handle<JSFunction> jsfunc = module.WrapCode(t.CompileAndAdd()); + Handle<JSFunction> jsfunc = r.module().WrapCode(t.function_index()); Handle<Object> args[] = { isolate->factory()->NewNumber(inputs.arg_d(0)), @@ -311,16 +297,16 @@ void RunWASMSelectAlignTest(int num_args, int num_params) { Isolate* isolate = CcTest::InitIsolateOnce(); const int kMaxParams = 10; DCHECK_LE(num_args, kMaxParams); - LocalType type = kAstF64; - LocalType types[kMaxParams + 1] = {type, type, type, type, type, type, + ValueType type = kWasmF64; + ValueType types[kMaxParams + 1] = {type, type, type, type, type, type, type, type, type, type, type}; FunctionSig sig(1, num_params, types); for (int which = 0; which < num_params; which++) { - TestingModule module; - WasmFunctionCompiler t(&sig, &module); + WasmRunner<void> r(kExecuteCompiled); + WasmFunctionCompiler& t = r.NewFunction(&sig); BUILD(t, WASM_GET_LOCAL(which)); - Handle<JSFunction> jsfunc = module.WrapCode(t.CompileAndAdd()); + Handle<JSFunction> jsfunc = r.module().WrapCode(t.function_index()); Handle<Object> args[] = {isolate->factory()->NewNumber(inputs.arg_d(0)), isolate->factory()->NewNumber(inputs.arg_d(1)), @@ -407,10 +393,12 @@ void RunJSSelectAlignTest(int num_args, int num_params) { const int kMaxParams = 10; CHECK_LE(num_args, kMaxParams); CHECK_LE(num_params, kMaxParams); - LocalType type = kAstF64; - LocalType types[kMaxParams + 1] = {type, type, type, type, type, type, + ValueType type = kWasmF64; + ValueType types[kMaxParams + 1] = {type, type, type, type, type, type, type, type, type, type, type}; FunctionSig sig(1, num_params, types); + i::AccountingAllocator allocator; + Zone zone(&allocator, ZONE_NAME); // Build the calling code. std::vector<byte> code; @@ -419,21 +407,21 @@ void RunJSSelectAlignTest(int num_args, int num_params) { ADD_CODE(code, WASM_GET_LOCAL(i)); } - ADD_CODE(code, kExprCallFunction, 0); + uint8_t predicted_js_index = 1; + ADD_CODE(code, kExprCallFunction, predicted_js_index); size_t end = code.size(); code.push_back(0); // Call different select JS functions. for (int which = 0; which < num_params; which++) { - HandleScope scope(isolate); - TestingModule module; - uint32_t js_index = AddJSSelector(&module, &sig, which); - CHECK_EQ(0u, js_index); - WasmFunctionCompiler t(&sig, &module); + WasmRunner<void> r(kExecuteCompiled); + uint32_t js_index = AddJSSelector(&r.module(), &sig, which); + CHECK_EQ(predicted_js_index, js_index); + WasmFunctionCompiler& t = r.NewFunction(&sig); t.Build(&code[0], &code[end]); - Handle<JSFunction> jsfunc = module.WrapCode(t.CompileAndAdd()); + Handle<JSFunction> jsfunc = r.module().WrapCode(t.function_index()); Handle<Object> args[] = { factory->NewNumber(inputs.arg_d(0)), diff --git a/deps/v8/test/cctest/wasm/test-run-wasm-module.cc b/deps/v8/test/cctest/wasm/test-run-wasm-module.cc index 94054bd388..468dc81159 100644 --- a/deps/v8/test/cctest/wasm/test-run-wasm-module.cc +++ b/deps/v8/test/cctest/wasm/test-run-wasm-module.cc @@ -63,6 +63,12 @@ void TestModuleException(Zone* zone, WasmModuleBuilder* builder) { void ExportAsMain(WasmFunctionBuilder* f) { f->ExportAs(CStrVector("main")); } +#define EMIT_CODE_WITH_END(f, code) \ + do { \ + f->EmitCode(code, sizeof(code)); \ + f->Emit(kExprEnd); \ + } while (false) + } // namespace TEST(Run_WasmModule_Return114) { @@ -75,8 +81,8 @@ TEST(Run_WasmModule_Return114) { WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v()); ExportAsMain(f); - byte code[] = {WASM_I8(kReturnValue)}; - f->EmitCode(code, sizeof(code)); + byte code[] = {WASM_I32V_2(kReturnValue)}; + EMIT_CODE_WITH_END(f, code); TestModule(&zone, builder, kReturnValue); } Cleanup(); @@ -95,14 +101,14 @@ TEST(Run_WasmModule_CallAdd) { uint16_t param2 = 1; byte code1[] = { WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; - f1->EmitCode(code1, sizeof(code1)); + EMIT_CODE_WITH_END(f1, code1); WasmFunctionBuilder* f2 = builder->AddFunction(sigs.i_v()); ExportAsMain(f2); byte code2[] = { - WASM_CALL_FUNCTION(f1->func_index(), WASM_I8(77), WASM_I8(22))}; - f2->EmitCode(code2, sizeof(code2)); + WASM_CALL_FUNCTION(f1->func_index(), WASM_I32V_2(77), WASM_I32V_1(22))}; + EMIT_CODE_WITH_END(f2, code2); TestModule(&zone, builder, 99); } Cleanup(); @@ -120,8 +126,8 @@ TEST(Run_WasmModule_ReadLoadedDataSegment) { ExportAsMain(f); byte code[] = { - WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; - f->EmitCode(code, sizeof(code)); + WASM_LOAD_MEM(MachineType::Int32(), WASM_I32V_1(kDataSegmentDest0))}; + EMIT_CODE_WITH_END(f, code); byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; builder->AddDataSegment(data, sizeof(data), kDataSegmentDest0); TestModule(&zone, builder, 0xddccbbaa); @@ -139,16 +145,17 @@ TEST(Run_WasmModule_CheckMemoryIsZero) { WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v()); - uint16_t localIndex = f->AddLocal(kAstI32); + uint16_t localIndex = f->AddLocal(kWasmI32); ExportAsMain(f); byte code[] = {WASM_BLOCK_I( WASM_WHILE( WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), WASM_IF_ELSE( WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), - WASM_BRV(3, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), - WASM_I8(11))}; - f->EmitCode(code, sizeof(code)); + WASM_BRV(3, WASM_I32V_1(-1)), + WASM_INC_LOCAL_BY(localIndex, 4))), + WASM_I32V_1(11))}; + EMIT_CODE_WITH_END(f, code); TestModule(&zone, builder, 11); } Cleanup(); @@ -163,17 +170,17 @@ TEST(Run_WasmModule_CallMain_recursive) { WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v()); - uint16_t localIndex = f->AddLocal(kAstI32); + uint16_t localIndex = f->AddLocal(kWasmI32); ExportAsMain(f); byte code[] = { WASM_SET_LOCAL(localIndex, WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), - WASM_IF_ELSE_I(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), + WASM_IF_ELSE_I(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_1(5)), WASM_SEQ(WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, WASM_INC_LOCAL(localIndex)), WASM_CALL_FUNCTION0(0)), - WASM_I8(55))}; - f->EmitCode(code, sizeof(code)); + WASM_I32V_1(55))}; + EMIT_CODE_WITH_END(f, code); TestModule(&zone, builder, 55); } Cleanup(); @@ -186,18 +193,18 @@ TEST(Run_WasmModule_Global) { TestSignatures sigs; WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); - uint32_t global1 = builder->AddGlobal(kAstI32, 0); - uint32_t global2 = builder->AddGlobal(kAstI32, 0); + uint32_t global1 = builder->AddGlobal(kWasmI32, 0); + uint32_t global2 = builder->AddGlobal(kWasmI32, 0); WasmFunctionBuilder* f1 = builder->AddFunction(sigs.i_v()); byte code1[] = { WASM_I32_ADD(WASM_GET_GLOBAL(global1), WASM_GET_GLOBAL(global2))}; - f1->EmitCode(code1, sizeof(code1)); + EMIT_CODE_WITH_END(f1, code1); WasmFunctionBuilder* f2 = builder->AddFunction(sigs.i_v()); ExportAsMain(f2); byte code2[] = {WASM_SET_GLOBAL(global1, WASM_I32V_1(56)), WASM_SET_GLOBAL(global2, WASM_I32V_1(41)), WASM_RETURN1(WASM_CALL_FUNCTION0(f1->func_index()))}; - f2->EmitCode(code2, sizeof(code2)); + EMIT_CODE_WITH_END(f2, code2); TestModule(&zone, builder, 97); } Cleanup(); @@ -211,6 +218,18 @@ class WasmSerializationTest { SetUp(); } + static void BuildWireBytes(Zone* zone, ZoneBuffer* buffer) { + WasmModuleBuilder* builder = new (zone) WasmModuleBuilder(zone); + TestSignatures sigs; + + WasmFunctionBuilder* f = builder->AddFunction(sigs.i_i()); + byte code[] = {WASM_GET_LOCAL(0), kExprI32Const, 1, kExprI32Add}; + EMIT_CODE_WITH_END(f, code); + f->ExportAs(CStrVector(kFunctionName)); + + builder->WriteTo(*buffer); + } + void ClearSerializedData() { serialized_bytes_.first = nullptr; serialized_bytes_.second = 0; @@ -239,8 +258,8 @@ class WasmSerializationTest { ErrorThrower thrower(current_isolate(), ""); v8::Local<v8::WasmCompiledModule> deserialized_module; CHECK(Deserialize().ToLocal(&deserialized_module)); - Handle<JSObject> module_object = - Handle<JSObject>::cast(v8::Utils::OpenHandle(*deserialized_module)); + Handle<WasmModuleObject> module_object = Handle<WasmModuleObject>::cast( + v8::Utils::OpenHandle(*deserialized_module)); { DisallowHeapAllocation assume_no_gc; Handle<WasmCompiledModule> compiled_part( @@ -272,6 +291,8 @@ class WasmSerializationTest { TearDown(); } + v8::Isolate* current_isolate_v8() { return current_isolate_v8_; } + private: static const char* kFunctionName; @@ -284,19 +305,17 @@ class WasmSerializationTest { return serialized_bytes_; } - v8::Isolate* current_isolate_v8() { return current_isolate_v8_; } - void SetUp() { WasmModuleBuilder* builder = new (zone()) WasmModuleBuilder(zone()); TestSignatures sigs; WasmFunctionBuilder* f = builder->AddFunction(sigs.i_i()); byte code[] = {WASM_GET_LOCAL(0), kExprI32Const, 1, kExprI32Add}; - f->EmitCode(code, sizeof(code)); + EMIT_CODE_WITH_END(f, code); f->ExportAs(CStrVector(kFunctionName)); ZoneBuffer buffer(&zone_); - builder->WriteTo(buffer); + WasmSerializationTest::BuildWireBytes(zone(), &buffer); Isolate* serialization_isolate = CcTest::InitIsolateOnce(); ErrorThrower thrower(serialization_isolate, ""); @@ -315,8 +334,10 @@ class WasmSerializationTest { serialization_isolate, const_cast<WasmModule*>(decoding_result.val)); MaybeHandle<WasmCompiledModule> compiled_module = - decoding_result.val->CompileFunctions(serialization_isolate, - module_wrapper, &thrower); + decoding_result.val->CompileFunctions( + serialization_isolate, module_wrapper, &thrower, + ModuleWireBytes(buffer.begin(), buffer.end()), + Handle<Script>::null(), Vector<const byte>::empty()); CHECK(!compiled_module.is_null()); Handle<JSObject> module_obj = WasmModuleObject::New( serialization_isolate, compiled_module.ToHandleChecked()); @@ -410,6 +431,40 @@ TEST(DeserializeWireBytesAndSerializedDataInvalid) { Cleanup(); } +bool False(v8::Local<v8::Context> context) { return false; } + +TEST(BlockWasmCodeGen) { + v8::internal::AccountingAllocator allocator; + Zone zone(&allocator, ZONE_NAME); + ZoneBuffer buffer(&zone); + WasmSerializationTest::BuildWireBytes(&zone, &buffer); + Isolate* isolate = CcTest::InitIsolateOnce(); + HandleScope scope(isolate); + testing::SetupIsolateForWasmModule(isolate); + CcTest::isolate()->SetAllowCodeGenerationFromStringsCallback(False); + + ErrorThrower thrower(isolate, "block codegen"); + MaybeHandle<WasmModuleObject> ret = wasm::CreateModuleObjectFromBytes( + isolate, buffer.begin(), buffer.end(), &thrower, + wasm::ModuleOrigin::kWasmOrigin, Handle<v8::internal::Script>::null(), + Vector<const byte>::empty()); + CcTest::isolate()->SetAllowCodeGenerationFromStringsCallback(nullptr); + CHECK(ret.is_null()); + CHECK(thrower.error()); +} + +TEST(BlockWasmCodeGenAtDeserialization) { + WasmSerializationTest test; + { + HandleScope scope(test.current_isolate()); + test.current_isolate_v8()->SetAllowCodeGenerationFromStringsCallback(False); + v8::MaybeLocal<v8::WasmCompiledModule> nothing = test.Deserialize(); + CHECK(nothing.IsEmpty()); + } + Cleanup(test.current_isolate()); + Cleanup(); +} + TEST(MemorySize) { { // Initial memory size is 16, see wasm-module-builder.cc @@ -422,7 +477,7 @@ TEST(MemorySize) { WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v()); ExportAsMain(f); byte code[] = {WASM_MEMORY_SIZE}; - f->EmitCode(code, sizeof(code)); + EMIT_CODE_WITH_END(f, code); TestModule(&zone, builder, kExpectedValue); } Cleanup(); @@ -439,8 +494,9 @@ TEST(Run_WasmModule_MemSize_GrowMem) { WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v()); ExportAsMain(f); - byte code[] = {WASM_GROW_MEMORY(WASM_I8(10)), WASM_DROP, WASM_MEMORY_SIZE}; - f->EmitCode(code, sizeof(code)); + byte code[] = {WASM_GROW_MEMORY(WASM_I32V_1(10)), WASM_DROP, + WASM_MEMORY_SIZE}; + EMIT_CODE_WITH_END(f, code); TestModule(&zone, builder, kExpectedValue); } Cleanup(); @@ -458,7 +514,7 @@ TEST(GrowMemoryZero) { WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v()); ExportAsMain(f); byte code[] = {WASM_GROW_MEMORY(WASM_I32V(0))}; - f->EmitCode(code, sizeof(code)); + EMIT_CODE_WITH_END(f, code); TestModule(&zone, builder, kExpectedValue); } Cleanup(); @@ -529,14 +585,14 @@ TEST(TestInterruptLoop) { WASM_I32V(InterruptThread::signal_value_)), WASM_BR(1))), WASM_I32V(121)}; - f->EmitCode(code, sizeof(code)); + EMIT_CODE_WITH_END(f, code); ZoneBuffer buffer(&zone); builder->WriteTo(buffer); HandleScope scope(isolate); testing::SetupIsolateForWasmModule(isolate); ErrorThrower thrower(isolate, "Test"); - const Handle<JSObject> instance = + const Handle<WasmInstanceObject> instance = testing::CompileInstantiateWasmModuleForTesting( isolate, &thrower, buffer.begin(), buffer.end(), ModuleOrigin::kWasmOrigin); @@ -568,7 +624,7 @@ TEST(Run_WasmModule_GrowMemoryInIf) { ExportAsMain(f); byte code[] = {WASM_IF_ELSE_I(WASM_I32V(0), WASM_GROW_MEMORY(WASM_I32V(1)), WASM_I32V(12))}; - f->EmitCode(code, sizeof(code)); + EMIT_CODE_WITH_END(f, code); TestModule(&zone, builder, 12); } Cleanup(); @@ -587,10 +643,10 @@ TEST(Run_WasmModule_GrowMemOobOffset) { WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v()); ExportAsMain(f); - byte code[] = {WASM_GROW_MEMORY(WASM_I8(1)), + byte code[] = {WASM_GROW_MEMORY(WASM_I32V_1(1)), WASM_STORE_MEM(MachineType::Int32(), WASM_I32V(index), WASM_I32V(value))}; - f->EmitCode(code, sizeof(code)); + EMIT_CODE_WITH_END(f, code); TestModuleException(&zone, builder); } Cleanup(); @@ -613,7 +669,7 @@ TEST(Run_WasmModule_GrowMemOobFixedIndex) { WASM_STORE_MEM(MachineType::Int32(), WASM_I32V(index), WASM_I32V(value)), WASM_LOAD_MEM(MachineType::Int32(), WASM_I32V(index))}; - f->EmitCode(code, sizeof(code)); + EMIT_CODE_WITH_END(f, code); HandleScope scope(isolate); ZoneBuffer buffer(&zone); @@ -657,11 +713,11 @@ TEST(Run_WasmModule_GrowMemOobVariableIndex) { WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); WasmFunctionBuilder* f = builder->AddFunction(sigs.i_i()); ExportAsMain(f); - byte code[] = {WASM_GROW_MEMORY(WASM_I8(1)), WASM_DROP, + byte code[] = {WASM_GROW_MEMORY(WASM_I32V_1(1)), WASM_DROP, WASM_STORE_MEM(MachineType::Int32(), WASM_GET_LOCAL(0), WASM_I32V(value)), WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0))}; - f->EmitCode(code, sizeof(code)); + EMIT_CODE_WITH_END(f, code); HandleScope scope(isolate); ZoneBuffer buffer(&zone); @@ -714,13 +770,13 @@ TEST(Run_WasmModule_Global_init) { WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); uint32_t global1 = - builder->AddGlobal(kAstI32, false, false, WasmInitExpr(777777)); + builder->AddGlobal(kWasmI32, false, false, WasmInitExpr(777777)); uint32_t global2 = - builder->AddGlobal(kAstI32, false, false, WasmInitExpr(222222)); + builder->AddGlobal(kWasmI32, false, false, WasmInitExpr(222222)); WasmFunctionBuilder* f1 = builder->AddFunction(sigs.i_v()); byte code[] = { WASM_I32_ADD(WASM_GET_GLOBAL(global1), WASM_GET_GLOBAL(global2))}; - f1->EmitCode(code, sizeof(code)); + EMIT_CODE_WITH_END(f1, code); ExportAsMain(f1); TestModule(&zone, builder, 999999); } @@ -728,13 +784,13 @@ TEST(Run_WasmModule_Global_init) { } template <typename CType> -static void RunWasmModuleGlobalInitTest(LocalType type, CType expected) { +static void RunWasmModuleGlobalInitTest(ValueType type, CType expected) { { v8::internal::AccountingAllocator allocator; Zone zone(&allocator, ZONE_NAME); TestSignatures sigs; - LocalType types[] = {type}; + ValueType types[] = {type}; FunctionSig sig(1, 0, types); for (int padding = 0; padding < 5; padding++) { @@ -742,17 +798,17 @@ static void RunWasmModuleGlobalInitTest(LocalType type, CType expected) { WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); for (int i = 0; i < padding; i++) { // pad global before - builder->AddGlobal(kAstI32, false, false, WasmInitExpr(i + 20000)); + builder->AddGlobal(kWasmI32, false, false, WasmInitExpr(i + 20000)); } uint32_t global = builder->AddGlobal(type, false, false, WasmInitExpr(expected)); for (int i = 0; i < padding; i++) { // pad global after - builder->AddGlobal(kAstI32, false, false, WasmInitExpr(i + 30000)); + builder->AddGlobal(kWasmI32, false, false, WasmInitExpr(i + 30000)); } WasmFunctionBuilder* f1 = builder->AddFunction(&sig); byte code[] = {WASM_GET_GLOBAL(global)}; - f1->EmitCode(code, sizeof(code)); + EMIT_CODE_WITH_END(f1, code); ExportAsMain(f1); TestModule(&zone, builder, expected); } @@ -761,18 +817,18 @@ static void RunWasmModuleGlobalInitTest(LocalType type, CType expected) { } TEST(Run_WasmModule_Global_i32) { - RunWasmModuleGlobalInitTest<int32_t>(kAstI32, -983489); - RunWasmModuleGlobalInitTest<int32_t>(kAstI32, 11223344); + RunWasmModuleGlobalInitTest<int32_t>(kWasmI32, -983489); + RunWasmModuleGlobalInitTest<int32_t>(kWasmI32, 11223344); } TEST(Run_WasmModule_Global_f32) { - RunWasmModuleGlobalInitTest<float>(kAstF32, -983.9f); - RunWasmModuleGlobalInitTest<float>(kAstF32, 1122.99f); + RunWasmModuleGlobalInitTest<float>(kWasmF32, -983.9f); + RunWasmModuleGlobalInitTest<float>(kWasmF32, 1122.99f); } TEST(Run_WasmModule_Global_f64) { - RunWasmModuleGlobalInitTest<double>(kAstF64, -833.9); - RunWasmModuleGlobalInitTest<double>(kAstF64, 86374.25); + RunWasmModuleGlobalInitTest<double>(kWasmF64, -833.9); + RunWasmModuleGlobalInitTest<double>(kWasmF64, 86374.25); } TEST(InitDataAtTheUpperLimit) { @@ -867,7 +923,7 @@ TEST(EmptyMemoryEmptyDataSegment) { U32V_1(6), // section size ENTRY_COUNT(1), // -- 0, // linear memory index - WASM_I32V_1(24), // destination offset + WASM_I32V_1(0), // destination offset kExprEnd, U32V_1(0), // source size }; @@ -909,8 +965,8 @@ TEST(MemoryWithOOBEmptyDataSegment) { testing::CompileInstantiateWasmModuleForTesting(isolate, &thrower, data, data + arraysize(data), ModuleOrigin::kWasmOrigin); - // It should be possible to instantiate this module. - CHECK(!thrower.error()); + // It should not be possible to instantiate this module. + CHECK(thrower.error()); } Cleanup(); } diff --git a/deps/v8/test/cctest/wasm/test-run-wasm-relocation.cc b/deps/v8/test/cctest/wasm/test-run-wasm-relocation.cc index 614e9a4ba5..f34a1a323e 100644 --- a/deps/v8/test/cctest/wasm/test-run-wasm-relocation.cc +++ b/deps/v8/test/cctest/wasm/test-run-wasm-relocation.cc @@ -13,31 +13,29 @@ using namespace v8::internal; using namespace v8::internal::compiler; -#define FOREACH_TYPE(TEST_BODY) \ - TEST_BODY(int32_t, I32, WASM_I32_ADD) \ - TEST_BODY(int64_t, I64, WASM_I64_ADD) \ - TEST_BODY(float, F32, WASM_F32_ADD) \ - TEST_BODY(double, F64, WASM_F64_ADD) +#define FOREACH_TYPE(TEST_BODY) \ + TEST_BODY(int32_t, WASM_I32_ADD) \ + TEST_BODY(int64_t, WASM_I64_ADD) \ + TEST_BODY(float, WASM_F32_ADD) \ + TEST_BODY(double, WASM_F64_ADD) -#define LOAD_SET_GLOBAL_TEST_BODY(C_TYPE, MACHINE_TYPE, ADD) \ - TEST(WasmRelocateGlobal##MACHINE_TYPE) { \ - TestingModule module(kExecuteCompiled); \ - module.AddGlobal<C_TYPE>(kAst##MACHINE_TYPE); \ - module.AddGlobal<C_TYPE>(kAst##MACHINE_TYPE); \ +#define LOAD_SET_GLOBAL_TEST_BODY(C_TYPE, ADD) \ + TEST(WasmRelocateGlobal_##C_TYPE) { \ + WasmRunner<C_TYPE, C_TYPE> r(kExecuteCompiled); \ \ - WasmRunner<C_TYPE> r(&module, \ - WasmOpcodes::MachineTypeFor(kAst##MACHINE_TYPE)); \ + r.module().AddGlobal<C_TYPE>(); \ + r.module().AddGlobal<C_TYPE>(); \ \ /* global = global + p0 */ \ BUILD(r, WASM_SET_GLOBAL(1, ADD(WASM_GET_GLOBAL(0), WASM_GET_LOCAL(0))), \ WASM_GET_GLOBAL(0)); \ - CHECK_EQ(1u, module.instance->function_code.size()); \ + CHECK_EQ(1, r.module().instance->function_code.size()); \ \ int filter = 1 << RelocInfo::WASM_GLOBAL_REFERENCE; \ \ - Handle<Code> code = module.instance->function_code[0]; \ + Handle<Code> code = r.module().instance->function_code[0]; \ \ - Address old_start = module.instance->globals_start; \ + Address old_start = r.module().instance->globals_start; \ Address new_start = old_start + 1; \ \ Address old_addresses[4]; \ diff --git a/deps/v8/test/cctest/wasm/test-run-wasm-simd-lowering.cc b/deps/v8/test/cctest/wasm/test-run-wasm-simd-lowering.cc index 69e770b9b8..706de8de35 100644 --- a/deps/v8/test/cctest/wasm/test-run-wasm-simd-lowering.cc +++ b/deps/v8/test/cctest/wasm/test-run-wasm-simd-lowering.cc @@ -15,82 +15,282 @@ using namespace v8::internal; using namespace v8::internal::compiler; using namespace v8::internal::wasm; -WASM_EXEC_TEST(Simd_I32x4_Splat) { +WASM_EXEC_COMPILED_TEST(Simd_I32x4_Splat) { FLAG_wasm_simd_prototype = true; - WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32()); + WasmRunner<int32_t> r(kExecuteCompiled); BUILD(r, WASM_SIMD_I32x4_EXTRACT_LANE(0, WASM_SIMD_I32x4_SPLAT(WASM_I32V(5)))); FOR_INT32_INPUTS(i) { CHECK_EQ(5, r.Call()); } } -WASM_EXEC_TEST(Simd_I32x4_Add) { +WASM_EXEC_COMPILED_TEST(Simd_I32x4_Add) { FLAG_wasm_simd_prototype = true; - WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32()); + WasmRunner<int32_t> r(kExecuteCompiled); BUILD(r, WASM_SIMD_I32x4_EXTRACT_LANE( 0, WASM_SIMD_I32x4_ADD(WASM_SIMD_I32x4_SPLAT(WASM_I32V(5)), WASM_SIMD_I32x4_SPLAT(WASM_I32V(6))))); FOR_INT32_INPUTS(i) { CHECK_EQ(11, r.Call()); } } -WASM_EXEC_TEST(Simd_F32x4_Splat) { +WASM_EXEC_COMPILED_TEST(Simd_F32x4_Splat) { FLAG_wasm_simd_prototype = true; - WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32()); + WasmRunner<int32_t> r(kExecuteCompiled); BUILD(r, - WASM_IF_ELSE(WASM_F32_EQ(WASM_SIMD_F32x4_EXTRACT_LANE( - 0, WASM_SIMD_F32x4_SPLAT(WASM_F32(9.5))), - WASM_F32(9.5)), - WASM_RETURN1(WASM_I32V(1)), WASM_RETURN1(WASM_I32V(0)))); + WASM_IF_ELSE_I(WASM_F32_EQ(WASM_SIMD_F32x4_EXTRACT_LANE( + 0, WASM_SIMD_F32x4_SPLAT(WASM_F32(9.5))), + WASM_F32(9.5)), + WASM_I32V(1), WASM_I32V(0))); FOR_INT32_INPUTS(i) { CHECK_EQ(1, r.Call()); } } -WASM_EXEC_TEST(Simd_I32x4_Extract_With_F32x4) { +WASM_EXEC_COMPILED_TEST(Simd_I32x4_Extract_With_F32x4) { FLAG_wasm_simd_prototype = true; - WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32()); - BUILD(r, - WASM_IF_ELSE(WASM_I32_EQ(WASM_SIMD_I32x4_EXTRACT_LANE( - 0, WASM_SIMD_F32x4_SPLAT(WASM_F32(30.5))), - WASM_I32_REINTERPRET_F32(WASM_F32(30.5))), - WASM_RETURN1(WASM_I32V(1)), WASM_RETURN1(WASM_I32V(0)))); + WasmRunner<int32_t> r(kExecuteCompiled); + BUILD(r, WASM_IF_ELSE_I( + WASM_I32_EQ(WASM_SIMD_I32x4_EXTRACT_LANE( + 0, WASM_SIMD_F32x4_SPLAT(WASM_F32(30.5))), + WASM_I32_REINTERPRET_F32(WASM_F32(30.5))), + WASM_I32V(1), WASM_I32V(0))); FOR_INT32_INPUTS(i) { CHECK_EQ(1, r.Call()); } } -WASM_EXEC_TEST(Simd_F32x4_Extract_With_I32x4) { +WASM_EXEC_COMPILED_TEST(Simd_F32x4_Extract_With_I32x4) { FLAG_wasm_simd_prototype = true; - WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32()); + WasmRunner<int32_t> r(kExecuteCompiled); BUILD(r, - WASM_IF_ELSE(WASM_F32_EQ(WASM_SIMD_F32x4_EXTRACT_LANE( - 0, WASM_SIMD_I32x4_SPLAT(WASM_I32V(15))), - WASM_F32_REINTERPRET_I32(WASM_I32V(15))), - WASM_RETURN1(WASM_I32V(1)), WASM_RETURN1(WASM_I32V(0)))); + WASM_IF_ELSE_I(WASM_F32_EQ(WASM_SIMD_F32x4_EXTRACT_LANE( + 0, WASM_SIMD_I32x4_SPLAT(WASM_I32V(15))), + WASM_F32_REINTERPRET_I32(WASM_I32V(15))), + WASM_I32V(1), WASM_I32V(0))); FOR_INT32_INPUTS(i) { CHECK_EQ(1, r.Call()); } } -WASM_EXEC_TEST(Simd_F32x4_Add_With_I32x4) { +WASM_EXEC_COMPILED_TEST(Simd_F32x4_Add_With_I32x4) { FLAG_wasm_simd_prototype = true; - WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32()); + WasmRunner<int32_t> r(kExecuteCompiled); BUILD(r, - WASM_IF_ELSE( + WASM_IF_ELSE_I( WASM_F32_EQ(WASM_SIMD_F32x4_EXTRACT_LANE( 0, WASM_SIMD_F32x4_ADD( WASM_SIMD_I32x4_SPLAT(WASM_I32V(32)), WASM_SIMD_I32x4_SPLAT(WASM_I32V(19)))), WASM_F32_ADD(WASM_F32_REINTERPRET_I32(WASM_I32V(32)), WASM_F32_REINTERPRET_I32(WASM_I32V(19)))), - WASM_RETURN1(WASM_I32V(1)), WASM_RETURN1(WASM_I32V(0)))); + WASM_I32V(1), WASM_I32V(0))); FOR_INT32_INPUTS(i) { CHECK_EQ(1, r.Call()); } } -WASM_EXEC_TEST(Simd_I32x4_Add_With_F32x4) { +WASM_EXEC_COMPILED_TEST(Simd_I32x4_Add_With_F32x4) { FLAG_wasm_simd_prototype = true; - WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32()); + WasmRunner<int32_t> r(kExecuteCompiled); BUILD(r, - WASM_IF_ELSE( + WASM_IF_ELSE_I( WASM_I32_EQ(WASM_SIMD_I32x4_EXTRACT_LANE( 0, WASM_SIMD_I32x4_ADD( WASM_SIMD_F32x4_SPLAT(WASM_F32(21.25)), WASM_SIMD_F32x4_SPLAT(WASM_F32(31.5)))), WASM_I32_ADD(WASM_I32_REINTERPRET_F32(WASM_F32(21.25)), WASM_I32_REINTERPRET_F32(WASM_F32(31.5)))), - WASM_RETURN1(WASM_I32V(1)), WASM_RETURN1(WASM_I32V(0)))); + WASM_I32V(1), WASM_I32V(0))); + FOR_INT32_INPUTS(i) { CHECK_EQ(1, r.Call()); } +} + +WASM_EXEC_COMPILED_TEST(Simd_I32x4_Local) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t> r(kExecuteCompiled); + r.AllocateLocal(kWasmS128); + BUILD(r, WASM_SET_LOCAL(0, WASM_SIMD_I32x4_SPLAT(WASM_I32V(31))), + + WASM_SIMD_I32x4_EXTRACT_LANE(0, WASM_GET_LOCAL(0))); + FOR_INT32_INPUTS(i) { CHECK_EQ(31, r.Call()); } +} + +WASM_EXEC_COMPILED_TEST(Simd_I32x4_Replace_Lane) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t> r(kExecuteCompiled); + r.AllocateLocal(kWasmS128); + BUILD(r, WASM_SET_LOCAL(0, WASM_SIMD_I32x4_SPLAT(WASM_I32V(31))), + WASM_SET_LOCAL(0, WASM_SIMD_I32x4_REPLACE_LANE(2, WASM_GET_LOCAL(0), + WASM_I32V(53))), + WASM_SIMD_I32x4_EXTRACT_LANE(2, WASM_GET_LOCAL(0))); + FOR_INT32_INPUTS(i) { CHECK_EQ(53, r.Call()); } +} + +WASM_EXEC_COMPILED_TEST(Simd_F32x4_Replace_Lane) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t> r(kExecuteCompiled); + r.AllocateLocal(kWasmF32); + r.AllocateLocal(kWasmS128); + BUILD(r, WASM_SET_LOCAL(1, WASM_SIMD_F32x4_SPLAT(WASM_F32(23.5))), + WASM_SET_LOCAL(1, WASM_SIMD_F32x4_REPLACE_LANE(3, WASM_GET_LOCAL(1), + WASM_F32(65.25))), + WASM_SET_LOCAL(0, WASM_SIMD_F32x4_EXTRACT_LANE(3, WASM_GET_LOCAL(1))), + WASM_IF(WASM_F32_EQ(WASM_GET_LOCAL(0), WASM_F32(65.25)), + WASM_RETURN1(WASM_I32V(1))), + WASM_I32V(0)); + FOR_INT32_INPUTS(i) { CHECK_EQ(1, r.Call()); } +} + +WASM_EXEC_COMPILED_TEST(Simd_I32x4_Splat_From_Extract) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t> r(kExecuteCompiled); + r.AllocateLocal(kWasmI32); + r.AllocateLocal(kWasmS128); + BUILD(r, WASM_SET_LOCAL(0, WASM_SIMD_I32x4_EXTRACT_LANE( + 0, WASM_SIMD_I32x4_SPLAT(WASM_I32V(76)))), + WASM_SET_LOCAL(1, WASM_SIMD_I32x4_SPLAT(WASM_GET_LOCAL(0))), + WASM_SIMD_I32x4_EXTRACT_LANE(1, WASM_GET_LOCAL(1))); + FOR_INT32_INPUTS(i) { CHECK_EQ(76, r.Call()); } +} + +WASM_EXEC_COMPILED_TEST(Simd_I32x4_Get_Global) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t, int32_t> r(kExecuteCompiled); + int32_t* global = r.module().AddGlobal<int32_t>(kWasmS128); + *(global) = 0; + *(global + 1) = 1; + *(global + 2) = 2; + *(global + 3) = 3; + r.AllocateLocal(kWasmI32); + BUILD( + r, WASM_SET_LOCAL(1, WASM_I32V(1)), + WASM_IF(WASM_I32_NE(WASM_I32V(0), + WASM_SIMD_I32x4_EXTRACT_LANE(0, WASM_GET_GLOBAL(0))), + WASM_SET_LOCAL(1, WASM_I32V(0))), + WASM_IF(WASM_I32_NE(WASM_I32V(1), + WASM_SIMD_I32x4_EXTRACT_LANE(1, WASM_GET_GLOBAL(0))), + WASM_SET_LOCAL(1, WASM_I32V(0))), + WASM_IF(WASM_I32_NE(WASM_I32V(2), + WASM_SIMD_I32x4_EXTRACT_LANE(2, WASM_GET_GLOBAL(0))), + WASM_SET_LOCAL(1, WASM_I32V(0))), + WASM_IF(WASM_I32_NE(WASM_I32V(3), + WASM_SIMD_I32x4_EXTRACT_LANE(3, WASM_GET_GLOBAL(0))), + WASM_SET_LOCAL(1, WASM_I32V(0))), + WASM_GET_LOCAL(1)); + FOR_INT32_INPUTS(i) { CHECK_EQ(1, r.Call(0)); } +} + +WASM_EXEC_COMPILED_TEST(Simd_I32x4_Set_Global) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t, int32_t> r(kExecuteCompiled); + int32_t* global = r.module().AddGlobal<int32_t>(kWasmS128); + BUILD(r, WASM_SET_GLOBAL(0, WASM_SIMD_I32x4_SPLAT(WASM_I32V(23))), + WASM_SET_GLOBAL(0, WASM_SIMD_I32x4_REPLACE_LANE(1, WASM_GET_GLOBAL(0), + WASM_I32V(34))), + WASM_SET_GLOBAL(0, WASM_SIMD_I32x4_REPLACE_LANE(2, WASM_GET_GLOBAL(0), + WASM_I32V(45))), + WASM_SET_GLOBAL(0, WASM_SIMD_I32x4_REPLACE_LANE(3, WASM_GET_GLOBAL(0), + WASM_I32V(56))), + WASM_I32V(1)); + FOR_INT32_INPUTS(i) { CHECK_EQ(1, r.Call(0)); } + CHECK_EQ(*global, 23); + CHECK_EQ(*(global + 1), 34); + CHECK_EQ(*(global + 2), 45); + CHECK_EQ(*(global + 3), 56); +} + +WASM_EXEC_COMPILED_TEST(Simd_F32x4_Get_Global) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t, int32_t> r(kExecuteCompiled); + float* global = r.module().AddGlobal<float>(kWasmS128); + *(global) = 0.0; + *(global + 1) = 1.5; + *(global + 2) = 2.25; + *(global + 3) = 3.5; + r.AllocateLocal(kWasmI32); + BUILD( + r, WASM_SET_LOCAL(1, WASM_I32V(1)), + WASM_IF(WASM_F32_NE(WASM_F32(0.0), + WASM_SIMD_F32x4_EXTRACT_LANE(0, WASM_GET_GLOBAL(0))), + WASM_SET_LOCAL(1, WASM_I32V(0))), + WASM_IF(WASM_F32_NE(WASM_F32(1.5), + WASM_SIMD_F32x4_EXTRACT_LANE(1, WASM_GET_GLOBAL(0))), + WASM_SET_LOCAL(1, WASM_I32V(0))), + WASM_IF(WASM_F32_NE(WASM_F32(2.25), + WASM_SIMD_F32x4_EXTRACT_LANE(2, WASM_GET_GLOBAL(0))), + WASM_SET_LOCAL(1, WASM_I32V(0))), + WASM_IF(WASM_F32_NE(WASM_F32(3.5), + WASM_SIMD_F32x4_EXTRACT_LANE(3, WASM_GET_GLOBAL(0))), + WASM_SET_LOCAL(1, WASM_I32V(0))), + WASM_GET_LOCAL(1)); + FOR_INT32_INPUTS(i) { CHECK_EQ(1, r.Call(0)); } +} + +WASM_EXEC_COMPILED_TEST(Simd_F32x4_Set_Global) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t, int32_t> r(kExecuteCompiled); + float* global = r.module().AddGlobal<float>(kWasmS128); + BUILD(r, WASM_SET_GLOBAL(0, WASM_SIMD_F32x4_SPLAT(WASM_F32(13.5))), + WASM_SET_GLOBAL(0, WASM_SIMD_F32x4_REPLACE_LANE(1, WASM_GET_GLOBAL(0), + WASM_F32(45.5))), + WASM_SET_GLOBAL(0, WASM_SIMD_F32x4_REPLACE_LANE(2, WASM_GET_GLOBAL(0), + WASM_F32(32.25))), + WASM_SET_GLOBAL(0, WASM_SIMD_F32x4_REPLACE_LANE(3, WASM_GET_GLOBAL(0), + WASM_F32(65.0))), + WASM_I32V(1)); + FOR_INT32_INPUTS(i) { CHECK_EQ(1, r.Call(0)); } + CHECK_EQ(*global, 13.5); + CHECK_EQ(*(global + 1), 45.5); + CHECK_EQ(*(global + 2), 32.25); + CHECK_EQ(*(global + 3), 65.0); +} + +WASM_EXEC_COMPILED_TEST(Simd_I32x4_For) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t> r(kExecuteCompiled); + r.AllocateLocal(kWasmI32); + r.AllocateLocal(kWasmS128); + BUILD(r, + + WASM_SET_LOCAL(1, WASM_SIMD_I32x4_SPLAT(WASM_I32V(31))), + WASM_SET_LOCAL(1, WASM_SIMD_I32x4_REPLACE_LANE(1, WASM_GET_LOCAL(1), + WASM_I32V(53))), + WASM_SET_LOCAL(1, WASM_SIMD_I32x4_REPLACE_LANE(2, WASM_GET_LOCAL(1), + WASM_I32V(23))), + WASM_SET_LOCAL(0, WASM_I32V(0)), + WASM_LOOP( + WASM_SET_LOCAL( + 1, WASM_SIMD_I32x4_ADD(WASM_GET_LOCAL(1), + WASM_SIMD_I32x4_SPLAT(WASM_I32V(1)))), + WASM_IF(WASM_I32_NE(WASM_INC_LOCAL(0), WASM_I32V(5)), WASM_BR(1))), + WASM_SET_LOCAL(0, WASM_I32V(1)), + WASM_IF(WASM_I32_NE(WASM_SIMD_I32x4_EXTRACT_LANE(0, WASM_GET_LOCAL(1)), + WASM_I32V(36)), + WASM_SET_LOCAL(0, WASM_I32V(0))), + WASM_IF(WASM_I32_NE(WASM_SIMD_I32x4_EXTRACT_LANE(1, WASM_GET_LOCAL(1)), + WASM_I32V(58)), + WASM_SET_LOCAL(0, WASM_I32V(0))), + WASM_IF(WASM_I32_NE(WASM_SIMD_I32x4_EXTRACT_LANE(2, WASM_GET_LOCAL(1)), + WASM_I32V(28)), + WASM_SET_LOCAL(0, WASM_I32V(0))), + WASM_IF(WASM_I32_NE(WASM_SIMD_I32x4_EXTRACT_LANE(3, WASM_GET_LOCAL(1)), + WASM_I32V(36)), + WASM_SET_LOCAL(0, WASM_I32V(0))), + WASM_GET_LOCAL(0)); + FOR_INT32_INPUTS(i) { CHECK_EQ(1, r.Call()); } +} + +WASM_EXEC_COMPILED_TEST(Simd_F32x4_For) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t> r(kExecuteCompiled); + r.AllocateLocal(kWasmI32); + r.AllocateLocal(kWasmS128); + BUILD(r, WASM_SET_LOCAL(1, WASM_SIMD_F32x4_SPLAT(WASM_F32(21.25))), + WASM_SET_LOCAL(1, WASM_SIMD_F32x4_REPLACE_LANE(3, WASM_GET_LOCAL(1), + WASM_F32(19.5))), + WASM_SET_LOCAL(0, WASM_I32V(0)), + WASM_LOOP( + WASM_SET_LOCAL( + 1, WASM_SIMD_F32x4_ADD(WASM_GET_LOCAL(1), + WASM_SIMD_F32x4_SPLAT(WASM_F32(2.0)))), + WASM_IF(WASM_I32_NE(WASM_INC_LOCAL(0), WASM_I32V(3)), WASM_BR(1))), + WASM_SET_LOCAL(0, WASM_I32V(1)), + WASM_IF(WASM_F32_NE(WASM_SIMD_F32x4_EXTRACT_LANE(0, WASM_GET_LOCAL(1)), + WASM_F32(27.25)), + WASM_SET_LOCAL(0, WASM_I32V(0))), + WASM_IF(WASM_F32_NE(WASM_SIMD_F32x4_EXTRACT_LANE(3, WASM_GET_LOCAL(1)), + WASM_F32(25.5)), + WASM_SET_LOCAL(0, WASM_I32V(0))), + WASM_GET_LOCAL(0)); FOR_INT32_INPUTS(i) { CHECK_EQ(1, r.Call()); } } diff --git a/deps/v8/test/cctest/wasm/test-run-wasm-simd.cc b/deps/v8/test/cctest/wasm/test-run-wasm-simd.cc index 76eac5e793..9764545d45 100644 --- a/deps/v8/test/cctest/wasm/test-run-wasm-simd.cc +++ b/deps/v8/test/cctest/wasm/test-run-wasm-simd.cc @@ -13,7 +13,259 @@ using namespace v8::internal; using namespace v8::internal::compiler; using namespace v8::internal::wasm; -WASM_EXEC_TEST(Splat) { +namespace { + +typedef float (*FloatUnOp)(float); +typedef float (*FloatBinOp)(float, float); +typedef int32_t (*FloatCompareOp)(float, float); +typedef int32_t (*Int32BinOp)(int32_t, int32_t); + +template <typename T> +T Negate(T a) { + return -a; +} + +template <typename T> +T Add(T a, T b) { + return a + b; +} + +template <typename T> +T Sub(T a, T b) { + return a - b; +} + +template <typename T> +int32_t Equal(T a, T b) { + return a == b ? 0xFFFFFFFF : 0; +} + +template <typename T> +int32_t NotEqual(T a, T b) { + return a != b ? 0xFFFFFFFF : 0; +} + +#if V8_TARGET_ARCH_ARM +int32_t Equal(float a, float b) { return a == b ? 0xFFFFFFFF : 0; } + +int32_t NotEqual(float a, float b) { return a != b ? 0xFFFFFFFF : 0; } +#endif // V8_TARGET_ARCH_ARM + +} // namespace + +// TODO(gdeepti): These are tests using sample values to verify functional +// correctness of opcodes, add more tests for a range of values and macroize +// tests. + +// TODO(bbudge) Figure out how to compare floats in Wasm code that can handle +// NaNs. For now, our tests avoid using NaNs. +#define WASM_SIMD_CHECK_LANE(TYPE, value, LANE_TYPE, lane_value, lane_index) \ + WASM_IF(WASM_##LANE_TYPE##_NE(WASM_GET_LOCAL(lane_value), \ + WASM_SIMD_##TYPE##_EXTRACT_LANE( \ + lane_index, WASM_GET_LOCAL(value))), \ + WASM_RETURN1(WASM_ZERO)) + +#define WASM_SIMD_CHECK4(TYPE, value, LANE_TYPE, lv0, lv1, lv2, lv3) \ + WASM_SIMD_CHECK_LANE(TYPE, value, LANE_TYPE, lv0, 0) \ + , WASM_SIMD_CHECK_LANE(TYPE, value, LANE_TYPE, lv1, 1), \ + WASM_SIMD_CHECK_LANE(TYPE, value, LANE_TYPE, lv2, 2), \ + WASM_SIMD_CHECK_LANE(TYPE, value, LANE_TYPE, lv3, 3) + +#define WASM_SIMD_CHECK_SPLAT4(TYPE, value, LANE_TYPE, lv) \ + WASM_SIMD_CHECK4(TYPE, value, LANE_TYPE, lv, lv, lv, lv) + +#define WASM_SIMD_CHECK_F32_LANE(TYPE, value, lane_value, lane_index) \ + WASM_IF( \ + WASM_I32_NE(WASM_I32_REINTERPRET_F32(WASM_GET_LOCAL(lane_value)), \ + WASM_I32_REINTERPRET_F32(WASM_SIMD_##TYPE##_EXTRACT_LANE( \ + lane_index, WASM_GET_LOCAL(value)))), \ + WASM_RETURN1(WASM_ZERO)) + +#define WASM_SIMD_CHECK4_F32(TYPE, value, lv0, lv1, lv2, lv3) \ + WASM_SIMD_CHECK_F32_LANE(TYPE, value, lv0, 0) \ + , WASM_SIMD_CHECK_F32_LANE(TYPE, value, lv1, 1), \ + WASM_SIMD_CHECK_F32_LANE(TYPE, value, lv2, 2), \ + WASM_SIMD_CHECK_F32_LANE(TYPE, value, lv3, 3) + +#define WASM_SIMD_CHECK_SPLAT4_F32(TYPE, value, lv) \ + WASM_SIMD_CHECK4_F32(TYPE, value, lv, lv, lv, lv) + +#if V8_TARGET_ARCH_ARM +WASM_EXEC_TEST(F32x4Splat) { + FLAG_wasm_simd_prototype = true; + + WasmRunner<int32_t, float> r(kExecuteCompiled); + byte lane_val = 0; + byte simd = r.AllocateLocal(kWasmS128); + BUILD(r, + WASM_SET_LOCAL(simd, WASM_SIMD_F32x4_SPLAT(WASM_GET_LOCAL(lane_val))), + WASM_SIMD_CHECK_SPLAT4_F32(F32x4, simd, lane_val), WASM_ONE); + + FOR_FLOAT32_INPUTS(i) { CHECK_EQ(1, r.Call(*i)); } +} + +WASM_EXEC_TEST(F32x4ReplaceLane) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t, float, float> r(kExecuteCompiled); + byte old_val = 0; + byte new_val = 1; + byte simd = r.AllocateLocal(kWasmS128); + BUILD(r, WASM_SET_LOCAL(simd, WASM_SIMD_F32x4_SPLAT(WASM_GET_LOCAL(old_val))), + WASM_SET_LOCAL(simd, + WASM_SIMD_F32x4_REPLACE_LANE(0, WASM_GET_LOCAL(simd), + WASM_GET_LOCAL(new_val))), + WASM_SIMD_CHECK4(F32x4, simd, F32, new_val, old_val, old_val, old_val), + WASM_SET_LOCAL(simd, + WASM_SIMD_F32x4_REPLACE_LANE(1, WASM_GET_LOCAL(simd), + WASM_GET_LOCAL(new_val))), + WASM_SIMD_CHECK4(F32x4, simd, F32, new_val, new_val, old_val, old_val), + WASM_SET_LOCAL(simd, + WASM_SIMD_F32x4_REPLACE_LANE(2, WASM_GET_LOCAL(simd), + WASM_GET_LOCAL(new_val))), + WASM_SIMD_CHECK4(F32x4, simd, F32, new_val, new_val, new_val, old_val), + WASM_SET_LOCAL(simd, + WASM_SIMD_F32x4_REPLACE_LANE(3, WASM_GET_LOCAL(simd), + WASM_GET_LOCAL(new_val))), + WASM_SIMD_CHECK_SPLAT4(F32x4, simd, F32, new_val), WASM_ONE); + + CHECK_EQ(1, r.Call(3.14159, -1.5)); +} + +// Tests both signed and unsigned conversion. +WASM_EXEC_TEST(F32x4FromInt32x4) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t, int32_t, float, float> r(kExecuteCompiled); + byte a = 0; + byte expected_signed = 1; + byte expected_unsigned = 2; + byte simd0 = r.AllocateLocal(kWasmS128); + byte simd1 = r.AllocateLocal(kWasmS128); + byte simd2 = r.AllocateLocal(kWasmS128); + BUILD( + r, WASM_SET_LOCAL(simd0, WASM_SIMD_I32x4_SPLAT(WASM_GET_LOCAL(a))), + WASM_SET_LOCAL(simd1, WASM_SIMD_F32x4_FROM_I32x4(WASM_GET_LOCAL(simd0))), + WASM_SIMD_CHECK_SPLAT4_F32(F32x4, simd1, expected_signed), + WASM_SET_LOCAL(simd2, WASM_SIMD_F32x4_FROM_U32x4(WASM_GET_LOCAL(simd0))), + WASM_SIMD_CHECK_SPLAT4_F32(F32x4, simd2, expected_unsigned), WASM_ONE); + + FOR_INT32_INPUTS(i) { + CHECK_EQ(1, r.Call(*i, static_cast<float>(*i), + static_cast<float>(static_cast<uint32_t>(*i)))); + } +} + +WASM_EXEC_TEST(S32x4Select) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t, int32_t, int32_t> r(kExecuteCompiled); + byte val1 = 0; + byte val2 = 1; + byte mask = r.AllocateLocal(kWasmS128); + byte src1 = r.AllocateLocal(kWasmS128); + byte src2 = r.AllocateLocal(kWasmS128); + BUILD(r, + + WASM_SET_LOCAL(mask, WASM_SIMD_I32x4_SPLAT(WASM_ZERO)), + WASM_SET_LOCAL(src1, WASM_SIMD_I32x4_SPLAT(WASM_GET_LOCAL(val1))), + WASM_SET_LOCAL(src2, WASM_SIMD_I32x4_SPLAT(WASM_GET_LOCAL(val2))), + WASM_SET_LOCAL(mask, WASM_SIMD_I32x4_REPLACE_LANE( + 1, WASM_GET_LOCAL(mask), WASM_I32V(-1))), + WASM_SET_LOCAL(mask, WASM_SIMD_I32x4_REPLACE_LANE( + 2, WASM_GET_LOCAL(mask), WASM_I32V(-1))), + WASM_SET_LOCAL(mask, WASM_SIMD_S32x4_SELECT(WASM_GET_LOCAL(mask), + WASM_GET_LOCAL(src1), + WASM_GET_LOCAL(src2))), + WASM_SIMD_CHECK_LANE(I32x4, mask, I32, val2, 0), + WASM_SIMD_CHECK_LANE(I32x4, mask, I32, val1, 1), + WASM_SIMD_CHECK_LANE(I32x4, mask, I32, val1, 2), + WASM_SIMD_CHECK_LANE(I32x4, mask, I32, val2, 3), WASM_ONE); + + CHECK_EQ(1, r.Call(0x1234, 0x5678)); +} + +void RunF32x4UnOpTest(WasmOpcode simd_op, FloatUnOp expected_op) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t, float, float> r(kExecuteCompiled); + byte a = 0; + byte expected = 1; + byte simd = r.AllocateLocal(kWasmS128); + BUILD(r, WASM_SET_LOCAL(simd, WASM_SIMD_F32x4_SPLAT(WASM_GET_LOCAL(a))), + WASM_SET_LOCAL(simd, + WASM_SIMD_UNOP(simd_op & 0xffu, WASM_GET_LOCAL(simd))), + WASM_SIMD_CHECK_SPLAT4_F32(F32x4, simd, expected), WASM_ONE); + + FOR_FLOAT32_INPUTS(i) { + if (std::isnan(*i)) continue; + CHECK_EQ(1, r.Call(*i, expected_op(*i))); + } +} + +WASM_EXEC_TEST(F32x4Abs) { RunF32x4UnOpTest(kExprF32x4Abs, std::abs); } +WASM_EXEC_TEST(F32x4Neg) { RunF32x4UnOpTest(kExprF32x4Neg, Negate); } + +void RunF32x4BinOpTest(WasmOpcode simd_op, FloatBinOp expected_op) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t, float, float, float> r(kExecuteCompiled); + byte a = 0; + byte b = 1; + byte expected = 2; + byte simd0 = r.AllocateLocal(kWasmS128); + byte simd1 = r.AllocateLocal(kWasmS128); + BUILD(r, WASM_SET_LOCAL(simd0, WASM_SIMD_F32x4_SPLAT(WASM_GET_LOCAL(a))), + WASM_SET_LOCAL(simd1, WASM_SIMD_F32x4_SPLAT(WASM_GET_LOCAL(b))), + WASM_SET_LOCAL(simd1, + WASM_SIMD_BINOP(simd_op & 0xffu, WASM_GET_LOCAL(simd0), + WASM_GET_LOCAL(simd1))), + WASM_SIMD_CHECK_SPLAT4_F32(F32x4, simd1, expected), WASM_ONE); + + FOR_FLOAT32_INPUTS(i) { + if (std::isnan(*i)) continue; + FOR_FLOAT32_INPUTS(j) { + if (std::isnan(*j)) continue; + float expected = expected_op(*i, *j); + // SIMD on some platforms may handle denormalized numbers differently. + // TODO(bbudge) On platforms that flush denorms to zero, test with + // expected == 0. + if (std::fpclassify(expected) == FP_SUBNORMAL) continue; + CHECK_EQ(1, r.Call(*i, *j, expected)); + } + } +} + +WASM_EXEC_TEST(F32x4Add) { RunF32x4BinOpTest(kExprF32x4Add, Add); } +WASM_EXEC_TEST(F32x4Sub) { RunF32x4BinOpTest(kExprF32x4Sub, Sub); } + +void RunF32x4CompareOpTest(WasmOpcode simd_op, FloatCompareOp expected_op) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t, float, float, int32_t> r(kExecuteCompiled); + byte a = 0; + byte b = 1; + byte expected = 2; + byte simd0 = r.AllocateLocal(kWasmS128); + byte simd1 = r.AllocateLocal(kWasmS128); + BUILD(r, WASM_SET_LOCAL(simd0, WASM_SIMD_F32x4_SPLAT(WASM_GET_LOCAL(a))), + WASM_SET_LOCAL(simd1, WASM_SIMD_F32x4_SPLAT(WASM_GET_LOCAL(b))), + WASM_SET_LOCAL(simd1, + WASM_SIMD_BINOP(simd_op & 0xffu, WASM_GET_LOCAL(simd0), + WASM_GET_LOCAL(simd1))), + WASM_SIMD_CHECK_SPLAT4(I32x4, simd1, I32, expected), WASM_ONE); + + FOR_FLOAT32_INPUTS(i) { + if (std::isnan(*i)) continue; + FOR_FLOAT32_INPUTS(j) { + if (std::isnan(*j)) continue; + // SIMD on some platforms may handle denormalized numbers differently. + // Check for number pairs that are very close together. + if (std::fpclassify(*i - *j) == FP_SUBNORMAL) continue; + CHECK_EQ(1, r.Call(*i, *j, expected_op(*i, *j))); + } + } +} + +WASM_EXEC_TEST(F32x4Equal) { RunF32x4CompareOpTest(kExprF32x4Eq, Equal); } +WASM_EXEC_TEST(F32x4NotEqual) { RunF32x4CompareOpTest(kExprF32x4Ne, NotEqual); } +#endif // V8_TARGET_ARCH_ARM + +WASM_EXEC_TEST(I32x4Splat) { FLAG_wasm_simd_prototype = true; // Store SIMD value in a local variable, use extract lane to check lane values @@ -26,24 +278,136 @@ WASM_EXEC_TEST(Splat) { // return 0 // // return 1 - WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32()); - r.AllocateLocal(kAstS128); + WasmRunner<int32_t, int32_t> r(kExecuteCompiled); + byte lane_val = 0; + byte simd = r.AllocateLocal(kWasmS128); BUILD(r, - WASM_BLOCK( - WASM_SET_LOCAL(1, WASM_SIMD_I32x4_SPLAT(WASM_GET_LOCAL(0))), - WASM_IF(WASM_I32_NE(WASM_GET_LOCAL(0), WASM_SIMD_I32x4_EXTRACT_LANE( - 0, WASM_GET_LOCAL(1))), - WASM_RETURN1(WASM_ZERO)), - WASM_IF(WASM_I32_NE(WASM_GET_LOCAL(0), WASM_SIMD_I32x4_EXTRACT_LANE( - 1, WASM_GET_LOCAL(1))), - WASM_RETURN1(WASM_ZERO)), - WASM_IF(WASM_I32_NE(WASM_GET_LOCAL(0), WASM_SIMD_I32x4_EXTRACT_LANE( - 2, WASM_GET_LOCAL(1))), - WASM_RETURN1(WASM_ZERO)), - WASM_IF(WASM_I32_NE(WASM_GET_LOCAL(0), WASM_SIMD_I32x4_EXTRACT_LANE( - 3, WASM_GET_LOCAL(1))), - WASM_RETURN1(WASM_ZERO)), - WASM_RETURN1(WASM_ONE))); + WASM_SET_LOCAL(simd, WASM_SIMD_I32x4_SPLAT(WASM_GET_LOCAL(lane_val))), + WASM_SIMD_CHECK_SPLAT4(I32x4, simd, I32, lane_val), WASM_ONE); FOR_INT32_INPUTS(i) { CHECK_EQ(1, r.Call(*i)); } } + +WASM_EXEC_TEST(I32x4ReplaceLane) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t, int32_t, int32_t> r(kExecuteCompiled); + byte old_val = 0; + byte new_val = 1; + byte simd = r.AllocateLocal(kWasmS128); + BUILD(r, WASM_SET_LOCAL(simd, WASM_SIMD_I32x4_SPLAT(WASM_GET_LOCAL(old_val))), + WASM_SET_LOCAL(simd, + WASM_SIMD_I32x4_REPLACE_LANE(0, WASM_GET_LOCAL(simd), + WASM_GET_LOCAL(new_val))), + WASM_SIMD_CHECK4(I32x4, simd, I32, new_val, old_val, old_val, old_val), + WASM_SET_LOCAL(simd, + WASM_SIMD_I32x4_REPLACE_LANE(1, WASM_GET_LOCAL(simd), + WASM_GET_LOCAL(new_val))), + WASM_SIMD_CHECK4(I32x4, simd, I32, new_val, new_val, old_val, old_val), + WASM_SET_LOCAL(simd, + WASM_SIMD_I32x4_REPLACE_LANE(2, WASM_GET_LOCAL(simd), + WASM_GET_LOCAL(new_val))), + WASM_SIMD_CHECK4(I32x4, simd, I32, new_val, new_val, new_val, old_val), + WASM_SET_LOCAL(simd, + WASM_SIMD_I32x4_REPLACE_LANE(3, WASM_GET_LOCAL(simd), + WASM_GET_LOCAL(new_val))), + WASM_SIMD_CHECK_SPLAT4(I32x4, simd, I32, new_val), WASM_ONE); + + CHECK_EQ(1, r.Call(1, 2)); +} + +#if V8_TARGET_ARCH_ARM + +// Determines if conversion from float to int will be valid. +bool CanRoundToZeroAndConvert(double val, bool unsigned_integer) { + const double max_uint = static_cast<double>(0xffffffffu); + const double max_int = static_cast<double>(kMaxInt); + const double min_int = static_cast<double>(kMinInt); + + // Check for NaN. + if (val != val) { + return false; + } + + // Round to zero and check for overflow. This code works because 32 bit + // integers can be exactly represented by ieee-754 64bit floating-point + // values. + return unsigned_integer ? (val < (max_uint + 1.0)) && (val > -1) + : (val < (max_int + 1.0)) && (val > (min_int - 1.0)); +} + +int ConvertInvalidValue(double val, bool unsigned_integer) { + if (val != val) { + return 0; + } else { + if (unsigned_integer) { + return (val < 0) ? 0 : 0xffffffffu; + } else { + return (val < 0) ? kMinInt : kMaxInt; + } + } +} + +int32_t ConvertToInt(double val, bool unsigned_integer) { + int32_t result = + unsigned_integer ? static_cast<uint32_t>(val) : static_cast<int32_t>(val); + + if (!CanRoundToZeroAndConvert(val, unsigned_integer)) { + result = ConvertInvalidValue(val, unsigned_integer); + } + return result; +} + +// Tests both signed and unsigned conversion. +WASM_EXEC_TEST(I32x4FromFloat32x4) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t, float, int32_t, int32_t> r(kExecuteCompiled); + byte a = 0; + byte expected_signed = 1; + byte expected_unsigned = 2; + byte simd0 = r.AllocateLocal(kWasmS128); + byte simd1 = r.AllocateLocal(kWasmS128); + byte simd2 = r.AllocateLocal(kWasmS128); + BUILD( + r, WASM_SET_LOCAL(simd0, WASM_SIMD_F32x4_SPLAT(WASM_GET_LOCAL(a))), + WASM_SET_LOCAL(simd1, WASM_SIMD_I32x4_FROM_F32x4(WASM_GET_LOCAL(simd0))), + WASM_SIMD_CHECK_SPLAT4(I32x4, simd1, I32, expected_signed), + WASM_SET_LOCAL(simd2, WASM_SIMD_U32x4_FROM_F32x4(WASM_GET_LOCAL(simd0))), + WASM_SIMD_CHECK_SPLAT4(I32x4, simd2, I32, expected_unsigned), WASM_ONE); + + FOR_FLOAT32_INPUTS(i) { + int32_t signed_value = ConvertToInt(*i, false); + int32_t unsigned_value = ConvertToInt(*i, true); + CHECK_EQ(1, r.Call(*i, signed_value, unsigned_value)); + } +} +#endif // V8_TARGET_ARCH_ARM + +void RunI32x4BinOpTest(WasmOpcode simd_op, Int32BinOp expected_op) { + FLAG_wasm_simd_prototype = true; + WasmRunner<int32_t, int32_t, int32_t, int32_t> r(kExecuteCompiled); + byte a = 0; + byte b = 1; + byte expected = 2; + byte simd0 = r.AllocateLocal(kWasmS128); + byte simd1 = r.AllocateLocal(kWasmS128); + BUILD(r, WASM_SET_LOCAL(simd0, WASM_SIMD_I32x4_SPLAT(WASM_GET_LOCAL(a))), + WASM_SET_LOCAL(simd1, WASM_SIMD_I32x4_SPLAT(WASM_GET_LOCAL(b))), + WASM_SET_LOCAL(simd1, + WASM_SIMD_BINOP(simd_op & 0xffu, WASM_GET_LOCAL(simd0), + WASM_GET_LOCAL(simd1))), + WASM_SIMD_CHECK_SPLAT4(I32x4, simd1, I32, expected), WASM_ONE); + + FOR_INT32_INPUTS(i) { + FOR_INT32_INPUTS(j) { CHECK_EQ(1, r.Call(*i, *j, expected_op(*i, *j))); } + } +} + +WASM_EXEC_TEST(I32x4Add) { RunI32x4BinOpTest(kExprI32x4Add, Add); } + +WASM_EXEC_TEST(I32x4Sub) { RunI32x4BinOpTest(kExprI32x4Sub, Sub); } + +#if V8_TARGET_ARCH_ARM +WASM_EXEC_TEST(I32x4Equal) { RunI32x4BinOpTest(kExprI32x4Eq, Equal); } + +WASM_EXEC_TEST(I32x4NotEqual) { RunI32x4BinOpTest(kExprI32x4Ne, NotEqual); } +#endif // V8_TARGET_ARCH_ARM diff --git a/deps/v8/test/cctest/wasm/test-run-wasm.cc b/deps/v8/test/cctest/wasm/test-run-wasm.cc index a42a81ba27..05370b5775 100644 --- a/deps/v8/test/cctest/wasm/test-run-wasm.cc +++ b/deps/v8/test/cctest/wasm/test-run-wasm.cc @@ -25,41 +25,7 @@ using namespace v8::internal::wasm; #define B2(a, b) WASM_BLOCK(a, b) #define B3(a, b, c) WASM_BLOCK(a, b, c) #define RET(x) x, kExprReturn -#define RET_I8(x) kExprI8Const, x, kExprReturn - -WASM_EXEC_TEST(Int8Const) { - WasmRunner<int32_t> r(execution_mode); - const byte kExpectedValue = 121; - // return(kExpectedValue) - BUILD(r, WASM_I8(kExpectedValue)); - CHECK_EQ(kExpectedValue, r.Call()); -} - -WASM_EXEC_TEST(Int8Const_end) { - WasmRunner<int32_t> r(execution_mode); - const byte kExpectedValue = 121; - // return(kExpectedValue) - BUILD(r, WASM_I8(kExpectedValue), kExprEnd); - CHECK_EQ(kExpectedValue, r.Call()); -} - -WASM_EXEC_TEST(Int8Const_fallthru2) { - WasmRunner<int32_t> r(execution_mode); - const byte kExpectedValue = 123; - // -99 kExpectedValue - BUILD(r, WASM_I8(-99), WASM_DROP, WASM_I8(kExpectedValue)); - CHECK_EQ(kExpectedValue, r.Call()); -} - -WASM_EXEC_TEST(Int8Const_all) { - for (int value = -128; value <= 127; ++value) { - WasmRunner<int32_t> r(execution_mode); - // return(value) - BUILD(r, WASM_I8(value)); - int32_t result = r.Call(); - CHECK_EQ(value, result); - } -} +#define RET_I8(x) WASM_I32V_2(x), kExprReturn WASM_EXEC_TEST(Int32Const) { WasmRunner<int32_t> r(execution_mode); @@ -81,29 +47,28 @@ WASM_EXEC_TEST(Int32Const_many) { WASM_EXEC_TEST(GraphTrimming) { // This WebAssembly code requires graph trimming in the TurboFan compiler. - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, kExprGetLocal, 0, kExprGetLocal, 0, kExprGetLocal, 0, kExprI32RemS, kExprI32Eq, kExprGetLocal, 0, kExprI32DivS, kExprUnreachable); r.Call(1); } WASM_EXEC_TEST(Int32Param0) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // return(local[0]) BUILD(r, WASM_GET_LOCAL(0)); FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Int32Param0_fallthru) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // local[0] BUILD(r, WASM_GET_LOCAL(0)); FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Int32Param1) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32(), - MachineType::Int32()); + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); // local[1] BUILD(r, WASM_GET_LOCAL(1)); FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(-111, *i)); } @@ -112,28 +77,27 @@ WASM_EXEC_TEST(Int32Param1) { WASM_EXEC_TEST(Int32Add) { WasmRunner<int32_t> r(execution_mode); // 11 + 44 - BUILD(r, WASM_I32_ADD(WASM_I8(11), WASM_I8(44))); + BUILD(r, WASM_I32_ADD(WASM_I32V_1(11), WASM_I32V_1(44))); CHECK_EQ(55, r.Call()); } WASM_EXEC_TEST(Int32Add_P) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // p0 + 13 - BUILD(r, WASM_I32_ADD(WASM_I8(13), WASM_GET_LOCAL(0))); + BUILD(r, WASM_I32_ADD(WASM_I32V_1(13), WASM_GET_LOCAL(0))); FOR_INT32_INPUTS(i) { CHECK_EQ(*i + 13, r.Call(*i)); } } WASM_EXEC_TEST(Int32Add_P_fallthru) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // p0 + 13 - BUILD(r, WASM_I32_ADD(WASM_I8(13), WASM_GET_LOCAL(0))); + BUILD(r, WASM_I32_ADD(WASM_I32V_1(13), WASM_GET_LOCAL(0))); FOR_INT32_INPUTS(i) { CHECK_EQ(*i + 13, r.Call(*i)); } } static void RunInt32AddTest(WasmExecutionMode execution_mode, const byte* code, size_t size) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32(), - MachineType::Int32()); + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); r.Build(code, code + size); FOR_INT32_INPUTS(i) { FOR_INT32_INPUTS(j) { @@ -154,7 +118,7 @@ WASM_EXEC_TEST(Int32Add_P2) { WASM_EXEC_TEST(Int32Add_block1) { FLAG_wasm_mv_prototype = true; static const byte code[] = { - WASM_BLOCK_TT(kAstI32, kAstI32, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)), + WASM_BLOCK_TT(kWasmI32, kWasmI32, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)), kExprI32Add}; RunInt32AddTest(execution_mode, code, sizeof(code)); } @@ -162,7 +126,7 @@ WASM_EXEC_TEST(Int32Add_block1) { WASM_EXEC_TEST(Int32Add_block2) { FLAG_wasm_mv_prototype = true; static const byte code[] = { - WASM_BLOCK_TT(kAstI32, kAstI32, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), + WASM_BLOCK_TT(kWasmI32, kWasmI32, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), kExprBr, DEPTH_0), kExprI32Add}; RunInt32AddTest(execution_mode, code, sizeof(code)); @@ -171,7 +135,7 @@ WASM_EXEC_TEST(Int32Add_block2) { WASM_EXEC_TEST(Int32Add_multi_if) { FLAG_wasm_mv_prototype = true; static const byte code[] = { - WASM_IF_ELSE_TT(kAstI32, kAstI32, WASM_GET_LOCAL(0), + WASM_IF_ELSE_TT(kWasmI32, kWasmI32, WASM_GET_LOCAL(0), WASM_SEQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)), WASM_SEQ(WASM_GET_LOCAL(1), WASM_GET_LOCAL(0))), kExprI32Add}; @@ -202,8 +166,7 @@ void TestInt32Binop(WasmExecutionMode execution_mode, WasmOpcode opcode, CHECK_EQ(expected, r.Call()); } { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32(), - MachineType::Int32()); + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); // a op b BUILD(r, WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); CHECK_EQ(expected, r.Call(a, b)); @@ -252,7 +215,7 @@ void TestInt32Unop(WasmExecutionMode execution_mode, WasmOpcode opcode, CHECK_EQ(expected, r.Call()); } { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // return op a BUILD(r, WASM_UNOP(opcode, WASM_GET_LOCAL(0))); CHECK_EQ(expected, r.Call(a)); @@ -348,8 +311,7 @@ WASM_EXEC_TEST(I32Eqz) { } WASM_EXEC_TEST(I32Shl) { - WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(), - MachineType::Uint32()); + WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_mode); BUILD(r, WASM_I32_SHL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_UINT32_INPUTS(i) { @@ -361,8 +323,7 @@ WASM_EXEC_TEST(I32Shl) { } WASM_EXEC_TEST(I32Shr) { - WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(), - MachineType::Uint32()); + WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_mode); BUILD(r, WASM_I32_SHR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_UINT32_INPUTS(i) { @@ -374,8 +335,7 @@ WASM_EXEC_TEST(I32Shr) { } WASM_EXEC_TEST(I32Sar) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32(), - MachineType::Int32()); + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); BUILD(r, WASM_I32_SAR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_INT32_INPUTS(i) { @@ -386,9 +346,8 @@ WASM_EXEC_TEST(I32Sar) { } } -WASM_EXEC_TEST(Int32DivS_trap) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32(), - MachineType::Int32()); +WASM_EXEC_TEST_WITH_TRAP(Int32DivS_trap) { + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); BUILD(r, WASM_I32_DIVS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); const int32_t kMin = std::numeric_limits<int32_t>::min(); CHECK_EQ(0, r.Call(0, 100)); @@ -398,9 +357,8 @@ WASM_EXEC_TEST(Int32DivS_trap) { CHECK_TRAP(r.Call(kMin, 0)); } -WASM_EXEC_TEST(Int32RemS_trap) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32(), - MachineType::Int32()); +WASM_EXEC_TEST_WITH_TRAP(Int32RemS_trap) { + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); BUILD(r, WASM_I32_REMS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); const int32_t kMin = std::numeric_limits<int32_t>::min(); CHECK_EQ(33, r.Call(133, 100)); @@ -410,9 +368,8 @@ WASM_EXEC_TEST(Int32RemS_trap) { CHECK_TRAP(r.Call(kMin, 0)); } -WASM_EXEC_TEST(Int32DivU_trap) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32(), - MachineType::Int32()); +WASM_EXEC_TEST_WITH_TRAP(Int32DivU_trap) { + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); BUILD(r, WASM_I32_DIVU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); const int32_t kMin = std::numeric_limits<int32_t>::min(); CHECK_EQ(0, r.Call(0, 100)); @@ -422,9 +379,8 @@ WASM_EXEC_TEST(Int32DivU_trap) { CHECK_TRAP(r.Call(kMin, 0)); } -WASM_EXEC_TEST(Int32RemU_trap) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32(), - MachineType::Int32()); +WASM_EXEC_TEST_WITH_TRAP(Int32RemU_trap) { + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); BUILD(r, WASM_I32_REMU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); CHECK_EQ(17, r.Call(217, 100)); const int32_t kMin = std::numeric_limits<int32_t>::min(); @@ -434,10 +390,10 @@ WASM_EXEC_TEST(Int32RemU_trap) { CHECK_EQ(kMin, r.Call(kMin, -1)); } -WASM_EXEC_TEST(Int32DivS_byzero_const) { +WASM_EXEC_TEST_WITH_TRAP(Int32DivS_byzero_const) { for (int8_t denom = -2; denom < 8; ++denom) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, WASM_I32_DIVS(WASM_GET_LOCAL(0), WASM_I8(denom))); + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_I32_DIVS(WASM_GET_LOCAL(0), WASM_I32V_1(denom))); for (int32_t val = -7; val < 8; ++val) { if (denom == 0) { CHECK_TRAP(r.Call(val)); @@ -450,10 +406,9 @@ WASM_EXEC_TEST(Int32DivS_byzero_const) { WASM_EXEC_TEST(Int32AsmjsDivS_byzero_const) { for (int8_t denom = -2; denom < 8; ++denom) { - TestingModule module(execution_mode); - module.ChangeOriginToAsmjs(); - WasmRunner<int32_t> r(&module, MachineType::Int32()); - BUILD(r, WASM_I32_ASMJS_DIVS(WASM_GET_LOCAL(0), WASM_I8(denom))); + WasmRunner<int32_t, int32_t> r(execution_mode); + r.module().ChangeOriginToAsmjs(); + BUILD(r, WASM_I32_ASMJS_DIVS(WASM_GET_LOCAL(0), WASM_I32V_1(denom))); FOR_INT32_INPUTS(i) { if (denom == 0) { CHECK_EQ(0, r.Call(*i)); @@ -468,10 +423,9 @@ WASM_EXEC_TEST(Int32AsmjsDivS_byzero_const) { WASM_EXEC_TEST(Int32AsmjsRemS_byzero_const) { for (int8_t denom = -2; denom < 8; ++denom) { - TestingModule module(execution_mode); - module.ChangeOriginToAsmjs(); - WasmRunner<int32_t> r(&module, MachineType::Int32()); - BUILD(r, WASM_I32_ASMJS_REMS(WASM_GET_LOCAL(0), WASM_I8(denom))); + WasmRunner<int32_t, int32_t> r(execution_mode); + r.module().ChangeOriginToAsmjs(); + BUILD(r, WASM_I32_ASMJS_REMS(WASM_GET_LOCAL(0), WASM_I32V_1(denom))); FOR_INT32_INPUTS(i) { if (denom == 0) { CHECK_EQ(0, r.Call(*i)); @@ -484,9 +438,9 @@ WASM_EXEC_TEST(Int32AsmjsRemS_byzero_const) { } } -WASM_EXEC_TEST(Int32DivU_byzero_const) { +WASM_EXEC_TEST_WITH_TRAP(Int32DivU_byzero_const) { for (uint32_t denom = 0xfffffffe; denom < 8; ++denom) { - WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32()); + WasmRunner<uint32_t, uint32_t> r(execution_mode); BUILD(r, WASM_I32_DIVU(WASM_GET_LOCAL(0), WASM_I32V_1(denom))); for (uint32_t val = 0xfffffff0; val < 8; ++val) { @@ -499,10 +453,9 @@ WASM_EXEC_TEST(Int32DivU_byzero_const) { } } -WASM_EXEC_TEST(Int32DivS_trap_effect) { - TestingModule module(execution_mode); - module.AddMemoryElems<int32_t>(8); - WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32()); +WASM_EXEC_TEST_WITH_TRAP(Int32DivS_trap_effect) { + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); + r.module().AddMemoryElems<int32_t>(8); BUILD(r, WASM_IF_ELSE_I( WASM_GET_LOCAL(0), @@ -531,8 +484,7 @@ void TestFloat32Binop(WasmExecutionMode execution_mode, WasmOpcode opcode, CHECK_EQ(expected, r.Call()); } { - WasmRunner<int32_t> r(execution_mode, MachineType::Float32(), - MachineType::Float32()); + WasmRunner<int32_t, float, float> r(execution_mode); // return a op b BUILD(r, WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); CHECK_EQ(expected, r.Call(a, b)); @@ -550,8 +502,7 @@ void TestFloat32BinopWithConvert(WasmExecutionMode execution_mode, CHECK_EQ(expected, r.Call()); } { - WasmRunner<int32_t> r(execution_mode, MachineType::Float32(), - MachineType::Float32()); + WasmRunner<int32_t, float, float> r(execution_mode); // return int(a op b) BUILD(r, WASM_I32_SCONVERT_F32( WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)))); @@ -568,7 +519,7 @@ void TestFloat32UnopWithConvert(WasmExecutionMode execution_mode, CHECK_EQ(expected, r.Call()); } { - WasmRunner<int32_t> r(execution_mode, MachineType::Float32()); + WasmRunner<int32_t, float> r(execution_mode); // return int(op(a)) BUILD(r, WASM_I32_SCONVERT_F32(WASM_UNOP(opcode, WASM_GET_LOCAL(0)))); CHECK_EQ(expected, r.Call(a)); @@ -584,8 +535,7 @@ void TestFloat64Binop(WasmExecutionMode execution_mode, WasmOpcode opcode, CHECK_EQ(expected, r.Call()); } { - WasmRunner<int32_t> r(execution_mode, MachineType::Float64(), - MachineType::Float64()); + WasmRunner<int32_t, double, double> r(execution_mode); // return a op b BUILD(r, WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); CHECK_EQ(expected, r.Call(a, b)); @@ -603,8 +553,7 @@ void TestFloat64BinopWithConvert(WasmExecutionMode execution_mode, CHECK_EQ(expected, r.Call()); } { - WasmRunner<int32_t> r(execution_mode, MachineType::Float64(), - MachineType::Float64()); + WasmRunner<int32_t, double, double> r(execution_mode); BUILD(r, WASM_I32_SCONVERT_F64( WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)))); CHECK_EQ(expected, r.Call(a, b)); @@ -620,7 +569,7 @@ void TestFloat64UnopWithConvert(WasmExecutionMode execution_mode, CHECK_EQ(expected, r.Call()); } { - WasmRunner<int32_t> r(execution_mode, MachineType::Float64()); + WasmRunner<int32_t, double> r(execution_mode); // return int(op(a)) BUILD(r, WASM_I32_SCONVERT_F64(WASM_UNOP(opcode, WASM_GET_LOCAL(0)))); CHECK_EQ(expected, r.Call(a)); @@ -671,7 +620,7 @@ WASM_EXEC_TEST(Float64Unops) { } WASM_EXEC_TEST(Float32Neg) { - WasmRunner<float> r(execution_mode, MachineType::Float32()); + WasmRunner<float, float> r(execution_mode); BUILD(r, WASM_F32_NEG(WASM_GET_LOCAL(0))); FOR_FLOAT32_INPUTS(i) { @@ -681,7 +630,7 @@ WASM_EXEC_TEST(Float32Neg) { } WASM_EXEC_TEST(Float64Neg) { - WasmRunner<double> r(execution_mode, MachineType::Float64()); + WasmRunner<double, double> r(execution_mode); BUILD(r, WASM_F64_NEG(WASM_GET_LOCAL(0))); FOR_FLOAT64_INPUTS(i) { @@ -691,11 +640,11 @@ WASM_EXEC_TEST(Float64Neg) { } WASM_EXEC_TEST(IfElse_P) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // if (p0) return 11; else return 22; BUILD(r, WASM_IF_ELSE_I(WASM_GET_LOCAL(0), // -- - WASM_I8(11), // -- - WASM_I8(22))); // -- + WASM_I32V_1(11), // -- + WASM_I32V_1(22))); // -- FOR_INT32_INPUTS(i) { int32_t expected = *i ? 11 : 22; CHECK_EQ(expected, r.Call(*i)); @@ -704,50 +653,45 @@ WASM_EXEC_TEST(IfElse_P) { #define EMPTY WASM_EXEC_TEST(If_empty1) { - WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(), - MachineType::Uint32()); + WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_mode); BUILD(r, WASM_GET_LOCAL(0), kExprIf, kLocalVoid, kExprEnd, WASM_GET_LOCAL(1)); FOR_UINT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i - 9, *i)); } } WASM_EXEC_TEST(IfElse_empty1) { - WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(), - MachineType::Uint32()); + WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_mode); BUILD(r, WASM_GET_LOCAL(0), kExprIf, kLocalVoid, kExprElse, kExprEnd, WASM_GET_LOCAL(1)); FOR_UINT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i - 8, *i)); } } WASM_EXEC_TEST(IfElse_empty2) { - WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(), - MachineType::Uint32()); + WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_mode); BUILD(r, WASM_GET_LOCAL(0), kExprIf, kLocalVoid, WASM_NOP, kExprElse, kExprEnd, WASM_GET_LOCAL(1)); FOR_UINT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i - 7, *i)); } } WASM_EXEC_TEST(IfElse_empty3) { - WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(), - MachineType::Uint32()); + WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_mode); BUILD(r, WASM_GET_LOCAL(0), kExprIf, kLocalVoid, kExprElse, WASM_NOP, kExprEnd, WASM_GET_LOCAL(1)); FOR_UINT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i - 6, *i)); } } WASM_EXEC_TEST(If_chain1) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // if (p0) 13; if (p0) 14; 15 BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_NOP), - WASM_IF(WASM_GET_LOCAL(0), WASM_NOP), WASM_I8(15)); + WASM_IF(WASM_GET_LOCAL(0), WASM_NOP), WASM_I32V_1(15)); FOR_INT32_INPUTS(i) { CHECK_EQ(15, r.Call(*i)); } } WASM_EXEC_TEST(If_chain_set) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32(), - MachineType::Int32()); + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); // if (p0) p1 = 73; if (p0) p1 = 74; p1 - BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(1, WASM_I8(73))), - WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(1, WASM_I8(74))), + BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(1, WASM_I32V_2(73))), + WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(1, WASM_I32V_2(74))), WASM_GET_LOCAL(1)); FOR_INT32_INPUTS(i) { int32_t expected = *i ? 74 : *i; @@ -758,17 +702,17 @@ WASM_EXEC_TEST(If_chain_set) { WASM_EXEC_TEST(IfElse_Unreachable1) { WasmRunner<int32_t> r(execution_mode); // 0 ? unreachable : 27 - BUILD(r, WASM_IF_ELSE_I(WASM_ZERO, // -- - WASM_UNREACHABLE, // -- - WASM_I8(27))); // -- + BUILD(r, WASM_IF_ELSE_I(WASM_ZERO, // -- + WASM_UNREACHABLE, // -- + WASM_I32V_1(27))); // -- CHECK_EQ(27, r.Call()); } WASM_EXEC_TEST(IfElse_Unreachable2) { WasmRunner<int32_t> r(execution_mode); // 1 ? 28 : unreachable - BUILD(r, WASM_IF_ELSE_I(WASM_I8(1), // -- - WASM_I8(28), // -- + BUILD(r, WASM_IF_ELSE_I(WASM_I32V_1(1), // -- + WASM_I32V_1(28), // -- WASM_UNREACHABLE)); // -- CHECK_EQ(28, r.Call()); } @@ -783,12 +727,12 @@ WASM_EXEC_TEST(Return12) { WASM_EXEC_TEST(Return17) { WasmRunner<int32_t> r(execution_mode); - BUILD(r, WASM_BLOCK(RET_I8(17))); + BUILD(r, WASM_BLOCK(RET_I8(17)), WASM_ZERO); CHECK_EQ(17, r.Call()); } WASM_EXEC_TEST(Return_I32) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, RET(WASM_GET_LOCAL(0))); @@ -796,7 +740,7 @@ WASM_EXEC_TEST(Return_I32) { } WASM_EXEC_TEST(Return_F32) { - WasmRunner<float> r(execution_mode, MachineType::Float32()); + WasmRunner<float, float> r(execution_mode); BUILD(r, RET(WASM_GET_LOCAL(0))); @@ -812,7 +756,7 @@ WASM_EXEC_TEST(Return_F32) { } WASM_EXEC_TEST(Return_F64) { - WasmRunner<double> r(execution_mode, MachineType::Float64()); + WasmRunner<double, double> r(execution_mode); BUILD(r, RET(WASM_GET_LOCAL(0))); @@ -828,8 +772,7 @@ WASM_EXEC_TEST(Return_F64) { } WASM_EXEC_TEST(Select_float_parameters) { - WasmRunner<float> r(execution_mode, MachineType::Float32(), - MachineType::Float32(), MachineType::Int32()); + WasmRunner<float, float, float, int32_t> r(execution_mode); // return select(11, 22, a); BUILD(r, WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), WASM_GET_LOCAL(2))); @@ -837,9 +780,9 @@ WASM_EXEC_TEST(Select_float_parameters) { } WASM_EXEC_TEST(Select) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // return select(11, 22, a); - BUILD(r, WASM_SELECT(WASM_I8(11), WASM_I8(22), WASM_GET_LOCAL(0))); + BUILD(r, WASM_SELECT(WASM_I32V_1(11), WASM_I32V_1(22), WASM_GET_LOCAL(0))); FOR_INT32_INPUTS(i) { int32_t expected = *i ? 11 : 22; CHECK_EQ(expected, r.Call(*i)); @@ -847,22 +790,22 @@ WASM_EXEC_TEST(Select) { } WASM_EXEC_TEST(Select_strict1) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // select(a=0, a=1, a=2); return a - BUILD(r, WASM_SELECT(WASM_TEE_LOCAL(0, WASM_I8(0)), - WASM_TEE_LOCAL(0, WASM_I8(1)), - WASM_TEE_LOCAL(0, WASM_I8(2))), + BUILD(r, WASM_SELECT(WASM_TEE_LOCAL(0, WASM_ZERO), + WASM_TEE_LOCAL(0, WASM_I32V_1(1)), + WASM_TEE_LOCAL(0, WASM_I32V_1(2))), WASM_DROP, WASM_GET_LOCAL(0)); FOR_INT32_INPUTS(i) { CHECK_EQ(2, r.Call(*i)); } } WASM_EXEC_TEST(Select_strict2) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - r.AllocateLocal(kAstI32); - r.AllocateLocal(kAstI32); + WasmRunner<int32_t, int32_t> r(execution_mode); + r.AllocateLocal(kWasmI32); + r.AllocateLocal(kWasmI32); // select(b=5, c=6, a) - BUILD(r, WASM_SELECT(WASM_TEE_LOCAL(1, WASM_I8(5)), - WASM_TEE_LOCAL(2, WASM_I8(6)), WASM_GET_LOCAL(0))); + BUILD(r, WASM_SELECT(WASM_TEE_LOCAL(1, WASM_I32V_1(5)), + WASM_TEE_LOCAL(2, WASM_I32V_1(6)), WASM_GET_LOCAL(0))); FOR_INT32_INPUTS(i) { int32_t expected = *i ? 5 : 6; CHECK_EQ(expected, r.Call(*i)); @@ -870,12 +813,12 @@ WASM_EXEC_TEST(Select_strict2) { } WASM_EXEC_TEST(Select_strict3) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - r.AllocateLocal(kAstI32); - r.AllocateLocal(kAstI32); + WasmRunner<int32_t, int32_t> r(execution_mode); + r.AllocateLocal(kWasmI32); + r.AllocateLocal(kWasmI32); // select(b=5, c=6, a=b) - BUILD(r, WASM_SELECT(WASM_TEE_LOCAL(1, WASM_I8(5)), - WASM_TEE_LOCAL(2, WASM_I8(6)), + BUILD(r, WASM_SELECT(WASM_TEE_LOCAL(1, WASM_I32V_1(5)), + WASM_TEE_LOCAL(2, WASM_I32V_1(6)), WASM_TEE_LOCAL(0, WASM_GET_LOCAL(1)))); FOR_INT32_INPUTS(i) { int32_t expected = 5; @@ -884,20 +827,19 @@ WASM_EXEC_TEST(Select_strict3) { } WASM_EXEC_TEST(BrIf_strict) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, WASM_BLOCK_I(WASM_BRV_IF(0, WASM_GET_LOCAL(0), - WASM_TEE_LOCAL(0, WASM_I8(99))))); + WASM_TEE_LOCAL(0, WASM_I32V_2(99))))); FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Br_height) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, - WASM_BLOCK_I( - WASM_BLOCK_I(WASM_BRV_IFD(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)), - WASM_RETURN1(WASM_I8(9)), WASM_I8(7), WASM_I8(7)), - WASM_BRV(0, WASM_I8(8)))); + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_BLOCK_I( + WASM_BLOCK(WASM_BRV_IFD(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)), + WASM_RETURN1(WASM_I32V_1(9))), + WASM_BRV(0, WASM_I32V_1(8)))); for (int32_t i = 0; i < 5; i++) { int32_t expected = i != 0 ? 8 : 9; @@ -906,36 +848,35 @@ WASM_EXEC_TEST(Br_height) { } WASM_EXEC_TEST(Regression_660262) { - TestingModule module(execution_mode); - module.AddMemoryElems<int32_t>(8); - WasmRunner<int32_t> r(&module); - BUILD(r, kExprI8Const, 0x00, kExprI8Const, 0x00, kExprI32LoadMem, 0x00, 0x0f, - kExprBrTable, 0x00, 0x80, 0x00); // entries=0 + WasmRunner<int32_t> r(execution_mode); + r.module().AddMemoryElems<int32_t>(8); + BUILD(r, kExprI32Const, 0x00, kExprI32Const, 0x00, kExprI32LoadMem, 0x00, + 0x0f, kExprBrTable, 0x00, 0x80, 0x00); // entries=0 r.Call(); } WASM_EXEC_TEST(BrTable0a) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, B1(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(0)))), - WASM_I8(91)); + WASM_I32V_2(91)); FOR_INT32_INPUTS(i) { CHECK_EQ(91, r.Call(*i)); } } WASM_EXEC_TEST(BrTable0b) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, B1(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 1, BR_TARGET(0), BR_TARGET(0)))), - WASM_I8(92)); + WASM_I32V_2(92)); FOR_INT32_INPUTS(i) { CHECK_EQ(92, r.Call(*i)); } } WASM_EXEC_TEST(BrTable0c) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD( r, B1(B2(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 1, BR_TARGET(0), BR_TARGET(1))), RET_I8(76))), - WASM_I8(77)); + WASM_I32V_2(77)); FOR_INT32_INPUTS(i) { int32_t expected = *i == 0 ? 76 : 77; CHECK_EQ(expected, r.Call(*i)); @@ -943,18 +884,18 @@ WASM_EXEC_TEST(BrTable0c) { } WASM_EXEC_TEST(BrTable1) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(0))), RET_I8(93)); FOR_INT32_INPUTS(i) { CHECK_EQ(93, r.Call(*i)); } } WASM_EXEC_TEST(BrTable_loop) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, B2(B1(WASM_LOOP(WASM_BR_TABLE(WASM_INC_LOCAL_BYV(0, 1), 2, BR_TARGET(2), BR_TARGET(1), BR_TARGET(0)))), RET_I8(99)), - WASM_I8(98)); + WASM_I32V_2(98)); CHECK_EQ(99, r.Call(0)); CHECK_EQ(98, r.Call(-1)); CHECK_EQ(98, r.Call(-2)); @@ -963,11 +904,11 @@ WASM_EXEC_TEST(BrTable_loop) { } WASM_EXEC_TEST(BrTable_br) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, B2(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 1, BR_TARGET(1), BR_TARGET(0))), RET_I8(91)), - WASM_I8(99)); + WASM_I32V_2(99)); CHECK_EQ(99, r.Call(0)); CHECK_EQ(91, r.Call(1)); CHECK_EQ(91, r.Call(2)); @@ -975,14 +916,14 @@ WASM_EXEC_TEST(BrTable_br) { } WASM_EXEC_TEST(BrTable_br2) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, B2(B2(B2(B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 3, BR_TARGET(1), BR_TARGET(2), BR_TARGET(3), BR_TARGET(0))), RET_I8(85)), RET_I8(86)), RET_I8(87)), - WASM_I8(88)); + WASM_I32V_2(88)); CHECK_EQ(86, r.Call(0)); CHECK_EQ(87, r.Call(1)); CHECK_EQ(88, r.Call(2)); @@ -1004,9 +945,9 @@ WASM_EXEC_TEST(BrTable4) { RET_I8(71)), RET_I8(72)), RET_I8(73)), - WASM_I8(75)}; + WASM_I32V_2(75)}; - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); r.Build(code, code + arraysize(code)); for (int x = -3; x < 50; ++x) { @@ -1034,9 +975,9 @@ WASM_EXEC_TEST(BrTable4x4) { RET_I8(51)), RET_I8(52)), RET_I8(53)), - WASM_I8(55)}; + WASM_I32V_2(55)}; - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); r.Build(code, code + arraysize(code)); for (int x = -6; x < 47; ++x) { @@ -1061,8 +1002,7 @@ WASM_EXEC_TEST(BrTable4_fallthru) { WASM_INC_LOCAL_BY(1, 8)), WASM_GET_LOCAL(1)}; - WasmRunner<int32_t> r(execution_mode, MachineType::Int32(), - MachineType::Int32()); + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); r.Build(code, code + arraysize(code)); CHECK_EQ(15, r.Call(0, 0)); @@ -1079,42 +1019,49 @@ WASM_EXEC_TEST(BrTable4_fallthru) { } WASM_EXEC_TEST(F32ReinterpretI32) { - TestingModule module(execution_mode); - int32_t* memory = module.AddMemoryElems<int32_t>(8); - WasmRunner<int32_t> r(&module); + WasmRunner<int32_t> r(execution_mode); + int32_t* memory = r.module().AddMemoryElems<int32_t>(8); BUILD(r, WASM_I32_REINTERPRET_F32( WASM_LOAD_MEM(MachineType::Float32(), WASM_ZERO))); FOR_INT32_INPUTS(i) { int32_t expected = *i; - module.WriteMemory(&memory[0], expected); + r.module().WriteMemory(&memory[0], expected); CHECK_EQ(expected, r.Call()); } } WASM_EXEC_TEST(I32ReinterpretF32) { - TestingModule module(execution_mode); - int32_t* memory = module.AddMemoryElems<int32_t>(8); - WasmRunner<int32_t> r(&module, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); + int32_t* memory = r.module().AddMemoryElems<int32_t>(8); BUILD(r, WASM_STORE_MEM(MachineType::Float32(), WASM_ZERO, WASM_F32_REINTERPRET_I32(WASM_GET_LOCAL(0))), - WASM_I8(107)); + WASM_I32V_2(107)); FOR_INT32_INPUTS(i) { int32_t expected = *i; CHECK_EQ(107, r.Call(expected)); - CHECK_EQ(expected, module.ReadMemory(&memory[0])); + CHECK_EQ(expected, r.module().ReadMemory(&memory[0])); } } -WASM_EXEC_TEST(LoadMaxUint32Offset) { - TestingModule module(execution_mode); - module.AddMemoryElems<int32_t>(8); - WasmRunner<int32_t> r(&module); +WASM_EXEC_TEST(SignallingNanSurvivesI32ReinterpretF32) { + WasmRunner<int32_t> r(execution_mode); + + BUILD(r, WASM_I32_REINTERPRET_F32( + WASM_SEQ(kExprF32Const, 0x00, 0x00, 0xa0, 0x7f))); + + // This is a signalling nan. + CHECK_EQ(0x7fa00000, r.Call()); +} + +WASM_EXEC_TEST_WITH_TRAP(LoadMaxUint32Offset) { + WasmRunner<int32_t> r(execution_mode); + r.module().AddMemoryElems<int32_t>(8); - BUILD(r, kExprI8Const, 0, // index + BUILD(r, kExprI32Const, 0, // index static_cast<byte>(v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf( MachineType::Int32(), false)), // -- 0, // alignment @@ -1124,9 +1071,8 @@ WASM_EXEC_TEST(LoadMaxUint32Offset) { } WASM_EXEC_TEST(LoadStoreLoad) { - TestingModule module(execution_mode); - int32_t* memory = module.AddMemoryElems<int32_t>(8); - WasmRunner<int32_t> r(&module); + WasmRunner<int32_t> r(execution_mode); + int32_t* memory = r.module().AddMemoryElems<int32_t>(8); BUILD(r, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), @@ -1134,116 +1080,112 @@ WASM_EXEC_TEST(LoadStoreLoad) { FOR_INT32_INPUTS(i) { int32_t expected = *i; - module.WriteMemory(&memory[0], expected); + r.module().WriteMemory(&memory[0], expected); CHECK_EQ(expected, r.Call()); } } WASM_EXEC_TEST(VoidReturn1) { - // We use a wrapper function because WasmRunner<void> does not exist. + const int32_t kExpected = -414444; + WasmRunner<int32_t> r(execution_mode); // Build the test function. - TestSignatures sigs; - TestingModule module(execution_mode); - WasmFunctionCompiler t(sigs.v_v(), &module); - BUILD(t, kExprNop); - uint32_t index = t.CompileAndAdd(); + WasmFunctionCompiler& test_func = r.NewFunction<void>(); + BUILD(test_func, kExprNop); - const int32_t kExpected = -414444; // Build the calling function. - WasmRunner<int32_t> r(&module); - BUILD(r, WASM_CALL_FUNCTION0(index), WASM_I32V_3(kExpected)); + BUILD(r, WASM_CALL_FUNCTION0(test_func.function_index()), + WASM_I32V_3(kExpected)); + // Call and check. int32_t result = r.Call(); CHECK_EQ(kExpected, result); } WASM_EXEC_TEST(VoidReturn2) { - // We use a wrapper function because WasmRunner<void> does not exist. + const int32_t kExpected = -414444; + WasmRunner<int32_t> r(execution_mode); + // Build the test function. - TestSignatures sigs; - TestingModule module(execution_mode); - WasmFunctionCompiler t(sigs.v_v(), &module); - BUILD(t, WASM_RETURN0); - uint32_t index = t.CompileAndAdd(); + WasmFunctionCompiler& test_func = r.NewFunction<void>(); + BUILD(test_func, WASM_RETURN0); - const int32_t kExpected = -414444; // Build the calling function. - WasmRunner<int32_t> r(&module); - BUILD(r, B1(WASM_CALL_FUNCTION0(index)), WASM_I32V_3(kExpected)); + BUILD(r, WASM_CALL_FUNCTION0(test_func.function_index()), + WASM_I32V_3(kExpected)); + // Call and check. int32_t result = r.Call(); CHECK_EQ(kExpected, result); } WASM_EXEC_TEST(BrEmpty) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, WASM_BRV(0, WASM_GET_LOCAL(0))); FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(BrIfEmpty) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, WASM_BRV_IF(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(0))); FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Block_empty) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, kExprBlock, kLocalVoid, kExprEnd, WASM_GET_LOCAL(0)); FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Block_empty_br1) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, B1(WASM_BR(0)), WASM_GET_LOCAL(0)); FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Block_empty_brif1) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, WASM_BLOCK(WASM_BR_IF(0, WASM_ZERO)), WASM_GET_LOCAL(0)); FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Block_empty_brif2) { - WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(), - MachineType::Uint32()); + WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_mode); BUILD(r, WASM_BLOCK(WASM_BR_IF(0, WASM_GET_LOCAL(1))), WASM_GET_LOCAL(0)); FOR_UINT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i, *i + 1)); } } WASM_EXEC_TEST(Block_i) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, WASM_BLOCK_I(WASM_GET_LOCAL(0))); FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Block_f) { - WasmRunner<float> r(execution_mode, MachineType::Float32()); + WasmRunner<float, float> r(execution_mode); BUILD(r, WASM_BLOCK_F(WASM_GET_LOCAL(0))); FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Block_d) { - WasmRunner<double> r(execution_mode, MachineType::Float64()); + WasmRunner<double, double> r(execution_mode); BUILD(r, WASM_BLOCK_D(WASM_GET_LOCAL(0))); FOR_FLOAT64_INPUTS(i) { CHECK_FLOAT_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Block_br2) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, WASM_BLOCK_I(WASM_BRV(0, WASM_GET_LOCAL(0)))); FOR_UINT32_INPUTS(i) { CHECK_EQ(*i, static_cast<uint32_t>(r.Call(*i))); } } WASM_EXEC_TEST(Block_If_P) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // block { if (p0) break 51; 52; } - BUILD(r, WASM_BLOCK_I( // -- - WASM_IF(WASM_GET_LOCAL(0), // -- - WASM_BRV(1, WASM_I8(51))), // -- - WASM_I8(52))); // -- + BUILD(r, WASM_BLOCK_I( // -- + WASM_IF(WASM_GET_LOCAL(0), // -- + WASM_BRV(1, WASM_I32V_1(51))), // -- + WASM_I32V_1(52))); // -- FOR_INT32_INPUTS(i) { int32_t expected = *i ? 51 : 52; CHECK_EQ(expected, r.Call(*i)); @@ -1251,51 +1193,49 @@ WASM_EXEC_TEST(Block_If_P) { } WASM_EXEC_TEST(Loop_empty) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, kExprLoop, kLocalVoid, kExprEnd, WASM_GET_LOCAL(0)); FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Loop_i) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, WASM_LOOP_I(WASM_GET_LOCAL(0))); FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Loop_f) { - WasmRunner<float> r(execution_mode, MachineType::Float32()); + WasmRunner<float, float> r(execution_mode); BUILD(r, WASM_LOOP_F(WASM_GET_LOCAL(0))); FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Loop_d) { - WasmRunner<double> r(execution_mode, MachineType::Float64()); + WasmRunner<double, double> r(execution_mode); BUILD(r, WASM_LOOP_D(WASM_GET_LOCAL(0))); FOR_FLOAT64_INPUTS(i) { CHECK_FLOAT_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Loop_empty_br1) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, B1(WASM_LOOP(WASM_BR(1))), WASM_GET_LOCAL(0)); FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Loop_empty_brif1) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, B1(WASM_LOOP(WASM_BR_IF(1, WASM_ZERO))), WASM_GET_LOCAL(0)); FOR_INT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); } } WASM_EXEC_TEST(Loop_empty_brif2) { - WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(), - MachineType::Uint32()); + WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_mode); BUILD(r, WASM_LOOP_I(WASM_BRV_IF(1, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)))); FOR_UINT32_INPUTS(i) { CHECK_EQ(*i, r.Call(*i, *i + 1)); } } WASM_EXEC_TEST(Loop_empty_brif3) { - WasmRunner<uint32_t> r(execution_mode, MachineType::Uint32(), - MachineType::Uint32(), MachineType::Uint32()); + WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_mode); BUILD(r, WASM_LOOP(WASM_BRV_IFD(1, WASM_GET_LOCAL(2), WASM_GET_LOCAL(0))), WASM_GET_LOCAL(1)); FOR_UINT32_INPUTS(i) { @@ -1307,9 +1247,9 @@ WASM_EXEC_TEST(Loop_empty_brif3) { } WASM_EXEC_TEST(Block_BrIf_P) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, WASM_BLOCK_I(WASM_BRV_IFD(0, WASM_I8(51), WASM_GET_LOCAL(0)), - WASM_I8(52))); + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_BLOCK_I(WASM_BRV_IFD(0, WASM_I32V_1(51), WASM_GET_LOCAL(0)), + WASM_I32V_1(52))); FOR_INT32_INPUTS(i) { int32_t expected = *i ? 51 : 52; CHECK_EQ(expected, r.Call(*i)); @@ -1317,12 +1257,12 @@ WASM_EXEC_TEST(Block_BrIf_P) { } WASM_EXEC_TEST(Block_IfElse_P_assign) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // { if (p0) p0 = 71; else p0 = 72; return p0; } - BUILD(r, // -- - WASM_IF_ELSE(WASM_GET_LOCAL(0), // -- - WASM_SET_LOCAL(0, WASM_I8(71)), // -- - WASM_SET_LOCAL(0, WASM_I8(72))), // -- + BUILD(r, // -- + WASM_IF_ELSE(WASM_GET_LOCAL(0), // -- + WASM_SET_LOCAL(0, WASM_I32V_2(71)), // -- + WASM_SET_LOCAL(0, WASM_I32V_2(72))), // -- WASM_GET_LOCAL(0)); FOR_INT32_INPUTS(i) { int32_t expected = *i ? 71 : 72; @@ -1331,12 +1271,13 @@ WASM_EXEC_TEST(Block_IfElse_P_assign) { } WASM_EXEC_TEST(Block_IfElse_P_return) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // if (p0) return 81; else return 82; BUILD(r, // -- WASM_IF_ELSE(WASM_GET_LOCAL(0), // -- RET_I8(81), // -- - RET_I8(82))); // -- + RET_I8(82)), // -- + WASM_ZERO); // -- FOR_INT32_INPUTS(i) { int32_t expected = *i ? 81 : 82; CHECK_EQ(expected, r.Call(*i)); @@ -1344,9 +1285,9 @@ WASM_EXEC_TEST(Block_IfElse_P_return) { } WASM_EXEC_TEST(Block_If_P_assign) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // { if (p0) p0 = 61; p0; } - BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(0, WASM_I8(61))), + BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(0, WASM_I32V_1(61))), WASM_GET_LOCAL(0)); FOR_INT32_INPUTS(i) { int32_t expected = *i ? 61 : *i; @@ -1355,18 +1296,18 @@ WASM_EXEC_TEST(Block_If_P_assign) { } WASM_EXEC_TEST(DanglingAssign) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // { return 0; p0 = 0; } - BUILD(r, B2(RET_I8(99), WASM_SET_LOCAL(0, WASM_ZERO))); + BUILD(r, WASM_BLOCK_I(RET_I8(99), WASM_TEE_LOCAL(0, WASM_ZERO))); CHECK_EQ(99, r.Call(1)); } WASM_EXEC_TEST(ExprIf_P) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // p0 ? 11 : 22; BUILD(r, WASM_IF_ELSE_I(WASM_GET_LOCAL(0), // -- - WASM_I8(11), // -- - WASM_I8(22))); // -- + WASM_I32V_1(11), // -- + WASM_I32V_1(22))); // -- FOR_INT32_INPUTS(i) { int32_t expected = *i ? 11 : 22; CHECK_EQ(expected, r.Call(*i)); @@ -1374,11 +1315,11 @@ WASM_EXEC_TEST(ExprIf_P) { } WASM_EXEC_TEST(CountDown) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, WASM_LOOP(WASM_IFB( - WASM_GET_LOCAL(0), - WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(1))), - WASM_BR(1))), + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_LOOP(WASM_IFB(WASM_GET_LOCAL(0), + WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), + WASM_I32V_1(1))), + WASM_BR(1))), WASM_GET_LOCAL(0)); CHECK_EQ(0, r.Call(1)); CHECK_EQ(0, r.Call(10)); @@ -1386,23 +1327,24 @@ WASM_EXEC_TEST(CountDown) { } WASM_EXEC_TEST(CountDown_fallthru) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, WASM_LOOP( - WASM_IF(WASM_NOT(WASM_GET_LOCAL(0)), - WASM_BRV(2, WASM_GET_LOCAL(0))), - WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(1))), - WASM_CONTINUE(0)), - WASM_GET_LOCAL(0)); + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD( + r, + WASM_LOOP( + WASM_IF(WASM_NOT(WASM_GET_LOCAL(0)), WASM_BRV(2, WASM_GET_LOCAL(0))), + WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I32V_1(1))), + WASM_CONTINUE(0)), + WASM_GET_LOCAL(0)); CHECK_EQ(0, r.Call(1)); CHECK_EQ(0, r.Call(10)); CHECK_EQ(0, r.Call(100)); } WASM_EXEC_TEST(WhileCountDown) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, WASM_WHILE( - WASM_GET_LOCAL(0), - WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(1)))), + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_WHILE(WASM_GET_LOCAL(0), + WASM_SET_LOCAL( + 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I32V_1(1)))), WASM_GET_LOCAL(0)); CHECK_EQ(0, r.Call(1)); CHECK_EQ(0, r.Call(10)); @@ -1410,10 +1352,9 @@ WASM_EXEC_TEST(WhileCountDown) { } WASM_EXEC_TEST(Loop_if_break1) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32(), - MachineType::Int32()); + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); BUILD(r, WASM_LOOP(WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(2, WASM_GET_LOCAL(1))), - WASM_SET_LOCAL(0, WASM_I8(99))), + WASM_SET_LOCAL(0, WASM_I32V_2(99))), WASM_GET_LOCAL(0)); CHECK_EQ(99, r.Call(0, 11)); CHECK_EQ(65, r.Call(3, 65)); @@ -1422,10 +1363,9 @@ WASM_EXEC_TEST(Loop_if_break1) { } WASM_EXEC_TEST(Loop_if_break2) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32(), - MachineType::Int32()); + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); BUILD(r, WASM_LOOP(WASM_BRV_IF(1, WASM_GET_LOCAL(1), WASM_GET_LOCAL(0)), - WASM_DROP, WASM_SET_LOCAL(0, WASM_I8(99))), + WASM_DROP, WASM_SET_LOCAL(0, WASM_I32V_2(99))), WASM_GET_LOCAL(0)); CHECK_EQ(99, r.Call(0, 33)); CHECK_EQ(3, r.Call(1, 3)); @@ -1434,9 +1374,9 @@ WASM_EXEC_TEST(Loop_if_break2) { } WASM_EXEC_TEST(Loop_if_break_fallthru) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, B1(WASM_LOOP(WASM_IF(WASM_GET_LOCAL(0), WASM_BR(2)), - WASM_SET_LOCAL(0, WASM_I8(93)))), + WASM_SET_LOCAL(0, WASM_I32V_2(93)))), WASM_GET_LOCAL(0)); CHECK_EQ(93, r.Call(0)); CHECK_EQ(3, r.Call(3)); @@ -1445,9 +1385,9 @@ WASM_EXEC_TEST(Loop_if_break_fallthru) { } WASM_EXEC_TEST(Loop_if_break_fallthru2) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, B1(B1(WASM_LOOP(WASM_IF(WASM_GET_LOCAL(0), WASM_BR(2)), - WASM_SET_LOCAL(0, WASM_I8(93))))), + WASM_SET_LOCAL(0, WASM_I32V_2(93))))), WASM_GET_LOCAL(0)); CHECK_EQ(93, r.Call(0)); CHECK_EQ(3, r.Call(3)); @@ -1456,71 +1396,68 @@ WASM_EXEC_TEST(Loop_if_break_fallthru2) { } WASM_EXEC_TEST(IfBreak1) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SEQ(WASM_BR(0), WASM_UNREACHABLE)), - WASM_I8(91)); + WASM_I32V_2(91)); CHECK_EQ(91, r.Call(0)); CHECK_EQ(91, r.Call(1)); CHECK_EQ(91, r.Call(-8734)); } WASM_EXEC_TEST(IfBreak2) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_SEQ(WASM_BR(0), RET_I8(77))), - WASM_I8(81)); + WASM_I32V_2(81)); CHECK_EQ(81, r.Call(0)); CHECK_EQ(81, r.Call(1)); CHECK_EQ(81, r.Call(-8734)); } WASM_EXEC_TEST(LoadMemI32) { - TestingModule module(execution_mode); - int32_t* memory = module.AddMemoryElems<int32_t>(8); - WasmRunner<int32_t> r(&module, MachineType::Int32()); - module.RandomizeMemory(1111); + WasmRunner<int32_t, int32_t> r(execution_mode); + int32_t* memory = r.module().AddMemoryElems<int32_t>(8); + r.module().RandomizeMemory(1111); - BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(0))); + BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)); - module.WriteMemory(&memory[0], 99999999); + r.module().WriteMemory(&memory[0], 99999999); CHECK_EQ(99999999, r.Call(0)); - module.WriteMemory(&memory[0], 88888888); + r.module().WriteMemory(&memory[0], 88888888); CHECK_EQ(88888888, r.Call(0)); - module.WriteMemory(&memory[0], 77777777); + r.module().WriteMemory(&memory[0], 77777777); CHECK_EQ(77777777, r.Call(0)); } WASM_EXEC_TEST(LoadMemI32_alignment) { - TestingModule module(execution_mode); - int32_t* memory = module.AddMemoryElems<int32_t>(8); for (byte alignment = 0; alignment <= 2; ++alignment) { - WasmRunner<int32_t> r(&module, MachineType::Int32()); - module.RandomizeMemory(1111); + WasmRunner<int32_t, int32_t> r(execution_mode); + int32_t* memory = r.module().AddMemoryElems<int32_t>(8); + r.module().RandomizeMemory(1111); BUILD(r, - WASM_LOAD_MEM_ALIGNMENT(MachineType::Int32(), WASM_I8(0), alignment)); + WASM_LOAD_MEM_ALIGNMENT(MachineType::Int32(), WASM_ZERO, alignment)); - module.WriteMemory(&memory[0], 0x1a2b3c4d); + r.module().WriteMemory(&memory[0], 0x1a2b3c4d); CHECK_EQ(0x1a2b3c4d, r.Call(0)); - module.WriteMemory(&memory[0], 0x5e6f7a8b); + r.module().WriteMemory(&memory[0], 0x5e6f7a8b); CHECK_EQ(0x5e6f7a8b, r.Call(0)); - module.WriteMemory(&memory[0], 0x7ca0b1c2); + r.module().WriteMemory(&memory[0], 0x7ca0b1c2); CHECK_EQ(0x7ca0b1c2, r.Call(0)); } } -WASM_EXEC_TEST(LoadMemI32_oob) { - TestingModule module(execution_mode); - int32_t* memory = module.AddMemoryElems<int32_t>(8); - WasmRunner<int32_t> r(&module, MachineType::Uint32()); - module.RandomizeMemory(1111); +WASM_EXEC_TEST_WITH_TRAP(LoadMemI32_oob) { + WasmRunner<int32_t, uint32_t> r(execution_mode); + int32_t* memory = r.module().AddMemoryElems<int32_t>(8); + r.module().RandomizeMemory(1111); BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0))); - module.WriteMemory(&memory[0], 88888888); + r.module().WriteMemory(&memory[0], 88888888); CHECK_EQ(88888888, r.Call(0u)); for (uint32_t offset = 29; offset < 40; ++offset) { CHECK_TRAP(r.Call(offset)); @@ -1531,10 +1468,7 @@ WASM_EXEC_TEST(LoadMemI32_oob) { } } -WASM_EXEC_TEST(LoadMem_offset_oob) { - TestingModule module(execution_mode); - module.AddMemoryElems<int32_t>(8); - +WASM_EXEC_TEST_WITH_TRAP(LoadMem_offset_oob) { static const MachineType machineTypes[] = { MachineType::Int8(), MachineType::Uint8(), MachineType::Int16(), MachineType::Uint16(), MachineType::Int32(), MachineType::Uint32(), @@ -1542,8 +1476,10 @@ WASM_EXEC_TEST(LoadMem_offset_oob) { MachineType::Float64()}; for (size_t m = 0; m < arraysize(machineTypes); ++m) { - module.RandomizeMemory(1116 + static_cast<int>(m)); - WasmRunner<int32_t> r(&module, MachineType::Uint32()); + WasmRunner<int32_t, uint32_t> r(execution_mode); + r.module().AddMemoryElems<int32_t>(8); + r.module().RandomizeMemory(1116 + static_cast<int>(m)); + uint32_t boundary = 24 - WasmOpcodes::MemSize(machineTypes[m]); BUILD(r, WASM_LOAD_MEM_OFFSET(machineTypes[m], 8, WASM_GET_LOCAL(0)), @@ -1558,46 +1494,43 @@ WASM_EXEC_TEST(LoadMem_offset_oob) { } WASM_EXEC_TEST(LoadMemI32_offset) { - TestingModule module(execution_mode); - int32_t* memory = module.AddMemoryElems<int32_t>(4); - WasmRunner<int32_t> r(&module, MachineType::Int32()); - module.RandomizeMemory(1111); + WasmRunner<int32_t, int32_t> r(execution_mode); + int32_t* memory = r.module().AddMemoryElems<int32_t>(4); + r.module().RandomizeMemory(1111); BUILD(r, WASM_LOAD_MEM_OFFSET(MachineType::Int32(), 4, WASM_GET_LOCAL(0))); - module.WriteMemory(&memory[0], 66666666); - module.WriteMemory(&memory[1], 77777777); - module.WriteMemory(&memory[2], 88888888); - module.WriteMemory(&memory[3], 99999999); + r.module().WriteMemory(&memory[0], 66666666); + r.module().WriteMemory(&memory[1], 77777777); + r.module().WriteMemory(&memory[2], 88888888); + r.module().WriteMemory(&memory[3], 99999999); CHECK_EQ(77777777, r.Call(0)); CHECK_EQ(88888888, r.Call(4)); CHECK_EQ(99999999, r.Call(8)); - module.WriteMemory(&memory[0], 11111111); - module.WriteMemory(&memory[1], 22222222); - module.WriteMemory(&memory[2], 33333333); - module.WriteMemory(&memory[3], 44444444); + r.module().WriteMemory(&memory[0], 11111111); + r.module().WriteMemory(&memory[1], 22222222); + r.module().WriteMemory(&memory[2], 33333333); + r.module().WriteMemory(&memory[3], 44444444); CHECK_EQ(22222222, r.Call(0)); CHECK_EQ(33333333, r.Call(4)); CHECK_EQ(44444444, r.Call(8)); } -WASM_EXEC_TEST(LoadMemI32_const_oob_misaligned) { +WASM_EXEC_TEST_WITH_TRAP(LoadMemI32_const_oob_misaligned) { const int kMemSize = 12; // TODO(titzer): Fix misaligned accesses on MIPS and re-enable. for (int offset = 0; offset < kMemSize + 5; ++offset) { for (int index = 0; index < kMemSize + 5; ++index) { - TestingModule module(execution_mode); - module.AddMemoryElems<byte>(kMemSize); - - WasmRunner<int32_t> r(&module); - module.RandomizeMemory(); + WasmRunner<int32_t> r(execution_mode); + r.module().AddMemoryElems<byte>(kMemSize); + r.module().RandomizeMemory(); - BUILD(r, - WASM_LOAD_MEM_OFFSET(MachineType::Int32(), offset, WASM_I8(index))); + BUILD(r, WASM_LOAD_MEM_OFFSET(MachineType::Int32(), offset, + WASM_I32V_2(index))); if ((offset + index) <= static_cast<int>((kMemSize - sizeof(int32_t)))) { - CHECK_EQ(module.raw_val_at<int32_t>(offset + index), r.Call()); + CHECK_EQ(r.module().raw_val_at<int32_t>(offset + index), r.Call()); } else { CHECK_TRAP(r.Call()); } @@ -1605,21 +1538,19 @@ WASM_EXEC_TEST(LoadMemI32_const_oob_misaligned) { } } -WASM_EXEC_TEST(LoadMemI32_const_oob) { +WASM_EXEC_TEST_WITH_TRAP(LoadMemI32_const_oob) { const int kMemSize = 24; for (int offset = 0; offset < kMemSize + 5; offset += 4) { for (int index = 0; index < kMemSize + 5; index += 4) { - TestingModule module(execution_mode); - module.AddMemoryElems<byte>(kMemSize); - - WasmRunner<int32_t> r(&module); - module.RandomizeMemory(); + WasmRunner<int32_t> r(execution_mode); + r.module().AddMemoryElems<byte>(kMemSize); + r.module().RandomizeMemory(); - BUILD(r, - WASM_LOAD_MEM_OFFSET(MachineType::Int32(), offset, WASM_I8(index))); + BUILD(r, WASM_LOAD_MEM_OFFSET(MachineType::Int32(), offset, + WASM_I32V_2(index))); if ((offset + index) <= static_cast<int>((kMemSize - sizeof(int32_t)))) { - CHECK_EQ(module.raw_val_at<int32_t>(offset + index), r.Call()); + CHECK_EQ(r.module().raw_val_at<int32_t>(offset + index), r.Call()); } else { CHECK_TRAP(r.Call()); } @@ -1628,27 +1559,25 @@ WASM_EXEC_TEST(LoadMemI32_const_oob) { } WASM_EXEC_TEST(StoreMemI32_alignment) { - TestingModule module(execution_mode); - int32_t* memory = module.AddMemoryElems<int32_t>(4); const int32_t kWritten = 0x12345678; for (byte i = 0; i <= 2; ++i) { - WasmRunner<int32_t> r(&module, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); + int32_t* memory = r.module().AddMemoryElems<int32_t>(4); BUILD(r, WASM_STORE_MEM_ALIGNMENT(MachineType::Int32(), WASM_ZERO, i, WASM_GET_LOCAL(0)), WASM_GET_LOCAL(0)); - module.RandomizeMemory(1111); + r.module().RandomizeMemory(1111); memory[0] = 0; CHECK_EQ(kWritten, r.Call(kWritten)); - CHECK_EQ(kWritten, module.ReadMemory(&memory[0])); + CHECK_EQ(kWritten, r.module().ReadMemory(&memory[0])); } } WASM_EXEC_TEST(StoreMemI32_offset) { - TestingModule module(execution_mode); - int32_t* memory = module.AddMemoryElems<int32_t>(4); - WasmRunner<int32_t> r(&module, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); + int32_t* memory = r.module().AddMemoryElems<int32_t>(4); const int32_t kWritten = 0xaabbccdd; BUILD(r, WASM_STORE_MEM_OFFSET(MachineType::Int32(), 4, WASM_GET_LOCAL(0), @@ -1656,23 +1585,20 @@ WASM_EXEC_TEST(StoreMemI32_offset) { WASM_I32V_5(kWritten)); for (int i = 0; i < 2; ++i) { - module.RandomizeMemory(1111); - module.WriteMemory(&memory[0], 66666666); - module.WriteMemory(&memory[1], 77777777); - module.WriteMemory(&memory[2], 88888888); - module.WriteMemory(&memory[3], 99999999); + r.module().RandomizeMemory(1111); + r.module().WriteMemory(&memory[0], 66666666); + r.module().WriteMemory(&memory[1], 77777777); + r.module().WriteMemory(&memory[2], 88888888); + r.module().WriteMemory(&memory[3], 99999999); CHECK_EQ(kWritten, r.Call(i * 4)); - CHECK_EQ(66666666, module.ReadMemory(&memory[0])); - CHECK_EQ(i == 0 ? kWritten : 77777777, module.ReadMemory(&memory[1])); - CHECK_EQ(i == 1 ? kWritten : 88888888, module.ReadMemory(&memory[2])); - CHECK_EQ(i == 2 ? kWritten : 99999999, module.ReadMemory(&memory[3])); + CHECK_EQ(66666666, r.module().ReadMemory(&memory[0])); + CHECK_EQ(i == 0 ? kWritten : 77777777, r.module().ReadMemory(&memory[1])); + CHECK_EQ(i == 1 ? kWritten : 88888888, r.module().ReadMemory(&memory[2])); + CHECK_EQ(i == 2 ? kWritten : 99999999, r.module().ReadMemory(&memory[3])); } } -WASM_EXEC_TEST(StoreMem_offset_oob) { - TestingModule module(execution_mode); - byte* memory = module.AddMemoryElems<byte>(32); - +WASM_EXEC_TEST_WITH_TRAP(StoreMem_offset_oob) { // 64-bit cases are handled in test-run-wasm-64.cc static const MachineType machineTypes[] = { MachineType::Int8(), MachineType::Uint8(), MachineType::Int16(), @@ -1680,8 +1606,10 @@ WASM_EXEC_TEST(StoreMem_offset_oob) { MachineType::Float32(), MachineType::Float64()}; for (size_t m = 0; m < arraysize(machineTypes); ++m) { - module.RandomizeMemory(1119 + static_cast<int>(m)); - WasmRunner<int32_t> r(&module, MachineType::Uint32()); + WasmRunner<int32_t, uint32_t> r(execution_mode); + byte* memory = r.module().AddMemoryElems<byte>(32); + + r.module().RandomizeMemory(1119 + static_cast<int>(m)); BUILD(r, WASM_STORE_MEM_OFFSET(machineTypes[m], 8, WASM_GET_LOCAL(0), WASM_LOAD_MEM(machineTypes[m], WASM_ZERO)), @@ -1700,43 +1628,40 @@ WASM_EXEC_TEST(StoreMem_offset_oob) { WASM_EXEC_TEST(LoadMemI32_P) { const int kNumElems = 8; - TestingModule module(execution_mode); - int32_t* memory = module.AddMemoryElems<int32_t>(kNumElems); - WasmRunner<int32_t> r(&module, MachineType::Int32()); - module.RandomizeMemory(2222); + WasmRunner<int32_t, int32_t> r(execution_mode); + int32_t* memory = r.module().AddMemoryElems<int32_t>(kNumElems); + r.module().RandomizeMemory(2222); BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0))); for (int i = 0; i < kNumElems; ++i) { - CHECK_EQ(module.ReadMemory(&memory[i]), r.Call(i * 4)); + CHECK_EQ(r.module().ReadMemory(&memory[i]), r.Call(i * 4)); } } WASM_EXEC_TEST(MemI32_Sum) { const int kNumElems = 20; - TestingModule module(execution_mode); - uint32_t* memory = module.AddMemoryElems<uint32_t>(kNumElems); - WasmRunner<uint32_t> r(&module, MachineType::Int32()); - const byte kSum = r.AllocateLocal(kAstI32); + WasmRunner<uint32_t, int32_t> r(execution_mode); + uint32_t* memory = r.module().AddMemoryElems<uint32_t>(kNumElems); + const byte kSum = r.AllocateLocal(kWasmI32); - BUILD( - r, - WASM_WHILE( - WASM_GET_LOCAL(0), - WASM_BLOCK( - WASM_SET_LOCAL(kSum, - WASM_I32_ADD(WASM_GET_LOCAL(kSum), + BUILD(r, WASM_WHILE( + WASM_GET_LOCAL(0), + WASM_BLOCK( + WASM_SET_LOCAL( + kSum, WASM_I32_ADD(WASM_GET_LOCAL(kSum), WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0)))), - WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(4))))), - WASM_GET_LOCAL(1)); + WASM_SET_LOCAL( + 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I32V_1(4))))), + WASM_GET_LOCAL(1)); // Run 4 trials. for (int i = 0; i < 3; ++i) { - module.RandomizeMemory(i * 33); + r.module().RandomizeMemory(i * 33); uint32_t expected = 0; for (size_t j = kNumElems - 1; j > 0; --j) { - expected += module.ReadMemory(&memory[j]); + expected += r.module().ReadMemory(&memory[j]); } uint32_t result = r.Call(4 * (kNumElems - 1)); CHECK_EQ(expected, result); @@ -1745,9 +1670,8 @@ WASM_EXEC_TEST(MemI32_Sum) { WASM_EXEC_TEST(CheckMachIntsZero) { const int kNumElems = 55; - TestingModule module(execution_mode); - module.AddMemoryElems<uint32_t>(kNumElems); - WasmRunner<uint32_t> r(&module, MachineType::Int32()); + WasmRunner<uint32_t, int32_t> r(execution_mode); + r.module().AddMemoryElems<uint32_t>(kNumElems); BUILD(r, // -- /**/ kExprLoop, kLocalVoid, // -- @@ -1756,78 +1680,76 @@ WASM_EXEC_TEST(CheckMachIntsZero) { /* */ kExprGetLocal, 0, // -- /* */ kExprI32LoadMem, 0, 0, // -- /* */ kExprIf, kLocalVoid, // -- - /* */ kExprI8Const, 255, // -- + /* */ kExprI32Const, 127, // -- /* */ kExprReturn, // -- /* */ kExprEnd, // -- /* */ kExprGetLocal, 0, // -- - /* */ kExprI8Const, 4, // -- + /* */ kExprI32Const, 4, // -- /* */ kExprI32Sub, // -- /* */ kExprTeeLocal, 0, // -- /* */ kExprBr, DEPTH_0, // -- /* */ kExprEnd, // -- /**/ kExprEnd, // -- - /**/ kExprI8Const, 0); // -- + /**/ kExprI32Const, 0); // -- - module.BlankMemory(); - CHECK_EQ(0u, r.Call((kNumElems - 1) * 4)); + r.module().BlankMemory(); + CHECK_EQ(0, r.Call((kNumElems - 1) * 4)); } WASM_EXEC_TEST(MemF32_Sum) { const int kSize = 5; - TestingModule module(execution_mode); - module.AddMemoryElems<float>(kSize); - float* buffer = module.raw_mem_start<float>(); - module.WriteMemory(&buffer[0], -99.25f); - module.WriteMemory(&buffer[1], -888.25f); - module.WriteMemory(&buffer[2], -77.25f); - module.WriteMemory(&buffer[3], 66666.25f); - module.WriteMemory(&buffer[4], 5555.25f); - WasmRunner<int32_t> r(&module, MachineType::Int32()); - const byte kSum = r.AllocateLocal(kAstF32); + WasmRunner<int32_t, int32_t> r(execution_mode); + r.module().AddMemoryElems<float>(kSize); + float* buffer = r.module().raw_mem_start<float>(); + r.module().WriteMemory(&buffer[0], -99.25f); + r.module().WriteMemory(&buffer[1], -888.25f); + r.module().WriteMemory(&buffer[2], -77.25f); + r.module().WriteMemory(&buffer[3], 66666.25f); + r.module().WriteMemory(&buffer[4], 5555.25f); + const byte kSum = r.AllocateLocal(kWasmF32); - BUILD( - r, - WASM_WHILE( - WASM_GET_LOCAL(0), - WASM_BLOCK( - WASM_SET_LOCAL(kSum, - WASM_F32_ADD(WASM_GET_LOCAL(kSum), + BUILD(r, WASM_WHILE( + WASM_GET_LOCAL(0), + WASM_BLOCK( + WASM_SET_LOCAL( + kSum, WASM_F32_ADD(WASM_GET_LOCAL(kSum), WASM_LOAD_MEM(MachineType::Float32(), WASM_GET_LOCAL(0)))), - WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(4))))), - WASM_STORE_MEM(MachineType::Float32(), WASM_ZERO, WASM_GET_LOCAL(kSum)), - WASM_GET_LOCAL(0)); + WASM_SET_LOCAL( + 0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I32V_1(4))))), + WASM_STORE_MEM(MachineType::Float32(), WASM_ZERO, WASM_GET_LOCAL(kSum)), + WASM_GET_LOCAL(0)); CHECK_EQ(0, r.Call(4 * (kSize - 1))); - CHECK_NE(-99.25f, module.ReadMemory(&buffer[0])); - CHECK_EQ(71256.0f, module.ReadMemory(&buffer[0])); + CHECK_NE(-99.25f, r.module().ReadMemory(&buffer[0])); + CHECK_EQ(71256.0f, r.module().ReadMemory(&buffer[0])); } template <typename T> T GenerateAndRunFold(WasmExecutionMode execution_mode, WasmOpcode binop, - T* buffer, uint32_t size, LocalType astType, + T* buffer, uint32_t size, ValueType astType, MachineType memType) { - TestingModule module(execution_mode); - T* memory = module.AddMemoryElems<T>(size); + WasmRunner<int32_t, int32_t> r(execution_mode); + T* memory = r.module().AddMemoryElems<T>(size); for (uint32_t i = 0; i < size; ++i) { - module.WriteMemory(&memory[i], buffer[i]); + r.module().WriteMemory(&memory[i], buffer[i]); } - WasmRunner<int32_t> r(&module, MachineType::Int32()); const byte kAccum = r.AllocateLocal(astType); - BUILD(r, WASM_SET_LOCAL(kAccum, WASM_LOAD_MEM(memType, WASM_ZERO)), - WASM_WHILE( - WASM_GET_LOCAL(0), - WASM_BLOCK(WASM_SET_LOCAL( - kAccum, WASM_BINOP(binop, WASM_GET_LOCAL(kAccum), - WASM_LOAD_MEM( - memType, WASM_GET_LOCAL(0)))), - WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), - WASM_I8(sizeof(T)))))), - WASM_STORE_MEM(memType, WASM_ZERO, WASM_GET_LOCAL(kAccum)), - WASM_GET_LOCAL(0)); + BUILD( + r, WASM_SET_LOCAL(kAccum, WASM_LOAD_MEM(memType, WASM_ZERO)), + WASM_WHILE( + WASM_GET_LOCAL(0), + WASM_BLOCK(WASM_SET_LOCAL( + kAccum, + WASM_BINOP(binop, WASM_GET_LOCAL(kAccum), + WASM_LOAD_MEM(memType, WASM_GET_LOCAL(0)))), + WASM_SET_LOCAL(0, WASM_I32_SUB(WASM_GET_LOCAL(0), + WASM_I32V_1(sizeof(T)))))), + WASM_STORE_MEM(memType, WASM_ZERO, WASM_GET_LOCAL(kAccum)), + WASM_GET_LOCAL(0)); r.Call(static_cast<int>(sizeof(T) * (size - 1))); - return module.ReadMemory(&memory[0]); + return r.module().ReadMemory(&memory[0]); } WASM_EXEC_TEST(MemF64_Mul) { @@ -1835,20 +1757,19 @@ WASM_EXEC_TEST(MemF64_Mul) { double buffer[kSize] = {1, 2, 2, 2, 2, 2}; double result = GenerateAndRunFold<double>(execution_mode, kExprF64Mul, buffer, kSize, - kAstF64, MachineType::Float64()); + kWasmF64, MachineType::Float64()); CHECK_EQ(32, result); } WASM_EXEC_TEST(Build_Wasm_Infinite_Loop) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); // Only build the graph and compile, don't run. - BUILD(r, WASM_INFINITE_LOOP); + BUILD(r, WASM_INFINITE_LOOP, WASM_ZERO); } WASM_EXEC_TEST(Build_Wasm_Infinite_Loop_effect) { - TestingModule module(execution_mode); - module.AddMemoryElems<int8_t>(16); - WasmRunner<int32_t> r(&module, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); + r.module().AddMemoryElems<int8_t>(16); // Only build the graph and compile, don't run. BUILD(r, WASM_LOOP(WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO), WASM_DROP), @@ -1856,72 +1777,83 @@ WASM_EXEC_TEST(Build_Wasm_Infinite_Loop_effect) { } WASM_EXEC_TEST(Unreachable0a) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, WASM_BLOCK_I(WASM_BRV(0, WASM_I8(9)), RET(WASM_GET_LOCAL(0)))); + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_BLOCK_I(WASM_BRV(0, WASM_I32V_1(9)), RET(WASM_GET_LOCAL(0)))); CHECK_EQ(9, r.Call(0)); CHECK_EQ(9, r.Call(1)); } WASM_EXEC_TEST(Unreachable0b) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, WASM_BLOCK_I(WASM_BRV(0, WASM_I8(7)), WASM_UNREACHABLE)); + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_BLOCK_I(WASM_BRV(0, WASM_I32V_1(7)), WASM_UNREACHABLE)); CHECK_EQ(7, r.Call(0)); CHECK_EQ(7, r.Call(1)); } TEST(Build_Wasm_Unreachable1) { - WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(kExecuteCompiled); BUILD(r, WASM_UNREACHABLE); } TEST(Build_Wasm_Unreachable2) { - WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(kExecuteCompiled); BUILD(r, WASM_UNREACHABLE, WASM_UNREACHABLE); } TEST(Build_Wasm_Unreachable3) { - WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(kExecuteCompiled); BUILD(r, WASM_UNREACHABLE, WASM_UNREACHABLE, WASM_UNREACHABLE); } TEST(Build_Wasm_UnreachableIf1) { - WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32()); - BUILD(r, WASM_UNREACHABLE, WASM_IF(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0))); + WasmRunner<int32_t, int32_t> r(kExecuteCompiled); + BUILD(r, WASM_UNREACHABLE, + WASM_IF(WASM_GET_LOCAL(0), WASM_SEQ(WASM_GET_LOCAL(0), WASM_DROP)), + WASM_ZERO); } TEST(Build_Wasm_UnreachableIf2) { - WasmRunner<int32_t> r(kExecuteCompiled, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(kExecuteCompiled); BUILD(r, WASM_UNREACHABLE, - WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_UNREACHABLE)); + WASM_IF_ELSE_I(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_UNREACHABLE)); } WASM_EXEC_TEST(Unreachable_Load) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); + r.module().AddMemory(8); BUILD(r, WASM_BLOCK_I(WASM_BRV(0, WASM_GET_LOCAL(0)), WASM_LOAD_MEM(MachineType::Int8(), WASM_GET_LOCAL(0)))); CHECK_EQ(11, r.Call(11)); CHECK_EQ(21, r.Call(21)); } +WASM_EXEC_TEST(BrV_Fallthrough) { + WasmRunner<int32_t> r(execution_mode); + BUILD(r, WASM_BLOCK_I(WASM_BLOCK(WASM_BRV(1, WASM_I32V_1(42))), + WASM_I32V_1(22))); + CHECK_EQ(42, r.Call()); +} + WASM_EXEC_TEST(Infinite_Loop_not_taken1) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_INFINITE_LOOP), WASM_I8(45)); + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_INFINITE_LOOP), WASM_I32V_1(45)); // Run the code, but don't go into the infinite loop. CHECK_EQ(45, r.Call(0)); } WASM_EXEC_TEST(Infinite_Loop_not_taken2) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, - WASM_BLOCK_I(WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_I8(45)), - WASM_INFINITE_LOOP))); + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_BLOCK_I( + WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_I32V_1(45)), + WASM_INFINITE_LOOP), + WASM_ZERO)); // Run the code, but don't go into the infinite loop. CHECK_EQ(45, r.Call(1)); } WASM_EXEC_TEST(Infinite_Loop_not_taken2_brif) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, WASM_BLOCK_I(WASM_BRV_IF(0, WASM_I8(45), WASM_GET_LOCAL(0)), + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_BLOCK_I(WASM_BRV_IF(0, WASM_I32V_1(45), WASM_GET_LOCAL(0)), WASM_INFINITE_LOOP)); // Run the code, but don't go into the infinite loop. CHECK_EQ(45, r.Call(1)); @@ -1940,13 +1872,19 @@ static void TestBuildGraphForSimpleExpression(WasmOpcode opcode) { FunctionSig* sig = WasmOpcodes::Signature(opcode); if (sig->parameter_count() == 1) { - byte code[] = {WASM_NO_LOCALS, kExprGetLocal, 0, static_cast<byte>(opcode)}; + byte code[] = {WASM_NO_LOCALS, kExprGetLocal, 0, static_cast<byte>(opcode), + WASM_END}; TestBuildingGraph(&zone, &jsgraph, nullptr, sig, nullptr, code, code + arraysize(code)); } else { - CHECK_EQ(2u, sig->parameter_count()); - byte code[] = {WASM_NO_LOCALS, kExprGetLocal, 0, kExprGetLocal, 1, - static_cast<byte>(opcode)}; + CHECK_EQ(2, sig->parameter_count()); + byte code[] = {WASM_NO_LOCALS, + kExprGetLocal, + 0, + kExprGetLocal, + 1, + static_cast<byte>(opcode), + WASM_END}; TestBuildingGraph(&zone, &jsgraph, nullptr, sig, nullptr, code, code + arraysize(code)); } @@ -1963,12 +1901,11 @@ TEST(Build_Wasm_SimpleExprs) { } WASM_EXEC_TEST(Int32LoadInt8_signext) { - TestingModule module(execution_mode); + WasmRunner<int32_t, int32_t> r(execution_mode); const int kNumElems = 16; - int8_t* memory = module.AddMemoryElems<int8_t>(kNumElems); - module.RandomizeMemory(); + int8_t* memory = r.module().AddMemoryElems<int8_t>(kNumElems); + r.module().RandomizeMemory(); memory[0] = -1; - WasmRunner<int32_t> r(&module, MachineType::Int32()); BUILD(r, WASM_LOAD_MEM(MachineType::Int8(), WASM_GET_LOCAL(0))); for (int i = 0; i < kNumElems; ++i) { @@ -1977,12 +1914,11 @@ WASM_EXEC_TEST(Int32LoadInt8_signext) { } WASM_EXEC_TEST(Int32LoadInt8_zeroext) { - TestingModule module(execution_mode); + WasmRunner<int32_t, int32_t> r(execution_mode); const int kNumElems = 16; - byte* memory = module.AddMemory(kNumElems); - module.RandomizeMemory(77); + byte* memory = r.module().AddMemory(kNumElems); + r.module().RandomizeMemory(77); memory[0] = 255; - WasmRunner<int32_t> r(&module, MachineType::Int32()); BUILD(r, WASM_LOAD_MEM(MachineType::Uint8(), WASM_GET_LOCAL(0))); for (int i = 0; i < kNumElems; ++i) { @@ -1991,12 +1927,11 @@ WASM_EXEC_TEST(Int32LoadInt8_zeroext) { } WASM_EXEC_TEST(Int32LoadInt16_signext) { - TestingModule module(execution_mode); + WasmRunner<int32_t, int32_t> r(execution_mode); const int kNumBytes = 16; - byte* memory = module.AddMemory(kNumBytes); - module.RandomizeMemory(888); + byte* memory = r.module().AddMemory(kNumBytes); + r.module().RandomizeMemory(888); memory[1] = 200; - WasmRunner<int32_t> r(&module, MachineType::Int32()); BUILD(r, WASM_LOAD_MEM(MachineType::Int16(), WASM_GET_LOCAL(0))); for (int i = 0; i < kNumBytes; i += 2) { @@ -2006,12 +1941,11 @@ WASM_EXEC_TEST(Int32LoadInt16_signext) { } WASM_EXEC_TEST(Int32LoadInt16_zeroext) { - TestingModule module(execution_mode); + WasmRunner<int32_t, int32_t> r(execution_mode); const int kNumBytes = 16; - byte* memory = module.AddMemory(kNumBytes); - module.RandomizeMemory(9999); + byte* memory = r.module().AddMemory(kNumBytes); + r.module().RandomizeMemory(9999); memory[1] = 204; - WasmRunner<int32_t> r(&module, MachineType::Int32()); BUILD(r, WASM_LOAD_MEM(MachineType::Uint16(), WASM_GET_LOCAL(0))); for (int i = 0; i < kNumBytes; i += 2) { @@ -2021,9 +1955,8 @@ WASM_EXEC_TEST(Int32LoadInt16_zeroext) { } WASM_EXEC_TEST(Int32Global) { - TestingModule module(execution_mode); - int32_t* global = module.AddGlobal<int32_t>(kAstI32); - WasmRunner<int32_t> r(&module, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); + int32_t* global = r.module().AddGlobal<int32_t>(); // global = global + p0 BUILD(r, WASM_SET_GLOBAL(0, WASM_I32_ADD(WASM_GET_GLOBAL(0), WASM_GET_LOCAL(0))), @@ -2039,14 +1972,13 @@ WASM_EXEC_TEST(Int32Global) { WASM_EXEC_TEST(Int32Globals_DontAlias) { const int kNumGlobals = 3; - TestingModule module(execution_mode); - int32_t* globals[] = {module.AddGlobal<int32_t>(kAstI32), - module.AddGlobal<int32_t>(kAstI32), - module.AddGlobal<int32_t>(kAstI32)}; - for (int g = 0; g < kNumGlobals; ++g) { // global = global + p0 - WasmRunner<int32_t> r(&module, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); + int32_t* globals[] = {r.module().AddGlobal<int32_t>(), + r.module().AddGlobal<int32_t>(), + r.module().AddGlobal<int32_t>()}; + BUILD(r, WASM_SET_GLOBAL( g, WASM_I32_ADD(WASM_GET_GLOBAL(g), WASM_GET_LOCAL(0))), WASM_GET_GLOBAL(g)); @@ -2068,9 +2000,8 @@ WASM_EXEC_TEST(Int32Globals_DontAlias) { } WASM_EXEC_TEST(Float32Global) { - TestingModule module(execution_mode); - float* global = module.AddGlobal<float>(kAstF32); - WasmRunner<int32_t> r(&module, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); + float* global = r.module().AddGlobal<float>(); // global = global + p0 BUILD(r, WASM_SET_GLOBAL( 0, WASM_F32_ADD(WASM_GET_GLOBAL(0), @@ -2086,9 +2017,8 @@ WASM_EXEC_TEST(Float32Global) { } WASM_EXEC_TEST(Float64Global) { - TestingModule module(execution_mode); - double* global = module.AddGlobal<double>(kAstF64); - WasmRunner<int32_t> r(&module, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); + double* global = r.module().AddGlobal<double>(); // global = global + p0 BUILD(r, WASM_SET_GLOBAL( 0, WASM_F64_ADD(WASM_GET_GLOBAL(0), @@ -2104,16 +2034,15 @@ WASM_EXEC_TEST(Float64Global) { } WASM_EXEC_TEST(MixedGlobals) { - TestingModule module(execution_mode); - int32_t* unused = module.AddGlobal<int32_t>(kAstI32); - byte* memory = module.AddMemory(32); + WasmRunner<int32_t, int32_t> r(execution_mode); - int32_t* var_int32 = module.AddGlobal<int32_t>(kAstI32); - uint32_t* var_uint32 = module.AddGlobal<uint32_t>(kAstI32); - float* var_float = module.AddGlobal<float>(kAstF32); - double* var_double = module.AddGlobal<double>(kAstF64); + int32_t* unused = r.module().AddGlobal<int32_t>(); + byte* memory = r.module().AddMemory(32); - WasmRunner<int32_t> r(&module, MachineType::Int32()); + int32_t* var_int32 = r.module().AddGlobal<int32_t>(); + uint32_t* var_uint32 = r.module().AddGlobal<uint32_t>(); + float* var_float = r.module().AddGlobal<float>(); + double* var_double = r.module().AddGlobal<double>(); BUILD(r, WASM_SET_GLOBAL(1, WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), WASM_SET_GLOBAL(2, WASM_LOAD_MEM(MachineType::Uint32(), WASM_ZERO)), @@ -2141,36 +2070,33 @@ WASM_EXEC_TEST(MixedGlobals) { WASM_EXEC_TEST(CallEmpty) { const int32_t kExpected = -414444; + WasmRunner<int32_t> r(execution_mode); + // Build the target function. - TestSignatures sigs; - TestingModule module(execution_mode); - WasmFunctionCompiler t(sigs.i_v(), &module); - BUILD(t, WASM_I32V_3(kExpected)); - uint32_t index = t.CompileAndAdd(); + WasmFunctionCompiler& target_func = r.NewFunction<int>(); + BUILD(target_func, WASM_I32V_3(kExpected)); // Build the calling function. - WasmRunner<int32_t> r(&module); - BUILD(r, WASM_CALL_FUNCTION0(index)); + BUILD(r, WASM_CALL_FUNCTION0(target_func.function_index())); int32_t result = r.Call(); CHECK_EQ(kExpected, result); } WASM_EXEC_TEST(CallF32StackParameter) { + WasmRunner<float> r(execution_mode); + // Build the target function. - LocalType param_types[20]; - for (int i = 0; i < 20; ++i) param_types[i] = kAstF32; + ValueType param_types[20]; + for (int i = 0; i < 20; ++i) param_types[i] = kWasmF32; FunctionSig sig(1, 19, param_types); - TestingModule module(execution_mode); - WasmFunctionCompiler t(&sig, &module); + WasmFunctionCompiler& t = r.NewFunction(&sig); BUILD(t, WASM_GET_LOCAL(17)); - uint32_t index = t.CompileAndAdd(); // Build the calling function. - WasmRunner<float> r(&module); BUILD(r, WASM_CALL_FUNCTION( - index, WASM_F32(1.0f), WASM_F32(2.0f), WASM_F32(4.0f), - WASM_F32(8.0f), WASM_F32(16.0f), WASM_F32(32.0f), + t.function_index(), WASM_F32(1.0f), WASM_F32(2.0f), + WASM_F32(4.0f), WASM_F32(8.0f), WASM_F32(16.0f), WASM_F32(32.0f), WASM_F32(64.0f), WASM_F32(128.0f), WASM_F32(256.0f), WASM_F32(1.5f), WASM_F32(2.5f), WASM_F32(4.5f), WASM_F32(8.5f), WASM_F32(16.5f), WASM_F32(32.5f), WASM_F32(64.5f), @@ -2181,18 +2107,17 @@ WASM_EXEC_TEST(CallF32StackParameter) { } WASM_EXEC_TEST(CallF64StackParameter) { + WasmRunner<double> r(execution_mode); + // Build the target function. - LocalType param_types[20]; - for (int i = 0; i < 20; ++i) param_types[i] = kAstF64; + ValueType param_types[20]; + for (int i = 0; i < 20; ++i) param_types[i] = kWasmF64; FunctionSig sig(1, 19, param_types); - TestingModule module(execution_mode); - WasmFunctionCompiler t(&sig, &module); + WasmFunctionCompiler& t = r.NewFunction(&sig); BUILD(t, WASM_GET_LOCAL(17)); - uint32_t index = t.CompileAndAdd(); // Build the calling function. - WasmRunner<double> r(&module); - BUILD(r, WASM_CALL_FUNCTION(index, WASM_F64(1.0), WASM_F64(2.0), + BUILD(r, WASM_CALL_FUNCTION(t.function_index(), WASM_F64(1.0), WASM_F64(2.0), WASM_F64(4.0), WASM_F64(8.0), WASM_F64(16.0), WASM_F64(32.0), WASM_F64(64.0), WASM_F64(128.0), WASM_F64(256.0), WASM_F64(1.5), WASM_F64(2.5), @@ -2205,41 +2130,39 @@ WASM_EXEC_TEST(CallF64StackParameter) { } WASM_EXEC_TEST(CallVoid) { + WasmRunner<int32_t> r(execution_mode); + const byte kMemOffset = 8; const int32_t kElemNum = kMemOffset / sizeof(int32_t); const int32_t kExpected = 414444; // Build the target function. TestSignatures sigs; - TestingModule module(execution_mode); - int32_t* memory = module.AddMemoryElems<int32_t>(16 / sizeof(int32_t)); - module.RandomizeMemory(); - WasmFunctionCompiler t(sigs.v_v(), &module); - BUILD(t, WASM_STORE_MEM(MachineType::Int32(), WASM_I8(kMemOffset), + int32_t* memory = r.module().AddMemoryElems<int32_t>(16 / sizeof(int32_t)); + r.module().RandomizeMemory(); + WasmFunctionCompiler& t = r.NewFunction(sigs.v_v()); + BUILD(t, WASM_STORE_MEM(MachineType::Int32(), WASM_I32V_1(kMemOffset), WASM_I32V_3(kExpected))); - uint32_t index = t.CompileAndAdd(); // Build the calling function. - WasmRunner<int32_t> r(&module); - BUILD(r, WASM_CALL_FUNCTION0(index), - WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kMemOffset))); + BUILD(r, WASM_CALL_FUNCTION0(t.function_index()), + WASM_LOAD_MEM(MachineType::Int32(), WASM_I32V_1(kMemOffset))); int32_t result = r.Call(); CHECK_EQ(kExpected, result); CHECK_EQ(static_cast<int64_t>(kExpected), - static_cast<int64_t>(module.ReadMemory(&memory[kElemNum]))); + static_cast<int64_t>(r.module().ReadMemory(&memory[kElemNum]))); } WASM_EXEC_TEST(Call_Int32Add) { + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); + // Build the target function. - TestSignatures sigs; - TestingModule module(execution_mode); - WasmFunctionCompiler t(sigs.i_ii(), &module); + WasmFunctionCompiler& t = r.NewFunction<int32_t, int32_t, int32_t>(); BUILD(t, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - uint32_t index = t.CompileAndAdd(); // Build the caller function. - WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32()); - BUILD(r, WASM_CALL_FUNCTION(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); + BUILD(r, WASM_CALL_FUNCTION(t.function_index(), WASM_GET_LOCAL(0), + WASM_GET_LOCAL(1))); FOR_INT32_INPUTS(i) { FOR_INT32_INPUTS(j) { @@ -2251,17 +2174,15 @@ WASM_EXEC_TEST(Call_Int32Add) { } WASM_EXEC_TEST(Call_Float32Sub) { - TestSignatures sigs; - TestingModule module(execution_mode); - WasmFunctionCompiler t(sigs.f_ff(), &module); + WasmRunner<float, float, float> r(execution_mode); // Build the target function. - BUILD(t, WASM_F32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - uint32_t index = t.CompileAndAdd(); + WasmFunctionCompiler& target_func = r.NewFunction<float, float, float>(); + BUILD(target_func, WASM_F32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - // Builder the caller function. - WasmRunner<float> r(&module, MachineType::Float32(), MachineType::Float32()); - BUILD(r, WASM_CALL_FUNCTION(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); + // Build the caller function. + BUILD(r, WASM_CALL_FUNCTION(target_func.function_index(), WASM_GET_LOCAL(0), + WASM_GET_LOCAL(1))); FOR_FLOAT32_INPUTS(i) { FOR_FLOAT32_INPUTS(j) { CHECK_FLOAT_EQ(*i - *j, r.Call(*i, *j)); } @@ -2269,27 +2190,28 @@ WASM_EXEC_TEST(Call_Float32Sub) { } WASM_EXEC_TEST(Call_Float64Sub) { - TestingModule module(execution_mode); - double* memory = module.AddMemoryElems<double>(16); - WasmRunner<int32_t> r(&module); + WasmRunner<int32_t> r(execution_mode); + double* memory = r.module().AddMemoryElems<double>(16); BUILD(r, WASM_STORE_MEM( MachineType::Float64(), WASM_ZERO, - WASM_F64_SUB(WASM_LOAD_MEM(MachineType::Float64(), WASM_ZERO), - WASM_LOAD_MEM(MachineType::Float64(), WASM_I8(8)))), - WASM_I8(107)); + WASM_F64_SUB( + WASM_LOAD_MEM(MachineType::Float64(), WASM_ZERO), + WASM_LOAD_MEM(MachineType::Float64(), WASM_I32V_1(8)))), + WASM_I32V_2(107)); FOR_FLOAT64_INPUTS(i) { FOR_FLOAT64_INPUTS(j) { - module.WriteMemory(&memory[0], *i); - module.WriteMemory(&memory[1], *j); + r.module().WriteMemory(&memory[0], *i); + r.module().WriteMemory(&memory[1], *j); double expected = *i - *j; CHECK_EQ(107, r.Call()); if (expected != expected) { - CHECK(module.ReadMemory(&memory[0]) != module.ReadMemory(&memory[0])); + CHECK(r.module().ReadMemory(&memory[0]) != + r.module().ReadMemory(&memory[0])); } else { - CHECK_EQ(expected, module.ReadMemory(&memory[0])); + CHECK_EQ(expected, r.module().ReadMemory(&memory[0])); } } } @@ -2317,28 +2239,25 @@ static void Run_WasmMixedCall_N(WasmExecutionMode execution_mode, int start) { for (int which = 0; which < num_params; ++which) { v8::internal::AccountingAllocator allocator; Zone zone(&allocator, ZONE_NAME); - TestingModule module(execution_mode); - module.AddMemory(1024); + WasmRunner<int32_t> r(execution_mode); + r.module().AddMemory(1024); MachineType* memtypes = &mixed[start]; MachineType result = memtypes[which]; // ========================================================================= // Build the selector function. // ========================================================================= - uint32_t index; FunctionSig::Builder b(&zone, 1, num_params); - b.AddReturn(WasmOpcodes::LocalTypeFor(result)); + b.AddReturn(WasmOpcodes::ValueTypeFor(result)); for (int i = 0; i < num_params; ++i) { - b.AddParam(WasmOpcodes::LocalTypeFor(memtypes[i])); + b.AddParam(WasmOpcodes::ValueTypeFor(memtypes[i])); } - WasmFunctionCompiler t(b.Build(), &module); + WasmFunctionCompiler& t = r.NewFunction(b.Build()); BUILD(t, WASM_GET_LOCAL(which)); - index = t.CompileAndAdd(); // ========================================================================= // Build the calling function. // ========================================================================= - WasmRunner<int32_t> r(&module); std::vector<byte> code; // Load the offset for the store. @@ -2347,11 +2266,11 @@ static void Run_WasmMixedCall_N(WasmExecutionMode execution_mode, int start) { // Load the arguments. for (int i = 0; i < num_params; ++i) { int offset = (i + 1) * kElemSize; - ADD_CODE(code, WASM_LOAD_MEM(memtypes[i], WASM_I8(offset))); + ADD_CODE(code, WASM_LOAD_MEM(memtypes[i], WASM_I32V_2(offset))); } // Call the selector function. - ADD_CODE(code, kExprCallFunction, static_cast<byte>(index)); + ADD_CODE(code, WASM_CALL_FUNCTION0(t.function_index())); // Store the result in memory. ADD_CODE(code, @@ -2365,14 +2284,14 @@ static void Run_WasmMixedCall_N(WasmExecutionMode execution_mode, int start) { // Run the code. for (int t = 0; t < 10; ++t) { - module.RandomizeMemory(); + r.module().RandomizeMemory(); CHECK_EQ(kExpected, r.Call()); int size = WasmOpcodes::MemSize(result); for (int i = 0; i < size; ++i) { int base = (which + 1) * kElemSize; - byte expected = module.raw_mem_at<byte>(base + i); - byte result = module.raw_mem_at<byte>(i); + byte expected = r.module().raw_mem_at<byte>(base + i); + byte result = r.module().raw_mem_at<byte>(i); CHECK_EQ(expected, result); } } @@ -2385,15 +2304,12 @@ WASM_EXEC_TEST(MixedCall_2) { Run_WasmMixedCall_N(execution_mode, 2); } WASM_EXEC_TEST(MixedCall_3) { Run_WasmMixedCall_N(execution_mode, 3); } WASM_EXEC_TEST(AddCall) { - TestSignatures sigs; - TestingModule module(execution_mode); - WasmFunctionCompiler t1(sigs.i_ii(), &module); + WasmRunner<int32_t, int32_t> r(kExecuteCompiled); + WasmFunctionCompiler& t1 = r.NewFunction<int32_t, int32_t, int32_t>(); BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - t1.CompileAndAdd(); - WasmRunner<int32_t> r(&module, MachineType::Int32()); - byte local = r.AllocateLocal(kAstI32); - BUILD(r, WASM_SET_LOCAL(local, WASM_I8(99)), + byte local = r.AllocateLocal(kWasmI32); + BUILD(r, WASM_SET_LOCAL(local, WASM_I32V_2(99)), WASM_I32_ADD(WASM_CALL_FUNCTION(t1.function_index(), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)), WASM_CALL_FUNCTION(t1.function_index(), WASM_GET_LOCAL(1), @@ -2406,16 +2322,15 @@ WASM_EXEC_TEST(AddCall) { WASM_EXEC_TEST(MultiReturnSub) { FLAG_wasm_mv_prototype = true; - LocalType storage[] = {kAstI32, kAstI32, kAstI32, kAstI32}; + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); + + ValueType storage[] = {kWasmI32, kWasmI32, kWasmI32, kWasmI32}; FunctionSig sig_ii_ii(2, 2, storage); - TestingModule module(execution_mode); - WasmFunctionCompiler t1(&sig_ii_ii, &module); + WasmFunctionCompiler& t1 = r.NewFunction(&sig_ii_ii); BUILD(t1, WASM_GET_LOCAL(1), WASM_GET_LOCAL(0)); - t1.CompileAndAdd(); - WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32()); - BUILD(r, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), kExprCallFunction, 0, - kExprI32Sub); + BUILD(r, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), + WASM_CALL_FUNCTION0(t1.function_index()), kExprI32Sub); FOR_INT32_INPUTS(i) { FOR_INT32_INPUTS(j) { @@ -2427,10 +2342,10 @@ WASM_EXEC_TEST(MultiReturnSub) { } template <typename T> -void RunMultiReturnSelect(WasmExecutionMode execution_mode, LocalType type, - const T* inputs) { +void RunMultiReturnSelect(WasmExecutionMode execution_mode, const T* inputs) { FLAG_wasm_mv_prototype = true; - LocalType storage[] = {type, type, type, type, type, type}; + ValueType type = WasmOpcodes::ValueTypeFor(MachineTypeForC<T>()); + ValueType storage[] = {type, type, type, type, type, type}; const size_t kNumReturns = 2; const size_t kNumParams = arraysize(storage) - kNumReturns; FunctionSig sig(kNumReturns, kNumParams, storage); @@ -2438,30 +2353,25 @@ void RunMultiReturnSelect(WasmExecutionMode execution_mode, LocalType type, for (size_t i = 0; i < kNumParams; i++) { for (size_t j = 0; j < kNumParams; j++) { for (int k = 0; k < 2; k++) { - TestingModule module(execution_mode); - WasmFunctionCompiler r1(&sig, &module); + WasmRunner<T, T, T, T, T> r(execution_mode); + WasmFunctionCompiler& r1 = r.NewFunction(&sig); BUILD(r1, WASM_GET_LOCAL(i), WASM_GET_LOCAL(j)); - r1.CompileAndAdd(); - - MachineType machine_type = WasmOpcodes::MachineTypeFor(type); - WasmRunner<T> r2(&module, machine_type, machine_type, machine_type, - machine_type); if (k == 0) { - BUILD(r2, WASM_CALL_FUNCTION(r1.function_index(), WASM_GET_LOCAL(0), - WASM_GET_LOCAL(1), WASM_GET_LOCAL(2), - WASM_GET_LOCAL(3)), + BUILD(r, WASM_CALL_FUNCTION(r1.function_index(), WASM_GET_LOCAL(0), + WASM_GET_LOCAL(1), WASM_GET_LOCAL(2), + WASM_GET_LOCAL(3)), WASM_DROP); } else { - BUILD(r2, WASM_CALL_FUNCTION(r1.function_index(), WASM_GET_LOCAL(0), - WASM_GET_LOCAL(1), WASM_GET_LOCAL(2), - WASM_GET_LOCAL(3)), + BUILD(r, WASM_CALL_FUNCTION(r1.function_index(), WASM_GET_LOCAL(0), + WASM_GET_LOCAL(1), WASM_GET_LOCAL(2), + WASM_GET_LOCAL(3)), kExprSetLocal, 0, WASM_DROP, WASM_GET_LOCAL(0)); } T expected = inputs[k == 0 ? i : j]; - CHECK_EQ(expected, r2.Call(inputs[0], inputs[1], inputs[2], inputs[3])); + CHECK_EQ(expected, r.Call(inputs[0], inputs[1], inputs[2], inputs[3])); } } } @@ -2469,12 +2379,12 @@ void RunMultiReturnSelect(WasmExecutionMode execution_mode, LocalType type, WASM_EXEC_TEST(MultiReturnSelect_i32) { static const int32_t inputs[] = {3333333, 4444444, -55555555, -7777777}; - RunMultiReturnSelect<int32_t>(execution_mode, kAstI32, inputs); + RunMultiReturnSelect<int32_t>(execution_mode, inputs); } WASM_EXEC_TEST(MultiReturnSelect_f32) { static const float inputs[] = {33.33333f, 444.4444f, -55555.555f, -77777.77f}; - RunMultiReturnSelect<float>(execution_mode, kAstF32, inputs); + RunMultiReturnSelect<float>(execution_mode, inputs); } WASM_EXEC_TEST(MultiReturnSelect_i64) { @@ -2482,60 +2392,60 @@ WASM_EXEC_TEST(MultiReturnSelect_i64) { // TODO(titzer): implement int64-lowering for multiple return values static const int64_t inputs[] = {33333338888, 44444446666, -555555553333, -77777771111}; - RunMultiReturnSelect<int64_t>(execution_mode, kAstI64, inputs); + RunMultiReturnSelect<int64_t>(execution_mode, inputs); #endif } WASM_EXEC_TEST(MultiReturnSelect_f64) { static const double inputs[] = {3.333333, 44444.44, -55.555555, -7777.777}; - RunMultiReturnSelect<double>(execution_mode, kAstF64, inputs); + RunMultiReturnSelect<double>(execution_mode, inputs); } WASM_EXEC_TEST(ExprBlock2a) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, WASM_BLOCK_I(WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_I8(1))), - WASM_I8(1))); + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_BLOCK_I(WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_I32V_1(1))), + WASM_I32V_1(1))); CHECK_EQ(1, r.Call(0)); CHECK_EQ(1, r.Call(1)); } WASM_EXEC_TEST(ExprBlock2b) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, WASM_BLOCK_I(WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_I8(1))), - WASM_I8(2))); + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_BLOCK_I(WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_I32V_1(1))), + WASM_I32V_1(2))); CHECK_EQ(2, r.Call(0)); CHECK_EQ(1, r.Call(1)); } WASM_EXEC_TEST(ExprBlock2c) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, WASM_BLOCK_I(WASM_BRV_IFD(0, WASM_I8(1), WASM_GET_LOCAL(0)), - WASM_I8(1))); + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_BLOCK_I(WASM_BRV_IFD(0, WASM_I32V_1(1), WASM_GET_LOCAL(0)), + WASM_I32V_1(1))); CHECK_EQ(1, r.Call(0)); CHECK_EQ(1, r.Call(1)); } WASM_EXEC_TEST(ExprBlock2d) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, WASM_BLOCK_I(WASM_BRV_IFD(0, WASM_I8(1), WASM_GET_LOCAL(0)), - WASM_I8(2))); + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_BLOCK_I(WASM_BRV_IFD(0, WASM_I32V_1(1), WASM_GET_LOCAL(0)), + WASM_I32V_1(2))); CHECK_EQ(2, r.Call(0)); CHECK_EQ(1, r.Call(1)); } WASM_EXEC_TEST(ExprBlock_ManualSwitch) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, WASM_BLOCK_I(WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(1)), - WASM_BRV(1, WASM_I8(11))), - WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(2)), - WASM_BRV(1, WASM_I8(12))), - WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(3)), - WASM_BRV(1, WASM_I8(13))), - WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(4)), - WASM_BRV(1, WASM_I8(14))), - WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(5)), - WASM_BRV(1, WASM_I8(15))), - WASM_I8(99))); + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_BLOCK_I(WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(1)), + WASM_BRV(1, WASM_I32V_1(11))), + WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(2)), + WASM_BRV(1, WASM_I32V_1(12))), + WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(3)), + WASM_BRV(1, WASM_I32V_1(13))), + WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(4)), + WASM_BRV(1, WASM_I32V_1(14))), + WASM_IF(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(5)), + WASM_BRV(1, WASM_I32V_1(15))), + WASM_I32V_2(99))); CHECK_EQ(99, r.Call(0)); CHECK_EQ(11, r.Call(1)); CHECK_EQ(12, r.Call(2)); @@ -2546,19 +2456,19 @@ WASM_EXEC_TEST(ExprBlock_ManualSwitch) { } WASM_EXEC_TEST(ExprBlock_ManualSwitch_brif) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); - BUILD(r, - WASM_BLOCK_I(WASM_BRV_IFD(0, WASM_I8(11), - WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(1))), - WASM_BRV_IFD(0, WASM_I8(12), - WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(2))), - WASM_BRV_IFD(0, WASM_I8(13), - WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(3))), - WASM_BRV_IFD(0, WASM_I8(14), - WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(4))), - WASM_BRV_IFD(0, WASM_I8(15), - WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(5))), - WASM_I8(99))); + WasmRunner<int32_t, int32_t> r(execution_mode); + BUILD(r, WASM_BLOCK_I( + WASM_BRV_IFD(0, WASM_I32V_1(11), + WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(1))), + WASM_BRV_IFD(0, WASM_I32V_1(12), + WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(2))), + WASM_BRV_IFD(0, WASM_I32V_1(13), + WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(3))), + WASM_BRV_IFD(0, WASM_I32V_1(14), + WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(4))), + WASM_BRV_IFD(0, WASM_I32V_1(15), + WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(5))), + WASM_I32V_2(99))); CHECK_EQ(99, r.Call(0)); CHECK_EQ(11, r.Call(1)); CHECK_EQ(12, r.Call(2)); @@ -2569,13 +2479,14 @@ WASM_EXEC_TEST(ExprBlock_ManualSwitch_brif) { } WASM_EXEC_TEST(If_nested) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32(), - MachineType::Int32()); + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); - BUILD(r, WASM_IF_ELSE_I( - WASM_GET_LOCAL(0), - WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_I8(11), WASM_I8(12)), - WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_I8(13), WASM_I8(14)))); + BUILD( + r, + WASM_IF_ELSE_I( + WASM_GET_LOCAL(0), + WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_I32V_1(11), WASM_I32V_1(12)), + WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_I32V_1(13), WASM_I32V_1(14)))); CHECK_EQ(11, r.Call(1, 1)); CHECK_EQ(12, r.Call(1, 0)); @@ -2584,26 +2495,25 @@ WASM_EXEC_TEST(If_nested) { } WASM_EXEC_TEST(ExprBlock_if) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32()); + WasmRunner<int32_t, int32_t> r(execution_mode); - BUILD(r, - WASM_BLOCK_I(WASM_IF_ELSE_I(WASM_GET_LOCAL(0), WASM_BRV(0, WASM_I8(11)), - WASM_BRV(1, WASM_I8(14))))); + BUILD(r, WASM_BLOCK_I(WASM_IF_ELSE_I(WASM_GET_LOCAL(0), + WASM_BRV(0, WASM_I32V_1(11)), + WASM_BRV(1, WASM_I32V_1(14))))); CHECK_EQ(11, r.Call(1)); CHECK_EQ(14, r.Call(0)); } WASM_EXEC_TEST(ExprBlock_nested_ifs) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32(), - MachineType::Int32()); + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); BUILD(r, WASM_BLOCK_I(WASM_IF_ELSE_I( WASM_GET_LOCAL(0), - WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_BRV(0, WASM_I8(11)), - WASM_BRV(1, WASM_I8(12))), - WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_BRV(0, WASM_I8(13)), - WASM_BRV(1, WASM_I8(14)))))); + WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_BRV(0, WASM_I32V_1(11)), + WASM_BRV(1, WASM_I32V_1(12))), + WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_BRV(0, WASM_I32V_1(13)), + WASM_BRV(1, WASM_I32V_1(14)))))); CHECK_EQ(11, r.Call(1, 1)); CHECK_EQ(12, r.Call(1, 0)); @@ -2611,64 +2521,66 @@ WASM_EXEC_TEST(ExprBlock_nested_ifs) { CHECK_EQ(14, r.Call(0, 0)); } -WASM_EXEC_TEST(SimpleCallIndirect) { +WASM_EXEC_TEST_WITH_TRAP(SimpleCallIndirect) { TestSignatures sigs; - TestingModule module(execution_mode); + WasmRunner<int32_t, int32_t> r(execution_mode); - WasmFunctionCompiler t1(sigs.i_ii(), &module); + WasmFunctionCompiler& t1 = r.NewFunction(sigs.i_ii()); BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - t1.CompileAndAdd(/*sig_index*/ 1); + t1.SetSigIndex(1); - WasmFunctionCompiler t2(sigs.i_ii(), &module); + WasmFunctionCompiler& t2 = r.NewFunction(sigs.i_ii()); BUILD(t2, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - t2.CompileAndAdd(/*sig_index*/ 1); + t2.SetSigIndex(1); // Signature table. - module.AddSignature(sigs.f_ff()); - module.AddSignature(sigs.i_ii()); - module.AddSignature(sigs.d_dd()); + r.module().AddSignature(sigs.f_ff()); + r.module().AddSignature(sigs.i_ii()); + r.module().AddSignature(sigs.d_dd()); // Function table. - uint16_t indirect_function_table[] = {0, 1}; - module.AddIndirectFunctionTable(indirect_function_table, - arraysize(indirect_function_table)); - module.PopulateIndirectFunctionTable(); + uint16_t indirect_function_table[] = { + static_cast<uint16_t>(t1.function_index()), + static_cast<uint16_t>(t2.function_index())}; + r.module().AddIndirectFunctionTable(indirect_function_table, + arraysize(indirect_function_table)); + r.module().PopulateIndirectFunctionTable(); - // Builder the caller function. - WasmRunner<int32_t> r(&module, MachineType::Int32()); - BUILD(r, WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_I8(66), WASM_I8(22))); + // Build the caller function. + BUILD(r, WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_I32V_2(66), + WASM_I32V_1(22))); CHECK_EQ(88, r.Call(0)); CHECK_EQ(44, r.Call(1)); CHECK_TRAP(r.Call(2)); } -WASM_EXEC_TEST(MultipleCallIndirect) { +WASM_EXEC_TEST_WITH_TRAP(MultipleCallIndirect) { TestSignatures sigs; - TestingModule module(execution_mode); + WasmRunner<int32_t, int32_t, int32_t, int32_t> r(execution_mode); - WasmFunctionCompiler t1(sigs.i_ii(), &module); + WasmFunctionCompiler& t1 = r.NewFunction(sigs.i_ii()); BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - t1.CompileAndAdd(/*sig_index*/ 1); + t1.SetSigIndex(1); - WasmFunctionCompiler t2(sigs.i_ii(), &module); + WasmFunctionCompiler& t2 = r.NewFunction(sigs.i_ii()); BUILD(t2, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - t2.CompileAndAdd(/*sig_index*/ 1); + t2.SetSigIndex(1); // Signature table. - module.AddSignature(sigs.f_ff()); - module.AddSignature(sigs.i_ii()); - module.AddSignature(sigs.d_dd()); + r.module().AddSignature(sigs.f_ff()); + r.module().AddSignature(sigs.i_ii()); + r.module().AddSignature(sigs.d_dd()); // Function table. - uint16_t indirect_function_table[] = {0, 1}; - module.AddIndirectFunctionTable(indirect_function_table, - arraysize(indirect_function_table)); - module.PopulateIndirectFunctionTable(); - - // Builder the caller function. - WasmRunner<int32_t> r(&module, MachineType::Int32(), MachineType::Int32(), - MachineType::Int32()); + uint16_t indirect_function_table[] = { + static_cast<uint16_t>(t1.function_index()), + static_cast<uint16_t>(t2.function_index())}; + r.module().AddIndirectFunctionTable(indirect_function_table, + arraysize(indirect_function_table)); + r.module().PopulateIndirectFunctionTable(); + + // Build the caller function. BUILD(r, WASM_I32_ADD( WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)), @@ -2686,49 +2598,49 @@ WASM_EXEC_TEST(MultipleCallIndirect) { CHECK_TRAP(r.Call(2, 1, 0)); } -WASM_EXEC_TEST(CallIndirect_EmptyTable) { +WASM_EXEC_TEST_WITH_TRAP(CallIndirect_EmptyTable) { TestSignatures sigs; - TestingModule module(execution_mode); + WasmRunner<int32_t, int32_t> r(execution_mode); // One function. - WasmFunctionCompiler t1(sigs.i_ii(), &module); + WasmFunctionCompiler& t1 = r.NewFunction(sigs.i_ii()); BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - t1.CompileAndAdd(/*sig_index*/ 1); + t1.SetSigIndex(1); // Signature table. - module.AddSignature(sigs.f_ff()); - module.AddSignature(sigs.i_ii()); - module.AddIndirectFunctionTable(nullptr, 0); + r.module().AddSignature(sigs.f_ff()); + r.module().AddSignature(sigs.i_ii()); + r.module().AddIndirectFunctionTable(nullptr, 0); - // Builder the caller function. - WasmRunner<int32_t> r(&module, MachineType::Int32()); - BUILD(r, WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_I8(66), WASM_I8(22))); + // Build the caller function. + BUILD(r, WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_I32V_2(66), + WASM_I32V_1(22))); CHECK_TRAP(r.Call(0)); CHECK_TRAP(r.Call(1)); CHECK_TRAP(r.Call(2)); } -WASM_EXEC_TEST(CallIndirect_canonical) { +WASM_EXEC_TEST_WITH_TRAP(CallIndirect_canonical) { TestSignatures sigs; - TestingModule module(execution_mode); + WasmRunner<int32_t, int32_t> r(execution_mode); - WasmFunctionCompiler t1(sigs.i_ii(), &module); + WasmFunctionCompiler& t1 = r.NewFunction(sigs.i_ii()); BUILD(t1, WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - t1.CompileAndAdd(/*sig_index*/ 0); + t1.SetSigIndex(0); - WasmFunctionCompiler t2(sigs.i_ii(), &module); + WasmFunctionCompiler& t2 = r.NewFunction(sigs.i_ii()); BUILD(t2, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - t2.CompileAndAdd(/*sig_index*/ 1); + t2.SetSigIndex(1); - WasmFunctionCompiler t3(sigs.f_ff(), &module); + WasmFunctionCompiler& t3 = r.NewFunction(sigs.f_ff()); BUILD(t3, WASM_F32_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); - t3.CompileAndAdd(/*sig_index*/ 2); + t3.SetSigIndex(2); // Signature table. - module.AddSignature(sigs.i_ii()); - module.AddSignature(sigs.i_ii()); - module.AddSignature(sigs.f_ff()); + r.module().AddSignature(sigs.i_ii()); + r.module().AddSignature(sigs.i_ii()); + r.module().AddSignature(sigs.f_ff()); // Function table. uint16_t i1 = static_cast<uint16_t>(t1.function_index()); @@ -2736,13 +2648,13 @@ WASM_EXEC_TEST(CallIndirect_canonical) { uint16_t i3 = static_cast<uint16_t>(t3.function_index()); uint16_t indirect_function_table[] = {i1, i2, i3, i1, i2}; - module.AddIndirectFunctionTable(indirect_function_table, - arraysize(indirect_function_table)); - module.PopulateIndirectFunctionTable(); + r.module().AddIndirectFunctionTable(indirect_function_table, + arraysize(indirect_function_table)); + r.module().PopulateIndirectFunctionTable(); - // Builder the caller function. - WasmRunner<int32_t> r(&module, MachineType::Int32()); - BUILD(r, WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_I8(77), WASM_I8(11))); + // Build the caller function. + BUILD(r, WASM_CALL_INDIRECT2(1, WASM_GET_LOCAL(0), WASM_I32V_2(77), + WASM_I32V_1(11))); CHECK_EQ(88, r.Call(0)); CHECK_EQ(66, r.Call(1)); @@ -2753,64 +2665,63 @@ WASM_EXEC_TEST(CallIndirect_canonical) { } WASM_EXEC_TEST(F32Floor) { - WasmRunner<float> r(execution_mode, MachineType::Float32()); + WasmRunner<float, float> r(execution_mode); BUILD(r, WASM_F32_FLOOR(WASM_GET_LOCAL(0))); FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(floorf(*i), r.Call(*i)); } } WASM_EXEC_TEST(F32Ceil) { - WasmRunner<float> r(execution_mode, MachineType::Float32()); + WasmRunner<float, float> r(execution_mode); BUILD(r, WASM_F32_CEIL(WASM_GET_LOCAL(0))); FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(ceilf(*i), r.Call(*i)); } } WASM_EXEC_TEST(F32Trunc) { - WasmRunner<float> r(execution_mode, MachineType::Float32()); + WasmRunner<float, float> r(execution_mode); BUILD(r, WASM_F32_TRUNC(WASM_GET_LOCAL(0))); FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(truncf(*i), r.Call(*i)); } } WASM_EXEC_TEST(F32NearestInt) { - WasmRunner<float> r(execution_mode, MachineType::Float32()); + WasmRunner<float, float> r(execution_mode); BUILD(r, WASM_F32_NEARESTINT(WASM_GET_LOCAL(0))); FOR_FLOAT32_INPUTS(i) { CHECK_FLOAT_EQ(nearbyintf(*i), r.Call(*i)); } } WASM_EXEC_TEST(F64Floor) { - WasmRunner<double> r(execution_mode, MachineType::Float64()); + WasmRunner<double, double> r(execution_mode); BUILD(r, WASM_F64_FLOOR(WASM_GET_LOCAL(0))); FOR_FLOAT64_INPUTS(i) { CHECK_DOUBLE_EQ(floor(*i), r.Call(*i)); } } WASM_EXEC_TEST(F64Ceil) { - WasmRunner<double> r(execution_mode, MachineType::Float64()); + WasmRunner<double, double> r(execution_mode); BUILD(r, WASM_F64_CEIL(WASM_GET_LOCAL(0))); FOR_FLOAT64_INPUTS(i) { CHECK_DOUBLE_EQ(ceil(*i), r.Call(*i)); } } WASM_EXEC_TEST(F64Trunc) { - WasmRunner<double> r(execution_mode, MachineType::Float64()); + WasmRunner<double, double> r(execution_mode); BUILD(r, WASM_F64_TRUNC(WASM_GET_LOCAL(0))); FOR_FLOAT64_INPUTS(i) { CHECK_DOUBLE_EQ(trunc(*i), r.Call(*i)); } } WASM_EXEC_TEST(F64NearestInt) { - WasmRunner<double> r(execution_mode, MachineType::Float64()); + WasmRunner<double, double> r(execution_mode); BUILD(r, WASM_F64_NEARESTINT(WASM_GET_LOCAL(0))); FOR_FLOAT64_INPUTS(i) { CHECK_DOUBLE_EQ(nearbyint(*i), r.Call(*i)); } } WASM_EXEC_TEST(F32Min) { - WasmRunner<float> r(execution_mode, MachineType::Float32(), - MachineType::Float32()); + WasmRunner<float, float, float> r(execution_mode); BUILD(r, WASM_F32_MIN(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_FLOAT32_INPUTS(i) { @@ -2819,8 +2730,7 @@ WASM_EXEC_TEST(F32Min) { } WASM_EXEC_TEST(F64Min) { - WasmRunner<double> r(execution_mode, MachineType::Float64(), - MachineType::Float64()); + WasmRunner<double, double, double> r(execution_mode); BUILD(r, WASM_F64_MIN(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_FLOAT64_INPUTS(i) { @@ -2829,8 +2739,7 @@ WASM_EXEC_TEST(F64Min) { } WASM_EXEC_TEST(F32Max) { - WasmRunner<float> r(execution_mode, MachineType::Float32(), - MachineType::Float32()); + WasmRunner<float, float, float> r(execution_mode); BUILD(r, WASM_F32_MAX(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_FLOAT32_INPUTS(i) { @@ -2839,8 +2748,7 @@ WASM_EXEC_TEST(F32Max) { } WASM_EXEC_TEST(F64Max) { - WasmRunner<double> r(execution_mode, MachineType::Float64(), - MachineType::Float64()); + WasmRunner<double, double, double> r(execution_mode); BUILD(r, WASM_F64_MAX(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_FLOAT64_INPUTS(i) { @@ -2851,8 +2759,8 @@ WASM_EXEC_TEST(F64Max) { } } -WASM_EXEC_TEST(I32SConvertF32) { - WasmRunner<int32_t> r(execution_mode, MachineType::Float32()); +WASM_EXEC_TEST_WITH_TRAP(I32SConvertF32) { + WasmRunner<int32_t, float> r(execution_mode); BUILD(r, WASM_I32_SCONVERT_F32(WASM_GET_LOCAL(0))); // The upper bound is (INT32_MAX + 1), which is the lowest float-representable @@ -2871,8 +2779,8 @@ WASM_EXEC_TEST(I32SConvertF32) { } } -WASM_EXEC_TEST(I32SConvertF64) { - WasmRunner<int32_t> r(execution_mode, MachineType::Float64()); +WASM_EXEC_TEST_WITH_TRAP(I32SConvertF64) { + WasmRunner<int32_t, double> r(execution_mode); BUILD(r, WASM_I32_SCONVERT_F64(WASM_GET_LOCAL(0))); // The upper bound is (INT32_MAX + 1), which is the lowest double- @@ -2890,8 +2798,8 @@ WASM_EXEC_TEST(I32SConvertF64) { } } -WASM_EXEC_TEST(I32UConvertF32) { - WasmRunner<uint32_t> r(execution_mode, MachineType::Float32()); +WASM_EXEC_TEST_WITH_TRAP(I32UConvertF32) { + WasmRunner<uint32_t, float> r(execution_mode); BUILD(r, WASM_I32_UCONVERT_F32(WASM_GET_LOCAL(0))); // The upper bound is (UINT32_MAX + 1), which is the lowest // float-representable number above UINT32_MAX which cannot be represented as @@ -2907,8 +2815,8 @@ WASM_EXEC_TEST(I32UConvertF32) { } } -WASM_EXEC_TEST(I32UConvertF64) { - WasmRunner<uint32_t> r(execution_mode, MachineType::Float64()); +WASM_EXEC_TEST_WITH_TRAP(I32UConvertF64) { + WasmRunner<uint32_t, double> r(execution_mode); BUILD(r, WASM_I32_UCONVERT_F64(WASM_GET_LOCAL(0))); // The upper bound is (UINT32_MAX + 1), which is the lowest // double-representable number above UINT32_MAX which cannot be represented as @@ -2925,8 +2833,7 @@ WASM_EXEC_TEST(I32UConvertF64) { } WASM_EXEC_TEST(F64CopySign) { - WasmRunner<double> r(execution_mode, MachineType::Float64(), - MachineType::Float64()); + WasmRunner<double, double, double> r(execution_mode); BUILD(r, WASM_F64_COPYSIGN(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_FLOAT64_INPUTS(i) { @@ -2935,8 +2842,7 @@ WASM_EXEC_TEST(F64CopySign) { } WASM_EXEC_TEST(F32CopySign) { - WasmRunner<float> r(execution_mode, MachineType::Float32(), - MachineType::Float32()); + WasmRunner<float, float, float> r(execution_mode); BUILD(r, WASM_F32_COPYSIGN(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); FOR_FLOAT32_INPUTS(i) { @@ -2944,44 +2850,39 @@ WASM_EXEC_TEST(F32CopySign) { } } -static void CompileCallIndirectMany(LocalType param) { +static void CompileCallIndirectMany(ValueType param) { // Make sure we don't run out of registers when compiling indirect calls // with many many parameters. TestSignatures sigs; for (byte num_params = 0; num_params < 40; ++num_params) { - v8::internal::AccountingAllocator allocator; - Zone zone(&allocator, ZONE_NAME); - HandleScope scope(CcTest::InitIsolateOnce()); - TestingModule module(kExecuteCompiled); - FunctionSig* sig = sigs.many(&zone, kAstStmt, param, num_params); + WasmRunner<void> r(kExecuteCompiled); + FunctionSig* sig = sigs.many(r.zone(), kWasmStmt, param, num_params); - module.AddSignature(sig); - module.AddSignature(sig); - module.AddIndirectFunctionTable(nullptr, 0); + r.module().AddSignature(sig); + r.module().AddSignature(sig); + r.module().AddIndirectFunctionTable(nullptr, 0); - WasmFunctionCompiler t(sig, &module); + WasmFunctionCompiler& t = r.NewFunction(sig); std::vector<byte> code; for (byte p = 0; p < num_params; ++p) { ADD_CODE(code, kExprGetLocal, p); } - ADD_CODE(code, kExprI8Const, 0); + ADD_CODE(code, kExprI32Const, 0); ADD_CODE(code, kExprCallIndirect, 1, TABLE_ZERO); t.Build(&code[0], &code[0] + code.size()); - t.Compile(); } } -TEST(Compile_Wasm_CallIndirect_Many_i32) { CompileCallIndirectMany(kAstI32); } +TEST(Compile_Wasm_CallIndirect_Many_i32) { CompileCallIndirectMany(kWasmI32); } -TEST(Compile_Wasm_CallIndirect_Many_f32) { CompileCallIndirectMany(kAstF32); } +TEST(Compile_Wasm_CallIndirect_Many_f32) { CompileCallIndirectMany(kWasmF32); } -TEST(Compile_Wasm_CallIndirect_Many_f64) { CompileCallIndirectMany(kAstF64); } +TEST(Compile_Wasm_CallIndirect_Many_f64) { CompileCallIndirectMany(kWasmF64); } -WASM_EXEC_TEST(Int32RemS_dead) { - WasmRunner<int32_t> r(execution_mode, MachineType::Int32(), - MachineType::Int32()); +WASM_EXEC_TEST_WITH_TRAP(Int32RemS_dead) { + WasmRunner<int32_t, int32_t, int32_t> r(execution_mode); BUILD(r, WASM_I32_REMS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)), WASM_DROP, WASM_ZERO); const int32_t kMin = std::numeric_limits<int32_t>::min(); diff --git a/deps/v8/test/cctest/wasm/test-wasm-breakpoints.cc b/deps/v8/test/cctest/wasm/test-wasm-breakpoints.cc new file mode 100644 index 0000000000..d2374a44c0 --- /dev/null +++ b/deps/v8/test/cctest/wasm/test-wasm-breakpoints.cc @@ -0,0 +1,70 @@ +// Copyright 2016 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "src/debug/debug-interface.h" +#include "src/wasm/wasm-macro-gen.h" +#include "src/wasm/wasm-objects.h" + +#include "test/cctest/cctest.h" +#include "test/cctest/compiler/value-helper.h" +#include "test/cctest/wasm/wasm-run-utils.h" +#include "test/common/wasm/test-signatures.h" + +using namespace v8::internal; +using namespace v8::internal::wasm; +namespace debug = v8::debug; + +namespace { + +void CheckLocations( + WasmCompiledModule *compiled_module, debug::Location start, + debug::Location end, + std::initializer_list<debug::Location> expected_locations_init) { + std::vector<debug::Location> locations; + bool success = + compiled_module->GetPossibleBreakpoints(start, end, &locations); + CHECK(success); + + printf("got %d locations: ", static_cast<int>(locations.size())); + for (size_t i = 0, e = locations.size(); i != e; ++i) { + printf("%s<%d,%d>", i == 0 ? "" : ", ", locations[i].GetLineNumber(), + locations[i].GetColumnNumber()); + } + printf("\n"); + + std::vector<debug::Location> expected_locations(expected_locations_init); + CHECK_EQ(expected_locations.size(), locations.size()); + for (size_t i = 0, e = locations.size(); i != e; ++i) { + CHECK_EQ(expected_locations[i].GetLineNumber(), + locations[i].GetLineNumber()); + CHECK_EQ(expected_locations[i].GetColumnNumber(), + locations[i].GetColumnNumber()); + } +} +void CheckLocationsFail(WasmCompiledModule *compiled_module, + debug::Location start, debug::Location end) { + std::vector<debug::Location> locations; + bool success = + compiled_module->GetPossibleBreakpoints(start, end, &locations); + CHECK(!success); +} + +} // namespace + +TEST(CollectPossibleBreakpoints) { + WasmRunner<int> runner(kExecuteCompiled); + + BUILD(runner, WASM_NOP, WASM_I32_ADD(WASM_ZERO, WASM_ONE)); + + Handle<WasmInstanceObject> instance = runner.module().instance_object(); + std::vector<debug::Location> locations; + CheckLocations(instance->compiled_module(), {0, 0}, {1, 0}, + {{0, 1}, {0, 2}, {0, 4}, {0, 6}, {0, 7}}); + CheckLocations(instance->compiled_module(), {0, 2}, {0, 4}, {{0, 2}}); + CheckLocations(instance->compiled_module(), {0, 2}, {0, 5}, {{0, 2}, {0, 4}}); + CheckLocations(instance->compiled_module(), {0, 7}, {0, 8}, {{0, 7}}); + CheckLocations(instance->compiled_module(), {0, 7}, {1, 0}, {{0, 7}}); + CheckLocations(instance->compiled_module(), {0, 8}, {1, 0}, {}); + CheckLocationsFail(instance->compiled_module(), {0, 9}, {1, 0}); +} diff --git a/deps/v8/test/cctest/wasm/test-wasm-stack.cc b/deps/v8/test/cctest/wasm/test-wasm-stack.cc index a0c2e73f9d..357a4e487e 100644 --- a/deps/v8/test/cctest/wasm/test-wasm-stack.cc +++ b/deps/v8/test/cctest/wasm/test-wasm-stack.cc @@ -76,25 +76,22 @@ void CheckExceptionInfos(Handle<Object> exc, // Call from JS to WASM to JS and throw an Error from JS. TEST(CollectDetailedWasmStack_ExplicitThrowFromJs) { + WasmRunner<void> r(kExecuteCompiled); TestSignatures sigs; - TestingModule module; - // Initialize WasmFunctionCompiler first, since it sets up the HandleScope. - WasmFunctionCompiler comp1(sigs.v_v(), &module); - - uint32_t js_throwing_index = module.AddJsFunction( + uint32_t js_throwing_index = r.module().AddJsFunction( sigs.v_v(), "(function js() {\n function a() {\n throw new Error(); };\n a(); })"); // Add a nop such that we don't always get position 1. - BUILD(comp1, WASM_NOP, WASM_CALL_FUNCTION0(js_throwing_index)); - uint32_t wasm_index = comp1.CompileAndAdd(); + BUILD(r, WASM_NOP, WASM_CALL_FUNCTION0(js_throwing_index)); + uint32_t wasm_index_1 = r.function()->func_index; - WasmFunctionCompiler comp2(sigs.v_v(), &module); - BUILD(comp2, WASM_CALL_FUNCTION0(wasm_index)); - uint32_t wasm_index_2 = comp2.CompileAndAdd(); + WasmFunctionCompiler& f2 = r.NewFunction<void>("call_main"); + BUILD(f2, WASM_CALL_FUNCTION0(wasm_index_1)); + uint32_t wasm_index_2 = f2.function_index(); - Handle<JSFunction> js_wasm_wrapper = module.WrapCode(wasm_index_2); + Handle<JSFunction> js_wasm_wrapper = r.module().WrapCode(wasm_index_2); Handle<JSFunction> js_trampoline = Handle<JSFunction>::cast( v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( @@ -107,16 +104,17 @@ TEST(CollectDetailedWasmStack_ExplicitThrowFromJs) { MaybeHandle<Object> maybe_exc; Handle<Object> args[] = {js_wasm_wrapper}; MaybeHandle<Object> returnObjMaybe = - Execution::TryCall(isolate, js_trampoline, global, 1, args, &maybe_exc); + Execution::TryCall(isolate, js_trampoline, global, 1, args, + Execution::MessageHandling::kReport, &maybe_exc); CHECK(returnObjMaybe.is_null()); // Line and column are 1-based, so add 1 for the expected wasm output. ExceptionInfo expected_exceptions[] = { - {"a", 3, 8}, // - - {"js", 4, 2}, // - - {"<WASM UNNAMED>", static_cast<int>(wasm_index) + 1, 3}, // - - {"<WASM UNNAMED>", static_cast<int>(wasm_index_2) + 1, 2}, // - - {"callFn", 1, 24} // - + {"a", 3, 8}, // - + {"js", 4, 2}, // - + {"main", static_cast<int>(wasm_index_1) + 1, 3}, // - + {"call_main", static_cast<int>(wasm_index_2) + 1, 2}, // - + {"callFn", 1, 24} // - }; CheckExceptionInfos(maybe_exc.ToHandleChecked(), expected_exceptions); } @@ -124,21 +122,18 @@ TEST(CollectDetailedWasmStack_ExplicitThrowFromJs) { // Trigger a trap in WASM, stack should be JS -> WASM -> WASM. TEST(CollectDetailedWasmStack_WasmError) { TestSignatures sigs; - TestingModule module; - - WasmFunctionCompiler comp1(sigs.i_v(), &module, - ArrayVector("exec_unreachable")); + WasmRunner<int> r(kExecuteCompiled); // Set the execution context, such that a runtime error can be thrown. - comp1.SetModuleContext(); - BUILD(comp1, WASM_UNREACHABLE); - uint32_t wasm_index = comp1.CompileAndAdd(); + r.SetModuleContext(); + + BUILD(r, WASM_UNREACHABLE); + uint32_t wasm_index_1 = r.function()->func_index; - WasmFunctionCompiler comp2(sigs.i_v(), &module, - ArrayVector("call_exec_unreachable")); - BUILD(comp2, WASM_CALL_FUNCTION0(wasm_index)); - uint32_t wasm_index_2 = comp2.CompileAndAdd(); + WasmFunctionCompiler& f2 = r.NewFunction<int>("call_main"); + BUILD(f2, WASM_CALL_FUNCTION0(0)); + uint32_t wasm_index_2 = f2.function_index(); - Handle<JSFunction> js_wasm_wrapper = module.WrapCode(wasm_index_2); + Handle<JSFunction> js_wasm_wrapper = r.module().WrapCode(wasm_index_2); Handle<JSFunction> js_trampoline = Handle<JSFunction>::cast( v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( @@ -151,14 +146,15 @@ TEST(CollectDetailedWasmStack_WasmError) { MaybeHandle<Object> maybe_exc; Handle<Object> args[] = {js_wasm_wrapper}; MaybeHandle<Object> maybe_return_obj = - Execution::TryCall(isolate, js_trampoline, global, 1, args, &maybe_exc); + Execution::TryCall(isolate, js_trampoline, global, 1, args, + Execution::MessageHandling::kReport, &maybe_exc); CHECK(maybe_return_obj.is_null()); // Line and column are 1-based, so add 1 for the expected wasm output. ExceptionInfo expected_exceptions[] = { - {"<WASM UNNAMED>", static_cast<int>(wasm_index) + 1, 2}, // - - {"<WASM UNNAMED>", static_cast<int>(wasm_index_2) + 1, 2}, // - - {"callFn", 1, 24} //- + {"main", static_cast<int>(wasm_index_1) + 1, 2}, // - + {"call_main", static_cast<int>(wasm_index_2) + 1, 2}, // - + {"callFn", 1, 24} //- }; CheckExceptionInfos(maybe_exc.ToHandleChecked(), expected_exceptions); } diff --git a/deps/v8/test/cctest/wasm/test-wasm-trap-position.cc b/deps/v8/test/cctest/wasm/test-wasm-trap-position.cc index bd4e82dc4c..0418d46bab 100644 --- a/deps/v8/test/cctest/wasm/test-wasm-trap-position.cc +++ b/deps/v8/test/cctest/wasm/test-wasm-trap-position.cc @@ -62,17 +62,15 @@ void CheckExceptionInfos(Handle<Object> exc, // Trigger a trap for executing unreachable. TEST(Unreachable) { + WasmRunner<void> r(kExecuteCompiled); TestSignatures sigs; - TestingModule module; - - WasmFunctionCompiler comp1(sigs.v_v(), &module, - ArrayVector("exec_unreachable")); // Set the execution context, such that a runtime error can be thrown. - comp1.SetModuleContext(); - BUILD(comp1, WASM_UNREACHABLE); - uint32_t wasm_index = comp1.CompileAndAdd(); + r.SetModuleContext(); + + BUILD(r, WASM_UNREACHABLE); + uint32_t wasm_index = r.function()->func_index; - Handle<JSFunction> js_wasm_wrapper = module.WrapCode(wasm_index); + Handle<JSFunction> js_wasm_wrapper = r.module().WrapCode(wasm_index); Handle<JSFunction> js_trampoline = Handle<JSFunction>::cast( v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( @@ -85,36 +83,37 @@ TEST(Unreachable) { MaybeHandle<Object> maybe_exc; Handle<Object> args[] = {js_wasm_wrapper}; MaybeHandle<Object> returnObjMaybe = - Execution::TryCall(isolate, js_trampoline, global, 1, args, &maybe_exc); + Execution::TryCall(isolate, js_trampoline, global, 1, args, + Execution::MessageHandling::kReport, &maybe_exc); CHECK(returnObjMaybe.is_null()); // Line and column are 1-based, so add 1 for the expected wasm output. ExceptionInfo expected_exceptions[] = { - {"<WASM UNNAMED>", static_cast<int>(wasm_index) + 1, 2}, // -- - {"callFn", 1, 24} // -- + {"main", static_cast<int>(wasm_index) + 1, 2}, // -- + {"callFn", 1, 24} // -- }; CheckExceptionInfos(maybe_exc.ToHandleChecked(), expected_exceptions); } // Trigger a trap for loading from out-of-bounds. TEST(IllegalLoad) { + WasmRunner<void> r(kExecuteCompiled); TestSignatures sigs; - TestingModule module; - - WasmFunctionCompiler comp1(sigs.v_v(), &module, ArrayVector("mem_oob")); // Set the execution context, such that a runtime error can be thrown. - comp1.SetModuleContext(); - BUILD(comp1, WASM_IF(WASM_ONE, WASM_SEQ(WASM_LOAD_MEM(MachineType::Int32(), - WASM_I32V_1(-3)), - WASM_DROP))); - uint32_t wasm_index = comp1.CompileAndAdd(); + r.SetModuleContext(); + r.module().AddMemory(0L); + + BUILD(r, WASM_IF(WASM_ONE, WASM_SEQ(WASM_LOAD_MEM(MachineType::Int32(), + WASM_I32V_1(-3)), + WASM_DROP))); + uint32_t wasm_index_1 = r.function()->func_index; - WasmFunctionCompiler comp2(sigs.v_v(), &module, ArrayVector("call_mem_oob")); + WasmFunctionCompiler& f2 = r.NewFunction<void>("call_main"); // Insert a NOP such that the position of the call is not one. - BUILD(comp2, WASM_NOP, WASM_CALL_FUNCTION0(wasm_index)); - uint32_t wasm_index_2 = comp2.CompileAndAdd(); + BUILD(f2, WASM_NOP, WASM_CALL_FUNCTION0(wasm_index_1)); + uint32_t wasm_index_2 = f2.function_index(); - Handle<JSFunction> js_wasm_wrapper = module.WrapCode(wasm_index_2); + Handle<JSFunction> js_wasm_wrapper = r.module().WrapCode(wasm_index_2); Handle<JSFunction> js_trampoline = Handle<JSFunction>::cast( v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast( @@ -127,14 +126,15 @@ TEST(IllegalLoad) { MaybeHandle<Object> maybe_exc; Handle<Object> args[] = {js_wasm_wrapper}; MaybeHandle<Object> returnObjMaybe = - Execution::TryCall(isolate, js_trampoline, global, 1, args, &maybe_exc); + Execution::TryCall(isolate, js_trampoline, global, 1, args, + Execution::MessageHandling::kReport, &maybe_exc); CHECK(returnObjMaybe.is_null()); // Line and column are 1-based, so add 1 for the expected wasm output. ExceptionInfo expected_exceptions[] = { - {"<WASM UNNAMED>", static_cast<int>(wasm_index) + 1, 8}, // -- - {"<WASM UNNAMED>", static_cast<int>(wasm_index_2) + 1, 3}, // -- - {"callFn", 1, 24} // -- + {"main", static_cast<int>(wasm_index_1) + 1, 8}, // -- + {"call_main", static_cast<int>(wasm_index_2) + 1, 3}, // -- + {"callFn", 1, 24} // -- }; CheckExceptionInfos(maybe_exc.ToHandleChecked(), expected_exceptions); } diff --git a/deps/v8/test/cctest/wasm/wasm-run-utils.h b/deps/v8/test/cctest/wasm/wasm-run-utils.h index 284b21c7c4..782e7d9635 100644 --- a/deps/v8/test/cctest/wasm/wasm-run-utils.h +++ b/deps/v8/test/cctest/wasm/wasm-run-utils.h @@ -5,9 +5,11 @@ #ifndef WASM_RUN_UTILS_H #define WASM_RUN_UTILS_H +#include <setjmp.h> #include <stdint.h> #include <stdlib.h> #include <string.h> +#include <array> #include <memory> #include "src/base/utils/random-number-generator.h" @@ -21,7 +23,8 @@ #include "src/compiler/pipeline.h" #include "src/compiler/wasm-compiler.h" #include "src/compiler/zone-stats.h" -#include "src/wasm/ast-decoder.h" +#include "src/wasm/function-body-decoder.h" +#include "src/wasm/wasm-external-refs.h" #include "src/wasm/wasm-interpreter.h" #include "src/wasm/wasm-js.h" #include "src/wasm/wasm-macro-gen.h" @@ -49,7 +52,6 @@ enum WasmExecutionMode { kExecuteInterpreted, kExecuteCompiled }; CHECK_EQ(0xdeadbeefdeadbeef, (bit_cast<uint64_t>(x)) & 0xFFFFFFFFFFFFFFFF) #define CHECK_TRAP(x) CHECK_TRAP32(x) -#define WASM_RUNNER_MAX_NUM_PARAMETERS 4 #define WASM_WRAPPER_RETURN_VALUE 8754 #define BUILD(r, ...) \ @@ -71,23 +73,26 @@ const uint32_t kMaxGlobalsSize = 128; // {WasmInstance}. class TestingModule : public ModuleEnv { public: - explicit TestingModule(WasmExecutionMode mode = kExecuteCompiled) - : execution_mode_(mode), + explicit TestingModule(Zone* zone, WasmExecutionMode mode = kExecuteCompiled) + : ModuleEnv(&module_, &instance_), + execution_mode_(mode), instance_(&module_), isolate_(CcTest::InitIsolateOnce()), global_offset(0), interpreter_(mode == kExecuteInterpreted - ? new WasmInterpreter(&instance_, &allocator_) + ? new WasmInterpreter( + ModuleBytesEnv(&module_, &instance_, + Vector<const byte>::empty()), + zone->allocator()) : nullptr) { - module = &module_; - instance = &instance_; + WasmJs::Install(isolate_); instance->module = &module_; instance->globals_start = global_data; module_.globals_size = kMaxGlobalsSize; instance->mem_start = nullptr; instance->mem_size = 0; - origin = kWasmOrigin; memset(global_data, 0, sizeof(global_data)); + instance_object_ = InitInstanceObject(); } ~TestingModule() { @@ -97,13 +102,15 @@ class TestingModule : public ModuleEnv { if (interpreter_) delete interpreter_; } - void ChangeOriginToAsmjs() { origin = kAsmJsOrigin; } + void ChangeOriginToAsmjs() { module_.origin = kAsmJsOrigin; } byte* AddMemory(uint32_t size) { + CHECK(!module_.has_memory); CHECK_NULL(instance->mem_start); - CHECK_EQ(0u, instance->mem_size); + CHECK_EQ(0, instance->mem_size); + module_.has_memory = true; instance->mem_start = reinterpret_cast<byte*>(malloc(size)); - CHECK(instance->mem_start); + CHECK(size == 0 || instance->mem_start); memset(instance->mem_start, 0, size); instance->mem_size = size; return raw_mem_start<byte>(); @@ -116,7 +123,8 @@ class TestingModule : public ModuleEnv { } template <typename T> - T* AddGlobal(LocalType type) { + T* AddGlobal( + ValueType type = WasmOpcodes::ValueTypeFor(MachineTypeForC<T>())) { const WasmGlobal* global = AddGlobal(type); return reinterpret_cast<T*>(instance->globals_start + global->offset); } @@ -176,7 +184,11 @@ class TestingModule : public ModuleEnv { rng.NextBytes(raw, end - raw); } - uint32_t AddFunction(FunctionSig* sig, Handle<Code> code) { + void SetMaxMemPages(uint32_t max_mem_pages) { + module_.max_mem_pages = max_mem_pages; + } + + uint32_t AddFunction(FunctionSig* sig, Handle<Code> code, const char* name) { if (module->functions.size() == 0) { // TODO(titzer): Reserving space here to avoid the underlying WasmFunction // structs from moving. @@ -184,6 +196,11 @@ class TestingModule : public ModuleEnv { } uint32_t index = static_cast<uint32_t>(module->functions.size()); module_.functions.push_back({sig, index, 0, 0, 0, 0, 0, false, false}); + if (name) { + Vector<const byte> name_vec = Vector<const byte>::cast(CStrVector(name)); + module_.functions.back().name_offset = AddBytes(name_vec); + module_.functions.back().name_length = name_vec.length(); + } instance->function_code.push_back(code); if (interpreter_) { const WasmFunction* function = &module->functions.back(); @@ -197,26 +214,24 @@ class TestingModule : public ModuleEnv { uint32_t AddJsFunction(FunctionSig* sig, const char* source) { Handle<JSFunction> jsfunc = Handle<JSFunction>::cast(v8::Utils::OpenHandle( *v8::Local<v8::Function>::Cast(CompileRun(source)))); - uint32_t index = AddFunction(sig, Handle<Code>::null()); - Handle<Code> code = - CompileWasmToJSWrapper(isolate_, jsfunc, sig, index, - Handle<String>::null(), Handle<String>::null()); + uint32_t index = AddFunction(sig, Handle<Code>::null(), nullptr); + Handle<Code> code = CompileWasmToJSWrapper( + isolate_, jsfunc, sig, index, Handle<String>::null(), + Handle<String>::null(), module->origin); instance->function_code[index] = code; return index; } Handle<JSFunction> WrapCode(uint32_t index) { // Wrap the code so it can be called as a JS function. - Handle<String> name = isolate_->factory()->NewStringFromStaticChars("main"); Handle<WasmInstanceObject> instance_obj(0, isolate_); Handle<Code> code = instance->function_code[index]; - WasmJs::InstallWasmMapsIfNeeded(isolate_, isolate_->native_context()); Handle<Code> ret_code = - compiler::CompileJSToWasmWrapper(isolate_, this, code, index); + compiler::CompileJSToWasmWrapper(isolate_, &module_, code, index); Handle<JSFunction> ret = WasmExportedFunction::New( - isolate_, instance_obj, name, ret_code, + isolate_, instance_obj, MaybeHandle<String>(), static_cast<int>(index), static_cast<int>(this->module->functions[index].sig->parameter_count()), - static_cast<int>(index)); + ret_code); return ret; } @@ -238,40 +253,59 @@ class TestingModule : public ModuleEnv { } instance->function_tables.push_back( - isolate_->factory()->NewFixedArray(table_size * 2)); + isolate_->factory()->NewFixedArray(table_size)); + instance->signature_tables.push_back( + isolate_->factory()->NewFixedArray(table_size)); } void PopulateIndirectFunctionTable() { + if (execution_mode_ == kExecuteInterpreted) return; // Initialize the fixed arrays in instance->function_tables. for (uint32_t i = 0; i < instance->function_tables.size(); i++) { WasmIndirectFunctionTable& table = module_.function_tables[i]; - Handle<FixedArray> array = instance->function_tables[i]; + Handle<FixedArray> function_table = instance->function_tables[i]; + Handle<FixedArray> signature_table = instance->signature_tables[i]; int table_size = static_cast<int>(table.values.size()); for (int j = 0; j < table_size; j++) { WasmFunction& function = module_.functions[table.values[j]]; - array->set(j, Smi::FromInt(table.map.Find(function.sig))); - array->set(j + table_size, - *instance->function_code[function.func_index]); + signature_table->set(j, Smi::FromInt(table.map.Find(function.sig))); + function_table->set(j, *instance->function_code[function.func_index]); } } } + uint32_t AddBytes(Vector<const byte> bytes) { + Handle<SeqOneByteString> old_bytes( + instance_object_->compiled_module()->module_bytes(), isolate_); + uint32_t old_size = static_cast<uint32_t>(old_bytes->length()); + ScopedVector<byte> new_bytes(old_size + bytes.length()); + memcpy(new_bytes.start(), old_bytes->GetChars(), old_size); + memcpy(new_bytes.start() + old_size, bytes.start(), bytes.length()); + Handle<SeqOneByteString> new_bytes_str = Handle<SeqOneByteString>::cast( + isolate_->factory()->NewStringFromOneByte(new_bytes).ToHandleChecked()); + instance_object_->compiled_module()->shared()->set_module_bytes( + *new_bytes_str); + return old_size; + } + WasmFunction* GetFunctionAt(int index) { return &module_.functions[index]; } WasmInterpreter* interpreter() { return interpreter_; } WasmExecutionMode execution_mode() { return execution_mode_; } + Isolate* isolate() { return isolate_; } + Handle<WasmInstanceObject> instance_object() { return instance_object_; } private: WasmExecutionMode execution_mode_; WasmModule module_; WasmInstance instance_; Isolate* isolate_; - v8::internal::AccountingAllocator allocator_; uint32_t global_offset; V8_ALIGNED(8) byte global_data[kMaxGlobalsSize]; // preallocated global data. WasmInterpreter* interpreter_; + Handle<WasmInstanceObject> instance_object_; - const WasmGlobal* AddGlobal(LocalType type) { + const WasmGlobal* AddGlobal(ValueType type) { byte size = WasmOpcodes::MemSize(WasmOpcodes::MachineTypeFor(type)); global_offset = (global_offset + size - 1) & ~(size - 1); // align module_.globals.push_back( @@ -281,21 +315,43 @@ class TestingModule : public ModuleEnv { CHECK_LT(global_offset, kMaxGlobalsSize); return &module->globals.back(); } + + Handle<WasmInstanceObject> InitInstanceObject() { + Handle<SeqOneByteString> empty_string = Handle<SeqOneByteString>::cast( + isolate_->factory()->NewStringFromOneByte({}).ToHandleChecked()); + Handle<Managed<wasm::WasmModule>> module_wrapper = + Managed<wasm::WasmModule>::New(isolate_, &module_, false); + Handle<Script> script = + isolate_->factory()->NewScript(isolate_->factory()->empty_string()); + script->set_type(Script::TYPE_WASM); + Handle<WasmSharedModuleData> shared_module_data = + WasmSharedModuleData::New(isolate_, module_wrapper, empty_string, + script, Handle<ByteArray>::null()); + Handle<WasmCompiledModule> compiled_module = + WasmCompiledModule::New(isolate_, shared_module_data); + // Minimally initialize the compiled module such that IsWasmCompiledModule + // passes. + // If tests need more (correct) information, add it later. + compiled_module->set_min_mem_pages(0); + compiled_module->set_max_mem_pages(Smi::kMaxValue); + DCHECK(WasmCompiledModule::IsWasmCompiledModule(*compiled_module)); + return WasmInstanceObject::New(isolate_, compiled_module); + } }; inline void TestBuildingGraph(Zone* zone, JSGraph* jsgraph, ModuleEnv* module, FunctionSig* sig, SourcePositionTable* source_position_table, const byte* start, const byte* end) { - compiler::WasmGraphBuilder builder(zone, jsgraph, sig, source_position_table); + compiler::WasmGraphBuilder builder(module, zone, jsgraph, sig, + source_position_table); DecodeResult result = - BuildTFGraph(zone->allocator(), &builder, module, sig, start, end); + BuildTFGraph(zone->allocator(), &builder, sig, start, end); if (result.failed()) { if (!FLAG_trace_wasm_decoder) { // Retry the compilation with the tracing flag on, to help in debugging. FLAG_trace_wasm_decoder = true; - result = - BuildTFGraph(zone->allocator(), &builder, module, sig, start, end); + result = BuildTFGraph(zone->allocator(), &builder, sig, start, end); } ptrdiff_t pc = result.error_pc - result.start; @@ -307,39 +363,34 @@ inline void TestBuildingGraph(Zone* zone, JSGraph* jsgraph, ModuleEnv* module, FATAL(str.str().c_str()); } builder.Int64LoweringForTesting(); - builder.SimdScalarLoweringForTesting(); + if (!CpuFeatures::SupportsSimd128()) { + builder.SimdScalarLoweringForTesting(); + } } -template <typename ReturnType> -class WasmFunctionWrapper : public HandleAndZoneScope, - private GraphAndBuilders { +class WasmFunctionWrapper : private GraphAndBuilders { public: - WasmFunctionWrapper() - : GraphAndBuilders(main_zone()), - inner_code_node_(nullptr), - signature_(nullptr) { + explicit WasmFunctionWrapper(Zone* zone, int num_params) + : GraphAndBuilders(zone), inner_code_node_(nullptr), signature_(nullptr) { // One additional parameter for the pointer to the return value memory. - Signature<MachineType>::Builder sig_builder( - zone(), 1, WASM_RUNNER_MAX_NUM_PARAMETERS + 1); + Signature<MachineType>::Builder sig_builder(zone, 1, num_params + 1); sig_builder.AddReturn(MachineType::Int32()); - for (int i = 0; i < WASM_RUNNER_MAX_NUM_PARAMETERS + 1; i++) { + for (int i = 0; i < num_params + 1; i++) { sig_builder.AddParam(MachineType::Pointer()); } signature_ = sig_builder.Build(); } - void Init(CallDescriptor* descriptor, MachineType p0 = MachineType::None(), - MachineType p1 = MachineType::None(), - MachineType p2 = MachineType::None(), - MachineType p3 = MachineType::None()) { - // Create the TF graph for the wrapper. The wrapper always takes four - // pointers as parameters, but may not pass the values of all pointers to - // the actual test function. + void Init(CallDescriptor* descriptor, MachineType return_type, + Vector<MachineType> param_types) { + DCHECK_NOT_NULL(descriptor); + DCHECK_EQ(signature_->parameter_count(), param_types.length() + 1); + + // Create the TF graph for the wrapper. // Function, effect, and control. - Node** parameters = - zone()->template NewArray<Node*>(WASM_RUNNER_MAX_NUM_PARAMETERS + 3); + Node** parameters = zone()->NewArray<Node*>(param_types.length() + 3); graph()->SetStart(graph()->NewNode(common()->Start(6))); Node* effect = graph()->start(); int parameter_count = 0; @@ -348,34 +399,12 @@ class WasmFunctionWrapper : public HandleAndZoneScope, inner_code_node_ = graph()->NewNode(common()->Int32Constant(0)); parameters[parameter_count++] = inner_code_node_; - if (p0 != MachineType::None()) { - parameters[parameter_count] = graph()->NewNode( - machine()->Load(p0), - graph()->NewNode(common()->Parameter(0), graph()->start()), - graph()->NewNode(common()->Int32Constant(0)), effect, - graph()->start()); - effect = parameters[parameter_count++]; - } - if (p1 != MachineType::None()) { - parameters[parameter_count] = graph()->NewNode( - machine()->Load(p1), - graph()->NewNode(common()->Parameter(1), graph()->start()), - graph()->NewNode(common()->Int32Constant(0)), effect, - graph()->start()); - effect = parameters[parameter_count++]; - } - if (p2 != MachineType::None()) { - parameters[parameter_count] = graph()->NewNode( - machine()->Load(p2), - graph()->NewNode(common()->Parameter(2), graph()->start()), - graph()->NewNode(common()->Int32Constant(0)), effect, - graph()->start()); - effect = parameters[parameter_count++]; - } - if (p3 != MachineType::None()) { + int param_idx = 0; + for (MachineType t : param_types) { + DCHECK_NE(MachineType::None(), t); parameters[parameter_count] = graph()->NewNode( - machine()->Load(p3), - graph()->NewNode(common()->Parameter(3), graph()->start()), + machine()->Load(t), + graph()->NewNode(common()->Parameter(param_idx++), graph()->start()), graph()->NewNode(common()->Int32Constant(0)), effect, graph()->start()); effect = parameters[parameter_count++]; @@ -386,14 +415,15 @@ class WasmFunctionWrapper : public HandleAndZoneScope, Node* call = graph()->NewNode(common()->Call(descriptor), parameter_count, parameters); - effect = graph()->NewNode( - machine()->Store( - StoreRepresentation(MachineTypeForC<ReturnType>().representation(), - WriteBarrierKind::kNoWriteBarrier)), - graph()->NewNode(common()->Parameter(WASM_RUNNER_MAX_NUM_PARAMETERS), - graph()->start()), - graph()->NewNode(common()->Int32Constant(0)), call, effect, - graph()->start()); + if (!return_type.IsNone()) { + effect = graph()->NewNode( + machine()->Store(StoreRepresentation( + return_type.representation(), WriteBarrierKind::kNoWriteBarrier)), + graph()->NewNode(common()->Parameter(param_types.length()), + graph()->start()), + graph()->NewNode(common()->Int32Constant(0)), call, effect, + graph()->start()); + } Node* zero = graph()->NewNode(common()->Int32Constant(0)); Node* r = graph()->NewNode( common()->Return(), zero, @@ -402,6 +432,15 @@ class WasmFunctionWrapper : public HandleAndZoneScope, graph()->SetEnd(graph()->NewNode(common()->End(2), r, graph()->start())); } + template <typename ReturnType, typename... ParamTypes> + void Init(CallDescriptor* descriptor) { + std::array<MachineType, sizeof...(ParamTypes)> param_machine_types{ + {MachineTypeForC<ParamTypes>()...}}; + Vector<MachineType> param_vec(param_machine_types.data(), + param_machine_types.size()); + Init(descriptor, MachineTypeForC<ReturnType>(), param_vec); + } + void SetInnerCode(Handle<Code> code_handle) { NodeProperties::ChangeOp(inner_code_node_, common()->HeapConstant(code_handle)); @@ -415,12 +454,13 @@ class WasmFunctionWrapper : public HandleAndZoneScope, Linkage::GetSimplifiedCDescriptor(zone(), signature_, true); if (kPointerSize == 4) { + size_t num_params = signature_->parameter_count(); // One additional parameter for the pointer of the return value. - Signature<MachineRepresentation>::Builder rep_builder( - zone(), 1, WASM_RUNNER_MAX_NUM_PARAMETERS + 1); + Signature<MachineRepresentation>::Builder rep_builder(zone(), 1, + num_params + 1); rep_builder.AddReturn(MachineRepresentation::kWord32); - for (int i = 0; i < WASM_RUNNER_MAX_NUM_PARAMETERS + 1; i++) { + for (size_t i = 0; i < num_params + 1; i++) { rep_builder.AddParam(MachineRepresentation::kWord32); } Int64Lowering r(graph(), machine(), common(), zone(), @@ -452,129 +492,108 @@ class WasmFunctionWrapper : public HandleAndZoneScope, Signature<MachineType>* signature_; }; -// A helper for compiling WASM functions for testing. This class can create a -// standalone function if {module} is NULL or a function within a -// {TestingModule}. It contains the internal state for compilation (i.e. -// TurboFan graph) and interpretation (by adding to the interpreter manually). -class WasmFunctionCompiler : public HandleAndZoneScope, - private GraphAndBuilders { +// A helper for compiling WASM functions for testing. +// It contains the internal state for compilation (i.e. TurboFan graph) and +// interpretation (by adding to the interpreter manually). +class WasmFunctionCompiler : private GraphAndBuilders { public: - explicit WasmFunctionCompiler( - FunctionSig* sig, WasmExecutionMode mode, - Vector<const char> debug_name = ArrayVector("<WASM UNNAMED>")) - : GraphAndBuilders(main_zone()), - execution_mode_(mode), - jsgraph(this->isolate(), this->graph(), this->common(), nullptr, - nullptr, this->machine()), - sig(sig), - descriptor_(nullptr), - testing_module_(nullptr), - debug_name_(debug_name), - local_decls(main_zone(), sig), - source_position_table_(this->graph()), - interpreter_(nullptr) { - // Create our own function. - function_ = new WasmFunction(); - function_->sig = sig; - function_->func_index = 0; - function_->sig_index = 0; - if (mode == kExecuteInterpreted) { - interpreter_ = new WasmInterpreter(nullptr, zone()->allocator()); - int index = interpreter_->AddFunctionForTesting(function_); - CHECK_EQ(0, index); - } - } - - explicit WasmFunctionCompiler( - FunctionSig* sig, TestingModule* module, - Vector<const char> debug_name = ArrayVector("<WASM UNNAMED>")) - : GraphAndBuilders(main_zone()), - execution_mode_(module->execution_mode()), - jsgraph(this->isolate(), this->graph(), this->common(), nullptr, - nullptr, this->machine()), - sig(sig), - descriptor_(nullptr), - testing_module_(module), - debug_name_(debug_name), - local_decls(main_zone(), sig), - source_position_table_(this->graph()), - interpreter_(module->interpreter()) { - // Get a new function from the testing module. - int index = module->AddFunction(sig, Handle<Code>::null()); - function_ = testing_module_->GetFunctionAt(index); - } - - ~WasmFunctionCompiler() { - if (testing_module_) return; // testing module owns the below things. - delete function_; - if (interpreter_) delete interpreter_; - } - - WasmExecutionMode execution_mode_; - JSGraph jsgraph; - FunctionSig* sig; - // The call descriptor is initialized when the function is compiled. - CallDescriptor* descriptor_; - TestingModule* testing_module_; - Vector<const char> debug_name_; - WasmFunction* function_; - LocalDeclEncoder local_decls; - SourcePositionTable source_position_table_; - WasmInterpreter* interpreter_; - - Isolate* isolate() { return main_isolate(); } + Isolate* isolate() { return testing_module_->isolate(); } Graph* graph() const { return main_graph_; } Zone* zone() const { return graph()->zone(); } CommonOperatorBuilder* common() { return &main_common_; } MachineOperatorBuilder* machine() { return &main_machine_; } - void InitializeDescriptor() { + CallDescriptor* descriptor() { if (descriptor_ == nullptr) { - descriptor_ = testing_module_->GetWasmCallDescriptor(main_zone(), sig); + descriptor_ = testing_module_->GetWasmCallDescriptor(zone(), sig); } + return descriptor_; } - CallDescriptor* descriptor() { return descriptor_; } uint32_t function_index() { return function_->func_index; } void Build(const byte* start, const byte* end) { - // Build the TurboFan graph. - local_decls.Prepend(main_zone(), &start, &end); - TestBuildingGraph(main_zone(), &jsgraph, testing_module_, sig, - &source_position_table_, start, end); + size_t locals_size = local_decls.Size(); + size_t total_size = end - start + locals_size + 1; + byte* buffer = static_cast<byte*>(zone()->New(total_size)); + // Prepend the local decls to the code. + local_decls.Emit(buffer); + // Emit the code. + memcpy(buffer + locals_size, start, end - start); + // Append an extra end opcode. + buffer[total_size - 1] = kExprEnd; + + start = buffer; + end = buffer + total_size; + + CHECK_GE(kMaxInt, end - start); + int len = static_cast<int>(end - start); + function_->code_start_offset = + testing_module_->AddBytes(Vector<const byte>(start, len)); + function_->code_end_offset = function_->code_start_offset + len; + if (interpreter_) { // Add the code to the interpreter. CHECK(interpreter_->SetFunctionCodeForTesting(function_, start, end)); + return; } + + // Build the TurboFan graph. + TestBuildingGraph(zone(), &jsgraph, testing_module_, sig, + &source_position_table_, start, end); + Handle<Code> code = Compile(); + testing_module_->SetFunctionCode(function_index(), code); } - byte AllocateLocal(LocalType type) { + byte AllocateLocal(ValueType type) { uint32_t index = local_decls.AddLocals(1, type); byte result = static_cast<byte>(index); DCHECK_EQ(index, result); return result; } + void SetSigIndex(int sig_index) { function_->sig_index = sig_index; } + + private: + friend class WasmRunnerBase; + + explicit WasmFunctionCompiler(Zone* zone, FunctionSig* sig, + TestingModule* module, const char* name) + : GraphAndBuilders(zone), + jsgraph(module->isolate(), this->graph(), this->common(), nullptr, + nullptr, this->machine()), + sig(sig), + descriptor_(nullptr), + testing_module_(module), + local_decls(zone, sig), + source_position_table_(this->graph()), + interpreter_(module->interpreter()) { + // Get a new function from the testing module. + int index = module->AddFunction(sig, Handle<Code>::null(), name); + function_ = testing_module_->GetFunctionAt(index); + } + Handle<Code> Compile() { - InitializeDescriptor(); - CallDescriptor* desc = descriptor_; + CallDescriptor* desc = descriptor(); if (kPointerSize == 4) { desc = testing_module_->GetI32WasmCallDescriptor(this->zone(), desc); } - CompilationInfo info(debug_name_, this->isolate(), this->zone(), + CompilationInfo info(CStrVector("wasm"), this->isolate(), this->zone(), Code::ComputeFlags(Code::WASM_FUNCTION)); std::unique_ptr<CompilationJob> job(Pipeline::NewWasmCompilationJob( - &info, graph(), desc, &source_position_table_)); + &info, &jsgraph, desc, &source_position_table_, nullptr, false)); if (job->ExecuteJob() != CompilationJob::SUCCEEDED || job->FinalizeJob() != CompilationJob::SUCCEEDED) return Handle<Code>::null(); Handle<Code> code = info.code(); - // Length is always 2, since usually <wasm_obj, func_index> is stored in - // the deopt data. Here, we only store the function index. + // Deopt data holds <WeakCell<wasm_instance>, func_index>. DCHECK(code->deoptimization_data() == nullptr || code->deoptimization_data()->length() == 0); Handle<FixedArray> deopt_data = isolate()->factory()->NewFixedArray(2, TENURED); + Handle<Object> weak_instance = + isolate()->factory()->NewWeakCell(testing_module_->instance_object()); + deopt_data->set(0, *weak_instance); deopt_data->set(1, Smi::FromInt(static_cast<int>(function_index()))); deopt_data->set_length(2); code->set_deoptimization_data(*deopt_data); @@ -589,76 +608,26 @@ class WasmFunctionCompiler : public HandleAndZoneScope, return code; } - uint32_t CompileAndAdd(uint16_t sig_index = 0) { - CHECK(testing_module_); - function_->sig_index = sig_index; - Handle<Code> code = Compile(); - testing_module_->SetFunctionCode(function_index(), code); - return function_index(); - } - - // Set the context, such that e.g. runtime functions can be called. - void SetModuleContext() { - if (!testing_module_->instance->context.is_null()) { - CHECK(testing_module_->instance->context.is_identical_to( - main_isolate()->native_context())); - return; - } - testing_module_->instance->context = main_isolate()->native_context(); - } + JSGraph jsgraph; + FunctionSig* sig; + // The call descriptor is initialized when the function is compiled. + CallDescriptor* descriptor_; + TestingModule* testing_module_; + Vector<const char> debug_name_; + WasmFunction* function_; + LocalDeclEncoder local_decls; + SourcePositionTable source_position_table_; + WasmInterpreter* interpreter_; }; -// A helper class to build graphs from Wasm bytecode, generate machine +// A helper class to build a module around Wasm bytecode, generate machine // code, and run that code. -template <typename ReturnType> -class WasmRunner { +class WasmRunnerBase : public HandleAndZoneScope { public: - WasmRunner(WasmExecutionMode execution_mode, - MachineType p0 = MachineType::None(), - MachineType p1 = MachineType::None(), - MachineType p2 = MachineType::None(), - MachineType p3 = MachineType::None()) - : zone(&allocator_, ZONE_NAME), - compiled_(false), - signature_(MachineTypeForC<ReturnType>() == MachineType::None() ? 0 : 1, - GetParameterCount(p0, p1, p2, p3), storage_), - compiler_(&signature_, execution_mode) { - InitSigStorage(p0, p1, p2, p3); - } - - WasmRunner(TestingModule* module, MachineType p0 = MachineType::None(), - MachineType p1 = MachineType::None(), - MachineType p2 = MachineType::None(), - MachineType p3 = MachineType::None()) - : zone(&allocator_, ZONE_NAME), - compiled_(false), - signature_(MachineTypeForC<ReturnType>() == MachineType::None() ? 0 : 1, - GetParameterCount(p0, p1, p2, p3), storage_), - compiler_(&signature_, module), - possible_nondeterminism_(false) { - DCHECK(module); - InitSigStorage(p0, p1, p2, p3); - } - - void InitSigStorage(MachineType p0, MachineType p1, MachineType p2, - MachineType p3) { - int index = 0; - MachineType ret = MachineTypeForC<ReturnType>(); - if (ret != MachineType::None()) { - storage_[index++] = WasmOpcodes::LocalTypeFor(ret); - } - if (p0 != MachineType::None()) - storage_[index++] = WasmOpcodes::LocalTypeFor(p0); - if (p1 != MachineType::None()) - storage_[index++] = WasmOpcodes::LocalTypeFor(p1); - if (p2 != MachineType::None()) - storage_[index++] = WasmOpcodes::LocalTypeFor(p2); - if (p3 != MachineType::None()) - storage_[index++] = WasmOpcodes::LocalTypeFor(p3); - - compiler_.InitializeDescriptor(); - wrapper_.Init(compiler_.descriptor(), p0, p1, p2, p3); - } + explicit WasmRunnerBase(WasmExecutionMode execution_mode, int num_params) + : zone_(&allocator_, ZONE_NAME), + module_(&zone_, execution_mode), + wrapper_(&zone_, num_params) {} // Builds a graph from the given Wasm code and generates the machine // code and call wrapper for that graph. This method must not be called @@ -666,83 +635,125 @@ class WasmRunner { void Build(const byte* start, const byte* end) { CHECK(!compiled_); compiled_ = true; - compiler_.Build(start, end); + functions_[0]->Build(start, end); + } - if (!interpret()) { - // Compile machine code and install it into the module. - Handle<Code> code = compiler_.Compile(); + // Resets the state for building the next function. + // The main function called will always be the first function. + template <typename ReturnType, typename... ParamTypes> + WasmFunctionCompiler& NewFunction(const char* name = nullptr) { + return NewFunction(CreateSig<ReturnType, ParamTypes...>(), name); + } - if (compiler_.testing_module_) { - // Update the table of function code in the module. - compiler_.testing_module_->SetFunctionCode( - compiler_.function_->func_index, code); - } + // Resets the state for building the next function. + // The main function called will be the last generated function. + // Returns the index of the previously built function. + WasmFunctionCompiler& NewFunction(FunctionSig* sig, + const char* name = nullptr) { + functions_.emplace_back( + new WasmFunctionCompiler(&zone_, sig, &module_, name)); + return *functions_.back(); + } - wrapper_.SetInnerCode(code); - } + byte AllocateLocal(ValueType type) { + return functions_[0]->AllocateLocal(type); } - ReturnType Call() { - if (interpret()) { - return CallInterpreter(Vector<WasmVal>(nullptr, 0)); - } else { - return Call(0, 0, 0, 0); + WasmFunction* function() { return functions_[0]->function_; } + WasmInterpreter* interpreter() { return functions_[0]->interpreter_; } + bool possible_nondeterminism() { return possible_nondeterminism_; } + TestingModule& module() { return module_; } + Zone* zone() { return &zone_; } + + // Set the context, such that e.g. runtime functions can be called. + void SetModuleContext() { + if (!module_.instance->context.is_null()) { + CHECK(module_.instance->context.is_identical_to( + main_isolate()->native_context())); + return; } + module_.instance->context = main_isolate()->native_context(); } - template <typename P0> - ReturnType Call(P0 p0) { - if (interpret()) { - WasmVal args[] = {WasmVal(p0)}; - return CallInterpreter(ArrayVector(args)); - } else { - return Call(p0, 0, 0, 0); + private: + FunctionSig* CreateSig(MachineType return_type, + Vector<MachineType> param_types) { + int return_count = return_type.IsNone() ? 0 : 1; + int param_count = param_types.length(); + + // Allocate storage array in zone. + ValueType* sig_types = + zone_.NewArray<ValueType>(return_count + param_count); + + // Convert machine types to local types, and check that there are no + // MachineType::None()'s in the parameters. + int idx = 0; + if (return_count) sig_types[idx++] = WasmOpcodes::ValueTypeFor(return_type); + for (MachineType param : param_types) { + CHECK_NE(MachineType::None(), param); + sig_types[idx++] = WasmOpcodes::ValueTypeFor(param); } + return new (&zone_) FunctionSig(return_count, param_count, sig_types); } - template <typename P0, typename P1> - ReturnType Call(P0 p0, P1 p1) { - if (interpret()) { - WasmVal args[] = {WasmVal(p0), WasmVal(p1)}; - return CallInterpreter(ArrayVector(args)); - } else { - return Call(p0, p1, 0, 0); - } + template <typename ReturnType, typename... ParamTypes> + FunctionSig* CreateSig() { + std::array<MachineType, sizeof...(ParamTypes)> param_machine_types{ + {MachineTypeForC<ParamTypes>()...}}; + Vector<MachineType> param_vec(param_machine_types.data(), + param_machine_types.size()); + return CreateSig(MachineTypeForC<ReturnType>(), param_vec); } - template <typename P0, typename P1, typename P2> - ReturnType Call(P0 p0, P1 p1, P2 p2) { - if (interpret()) { - WasmVal args[] = {WasmVal(p0), WasmVal(p1), WasmVal(p2)}; - return CallInterpreter(ArrayVector(args)); - } else { - return Call(p0, p1, p2, 0); + protected: + v8::internal::AccountingAllocator allocator_; + Zone zone_; + TestingModule module_; + std::vector<std::unique_ptr<WasmFunctionCompiler>> functions_; + WasmFunctionWrapper wrapper_; + bool compiled_ = false; + bool possible_nondeterminism_ = false; + + bool interpret() { return module_.execution_mode() == kExecuteInterpreted; } + + public: + // This field has to be static. Otherwise, gcc complains about the using in + // the lambda context below. + static jmp_buf jump_buffer; +}; + +template <typename ReturnType, typename... ParamTypes> +class WasmRunner : public WasmRunnerBase { + public: + explicit WasmRunner(WasmExecutionMode execution_mode, + const char* main_fn_name = "main") + : WasmRunnerBase(execution_mode, sizeof...(ParamTypes)) { + NewFunction<ReturnType, ParamTypes...>(main_fn_name); + if (!interpret()) { + wrapper_.Init<ReturnType, ParamTypes...>(functions_[0]->descriptor()); } } - template <typename P0, typename P1, typename P2, typename P3> - ReturnType Call(P0 p0, P1 p1, P2 p2, P3 p3) { - if (interpret()) { - WasmVal args[] = {WasmVal(p0), WasmVal(p1), WasmVal(p2), WasmVal(p3)}; - return CallInterpreter(ArrayVector(args)); - } else { - CodeRunner<int32_t> runner(CcTest::InitIsolateOnce(), - wrapper_.GetWrapperCode(), - wrapper_.signature()); - ReturnType return_value; - int32_t result = runner.Call<void*, void*, void*, void*, void*>( - &p0, &p1, &p2, &p3, &return_value); - CHECK_EQ(WASM_WRAPPER_RETURN_VALUE, result); - return return_value; + ReturnType Call(ParamTypes... p) { + DCHECK(compiled_); + if (interpret()) return CallInterpreter(p...); + + // Use setjmp/longjmp to deal with traps in WebAssembly code. + ReturnType return_value = static_cast<ReturnType>(0xdeadbeefdeadbeef); + static int setjmp_ret; + setjmp_ret = setjmp(WasmRunnerBase::jump_buffer); + // setjmp returns 0 on the first return, 1 (passed to longjmp) after trap. + if (setjmp_ret == 0) { + DoCall(static_cast<void*>(&p)..., static_cast<void*>(&return_value)); } + return return_value; } - ReturnType CallInterpreter(Vector<WasmVal> args) { - CHECK_EQ(args.length(), - static_cast<int>(compiler_.function_->sig->parameter_count())); + ReturnType CallInterpreter(ParamTypes... p) { WasmInterpreter::Thread* thread = interpreter()->GetThread(0); thread->Reset(); - thread->PushFrame(compiler_.function_, args.start()); + std::array<WasmVal, sizeof...(p)> args{{WasmVal(p)...}}; + thread->PushFrame(function(), args.data()); if (thread->Run() == WasmInterpreter::FINISHED) { WasmVal val = thread->GetReturnValue(); possible_nondeterminism_ |= thread->PossibleNondeterminism(); @@ -753,49 +764,59 @@ class WasmRunner { return static_cast<ReturnType>(result); } else { // TODO(titzer): falling off end - ReturnType val = 0; - return val; + return ReturnType{0}; } } - byte AllocateLocal(LocalType type) { return compiler_.AllocateLocal(type); } - - WasmFunction* function() { return compiler_.function_; } - WasmInterpreter* interpreter() { return compiler_.interpreter_; } - bool possible_nondeterminism() { return possible_nondeterminism_; } - - protected: - v8::internal::AccountingAllocator allocator_; - Zone zone; - bool compiled_; - LocalType storage_[WASM_RUNNER_MAX_NUM_PARAMETERS]; - FunctionSig signature_; - WasmFunctionCompiler compiler_; - WasmFunctionWrapper<ReturnType> wrapper_; - bool possible_nondeterminism_; - - bool interpret() { return compiler_.execution_mode_ == kExecuteInterpreted; } - - static size_t GetParameterCount(MachineType p0, MachineType p1, - MachineType p2, MachineType p3) { - if (p0 == MachineType::None()) return 0; - if (p1 == MachineType::None()) return 1; - if (p2 == MachineType::None()) return 2; - if (p3 == MachineType::None()) return 3; - return 4; + private: + // Don't inline this function. The setjmp above should be followed immediately + // by a call. + template <typename... Ptrs> + V8_NOINLINE void DoCall(Ptrs... ptrs) { + auto trap_callback = []() -> void { + set_trap_callback_for_testing(nullptr); + longjmp(WasmRunnerBase::jump_buffer, 1); + }; + set_trap_callback_for_testing(trap_callback); + + wrapper_.SetInnerCode( + module_.GetFunctionCode(functions_[0]->function_index())); + CodeRunner<int32_t> runner(CcTest::InitIsolateOnce(), + wrapper_.GetWrapperCode(), wrapper_.signature()); + int32_t result = runner.Call(ptrs...); + // If we arrive here, no trap happened. + CHECK_EQ(WASM_WRAPPER_RETURN_VALUE, result); } }; +// Declare static variable. +jmp_buf WasmRunnerBase::jump_buffer; + // A macro to define tests that run in different engine configurations. -// Currently only supports compiled tests, but a future -// RunWasmInterpreted_##name version will allow each test to also run in the -// interpreter. #define WASM_EXEC_TEST(name) \ void RunWasm_##name(WasmExecutionMode execution_mode); \ TEST(RunWasmCompiled_##name) { RunWasm_##name(kExecuteCompiled); } \ TEST(RunWasmInterpreted_##name) { RunWasm_##name(kExecuteInterpreted); } \ void RunWasm_##name(WasmExecutionMode execution_mode) +#define WASM_EXEC_TEST_WITH_TRAP(name) \ + void RunWasm_##name(WasmExecutionMode execution_mode); \ + TEST(RunWasmCompiled_##name) { RunWasm_##name(kExecuteCompiled); } \ + void RunWasm_##name(WasmExecutionMode execution_mode); \ + TEST(RunWasmCompiledWithTrapIf_##name) { \ + bool trap_if = FLAG_wasm_trap_if; \ + FLAG_wasm_trap_if = true; \ + RunWasm_##name(kExecuteCompiled); \ + FLAG_wasm_trap_if = trap_if; \ + } \ + TEST(RunWasmInterpreted_##name) { RunWasm_##name(kExecuteInterpreted); } \ + void RunWasm_##name(WasmExecutionMode execution_mode) + +#define WASM_EXEC_COMPILED_TEST(name) \ + void RunWasm_##name(WasmExecutionMode execution_mode); \ + TEST(RunWasmCompiled_##name) { RunWasm_##name(kExecuteCompiled); } \ + void RunWasm_##name(WasmExecutionMode execution_mode) + } // namespace #endif |