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