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