• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_X87_ASSEMBLER_X87_H_
38 #define V8_X87_ASSEMBLER_X87_H_
39 
40 #include <deque>
41 
42 #include "src/assembler.h"
43 #include "src/isolate.h"
44 #include "src/utils.h"
45 
46 namespace v8 {
47 namespace internal {
48 
49 #define GENERAL_REGISTERS(V) \
50   V(eax)                     \
51   V(ecx)                     \
52   V(edx)                     \
53   V(ebx)                     \
54   V(esp)                     \
55   V(ebp)                     \
56   V(esi)                     \
57   V(edi)
58 
59 #define ALLOCATABLE_GENERAL_REGISTERS(V) \
60   V(eax)                                 \
61   V(ecx)                                 \
62   V(edx)                                 \
63   V(ebx)                                 \
64   V(esi)                                 \
65   V(edi)
66 
67 #define DOUBLE_REGISTERS(V) \
68   V(stX_0)                  \
69   V(stX_1)                  \
70   V(stX_2)                  \
71   V(stX_3)                  \
72   V(stX_4)                  \
73   V(stX_5)                  \
74   V(stX_6)                  \
75   V(stX_7)
76 
77 #define FLOAT_REGISTERS DOUBLE_REGISTERS
78 #define SIMD128_REGISTERS DOUBLE_REGISTERS
79 
80 #define ALLOCATABLE_DOUBLE_REGISTERS(V) \
81   V(stX_0)                              \
82   V(stX_1)                              \
83   V(stX_2)                              \
84   V(stX_3)                              \
85   V(stX_4)                              \
86   V(stX_5)
87 
88 // CPU Registers.
89 //
90 // 1) We would prefer to use an enum, but enum values are assignment-
91 // compatible with int, which has caused code-generation bugs.
92 //
93 // 2) We would prefer to use a class instead of a struct but we don't like
94 // the register initialization to depend on the particular initialization
95 // order (which appears to be different on OS X, Linux, and Windows for the
96 // installed versions of C++ we tried). Using a struct permits C-style
97 // "initialization". Also, the Register objects cannot be const as this
98 // forces initialization stubs in MSVC, making us dependent on initialization
99 // order.
100 //
101 // 3) By not using an enum, we are possibly preventing the compiler from
102 // doing certain constant folds, which may significantly reduce the
103 // code generated for some assembly instructions (because they boil down
104 // to a few constants). If this is a problem, we could change the code
105 // such that we use an enum in optimized mode, and the struct in debug
106 // mode. This way we get the compile-time error checking in debug mode
107 // and best performance in optimized code.
108 //
109 struct Register {
110   enum Code {
111 #define REGISTER_CODE(R) kCode_##R,
112     GENERAL_REGISTERS(REGISTER_CODE)
113 #undef REGISTER_CODE
114         kAfterLast,
115     kCode_no_reg = -1
116   };
117 
118   static const int kNumRegisters = Code::kAfterLast;
119 
from_codeRegister120   static Register from_code(int code) {
121     DCHECK(code >= 0);
122     DCHECK(code < kNumRegisters);
123     Register r = {code};
124     return r;
125   }
is_validRegister126   bool is_valid() const { return 0 <= reg_code && reg_code < kNumRegisters; }
isRegister127   bool is(Register reg) const { return reg_code == reg.reg_code; }
codeRegister128   int code() const {
129     DCHECK(is_valid());
130     return reg_code;
131   }
bitRegister132   int bit() const {
133     DCHECK(is_valid());
134     return 1 << reg_code;
135   }
136 
is_byte_registerRegister137   bool is_byte_register() const { return reg_code <= 3; }
138 
139   // Unfortunately we can't make this private in a struct.
140   int reg_code;
141 };
142 
143 
144 #define DECLARE_REGISTER(R) const Register R = {Register::kCode_##R};
145 GENERAL_REGISTERS(DECLARE_REGISTER)
146 #undef DECLARE_REGISTER
147 const Register no_reg = {Register::kCode_no_reg};
148 
149 static const bool kSimpleFPAliasing = true;
150 
151 struct X87Register {
152   enum Code {
153 #define REGISTER_CODE(R) kCode_##R,
154     DOUBLE_REGISTERS(REGISTER_CODE)
155 #undef REGISTER_CODE
156         kAfterLast,
157     kCode_no_reg = -1
158   };
159 
160   static const int kMaxNumRegisters = Code::kAfterLast;
161   static const int kMaxNumAllocatableRegisters = 6;
162 
from_codeX87Register163   static X87Register from_code(int code) {
164     X87Register result = {code};
165     return result;
166   }
167 
is_validX87Register168   bool is_valid() const { return 0 <= reg_code && reg_code < kMaxNumRegisters; }
169 
codeX87Register170   int code() const {
171     DCHECK(is_valid());
172     return reg_code;
173   }
174 
isX87Register175   bool is(X87Register reg) const { return reg_code == reg.reg_code; }
176 
177   int reg_code;
178 };
179 
180 typedef X87Register FloatRegister;
181 
182 typedef X87Register DoubleRegister;
183 
184 // TODO(x87) Define SIMD registers.
185 typedef X87Register Simd128Register;
186 
187 #define DECLARE_REGISTER(R) \
188   const DoubleRegister R = {DoubleRegister::kCode_##R};
189 DOUBLE_REGISTERS(DECLARE_REGISTER)
190 #undef DECLARE_REGISTER
191 const DoubleRegister no_double_reg = {DoubleRegister::kCode_no_reg};
192 
193 enum Condition {
194   // any value < 0 is considered no_condition
195   no_condition  = -1,
196 
197   overflow      =  0,
198   no_overflow   =  1,
199   below         =  2,
200   above_equal   =  3,
201   equal         =  4,
202   not_equal     =  5,
203   below_equal   =  6,
204   above         =  7,
205   negative      =  8,
206   positive      =  9,
207   parity_even   = 10,
208   parity_odd    = 11,
209   less          = 12,
210   greater_equal = 13,
211   less_equal    = 14,
212   greater       = 15,
213 
214   // aliases
215   carry         = below,
216   not_carry     = above_equal,
217   zero          = equal,
218   not_zero      = not_equal,
219   sign          = negative,
220   not_sign      = positive
221 };
222 
223 
224 // Returns the equivalent of !cc.
225 // Negation of the default no_condition (-1) results in a non-default
226 // no_condition value (-2). As long as tests for no_condition check
227 // for condition < 0, this will work as expected.
NegateCondition(Condition cc)228 inline Condition NegateCondition(Condition cc) {
229   return static_cast<Condition>(cc ^ 1);
230 }
231 
232 
233 // Commute a condition such that {a cond b == b cond' a}.
CommuteCondition(Condition cc)234 inline Condition CommuteCondition(Condition cc) {
235   switch (cc) {
236     case below:
237       return above;
238     case above:
239       return below;
240     case above_equal:
241       return below_equal;
242     case below_equal:
243       return above_equal;
244     case less:
245       return greater;
246     case greater:
247       return less;
248     case greater_equal:
249       return less_equal;
250     case less_equal:
251       return greater_equal;
252     default:
253       return cc;
254   }
255 }
256 
257 
258 enum RoundingMode {
259   kRoundToNearest = 0x0,
260   kRoundDown = 0x1,
261   kRoundUp = 0x2,
262   kRoundToZero = 0x3
263 };
264 
265 
266 // -----------------------------------------------------------------------------
267 // Machine instruction Immediates
268 
269 class Immediate BASE_EMBEDDED {
270  public:
271   inline explicit Immediate(int x);
272   inline explicit Immediate(const ExternalReference& ext);
273   inline explicit Immediate(Handle<Object> handle);
274   inline explicit Immediate(Smi* value);
275   inline explicit Immediate(Address addr);
276   inline explicit Immediate(Address x, RelocInfo::Mode rmode);
277 
CodeRelativeOffset(Label * label)278   static Immediate CodeRelativeOffset(Label* label) {
279     return Immediate(label);
280   }
281 
is_zero()282   bool is_zero() const { return x_ == 0 && RelocInfo::IsNone(rmode_); }
is_int8()283   bool is_int8() const {
284     return -128 <= x_ && x_ < 128 && RelocInfo::IsNone(rmode_);
285   }
is_uint8()286   bool is_uint8() const {
287     return v8::internal::is_uint8(x_) && RelocInfo::IsNone(rmode_);
288   }
is_int16()289   bool is_int16() const {
290     return -32768 <= x_ && x_ < 32768 && RelocInfo::IsNone(rmode_);
291   }
is_uint16()292   bool is_uint16() const {
293     return v8::internal::is_uint16(x_) && RelocInfo::IsNone(rmode_);
294   }
295 
296  private:
297   inline explicit Immediate(Label* value);
298 
299   int x_;
300   RelocInfo::Mode rmode_;
301 
302   friend class Operand;
303   friend class Assembler;
304   friend class MacroAssembler;
305 };
306 
307 
308 // -----------------------------------------------------------------------------
309 // Machine instruction Operands
310 
311 enum ScaleFactor {
312   times_1 = 0,
313   times_2 = 1,
314   times_4 = 2,
315   times_8 = 3,
316   times_int_size = times_4,
317   times_half_pointer_size = times_2,
318   times_pointer_size = times_4,
319   times_twice_pointer_size = times_8
320 };
321 
322 
323 class Operand BASE_EMBEDDED {
324  public:
325   // reg
326   INLINE(explicit Operand(Register reg));
327 
328   // [disp/r]
329   INLINE(explicit Operand(int32_t disp, RelocInfo::Mode rmode));
330 
331   // [disp/r]
332   INLINE(explicit Operand(Immediate imm));
333 
334   // [base + disp/r]
335   explicit Operand(Register base, int32_t disp,
336                    RelocInfo::Mode rmode = RelocInfo::NONE32);
337 
338   // [base + index*scale + disp/r]
339   explicit Operand(Register base,
340                    Register index,
341                    ScaleFactor scale,
342                    int32_t disp,
343                    RelocInfo::Mode rmode = RelocInfo::NONE32);
344 
345   // [index*scale + disp/r]
346   explicit Operand(Register index,
347                    ScaleFactor scale,
348                    int32_t disp,
349                    RelocInfo::Mode rmode = RelocInfo::NONE32);
350 
JumpTable(Register index,ScaleFactor scale,Label * table)351   static Operand JumpTable(Register index, ScaleFactor scale, Label* table) {
352     return Operand(index, scale, reinterpret_cast<int32_t>(table),
353                    RelocInfo::INTERNAL_REFERENCE);
354   }
355 
StaticVariable(const ExternalReference & ext)356   static Operand StaticVariable(const ExternalReference& ext) {
357     return Operand(reinterpret_cast<int32_t>(ext.address()),
358                    RelocInfo::EXTERNAL_REFERENCE);
359   }
360 
StaticArray(Register index,ScaleFactor scale,const ExternalReference & arr)361   static Operand StaticArray(Register index,
362                              ScaleFactor scale,
363                              const ExternalReference& arr) {
364     return Operand(index, scale, reinterpret_cast<int32_t>(arr.address()),
365                    RelocInfo::EXTERNAL_REFERENCE);
366   }
367 
ForCell(Handle<Cell> cell)368   static Operand ForCell(Handle<Cell> cell) {
369     AllowDeferredHandleDereference embedding_raw_address;
370     return Operand(reinterpret_cast<int32_t>(cell.location()),
371                    RelocInfo::CELL);
372   }
373 
ForRegisterPlusImmediate(Register base,Immediate imm)374   static Operand ForRegisterPlusImmediate(Register base, Immediate imm) {
375     return Operand(base, imm.x_, imm.rmode_);
376   }
377 
378   // Returns true if this Operand is a wrapper for the specified register.
379   bool is_reg(Register reg) const;
380 
381   // Returns true if this Operand is a wrapper for one register.
382   bool is_reg_only() const;
383 
384   // Asserts that this Operand is a wrapper for one register and returns the
385   // register.
386   Register reg() const;
387 
388  private:
389   // Set the ModRM byte without an encoded 'reg' register. The
390   // register is encoded later as part of the emit_operand operation.
391   inline void set_modrm(int mod, Register rm);
392 
393   inline void set_sib(ScaleFactor scale, Register index, Register base);
394   inline void set_disp8(int8_t disp);
395   inline void set_dispr(int32_t disp, RelocInfo::Mode rmode);
396 
397   byte buf_[6];
398   // The number of bytes in buf_.
399   unsigned int len_;
400   // Only valid if len_ > 4.
401   RelocInfo::Mode rmode_;
402 
403   friend class Assembler;
404   friend class MacroAssembler;
405 };
406 
407 
408 // -----------------------------------------------------------------------------
409 // A Displacement describes the 32bit immediate field of an instruction which
410 // may be used together with a Label in order to refer to a yet unknown code
411 // position. Displacements stored in the instruction stream are used to describe
412 // the instruction and to chain a list of instructions using the same Label.
413 // A Displacement contains 2 different fields:
414 //
415 // next field: position of next displacement in the chain (0 = end of list)
416 // type field: instruction type
417 //
418 // A next value of null (0) indicates the end of a chain (note that there can
419 // be no displacement at position zero, because there is always at least one
420 // instruction byte before the displacement).
421 //
422 // Displacement _data field layout
423 //
424 // |31.....2|1......0|
425 // [  next  |  type  |
426 
427 class Displacement BASE_EMBEDDED {
428  public:
429   enum Type { UNCONDITIONAL_JUMP, CODE_RELATIVE, OTHER, CODE_ABSOLUTE };
430 
data()431   int data() const { return data_; }
type()432   Type type() const { return TypeField::decode(data_); }
next(Label * L)433   void next(Label* L) const {
434     int n = NextField::decode(data_);
435     n > 0 ? L->link_to(n) : L->Unuse();
436   }
link_to(Label * L)437   void link_to(Label* L) { init(L, type()); }
438 
Displacement(int data)439   explicit Displacement(int data) { data_ = data; }
440 
Displacement(Label * L,Type type)441   Displacement(Label* L, Type type) { init(L, type); }
442 
print()443   void print() {
444     PrintF("%s (%x) ", (type() == UNCONDITIONAL_JUMP ? "jmp" : "[other]"),
445                        NextField::decode(data_));
446   }
447 
448  private:
449   int data_;
450 
451   class TypeField: public BitField<Type, 0, 2> {};
452   class NextField: public BitField<int,  2, 32-2> {};
453 
454   void init(Label* L, Type type);
455 };
456 
457 
458 class Assembler : public AssemblerBase {
459  private:
460   // We check before assembling an instruction that there is sufficient
461   // space to write an instruction and its relocation information.
462   // The relocation writer's position must be kGap bytes above the end of
463   // the generated instructions. This leaves enough space for the
464   // longest possible ia32 instruction, 15 bytes, and the longest possible
465   // relocation information encoding, RelocInfoWriter::kMaxLength == 16.
466   // (There is a 15 byte limit on ia32 instruction length that rules out some
467   // otherwise valid instructions.)
468   // This allows for a single, fast space check per instruction.
469   static const int kGap = 32;
470 
471  public:
472   // Create an assembler. Instructions and relocation information are emitted
473   // into a buffer, with the instructions starting from the beginning and the
474   // relocation information starting from the end of the buffer. See CodeDesc
475   // for a detailed comment on the layout (globals.h).
476   //
477   // If the provided buffer is NULL, the assembler allocates and grows its own
478   // buffer, and buffer_size determines the initial buffer size. The buffer is
479   // owned by the assembler and deallocated upon destruction of the assembler.
480   //
481   // If the provided buffer is not NULL, the assembler uses the provided buffer
482   // for code generation and assumes its size to be buffer_size. If the buffer
483   // is too small, a fatal error occurs. No deallocation of the buffer is done
484   // upon destruction of the assembler.
485   // TODO(vitalyr): the assembler does not need an isolate.
486   Assembler(Isolate* isolate, void* buffer, int buffer_size);
~Assembler()487   virtual ~Assembler() { }
488 
489   // GetCode emits any pending (non-emitted) code and fills the descriptor
490   // desc. GetCode() is idempotent; it returns the same result if no other
491   // Assembler functions are invoked in between GetCode() calls.
492   void GetCode(CodeDesc* desc);
493 
494   // Read/Modify the code target in the branch/call instruction at pc.
495   inline static Address target_address_at(Address pc, Address constant_pool);
496   inline static void set_target_address_at(
497       Isolate* isolate, Address pc, Address constant_pool, Address target,
498       ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED);
target_address_at(Address pc,Code * code)499   static inline Address target_address_at(Address pc, Code* code) {
500     Address constant_pool = code ? code->constant_pool() : NULL;
501     return target_address_at(pc, constant_pool);
502   }
503   static inline void set_target_address_at(
504       Isolate* isolate, Address pc, Code* code, Address target,
505       ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED) {
506     Address constant_pool = code ? code->constant_pool() : NULL;
507     set_target_address_at(isolate, pc, constant_pool, target);
508   }
509 
510   // Return the code target address at a call site from the return address
511   // of that call in the instruction stream.
512   inline static Address target_address_from_return_address(Address pc);
513 
514   // This sets the branch destination (which is in the instruction on x86).
515   // This is for calls and branches within generated code.
deserialization_set_special_target_at(Isolate * isolate,Address instruction_payload,Code * code,Address target)516   inline static void deserialization_set_special_target_at(
517       Isolate* isolate, Address instruction_payload, Code* code,
518       Address target) {
519     set_target_address_at(isolate, instruction_payload, code, target);
520   }
521 
522   // This sets the internal reference at the pc.
523   inline static void deserialization_set_target_internal_reference_at(
524       Isolate* isolate, Address pc, Address target,
525       RelocInfo::Mode mode = RelocInfo::INTERNAL_REFERENCE);
526 
527   static const int kSpecialTargetSize = kPointerSize;
528 
529   // Distance between the address of the code target in the call instruction
530   // and the return address
531   static const int kCallTargetAddressOffset = kPointerSize;
532 
533   static const int kCallInstructionLength = 5;
534 
535   // The debug break slot must be able to contain a call instruction.
536   static const int kDebugBreakSlotLength = kCallInstructionLength;
537 
538   // Distance between start of patched debug break slot and the emitted address
539   // to jump to.
540   static const int kPatchDebugBreakSlotAddressOffset = 1;  // JMP imm32.
541 
542   // One byte opcode for test al, 0xXX.
543   static const byte kTestAlByte = 0xA8;
544   // One byte opcode for nop.
545   static const byte kNopByte = 0x90;
546 
547   // One byte opcode for a short unconditional jump.
548   static const byte kJmpShortOpcode = 0xEB;
549   // One byte prefix for a short conditional jump.
550   static const byte kJccShortPrefix = 0x70;
551   static const byte kJncShortOpcode = kJccShortPrefix | not_carry;
552   static const byte kJcShortOpcode = kJccShortPrefix | carry;
553   static const byte kJnzShortOpcode = kJccShortPrefix | not_zero;
554   static const byte kJzShortOpcode = kJccShortPrefix | zero;
555 
556 
557   // ---------------------------------------------------------------------------
558   // Code generation
559   //
560   // - function names correspond one-to-one to ia32 instruction mnemonics
561   // - unless specified otherwise, instructions operate on 32bit operands
562   // - instructions on 8bit (byte) operands/registers have a trailing '_b'
563   // - instructions on 16bit (word) operands/registers have a trailing '_w'
564   // - naming conflicts with C++ keywords are resolved via a trailing '_'
565 
566   // NOTE ON INTERFACE: Currently, the interface is not very consistent
567   // in the sense that some operations (e.g. mov()) can be called in more
568   // the one way to generate the same instruction: The Register argument
569   // can in some cases be replaced with an Operand(Register) argument.
570   // This should be cleaned up and made more orthogonal. The questions
571   // is: should we always use Operands instead of Registers where an
572   // Operand is possible, or should we have a Register (overloaded) form
573   // instead? We must be careful to make sure that the selected instruction
574   // is obvious from the parameters to avoid hard-to-find code generation
575   // bugs.
576 
577   // Insert the smallest number of nop instructions
578   // possible to align the pc offset to a multiple
579   // of m. m must be a power of 2.
580   void Align(int m);
581   // Insert the smallest number of zero bytes possible to align the pc offset
582   // to a mulitple of m. m must be a power of 2 (>= 2).
583   void DataAlign(int m);
584   void Nop(int bytes = 1);
585   // Aligns code to something that's optimal for a jump target for the platform.
586   void CodeTargetAlign();
587 
588   // Stack
589   void pushad();
590   void popad();
591 
592   void pushfd();
593   void popfd();
594 
595   void push(const Immediate& x);
596   void push_imm32(int32_t imm32);
597   void push(Register src);
598   void push(const Operand& src);
599 
600   void pop(Register dst);
601   void pop(const Operand& dst);
602 
603   void enter(const Immediate& size);
604   void leave();
605 
606   // Moves
mov_b(Register dst,Register src)607   void mov_b(Register dst, Register src) { mov_b(dst, Operand(src)); }
608   void mov_b(Register dst, const Operand& src);
mov_b(Register dst,int8_t imm8)609   void mov_b(Register dst, int8_t imm8) { mov_b(Operand(dst), imm8); }
610   void mov_b(const Operand& dst, int8_t imm8);
611   void mov_b(const Operand& dst, const Immediate& src);
612   void mov_b(const Operand& dst, Register src);
613 
614   void mov_w(Register dst, const Operand& src);
615   void mov_w(const Operand& dst, Register src);
616   void mov_w(const Operand& dst, int16_t imm16);
617   void mov_w(const Operand& dst, const Immediate& src);
618 
619 
620   void mov(Register dst, int32_t imm32);
621   void mov(Register dst, const Immediate& x);
622   void mov(Register dst, Handle<Object> handle);
623   void mov(Register dst, const Operand& src);
624   void mov(Register dst, Register src);
625   void mov(const Operand& dst, const Immediate& x);
626   void mov(const Operand& dst, Handle<Object> handle);
627   void mov(const Operand& dst, Register src);
628 
movsx_b(Register dst,Register src)629   void movsx_b(Register dst, Register src) { movsx_b(dst, Operand(src)); }
630   void movsx_b(Register dst, const Operand& src);
631 
movsx_w(Register dst,Register src)632   void movsx_w(Register dst, Register src) { movsx_w(dst, Operand(src)); }
633   void movsx_w(Register dst, const Operand& src);
634 
movzx_b(Register dst,Register src)635   void movzx_b(Register dst, Register src) { movzx_b(dst, Operand(src)); }
636   void movzx_b(Register dst, const Operand& src);
637 
movzx_w(Register dst,Register src)638   void movzx_w(Register dst, Register src) { movzx_w(dst, Operand(src)); }
639   void movzx_w(Register dst, const Operand& src);
640 
641   // Flag management.
642   void cld();
643 
644   // Repetitive string instructions.
645   void rep_movs();
646   void rep_stos();
647   void stos();
648 
649   // Exchange
650   void xchg(Register dst, Register src);
651   void xchg(Register dst, const Operand& src);
652   void xchg_b(Register reg, const Operand& op);
653   void xchg_w(Register reg, const Operand& op);
654 
655   // Lock prefix
656   void lock();
657 
658   // CompareExchange
659   void cmpxchg(const Operand& dst, Register src);
660   void cmpxchg_b(const Operand& dst, Register src);
661   void cmpxchg_w(const Operand& dst, Register src);
662 
663   // Arithmetics
664   void adc(Register dst, int32_t imm32);
665   void adc(Register dst, const Operand& src);
666 
add(Register dst,Register src)667   void add(Register dst, Register src) { add(dst, Operand(src)); }
668   void add(Register dst, const Operand& src);
669   void add(const Operand& dst, Register src);
add(Register dst,const Immediate & imm)670   void add(Register dst, const Immediate& imm) { add(Operand(dst), imm); }
671   void add(const Operand& dst, const Immediate& x);
672 
673   void and_(Register dst, int32_t imm32);
674   void and_(Register dst, const Immediate& x);
and_(Register dst,Register src)675   void and_(Register dst, Register src) { and_(dst, Operand(src)); }
676   void and_(Register dst, const Operand& src);
677   void and_(const Operand& dst, Register src);
678   void and_(const Operand& dst, const Immediate& x);
679 
cmpb(Register reg,Immediate imm8)680   void cmpb(Register reg, Immediate imm8) { cmpb(Operand(reg), imm8); }
681   void cmpb(const Operand& op, Immediate imm8);
682   void cmpb(Register reg, const Operand& op);
683   void cmpb(const Operand& op, Register reg);
cmpb(Register dst,Register src)684   void cmpb(Register dst, Register src) { cmpb(Operand(dst), src); }
685   void cmpb_al(const Operand& op);
686   void cmpw_ax(const Operand& op);
687   void cmpw(const Operand& dst, Immediate src);
cmpw(Register dst,Immediate src)688   void cmpw(Register dst, Immediate src) { cmpw(Operand(dst), src); }
689   void cmpw(Register dst, const Operand& src);
cmpw(Register dst,Register src)690   void cmpw(Register dst, Register src) { cmpw(Operand(dst), src); }
691   void cmpw(const Operand& dst, Register src);
692   void cmp(Register reg, int32_t imm32);
693   void cmp(Register reg, Handle<Object> handle);
cmp(Register reg0,Register reg1)694   void cmp(Register reg0, Register reg1) { cmp(reg0, Operand(reg1)); }
695   void cmp(Register reg, const Operand& op);
cmp(Register reg,const Immediate & imm)696   void cmp(Register reg, const Immediate& imm) { cmp(Operand(reg), imm); }
697   void cmp(const Operand& op, Register reg);
698   void cmp(const Operand& op, const Immediate& imm);
699   void cmp(const Operand& op, Handle<Object> handle);
700 
701   void dec_b(Register dst);
702   void dec_b(const Operand& dst);
703 
704   void dec(Register dst);
705   void dec(const Operand& dst);
706 
707   void cdq();
708 
idiv(Register src)709   void idiv(Register src) { idiv(Operand(src)); }
710   void idiv(const Operand& src);
div(Register src)711   void div(Register src) { div(Operand(src)); }
712   void div(const Operand& src);
713 
714   // Signed multiply instructions.
715   void imul(Register src);                               // edx:eax = eax * src.
imul(Register dst,Register src)716   void imul(Register dst, Register src) { imul(dst, Operand(src)); }
717   void imul(Register dst, const Operand& src);           // dst = dst * src.
718   void imul(Register dst, Register src, int32_t imm32);  // dst = src * imm32.
719   void imul(Register dst, const Operand& src, int32_t imm32);
720 
721   void inc(Register dst);
722   void inc(const Operand& dst);
723 
724   void lea(Register dst, const Operand& src);
725 
726   // Unsigned multiply instruction.
727   void mul(Register src);                                // edx:eax = eax * reg.
728 
729   void neg(Register dst);
730   void neg(const Operand& dst);
731 
732   void not_(Register dst);
733   void not_(const Operand& dst);
734 
735   void or_(Register dst, int32_t imm32);
or_(Register dst,Register src)736   void or_(Register dst, Register src) { or_(dst, Operand(src)); }
737   void or_(Register dst, const Operand& src);
738   void or_(const Operand& dst, Register src);
or_(Register dst,const Immediate & imm)739   void or_(Register dst, const Immediate& imm) { or_(Operand(dst), imm); }
740   void or_(const Operand& dst, const Immediate& x);
741 
742   void rcl(Register dst, uint8_t imm8);
743   void rcr(Register dst, uint8_t imm8);
744 
ror(Register dst,uint8_t imm8)745   void ror(Register dst, uint8_t imm8) { ror(Operand(dst), imm8); }
746   void ror(const Operand& dst, uint8_t imm8);
ror_cl(Register dst)747   void ror_cl(Register dst) { ror_cl(Operand(dst)); }
748   void ror_cl(const Operand& dst);
749 
sar(Register dst,uint8_t imm8)750   void sar(Register dst, uint8_t imm8) { sar(Operand(dst), imm8); }
751   void sar(const Operand& dst, uint8_t imm8);
sar_cl(Register dst)752   void sar_cl(Register dst) { sar_cl(Operand(dst)); }
753   void sar_cl(const Operand& dst);
754 
755   void sbb(Register dst, const Operand& src);
756 
shl(Register dst,uint8_t imm8)757   void shl(Register dst, uint8_t imm8) { shl(Operand(dst), imm8); }
758   void shl(const Operand& dst, uint8_t imm8);
shl_cl(Register dst)759   void shl_cl(Register dst) { shl_cl(Operand(dst)); }
760   void shl_cl(const Operand& dst);
761   void shld(Register dst, Register src, uint8_t shift);
762   void shld_cl(Register dst, Register src);
763 
shr(Register dst,uint8_t imm8)764   void shr(Register dst, uint8_t imm8) { shr(Operand(dst), imm8); }
765   void shr(const Operand& dst, uint8_t imm8);
shr_cl(Register dst)766   void shr_cl(Register dst) { shr_cl(Operand(dst)); }
767   void shr_cl(const Operand& dst);
768   void shrd(Register dst, Register src, uint8_t shift);
shrd_cl(Register dst,Register src)769   void shrd_cl(Register dst, Register src) { shrd_cl(Operand(dst), src); }
770   void shrd_cl(const Operand& dst, Register src);
771 
sub(Register dst,const Immediate & imm)772   void sub(Register dst, const Immediate& imm) { sub(Operand(dst), imm); }
773   void sub(const Operand& dst, const Immediate& x);
sub(Register dst,Register src)774   void sub(Register dst, Register src) { sub(dst, Operand(src)); }
775   void sub(Register dst, const Operand& src);
776   void sub(const Operand& dst, Register src);
777 
778   void test(Register reg, const Immediate& imm);
test(Register reg0,Register reg1)779   void test(Register reg0, Register reg1) { test(reg0, Operand(reg1)); }
780   void test(Register reg, const Operand& op);
781   void test(const Operand& op, const Immediate& imm);
test(const Operand & op,Register reg)782   void test(const Operand& op, Register reg) { test(reg, op); }
783   void test_b(Register reg, const Operand& op);
784   void test_b(Register reg, Immediate imm8);
785   void test_b(const Operand& op, Immediate imm8);
test_b(const Operand & op,Register reg)786   void test_b(const Operand& op, Register reg) { test_b(reg, op); }
test_b(Register dst,Register src)787   void test_b(Register dst, Register src) { test_b(dst, Operand(src)); }
788   void test_w(Register reg, const Operand& op);
789   void test_w(Register reg, Immediate imm16);
790   void test_w(const Operand& op, Immediate imm16);
test_w(const Operand & op,Register reg)791   void test_w(const Operand& op, Register reg) { test_w(reg, op); }
test_w(Register dst,Register src)792   void test_w(Register dst, Register src) { test_w(dst, Operand(src)); }
793 
794   void xor_(Register dst, int32_t imm32);
xor_(Register dst,Register src)795   void xor_(Register dst, Register src) { xor_(dst, Operand(src)); }
796   void xor_(Register dst, const Operand& src);
797   void xor_(const Operand& dst, Register src);
xor_(Register dst,const Immediate & imm)798   void xor_(Register dst, const Immediate& imm) { xor_(Operand(dst), imm); }
799   void xor_(const Operand& dst, const Immediate& x);
800 
801   // Bit operations.
802   void bt(const Operand& dst, Register src);
bts(Register dst,Register src)803   void bts(Register dst, Register src) { bts(Operand(dst), src); }
804   void bts(const Operand& dst, Register src);
bsr(Register dst,Register src)805   void bsr(Register dst, Register src) { bsr(dst, Operand(src)); }
806   void bsr(Register dst, const Operand& src);
bsf(Register dst,Register src)807   void bsf(Register dst, Register src) { bsf(dst, Operand(src)); }
808   void bsf(Register dst, const Operand& src);
809 
810   // Miscellaneous
811   void hlt();
812   void int3();
813   void nop();
814   void ret(int imm16);
815   void ud2();
816 
817   // Label operations & relative jumps (PPUM Appendix D)
818   //
819   // Takes a branch opcode (cc) and a label (L) and generates
820   // either a backward branch or a forward branch and links it
821   // to the label fixup chain. Usage:
822   //
823   // Label L;    // unbound label
824   // j(cc, &L);  // forward branch to unbound label
825   // bind(&L);   // bind label to the current pc
826   // j(cc, &L);  // backward branch to bound label
827   // bind(&L);   // illegal: a label may be bound only once
828   //
829   // Note: The same Label can be used for forward and backward branches
830   // but it may be bound only once.
831 
832   void bind(Label* L);  // binds an unbound label L to the current code position
833 
834   // Calls
835   void call(Label* L);
836   void call(byte* entry, RelocInfo::Mode rmode);
837   int CallSize(const Operand& adr);
call(Register reg)838   void call(Register reg) { call(Operand(reg)); }
839   void call(const Operand& adr);
840   int CallSize(Handle<Code> code, RelocInfo::Mode mode);
841   void call(Handle<Code> code,
842             RelocInfo::Mode rmode,
843             TypeFeedbackId id = TypeFeedbackId::None());
844 
845   // Jumps
846   // unconditional jump to L
847   void jmp(Label* L, Label::Distance distance = Label::kFar);
848   void jmp(byte* entry, RelocInfo::Mode rmode);
jmp(Register reg)849   void jmp(Register reg) { jmp(Operand(reg)); }
850   void jmp(const Operand& adr);
851   void jmp(Handle<Code> code, RelocInfo::Mode rmode);
852 
853   // Conditional jumps
854   void j(Condition cc,
855          Label* L,
856          Label::Distance distance = Label::kFar);
857   void j(Condition cc, byte* entry, RelocInfo::Mode rmode);
858   void j(Condition cc, Handle<Code> code,
859          RelocInfo::Mode rmode = RelocInfo::CODE_TARGET);
860 
861   // Floating-point operations
862   void fld(int i);
863   void fstp(int i);
864 
865   void fld1();
866   void fldz();
867   void fldpi();
868   void fldln2();
869 
870   void fld_s(const Operand& adr);
871   void fld_d(const Operand& adr);
872 
873   void fstp_s(const Operand& adr);
874   void fst_s(const Operand& adr);
875   void fstp_d(const Operand& adr);
876   void fst_d(const Operand& adr);
877 
878   void fild_s(const Operand& adr);
879   void fild_d(const Operand& adr);
880 
881   void fist_s(const Operand& adr);
882 
883   void fistp_s(const Operand& adr);
884   void fistp_d(const Operand& adr);
885 
886   // The fisttp instructions require SSE3.
887   void fisttp_s(const Operand& adr);
888   void fisttp_d(const Operand& adr);
889 
890   void fabs();
891   void fchs();
892   void fsqrt();
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 fadd_d(const Operand& adr);
904   void fsub(int i);
905   void fsub_i(int i);
906   void fsub_d(const Operand& adr);
907   void fsubr_d(const Operand& adr);
908   void fmul(int i);
909   void fmul_d(const Operand& adr);
910   void fmul_i(int i);
911   void fdiv(int i);
912   void fdiv_d(const Operand& adr);
913   void fdivr_d(const Operand& adr);
914   void fdiv_i(int i);
915 
916   void fisub_s(const Operand& adr);
917 
918   void faddp(int i = 1);
919   void fsubp(int i = 1);
920   void fsubr(int i = 1);
921   void fsubrp(int i = 1);
922   void fmulp(int i = 1);
923   void fdivp(int i = 1);
924   void fprem();
925   void fprem1();
926 
927   void fxch(int i = 1);
928   void fincstp();
929   void ffree(int i = 0);
930 
931   void ftst();
932   void fxam();
933   void fucomp(int i);
934   void fucompp();
935   void fucomi(int i);
936   void fucomip();
937   void fcompp();
938   void fnstsw_ax();
939   void fldcw(const Operand& adr);
940   void fnstcw(const Operand& adr);
941   void fwait();
942   void fnclex();
943   void fnsave(const Operand& adr);
944   void frstor(const Operand& adr);
945 
946   void frndint();
947 
948   void sahf();
949   void setcc(Condition cc, Register reg);
950 
951   void cpuid();
952 
953   // TODO(lrn): Need SFENCE for movnt?
954 
955   // Check the code size generated from label to here.
SizeOfCodeGeneratedSince(Label * label)956   int SizeOfCodeGeneratedSince(Label* label) {
957     return pc_offset() - label->pos();
958   }
959 
960   // Mark generator continuation.
961   void RecordGeneratorContinuation();
962 
963   // Mark address of a debug break slot.
964   void RecordDebugBreakSlot(RelocInfo::Mode mode);
965 
966   // Record a comment relocation entry that can be used by a disassembler.
967   // Use --code-comments to enable.
968   void RecordComment(const char* msg);
969 
970   // Record a deoptimization reason that can be used by a log or cpu profiler.
971   // Use --trace-deopt to enable.
972   void RecordDeoptReason(DeoptimizeReason reason, SourcePosition position,
973                          int id);
974 
975   // Writes a single byte or word of data in the code stream.  Used for
976   // inline tables, e.g., jump-tables.
977   void db(uint8_t data);
978   void dd(uint32_t data);
979   void dq(uint64_t data);
dp(uintptr_t data)980   void dp(uintptr_t data) { dd(data); }
981   void dd(Label* label);
982 
983   // Check if there is less than kGap bytes available in the buffer.
984   // If this is the case, we need to grow the buffer before emitting
985   // an instruction or relocation information.
buffer_overflow()986   inline bool buffer_overflow() const {
987     return pc_ >= reloc_info_writer.pos() - kGap;
988   }
989 
990   // Get the number of bytes available in the buffer.
available_space()991   inline int available_space() const { return reloc_info_writer.pos() - pc_; }
992 
993   static bool IsNop(Address addr);
994 
relocation_writer_size()995   int relocation_writer_size() {
996     return (buffer_ + buffer_size_) - reloc_info_writer.pos();
997   }
998 
999   // Avoid overflows for displacements etc.
1000   static const int kMaximalBufferSize = 512*MB;
1001 
byte_at(int pos)1002   byte byte_at(int pos) { return buffer_[pos]; }
set_byte_at(int pos,byte value)1003   void set_byte_at(int pos, byte value) { buffer_[pos] = value; }
1004 
PatchConstantPoolAccessInstruction(int pc_offset,int offset,ConstantPoolEntry::Access access,ConstantPoolEntry::Type type)1005   void PatchConstantPoolAccessInstruction(int pc_offset, int offset,
1006                                           ConstantPoolEntry::Access access,
1007                                           ConstantPoolEntry::Type type) {
1008     // No embedded constant pool support.
1009     UNREACHABLE();
1010   }
1011 
1012  protected:
addr_at(int pos)1013   byte* addr_at(int pos) { return buffer_ + pos; }
1014 
1015 
1016  private:
long_at(int pos)1017   uint32_t long_at(int pos)  {
1018     return *reinterpret_cast<uint32_t*>(addr_at(pos));
1019   }
long_at_put(int pos,uint32_t x)1020   void long_at_put(int pos, uint32_t x)  {
1021     *reinterpret_cast<uint32_t*>(addr_at(pos)) = x;
1022   }
1023 
1024   // code emission
1025   void GrowBuffer();
1026   inline void emit(uint32_t x);
1027   inline void emit(Handle<Object> handle);
1028   inline void emit(uint32_t x,
1029                    RelocInfo::Mode rmode,
1030                    TypeFeedbackId id = TypeFeedbackId::None());
1031   inline void emit(Handle<Code> code,
1032                    RelocInfo::Mode rmode,
1033                    TypeFeedbackId id = TypeFeedbackId::None());
1034   inline void emit(const Immediate& x);
1035   inline void emit_b(Immediate x);
1036   inline void emit_w(const Immediate& x);
1037   inline void emit_q(uint64_t x);
1038 
1039   // Emit the code-object-relative offset of the label's position
1040   inline void emit_code_relative_offset(Label* label);
1041 
1042   // instruction generation
1043   void emit_arith_b(int op1, int op2, Register dst, int imm8);
1044 
1045   // Emit a basic arithmetic instruction (i.e. first byte of the family is 0x81)
1046   // with a given destination expression and an immediate operand.  It attempts
1047   // to use the shortest encoding possible.
1048   // sel specifies the /n in the modrm byte (see the Intel PRM).
1049   void emit_arith(int sel, Operand dst, const Immediate& x);
1050 
1051   void emit_operand(Register reg, const Operand& adr);
1052 
1053   void emit_label(Label* label);
1054 
1055   void emit_farith(int b1, int b2, int i);
1056 
1057   // labels
1058   void print(Label* L);
1059   void bind_to(Label* L, int pos);
1060 
1061   // displacements
1062   inline Displacement disp_at(Label* L);
1063   inline void disp_at_put(Label* L, Displacement disp);
1064   inline void emit_disp(Label* L, Displacement::Type type);
1065   inline void emit_near_disp(Label* L);
1066 
1067   // record reloc info for current pc_
1068   void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
1069 
1070   friend class CodePatcher;
1071   friend class EnsureSpace;
1072 
1073   // Internal reference positions, required for (potential) patching in
1074   // GrowBuffer(); contains only those internal references whose labels
1075   // are already bound.
1076   std::deque<int> internal_reference_positions_;
1077 
1078   // code generation
1079   RelocInfoWriter reloc_info_writer;
1080 };
1081 
1082 
1083 // Helper class that ensures that there is enough space for generating
1084 // instructions and relocation information.  The constructor makes
1085 // sure that there is enough space and (in debug mode) the destructor
1086 // checks that we did not generate too much.
1087 class EnsureSpace BASE_EMBEDDED {
1088  public:
EnsureSpace(Assembler * assembler)1089   explicit EnsureSpace(Assembler* assembler) : assembler_(assembler) {
1090     if (assembler_->buffer_overflow()) assembler_->GrowBuffer();
1091 #ifdef DEBUG
1092     space_before_ = assembler_->available_space();
1093 #endif
1094   }
1095 
1096 #ifdef DEBUG
~EnsureSpace()1097   ~EnsureSpace() {
1098     int bytes_generated = space_before_ - assembler_->available_space();
1099     DCHECK(bytes_generated < assembler_->kGap);
1100   }
1101 #endif
1102 
1103  private:
1104   Assembler* assembler_;
1105 #ifdef DEBUG
1106   int space_before_;
1107 #endif
1108 };
1109 
1110 }  // namespace internal
1111 }  // namespace v8
1112 
1113 #endif  // V8_X87_ASSEMBLER_X87_H_
1114