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-writer.h" | 5 #include "src/interpreter/bytecode-array-writer.h" |
6 | 6 |
7 #include "src/api.h" | 7 #include "src/api.h" |
8 #include "src/interpreter/bytecode-label.h" | 8 #include "src/interpreter/bytecode-label.h" |
9 #include "src/interpreter/constant-array-builder.h" | 9 #include "src/interpreter/constant-array-builder.h" |
10 #include "src/log.h" | 10 #include "src/log.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 const BytecodeNode* const node) { | 98 const BytecodeNode* const node) { |
99 int bytecode_offset = static_cast<int>(bytecodes()->size()); | 99 int bytecode_offset = static_cast<int>(bytecodes()->size()); |
100 const BytecodeSourceInfo& source_info = node->source_info(); | 100 const BytecodeSourceInfo& source_info = node->source_info(); |
101 if (source_info.is_valid()) { | 101 if (source_info.is_valid()) { |
102 source_position_table_builder()->AddPosition(bytecode_offset, | 102 source_position_table_builder()->AddPosition(bytecode_offset, |
103 source_info.source_position(), | 103 source_info.source_position(), |
104 source_info.is_statement()); | 104 source_info.is_statement()); |
105 } | 105 } |
106 } | 106 } |
107 | 107 |
108 namespace { | |
109 | |
110 OperandScale GetOperandScale(const BytecodeNode* const node) { | |
111 static const OperandTypeInfo kTypeInfo[] = { | |
112 #define VALUE(Name, Info) Info, | |
113 OPERAND_TYPE_LIST(VALUE) | |
114 #undef VALUE | |
115 }; | |
116 | |
117 OperandSize operand_size = OperandSize::kByte; | |
118 const int operand_count = node->operand_count(); | |
119 const OperandType* operand_types = | |
120 Bytecodes::GetOperandTypes(node->bytecode()); | |
rmcilroy
2016/06/07 10:32:46
Could we just add Bytecodes::GetOperandTypeInfos i
oth
2016/06/08 15:08:41
Done.
| |
121 for (int i = 0; i < operand_count; ++i) { | |
122 OperandType operand_type = operand_types[i]; | |
123 OperandTypeInfo operand_type_info = | |
124 kTypeInfo[static_cast<size_t>(operand_type)]; | |
125 switch (operand_type_info) { | |
126 case OperandTypeInfo::kNone: | |
127 UNREACHABLE(); | |
128 break; | |
129 case OperandTypeInfo::kScalableSignedByte: { | |
130 int32_t signed_operand = static_cast<int32_t>(node->operand(i)); | |
131 operand_size = std::max( | |
132 operand_size, Bytecodes::SizeForSignedOperand(signed_operand)); | |
133 break; | |
134 } | |
135 case OperandTypeInfo::kScalableUnsignedByte: { | |
136 uint32_t unsigned_operand = node->operand(i); | |
137 operand_size = std::max( | |
138 operand_size, Bytecodes::SizeForUnsignedOperand(unsigned_operand)); | |
139 break; | |
140 } | |
141 case OperandTypeInfo::kFixedUnsignedByte: { | |
142 DCHECK_EQ(Bytecodes::SizeForUnsignedOperand(node->operand(i)), | |
143 OperandSize::kByte); | |
144 break; | |
145 } | |
146 case OperandTypeInfo::kFixedUnsignedShort: { | |
147 DCHECK_EQ(Bytecodes::SizeForUnsignedOperand(node->operand(i)), | |
148 OperandSize::kShort); | |
149 break; | |
150 } | |
151 } | |
152 } | |
153 return Bytecodes::OperandSizesToScale(operand_size); | |
154 } | |
155 | |
156 } // namespace | |
157 | |
108 void BytecodeArrayWriter::EmitBytecode(const BytecodeNode* const node) { | 158 void BytecodeArrayWriter::EmitBytecode(const BytecodeNode* const node) { |
109 DCHECK_NE(node->bytecode(), Bytecode::kIllegal); | 159 DCHECK_NE(node->bytecode(), Bytecode::kIllegal); |
110 | 160 |
111 OperandScale operand_scale = node->operand_scale(); | 161 OperandScale operand_scale = GetOperandScale(node); |
112 if (operand_scale != OperandScale::kSingle) { | 162 if (operand_scale != OperandScale::kSingle) { |
113 Bytecode prefix = Bytecodes::OperandScaleToPrefixBytecode(operand_scale); | 163 Bytecode prefix = Bytecodes::OperandScaleToPrefixBytecode(operand_scale); |
114 bytecodes()->push_back(Bytecodes::ToByte(prefix)); | 164 bytecodes()->push_back(Bytecodes::ToByte(prefix)); |
115 } | 165 } |
116 | 166 |
117 Bytecode bytecode = node->bytecode(); | 167 Bytecode bytecode = node->bytecode(); |
118 bytecodes()->push_back(Bytecodes::ToByte(bytecode)); | 168 bytecodes()->push_back(Bytecodes::ToByte(bytecode)); |
119 | 169 |
120 int register_operand_bitmap = Bytecodes::GetRegisterOperandBitmap(bytecode); | 170 int register_operand_bitmap = Bytecodes::GetRegisterOperandBitmap(bytecode); |
121 const uint32_t* const operands = node->operands(); | 171 const uint32_t* const operands = node->operands(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 count = static_cast<int>(operands[i + 1]); | 203 count = static_cast<int>(operands[i + 1]); |
154 } else { | 204 } else { |
155 count = Bytecodes::GetNumberOfRegistersRepresentedBy(operand_type); | 205 count = Bytecodes::GetNumberOfRegistersRepresentedBy(operand_type); |
156 } | 206 } |
157 Register reg = Register::FromOperand(static_cast<int32_t>(operands[i])); | 207 Register reg = Register::FromOperand(static_cast<int32_t>(operands[i])); |
158 max_register_count_ = std::max(max_register_count_, reg.index() + count); | 208 max_register_count_ = std::max(max_register_count_, reg.index() + count); |
159 } | 209 } |
160 } | 210 } |
161 } | 211 } |
162 | 212 |
163 // TODO(rmcilroy): This is the same as SignedOperand in BytecodeArrayBuilder. | |
164 // Once we move the scalable operand processing here remove the SignedOperand | |
165 // in BytecodeArrayBuilder. | |
166 static uint32_t SignedOperand(int value, OperandSize size) { | |
167 switch (size) { | |
168 case OperandSize::kByte: | |
169 return static_cast<uint8_t>(value & 0xff); | |
170 case OperandSize::kShort: | |
171 return static_cast<uint16_t>(value & 0xffff); | |
172 case OperandSize::kQuad: | |
173 return static_cast<uint32_t>(value); | |
174 case OperandSize::kNone: | |
175 UNREACHABLE(); | |
176 } | |
177 return 0; | |
178 } | |
179 | |
180 // static | 213 // static |
181 Bytecode GetJumpWithConstantOperand(Bytecode jump_bytecode) { | 214 Bytecode GetJumpWithConstantOperand(Bytecode jump_bytecode) { |
182 switch (jump_bytecode) { | 215 switch (jump_bytecode) { |
183 case Bytecode::kJump: | 216 case Bytecode::kJump: |
184 return Bytecode::kJumpConstant; | 217 return Bytecode::kJumpConstant; |
185 case Bytecode::kJumpIfTrue: | 218 case Bytecode::kJumpIfTrue: |
186 return Bytecode::kJumpIfTrueConstant; | 219 return Bytecode::kJumpIfTrueConstant; |
187 case Bytecode::kJumpIfFalse: | 220 case Bytecode::kJumpIfFalse: |
188 return Bytecode::kJumpIfFalseConstant; | 221 return Bytecode::kJumpIfFalseConstant; |
189 case Bytecode::kJumpIfToBooleanTrue: | 222 case Bytecode::kJumpIfToBooleanTrue: |
(...skipping 10 matching lines...) Expand all Loading... | |
200 UNREACHABLE(); | 233 UNREACHABLE(); |
201 return Bytecode::kIllegal; | 234 return Bytecode::kIllegal; |
202 } | 235 } |
203 } | 236 } |
204 | 237 |
205 void BytecodeArrayWriter::PatchJumpWith8BitOperand(size_t jump_location, | 238 void BytecodeArrayWriter::PatchJumpWith8BitOperand(size_t jump_location, |
206 int delta) { | 239 int delta) { |
207 Bytecode jump_bytecode = Bytecodes::FromByte(bytecodes()->at(jump_location)); | 240 Bytecode jump_bytecode = Bytecodes::FromByte(bytecodes()->at(jump_location)); |
208 DCHECK(Bytecodes::IsJumpImmediate(jump_bytecode)); | 241 DCHECK(Bytecodes::IsJumpImmediate(jump_bytecode)); |
209 size_t operand_location = jump_location + 1; | 242 size_t operand_location = jump_location + 1; |
210 DCHECK_EQ(bytecodes()->at(operand_location), 0); | 243 DCHECK_EQ(bytecodes()->at(operand_location), k8BitPlaceholder); |
211 if (Bytecodes::SizeForSignedOperand(delta) == OperandSize::kByte) { | 244 if (Bytecodes::SizeForSignedOperand(delta) == OperandSize::kByte) { |
212 // The jump fits within the range of an Imm operand, so cancel | 245 // The jump fits within the range of an Imm operand, so cancel |
213 // the reservation and jump directly. | 246 // the reservation and jump directly. |
214 constant_array_builder()->DiscardReservedEntry(OperandSize::kByte); | 247 constant_array_builder()->DiscardReservedEntry(OperandSize::kByte); |
215 bytecodes()->at(operand_location) = static_cast<uint8_t>(delta); | 248 bytecodes()->at(operand_location) = static_cast<uint8_t>(delta); |
216 } else { | 249 } else { |
217 // The jump does not fit within the range of an Imm operand, so | 250 // The jump does not fit within the range of an Imm operand, so |
218 // commit reservation putting the offset into the constant pool, | 251 // commit reservation putting the offset into the constant pool, |
219 // and update the jump instruction and operand. | 252 // and update the jump instruction and operand. |
220 size_t entry = constant_array_builder()->CommitReservedEntry( | 253 size_t entry = constant_array_builder()->CommitReservedEntry( |
221 OperandSize::kByte, handle(Smi::FromInt(delta), isolate())); | 254 OperandSize::kByte, handle(Smi::FromInt(delta), isolate())); |
222 DCHECK(Bytecodes::SizeForUnsignedOperand(entry) == OperandSize::kByte); | 255 DCHECK_LE(entry, kMaxUInt32); |
256 DCHECK_EQ(Bytecodes::SizeForUnsignedOperand(static_cast<uint32_t>(entry)), | |
257 OperandSize::kByte); | |
223 jump_bytecode = GetJumpWithConstantOperand(jump_bytecode); | 258 jump_bytecode = GetJumpWithConstantOperand(jump_bytecode); |
224 bytecodes()->at(jump_location) = Bytecodes::ToByte(jump_bytecode); | 259 bytecodes()->at(jump_location) = Bytecodes::ToByte(jump_bytecode); |
225 bytecodes()->at(operand_location) = static_cast<uint8_t>(entry); | 260 bytecodes()->at(operand_location) = static_cast<uint8_t>(entry); |
226 } | 261 } |
227 } | 262 } |
228 | 263 |
229 void BytecodeArrayWriter::PatchJumpWith16BitOperand(size_t jump_location, | 264 void BytecodeArrayWriter::PatchJumpWith16BitOperand(size_t jump_location, |
230 int delta) { | 265 int delta) { |
231 Bytecode jump_bytecode = Bytecodes::FromByte(bytecodes()->at(jump_location)); | 266 Bytecode jump_bytecode = Bytecodes::FromByte(bytecodes()->at(jump_location)); |
232 DCHECK(Bytecodes::IsJumpImmediate(jump_bytecode)); | 267 DCHECK(Bytecodes::IsJumpImmediate(jump_bytecode)); |
233 size_t operand_location = jump_location + 1; | 268 size_t operand_location = jump_location + 1; |
234 uint8_t operand_bytes[2]; | 269 uint8_t operand_bytes[2]; |
235 if (Bytecodes::SizeForSignedOperand(delta) <= OperandSize::kShort) { | 270 if (Bytecodes::SizeForSignedOperand(delta) <= OperandSize::kShort) { |
236 constant_array_builder()->DiscardReservedEntry(OperandSize::kShort); | 271 constant_array_builder()->DiscardReservedEntry(OperandSize::kShort); |
237 WriteUnalignedUInt16(operand_bytes, static_cast<uint16_t>(delta)); | 272 WriteUnalignedUInt16(operand_bytes, static_cast<uint16_t>(delta)); |
238 } else { | 273 } else { |
239 jump_bytecode = GetJumpWithConstantOperand(jump_bytecode); | 274 jump_bytecode = GetJumpWithConstantOperand(jump_bytecode); |
240 bytecodes()->at(jump_location) = Bytecodes::ToByte(jump_bytecode); | 275 bytecodes()->at(jump_location) = Bytecodes::ToByte(jump_bytecode); |
241 size_t entry = constant_array_builder()->CommitReservedEntry( | 276 size_t entry = constant_array_builder()->CommitReservedEntry( |
242 OperandSize::kShort, handle(Smi::FromInt(delta), isolate())); | 277 OperandSize::kShort, handle(Smi::FromInt(delta), isolate())); |
243 WriteUnalignedUInt16(operand_bytes, static_cast<uint16_t>(entry)); | 278 WriteUnalignedUInt16(operand_bytes, static_cast<uint16_t>(entry)); |
244 } | 279 } |
245 DCHECK(bytecodes()->at(operand_location) == 0 && | 280 DCHECK(bytecodes()->at(operand_location) == k8BitPlaceholder && |
246 bytecodes()->at(operand_location + 1) == 0); | 281 bytecodes()->at(operand_location + 1) == k8BitPlaceholder); |
247 bytecodes()->at(operand_location++) = operand_bytes[0]; | 282 bytecodes()->at(operand_location++) = operand_bytes[0]; |
248 bytecodes()->at(operand_location) = operand_bytes[1]; | 283 bytecodes()->at(operand_location) = operand_bytes[1]; |
249 } | 284 } |
250 | 285 |
251 void BytecodeArrayWriter::PatchJumpWith32BitOperand(size_t jump_location, | 286 void BytecodeArrayWriter::PatchJumpWith32BitOperand(size_t jump_location, |
252 int delta) { | 287 int delta) { |
253 DCHECK(Bytecodes::IsJumpImmediate( | 288 DCHECK(Bytecodes::IsJumpImmediate( |
254 Bytecodes::FromByte(bytecodes()->at(jump_location)))); | 289 Bytecodes::FromByte(bytecodes()->at(jump_location)))); |
255 constant_array_builder()->DiscardReservedEntry(OperandSize::kQuad); | 290 constant_array_builder()->DiscardReservedEntry(OperandSize::kQuad); |
256 uint8_t operand_bytes[4]; | 291 uint8_t operand_bytes[4]; |
257 WriteUnalignedUInt32(operand_bytes, static_cast<uint32_t>(delta)); | 292 WriteUnalignedUInt32(operand_bytes, static_cast<uint32_t>(delta)); |
258 size_t operand_location = jump_location + 1; | 293 size_t operand_location = jump_location + 1; |
259 DCHECK(bytecodes()->at(operand_location) == 0 && | 294 DCHECK(bytecodes()->at(operand_location) == k8BitPlaceholder && |
260 bytecodes()->at(operand_location + 1) == 0 && | 295 bytecodes()->at(operand_location + 1) == k8BitPlaceholder && |
261 bytecodes()->at(operand_location + 2) == 0 && | 296 bytecodes()->at(operand_location + 2) == k8BitPlaceholder && |
262 bytecodes()->at(operand_location + 3) == 0); | 297 bytecodes()->at(operand_location + 3) == k8BitPlaceholder); |
263 bytecodes()->at(operand_location++) = operand_bytes[0]; | 298 bytecodes()->at(operand_location++) = operand_bytes[0]; |
264 bytecodes()->at(operand_location++) = operand_bytes[1]; | 299 bytecodes()->at(operand_location++) = operand_bytes[1]; |
265 bytecodes()->at(operand_location++) = operand_bytes[2]; | 300 bytecodes()->at(operand_location++) = operand_bytes[2]; |
266 bytecodes()->at(operand_location) = operand_bytes[3]; | 301 bytecodes()->at(operand_location) = operand_bytes[3]; |
267 } | 302 } |
268 | 303 |
269 void BytecodeArrayWriter::PatchJump(size_t jump_target, size_t jump_location) { | 304 void BytecodeArrayWriter::PatchJump(size_t jump_target, size_t jump_location) { |
270 Bytecode jump_bytecode = Bytecodes::FromByte(bytecodes()->at(jump_location)); | 305 Bytecode jump_bytecode = Bytecodes::FromByte(bytecodes()->at(jump_location)); |
271 int delta = static_cast<int>(jump_target - jump_location); | 306 int delta = static_cast<int>(jump_target - jump_location); |
272 int prefix_offset = 0; | 307 int prefix_offset = 0; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
309 CHECK_LE(current_offset, static_cast<size_t>(kMaxInt)); | 344 CHECK_LE(current_offset, static_cast<size_t>(kMaxInt)); |
310 // Label has been bound already so this is a backwards jump. | 345 // Label has been bound already so this is a backwards jump. |
311 size_t abs_delta = current_offset - label->offset(); | 346 size_t abs_delta = current_offset - label->offset(); |
312 int delta = -static_cast<int>(abs_delta); | 347 int delta = -static_cast<int>(abs_delta); |
313 OperandSize operand_size = Bytecodes::SizeForSignedOperand(delta); | 348 OperandSize operand_size = Bytecodes::SizeForSignedOperand(delta); |
314 if (operand_size > OperandSize::kByte) { | 349 if (operand_size > OperandSize::kByte) { |
315 // Adjust for scaling byte prefix for wide jump offset. | 350 // Adjust for scaling byte prefix for wide jump offset. |
316 DCHECK_LE(delta, 0); | 351 DCHECK_LE(delta, 0); |
317 delta -= 1; | 352 delta -= 1; |
318 } | 353 } |
319 node->set_bytecode(node->bytecode(), SignedOperand(delta, operand_size), | 354 node->set_bytecode(node->bytecode(), delta); |
320 Bytecodes::OperandSizesToScale(operand_size)); | |
321 } else { | 355 } else { |
322 // The label has not yet been bound so this is a forward reference | 356 // The label has not yet been bound so this is a forward reference |
323 // that will be patched when the label is bound. We create a | 357 // that will be patched when the label is bound. We create a |
324 // reservation in the constant pool so the jump can be patched | 358 // reservation in the constant pool so the jump can be patched |
325 // when the label is bound. The reservation means the maximum size | 359 // when the label is bound. The reservation means the maximum size |
326 // of the operand for the constant is known and the jump can | 360 // of the operand for the constant is known and the jump can |
327 // be emitted into the bytecode stream with space for the operand. | 361 // be emitted into the bytecode stream with space for the operand. |
328 unbound_jumps_++; | 362 unbound_jumps_++; |
329 label->set_referrer(current_offset); | 363 label->set_referrer(current_offset); |
330 OperandSize reserved_operand_size = | 364 OperandSize reserved_operand_size = |
331 constant_array_builder()->CreateReservedEntry(); | 365 constant_array_builder()->CreateReservedEntry(); |
332 OperandScale operand_scale = | 366 switch (reserved_operand_size) { |
333 Bytecodes::OperandSizesToScale(reserved_operand_size); | 367 case OperandSize::kNone: |
334 node->set_bytecode(node->bytecode(), 0, operand_scale); | 368 UNREACHABLE(); |
369 break; | |
370 case OperandSize::kByte: | |
371 node->set_bytecode(node->bytecode(), k8BitPlaceholder); | |
372 break; | |
373 case OperandSize::kShort: | |
374 node->set_bytecode(node->bytecode(), k16BitPlaceholder); | |
375 break; | |
376 case OperandSize::kQuad: | |
377 node->set_bytecode(node->bytecode(), k32BitPlaceholder); | |
378 break; | |
379 } | |
335 } | 380 } |
336 EmitBytecode(node); | 381 EmitBytecode(node); |
337 } | 382 } |
338 | 383 |
339 } // namespace interpreter | 384 } // namespace interpreter |
340 } // namespace internal | 385 } // namespace internal |
341 } // namespace v8 | 386 } // namespace v8 |
OLD | NEW |