d13c21f4b7c17a2c044d7eee052df85c95797a4a
[platform/framework/web/crosswalk.git] / src / v8 / src / x64 / assembler-x64.cc
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 #include "src/v8.h"
6
7 #if V8_TARGET_ARCH_X64
8
9 #include "src/macro-assembler.h"
10 #include "src/serialize.h"
11
12 namespace v8 {
13 namespace internal {
14
15 // -----------------------------------------------------------------------------
16 // Implementation of CpuFeatures
17
18 void CpuFeatures::ProbeImpl(bool cross_compile) {
19   base::CPU cpu;
20   CHECK(cpu.has_sse2());  // SSE2 support is mandatory.
21   CHECK(cpu.has_cmov());  // CMOV support is mandatory.
22
23   // Only use statically determined features for cross compile (snapshot).
24   if (cross_compile) return;
25
26   if (cpu.has_sse41() && FLAG_enable_sse4_1) supported_ |= 1u << SSE4_1;
27   if (cpu.has_sse3() && FLAG_enable_sse3) supported_ |= 1u << SSE3;
28   // SAHF is not generally available in long mode.
29   if (cpu.has_sahf() && FLAG_enable_sahf) supported_|= 1u << SAHF;
30 }
31
32
33 void CpuFeatures::PrintTarget() { }
34 void CpuFeatures::PrintFeatures() { }
35
36
37 // -----------------------------------------------------------------------------
38 // Implementation of RelocInfo
39
40 // Patch the code at the current PC with a call to the target address.
41 // Additional guard int3 instructions can be added if required.
42 void RelocInfo::PatchCodeWithCall(Address target, int guard_bytes) {
43   int code_size = Assembler::kCallSequenceLength + guard_bytes;
44
45   // Create a code patcher.
46   CodePatcher patcher(pc_, code_size);
47
48   // Add a label for checking the size of the code used for returning.
49 #ifdef DEBUG
50   Label check_codesize;
51   patcher.masm()->bind(&check_codesize);
52 #endif
53
54   // Patch the code.
55   patcher.masm()->movp(kScratchRegister, reinterpret_cast<void*>(target),
56                        Assembler::RelocInfoNone());
57   patcher.masm()->call(kScratchRegister);
58
59   // Check that the size of the code generated is as expected.
60   DCHECK_EQ(Assembler::kCallSequenceLength,
61             patcher.masm()->SizeOfCodeGeneratedSince(&check_codesize));
62
63   // Add the requested number of int3 instructions after the call.
64   for (int i = 0; i < guard_bytes; i++) {
65     patcher.masm()->int3();
66   }
67 }
68
69
70 void RelocInfo::PatchCode(byte* instructions, int instruction_count) {
71   // Patch the code at the current address with the supplied instructions.
72   for (int i = 0; i < instruction_count; i++) {
73     *(pc_ + i) = *(instructions + i);
74   }
75
76   // Indicate that code has changed.
77   CpuFeatures::FlushICache(pc_, instruction_count);
78 }
79
80
81 // -----------------------------------------------------------------------------
82 // Register constants.
83
84 const int
85     Register::kRegisterCodeByAllocationIndex[kMaxNumAllocatableRegisters] = {
86   // rax, rbx, rdx, rcx, rsi, rdi, r8, r9, r11, r14, r15
87   0, 3, 2, 1, 6, 7, 8, 9, 11, 14, 15
88 };
89
90 const int Register::kAllocationIndexByRegisterCode[kNumRegisters] = {
91   0, 3, 2, 1, -1, -1, 4, 5, 6, 7, -1, 8, -1, -1, 9, 10
92 };
93
94
95 // -----------------------------------------------------------------------------
96 // Implementation of Operand
97
98 Operand::Operand(Register base, int32_t disp) : rex_(0) {
99   len_ = 1;
100   if (base.is(rsp) || base.is(r12)) {
101     // SIB byte is needed to encode (rsp + offset) or (r12 + offset).
102     set_sib(times_1, rsp, base);
103   }
104
105   if (disp == 0 && !base.is(rbp) && !base.is(r13)) {
106     set_modrm(0, base);
107   } else if (is_int8(disp)) {
108     set_modrm(1, base);
109     set_disp8(disp);
110   } else {
111     set_modrm(2, base);
112     set_disp32(disp);
113   }
114 }
115
116
117 Operand::Operand(Register base,
118                  Register index,
119                  ScaleFactor scale,
120                  int32_t disp) : rex_(0) {
121   DCHECK(!index.is(rsp));
122   len_ = 1;
123   set_sib(scale, index, base);
124   if (disp == 0 && !base.is(rbp) && !base.is(r13)) {
125     // This call to set_modrm doesn't overwrite the REX.B (or REX.X) bits
126     // possibly set by set_sib.
127     set_modrm(0, rsp);
128   } else if (is_int8(disp)) {
129     set_modrm(1, rsp);
130     set_disp8(disp);
131   } else {
132     set_modrm(2, rsp);
133     set_disp32(disp);
134   }
135 }
136
137
138 Operand::Operand(Register index,
139                  ScaleFactor scale,
140                  int32_t disp) : rex_(0) {
141   DCHECK(!index.is(rsp));
142   len_ = 1;
143   set_modrm(0, rsp);
144   set_sib(scale, index, rbp);
145   set_disp32(disp);
146 }
147
148
149 Operand::Operand(const Operand& operand, int32_t offset) {
150   DCHECK(operand.len_ >= 1);
151   // Operand encodes REX ModR/M [SIB] [Disp].
152   byte modrm = operand.buf_[0];
153   DCHECK(modrm < 0xC0);  // Disallow mode 3 (register target).
154   bool has_sib = ((modrm & 0x07) == 0x04);
155   byte mode = modrm & 0xC0;
156   int disp_offset = has_sib ? 2 : 1;
157   int base_reg = (has_sib ? operand.buf_[1] : modrm) & 0x07;
158   // Mode 0 with rbp/r13 as ModR/M or SIB base register always has a 32-bit
159   // displacement.
160   bool is_baseless = (mode == 0) && (base_reg == 0x05);  // No base or RIP base.
161   int32_t disp_value = 0;
162   if (mode == 0x80 || is_baseless) {
163     // Mode 2 or mode 0 with rbp/r13 as base: Word displacement.
164     disp_value = *BitCast<const int32_t*>(&operand.buf_[disp_offset]);
165   } else if (mode == 0x40) {
166     // Mode 1: Byte displacement.
167     disp_value = static_cast<signed char>(operand.buf_[disp_offset]);
168   }
169
170   // Write new operand with same registers, but with modified displacement.
171   DCHECK(offset >= 0 ? disp_value + offset > disp_value
172                      : disp_value + offset < disp_value);  // No overflow.
173   disp_value += offset;
174   rex_ = operand.rex_;
175   if (!is_int8(disp_value) || is_baseless) {
176     // Need 32 bits of displacement, mode 2 or mode 1 with register rbp/r13.
177     buf_[0] = (modrm & 0x3f) | (is_baseless ? 0x00 : 0x80);
178     len_ = disp_offset + 4;
179     Memory::int32_at(&buf_[disp_offset]) = disp_value;
180   } else if (disp_value != 0 || (base_reg == 0x05)) {
181     // Need 8 bits of displacement.
182     buf_[0] = (modrm & 0x3f) | 0x40;  // Mode 1.
183     len_ = disp_offset + 1;
184     buf_[disp_offset] = static_cast<byte>(disp_value);
185   } else {
186     // Need no displacement.
187     buf_[0] = (modrm & 0x3f);  // Mode 0.
188     len_ = disp_offset;
189   }
190   if (has_sib) {
191     buf_[1] = operand.buf_[1];
192   }
193 }
194
195
196 bool Operand::AddressUsesRegister(Register reg) const {
197   int code = reg.code();
198   DCHECK((buf_[0] & 0xC0) != 0xC0);  // Always a memory operand.
199   // Start with only low three bits of base register. Initial decoding doesn't
200   // distinguish on the REX.B bit.
201   int base_code = buf_[0] & 0x07;
202   if (base_code == rsp.code()) {
203     // SIB byte present in buf_[1].
204     // Check the index register from the SIB byte + REX.X prefix.
205     int index_code = ((buf_[1] >> 3) & 0x07) | ((rex_ & 0x02) << 2);
206     // Index code (including REX.X) of 0x04 (rsp) means no index register.
207     if (index_code != rsp.code() && index_code == code) return true;
208     // Add REX.B to get the full base register code.
209     base_code = (buf_[1] & 0x07) | ((rex_ & 0x01) << 3);
210     // A base register of 0x05 (rbp) with mod = 0 means no base register.
211     if (base_code == rbp.code() && ((buf_[0] & 0xC0) == 0)) return false;
212     return code == base_code;
213   } else {
214     // A base register with low bits of 0x05 (rbp or r13) and mod = 0 means
215     // no base register.
216     if (base_code == rbp.code() && ((buf_[0] & 0xC0) == 0)) return false;
217     base_code |= ((rex_ & 0x01) << 3);
218     return code == base_code;
219   }
220 }
221
222
223 // -----------------------------------------------------------------------------
224 // Implementation of Assembler.
225
226 #ifdef GENERATED_CODE_COVERAGE
227 static void InitCoverageLog();
228 #endif
229
230 Assembler::Assembler(Isolate* isolate, void* buffer, int buffer_size)
231     : AssemblerBase(isolate, buffer, buffer_size),
232       code_targets_(100),
233       positions_recorder_(this) {
234   // Clear the buffer in debug mode unless it was provided by the
235   // caller in which case we can't be sure it's okay to overwrite
236   // existing code in it.
237 #ifdef DEBUG
238   if (own_buffer_) {
239     memset(buffer_, 0xCC, buffer_size_);  // int3
240   }
241 #endif
242
243   reloc_info_writer.Reposition(buffer_ + buffer_size_, pc_);
244
245
246 #ifdef GENERATED_CODE_COVERAGE
247   InitCoverageLog();
248 #endif
249 }
250
251
252 void Assembler::GetCode(CodeDesc* desc) {
253   // Finalize code (at this point overflow() may be true, but the gap ensures
254   // that we are still not overlapping instructions and relocation info).
255   DCHECK(pc_ <= reloc_info_writer.pos());  // No overlap.
256   // Set up code descriptor.
257   desc->buffer = buffer_;
258   desc->buffer_size = buffer_size_;
259   desc->instr_size = pc_offset();
260   DCHECK(desc->instr_size > 0);  // Zero-size code objects upset the system.
261   desc->reloc_size =
262       static_cast<int>((buffer_ + buffer_size_) - reloc_info_writer.pos());
263   desc->origin = this;
264 }
265
266
267 void Assembler::Align(int m) {
268   DCHECK(IsPowerOf2(m));
269   int delta = (m - (pc_offset() & (m - 1))) & (m - 1);
270   Nop(delta);
271 }
272
273
274 void Assembler::CodeTargetAlign() {
275   Align(16);  // Preferred alignment of jump targets on x64.
276 }
277
278
279 bool Assembler::IsNop(Address addr) {
280   Address a = addr;
281   while (*a == 0x66) a++;
282   if (*a == 0x90) return true;
283   if (a[0] == 0xf && a[1] == 0x1f) return true;
284   return false;
285 }
286
287
288 void Assembler::bind_to(Label* L, int pos) {
289   DCHECK(!L->is_bound());  // Label may only be bound once.
290   DCHECK(0 <= pos && pos <= pc_offset());  // Position must be valid.
291   if (L->is_linked()) {
292     int current = L->pos();
293     int next = long_at(current);
294     while (next != current) {
295       // Relative address, relative to point after address.
296       int imm32 = pos - (current + sizeof(int32_t));
297       long_at_put(current, imm32);
298       current = next;
299       next = long_at(next);
300     }
301     // Fix up last fixup on linked list.
302     int last_imm32 = pos - (current + sizeof(int32_t));
303     long_at_put(current, last_imm32);
304   }
305   while (L->is_near_linked()) {
306     int fixup_pos = L->near_link_pos();
307     int offset_to_next =
308         static_cast<int>(*reinterpret_cast<int8_t*>(addr_at(fixup_pos)));
309     DCHECK(offset_to_next <= 0);
310     int disp = pos - (fixup_pos + sizeof(int8_t));
311     CHECK(is_int8(disp));
312     set_byte_at(fixup_pos, disp);
313     if (offset_to_next < 0) {
314       L->link_to(fixup_pos + offset_to_next, Label::kNear);
315     } else {
316       L->UnuseNear();
317     }
318   }
319   L->bind_to(pos);
320 }
321
322
323 void Assembler::bind(Label* L) {
324   bind_to(L, pc_offset());
325 }
326
327
328 void Assembler::GrowBuffer() {
329   DCHECK(buffer_overflow());
330   if (!own_buffer_) FATAL("external code buffer is too small");
331
332   // Compute new buffer size.
333   CodeDesc desc;  // the new buffer
334   desc.buffer_size = 2 * buffer_size_;
335
336   // Some internal data structures overflow for very large buffers,
337   // they must ensure that kMaximalBufferSize is not too large.
338   if ((desc.buffer_size > kMaximalBufferSize) ||
339       (desc.buffer_size > isolate()->heap()->MaxOldGenerationSize())) {
340     V8::FatalProcessOutOfMemory("Assembler::GrowBuffer");
341   }
342
343   // Set up new buffer.
344   desc.buffer = NewArray<byte>(desc.buffer_size);
345   desc.instr_size = pc_offset();
346   desc.reloc_size =
347       static_cast<int>((buffer_ + buffer_size_) - (reloc_info_writer.pos()));
348
349   // Clear the buffer in debug mode. Use 'int3' instructions to make
350   // sure to get into problems if we ever run uninitialized code.
351 #ifdef DEBUG
352   memset(desc.buffer, 0xCC, desc.buffer_size);
353 #endif
354
355   // Copy the data.
356   intptr_t pc_delta = desc.buffer - buffer_;
357   intptr_t rc_delta = (desc.buffer + desc.buffer_size) -
358       (buffer_ + buffer_size_);
359   MemMove(desc.buffer, buffer_, desc.instr_size);
360   MemMove(rc_delta + reloc_info_writer.pos(), reloc_info_writer.pos(),
361           desc.reloc_size);
362
363   // Switch buffers.
364   DeleteArray(buffer_);
365   buffer_ = desc.buffer;
366   buffer_size_ = desc.buffer_size;
367   pc_ += pc_delta;
368   reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
369                                reloc_info_writer.last_pc() + pc_delta);
370
371   // Relocate runtime entries.
372   for (RelocIterator it(desc); !it.done(); it.next()) {
373     RelocInfo::Mode rmode = it.rinfo()->rmode();
374     if (rmode == RelocInfo::INTERNAL_REFERENCE) {
375       intptr_t* p = reinterpret_cast<intptr_t*>(it.rinfo()->pc());
376       if (*p != 0) {  // 0 means uninitialized.
377         *p += pc_delta;
378       }
379     }
380   }
381
382   DCHECK(!buffer_overflow());
383 }
384
385
386 void Assembler::emit_operand(int code, const Operand& adr) {
387   DCHECK(is_uint3(code));
388   const unsigned length = adr.len_;
389   DCHECK(length > 0);
390
391   // Emit updated ModR/M byte containing the given register.
392   DCHECK((adr.buf_[0] & 0x38) == 0);
393   pc_[0] = adr.buf_[0] | code << 3;
394
395   // Emit the rest of the encoded operand.
396   for (unsigned i = 1; i < length; i++) pc_[i] = adr.buf_[i];
397   pc_ += length;
398 }
399
400
401 // Assembler Instruction implementations.
402
403 void Assembler::arithmetic_op(byte opcode,
404                               Register reg,
405                               const Operand& op,
406                               int size) {
407   EnsureSpace ensure_space(this);
408   emit_rex(reg, op, size);
409   emit(opcode);
410   emit_operand(reg, op);
411 }
412
413
414 void Assembler::arithmetic_op(byte opcode,
415                               Register reg,
416                               Register rm_reg,
417                               int size) {
418   EnsureSpace ensure_space(this);
419   DCHECK((opcode & 0xC6) == 2);
420   if (rm_reg.low_bits() == 4)  {  // Forces SIB byte.
421     // Swap reg and rm_reg and change opcode operand order.
422     emit_rex(rm_reg, reg, size);
423     emit(opcode ^ 0x02);
424     emit_modrm(rm_reg, reg);
425   } else {
426     emit_rex(reg, rm_reg, size);
427     emit(opcode);
428     emit_modrm(reg, rm_reg);
429   }
430 }
431
432
433 void Assembler::arithmetic_op_16(byte opcode, Register reg, Register rm_reg) {
434   EnsureSpace ensure_space(this);
435   DCHECK((opcode & 0xC6) == 2);
436   if (rm_reg.low_bits() == 4) {  // Forces SIB byte.
437     // Swap reg and rm_reg and change opcode operand order.
438     emit(0x66);
439     emit_optional_rex_32(rm_reg, reg);
440     emit(opcode ^ 0x02);
441     emit_modrm(rm_reg, reg);
442   } else {
443     emit(0x66);
444     emit_optional_rex_32(reg, rm_reg);
445     emit(opcode);
446     emit_modrm(reg, rm_reg);
447   }
448 }
449
450
451 void Assembler::arithmetic_op_16(byte opcode,
452                                  Register reg,
453                                  const Operand& rm_reg) {
454   EnsureSpace ensure_space(this);
455   emit(0x66);
456   emit_optional_rex_32(reg, rm_reg);
457   emit(opcode);
458   emit_operand(reg, rm_reg);
459 }
460
461
462 void Assembler::arithmetic_op_8(byte opcode, Register reg, const Operand& op) {
463   EnsureSpace ensure_space(this);
464   if (!reg.is_byte_register()) {
465     // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
466     emit_rex_32(reg);
467   }
468   emit(opcode);
469   emit_operand(reg, op);
470 }
471
472
473 void Assembler::arithmetic_op_8(byte opcode, Register reg, Register rm_reg) {
474   EnsureSpace ensure_space(this);
475   DCHECK((opcode & 0xC6) == 2);
476   if (rm_reg.low_bits() == 4)  {  // Forces SIB byte.
477     // Swap reg and rm_reg and change opcode operand order.
478     if (!rm_reg.is_byte_register() || !reg.is_byte_register()) {
479       // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
480       emit_rex_32(rm_reg, reg);
481     }
482     emit(opcode ^ 0x02);
483     emit_modrm(rm_reg, reg);
484   } else {
485     if (!reg.is_byte_register() || !rm_reg.is_byte_register()) {
486       // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
487       emit_rex_32(reg, rm_reg);
488     }
489     emit(opcode);
490     emit_modrm(reg, rm_reg);
491   }
492 }
493
494
495 void Assembler::immediate_arithmetic_op(byte subcode,
496                                         Register dst,
497                                         Immediate src,
498                                         int size) {
499   EnsureSpace ensure_space(this);
500   emit_rex(dst, size);
501   if (is_int8(src.value_)) {
502     emit(0x83);
503     emit_modrm(subcode, dst);
504     emit(src.value_);
505   } else if (dst.is(rax)) {
506     emit(0x05 | (subcode << 3));
507     emitl(src.value_);
508   } else {
509     emit(0x81);
510     emit_modrm(subcode, dst);
511     emitl(src.value_);
512   }
513 }
514
515 void Assembler::immediate_arithmetic_op(byte subcode,
516                                         const Operand& dst,
517                                         Immediate src,
518                                         int size) {
519   EnsureSpace ensure_space(this);
520   emit_rex(dst, size);
521   if (is_int8(src.value_)) {
522     emit(0x83);
523     emit_operand(subcode, dst);
524     emit(src.value_);
525   } else {
526     emit(0x81);
527     emit_operand(subcode, dst);
528     emitl(src.value_);
529   }
530 }
531
532
533 void Assembler::immediate_arithmetic_op_16(byte subcode,
534                                            Register dst,
535                                            Immediate src) {
536   EnsureSpace ensure_space(this);
537   emit(0x66);  // Operand size override prefix.
538   emit_optional_rex_32(dst);
539   if (is_int8(src.value_)) {
540     emit(0x83);
541     emit_modrm(subcode, dst);
542     emit(src.value_);
543   } else if (dst.is(rax)) {
544     emit(0x05 | (subcode << 3));
545     emitw(src.value_);
546   } else {
547     emit(0x81);
548     emit_modrm(subcode, dst);
549     emitw(src.value_);
550   }
551 }
552
553
554 void Assembler::immediate_arithmetic_op_16(byte subcode,
555                                            const Operand& dst,
556                                            Immediate src) {
557   EnsureSpace ensure_space(this);
558   emit(0x66);  // Operand size override prefix.
559   emit_optional_rex_32(dst);
560   if (is_int8(src.value_)) {
561     emit(0x83);
562     emit_operand(subcode, dst);
563     emit(src.value_);
564   } else {
565     emit(0x81);
566     emit_operand(subcode, dst);
567     emitw(src.value_);
568   }
569 }
570
571
572 void Assembler::immediate_arithmetic_op_8(byte subcode,
573                                           const Operand& dst,
574                                           Immediate src) {
575   EnsureSpace ensure_space(this);
576   emit_optional_rex_32(dst);
577   DCHECK(is_int8(src.value_) || is_uint8(src.value_));
578   emit(0x80);
579   emit_operand(subcode, dst);
580   emit(src.value_);
581 }
582
583
584 void Assembler::immediate_arithmetic_op_8(byte subcode,
585                                           Register dst,
586                                           Immediate src) {
587   EnsureSpace ensure_space(this);
588   if (!dst.is_byte_register()) {
589     // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
590     emit_rex_32(dst);
591   }
592   DCHECK(is_int8(src.value_) || is_uint8(src.value_));
593   emit(0x80);
594   emit_modrm(subcode, dst);
595   emit(src.value_);
596 }
597
598
599 void Assembler::shift(Register dst,
600                       Immediate shift_amount,
601                       int subcode,
602                       int size) {
603   EnsureSpace ensure_space(this);
604   DCHECK(size == kInt64Size ? is_uint6(shift_amount.value_)
605                             : is_uint5(shift_amount.value_));
606   if (shift_amount.value_ == 1) {
607     emit_rex(dst, size);
608     emit(0xD1);
609     emit_modrm(subcode, dst);
610   } else {
611     emit_rex(dst, size);
612     emit(0xC1);
613     emit_modrm(subcode, dst);
614     emit(shift_amount.value_);
615   }
616 }
617
618
619 void Assembler::shift(Register dst, int subcode, int size) {
620   EnsureSpace ensure_space(this);
621   emit_rex(dst, size);
622   emit(0xD3);
623   emit_modrm(subcode, dst);
624 }
625
626
627 void Assembler::bt(const Operand& dst, Register src) {
628   EnsureSpace ensure_space(this);
629   emit_rex_64(src, dst);
630   emit(0x0F);
631   emit(0xA3);
632   emit_operand(src, dst);
633 }
634
635
636 void Assembler::bts(const Operand& dst, Register src) {
637   EnsureSpace ensure_space(this);
638   emit_rex_64(src, dst);
639   emit(0x0F);
640   emit(0xAB);
641   emit_operand(src, dst);
642 }
643
644
645 void Assembler::bsrl(Register dst, Register src) {
646   EnsureSpace ensure_space(this);
647   emit_optional_rex_32(dst, src);
648   emit(0x0F);
649   emit(0xBD);
650   emit_modrm(dst, src);
651 }
652
653
654 void Assembler::call(Label* L) {
655   positions_recorder()->WriteRecordedPositions();
656   EnsureSpace ensure_space(this);
657   // 1110 1000 #32-bit disp.
658   emit(0xE8);
659   if (L->is_bound()) {
660     int offset = L->pos() - pc_offset() - sizeof(int32_t);
661     DCHECK(offset <= 0);
662     emitl(offset);
663   } else if (L->is_linked()) {
664     emitl(L->pos());
665     L->link_to(pc_offset() - sizeof(int32_t));
666   } else {
667     DCHECK(L->is_unused());
668     int32_t current = pc_offset();
669     emitl(current);
670     L->link_to(current);
671   }
672 }
673
674
675 void Assembler::call(Address entry, RelocInfo::Mode rmode) {
676   DCHECK(RelocInfo::IsRuntimeEntry(rmode));
677   positions_recorder()->WriteRecordedPositions();
678   EnsureSpace ensure_space(this);
679   // 1110 1000 #32-bit disp.
680   emit(0xE8);
681   emit_runtime_entry(entry, rmode);
682 }
683
684
685 void Assembler::call(Handle<Code> target,
686                      RelocInfo::Mode rmode,
687                      TypeFeedbackId ast_id) {
688   positions_recorder()->WriteRecordedPositions();
689   EnsureSpace ensure_space(this);
690   // 1110 1000 #32-bit disp.
691   emit(0xE8);
692   emit_code_target(target, rmode, ast_id);
693 }
694
695
696 void Assembler::call(Register adr) {
697   positions_recorder()->WriteRecordedPositions();
698   EnsureSpace ensure_space(this);
699   // Opcode: FF /2 r64.
700   emit_optional_rex_32(adr);
701   emit(0xFF);
702   emit_modrm(0x2, adr);
703 }
704
705
706 void Assembler::call(const Operand& op) {
707   positions_recorder()->WriteRecordedPositions();
708   EnsureSpace ensure_space(this);
709   // Opcode: FF /2 m64.
710   emit_optional_rex_32(op);
711   emit(0xFF);
712   emit_operand(0x2, op);
713 }
714
715
716 // Calls directly to the given address using a relative offset.
717 // Should only ever be used in Code objects for calls within the
718 // same Code object. Should not be used when generating new code (use labels),
719 // but only when patching existing code.
720 void Assembler::call(Address target) {
721   positions_recorder()->WriteRecordedPositions();
722   EnsureSpace ensure_space(this);
723   // 1110 1000 #32-bit disp.
724   emit(0xE8);
725   Address source = pc_ + 4;
726   intptr_t displacement = target - source;
727   DCHECK(is_int32(displacement));
728   emitl(static_cast<int32_t>(displacement));
729 }
730
731
732 void Assembler::clc() {
733   EnsureSpace ensure_space(this);
734   emit(0xF8);
735 }
736
737
738 void Assembler::cld() {
739   EnsureSpace ensure_space(this);
740   emit(0xFC);
741 }
742
743
744 void Assembler::cdq() {
745   EnsureSpace ensure_space(this);
746   emit(0x99);
747 }
748
749
750 void Assembler::cmovq(Condition cc, Register dst, Register src) {
751   if (cc == always) {
752     movq(dst, src);
753   } else if (cc == never) {
754     return;
755   }
756   // No need to check CpuInfo for CMOV support, it's a required part of the
757   // 64-bit architecture.
758   DCHECK(cc >= 0);  // Use mov for unconditional moves.
759   EnsureSpace ensure_space(this);
760   // Opcode: REX.W 0f 40 + cc /r.
761   emit_rex_64(dst, src);
762   emit(0x0f);
763   emit(0x40 + cc);
764   emit_modrm(dst, src);
765 }
766
767
768 void Assembler::cmovq(Condition cc, Register dst, const Operand& src) {
769   if (cc == always) {
770     movq(dst, src);
771   } else if (cc == never) {
772     return;
773   }
774   DCHECK(cc >= 0);
775   EnsureSpace ensure_space(this);
776   // Opcode: REX.W 0f 40 + cc /r.
777   emit_rex_64(dst, src);
778   emit(0x0f);
779   emit(0x40 + cc);
780   emit_operand(dst, src);
781 }
782
783
784 void Assembler::cmovl(Condition cc, Register dst, Register src) {
785   if (cc == always) {
786     movl(dst, src);
787   } else if (cc == never) {
788     return;
789   }
790   DCHECK(cc >= 0);
791   EnsureSpace ensure_space(this);
792   // Opcode: 0f 40 + cc /r.
793   emit_optional_rex_32(dst, src);
794   emit(0x0f);
795   emit(0x40 + cc);
796   emit_modrm(dst, src);
797 }
798
799
800 void Assembler::cmovl(Condition cc, Register dst, const Operand& src) {
801   if (cc == always) {
802     movl(dst, src);
803   } else if (cc == never) {
804     return;
805   }
806   DCHECK(cc >= 0);
807   EnsureSpace ensure_space(this);
808   // Opcode: 0f 40 + cc /r.
809   emit_optional_rex_32(dst, src);
810   emit(0x0f);
811   emit(0x40 + cc);
812   emit_operand(dst, src);
813 }
814
815
816 void Assembler::cmpb_al(Immediate imm8) {
817   DCHECK(is_int8(imm8.value_) || is_uint8(imm8.value_));
818   EnsureSpace ensure_space(this);
819   emit(0x3c);
820   emit(imm8.value_);
821 }
822
823
824 void Assembler::cpuid() {
825   EnsureSpace ensure_space(this);
826   emit(0x0F);
827   emit(0xA2);
828 }
829
830
831 void Assembler::cqo() {
832   EnsureSpace ensure_space(this);
833   emit_rex_64();
834   emit(0x99);
835 }
836
837
838 void Assembler::emit_dec(Register dst, int size) {
839   EnsureSpace ensure_space(this);
840   emit_rex(dst, size);
841   emit(0xFF);
842   emit_modrm(0x1, dst);
843 }
844
845
846 void Assembler::emit_dec(const Operand& dst, int size) {
847   EnsureSpace ensure_space(this);
848   emit_rex(dst, size);
849   emit(0xFF);
850   emit_operand(1, dst);
851 }
852
853
854 void Assembler::decb(Register dst) {
855   EnsureSpace ensure_space(this);
856   if (!dst.is_byte_register()) {
857     // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
858     emit_rex_32(dst);
859   }
860   emit(0xFE);
861   emit_modrm(0x1, dst);
862 }
863
864
865 void Assembler::decb(const Operand& dst) {
866   EnsureSpace ensure_space(this);
867   emit_optional_rex_32(dst);
868   emit(0xFE);
869   emit_operand(1, dst);
870 }
871
872
873 void Assembler::enter(Immediate size) {
874   EnsureSpace ensure_space(this);
875   emit(0xC8);
876   emitw(size.value_);  // 16 bit operand, always.
877   emit(0);
878 }
879
880
881 void Assembler::hlt() {
882   EnsureSpace ensure_space(this);
883   emit(0xF4);
884 }
885
886
887 void Assembler::emit_idiv(Register src, int size) {
888   EnsureSpace ensure_space(this);
889   emit_rex(src, size);
890   emit(0xF7);
891   emit_modrm(0x7, src);
892 }
893
894
895 void Assembler::emit_div(Register src, int size) {
896   EnsureSpace ensure_space(this);
897   emit_rex(src, size);
898   emit(0xF7);
899   emit_modrm(0x6, src);
900 }
901
902
903 void Assembler::emit_imul(Register src, int size) {
904   EnsureSpace ensure_space(this);
905   emit_rex(src, size);
906   emit(0xF7);
907   emit_modrm(0x5, src);
908 }
909
910
911 void Assembler::emit_imul(Register dst, Register src, int size) {
912   EnsureSpace ensure_space(this);
913   emit_rex(dst, src, size);
914   emit(0x0F);
915   emit(0xAF);
916   emit_modrm(dst, src);
917 }
918
919
920 void Assembler::emit_imul(Register dst, const Operand& src, int size) {
921   EnsureSpace ensure_space(this);
922   emit_rex(dst, src, size);
923   emit(0x0F);
924   emit(0xAF);
925   emit_operand(dst, src);
926 }
927
928
929 void Assembler::emit_imul(Register dst, Register src, Immediate imm, int size) {
930   EnsureSpace ensure_space(this);
931   emit_rex(dst, src, size);
932   if (is_int8(imm.value_)) {
933     emit(0x6B);
934     emit_modrm(dst, src);
935     emit(imm.value_);
936   } else {
937     emit(0x69);
938     emit_modrm(dst, src);
939     emitl(imm.value_);
940   }
941 }
942
943
944 void Assembler::emit_inc(Register dst, int size) {
945   EnsureSpace ensure_space(this);
946   emit_rex(dst, size);
947   emit(0xFF);
948   emit_modrm(0x0, dst);
949 }
950
951
952 void Assembler::emit_inc(const Operand& dst, int size) {
953   EnsureSpace ensure_space(this);
954   emit_rex(dst, size);
955   emit(0xFF);
956   emit_operand(0, dst);
957 }
958
959
960 void Assembler::int3() {
961   EnsureSpace ensure_space(this);
962   emit(0xCC);
963 }
964
965
966 void Assembler::j(Condition cc, Label* L, Label::Distance distance) {
967   if (cc == always) {
968     jmp(L);
969     return;
970   } else if (cc == never) {
971     return;
972   }
973   EnsureSpace ensure_space(this);
974   DCHECK(is_uint4(cc));
975   if (L->is_bound()) {
976     const int short_size = 2;
977     const int long_size  = 6;
978     int offs = L->pos() - pc_offset();
979     DCHECK(offs <= 0);
980     // Determine whether we can use 1-byte offsets for backwards branches,
981     // which have a max range of 128 bytes.
982
983     // We also need to check predictable_code_size() flag here, because on x64,
984     // when the full code generator recompiles code for debugging, some places
985     // need to be padded out to a certain size. The debugger is keeping track of
986     // how often it did this so that it can adjust return addresses on the
987     // stack, but if the size of jump instructions can also change, that's not
988     // enough and the calculated offsets would be incorrect.
989     if (is_int8(offs - short_size) && !predictable_code_size()) {
990       // 0111 tttn #8-bit disp.
991       emit(0x70 | cc);
992       emit((offs - short_size) & 0xFF);
993     } else {
994       // 0000 1111 1000 tttn #32-bit disp.
995       emit(0x0F);
996       emit(0x80 | cc);
997       emitl(offs - long_size);
998     }
999   } else if (distance == Label::kNear) {
1000     // 0111 tttn #8-bit disp
1001     emit(0x70 | cc);
1002     byte disp = 0x00;
1003     if (L->is_near_linked()) {
1004       int offset = L->near_link_pos() - pc_offset();
1005       DCHECK(is_int8(offset));
1006       disp = static_cast<byte>(offset & 0xFF);
1007     }
1008     L->link_to(pc_offset(), Label::kNear);
1009     emit(disp);
1010   } else if (L->is_linked()) {
1011     // 0000 1111 1000 tttn #32-bit disp.
1012     emit(0x0F);
1013     emit(0x80 | cc);
1014     emitl(L->pos());
1015     L->link_to(pc_offset() - sizeof(int32_t));
1016   } else {
1017     DCHECK(L->is_unused());
1018     emit(0x0F);
1019     emit(0x80 | cc);
1020     int32_t current = pc_offset();
1021     emitl(current);
1022     L->link_to(current);
1023   }
1024 }
1025
1026
1027 void Assembler::j(Condition cc, Address entry, RelocInfo::Mode rmode) {
1028   DCHECK(RelocInfo::IsRuntimeEntry(rmode));
1029   EnsureSpace ensure_space(this);
1030   DCHECK(is_uint4(cc));
1031   emit(0x0F);
1032   emit(0x80 | cc);
1033   emit_runtime_entry(entry, rmode);
1034 }
1035
1036
1037 void Assembler::j(Condition cc,
1038                   Handle<Code> target,
1039                   RelocInfo::Mode rmode) {
1040   EnsureSpace ensure_space(this);
1041   DCHECK(is_uint4(cc));
1042   // 0000 1111 1000 tttn #32-bit disp.
1043   emit(0x0F);
1044   emit(0x80 | cc);
1045   emit_code_target(target, rmode);
1046 }
1047
1048
1049 void Assembler::jmp(Label* L, Label::Distance distance) {
1050   EnsureSpace ensure_space(this);
1051   const int short_size = sizeof(int8_t);
1052   const int long_size = sizeof(int32_t);
1053   if (L->is_bound()) {
1054     int offs = L->pos() - pc_offset() - 1;
1055     DCHECK(offs <= 0);
1056     if (is_int8(offs - short_size) && !predictable_code_size()) {
1057       // 1110 1011 #8-bit disp.
1058       emit(0xEB);
1059       emit((offs - short_size) & 0xFF);
1060     } else {
1061       // 1110 1001 #32-bit disp.
1062       emit(0xE9);
1063       emitl(offs - long_size);
1064     }
1065   } else if (distance == Label::kNear) {
1066     emit(0xEB);
1067     byte disp = 0x00;
1068     if (L->is_near_linked()) {
1069       int offset = L->near_link_pos() - pc_offset();
1070       DCHECK(is_int8(offset));
1071       disp = static_cast<byte>(offset & 0xFF);
1072     }
1073     L->link_to(pc_offset(), Label::kNear);
1074     emit(disp);
1075   } else if (L->is_linked()) {
1076     // 1110 1001 #32-bit disp.
1077     emit(0xE9);
1078     emitl(L->pos());
1079     L->link_to(pc_offset() - long_size);
1080   } else {
1081     // 1110 1001 #32-bit disp.
1082     DCHECK(L->is_unused());
1083     emit(0xE9);
1084     int32_t current = pc_offset();
1085     emitl(current);
1086     L->link_to(current);
1087   }
1088 }
1089
1090
1091 void Assembler::jmp(Handle<Code> target, RelocInfo::Mode rmode) {
1092   EnsureSpace ensure_space(this);
1093   // 1110 1001 #32-bit disp.
1094   emit(0xE9);
1095   emit_code_target(target, rmode);
1096 }
1097
1098
1099 void Assembler::jmp(Address entry, RelocInfo::Mode rmode) {
1100   DCHECK(RelocInfo::IsRuntimeEntry(rmode));
1101   EnsureSpace ensure_space(this);
1102   DCHECK(RelocInfo::IsRuntimeEntry(rmode));
1103   emit(0xE9);
1104   emit_runtime_entry(entry, rmode);
1105 }
1106
1107
1108 void Assembler::jmp(Register target) {
1109   EnsureSpace ensure_space(this);
1110   // Opcode FF/4 r64.
1111   emit_optional_rex_32(target);
1112   emit(0xFF);
1113   emit_modrm(0x4, target);
1114 }
1115
1116
1117 void Assembler::jmp(const Operand& src) {
1118   EnsureSpace ensure_space(this);
1119   // Opcode FF/4 m64.
1120   emit_optional_rex_32(src);
1121   emit(0xFF);
1122   emit_operand(0x4, src);
1123 }
1124
1125
1126 void Assembler::emit_lea(Register dst, const Operand& src, int size) {
1127   EnsureSpace ensure_space(this);
1128   emit_rex(dst, src, size);
1129   emit(0x8D);
1130   emit_operand(dst, src);
1131 }
1132
1133
1134 void Assembler::load_rax(void* value, RelocInfo::Mode mode) {
1135   EnsureSpace ensure_space(this);
1136   if (kPointerSize == kInt64Size) {
1137     emit(0x48);  // REX.W
1138     emit(0xA1);
1139     emitp(value, mode);
1140   } else {
1141     DCHECK(kPointerSize == kInt32Size);
1142     emit(0xA1);
1143     emitp(value, mode);
1144     // In 64-bit mode, need to zero extend the operand to 8 bytes.
1145     // See 2.2.1.4 in Intel64 and IA32 Architectures Software
1146     // Developer's Manual Volume 2.
1147     emitl(0);
1148   }
1149 }
1150
1151
1152 void Assembler::load_rax(ExternalReference ref) {
1153   load_rax(ref.address(), RelocInfo::EXTERNAL_REFERENCE);
1154 }
1155
1156
1157 void Assembler::leave() {
1158   EnsureSpace ensure_space(this);
1159   emit(0xC9);
1160 }
1161
1162
1163 void Assembler::movb(Register dst, const Operand& src) {
1164   EnsureSpace ensure_space(this);
1165   if (!dst.is_byte_register()) {
1166     // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
1167     emit_rex_32(dst, src);
1168   } else {
1169     emit_optional_rex_32(dst, src);
1170   }
1171   emit(0x8A);
1172   emit_operand(dst, src);
1173 }
1174
1175
1176 void Assembler::movb(Register dst, Immediate imm) {
1177   EnsureSpace ensure_space(this);
1178   if (!dst.is_byte_register()) {
1179     emit_rex_32(dst);
1180   }
1181   emit(0xB0 + dst.low_bits());
1182   emit(imm.value_);
1183 }
1184
1185
1186 void Assembler::movb(const Operand& dst, Register src) {
1187   EnsureSpace ensure_space(this);
1188   if (!src.is_byte_register()) {
1189     emit_rex_32(src, dst);
1190   } else {
1191     emit_optional_rex_32(src, dst);
1192   }
1193   emit(0x88);
1194   emit_operand(src, dst);
1195 }
1196
1197
1198 void Assembler::movb(const Operand& dst, Immediate imm) {
1199   EnsureSpace ensure_space(this);
1200   emit_optional_rex_32(dst);
1201   emit(0xC6);
1202   emit_operand(0x0, dst);
1203   emit(static_cast<byte>(imm.value_));
1204 }
1205
1206
1207 void Assembler::movw(Register dst, const Operand& src) {
1208   EnsureSpace ensure_space(this);
1209   emit(0x66);
1210   emit_optional_rex_32(dst, src);
1211   emit(0x8B);
1212   emit_operand(dst, src);
1213 }
1214
1215
1216 void Assembler::movw(const Operand& dst, Register src) {
1217   EnsureSpace ensure_space(this);
1218   emit(0x66);
1219   emit_optional_rex_32(src, dst);
1220   emit(0x89);
1221   emit_operand(src, dst);
1222 }
1223
1224
1225 void Assembler::movw(const Operand& dst, Immediate imm) {
1226   EnsureSpace ensure_space(this);
1227   emit(0x66);
1228   emit_optional_rex_32(dst);
1229   emit(0xC7);
1230   emit_operand(0x0, dst);
1231   emit(static_cast<byte>(imm.value_ & 0xff));
1232   emit(static_cast<byte>(imm.value_ >> 8));
1233 }
1234
1235
1236 void Assembler::emit_mov(Register dst, const Operand& src, int size) {
1237   EnsureSpace ensure_space(this);
1238   emit_rex(dst, src, size);
1239   emit(0x8B);
1240   emit_operand(dst, src);
1241 }
1242
1243
1244 void Assembler::emit_mov(Register dst, Register src, int size) {
1245   EnsureSpace ensure_space(this);
1246   if (src.low_bits() == 4) {
1247     emit_rex(src, dst, size);
1248     emit(0x89);
1249     emit_modrm(src, dst);
1250   } else {
1251     emit_rex(dst, src, size);
1252     emit(0x8B);
1253     emit_modrm(dst, src);
1254   }
1255 }
1256
1257
1258 void Assembler::emit_mov(const Operand& dst, Register src, int size) {
1259   EnsureSpace ensure_space(this);
1260   emit_rex(src, dst, size);
1261   emit(0x89);
1262   emit_operand(src, dst);
1263 }
1264
1265
1266 void Assembler::emit_mov(Register dst, Immediate value, int size) {
1267   EnsureSpace ensure_space(this);
1268   emit_rex(dst, size);
1269   if (size == kInt64Size) {
1270     emit(0xC7);
1271     emit_modrm(0x0, dst);
1272   } else {
1273     DCHECK(size == kInt32Size);
1274     emit(0xB8 + dst.low_bits());
1275   }
1276   emit(value);
1277 }
1278
1279
1280 void Assembler::emit_mov(const Operand& dst, Immediate value, int size) {
1281   EnsureSpace ensure_space(this);
1282   emit_rex(dst, size);
1283   emit(0xC7);
1284   emit_operand(0x0, dst);
1285   emit(value);
1286 }
1287
1288
1289 void Assembler::movp(Register dst, void* value, RelocInfo::Mode rmode) {
1290   EnsureSpace ensure_space(this);
1291   emit_rex(dst, kPointerSize);
1292   emit(0xB8 | dst.low_bits());
1293   emitp(value, rmode);
1294 }
1295
1296
1297 void Assembler::movq(Register dst, int64_t value) {
1298   EnsureSpace ensure_space(this);
1299   emit_rex_64(dst);
1300   emit(0xB8 | dst.low_bits());
1301   emitq(value);
1302 }
1303
1304
1305 void Assembler::movq(Register dst, uint64_t value) {
1306   movq(dst, static_cast<int64_t>(value));
1307 }
1308
1309
1310 // Loads the ip-relative location of the src label into the target location
1311 // (as a 32-bit offset sign extended to 64-bit).
1312 void Assembler::movl(const Operand& dst, Label* src) {
1313   EnsureSpace ensure_space(this);
1314   emit_optional_rex_32(dst);
1315   emit(0xC7);
1316   emit_operand(0, dst);
1317   if (src->is_bound()) {
1318     int offset = src->pos() - pc_offset() - sizeof(int32_t);
1319     DCHECK(offset <= 0);
1320     emitl(offset);
1321   } else if (src->is_linked()) {
1322     emitl(src->pos());
1323     src->link_to(pc_offset() - sizeof(int32_t));
1324   } else {
1325     DCHECK(src->is_unused());
1326     int32_t current = pc_offset();
1327     emitl(current);
1328     src->link_to(current);
1329   }
1330 }
1331
1332
1333 void Assembler::movsxbl(Register dst, const Operand& src) {
1334   EnsureSpace ensure_space(this);
1335   emit_optional_rex_32(dst, src);
1336   emit(0x0F);
1337   emit(0xBE);
1338   emit_operand(dst, src);
1339 }
1340
1341
1342 void Assembler::movsxbq(Register dst, const Operand& src) {
1343   EnsureSpace ensure_space(this);
1344   emit_rex_64(dst, src);
1345   emit(0x0F);
1346   emit(0xBE);
1347   emit_operand(dst, src);
1348 }
1349
1350
1351 void Assembler::movsxwl(Register dst, const Operand& src) {
1352   EnsureSpace ensure_space(this);
1353   emit_optional_rex_32(dst, src);
1354   emit(0x0F);
1355   emit(0xBF);
1356   emit_operand(dst, src);
1357 }
1358
1359
1360 void Assembler::movsxwq(Register dst, const Operand& src) {
1361   EnsureSpace ensure_space(this);
1362   emit_rex_64(dst, src);
1363   emit(0x0F);
1364   emit(0xBF);
1365   emit_operand(dst, src);
1366 }
1367
1368
1369 void Assembler::movsxlq(Register dst, Register src) {
1370   EnsureSpace ensure_space(this);
1371   emit_rex_64(dst, src);
1372   emit(0x63);
1373   emit_modrm(dst, src);
1374 }
1375
1376
1377 void Assembler::movsxlq(Register dst, const Operand& src) {
1378   EnsureSpace ensure_space(this);
1379   emit_rex_64(dst, src);
1380   emit(0x63);
1381   emit_operand(dst, src);
1382 }
1383
1384
1385 void Assembler::emit_movzxb(Register dst, const Operand& src, int size) {
1386   EnsureSpace ensure_space(this);
1387   // 32 bit operations zero the top 32 bits of 64 bit registers.  Therefore
1388   // there is no need to make this a 64 bit operation.
1389   emit_optional_rex_32(dst, src);
1390   emit(0x0F);
1391   emit(0xB6);
1392   emit_operand(dst, src);
1393 }
1394
1395
1396 void Assembler::emit_movzxb(Register dst, Register src, int size) {
1397   EnsureSpace ensure_space(this);
1398   // 32 bit operations zero the top 32 bits of 64 bit registers.  Therefore
1399   // there is no need to make this a 64 bit operation.
1400   emit_optional_rex_32(dst, src);
1401   emit(0x0F);
1402   emit(0xB6);
1403   emit_modrm(dst, src);
1404 }
1405
1406
1407 void Assembler::emit_movzxw(Register dst, const Operand& src, int size) {
1408   EnsureSpace ensure_space(this);
1409   // 32 bit operations zero the top 32 bits of 64 bit registers.  Therefore
1410   // there is no need to make this a 64 bit operation.
1411   emit_optional_rex_32(dst, src);
1412   emit(0x0F);
1413   emit(0xB7);
1414   emit_operand(dst, src);
1415 }
1416
1417
1418 void Assembler::emit_movzxw(Register dst, Register src, int size) {
1419   EnsureSpace ensure_space(this);
1420   // 32 bit operations zero the top 32 bits of 64 bit registers.  Therefore
1421   // there is no need to make this a 64 bit operation.
1422   emit_optional_rex_32(dst, src);
1423   emit(0x0F);
1424   emit(0xB7);
1425   emit_modrm(dst, src);
1426 }
1427
1428
1429 void Assembler::repmovsb() {
1430   EnsureSpace ensure_space(this);
1431   emit(0xF3);
1432   emit(0xA4);
1433 }
1434
1435
1436 void Assembler::repmovsw() {
1437   EnsureSpace ensure_space(this);
1438   emit(0x66);  // Operand size override.
1439   emit(0xF3);
1440   emit(0xA4);
1441 }
1442
1443
1444 void Assembler::emit_repmovs(int size) {
1445   EnsureSpace ensure_space(this);
1446   emit(0xF3);
1447   emit_rex(size);
1448   emit(0xA5);
1449 }
1450
1451
1452 void Assembler::mul(Register src) {
1453   EnsureSpace ensure_space(this);
1454   emit_rex_64(src);
1455   emit(0xF7);
1456   emit_modrm(0x4, src);
1457 }
1458
1459
1460 void Assembler::emit_neg(Register dst, int size) {
1461   EnsureSpace ensure_space(this);
1462   emit_rex(dst, size);
1463   emit(0xF7);
1464   emit_modrm(0x3, dst);
1465 }
1466
1467
1468 void Assembler::emit_neg(const Operand& dst, int size) {
1469   EnsureSpace ensure_space(this);
1470   emit_rex_64(dst);
1471   emit(0xF7);
1472   emit_operand(3, dst);
1473 }
1474
1475
1476 void Assembler::nop() {
1477   EnsureSpace ensure_space(this);
1478   emit(0x90);
1479 }
1480
1481
1482 void Assembler::emit_not(Register dst, int size) {
1483   EnsureSpace ensure_space(this);
1484   emit_rex(dst, size);
1485   emit(0xF7);
1486   emit_modrm(0x2, dst);
1487 }
1488
1489
1490 void Assembler::emit_not(const Operand& dst, int size) {
1491   EnsureSpace ensure_space(this);
1492   emit_rex(dst, size);
1493   emit(0xF7);
1494   emit_operand(2, dst);
1495 }
1496
1497
1498 void Assembler::Nop(int n) {
1499   // The recommended muti-byte sequences of NOP instructions from the Intel 64
1500   // and IA-32 Architectures Software Developer's Manual.
1501   //
1502   // Length   Assembly                                Byte Sequence
1503   // 2 bytes  66 NOP                                  66 90H
1504   // 3 bytes  NOP DWORD ptr [EAX]                     0F 1F 00H
1505   // 4 bytes  NOP DWORD ptr [EAX + 00H]               0F 1F 40 00H
1506   // 5 bytes  NOP DWORD ptr [EAX + EAX*1 + 00H]       0F 1F 44 00 00H
1507   // 6 bytes  66 NOP DWORD ptr [EAX + EAX*1 + 00H]    66 0F 1F 44 00 00H
1508   // 7 bytes  NOP DWORD ptr [EAX + 00000000H]         0F 1F 80 00 00 00 00H
1509   // 8 bytes  NOP DWORD ptr [EAX + EAX*1 + 00000000H] 0F 1F 84 00 00 00 00 00H
1510   // 9 bytes  66 NOP DWORD ptr [EAX + EAX*1 +         66 0F 1F 84 00 00 00 00
1511   //          00000000H]                              00H
1512
1513   EnsureSpace ensure_space(this);
1514   while (n > 0) {
1515     switch (n) {
1516       case 2:
1517         emit(0x66);
1518       case 1:
1519         emit(0x90);
1520         return;
1521       case 3:
1522         emit(0x0f);
1523         emit(0x1f);
1524         emit(0x00);
1525         return;
1526       case 4:
1527         emit(0x0f);
1528         emit(0x1f);
1529         emit(0x40);
1530         emit(0x00);
1531         return;
1532       case 6:
1533         emit(0x66);
1534       case 5:
1535         emit(0x0f);
1536         emit(0x1f);
1537         emit(0x44);
1538         emit(0x00);
1539         emit(0x00);
1540         return;
1541       case 7:
1542         emit(0x0f);
1543         emit(0x1f);
1544         emit(0x80);
1545         emit(0x00);
1546         emit(0x00);
1547         emit(0x00);
1548         emit(0x00);
1549         return;
1550       default:
1551       case 11:
1552         emit(0x66);
1553         n--;
1554       case 10:
1555         emit(0x66);
1556         n--;
1557       case 9:
1558         emit(0x66);
1559         n--;
1560       case 8:
1561         emit(0x0f);
1562         emit(0x1f);
1563         emit(0x84);
1564         emit(0x00);
1565         emit(0x00);
1566         emit(0x00);
1567         emit(0x00);
1568         emit(0x00);
1569         n -= 8;
1570     }
1571   }
1572 }
1573
1574
1575 void Assembler::popq(Register dst) {
1576   EnsureSpace ensure_space(this);
1577   emit_optional_rex_32(dst);
1578   emit(0x58 | dst.low_bits());
1579 }
1580
1581
1582 void Assembler::popq(const Operand& dst) {
1583   EnsureSpace ensure_space(this);
1584   emit_optional_rex_32(dst);
1585   emit(0x8F);
1586   emit_operand(0, dst);
1587 }
1588
1589
1590 void Assembler::popfq() {
1591   EnsureSpace ensure_space(this);
1592   emit(0x9D);
1593 }
1594
1595
1596 void Assembler::pushq(Register src) {
1597   EnsureSpace ensure_space(this);
1598   emit_optional_rex_32(src);
1599   emit(0x50 | src.low_bits());
1600 }
1601
1602
1603 void Assembler::pushq(const Operand& src) {
1604   EnsureSpace ensure_space(this);
1605   emit_optional_rex_32(src);
1606   emit(0xFF);
1607   emit_operand(6, src);
1608 }
1609
1610
1611 void Assembler::pushq(Immediate value) {
1612   EnsureSpace ensure_space(this);
1613   if (is_int8(value.value_)) {
1614     emit(0x6A);
1615     emit(value.value_);  // Emit low byte of value.
1616   } else {
1617     emit(0x68);
1618     emitl(value.value_);
1619   }
1620 }
1621
1622
1623 void Assembler::pushq_imm32(int32_t imm32) {
1624   EnsureSpace ensure_space(this);
1625   emit(0x68);
1626   emitl(imm32);
1627 }
1628
1629
1630 void Assembler::pushfq() {
1631   EnsureSpace ensure_space(this);
1632   emit(0x9C);
1633 }
1634
1635
1636 void Assembler::ret(int imm16) {
1637   EnsureSpace ensure_space(this);
1638   DCHECK(is_uint16(imm16));
1639   if (imm16 == 0) {
1640     emit(0xC3);
1641   } else {
1642     emit(0xC2);
1643     emit(imm16 & 0xFF);
1644     emit((imm16 >> 8) & 0xFF);
1645   }
1646 }
1647
1648
1649 void Assembler::setcc(Condition cc, Register reg) {
1650   if (cc > last_condition) {
1651     movb(reg, Immediate(cc == always ? 1 : 0));
1652     return;
1653   }
1654   EnsureSpace ensure_space(this);
1655   DCHECK(is_uint4(cc));
1656   if (!reg.is_byte_register()) {  // Use x64 byte registers, where different.
1657     emit_rex_32(reg);
1658   }
1659   emit(0x0F);
1660   emit(0x90 | cc);
1661   emit_modrm(0x0, reg);
1662 }
1663
1664
1665 void Assembler::shld(Register dst, Register src) {
1666   EnsureSpace ensure_space(this);
1667   emit_rex_64(src, dst);
1668   emit(0x0F);
1669   emit(0xA5);
1670   emit_modrm(src, dst);
1671 }
1672
1673
1674 void Assembler::shrd(Register dst, Register src) {
1675   EnsureSpace ensure_space(this);
1676   emit_rex_64(src, dst);
1677   emit(0x0F);
1678   emit(0xAD);
1679   emit_modrm(src, dst);
1680 }
1681
1682
1683 void Assembler::emit_xchg(Register dst, Register src, int size) {
1684   EnsureSpace ensure_space(this);
1685   if (src.is(rax) || dst.is(rax)) {  // Single-byte encoding
1686     Register other = src.is(rax) ? dst : src;
1687     emit_rex(other, size);
1688     emit(0x90 | other.low_bits());
1689   } else if (dst.low_bits() == 4) {
1690     emit_rex(dst, src, size);
1691     emit(0x87);
1692     emit_modrm(dst, src);
1693   } else {
1694     emit_rex(src, dst, size);
1695     emit(0x87);
1696     emit_modrm(src, dst);
1697   }
1698 }
1699
1700
1701 void Assembler::emit_xchg(Register dst, const Operand& src, int size) {
1702   EnsureSpace ensure_space(this);
1703   emit_rex(dst, src, size);
1704   emit(0x87);
1705   emit_operand(dst, src);
1706 }
1707
1708
1709 void Assembler::store_rax(void* dst, RelocInfo::Mode mode) {
1710   EnsureSpace ensure_space(this);
1711   if (kPointerSize == kInt64Size) {
1712     emit(0x48);  // REX.W
1713     emit(0xA3);
1714     emitp(dst, mode);
1715   } else {
1716     DCHECK(kPointerSize == kInt32Size);
1717     emit(0xA3);
1718     emitp(dst, mode);
1719     // In 64-bit mode, need to zero extend the operand to 8 bytes.
1720     // See 2.2.1.4 in Intel64 and IA32 Architectures Software
1721     // Developer's Manual Volume 2.
1722     emitl(0);
1723   }
1724 }
1725
1726
1727 void Assembler::store_rax(ExternalReference ref) {
1728   store_rax(ref.address(), RelocInfo::EXTERNAL_REFERENCE);
1729 }
1730
1731
1732 void Assembler::testb(Register dst, Register src) {
1733   EnsureSpace ensure_space(this);
1734   if (src.low_bits() == 4) {
1735     emit_rex_32(src, dst);
1736     emit(0x84);
1737     emit_modrm(src, dst);
1738   } else {
1739     if (!dst.is_byte_register() || !src.is_byte_register()) {
1740       // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
1741       emit_rex_32(dst, src);
1742     }
1743     emit(0x84);
1744     emit_modrm(dst, src);
1745   }
1746 }
1747
1748
1749 void Assembler::testb(Register reg, Immediate mask) {
1750   DCHECK(is_int8(mask.value_) || is_uint8(mask.value_));
1751   EnsureSpace ensure_space(this);
1752   if (reg.is(rax)) {
1753     emit(0xA8);
1754     emit(mask.value_);  // Low byte emitted.
1755   } else {
1756     if (!reg.is_byte_register()) {
1757       // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
1758       emit_rex_32(reg);
1759     }
1760     emit(0xF6);
1761     emit_modrm(0x0, reg);
1762     emit(mask.value_);  // Low byte emitted.
1763   }
1764 }
1765
1766
1767 void Assembler::testb(const Operand& op, Immediate mask) {
1768   DCHECK(is_int8(mask.value_) || is_uint8(mask.value_));
1769   EnsureSpace ensure_space(this);
1770   emit_optional_rex_32(rax, op);
1771   emit(0xF6);
1772   emit_operand(rax, op);  // Operation code 0
1773   emit(mask.value_);  // Low byte emitted.
1774 }
1775
1776
1777 void Assembler::testb(const Operand& op, Register reg) {
1778   EnsureSpace ensure_space(this);
1779   if (!reg.is_byte_register()) {
1780     // Register is not one of al, bl, cl, dl.  Its encoding needs REX.
1781     emit_rex_32(reg, op);
1782   } else {
1783     emit_optional_rex_32(reg, op);
1784   }
1785   emit(0x84);
1786   emit_operand(reg, op);
1787 }
1788
1789
1790 void Assembler::emit_test(Register dst, Register src, int size) {
1791   EnsureSpace ensure_space(this);
1792   if (src.low_bits() == 4) {
1793     emit_rex(src, dst, size);
1794     emit(0x85);
1795     emit_modrm(src, dst);
1796   } else {
1797     emit_rex(dst, src, size);
1798     emit(0x85);
1799     emit_modrm(dst, src);
1800   }
1801 }
1802
1803
1804 void Assembler::emit_test(Register reg, Immediate mask, int size) {
1805   // testl with a mask that fits in the low byte is exactly testb.
1806   if (is_uint8(mask.value_)) {
1807     testb(reg, mask);
1808     return;
1809   }
1810   EnsureSpace ensure_space(this);
1811   if (reg.is(rax)) {
1812     emit_rex(rax, size);
1813     emit(0xA9);
1814     emit(mask);
1815   } else {
1816     emit_rex(reg, size);
1817     emit(0xF7);
1818     emit_modrm(0x0, reg);
1819     emit(mask);
1820   }
1821 }
1822
1823
1824 void Assembler::emit_test(const Operand& op, Immediate mask, int size) {
1825   // testl with a mask that fits in the low byte is exactly testb.
1826   if (is_uint8(mask.value_)) {
1827     testb(op, mask);
1828     return;
1829   }
1830   EnsureSpace ensure_space(this);
1831   emit_rex(rax, op, size);
1832   emit(0xF7);
1833   emit_operand(rax, op);  // Operation code 0
1834   emit(mask);
1835 }
1836
1837
1838 void Assembler::emit_test(const Operand& op, Register reg, int size) {
1839   EnsureSpace ensure_space(this);
1840   emit_rex(reg, op, size);
1841   emit(0x85);
1842   emit_operand(reg, op);
1843 }
1844
1845
1846 // FPU instructions.
1847
1848
1849 void Assembler::fld(int i) {
1850   EnsureSpace ensure_space(this);
1851   emit_farith(0xD9, 0xC0, i);
1852 }
1853
1854
1855 void Assembler::fld1() {
1856   EnsureSpace ensure_space(this);
1857   emit(0xD9);
1858   emit(0xE8);
1859 }
1860
1861
1862 void Assembler::fldz() {
1863   EnsureSpace ensure_space(this);
1864   emit(0xD9);
1865   emit(0xEE);
1866 }
1867
1868
1869 void Assembler::fldpi() {
1870   EnsureSpace ensure_space(this);
1871   emit(0xD9);
1872   emit(0xEB);
1873 }
1874
1875
1876 void Assembler::fldln2() {
1877   EnsureSpace ensure_space(this);
1878   emit(0xD9);
1879   emit(0xED);
1880 }
1881
1882
1883 void Assembler::fld_s(const Operand& adr) {
1884   EnsureSpace ensure_space(this);
1885   emit_optional_rex_32(adr);
1886   emit(0xD9);
1887   emit_operand(0, adr);
1888 }
1889
1890
1891 void Assembler::fld_d(const Operand& adr) {
1892   EnsureSpace ensure_space(this);
1893   emit_optional_rex_32(adr);
1894   emit(0xDD);
1895   emit_operand(0, adr);
1896 }
1897
1898
1899 void Assembler::fstp_s(const Operand& adr) {
1900   EnsureSpace ensure_space(this);
1901   emit_optional_rex_32(adr);
1902   emit(0xD9);
1903   emit_operand(3, adr);
1904 }
1905
1906
1907 void Assembler::fstp_d(const Operand& adr) {
1908   EnsureSpace ensure_space(this);
1909   emit_optional_rex_32(adr);
1910   emit(0xDD);
1911   emit_operand(3, adr);
1912 }
1913
1914
1915 void Assembler::fstp(int index) {
1916   DCHECK(is_uint3(index));
1917   EnsureSpace ensure_space(this);
1918   emit_farith(0xDD, 0xD8, index);
1919 }
1920
1921
1922 void Assembler::fild_s(const Operand& adr) {
1923   EnsureSpace ensure_space(this);
1924   emit_optional_rex_32(adr);
1925   emit(0xDB);
1926   emit_operand(0, adr);
1927 }
1928
1929
1930 void Assembler::fild_d(const Operand& adr) {
1931   EnsureSpace ensure_space(this);
1932   emit_optional_rex_32(adr);
1933   emit(0xDF);
1934   emit_operand(5, adr);
1935 }
1936
1937
1938 void Assembler::fistp_s(const Operand& adr) {
1939   EnsureSpace ensure_space(this);
1940   emit_optional_rex_32(adr);
1941   emit(0xDB);
1942   emit_operand(3, adr);
1943 }
1944
1945
1946 void Assembler::fisttp_s(const Operand& adr) {
1947   DCHECK(IsEnabled(SSE3));
1948   EnsureSpace ensure_space(this);
1949   emit_optional_rex_32(adr);
1950   emit(0xDB);
1951   emit_operand(1, adr);
1952 }
1953
1954
1955 void Assembler::fisttp_d(const Operand& adr) {
1956   DCHECK(IsEnabled(SSE3));
1957   EnsureSpace ensure_space(this);
1958   emit_optional_rex_32(adr);
1959   emit(0xDD);
1960   emit_operand(1, adr);
1961 }
1962
1963
1964 void Assembler::fist_s(const Operand& adr) {
1965   EnsureSpace ensure_space(this);
1966   emit_optional_rex_32(adr);
1967   emit(0xDB);
1968   emit_operand(2, adr);
1969 }
1970
1971
1972 void Assembler::fistp_d(const Operand& adr) {
1973   EnsureSpace ensure_space(this);
1974   emit_optional_rex_32(adr);
1975   emit(0xDF);
1976   emit_operand(7, adr);
1977 }
1978
1979
1980 void Assembler::fabs() {
1981   EnsureSpace ensure_space(this);
1982   emit(0xD9);
1983   emit(0xE1);
1984 }
1985
1986
1987 void Assembler::fchs() {
1988   EnsureSpace ensure_space(this);
1989   emit(0xD9);
1990   emit(0xE0);
1991 }
1992
1993
1994 void Assembler::fcos() {
1995   EnsureSpace ensure_space(this);
1996   emit(0xD9);
1997   emit(0xFF);
1998 }
1999
2000
2001 void Assembler::fsin() {
2002   EnsureSpace ensure_space(this);
2003   emit(0xD9);
2004   emit(0xFE);
2005 }
2006
2007
2008 void Assembler::fptan() {
2009   EnsureSpace ensure_space(this);
2010   emit(0xD9);
2011   emit(0xF2);
2012 }
2013
2014
2015 void Assembler::fyl2x() {
2016   EnsureSpace ensure_space(this);
2017   emit(0xD9);
2018   emit(0xF1);
2019 }
2020
2021
2022 void Assembler::f2xm1() {
2023   EnsureSpace ensure_space(this);
2024   emit(0xD9);
2025   emit(0xF0);
2026 }
2027
2028
2029 void Assembler::fscale() {
2030   EnsureSpace ensure_space(this);
2031   emit(0xD9);
2032   emit(0xFD);
2033 }
2034
2035
2036 void Assembler::fninit() {
2037   EnsureSpace ensure_space(this);
2038   emit(0xDB);
2039   emit(0xE3);
2040 }
2041
2042
2043 void Assembler::fadd(int i) {
2044   EnsureSpace ensure_space(this);
2045   emit_farith(0xDC, 0xC0, i);
2046 }
2047
2048
2049 void Assembler::fsub(int i) {
2050   EnsureSpace ensure_space(this);
2051   emit_farith(0xDC, 0xE8, i);
2052 }
2053
2054
2055 void Assembler::fisub_s(const Operand& adr) {
2056   EnsureSpace ensure_space(this);
2057   emit_optional_rex_32(adr);
2058   emit(0xDA);
2059   emit_operand(4, adr);
2060 }
2061
2062
2063 void Assembler::fmul(int i) {
2064   EnsureSpace ensure_space(this);
2065   emit_farith(0xDC, 0xC8, i);
2066 }
2067
2068
2069 void Assembler::fdiv(int i) {
2070   EnsureSpace ensure_space(this);
2071   emit_farith(0xDC, 0xF8, i);
2072 }
2073
2074
2075 void Assembler::faddp(int i) {
2076   EnsureSpace ensure_space(this);
2077   emit_farith(0xDE, 0xC0, i);
2078 }
2079
2080
2081 void Assembler::fsubp(int i) {
2082   EnsureSpace ensure_space(this);
2083   emit_farith(0xDE, 0xE8, i);
2084 }
2085
2086
2087 void Assembler::fsubrp(int i) {
2088   EnsureSpace ensure_space(this);
2089   emit_farith(0xDE, 0xE0, i);
2090 }
2091
2092
2093 void Assembler::fmulp(int i) {
2094   EnsureSpace ensure_space(this);
2095   emit_farith(0xDE, 0xC8, i);
2096 }
2097
2098
2099 void Assembler::fdivp(int i) {
2100   EnsureSpace ensure_space(this);
2101   emit_farith(0xDE, 0xF8, i);
2102 }
2103
2104
2105 void Assembler::fprem() {
2106   EnsureSpace ensure_space(this);
2107   emit(0xD9);
2108   emit(0xF8);
2109 }
2110
2111
2112 void Assembler::fprem1() {
2113   EnsureSpace ensure_space(this);
2114   emit(0xD9);
2115   emit(0xF5);
2116 }
2117
2118
2119 void Assembler::fxch(int i) {
2120   EnsureSpace ensure_space(this);
2121   emit_farith(0xD9, 0xC8, i);
2122 }
2123
2124
2125 void Assembler::fincstp() {
2126   EnsureSpace ensure_space(this);
2127   emit(0xD9);
2128   emit(0xF7);
2129 }
2130
2131
2132 void Assembler::ffree(int i) {
2133   EnsureSpace ensure_space(this);
2134   emit_farith(0xDD, 0xC0, i);
2135 }
2136
2137
2138 void Assembler::ftst() {
2139   EnsureSpace ensure_space(this);
2140   emit(0xD9);
2141   emit(0xE4);
2142 }
2143
2144
2145 void Assembler::fucomp(int i) {
2146   EnsureSpace ensure_space(this);
2147   emit_farith(0xDD, 0xE8, i);
2148 }
2149
2150
2151 void Assembler::fucompp() {
2152   EnsureSpace ensure_space(this);
2153   emit(0xDA);
2154   emit(0xE9);
2155 }
2156
2157
2158 void Assembler::fucomi(int i) {
2159   EnsureSpace ensure_space(this);
2160   emit(0xDB);
2161   emit(0xE8 + i);
2162 }
2163
2164
2165 void Assembler::fucomip() {
2166   EnsureSpace ensure_space(this);
2167   emit(0xDF);
2168   emit(0xE9);
2169 }
2170
2171
2172 void Assembler::fcompp() {
2173   EnsureSpace ensure_space(this);
2174   emit(0xDE);
2175   emit(0xD9);
2176 }
2177
2178
2179 void Assembler::fnstsw_ax() {
2180   EnsureSpace ensure_space(this);
2181   emit(0xDF);
2182   emit(0xE0);
2183 }
2184
2185
2186 void Assembler::fwait() {
2187   EnsureSpace ensure_space(this);
2188   emit(0x9B);
2189 }
2190
2191
2192 void Assembler::frndint() {
2193   EnsureSpace ensure_space(this);
2194   emit(0xD9);
2195   emit(0xFC);
2196 }
2197
2198
2199 void Assembler::fnclex() {
2200   EnsureSpace ensure_space(this);
2201   emit(0xDB);
2202   emit(0xE2);
2203 }
2204
2205
2206 void Assembler::sahf() {
2207   // TODO(X64): Test for presence. Not all 64-bit intel CPU's have sahf
2208   // in 64-bit mode. Test CpuID.
2209   DCHECK(IsEnabled(SAHF));
2210   EnsureSpace ensure_space(this);
2211   emit(0x9E);
2212 }
2213
2214
2215 void Assembler::emit_farith(int b1, int b2, int i) {
2216   DCHECK(is_uint8(b1) && is_uint8(b2));  // wrong opcode
2217   DCHECK(is_uint3(i));  // illegal stack offset
2218   emit(b1);
2219   emit(b2 + i);
2220 }
2221
2222
2223 // SSE operations.
2224
2225 void Assembler::andps(XMMRegister dst, XMMRegister src) {
2226   EnsureSpace ensure_space(this);
2227   emit_optional_rex_32(dst, src);
2228   emit(0x0F);
2229   emit(0x54);
2230   emit_sse_operand(dst, src);
2231 }
2232
2233
2234 void Assembler::andps(XMMRegister dst, const Operand& src) {
2235   EnsureSpace ensure_space(this);
2236   emit_optional_rex_32(dst, src);
2237   emit(0x0F);
2238   emit(0x54);
2239   emit_sse_operand(dst, src);
2240 }
2241
2242
2243 void Assembler::orps(XMMRegister dst, XMMRegister src) {
2244   EnsureSpace ensure_space(this);
2245   emit_optional_rex_32(dst, src);
2246   emit(0x0F);
2247   emit(0x56);
2248   emit_sse_operand(dst, src);
2249 }
2250
2251
2252 void Assembler::orps(XMMRegister dst, const Operand& src) {
2253   EnsureSpace ensure_space(this);
2254   emit_optional_rex_32(dst, src);
2255   emit(0x0F);
2256   emit(0x56);
2257   emit_sse_operand(dst, src);
2258 }
2259
2260
2261 void Assembler::xorps(XMMRegister dst, XMMRegister src) {
2262   EnsureSpace ensure_space(this);
2263   emit_optional_rex_32(dst, src);
2264   emit(0x0F);
2265   emit(0x57);
2266   emit_sse_operand(dst, src);
2267 }
2268
2269
2270 void Assembler::xorps(XMMRegister dst, const Operand& src) {
2271   EnsureSpace ensure_space(this);
2272   emit_optional_rex_32(dst, src);
2273   emit(0x0F);
2274   emit(0x57);
2275   emit_sse_operand(dst, src);
2276 }
2277
2278
2279 void Assembler::addps(XMMRegister dst, XMMRegister src) {
2280   EnsureSpace ensure_space(this);
2281   emit_optional_rex_32(dst, src);
2282   emit(0x0F);
2283   emit(0x58);
2284   emit_sse_operand(dst, src);
2285 }
2286
2287
2288 void Assembler::addps(XMMRegister dst, const Operand& src) {
2289   EnsureSpace ensure_space(this);
2290   emit_optional_rex_32(dst, src);
2291   emit(0x0F);
2292   emit(0x58);
2293   emit_sse_operand(dst, src);
2294 }
2295
2296
2297 void Assembler::subps(XMMRegister dst, XMMRegister src) {
2298   EnsureSpace ensure_space(this);
2299   emit_optional_rex_32(dst, src);
2300   emit(0x0F);
2301   emit(0x5C);
2302   emit_sse_operand(dst, src);
2303 }
2304
2305
2306 void Assembler::subps(XMMRegister dst, const Operand& src) {
2307   EnsureSpace ensure_space(this);
2308   emit_optional_rex_32(dst, src);
2309   emit(0x0F);
2310   emit(0x5C);
2311   emit_sse_operand(dst, src);
2312 }
2313
2314
2315 void Assembler::mulps(XMMRegister dst, XMMRegister src) {
2316   EnsureSpace ensure_space(this);
2317   emit_optional_rex_32(dst, src);
2318   emit(0x0F);
2319   emit(0x59);
2320   emit_sse_operand(dst, src);
2321 }
2322
2323
2324 void Assembler::mulps(XMMRegister dst, const Operand& src) {
2325   EnsureSpace ensure_space(this);
2326   emit_optional_rex_32(dst, src);
2327   emit(0x0F);
2328   emit(0x59);
2329   emit_sse_operand(dst, src);
2330 }
2331
2332
2333 void Assembler::divps(XMMRegister dst, XMMRegister src) {
2334   EnsureSpace ensure_space(this);
2335   emit_optional_rex_32(dst, src);
2336   emit(0x0F);
2337   emit(0x5E);
2338   emit_sse_operand(dst, src);
2339 }
2340
2341
2342 void Assembler::divps(XMMRegister dst, const Operand& src) {
2343   EnsureSpace ensure_space(this);
2344   emit_optional_rex_32(dst, src);
2345   emit(0x0F);
2346   emit(0x5E);
2347   emit_sse_operand(dst, src);
2348 }
2349
2350
2351 // SSE 2 operations.
2352
2353 void Assembler::movd(XMMRegister dst, Register src) {
2354   EnsureSpace ensure_space(this);
2355   emit(0x66);
2356   emit_optional_rex_32(dst, src);
2357   emit(0x0F);
2358   emit(0x6E);
2359   emit_sse_operand(dst, src);
2360 }
2361
2362
2363 void Assembler::movd(Register dst, XMMRegister src) {
2364   EnsureSpace ensure_space(this);
2365   emit(0x66);
2366   emit_optional_rex_32(src, dst);
2367   emit(0x0F);
2368   emit(0x7E);
2369   emit_sse_operand(src, dst);
2370 }
2371
2372
2373 void Assembler::movq(XMMRegister dst, Register src) {
2374   EnsureSpace ensure_space(this);
2375   emit(0x66);
2376   emit_rex_64(dst, src);
2377   emit(0x0F);
2378   emit(0x6E);
2379   emit_sse_operand(dst, src);
2380 }
2381
2382
2383 void Assembler::movq(Register dst, XMMRegister src) {
2384   EnsureSpace ensure_space(this);
2385   emit(0x66);
2386   emit_rex_64(src, dst);
2387   emit(0x0F);
2388   emit(0x7E);
2389   emit_sse_operand(src, dst);
2390 }
2391
2392
2393 void Assembler::movq(XMMRegister dst, XMMRegister src) {
2394   EnsureSpace ensure_space(this);
2395   if (dst.low_bits() == 4) {
2396     // Avoid unnecessary SIB byte.
2397     emit(0xf3);
2398     emit_optional_rex_32(dst, src);
2399     emit(0x0F);
2400     emit(0x7e);
2401     emit_sse_operand(dst, src);
2402   } else {
2403     emit(0x66);
2404     emit_optional_rex_32(src, dst);
2405     emit(0x0F);
2406     emit(0xD6);
2407     emit_sse_operand(src, dst);
2408   }
2409 }
2410
2411
2412 void Assembler::movdqa(const Operand& dst, XMMRegister src) {
2413   EnsureSpace ensure_space(this);
2414   emit(0x66);
2415   emit_rex_64(src, dst);
2416   emit(0x0F);
2417   emit(0x7F);
2418   emit_sse_operand(src, dst);
2419 }
2420
2421
2422 void Assembler::movdqa(XMMRegister dst, const Operand& src) {
2423   EnsureSpace ensure_space(this);
2424   emit(0x66);
2425   emit_rex_64(dst, src);
2426   emit(0x0F);
2427   emit(0x6F);
2428   emit_sse_operand(dst, src);
2429 }
2430
2431
2432 void Assembler::movdqu(const Operand& dst, XMMRegister src) {
2433   EnsureSpace ensure_space(this);
2434   emit(0xF3);
2435   emit_rex_64(src, dst);
2436   emit(0x0F);
2437   emit(0x7F);
2438   emit_sse_operand(src, dst);
2439 }
2440
2441
2442 void Assembler::movdqu(XMMRegister dst, const Operand& src) {
2443   EnsureSpace ensure_space(this);
2444   emit(0xF3);
2445   emit_rex_64(dst, src);
2446   emit(0x0F);
2447   emit(0x6F);
2448   emit_sse_operand(dst, src);
2449 }
2450
2451
2452 void Assembler::extractps(Register dst, XMMRegister src, byte imm8) {
2453   DCHECK(IsEnabled(SSE4_1));
2454   DCHECK(is_uint8(imm8));
2455   EnsureSpace ensure_space(this);
2456   emit(0x66);
2457   emit_optional_rex_32(src, dst);
2458   emit(0x0F);
2459   emit(0x3A);
2460   emit(0x17);
2461   emit_sse_operand(src, dst);
2462   emit(imm8);
2463 }
2464
2465
2466 void Assembler::movsd(const Operand& dst, XMMRegister src) {
2467   EnsureSpace ensure_space(this);
2468   emit(0xF2);  // double
2469   emit_optional_rex_32(src, dst);
2470   emit(0x0F);
2471   emit(0x11);  // store
2472   emit_sse_operand(src, dst);
2473 }
2474
2475
2476 void Assembler::movsd(XMMRegister dst, XMMRegister src) {
2477   EnsureSpace ensure_space(this);
2478   emit(0xF2);  // double
2479   emit_optional_rex_32(dst, src);
2480   emit(0x0F);
2481   emit(0x10);  // load
2482   emit_sse_operand(dst, src);
2483 }
2484
2485
2486 void Assembler::movsd(XMMRegister dst, const Operand& src) {
2487   EnsureSpace ensure_space(this);
2488   emit(0xF2);  // double
2489   emit_optional_rex_32(dst, src);
2490   emit(0x0F);
2491   emit(0x10);  // load
2492   emit_sse_operand(dst, src);
2493 }
2494
2495
2496 void Assembler::movaps(XMMRegister dst, XMMRegister src) {
2497   EnsureSpace ensure_space(this);
2498   if (src.low_bits() == 4) {
2499     // Try to avoid an unnecessary SIB byte.
2500     emit_optional_rex_32(src, dst);
2501     emit(0x0F);
2502     emit(0x29);
2503     emit_sse_operand(src, dst);
2504   } else {
2505     emit_optional_rex_32(dst, src);
2506     emit(0x0F);
2507     emit(0x28);
2508     emit_sse_operand(dst, src);
2509   }
2510 }
2511
2512
2513 void Assembler::shufps(XMMRegister dst, XMMRegister src, byte imm8) {
2514   DCHECK(is_uint8(imm8));
2515   EnsureSpace ensure_space(this);
2516   emit_optional_rex_32(src, dst);
2517   emit(0x0F);
2518   emit(0xC6);
2519   emit_sse_operand(dst, src);
2520   emit(imm8);
2521 }
2522
2523
2524 void Assembler::movapd(XMMRegister dst, XMMRegister src) {
2525   EnsureSpace ensure_space(this);
2526   if (src.low_bits() == 4) {
2527     // Try to avoid an unnecessary SIB byte.
2528     emit(0x66);
2529     emit_optional_rex_32(src, dst);
2530     emit(0x0F);
2531     emit(0x29);
2532     emit_sse_operand(src, dst);
2533   } else {
2534     emit(0x66);
2535     emit_optional_rex_32(dst, src);
2536     emit(0x0F);
2537     emit(0x28);
2538     emit_sse_operand(dst, src);
2539   }
2540 }
2541
2542
2543 void Assembler::movss(XMMRegister dst, const Operand& src) {
2544   EnsureSpace ensure_space(this);
2545   emit(0xF3);  // single
2546   emit_optional_rex_32(dst, src);
2547   emit(0x0F);
2548   emit(0x10);  // load
2549   emit_sse_operand(dst, src);
2550 }
2551
2552
2553 void Assembler::movss(const Operand& src, XMMRegister dst) {
2554   EnsureSpace ensure_space(this);
2555   emit(0xF3);  // single
2556   emit_optional_rex_32(dst, src);
2557   emit(0x0F);
2558   emit(0x11);  // store
2559   emit_sse_operand(dst, src);
2560 }
2561
2562
2563 void Assembler::psllq(XMMRegister reg, byte imm8) {
2564   EnsureSpace ensure_space(this);
2565   emit(0x66);
2566   emit(0x0F);
2567   emit(0x73);
2568   emit_sse_operand(rsi, reg);  // rsi == 6
2569   emit(imm8);
2570 }
2571
2572
2573 void Assembler::cvttss2si(Register dst, const Operand& src) {
2574   EnsureSpace ensure_space(this);
2575   emit(0xF3);
2576   emit_optional_rex_32(dst, src);
2577   emit(0x0F);
2578   emit(0x2C);
2579   emit_operand(dst, src);
2580 }
2581
2582
2583 void Assembler::cvttss2si(Register dst, XMMRegister src) {
2584   EnsureSpace ensure_space(this);
2585   emit(0xF3);
2586   emit_optional_rex_32(dst, src);
2587   emit(0x0F);
2588   emit(0x2C);
2589   emit_sse_operand(dst, src);
2590 }
2591
2592
2593 void Assembler::cvttsd2si(Register dst, const Operand& src) {
2594   EnsureSpace ensure_space(this);
2595   emit(0xF2);
2596   emit_optional_rex_32(dst, src);
2597   emit(0x0F);
2598   emit(0x2C);
2599   emit_operand(dst, src);
2600 }
2601
2602
2603 void Assembler::cvttsd2si(Register dst, XMMRegister src) {
2604   EnsureSpace ensure_space(this);
2605   emit(0xF2);
2606   emit_optional_rex_32(dst, src);
2607   emit(0x0F);
2608   emit(0x2C);
2609   emit_sse_operand(dst, src);
2610 }
2611
2612
2613 void Assembler::cvttsd2siq(Register dst, XMMRegister src) {
2614   EnsureSpace ensure_space(this);
2615   emit(0xF2);
2616   emit_rex_64(dst, src);
2617   emit(0x0F);
2618   emit(0x2C);
2619   emit_sse_operand(dst, src);
2620 }
2621
2622
2623 void Assembler::cvtlsi2sd(XMMRegister dst, const Operand& src) {
2624   EnsureSpace ensure_space(this);
2625   emit(0xF2);
2626   emit_optional_rex_32(dst, src);
2627   emit(0x0F);
2628   emit(0x2A);
2629   emit_sse_operand(dst, src);
2630 }
2631
2632
2633 void Assembler::cvtlsi2sd(XMMRegister dst, Register src) {
2634   EnsureSpace ensure_space(this);
2635   emit(0xF2);
2636   emit_optional_rex_32(dst, src);
2637   emit(0x0F);
2638   emit(0x2A);
2639   emit_sse_operand(dst, src);
2640 }
2641
2642
2643 void Assembler::cvtlsi2ss(XMMRegister dst, Register src) {
2644   EnsureSpace ensure_space(this);
2645   emit(0xF3);
2646   emit_optional_rex_32(dst, src);
2647   emit(0x0F);
2648   emit(0x2A);
2649   emit_sse_operand(dst, src);
2650 }
2651
2652
2653 void Assembler::cvtqsi2sd(XMMRegister dst, Register src) {
2654   EnsureSpace ensure_space(this);
2655   emit(0xF2);
2656   emit_rex_64(dst, src);
2657   emit(0x0F);
2658   emit(0x2A);
2659   emit_sse_operand(dst, src);
2660 }
2661
2662
2663 void Assembler::cvtss2sd(XMMRegister dst, XMMRegister src) {
2664   EnsureSpace ensure_space(this);
2665   emit(0xF3);
2666   emit_optional_rex_32(dst, src);
2667   emit(0x0F);
2668   emit(0x5A);
2669   emit_sse_operand(dst, src);
2670 }
2671
2672
2673 void Assembler::cvtss2sd(XMMRegister dst, const Operand& src) {
2674   EnsureSpace ensure_space(this);
2675   emit(0xF3);
2676   emit_optional_rex_32(dst, src);
2677   emit(0x0F);
2678   emit(0x5A);
2679   emit_sse_operand(dst, src);
2680 }
2681
2682
2683 void Assembler::cvtsd2ss(XMMRegister dst, XMMRegister src) {
2684   EnsureSpace ensure_space(this);
2685   emit(0xF2);
2686   emit_optional_rex_32(dst, src);
2687   emit(0x0F);
2688   emit(0x5A);
2689   emit_sse_operand(dst, src);
2690 }
2691
2692
2693 void Assembler::cvtsd2si(Register dst, XMMRegister src) {
2694   EnsureSpace ensure_space(this);
2695   emit(0xF2);
2696   emit_optional_rex_32(dst, src);
2697   emit(0x0F);
2698   emit(0x2D);
2699   emit_sse_operand(dst, src);
2700 }
2701
2702
2703 void Assembler::cvtsd2siq(Register dst, XMMRegister src) {
2704   EnsureSpace ensure_space(this);
2705   emit(0xF2);
2706   emit_rex_64(dst, src);
2707   emit(0x0F);
2708   emit(0x2D);
2709   emit_sse_operand(dst, src);
2710 }
2711
2712
2713 void Assembler::addsd(XMMRegister dst, XMMRegister src) {
2714   EnsureSpace ensure_space(this);
2715   emit(0xF2);
2716   emit_optional_rex_32(dst, src);
2717   emit(0x0F);
2718   emit(0x58);
2719   emit_sse_operand(dst, src);
2720 }
2721
2722
2723 void Assembler::addsd(XMMRegister dst, const Operand& src) {
2724   EnsureSpace ensure_space(this);
2725   emit(0xF2);
2726   emit_optional_rex_32(dst, src);
2727   emit(0x0F);
2728   emit(0x58);
2729   emit_sse_operand(dst, src);
2730 }
2731
2732
2733 void Assembler::mulsd(XMMRegister dst, XMMRegister src) {
2734   EnsureSpace ensure_space(this);
2735   emit(0xF2);
2736   emit_optional_rex_32(dst, src);
2737   emit(0x0F);
2738   emit(0x59);
2739   emit_sse_operand(dst, src);
2740 }
2741
2742
2743 void Assembler::mulsd(XMMRegister dst, const Operand& src) {
2744   EnsureSpace ensure_space(this);
2745   emit(0xF2);
2746   emit_optional_rex_32(dst, src);
2747   emit(0x0F);
2748   emit(0x59);
2749   emit_sse_operand(dst, src);
2750 }
2751
2752
2753 void Assembler::subsd(XMMRegister dst, XMMRegister src) {
2754   EnsureSpace ensure_space(this);
2755   emit(0xF2);
2756   emit_optional_rex_32(dst, src);
2757   emit(0x0F);
2758   emit(0x5C);
2759   emit_sse_operand(dst, src);
2760 }
2761
2762
2763 void Assembler::divsd(XMMRegister dst, XMMRegister src) {
2764   EnsureSpace ensure_space(this);
2765   emit(0xF2);
2766   emit_optional_rex_32(dst, src);
2767   emit(0x0F);
2768   emit(0x5E);
2769   emit_sse_operand(dst, src);
2770 }
2771
2772
2773 void Assembler::andpd(XMMRegister dst, XMMRegister src) {
2774   EnsureSpace ensure_space(this);
2775   emit(0x66);
2776   emit_optional_rex_32(dst, src);
2777   emit(0x0F);
2778   emit(0x54);
2779   emit_sse_operand(dst, src);
2780 }
2781
2782
2783 void Assembler::orpd(XMMRegister dst, XMMRegister src) {
2784   EnsureSpace ensure_space(this);
2785   emit(0x66);
2786   emit_optional_rex_32(dst, src);
2787   emit(0x0F);
2788   emit(0x56);
2789   emit_sse_operand(dst, src);
2790 }
2791
2792
2793 void Assembler::xorpd(XMMRegister dst, XMMRegister src) {
2794   EnsureSpace ensure_space(this);
2795   emit(0x66);
2796   emit_optional_rex_32(dst, src);
2797   emit(0x0F);
2798   emit(0x57);
2799   emit_sse_operand(dst, src);
2800 }
2801
2802
2803 void Assembler::sqrtsd(XMMRegister dst, XMMRegister src) {
2804   EnsureSpace ensure_space(this);
2805   emit(0xF2);
2806   emit_optional_rex_32(dst, src);
2807   emit(0x0F);
2808   emit(0x51);
2809   emit_sse_operand(dst, src);
2810 }
2811
2812
2813 void Assembler::sqrtsd(XMMRegister dst, const Operand& src) {
2814   EnsureSpace ensure_space(this);
2815   emit(0xF2);
2816   emit_optional_rex_32(dst, src);
2817   emit(0x0F);
2818   emit(0x51);
2819   emit_sse_operand(dst, src);
2820 }
2821
2822
2823 void Assembler::ucomisd(XMMRegister dst, XMMRegister src) {
2824   EnsureSpace ensure_space(this);
2825   emit(0x66);
2826   emit_optional_rex_32(dst, src);
2827   emit(0x0f);
2828   emit(0x2e);
2829   emit_sse_operand(dst, src);
2830 }
2831
2832
2833 void Assembler::ucomisd(XMMRegister dst, const Operand& src) {
2834   EnsureSpace ensure_space(this);
2835   emit(0x66);
2836   emit_optional_rex_32(dst, src);
2837   emit(0x0f);
2838   emit(0x2e);
2839   emit_sse_operand(dst, src);
2840 }
2841
2842
2843 void Assembler::cmpltsd(XMMRegister dst, XMMRegister src) {
2844   EnsureSpace ensure_space(this);
2845   emit(0xF2);
2846   emit_optional_rex_32(dst, src);
2847   emit(0x0F);
2848   emit(0xC2);
2849   emit_sse_operand(dst, src);
2850   emit(0x01);  // LT == 1
2851 }
2852
2853
2854 void Assembler::roundsd(XMMRegister dst, XMMRegister src,
2855                         Assembler::RoundingMode mode) {
2856   DCHECK(IsEnabled(SSE4_1));
2857   EnsureSpace ensure_space(this);
2858   emit(0x66);
2859   emit_optional_rex_32(dst, src);
2860   emit(0x0f);
2861   emit(0x3a);
2862   emit(0x0b);
2863   emit_sse_operand(dst, src);
2864   // Mask precision exeption.
2865   emit(static_cast<byte>(mode) | 0x8);
2866 }
2867
2868
2869 void Assembler::movmskpd(Register dst, XMMRegister src) {
2870   EnsureSpace ensure_space(this);
2871   emit(0x66);
2872   emit_optional_rex_32(dst, src);
2873   emit(0x0f);
2874   emit(0x50);
2875   emit_sse_operand(dst, src);
2876 }
2877
2878
2879 void Assembler::movmskps(Register dst, XMMRegister src) {
2880   EnsureSpace ensure_space(this);
2881   emit_optional_rex_32(dst, src);
2882   emit(0x0f);
2883   emit(0x50);
2884   emit_sse_operand(dst, src);
2885 }
2886
2887
2888 void Assembler::emit_sse_operand(XMMRegister reg, const Operand& adr) {
2889   Register ireg = { reg.code() };
2890   emit_operand(ireg, adr);
2891 }
2892
2893
2894 void Assembler::emit_sse_operand(XMMRegister dst, XMMRegister src) {
2895   emit(0xC0 | (dst.low_bits() << 3) | src.low_bits());
2896 }
2897
2898
2899 void Assembler::emit_sse_operand(XMMRegister dst, Register src) {
2900   emit(0xC0 | (dst.low_bits() << 3) | src.low_bits());
2901 }
2902
2903
2904 void Assembler::emit_sse_operand(Register dst, XMMRegister src) {
2905   emit(0xC0 | (dst.low_bits() << 3) | src.low_bits());
2906 }
2907
2908
2909 void Assembler::db(uint8_t data) {
2910   EnsureSpace ensure_space(this);
2911   emit(data);
2912 }
2913
2914
2915 void Assembler::dd(uint32_t data) {
2916   EnsureSpace ensure_space(this);
2917   emitl(data);
2918 }
2919
2920
2921 // Relocation information implementations.
2922
2923 void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
2924   DCHECK(!RelocInfo::IsNone(rmode));
2925   // Don't record external references unless the heap will be serialized.
2926   if (rmode == RelocInfo::EXTERNAL_REFERENCE &&
2927       !serializer_enabled() && !emit_debug_code()) {
2928     return;
2929   } else if (rmode == RelocInfo::CODE_AGE_SEQUENCE) {
2930     // Don't record psuedo relocation info for code age sequence mode.
2931     return;
2932   }
2933   RelocInfo rinfo(pc_, rmode, data, NULL);
2934   reloc_info_writer.Write(&rinfo);
2935 }
2936
2937
2938 void Assembler::RecordJSReturn() {
2939   positions_recorder()->WriteRecordedPositions();
2940   EnsureSpace ensure_space(this);
2941   RecordRelocInfo(RelocInfo::JS_RETURN);
2942 }
2943
2944
2945 void Assembler::RecordDebugBreakSlot() {
2946   positions_recorder()->WriteRecordedPositions();
2947   EnsureSpace ensure_space(this);
2948   RecordRelocInfo(RelocInfo::DEBUG_BREAK_SLOT);
2949 }
2950
2951
2952 void Assembler::RecordComment(const char* msg, bool force) {
2953   if (FLAG_code_comments || force) {
2954     EnsureSpace ensure_space(this);
2955     RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
2956   }
2957 }
2958
2959
2960 Handle<ConstantPoolArray> Assembler::NewConstantPool(Isolate* isolate) {
2961   // No out-of-line constant pool support.
2962   DCHECK(!FLAG_enable_ool_constant_pool);
2963   return isolate->factory()->empty_constant_pool_array();
2964 }
2965
2966
2967 void Assembler::PopulateConstantPool(ConstantPoolArray* constant_pool) {
2968   // No out-of-line constant pool support.
2969   DCHECK(!FLAG_enable_ool_constant_pool);
2970   return;
2971 }
2972
2973
2974 const int RelocInfo::kApplyMask = RelocInfo::kCodeTargetMask |
2975     1 << RelocInfo::RUNTIME_ENTRY |
2976     1 << RelocInfo::INTERNAL_REFERENCE |
2977     1 << RelocInfo::CODE_AGE_SEQUENCE;
2978
2979
2980 bool RelocInfo::IsCodedSpecially() {
2981   // The deserializer needs to know whether a pointer is specially coded.  Being
2982   // specially coded on x64 means that it is a relative 32 bit address, as used
2983   // by branch instructions.
2984   return (1 << rmode_) & kApplyMask;
2985 }
2986
2987
2988 bool RelocInfo::IsInConstantPool() {
2989   return false;
2990 }
2991
2992
2993 } }  // namespace v8::internal
2994
2995 #endif  // V8_TARGET_ARCH_X64