OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "test/unittests/test-utils.h" | 5 #include "test/unittests/test-utils.h" |
6 | 6 |
7 #include "src/v8.h" | 7 #include "src/v8.h" |
8 | 8 |
9 #include "test/common/wasm/test-signatures.h" | 9 #include "test/common/wasm/test-signatures.h" |
10 | 10 |
(...skipping 2528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2539 FOREACH_SIMD_0_OPERAND_OPCODE(TEST_SIMD) | 2539 FOREACH_SIMD_0_OPERAND_OPCODE(TEST_SIMD) |
2540 #undef TEST_SIMD | 2540 #undef TEST_SIMD |
2541 #define TEST_SIMD(name, opcode, sig) \ | 2541 #define TEST_SIMD(name, opcode, sig) \ |
2542 EXPECT_LENGTH_N(3, kSimdPrefix, static_cast<byte>(kExpr##name & 0xff)); | 2542 EXPECT_LENGTH_N(3, kSimdPrefix, static_cast<byte>(kExpr##name & 0xff)); |
2543 FOREACH_SIMD_1_OPERAND_OPCODE(TEST_SIMD) | 2543 FOREACH_SIMD_1_OPERAND_OPCODE(TEST_SIMD) |
2544 #undef TEST_SIMD | 2544 #undef TEST_SIMD |
2545 // test for bad simd opcode | 2545 // test for bad simd opcode |
2546 EXPECT_LENGTH_N(2, kSimdPrefix, 0xff); | 2546 EXPECT_LENGTH_N(2, kSimdPrefix, 0xff); |
2547 } | 2547 } |
2548 | 2548 |
2549 typedef ZoneVector<ValueType> ValueTypeMap; | 2549 typedef ZoneVector<ValueType> TypesOfLocals; |
2550 | 2550 |
2551 class LocalDeclDecoderTest : public TestWithZone { | 2551 class LocalDeclDecoderTest : public TestWithZone { |
2552 public: | 2552 public: |
2553 v8::internal::AccountingAllocator allocator; | 2553 v8::internal::AccountingAllocator allocator; |
2554 | 2554 |
2555 size_t ExpectRun(ValueTypeMap map, size_t pos, ValueType expected, | 2555 size_t ExpectRun(TypesOfLocals map, size_t pos, ValueType expected, |
2556 size_t count) { | 2556 size_t count) { |
2557 for (size_t i = 0; i < count; i++) { | 2557 for (size_t i = 0; i < count; i++) { |
2558 EXPECT_EQ(expected, map[pos++]); | 2558 EXPECT_EQ(expected, map[pos++]); |
2559 } | 2559 } |
2560 return pos; | 2560 return pos; |
2561 } | 2561 } |
2562 | |
2563 ValueTypeMap Expand(BodyLocalDecls& decls) { | |
2564 ZoneVector<ValueType> map(zone()); | |
2565 for (auto p : decls.local_types) { | |
2566 map.insert(map.end(), p.second, p.first); | |
2567 } | |
2568 return map; | |
2569 } | |
2570 }; | 2562 }; |
2571 | 2563 |
2572 TEST_F(LocalDeclDecoderTest, EmptyLocals) { | 2564 TEST_F(LocalDeclDecoderTest, EmptyLocals) { |
2573 BodyLocalDecls decls(zone()); | 2565 BodyLocalDecls decls(zone()); |
2574 bool result = DecodeLocalDecls(decls, nullptr, nullptr); | 2566 bool result = DecodeLocalDecls(&decls, nullptr, nullptr); |
2575 EXPECT_FALSE(result); | 2567 EXPECT_FALSE(result); |
2576 } | 2568 } |
2577 | 2569 |
2578 TEST_F(LocalDeclDecoderTest, NoLocals) { | 2570 TEST_F(LocalDeclDecoderTest, NoLocals) { |
2579 static const byte data[] = {0}; | 2571 static const byte data[] = {0}; |
2580 BodyLocalDecls decls(zone()); | 2572 BodyLocalDecls decls(zone()); |
2581 bool result = DecodeLocalDecls(decls, data, data + sizeof(data)); | 2573 bool result = DecodeLocalDecls(&decls, data, data + sizeof(data)); |
2582 EXPECT_TRUE(result); | 2574 EXPECT_TRUE(result); |
2583 EXPECT_EQ(0u, decls.total_local_count); | 2575 EXPECT_TRUE(decls.type_list.empty()); |
2584 } | 2576 } |
2585 | 2577 |
2586 TEST_F(LocalDeclDecoderTest, OneLocal) { | 2578 TEST_F(LocalDeclDecoderTest, OneLocal) { |
2587 for (size_t i = 0; i < arraysize(kValueTypes); i++) { | 2579 for (size_t i = 0; i < arraysize(kValueTypes); i++) { |
2588 ValueType type = kValueTypes[i]; | 2580 ValueType type = kValueTypes[i]; |
2589 const byte data[] = { | 2581 const byte data[] = { |
2590 1, 1, static_cast<byte>(WasmOpcodes::ValueTypeCodeFor(type))}; | 2582 1, 1, static_cast<byte>(WasmOpcodes::ValueTypeCodeFor(type))}; |
2591 BodyLocalDecls decls(zone()); | 2583 BodyLocalDecls decls(zone()); |
2592 bool result = DecodeLocalDecls(decls, data, data + sizeof(data)); | 2584 bool result = DecodeLocalDecls(&decls, data, data + sizeof(data)); |
2593 EXPECT_TRUE(result); | 2585 EXPECT_TRUE(result); |
2594 EXPECT_EQ(1u, decls.total_local_count); | 2586 EXPECT_EQ(1u, decls.type_list.size()); |
2595 | 2587 |
2596 ValueTypeMap map = Expand(decls); | 2588 TypesOfLocals map = decls.type_list; |
2597 EXPECT_EQ(1u, map.size()); | |
2598 EXPECT_EQ(type, map[0]); | 2589 EXPECT_EQ(type, map[0]); |
2599 } | 2590 } |
2600 } | 2591 } |
2601 | 2592 |
2602 TEST_F(LocalDeclDecoderTest, FiveLocals) { | 2593 TEST_F(LocalDeclDecoderTest, FiveLocals) { |
2603 for (size_t i = 0; i < arraysize(kValueTypes); i++) { | 2594 for (size_t i = 0; i < arraysize(kValueTypes); i++) { |
2604 ValueType type = kValueTypes[i]; | 2595 ValueType type = kValueTypes[i]; |
2605 const byte data[] = { | 2596 const byte data[] = { |
2606 1, 5, static_cast<byte>(WasmOpcodes::ValueTypeCodeFor(type))}; | 2597 1, 5, static_cast<byte>(WasmOpcodes::ValueTypeCodeFor(type))}; |
2607 BodyLocalDecls decls(zone()); | 2598 BodyLocalDecls decls(zone()); |
2608 bool result = DecodeLocalDecls(decls, data, data + sizeof(data)); | 2599 bool result = DecodeLocalDecls(&decls, data, data + sizeof(data)); |
2609 EXPECT_TRUE(result); | 2600 EXPECT_TRUE(result); |
2610 EXPECT_EQ(sizeof(data), decls.decls_encoded_size); | 2601 EXPECT_EQ(sizeof(data), decls.encoded_size); |
2611 EXPECT_EQ(5u, decls.total_local_count); | 2602 EXPECT_EQ(5u, decls.type_list.size()); |
2612 | 2603 |
2613 ValueTypeMap map = Expand(decls); | 2604 TypesOfLocals map = decls.type_list; |
2614 EXPECT_EQ(5u, map.size()); | 2605 EXPECT_EQ(5u, map.size()); |
2615 ExpectRun(map, 0, type, 5); | 2606 ExpectRun(map, 0, type, 5); |
2616 } | 2607 } |
2617 } | 2608 } |
2618 | 2609 |
2619 TEST_F(LocalDeclDecoderTest, MixedLocals) { | 2610 TEST_F(LocalDeclDecoderTest, MixedLocals) { |
2620 for (byte a = 0; a < 3; a++) { | 2611 for (byte a = 0; a < 3; a++) { |
2621 for (byte b = 0; b < 3; b++) { | 2612 for (byte b = 0; b < 3; b++) { |
2622 for (byte c = 0; c < 3; c++) { | 2613 for (byte c = 0; c < 3; c++) { |
2623 for (byte d = 0; d < 3; d++) { | 2614 for (byte d = 0; d < 3; d++) { |
2624 const byte data[] = {4, a, kLocalI32, b, kLocalI64, | 2615 const byte data[] = {4, a, kLocalI32, b, kLocalI64, |
2625 c, kLocalF32, d, kLocalF64}; | 2616 c, kLocalF32, d, kLocalF64}; |
2626 BodyLocalDecls decls(zone()); | 2617 BodyLocalDecls decls(zone()); |
2627 bool result = DecodeLocalDecls(decls, data, data + sizeof(data)); | 2618 bool result = DecodeLocalDecls(&decls, data, data + sizeof(data)); |
2628 EXPECT_TRUE(result); | 2619 EXPECT_TRUE(result); |
2629 EXPECT_EQ(sizeof(data), decls.decls_encoded_size); | 2620 EXPECT_EQ(sizeof(data), decls.encoded_size); |
2630 EXPECT_EQ(static_cast<uint32_t>(a + b + c + d), | 2621 EXPECT_EQ(static_cast<uint32_t>(a + b + c + d), |
2631 decls.total_local_count); | 2622 decls.type_list.size()); |
2632 | 2623 |
2633 ValueTypeMap map = Expand(decls); | 2624 TypesOfLocals map = decls.type_list; |
2634 EXPECT_EQ(static_cast<uint32_t>(a + b + c + d), map.size()); | |
2635 | 2625 |
2636 size_t pos = 0; | 2626 size_t pos = 0; |
2637 pos = ExpectRun(map, pos, kWasmI32, a); | 2627 pos = ExpectRun(map, pos, kWasmI32, a); |
2638 pos = ExpectRun(map, pos, kWasmI64, b); | 2628 pos = ExpectRun(map, pos, kWasmI64, b); |
2639 pos = ExpectRun(map, pos, kWasmF32, c); | 2629 pos = ExpectRun(map, pos, kWasmF32, c); |
2640 pos = ExpectRun(map, pos, kWasmF64, d); | 2630 pos = ExpectRun(map, pos, kWasmF64, d); |
2641 } | 2631 } |
2642 } | 2632 } |
2643 } | 2633 } |
2644 } | 2634 } |
2645 } | 2635 } |
2646 | 2636 |
2647 TEST_F(LocalDeclDecoderTest, UseEncoder) { | 2637 TEST_F(LocalDeclDecoderTest, UseEncoder) { |
2648 const byte* data = nullptr; | 2638 const byte* data = nullptr; |
2649 const byte* end = nullptr; | 2639 const byte* end = nullptr; |
2650 LocalDeclEncoder local_decls(zone()); | 2640 LocalDeclEncoder local_decls(zone()); |
2651 | 2641 |
2652 local_decls.AddLocals(5, kWasmF32); | 2642 local_decls.AddLocals(5, kWasmF32); |
2653 local_decls.AddLocals(1337, kWasmI32); | 2643 local_decls.AddLocals(1337, kWasmI32); |
2654 local_decls.AddLocals(212, kWasmI64); | 2644 local_decls.AddLocals(212, kWasmI64); |
2655 local_decls.Prepend(zone(), &data, &end); | 2645 local_decls.Prepend(zone(), &data, &end); |
2656 | 2646 |
2657 BodyLocalDecls decls(zone()); | 2647 BodyLocalDecls decls(zone()); |
2658 bool result = DecodeLocalDecls(decls, data, end); | 2648 bool result = DecodeLocalDecls(&decls, data, end); |
2659 EXPECT_TRUE(result); | 2649 EXPECT_TRUE(result); |
2660 EXPECT_EQ(5u + 1337u + 212u, decls.total_local_count); | 2650 EXPECT_EQ(5u + 1337u + 212u, decls.type_list.size()); |
2661 | 2651 |
2662 ValueTypeMap map = Expand(decls); | 2652 TypesOfLocals map = decls.type_list; |
2663 size_t pos = 0; | 2653 size_t pos = 0; |
2664 pos = ExpectRun(map, pos, kWasmF32, 5); | 2654 pos = ExpectRun(map, pos, kWasmF32, 5); |
2665 pos = ExpectRun(map, pos, kWasmI32, 1337); | 2655 pos = ExpectRun(map, pos, kWasmI32, 1337); |
2666 pos = ExpectRun(map, pos, kWasmI64, 212); | 2656 pos = ExpectRun(map, pos, kWasmI64, 212); |
2667 } | 2657 } |
2668 | 2658 |
2669 class BytecodeIteratorTest : public TestWithZone {}; | 2659 class BytecodeIteratorTest : public TestWithZone {}; |
2670 | 2660 |
2671 TEST_F(BytecodeIteratorTest, SimpleForeach) { | 2661 TEST_F(BytecodeIteratorTest, SimpleForeach) { |
2672 byte code[] = {WASM_IF_ELSE(WASM_ZERO, WASM_ZERO, WASM_ZERO)}; | 2662 byte code[] = {WASM_IF_ELSE(WASM_ZERO, WASM_ZERO, WASM_ZERO)}; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2722 count++; | 2712 count++; |
2723 } | 2713 } |
2724 EXPECT_EQ(6, count); | 2714 EXPECT_EQ(6, count); |
2725 } | 2715 } |
2726 | 2716 |
2727 TEST_F(BytecodeIteratorTest, WithLocalDecls) { | 2717 TEST_F(BytecodeIteratorTest, WithLocalDecls) { |
2728 byte code[] = {1, 1, kLocalI32, WASM_I8(9), WASM_I8(11)}; | 2718 byte code[] = {1, 1, kLocalI32, WASM_I8(9), WASM_I8(11)}; |
2729 BodyLocalDecls decls(zone()); | 2719 BodyLocalDecls decls(zone()); |
2730 BytecodeIterator iter(code, code + sizeof(code), &decls); | 2720 BytecodeIterator iter(code, code + sizeof(code), &decls); |
2731 | 2721 |
2732 EXPECT_EQ(3u, decls.decls_encoded_size); | 2722 EXPECT_EQ(3u, decls.encoded_size); |
2733 EXPECT_EQ(3u, iter.pc_offset()); | 2723 EXPECT_EQ(3u, iter.pc_offset()); |
2734 EXPECT_TRUE(iter.has_next()); | 2724 EXPECT_TRUE(iter.has_next()); |
2735 EXPECT_EQ(kExprI8Const, iter.current()); | 2725 EXPECT_EQ(kExprI8Const, iter.current()); |
2736 iter.next(); | 2726 iter.next(); |
2737 EXPECT_TRUE(iter.has_next()); | 2727 EXPECT_TRUE(iter.has_next()); |
2738 EXPECT_EQ(kExprI8Const, iter.current()); | 2728 EXPECT_EQ(kExprI8Const, iter.current()); |
2739 iter.next(); | 2729 iter.next(); |
2740 EXPECT_FALSE(iter.has_next()); | 2730 EXPECT_FALSE(iter.has_next()); |
2741 } | 2731 } |
2742 | 2732 |
2743 } // namespace wasm | 2733 } // namespace wasm |
2744 } // namespace internal | 2734 } // namespace internal |
2745 } // namespace v8 | 2735 } // namespace v8 |
OLD | NEW |