Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / v8 / src / x64 / assembler-x64.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 2012 the V8 project authors. All rights reserved.
34
35 // A lightweight X64 Assembler.
36
37 #ifndef V8_X64_ASSEMBLER_X64_H_
38 #define V8_X64_ASSEMBLER_X64_H_
39
40 #include "src/serialize.h"
41
42 namespace v8 {
43 namespace internal {
44
45 // Utility functions
46
47 // CPU Registers.
48 //
49 // 1) We would prefer to use an enum, but enum values are assignment-
50 // compatible with int, which has caused code-generation bugs.
51 //
52 // 2) We would prefer to use a class instead of a struct but we don't like
53 // the register initialization to depend on the particular initialization
54 // order (which appears to be different on OS X, Linux, and Windows for the
55 // installed versions of C++ we tried). Using a struct permits C-style
56 // "initialization". Also, the Register objects cannot be const as this
57 // forces initialization stubs in MSVC, making us dependent on initialization
58 // order.
59 //
60 // 3) By not using an enum, we are possibly preventing the compiler from
61 // doing certain constant folds, which may significantly reduce the
62 // code generated for some assembly instructions (because they boil down
63 // to a few constants). If this is a problem, we could change the code
64 // such that we use an enum in optimized mode, and the struct in debug
65 // mode. This way we get the compile-time error checking in debug mode
66 // and best performance in optimized code.
67 //
68
69 struct Register {
70   // The non-allocatable registers are:
71   //  rsp - stack pointer
72   //  rbp - frame pointer
73   //  r10 - fixed scratch register
74   //  r12 - smi constant register
75   //  r13 - root register
76   static const int kMaxNumAllocatableRegisters = 11;
77   static int NumAllocatableRegisters() {
78     return kMaxNumAllocatableRegisters;
79   }
80   static const int kNumRegisters = 16;
81
82   static int ToAllocationIndex(Register reg) {
83     return kAllocationIndexByRegisterCode[reg.code()];
84   }
85
86   static Register FromAllocationIndex(int index) {
87     DCHECK(index >= 0 && index < kMaxNumAllocatableRegisters);
88     Register result = { kRegisterCodeByAllocationIndex[index] };
89     return result;
90   }
91
92   static const char* AllocationIndexToString(int index) {
93     DCHECK(index >= 0 && index < kMaxNumAllocatableRegisters);
94     const char* const names[] = {
95       "rax",
96       "rbx",
97       "rdx",
98       "rcx",
99       "rsi",
100       "rdi",
101       "r8",
102       "r9",
103       "r11",
104       "r14",
105       "r15"
106     };
107     return names[index];
108   }
109
110   static Register from_code(int code) {
111     Register r = { code };
112     return r;
113   }
114   bool is_valid() const { return 0 <= code_ && code_ < kNumRegisters; }
115   bool is(Register reg) const { return code_ == reg.code_; }
116   // rax, rbx, rcx and rdx are byte registers, the rest are not.
117   bool is_byte_register() const { return code_ <= 3; }
118   int code() const {
119     DCHECK(is_valid());
120     return code_;
121   }
122   int bit() const {
123     return 1 << code_;
124   }
125
126   // Return the high bit of the register code as a 0 or 1.  Used often
127   // when constructing the REX prefix byte.
128   int high_bit() const {
129     return code_ >> 3;
130   }
131   // Return the 3 low bits of the register code.  Used when encoding registers
132   // in modR/M, SIB, and opcode bytes.
133   int low_bits() const {
134     return code_ & 0x7;
135   }
136
137   // Unfortunately we can't make this private in a struct when initializing
138   // by assignment.
139   int code_;
140
141  private:
142   static const int kRegisterCodeByAllocationIndex[kMaxNumAllocatableRegisters];
143   static const int kAllocationIndexByRegisterCode[kNumRegisters];
144 };
145
146 const int kRegister_rax_Code = 0;
147 const int kRegister_rcx_Code = 1;
148 const int kRegister_rdx_Code = 2;
149 const int kRegister_rbx_Code = 3;
150 const int kRegister_rsp_Code = 4;
151 const int kRegister_rbp_Code = 5;
152 const int kRegister_rsi_Code = 6;
153 const int kRegister_rdi_Code = 7;
154 const int kRegister_r8_Code = 8;
155 const int kRegister_r9_Code = 9;
156 const int kRegister_r10_Code = 10;
157 const int kRegister_r11_Code = 11;
158 const int kRegister_r12_Code = 12;
159 const int kRegister_r13_Code = 13;
160 const int kRegister_r14_Code = 14;
161 const int kRegister_r15_Code = 15;
162 const int kRegister_no_reg_Code = -1;
163
164 const Register rax = { kRegister_rax_Code };
165 const Register rcx = { kRegister_rcx_Code };
166 const Register rdx = { kRegister_rdx_Code };
167 const Register rbx = { kRegister_rbx_Code };
168 const Register rsp = { kRegister_rsp_Code };
169 const Register rbp = { kRegister_rbp_Code };
170 const Register rsi = { kRegister_rsi_Code };
171 const Register rdi = { kRegister_rdi_Code };
172 const Register r8 = { kRegister_r8_Code };
173 const Register r9 = { kRegister_r9_Code };
174 const Register r10 = { kRegister_r10_Code };
175 const Register r11 = { kRegister_r11_Code };
176 const Register r12 = { kRegister_r12_Code };
177 const Register r13 = { kRegister_r13_Code };
178 const Register r14 = { kRegister_r14_Code };
179 const Register r15 = { kRegister_r15_Code };
180 const Register no_reg = { kRegister_no_reg_Code };
181
182 #ifdef _WIN64
183   // Windows calling convention
184   const Register arg_reg_1 = { kRegister_rcx_Code };
185   const Register arg_reg_2 = { kRegister_rdx_Code };
186   const Register arg_reg_3 = { kRegister_r8_Code };
187   const Register arg_reg_4 = { kRegister_r9_Code };
188 #else
189   // AMD64 calling convention
190   const Register arg_reg_1 = { kRegister_rdi_Code };
191   const Register arg_reg_2 = { kRegister_rsi_Code };
192   const Register arg_reg_3 = { kRegister_rdx_Code };
193   const Register arg_reg_4 = { kRegister_rcx_Code };
194 #endif  // _WIN64
195
196 struct XMMRegister {
197   static const int kMaxNumRegisters = 16;
198   static const int kMaxNumAllocatableRegisters = 15;
199   static int NumAllocatableRegisters() {
200     return kMaxNumAllocatableRegisters;
201   }
202
203   // TODO(turbofan): Proper support for float32.
204   static int NumAllocatableAliasedRegisters() {
205     return NumAllocatableRegisters();
206   }
207
208   static int ToAllocationIndex(XMMRegister reg) {
209     DCHECK(reg.code() != 0);
210     return reg.code() - 1;
211   }
212
213   static XMMRegister FromAllocationIndex(int index) {
214     DCHECK(0 <= index && index < kMaxNumAllocatableRegisters);
215     XMMRegister result = { index + 1 };
216     return result;
217   }
218
219   static const char* AllocationIndexToString(int index) {
220     DCHECK(index >= 0 && index < kMaxNumAllocatableRegisters);
221     const char* const names[] = {
222       "xmm1",
223       "xmm2",
224       "xmm3",
225       "xmm4",
226       "xmm5",
227       "xmm6",
228       "xmm7",
229       "xmm8",
230       "xmm9",
231       "xmm10",
232       "xmm11",
233       "xmm12",
234       "xmm13",
235       "xmm14",
236       "xmm15"
237     };
238     return names[index];
239   }
240
241   static XMMRegister from_code(int code) {
242     DCHECK(code >= 0);
243     DCHECK(code < kMaxNumRegisters);
244     XMMRegister r = { code };
245     return r;
246   }
247   bool is_valid() const { return 0 <= code_ && code_ < kMaxNumRegisters; }
248   bool is(XMMRegister reg) const { return code_ == reg.code_; }
249   int code() const {
250     DCHECK(is_valid());
251     return code_;
252   }
253
254   // Return the high bit of the register code as a 0 or 1.  Used often
255   // when constructing the REX prefix byte.
256   int high_bit() const {
257     return code_ >> 3;
258   }
259   // Return the 3 low bits of the register code.  Used when encoding registers
260   // in modR/M, SIB, and opcode bytes.
261   int low_bits() const {
262     return code_ & 0x7;
263   }
264
265   int code_;
266 };
267
268 const XMMRegister xmm0 = { 0 };
269 const XMMRegister xmm1 = { 1 };
270 const XMMRegister xmm2 = { 2 };
271 const XMMRegister xmm3 = { 3 };
272 const XMMRegister xmm4 = { 4 };
273 const XMMRegister xmm5 = { 5 };
274 const XMMRegister xmm6 = { 6 };
275 const XMMRegister xmm7 = { 7 };
276 const XMMRegister xmm8 = { 8 };
277 const XMMRegister xmm9 = { 9 };
278 const XMMRegister xmm10 = { 10 };
279 const XMMRegister xmm11 = { 11 };
280 const XMMRegister xmm12 = { 12 };
281 const XMMRegister xmm13 = { 13 };
282 const XMMRegister xmm14 = { 14 };
283 const XMMRegister xmm15 = { 15 };
284
285
286 typedef XMMRegister DoubleRegister;
287 typedef XMMRegister SIMD128Register;
288
289
290 enum Condition {
291   // any value < 0 is considered no_condition
292   no_condition  = -1,
293
294   overflow      =  0,
295   no_overflow   =  1,
296   below         =  2,
297   above_equal   =  3,
298   equal         =  4,
299   not_equal     =  5,
300   below_equal   =  6,
301   above         =  7,
302   negative      =  8,
303   positive      =  9,
304   parity_even   = 10,
305   parity_odd    = 11,
306   less          = 12,
307   greater_equal = 13,
308   less_equal    = 14,
309   greater       = 15,
310
311   // Fake conditions that are handled by the
312   // opcodes using them.
313   always        = 16,
314   never         = 17,
315   // aliases
316   carry         = below,
317   not_carry     = above_equal,
318   zero          = equal,
319   not_zero      = not_equal,
320   sign          = negative,
321   not_sign      = positive,
322   last_condition = greater
323 };
324
325
326 // Returns the equivalent of !cc.
327 // Negation of the default no_condition (-1) results in a non-default
328 // no_condition value (-2). As long as tests for no_condition check
329 // for condition < 0, this will work as expected.
330 inline Condition NegateCondition(Condition cc) {
331   return static_cast<Condition>(cc ^ 1);
332 }
333
334
335 // Commute a condition such that {a cond b == b cond' a}.
336 inline Condition CommuteCondition(Condition cc) {
337   switch (cc) {
338     case below:
339       return above;
340     case above:
341       return below;
342     case above_equal:
343       return below_equal;
344     case below_equal:
345       return above_equal;
346     case less:
347       return greater;
348     case greater:
349       return less;
350     case greater_equal:
351       return less_equal;
352     case less_equal:
353       return greater_equal;
354     default:
355       return cc;
356   }
357 }
358
359
360 // -----------------------------------------------------------------------------
361 // Machine instruction Immediates
362
363 class Immediate BASE_EMBEDDED {
364  public:
365   explicit Immediate(int32_t value) : value_(value) {}
366   explicit Immediate(Smi* value) {
367     DCHECK(SmiValuesAre31Bits());  // Only available for 31-bit SMI.
368     value_ = static_cast<int32_t>(reinterpret_cast<intptr_t>(value));
369   }
370
371  private:
372   int32_t value_;
373
374   friend class Assembler;
375 };
376
377
378 // -----------------------------------------------------------------------------
379 // Machine instruction Operands
380
381 enum ScaleFactor {
382   times_1 = 0,
383   times_2 = 1,
384   times_4 = 2,
385   times_8 = 3,
386   maximal_scale_factor = times_8,
387   times_int_size = times_4,
388   times_pointer_size = (kPointerSize == 8) ? times_8 : times_4
389 };
390
391
392 class Operand BASE_EMBEDDED {
393  public:
394   // [base + disp/r]
395   Operand(Register base, int32_t disp);
396
397   // [base + index*scale + disp/r]
398   Operand(Register base,
399           Register index,
400           ScaleFactor scale,
401           int32_t disp);
402
403   // [index*scale + disp/r]
404   Operand(Register index,
405           ScaleFactor scale,
406           int32_t disp);
407
408   // Offset from existing memory operand.
409   // Offset is added to existing displacement as 32-bit signed values and
410   // this must not overflow.
411   Operand(const Operand& base, int32_t offset);
412
413   // Checks whether either base or index register is the given register.
414   // Does not check the "reg" part of the Operand.
415   bool AddressUsesRegister(Register reg) const;
416
417   // Queries related to the size of the generated instruction.
418   // Whether the generated instruction will have a REX prefix.
419   bool requires_rex() const { return rex_ != 0; }
420   // Size of the ModR/M, SIB and displacement parts of the generated
421   // instruction.
422   int operand_size() const { return len_; }
423
424  private:
425   byte rex_;
426   byte buf_[6];
427   // The number of bytes of buf_ in use.
428   byte len_;
429
430   // Set the ModR/M byte without an encoded 'reg' register. The
431   // register is encoded later as part of the emit_operand operation.
432   // set_modrm can be called before or after set_sib and set_disp*.
433   inline void set_modrm(int mod, Register rm);
434
435   // Set the SIB byte if one is needed. Sets the length to 2 rather than 1.
436   inline void set_sib(ScaleFactor scale, Register index, Register base);
437
438   // Adds operand displacement fields (offsets added to the memory address).
439   // Needs to be called after set_sib, not before it.
440   inline void set_disp8(int disp);
441   inline void set_disp32(int disp);
442
443   friend class Assembler;
444 };
445
446
447 #define ASSEMBLER_INSTRUCTION_LIST(V) \
448   V(add)                              \
449   V(and)                              \
450   V(cmp)                              \
451   V(dec)                              \
452   V(idiv)                             \
453   V(div)                              \
454   V(imul)                             \
455   V(inc)                              \
456   V(lea)                              \
457   V(mov)                              \
458   V(movzxb)                           \
459   V(movzxw)                           \
460   V(neg)                              \
461   V(not)                              \
462   V(or)                               \
463   V(repmovs)                          \
464   V(sbb)                              \
465   V(sub)                              \
466   V(test)                             \
467   V(xchg)                             \
468   V(xor)
469
470
471 // Shift instructions on operands/registers with kPointerSize, kInt32Size and
472 // kInt64Size.
473 #define SHIFT_INSTRUCTION_LIST(V)       \
474   V(rol, 0x0)                           \
475   V(ror, 0x1)                           \
476   V(rcl, 0x2)                           \
477   V(rcr, 0x3)                           \
478   V(shl, 0x4)                           \
479   V(shr, 0x5)                           \
480   V(sar, 0x7)                           \
481
482
483 class Assembler : public AssemblerBase {
484  private:
485   // We check before assembling an instruction that there is sufficient
486   // space to write an instruction and its relocation information.
487   // The relocation writer's position must be kGap bytes above the end of
488   // the generated instructions. This leaves enough space for the
489   // longest possible x64 instruction, 15 bytes, and the longest possible
490   // relocation information encoding, RelocInfoWriter::kMaxLength == 16.
491   // (There is a 15 byte limit on x64 instruction length that rules out some
492   // otherwise valid instructions.)
493   // This allows for a single, fast space check per instruction.
494   static const int kGap = 32;
495
496  public:
497   // Create an assembler. Instructions and relocation information are emitted
498   // into a buffer, with the instructions starting from the beginning and the
499   // relocation information starting from the end of the buffer. See CodeDesc
500   // for a detailed comment on the layout (globals.h).
501   //
502   // If the provided buffer is NULL, the assembler allocates and grows its own
503   // buffer, and buffer_size determines the initial buffer size. The buffer is
504   // owned by the assembler and deallocated upon destruction of the assembler.
505   //
506   // If the provided buffer is not NULL, the assembler uses the provided buffer
507   // for code generation and assumes its size to be buffer_size. If the buffer
508   // is too small, a fatal error occurs. No deallocation of the buffer is done
509   // upon destruction of the assembler.
510   Assembler(Isolate* isolate, void* buffer, int buffer_size);
511   virtual ~Assembler() { }
512
513   // GetCode emits any pending (non-emitted) code and fills the descriptor
514   // desc. GetCode() is idempotent; it returns the same result if no other
515   // Assembler functions are invoked in between GetCode() calls.
516   void GetCode(CodeDesc* desc);
517
518   // Read/Modify the code target in the relative branch/call instruction at pc.
519   // On the x64 architecture, we use relative jumps with a 32-bit displacement
520   // to jump to other Code objects in the Code space in the heap.
521   // Jumps to C functions are done indirectly through a 64-bit register holding
522   // the absolute address of the target.
523   // These functions convert between absolute Addresses of Code objects and
524   // the relative displacements stored in the code.
525   static inline Address target_address_at(Address pc,
526                                           ConstantPoolArray* constant_pool);
527   static inline void set_target_address_at(Address pc,
528                                            ConstantPoolArray* constant_pool,
529                                            Address target,
530                                            ICacheFlushMode icache_flush_mode =
531                                                FLUSH_ICACHE_IF_NEEDED) ;
532   static inline Address target_address_at(Address pc, Code* code) {
533     ConstantPoolArray* constant_pool = code ? code->constant_pool() : NULL;
534     return target_address_at(pc, constant_pool);
535   }
536   static inline void set_target_address_at(Address pc,
537                                            Code* code,
538                                            Address target,
539                                            ICacheFlushMode icache_flush_mode =
540                                                FLUSH_ICACHE_IF_NEEDED) {
541     ConstantPoolArray* constant_pool = code ? code->constant_pool() : NULL;
542     set_target_address_at(pc, constant_pool, target, icache_flush_mode);
543   }
544
545   // Return the code target address at a call site from the return address
546   // of that call in the instruction stream.
547   static inline Address target_address_from_return_address(Address pc);
548
549   // Return the code target address of the patch debug break slot
550   inline static Address break_address_from_return_address(Address pc);
551
552   // This sets the branch destination (which is in the instruction on x64).
553   // This is for calls and branches within generated code.
554   inline static void deserialization_set_special_target_at(
555       Address instruction_payload, Code* code, Address target) {
556     set_target_address_at(instruction_payload, code, target);
557   }
558
559   static inline RelocInfo::Mode RelocInfoNone() {
560     if (kPointerSize == kInt64Size) {
561       return RelocInfo::NONE64;
562     } else {
563       DCHECK(kPointerSize == kInt32Size);
564       return RelocInfo::NONE32;
565     }
566   }
567
568   inline Handle<Object> code_target_object_handle_at(Address pc);
569   inline Address runtime_entry_at(Address pc);
570   // Number of bytes taken up by the branch target in the code.
571   static const int kSpecialTargetSize = 4;  // Use 32-bit displacement.
572   // Distance between the address of the code target in the call instruction
573   // and the return address pushed on the stack.
574   static const int kCallTargetAddressOffset = 4;  // Use 32-bit displacement.
575   // The length of call(kScratchRegister).
576   static const int kCallScratchRegisterInstructionLength = 3;
577   // The length of call(Immediate32).
578   static const int kShortCallInstructionLength = 5;
579   // The length of movq(kScratchRegister, address).
580   static const int kMoveAddressIntoScratchRegisterInstructionLength =
581       2 + kPointerSize;
582   // The length of movq(kScratchRegister, address) and call(kScratchRegister).
583   static const int kCallSequenceLength =
584       kMoveAddressIntoScratchRegisterInstructionLength +
585       kCallScratchRegisterInstructionLength;
586
587   // The js return and debug break slot must be able to contain an indirect
588   // call sequence, some x64 JS code is padded with int3 to make it large
589   // enough to hold an instruction when the debugger patches it.
590   static const int kJSReturnSequenceLength = kCallSequenceLength;
591   static const int kDebugBreakSlotLength = kCallSequenceLength;
592   static const int kPatchDebugBreakSlotReturnOffset = kCallTargetAddressOffset;
593   // Distance between the start of the JS return sequence and where the
594   // 32-bit displacement of a short call would be. The short call is from
595   // SetDebugBreakAtIC from debug-x64.cc.
596   static const int kPatchReturnSequenceAddressOffset =
597       kJSReturnSequenceLength - kPatchDebugBreakSlotReturnOffset;
598   // Distance between the start of the JS return sequence and where the
599   // 32-bit displacement of a short call would be. The short call is from
600   // SetDebugBreakAtIC from debug-x64.cc.
601   static const int kPatchDebugBreakSlotAddressOffset =
602       kDebugBreakSlotLength - kPatchDebugBreakSlotReturnOffset;
603   static const int kRealPatchReturnSequenceAddressOffset =
604       kMoveAddressIntoScratchRegisterInstructionLength - kPointerSize;
605
606   // One byte opcode for test eax,0xXXXXXXXX.
607   static const byte kTestEaxByte = 0xA9;
608   // One byte opcode for test al, 0xXX.
609   static const byte kTestAlByte = 0xA8;
610   // One byte opcode for nop.
611   static const byte kNopByte = 0x90;
612
613   // One byte prefix for a short conditional jump.
614   static const byte kJccShortPrefix = 0x70;
615   static const byte kJncShortOpcode = kJccShortPrefix | not_carry;
616   static const byte kJcShortOpcode = kJccShortPrefix | carry;
617   static const byte kJnzShortOpcode = kJccShortPrefix | not_zero;
618   static const byte kJzShortOpcode = kJccShortPrefix | zero;
619
620
621   // ---------------------------------------------------------------------------
622   // Code generation
623   //
624   // Function names correspond one-to-one to x64 instruction mnemonics.
625   // Unless specified otherwise, instructions operate on 64-bit operands.
626   //
627   // If we need versions of an assembly instruction that operate on different
628   // width arguments, we add a single-letter suffix specifying the width.
629   // This is done for the following instructions: mov, cmp, inc, dec,
630   // add, sub, and test.
631   // There are no versions of these instructions without the suffix.
632   // - Instructions on 8-bit (byte) operands/registers have a trailing 'b'.
633   // - Instructions on 16-bit (word) operands/registers have a trailing 'w'.
634   // - Instructions on 32-bit (doubleword) operands/registers use 'l'.
635   // - Instructions on 64-bit (quadword) operands/registers use 'q'.
636   // - Instructions on operands/registers with pointer size use 'p'.
637
638   STATIC_ASSERT(kPointerSize == kInt64Size || kPointerSize == kInt32Size);
639
640 #define DECLARE_INSTRUCTION(instruction)                \
641   template<class P1>                                    \
642   void instruction##p(P1 p1) {                          \
643     emit_##instruction(p1, kPointerSize);               \
644   }                                                     \
645                                                         \
646   template<class P1>                                    \
647   void instruction##l(P1 p1) {                          \
648     emit_##instruction(p1, kInt32Size);                 \
649   }                                                     \
650                                                         \
651   template<class P1>                                    \
652   void instruction##q(P1 p1) {                          \
653     emit_##instruction(p1, kInt64Size);                 \
654   }                                                     \
655                                                         \
656   template<class P1, class P2>                          \
657   void instruction##p(P1 p1, P2 p2) {                   \
658     emit_##instruction(p1, p2, kPointerSize);           \
659   }                                                     \
660                                                         \
661   template<class P1, class P2>                          \
662   void instruction##l(P1 p1, P2 p2) {                   \
663     emit_##instruction(p1, p2, kInt32Size);             \
664   }                                                     \
665                                                         \
666   template<class P1, class P2>                          \
667   void instruction##q(P1 p1, P2 p2) {                   \
668     emit_##instruction(p1, p2, kInt64Size);             \
669   }                                                     \
670                                                         \
671   template<class P1, class P2, class P3>                \
672   void instruction##p(P1 p1, P2 p2, P3 p3) {            \
673     emit_##instruction(p1, p2, p3, kPointerSize);       \
674   }                                                     \
675                                                         \
676   template<class P1, class P2, class P3>                \
677   void instruction##l(P1 p1, P2 p2, P3 p3) {            \
678     emit_##instruction(p1, p2, p3, kInt32Size);         \
679   }                                                     \
680                                                         \
681   template<class P1, class P2, class P3>                \
682   void instruction##q(P1 p1, P2 p2, P3 p3) {            \
683     emit_##instruction(p1, p2, p3, kInt64Size);         \
684   }
685   ASSEMBLER_INSTRUCTION_LIST(DECLARE_INSTRUCTION)
686 #undef DECLARE_INSTRUCTION
687
688   // Insert the smallest number of nop instructions
689   // possible to align the pc offset to a multiple
690   // of m, where m must be a power of 2.
691   void Align(int m);
692   void Nop(int bytes = 1);
693   // Aligns code to something that's optimal for a jump target for the platform.
694   void CodeTargetAlign();
695
696   // Stack
697   void pushfq();
698   void popfq();
699
700   void pushq(Immediate value);
701   // Push a 32 bit integer, and guarantee that it is actually pushed as a
702   // 32 bit value, the normal push will optimize the 8 bit case.
703   void pushq_imm32(int32_t imm32);
704   void pushq(Register src);
705   void pushq(const Operand& src);
706
707   void popq(Register dst);
708   void popq(const Operand& dst);
709
710   void enter(Immediate size);
711   void leave();
712
713   // Moves
714   void movb(Register dst, const Operand& src);
715   void movb(Register dst, Immediate imm);
716   void movb(const Operand& dst, Register src);
717   void movb(const Operand& dst, Immediate imm);
718
719   // Move the low 16 bits of a 64-bit register value to a 16-bit
720   // memory location.
721   void movw(Register dst, const Operand& src);
722   void movw(const Operand& dst, Register src);
723   void movw(const Operand& dst, Immediate imm);
724
725   // Move the offset of the label location relative to the current
726   // position (after the move) to the destination.
727   void movl(const Operand& dst, Label* src);
728
729   // Loads a pointer into a register with a relocation mode.
730   void movp(Register dst, void* ptr, RelocInfo::Mode rmode);
731
732   // Loads a 64-bit immediate into a register.
733   void movq(Register dst, int64_t value);
734   void movq(Register dst, uint64_t value);
735
736   void movsxbl(Register dst, const Operand& src);
737   void movsxbq(Register dst, const Operand& src);
738   void movsxwl(Register dst, const Operand& src);
739   void movsxwq(Register dst, const Operand& src);
740   void movsxlq(Register dst, Register src);
741   void movsxlq(Register dst, const Operand& src);
742
743   // Repeated moves.
744
745   void repmovsb();
746   void repmovsw();
747   void repmovsp() { emit_repmovs(kPointerSize); }
748   void repmovsl() { emit_repmovs(kInt32Size); }
749   void repmovsq() { emit_repmovs(kInt64Size); }
750
751   // Instruction to load from an immediate 64-bit pointer into RAX.
752   void load_rax(void* ptr, RelocInfo::Mode rmode);
753   void load_rax(ExternalReference ext);
754
755   // Conditional moves.
756   void cmovq(Condition cc, Register dst, Register src);
757   void cmovq(Condition cc, Register dst, const Operand& src);
758   void cmovl(Condition cc, Register dst, Register src);
759   void cmovl(Condition cc, Register dst, const Operand& src);
760
761   void cmpb(Register dst, Immediate src) {
762     immediate_arithmetic_op_8(0x7, dst, src);
763   }
764
765   void cmpb_al(Immediate src);
766
767   void cmpb(Register dst, Register src) {
768     arithmetic_op_8(0x3A, dst, src);
769   }
770
771   void cmpb(Register dst, const Operand& src) {
772     arithmetic_op_8(0x3A, dst, src);
773   }
774
775   void cmpb(const Operand& dst, Register src) {
776     arithmetic_op_8(0x38, src, dst);
777   }
778
779   void cmpb(const Operand& dst, Immediate src) {
780     immediate_arithmetic_op_8(0x7, dst, src);
781   }
782
783   void cmpw(const Operand& dst, Immediate src) {
784     immediate_arithmetic_op_16(0x7, dst, src);
785   }
786
787   void cmpw(Register dst, Immediate src) {
788     immediate_arithmetic_op_16(0x7, dst, src);
789   }
790
791   void cmpw(Register dst, const Operand& src) {
792     arithmetic_op_16(0x3B, dst, src);
793   }
794
795   void cmpw(Register dst, Register src) {
796     arithmetic_op_16(0x3B, dst, src);
797   }
798
799   void cmpw(const Operand& dst, Register src) {
800     arithmetic_op_16(0x39, src, dst);
801   }
802
803   void andb(Register dst, Immediate src) {
804     immediate_arithmetic_op_8(0x4, dst, src);
805   }
806
807   void decb(Register dst);
808   void decb(const Operand& dst);
809
810   // Sign-extends rax into rdx:rax.
811   void cqo();
812   // Sign-extends eax into edx:eax.
813   void cdq();
814
815   // Multiply eax by src, put the result in edx:eax.
816   void mull(Register src);
817   void mull(const Operand& src);
818   // Multiply rax by src, put the result in rdx:rax.
819   void mulq(Register src);
820
821 #define DECLARE_SHIFT_INSTRUCTION(instruction, subcode)                       \
822   void instruction##p(Register dst, Immediate imm8) {                         \
823     shift(dst, imm8, subcode, kPointerSize);                                  \
824   }                                                                           \
825                                                                               \
826   void instruction##l(Register dst, Immediate imm8) {                         \
827     shift(dst, imm8, subcode, kInt32Size);                                    \
828   }                                                                           \
829                                                                               \
830   void instruction##q(Register dst, Immediate imm8) {                         \
831     shift(dst, imm8, subcode, kInt64Size);                                    \
832   }                                                                           \
833                                                                               \
834   void instruction##p(Operand dst, Immediate imm8) {                          \
835     shift(dst, imm8, subcode, kPointerSize);                                  \
836   }                                                                           \
837                                                                               \
838   void instruction##l(Operand dst, Immediate imm8) {                          \
839     shift(dst, imm8, subcode, kInt32Size);                                    \
840   }                                                                           \
841                                                                               \
842   void instruction##q(Operand dst, Immediate imm8) {                          \
843     shift(dst, imm8, subcode, kInt64Size);                                    \
844   }                                                                           \
845                                                                               \
846   void instruction##p_cl(Register dst) { shift(dst, subcode, kPointerSize); } \
847                                                                               \
848   void instruction##l_cl(Register dst) { shift(dst, subcode, kInt32Size); }   \
849                                                                               \
850   void instruction##q_cl(Register dst) { shift(dst, subcode, kInt64Size); }   \
851                                                                               \
852   void instruction##p_cl(Operand dst) { shift(dst, subcode, kPointerSize); }  \
853                                                                               \
854   void instruction##l_cl(Operand dst) { shift(dst, subcode, kInt32Size); }    \
855                                                                               \
856   void instruction##q_cl(Operand dst) { shift(dst, subcode, kInt64Size); }
857   SHIFT_INSTRUCTION_LIST(DECLARE_SHIFT_INSTRUCTION)
858 #undef DECLARE_SHIFT_INSTRUCTION
859
860   // Shifts dst:src left by cl bits, affecting only dst.
861   void shld(Register dst, Register src);
862
863   // Shifts src:dst right by cl bits, affecting only dst.
864   void shrd(Register dst, Register src);
865
866   void store_rax(void* dst, RelocInfo::Mode mode);
867   void store_rax(ExternalReference ref);
868
869   void subb(Register dst, Immediate src) {
870     immediate_arithmetic_op_8(0x5, dst, src);
871   }
872
873   void testb(Register dst, Register src);
874   void testb(Register reg, Immediate mask);
875   void testb(const Operand& op, Immediate mask);
876   void testb(const Operand& op, Register reg);
877
878   // Bit operations.
879   void bt(const Operand& dst, Register src);
880   void bts(const Operand& dst, Register src);
881   void bsrl(Register dst, Register src);
882
883   // Miscellaneous
884   void clc();
885   void cld();
886   void cpuid();
887   void hlt();
888   void int3();
889   void nop();
890   void ret(int imm16);
891   void setcc(Condition cc, Register reg);
892
893   // Label operations & relative jumps (PPUM Appendix D)
894   //
895   // Takes a branch opcode (cc) and a label (L) and generates
896   // either a backward branch or a forward branch and links it
897   // to the label fixup chain. Usage:
898   //
899   // Label L;    // unbound label
900   // j(cc, &L);  // forward branch to unbound label
901   // bind(&L);   // bind label to the current pc
902   // j(cc, &L);  // backward branch to bound label
903   // bind(&L);   // illegal: a label may be bound only once
904   //
905   // Note: The same Label can be used for forward and backward branches
906   // but it may be bound only once.
907
908   void bind(Label* L);  // binds an unbound label L to the current code position
909
910   // Calls
911   // Call near relative 32-bit displacement, relative to next instruction.
912   void call(Label* L);
913   void call(Address entry, RelocInfo::Mode rmode);
914   void call(Handle<Code> target,
915             RelocInfo::Mode rmode = RelocInfo::CODE_TARGET,
916             TypeFeedbackId ast_id = TypeFeedbackId::None());
917
918   // Calls directly to the given address using a relative offset.
919   // Should only ever be used in Code objects for calls within the
920   // same Code object. Should not be used when generating new code (use labels),
921   // but only when patching existing code.
922   void call(Address target);
923
924   // Call near absolute indirect, address in register
925   void call(Register adr);
926
927   // Jumps
928   // Jump short or near relative.
929   // Use a 32-bit signed displacement.
930   // Unconditional jump to L
931   void jmp(Label* L, Label::Distance distance = Label::kFar);
932   void jmp(Address entry, RelocInfo::Mode rmode);
933   void jmp(Handle<Code> target, RelocInfo::Mode rmode);
934
935   // Jump near absolute indirect (r64)
936   void jmp(Register adr);
937
938   // Conditional jumps
939   void j(Condition cc,
940          Label* L,
941          Label::Distance distance = Label::kFar);
942   void j(Condition cc, Address entry, RelocInfo::Mode rmode);
943   void j(Condition cc, Handle<Code> target, RelocInfo::Mode rmode);
944
945   // Floating-point operations
946   void fld(int i);
947
948   void fld1();
949   void fldz();
950   void fldpi();
951   void fldln2();
952
953   void fld_s(const Operand& adr);
954   void fld_d(const Operand& adr);
955
956   void fstp_s(const Operand& adr);
957   void fstp_d(const Operand& adr);
958   void fstp(int index);
959
960   void fild_s(const Operand& adr);
961   void fild_d(const Operand& adr);
962
963   void fist_s(const Operand& adr);
964
965   void fistp_s(const Operand& adr);
966   void fistp_d(const Operand& adr);
967
968   void fisttp_s(const Operand& adr);
969   void fisttp_d(const Operand& adr);
970
971   void fabs();
972   void fchs();
973
974   void fadd(int i);
975   void fsub(int i);
976   void fmul(int i);
977   void fdiv(int i);
978
979   void fisub_s(const Operand& adr);
980
981   void faddp(int i = 1);
982   void fsubp(int i = 1);
983   void fsubrp(int i = 1);
984   void fmulp(int i = 1);
985   void fdivp(int i = 1);
986   void fprem();
987   void fprem1();
988
989   void fxch(int i = 1);
990   void fincstp();
991   void ffree(int i = 0);
992
993   void ftst();
994   void fucomp(int i);
995   void fucompp();
996   void fucomi(int i);
997   void fucomip();
998
999   void fcompp();
1000   void fnstsw_ax();
1001   void fwait();
1002   void fnclex();
1003
1004   void fsin();
1005   void fcos();
1006   void fptan();
1007   void fyl2x();
1008   void f2xm1();
1009   void fscale();
1010   void fninit();
1011
1012   void frndint();
1013
1014   void sahf();
1015
1016   // SSE instructions
1017   void movaps(XMMRegister dst, XMMRegister src);
1018   void movups(XMMRegister dst, const Operand& src);
1019   void movups(const Operand& dst, XMMRegister src);
1020   void movss(XMMRegister dst, const Operand& src);
1021   void movss(const Operand& dst, XMMRegister src);
1022   void shufps(XMMRegister dst, XMMRegister src, byte imm8);
1023   void shufpd(XMMRegister dst, XMMRegister src, byte imm8);
1024
1025   void cvttss2si(Register dst, const Operand& src);
1026   void cvttss2si(Register dst, XMMRegister src);
1027   void cvtlsi2ss(XMMRegister dst, Register src);
1028
1029   void andps(XMMRegister dst, XMMRegister src);
1030   void andps(XMMRegister dst, const Operand& src);
1031   void orps(XMMRegister dst, XMMRegister src);
1032   void orps(XMMRegister dst, const Operand& src);
1033   void xorps(XMMRegister dst, XMMRegister src);
1034   void xorps(XMMRegister dst, const Operand& src);
1035
1036   void addps(XMMRegister dst, XMMRegister src);
1037   void addps(XMMRegister dst, const Operand& src);
1038   void subps(XMMRegister dst, XMMRegister src);
1039   void subps(XMMRegister dst, const Operand& src);
1040   void mulps(XMMRegister dst, XMMRegister src);
1041   void mulps(XMMRegister dst, const Operand& src);
1042   void divps(XMMRegister dst, XMMRegister src);
1043   void divps(XMMRegister dst, const Operand& src);
1044
1045   void addpd(XMMRegister dst, XMMRegister src);
1046   void addpd(XMMRegister dst, const Operand& src);
1047   void subpd(XMMRegister dst, XMMRegister src);
1048   void subpd(XMMRegister dst, const Operand& src);
1049   void mulpd(XMMRegister dst, XMMRegister src);
1050   void mulpd(XMMRegister dst, const Operand& src);
1051   void divpd(XMMRegister dst, XMMRegister src);
1052   void divpd(XMMRegister dst, const Operand& src);
1053
1054   void movmskps(Register dst, XMMRegister src);
1055
1056   // SSE2 instructions
1057   void movd(XMMRegister dst, Register src);
1058   void movd(Register dst, XMMRegister src);
1059   void movq(XMMRegister dst, Register src);
1060   void movq(Register dst, XMMRegister src);
1061   void movq(XMMRegister dst, XMMRegister src);
1062
1063   // Don't use this unless it's important to keep the
1064   // top half of the destination register unchanged.
1065   // Used movaps when moving double values and movq for integer
1066   // values in xmm registers.
1067   void movsd(XMMRegister dst, XMMRegister src);
1068
1069   void movsd(const Operand& dst, XMMRegister src);
1070   void movsd(XMMRegister dst, const Operand& src);
1071
1072   void movdqa(const Operand& dst, XMMRegister src);
1073   void movdqa(XMMRegister dst, const Operand& src);
1074
1075   void movdqu(const Operand& dst, XMMRegister src);
1076   void movdqu(XMMRegister dst, const Operand& src);
1077
1078   void movapd(XMMRegister dst, XMMRegister src);
1079
1080   void psllq(XMMRegister reg, byte imm8);
1081   void psrlq(XMMRegister reg, byte imm8);
1082   void pslld(XMMRegister reg, byte imm8);
1083   void psrld(XMMRegister reg, byte imm8);
1084
1085   void cvttsd2si(Register dst, const Operand& src);
1086   void cvttsd2si(Register dst, XMMRegister src);
1087   void cvttsd2siq(Register dst, XMMRegister src);
1088   void cvttsd2siq(Register dst, const Operand& src);
1089
1090   void cvtlsi2sd(XMMRegister dst, const Operand& src);
1091   void cvtlsi2sd(XMMRegister dst, Register src);
1092   void cvtqsi2sd(XMMRegister dst, const Operand& src);
1093   void cvtqsi2sd(XMMRegister dst, Register src);
1094
1095
1096   void cvtss2sd(XMMRegister dst, XMMRegister src);
1097   void cvtss2sd(XMMRegister dst, const Operand& src);
1098   void cvtsd2ss(XMMRegister dst, XMMRegister src);
1099   void cvtsd2ss(XMMRegister dst, const Operand& src);
1100
1101   void cvtsd2si(Register dst, XMMRegister src);
1102   void cvtsd2siq(Register dst, XMMRegister src);
1103
1104   void addsd(XMMRegister dst, XMMRegister src);
1105   void addsd(XMMRegister dst, const Operand& src);
1106   void subsd(XMMRegister dst, XMMRegister src);
1107   void subsd(XMMRegister dst, const Operand& src);
1108   void mulsd(XMMRegister dst, XMMRegister src);
1109   void mulsd(XMMRegister dst, const Operand& src);
1110   void divsd(XMMRegister dst, XMMRegister src);
1111   void divsd(XMMRegister dst, const Operand& src);
1112
1113   void andpd(XMMRegister dst, XMMRegister src);
1114   void andpd(XMMRegister dst, const Operand& src);
1115   void orpd(XMMRegister dst, XMMRegister src);
1116   void xorpd(XMMRegister dst, XMMRegister src);
1117   void xorpd(XMMRegister dst, const Operand& src);
1118   void sqrtsd(XMMRegister dst, XMMRegister src);
1119   void sqrtsd(XMMRegister dst, const Operand& src);
1120
1121   void ucomisd(XMMRegister dst, XMMRegister src);
1122   void ucomisd(XMMRegister dst, const Operand& src);
1123   void cmpltsd(XMMRegister dst, XMMRegister src);
1124   void pcmpeqd(XMMRegister dst, XMMRegister src);
1125
1126   void movmskpd(Register dst, XMMRegister src);
1127
1128   // SSE 4.1 instruction
1129   void extractps(Register dst, XMMRegister src, byte imm8);
1130   void insertps(XMMRegister dst, XMMRegister src, byte imm8);
1131   void pinsrd(XMMRegister dst, Register src, byte imm8);
1132
1133   void minps(XMMRegister dst, XMMRegister src);
1134   void minps(XMMRegister dst, const Operand& src);
1135   void maxps(XMMRegister dst, XMMRegister src);
1136   void maxps(XMMRegister dst, const Operand& src);
1137   void minpd(XMMRegister dst, XMMRegister src);
1138   void minpd(XMMRegister dst, const Operand& src);
1139   void maxpd(XMMRegister dst, XMMRegister src);
1140   void maxpd(XMMRegister dst, const Operand& src);
1141   void rcpps(XMMRegister dst, XMMRegister src);
1142   void rcpps(XMMRegister dst, const Operand& src);
1143   void rsqrtps(XMMRegister dst, XMMRegister src);
1144   void rsqrtps(XMMRegister dst, const Operand& src);
1145   void sqrtps(XMMRegister dst, XMMRegister src);
1146   void sqrtps(XMMRegister dst, const Operand& src);
1147   void sqrtpd(XMMRegister dst, XMMRegister src);
1148   void sqrtpd(XMMRegister dst, const Operand& src);
1149   void paddd(XMMRegister dst, XMMRegister src);
1150   void paddd(XMMRegister dst, const Operand& src);
1151   void psubd(XMMRegister dst, XMMRegister src);
1152   void psubd(XMMRegister dst, const Operand& src);
1153   void pmulld(XMMRegister dst, XMMRegister src);
1154   void pmulld(XMMRegister dst, const Operand& src);
1155   void pmuludq(XMMRegister dst, XMMRegister src);
1156   void pmuludq(XMMRegister dst, const Operand& src);
1157   void punpackldq(XMMRegister dst, XMMRegister src);
1158   void punpackldq(XMMRegister dst, const Operand& src);
1159   void psrldq(XMMRegister dst, uint8_t shift);
1160   void pshufd(XMMRegister dst, XMMRegister src, uint8_t shuffle);
1161   void cvtps2dq(XMMRegister dst, XMMRegister src);
1162   void cvtps2dq(XMMRegister dst, const Operand& src);
1163   void cvtdq2ps(XMMRegister dst, XMMRegister src);
1164   void cvtdq2ps(XMMRegister dst, const Operand& src);
1165
1166   enum RoundingMode {
1167     kRoundToNearest = 0x0,
1168     kRoundDown      = 0x1,
1169     kRoundUp        = 0x2,
1170     kRoundToZero    = 0x3
1171   };
1172
1173   void roundsd(XMMRegister dst, XMMRegister src, RoundingMode mode);
1174
1175   void cmpps(XMMRegister dst, XMMRegister src, int8_t cmp);
1176   void cmpeqps(XMMRegister dst, XMMRegister src);
1177   void cmpltps(XMMRegister dst, XMMRegister src);
1178   void cmpleps(XMMRegister dst, XMMRegister src);
1179   void cmpneqps(XMMRegister dst, XMMRegister src);
1180   void cmpnltps(XMMRegister dst, XMMRegister src);
1181   void cmpnleps(XMMRegister dst, XMMRegister src);
1182
1183   void pslld(XMMRegister dst, XMMRegister src);
1184   void psrld(XMMRegister dst, XMMRegister src);
1185   void psrad(XMMRegister reg, int8_t shift);
1186   void psrad(XMMRegister dst, XMMRegister src);
1187
1188   void pcmpgtd(XMMRegister dst, XMMRegister src);
1189   void pcmpltd(XMMRegister dst, XMMRegister src);
1190
1191   // Debugging
1192   void Print();
1193
1194   // Check the code size generated from label to here.
1195   int SizeOfCodeGeneratedSince(Label* label) {
1196     return pc_offset() - label->pos();
1197   }
1198
1199   // Mark address of the ExitJSFrame code.
1200   void RecordJSReturn();
1201
1202   // Mark address of a debug break slot.
1203   void RecordDebugBreakSlot();
1204
1205   // Record a comment relocation entry that can be used by a disassembler.
1206   // Use --code-comments to enable.
1207   void RecordComment(const char* msg, bool force = false);
1208
1209   // Allocate a constant pool of the correct size for the generated code.
1210   Handle<ConstantPoolArray> NewConstantPool(Isolate* isolate);
1211
1212   // Generate the constant pool for the generated code.
1213   void PopulateConstantPool(ConstantPoolArray* constant_pool);
1214
1215   // Writes a single word of data in the code stream.
1216   // Used for inline tables, e.g., jump-tables.
1217   void db(uint8_t data);
1218   void dd(uint32_t data);
1219
1220   PositionsRecorder* positions_recorder() { return &positions_recorder_; }
1221
1222   // Check if there is less than kGap bytes available in the buffer.
1223   // If this is the case, we need to grow the buffer before emitting
1224   // an instruction or relocation information.
1225   inline bool buffer_overflow() const {
1226     return pc_ >= reloc_info_writer.pos() - kGap;
1227   }
1228
1229   // Get the number of bytes available in the buffer.
1230   inline int available_space() const {
1231     return static_cast<int>(reloc_info_writer.pos() - pc_);
1232   }
1233
1234   static bool IsNop(Address addr);
1235
1236   // Avoid overflows for displacements etc.
1237   static const int kMaximalBufferSize = 512*MB;
1238
1239   byte byte_at(int pos)  { return buffer_[pos]; }
1240   void set_byte_at(int pos, byte value) { buffer_[pos] = value; }
1241
1242  protected:
1243   // Call near indirect
1244   void call(const Operand& operand);
1245
1246   // Jump near absolute indirect (m64)
1247   void jmp(const Operand& src);
1248
1249  private:
1250   byte* addr_at(int pos)  { return buffer_ + pos; }
1251   uint32_t long_at(int pos)  {
1252     return *reinterpret_cast<uint32_t*>(addr_at(pos));
1253   }
1254   void long_at_put(int pos, uint32_t x)  {
1255     *reinterpret_cast<uint32_t*>(addr_at(pos)) = x;
1256   }
1257
1258   // code emission
1259   void GrowBuffer();
1260
1261   void emit(byte x) { *pc_++ = x; }
1262   inline void emitl(uint32_t x);
1263   inline void emitp(void* x, RelocInfo::Mode rmode);
1264   inline void emitq(uint64_t x);
1265   inline void emitw(uint16_t x);
1266   inline void emit_code_target(Handle<Code> target,
1267                                RelocInfo::Mode rmode,
1268                                TypeFeedbackId ast_id = TypeFeedbackId::None());
1269   inline void emit_runtime_entry(Address entry, RelocInfo::Mode rmode);
1270   void emit(Immediate x) { emitl(x.value_); }
1271
1272   // Emits a REX prefix that encodes a 64-bit operand size and
1273   // the top bit of both register codes.
1274   // High bit of reg goes to REX.R, high bit of rm_reg goes to REX.B.
1275   // REX.W is set.
1276   inline void emit_rex_64(XMMRegister reg, Register rm_reg);
1277   inline void emit_rex_64(Register reg, XMMRegister rm_reg);
1278   inline void emit_rex_64(Register reg, Register rm_reg);
1279
1280   // Emits a REX prefix that encodes a 64-bit operand size and
1281   // the top bit of the destination, index, and base register codes.
1282   // The high bit of reg is used for REX.R, the high bit of op's base
1283   // register is used for REX.B, and the high bit of op's index register
1284   // is used for REX.X.  REX.W is set.
1285   inline void emit_rex_64(Register reg, const Operand& op);
1286   inline void emit_rex_64(XMMRegister reg, const Operand& op);
1287
1288   // Emits a REX prefix that encodes a 64-bit operand size and
1289   // the top bit of the register code.
1290   // The high bit of register is used for REX.B.
1291   // REX.W is set and REX.R and REX.X are clear.
1292   inline void emit_rex_64(Register rm_reg);
1293
1294   // Emits a REX prefix that encodes a 64-bit operand size and
1295   // the top bit of the index and base register codes.
1296   // The high bit of op's base register is used for REX.B, and the high
1297   // bit of op's index register is used for REX.X.
1298   // REX.W is set and REX.R clear.
1299   inline void emit_rex_64(const Operand& op);
1300
1301   // Emit a REX prefix that only sets REX.W to choose a 64-bit operand size.
1302   void emit_rex_64() { emit(0x48); }
1303
1304   // High bit of reg goes to REX.R, high bit of rm_reg goes to REX.B.
1305   // REX.W is clear.
1306   inline void emit_rex_32(Register reg, Register rm_reg);
1307
1308   // The high bit of reg is used for REX.R, the high bit of op's base
1309   // register is used for REX.B, and the high bit of op's index register
1310   // is used for REX.X.  REX.W is cleared.
1311   inline void emit_rex_32(Register reg, const Operand& op);
1312
1313   // High bit of rm_reg goes to REX.B.
1314   // REX.W, REX.R and REX.X are clear.
1315   inline void emit_rex_32(Register rm_reg);
1316
1317   // High bit of base goes to REX.B and high bit of index to REX.X.
1318   // REX.W and REX.R are clear.
1319   inline void emit_rex_32(const Operand& op);
1320
1321   // High bit of reg goes to REX.R, high bit of rm_reg goes to REX.B.
1322   // REX.W is cleared.  If no REX bits are set, no byte is emitted.
1323   inline void emit_optional_rex_32(Register reg, Register rm_reg);
1324
1325   // The high bit of reg is used for REX.R, the high bit of op's base
1326   // register is used for REX.B, and the high bit of op's index register
1327   // is used for REX.X.  REX.W is cleared.  If no REX bits are set, nothing
1328   // is emitted.
1329   inline void emit_optional_rex_32(Register reg, const Operand& op);
1330
1331   // As for emit_optional_rex_32(Register, Register), except that
1332   // the registers are XMM registers.
1333   inline void emit_optional_rex_32(XMMRegister reg, XMMRegister base);
1334
1335   // As for emit_optional_rex_32(Register, Register), except that
1336   // one of the registers is an XMM registers.
1337   inline void emit_optional_rex_32(XMMRegister reg, Register base);
1338
1339   // As for emit_optional_rex_32(Register, Register), except that
1340   // one of the registers is an XMM registers.
1341   inline void emit_optional_rex_32(Register reg, XMMRegister base);
1342
1343   // As for emit_optional_rex_32(Register, const Operand&), except that
1344   // the register is an XMM register.
1345   inline void emit_optional_rex_32(XMMRegister reg, const Operand& op);
1346
1347   // Optionally do as emit_rex_32(Register) if the register number has
1348   // the high bit set.
1349   inline void emit_optional_rex_32(Register rm_reg);
1350   inline void emit_optional_rex_32(XMMRegister rm_reg);
1351
1352   // Optionally do as emit_rex_32(const Operand&) if the operand register
1353   // numbers have a high bit set.
1354   inline void emit_optional_rex_32(const Operand& op);
1355
1356   void emit_rex(int size) {
1357     if (size == kInt64Size) {
1358       emit_rex_64();
1359     } else {
1360       DCHECK(size == kInt32Size);
1361     }
1362   }
1363
1364   template<class P1>
1365   void emit_rex(P1 p1, int size) {
1366     if (size == kInt64Size) {
1367       emit_rex_64(p1);
1368     } else {
1369       DCHECK(size == kInt32Size);
1370       emit_optional_rex_32(p1);
1371     }
1372   }
1373
1374   template<class P1, class P2>
1375   void emit_rex(P1 p1, P2 p2, int size) {
1376     if (size == kInt64Size) {
1377       emit_rex_64(p1, p2);
1378     } else {
1379       DCHECK(size == kInt32Size);
1380       emit_optional_rex_32(p1, p2);
1381     }
1382   }
1383
1384   // Emit the ModR/M byte, and optionally the SIB byte and
1385   // 1- or 4-byte offset for a memory operand.  Also encodes
1386   // the second operand of the operation, a register or operation
1387   // subcode, into the reg field of the ModR/M byte.
1388   void emit_operand(Register reg, const Operand& adr) {
1389     emit_operand(reg.low_bits(), adr);
1390   }
1391
1392   // Emit the ModR/M byte, and optionally the SIB byte and
1393   // 1- or 4-byte offset for a memory operand.  Also used to encode
1394   // a three-bit opcode extension into the ModR/M byte.
1395   void emit_operand(int rm, const Operand& adr);
1396
1397   // Emit a ModR/M byte with registers coded in the reg and rm_reg fields.
1398   void emit_modrm(Register reg, Register rm_reg) {
1399     emit(0xC0 | reg.low_bits() << 3 | rm_reg.low_bits());
1400   }
1401
1402   // Emit a ModR/M byte with an operation subcode in the reg field and
1403   // a register in the rm_reg field.
1404   void emit_modrm(int code, Register rm_reg) {
1405     DCHECK(is_uint3(code));
1406     emit(0xC0 | code << 3 | rm_reg.low_bits());
1407   }
1408
1409   // Emit the code-object-relative offset of the label's position
1410   inline void emit_code_relative_offset(Label* label);
1411
1412   // The first argument is the reg field, the second argument is the r/m field.
1413   void emit_sse_operand(XMMRegister dst, XMMRegister src);
1414   void emit_sse_operand(XMMRegister reg, const Operand& adr);
1415   void emit_sse_operand(Register reg, const Operand& adr);
1416   void emit_sse_operand(XMMRegister dst, Register src);
1417   void emit_sse_operand(Register dst, XMMRegister src);
1418   void emit_sse_operand(XMMRegister dst);
1419
1420   // Emit machine code for one of the operations ADD, ADC, SUB, SBC,
1421   // AND, OR, XOR, or CMP.  The encodings of these operations are all
1422   // similar, differing just in the opcode or in the reg field of the
1423   // ModR/M byte.
1424   void arithmetic_op_8(byte opcode, Register reg, Register rm_reg);
1425   void arithmetic_op_8(byte opcode, Register reg, const Operand& rm_reg);
1426   void arithmetic_op_16(byte opcode, Register reg, Register rm_reg);
1427   void arithmetic_op_16(byte opcode, Register reg, const Operand& rm_reg);
1428   // Operate on operands/registers with pointer size, 32-bit or 64-bit size.
1429   void arithmetic_op(byte opcode, Register reg, Register rm_reg, int size);
1430   void arithmetic_op(byte opcode,
1431                      Register reg,
1432                      const Operand& rm_reg,
1433                      int size);
1434   // Operate on a byte in memory or register.
1435   void immediate_arithmetic_op_8(byte subcode,
1436                                  Register dst,
1437                                  Immediate src);
1438   void immediate_arithmetic_op_8(byte subcode,
1439                                  const Operand& dst,
1440                                  Immediate src);
1441   // Operate on a word in memory or register.
1442   void immediate_arithmetic_op_16(byte subcode,
1443                                   Register dst,
1444                                   Immediate src);
1445   void immediate_arithmetic_op_16(byte subcode,
1446                                   const Operand& dst,
1447                                   Immediate src);
1448   // Operate on operands/registers with pointer size, 32-bit or 64-bit size.
1449   void immediate_arithmetic_op(byte subcode,
1450                                Register dst,
1451                                Immediate src,
1452                                int size);
1453   void immediate_arithmetic_op(byte subcode,
1454                                const Operand& dst,
1455                                Immediate src,
1456                                int size);
1457
1458   // Emit machine code for a shift operation.
1459   void shift(Operand dst, Immediate shift_amount, int subcode, int size);
1460   void shift(Register dst, Immediate shift_amount, int subcode, int size);
1461   // Shift dst by cl % 64 bits.
1462   void shift(Register dst, int subcode, int size);
1463   void shift(Operand dst, int subcode, int size);
1464
1465   void emit_farith(int b1, int b2, int i);
1466
1467   // labels
1468   // void print(Label* L);
1469   void bind_to(Label* L, int pos);
1470
1471   // record reloc info for current pc_
1472   void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
1473
1474   // Arithmetics
1475   void emit_add(Register dst, Register src, int size) {
1476     arithmetic_op(0x03, dst, src, size);
1477   }
1478
1479   void emit_add(Register dst, Immediate src, int size) {
1480     immediate_arithmetic_op(0x0, dst, src, size);
1481   }
1482
1483   void emit_add(Register dst, const Operand& src, int size) {
1484     arithmetic_op(0x03, dst, src, size);
1485   }
1486
1487   void emit_add(const Operand& dst, Register src, int size) {
1488     arithmetic_op(0x1, src, dst, size);
1489   }
1490
1491   void emit_add(const Operand& dst, Immediate src, int size) {
1492     immediate_arithmetic_op(0x0, dst, src, size);
1493   }
1494
1495   void emit_and(Register dst, Register src, int size) {
1496     arithmetic_op(0x23, dst, src, size);
1497   }
1498
1499   void emit_and(Register dst, const Operand& src, int size) {
1500     arithmetic_op(0x23, dst, src, size);
1501   }
1502
1503   void emit_and(const Operand& dst, Register src, int size) {
1504     arithmetic_op(0x21, src, dst, size);
1505   }
1506
1507   void emit_and(Register dst, Immediate src, int size) {
1508     immediate_arithmetic_op(0x4, dst, src, size);
1509   }
1510
1511   void emit_and(const Operand& dst, Immediate src, int size) {
1512     immediate_arithmetic_op(0x4, dst, src, size);
1513   }
1514
1515   void emit_cmp(Register dst, Register src, int size) {
1516     arithmetic_op(0x3B, dst, src, size);
1517   }
1518
1519   void emit_cmp(Register dst, const Operand& src, int size) {
1520     arithmetic_op(0x3B, dst, src, size);
1521   }
1522
1523   void emit_cmp(const Operand& dst, Register src, int size) {
1524     arithmetic_op(0x39, src, dst, size);
1525   }
1526
1527   void emit_cmp(Register dst, Immediate src, int size) {
1528     immediate_arithmetic_op(0x7, dst, src, size);
1529   }
1530
1531   void emit_cmp(const Operand& dst, Immediate src, int size) {
1532     immediate_arithmetic_op(0x7, dst, src, size);
1533   }
1534
1535   void emit_dec(Register dst, int size);
1536   void emit_dec(const Operand& dst, int size);
1537
1538   // Divide rdx:rax by src.  Quotient in rax, remainder in rdx when size is 64.
1539   // Divide edx:eax by lower 32 bits of src.  Quotient in eax, remainder in edx
1540   // when size is 32.
1541   void emit_idiv(Register src, int size);
1542   void emit_div(Register src, int size);
1543
1544   // Signed multiply instructions.
1545   // rdx:rax = rax * src when size is 64 or edx:eax = eax * src when size is 32.
1546   void emit_imul(Register src, int size);
1547   void emit_imul(const Operand& src, int size);
1548   void emit_imul(Register dst, Register src, int size);
1549   void emit_imul(Register dst, const Operand& src, int size);
1550   void emit_imul(Register dst, Register src, Immediate imm, int size);
1551   void emit_imul(Register dst, const Operand& src, Immediate imm, int size);
1552
1553   void emit_inc(Register dst, int size);
1554   void emit_inc(const Operand& dst, int size);
1555
1556   void emit_lea(Register dst, const Operand& src, int size);
1557
1558   void emit_mov(Register dst, const Operand& src, int size);
1559   void emit_mov(Register dst, Register src, int size);
1560   void emit_mov(const Operand& dst, Register src, int size);
1561   void emit_mov(Register dst, Immediate value, int size);
1562   void emit_mov(const Operand& dst, Immediate value, int size);
1563
1564   void emit_movzxb(Register dst, const Operand& src, int size);
1565   void emit_movzxb(Register dst, Register src, int size);
1566   void emit_movzxw(Register dst, const Operand& src, int size);
1567   void emit_movzxw(Register dst, Register src, int size);
1568
1569   void emit_neg(Register dst, int size);
1570   void emit_neg(const Operand& dst, int size);
1571
1572   void emit_not(Register dst, int size);
1573   void emit_not(const Operand& dst, int size);
1574
1575   void emit_or(Register dst, Register src, int size) {
1576     arithmetic_op(0x0B, dst, src, size);
1577   }
1578
1579   void emit_or(Register dst, const Operand& src, int size) {
1580     arithmetic_op(0x0B, dst, src, size);
1581   }
1582
1583   void emit_or(const Operand& dst, Register src, int size) {
1584     arithmetic_op(0x9, src, dst, size);
1585   }
1586
1587   void emit_or(Register dst, Immediate src, int size) {
1588     immediate_arithmetic_op(0x1, dst, src, size);
1589   }
1590
1591   void emit_or(const Operand& dst, Immediate src, int size) {
1592     immediate_arithmetic_op(0x1, dst, src, size);
1593   }
1594
1595   void emit_repmovs(int size);
1596
1597   void emit_sbb(Register dst, Register src, int size) {
1598     arithmetic_op(0x1b, dst, src, size);
1599   }
1600
1601   void emit_sub(Register dst, Register src, int size) {
1602     arithmetic_op(0x2B, dst, src, size);
1603   }
1604
1605   void emit_sub(Register dst, Immediate src, int size) {
1606     immediate_arithmetic_op(0x5, dst, src, size);
1607   }
1608
1609   void emit_sub(Register dst, const Operand& src, int size) {
1610     arithmetic_op(0x2B, dst, src, size);
1611   }
1612
1613   void emit_sub(const Operand& dst, Register src, int size) {
1614     arithmetic_op(0x29, src, dst, size);
1615   }
1616
1617   void emit_sub(const Operand& dst, Immediate src, int size) {
1618     immediate_arithmetic_op(0x5, dst, src, size);
1619   }
1620
1621   void emit_test(Register dst, Register src, int size);
1622   void emit_test(Register reg, Immediate mask, int size);
1623   void emit_test(const Operand& op, Register reg, int size);
1624   void emit_test(const Operand& op, Immediate mask, int size);
1625   void emit_test(Register reg, const Operand& op, int size) {
1626     return emit_test(op, reg, size);
1627   }
1628
1629   void emit_xchg(Register dst, Register src, int size);
1630   void emit_xchg(Register dst, const Operand& src, int size);
1631
1632   void emit_xor(Register dst, Register src, int size) {
1633     if (size == kInt64Size && dst.code() == src.code()) {
1634     // 32 bit operations zero the top 32 bits of 64 bit registers. Therefore
1635     // there is no need to make this a 64 bit operation.
1636       arithmetic_op(0x33, dst, src, kInt32Size);
1637     } else {
1638       arithmetic_op(0x33, dst, src, size);
1639     }
1640   }
1641
1642   void emit_xor(Register dst, const Operand& src, int size) {
1643     arithmetic_op(0x33, dst, src, size);
1644   }
1645
1646   void emit_xor(Register dst, Immediate src, int size) {
1647     immediate_arithmetic_op(0x6, dst, src, size);
1648   }
1649
1650   void emit_xor(const Operand& dst, Immediate src, int size) {
1651     immediate_arithmetic_op(0x6, dst, src, size);
1652   }
1653
1654   void emit_xor(const Operand& dst, Register src, int size) {
1655     arithmetic_op(0x31, src, dst, size);
1656   }
1657
1658   friend class CodePatcher;
1659   friend class EnsureSpace;
1660   friend class RegExpMacroAssemblerX64;
1661
1662   // code generation
1663   RelocInfoWriter reloc_info_writer;
1664
1665   List< Handle<Code> > code_targets_;
1666
1667   PositionsRecorder positions_recorder_;
1668   friend class PositionsRecorder;
1669 };
1670
1671
1672 // Helper class that ensures that there is enough space for generating
1673 // instructions and relocation information.  The constructor makes
1674 // sure that there is enough space and (in debug mode) the destructor
1675 // checks that we did not generate too much.
1676 class EnsureSpace BASE_EMBEDDED {
1677  public:
1678   explicit EnsureSpace(Assembler* assembler) : assembler_(assembler) {
1679     if (assembler_->buffer_overflow()) assembler_->GrowBuffer();
1680 #ifdef DEBUG
1681     space_before_ = assembler_->available_space();
1682 #endif
1683   }
1684
1685 #ifdef DEBUG
1686   ~EnsureSpace() {
1687     int bytes_generated = space_before_ - assembler_->available_space();
1688     DCHECK(bytes_generated < assembler_->kGap);
1689   }
1690 #endif
1691
1692  private:
1693   Assembler* assembler_;
1694 #ifdef DEBUG
1695   int space_before_;
1696 #endif
1697 };
1698
1699 } }  // namespace v8::internal
1700
1701 #endif  // V8_X64_ASSEMBLER_X64_H_