1 // Copyright 2012 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.
5 #ifndef V8_X64_ASSEMBLER_X64_INL_H_
6 #define V8_X64_ASSEMBLER_X64_INL_H_
8 #include "src/x64/assembler-x64.h"
10 #include "src/base/cpu.h"
11 #include "src/debug.h"
12 #include "src/v8memory.h"
17 bool CpuFeatures::SupportsCrankshaft() { return true; }
18 bool CpuFeatures::SupportsSIMD128InCrankshaft() { return true; }
21 // -----------------------------------------------------------------------------
22 // Implementation of Assembler
25 static const byte kCallOpcode = 0xE8;
26 // The length of pushq(rbp), movp(rbp, rsp), Push(rsi) and Push(rdi).
27 static const int kNoCodeAgeSequenceLength = kPointerSize == kInt64Size ? 6 : 17;
30 void Assembler::emitl(uint32_t x) {
31 Memory::uint32_at(pc_) = x;
32 pc_ += sizeof(uint32_t);
36 void Assembler::emitp(void* x, RelocInfo::Mode rmode) {
37 uintptr_t value = reinterpret_cast<uintptr_t>(x);
38 Memory::uintptr_at(pc_) = value;
39 if (!RelocInfo::IsNone(rmode)) {
40 RecordRelocInfo(rmode, value);
42 pc_ += sizeof(uintptr_t);
46 void Assembler::emitq(uint64_t x) {
47 Memory::uint64_at(pc_) = x;
48 pc_ += sizeof(uint64_t);
52 void Assembler::emitw(uint16_t x) {
53 Memory::uint16_at(pc_) = x;
54 pc_ += sizeof(uint16_t);
58 void Assembler::emit_code_target(Handle<Code> target,
59 RelocInfo::Mode rmode,
60 TypeFeedbackId ast_id) {
61 DCHECK(RelocInfo::IsCodeTarget(rmode) ||
62 rmode == RelocInfo::CODE_AGE_SEQUENCE);
63 if (rmode == RelocInfo::CODE_TARGET && !ast_id.IsNone()) {
64 RecordRelocInfo(RelocInfo::CODE_TARGET_WITH_ID, ast_id.ToInt());
66 RecordRelocInfo(rmode);
68 int current = code_targets_.length();
69 if (current > 0 && code_targets_.last().is_identical_to(target)) {
70 // Optimization if we keep jumping to the same code target.
73 code_targets_.Add(target);
79 void Assembler::emit_runtime_entry(Address entry, RelocInfo::Mode rmode) {
80 DCHECK(RelocInfo::IsRuntimeEntry(rmode));
81 RecordRelocInfo(rmode);
82 emitl(static_cast<uint32_t>(entry - isolate()->code_range()->start()));
86 void Assembler::emit_rex_64(Register reg, Register rm_reg) {
87 emit(0x48 | reg.high_bit() << 2 | rm_reg.high_bit());
91 void Assembler::emit_rex_64(XMMRegister reg, Register rm_reg) {
92 emit(0x48 | (reg.code() & 0x8) >> 1 | rm_reg.code() >> 3);
96 void Assembler::emit_rex_64(Register reg, XMMRegister rm_reg) {
97 emit(0x48 | (reg.code() & 0x8) >> 1 | rm_reg.code() >> 3);
101 void Assembler::emit_rex_64(Register reg, const Operand& op) {
102 emit(0x48 | reg.high_bit() << 2 | op.rex_);
106 void Assembler::emit_rex_64(XMMRegister reg, const Operand& op) {
107 emit(0x48 | (reg.code() & 0x8) >> 1 | op.rex_);
111 void Assembler::emit_rex_64(Register rm_reg) {
112 DCHECK_EQ(rm_reg.code() & 0xf, rm_reg.code());
113 emit(0x48 | rm_reg.high_bit());
117 void Assembler::emit_rex_64(const Operand& op) {
118 emit(0x48 | op.rex_);
122 void Assembler::emit_rex_32(Register reg, Register rm_reg) {
123 emit(0x40 | reg.high_bit() << 2 | rm_reg.high_bit());
127 void Assembler::emit_rex_32(Register reg, const Operand& op) {
128 emit(0x40 | reg.high_bit() << 2 | op.rex_);
132 void Assembler::emit_rex_32(Register rm_reg) {
133 emit(0x40 | rm_reg.high_bit());
137 void Assembler::emit_rex_32(const Operand& op) {
138 emit(0x40 | op.rex_);
142 void Assembler::emit_optional_rex_32(Register reg, Register rm_reg) {
143 byte rex_bits = reg.high_bit() << 2 | rm_reg.high_bit();
144 if (rex_bits != 0) emit(0x40 | rex_bits);
148 void Assembler::emit_optional_rex_32(Register reg, const Operand& op) {
149 byte rex_bits = reg.high_bit() << 2 | op.rex_;
150 if (rex_bits != 0) emit(0x40 | rex_bits);
154 void Assembler::emit_optional_rex_32(XMMRegister reg, const Operand& op) {
155 byte rex_bits = (reg.code() & 0x8) >> 1 | op.rex_;
156 if (rex_bits != 0) emit(0x40 | rex_bits);
160 void Assembler::emit_optional_rex_32(XMMRegister reg, XMMRegister base) {
161 byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3;
162 if (rex_bits != 0) emit(0x40 | rex_bits);
166 void Assembler::emit_optional_rex_32(XMMRegister reg, Register base) {
167 byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3;
168 if (rex_bits != 0) emit(0x40 | rex_bits);
172 void Assembler::emit_optional_rex_32(Register reg, XMMRegister base) {
173 byte rex_bits = (reg.code() & 0x8) >> 1 | (base.code() & 0x8) >> 3;
174 if (rex_bits != 0) emit(0x40 | rex_bits);
178 void Assembler::emit_optional_rex_32(Register rm_reg) {
179 if (rm_reg.high_bit()) emit(0x41);
183 void Assembler::emit_optional_rex_32(XMMRegister rm_reg) {
184 if (rm_reg.high_bit()) emit(0x41);
188 void Assembler::emit_optional_rex_32(const Operand& op) {
189 if (op.rex_ != 0) emit(0x40 | op.rex_);
193 Address Assembler::target_address_at(Address pc,
194 ConstantPoolArray* constant_pool) {
195 return Memory::int32_at(pc) + pc + 4;
199 void Assembler::set_target_address_at(Address pc,
200 ConstantPoolArray* constant_pool,
202 ICacheFlushMode icache_flush_mode) {
203 Memory::int32_at(pc) = static_cast<int32_t>(target - pc - 4);
204 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
205 CpuFeatures::FlushICache(pc, sizeof(int32_t));
210 Address Assembler::target_address_from_return_address(Address pc) {
211 return pc - kCallTargetAddressOffset;
215 Address Assembler::break_address_from_return_address(Address pc) {
216 return pc - Assembler::kPatchDebugBreakSlotReturnOffset;
220 Handle<Object> Assembler::code_target_object_handle_at(Address pc) {
221 return code_targets_[Memory::int32_at(pc)];
225 Address Assembler::runtime_entry_at(Address pc) {
226 return Memory::int32_at(pc) + isolate()->code_range()->start();
229 // -----------------------------------------------------------------------------
230 // Implementation of RelocInfo
232 // The modes possibly affected by apply must be in kApplyMask.
233 void RelocInfo::apply(intptr_t delta, ICacheFlushMode icache_flush_mode) {
234 bool flush_icache = icache_flush_mode != SKIP_ICACHE_FLUSH;
235 if (IsInternalReference(rmode_)) {
236 // absolute code pointer inside code object moves with the code object.
237 Memory::Address_at(pc_) += static_cast<int32_t>(delta);
238 if (flush_icache) CpuFeatures::FlushICache(pc_, sizeof(Address));
239 } else if (IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)) {
240 Memory::int32_at(pc_) -= static_cast<int32_t>(delta);
241 if (flush_icache) CpuFeatures::FlushICache(pc_, sizeof(int32_t));
242 } else if (rmode_ == CODE_AGE_SEQUENCE) {
243 if (*pc_ == kCallOpcode) {
244 int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1);
245 *p -= static_cast<int32_t>(delta); // Relocate entry.
246 if (flush_icache) CpuFeatures::FlushICache(p, sizeof(uint32_t));
252 Address RelocInfo::target_address() {
253 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
254 return Assembler::target_address_at(pc_, host_);
258 Address RelocInfo::target_address_address() {
259 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)
260 || rmode_ == EMBEDDED_OBJECT
261 || rmode_ == EXTERNAL_REFERENCE);
262 return reinterpret_cast<Address>(pc_);
266 Address RelocInfo::constant_pool_entry_address() {
272 int RelocInfo::target_address_size() {
273 if (IsCodedSpecially()) {
274 return Assembler::kSpecialTargetSize;
281 void RelocInfo::set_target_address(Address target,
282 WriteBarrierMode write_barrier_mode,
283 ICacheFlushMode icache_flush_mode) {
284 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
285 Assembler::set_target_address_at(pc_, host_, target, icache_flush_mode);
286 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL &&
287 IsCodeTarget(rmode_)) {
288 Object* target_code = Code::GetCodeFromTargetAddress(target);
289 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
290 host(), this, HeapObject::cast(target_code));
295 Object* RelocInfo::target_object() {
296 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
297 return Memory::Object_at(pc_);
301 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
302 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
303 if (rmode_ == EMBEDDED_OBJECT) {
304 return Memory::Object_Handle_at(pc_);
306 return origin->code_target_object_handle_at(pc_);
311 Address RelocInfo::target_reference() {
312 DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
313 return Memory::Address_at(pc_);
317 void RelocInfo::set_target_object(Object* target,
318 WriteBarrierMode write_barrier_mode,
319 ICacheFlushMode icache_flush_mode) {
320 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
321 Memory::Object_at(pc_) = target;
322 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
323 CpuFeatures::FlushICache(pc_, sizeof(Address));
325 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
327 target->IsHeapObject()) {
328 host()->GetHeap()->incremental_marking()->RecordWrite(
329 host(), &Memory::Object_at(pc_), HeapObject::cast(target));
334 Address RelocInfo::target_runtime_entry(Assembler* origin) {
335 DCHECK(IsRuntimeEntry(rmode_));
336 return origin->runtime_entry_at(pc_);
340 void RelocInfo::set_target_runtime_entry(Address target,
341 WriteBarrierMode write_barrier_mode,
342 ICacheFlushMode icache_flush_mode) {
343 DCHECK(IsRuntimeEntry(rmode_));
344 if (target_address() != target) {
345 set_target_address(target, write_barrier_mode, icache_flush_mode);
350 Handle<Cell> RelocInfo::target_cell_handle() {
351 DCHECK(rmode_ == RelocInfo::CELL);
352 Address address = Memory::Address_at(pc_);
353 return Handle<Cell>(reinterpret_cast<Cell**>(address));
357 Cell* RelocInfo::target_cell() {
358 DCHECK(rmode_ == RelocInfo::CELL);
359 return Cell::FromValueAddress(Memory::Address_at(pc_));
363 void RelocInfo::set_target_cell(Cell* cell,
364 WriteBarrierMode write_barrier_mode,
365 ICacheFlushMode icache_flush_mode) {
366 DCHECK(rmode_ == RelocInfo::CELL);
367 Address address = cell->address() + Cell::kValueOffset;
368 Memory::Address_at(pc_) = address;
369 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
370 CpuFeatures::FlushICache(pc_, sizeof(Address));
372 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
374 // TODO(1550) We are passing NULL as a slot because cell can never be on
375 // evacuation candidate.
376 host()->GetHeap()->incremental_marking()->RecordWrite(
382 void RelocInfo::WipeOut() {
383 if (IsEmbeddedObject(rmode_) || IsExternalReference(rmode_)) {
384 Memory::Address_at(pc_) = NULL;
385 } else if (IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)) {
386 // Effectively write zero into the relocation.
387 Assembler::set_target_address_at(pc_, host_, pc_ + sizeof(int32_t));
394 bool RelocInfo::IsPatchedReturnSequence() {
395 // The recognized call sequence is:
396 // movq(kScratchRegister, address); call(kScratchRegister);
397 // It only needs to be distinguished from a return sequence
398 // movq(rsp, rbp); pop(rbp); ret(n); int3 *6
399 // The 11th byte is int3 (0xCC) in the return sequence and
400 // REX.WB (0x48+register bit) for the call sequence.
401 return pc_[Assembler::kMoveAddressIntoScratchRegisterInstructionLength] !=
406 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
407 return !Assembler::IsNop(pc());
411 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
412 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
413 DCHECK(*pc_ == kCallOpcode);
414 return origin->code_target_object_handle_at(pc_ + 1);
418 Code* RelocInfo::code_age_stub() {
419 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
420 DCHECK(*pc_ == kCallOpcode);
421 return Code::GetCodeFromTargetAddress(
422 Assembler::target_address_at(pc_ + 1, host_));
426 void RelocInfo::set_code_age_stub(Code* stub,
427 ICacheFlushMode icache_flush_mode) {
428 DCHECK(*pc_ == kCallOpcode);
429 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
430 Assembler::set_target_address_at(pc_ + 1, host_, stub->instruction_start(),
435 Address RelocInfo::call_address() {
436 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
437 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
438 return Memory::Address_at(
439 pc_ + Assembler::kRealPatchReturnSequenceAddressOffset);
443 void RelocInfo::set_call_address(Address target) {
444 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
445 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
446 Memory::Address_at(pc_ + Assembler::kRealPatchReturnSequenceAddressOffset) =
448 CpuFeatures::FlushICache(
449 pc_ + Assembler::kRealPatchReturnSequenceAddressOffset, sizeof(Address));
450 if (host() != NULL) {
451 Object* target_code = Code::GetCodeFromTargetAddress(target);
452 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
453 host(), this, HeapObject::cast(target_code));
458 Object* RelocInfo::call_object() {
459 return *call_object_address();
463 void RelocInfo::set_call_object(Object* target) {
464 *call_object_address() = target;
468 Object** RelocInfo::call_object_address() {
469 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
470 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
471 return reinterpret_cast<Object**>(
472 pc_ + Assembler::kPatchReturnSequenceAddressOffset);
476 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
477 RelocInfo::Mode mode = rmode();
478 if (mode == RelocInfo::EMBEDDED_OBJECT) {
479 visitor->VisitEmbeddedPointer(this);
480 CpuFeatures::FlushICache(pc_, sizeof(Address));
481 } else if (RelocInfo::IsCodeTarget(mode)) {
482 visitor->VisitCodeTarget(this);
483 } else if (mode == RelocInfo::CELL) {
484 visitor->VisitCell(this);
485 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
486 visitor->VisitExternalReference(this);
487 CpuFeatures::FlushICache(pc_, sizeof(Address));
488 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
489 visitor->VisitCodeAgeSequence(this);
490 } else if (((RelocInfo::IsJSReturn(mode) &&
491 IsPatchedReturnSequence()) ||
492 (RelocInfo::IsDebugBreakSlot(mode) &&
493 IsPatchedDebugBreakSlotSequence())) &&
494 isolate->debug()->has_break_points()) {
495 visitor->VisitDebugTarget(this);
496 } else if (RelocInfo::IsRuntimeEntry(mode)) {
497 visitor->VisitRuntimeEntry(this);
502 template<typename StaticVisitor>
503 void RelocInfo::Visit(Heap* heap) {
504 RelocInfo::Mode mode = rmode();
505 if (mode == RelocInfo::EMBEDDED_OBJECT) {
506 StaticVisitor::VisitEmbeddedPointer(heap, this);
507 CpuFeatures::FlushICache(pc_, sizeof(Address));
508 } else if (RelocInfo::IsCodeTarget(mode)) {
509 StaticVisitor::VisitCodeTarget(heap, this);
510 } else if (mode == RelocInfo::CELL) {
511 StaticVisitor::VisitCell(heap, this);
512 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
513 StaticVisitor::VisitExternalReference(this);
514 CpuFeatures::FlushICache(pc_, sizeof(Address));
515 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
516 StaticVisitor::VisitCodeAgeSequence(heap, this);
517 } else if (heap->isolate()->debug()->has_break_points() &&
518 ((RelocInfo::IsJSReturn(mode) &&
519 IsPatchedReturnSequence()) ||
520 (RelocInfo::IsDebugBreakSlot(mode) &&
521 IsPatchedDebugBreakSlotSequence()))) {
522 StaticVisitor::VisitDebugTarget(heap, this);
523 } else if (RelocInfo::IsRuntimeEntry(mode)) {
524 StaticVisitor::VisitRuntimeEntry(this);
529 // -----------------------------------------------------------------------------
530 // Implementation of Operand
532 void Operand::set_modrm(int mod, Register rm_reg) {
533 DCHECK(is_uint2(mod));
534 buf_[0] = mod << 6 | rm_reg.low_bits();
535 // Set REX.B to the high bit of rm.code().
536 rex_ |= rm_reg.high_bit();
540 void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
542 DCHECK(is_uint2(scale));
543 // Use SIB with no index register only for base rsp or r12. Otherwise we
544 // would skip the SIB byte entirely.
545 DCHECK(!index.is(rsp) || base.is(rsp) || base.is(r12));
546 buf_[1] = (scale << 6) | (index.low_bits() << 3) | base.low_bits();
547 rex_ |= index.high_bit() << 1 | base.high_bit();
551 void Operand::set_disp8(int disp) {
552 DCHECK(is_int8(disp));
553 DCHECK(len_ == 1 || len_ == 2);
554 int8_t* p = reinterpret_cast<int8_t*>(&buf_[len_]);
556 len_ += sizeof(int8_t);
559 void Operand::set_disp32(int disp) {
560 DCHECK(len_ == 1 || len_ == 2);
561 int32_t* p = reinterpret_cast<int32_t*>(&buf_[len_]);
563 len_ += sizeof(int32_t);
567 } } // namespace v8::internal
569 #endif // V8_X64_ASSEMBLER_X64_INL_H_