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