| 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-iterator.h" | 5 #include "src/interpreter/bytecode-array-iterator.h" |
| 6 | 6 |
| 7 #include "src/objects-inl.h" | 7 #include "src/objects-inl.h" |
| 8 | 8 |
| 9 namespace v8 { | 9 namespace v8 { |
| 10 namespace internal { | 10 namespace internal { |
| 11 namespace interpreter { | 11 namespace interpreter { |
| 12 | 12 |
| 13 BytecodeArrayIterator::BytecodeArrayIterator( | 13 BytecodeArrayIterator::BytecodeArrayIterator( |
| 14 Handle<BytecodeArray> bytecode_array) | 14 Handle<BytecodeArray> bytecode_array) |
| 15 : bytecode_array_(bytecode_array), bytecode_offset_(0) {} | 15 : bytecode_array_(bytecode_array), |
| 16 | 16 bytecode_offset_(0), |
| 17 operand_scale_(OperandScale::kSingle), |
| 18 prefix_offset_(0) { |
| 19 UpdateOperandScale(); |
| 20 } |
| 17 | 21 |
| 18 void BytecodeArrayIterator::Advance() { | 22 void BytecodeArrayIterator::Advance() { |
| 19 bytecode_offset_ += Bytecodes::Size(current_bytecode()); | 23 bytecode_offset_ += current_bytecode_size(); |
| 24 UpdateOperandScale(); |
| 20 } | 25 } |
| 21 | 26 |
| 27 void BytecodeArrayIterator::UpdateOperandScale() { |
| 28 if (!done()) { |
| 29 uint8_t current_byte = bytecode_array()->get(bytecode_offset_); |
| 30 Bytecode current_bytecode = Bytecodes::FromByte(current_byte); |
| 31 if (Bytecodes::IsPrefixScalingBytecode(current_bytecode)) { |
| 32 operand_scale_ = |
| 33 Bytecodes::PrefixBytecodeToOperandScale(current_bytecode); |
| 34 prefix_offset_ = 1; |
| 35 } else { |
| 36 operand_scale_ = OperandScale::kSingle; |
| 37 prefix_offset_ = 0; |
| 38 } |
| 39 } |
| 40 } |
| 22 | 41 |
| 23 bool BytecodeArrayIterator::done() const { | 42 bool BytecodeArrayIterator::done() const { |
| 24 return bytecode_offset_ >= bytecode_array()->length(); | 43 return bytecode_offset_ >= bytecode_array()->length(); |
| 25 } | 44 } |
| 26 | 45 |
| 27 | |
| 28 Bytecode BytecodeArrayIterator::current_bytecode() const { | 46 Bytecode BytecodeArrayIterator::current_bytecode() const { |
| 29 DCHECK(!done()); | 47 DCHECK(!done()); |
| 30 uint8_t current_byte = bytecode_array()->get(bytecode_offset_); | 48 uint8_t current_byte = |
| 31 return interpreter::Bytecodes::FromByte(current_byte); | 49 bytecode_array()->get(bytecode_offset_ + current_prefix_offset()); |
| 50 Bytecode current_bytecode = Bytecodes::FromByte(current_byte); |
| 51 DCHECK(!Bytecodes::IsPrefixScalingBytecode(current_bytecode)); |
| 52 return current_bytecode; |
| 32 } | 53 } |
| 33 | 54 |
| 34 | |
| 35 int BytecodeArrayIterator::current_bytecode_size() const { | 55 int BytecodeArrayIterator::current_bytecode_size() const { |
| 36 return Bytecodes::Size(current_bytecode()); | 56 return current_prefix_offset() + |
| 57 Bytecodes::Size(current_bytecode(), current_operand_scale()); |
| 37 } | 58 } |
| 38 | 59 |
| 39 | 60 uint32_t BytecodeArrayIterator::GetUnsignedOperand( |
| 40 uint32_t BytecodeArrayIterator::GetRawOperand(int operand_index, | 61 int operand_index, OperandType operand_type) const { |
| 41 OperandType operand_type) const { | |
| 42 DCHECK_GE(operand_index, 0); | 62 DCHECK_GE(operand_index, 0); |
| 43 DCHECK_LT(operand_index, Bytecodes::NumberOfOperands(current_bytecode())); | 63 DCHECK_LT(operand_index, Bytecodes::NumberOfOperands(current_bytecode())); |
| 44 DCHECK_EQ(operand_type, | 64 DCHECK_EQ(operand_type, |
| 45 Bytecodes::GetOperandType(current_bytecode(), operand_index)); | 65 Bytecodes::GetOperandType(current_bytecode(), operand_index)); |
| 46 uint8_t* operand_start = | 66 DCHECK(Bytecodes::IsUnsignedOperandType(operand_type)); |
| 67 const uint8_t* operand_start = |
| 47 bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ + | 68 bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ + |
| 48 Bytecodes::GetOperandOffset(current_bytecode(), operand_index); | 69 current_prefix_offset() + |
| 49 switch (Bytecodes::SizeOfOperand(operand_type)) { | 70 Bytecodes::GetOperandOffset(current_bytecode(), operand_index, |
| 50 case OperandSize::kByte: | 71 current_operand_scale()); |
| 51 return static_cast<uint32_t>(*operand_start); | 72 return Bytecodes::DecodeUnsignedOperand(operand_start, operand_type, |
| 52 case OperandSize::kShort: | 73 current_operand_scale()); |
| 53 return ReadUnalignedUInt16(operand_start); | |
| 54 case OperandSize::kNone: | |
| 55 UNREACHABLE(); | |
| 56 } | |
| 57 return 0; | |
| 58 } | 74 } |
| 59 | 75 |
| 60 | 76 int32_t BytecodeArrayIterator::GetSignedOperand( |
| 61 int8_t BytecodeArrayIterator::GetImmediateOperand(int operand_index) const { | 77 int operand_index, OperandType operand_type) const { |
| 62 uint32_t operand = GetRawOperand(operand_index, OperandType::kImm8); | 78 DCHECK_GE(operand_index, 0); |
| 63 return static_cast<int8_t>(operand); | 79 DCHECK_LT(operand_index, Bytecodes::NumberOfOperands(current_bytecode())); |
| 80 DCHECK_EQ(operand_type, |
| 81 Bytecodes::GetOperandType(current_bytecode(), operand_index)); |
| 82 DCHECK(!Bytecodes::IsUnsignedOperandType(operand_type)); |
| 83 const uint8_t* operand_start = |
| 84 bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ + |
| 85 current_prefix_offset() + |
| 86 Bytecodes::GetOperandOffset(current_bytecode(), operand_index, |
| 87 current_operand_scale()); |
| 88 return Bytecodes::DecodeSignedOperand(operand_start, operand_type, |
| 89 current_operand_scale()); |
| 64 } | 90 } |
| 65 | 91 |
| 66 int BytecodeArrayIterator::GetRegisterCountOperand(int operand_index) const { | 92 uint32_t BytecodeArrayIterator::GetFlagOperand(int operand_index) const { |
| 67 OperandSize size = | 93 DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index), |
| 68 Bytecodes::GetOperandSize(current_bytecode(), operand_index); | 94 OperandType::kFlag8); |
| 69 OperandType type = (size == OperandSize::kByte) ? OperandType::kRegCount8 | 95 return GetUnsignedOperand(operand_index, OperandType::kFlag8); |
| 70 : OperandType::kRegCount16; | |
| 71 uint32_t operand = GetRawOperand(operand_index, type); | |
| 72 return static_cast<int>(operand); | |
| 73 } | 96 } |
| 74 | 97 |
| 98 int32_t BytecodeArrayIterator::GetImmediateOperand(int operand_index) const { |
| 99 DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index), |
| 100 OperandType::kImm); |
| 101 return GetSignedOperand(operand_index, OperandType::kImm); |
| 102 } |
| 75 | 103 |
| 76 int BytecodeArrayIterator::GetIndexOperand(int operand_index) const { | 104 uint32_t BytecodeArrayIterator::GetRegisterCountOperand( |
| 105 int operand_index) const { |
| 106 DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index), |
| 107 OperandType::kRegCount); |
| 108 return GetUnsignedOperand(operand_index, OperandType::kRegCount); |
| 109 } |
| 110 |
| 111 uint32_t BytecodeArrayIterator::GetIndexOperand(int operand_index) const { |
| 77 OperandType operand_type = | 112 OperandType operand_type = |
| 78 Bytecodes::GetOperandType(current_bytecode(), operand_index); | 113 Bytecodes::GetOperandType(current_bytecode(), operand_index); |
| 79 DCHECK(operand_type == OperandType::kIdx8 || | 114 DCHECK_EQ(operand_type, OperandType::kIdx); |
| 80 operand_type == OperandType::kIdx16); | 115 return GetUnsignedOperand(operand_index, operand_type); |
| 81 uint32_t operand = GetRawOperand(operand_index, operand_type); | |
| 82 return static_cast<int>(operand); | |
| 83 } | 116 } |
| 84 | 117 |
| 85 | |
| 86 Register BytecodeArrayIterator::GetRegisterOperand(int operand_index) const { | 118 Register BytecodeArrayIterator::GetRegisterOperand(int operand_index) const { |
| 87 OperandType operand_type = | 119 OperandType operand_type = |
| 88 Bytecodes::GetOperandType(current_bytecode(), operand_index); | 120 Bytecodes::GetOperandType(current_bytecode(), operand_index); |
| 89 DCHECK(Bytecodes::IsRegisterOperandType(operand_type)); | 121 const uint8_t* operand_start = |
| 90 uint32_t operand = GetRawOperand(operand_index, operand_type); | 122 bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ + |
| 91 Register reg; | 123 current_prefix_offset() + |
| 92 switch (Bytecodes::GetOperandSize(current_bytecode(), operand_index)) { | 124 Bytecodes::GetOperandOffset(current_bytecode(), operand_index, |
| 93 case OperandSize::kByte: | 125 current_operand_scale()); |
| 94 reg = Register::FromOperand(static_cast<uint8_t>(operand)); | 126 return Bytecodes::DecodeRegisterOperand(operand_start, operand_type, |
| 95 break; | 127 current_operand_scale()); |
| 96 case OperandSize::kShort: | |
| 97 reg = Register::FromWideOperand(static_cast<uint16_t>(operand)); | |
| 98 break; | |
| 99 case OperandSize::kNone: | |
| 100 UNREACHABLE(); | |
| 101 reg = Register::invalid_value(); | |
| 102 break; | |
| 103 } | |
| 104 DCHECK_GE(reg.index(), | |
| 105 Register::FromParameterIndex(0, bytecode_array()->parameter_count()) | |
| 106 .index()); | |
| 107 DCHECK(reg.index() < bytecode_array()->register_count() || | |
| 108 (reg.index() == 0 && | |
| 109 Bytecodes::IsMaybeRegisterOperandType( | |
| 110 Bytecodes::GetOperandType(current_bytecode(), operand_index)))); | |
| 111 return reg; | |
| 112 } | 128 } |
| 113 | 129 |
| 114 int BytecodeArrayIterator::GetRegisterOperandRange(int operand_index) const { | 130 int BytecodeArrayIterator::GetRegisterOperandRange(int operand_index) const { |
| 115 interpreter::OperandType operand_type = | 131 interpreter::OperandType operand_type = |
| 116 Bytecodes::GetOperandType(current_bytecode(), operand_index); | 132 Bytecodes::GetOperandType(current_bytecode(), operand_index); |
| 117 DCHECK(Bytecodes::IsRegisterOperandType(operand_type)); | 133 DCHECK(Bytecodes::IsRegisterOperandType(operand_type)); |
| 118 switch (operand_type) { | 134 switch (operand_type) { |
| 119 case OperandType::kRegPair8: | 135 case OperandType::kRegPair: |
| 120 case OperandType::kRegPair16: | 136 case OperandType::kRegOutPair: |
| 121 case OperandType::kRegOutPair8: | |
| 122 case OperandType::kRegOutPair16: | |
| 123 return 2; | 137 return 2; |
| 124 case OperandType::kRegOutTriple8: | 138 case OperandType::kRegOutTriple: |
| 125 case OperandType::kRegOutTriple16: | |
| 126 return 3; | 139 return 3; |
| 127 default: { | 140 default: { |
| 128 if (operand_index + 1 != | 141 if (operand_index + 1 != |
| 129 Bytecodes::NumberOfOperands(current_bytecode())) { | 142 Bytecodes::NumberOfOperands(current_bytecode())) { |
| 130 OperandType next_operand_type = | 143 OperandType next_operand_type = |
| 131 Bytecodes::GetOperandType(current_bytecode(), operand_index + 1); | 144 Bytecodes::GetOperandType(current_bytecode(), operand_index + 1); |
| 132 if (Bytecodes::IsRegisterCountOperandType(next_operand_type)) { | 145 if (OperandType::kRegCount == next_operand_type) { |
| 133 return GetRegisterCountOperand(operand_index + 1); | 146 return GetRegisterCountOperand(operand_index + 1); |
| 134 } | 147 } |
| 135 } | 148 } |
| 136 return 1; | 149 return 1; |
| 137 } | 150 } |
| 138 } | 151 } |
| 139 } | 152 } |
| 140 | 153 |
| 154 uint32_t BytecodeArrayIterator::GetRuntimeIdOperand(int operand_index) const { |
| 155 OperandType operand_type = |
| 156 Bytecodes::GetOperandType(current_bytecode(), operand_index); |
| 157 DCHECK(operand_type == OperandType::kRuntimeId); |
| 158 return GetUnsignedOperand(operand_index, operand_type); |
| 159 } |
| 160 |
| 141 Handle<Object> BytecodeArrayIterator::GetConstantForIndexOperand( | 161 Handle<Object> BytecodeArrayIterator::GetConstantForIndexOperand( |
| 142 int operand_index) const { | 162 int operand_index) const { |
| 143 return FixedArray::get(bytecode_array()->constant_pool(), | 163 return FixedArray::get(bytecode_array()->constant_pool(), |
| 144 GetIndexOperand(operand_index), | 164 GetIndexOperand(operand_index), |
| 145 bytecode_array()->GetIsolate()); | 165 bytecode_array()->GetIsolate()); |
| 146 } | 166 } |
| 147 | 167 |
| 148 | 168 |
| 149 int BytecodeArrayIterator::GetJumpTargetOffset() const { | 169 int BytecodeArrayIterator::GetJumpTargetOffset() const { |
| 150 Bytecode bytecode = current_bytecode(); | 170 Bytecode bytecode = current_bytecode(); |
| 151 if (interpreter::Bytecodes::IsJumpImmediate(bytecode)) { | 171 if (interpreter::Bytecodes::IsJumpImmediate(bytecode)) { |
| 152 int relative_offset = GetImmediateOperand(0); | 172 int relative_offset = GetImmediateOperand(0); |
| 153 return current_offset() + relative_offset; | 173 return current_offset() + relative_offset + current_prefix_offset(); |
| 154 } else if (interpreter::Bytecodes::IsJumpConstant(bytecode) || | 174 } else if (interpreter::Bytecodes::IsJumpConstant(bytecode)) { |
| 155 interpreter::Bytecodes::IsJumpConstantWide(bytecode)) { | |
| 156 Smi* smi = Smi::cast(*GetConstantForIndexOperand(0)); | 175 Smi* smi = Smi::cast(*GetConstantForIndexOperand(0)); |
| 157 return current_offset() + smi->value(); | 176 return current_offset() + smi->value() + current_prefix_offset(); |
| 158 } else { | 177 } else { |
| 159 UNREACHABLE(); | 178 UNREACHABLE(); |
| 160 return kMinInt; | 179 return kMinInt; |
| 161 } | 180 } |
| 162 } | 181 } |
| 163 | 182 |
| 164 } // namespace interpreter | 183 } // namespace interpreter |
| 165 } // namespace internal | 184 } // namespace internal |
| 166 } // namespace v8 | 185 } // namespace v8 |
| OLD | NEW |