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/bytecode-register.h" | 9 #include "src/interpreter/bytecode-register.h" |
10 #include "src/interpreter/constant-array-builder.h" | 10 #include "src/interpreter/constant-array-builder.h" |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 return Bytecode::kJumpIfJSReceiverConstant; | 168 return Bytecode::kJumpIfJSReceiverConstant; |
169 default: | 169 default: |
170 UNREACHABLE(); | 170 UNREACHABLE(); |
171 return Bytecode::kIllegal; | 171 return Bytecode::kIllegal; |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
175 void BytecodeArrayWriter::PatchJumpWith8BitOperand(size_t jump_location, | 175 void BytecodeArrayWriter::PatchJumpWith8BitOperand(size_t jump_location, |
176 int delta) { | 176 int delta) { |
177 Bytecode jump_bytecode = Bytecodes::FromByte(bytecodes()->at(jump_location)); | 177 Bytecode jump_bytecode = Bytecodes::FromByte(bytecodes()->at(jump_location)); |
| 178 DCHECK(Bytecodes::IsForwardJump(jump_bytecode)); |
178 DCHECK(Bytecodes::IsJumpImmediate(jump_bytecode)); | 179 DCHECK(Bytecodes::IsJumpImmediate(jump_bytecode)); |
| 180 DCHECK_EQ(Bytecodes::GetOperandType(jump_bytecode, 0), OperandType::kUImm); |
| 181 DCHECK_GT(delta, 0); |
179 size_t operand_location = jump_location + 1; | 182 size_t operand_location = jump_location + 1; |
180 DCHECK_EQ(bytecodes()->at(operand_location), k8BitJumpPlaceholder); | 183 DCHECK_EQ(bytecodes()->at(operand_location), k8BitJumpPlaceholder); |
181 if (Bytecodes::ScaleForSignedOperand(delta) == OperandScale::kSingle) { | 184 if (Bytecodes::ScaleForUnsignedOperand(delta) == OperandScale::kSingle) { |
182 // The jump fits within the range of an Imm8 operand, so cancel | 185 // The jump fits within the range of an UImm8 operand, so cancel |
183 // the reservation and jump directly. | 186 // the reservation and jump directly. |
184 constant_array_builder()->DiscardReservedEntry(OperandSize::kByte); | 187 constant_array_builder()->DiscardReservedEntry(OperandSize::kByte); |
185 bytecodes()->at(operand_location) = static_cast<uint8_t>(delta); | 188 bytecodes()->at(operand_location) = static_cast<uint8_t>(delta); |
186 } else { | 189 } else { |
187 // The jump does not fit within the range of an Imm8 operand, so | 190 // The jump does not fit within the range of an UImm8 operand, so |
188 // commit reservation putting the offset into the constant pool, | 191 // commit reservation putting the offset into the constant pool, |
189 // and update the jump instruction and operand. | 192 // and update the jump instruction and operand. |
190 size_t entry = constant_array_builder()->CommitReservedEntry( | 193 size_t entry = constant_array_builder()->CommitReservedEntry( |
191 OperandSize::kByte, Smi::FromInt(delta)); | 194 OperandSize::kByte, Smi::FromInt(delta)); |
192 DCHECK_EQ(Bytecodes::SizeForUnsignedOperand(static_cast<uint32_t>(entry)), | 195 DCHECK_EQ(Bytecodes::SizeForUnsignedOperand(static_cast<uint32_t>(entry)), |
193 OperandSize::kByte); | 196 OperandSize::kByte); |
194 jump_bytecode = GetJumpWithConstantOperand(jump_bytecode); | 197 jump_bytecode = GetJumpWithConstantOperand(jump_bytecode); |
195 bytecodes()->at(jump_location) = Bytecodes::ToByte(jump_bytecode); | 198 bytecodes()->at(jump_location) = Bytecodes::ToByte(jump_bytecode); |
196 bytecodes()->at(operand_location) = static_cast<uint8_t>(entry); | 199 bytecodes()->at(operand_location) = static_cast<uint8_t>(entry); |
197 } | 200 } |
198 } | 201 } |
199 | 202 |
200 void BytecodeArrayWriter::PatchJumpWith16BitOperand(size_t jump_location, | 203 void BytecodeArrayWriter::PatchJumpWith16BitOperand(size_t jump_location, |
201 int delta) { | 204 int delta) { |
202 Bytecode jump_bytecode = Bytecodes::FromByte(bytecodes()->at(jump_location)); | 205 Bytecode jump_bytecode = Bytecodes::FromByte(bytecodes()->at(jump_location)); |
| 206 DCHECK(Bytecodes::IsForwardJump(jump_bytecode)); |
203 DCHECK(Bytecodes::IsJumpImmediate(jump_bytecode)); | 207 DCHECK(Bytecodes::IsJumpImmediate(jump_bytecode)); |
| 208 DCHECK_EQ(Bytecodes::GetOperandType(jump_bytecode, 0), OperandType::kUImm); |
| 209 DCHECK_GT(delta, 0); |
204 size_t operand_location = jump_location + 1; | 210 size_t operand_location = jump_location + 1; |
205 uint8_t operand_bytes[2]; | 211 uint8_t operand_bytes[2]; |
206 if (Bytecodes::ScaleForSignedOperand(delta) <= OperandScale::kDouble) { | 212 if (Bytecodes::ScaleForUnsignedOperand(delta) <= OperandScale::kDouble) { |
207 // The jump fits within the range of an Imm16 operand, so cancel | 213 // The jump fits within the range of an Imm16 operand, so cancel |
208 // the reservation and jump directly. | 214 // the reservation and jump directly. |
209 constant_array_builder()->DiscardReservedEntry(OperandSize::kShort); | 215 constant_array_builder()->DiscardReservedEntry(OperandSize::kShort); |
210 WriteUnalignedUInt16(operand_bytes, static_cast<uint16_t>(delta)); | 216 WriteUnalignedUInt16(operand_bytes, static_cast<uint16_t>(delta)); |
211 } else { | 217 } else { |
212 // The jump does not fit within the range of an Imm16 operand, so | 218 // The jump does not fit within the range of an Imm16 operand, so |
213 // commit reservation putting the offset into the constant pool, | 219 // commit reservation putting the offset into the constant pool, |
214 // and update the jump instruction and operand. | 220 // and update the jump instruction and operand. |
215 size_t entry = constant_array_builder()->CommitReservedEntry( | 221 size_t entry = constant_array_builder()->CommitReservedEntry( |
216 OperandSize::kShort, Smi::FromInt(delta)); | 222 OperandSize::kShort, Smi::FromInt(delta)); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 } | 281 } |
276 | 282 |
277 void BytecodeArrayWriter::EmitJump(BytecodeNode* node, BytecodeLabel* label) { | 283 void BytecodeArrayWriter::EmitJump(BytecodeNode* node, BytecodeLabel* label) { |
278 DCHECK(Bytecodes::IsJump(node->bytecode())); | 284 DCHECK(Bytecodes::IsJump(node->bytecode())); |
279 DCHECK_EQ(0u, node->operand(0)); | 285 DCHECK_EQ(0u, node->operand(0)); |
280 | 286 |
281 size_t current_offset = bytecodes()->size(); | 287 size_t current_offset = bytecodes()->size(); |
282 | 288 |
283 if (label->is_bound()) { | 289 if (label->is_bound()) { |
284 CHECK_GE(current_offset, label->offset()); | 290 CHECK_GE(current_offset, label->offset()); |
285 CHECK_LE(current_offset, static_cast<size_t>(kMaxInt)); | 291 CHECK_LE(current_offset, static_cast<size_t>(kMaxUInt32)); |
286 // Label has been bound already so this is a backwards jump. | 292 // Label has been bound already so this is a backwards jump. |
287 size_t abs_delta = current_offset - label->offset(); | 293 uint32_t delta = static_cast<uint32_t>(current_offset - label->offset()); |
288 int delta = -static_cast<int>(abs_delta); | 294 OperandScale operand_scale = Bytecodes::ScaleForUnsignedOperand(delta); |
289 OperandScale operand_scale = Bytecodes::ScaleForSignedOperand(delta); | |
290 if (operand_scale > OperandScale::kSingle) { | 295 if (operand_scale > OperandScale::kSingle) { |
291 // Adjust for scaling byte prefix for wide jump offset. | 296 // Adjust for scaling byte prefix for wide jump offset. |
292 DCHECK_LE(delta, 0); | 297 delta += 1; |
293 delta -= 1; | |
294 } | 298 } |
295 DCHECK_EQ(Bytecode::kJumpLoop, node->bytecode()); | 299 DCHECK_EQ(Bytecode::kJumpLoop, node->bytecode()); |
296 node->update_operand0(delta); | 300 node->update_operand0(delta); |
297 } else { | 301 } else { |
298 // The label has not yet been bound so this is a forward reference | 302 // The label has not yet been bound so this is a forward reference |
299 // that will be patched when the label is bound. We create a | 303 // that will be patched when the label is bound. We create a |
300 // reservation in the constant pool so the jump can be patched | 304 // reservation in the constant pool so the jump can be patched |
301 // when the label is bound. The reservation means the maximum size | 305 // when the label is bound. The reservation means the maximum size |
302 // of the operand for the constant is known and the jump can | 306 // of the operand for the constant is known and the jump can |
303 // be emitted into the bytecode stream with space for the operand. | 307 // be emitted into the bytecode stream with space for the operand. |
(...skipping 16 matching lines...) Expand all Loading... |
320 node->update_operand0(k32BitJumpPlaceholder); | 324 node->update_operand0(k32BitJumpPlaceholder); |
321 break; | 325 break; |
322 } | 326 } |
323 } | 327 } |
324 EmitBytecode(node); | 328 EmitBytecode(node); |
325 } | 329 } |
326 | 330 |
327 } // namespace interpreter | 331 } // namespace interpreter |
328 } // namespace internal | 332 } // namespace internal |
329 } // namespace v8 | 333 } // namespace v8 |
OLD | NEW |