Upstream version 11.40.277.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
183 void Assembler::emit_optional_rex_32(XMMRegister rm_reg) {
184   if (rm_reg.high_bit()) emit(0x41);
185 }
186
187
188 void Assembler::emit_optional_rex_32(const Operand& op) {
189   if (op.rex_ != 0) emit(0x40 | op.rex_);
190 }
191
192
193 Address Assembler::target_address_at(Address pc,
194                                      ConstantPoolArray* constant_pool) {
195   return Memory::int32_at(pc) + pc + 4;
196 }
197
198
199 void Assembler::set_target_address_at(Address pc,
200                                       ConstantPoolArray* constant_pool,
201                                       Address target,
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));
206   }
207 }
208
209
210 Address Assembler::target_address_from_return_address(Address pc) {
211   return pc - kCallTargetAddressOffset;
212 }
213
214
215 Address Assembler::break_address_from_return_address(Address pc) {
216   return pc - Assembler::kPatchDebugBreakSlotReturnOffset;
217 }
218
219
220 Handle<Object> Assembler::code_target_object_handle_at(Address pc) {
221   return code_targets_[Memory::int32_at(pc)];
222 }
223
224
225 Address Assembler::runtime_entry_at(Address pc) {
226   return Memory::int32_at(pc) + isolate()->code_range()->start();
227 }
228
229 // -----------------------------------------------------------------------------
230 // Implementation of RelocInfo
231
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));
247     }
248   }
249 }
250
251
252 Address RelocInfo::target_address() {
253   DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
254   return Assembler::target_address_at(pc_, host_);
255 }
256
257
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_);
263 }
264
265
266 Address RelocInfo::constant_pool_entry_address() {
267   UNREACHABLE();
268   return NULL;
269 }
270
271
272 int RelocInfo::target_address_size() {
273   if (IsCodedSpecially()) {
274     return Assembler::kSpecialTargetSize;
275   } else {
276     return kPointerSize;
277   }
278 }
279
280
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));
291   }
292 }
293
294
295 Object* RelocInfo::target_object() {
296   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
297   return Memory::Object_at(pc_);
298 }
299
300
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_);
305   } else {
306     return origin->code_target_object_handle_at(pc_);
307   }
308 }
309
310
311 Address RelocInfo::target_reference() {
312   DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
313   return Memory::Address_at(pc_);
314 }
315
316
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));
324   }
325   if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
326       host() != NULL &&
327       target->IsHeapObject()) {
328     host()->GetHeap()->incremental_marking()->RecordWrite(
329         host(), &Memory::Object_at(pc_), HeapObject::cast(target));
330   }
331 }
332
333
334 Address RelocInfo::target_runtime_entry(Assembler* origin) {
335   DCHECK(IsRuntimeEntry(rmode_));
336   return origin->runtime_entry_at(pc_);
337 }
338
339
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);
346   }
347 }
348
349
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));
354 }
355
356
357 Cell* RelocInfo::target_cell() {
358   DCHECK(rmode_ == RelocInfo::CELL);
359   return Cell::FromValueAddress(Memory::Address_at(pc_));
360 }
361
362
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));
371   }
372   if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
373       host() != NULL) {
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(
377         host(), NULL, cell);
378   }
379 }
380
381
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));
388   } else {
389     UNREACHABLE();
390   }
391 }
392
393
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] !=
402          0xCC;
403 }
404
405
406 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
407   return !Assembler::IsNop(pc());
408 }
409
410
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);
415 }
416
417
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_));
423 }
424
425
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(),
431                                    icache_flush_mode);
432 }
433
434
435 Address RelocInfo::call_address() {
436   DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
437          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
438   return Memory::Address_at(
439       pc_ + Assembler::kRealPatchReturnSequenceAddressOffset);
440 }
441
442
443 void RelocInfo::set_call_address(Address target) {
444   DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
445          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
446   Memory::Address_at(pc_ + Assembler::kRealPatchReturnSequenceAddressOffset) =
447       target;
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));
454   }
455 }
456
457
458 Object* RelocInfo::call_object() {
459   return *call_object_address();
460 }
461
462
463 void RelocInfo::set_call_object(Object* target) {
464   *call_object_address() = target;
465 }
466
467
468 Object** RelocInfo::call_object_address() {
469   DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
470          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
471   return reinterpret_cast<Object**>(
472       pc_ + Assembler::kPatchReturnSequenceAddressOffset);
473 }
474
475
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);
498   }
499 }
500
501
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);
525   }
526 }
527
528
529 // -----------------------------------------------------------------------------
530 // Implementation of Operand
531
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();
537 }
538
539
540 void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
541   DCHECK(len_ == 1);
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();
548   len_ = 2;
549 }
550
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_]);
555   *p = disp;
556   len_ += sizeof(int8_t);
557 }
558
559 void Operand::set_disp32(int disp) {
560   DCHECK(len_ == 1 || len_ == 2);
561   int32_t* p = reinterpret_cast<int32_t*>(&buf_[len_]);
562   *p = disp;
563   len_ += sizeof(int32_t);
564 }
565
566
567 } }  // namespace v8::internal
568
569 #endif  // V8_X64_ASSEMBLER_X64_INL_H_