511b1c868867d2a3a75955c42e060e8dcb903b62
[platform/upstream/nodejs.git] / deps / v8 / src / ia32 / assembler-ia32.cc
1 // Copyright (c) 1994-2006 Sun Microsystems Inc.
2 // All Rights Reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
6 // are met:
7 //
8 // - Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // - Redistribution in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the
14 // distribution.
15 //
16 // - Neither the name of Sun Microsystems or the names of contributors may
17 // be used to endorse or promote products derived from this software without
18 // specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 // OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 // The original source code covered by the above license above has been modified
34 // significantly by Google Inc.
35 // Copyright 2012 the V8 project authors. All rights reserved.
36
37 #include "src/ia32/assembler-ia32.h"
38
39 #include <cstring>
40
41 #if V8_TARGET_ARCH_IA32
42
43 #if V8_LIBC_MSVCRT
44 #include <intrin.h>  // _xgetbv()
45 #endif
46 #if V8_OS_MACOSX
47 #include <sys/sysctl.h>
48 #endif
49
50 #include "src/base/bits.h"
51 #include "src/base/cpu.h"
52 #include "src/disassembler.h"
53 #include "src/macro-assembler.h"
54 #include "src/v8.h"
55
56 namespace v8 {
57 namespace internal {
58
59 // -----------------------------------------------------------------------------
60 // Implementation of CpuFeatures
61
62 namespace {
63
64 #if !V8_LIBC_MSVCRT
65
66 V8_INLINE uint64_t _xgetbv(unsigned int xcr) {
67   unsigned eax, edx;
68   // Check xgetbv; this uses a .byte sequence instead of the instruction
69   // directly because older assemblers do not include support for xgetbv and
70   // there is no easy way to conditionally compile based on the assembler
71   // used.
72   __asm__ volatile(".byte 0x0f, 0x01, 0xd0" : "=a"(eax), "=d"(edx) : "c"(xcr));
73   return static_cast<uint64_t>(eax) | (static_cast<uint64_t>(edx) << 32);
74 }
75
76 #define _XCR_XFEATURE_ENABLED_MASK 0
77
78 #endif  // !V8_LIBC_MSVCRT
79
80
81 bool OSHasAVXSupport() {
82 #if V8_OS_MACOSX
83   // Mac OS X up to 10.9 has a bug where AVX transitions were indeed being
84   // caused by ISRs, so we detect that here and disable AVX in that case.
85   char buffer[128];
86   size_t buffer_size = arraysize(buffer);
87   int ctl_name[] = {CTL_KERN, KERN_OSRELEASE};
88   if (sysctl(ctl_name, 2, buffer, &buffer_size, nullptr, 0) != 0) {
89     V8_Fatal(__FILE__, __LINE__, "V8 failed to get kernel version");
90   }
91   // The buffer now contains a string of the form XX.YY.ZZ, where
92   // XX is the major kernel version component.
93   char* period_pos = strchr(buffer, '.');
94   DCHECK_NOT_NULL(period_pos);
95   *period_pos = '\0';
96   long kernel_version_major = strtol(buffer, nullptr, 10);  // NOLINT
97   if (kernel_version_major <= 13) return false;
98 #endif  // V8_OS_MACOSX
99   // Check whether OS claims to support AVX.
100   uint64_t feature_mask = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
101   return (feature_mask & 0x6) == 0x6;
102 }
103
104 }  // namespace
105
106
107 void CpuFeatures::ProbeImpl(bool cross_compile) {
108   base::CPU cpu;
109   CHECK(cpu.has_sse2());  // SSE2 support is mandatory.
110   CHECK(cpu.has_cmov());  // CMOV support is mandatory.
111
112   // Only use statically determined features for cross compile (snapshot).
113   if (cross_compile) return;
114
115   if (cpu.has_sse41() && FLAG_enable_sse4_1) supported_ |= 1u << SSE4_1;
116   if (cpu.has_sse3() && FLAG_enable_sse3) supported_ |= 1u << SSE3;
117   if (cpu.has_avx() && FLAG_enable_avx && cpu.has_osxsave() &&
118       OSHasAVXSupport()) {
119     supported_ |= 1u << AVX;
120   }
121   if (cpu.has_fma3() && FLAG_enable_fma3 && cpu.has_osxsave() &&
122       OSHasAVXSupport()) {
123     supported_ |= 1u << FMA3;
124   }
125   if (strcmp(FLAG_mcpu, "auto") == 0) {
126     if (cpu.is_atom()) supported_ |= 1u << ATOM;
127   } else if (strcmp(FLAG_mcpu, "atom") == 0) {
128     supported_ |= 1u << ATOM;
129   }
130 }
131
132
133 void CpuFeatures::PrintTarget() { }
134 void CpuFeatures::PrintFeatures() {
135   printf("SSE3=%d SSE4_1=%d AVX=%d FMA3=%d ATOM=%d\n",
136          CpuFeatures::IsSupported(SSE3), CpuFeatures::IsSupported(SSE4_1),
137          CpuFeatures::IsSupported(AVX), CpuFeatures::IsSupported(FMA3),
138          CpuFeatures::IsSupported(ATOM));
139 }
140
141
142 // -----------------------------------------------------------------------------
143 // Implementation of Displacement
144
145 void Displacement::init(Label* L, Type type) {
146   DCHECK(!L->is_bound());
147   int next = 0;
148   if (L->is_linked()) {
149     next = L->pos();
150     DCHECK(next > 0);  // Displacements must be at positions > 0
151   }
152   // Ensure that we _never_ overflow the next field.
153   DCHECK(NextField::is_valid(Assembler::kMaximalBufferSize));
154   data_ = NextField::encode(next) | TypeField::encode(type);
155 }
156
157
158 // -----------------------------------------------------------------------------
159 // Implementation of RelocInfo
160
161
162 const int RelocInfo::kApplyMask =
163   RelocInfo::kCodeTargetMask | 1 << RelocInfo::RUNTIME_ENTRY |
164     1 << RelocInfo::JS_RETURN | 1 << RelocInfo::INTERNAL_REFERENCE |
165     1 << RelocInfo::DEBUG_BREAK_SLOT | 1 << RelocInfo::CODE_AGE_SEQUENCE;
166
167
168 bool RelocInfo::IsCodedSpecially() {
169   // The deserializer needs to know whether a pointer is specially coded.  Being
170   // specially coded on IA32 means that it is a relative address, as used by
171   // branch instructions.  These are also the ones that need changing when a
172   // code object moves.
173   return (1 << rmode_) & kApplyMask;
174 }
175
176
177 bool RelocInfo::IsInConstantPool() {
178   return false;
179 }
180
181
182 void RelocInfo::PatchCode(byte* instructions, int instruction_count) {
183   // Patch the code at the current address with the supplied instructions.
184   for (int i = 0; i < instruction_count; i++) {
185     *(pc_ + i) = *(instructions + i);
186   }
187
188   // Indicate that code has changed.
189   CpuFeatures::FlushICache(pc_, instruction_count);
190 }
191
192
193 // Patch the code at the current PC with a call to the target address.
194 // Additional guard int3 instructions can be added if required.
195 void RelocInfo::PatchCodeWithCall(Address target, int guard_bytes) {
196   // Call instruction takes up 5 bytes and int3 takes up one byte.
197   static const int kCallCodeSize = 5;
198   int code_size = kCallCodeSize + guard_bytes;
199
200   // Create a code patcher.
201   CodePatcher patcher(pc_, code_size);
202
203   // Add a label for checking the size of the code used for returning.
204 #ifdef DEBUG
205   Label check_codesize;
206   patcher.masm()->bind(&check_codesize);
207 #endif
208
209   // Patch the code.
210   patcher.masm()->call(target, RelocInfo::NONE32);
211
212   // Check that the size of the code generated is as expected.
213   DCHECK_EQ(kCallCodeSize,
214             patcher.masm()->SizeOfCodeGeneratedSince(&check_codesize));
215
216   // Add the requested number of int3 instructions after the call.
217   DCHECK_GE(guard_bytes, 0);
218   for (int i = 0; i < guard_bytes; i++) {
219     patcher.masm()->int3();
220   }
221 }
222
223
224 // -----------------------------------------------------------------------------
225 // Implementation of Operand
226
227 Operand::Operand(Register base, int32_t disp, RelocInfo::Mode rmode) {
228   // [base + disp/r]
229   if (disp == 0 && RelocInfo::IsNone(rmode) && !base.is(ebp)) {
230     // [base]
231     set_modrm(0, base);
232     if (base.is(esp)) set_sib(times_1, esp, base);
233   } else if (is_int8(disp) && RelocInfo::IsNone(rmode)) {
234     // [base + disp8]
235     set_modrm(1, base);
236     if (base.is(esp)) set_sib(times_1, esp, base);
237     set_disp8(disp);
238   } else {
239     // [base + disp/r]
240     set_modrm(2, base);
241     if (base.is(esp)) set_sib(times_1, esp, base);
242     set_dispr(disp, rmode);
243   }
244 }
245
246
247 Operand::Operand(Register base,
248                  Register index,
249                  ScaleFactor scale,
250                  int32_t disp,
251                  RelocInfo::Mode rmode) {
252   DCHECK(!index.is(esp));  // illegal addressing mode
253   // [base + index*scale + disp/r]
254   if (disp == 0 && RelocInfo::IsNone(rmode) && !base.is(ebp)) {
255     // [base + index*scale]
256     set_modrm(0, esp);
257     set_sib(scale, index, base);
258   } else if (is_int8(disp) && RelocInfo::IsNone(rmode)) {
259     // [base + index*scale + disp8]
260     set_modrm(1, esp);
261     set_sib(scale, index, base);
262     set_disp8(disp);
263   } else {
264     // [base + index*scale + disp/r]
265     set_modrm(2, esp);
266     set_sib(scale, index, base);
267     set_dispr(disp, rmode);
268   }
269 }
270
271
272 Operand::Operand(Register index,
273                  ScaleFactor scale,
274                  int32_t disp,
275                  RelocInfo::Mode rmode) {
276   DCHECK(!index.is(esp));  // illegal addressing mode
277   // [index*scale + disp/r]
278   set_modrm(0, esp);
279   set_sib(scale, index, ebp);
280   set_dispr(disp, rmode);
281 }
282
283
284 bool Operand::is_reg(Register reg) const {
285   return ((buf_[0] & 0xF8) == 0xC0)  // addressing mode is register only.
286       && ((buf_[0] & 0x07) == reg.code());  // register codes match.
287 }
288
289
290 bool Operand::is_reg_only() const {
291   return (buf_[0] & 0xF8) == 0xC0;  // Addressing mode is register only.
292 }
293
294
295 Register Operand::reg() const {
296   DCHECK(is_reg_only());
297   return Register::from_code(buf_[0] & 0x07);
298 }
299
300
301 // -----------------------------------------------------------------------------
302 // Implementation of Assembler.
303
304 // Emit a single byte. Must always be inlined.
305 #define EMIT(x)                                 \
306   *pc_++ = (x)
307
308
309 #ifdef GENERATED_CODE_COVERAGE
310 static void InitCoverageLog();
311 #endif
312
313 Assembler::Assembler(Isolate* isolate, void* buffer, int buffer_size)
314     : AssemblerBase(isolate, buffer, buffer_size),
315       positions_recorder_(this) {
316   // Clear the buffer in debug mode unless it was provided by the
317   // caller in which case we can't be sure it's okay to overwrite
318   // existing code in it; see CodePatcher::CodePatcher(...).
319 #ifdef DEBUG
320   if (own_buffer_) {
321     memset(buffer_, 0xCC, buffer_size_);  // int3
322   }
323 #endif
324
325   reloc_info_writer.Reposition(buffer_ + buffer_size_, pc_);
326
327 #ifdef GENERATED_CODE_COVERAGE
328   InitCoverageLog();
329 #endif
330 }
331
332
333 void Assembler::GetCode(CodeDesc* desc) {
334   // Finalize code (at this point overflow() may be true, but the gap ensures
335   // that we are still not overlapping instructions and relocation info).
336   reloc_info_writer.Finish();
337   DCHECK(pc_ <= reloc_info_writer.pos());  // No overlap.
338   // Set up code descriptor.
339   desc->buffer = buffer_;
340   desc->buffer_size = buffer_size_;
341   desc->instr_size = pc_offset();
342   desc->reloc_size = (buffer_ + buffer_size_) - reloc_info_writer.pos();
343   desc->origin = this;
344 }
345
346
347 void Assembler::Align(int m) {
348   DCHECK(base::bits::IsPowerOfTwo32(m));
349   int mask = m - 1;
350   int addr = pc_offset();
351   Nop((m - (addr & mask)) & mask);
352 }
353
354
355 bool Assembler::IsNop(Address addr) {
356   Address a = addr;
357   while (*a == 0x66) a++;
358   if (*a == 0x90) return true;
359   if (a[0] == 0xf && a[1] == 0x1f) return true;
360   return false;
361 }
362
363
364 void Assembler::Nop(int bytes) {
365   EnsureSpace ensure_space(this);
366
367   // Multi byte nops from http://support.amd.com/us/Processor_TechDocs/40546.pdf
368   while (bytes > 0) {
369     switch (bytes) {
370       case 2:
371         EMIT(0x66);
372       case 1:
373         EMIT(0x90);
374         return;
375       case 3:
376         EMIT(0xf);
377         EMIT(0x1f);
378         EMIT(0);
379         return;
380       case 4:
381         EMIT(0xf);
382         EMIT(0x1f);
383         EMIT(0x40);
384         EMIT(0);
385         return;
386       case 6:
387         EMIT(0x66);
388       case 5:
389         EMIT(0xf);
390         EMIT(0x1f);
391         EMIT(0x44);
392         EMIT(0);
393         EMIT(0);
394         return;
395       case 7:
396         EMIT(0xf);
397         EMIT(0x1f);
398         EMIT(0x80);
399         EMIT(0);
400         EMIT(0);
401         EMIT(0);
402         EMIT(0);
403         return;
404       default:
405       case 11:
406         EMIT(0x66);
407         bytes--;
408       case 10:
409         EMIT(0x66);
410         bytes--;
411       case 9:
412         EMIT(0x66);
413         bytes--;
414       case 8:
415         EMIT(0xf);
416         EMIT(0x1f);
417         EMIT(0x84);
418         EMIT(0);
419         EMIT(0);
420         EMIT(0);
421         EMIT(0);
422         EMIT(0);
423         bytes -= 8;
424     }
425   }
426 }
427
428
429 void Assembler::CodeTargetAlign() {
430   Align(16);  // Preferred alignment of jump targets on ia32.
431 }
432
433
434 void Assembler::cpuid() {
435   EnsureSpace ensure_space(this);
436   EMIT(0x0F);
437   EMIT(0xA2);
438 }
439
440
441 void Assembler::pushad() {
442   EnsureSpace ensure_space(this);
443   EMIT(0x60);
444 }
445
446
447 void Assembler::popad() {
448   EnsureSpace ensure_space(this);
449   EMIT(0x61);
450 }
451
452
453 void Assembler::pushfd() {
454   EnsureSpace ensure_space(this);
455   EMIT(0x9C);
456 }
457
458
459 void Assembler::popfd() {
460   EnsureSpace ensure_space(this);
461   EMIT(0x9D);
462 }
463
464
465 void Assembler::push(const Immediate& x) {
466   EnsureSpace ensure_space(this);
467   if (x.is_int8()) {
468     EMIT(0x6a);
469     EMIT(x.x_);
470   } else {
471     EMIT(0x68);
472     emit(x);
473   }
474 }
475
476
477 void Assembler::push_imm32(int32_t imm32) {
478   EnsureSpace ensure_space(this);
479   EMIT(0x68);
480   emit(imm32);
481 }
482
483
484 void Assembler::push(Register src) {
485   EnsureSpace ensure_space(this);
486   EMIT(0x50 | src.code());
487 }
488
489
490 void Assembler::push(const Operand& src) {
491   EnsureSpace ensure_space(this);
492   EMIT(0xFF);
493   emit_operand(esi, src);
494 }
495
496
497 void Assembler::pop(Register dst) {
498   DCHECK(reloc_info_writer.last_pc() != NULL);
499   EnsureSpace ensure_space(this);
500   EMIT(0x58 | dst.code());
501 }
502
503
504 void Assembler::pop(const Operand& dst) {
505   EnsureSpace ensure_space(this);
506   EMIT(0x8F);
507   emit_operand(eax, dst);
508 }
509
510
511 void Assembler::enter(const Immediate& size) {
512   EnsureSpace ensure_space(this);
513   EMIT(0xC8);
514   emit_w(size);
515   EMIT(0);
516 }
517
518
519 void Assembler::leave() {
520   EnsureSpace ensure_space(this);
521   EMIT(0xC9);
522 }
523
524
525 void Assembler::mov_b(Register dst, const Operand& src) {
526   CHECK(dst.is_byte_register());
527   EnsureSpace ensure_space(this);
528   EMIT(0x8A);
529   emit_operand(dst, src);
530 }
531
532
533 void Assembler::mov_b(const Operand& dst, const Immediate& src) {
534   EnsureSpace ensure_space(this);
535   EMIT(0xC6);
536   emit_operand(eax, dst);
537   EMIT(static_cast<int8_t>(src.x_));
538 }
539
540
541 void Assembler::mov_b(const Operand& dst, Register src) {
542   CHECK(src.is_byte_register());
543   EnsureSpace ensure_space(this);
544   EMIT(0x88);
545   emit_operand(src, dst);
546 }
547
548
549 void Assembler::mov_w(Register dst, const Operand& src) {
550   EnsureSpace ensure_space(this);
551   EMIT(0x66);
552   EMIT(0x8B);
553   emit_operand(dst, src);
554 }
555
556
557 void Assembler::mov_w(const Operand& dst, Register src) {
558   EnsureSpace ensure_space(this);
559   EMIT(0x66);
560   EMIT(0x89);
561   emit_operand(src, dst);
562 }
563
564
565 void Assembler::mov_w(const Operand& dst, const Immediate& src) {
566   EnsureSpace ensure_space(this);
567   EMIT(0x66);
568   EMIT(0xC7);
569   emit_operand(eax, dst);
570   EMIT(static_cast<int8_t>(src.x_ & 0xff));
571   EMIT(static_cast<int8_t>(src.x_ >> 8));
572 }
573
574
575 void Assembler::mov(Register dst, int32_t imm32) {
576   EnsureSpace ensure_space(this);
577   EMIT(0xB8 | dst.code());
578   emit(imm32);
579 }
580
581
582 void Assembler::mov(Register dst, const Immediate& x) {
583   EnsureSpace ensure_space(this);
584   EMIT(0xB8 | dst.code());
585   emit(x);
586 }
587
588
589 void Assembler::mov(Register dst, Handle<Object> handle) {
590   EnsureSpace ensure_space(this);
591   EMIT(0xB8 | dst.code());
592   emit(handle);
593 }
594
595
596 void Assembler::mov(Register dst, const Operand& src) {
597   EnsureSpace ensure_space(this);
598   EMIT(0x8B);
599   emit_operand(dst, src);
600 }
601
602
603 void Assembler::mov(Register dst, Register src) {
604   EnsureSpace ensure_space(this);
605   EMIT(0x89);
606   EMIT(0xC0 | src.code() << 3 | dst.code());
607 }
608
609
610 void Assembler::mov(const Operand& dst, const Immediate& x) {
611   EnsureSpace ensure_space(this);
612   EMIT(0xC7);
613   emit_operand(eax, dst);
614   emit(x);
615 }
616
617
618 void Assembler::mov(const Operand& dst, Handle<Object> handle) {
619   EnsureSpace ensure_space(this);
620   EMIT(0xC7);
621   emit_operand(eax, dst);
622   emit(handle);
623 }
624
625
626 void Assembler::mov(const Operand& dst, Register src) {
627   EnsureSpace ensure_space(this);
628   EMIT(0x89);
629   emit_operand(src, dst);
630 }
631
632
633 void Assembler::movsx_b(Register dst, const Operand& src) {
634   EnsureSpace ensure_space(this);
635   EMIT(0x0F);
636   EMIT(0xBE);
637   emit_operand(dst, src);
638 }
639
640
641 void Assembler::movsx_w(Register dst, const Operand& src) {
642   EnsureSpace ensure_space(this);
643   EMIT(0x0F);
644   EMIT(0xBF);
645   emit_operand(dst, src);
646 }
647
648
649 void Assembler::movzx_b(Register dst, const Operand& src) {
650   EnsureSpace ensure_space(this);
651   EMIT(0x0F);
652   EMIT(0xB6);
653   emit_operand(dst, src);
654 }
655
656
657 void Assembler::movzx_w(Register dst, const Operand& src) {
658   EnsureSpace ensure_space(this);
659   EMIT(0x0F);
660   EMIT(0xB7);
661   emit_operand(dst, src);
662 }
663
664
665 void Assembler::cmov(Condition cc, Register dst, const Operand& src) {
666   EnsureSpace ensure_space(this);
667   // Opcode: 0f 40 + cc /r.
668   EMIT(0x0F);
669   EMIT(0x40 + cc);
670   emit_operand(dst, src);
671 }
672
673
674 void Assembler::cld() {
675   EnsureSpace ensure_space(this);
676   EMIT(0xFC);
677 }
678
679
680 void Assembler::rep_movs() {
681   EnsureSpace ensure_space(this);
682   EMIT(0xF3);
683   EMIT(0xA5);
684 }
685
686
687 void Assembler::rep_stos() {
688   EnsureSpace ensure_space(this);
689   EMIT(0xF3);
690   EMIT(0xAB);
691 }
692
693
694 void Assembler::stos() {
695   EnsureSpace ensure_space(this);
696   EMIT(0xAB);
697 }
698
699
700 void Assembler::xchg(Register dst, Register src) {
701   EnsureSpace ensure_space(this);
702   if (src.is(eax) || dst.is(eax)) {  // Single-byte encoding.
703     EMIT(0x90 | (src.is(eax) ? dst.code() : src.code()));
704   } else {
705     EMIT(0x87);
706     EMIT(0xC0 | src.code() << 3 | dst.code());
707   }
708 }
709
710
711 void Assembler::xchg(Register dst, const Operand& src) {
712   EnsureSpace ensure_space(this);
713   EMIT(0x87);
714   emit_operand(dst, src);
715 }
716
717
718 void Assembler::adc(Register dst, int32_t imm32) {
719   EnsureSpace ensure_space(this);
720   emit_arith(2, Operand(dst), Immediate(imm32));
721 }
722
723
724 void Assembler::adc(Register dst, const Operand& src) {
725   EnsureSpace ensure_space(this);
726   EMIT(0x13);
727   emit_operand(dst, src);
728 }
729
730
731 void Assembler::add(Register dst, const Operand& src) {
732   EnsureSpace ensure_space(this);
733   EMIT(0x03);
734   emit_operand(dst, src);
735 }
736
737
738 void Assembler::add(const Operand& dst, Register src) {
739   EnsureSpace ensure_space(this);
740   EMIT(0x01);
741   emit_operand(src, dst);
742 }
743
744
745 void Assembler::add(const Operand& dst, const Immediate& x) {
746   DCHECK(reloc_info_writer.last_pc() != NULL);
747   EnsureSpace ensure_space(this);
748   emit_arith(0, dst, x);
749 }
750
751
752 void Assembler::and_(Register dst, int32_t imm32) {
753   and_(dst, Immediate(imm32));
754 }
755
756
757 void Assembler::and_(Register dst, const Immediate& x) {
758   EnsureSpace ensure_space(this);
759   emit_arith(4, Operand(dst), x);
760 }
761
762
763 void Assembler::and_(Register dst, const Operand& src) {
764   EnsureSpace ensure_space(this);
765   EMIT(0x23);
766   emit_operand(dst, src);
767 }
768
769
770 void Assembler::and_(const Operand& dst, const Immediate& x) {
771   EnsureSpace ensure_space(this);
772   emit_arith(4, dst, x);
773 }
774
775
776 void Assembler::and_(const Operand& dst, Register src) {
777   EnsureSpace ensure_space(this);
778   EMIT(0x21);
779   emit_operand(src, dst);
780 }
781
782
783 void Assembler::cmpb(const Operand& op, int8_t imm8) {
784   EnsureSpace ensure_space(this);
785   if (op.is_reg(eax)) {
786     EMIT(0x3C);
787   } else {
788     EMIT(0x80);
789     emit_operand(edi, op);  // edi == 7
790   }
791   EMIT(imm8);
792 }
793
794
795 void Assembler::cmpb(const Operand& op, Register reg) {
796   CHECK(reg.is_byte_register());
797   EnsureSpace ensure_space(this);
798   EMIT(0x38);
799   emit_operand(reg, op);
800 }
801
802
803 void Assembler::cmpb(Register reg, const Operand& op) {
804   CHECK(reg.is_byte_register());
805   EnsureSpace ensure_space(this);
806   EMIT(0x3A);
807   emit_operand(reg, op);
808 }
809
810
811 void Assembler::cmpw(const Operand& op, Immediate imm16) {
812   DCHECK(imm16.is_int16());
813   EnsureSpace ensure_space(this);
814   EMIT(0x66);
815   EMIT(0x81);
816   emit_operand(edi, op);
817   emit_w(imm16);
818 }
819
820
821 void Assembler::cmp(Register reg, int32_t imm32) {
822   EnsureSpace ensure_space(this);
823   emit_arith(7, Operand(reg), Immediate(imm32));
824 }
825
826
827 void Assembler::cmp(Register reg, Handle<Object> handle) {
828   EnsureSpace ensure_space(this);
829   emit_arith(7, Operand(reg), Immediate(handle));
830 }
831
832
833 void Assembler::cmp(Register reg, const Operand& op) {
834   EnsureSpace ensure_space(this);
835   EMIT(0x3B);
836   emit_operand(reg, op);
837 }
838
839
840 void Assembler::cmp(const Operand& op, const Immediate& imm) {
841   EnsureSpace ensure_space(this);
842   emit_arith(7, op, imm);
843 }
844
845
846 void Assembler::cmp(const Operand& op, Handle<Object> handle) {
847   EnsureSpace ensure_space(this);
848   emit_arith(7, op, Immediate(handle));
849 }
850
851
852 void Assembler::cmpb_al(const Operand& op) {
853   EnsureSpace ensure_space(this);
854   EMIT(0x38);  // CMP r/m8, r8
855   emit_operand(eax, op);  // eax has same code as register al.
856 }
857
858
859 void Assembler::cmpw_ax(const Operand& op) {
860   EnsureSpace ensure_space(this);
861   EMIT(0x66);
862   EMIT(0x39);  // CMP r/m16, r16
863   emit_operand(eax, op);  // eax has same code as register ax.
864 }
865
866
867 void Assembler::dec_b(Register dst) {
868   CHECK(dst.is_byte_register());
869   EnsureSpace ensure_space(this);
870   EMIT(0xFE);
871   EMIT(0xC8 | dst.code());
872 }
873
874
875 void Assembler::dec_b(const Operand& dst) {
876   EnsureSpace ensure_space(this);
877   EMIT(0xFE);
878   emit_operand(ecx, dst);
879 }
880
881
882 void Assembler::dec(Register dst) {
883   EnsureSpace ensure_space(this);
884   EMIT(0x48 | dst.code());
885 }
886
887
888 void Assembler::dec(const Operand& dst) {
889   EnsureSpace ensure_space(this);
890   EMIT(0xFF);
891   emit_operand(ecx, dst);
892 }
893
894
895 void Assembler::cdq() {
896   EnsureSpace ensure_space(this);
897   EMIT(0x99);
898 }
899
900
901 void Assembler::idiv(const Operand& src) {
902   EnsureSpace ensure_space(this);
903   EMIT(0xF7);
904   emit_operand(edi, src);
905 }
906
907
908 void Assembler::div(const Operand& src) {
909   EnsureSpace ensure_space(this);
910   EMIT(0xF7);
911   emit_operand(esi, src);
912 }
913
914
915 void Assembler::imul(Register reg) {
916   EnsureSpace ensure_space(this);
917   EMIT(0xF7);
918   EMIT(0xE8 | reg.code());
919 }
920
921
922 void Assembler::imul(Register dst, const Operand& src) {
923   EnsureSpace ensure_space(this);
924   EMIT(0x0F);
925   EMIT(0xAF);
926   emit_operand(dst, src);
927 }
928
929
930 void Assembler::imul(Register dst, Register src, int32_t imm32) {
931   imul(dst, Operand(src), imm32);
932 }
933
934
935 void Assembler::imul(Register dst, const Operand& src, int32_t imm32) {
936   EnsureSpace ensure_space(this);
937   if (is_int8(imm32)) {
938     EMIT(0x6B);
939     emit_operand(dst, src);
940     EMIT(imm32);
941   } else {
942     EMIT(0x69);
943     emit_operand(dst, src);
944     emit(imm32);
945   }
946 }
947
948
949 void Assembler::inc(Register dst) {
950   EnsureSpace ensure_space(this);
951   EMIT(0x40 | dst.code());
952 }
953
954
955 void Assembler::inc(const Operand& dst) {
956   EnsureSpace ensure_space(this);
957   EMIT(0xFF);
958   emit_operand(eax, dst);
959 }
960
961
962 void Assembler::lea(Register dst, const Operand& src) {
963   EnsureSpace ensure_space(this);
964   EMIT(0x8D);
965   emit_operand(dst, src);
966 }
967
968
969 void Assembler::mul(Register src) {
970   EnsureSpace ensure_space(this);
971   EMIT(0xF7);
972   EMIT(0xE0 | src.code());
973 }
974
975
976 void Assembler::neg(Register dst) {
977   EnsureSpace ensure_space(this);
978   EMIT(0xF7);
979   EMIT(0xD8 | dst.code());
980 }
981
982
983 void Assembler::neg(const Operand& dst) {
984   EnsureSpace ensure_space(this);
985   EMIT(0xF7);
986   emit_operand(ebx, dst);
987 }
988
989
990 void Assembler::not_(Register dst) {
991   EnsureSpace ensure_space(this);
992   EMIT(0xF7);
993   EMIT(0xD0 | dst.code());
994 }
995
996
997 void Assembler::not_(const Operand& dst) {
998   EnsureSpace ensure_space(this);
999   EMIT(0xF7);
1000   emit_operand(edx, dst);
1001 }
1002
1003
1004 void Assembler::or_(Register dst, int32_t imm32) {
1005   EnsureSpace ensure_space(this);
1006   emit_arith(1, Operand(dst), Immediate(imm32));
1007 }
1008
1009
1010 void Assembler::or_(Register dst, const Operand& src) {
1011   EnsureSpace ensure_space(this);
1012   EMIT(0x0B);
1013   emit_operand(dst, src);
1014 }
1015
1016
1017 void Assembler::or_(const Operand& dst, const Immediate& x) {
1018   EnsureSpace ensure_space(this);
1019   emit_arith(1, dst, x);
1020 }
1021
1022
1023 void Assembler::or_(const Operand& dst, Register src) {
1024   EnsureSpace ensure_space(this);
1025   EMIT(0x09);
1026   emit_operand(src, dst);
1027 }
1028
1029
1030 void Assembler::rcl(Register dst, uint8_t imm8) {
1031   EnsureSpace ensure_space(this);
1032   DCHECK(is_uint5(imm8));  // illegal shift count
1033   if (imm8 == 1) {
1034     EMIT(0xD1);
1035     EMIT(0xD0 | dst.code());
1036   } else {
1037     EMIT(0xC1);
1038     EMIT(0xD0 | dst.code());
1039     EMIT(imm8);
1040   }
1041 }
1042
1043
1044 void Assembler::rcr(Register dst, uint8_t imm8) {
1045   EnsureSpace ensure_space(this);
1046   DCHECK(is_uint5(imm8));  // illegal shift count
1047   if (imm8 == 1) {
1048     EMIT(0xD1);
1049     EMIT(0xD8 | dst.code());
1050   } else {
1051     EMIT(0xC1);
1052     EMIT(0xD8 | dst.code());
1053     EMIT(imm8);
1054   }
1055 }
1056
1057
1058 void Assembler::ror(const Operand& dst, uint8_t imm8) {
1059   EnsureSpace ensure_space(this);
1060   DCHECK(is_uint5(imm8));  // illegal shift count
1061   if (imm8 == 1) {
1062     EMIT(0xD1);
1063     emit_operand(ecx, dst);
1064   } else {
1065     EMIT(0xC1);
1066     emit_operand(ecx, dst);
1067     EMIT(imm8);
1068   }
1069 }
1070
1071
1072 void Assembler::ror_cl(const Operand& dst) {
1073   EnsureSpace ensure_space(this);
1074   EMIT(0xD3);
1075   emit_operand(ecx, dst);
1076 }
1077
1078
1079 void Assembler::sar(const Operand& dst, uint8_t imm8) {
1080   EnsureSpace ensure_space(this);
1081   DCHECK(is_uint5(imm8));  // illegal shift count
1082   if (imm8 == 1) {
1083     EMIT(0xD1);
1084     emit_operand(edi, dst);
1085   } else {
1086     EMIT(0xC1);
1087     emit_operand(edi, dst);
1088     EMIT(imm8);
1089   }
1090 }
1091
1092
1093 void Assembler::sar_cl(const Operand& dst) {
1094   EnsureSpace ensure_space(this);
1095   EMIT(0xD3);
1096   emit_operand(edi, dst);
1097 }
1098
1099
1100 void Assembler::sbb(Register dst, const Operand& src) {
1101   EnsureSpace ensure_space(this);
1102   EMIT(0x1B);
1103   emit_operand(dst, src);
1104 }
1105
1106
1107 void Assembler::shld(Register dst, const Operand& src) {
1108   EnsureSpace ensure_space(this);
1109   EMIT(0x0F);
1110   EMIT(0xA5);
1111   emit_operand(dst, src);
1112 }
1113
1114
1115 void Assembler::shl(const Operand& dst, uint8_t imm8) {
1116   EnsureSpace ensure_space(this);
1117   DCHECK(is_uint5(imm8));  // illegal shift count
1118   if (imm8 == 1) {
1119     EMIT(0xD1);
1120     emit_operand(esp, dst);
1121   } else {
1122     EMIT(0xC1);
1123     emit_operand(esp, dst);
1124     EMIT(imm8);
1125   }
1126 }
1127
1128
1129 void Assembler::shl_cl(const Operand& dst) {
1130   EnsureSpace ensure_space(this);
1131   EMIT(0xD3);
1132   emit_operand(esp, dst);
1133 }
1134
1135
1136 void Assembler::shrd(Register dst, const Operand& src) {
1137   EnsureSpace ensure_space(this);
1138   EMIT(0x0F);
1139   EMIT(0xAD);
1140   emit_operand(dst, src);
1141 }
1142
1143
1144 void Assembler::shr(const Operand& dst, uint8_t imm8) {
1145   EnsureSpace ensure_space(this);
1146   DCHECK(is_uint5(imm8));  // illegal shift count
1147   if (imm8 == 1) {
1148     EMIT(0xD1);
1149     emit_operand(ebp, dst);
1150   } else {
1151     EMIT(0xC1);
1152     emit_operand(ebp, dst);
1153     EMIT(imm8);
1154   }
1155 }
1156
1157
1158 void Assembler::shr_cl(const Operand& dst) {
1159   EnsureSpace ensure_space(this);
1160   EMIT(0xD3);
1161   emit_operand(ebp, dst);
1162 }
1163
1164
1165 void Assembler::sub(const Operand& dst, const Immediate& x) {
1166   EnsureSpace ensure_space(this);
1167   emit_arith(5, dst, x);
1168 }
1169
1170
1171 void Assembler::sub(Register dst, const Operand& src) {
1172   EnsureSpace ensure_space(this);
1173   EMIT(0x2B);
1174   emit_operand(dst, src);
1175 }
1176
1177
1178 void Assembler::sub(const Operand& dst, Register src) {
1179   EnsureSpace ensure_space(this);
1180   EMIT(0x29);
1181   emit_operand(src, dst);
1182 }
1183
1184
1185 void Assembler::test(Register reg, const Immediate& imm) {
1186   if (RelocInfo::IsNone(imm.rmode_) && is_uint8(imm.x_)) {
1187     test_b(reg, imm.x_);
1188     return;
1189   }
1190
1191   EnsureSpace ensure_space(this);
1192   // This is not using emit_arith because test doesn't support
1193   // sign-extension of 8-bit operands.
1194   if (reg.is(eax)) {
1195     EMIT(0xA9);
1196   } else {
1197     EMIT(0xF7);
1198     EMIT(0xC0 | reg.code());
1199   }
1200   emit(imm);
1201 }
1202
1203
1204 void Assembler::test(Register reg, const Operand& op) {
1205   EnsureSpace ensure_space(this);
1206   EMIT(0x85);
1207   emit_operand(reg, op);
1208 }
1209
1210
1211 void Assembler::test_b(Register reg, const Operand& op) {
1212   CHECK(reg.is_byte_register());
1213   EnsureSpace ensure_space(this);
1214   EMIT(0x84);
1215   emit_operand(reg, op);
1216 }
1217
1218
1219 void Assembler::test(const Operand& op, const Immediate& imm) {
1220   if (op.is_reg_only()) {
1221     test(op.reg(), imm);
1222     return;
1223   }
1224   if (RelocInfo::IsNone(imm.rmode_) && is_uint8(imm.x_)) {
1225     return test_b(op, imm.x_);
1226   }
1227   EnsureSpace ensure_space(this);
1228   EMIT(0xF7);
1229   emit_operand(eax, op);
1230   emit(imm);
1231 }
1232
1233
1234 void Assembler::test_b(Register reg, uint8_t imm8) {
1235   EnsureSpace ensure_space(this);
1236   // Only use test against byte for registers that have a byte
1237   // variant: eax, ebx, ecx, and edx.
1238   if (reg.is(eax)) {
1239     EMIT(0xA8);
1240     EMIT(imm8);
1241   } else if (reg.is_byte_register()) {
1242     emit_arith_b(0xF6, 0xC0, reg, imm8);
1243   } else {
1244     EMIT(0xF7);
1245     EMIT(0xC0 | reg.code());
1246     emit(imm8);
1247   }
1248 }
1249
1250
1251 void Assembler::test_b(const Operand& op, uint8_t imm8) {
1252   if (op.is_reg_only()) {
1253     test_b(op.reg(), imm8);
1254     return;
1255   }
1256   EnsureSpace ensure_space(this);
1257   EMIT(0xF6);
1258   emit_operand(eax, op);
1259   EMIT(imm8);
1260 }
1261
1262
1263 void Assembler::xor_(Register dst, int32_t imm32) {
1264   EnsureSpace ensure_space(this);
1265   emit_arith(6, Operand(dst), Immediate(imm32));
1266 }
1267
1268
1269 void Assembler::xor_(Register dst, const Operand& src) {
1270   EnsureSpace ensure_space(this);
1271   EMIT(0x33);
1272   emit_operand(dst, src);
1273 }
1274
1275
1276 void Assembler::xor_(const Operand& dst, Register src) {
1277   EnsureSpace ensure_space(this);
1278   EMIT(0x31);
1279   emit_operand(src, dst);
1280 }
1281
1282
1283 void Assembler::xor_(const Operand& dst, const Immediate& x) {
1284   EnsureSpace ensure_space(this);
1285   emit_arith(6, dst, x);
1286 }
1287
1288
1289 void Assembler::bt(const Operand& dst, Register src) {
1290   EnsureSpace ensure_space(this);
1291   EMIT(0x0F);
1292   EMIT(0xA3);
1293   emit_operand(src, dst);
1294 }
1295
1296
1297 void Assembler::bts(const Operand& dst, Register src) {
1298   EnsureSpace ensure_space(this);
1299   EMIT(0x0F);
1300   EMIT(0xAB);
1301   emit_operand(src, dst);
1302 }
1303
1304
1305 void Assembler::bsr(Register dst, const Operand& src) {
1306   EnsureSpace ensure_space(this);
1307   EMIT(0x0F);
1308   EMIT(0xBD);
1309   emit_operand(dst, src);
1310 }
1311
1312
1313 void Assembler::hlt() {
1314   EnsureSpace ensure_space(this);
1315   EMIT(0xF4);
1316 }
1317
1318
1319 void Assembler::int3() {
1320   EnsureSpace ensure_space(this);
1321   EMIT(0xCC);
1322 }
1323
1324
1325 void Assembler::nop() {
1326   EnsureSpace ensure_space(this);
1327   EMIT(0x90);
1328 }
1329
1330
1331 void Assembler::ret(int imm16) {
1332   EnsureSpace ensure_space(this);
1333   DCHECK(is_uint16(imm16));
1334   if (imm16 == 0) {
1335     EMIT(0xC3);
1336   } else {
1337     EMIT(0xC2);
1338     EMIT(imm16 & 0xFF);
1339     EMIT((imm16 >> 8) & 0xFF);
1340   }
1341 }
1342
1343
1344 void Assembler::ud2() {
1345   EnsureSpace ensure_space(this);
1346   EMIT(0x0F);
1347   EMIT(0x0B);
1348 }
1349
1350
1351 // Labels refer to positions in the (to be) generated code.
1352 // There are bound, linked, and unused labels.
1353 //
1354 // Bound labels refer to known positions in the already
1355 // generated code. pos() is the position the label refers to.
1356 //
1357 // Linked labels refer to unknown positions in the code
1358 // to be generated; pos() is the position of the 32bit
1359 // Displacement of the last instruction using the label.
1360
1361
1362 void Assembler::print(Label* L) {
1363   if (L->is_unused()) {
1364     PrintF("unused label\n");
1365   } else if (L->is_bound()) {
1366     PrintF("bound label to %d\n", L->pos());
1367   } else if (L->is_linked()) {
1368     Label l = *L;
1369     PrintF("unbound label");
1370     while (l.is_linked()) {
1371       Displacement disp = disp_at(&l);
1372       PrintF("@ %d ", l.pos());
1373       disp.print();
1374       PrintF("\n");
1375       disp.next(&l);
1376     }
1377   } else {
1378     PrintF("label in inconsistent state (pos = %d)\n", L->pos_);
1379   }
1380 }
1381
1382
1383 void Assembler::bind_to(Label* L, int pos) {
1384   EnsureSpace ensure_space(this);
1385   DCHECK(0 <= pos && pos <= pc_offset());  // must have a valid binding position
1386   while (L->is_linked()) {
1387     Displacement disp = disp_at(L);
1388     int fixup_pos = L->pos();
1389     if (disp.type() == Displacement::CODE_ABSOLUTE) {
1390       long_at_put(fixup_pos, reinterpret_cast<int>(buffer_ + pos));
1391       internal_reference_positions_.push_back(fixup_pos);
1392     } else if (disp.type() == Displacement::CODE_RELATIVE) {
1393       // Relative to Code* heap object pointer.
1394       long_at_put(fixup_pos, pos + Code::kHeaderSize - kHeapObjectTag);
1395     } else {
1396       if (disp.type() == Displacement::UNCONDITIONAL_JUMP) {
1397         DCHECK(byte_at(fixup_pos - 1) == 0xE9);  // jmp expected
1398       }
1399       // Relative address, relative to point after address.
1400       int imm32 = pos - (fixup_pos + sizeof(int32_t));
1401       long_at_put(fixup_pos, imm32);
1402     }
1403     disp.next(L);
1404   }
1405   while (L->is_near_linked()) {
1406     int fixup_pos = L->near_link_pos();
1407     int offset_to_next =
1408         static_cast<int>(*reinterpret_cast<int8_t*>(addr_at(fixup_pos)));
1409     DCHECK(offset_to_next <= 0);
1410     // Relative address, relative to point after address.
1411     int disp = pos - fixup_pos - sizeof(int8_t);
1412     CHECK(0 <= disp && disp <= 127);
1413     set_byte_at(fixup_pos, disp);
1414     if (offset_to_next < 0) {
1415       L->link_to(fixup_pos + offset_to_next, Label::kNear);
1416     } else {
1417       L->UnuseNear();
1418     }
1419   }
1420   L->bind_to(pos);
1421 }
1422
1423
1424 void Assembler::bind(Label* L) {
1425   EnsureSpace ensure_space(this);
1426   DCHECK(!L->is_bound());  // label can only be bound once
1427   bind_to(L, pc_offset());
1428 }
1429
1430
1431 void Assembler::call(Label* L) {
1432   positions_recorder()->WriteRecordedPositions();
1433   EnsureSpace ensure_space(this);
1434   if (L->is_bound()) {
1435     const int long_size = 5;
1436     int offs = L->pos() - pc_offset();
1437     DCHECK(offs <= 0);
1438     // 1110 1000 #32-bit disp.
1439     EMIT(0xE8);
1440     emit(offs - long_size);
1441   } else {
1442     // 1110 1000 #32-bit disp.
1443     EMIT(0xE8);
1444     emit_disp(L, Displacement::OTHER);
1445   }
1446 }
1447
1448
1449 void Assembler::call(byte* entry, RelocInfo::Mode rmode) {
1450   positions_recorder()->WriteRecordedPositions();
1451   EnsureSpace ensure_space(this);
1452   DCHECK(!RelocInfo::IsCodeTarget(rmode));
1453   EMIT(0xE8);
1454   if (RelocInfo::IsRuntimeEntry(rmode)) {
1455     emit(reinterpret_cast<uint32_t>(entry), rmode);
1456   } else {
1457     emit(entry - (pc_ + sizeof(int32_t)), rmode);
1458   }
1459 }
1460
1461
1462 int Assembler::CallSize(const Operand& adr) {
1463   // Call size is 1 (opcode) + adr.len_ (operand).
1464   return 1 + adr.len_;
1465 }
1466
1467
1468 void Assembler::call(const Operand& adr) {
1469   positions_recorder()->WriteRecordedPositions();
1470   EnsureSpace ensure_space(this);
1471   EMIT(0xFF);
1472   emit_operand(edx, adr);
1473 }
1474
1475
1476 int Assembler::CallSize(Handle<Code> code, RelocInfo::Mode rmode) {
1477   return 1 /* EMIT */ + sizeof(uint32_t) /* emit */;
1478 }
1479
1480
1481 void Assembler::call(Handle<Code> code,
1482                      RelocInfo::Mode rmode,
1483                      TypeFeedbackId ast_id) {
1484   positions_recorder()->WriteRecordedPositions();
1485   EnsureSpace ensure_space(this);
1486   DCHECK(RelocInfo::IsCodeTarget(rmode)
1487       || rmode == RelocInfo::CODE_AGE_SEQUENCE);
1488   EMIT(0xE8);
1489   emit(code, rmode, ast_id);
1490 }
1491
1492
1493 void Assembler::jmp(Label* L, Label::Distance distance) {
1494   EnsureSpace ensure_space(this);
1495   if (L->is_bound()) {
1496     const int short_size = 2;
1497     const int long_size  = 5;
1498     int offs = L->pos() - pc_offset();
1499     DCHECK(offs <= 0);
1500     if (is_int8(offs - short_size)) {
1501       // 1110 1011 #8-bit disp.
1502       EMIT(0xEB);
1503       EMIT((offs - short_size) & 0xFF);
1504     } else {
1505       // 1110 1001 #32-bit disp.
1506       EMIT(0xE9);
1507       emit(offs - long_size);
1508     }
1509   } else if (distance == Label::kNear) {
1510     EMIT(0xEB);
1511     emit_near_disp(L);
1512   } else {
1513     // 1110 1001 #32-bit disp.
1514     EMIT(0xE9);
1515     emit_disp(L, Displacement::UNCONDITIONAL_JUMP);
1516   }
1517 }
1518
1519
1520 void Assembler::jmp(byte* entry, RelocInfo::Mode rmode) {
1521   EnsureSpace ensure_space(this);
1522   DCHECK(!RelocInfo::IsCodeTarget(rmode));
1523   EMIT(0xE9);
1524   if (RelocInfo::IsRuntimeEntry(rmode)) {
1525     emit(reinterpret_cast<uint32_t>(entry), rmode);
1526   } else {
1527     emit(entry - (pc_ + sizeof(int32_t)), rmode);
1528   }
1529 }
1530
1531
1532 void Assembler::jmp(const Operand& adr) {
1533   EnsureSpace ensure_space(this);
1534   EMIT(0xFF);
1535   emit_operand(esp, adr);
1536 }
1537
1538
1539 void Assembler::jmp(Handle<Code> code, RelocInfo::Mode rmode) {
1540   EnsureSpace ensure_space(this);
1541   DCHECK(RelocInfo::IsCodeTarget(rmode));
1542   EMIT(0xE9);
1543   emit(code, rmode);
1544 }
1545
1546
1547 void Assembler::j(Condition cc, Label* L, Label::Distance distance) {
1548   EnsureSpace ensure_space(this);
1549   DCHECK(0 <= cc && static_cast<int>(cc) < 16);
1550   if (L->is_bound()) {
1551     const int short_size = 2;
1552     const int long_size  = 6;
1553     int offs = L->pos() - pc_offset();
1554     DCHECK(offs <= 0);
1555     if (is_int8(offs - short_size)) {
1556       // 0111 tttn #8-bit disp
1557       EMIT(0x70 | cc);
1558       EMIT((offs - short_size) & 0xFF);
1559     } else {
1560       // 0000 1111 1000 tttn #32-bit disp
1561       EMIT(0x0F);
1562       EMIT(0x80 | cc);
1563       emit(offs - long_size);
1564     }
1565   } else if (distance == Label::kNear) {
1566     EMIT(0x70 | cc);
1567     emit_near_disp(L);
1568   } else {
1569     // 0000 1111 1000 tttn #32-bit disp
1570     // Note: could eliminate cond. jumps to this jump if condition
1571     //       is the same however, seems to be rather unlikely case.
1572     EMIT(0x0F);
1573     EMIT(0x80 | cc);
1574     emit_disp(L, Displacement::OTHER);
1575   }
1576 }
1577
1578
1579 void Assembler::j(Condition cc, byte* entry, RelocInfo::Mode rmode) {
1580   EnsureSpace ensure_space(this);
1581   DCHECK((0 <= cc) && (static_cast<int>(cc) < 16));
1582   // 0000 1111 1000 tttn #32-bit disp.
1583   EMIT(0x0F);
1584   EMIT(0x80 | cc);
1585   if (RelocInfo::IsRuntimeEntry(rmode)) {
1586     emit(reinterpret_cast<uint32_t>(entry), rmode);
1587   } else {
1588     emit(entry - (pc_ + sizeof(int32_t)), rmode);
1589   }
1590 }
1591
1592
1593 void Assembler::j(Condition cc, Handle<Code> code) {
1594   EnsureSpace ensure_space(this);
1595   // 0000 1111 1000 tttn #32-bit disp
1596   EMIT(0x0F);
1597   EMIT(0x80 | cc);
1598   emit(code, RelocInfo::CODE_TARGET);
1599 }
1600
1601
1602 // FPU instructions.
1603
1604 void Assembler::fld(int i) {
1605   EnsureSpace ensure_space(this);
1606   emit_farith(0xD9, 0xC0, i);
1607 }
1608
1609
1610 void Assembler::fstp(int i) {
1611   EnsureSpace ensure_space(this);
1612   emit_farith(0xDD, 0xD8, i);
1613 }
1614
1615
1616 void Assembler::fld1() {
1617   EnsureSpace ensure_space(this);
1618   EMIT(0xD9);
1619   EMIT(0xE8);
1620 }
1621
1622
1623 void Assembler::fldpi() {
1624   EnsureSpace ensure_space(this);
1625   EMIT(0xD9);
1626   EMIT(0xEB);
1627 }
1628
1629
1630 void Assembler::fldz() {
1631   EnsureSpace ensure_space(this);
1632   EMIT(0xD9);
1633   EMIT(0xEE);
1634 }
1635
1636
1637 void Assembler::fldln2() {
1638   EnsureSpace ensure_space(this);
1639   EMIT(0xD9);
1640   EMIT(0xED);
1641 }
1642
1643
1644 void Assembler::fld_s(const Operand& adr) {
1645   EnsureSpace ensure_space(this);
1646   EMIT(0xD9);
1647   emit_operand(eax, adr);
1648 }
1649
1650
1651 void Assembler::fld_d(const Operand& adr) {
1652   EnsureSpace ensure_space(this);
1653   EMIT(0xDD);
1654   emit_operand(eax, adr);
1655 }
1656
1657
1658 void Assembler::fstp_s(const Operand& adr) {
1659   EnsureSpace ensure_space(this);
1660   EMIT(0xD9);
1661   emit_operand(ebx, adr);
1662 }
1663
1664
1665 void Assembler::fst_s(const Operand& adr) {
1666   EnsureSpace ensure_space(this);
1667   EMIT(0xD9);
1668   emit_operand(edx, adr);
1669 }
1670
1671
1672 void Assembler::fstp_d(const Operand& adr) {
1673   EnsureSpace ensure_space(this);
1674   EMIT(0xDD);
1675   emit_operand(ebx, adr);
1676 }
1677
1678
1679 void Assembler::fst_d(const Operand& adr) {
1680   EnsureSpace ensure_space(this);
1681   EMIT(0xDD);
1682   emit_operand(edx, adr);
1683 }
1684
1685
1686 void Assembler::fild_s(const Operand& adr) {
1687   EnsureSpace ensure_space(this);
1688   EMIT(0xDB);
1689   emit_operand(eax, adr);
1690 }
1691
1692
1693 void Assembler::fild_d(const Operand& adr) {
1694   EnsureSpace ensure_space(this);
1695   EMIT(0xDF);
1696   emit_operand(ebp, adr);
1697 }
1698
1699
1700 void Assembler::fistp_s(const Operand& adr) {
1701   EnsureSpace ensure_space(this);
1702   EMIT(0xDB);
1703   emit_operand(ebx, adr);
1704 }
1705
1706
1707 void Assembler::fisttp_s(const Operand& adr) {
1708   DCHECK(IsEnabled(SSE3));
1709   EnsureSpace ensure_space(this);
1710   EMIT(0xDB);
1711   emit_operand(ecx, adr);
1712 }
1713
1714
1715 void Assembler::fisttp_d(const Operand& adr) {
1716   DCHECK(IsEnabled(SSE3));
1717   EnsureSpace ensure_space(this);
1718   EMIT(0xDD);
1719   emit_operand(ecx, adr);
1720 }
1721
1722
1723 void Assembler::fist_s(const Operand& adr) {
1724   EnsureSpace ensure_space(this);
1725   EMIT(0xDB);
1726   emit_operand(edx, adr);
1727 }
1728
1729
1730 void Assembler::fistp_d(const Operand& adr) {
1731   EnsureSpace ensure_space(this);
1732   EMIT(0xDF);
1733   emit_operand(edi, adr);
1734 }
1735
1736
1737 void Assembler::fabs() {
1738   EnsureSpace ensure_space(this);
1739   EMIT(0xD9);
1740   EMIT(0xE1);
1741 }
1742
1743
1744 void Assembler::fchs() {
1745   EnsureSpace ensure_space(this);
1746   EMIT(0xD9);
1747   EMIT(0xE0);
1748 }
1749
1750
1751 void Assembler::fcos() {
1752   EnsureSpace ensure_space(this);
1753   EMIT(0xD9);
1754   EMIT(0xFF);
1755 }
1756
1757
1758 void Assembler::fsin() {
1759   EnsureSpace ensure_space(this);
1760   EMIT(0xD9);
1761   EMIT(0xFE);
1762 }
1763
1764
1765 void Assembler::fptan() {
1766   EnsureSpace ensure_space(this);
1767   EMIT(0xD9);
1768   EMIT(0xF2);
1769 }
1770
1771
1772 void Assembler::fyl2x() {
1773   EnsureSpace ensure_space(this);
1774   EMIT(0xD9);
1775   EMIT(0xF1);
1776 }
1777
1778
1779 void Assembler::f2xm1() {
1780   EnsureSpace ensure_space(this);
1781   EMIT(0xD9);
1782   EMIT(0xF0);
1783 }
1784
1785
1786 void Assembler::fscale() {
1787   EnsureSpace ensure_space(this);
1788   EMIT(0xD9);
1789   EMIT(0xFD);
1790 }
1791
1792
1793 void Assembler::fninit() {
1794   EnsureSpace ensure_space(this);
1795   EMIT(0xDB);
1796   EMIT(0xE3);
1797 }
1798
1799
1800 void Assembler::fadd(int i) {
1801   EnsureSpace ensure_space(this);
1802   emit_farith(0xDC, 0xC0, i);
1803 }
1804
1805
1806 void Assembler::fadd_i(int i) {
1807   EnsureSpace ensure_space(this);
1808   emit_farith(0xD8, 0xC0, i);
1809 }
1810
1811
1812 void Assembler::fsub(int i) {
1813   EnsureSpace ensure_space(this);
1814   emit_farith(0xDC, 0xE8, i);
1815 }
1816
1817
1818 void Assembler::fsub_i(int i) {
1819   EnsureSpace ensure_space(this);
1820   emit_farith(0xD8, 0xE0, i);
1821 }
1822
1823
1824 void Assembler::fisub_s(const Operand& adr) {
1825   EnsureSpace ensure_space(this);
1826   EMIT(0xDA);
1827   emit_operand(esp, adr);
1828 }
1829
1830
1831 void Assembler::fmul_i(int i) {
1832   EnsureSpace ensure_space(this);
1833   emit_farith(0xD8, 0xC8, i);
1834 }
1835
1836
1837 void Assembler::fmul(int i) {
1838   EnsureSpace ensure_space(this);
1839   emit_farith(0xDC, 0xC8, i);
1840 }
1841
1842
1843 void Assembler::fdiv(int i) {
1844   EnsureSpace ensure_space(this);
1845   emit_farith(0xDC, 0xF8, i);
1846 }
1847
1848
1849 void Assembler::fdiv_i(int i) {
1850   EnsureSpace ensure_space(this);
1851   emit_farith(0xD8, 0xF0, i);
1852 }
1853
1854
1855 void Assembler::faddp(int i) {
1856   EnsureSpace ensure_space(this);
1857   emit_farith(0xDE, 0xC0, i);
1858 }
1859
1860
1861 void Assembler::fsubp(int i) {
1862   EnsureSpace ensure_space(this);
1863   emit_farith(0xDE, 0xE8, i);
1864 }
1865
1866
1867 void Assembler::fsubrp(int i) {
1868   EnsureSpace ensure_space(this);
1869   emit_farith(0xDE, 0xE0, i);
1870 }
1871
1872
1873 void Assembler::fmulp(int i) {
1874   EnsureSpace ensure_space(this);
1875   emit_farith(0xDE, 0xC8, i);
1876 }
1877
1878
1879 void Assembler::fdivp(int i) {
1880   EnsureSpace ensure_space(this);
1881   emit_farith(0xDE, 0xF8, i);
1882 }
1883
1884
1885 void Assembler::fprem() {
1886   EnsureSpace ensure_space(this);
1887   EMIT(0xD9);
1888   EMIT(0xF8);
1889 }
1890
1891
1892 void Assembler::fprem1() {
1893   EnsureSpace ensure_space(this);
1894   EMIT(0xD9);
1895   EMIT(0xF5);
1896 }
1897
1898
1899 void Assembler::fxch(int i) {
1900   EnsureSpace ensure_space(this);
1901   emit_farith(0xD9, 0xC8, i);
1902 }
1903
1904
1905 void Assembler::fincstp() {
1906   EnsureSpace ensure_space(this);
1907   EMIT(0xD9);
1908   EMIT(0xF7);
1909 }
1910
1911
1912 void Assembler::ffree(int i) {
1913   EnsureSpace ensure_space(this);
1914   emit_farith(0xDD, 0xC0, i);
1915 }
1916
1917
1918 void Assembler::ftst() {
1919   EnsureSpace ensure_space(this);
1920   EMIT(0xD9);
1921   EMIT(0xE4);
1922 }
1923
1924
1925 void Assembler::fucomp(int i) {
1926   EnsureSpace ensure_space(this);
1927   emit_farith(0xDD, 0xE8, i);
1928 }
1929
1930
1931 void Assembler::fucompp() {
1932   EnsureSpace ensure_space(this);
1933   EMIT(0xDA);
1934   EMIT(0xE9);
1935 }
1936
1937
1938 void Assembler::fucomi(int i) {
1939   EnsureSpace ensure_space(this);
1940   EMIT(0xDB);
1941   EMIT(0xE8 + i);
1942 }
1943
1944
1945 void Assembler::fucomip() {
1946   EnsureSpace ensure_space(this);
1947   EMIT(0xDF);
1948   EMIT(0xE9);
1949 }
1950
1951
1952 void Assembler::fcompp() {
1953   EnsureSpace ensure_space(this);
1954   EMIT(0xDE);
1955   EMIT(0xD9);
1956 }
1957
1958
1959 void Assembler::fnstsw_ax() {
1960   EnsureSpace ensure_space(this);
1961   EMIT(0xDF);
1962   EMIT(0xE0);
1963 }
1964
1965
1966 void Assembler::fwait() {
1967   EnsureSpace ensure_space(this);
1968   EMIT(0x9B);
1969 }
1970
1971
1972 void Assembler::frndint() {
1973   EnsureSpace ensure_space(this);
1974   EMIT(0xD9);
1975   EMIT(0xFC);
1976 }
1977
1978
1979 void Assembler::fnclex() {
1980   EnsureSpace ensure_space(this);
1981   EMIT(0xDB);
1982   EMIT(0xE2);
1983 }
1984
1985
1986 void Assembler::sahf() {
1987   EnsureSpace ensure_space(this);
1988   EMIT(0x9E);
1989 }
1990
1991
1992 void Assembler::setcc(Condition cc, Register reg) {
1993   DCHECK(reg.is_byte_register());
1994   EnsureSpace ensure_space(this);
1995   EMIT(0x0F);
1996   EMIT(0x90 | cc);
1997   EMIT(0xC0 | reg.code());
1998 }
1999
2000
2001 void Assembler::cvttss2si(Register dst, const Operand& src) {
2002   EnsureSpace ensure_space(this);
2003   EMIT(0xF3);
2004   EMIT(0x0F);
2005   EMIT(0x2C);
2006   emit_operand(dst, src);
2007 }
2008
2009
2010 void Assembler::cvttsd2si(Register dst, const Operand& src) {
2011   EnsureSpace ensure_space(this);
2012   EMIT(0xF2);
2013   EMIT(0x0F);
2014   EMIT(0x2C);
2015   emit_operand(dst, src);
2016 }
2017
2018
2019 void Assembler::cvtsd2si(Register dst, XMMRegister src) {
2020   EnsureSpace ensure_space(this);
2021   EMIT(0xF2);
2022   EMIT(0x0F);
2023   EMIT(0x2D);
2024   emit_sse_operand(dst, src);
2025 }
2026
2027
2028 void Assembler::cvtsi2sd(XMMRegister dst, const Operand& src) {
2029   EnsureSpace ensure_space(this);
2030   EMIT(0xF2);
2031   EMIT(0x0F);
2032   EMIT(0x2A);
2033   emit_sse_operand(dst, src);
2034 }
2035
2036
2037 void Assembler::cvtss2sd(XMMRegister dst, const Operand& src) {
2038   EnsureSpace ensure_space(this);
2039   EMIT(0xF3);
2040   EMIT(0x0F);
2041   EMIT(0x5A);
2042   emit_sse_operand(dst, src);
2043 }
2044
2045
2046 void Assembler::cvtsd2ss(XMMRegister dst, const Operand& src) {
2047   EnsureSpace ensure_space(this);
2048   EMIT(0xF2);
2049   EMIT(0x0F);
2050   EMIT(0x5A);
2051   emit_sse_operand(dst, src);
2052 }
2053
2054
2055 void Assembler::addsd(XMMRegister dst, const Operand& src) {
2056   EnsureSpace ensure_space(this);
2057   EMIT(0xF2);
2058   EMIT(0x0F);
2059   EMIT(0x58);
2060   emit_sse_operand(dst, src);
2061 }
2062
2063
2064 void Assembler::mulsd(XMMRegister dst, const Operand& src) {
2065   EnsureSpace ensure_space(this);
2066   EMIT(0xF2);
2067   EMIT(0x0F);
2068   EMIT(0x59);
2069   emit_sse_operand(dst, src);
2070 }
2071
2072
2073 void Assembler::subsd(XMMRegister dst, const Operand& src) {
2074   EnsureSpace ensure_space(this);
2075   EMIT(0xF2);
2076   EMIT(0x0F);
2077   EMIT(0x5C);
2078   emit_sse_operand(dst, src);
2079 }
2080
2081
2082 void Assembler::divsd(XMMRegister dst, const Operand& src) {
2083   EnsureSpace ensure_space(this);
2084   EMIT(0xF2);
2085   EMIT(0x0F);
2086   EMIT(0x5E);
2087   emit_sse_operand(dst, src);
2088 }
2089
2090
2091 void Assembler::xorpd(XMMRegister dst, XMMRegister src) {
2092   EnsureSpace ensure_space(this);
2093   EMIT(0x66);
2094   EMIT(0x0F);
2095   EMIT(0x57);
2096   emit_sse_operand(dst, src);
2097 }
2098
2099
2100 void Assembler::andps(XMMRegister dst, const Operand& src) {
2101   EnsureSpace ensure_space(this);
2102   EMIT(0x0F);
2103   EMIT(0x54);
2104   emit_sse_operand(dst, src);
2105 }
2106
2107
2108 void Assembler::orps(XMMRegister dst, const Operand& src) {
2109   EnsureSpace ensure_space(this);
2110   EMIT(0x0F);
2111   EMIT(0x56);
2112   emit_sse_operand(dst, src);
2113 }
2114
2115
2116 void Assembler::xorps(XMMRegister dst, const Operand& src) {
2117   EnsureSpace ensure_space(this);
2118   EMIT(0x0F);
2119   EMIT(0x57);
2120   emit_sse_operand(dst, src);
2121 }
2122
2123
2124 void Assembler::addps(XMMRegister dst, const Operand& src) {
2125   EnsureSpace ensure_space(this);
2126   EMIT(0x0F);
2127   EMIT(0x58);
2128   emit_sse_operand(dst, src);
2129 }
2130
2131
2132 void Assembler::subps(XMMRegister dst, const Operand& src) {
2133   EnsureSpace ensure_space(this);
2134   EMIT(0x0F);
2135   EMIT(0x5C);
2136   emit_sse_operand(dst, src);
2137 }
2138
2139
2140 void Assembler::mulps(XMMRegister dst, const Operand& src) {
2141   EnsureSpace ensure_space(this);
2142   EMIT(0x0F);
2143   EMIT(0x59);
2144   emit_sse_operand(dst, src);
2145 }
2146
2147
2148 void Assembler::divps(XMMRegister dst, const Operand& src) {
2149   EnsureSpace ensure_space(this);
2150   EMIT(0x0F);
2151   EMIT(0x5E);
2152   emit_sse_operand(dst, src);
2153 }
2154
2155
2156 void Assembler::sqrtsd(XMMRegister dst, const Operand& src) {
2157   EnsureSpace ensure_space(this);
2158   EMIT(0xF2);
2159   EMIT(0x0F);
2160   EMIT(0x51);
2161   emit_sse_operand(dst, src);
2162 }
2163
2164
2165 void Assembler::andpd(XMMRegister dst, XMMRegister src) {
2166   EnsureSpace ensure_space(this);
2167   EMIT(0x66);
2168   EMIT(0x0F);
2169   EMIT(0x54);
2170   emit_sse_operand(dst, src);
2171 }
2172
2173
2174 void Assembler::orpd(XMMRegister dst, XMMRegister src) {
2175   EnsureSpace ensure_space(this);
2176   EMIT(0x66);
2177   EMIT(0x0F);
2178   EMIT(0x56);
2179   emit_sse_operand(dst, src);
2180 }
2181
2182
2183 void Assembler::ucomisd(XMMRegister dst, const Operand& src) {
2184   EnsureSpace ensure_space(this);
2185   EMIT(0x66);
2186   EMIT(0x0F);
2187   EMIT(0x2E);
2188   emit_sse_operand(dst, src);
2189 }
2190
2191
2192 void Assembler::roundsd(XMMRegister dst, XMMRegister src, RoundingMode mode) {
2193   DCHECK(IsEnabled(SSE4_1));
2194   EnsureSpace ensure_space(this);
2195   EMIT(0x66);
2196   EMIT(0x0F);
2197   EMIT(0x3A);
2198   EMIT(0x0B);
2199   emit_sse_operand(dst, src);
2200   // Mask precision exeption.
2201   EMIT(static_cast<byte>(mode) | 0x8);
2202 }
2203
2204
2205 void Assembler::movmskpd(Register dst, XMMRegister src) {
2206   EnsureSpace ensure_space(this);
2207   EMIT(0x66);
2208   EMIT(0x0F);
2209   EMIT(0x50);
2210   emit_sse_operand(dst, src);
2211 }
2212
2213
2214 void Assembler::movmskps(Register dst, XMMRegister src) {
2215   EnsureSpace ensure_space(this);
2216   EMIT(0x0F);
2217   EMIT(0x50);
2218   emit_sse_operand(dst, src);
2219 }
2220
2221
2222 void Assembler::pcmpeqd(XMMRegister dst, XMMRegister src) {
2223   EnsureSpace ensure_space(this);
2224   EMIT(0x66);
2225   EMIT(0x0F);
2226   EMIT(0x76);
2227   emit_sse_operand(dst, src);
2228 }
2229
2230
2231 void Assembler::cmpltsd(XMMRegister dst, XMMRegister src) {
2232   EnsureSpace ensure_space(this);
2233   EMIT(0xF2);
2234   EMIT(0x0F);
2235   EMIT(0xC2);
2236   emit_sse_operand(dst, src);
2237   EMIT(1);  // LT == 1
2238 }
2239
2240
2241 void Assembler::movaps(XMMRegister dst, XMMRegister src) {
2242   EnsureSpace ensure_space(this);
2243   EMIT(0x0F);
2244   EMIT(0x28);
2245   emit_sse_operand(dst, src);
2246 }
2247
2248
2249 void Assembler::shufps(XMMRegister dst, XMMRegister src, byte imm8) {
2250   DCHECK(is_uint8(imm8));
2251   EnsureSpace ensure_space(this);
2252   EMIT(0x0F);
2253   EMIT(0xC6);
2254   emit_sse_operand(dst, src);
2255   EMIT(imm8);
2256 }
2257
2258
2259 void Assembler::movdqa(const Operand& dst, XMMRegister src) {
2260   EnsureSpace ensure_space(this);
2261   EMIT(0x66);
2262   EMIT(0x0F);
2263   EMIT(0x7F);
2264   emit_sse_operand(src, dst);
2265 }
2266
2267
2268 void Assembler::movdqa(XMMRegister dst, const Operand& src) {
2269   EnsureSpace ensure_space(this);
2270   EMIT(0x66);
2271   EMIT(0x0F);
2272   EMIT(0x6F);
2273   emit_sse_operand(dst, src);
2274 }
2275
2276
2277 void Assembler::movdqu(const Operand& dst, XMMRegister src ) {
2278   EnsureSpace ensure_space(this);
2279   EMIT(0xF3);
2280   EMIT(0x0F);
2281   EMIT(0x7F);
2282   emit_sse_operand(src, dst);
2283 }
2284
2285
2286 void Assembler::movdqu(XMMRegister dst, const Operand& src) {
2287   EnsureSpace ensure_space(this);
2288   EMIT(0xF3);
2289   EMIT(0x0F);
2290   EMIT(0x6F);
2291   emit_sse_operand(dst, src);
2292 }
2293
2294
2295 void Assembler::movntdqa(XMMRegister dst, const Operand& src) {
2296   DCHECK(IsEnabled(SSE4_1));
2297   EnsureSpace ensure_space(this);
2298   EMIT(0x66);
2299   EMIT(0x0F);
2300   EMIT(0x38);
2301   EMIT(0x2A);
2302   emit_sse_operand(dst, src);
2303 }
2304
2305
2306 void Assembler::movntdq(const Operand& dst, XMMRegister src) {
2307   EnsureSpace ensure_space(this);
2308   EMIT(0x66);
2309   EMIT(0x0F);
2310   EMIT(0xE7);
2311   emit_sse_operand(src, dst);
2312 }
2313
2314
2315 void Assembler::prefetch(const Operand& src, int level) {
2316   DCHECK(is_uint2(level));
2317   EnsureSpace ensure_space(this);
2318   EMIT(0x0F);
2319   EMIT(0x18);
2320   // Emit hint number in Reg position of RegR/M.
2321   XMMRegister code = XMMRegister::from_code(level);
2322   emit_sse_operand(code, src);
2323 }
2324
2325
2326 void Assembler::movsd(const Operand& dst, XMMRegister src ) {
2327   EnsureSpace ensure_space(this);
2328   EMIT(0xF2);  // double
2329   EMIT(0x0F);
2330   EMIT(0x11);  // store
2331   emit_sse_operand(src, dst);
2332 }
2333
2334
2335 void Assembler::movsd(XMMRegister dst, const Operand& src) {
2336   EnsureSpace ensure_space(this);
2337   EMIT(0xF2);  // double
2338   EMIT(0x0F);
2339   EMIT(0x10);  // load
2340   emit_sse_operand(dst, src);
2341 }
2342
2343
2344 void Assembler::movss(const Operand& dst, XMMRegister src ) {
2345   EnsureSpace ensure_space(this);
2346   EMIT(0xF3);  // float
2347   EMIT(0x0F);
2348   EMIT(0x11);  // store
2349   emit_sse_operand(src, dst);
2350 }
2351
2352
2353 void Assembler::movss(XMMRegister dst, const Operand& src) {
2354   EnsureSpace ensure_space(this);
2355   EMIT(0xF3);  // float
2356   EMIT(0x0F);
2357   EMIT(0x10);  // load
2358   emit_sse_operand(dst, src);
2359 }
2360
2361
2362 void Assembler::movd(XMMRegister dst, const Operand& src) {
2363   EnsureSpace ensure_space(this);
2364   EMIT(0x66);
2365   EMIT(0x0F);
2366   EMIT(0x6E);
2367   emit_sse_operand(dst, src);
2368 }
2369
2370
2371 void Assembler::movd(const Operand& dst, XMMRegister src) {
2372   EnsureSpace ensure_space(this);
2373   EMIT(0x66);
2374   EMIT(0x0F);
2375   EMIT(0x7E);
2376   emit_sse_operand(src, dst);
2377 }
2378
2379
2380 void Assembler::extractps(Register dst, XMMRegister src, byte imm8) {
2381   DCHECK(IsEnabled(SSE4_1));
2382   DCHECK(is_uint8(imm8));
2383   EnsureSpace ensure_space(this);
2384   EMIT(0x66);
2385   EMIT(0x0F);
2386   EMIT(0x3A);
2387   EMIT(0x17);
2388   emit_sse_operand(src, dst);
2389   EMIT(imm8);
2390 }
2391
2392
2393 void Assembler::pand(XMMRegister dst, XMMRegister src) {
2394   EnsureSpace ensure_space(this);
2395   EMIT(0x66);
2396   EMIT(0x0F);
2397   EMIT(0xDB);
2398   emit_sse_operand(dst, src);
2399 }
2400
2401
2402 void Assembler::pxor(XMMRegister dst, XMMRegister src) {
2403   EnsureSpace ensure_space(this);
2404   EMIT(0x66);
2405   EMIT(0x0F);
2406   EMIT(0xEF);
2407   emit_sse_operand(dst, src);
2408 }
2409
2410
2411 void Assembler::por(XMMRegister dst, XMMRegister src) {
2412   EnsureSpace ensure_space(this);
2413   EMIT(0x66);
2414   EMIT(0x0F);
2415   EMIT(0xEB);
2416   emit_sse_operand(dst, src);
2417 }
2418
2419
2420 void Assembler::ptest(XMMRegister dst, XMMRegister src) {
2421   DCHECK(IsEnabled(SSE4_1));
2422   EnsureSpace ensure_space(this);
2423   EMIT(0x66);
2424   EMIT(0x0F);
2425   EMIT(0x38);
2426   EMIT(0x17);
2427   emit_sse_operand(dst, src);
2428 }
2429
2430
2431 void Assembler::pslld(XMMRegister reg, int8_t shift) {
2432   EnsureSpace ensure_space(this);
2433   EMIT(0x66);
2434   EMIT(0x0F);
2435   EMIT(0x72);
2436   emit_sse_operand(esi, reg);  // esi == 6
2437   EMIT(shift);
2438 }
2439
2440
2441 void Assembler::psrld(XMMRegister reg, int8_t shift) {
2442   EnsureSpace ensure_space(this);
2443   EMIT(0x66);
2444   EMIT(0x0F);
2445   EMIT(0x72);
2446   emit_sse_operand(edx, reg);  // edx == 2
2447   EMIT(shift);
2448 }
2449
2450
2451 void Assembler::psllq(XMMRegister reg, int8_t shift) {
2452   EnsureSpace ensure_space(this);
2453   EMIT(0x66);
2454   EMIT(0x0F);
2455   EMIT(0x73);
2456   emit_sse_operand(esi, reg);  // esi == 6
2457   EMIT(shift);
2458 }
2459
2460
2461 void Assembler::psllq(XMMRegister dst, XMMRegister src) {
2462   EnsureSpace ensure_space(this);
2463   EMIT(0x66);
2464   EMIT(0x0F);
2465   EMIT(0xF3);
2466   emit_sse_operand(dst, src);
2467 }
2468
2469
2470 void Assembler::psrlq(XMMRegister reg, int8_t shift) {
2471   EnsureSpace ensure_space(this);
2472   EMIT(0x66);
2473   EMIT(0x0F);
2474   EMIT(0x73);
2475   emit_sse_operand(edx, reg);  // edx == 2
2476   EMIT(shift);
2477 }
2478
2479
2480 void Assembler::psrlq(XMMRegister dst, XMMRegister src) {
2481   EnsureSpace ensure_space(this);
2482   EMIT(0x66);
2483   EMIT(0x0F);
2484   EMIT(0xD3);
2485   emit_sse_operand(dst, src);
2486 }
2487
2488
2489 void Assembler::pshufd(XMMRegister dst, XMMRegister src, uint8_t shuffle) {
2490   EnsureSpace ensure_space(this);
2491   EMIT(0x66);
2492   EMIT(0x0F);
2493   EMIT(0x70);
2494   emit_sse_operand(dst, src);
2495   EMIT(shuffle);
2496 }
2497
2498
2499 void Assembler::pextrd(const Operand& dst, XMMRegister src, int8_t offset) {
2500   DCHECK(IsEnabled(SSE4_1));
2501   EnsureSpace ensure_space(this);
2502   EMIT(0x66);
2503   EMIT(0x0F);
2504   EMIT(0x3A);
2505   EMIT(0x16);
2506   emit_sse_operand(src, dst);
2507   EMIT(offset);
2508 }
2509
2510
2511 void Assembler::pinsrd(XMMRegister dst, const Operand& src, int8_t offset) {
2512   DCHECK(IsEnabled(SSE4_1));
2513   EnsureSpace ensure_space(this);
2514   EMIT(0x66);
2515   EMIT(0x0F);
2516   EMIT(0x3A);
2517   EMIT(0x22);
2518   emit_sse_operand(dst, src);
2519   EMIT(offset);
2520 }
2521
2522
2523 void Assembler::addss(XMMRegister dst, const Operand& src) {
2524   EnsureSpace ensure_space(this);
2525   EMIT(0xF3);
2526   EMIT(0x0F);
2527   EMIT(0x58);
2528   emit_sse_operand(dst, src);
2529 }
2530
2531
2532 void Assembler::subss(XMMRegister dst, const Operand& src) {
2533   EnsureSpace ensure_space(this);
2534   EMIT(0xF3);
2535   EMIT(0x0F);
2536   EMIT(0x5C);
2537   emit_sse_operand(dst, src);
2538 }
2539
2540
2541 void Assembler::mulss(XMMRegister dst, const Operand& src) {
2542   EnsureSpace ensure_space(this);
2543   EMIT(0xF3);
2544   EMIT(0x0F);
2545   EMIT(0x59);
2546   emit_sse_operand(dst, src);
2547 }
2548
2549
2550 void Assembler::divss(XMMRegister dst, const Operand& src) {
2551   EnsureSpace ensure_space(this);
2552   EMIT(0xF3);
2553   EMIT(0x0F);
2554   EMIT(0x5E);
2555   emit_sse_operand(dst, src);
2556 }
2557
2558
2559 void Assembler::ucomiss(XMMRegister dst, const Operand& src) {
2560   EnsureSpace ensure_space(this);
2561   EMIT(0x0f);
2562   EMIT(0x2e);
2563   emit_sse_operand(dst, src);
2564 }
2565
2566
2567 // AVX instructions
2568 void Assembler::vfmasd(byte op, XMMRegister dst, XMMRegister src1,
2569                        const Operand& src2) {
2570   DCHECK(IsEnabled(FMA3));
2571   EnsureSpace ensure_space(this);
2572   emit_vex_prefix(src1, kLIG, k66, k0F38, kW1);
2573   EMIT(op);
2574   emit_sse_operand(dst, src2);
2575 }
2576
2577
2578 void Assembler::vfmass(byte op, XMMRegister dst, XMMRegister src1,
2579                        const Operand& src2) {
2580   DCHECK(IsEnabled(FMA3));
2581   EnsureSpace ensure_space(this);
2582   emit_vex_prefix(src1, kLIG, k66, k0F38, kW0);
2583   EMIT(op);
2584   emit_sse_operand(dst, src2);
2585 }
2586
2587
2588 void Assembler::vsd(byte op, XMMRegister dst, XMMRegister src1,
2589                     const Operand& src2) {
2590   DCHECK(IsEnabled(AVX));
2591   EnsureSpace ensure_space(this);
2592   emit_vex_prefix(src1, kLIG, kF2, k0F, kWIG);
2593   EMIT(op);
2594   emit_sse_operand(dst, src2);
2595 }
2596
2597
2598 void Assembler::emit_sse_operand(XMMRegister reg, const Operand& adr) {
2599   Register ireg = { reg.code() };
2600   emit_operand(ireg, adr);
2601 }
2602
2603
2604 void Assembler::emit_sse_operand(XMMRegister dst, XMMRegister src) {
2605   EMIT(0xC0 | dst.code() << 3 | src.code());
2606 }
2607
2608
2609 void Assembler::emit_sse_operand(Register dst, XMMRegister src) {
2610   EMIT(0xC0 | dst.code() << 3 | src.code());
2611 }
2612
2613
2614 void Assembler::emit_sse_operand(XMMRegister dst, Register src) {
2615   EMIT(0xC0 | (dst.code() << 3) | src.code());
2616 }
2617
2618
2619 void Assembler::emit_vex_prefix(XMMRegister vreg, VectorLength l, SIMDPrefix pp,
2620                                 LeadingOpcode mm, VexW w) {
2621   if (mm != k0F || w != kW0) {
2622     EMIT(0xc4);
2623     EMIT(0xc0 | mm);
2624     EMIT(w | ((~vreg.code() & 0xf) << 3) | l | pp);
2625   } else {
2626     EMIT(0xc5);
2627     EMIT(((~vreg.code()) << 3) | l | pp);
2628   }
2629 }
2630
2631
2632 void Assembler::GrowBuffer() {
2633   DCHECK(buffer_overflow());
2634   if (!own_buffer_) FATAL("external code buffer is too small");
2635
2636   // Compute new buffer size.
2637   CodeDesc desc;  // the new buffer
2638   desc.buffer_size = 2 * buffer_size_;
2639
2640   // Some internal data structures overflow for very large buffers,
2641   // they must ensure that kMaximalBufferSize is not too large.
2642   if ((desc.buffer_size > kMaximalBufferSize) ||
2643       (desc.buffer_size > isolate()->heap()->MaxOldGenerationSize())) {
2644     V8::FatalProcessOutOfMemory("Assembler::GrowBuffer");
2645   }
2646
2647   // Set up new buffer.
2648   desc.buffer = NewArray<byte>(desc.buffer_size);
2649   desc.instr_size = pc_offset();
2650   desc.reloc_size = (buffer_ + buffer_size_) - (reloc_info_writer.pos());
2651
2652   // Clear the buffer in debug mode. Use 'int3' instructions to make
2653   // sure to get into problems if we ever run uninitialized code.
2654 #ifdef DEBUG
2655   memset(desc.buffer, 0xCC, desc.buffer_size);
2656 #endif
2657
2658   // Copy the data.
2659   int pc_delta = desc.buffer - buffer_;
2660   int rc_delta = (desc.buffer + desc.buffer_size) - (buffer_ + buffer_size_);
2661   MemMove(desc.buffer, buffer_, desc.instr_size);
2662   MemMove(rc_delta + reloc_info_writer.pos(), reloc_info_writer.pos(),
2663           desc.reloc_size);
2664
2665   // Switch buffers.
2666   DeleteArray(buffer_);
2667   buffer_ = desc.buffer;
2668   buffer_size_ = desc.buffer_size;
2669   pc_ += pc_delta;
2670   reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
2671                                reloc_info_writer.last_pc() + pc_delta);
2672
2673   // Relocate internal references.
2674   for (auto pos : internal_reference_positions_) {
2675     int32_t* p = reinterpret_cast<int32_t*>(buffer_ + pos);
2676     *p += pc_delta;
2677   }
2678
2679   DCHECK(!buffer_overflow());
2680 }
2681
2682
2683 void Assembler::emit_arith_b(int op1, int op2, Register dst, int imm8) {
2684   DCHECK(is_uint8(op1) && is_uint8(op2));  // wrong opcode
2685   DCHECK(is_uint8(imm8));
2686   DCHECK((op1 & 0x01) == 0);  // should be 8bit operation
2687   EMIT(op1);
2688   EMIT(op2 | dst.code());
2689   EMIT(imm8);
2690 }
2691
2692
2693 void Assembler::emit_arith(int sel, Operand dst, const Immediate& x) {
2694   DCHECK((0 <= sel) && (sel <= 7));
2695   Register ireg = { sel };
2696   if (x.is_int8()) {
2697     EMIT(0x83);  // using a sign-extended 8-bit immediate.
2698     emit_operand(ireg, dst);
2699     EMIT(x.x_ & 0xFF);
2700   } else if (dst.is_reg(eax)) {
2701     EMIT((sel << 3) | 0x05);  // short form if the destination is eax.
2702     emit(x);
2703   } else {
2704     EMIT(0x81);  // using a literal 32-bit immediate.
2705     emit_operand(ireg, dst);
2706     emit(x);
2707   }
2708 }
2709
2710
2711 void Assembler::emit_operand(Register reg, const Operand& adr) {
2712   const unsigned length = adr.len_;
2713   DCHECK(length > 0);
2714
2715   // Emit updated ModRM byte containing the given register.
2716   pc_[0] = (adr.buf_[0] & ~0x38) | (reg.code() << 3);
2717
2718   // Emit the rest of the encoded operand.
2719   for (unsigned i = 1; i < length; i++) pc_[i] = adr.buf_[i];
2720   pc_ += length;
2721
2722   // Emit relocation information if necessary.
2723   if (length >= sizeof(int32_t) && !RelocInfo::IsNone(adr.rmode_)) {
2724     pc_ -= sizeof(int32_t);  // pc_ must be *at* disp32
2725     RecordRelocInfo(adr.rmode_);
2726     if (adr.rmode_ == RelocInfo::INTERNAL_REFERENCE) {  // Fixup for labels
2727       emit_label(*reinterpret_cast<Label**>(pc_));
2728     } else {
2729       pc_ += sizeof(int32_t);
2730     }
2731   }
2732 }
2733
2734
2735 void Assembler::emit_label(Label* label) {
2736   if (label->is_bound()) {
2737     internal_reference_positions_.push_back(pc_offset());
2738     emit(reinterpret_cast<uint32_t>(buffer_ + label->pos()));
2739   } else {
2740     emit_disp(label, Displacement::CODE_ABSOLUTE);
2741   }
2742 }
2743
2744
2745 void Assembler::emit_farith(int b1, int b2, int i) {
2746   DCHECK(is_uint8(b1) && is_uint8(b2));  // wrong opcode
2747   DCHECK(0 <= i &&  i < 8);  // illegal stack offset
2748   EMIT(b1);
2749   EMIT(b2 + i);
2750 }
2751
2752
2753 void Assembler::db(uint8_t data) {
2754   EnsureSpace ensure_space(this);
2755   EMIT(data);
2756 }
2757
2758
2759 void Assembler::dd(uint32_t data) {
2760   EnsureSpace ensure_space(this);
2761   emit(data);
2762 }
2763
2764
2765 void Assembler::dd(Label* label) {
2766   EnsureSpace ensure_space(this);
2767   RecordRelocInfo(RelocInfo::INTERNAL_REFERENCE);
2768   emit_label(label);
2769 }
2770
2771
2772 void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
2773   DCHECK(!RelocInfo::IsNone(rmode));
2774   // Don't record external references unless the heap will be serialized.
2775   if (rmode == RelocInfo::EXTERNAL_REFERENCE &&
2776       !serializer_enabled() && !emit_debug_code()) {
2777     return;
2778   }
2779   RelocInfo rinfo(pc_, rmode, data, NULL);
2780   reloc_info_writer.Write(&rinfo);
2781 }
2782
2783
2784 Handle<ConstantPoolArray> Assembler::NewConstantPool(Isolate* isolate) {
2785   // No out-of-line constant pool support.
2786   DCHECK(!FLAG_enable_ool_constant_pool);
2787   return isolate->factory()->empty_constant_pool_array();
2788 }
2789
2790
2791 void Assembler::PopulateConstantPool(ConstantPoolArray* constant_pool) {
2792   // No out-of-line constant pool support.
2793   DCHECK(!FLAG_enable_ool_constant_pool);
2794   return;
2795 }
2796
2797
2798 #ifdef GENERATED_CODE_COVERAGE
2799 static FILE* coverage_log = NULL;
2800
2801
2802 static void InitCoverageLog() {
2803   char* file_name = getenv("V8_GENERATED_CODE_COVERAGE_LOG");
2804   if (file_name != NULL) {
2805     coverage_log = fopen(file_name, "aw+");
2806   }
2807 }
2808
2809
2810 void LogGeneratedCodeCoverage(const char* file_line) {
2811   const char* return_address = (&file_line)[-1];
2812   char* push_insn = const_cast<char*>(return_address - 12);
2813   push_insn[0] = 0xeb;  // Relative branch insn.
2814   push_insn[1] = 13;    // Skip over coverage insns.
2815   if (coverage_log != NULL) {
2816     fprintf(coverage_log, "%s\n", file_line);
2817     fflush(coverage_log);
2818   }
2819 }
2820
2821 #endif
2822
2823 } }  // namespace v8::internal
2824
2825 #endif  // V8_TARGET_ARCH_IA32