Line data Source code
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
3 : // found in the LICENSE file.
4 :
5 : #include "src/interpreter/bytecode-array-accessor.h"
6 :
7 : #include "src/interpreter/bytecode-decoder.h"
8 : #include "src/interpreter/interpreter-intrinsics.h"
9 : #include "src/objects-inl.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 : namespace interpreter {
14 :
15 973476 : BytecodeArrayAccessor::BytecodeArrayAccessor(
16 : Handle<BytecodeArray> bytecode_array, int initial_offset)
17 : : bytecode_array_(bytecode_array),
18 : bytecode_offset_(initial_offset),
19 : operand_scale_(OperandScale::kSingle),
20 973476 : prefix_offset_(0) {
21 973476 : UpdateOperandScale();
22 973476 : }
23 :
24 47211340 : void BytecodeArrayAccessor::SetOffset(int offset) {
25 47211340 : bytecode_offset_ = offset;
26 47211340 : UpdateOperandScale();
27 47211350 : }
28 :
29 48184813 : void BytecodeArrayAccessor::UpdateOperandScale() {
30 48184813 : if (OffsetInBounds()) {
31 : uint8_t current_byte = bytecode_array()->get(bytecode_offset_);
32 : Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
33 47314561 : if (Bytecodes::IsPrefixScalingBytecode(current_bytecode)) {
34 : operand_scale_ =
35 5748205 : Bytecodes::PrefixBytecodeToOperandScale(current_bytecode);
36 5748205 : prefix_offset_ = 1;
37 : } else {
38 41566356 : operand_scale_ = OperandScale::kSingle;
39 41566356 : prefix_offset_ = 0;
40 : }
41 : }
42 48184813 : }
43 :
44 0 : bool BytecodeArrayAccessor::OffsetInBounds() const {
45 96369628 : return bytecode_offset_ >= 0 && bytecode_offset_ < bytecode_array()->length();
46 : }
47 :
48 90862487 : Bytecode BytecodeArrayAccessor::current_bytecode() const {
49 : DCHECK(OffsetInBounds());
50 : uint8_t current_byte =
51 162291467 : bytecode_array()->get(bytecode_offset_ + current_prefix_offset());
52 : Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
53 : DCHECK(!Bytecodes::IsPrefixScalingBytecode(current_bytecode));
54 30655082 : return current_bytecode;
55 : }
56 :
57 30139882 : int BytecodeArrayAccessor::current_bytecode_size() const {
58 : return current_prefix_offset() +
59 30139882 : Bytecodes::Size(current_bytecode(), current_operand_scale());
60 : }
61 :
62 10330996 : uint32_t BytecodeArrayAccessor::GetUnsignedOperand(
63 30992990 : int operand_index, OperandType operand_type) const {
64 : DCHECK_GE(operand_index, 0);
65 : DCHECK_LT(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
66 : DCHECK_EQ(operand_type,
67 : Bytecodes::GetOperandType(current_bytecode(), operand_index));
68 : DCHECK(Bytecodes::IsUnsignedOperandType(operand_type));
69 : const uint8_t* operand_start =
70 10330996 : bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
71 20661992 : current_prefix_offset() +
72 : Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
73 10330996 : current_operand_scale());
74 : return BytecodeDecoder::DecodeUnsignedOperand(operand_start, operand_type,
75 10330998 : current_operand_scale());
76 : }
77 :
78 1405779 : int32_t BytecodeArrayAccessor::GetSignedOperand(
79 4217337 : int operand_index, OperandType operand_type) const {
80 : DCHECK_GE(operand_index, 0);
81 : DCHECK_LT(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
82 : DCHECK_EQ(operand_type,
83 : Bytecodes::GetOperandType(current_bytecode(), operand_index));
84 : DCHECK(!Bytecodes::IsUnsignedOperandType(operand_type));
85 : const uint8_t* operand_start =
86 1405779 : bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
87 2811558 : current_prefix_offset() +
88 : Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
89 1405779 : current_operand_scale());
90 : return BytecodeDecoder::DecodeSignedOperand(operand_start, operand_type,
91 1405779 : current_operand_scale());
92 : }
93 :
94 380674 : uint32_t BytecodeArrayAccessor::GetFlagOperand(int operand_index) const {
95 : DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
96 : OperandType::kFlag8);
97 380674 : return GetUnsignedOperand(operand_index, OperandType::kFlag8);
98 : }
99 :
100 177989 : uint32_t BytecodeArrayAccessor::GetUnsignedImmediateOperand(
101 : int operand_index) const {
102 : DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
103 : OperandType::kUImm);
104 2006554 : return GetUnsignedOperand(operand_index, OperandType::kUImm);
105 : }
106 :
107 1405779 : int32_t BytecodeArrayAccessor::GetImmediateOperand(int operand_index) const {
108 : DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
109 : OperandType::kImm);
110 1405779 : return GetSignedOperand(operand_index, OperandType::kImm);
111 : }
112 :
113 782574 : uint32_t BytecodeArrayAccessor::GetRegisterCountOperand(
114 : int operand_index) const {
115 : DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
116 : OperandType::kRegCount);
117 782578 : return GetUnsignedOperand(operand_index, OperandType::kRegCount);
118 : }
119 :
120 6894333 : uint32_t BytecodeArrayAccessor::GetIndexOperand(int operand_index) const {
121 : OperandType operand_type =
122 : Bytecodes::GetOperandType(current_bytecode(), operand_index);
123 : DCHECK_EQ(operand_type, OperandType::kIdx);
124 6894333 : return GetUnsignedOperand(operand_index, operand_type);
125 : }
126 :
127 61527396 : Register BytecodeArrayAccessor::GetRegisterOperand(int operand_index) const {
128 : OperandType operand_type =
129 : Bytecodes::GetOperandType(current_bytecode(), operand_index);
130 : const uint8_t* operand_start =
131 20509134 : bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
132 20509134 : current_prefix_offset() +
133 : Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
134 20509134 : current_operand_scale());
135 : return BytecodeDecoder::DecodeRegisterOperand(operand_start, operand_type,
136 20509128 : current_operand_scale());
137 : }
138 :
139 43 : int BytecodeArrayAccessor::GetRegisterOperandRange(int operand_index) const {
140 : DCHECK_LE(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
141 : const OperandType* operand_types =
142 : Bytecodes::GetOperandTypes(current_bytecode());
143 43 : OperandType operand_type = operand_types[operand_index];
144 : DCHECK(Bytecodes::IsRegisterOperandType(operand_type));
145 43 : if (operand_type == OperandType::kRegList) {
146 8 : return GetRegisterCountOperand(operand_index + 1);
147 : } else {
148 39 : return Bytecodes::GetNumberOfRegistersRepresentedBy(operand_type);
149 : }
150 : }
151 :
152 209208 : Runtime::FunctionId BytecodeArrayAccessor::GetRuntimeIdOperand(
153 : int operand_index) const {
154 : OperandType operand_type =
155 : Bytecodes::GetOperandType(current_bytecode(), operand_index);
156 : DCHECK(operand_type == OperandType::kRuntimeId);
157 209208 : uint32_t raw_id = GetUnsignedOperand(operand_index, operand_type);
158 209208 : return static_cast<Runtime::FunctionId>(raw_id);
159 : }
160 :
161 57674 : Runtime::FunctionId BytecodeArrayAccessor::GetIntrinsicIdOperand(
162 : int operand_index) const {
163 : OperandType operand_type =
164 : Bytecodes::GetOperandType(current_bytecode(), operand_index);
165 : DCHECK(operand_type == OperandType::kIntrinsicId);
166 57674 : uint32_t raw_id = GetUnsignedOperand(operand_index, operand_type);
167 : return IntrinsicsHelper::ToRuntimeId(
168 57674 : static_cast<IntrinsicsHelper::IntrinsicId>(raw_id));
169 : }
170 :
171 2277412 : Handle<Object> BytecodeArrayAccessor::GetConstantForIndexOperand(
172 : int operand_index) const {
173 : return FixedArray::get(bytecode_array()->constant_pool(),
174 2277412 : GetIndexOperand(operand_index),
175 4554826 : bytecode_array()->GetIsolate());
176 : }
177 :
178 5645795 : int BytecodeArrayAccessor::GetJumpTargetOffset() const {
179 : Bytecode bytecode = current_bytecode();
180 1881931 : if (interpreter::Bytecodes::IsJumpImmediate(bytecode)) {
181 1828566 : int relative_offset = GetUnsignedImmediateOperand(0);
182 1828566 : if (bytecode == Bytecode::kJumpLoop) {
183 157003 : relative_offset = -relative_offset;
184 : }
185 3657132 : return current_offset() + relative_offset + current_prefix_offset();
186 53366 : } else if (interpreter::Bytecodes::IsJumpConstant(bytecode)) {
187 106732 : Smi* smi = Smi::cast(*GetConstantForIndexOperand(0));
188 106732 : return current_offset() + smi->value() + current_prefix_offset();
189 : } else {
190 0 : UNREACHABLE();
191 : return kMinInt;
192 : }
193 : }
194 :
195 0 : bool BytecodeArrayAccessor::OffsetWithinBytecode(int offset) const {
196 0 : return current_offset() <= offset &&
197 0 : offset < current_offset() + current_bytecode_size();
198 : }
199 :
200 51 : std::ostream& BytecodeArrayAccessor::PrintTo(std::ostream& os) const {
201 : return BytecodeDecoder::Decode(
202 51 : os, bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_,
203 51 : bytecode_array()->parameter_count());
204 : }
205 :
206 : } // namespace interpreter
207 : } // namespace internal
208 : } // namespace v8
|