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/interpreter/bytecode-array-builder.h" | 5 #include "src/interpreter/bytecode-array-builder.h" |
6 | 6 |
7 namespace v8 { | 7 namespace v8 { |
8 namespace internal { | 8 namespace internal { |
9 namespace interpreter { | 9 namespace interpreter { |
10 | 10 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
45 array_builder_.bytecodes()->at(operand_offset)); | 45 array_builder_.bytecodes()->at(operand_offset)); |
46 case OperandSize::kShort: | 46 case OperandSize::kShort: |
47 uint16_t operand = | 47 uint16_t operand = |
48 (array_builder_.bytecodes()->at(operand_offset) << 8) + | 48 (array_builder_.bytecodes()->at(operand_offset) << 8) + |
49 array_builder_.bytecodes()->at(operand_offset + 1); | 49 array_builder_.bytecodes()->at(operand_offset + 1); |
50 return static_cast<uint32_t>(operand); | 50 return static_cast<uint32_t>(operand); |
51 } | 51 } |
52 } | 52 } |
53 | 53 |
54 Handle<Object> GetConstantForIndexOperand(int operand_index) const { | 54 Handle<Object> GetConstantForIndexOperand(int operand_index) const { |
55 return array_builder_.constants_.at(GetOperand(operand_index)); | 55 return array_builder_.constant_array_builder()->At( |
56 GetOperand(operand_index)); | |
56 } | 57 } |
57 | 58 |
58 private: | 59 private: |
59 const BytecodeArrayBuilder& array_builder_; | 60 const BytecodeArrayBuilder& array_builder_; |
60 size_t previous_bytecode_start_; | 61 size_t previous_bytecode_start_; |
61 | 62 |
62 DISALLOW_COPY_AND_ASSIGN(PreviousBytecodeHelper); | 63 DISALLOW_COPY_AND_ASSIGN(PreviousBytecodeHelper); |
63 }; | 64 }; |
64 | 65 |
65 | 66 |
66 BytecodeArrayBuilder::BytecodeArrayBuilder(Isolate* isolate, Zone* zone) | 67 BytecodeArrayBuilder::BytecodeArrayBuilder(Isolate* isolate, Zone* zone) |
67 : isolate_(isolate), | 68 : isolate_(isolate), |
68 zone_(zone), | 69 zone_(zone), |
69 bytecodes_(zone), | 70 bytecodes_(zone), |
70 bytecode_generated_(false), | 71 bytecode_generated_(false), |
72 constant_array_builder_(isolate, zone), | |
71 last_block_end_(0), | 73 last_block_end_(0), |
72 last_bytecode_start_(~0), | 74 last_bytecode_start_(~0), |
73 exit_seen_in_block_(false), | 75 exit_seen_in_block_(false), |
74 unbound_jumps_(0), | 76 unbound_jumps_(0), |
75 constants_map_(isolate->heap(), zone), | |
76 constants_(zone), | |
77 parameter_count_(-1), | 77 parameter_count_(-1), |
78 local_register_count_(-1), | 78 local_register_count_(-1), |
79 context_register_count_(-1), | 79 context_register_count_(-1), |
80 temporary_register_count_(0), | 80 temporary_register_count_(0), |
81 free_temporaries_(zone) {} | 81 free_temporaries_(zone) {} |
82 | 82 |
83 | 83 |
84 BytecodeArrayBuilder::~BytecodeArrayBuilder() { DCHECK_EQ(0, unbound_jumps_); } | 84 BytecodeArrayBuilder::~BytecodeArrayBuilder() { DCHECK_EQ(0, unbound_jumps_); } |
85 | 85 |
86 | 86 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 | 137 |
138 | 138 |
139 bool BytecodeArrayBuilder::RegisterIsTemporary(Register reg) const { | 139 bool BytecodeArrayBuilder::RegisterIsTemporary(Register reg) const { |
140 return temporary_register_count_ > 0 && first_temporary_register() <= reg && | 140 return temporary_register_count_ > 0 && first_temporary_register() <= reg && |
141 reg <= last_temporary_register(); | 141 reg <= last_temporary_register(); |
142 } | 142 } |
143 | 143 |
144 | 144 |
145 Handle<BytecodeArray> BytecodeArrayBuilder::ToBytecodeArray() { | 145 Handle<BytecodeArray> BytecodeArrayBuilder::ToBytecodeArray() { |
146 DCHECK_EQ(bytecode_generated_, false); | 146 DCHECK_EQ(bytecode_generated_, false); |
147 | |
148 EnsureReturn(); | 147 EnsureReturn(); |
149 | 148 |
150 int bytecode_size = static_cast<int>(bytecodes_.size()); | 149 int bytecode_size = static_cast<int>(bytecodes_.size()); |
151 int register_count = fixed_register_count() + temporary_register_count_; | 150 int register_count = fixed_register_count() + temporary_register_count_; |
152 int frame_size = register_count * kPointerSize; | 151 int frame_size = register_count * kPointerSize; |
153 | |
154 Factory* factory = isolate_->factory(); | 152 Factory* factory = isolate_->factory(); |
155 int constants_count = static_cast<int>(constants_.size()); | |
156 Handle<FixedArray> constant_pool = | 153 Handle<FixedArray> constant_pool = |
157 factory->NewFixedArray(constants_count, TENURED); | 154 constant_array_builder()->ToFixedArray(factory); |
158 for (int i = 0; i < constants_count; i++) { | |
159 constant_pool->set(i, *constants_[i]); | |
160 } | |
161 | |
162 Handle<BytecodeArray> output = | 155 Handle<BytecodeArray> output = |
163 factory->NewBytecodeArray(bytecode_size, &bytecodes_.front(), frame_size, | 156 factory->NewBytecodeArray(bytecode_size, &bytecodes_.front(), frame_size, |
164 parameter_count(), constant_pool); | 157 parameter_count(), constant_pool); |
165 bytecode_generated_ = true; | 158 bytecode_generated_ = true; |
166 return output; | 159 return output; |
167 } | 160 } |
168 | 161 |
169 | 162 |
170 template <size_t N> | 163 template <size_t N> |
171 void BytecodeArrayBuilder::Output(Bytecode bytecode, uint32_t(&operands)[N]) { | 164 void BytecodeArrayBuilder::Output(Bytecode bytecode, uint32_t(&operands)[N]) { |
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
746 case Bytecode::kJumpIfToBooleanTrue: | 739 case Bytecode::kJumpIfToBooleanTrue: |
747 return Bytecode::kJumpIfToBooleanTrueConstant; | 740 return Bytecode::kJumpIfToBooleanTrueConstant; |
748 case Bytecode::kJumpIfToBooleanFalse: | 741 case Bytecode::kJumpIfToBooleanFalse: |
749 return Bytecode::kJumpIfToBooleanFalseConstant; | 742 return Bytecode::kJumpIfToBooleanFalseConstant; |
750 case Bytecode::kJumpIfNull: | 743 case Bytecode::kJumpIfNull: |
751 return Bytecode::kJumpIfNullConstant; | 744 return Bytecode::kJumpIfNullConstant; |
752 case Bytecode::kJumpIfUndefined: | 745 case Bytecode::kJumpIfUndefined: |
753 return Bytecode::kJumpIfUndefinedConstant; | 746 return Bytecode::kJumpIfUndefinedConstant; |
754 default: | 747 default: |
755 UNREACHABLE(); | 748 UNREACHABLE(); |
756 return Bytecode::kJumpConstant; | 749 return static_cast<Bytecode>(-1); |
757 } | 750 } |
758 } | 751 } |
759 | 752 |
760 | 753 |
761 void BytecodeArrayBuilder::PatchJump( | 754 // static |
762 const ZoneVector<uint8_t>::iterator& jump_target, | 755 Bytecode BytecodeArrayBuilder::GetJumpWithConstantWideOperand( |
763 ZoneVector<uint8_t>::iterator jump_location) { | 756 Bytecode jump_bytecode) { |
764 Bytecode jump_bytecode = Bytecodes::FromByte(*jump_location); | 757 switch (jump_bytecode) { |
765 int delta = static_cast<int>(jump_target - jump_location); | 758 case Bytecode::kJump: |
766 | 759 return Bytecode::kJumpConstantWide; |
767 DCHECK(Bytecodes::IsJump(jump_bytecode)); | 760 case Bytecode::kJumpIfTrue: |
768 DCHECK_EQ(Bytecodes::Size(jump_bytecode), 2); | 761 return Bytecode::kJumpIfTrueConstantWide; |
769 DCHECK_NE(delta, 0); | 762 case Bytecode::kJumpIfFalse: |
770 | 763 return Bytecode::kJumpIfFalseConstantWide; |
771 if (FitsInImm8Operand(delta)) { | 764 case Bytecode::kJumpIfToBooleanTrue: |
772 // Just update the operand | 765 return Bytecode::kJumpIfToBooleanTrueConstantWide; |
773 jump_location++; | 766 case Bytecode::kJumpIfToBooleanFalse: |
774 *jump_location = static_cast<uint8_t>(delta); | 767 return Bytecode::kJumpIfToBooleanFalseConstantWide; |
775 } else { | 768 case Bytecode::kJumpIfNull: |
776 // Update the jump type and operand | 769 return Bytecode::kJumpIfNullConstantWide; |
777 size_t entry = GetConstantPoolEntry(handle(Smi::FromInt(delta), isolate())); | 770 case Bytecode::kJumpIfUndefined: |
778 if (FitsInIdx8Operand(entry)) { | 771 return Bytecode::kJumpIfUndefinedConstantWide; |
779 jump_bytecode = GetJumpWithConstantOperand(jump_bytecode); | 772 default: |
780 *jump_location++ = Bytecodes::ToByte(jump_bytecode); | 773 UNREACHABLE(); |
781 *jump_location = static_cast<uint8_t>(entry); | 774 return static_cast<Bytecode>(-1); |
782 } else { | |
783 // TODO(oth): OutputJump should reserve a constant pool entry | |
784 // when jump is written. The reservation should be used here if | |
785 // needed, or cancelled if not. This is due to the patch needing | |
786 // to match the size of the code it's replacing. In future, | |
787 // there will probably be a jump with 32-bit operand for cases | |
788 // when constant pool is full, but that needs to be emitted in | |
789 // OutputJump too. | |
790 UNIMPLEMENTED(); | |
791 } | |
792 } | 775 } |
793 unbound_jumps_--; | |
794 } | 776 } |
795 | 777 |
796 | 778 |
797 // static | 779 // static |
798 Bytecode BytecodeArrayBuilder::GetJumpWithToBoolean(Bytecode jump_bytecode) { | 780 Bytecode BytecodeArrayBuilder::GetJumpWithToBoolean(Bytecode jump_bytecode) { |
799 switch (jump_bytecode) { | 781 switch (jump_bytecode) { |
800 case Bytecode::kJump: | 782 case Bytecode::kJump: |
801 case Bytecode::kJumpIfNull: | 783 case Bytecode::kJumpIfNull: |
802 case Bytecode::kJumpIfUndefined: | 784 case Bytecode::kJumpIfUndefined: |
803 return jump_bytecode; | 785 return jump_bytecode; |
804 case Bytecode::kJumpIfTrue: | 786 case Bytecode::kJumpIfTrue: |
805 return Bytecode::kJumpIfToBooleanTrue; | 787 return Bytecode::kJumpIfToBooleanTrue; |
806 case Bytecode::kJumpIfFalse: | 788 case Bytecode::kJumpIfFalse: |
807 return Bytecode::kJumpIfToBooleanFalse; | 789 return Bytecode::kJumpIfToBooleanFalse; |
808 default: | 790 default: |
809 UNREACHABLE(); | 791 UNREACHABLE(); |
810 } | 792 } |
811 return static_cast<Bytecode>(-1); | 793 return static_cast<Bytecode>(-1); |
812 } | 794 } |
813 | 795 |
814 | 796 |
797 void BytecodeArrayBuilder::PatchIndirectJumpWith8BitOperand( | |
798 const ZoneVector<uint8_t>::iterator& jump_target, | |
799 const ZoneVector<uint8_t>::iterator& jump_location) { | |
800 int delta = static_cast<int>(jump_target - jump_location); | |
801 Bytecode jump_bytecode = Bytecodes::FromByte(*jump_location); | |
802 ZoneVector<uint8_t>::iterator operand_location = jump_location + 1; | |
803 if (FitsInImm8Operand(delta)) { | |
804 // The jump fits within the range of an Imm8 operand, so cancel | |
805 // the reservation and jump directly. | |
806 constant_array_builder()->DiscardReservedEntry(OperandSize::kByte); | |
rmcilroy
2016/01/05 13:46:10
Could you add a DCHECK that the bytecode is the ex
oth
2016/01/05 18:31:59
Done + added DCHECK that the operand is zero in bo
| |
807 *operand_location = static_cast<uint8_t>(delta); | |
808 } else { | |
809 // The jump does not fit within the range of an Imm8 operand, so | |
810 // commit reservation putting the offset into the constant pool, | |
811 // and update the jump instruction and operand. | |
812 size_t entry = constant_array_builder()->CommitReservedEntry( | |
813 OperandSize::kByte, handle(Smi::FromInt(delta), isolate())); | |
814 DCHECK(FitsInIdx8Operand(entry)); | |
815 jump_bytecode = GetJumpWithConstantOperand(jump_bytecode); | |
816 *jump_location = Bytecodes::ToByte(jump_bytecode); | |
817 *operand_location = static_cast<uint8_t>(entry); | |
818 } | |
819 } | |
820 | |
821 | |
822 void BytecodeArrayBuilder::PatchIndirectJumpWith16BitOperand( | |
823 const ZoneVector<uint8_t>::iterator& jump_target, | |
824 const ZoneVector<uint8_t>::iterator& jump_location) { | |
825 DCHECK(Bytecodes::IsJumpConstantWide(Bytecodes::FromByte(*jump_location))); | |
826 int delta = static_cast<int>(jump_target - jump_location); | |
827 ZoneVector<uint8_t>::iterator operand_location = jump_location + 1; | |
828 size_t entry = constant_array_builder()->CommitReservedEntry( | |
829 OperandSize::kShort, handle(Smi::FromInt(delta), isolate())); | |
830 DCHECK(FitsInIdx16Operand(entry)); | |
831 uint8_t operand_bytes[2]; | |
832 WriteUnalignedUInt16(operand_bytes, static_cast<uint16_t>(entry)); | |
833 *operand_location++ = operand_bytes[0]; | |
834 *operand_location = operand_bytes[1]; | |
835 } | |
836 | |
837 | |
838 void BytecodeArrayBuilder::PatchJump( | |
839 const ZoneVector<uint8_t>::iterator& jump_target, | |
840 const ZoneVector<uint8_t>::iterator& jump_location) { | |
841 Bytecode jump_bytecode = Bytecodes::FromByte(*jump_location); | |
rmcilroy
2016/01/05 13:46:10
nit - you could calculate delta here and pass it t
oth
2016/01/05 18:31:59
Done.
| |
842 DCHECK(Bytecodes::IsJump(jump_bytecode)); | |
843 switch (Bytecodes::GetOperandSize(jump_bytecode, 0)) { | |
844 case OperandSize::kByte: | |
845 PatchIndirectJumpWith8BitOperand(jump_target, jump_location); | |
846 break; | |
847 case OperandSize::kShort: | |
848 PatchIndirectJumpWith16BitOperand(jump_target, jump_location); | |
849 break; | |
850 case OperandSize::kNone: | |
851 UNREACHABLE(); | |
852 } | |
853 unbound_jumps_--; | |
854 } | |
855 | |
856 | |
815 BytecodeArrayBuilder& BytecodeArrayBuilder::OutputJump(Bytecode jump_bytecode, | 857 BytecodeArrayBuilder& BytecodeArrayBuilder::OutputJump(Bytecode jump_bytecode, |
816 BytecodeLabel* label) { | 858 BytecodeLabel* label) { |
817 // Don't emit dead code. | 859 // Don't emit dead code. |
818 if (exit_seen_in_block_) return *this; | 860 if (exit_seen_in_block_) return *this; |
819 | 861 |
820 // Check if the value in accumulator is boolean, if not choose an | 862 // Check if the value in accumulator is boolean, if not choose an |
821 // appropriate JumpIfToBoolean bytecode. | 863 // appropriate JumpIfToBoolean bytecode. |
822 if (NeedToBooleanCast()) { | 864 if (NeedToBooleanCast()) { |
823 jump_bytecode = GetJumpWithToBoolean(jump_bytecode); | 865 jump_bytecode = GetJumpWithToBoolean(jump_bytecode); |
824 } | 866 } |
825 | 867 |
826 int delta; | |
827 if (label->is_bound()) { | 868 if (label->is_bound()) { |
828 // Label has been bound already so this is a backwards jump. | 869 // Label has been bound already so this is a backwards jump. |
829 CHECK_GE(bytecodes()->size(), label->offset()); | 870 CHECK_GE(bytecodes()->size(), label->offset()); |
830 CHECK_LE(bytecodes()->size(), static_cast<size_t>(kMaxInt)); | 871 CHECK_LE(bytecodes()->size(), static_cast<size_t>(kMaxInt)); |
831 size_t abs_delta = bytecodes()->size() - label->offset(); | 872 size_t abs_delta = bytecodes()->size() - label->offset(); |
832 delta = -static_cast<int>(abs_delta); | 873 int delta = -static_cast<int>(abs_delta); |
874 | |
875 if (FitsInImm8Operand(delta)) { | |
876 Output(jump_bytecode, static_cast<uint8_t>(delta)); | |
877 } else { | |
878 size_t entry = | |
879 GetConstantPoolEntry(handle(Smi::FromInt(delta), isolate())); | |
880 if (FitsInIdx8Operand(entry)) { | |
881 Output(GetJumpWithConstantOperand(jump_bytecode), | |
882 static_cast<uint8_t>(entry)); | |
883 } else if (FitsInIdx16Operand(entry)) { | |
884 Output(GetJumpWithConstantWideOperand(jump_bytecode), | |
885 static_cast<uint16_t>(entry)); | |
886 } else { | |
887 UNREACHABLE(); | |
888 } | |
889 } | |
833 } else { | 890 } else { |
834 // Label has not yet been bound so this is a forward reference | 891 // The label has not yet been bound so this is a forward reference |
835 // that will be patched when the label is bound. | 892 // that will be patched when the label is bound. We create a |
893 // reservation in the constant pool so the jump can be patched | |
894 // when the label is bound. The reservation means the maximum size | |
895 // of the operand for the constant is known and the jump can | |
896 // be emitted into the bytecode stream with space for the operand. | |
836 label->set_referrer(bytecodes()->size()); | 897 label->set_referrer(bytecodes()->size()); |
837 delta = 0; | |
838 unbound_jumps_++; | 898 unbound_jumps_++; |
839 } | 899 OperandSize reserved_operand_size = |
840 | 900 constant_array_builder()->CreateReservedEntry(); |
841 if (FitsInImm8Operand(delta)) { | 901 switch (reserved_operand_size) { |
842 Output(jump_bytecode, static_cast<uint8_t>(delta)); | 902 case OperandSize::kByte: |
843 } else { | 903 Output(jump_bytecode, 0); |
844 size_t entry = GetConstantPoolEntry(handle(Smi::FromInt(delta), isolate())); | 904 break; |
845 if (FitsInIdx8Operand(entry)) { | 905 case OperandSize::kShort: |
846 Output(GetJumpWithConstantOperand(jump_bytecode), | 906 Output(GetJumpWithConstantWideOperand(jump_bytecode), 0); |
847 static_cast<uint8_t>(entry)); | 907 break; |
848 } else { | 908 case OperandSize::kNone: |
849 UNIMPLEMENTED(); | 909 UNREACHABLE(); |
850 } | 910 } |
851 } | 911 } |
852 LeaveBasicBlock(); | 912 LeaveBasicBlock(); |
853 return *this; | 913 return *this; |
854 } | 914 } |
855 | 915 |
856 | 916 |
857 BytecodeArrayBuilder& BytecodeArrayBuilder::Jump(BytecodeLabel* label) { | 917 BytecodeArrayBuilder& BytecodeArrayBuilder::Jump(BytecodeLabel* label) { |
858 return OutputJump(Bytecode::kJump, label); | 918 return OutputJump(Bytecode::kJump, label); |
859 } | 919 } |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1005 } | 1065 } |
1006 | 1066 |
1007 | 1067 |
1008 BytecodeArrayBuilder& BytecodeArrayBuilder::DeleteLookupSlot() { | 1068 BytecodeArrayBuilder& BytecodeArrayBuilder::DeleteLookupSlot() { |
1009 Output(Bytecode::kDeleteLookupSlot); | 1069 Output(Bytecode::kDeleteLookupSlot); |
1010 return *this; | 1070 return *this; |
1011 } | 1071 } |
1012 | 1072 |
1013 | 1073 |
1014 size_t BytecodeArrayBuilder::GetConstantPoolEntry(Handle<Object> object) { | 1074 size_t BytecodeArrayBuilder::GetConstantPoolEntry(Handle<Object> object) { |
1015 // These constants shouldn't be added to the constant pool, the should use | 1075 return constant_array_builder()->Insert(object); |
1016 // specialzed bytecodes instead. | |
1017 DCHECK(!object.is_identical_to(isolate_->factory()->undefined_value())); | |
1018 DCHECK(!object.is_identical_to(isolate_->factory()->null_value())); | |
1019 DCHECK(!object.is_identical_to(isolate_->factory()->the_hole_value())); | |
1020 DCHECK(!object.is_identical_to(isolate_->factory()->true_value())); | |
1021 DCHECK(!object.is_identical_to(isolate_->factory()->false_value())); | |
1022 | |
1023 size_t* entry = constants_map_.Find(object); | |
1024 if (!entry) { | |
1025 entry = constants_map_.Get(object); | |
1026 *entry = constants_.size(); | |
1027 constants_.push_back(object); | |
1028 } | |
1029 DCHECK(constants_[*entry].is_identical_to(object)); | |
1030 return *entry; | |
1031 } | 1076 } |
1032 | 1077 |
1033 | 1078 |
1034 int BytecodeArrayBuilder::BorrowTemporaryRegister() { | 1079 int BytecodeArrayBuilder::BorrowTemporaryRegister() { |
1035 if (free_temporaries_.empty()) { | 1080 if (free_temporaries_.empty()) { |
1036 temporary_register_count_ += 1; | 1081 temporary_register_count_ += 1; |
1037 return last_temporary_register().index(); | 1082 return last_temporary_register().index(); |
1038 } else { | 1083 } else { |
1039 auto pos = free_temporaries_.begin(); | 1084 auto pos = free_temporaries_.begin(); |
1040 int retval = *pos; | 1085 int retval = *pos; |
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1543 DCHECK_GT(next_consecutive_count_, 0); | 1588 DCHECK_GT(next_consecutive_count_, 0); |
1544 builder_->BorrowConsecutiveTemporaryRegister(next_consecutive_register_); | 1589 builder_->BorrowConsecutiveTemporaryRegister(next_consecutive_register_); |
1545 allocated_.push_back(next_consecutive_register_); | 1590 allocated_.push_back(next_consecutive_register_); |
1546 next_consecutive_count_--; | 1591 next_consecutive_count_--; |
1547 return Register(next_consecutive_register_++); | 1592 return Register(next_consecutive_register_++); |
1548 } | 1593 } |
1549 | 1594 |
1550 } // namespace interpreter | 1595 } // namespace interpreter |
1551 } // namespace internal | 1596 } // namespace internal |
1552 } // namespace v8 | 1597 } // namespace v8 |
OLD | NEW |