1 // Copyright (c) 1994-2006 Sun Microsystems Inc.
2 // All Rights Reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // - Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
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.
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.
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.
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.
35 // A light-weight IA32 Assembler.
37 #ifndef V8_X87_ASSEMBLER_X87_H_
38 #define V8_X87_ASSEMBLER_X87_H_
42 #include "src/assembler.h"
43 #include "src/compiler.h"
44 #include "src/isolate.h"
51 // 1) We would prefer to use an enum, but enum values are assignment-
52 // compatible with int, which has caused code-generation bugs.
54 // 2) We would prefer to use a class instead of a struct but we don't like
55 // the register initialization to depend on the particular initialization
56 // order (which appears to be different on OS X, Linux, and Windows for the
57 // installed versions of C++ we tried). Using a struct permits C-style
58 // "initialization". Also, the Register objects cannot be const as this
59 // forces initialization stubs in MSVC, making us dependent on initialization
62 // 3) By not using an enum, we are possibly preventing the compiler from
63 // doing certain constant folds, which may significantly reduce the
64 // code generated for some assembly instructions (because they boil down
65 // to a few constants). If this is a problem, we could change the code
66 // such that we use an enum in optimized mode, and the struct in debug
67 // mode. This way we get the compile-time error checking in debug mode
68 // and best performance in optimized code.
71 static const int kMaxNumAllocatableRegisters = 6;
72 static int NumAllocatableRegisters() {
73 return kMaxNumAllocatableRegisters;
75 static const int kNumRegisters = 8;
77 static inline const char* AllocationIndexToString(int index);
79 static inline int ToAllocationIndex(Register reg);
81 static inline Register FromAllocationIndex(int index);
83 static Register from_code(int code) {
85 DCHECK(code < kNumRegisters);
86 Register r = { code };
89 bool is_valid() const { return 0 <= code_ && code_ < kNumRegisters; }
90 bool is(Register reg) const { return code_ == reg.code_; }
91 // eax, ebx, ecx and edx are byte registers, the rest are not.
92 bool is_byte_register() const { return code_ <= 3; }
102 // Unfortunately we can't make this private in a struct.
106 const int kRegister_eax_Code = 0;
107 const int kRegister_ecx_Code = 1;
108 const int kRegister_edx_Code = 2;
109 const int kRegister_ebx_Code = 3;
110 const int kRegister_esp_Code = 4;
111 const int kRegister_ebp_Code = 5;
112 const int kRegister_esi_Code = 6;
113 const int kRegister_edi_Code = 7;
114 const int kRegister_no_reg_Code = -1;
116 const Register eax = { kRegister_eax_Code };
117 const Register ecx = { kRegister_ecx_Code };
118 const Register edx = { kRegister_edx_Code };
119 const Register ebx = { kRegister_ebx_Code };
120 const Register esp = { kRegister_esp_Code };
121 const Register ebp = { kRegister_ebp_Code };
122 const Register esi = { kRegister_esi_Code };
123 const Register edi = { kRegister_edi_Code };
124 const Register no_reg = { kRegister_no_reg_Code };
127 inline const char* Register::AllocationIndexToString(int index) {
128 DCHECK(index >= 0 && index < kMaxNumAllocatableRegisters);
129 // This is the mapping of allocation indices to registers.
130 const char* const kNames[] = { "eax", "ecx", "edx", "ebx", "esi", "edi" };
131 return kNames[index];
135 inline int Register::ToAllocationIndex(Register reg) {
136 DCHECK(reg.is_valid() && !reg.is(esp) && !reg.is(ebp));
137 return (reg.code() >= 6) ? reg.code() - 2 : reg.code();
141 inline Register Register::FromAllocationIndex(int index) {
142 DCHECK(index >= 0 && index < kMaxNumAllocatableRegisters);
143 return (index >= 4) ? from_code(index + 2) : from_code(index);
148 static const int kMaxNumAllocatableRegisters = 6;
149 static const int kMaxNumRegisters = 8;
150 static int NumAllocatableRegisters() {
151 return kMaxNumAllocatableRegisters;
155 // TODO(turbofan): Proper support for float32.
156 static int NumAllocatableAliasedRegisters() {
157 return NumAllocatableRegisters();
161 static int ToAllocationIndex(X87Register reg) {
165 static const char* AllocationIndexToString(int index) {
166 DCHECK(index >= 0 && index < kMaxNumAllocatableRegisters);
167 const char* const names[] = {
168 "stX_0", "stX_1", "stX_2", "stX_3", "stX_4",
169 "stX_5", "stX_6", "stX_7"
174 static X87Register FromAllocationIndex(int index) {
175 DCHECK(index >= 0 && index < kMaxNumAllocatableRegisters);
177 result.code_ = index;
181 bool is_valid() const {
182 return 0 <= code_ && code_ < kMaxNumRegisters;
190 bool is(X87Register reg) const {
191 return code_ == reg.code_;
198 typedef X87Register DoubleRegister;
201 const X87Register stX_0 = { 0 };
202 const X87Register stX_1 = { 1 };
203 const X87Register stX_2 = { 2 };
204 const X87Register stX_3 = { 3 };
205 const X87Register stX_4 = { 4 };
206 const X87Register stX_5 = { 5 };
207 const X87Register stX_6 = { 6 };
208 const X87Register stX_7 = { 7 };
212 // any value < 0 is considered no_condition
234 not_carry = above_equal,
236 not_zero = not_equal,
242 // Returns the equivalent of !cc.
243 // Negation of the default no_condition (-1) results in a non-default
244 // no_condition value (-2). As long as tests for no_condition check
245 // for condition < 0, this will work as expected.
246 inline Condition NegateCondition(Condition cc) {
247 return static_cast<Condition>(cc ^ 1);
251 // Commute a condition such that {a cond b == b cond' a}.
252 inline Condition CommuteCondition(Condition cc) {
269 return greater_equal;
277 kRoundToNearest = 0x0,
284 // -----------------------------------------------------------------------------
285 // Machine instruction Immediates
287 class Immediate BASE_EMBEDDED {
289 inline explicit Immediate(int x);
290 inline explicit Immediate(const ExternalReference& ext);
291 inline explicit Immediate(Handle<Object> handle);
292 inline explicit Immediate(Smi* value);
293 inline explicit Immediate(Address addr);
295 static Immediate CodeRelativeOffset(Label* label) {
296 return Immediate(label);
299 bool is_zero() const { return x_ == 0 && RelocInfo::IsNone(rmode_); }
300 bool is_int8() const {
301 return -128 <= x_ && x_ < 128 && RelocInfo::IsNone(rmode_);
303 bool is_int16() const {
304 return -32768 <= x_ && x_ < 32768 && RelocInfo::IsNone(rmode_);
308 inline explicit Immediate(Label* value);
311 RelocInfo::Mode rmode_;
313 friend class Operand;
314 friend class Assembler;
315 friend class MacroAssembler;
319 // -----------------------------------------------------------------------------
320 // Machine instruction Operands
327 times_int_size = times_4,
328 times_half_pointer_size = times_2,
329 times_pointer_size = times_4,
330 times_twice_pointer_size = times_8
334 class Operand BASE_EMBEDDED {
337 INLINE(explicit Operand(Register reg));
340 INLINE(explicit Operand(int32_t disp, RelocInfo::Mode rmode));
343 INLINE(explicit Operand(Immediate imm));
346 explicit Operand(Register base, int32_t disp,
347 RelocInfo::Mode rmode = RelocInfo::NONE32);
349 // [base + index*scale + disp/r]
350 explicit Operand(Register base,
354 RelocInfo::Mode rmode = RelocInfo::NONE32);
356 // [index*scale + disp/r]
357 explicit Operand(Register index,
360 RelocInfo::Mode rmode = RelocInfo::NONE32);
362 static Operand JumpTable(Register index, ScaleFactor scale, Label* table) {
363 return Operand(index, scale, reinterpret_cast<int32_t>(table),
364 RelocInfo::INTERNAL_REFERENCE);
367 static Operand StaticVariable(const ExternalReference& ext) {
368 return Operand(reinterpret_cast<int32_t>(ext.address()),
369 RelocInfo::EXTERNAL_REFERENCE);
372 static Operand StaticArray(Register index,
374 const ExternalReference& arr) {
375 return Operand(index, scale, reinterpret_cast<int32_t>(arr.address()),
376 RelocInfo::EXTERNAL_REFERENCE);
379 static Operand ForCell(Handle<Cell> cell) {
380 AllowDeferredHandleDereference embedding_raw_address;
381 return Operand(reinterpret_cast<int32_t>(cell.location()),
385 static Operand ForRegisterPlusImmediate(Register base, Immediate imm) {
386 return Operand(base, imm.x_, imm.rmode_);
389 // Returns true if this Operand is a wrapper for the specified register.
390 bool is_reg(Register reg) const;
392 // Returns true if this Operand is a wrapper for one register.
393 bool is_reg_only() const;
395 // Asserts that this Operand is a wrapper for one register and returns the
397 Register reg() const;
400 // Set the ModRM byte without an encoded 'reg' register. The
401 // register is encoded later as part of the emit_operand operation.
402 inline void set_modrm(int mod, Register rm);
404 inline void set_sib(ScaleFactor scale, Register index, Register base);
405 inline void set_disp8(int8_t disp);
406 inline void set_dispr(int32_t disp, RelocInfo::Mode rmode);
409 // The number of bytes in buf_.
411 // Only valid if len_ > 4.
412 RelocInfo::Mode rmode_;
414 friend class Assembler;
415 friend class MacroAssembler;
419 // -----------------------------------------------------------------------------
420 // A Displacement describes the 32bit immediate field of an instruction which
421 // may be used together with a Label in order to refer to a yet unknown code
422 // position. Displacements stored in the instruction stream are used to describe
423 // the instruction and to chain a list of instructions using the same Label.
424 // A Displacement contains 2 different fields:
426 // next field: position of next displacement in the chain (0 = end of list)
427 // type field: instruction type
429 // A next value of null (0) indicates the end of a chain (note that there can
430 // be no displacement at position zero, because there is always at least one
431 // instruction byte before the displacement).
433 // Displacement _data field layout
435 // |31.....2|1......0|
438 class Displacement BASE_EMBEDDED {
440 enum Type { UNCONDITIONAL_JUMP, CODE_RELATIVE, OTHER, CODE_ABSOLUTE };
442 int data() const { return data_; }
443 Type type() const { return TypeField::decode(data_); }
444 void next(Label* L) const {
445 int n = NextField::decode(data_);
446 n > 0 ? L->link_to(n) : L->Unuse();
448 void link_to(Label* L) { init(L, type()); }
450 explicit Displacement(int data) { data_ = data; }
452 Displacement(Label* L, Type type) { init(L, type); }
455 PrintF("%s (%x) ", (type() == UNCONDITIONAL_JUMP ? "jmp" : "[other]"),
456 NextField::decode(data_));
462 class TypeField: public BitField<Type, 0, 2> {};
463 class NextField: public BitField<int, 2, 32-2> {};
465 void init(Label* L, Type type);
469 class Assembler : public AssemblerBase {
471 // We check before assembling an instruction that there is sufficient
472 // space to write an instruction and its relocation information.
473 // The relocation writer's position must be kGap bytes above the end of
474 // the generated instructions. This leaves enough space for the
475 // longest possible ia32 instruction, 15 bytes, and the longest possible
476 // relocation information encoding, RelocInfoWriter::kMaxLength == 16.
477 // (There is a 15 byte limit on ia32 instruction length that rules out some
478 // otherwise valid instructions.)
479 // This allows for a single, fast space check per instruction.
480 static const int kGap = 32;
483 // Create an assembler. Instructions and relocation information are emitted
484 // into a buffer, with the instructions starting from the beginning and the
485 // relocation information starting from the end of the buffer. See CodeDesc
486 // for a detailed comment on the layout (globals.h).
488 // If the provided buffer is NULL, the assembler allocates and grows its own
489 // buffer, and buffer_size determines the initial buffer size. The buffer is
490 // owned by the assembler and deallocated upon destruction of the assembler.
492 // If the provided buffer is not NULL, the assembler uses the provided buffer
493 // for code generation and assumes its size to be buffer_size. If the buffer
494 // is too small, a fatal error occurs. No deallocation of the buffer is done
495 // upon destruction of the assembler.
496 // TODO(vitalyr): the assembler does not need an isolate.
497 Assembler(Isolate* isolate, void* buffer, int buffer_size);
498 virtual ~Assembler() { }
500 // GetCode emits any pending (non-emitted) code and fills the descriptor
501 // desc. GetCode() is idempotent; it returns the same result if no other
502 // Assembler functions are invoked in between GetCode() calls.
503 void GetCode(CodeDesc* desc);
505 // Read/Modify the code target in the branch/call instruction at pc.
506 inline static Address target_address_at(Address pc, Address constant_pool);
507 inline static void set_target_address_at(
508 Address pc, Address constant_pool, Address target,
509 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED);
510 static inline Address target_address_at(Address pc, Code* code) {
511 Address constant_pool = code ? code->constant_pool() : NULL;
512 return target_address_at(pc, constant_pool);
514 static inline void set_target_address_at(Address pc,
517 ICacheFlushMode icache_flush_mode =
518 FLUSH_ICACHE_IF_NEEDED) {
519 Address constant_pool = code ? code->constant_pool() : NULL;
520 set_target_address_at(pc, constant_pool, target);
523 // Return the code target address at a call site from the return address
524 // of that call in the instruction stream.
525 inline static Address target_address_from_return_address(Address pc);
527 // This sets the branch destination (which is in the instruction on x86).
528 // This is for calls and branches within generated code.
529 inline static void deserialization_set_special_target_at(
530 Address instruction_payload, Code* code, Address target) {
531 set_target_address_at(instruction_payload, code, target);
534 // This sets the internal reference at the pc.
535 inline static void deserialization_set_target_internal_reference_at(
536 Address pc, Address target,
537 RelocInfo::Mode mode = RelocInfo::INTERNAL_REFERENCE);
539 static const int kSpecialTargetSize = kPointerSize;
541 // Distance between the address of the code target in the call instruction
542 // and the return address
543 static const int kCallTargetAddressOffset = kPointerSize;
545 static const int kCallInstructionLength = 5;
547 // The debug break slot must be able to contain a call instruction.
548 static const int kDebugBreakSlotLength = kCallInstructionLength;
550 // Distance between start of patched debug break slot and the emitted address
552 static const int kPatchDebugBreakSlotAddressOffset = 1; // JMP imm32.
554 // One byte opcode for test al, 0xXX.
555 static const byte kTestAlByte = 0xA8;
556 // One byte opcode for nop.
557 static const byte kNopByte = 0x90;
559 // One byte opcode for a short unconditional jump.
560 static const byte kJmpShortOpcode = 0xEB;
561 // One byte prefix for a short conditional jump.
562 static const byte kJccShortPrefix = 0x70;
563 static const byte kJncShortOpcode = kJccShortPrefix | not_carry;
564 static const byte kJcShortOpcode = kJccShortPrefix | carry;
565 static const byte kJnzShortOpcode = kJccShortPrefix | not_zero;
566 static const byte kJzShortOpcode = kJccShortPrefix | zero;
569 // ---------------------------------------------------------------------------
572 // - function names correspond one-to-one to ia32 instruction mnemonics
573 // - unless specified otherwise, instructions operate on 32bit operands
574 // - instructions on 8bit (byte) operands/registers have a trailing '_b'
575 // - instructions on 16bit (word) operands/registers have a trailing '_w'
576 // - naming conflicts with C++ keywords are resolved via a trailing '_'
578 // NOTE ON INTERFACE: Currently, the interface is not very consistent
579 // in the sense that some operations (e.g. mov()) can be called in more
580 // the one way to generate the same instruction: The Register argument
581 // can in some cases be replaced with an Operand(Register) argument.
582 // This should be cleaned up and made more orthogonal. The questions
583 // is: should we always use Operands instead of Registers where an
584 // Operand is possible, or should we have a Register (overloaded) form
585 // instead? We must be careful to make sure that the selected instruction
586 // is obvious from the parameters to avoid hard-to-find code generation
589 // Insert the smallest number of nop instructions
590 // possible to align the pc offset to a multiple
591 // of m. m must be a power of 2.
593 // Insert the smallest number of zero bytes possible to align the pc offset
594 // to a mulitple of m. m must be a power of 2 (>= 2).
595 void DataAlign(int m);
596 void Nop(int bytes = 1);
597 // Aligns code to something that's optimal for a jump target for the platform.
598 void CodeTargetAlign();
607 void push(const Immediate& x);
608 void push_imm32(int32_t imm32);
609 void push(Register src);
610 void push(const Operand& src);
612 void pop(Register dst);
613 void pop(const Operand& dst);
615 void enter(const Immediate& size);
619 void mov_b(Register dst, Register src) { mov_b(dst, Operand(src)); }
620 void mov_b(Register dst, const Operand& src);
621 void mov_b(Register dst, int8_t imm8) { mov_b(Operand(dst), imm8); }
622 void mov_b(const Operand& dst, int8_t imm8);
623 void mov_b(const Operand& dst, const Immediate& src);
624 void mov_b(const Operand& dst, Register src);
626 void mov_w(Register dst, const Operand& src);
627 void mov_w(const Operand& dst, Register src);
628 void mov_w(const Operand& dst, int16_t imm16);
629 void mov_w(const Operand& dst, const Immediate& src);
632 void mov(Register dst, int32_t imm32);
633 void mov(Register dst, const Immediate& x);
634 void mov(Register dst, Handle<Object> handle);
635 void mov(Register dst, const Operand& src);
636 void mov(Register dst, Register src);
637 void mov(const Operand& dst, const Immediate& x);
638 void mov(const Operand& dst, Handle<Object> handle);
639 void mov(const Operand& dst, Register src);
641 void movsx_b(Register dst, Register src) { movsx_b(dst, Operand(src)); }
642 void movsx_b(Register dst, const Operand& src);
644 void movsx_w(Register dst, Register src) { movsx_w(dst, Operand(src)); }
645 void movsx_w(Register dst, const Operand& src);
647 void movzx_b(Register dst, Register src) { movzx_b(dst, Operand(src)); }
648 void movzx_b(Register dst, const Operand& src);
650 void movzx_w(Register dst, Register src) { movzx_w(dst, Operand(src)); }
651 void movzx_w(Register dst, const Operand& src);
656 // Repetitive string instructions.
662 void xchg(Register dst, Register src);
663 void xchg(Register dst, const Operand& src);
666 void adc(Register dst, int32_t imm32);
667 void adc(Register dst, const Operand& src);
669 void add(Register dst, Register src) { add(dst, Operand(src)); }
670 void add(Register dst, const Operand& src);
671 void add(const Operand& dst, Register src);
672 void add(Register dst, const Immediate& imm) { add(Operand(dst), imm); }
673 void add(const Operand& dst, const Immediate& x);
675 void and_(Register dst, int32_t imm32);
676 void and_(Register dst, const Immediate& x);
677 void and_(Register dst, Register src) { and_(dst, Operand(src)); }
678 void and_(Register dst, const Operand& src);
679 void and_(const Operand& dst, Register src);
680 void and_(const Operand& dst, const Immediate& x);
682 void cmpb(Register reg, int8_t imm8) { cmpb(Operand(reg), imm8); }
683 void cmpb(const Operand& op, int8_t imm8);
684 void cmpb(Register reg, const Operand& op);
685 void cmpb(const Operand& op, Register reg);
686 void cmpb_al(const Operand& op);
687 void cmpw_ax(const Operand& op);
688 void cmpw(const Operand& op, Immediate imm16);
689 void cmp(Register reg, int32_t imm32);
690 void cmp(Register reg, Handle<Object> handle);
691 void cmp(Register reg0, Register reg1) { cmp(reg0, Operand(reg1)); }
692 void cmp(Register reg, const Operand& op);
693 void cmp(Register reg, const Immediate& imm) { cmp(Operand(reg), imm); }
694 void cmp(const Operand& op, const Immediate& imm);
695 void cmp(const Operand& op, Handle<Object> handle);
697 void dec_b(Register dst);
698 void dec_b(const Operand& dst);
700 void dec(Register dst);
701 void dec(const Operand& dst);
705 void idiv(Register src) { idiv(Operand(src)); }
706 void idiv(const Operand& src);
707 void div(Register src) { div(Operand(src)); }
708 void div(const Operand& src);
710 // Signed multiply instructions.
711 void imul(Register src); // edx:eax = eax * src.
712 void imul(Register dst, Register src) { imul(dst, Operand(src)); }
713 void imul(Register dst, const Operand& src); // dst = dst * src.
714 void imul(Register dst, Register src, int32_t imm32); // dst = src * imm32.
715 void imul(Register dst, const Operand& src, int32_t imm32);
717 void inc(Register dst);
718 void inc(const Operand& dst);
720 void lea(Register dst, const Operand& src);
722 // Unsigned multiply instruction.
723 void mul(Register src); // edx:eax = eax * reg.
725 void neg(Register dst);
726 void neg(const Operand& dst);
728 void not_(Register dst);
729 void not_(const Operand& dst);
731 void or_(Register dst, int32_t imm32);
732 void or_(Register dst, Register src) { or_(dst, Operand(src)); }
733 void or_(Register dst, const Operand& src);
734 void or_(const Operand& dst, Register src);
735 void or_(Register dst, const Immediate& imm) { or_(Operand(dst), imm); }
736 void or_(const Operand& dst, const Immediate& x);
738 void rcl(Register dst, uint8_t imm8);
739 void rcr(Register dst, uint8_t imm8);
741 void ror(Register dst, uint8_t imm8) { ror(Operand(dst), imm8); }
742 void ror(const Operand& dst, uint8_t imm8);
743 void ror_cl(Register dst) { ror_cl(Operand(dst)); }
744 void ror_cl(const Operand& dst);
746 void sar(Register dst, uint8_t imm8) { sar(Operand(dst), imm8); }
747 void sar(const Operand& dst, uint8_t imm8);
748 void sar_cl(Register dst) { sar_cl(Operand(dst)); }
749 void sar_cl(const Operand& dst);
751 void sbb(Register dst, const Operand& src);
753 void shld(Register dst, Register src) { shld(dst, Operand(src)); }
754 void shld(Register dst, const Operand& src);
756 void shl(Register dst, uint8_t imm8) { shl(Operand(dst), imm8); }
757 void shl(const Operand& dst, uint8_t imm8);
758 void shl_cl(Register dst) { shl_cl(Operand(dst)); }
759 void shl_cl(const Operand& dst);
761 void shrd(Register dst, Register src) { shrd(dst, Operand(src)); }
762 void shrd(Register dst, const Operand& src);
764 void shr(Register dst, uint8_t imm8) { shr(Operand(dst), imm8); }
765 void shr(const Operand& dst, uint8_t imm8);
766 void shr_cl(Register dst) { shr_cl(Operand(dst)); }
767 void shr_cl(const Operand& dst);
769 void sub(Register dst, const Immediate& imm) { sub(Operand(dst), imm); }
770 void sub(const Operand& dst, const Immediate& x);
771 void sub(Register dst, Register src) { sub(dst, Operand(src)); }
772 void sub(Register dst, const Operand& src);
773 void sub(const Operand& dst, Register src);
775 void test(Register reg, const Immediate& imm);
776 void test(Register reg0, Register reg1) { test(reg0, Operand(reg1)); }
777 void test(Register reg, const Operand& op);
778 void test_b(Register reg, const Operand& op);
779 void test(const Operand& op, const Immediate& imm);
780 void test_b(Register reg, uint8_t imm8);
781 void test_b(const Operand& op, uint8_t imm8);
783 void xor_(Register dst, int32_t imm32);
784 void xor_(Register dst, Register src) { xor_(dst, Operand(src)); }
785 void xor_(Register dst, const Operand& src);
786 void xor_(const Operand& dst, Register src);
787 void xor_(Register dst, const Immediate& imm) { xor_(Operand(dst), imm); }
788 void xor_(const Operand& dst, const Immediate& x);
791 void bt(const Operand& dst, Register src);
792 void bts(Register dst, Register src) { bts(Operand(dst), src); }
793 void bts(const Operand& dst, Register src);
794 void bsr(Register dst, Register src) { bsr(dst, Operand(src)); }
795 void bsr(Register dst, const Operand& src);
804 // Label operations & relative jumps (PPUM Appendix D)
806 // Takes a branch opcode (cc) and a label (L) and generates
807 // either a backward branch or a forward branch and links it
808 // to the label fixup chain. Usage:
810 // Label L; // unbound label
811 // j(cc, &L); // forward branch to unbound label
812 // bind(&L); // bind label to the current pc
813 // j(cc, &L); // backward branch to bound label
814 // bind(&L); // illegal: a label may be bound only once
816 // Note: The same Label can be used for forward and backward branches
817 // but it may be bound only once.
819 void bind(Label* L); // binds an unbound label L to the current code position
823 void call(byte* entry, RelocInfo::Mode rmode);
824 int CallSize(const Operand& adr);
825 void call(Register reg) { call(Operand(reg)); }
826 void call(const Operand& adr);
827 int CallSize(Handle<Code> code, RelocInfo::Mode mode);
828 void call(Handle<Code> code,
829 RelocInfo::Mode rmode,
830 TypeFeedbackId id = TypeFeedbackId::None());
833 // unconditional jump to L
834 void jmp(Label* L, Label::Distance distance = Label::kFar);
835 void jmp(byte* entry, RelocInfo::Mode rmode);
836 void jmp(Register reg) { jmp(Operand(reg)); }
837 void jmp(const Operand& adr);
838 void jmp(Handle<Code> code, RelocInfo::Mode rmode);
843 Label::Distance distance = Label::kFar);
844 void j(Condition cc, byte* entry, RelocInfo::Mode rmode);
845 void j(Condition cc, Handle<Code> code,
846 RelocInfo::Mode rmode = RelocInfo::CODE_TARGET);
848 // Floating-point operations
857 void fld_s(const Operand& adr);
858 void fld_d(const Operand& adr);
860 void fstp_s(const Operand& adr);
861 void fst_s(const Operand& adr);
862 void fstp_d(const Operand& adr);
863 void fst_d(const Operand& adr);
865 void fild_s(const Operand& adr);
866 void fild_d(const Operand& adr);
868 void fist_s(const Operand& adr);
870 void fistp_s(const Operand& adr);
871 void fistp_d(const Operand& adr);
873 // The fisttp instructions require SSE3.
874 void fisttp_s(const Operand& adr);
875 void fisttp_d(const Operand& adr);
890 void fadd_d(const Operand& adr);
893 void fsub_d(const Operand& adr);
894 void fsubr_d(const Operand& adr);
896 void fmul_d(const Operand& adr);
899 void fdiv_d(const Operand& adr);
900 void fdivr_d(const Operand& adr);
903 void fisub_s(const Operand& adr);
905 void faddp(int i = 1);
906 void fsubp(int i = 1);
907 void fsubr(int i = 1);
908 void fsubrp(int i = 1);
909 void fmulp(int i = 1);
910 void fdivp(int i = 1);
914 void fxch(int i = 1);
916 void ffree(int i = 0);
926 void fldcw(const Operand& adr);
927 void fnstcw(const Operand& adr);
930 void fnsave(const Operand& adr);
931 void frstor(const Operand& adr);
936 void setcc(Condition cc, Register reg);
940 // TODO(lrn): Need SFENCE for movnt?
942 // Check the code size generated from label to here.
943 int SizeOfCodeGeneratedSince(Label* label) {
944 return pc_offset() - label->pos();
947 // Mark generator continuation.
948 void RecordGeneratorContinuation();
950 // Mark address of a debug break slot.
951 void RecordDebugBreakSlot(RelocInfo::Mode mode, int argc = 0);
953 // Record a comment relocation entry that can be used by a disassembler.
954 // Use --code-comments to enable.
955 void RecordComment(const char* msg);
957 // Record a deoptimization reason that can be used by a log or cpu profiler.
958 // Use --trace-deopt to enable.
959 void RecordDeoptReason(const int reason, const SourcePosition position);
961 // Writes a single byte or word of data in the code stream. Used for
962 // inline tables, e.g., jump-tables.
963 void db(uint8_t data);
964 void dd(uint32_t data);
965 void dq(uint64_t data);
966 void dp(uintptr_t data) { dd(data); }
967 void dd(Label* label);
969 // Check if there is less than kGap bytes available in the buffer.
970 // If this is the case, we need to grow the buffer before emitting
971 // an instruction or relocation information.
972 inline bool buffer_overflow() const {
973 return pc_ >= reloc_info_writer.pos() - kGap;
976 // Get the number of bytes available in the buffer.
977 inline int available_space() const { return reloc_info_writer.pos() - pc_; }
979 static bool IsNop(Address addr);
981 PositionsRecorder* positions_recorder() { return &positions_recorder_; }
983 int relocation_writer_size() {
984 return (buffer_ + buffer_size_) - reloc_info_writer.pos();
987 // Avoid overflows for displacements etc.
988 static const int kMaximalBufferSize = 512*MB;
990 byte byte_at(int pos) { return buffer_[pos]; }
991 void set_byte_at(int pos, byte value) { buffer_[pos] = value; }
993 void PatchConstantPoolAccessInstruction(int pc_offset, int offset,
994 ConstantPoolEntry::Access access,
995 ConstantPoolEntry::Type type) {
996 // No embedded constant pool support.
1001 byte* addr_at(int pos) { return buffer_ + pos; }
1005 uint32_t long_at(int pos) {
1006 return *reinterpret_cast<uint32_t*>(addr_at(pos));
1008 void long_at_put(int pos, uint32_t x) {
1009 *reinterpret_cast<uint32_t*>(addr_at(pos)) = x;
1014 inline void emit(uint32_t x);
1015 inline void emit(Handle<Object> handle);
1016 inline void emit(uint32_t x,
1017 RelocInfo::Mode rmode,
1018 TypeFeedbackId id = TypeFeedbackId::None());
1019 inline void emit(Handle<Code> code,
1020 RelocInfo::Mode rmode,
1021 TypeFeedbackId id = TypeFeedbackId::None());
1022 inline void emit(const Immediate& x);
1023 inline void emit_w(const Immediate& x);
1024 inline void emit_q(uint64_t x);
1026 // Emit the code-object-relative offset of the label's position
1027 inline void emit_code_relative_offset(Label* label);
1029 // instruction generation
1030 void emit_arith_b(int op1, int op2, Register dst, int imm8);
1032 // Emit a basic arithmetic instruction (i.e. first byte of the family is 0x81)
1033 // with a given destination expression and an immediate operand. It attempts
1034 // to use the shortest encoding possible.
1035 // sel specifies the /n in the modrm byte (see the Intel PRM).
1036 void emit_arith(int sel, Operand dst, const Immediate& x);
1038 void emit_operand(Register reg, const Operand& adr);
1040 void emit_label(Label* label);
1042 void emit_farith(int b1, int b2, int i);
1045 void print(Label* L);
1046 void bind_to(Label* L, int pos);
1049 inline Displacement disp_at(Label* L);
1050 inline void disp_at_put(Label* L, Displacement disp);
1051 inline void emit_disp(Label* L, Displacement::Type type);
1052 inline void emit_near_disp(Label* L);
1054 // record reloc info for current pc_
1055 void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
1057 friend class CodePatcher;
1058 friend class EnsureSpace;
1060 // Internal reference positions, required for (potential) patching in
1061 // GrowBuffer(); contains only those internal references whose labels
1062 // are already bound.
1063 std::deque<int> internal_reference_positions_;
1066 RelocInfoWriter reloc_info_writer;
1068 PositionsRecorder positions_recorder_;
1069 friend class PositionsRecorder;
1073 // Helper class that ensures that there is enough space for generating
1074 // instructions and relocation information. The constructor makes
1075 // sure that there is enough space and (in debug mode) the destructor
1076 // checks that we did not generate too much.
1077 class EnsureSpace BASE_EMBEDDED {
1079 explicit EnsureSpace(Assembler* assembler) : assembler_(assembler) {
1080 if (assembler_->buffer_overflow()) assembler_->GrowBuffer();
1082 space_before_ = assembler_->available_space();
1088 int bytes_generated = space_before_ - assembler_->available_space();
1089 DCHECK(bytes_generated < assembler_->kGap);
1094 Assembler* assembler_;
1100 } } // namespace v8::internal
1102 #endif // V8_X87_ASSEMBLER_X87_H_