Upstream version 9.38.207.0
[platform/framework/web/crosswalk.git] / src / v8 / src / x64 / assembler-x64-inl.h
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.
4
5 #ifndef V8_X64_ASSEMBLER_X64_INL_H_
6 #define V8_X64_ASSEMBLER_X64_INL_H_
7
8 #include "src/x64/assembler-x64.h"
9
10 #include "src/base/cpu.h"
11 #include "src/debug.h"
12 #include "src/v8memory.h"
13
14 namespace v8 {
15 namespace internal {
16
17 bool CpuFeatures::SupportsCrankshaft() { return true; }
18 bool CpuFeatures::SupportsSIMD128InCrankshaft() { return true; }
19
20
21 // -----------------------------------------------------------------------------
22 // Implementation of Assembler
23
24
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;
28
29
30 void Assembler::emitl(uint32_t x) {
31   Memory::uint32_at(pc_) = x;
32   pc_ += sizeof(uint32_t);
33 }
34
35
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);
41   }
42   pc_ += sizeof(uintptr_t);
43 }
44
45
46 void Assembler::emitq(uint64_t x) {
47   Memory::uint64_at(pc_) = x;
48   pc_ += sizeof(uint64_t);
49 }
50
51
52 void Assembler::emitw(uint16_t x) {
53   Memory::uint16_at(pc_) = x;
54   pc_ += sizeof(uint16_t);
55 }
56
57
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());
65   } else {
66     RecordRelocInfo(rmode);
67   }
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.
71     emitl(current - 1);
72   } else {
73     code_targets_.Add(target);
74     emitl(current);
75   }
76 }
77
78
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()));
83 }
84
85
86 void Assembler::emit_rex_64(Register reg, Register rm_reg) {
87   emit(0x48 | reg.high_bit() << 2 | rm_reg.high_bit());
88 }
89
90
91 void Assembler::emit_rex_64(XMMRegister reg, Register rm_reg) {
92   emit(0x48 | (reg.code() & 0x8) >> 1 | rm_reg.code() >> 3);
93 }
94
95
96 void Assembler::emit_rex_64(Register reg, XMMRegister rm_reg) {
97   emit(0x48 | (reg.code() & 0x8) >> 1 | rm_reg.code() >> 3);
98 }
99
100
101 void Assembler::emit_rex_64(Register reg, const Operand& op) {
102   emit(0x48 | reg.high_bit() << 2 | op.rex_);
103 }
104
105
106 void Assembler::emit_rex_64(XMMRegister reg, const Operand& op) {
107   emit(0x48 | (reg.code() & 0x8) >> 1 | op.rex_);
108 }
109
110
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());
114 }
115
116
117 void Assembler::emit_rex_64(const Operand& op) {
118   emit(0x48 | op.rex_);
119 }
120
121
122 void Assembler::emit_rex_32(Register reg, Register rm_reg) {
123   emit(0x40 | reg.high_bit() << 2 | rm_reg.high_bit());
124 }
125
126
127 void Assembler::emit_rex_32(Register reg, const Operand& op) {
128   emit(0x40 | reg.high_bit() << 2  | op.rex_);
129 }
130
131
132 void Assembler::emit_rex_32(Register rm_reg) {
133   emit(0x40 | rm_reg.high_bit());
134 }
135
136
137 void Assembler::emit_rex_32(const Operand& op) {
138   emit(0x40 | op.rex_);
139 }
140
141
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);
145 }
146
147
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);
151 }
152
153
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);
157 }
158
159
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);
163 }
164
165
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);
169 }
170
171
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);
175 }
176
177
178 void Assembler::emit_optional_rex_32(Register rm_reg) {
179   if (rm_reg.high_bit()) emit(0x41);
180 }
181
182 void Assembler::emit_optional_rex_32(XMMRegister reg) {
183   byte rex_bits =  (reg.code() & 0x8) >> 1;
184   if (rex_bits != 0) emit(0x40 | rex_bits);
185 }
186
187 void Assembler::emit_optional_rex_32(const Operand& op) {
188   if (op.rex_ != 0) emit(0x40 | op.rex_);
189 }
190
191
192 Address Assembler::target_address_at(Address pc,
193                                      ConstantPoolArray* constant_pool) {
194   return Memory::int32_at(pc) + pc + 4;
195 }
196
197
198 void Assembler::set_target_address_at(Address pc,
199                                       ConstantPoolArray* constant_pool,
200                                       Address target,
201                                       ICacheFlushMode icache_flush_mode) {
202   Memory::int32_at(pc) = static_cast<int32_t>(target - pc - 4);
203   if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
204     CpuFeatures::FlushICache(pc, sizeof(int32_t));
205   }
206 }
207
208
209 Address Assembler::target_address_from_return_address(Address pc) {
210   return pc - kCallTargetAddressOffset;
211 }
212
213
214 Address Assembler::break_address_from_return_address(Address pc) {
215   return pc - Assembler::kPatchDebugBreakSlotReturnOffset;
216 }
217
218
219 Handle<Object> Assembler::code_target_object_handle_at(Address pc) {
220   return code_targets_[Memory::int32_at(pc)];
221 }
222
223
224 Address Assembler::runtime_entry_at(Address pc) {
225   return Memory::int32_at(pc) + isolate()->code_range()->start();
226 }
227
228 // -----------------------------------------------------------------------------
229 // Implementation of RelocInfo
230
231 // The modes possibly affected by apply must be in kApplyMask.
232 void RelocInfo::apply(intptr_t delta, ICacheFlushMode icache_flush_mode) {
233   bool flush_icache = icache_flush_mode != SKIP_ICACHE_FLUSH;
234   if (IsInternalReference(rmode_)) {
235     // absolute code pointer inside code object moves with the code object.
236     Memory::Address_at(pc_) += static_cast<int32_t>(delta);
237     if (flush_icache) CpuFeatures::FlushICache(pc_, sizeof(Address));
238   } else if (IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)) {
239     Memory::int32_at(pc_) -= static_cast<int32_t>(delta);
240     if (flush_icache) CpuFeatures::FlushICache(pc_, sizeof(int32_t));
241   } else if (rmode_ == CODE_AGE_SEQUENCE) {
242     if (*pc_ == kCallOpcode) {
243       int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1);
244       *p -= static_cast<int32_t>(delta);  // Relocate entry.
245       if (flush_icache) CpuFeatures::FlushICache(p, sizeof(uint32_t));
246     }
247   }
248 }
249
250
251 Address RelocInfo::target_address() {
252   DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
253   return Assembler::target_address_at(pc_, host_);
254 }
255
256
257 Address RelocInfo::target_address_address() {
258   DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)
259                               || rmode_ == EMBEDDED_OBJECT
260                               || rmode_ == EXTERNAL_REFERENCE);
261   return reinterpret_cast<Address>(pc_);
262 }
263
264
265 Address RelocInfo::constant_pool_entry_address() {
266   UNREACHABLE();
267   return NULL;
268 }
269
270
271 int RelocInfo::target_address_size() {
272   if (IsCodedSpecially()) {
273     return Assembler::kSpecialTargetSize;
274   } else {
275     return kPointerSize;
276   }
277 }
278
279
280 void RelocInfo::set_target_address(Address target,
281                                    WriteBarrierMode write_barrier_mode,
282                                    ICacheFlushMode icache_flush_mode) {
283   DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
284   Assembler::set_target_address_at(pc_, host_, target, icache_flush_mode);
285   if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL &&
286       IsCodeTarget(rmode_)) {
287     Object* target_code = Code::GetCodeFromTargetAddress(target);
288     host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
289         host(), this, HeapObject::cast(target_code));
290   }
291 }
292
293
294 Object* RelocInfo::target_object() {
295   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
296   return Memory::Object_at(pc_);
297 }
298
299
300 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
301   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
302   if (rmode_ == EMBEDDED_OBJECT) {
303     return Memory::Object_Handle_at(pc_);
304   } else {
305     return origin->code_target_object_handle_at(pc_);
306   }
307 }
308
309
310 Address RelocInfo::target_reference() {
311   DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
312   return Memory::Address_at(pc_);
313 }
314
315
316 void RelocInfo::set_target_object(Object* target,
317                                   WriteBarrierMode write_barrier_mode,
318                                   ICacheFlushMode icache_flush_mode) {
319   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
320   Memory::Object_at(pc_) = target;
321   if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
322     CpuFeatures::FlushICache(pc_, sizeof(Address));
323   }
324   if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
325       host() != NULL &&
326       target->IsHeapObject()) {
327     host()->GetHeap()->incremental_marking()->RecordWrite(
328         host(), &Memory::Object_at(pc_), HeapObject::cast(target));
329   }
330 }
331
332
333 Address RelocInfo::target_runtime_entry(Assembler* origin) {
334   DCHECK(IsRuntimeEntry(rmode_));
335   return origin->runtime_entry_at(pc_);
336 }
337
338
339 void RelocInfo::set_target_runtime_entry(Address target,
340                                          WriteBarrierMode write_barrier_mode,
341                                          ICacheFlushMode icache_flush_mode) {
342   DCHECK(IsRuntimeEntry(rmode_));
343   if (target_address() != target) {
344     set_target_address(target, write_barrier_mode, icache_flush_mode);
345   }
346 }
347
348
349 Handle<Cell> RelocInfo::target_cell_handle() {
350   DCHECK(rmode_ == RelocInfo::CELL);
351   Address address = Memory::Address_at(pc_);
352   return Handle<Cell>(reinterpret_cast<Cell**>(address));
353 }
354
355
356 Cell* RelocInfo::target_cell() {
357   DCHECK(rmode_ == RelocInfo::CELL);
358   return Cell::FromValueAddress(Memory::Address_at(pc_));
359 }
360
361
362 void RelocInfo::set_target_cell(Cell* cell,
363                                 WriteBarrierMode write_barrier_mode,
364                                 ICacheFlushMode icache_flush_mode) {
365   DCHECK(rmode_ == RelocInfo::CELL);
366   Address address = cell->address() + Cell::kValueOffset;
367   Memory::Address_at(pc_) = address;
368   if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
369     CpuFeatures::FlushICache(pc_, sizeof(Address));
370   }
371   if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
372       host() != NULL) {
373     // TODO(1550) We are passing NULL as a slot because cell can never be on
374     // evacuation candidate.
375     host()->GetHeap()->incremental_marking()->RecordWrite(
376         host(), NULL, cell);
377   }
378 }
379
380
381 void RelocInfo::WipeOut() {
382   if (IsEmbeddedObject(rmode_) || IsExternalReference(rmode_)) {
383     Memory::Address_at(pc_) = NULL;
384   } else if (IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)) {
385     // Effectively write zero into the relocation.
386     Assembler::set_target_address_at(pc_, host_, pc_ + sizeof(int32_t));
387   } else {
388     UNREACHABLE();
389   }
390 }
391
392
393 bool RelocInfo::IsPatchedReturnSequence() {
394   // The recognized call sequence is:
395   //  movq(kScratchRegister, address); call(kScratchRegister);
396   // It only needs to be distinguished from a return sequence
397   //  movq(rsp, rbp); pop(rbp); ret(n); int3 *6
398   // The 11th byte is int3 (0xCC) in the return sequence and
399   // REX.WB (0x48+register bit) for the call sequence.
400   return pc_[Assembler::kMoveAddressIntoScratchRegisterInstructionLength] !=
401          0xCC;
402 }
403
404
405 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
406   return !Assembler::IsNop(pc());
407 }
408
409
410 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
411   DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
412   DCHECK(*pc_ == kCallOpcode);
413   return origin->code_target_object_handle_at(pc_ + 1);
414 }
415
416
417 Code* RelocInfo::code_age_stub() {
418   DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
419   DCHECK(*pc_ == kCallOpcode);
420   return Code::GetCodeFromTargetAddress(
421       Assembler::target_address_at(pc_ + 1, host_));
422 }
423
424
425 void RelocInfo::set_code_age_stub(Code* stub,
426                                   ICacheFlushMode icache_flush_mode) {
427   DCHECK(*pc_ == kCallOpcode);
428   DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
429   Assembler::set_target_address_at(pc_ + 1, host_, stub->instruction_start(),
430                                    icache_flush_mode);
431 }
432
433
434 Address RelocInfo::call_address() {
435   DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
436          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
437   return Memory::Address_at(
438       pc_ + Assembler::kRealPatchReturnSequenceAddressOffset);
439 }
440
441
442 void RelocInfo::set_call_address(Address target) {
443   DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
444          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
445   Memory::Address_at(pc_ + Assembler::kRealPatchReturnSequenceAddressOffset) =
446       target;
447   CpuFeatures::FlushICache(
448       pc_ + Assembler::kRealPatchReturnSequenceAddressOffset, sizeof(Address));
449   if (host() != NULL) {
450     Object* target_code = Code::GetCodeFromTargetAddress(target);
451     host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
452         host(), this, HeapObject::cast(target_code));
453   }
454 }
455
456
457 Object* RelocInfo::call_object() {
458   return *call_object_address();
459 }
460
461
462 void RelocInfo::set_call_object(Object* target) {
463   *call_object_address() = target;
464 }
465
466
467 Object** RelocInfo::call_object_address() {
468   DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
469          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
470   return reinterpret_cast<Object**>(
471       pc_ + Assembler::kPatchReturnSequenceAddressOffset);
472 }
473
474
475 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
476   RelocInfo::Mode mode = rmode();
477   if (mode == RelocInfo::EMBEDDED_OBJECT) {
478     visitor->VisitEmbeddedPointer(this);
479     CpuFeatures::FlushICache(pc_, sizeof(Address));
480   } else if (RelocInfo::IsCodeTarget(mode)) {
481     visitor->VisitCodeTarget(this);
482   } else if (mode == RelocInfo::CELL) {
483     visitor->VisitCell(this);
484   } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
485     visitor->VisitExternalReference(this);
486     CpuFeatures::FlushICache(pc_, sizeof(Address));
487   } else if (RelocInfo::IsCodeAgeSequence(mode)) {
488     visitor->VisitCodeAgeSequence(this);
489   } else if (((RelocInfo::IsJSReturn(mode) &&
490               IsPatchedReturnSequence()) ||
491              (RelocInfo::IsDebugBreakSlot(mode) &&
492               IsPatchedDebugBreakSlotSequence())) &&
493              isolate->debug()->has_break_points()) {
494     visitor->VisitDebugTarget(this);
495   } else if (RelocInfo::IsRuntimeEntry(mode)) {
496     visitor->VisitRuntimeEntry(this);
497   }
498 }
499
500
501 template<typename StaticVisitor>
502 void RelocInfo::Visit(Heap* heap) {
503   RelocInfo::Mode mode = rmode();
504   if (mode == RelocInfo::EMBEDDED_OBJECT) {
505     StaticVisitor::VisitEmbeddedPointer(heap, this);
506     CpuFeatures::FlushICache(pc_, sizeof(Address));
507   } else if (RelocInfo::IsCodeTarget(mode)) {
508     StaticVisitor::VisitCodeTarget(heap, this);
509   } else if (mode == RelocInfo::CELL) {
510     StaticVisitor::VisitCell(heap, this);
511   } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
512     StaticVisitor::VisitExternalReference(this);
513     CpuFeatures::FlushICache(pc_, sizeof(Address));
514   } else if (RelocInfo::IsCodeAgeSequence(mode)) {
515     StaticVisitor::VisitCodeAgeSequence(heap, this);
516   } else if (heap->isolate()->debug()->has_break_points() &&
517              ((RelocInfo::IsJSReturn(mode) &&
518               IsPatchedReturnSequence()) ||
519              (RelocInfo::IsDebugBreakSlot(mode) &&
520               IsPatchedDebugBreakSlotSequence()))) {
521     StaticVisitor::VisitDebugTarget(heap, this);
522   } else if (RelocInfo::IsRuntimeEntry(mode)) {
523     StaticVisitor::VisitRuntimeEntry(this);
524   }
525 }
526
527
528 // -----------------------------------------------------------------------------
529 // Implementation of Operand
530
531 void Operand::set_modrm(int mod, Register rm_reg) {
532   DCHECK(is_uint2(mod));
533   buf_[0] = mod << 6 | rm_reg.low_bits();
534   // Set REX.B to the high bit of rm.code().
535   rex_ |= rm_reg.high_bit();
536 }
537
538
539 void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
540   DCHECK(len_ == 1);
541   DCHECK(is_uint2(scale));
542   // Use SIB with no index register only for base rsp or r12. Otherwise we
543   // would skip the SIB byte entirely.
544   DCHECK(!index.is(rsp) || base.is(rsp) || base.is(r12));
545   buf_[1] = (scale << 6) | (index.low_bits() << 3) | base.low_bits();
546   rex_ |= index.high_bit() << 1 | base.high_bit();
547   len_ = 2;
548 }
549
550 void Operand::set_disp8(int disp) {
551   DCHECK(is_int8(disp));
552   DCHECK(len_ == 1 || len_ == 2);
553   int8_t* p = reinterpret_cast<int8_t*>(&buf_[len_]);
554   *p = disp;
555   len_ += sizeof(int8_t);
556 }
557
558 void Operand::set_disp32(int disp) {
559   DCHECK(len_ == 1 || len_ == 2);
560   int32_t* p = reinterpret_cast<int32_t*>(&buf_[len_]);
561   *p = disp;
562   len_ += sizeof(int32_t);
563 }
564
565
566 } }  // namespace v8::internal
567
568 #endif  // V8_X64_ASSEMBLER_X64_INL_H_