blob: a4a8f799a3019bbacb28b6ea24913f87ac5aced2 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
3// found in the LICENSE file.
4
5#include "src/interpreter/bytecode-array-iterator.h"
6
Ben Murdoch61f157c2016-09-16 13:49:30 +01007#include "src/interpreter/interpreter-intrinsics.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include "src/objects-inl.h"
9
10namespace v8 {
11namespace internal {
12namespace interpreter {
13
14BytecodeArrayIterator::BytecodeArrayIterator(
15 Handle<BytecodeArray> bytecode_array)
Ben Murdochda12d292016-06-02 14:46:10 +010016 : bytecode_array_(bytecode_array),
17 bytecode_offset_(0),
18 operand_scale_(OperandScale::kSingle),
19 prefix_offset_(0) {
20 UpdateOperandScale();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000021}
22
Ben Murdochda12d292016-06-02 14:46:10 +010023void BytecodeArrayIterator::Advance() {
24 bytecode_offset_ += current_bytecode_size();
25 UpdateOperandScale();
26}
27
28void BytecodeArrayIterator::UpdateOperandScale() {
29 if (!done()) {
30 uint8_t current_byte = bytecode_array()->get(bytecode_offset_);
31 Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
32 if (Bytecodes::IsPrefixScalingBytecode(current_bytecode)) {
33 operand_scale_ =
34 Bytecodes::PrefixBytecodeToOperandScale(current_bytecode);
35 prefix_offset_ = 1;
36 } else {
37 operand_scale_ = OperandScale::kSingle;
38 prefix_offset_ = 0;
39 }
40 }
41}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042
43bool BytecodeArrayIterator::done() const {
44 return bytecode_offset_ >= bytecode_array()->length();
45}
46
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047Bytecode BytecodeArrayIterator::current_bytecode() const {
48 DCHECK(!done());
Ben Murdochda12d292016-06-02 14:46:10 +010049 uint8_t current_byte =
50 bytecode_array()->get(bytecode_offset_ + current_prefix_offset());
51 Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
52 DCHECK(!Bytecodes::IsPrefixScalingBytecode(current_bytecode));
53 return current_bytecode;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054}
55
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056int BytecodeArrayIterator::current_bytecode_size() const {
Ben Murdochda12d292016-06-02 14:46:10 +010057 return current_prefix_offset() +
58 Bytecodes::Size(current_bytecode(), current_operand_scale());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000059}
60
Ben Murdochda12d292016-06-02 14:46:10 +010061uint32_t BytecodeArrayIterator::GetUnsignedOperand(
62 int operand_index, OperandType operand_type) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000063 DCHECK_GE(operand_index, 0);
64 DCHECK_LT(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
65 DCHECK_EQ(operand_type,
66 Bytecodes::GetOperandType(current_bytecode(), operand_index));
Ben Murdochda12d292016-06-02 14:46:10 +010067 DCHECK(Bytecodes::IsUnsignedOperandType(operand_type));
68 const uint8_t* operand_start =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000069 bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
Ben Murdochda12d292016-06-02 14:46:10 +010070 current_prefix_offset() +
71 Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
72 current_operand_scale());
73 return Bytecodes::DecodeUnsignedOperand(operand_start, operand_type,
74 current_operand_scale());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075}
76
Ben Murdochda12d292016-06-02 14:46:10 +010077int32_t BytecodeArrayIterator::GetSignedOperand(
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 const uint8_t* 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 Bytecodes::DecodeSignedOperand(operand_start, operand_type,
90 current_operand_scale());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000091}
92
Ben Murdochda12d292016-06-02 14:46:10 +010093uint32_t BytecodeArrayIterator::GetFlagOperand(int operand_index) const {
94 DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
95 OperandType::kFlag8);
96 return GetUnsignedOperand(operand_index, OperandType::kFlag8);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097}
98
Ben Murdochda12d292016-06-02 14:46:10 +010099int32_t BytecodeArrayIterator::GetImmediateOperand(int operand_index) const {
100 DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
101 OperandType::kImm);
102 return GetSignedOperand(operand_index, OperandType::kImm);
103}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000104
Ben Murdochda12d292016-06-02 14:46:10 +0100105uint32_t BytecodeArrayIterator::GetRegisterCountOperand(
106 int operand_index) const {
107 DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
108 OperandType::kRegCount);
109 return GetUnsignedOperand(operand_index, OperandType::kRegCount);
110}
111
112uint32_t BytecodeArrayIterator::GetIndexOperand(int operand_index) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113 OperandType operand_type =
114 Bytecodes::GetOperandType(current_bytecode(), operand_index);
Ben Murdochda12d292016-06-02 14:46:10 +0100115 DCHECK_EQ(operand_type, OperandType::kIdx);
116 return GetUnsignedOperand(operand_index, operand_type);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000117}
118
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119Register BytecodeArrayIterator::GetRegisterOperand(int operand_index) const {
120 OperandType operand_type =
121 Bytecodes::GetOperandType(current_bytecode(), operand_index);
Ben Murdochda12d292016-06-02 14:46:10 +0100122 const uint8_t* operand_start =
123 bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
124 current_prefix_offset() +
125 Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
126 current_operand_scale());
127 return Bytecodes::DecodeRegisterOperand(operand_start, operand_type,
128 current_operand_scale());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129}
130
Ben Murdoch097c5b22016-05-18 11:27:45 +0100131int BytecodeArrayIterator::GetRegisterOperandRange(int operand_index) const {
Ben Murdochc5610432016-08-08 18:44:38 +0100132 DCHECK_LE(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
133 const OperandType* operand_types =
134 Bytecodes::GetOperandTypes(current_bytecode());
135 DCHECK(Bytecodes::IsRegisterOperandType(operand_types[operand_index]));
136 if (operand_types[operand_index + 1] == OperandType::kRegCount) {
137 return GetRegisterCountOperand(operand_index + 1);
138 } else {
139 OperandType operand_type = operand_types[operand_index];
140 return Bytecodes::GetNumberOfRegistersRepresentedBy(operand_type);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100141 }
142}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000143
Ben Murdoch61f157c2016-09-16 13:49:30 +0100144Runtime::FunctionId BytecodeArrayIterator::GetRuntimeIdOperand(
145 int operand_index) const {
Ben Murdochda12d292016-06-02 14:46:10 +0100146 OperandType operand_type =
147 Bytecodes::GetOperandType(current_bytecode(), operand_index);
148 DCHECK(operand_type == OperandType::kRuntimeId);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100149 uint32_t raw_id = GetUnsignedOperand(operand_index, operand_type);
150 return static_cast<Runtime::FunctionId>(raw_id);
151}
152
153Runtime::FunctionId BytecodeArrayIterator::GetIntrinsicIdOperand(
154 int operand_index) const {
155 OperandType operand_type =
156 Bytecodes::GetOperandType(current_bytecode(), operand_index);
157 DCHECK(operand_type == OperandType::kIntrinsicId);
158 uint32_t raw_id = GetUnsignedOperand(operand_index, operand_type);
159 return IntrinsicsHelper::ToRuntimeId(
160 static_cast<IntrinsicsHelper::IntrinsicId>(raw_id));
Ben Murdochda12d292016-06-02 14:46:10 +0100161}
162
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000163Handle<Object> BytecodeArrayIterator::GetConstantForIndexOperand(
164 int operand_index) const {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100165 return FixedArray::get(bytecode_array()->constant_pool(),
166 GetIndexOperand(operand_index),
167 bytecode_array()->GetIsolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000168}
169
170
171int BytecodeArrayIterator::GetJumpTargetOffset() const {
172 Bytecode bytecode = current_bytecode();
173 if (interpreter::Bytecodes::IsJumpImmediate(bytecode)) {
174 int relative_offset = GetImmediateOperand(0);
Ben Murdochda12d292016-06-02 14:46:10 +0100175 return current_offset() + relative_offset + current_prefix_offset();
176 } else if (interpreter::Bytecodes::IsJumpConstant(bytecode)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000177 Smi* smi = Smi::cast(*GetConstantForIndexOperand(0));
Ben Murdochda12d292016-06-02 14:46:10 +0100178 return current_offset() + smi->value() + current_prefix_offset();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000179 } else {
180 UNREACHABLE();
181 return kMinInt;
182 }
183}
184
185} // namespace interpreter
186} // namespace internal
187} // namespace v8