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 "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/compiler.h" | 7 #include "src/compiler.h" |
8 #include "src/interpreter/bytecode-generator.h" | 8 #include "src/interpreter/bytecode-generator.h" |
9 #include "src/interpreter/interpreter.h" | 9 #include "src/interpreter/interpreter.h" |
10 #include "test/cctest/cctest.h" | 10 #include "test/cctest/cctest.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 }; | 57 }; |
58 | 58 |
59 | 59 |
60 // Structure for containing expected bytecode snippets. | 60 // Structure for containing expected bytecode snippets. |
61 template<typename T> | 61 template<typename T> |
62 struct ExpectedSnippet { | 62 struct ExpectedSnippet { |
63 const char* code_snippet; | 63 const char* code_snippet; |
64 int frame_size; | 64 int frame_size; |
65 int parameter_count; | 65 int parameter_count; |
66 int bytecode_length; | 66 int bytecode_length; |
67 const uint8_t bytecode[32]; | 67 const uint8_t bytecode[512]; |
68 int constant_count; | 68 int constant_count; |
69 T constants[16]; | 69 T constants[16]; |
70 }; | 70 }; |
71 | 71 |
72 | 72 |
73 // Helper macros for handcrafting bytecode sequences. | 73 // Helper macros for handcrafting bytecode sequences. |
74 #define B(x) static_cast<uint8_t>(Bytecode::k##x) | 74 #define B(x) static_cast<uint8_t>(Bytecode::k##x) |
75 #define U8(x) static_cast<uint8_t>((x) & 0xff) | 75 #define U8(x) static_cast<uint8_t>((x) & 0xff) |
76 #define R(x) static_cast<uint8_t>(-(x) & 0xff) | 76 #define R(x) static_cast<uint8_t>(-(x) & 0xff) |
77 | 77 |
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
601 ba->length())); | 601 ba->length())); |
602 CHECK_EQ(ba->constant_pool()->length(), snippets[i].constant_count); | 602 CHECK_EQ(ba->constant_pool()->length(), snippets[i].constant_count); |
603 for (int j = 0; j < snippets[i].constant_count; j++) { | 603 for (int j = 0; j < snippets[i].constant_count; j++) { |
604 Handle<String> expected = | 604 Handle<String> expected = |
605 helper.factory()->NewStringFromAsciiChecked(snippets[i].constants[j]); | 605 helper.factory()->NewStringFromAsciiChecked(snippets[i].constants[j]); |
606 CHECK(String::cast(ba->constant_pool()->get(j))->Equals(*expected)); | 606 CHECK(String::cast(ba->constant_pool()->get(j))->Equals(*expected)); |
607 } | 607 } |
608 } | 608 } |
609 } | 609 } |
610 | 610 |
| 611 |
| 612 TEST(IfConditions) { |
| 613 InitializedHandleScope handle_scope; |
| 614 BytecodeGeneratorHelper helper; |
| 615 |
| 616 ExpectedSnippet<void*> snippets[] = { |
| 617 {"function f() { if (false) { return 1; } else { return -1; } }", |
| 618 0, |
| 619 1, |
| 620 12, |
| 621 {B(LdaFalse), B(CastToBoolean), B(JumpIfFalse), U8(7), B(LdaSmi8), U8(1), |
| 622 B(Return), B(Jump), |
| 623 U8(5), // TODO(oth): Fix implicit return so dead-jump has |
| 624 // a valid target. |
| 625 B(LdaSmi8), U8(-1), B(Return)}}, |
| 626 {"function f(a) { if (a <= 0) { return 200; } else { return -200; } }", |
| 627 kPointerSize, |
| 628 2, |
| 629 18, |
| 630 {B(Ldar), R(-5), B(Star), R(0), B(LdaZero), B(TestLessThanEqual), R(0), |
| 631 B(CastToBoolean), B(JumpIfFalse), U8(7), B(LdaConstant), U8(0), |
| 632 B(Return), B(Jump), |
| 633 U8(5), // TODO(oth): Fix implicit return so dead-jump has |
| 634 // a valid target. |
| 635 B(LdaConstant), U8(1), B(Return)}}, |
| 636 {"function f(a, b) { if (a in b) { return 200; } }", |
| 637 kPointerSize, |
| 638 3, |
| 639 18, |
| 640 {B(Ldar), R(-6), B(Star), R(0), B(Ldar), R(-5), B(TestIn), R(0), |
| 641 B(CastToBoolean), B(JumpIfFalse), U8(7), B(LdaConstant), U8(0), |
| 642 B(Return), B(Jump), |
| 643 U8(2), // TODO(oth): Fix implicit return so dead-jump has |
| 644 // a valid target. |
| 645 B(LdaUndefined), B(Return)}}, |
| 646 {"function f(a, b) { if (a instanceof b) { return 200; } }", |
| 647 kPointerSize, |
| 648 3, |
| 649 18, |
| 650 {B(Ldar), R(-6), B(Star), R(0), B(Ldar), R(-5), B(TestInstanceOf), R(0), |
| 651 B(CastToBoolean), B(JumpIfFalse), U8(7), B(LdaConstant), U8(0), |
| 652 B(Return), B(Jump), |
| 653 U8(2), // TODO(oth): Fix implicit return so dead-jump has |
| 654 // a valid target. |
| 655 B(LdaUndefined), B(Return)}}, |
| 656 {"function f(z) { var a = 0; var b = 0; if (a === 0.01) { " |
| 657 // Force a jump using a constant pool entry. |
| 658 #define X "b = a; a = b; " |
| 659 X X X X X X X X X X X X X X X X X X X X X X X X |
| 660 #undef X |
| 661 " return 200; } else { return -200; } }", |
| 662 3 * kPointerSize, |
| 663 2, |
| 664 217, |
| 665 {B(LdaZero), B(Star), R(0), B(LdaZero), B(Star), R(1), B(Ldar), R(0), |
| 666 B(Star), R(2), B(LdaConstant), U8(0), B(TestEqualStrict), R(2), |
| 667 B(CastToBoolean), B(JumpIfFalseConstant), U8(2), |
| 668 #define X B(Ldar), R(0), B(Star), R(1), B(Ldar), R(1), B(Star), R(0), |
| 669 X X X X X X X X X X X X X X X X X X X X X X X X |
| 670 #undef X |
| 671 B(LdaConstant), |
| 672 U8(1), B(Return), B(Jump), |
| 673 U8(5), // TODO(oth): Fix implicit return so dead-jump has |
| 674 // a valid target. |
| 675 B(LdaConstant), U8(3), B(Return)}}, |
| 676 }; |
| 677 |
| 678 size_t num_snippets = sizeof(snippets) / sizeof(snippets[0]); |
| 679 for (size_t i = 0; i < num_snippets; i++) { |
| 680 Handle<BytecodeArray> ba = |
| 681 helper.MakeBytecodeForFunction(snippets[i].code_snippet); |
| 682 CHECK_EQ(ba->frame_size(), snippets[i].frame_size); |
| 683 CHECK_EQ(ba->parameter_count(), snippets[i].parameter_count); |
| 684 CHECK_EQ(ba->length(), snippets[i].bytecode_length); |
| 685 CHECK(!memcmp(ba->GetFirstBytecodeAddress(), snippets[i].bytecode, |
| 686 ba->length())); |
| 687 } |
| 688 } |
| 689 |
| 690 |
611 } // namespace interpreter | 691 } // namespace interpreter |
612 } // namespace internal | 692 } // namespace internal |
613 } // namespance v8 | 693 } // namespance v8 |
OLD | NEW |