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 (0) { return 1; } else { return -1; } }", | |
618 0, | |
619 1, | |
620 14, | |
621 {B(LdaZero), 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. | |
rmcilroy
2015/09/23 14:00:29
Is the todo done?
oth
2015/09/24 11:15:27
No, there needs to be code to evaluate whether the
rmcilroy
2015/09/24 11:44:36
Right - I was confused about what the comment mean
| |
625 B(LdaSmi8), U8(-1), B(Return), B(LdaUndefined), B(Return)}}, | |
rmcilroy
2015/09/23 14:00:29
nit - I know git cl format messes this format up,
oth
2015/09/24 11:15:28
Done.
| |
626 {"function f() { if ('lucky') { return 1; } else { return -1; } }", | |
627 0, | |
628 1, | |
629 15, | |
630 {B(LdaConstant), U8(0), B(CastToBoolean), B(JumpIfFalse), U8(7), | |
631 B(LdaSmi8), U8(1), B(Return), B(Jump), | |
632 U8(5), // TODO(oth): Fix implicit return so dead-jump has | |
633 // a valid target. | |
634 B(LdaSmi8), U8(-1), B(Return), B(LdaUndefined), B(Return)}}, | |
635 {"function f() { if (false) { return 1; } else { return -1; } }", | |
636 0, | |
637 1, | |
638 13, | |
639 {B(LdaFalse), B(JumpIfFalse), U8(7), B(LdaSmi8), U8(1), B(Return), | |
640 B(Jump), | |
641 U8(5), // TODO(oth): Fix implicit return so dead-jump has | |
642 // a valid target. | |
643 B(LdaSmi8), U8(-1), B(Return), B(LdaUndefined), B(Return)}}, | |
644 {"function f(a) { if (a <= 0) { return 200; } else { return -200; } }", | |
645 kPointerSize, | |
646 2, | |
647 19, | |
648 {B(Ldar), R(-5), B(Star), R(0), B(LdaZero), B(TestLessThanEqual), R(0), | |
649 B(JumpIfFalse), U8(7), B(LdaConstant), U8(0), B(Return), B(Jump), | |
650 U8(5), // TODO(oth): Fix implicit return so dead-jump has | |
651 // a valid target. | |
652 B(LdaConstant), U8(1), B(Return), B(LdaUndefined), B(Return)}}, | |
653 {"function f(a, b) { if (a in b) { return 200; } }", | |
654 kPointerSize, | |
655 3, | |
656 17, | |
657 {B(Ldar), R(-6), B(Star), R(0), B(Ldar), R(-5), B(TestIn), R(0), | |
658 B(JumpIfFalse), U8(7), B(LdaConstant), U8(0), B(Return), B(Jump), | |
659 U8(2), // TODO(oth): Fix implicit return so dead-jump has | |
660 // a valid target. | |
661 B(LdaUndefined), B(Return)}}, | |
662 {"function f(a, b) { if (a instanceof b) { return 200; } }", | |
663 kPointerSize, | |
664 3, | |
665 17, | |
666 {B(Ldar), R(-6), B(Star), R(0), B(Ldar), R(-5), B(TestInstanceOf), R(0), | |
667 B(JumpIfFalse), U8(7), B(LdaConstant), U8(0), B(Return), B(Jump), | |
668 U8(2), // TODO(oth): Fix implicit return so dead-jump has | |
669 // a valid target. | |
670 B(LdaUndefined), B(Return)}}, | |
671 {"function f(z) { var a = 0; var b = 0; if (a === 0.01) { " | |
672 // Force a jump using a constant pool entry. | |
673 #define X "b = a; a = b; " | |
674 X X X X X X X X X X X X X X X X X X X X X X X X | |
675 #undef X | |
676 " return 200; } else { return -200; } }", | |
677 3 * kPointerSize, | |
678 2, | |
679 218, | |
680 {B(LdaZero), B(Star), R(0), B(LdaZero), B(Star), R(1), B(Ldar), R(0), | |
681 B(Star), R(2), B(LdaConstant), U8(0), B(TestEqualStrict), R(2), | |
682 B(JumpIfFalseConstant), U8(2), | |
683 #define X B(Ldar), R(0), B(Star), R(1), B(Ldar), R(1), B(Star), R(0), | |
684 X X X X X X X X X X X X X X X X X X X X X X X X | |
685 #undef X | |
686 B(LdaConstant), | |
687 U8(1), B(Return), B(Jump), | |
688 U8(5), // TODO(oth): Fix implicit return so dead-jump has | |
689 // a valid target. | |
690 B(LdaConstant), U8(3), B(Return), B(LdaUndefined), B(Return)}}, | |
691 }; | |
rmcilroy
2015/09/23 14:00:29
could you add the expected constant pool entry her
oth
2015/09/24 11:15:28
Done.
| |
692 | |
693 size_t num_snippets = sizeof(snippets) / sizeof(snippets[0]); | |
694 for (size_t i = 0; i < num_snippets; i++) { | |
695 Handle<BytecodeArray> ba = | |
696 helper.MakeBytecodeForFunction(snippets[i].code_snippet); | |
697 CHECK_EQ(ba->frame_size(), snippets[i].frame_size); | |
698 CHECK_EQ(ba->parameter_count(), snippets[i].parameter_count); | |
699 CHECK_EQ(ba->length(), snippets[i].bytecode_length); | |
700 CHECK(!memcmp(ba->GetFirstBytecodeAddress(), snippets[i].bytecode, | |
701 ba->length())); | |
702 } | |
703 } | |
704 | |
705 | |
611 } // namespace interpreter | 706 } // namespace interpreter |
612 } // namespace internal | 707 } // namespace internal |
613 } // namespance v8 | 708 } // namespance v8 |
OLD | NEW |