57987bc7513c6337ae1dff885ddb9fba162f6430
[platform/upstream/v8.git] / src / ia32 / assembler-ia32.h
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 are
6 // 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 distribution.
14 //
15 // - Neither the name of Sun Microsystems or the names of contributors may
16 // be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // The original source code covered by the above license above has been
32 // modified significantly by Google Inc.
33 // Copyright 2011 the V8 project authors. All rights reserved.
34
35 // A light-weight IA32 Assembler.
36
37 #ifndef V8_IA32_ASSEMBLER_IA32_H_
38 #define V8_IA32_ASSEMBLER_IA32_H_
39
40 #include <deque>
41
42 #include "src/assembler.h"
43 #include "src/compiler.h"
44 #include "src/isolate.h"
45
46 namespace v8 {
47 namespace internal {
48
49 // CPU Registers.
50 //
51 // 1) We would prefer to use an enum, but enum values are assignment-
52 // compatible with int, which has caused code-generation bugs.
53 //
54 // 2) We would prefer to use a class instead of a struct but we don't like
55 // the register initialization to depend on the particular initialization
56 // order (which appears to be different on OS X, Linux, and Windows for the
57 // installed versions of C++ we tried). Using a struct permits C-style
58 // "initialization". Also, the Register objects cannot be const as this
59 // forces initialization stubs in MSVC, making us dependent on initialization
60 // order.
61 //
62 // 3) By not using an enum, we are possibly preventing the compiler from
63 // doing certain constant folds, which may significantly reduce the
64 // code generated for some assembly instructions (because they boil down
65 // to a few constants). If this is a problem, we could change the code
66 // such that we use an enum in optimized mode, and the struct in debug
67 // mode. This way we get the compile-time error checking in debug mode
68 // and best performance in optimized code.
69 //
70 struct Register {
71   static const int kMaxNumAllocatableRegisters = 6;
72   static int NumAllocatableRegisters() {
73     return kMaxNumAllocatableRegisters;
74   }
75   static const int kNumRegisters = 8;
76
77   static inline const char* AllocationIndexToString(int index);
78
79   static inline int ToAllocationIndex(Register reg);
80
81   static inline Register FromAllocationIndex(int index);
82
83   static Register from_code(int code) {
84     DCHECK(code >= 0);
85     DCHECK(code < kNumRegisters);
86     Register r = { code };
87     return r;
88   }
89   bool is_valid() const { return 0 <= code_ && code_ < kNumRegisters; }
90   bool is(Register reg) const { return code_ == reg.code_; }
91   // eax, ebx, ecx and edx are byte registers, the rest are not.
92   bool is_byte_register() const { return code_ <= 3; }
93   int code() const {
94     DCHECK(is_valid());
95     return code_;
96   }
97   int bit() const {
98     DCHECK(is_valid());
99     return 1 << code_;
100   }
101
102   // Unfortunately we can't make this private in a struct.
103   int code_;
104 };
105
106 const int kRegister_eax_Code = 0;
107 const int kRegister_ecx_Code = 1;
108 const int kRegister_edx_Code = 2;
109 const int kRegister_ebx_Code = 3;
110 const int kRegister_esp_Code = 4;
111 const int kRegister_ebp_Code = 5;
112 const int kRegister_esi_Code = 6;
113 const int kRegister_edi_Code = 7;
114 const int kRegister_no_reg_Code = -1;
115
116 const Register eax = { kRegister_eax_Code };
117 const Register ecx = { kRegister_ecx_Code };
118 const Register edx = { kRegister_edx_Code };
119 const Register ebx = { kRegister_ebx_Code };
120 const Register esp = { kRegister_esp_Code };
121 const Register ebp = { kRegister_ebp_Code };
122 const Register esi = { kRegister_esi_Code };
123 const Register edi = { kRegister_edi_Code };
124 const Register no_reg = { kRegister_no_reg_Code };
125
126
127 inline const char* Register::AllocationIndexToString(int index) {
128   DCHECK(index >= 0 && index < kMaxNumAllocatableRegisters);
129   // This is the mapping of allocation indices to registers.
130   const char* const kNames[] = { "eax", "ecx", "edx", "ebx", "esi", "edi" };
131   return kNames[index];
132 }
133
134
135 inline int Register::ToAllocationIndex(Register reg) {
136   DCHECK(reg.is_valid() && !reg.is(esp) && !reg.is(ebp));
137   return (reg.code() >= 6) ? reg.code() - 2 : reg.code();
138 }
139
140
141 inline Register Register::FromAllocationIndex(int index)  {
142   DCHECK(index >= 0 && index < kMaxNumAllocatableRegisters);
143   return (index >= 4) ? from_code(index + 2) : from_code(index);
144 }
145
146
147 struct XMMRegister {
148   static const int kMaxNumAllocatableRegisters = 7;
149   static const int kMaxNumRegisters = 8;
150   static int NumAllocatableRegisters() {
151     return kMaxNumAllocatableRegisters;
152   }
153
154   // TODO(turbofan): Proper support for float32.
155   static int NumAllocatableAliasedRegisters() {
156     return NumAllocatableRegisters();
157   }
158
159   static int ToAllocationIndex(XMMRegister reg) {
160     DCHECK(reg.code() != 0);
161     return reg.code() - 1;
162   }
163
164   static XMMRegister FromAllocationIndex(int index) {
165     DCHECK(index >= 0 && index < kMaxNumAllocatableRegisters);
166     return from_code(index + 1);
167   }
168
169   static XMMRegister from_code(int code) {
170     XMMRegister result = { code };
171     return result;
172   }
173
174   bool is_valid() const {
175     return 0 <= code_ && code_ < kMaxNumRegisters;
176   }
177
178   int code() const {
179     DCHECK(is_valid());
180     return code_;
181   }
182
183   bool is(XMMRegister reg) const { return code_ == reg.code_; }
184
185   static const char* AllocationIndexToString(int index) {
186     DCHECK(index >= 0 && index < kMaxNumAllocatableRegisters);
187     const char* const names[] = {
188       "xmm1",
189       "xmm2",
190       "xmm3",
191       "xmm4",
192       "xmm5",
193       "xmm6",
194       "xmm7"
195     };
196     return names[index];
197   }
198
199   int code_;
200 };
201
202
203 typedef XMMRegister DoubleRegister;
204
205
206 const XMMRegister xmm0 = { 0 };
207 const XMMRegister xmm1 = { 1 };
208 const XMMRegister xmm2 = { 2 };
209 const XMMRegister xmm3 = { 3 };
210 const XMMRegister xmm4 = { 4 };
211 const XMMRegister xmm5 = { 5 };
212 const XMMRegister xmm6 = { 6 };
213 const XMMRegister xmm7 = { 7 };
214 const XMMRegister no_xmm_reg = { -1 };
215
216
217 enum Condition {
218   // any value < 0 is considered no_condition
219   no_condition  = -1,
220
221   overflow      =  0,
222   no_overflow   =  1,
223   below         =  2,
224   above_equal   =  3,
225   equal         =  4,
226   not_equal     =  5,
227   below_equal   =  6,
228   above         =  7,
229   negative      =  8,
230   positive      =  9,
231   parity_even   = 10,
232   parity_odd    = 11,
233   less          = 12,
234   greater_equal = 13,
235   less_equal    = 14,
236   greater       = 15,
237
238   // aliases
239   carry         = below,
240   not_carry     = above_equal,
241   zero          = equal,
242   not_zero      = not_equal,
243   sign          = negative,
244   not_sign      = positive
245 };
246
247
248 // Returns the equivalent of !cc.
249 // Negation of the default no_condition (-1) results in a non-default
250 // no_condition value (-2). As long as tests for no_condition check
251 // for condition < 0, this will work as expected.
252 inline Condition NegateCondition(Condition cc) {
253   return static_cast<Condition>(cc ^ 1);
254 }
255
256
257 // Commute a condition such that {a cond b == b cond' a}.
258 inline Condition CommuteCondition(Condition cc) {
259   switch (cc) {
260     case below:
261       return above;
262     case above:
263       return below;
264     case above_equal:
265       return below_equal;
266     case below_equal:
267       return above_equal;
268     case less:
269       return greater;
270     case greater:
271       return less;
272     case greater_equal:
273       return less_equal;
274     case less_equal:
275       return greater_equal;
276     default:
277       return cc;
278   }
279 }
280
281
282 enum RoundingMode {
283   kRoundToNearest = 0x0,
284   kRoundDown = 0x1,
285   kRoundUp = 0x2,
286   kRoundToZero = 0x3
287 };
288
289
290 // -----------------------------------------------------------------------------
291 // Machine instruction Immediates
292
293 class Immediate BASE_EMBEDDED {
294  public:
295   inline explicit Immediate(int x);
296   inline explicit Immediate(const ExternalReference& ext);
297   inline explicit Immediate(Handle<Object> handle);
298   inline explicit Immediate(Smi* value);
299   inline explicit Immediate(Address addr);
300
301   static Immediate CodeRelativeOffset(Label* label) {
302     return Immediate(label);
303   }
304
305   bool is_zero() const { return x_ == 0 && RelocInfo::IsNone(rmode_); }
306   bool is_int8() const {
307     return -128 <= x_ && x_ < 128 && RelocInfo::IsNone(rmode_);
308   }
309   bool is_int16() const {
310     return -32768 <= x_ && x_ < 32768 && RelocInfo::IsNone(rmode_);
311   }
312
313  private:
314   inline explicit Immediate(Label* value);
315
316   int x_;
317   RelocInfo::Mode rmode_;
318
319   friend class Operand;
320   friend class Assembler;
321   friend class MacroAssembler;
322 };
323
324
325 // -----------------------------------------------------------------------------
326 // Machine instruction Operands
327
328 enum ScaleFactor {
329   times_1 = 0,
330   times_2 = 1,
331   times_4 = 2,
332   times_8 = 3,
333   times_int_size = times_4,
334   times_half_pointer_size = times_2,
335   times_pointer_size = times_4,
336   times_twice_pointer_size = times_8
337 };
338
339
340 class Operand BASE_EMBEDDED {
341  public:
342   // reg
343   INLINE(explicit Operand(Register reg));
344
345   // XMM reg
346   INLINE(explicit Operand(XMMRegister xmm_reg));
347
348   // [disp/r]
349   INLINE(explicit Operand(int32_t disp, RelocInfo::Mode rmode));
350
351   // [disp/r]
352   INLINE(explicit Operand(Immediate imm));
353
354   // [base + disp/r]
355   explicit Operand(Register base, int32_t disp,
356                    RelocInfo::Mode rmode = RelocInfo::NONE32);
357
358   // [base + index*scale + disp/r]
359   explicit Operand(Register base,
360                    Register index,
361                    ScaleFactor scale,
362                    int32_t disp,
363                    RelocInfo::Mode rmode = RelocInfo::NONE32);
364
365   // [index*scale + disp/r]
366   explicit Operand(Register index,
367                    ScaleFactor scale,
368                    int32_t disp,
369                    RelocInfo::Mode rmode = RelocInfo::NONE32);
370
371   static Operand JumpTable(Register index, ScaleFactor scale, Label* table) {
372     return Operand(index, scale, reinterpret_cast<int32_t>(table),
373                    RelocInfo::INTERNAL_REFERENCE);
374   }
375
376   static Operand StaticVariable(const ExternalReference& ext) {
377     return Operand(reinterpret_cast<int32_t>(ext.address()),
378                    RelocInfo::EXTERNAL_REFERENCE);
379   }
380
381   static Operand StaticArray(Register index,
382                              ScaleFactor scale,
383                              const ExternalReference& arr) {
384     return Operand(index, scale, reinterpret_cast<int32_t>(arr.address()),
385                    RelocInfo::EXTERNAL_REFERENCE);
386   }
387
388   static Operand ForCell(Handle<Cell> cell) {
389     AllowDeferredHandleDereference embedding_raw_address;
390     return Operand(reinterpret_cast<int32_t>(cell.location()),
391                    RelocInfo::CELL);
392   }
393
394   static Operand ForRegisterPlusImmediate(Register base, Immediate imm) {
395     return Operand(base, imm.x_, imm.rmode_);
396   }
397
398   // Returns true if this Operand is a wrapper for the specified register.
399   bool is_reg(Register reg) const;
400
401   // Returns true if this Operand is a wrapper for one register.
402   bool is_reg_only() const;
403
404   // Asserts that this Operand is a wrapper for one register and returns the
405   // register.
406   Register reg() const;
407
408  private:
409   // Set the ModRM byte without an encoded 'reg' register. The
410   // register is encoded later as part of the emit_operand operation.
411   inline void set_modrm(int mod, Register rm);
412
413   inline void set_sib(ScaleFactor scale, Register index, Register base);
414   inline void set_disp8(int8_t disp);
415   inline void set_dispr(int32_t disp, RelocInfo::Mode rmode);
416
417   byte buf_[6];
418   // The number of bytes in buf_.
419   unsigned int len_;
420   // Only valid if len_ > 4.
421   RelocInfo::Mode rmode_;
422
423   friend class Assembler;
424   friend class MacroAssembler;
425 };
426
427
428 // -----------------------------------------------------------------------------
429 // A Displacement describes the 32bit immediate field of an instruction which
430 // may be used together with a Label in order to refer to a yet unknown code
431 // position. Displacements stored in the instruction stream are used to describe
432 // the instruction and to chain a list of instructions using the same Label.
433 // A Displacement contains 2 different fields:
434 //
435 // next field: position of next displacement in the chain (0 = end of list)
436 // type field: instruction type
437 //
438 // A next value of null (0) indicates the end of a chain (note that there can
439 // be no displacement at position zero, because there is always at least one
440 // instruction byte before the displacement).
441 //
442 // Displacement _data field layout
443 //
444 // |31.....2|1......0|
445 // [  next  |  type  |
446
447 class Displacement BASE_EMBEDDED {
448  public:
449   enum Type { UNCONDITIONAL_JUMP, CODE_RELATIVE, OTHER, CODE_ABSOLUTE };
450
451   int data() const { return data_; }
452   Type type() const { return TypeField::decode(data_); }
453   void next(Label* L) const {
454     int n = NextField::decode(data_);
455     n > 0 ? L->link_to(n) : L->Unuse();
456   }
457   void link_to(Label* L) { init(L, type()); }
458
459   explicit Displacement(int data) { data_ = data; }
460
461   Displacement(Label* L, Type type) { init(L, type); }
462
463   void print() {
464     PrintF("%s (%x) ", (type() == UNCONDITIONAL_JUMP ? "jmp" : "[other]"),
465                        NextField::decode(data_));
466   }
467
468  private:
469   int data_;
470
471   class TypeField: public BitField<Type, 0, 2> {};
472   class NextField: public BitField<int,  2, 32-2> {};
473
474   void init(Label* L, Type type);
475 };
476
477
478 class Assembler : public AssemblerBase {
479  private:
480   // We check before assembling an instruction that there is sufficient
481   // space to write an instruction and its relocation information.
482   // The relocation writer's position must be kGap bytes above the end of
483   // the generated instructions. This leaves enough space for the
484   // longest possible ia32 instruction, 15 bytes, and the longest possible
485   // relocation information encoding, RelocInfoWriter::kMaxLength == 16.
486   // (There is a 15 byte limit on ia32 instruction length that rules out some
487   // otherwise valid instructions.)
488   // This allows for a single, fast space check per instruction.
489   static const int kGap = 32;
490
491  public:
492   // Create an assembler. Instructions and relocation information are emitted
493   // into a buffer, with the instructions starting from the beginning and the
494   // relocation information starting from the end of the buffer. See CodeDesc
495   // for a detailed comment on the layout (globals.h).
496   //
497   // If the provided buffer is NULL, the assembler allocates and grows its own
498   // buffer, and buffer_size determines the initial buffer size. The buffer is
499   // owned by the assembler and deallocated upon destruction of the assembler.
500   //
501   // If the provided buffer is not NULL, the assembler uses the provided buffer
502   // for code generation and assumes its size to be buffer_size. If the buffer
503   // is too small, a fatal error occurs. No deallocation of the buffer is done
504   // upon destruction of the assembler.
505   // TODO(vitalyr): the assembler does not need an isolate.
506   Assembler(Isolate* isolate, void* buffer, int buffer_size);
507   virtual ~Assembler() { }
508
509   // GetCode emits any pending (non-emitted) code and fills the descriptor
510   // desc. GetCode() is idempotent; it returns the same result if no other
511   // Assembler functions are invoked in between GetCode() calls.
512   void GetCode(CodeDesc* desc);
513
514   // Read/Modify the code target in the branch/call instruction at pc.
515   inline static Address target_address_at(Address pc, Address constant_pool);
516   inline static void set_target_address_at(
517       Address pc, Address constant_pool, Address target,
518       ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED);
519   static inline Address target_address_at(Address pc, Code* code) {
520     Address constant_pool = code ? code->constant_pool() : NULL;
521     return target_address_at(pc, constant_pool);
522   }
523   static inline void set_target_address_at(Address pc,
524                                            Code* code,
525                                            Address target,
526                                            ICacheFlushMode icache_flush_mode =
527                                                FLUSH_ICACHE_IF_NEEDED) {
528     Address constant_pool = code ? code->constant_pool() : NULL;
529     set_target_address_at(pc, constant_pool, target);
530   }
531
532   // Return the code target address at a call site from the return address
533   // of that call in the instruction stream.
534   inline static Address target_address_from_return_address(Address pc);
535
536   // This sets the branch destination (which is in the instruction on x86).
537   // This is for calls and branches within generated code.
538   inline static void deserialization_set_special_target_at(
539       Address instruction_payload, Code* code, Address target) {
540     set_target_address_at(instruction_payload, code, target);
541   }
542
543   // This sets the internal reference at the pc.
544   inline static void deserialization_set_target_internal_reference_at(
545       Address pc, Address target,
546       RelocInfo::Mode mode = RelocInfo::INTERNAL_REFERENCE);
547
548   static const int kSpecialTargetSize = kPointerSize;
549
550   // Distance between the address of the code target in the call instruction
551   // and the return address
552   static const int kCallTargetAddressOffset = kPointerSize;
553
554   static const int kCallInstructionLength = 5;
555
556   // The debug break slot must be able to contain a call instruction.
557   static const int kDebugBreakSlotLength = kCallInstructionLength;
558
559   // Distance between start of patched debug break slot and the emitted address
560   // to jump to.
561   static const int kPatchDebugBreakSlotAddressOffset = 1;  // JMP imm32.
562
563   // One byte opcode for test al, 0xXX.
564   static const byte kTestAlByte = 0xA8;
565   // One byte opcode for nop.
566   static const byte kNopByte = 0x90;
567
568   // One byte opcode for a short unconditional jump.
569   static const byte kJmpShortOpcode = 0xEB;
570   // One byte prefix for a short conditional jump.
571   static const byte kJccShortPrefix = 0x70;
572   static const byte kJncShortOpcode = kJccShortPrefix | not_carry;
573   static const byte kJcShortOpcode = kJccShortPrefix | carry;
574   static const byte kJnzShortOpcode = kJccShortPrefix | not_zero;
575   static const byte kJzShortOpcode = kJccShortPrefix | zero;
576
577
578   // ---------------------------------------------------------------------------
579   // Code generation
580   //
581   // - function names correspond one-to-one to ia32 instruction mnemonics
582   // - unless specified otherwise, instructions operate on 32bit operands
583   // - instructions on 8bit (byte) operands/registers have a trailing '_b'
584   // - instructions on 16bit (word) operands/registers have a trailing '_w'
585   // - naming conflicts with C++ keywords are resolved via a trailing '_'
586
587   // NOTE ON INTERFACE: Currently, the interface is not very consistent
588   // in the sense that some operations (e.g. mov()) can be called in more
589   // the one way to generate the same instruction: The Register argument
590   // can in some cases be replaced with an Operand(Register) argument.
591   // This should be cleaned up and made more orthogonal. The questions
592   // is: should we always use Operands instead of Registers where an
593   // Operand is possible, or should we have a Register (overloaded) form
594   // instead? We must be careful to make sure that the selected instruction
595   // is obvious from the parameters to avoid hard-to-find code generation
596   // bugs.
597
598   // Insert the smallest number of nop instructions
599   // possible to align the pc offset to a multiple
600   // of m. m must be a power of 2.
601   void Align(int m);
602   // Insert the smallest number of zero bytes possible to align the pc offset
603   // to a mulitple of m. m must be a power of 2 (>= 2).
604   void DataAlign(int m);
605   void Nop(int bytes = 1);
606   // Aligns code to something that's optimal for a jump target for the platform.
607   void CodeTargetAlign();
608
609   // Stack
610   void pushad();
611   void popad();
612
613   void pushfd();
614   void popfd();
615
616   void push(const Immediate& x);
617   void push_imm32(int32_t imm32);
618   void push(Register src);
619   void push(const Operand& src);
620
621   void pop(Register dst);
622   void pop(const Operand& dst);
623
624   void enter(const Immediate& size);
625   void leave();
626
627   // Moves
628   void mov_b(Register dst, Register src) { mov_b(dst, Operand(src)); }
629   void mov_b(Register dst, const Operand& src);
630   void mov_b(Register dst, int8_t imm8) { mov_b(Operand(dst), imm8); }
631   void mov_b(const Operand& dst, int8_t src) { mov_b(dst, Immediate(src)); }
632   void mov_b(const Operand& dst, const Immediate& src);
633   void mov_b(const Operand& dst, Register src);
634
635   void mov_w(Register dst, const Operand& src);
636   void mov_w(const Operand& dst, int16_t src) { mov_w(dst, Immediate(src)); }
637   void mov_w(const Operand& dst, const Immediate& src);
638   void mov_w(const Operand& dst, Register src);
639
640   void mov(Register dst, int32_t imm32);
641   void mov(Register dst, const Immediate& x);
642   void mov(Register dst, Handle<Object> handle);
643   void mov(Register dst, const Operand& src);
644   void mov(Register dst, Register src);
645   void mov(const Operand& dst, const Immediate& x);
646   void mov(const Operand& dst, Handle<Object> handle);
647   void mov(const Operand& dst, Register src);
648
649   void movsx_b(Register dst, Register src) { movsx_b(dst, Operand(src)); }
650   void movsx_b(Register dst, const Operand& src);
651
652   void movsx_w(Register dst, Register src) { movsx_w(dst, Operand(src)); }
653   void movsx_w(Register dst, const Operand& src);
654
655   void movzx_b(Register dst, Register src) { movzx_b(dst, Operand(src)); }
656   void movzx_b(Register dst, const Operand& src);
657
658   void movzx_w(Register dst, Register src) { movzx_w(dst, Operand(src)); }
659   void movzx_w(Register dst, const Operand& src);
660
661   // Conditional moves
662   void cmov(Condition cc, Register dst, Register src) {
663     cmov(cc, dst, Operand(src));
664   }
665   void cmov(Condition cc, Register dst, const Operand& src);
666
667   // Flag management.
668   void cld();
669
670   // Repetitive string instructions.
671   void rep_movs();
672   void rep_stos();
673   void stos();
674
675   // Exchange
676   void xchg(Register dst, Register src);
677   void xchg(Register dst, const Operand& src);
678
679   // Arithmetics
680   void adc(Register dst, int32_t imm32);
681   void adc(Register dst, const Operand& src);
682
683   void add(Register dst, Register src) { add(dst, Operand(src)); }
684   void add(Register dst, const Operand& src);
685   void add(const Operand& dst, Register src);
686   void add(Register dst, const Immediate& imm) { add(Operand(dst), imm); }
687   void add(const Operand& dst, const Immediate& x);
688
689   void and_(Register dst, int32_t imm32);
690   void and_(Register dst, const Immediate& x);
691   void and_(Register dst, Register src) { and_(dst, Operand(src)); }
692   void and_(Register dst, const Operand& src);
693   void and_(const Operand& dst, Register src);
694   void and_(const Operand& dst, const Immediate& x);
695
696   void cmpb(Register reg, int8_t imm8) { cmpb(Operand(reg), imm8); }
697   void cmpb(const Operand& op, int8_t imm8);
698   void cmpb(Register reg, const Operand& op);
699   void cmpb(const Operand& op, Register reg);
700   void cmpb_al(const Operand& op);
701   void cmpw_ax(const Operand& op);
702   void cmpw(const Operand& op, Immediate imm16);
703   void cmp(Register reg, int32_t imm32);
704   void cmp(Register reg, Handle<Object> handle);
705   void cmp(Register reg0, Register reg1) { cmp(reg0, Operand(reg1)); }
706   void cmp(Register reg, const Operand& op);
707   void cmp(Register reg, const Immediate& imm) { cmp(Operand(reg), imm); }
708   void cmp(const Operand& op, const Immediate& imm);
709   void cmp(const Operand& op, Handle<Object> handle);
710
711   void dec_b(Register dst);
712   void dec_b(const Operand& dst);
713
714   void dec(Register dst);
715   void dec(const Operand& dst);
716
717   void cdq();
718
719   void idiv(Register src) { idiv(Operand(src)); }
720   void idiv(const Operand& src);
721   void div(Register src) { div(Operand(src)); }
722   void div(const Operand& src);
723
724   // Signed multiply instructions.
725   void imul(Register src);                               // edx:eax = eax * src.
726   void imul(Register dst, Register src) { imul(dst, Operand(src)); }
727   void imul(Register dst, const Operand& src);           // dst = dst * src.
728   void imul(Register dst, Register src, int32_t imm32);  // dst = src * imm32.
729   void imul(Register dst, const Operand& src, int32_t imm32);
730
731   void inc(Register dst);
732   void inc(const Operand& dst);
733
734   void lea(Register dst, const Operand& src);
735
736   // Unsigned multiply instruction.
737   void mul(Register src);                                // edx:eax = eax * reg.
738
739   void neg(Register dst);
740   void neg(const Operand& dst);
741
742   void not_(Register dst);
743   void not_(const Operand& dst);
744
745   void or_(Register dst, int32_t imm32);
746   void or_(Register dst, Register src) { or_(dst, Operand(src)); }
747   void or_(Register dst, const Operand& src);
748   void or_(const Operand& dst, Register src);
749   void or_(Register dst, const Immediate& imm) { or_(Operand(dst), imm); }
750   void or_(const Operand& dst, const Immediate& x);
751
752   void rcl(Register dst, uint8_t imm8);
753   void rcr(Register dst, uint8_t imm8);
754
755   void ror(Register dst, uint8_t imm8) { ror(Operand(dst), imm8); }
756   void ror(const Operand& dst, uint8_t imm8);
757   void ror_cl(Register dst) { ror_cl(Operand(dst)); }
758   void ror_cl(const Operand& dst);
759
760   void sar(Register dst, uint8_t imm8) { sar(Operand(dst), imm8); }
761   void sar(const Operand& dst, uint8_t imm8);
762   void sar_cl(Register dst) { sar_cl(Operand(dst)); }
763   void sar_cl(const Operand& dst);
764
765   void sbb(Register dst, const Operand& src);
766
767   void shld(Register dst, Register src) { shld(dst, Operand(src)); }
768   void shld(Register dst, const Operand& src);
769
770   void shl(Register dst, uint8_t imm8) { shl(Operand(dst), imm8); }
771   void shl(const Operand& dst, uint8_t imm8);
772   void shl_cl(Register dst) { shl_cl(Operand(dst)); }
773   void shl_cl(const Operand& dst);
774
775   void shrd(Register dst, Register src) { shrd(dst, Operand(src)); }
776   void shrd(Register dst, const Operand& src);
777
778   void shr(Register dst, uint8_t imm8) { shr(Operand(dst), imm8); }
779   void shr(const Operand& dst, uint8_t imm8);
780   void shr_cl(Register dst) { shr_cl(Operand(dst)); }
781   void shr_cl(const Operand& dst);
782
783   void sub(Register dst, const Immediate& imm) { sub(Operand(dst), imm); }
784   void sub(const Operand& dst, const Immediate& x);
785   void sub(Register dst, Register src) { sub(dst, Operand(src)); }
786   void sub(Register dst, const Operand& src);
787   void sub(const Operand& dst, Register src);
788
789   void test(Register reg, const Immediate& imm);
790   void test(Register reg0, Register reg1) { test(reg0, Operand(reg1)); }
791   void test(Register reg, const Operand& op);
792   void test_b(Register reg, const Operand& op);
793   void test(const Operand& op, const Immediate& imm);
794   void test_b(Register reg, uint8_t imm8);
795   void test_b(const Operand& op, uint8_t imm8);
796
797   void xor_(Register dst, int32_t imm32);
798   void xor_(Register dst, Register src) { xor_(dst, Operand(src)); }
799   void xor_(Register dst, const Operand& src);
800   void xor_(const Operand& dst, Register src);
801   void xor_(Register dst, const Immediate& imm) { xor_(Operand(dst), imm); }
802   void xor_(const Operand& dst, const Immediate& x);
803
804   // Bit operations.
805   void bt(const Operand& dst, Register src);
806   void bts(Register dst, Register src) { bts(Operand(dst), src); }
807   void bts(const Operand& dst, Register src);
808   void bsr(Register dst, Register src) { bsr(dst, Operand(src)); }
809   void bsr(Register dst, const Operand& src);
810
811   // Miscellaneous
812   void hlt();
813   void int3();
814   void nop();
815   void ret(int imm16);
816   void ud2();
817
818   // Label operations & relative jumps (PPUM Appendix D)
819   //
820   // Takes a branch opcode (cc) and a label (L) and generates
821   // either a backward branch or a forward branch and links it
822   // to the label fixup chain. Usage:
823   //
824   // Label L;    // unbound label
825   // j(cc, &L);  // forward branch to unbound label
826   // bind(&L);   // bind label to the current pc
827   // j(cc, &L);  // backward branch to bound label
828   // bind(&L);   // illegal: a label may be bound only once
829   //
830   // Note: The same Label can be used for forward and backward branches
831   // but it may be bound only once.
832
833   void bind(Label* L);  // binds an unbound label L to the current code position
834
835   // Calls
836   void call(Label* L);
837   void call(byte* entry, RelocInfo::Mode rmode);
838   int CallSize(const Operand& adr);
839   void call(Register reg) { call(Operand(reg)); }
840   void call(const Operand& adr);
841   int CallSize(Handle<Code> code, RelocInfo::Mode mode);
842   void call(Handle<Code> code,
843             RelocInfo::Mode rmode,
844             TypeFeedbackId id = TypeFeedbackId::None());
845
846   // Jumps
847   // unconditional jump to L
848   void jmp(Label* L, Label::Distance distance = Label::kFar);
849   void jmp(byte* entry, RelocInfo::Mode rmode);
850   void jmp(Register reg) { jmp(Operand(reg)); }
851   void jmp(const Operand& adr);
852   void jmp(Handle<Code> code, RelocInfo::Mode rmode);
853
854   // Conditional jumps
855   void j(Condition cc,
856          Label* L,
857          Label::Distance distance = Label::kFar);
858   void j(Condition cc, byte* entry, RelocInfo::Mode rmode);
859   void j(Condition cc, Handle<Code> code,
860          RelocInfo::Mode rmode = RelocInfo::CODE_TARGET);
861
862   // Floating-point operations
863   void fld(int i);
864   void fstp(int i);
865
866   void fld1();
867   void fldz();
868   void fldpi();
869   void fldln2();
870
871   void fld_s(const Operand& adr);
872   void fld_d(const Operand& adr);
873
874   void fstp_s(const Operand& adr);
875   void fst_s(const Operand& adr);
876   void fstp_d(const Operand& adr);
877   void fst_d(const Operand& adr);
878
879   void fild_s(const Operand& adr);
880   void fild_d(const Operand& adr);
881
882   void fist_s(const Operand& adr);
883
884   void fistp_s(const Operand& adr);
885   void fistp_d(const Operand& adr);
886
887   // The fisttp instructions require SSE3.
888   void fisttp_s(const Operand& adr);
889   void fisttp_d(const Operand& adr);
890
891   void fabs();
892   void fchs();
893   void fcos();
894   void fsin();
895   void fptan();
896   void fyl2x();
897   void f2xm1();
898   void fscale();
899   void fninit();
900
901   void fadd(int i);
902   void fadd_i(int i);
903   void fsub(int i);
904   void fsub_i(int i);
905   void fmul(int i);
906   void fmul_i(int i);
907   void fdiv(int i);
908   void fdiv_i(int i);
909
910   void fisub_s(const Operand& adr);
911
912   void faddp(int i = 1);
913   void fsubp(int i = 1);
914   void fsubrp(int i = 1);
915   void fmulp(int i = 1);
916   void fdivp(int i = 1);
917   void fprem();
918   void fprem1();
919
920   void fxch(int i = 1);
921   void fincstp();
922   void ffree(int i = 0);
923
924   void ftst();
925   void fucomp(int i);
926   void fucompp();
927   void fucomi(int i);
928   void fucomip();
929   void fcompp();
930   void fnstsw_ax();
931   void fwait();
932   void fnclex();
933
934   void frndint();
935
936   void sahf();
937   void setcc(Condition cc, Register reg);
938
939   void cpuid();
940
941   // SSE instructions
942   void addss(XMMRegister dst, XMMRegister src) { addss(dst, Operand(src)); }
943   void addss(XMMRegister dst, const Operand& src);
944   void subss(XMMRegister dst, XMMRegister src) { subss(dst, Operand(src)); }
945   void subss(XMMRegister dst, const Operand& src);
946   void mulss(XMMRegister dst, XMMRegister src) { mulss(dst, Operand(src)); }
947   void mulss(XMMRegister dst, const Operand& src);
948   void divss(XMMRegister dst, XMMRegister src) { divss(dst, Operand(src)); }
949   void divss(XMMRegister dst, const Operand& src);
950   void sqrtss(XMMRegister dst, XMMRegister src) { sqrtss(dst, Operand(src)); }
951   void sqrtss(XMMRegister dst, const Operand& src);
952
953   void ucomiss(XMMRegister dst, XMMRegister src) { ucomiss(dst, Operand(src)); }
954   void ucomiss(XMMRegister dst, const Operand& src);
955   void movaps(XMMRegister dst, XMMRegister src);
956   void shufps(XMMRegister dst, XMMRegister src, byte imm8);
957
958   void maxss(XMMRegister dst, XMMRegister src) { maxss(dst, Operand(src)); }
959   void maxss(XMMRegister dst, const Operand& src);
960   void minss(XMMRegister dst, XMMRegister src) { minss(dst, Operand(src)); }
961   void minss(XMMRegister dst, const Operand& src);
962
963   void andps(XMMRegister dst, const Operand& src);
964   void andps(XMMRegister dst, XMMRegister src) { andps(dst, Operand(src)); }
965   void xorps(XMMRegister dst, const Operand& src);
966   void xorps(XMMRegister dst, XMMRegister src) { xorps(dst, Operand(src)); }
967   void orps(XMMRegister dst, const Operand& src);
968   void orps(XMMRegister dst, XMMRegister src) { orps(dst, Operand(src)); }
969
970   void addps(XMMRegister dst, const Operand& src);
971   void addps(XMMRegister dst, XMMRegister src) { addps(dst, Operand(src)); }
972   void subps(XMMRegister dst, const Operand& src);
973   void subps(XMMRegister dst, XMMRegister src) { subps(dst, Operand(src)); }
974   void mulps(XMMRegister dst, const Operand& src);
975   void mulps(XMMRegister dst, XMMRegister src) { mulps(dst, Operand(src)); }
976   void divps(XMMRegister dst, const Operand& src);
977   void divps(XMMRegister dst, XMMRegister src) { divps(dst, Operand(src)); }
978
979   // SSE2 instructions
980   void cvttss2si(Register dst, const Operand& src);
981   void cvttss2si(Register dst, XMMRegister src) {
982     cvttss2si(dst, Operand(src));
983   }
984   void cvttsd2si(Register dst, const Operand& src);
985   void cvttsd2si(Register dst, XMMRegister src) {
986     cvttsd2si(dst, Operand(src));
987   }
988   void cvtsd2si(Register dst, XMMRegister src);
989
990   void cvtsi2sd(XMMRegister dst, Register src) { cvtsi2sd(dst, Operand(src)); }
991   void cvtsi2sd(XMMRegister dst, const Operand& src);
992   void cvtss2sd(XMMRegister dst, const Operand& src);
993   void cvtss2sd(XMMRegister dst, XMMRegister src) {
994     cvtss2sd(dst, Operand(src));
995   }
996   void cvtsd2ss(XMMRegister dst, const Operand& src);
997   void cvtsd2ss(XMMRegister dst, XMMRegister src) {
998     cvtsd2ss(dst, Operand(src));
999   }
1000   void addsd(XMMRegister dst, XMMRegister src) { addsd(dst, Operand(src)); }
1001   void addsd(XMMRegister dst, const Operand& src);
1002   void subsd(XMMRegister dst, XMMRegister src) { subsd(dst, Operand(src)); }
1003   void subsd(XMMRegister dst, const Operand& src);
1004   void mulsd(XMMRegister dst, XMMRegister src) { mulsd(dst, Operand(src)); }
1005   void mulsd(XMMRegister dst, const Operand& src);
1006   void divsd(XMMRegister dst, XMMRegister src) { divsd(dst, Operand(src)); }
1007   void divsd(XMMRegister dst, const Operand& src);
1008   void xorpd(XMMRegister dst, XMMRegister src);
1009   void sqrtsd(XMMRegister dst, XMMRegister src) { sqrtsd(dst, Operand(src)); }
1010   void sqrtsd(XMMRegister dst, const Operand& src);
1011
1012   void andpd(XMMRegister dst, XMMRegister src);
1013   void orpd(XMMRegister dst, XMMRegister src);
1014
1015   void ucomisd(XMMRegister dst, XMMRegister src) { ucomisd(dst, Operand(src)); }
1016   void ucomisd(XMMRegister dst, const Operand& src);
1017
1018   void roundsd(XMMRegister dst, XMMRegister src, RoundingMode mode);
1019
1020   void movmskpd(Register dst, XMMRegister src);
1021   void movmskps(Register dst, XMMRegister src);
1022
1023   void cmpltsd(XMMRegister dst, XMMRegister src);
1024   void pcmpeqd(XMMRegister dst, XMMRegister src);
1025
1026   void punpckldq(XMMRegister dst, XMMRegister src);
1027   void punpckhdq(XMMRegister dst, XMMRegister src);
1028
1029   void maxsd(XMMRegister dst, XMMRegister src) { maxsd(dst, Operand(src)); }
1030   void maxsd(XMMRegister dst, const Operand& src);
1031   void minsd(XMMRegister dst, XMMRegister src) { minsd(dst, Operand(src)); }
1032   void minsd(XMMRegister dst, const Operand& src);
1033
1034   void movdqa(XMMRegister dst, const Operand& src);
1035   void movdqa(const Operand& dst, XMMRegister src);
1036   void movdqu(XMMRegister dst, const Operand& src);
1037   void movdqu(const Operand& dst, XMMRegister src);
1038   void movdq(bool aligned, XMMRegister dst, const Operand& src) {
1039     if (aligned) {
1040       movdqa(dst, src);
1041     } else {
1042       movdqu(dst, src);
1043     }
1044   }
1045
1046   void movd(XMMRegister dst, Register src) { movd(dst, Operand(src)); }
1047   void movd(XMMRegister dst, const Operand& src);
1048   void movd(Register dst, XMMRegister src) { movd(Operand(dst), src); }
1049   void movd(const Operand& dst, XMMRegister src);
1050   void movsd(XMMRegister dst, XMMRegister src) { movsd(dst, Operand(src)); }
1051   void movsd(XMMRegister dst, const Operand& src);
1052   void movsd(const Operand& dst, XMMRegister src);
1053
1054
1055   void movss(XMMRegister dst, const Operand& src);
1056   void movss(const Operand& dst, XMMRegister src);
1057   void movss(XMMRegister dst, XMMRegister src) { movss(dst, Operand(src)); }
1058   void extractps(Register dst, XMMRegister src, byte imm8);
1059
1060   void pand(XMMRegister dst, XMMRegister src);
1061   void pxor(XMMRegister dst, XMMRegister src);
1062   void por(XMMRegister dst, XMMRegister src);
1063   void ptest(XMMRegister dst, XMMRegister src);
1064
1065   void pslld(XMMRegister reg, int8_t shift);
1066   void psrld(XMMRegister reg, int8_t shift);
1067   void psllq(XMMRegister reg, int8_t shift);
1068   void psllq(XMMRegister dst, XMMRegister src);
1069   void psrlq(XMMRegister reg, int8_t shift);
1070   void psrlq(XMMRegister dst, XMMRegister src);
1071   void pshufd(XMMRegister dst, XMMRegister src, uint8_t shuffle);
1072   void pextrd(Register dst, XMMRegister src, int8_t offset) {
1073     pextrd(Operand(dst), src, offset);
1074   }
1075   void pextrd(const Operand& dst, XMMRegister src, int8_t offset);
1076   void pinsrd(XMMRegister dst, Register src, int8_t offset) {
1077     pinsrd(dst, Operand(src), offset);
1078   }
1079   void pinsrd(XMMRegister dst, const Operand& src, int8_t offset);
1080
1081   // AVX instructions
1082   void vfmadd132sd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1083     vfmadd132sd(dst, src1, Operand(src2));
1084   }
1085   void vfmadd213sd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1086     vfmadd213sd(dst, src1, Operand(src2));
1087   }
1088   void vfmadd231sd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1089     vfmadd231sd(dst, src1, Operand(src2));
1090   }
1091   void vfmadd132sd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1092     vfmasd(0x99, dst, src1, src2);
1093   }
1094   void vfmadd213sd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1095     vfmasd(0xa9, dst, src1, src2);
1096   }
1097   void vfmadd231sd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1098     vfmasd(0xb9, dst, src1, src2);
1099   }
1100   void vfmsub132sd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1101     vfmsub132sd(dst, src1, Operand(src2));
1102   }
1103   void vfmsub213sd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1104     vfmsub213sd(dst, src1, Operand(src2));
1105   }
1106   void vfmsub231sd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1107     vfmsub231sd(dst, src1, Operand(src2));
1108   }
1109   void vfmsub132sd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1110     vfmasd(0x9b, dst, src1, src2);
1111   }
1112   void vfmsub213sd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1113     vfmasd(0xab, dst, src1, src2);
1114   }
1115   void vfmsub231sd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1116     vfmasd(0xbb, dst, src1, src2);
1117   }
1118   void vfnmadd132sd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1119     vfnmadd132sd(dst, src1, Operand(src2));
1120   }
1121   void vfnmadd213sd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1122     vfnmadd213sd(dst, src1, Operand(src2));
1123   }
1124   void vfnmadd231sd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1125     vfnmadd231sd(dst, src1, Operand(src2));
1126   }
1127   void vfnmadd132sd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1128     vfmasd(0x9d, dst, src1, src2);
1129   }
1130   void vfnmadd213sd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1131     vfmasd(0xad, dst, src1, src2);
1132   }
1133   void vfnmadd231sd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1134     vfmasd(0xbd, dst, src1, src2);
1135   }
1136   void vfnmsub132sd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1137     vfnmsub132sd(dst, src1, Operand(src2));
1138   }
1139   void vfnmsub213sd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1140     vfnmsub213sd(dst, src1, Operand(src2));
1141   }
1142   void vfnmsub231sd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1143     vfnmsub231sd(dst, src1, Operand(src2));
1144   }
1145   void vfnmsub132sd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1146     vfmasd(0x9f, dst, src1, src2);
1147   }
1148   void vfnmsub213sd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1149     vfmasd(0xaf, dst, src1, src2);
1150   }
1151   void vfnmsub231sd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1152     vfmasd(0xbf, dst, src1, src2);
1153   }
1154   void vfmasd(byte op, XMMRegister dst, XMMRegister src1, const Operand& src2);
1155
1156   void vfmadd132ss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1157     vfmadd132ss(dst, src1, Operand(src2));
1158   }
1159   void vfmadd213ss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1160     vfmadd213ss(dst, src1, Operand(src2));
1161   }
1162   void vfmadd231ss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1163     vfmadd231ss(dst, src1, Operand(src2));
1164   }
1165   void vfmadd132ss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1166     vfmass(0x99, dst, src1, src2);
1167   }
1168   void vfmadd213ss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1169     vfmass(0xa9, dst, src1, src2);
1170   }
1171   void vfmadd231ss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1172     vfmass(0xb9, dst, src1, src2);
1173   }
1174   void vfmsub132ss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1175     vfmsub132ss(dst, src1, Operand(src2));
1176   }
1177   void vfmsub213ss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1178     vfmsub213ss(dst, src1, Operand(src2));
1179   }
1180   void vfmsub231ss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1181     vfmsub231ss(dst, src1, Operand(src2));
1182   }
1183   void vfmsub132ss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1184     vfmass(0x9b, dst, src1, src2);
1185   }
1186   void vfmsub213ss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1187     vfmass(0xab, dst, src1, src2);
1188   }
1189   void vfmsub231ss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1190     vfmass(0xbb, dst, src1, src2);
1191   }
1192   void vfnmadd132ss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1193     vfnmadd132ss(dst, src1, Operand(src2));
1194   }
1195   void vfnmadd213ss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1196     vfnmadd213ss(dst, src1, Operand(src2));
1197   }
1198   void vfnmadd231ss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1199     vfnmadd231ss(dst, src1, Operand(src2));
1200   }
1201   void vfnmadd132ss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1202     vfmass(0x9d, dst, src1, src2);
1203   }
1204   void vfnmadd213ss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1205     vfmass(0xad, dst, src1, src2);
1206   }
1207   void vfnmadd231ss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1208     vfmass(0xbd, dst, src1, src2);
1209   }
1210   void vfnmsub132ss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1211     vfnmsub132ss(dst, src1, Operand(src2));
1212   }
1213   void vfnmsub213ss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1214     vfnmsub213ss(dst, src1, Operand(src2));
1215   }
1216   void vfnmsub231ss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1217     vfnmsub231ss(dst, src1, Operand(src2));
1218   }
1219   void vfnmsub132ss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1220     vfmass(0x9f, dst, src1, src2);
1221   }
1222   void vfnmsub213ss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1223     vfmass(0xaf, dst, src1, src2);
1224   }
1225   void vfnmsub231ss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1226     vfmass(0xbf, dst, src1, src2);
1227   }
1228   void vfmass(byte op, XMMRegister dst, XMMRegister src1, const Operand& src2);
1229
1230   void vaddsd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1231     vaddsd(dst, src1, Operand(src2));
1232   }
1233   void vaddsd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1234     vsd(0x58, dst, src1, src2);
1235   }
1236   void vsubsd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1237     vsubsd(dst, src1, Operand(src2));
1238   }
1239   void vsubsd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1240     vsd(0x5c, dst, src1, src2);
1241   }
1242   void vmulsd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1243     vmulsd(dst, src1, Operand(src2));
1244   }
1245   void vmulsd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1246     vsd(0x59, dst, src1, src2);
1247   }
1248   void vdivsd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1249     vdivsd(dst, src1, Operand(src2));
1250   }
1251   void vdivsd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1252     vsd(0x5e, dst, src1, src2);
1253   }
1254   void vmaxsd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1255     vmaxsd(dst, src1, Operand(src2));
1256   }
1257   void vmaxsd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1258     vsd(0x5f, dst, src1, src2);
1259   }
1260   void vminsd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1261     vminsd(dst, src1, Operand(src2));
1262   }
1263   void vminsd(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1264     vsd(0x5d, dst, src1, src2);
1265   }
1266   void vsd(byte op, XMMRegister dst, XMMRegister src1, const Operand& src2);
1267
1268   void vaddss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1269     vaddss(dst, src1, Operand(src2));
1270   }
1271   void vaddss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1272     vss(0x58, dst, src1, src2);
1273   }
1274   void vsubss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1275     vsubss(dst, src1, Operand(src2));
1276   }
1277   void vsubss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1278     vss(0x5c, dst, src1, src2);
1279   }
1280   void vmulss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1281     vmulss(dst, src1, Operand(src2));
1282   }
1283   void vmulss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1284     vss(0x59, dst, src1, src2);
1285   }
1286   void vdivss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1287     vdivss(dst, src1, Operand(src2));
1288   }
1289   void vdivss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1290     vss(0x5e, dst, src1, src2);
1291   }
1292   void vmaxss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1293     vmaxss(dst, src1, Operand(src2));
1294   }
1295   void vmaxss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1296     vss(0x5f, dst, src1, src2);
1297   }
1298   void vminss(XMMRegister dst, XMMRegister src1, XMMRegister src2) {
1299     vminss(dst, src1, Operand(src2));
1300   }
1301   void vminss(XMMRegister dst, XMMRegister src1, const Operand& src2) {
1302     vss(0x5d, dst, src1, src2);
1303   }
1304   void vss(byte op, XMMRegister dst, XMMRegister src1, const Operand& src2);
1305
1306   // BMI instruction
1307   void andn(Register dst, Register src1, Register src2) {
1308     andn(dst, src1, Operand(src2));
1309   }
1310   void andn(Register dst, Register src1, const Operand& src2) {
1311     bmi1(0xf2, dst, src1, src2);
1312   }
1313   void bextr(Register dst, Register src1, Register src2) {
1314     bextr(dst, Operand(src1), src2);
1315   }
1316   void bextr(Register dst, const Operand& src1, Register src2) {
1317     bmi1(0xf7, dst, src2, src1);
1318   }
1319   void blsi(Register dst, Register src) { blsi(dst, Operand(src)); }
1320   void blsi(Register dst, const Operand& src) {
1321     Register ireg = {3};
1322     bmi1(0xf3, ireg, dst, src);
1323   }
1324   void blsmsk(Register dst, Register src) { blsmsk(dst, Operand(src)); }
1325   void blsmsk(Register dst, const Operand& src) {
1326     Register ireg = {2};
1327     bmi1(0xf3, ireg, dst, src);
1328   }
1329   void blsr(Register dst, Register src) { blsr(dst, Operand(src)); }
1330   void blsr(Register dst, const Operand& src) {
1331     Register ireg = {1};
1332     bmi1(0xf3, ireg, dst, src);
1333   }
1334   void tzcnt(Register dst, Register src) { tzcnt(dst, Operand(src)); }
1335   void tzcnt(Register dst, const Operand& src);
1336
1337   void lzcnt(Register dst, Register src) { lzcnt(dst, Operand(src)); }
1338   void lzcnt(Register dst, const Operand& src);
1339
1340   void popcnt(Register dst, Register src) { popcnt(dst, Operand(src)); }
1341   void popcnt(Register dst, const Operand& src);
1342
1343   void bzhi(Register dst, Register src1, Register src2) {
1344     bzhi(dst, Operand(src1), src2);
1345   }
1346   void bzhi(Register dst, const Operand& src1, Register src2) {
1347     bmi2(kNone, 0xf5, dst, src2, src1);
1348   }
1349   void mulx(Register dst1, Register dst2, Register src) {
1350     mulx(dst1, dst2, Operand(src));
1351   }
1352   void mulx(Register dst1, Register dst2, const Operand& src) {
1353     bmi2(kF2, 0xf6, dst1, dst2, src);
1354   }
1355   void pdep(Register dst, Register src1, Register src2) {
1356     pdep(dst, src1, Operand(src2));
1357   }
1358   void pdep(Register dst, Register src1, const Operand& src2) {
1359     bmi2(kF2, 0xf5, dst, src1, src2);
1360   }
1361   void pext(Register dst, Register src1, Register src2) {
1362     pext(dst, src1, Operand(src2));
1363   }
1364   void pext(Register dst, Register src1, const Operand& src2) {
1365     bmi2(kF3, 0xf5, dst, src1, src2);
1366   }
1367   void sarx(Register dst, Register src1, Register src2) {
1368     sarx(dst, Operand(src1), src2);
1369   }
1370   void sarx(Register dst, const Operand& src1, Register src2) {
1371     bmi2(kF3, 0xf7, dst, src2, src1);
1372   }
1373   void shlx(Register dst, Register src1, Register src2) {
1374     shlx(dst, Operand(src1), src2);
1375   }
1376   void shlx(Register dst, const Operand& src1, Register src2) {
1377     bmi2(k66, 0xf7, dst, src2, src1);
1378   }
1379   void shrx(Register dst, Register src1, Register src2) {
1380     shrx(dst, Operand(src1), src2);
1381   }
1382   void shrx(Register dst, const Operand& src1, Register src2) {
1383     bmi2(kF2, 0xf7, dst, src2, src1);
1384   }
1385   void rorx(Register dst, Register src, byte imm8) {
1386     rorx(dst, Operand(src), imm8);
1387   }
1388   void rorx(Register dst, const Operand& src, byte imm8);
1389
1390 #define PACKED_OP_LIST(V) \
1391   V(and, 0x54)            \
1392   V(xor, 0x57)
1393
1394 #define AVX_PACKED_OP_DECLARE(name, opcode)                                  \
1395   void v##name##ps(XMMRegister dst, XMMRegister src1, XMMRegister src2) {    \
1396     vps(opcode, dst, src1, Operand(src2));                                   \
1397   }                                                                          \
1398   void v##name##ps(XMMRegister dst, XMMRegister src1, const Operand& src2) { \
1399     vps(opcode, dst, src1, src2);                                            \
1400   }                                                                          \
1401   void v##name##pd(XMMRegister dst, XMMRegister src1, XMMRegister src2) {    \
1402     vpd(opcode, dst, src1, Operand(src2));                                   \
1403   }                                                                          \
1404   void v##name##pd(XMMRegister dst, XMMRegister src1, const Operand& src2) { \
1405     vpd(opcode, dst, src1, src2);                                            \
1406   }
1407
1408   PACKED_OP_LIST(AVX_PACKED_OP_DECLARE);
1409   void vps(byte op, XMMRegister dst, XMMRegister src1, XMMRegister src2);
1410   void vps(byte op, XMMRegister dst, XMMRegister src1, const Operand& src2);
1411   void vpd(byte op, XMMRegister dst, XMMRegister src1, XMMRegister src2);
1412   void vpd(byte op, XMMRegister dst, XMMRegister src1, const Operand& src2);
1413
1414   // Prefetch src position into cache level.
1415   // Level 1, 2 or 3 specifies CPU cache level. Level 0 specifies a
1416   // non-temporal
1417   void prefetch(const Operand& src, int level);
1418   // TODO(lrn): Need SFENCE for movnt?
1419
1420   // Check the code size generated from label to here.
1421   int SizeOfCodeGeneratedSince(Label* label) {
1422     return pc_offset() - label->pos();
1423   }
1424
1425   // Mark generator continuation.
1426   void RecordGeneratorContinuation();
1427
1428   // Mark address of a debug break slot.
1429   void RecordDebugBreakSlot(RelocInfo::Mode mode, int argc = 0);
1430
1431   // Record a comment relocation entry that can be used by a disassembler.
1432   // Use --code-comments to enable.
1433   void RecordComment(const char* msg);
1434
1435   // Record a deoptimization reason that can be used by a log or cpu profiler.
1436   // Use --trace-deopt to enable.
1437   void RecordDeoptReason(const int reason, const SourcePosition position);
1438
1439   // Writes a single byte or word of data in the code stream.  Used for
1440   // inline tables, e.g., jump-tables.
1441   void db(uint8_t data);
1442   void dd(uint32_t data);
1443   void dq(uint64_t data);
1444   void dp(uintptr_t data) { dd(data); }
1445   void dd(Label* label);
1446
1447   // Check if there is less than kGap bytes available in the buffer.
1448   // If this is the case, we need to grow the buffer before emitting
1449   // an instruction or relocation information.
1450   inline bool buffer_overflow() const {
1451     return pc_ >= reloc_info_writer.pos() - kGap;
1452   }
1453
1454   // Get the number of bytes available in the buffer.
1455   inline int available_space() const { return reloc_info_writer.pos() - pc_; }
1456
1457   static bool IsNop(Address addr);
1458
1459   PositionsRecorder* positions_recorder() { return &positions_recorder_; }
1460
1461   int relocation_writer_size() {
1462     return (buffer_ + buffer_size_) - reloc_info_writer.pos();
1463   }
1464
1465   // Avoid overflows for displacements etc.
1466   static const int kMaximalBufferSize = 512*MB;
1467
1468   byte byte_at(int pos) { return buffer_[pos]; }
1469   void set_byte_at(int pos, byte value) { buffer_[pos] = value; }
1470
1471   void PatchConstantPoolAccessInstruction(int pc_offset, int offset,
1472                                           ConstantPoolEntry::Access access,
1473                                           ConstantPoolEntry::Type type) {
1474     // No embedded constant pool support.
1475     UNREACHABLE();
1476   }
1477
1478  protected:
1479   void emit_sse_operand(XMMRegister reg, const Operand& adr);
1480   void emit_sse_operand(XMMRegister dst, XMMRegister src);
1481   void emit_sse_operand(Register dst, XMMRegister src);
1482   void emit_sse_operand(XMMRegister dst, Register src);
1483
1484   byte* addr_at(int pos) { return buffer_ + pos; }
1485
1486
1487  private:
1488   uint32_t long_at(int pos)  {
1489     return *reinterpret_cast<uint32_t*>(addr_at(pos));
1490   }
1491   void long_at_put(int pos, uint32_t x)  {
1492     *reinterpret_cast<uint32_t*>(addr_at(pos)) = x;
1493   }
1494
1495   // code emission
1496   void GrowBuffer();
1497   inline void emit(uint32_t x);
1498   inline void emit(Handle<Object> handle);
1499   inline void emit(uint32_t x,
1500                    RelocInfo::Mode rmode,
1501                    TypeFeedbackId id = TypeFeedbackId::None());
1502   inline void emit(Handle<Code> code,
1503                    RelocInfo::Mode rmode,
1504                    TypeFeedbackId id = TypeFeedbackId::None());
1505   inline void emit(const Immediate& x);
1506   inline void emit_w(const Immediate& x);
1507   inline void emit_q(uint64_t x);
1508
1509   // Emit the code-object-relative offset of the label's position
1510   inline void emit_code_relative_offset(Label* label);
1511
1512   // instruction generation
1513   void emit_arith_b(int op1, int op2, Register dst, int imm8);
1514
1515   // Emit a basic arithmetic instruction (i.e. first byte of the family is 0x81)
1516   // with a given destination expression and an immediate operand.  It attempts
1517   // to use the shortest encoding possible.
1518   // sel specifies the /n in the modrm byte (see the Intel PRM).
1519   void emit_arith(int sel, Operand dst, const Immediate& x);
1520
1521   void emit_operand(Register reg, const Operand& adr);
1522
1523   void emit_label(Label* label);
1524
1525   void emit_farith(int b1, int b2, int i);
1526
1527   // Emit vex prefix
1528   enum SIMDPrefix { kNone = 0x0, k66 = 0x1, kF3 = 0x2, kF2 = 0x3 };
1529   enum VectorLength { kL128 = 0x0, kL256 = 0x4, kLIG = kL128, kLZ = kL128 };
1530   enum VexW { kW0 = 0x0, kW1 = 0x80, kWIG = kW0 };
1531   enum LeadingOpcode { k0F = 0x1, k0F38 = 0x2, k0F3A = 0x3 };
1532   inline void emit_vex_prefix(XMMRegister v, VectorLength l, SIMDPrefix pp,
1533                               LeadingOpcode m, VexW w);
1534   inline void emit_vex_prefix(Register v, VectorLength l, SIMDPrefix pp,
1535                               LeadingOpcode m, VexW w);
1536
1537   // labels
1538   void print(Label* L);
1539   void bind_to(Label* L, int pos);
1540
1541   // displacements
1542   inline Displacement disp_at(Label* L);
1543   inline void disp_at_put(Label* L, Displacement disp);
1544   inline void emit_disp(Label* L, Displacement::Type type);
1545   inline void emit_near_disp(Label* L);
1546
1547   // Most BMI instructions are similiar.
1548   void bmi1(byte op, Register reg, Register vreg, const Operand& rm);
1549   void bmi2(SIMDPrefix pp, byte op, Register reg, Register vreg,
1550             const Operand& rm);
1551
1552   // record reloc info for current pc_
1553   void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
1554
1555   friend class CodePatcher;
1556   friend class EnsureSpace;
1557
1558   // Internal reference positions, required for (potential) patching in
1559   // GrowBuffer(); contains only those internal references whose labels
1560   // are already bound.
1561   std::deque<int> internal_reference_positions_;
1562
1563   // code generation
1564   RelocInfoWriter reloc_info_writer;
1565
1566   PositionsRecorder positions_recorder_;
1567   friend class PositionsRecorder;
1568 };
1569
1570
1571 // Helper class that ensures that there is enough space for generating
1572 // instructions and relocation information.  The constructor makes
1573 // sure that there is enough space and (in debug mode) the destructor
1574 // checks that we did not generate too much.
1575 class EnsureSpace BASE_EMBEDDED {
1576  public:
1577   explicit EnsureSpace(Assembler* assembler) : assembler_(assembler) {
1578     if (assembler_->buffer_overflow()) assembler_->GrowBuffer();
1579 #ifdef DEBUG
1580     space_before_ = assembler_->available_space();
1581 #endif
1582   }
1583
1584 #ifdef DEBUG
1585   ~EnsureSpace() {
1586     int bytes_generated = space_before_ - assembler_->available_space();
1587     DCHECK(bytes_generated < assembler_->kGap);
1588   }
1589 #endif
1590
1591  private:
1592   Assembler* assembler_;
1593 #ifdef DEBUG
1594   int space_before_;
1595 #endif
1596 };
1597
1598 } }  // namespace v8::internal
1599
1600 #endif  // V8_IA32_ASSEMBLER_IA32_H_