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 971266 : 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 971266 : prefix_offset_(0) {
21 971266 : UpdateOperandScale();
22 971267 : }
23 :
24 47248677 : void BytecodeArrayAccessor::SetOffset(int offset) {
25 47248677 : bytecode_offset_ = offset;
26 47248677 : UpdateOperandScale();
27 47248693 : }
28 :
29 48219933 : void BytecodeArrayAccessor::UpdateOperandScale() {
30 48219933 : if (OffsetInBounds()) {
31 : uint8_t current_byte = bytecode_array()->get(bytecode_offset_);
32 : Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
33 47351898 : if (Bytecodes::IsPrefixScalingBytecode(current_bytecode)) {
34 : operand_scale_ =
35 5767645 : Bytecodes::PrefixBytecodeToOperandScale(current_bytecode);
36 5767645 : prefix_offset_ = 1;
37 : } else {
38 41584253 : operand_scale_ = OperandScale::kSingle;
39 41584253 : prefix_offset_ = 0;
40 : }
41 : }
42 48219933 : }
43 :
44 0 : bool BytecodeArrayAccessor::OffsetInBounds() const {
45 96439869 : return bytecode_offset_ >= 0 && bytecode_offset_ < bytecode_array()->length();
46 : }
47 :
48 90936788 : Bytecode BytecodeArrayAccessor::current_bytecode() const {
49 : DCHECK(OffsetInBounds());
50 : uint8_t current_byte =
51 162414684 : bytecode_array()->get(bytecode_offset_ + current_prefix_offset());
52 : Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
53 : DCHECK(!Bytecodes::IsPrefixScalingBytecode(current_bytecode));
54 30680909 : return current_bytecode;
55 : }
56 :
57 30159441 : int BytecodeArrayAccessor::current_bytecode_size() const {
58 : return current_prefix_offset() +
59 30159441 : Bytecodes::Size(current_bytecode(), current_operand_scale());
60 : }
61 :
62 10338913 : uint32_t BytecodeArrayAccessor::GetUnsignedOperand(
63 31016735 : 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 10338913 : bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
71 20677826 : current_prefix_offset() +
72 : Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
73 10338913 : current_operand_scale());
74 : return BytecodeDecoder::DecodeUnsignedOperand(operand_start, operand_type,
75 10338909 : current_operand_scale());
76 : }
77 :
78 1404572 : int32_t BytecodeArrayAccessor::GetSignedOperand(
79 4213716 : 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 1404572 : bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
87 2809144 : current_prefix_offset() +
88 : Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
89 1404572 : current_operand_scale());
90 : return BytecodeDecoder::DecodeSignedOperand(operand_start, operand_type,
91 1404572 : current_operand_scale());
92 : }
93 :
94 380757 : uint32_t BytecodeArrayAccessor::GetFlagOperand(int operand_index) const {
95 : DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
96 : OperandType::kFlag8);
97 380757 : return GetUnsignedOperand(operand_index, OperandType::kFlag8);
98 : }
99 :
100 178130 : uint32_t BytecodeArrayAccessor::GetUnsignedImmediateOperand(
101 : int operand_index) const {
102 : DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
103 : OperandType::kUImm);
104 2012053 : return GetUnsignedOperand(operand_index, OperandType::kUImm);
105 : }
106 :
107 1404572 : int32_t BytecodeArrayAccessor::GetImmediateOperand(int operand_index) const {
108 : DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
109 : OperandType::kImm);
110 1404572 : return GetSignedOperand(operand_index, OperandType::kImm);
111 : }
112 :
113 784672 : uint32_t BytecodeArrayAccessor::GetRegisterCountOperand(
114 : int operand_index) const {
115 : DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
116 : OperandType::kRegCount);
117 784676 : return GetUnsignedOperand(operand_index, OperandType::kRegCount);
118 : }
119 :
120 6893730 : 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 6893730 : return GetUnsignedOperand(operand_index, operand_type);
125 : }
126 :
127 61577670 : 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 20525895 : bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
132 20525895 : current_prefix_offset() +
133 : Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
134 20525895 : current_operand_scale());
135 : return BytecodeDecoder::DecodeRegisterOperand(operand_start, operand_type,
136 20525880 : 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 209766 : 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 209766 : uint32_t raw_id = GetUnsignedOperand(operand_index, operand_type);
158 209766 : return static_cast<Runtime::FunctionId>(raw_id);
159 : }
160 :
161 57959 : 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 57959 : uint32_t raw_id = GetUnsignedOperand(operand_index, operand_type);
167 : return IntrinsicsHelper::ToRuntimeId(
168 57959 : static_cast<IntrinsicsHelper::IntrinsicId>(raw_id));
169 : }
170 :
171 2277162 : Handle<Object> BytecodeArrayAccessor::GetConstantForIndexOperand(
172 : int operand_index) const {
173 : return FixedArray::get(bytecode_array()->constant_pool(),
174 2277162 : GetIndexOperand(operand_index),
175 4554322 : bytecode_array()->GetIsolate());
176 : }
177 :
178 5662731 : int BytecodeArrayAccessor::GetJumpTargetOffset() const {
179 : Bytecode bytecode = current_bytecode();
180 1887577 : if (interpreter::Bytecodes::IsJumpImmediate(bytecode)) {
181 1833923 : int relative_offset = GetUnsignedImmediateOperand(0);
182 1833923 : if (bytecode == Bytecode::kJumpLoop) {
183 157299 : relative_offset = -relative_offset;
184 : }
185 3667846 : return current_offset() + relative_offset + current_prefix_offset();
186 53654 : } else if (interpreter::Bytecodes::IsJumpConstant(bytecode)) {
187 107308 : Smi* smi = Smi::cast(*GetConstantForIndexOperand(0));
188 107308 : 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
|