5 #include "src/interpreter/bytecode-array-accessor.h" 7 #include "src/feedback-vector.h" 8 #include "src/interpreter/bytecode-decoder.h" 9 #include "src/interpreter/interpreter-intrinsics.h" 10 #include "src/objects-inl.h" 11 #include "src/objects/code-inl.h" 15 namespace interpreter {
17 BytecodeArrayAccessor::BytecodeArrayAccessor(
18 Handle<BytecodeArray> bytecode_array,
int initial_offset)
19 : bytecode_array_(bytecode_array),
20 bytecode_offset_(initial_offset),
21 operand_scale_(OperandScale::kSingle),
26 void BytecodeArrayAccessor::SetOffset(
int offset) {
27 bytecode_offset_ = offset;
31 void BytecodeArrayAccessor::ApplyDebugBreak() {
35 interpreter::Bytecode bytecode =
36 interpreter::Bytecodes::FromByte(bytecode_array_->get(bytecode_offset_));
37 if (interpreter::Bytecodes::IsDebugBreak(bytecode))
return;
38 interpreter::Bytecode debugbreak =
39 interpreter::Bytecodes::GetDebugBreak(bytecode);
40 bytecode_array_->set(bytecode_offset_,
41 interpreter::Bytecodes::ToByte(debugbreak));
44 void BytecodeArrayAccessor::UpdateOperandScale() {
45 if (OffsetInBounds()) {
46 uint8_t current_byte = bytecode_array()->get(bytecode_offset_);
47 Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
48 if (Bytecodes::IsPrefixScalingBytecode(current_bytecode)) {
50 Bytecodes::PrefixBytecodeToOperandScale(current_bytecode);
53 operand_scale_ = OperandScale::kSingle;
59 bool BytecodeArrayAccessor::OffsetInBounds()
const {
60 return bytecode_offset_ >= 0 && bytecode_offset_ < bytecode_array()->length();
63 Bytecode BytecodeArrayAccessor::current_bytecode()
const {
64 DCHECK(OffsetInBounds());
65 uint8_t current_byte =
66 bytecode_array()->get(bytecode_offset_ + current_prefix_offset());
67 Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
68 DCHECK(!Bytecodes::IsPrefixScalingBytecode(current_bytecode));
69 return current_bytecode;
72 int BytecodeArrayAccessor::current_bytecode_size()
const {
73 return current_prefix_offset() +
74 Bytecodes::Size(current_bytecode(), current_operand_scale());
77 uint32_t BytecodeArrayAccessor::GetUnsignedOperand(
78 int operand_index, OperandType operand_type)
const {
79 DCHECK_GE(operand_index, 0);
80 DCHECK_LT(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
81 DCHECK_EQ(operand_type,
82 Bytecodes::GetOperandType(current_bytecode(), operand_index));
83 DCHECK(Bytecodes::IsUnsignedOperandType(operand_type));
84 Address operand_start =
85 bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
86 current_prefix_offset() +
87 Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
88 current_operand_scale());
89 return BytecodeDecoder::DecodeUnsignedOperand(operand_start, operand_type,
90 current_operand_scale());
93 int32_t BytecodeArrayAccessor::GetSignedOperand(
94 int operand_index, OperandType operand_type)
const {
95 DCHECK_GE(operand_index, 0);
96 DCHECK_LT(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
97 DCHECK_EQ(operand_type,
98 Bytecodes::GetOperandType(current_bytecode(), operand_index));
99 DCHECK(!Bytecodes::IsUnsignedOperandType(operand_type));
100 Address operand_start =
101 bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
102 current_prefix_offset() +
103 Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
104 current_operand_scale());
105 return BytecodeDecoder::DecodeSignedOperand(operand_start, operand_type,
106 current_operand_scale());
109 uint32_t BytecodeArrayAccessor::GetFlagOperand(
int operand_index)
const {
110 DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
111 OperandType::kFlag8);
112 return GetUnsignedOperand(operand_index, OperandType::kFlag8);
115 uint32_t BytecodeArrayAccessor::GetUnsignedImmediateOperand(
116 int operand_index)
const {
117 DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
119 return GetUnsignedOperand(operand_index, OperandType::kUImm);
122 int32_t BytecodeArrayAccessor::GetImmediateOperand(
int operand_index)
const {
123 DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
125 return GetSignedOperand(operand_index, OperandType::kImm);
128 uint32_t BytecodeArrayAccessor::GetRegisterCountOperand(
129 int operand_index)
const {
130 DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
131 OperandType::kRegCount);
132 return GetUnsignedOperand(operand_index, OperandType::kRegCount);
135 uint32_t BytecodeArrayAccessor::GetIndexOperand(
int operand_index)
const {
136 OperandType operand_type =
137 Bytecodes::GetOperandType(current_bytecode(), operand_index);
138 DCHECK_EQ(operand_type, OperandType::kIdx);
139 return GetUnsignedOperand(operand_index, operand_type);
142 FeedbackSlot BytecodeArrayAccessor::GetSlotOperand(
int operand_index)
const {
143 int index = GetIndexOperand(operand_index);
144 return FeedbackVector::ToSlot(index);
147 Register BytecodeArrayAccessor::GetRegisterOperand(
int operand_index)
const {
148 OperandType operand_type =
149 Bytecodes::GetOperandType(current_bytecode(), operand_index);
150 Address operand_start =
151 bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
152 current_prefix_offset() +
153 Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
154 current_operand_scale());
155 return BytecodeDecoder::DecodeRegisterOperand(operand_start, operand_type,
156 current_operand_scale());
159 int BytecodeArrayAccessor::GetRegisterOperandRange(
int operand_index)
const {
160 DCHECK_LE(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
161 const OperandType* operand_types =
162 Bytecodes::GetOperandTypes(current_bytecode());
163 OperandType operand_type = operand_types[operand_index];
164 DCHECK(Bytecodes::IsRegisterOperandType(operand_type));
165 if (operand_type == OperandType::kRegList ||
166 operand_type == OperandType::kRegOutList) {
167 return GetRegisterCountOperand(operand_index + 1);
169 return Bytecodes::GetNumberOfRegistersRepresentedBy(operand_type);
173 Runtime::FunctionId BytecodeArrayAccessor::GetRuntimeIdOperand(
174 int operand_index)
const {
175 OperandType operand_type =
176 Bytecodes::GetOperandType(current_bytecode(), operand_index);
177 DCHECK_EQ(operand_type, OperandType::kRuntimeId);
178 uint32_t raw_id = GetUnsignedOperand(operand_index, operand_type);
179 return static_cast<Runtime::FunctionId
>(raw_id);
182 uint32_t BytecodeArrayAccessor::GetNativeContextIndexOperand(
183 int operand_index)
const {
184 OperandType operand_type =
185 Bytecodes::GetOperandType(current_bytecode(), operand_index);
186 DCHECK_EQ(operand_type, OperandType::kNativeContextIndex);
187 return GetUnsignedOperand(operand_index, operand_type);
190 Runtime::FunctionId BytecodeArrayAccessor::GetIntrinsicIdOperand(
191 int operand_index)
const {
192 OperandType operand_type =
193 Bytecodes::GetOperandType(current_bytecode(), operand_index);
194 DCHECK_EQ(operand_type, OperandType::kIntrinsicId);
195 uint32_t raw_id = GetUnsignedOperand(operand_index, operand_type);
196 return IntrinsicsHelper::ToRuntimeId(
197 static_cast<IntrinsicsHelper::IntrinsicId>(raw_id));
200 Object* BytecodeArrayAccessor::GetConstantAtIndex(
int index)
const {
201 return bytecode_array()->constant_pool()->get(index);
204 Object* BytecodeArrayAccessor::GetConstantForIndexOperand(
205 int operand_index)
const {
206 return GetConstantAtIndex(GetIndexOperand(operand_index));
209 int BytecodeArrayAccessor::GetJumpTargetOffset()
const {
210 Bytecode bytecode = current_bytecode();
211 if (interpreter::Bytecodes::IsJumpImmediate(bytecode)) {
212 int relative_offset = GetUnsignedImmediateOperand(0);
213 if (bytecode == Bytecode::kJumpLoop) {
214 relative_offset = -relative_offset;
216 return GetAbsoluteOffset(relative_offset);
217 }
else if (interpreter::Bytecodes::IsJumpConstant(bytecode)) {
218 Smi smi = Smi::cast(GetConstantForIndexOperand(0));
219 return GetAbsoluteOffset(smi->value());
225 JumpTableTargetOffsets BytecodeArrayAccessor::GetJumpTableTargetOffsets()
228 int32_t case_value_base;
229 if (current_bytecode() == Bytecode::kSwitchOnGeneratorState) {
230 table_start = GetIndexOperand(1);
231 table_size = GetUnsignedImmediateOperand(2);
234 DCHECK_EQ(current_bytecode(), Bytecode::kSwitchOnSmiNoFeedback);
235 table_start = GetIndexOperand(0);
236 table_size = GetUnsignedImmediateOperand(1);
237 case_value_base = GetImmediateOperand(2);
239 return JumpTableTargetOffsets(
this, table_start, table_size, case_value_base);
242 int BytecodeArrayAccessor::GetAbsoluteOffset(
int relative_offset)
const {
243 return current_offset() + relative_offset + current_prefix_offset();
246 bool BytecodeArrayAccessor::OffsetWithinBytecode(
int offset)
const {
247 return current_offset() <= offset &&
248 offset < current_offset() + current_bytecode_size();
251 std::ostream& BytecodeArrayAccessor::PrintTo(std::ostream& os)
const {
252 const uint8_t* bytecode_addr =
reinterpret_cast<const uint8_t*
>(
253 bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_);
254 return BytecodeDecoder::Decode(os, bytecode_addr,
255 bytecode_array()->parameter_count());
258 JumpTableTargetOffsets::JumpTableTargetOffsets(
259 const BytecodeArrayAccessor* accessor,
int table_start,
int table_size,
261 : accessor_(accessor),
262 table_start_(table_start),
263 table_size_(table_size),
264 case_value_base_(case_value_base) {}
266 JumpTableTargetOffsets::iterator JumpTableTargetOffsets::begin()
const {
267 return iterator(case_value_base_, table_start_, table_start_ + table_size_,
270 JumpTableTargetOffsets::iterator JumpTableTargetOffsets::end()
const {
271 return iterator(case_value_base_ + table_size_, table_start_ + table_size_,
272 table_start_ + table_size_, accessor_);
274 int JumpTableTargetOffsets::size()
const {
277 for (
const auto& entry : *
this) {
284 JumpTableTargetOffsets::iterator::iterator(
285 int case_value,
int table_offset,
int table_end,
286 const BytecodeArrayAccessor* accessor)
287 : accessor_(accessor),
288 current_(Smi::zero()),
290 table_offset_(table_offset),
291 table_end_(table_end) {
292 UpdateAndAdvanceToValid();
295 JumpTableTargetOffset JumpTableTargetOffsets::iterator::operator*() {
296 DCHECK_LT(table_offset_, table_end_);
297 return {index_, accessor_->GetAbsoluteOffset(Smi::ToInt(current_))};
300 JumpTableTargetOffsets::iterator& JumpTableTargetOffsets::iterator::
302 DCHECK_LT(table_offset_, table_end_);
305 UpdateAndAdvanceToValid();
309 bool JumpTableTargetOffsets::iterator::operator!=(
310 const JumpTableTargetOffsets::iterator& other) {
311 DCHECK_EQ(accessor_, other.accessor_);
312 DCHECK_EQ(table_end_, other.table_end_);
313 DCHECK_EQ(index_ - other.index_, table_offset_ - other.table_offset_);
314 return index_ != other.index_;
317 void JumpTableTargetOffsets::iterator::UpdateAndAdvanceToValid() {
318 if (table_offset_ >= table_end_)
return;
320 Object* current = accessor_->GetConstantAtIndex(table_offset_);
321 while (!current->IsSmi()) {
322 DCHECK(current->IsTheHole());
325 if (table_offset_ >= table_end_)
break;
326 current = accessor_->GetConstantAtIndex(table_offset_);
329 if (current->IsSmi()) {
330 current_ = Smi::cast(current);