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