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