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