| OLD | NEW |
| 1 //===- subzero/src/assembler_ia32.h - Assembler for x86-32 ------*- C++ -*-===// | 1 //===- subzero/src/assembler_ia32.h - Assembler for x86-32 ------*- C++ -*-===// |
| 2 // | 2 // |
| 3 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 3 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 4 // for details. All rights reserved. Use of this source code is governed by a | 4 // for details. All rights reserved. Use of this source code is governed by a |
| 5 // BSD-style license that can be found in the LICENSE file. | 5 // BSD-style license that can be found in the LICENSE file. |
| 6 // | 6 // |
| 7 // Modified by the Subzero authors. | 7 // Modified by the Subzero authors. |
| 8 // | 8 // |
| 9 //===----------------------------------------------------------------------===// | 9 //===----------------------------------------------------------------------===// |
| 10 // | 10 // |
| 11 // The Subzero Code Generator | 11 // The Subzero Code Generator |
| 12 // | 12 // |
| 13 // This file is distributed under the University of Illinois Open Source | 13 // This file is distributed under the University of Illinois Open Source |
| 14 // License. See LICENSE.TXT for details. | 14 // License. See LICENSE.TXT for details. |
| 15 // | 15 // |
| 16 //===----------------------------------------------------------------------===// | 16 //===----------------------------------------------------------------------===// |
| 17 // | 17 /// |
| 18 // This file implements the Assembler class for x86-32. | 18 /// \file |
| 19 // | 19 /// This file implements the Assembler class for x86-32. |
| 20 /// |
| 20 //===----------------------------------------------------------------------===// | 21 //===----------------------------------------------------------------------===// |
| 21 | 22 |
| 22 #ifndef SUBZERO_SRC_ICEASSEMBLERX8632_H | 23 #ifndef SUBZERO_SRC_ICEASSEMBLERX8632_H |
| 23 #define SUBZERO_SRC_ICEASSEMBLERX8632_H | 24 #define SUBZERO_SRC_ICEASSEMBLERX8632_H |
| 24 | 25 |
| 25 #include "IceAssembler.h" | 26 #include "IceAssembler.h" |
| 26 #include "IceConditionCodesX8632.h" | 27 #include "IceConditionCodesX8632.h" |
| 27 #include "IceDefs.h" | 28 #include "IceDefs.h" |
| 28 #include "IceOperand.h" | 29 #include "IceOperand.h" |
| 29 #include "IceRegistersX8632.h" | 30 #include "IceRegistersX8632.h" |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 | 150 |
| 150 void SetFixup(AssemblerFixup *fixup) { fixup_ = fixup; } | 151 void SetFixup(AssemblerFixup *fixup) { fixup_ = fixup; } |
| 151 | 152 |
| 152 private: | 153 private: |
| 153 uint8_t length_; | 154 uint8_t length_; |
| 154 uint8_t encoding_[6]; | 155 uint8_t encoding_[6]; |
| 155 AssemblerFixup *fixup_; | 156 AssemblerFixup *fixup_; |
| 156 | 157 |
| 157 explicit Operand(GPRRegister reg) : fixup_(nullptr) { SetModRM(3, reg); } | 158 explicit Operand(GPRRegister reg) : fixup_(nullptr) { SetModRM(3, reg); } |
| 158 | 159 |
| 159 // Get the operand encoding byte at the given index. | 160 /// Get the operand encoding byte at the given index. |
| 160 uint8_t encoding_at(intptr_t index) const { | 161 uint8_t encoding_at(intptr_t index) const { |
| 161 assert(index >= 0 && index < length_); | 162 assert(index >= 0 && index < length_); |
| 162 return encoding_[index]; | 163 return encoding_[index]; |
| 163 } | 164 } |
| 164 | 165 |
| 165 // Returns whether or not this operand is really the given register in | 166 /// Returns whether or not this operand is really the given register in |
| 166 // disguise. Used from the assembler to generate better encodings. | 167 /// disguise. Used from the assembler to generate better encodings. |
| 167 bool IsRegister(GPRRegister reg) const { | 168 bool IsRegister(GPRRegister reg) const { |
| 168 return ((encoding_[0] & 0xF8) == 0xC0) // Addressing mode is register only. | 169 return ((encoding_[0] & 0xF8) == 0xC0) // Addressing mode is register only. |
| 169 && ((encoding_[0] & 0x07) == reg); // Register codes match. | 170 && ((encoding_[0] & 0x07) == reg); // Register codes match. |
| 170 } | 171 } |
| 171 | 172 |
| 172 friend class AssemblerX8632; | 173 friend class AssemblerX8632; |
| 173 }; | 174 }; |
| 174 | 175 |
| 175 class Address : public Operand { | 176 class Address : public Operand { |
| 176 public: | 177 public: |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 | 264 |
| 264 void FinalCheck() const { | 265 void FinalCheck() const { |
| 265 // Assert if label is being destroyed with unresolved branches pending. | 266 // Assert if label is being destroyed with unresolved branches pending. |
| 266 assert(!IsLinked()); | 267 assert(!IsLinked()); |
| 267 assert(!HasNear()); | 268 assert(!HasNear()); |
| 268 } | 269 } |
| 269 | 270 |
| 270 // TODO(jvoung): why are labels offset by this? | 271 // TODO(jvoung): why are labels offset by this? |
| 271 static const uint32_t kWordSize = sizeof(uint32_t); | 272 static const uint32_t kWordSize = sizeof(uint32_t); |
| 272 | 273 |
| 273 // Returns the position for bound labels (branches that come after this | 274 /// Returns the position for bound labels (branches that come after this |
| 274 // are considered backward branches). Cannot be used for unused or linked | 275 /// are considered backward branches). Cannot be used for unused or linked |
| 275 // labels. | 276 /// labels. |
| 276 intptr_t Position() const { | 277 intptr_t Position() const { |
| 277 assert(IsBound()); | 278 assert(IsBound()); |
| 278 return -position_ - kWordSize; | 279 return -position_ - kWordSize; |
| 279 } | 280 } |
| 280 | 281 |
| 281 // Returns the position of an earlier branch instruction that was linked | 282 /// Returns the position of an earlier branch instruction that was linked |
| 282 // to this label (branches that use this are considered forward branches). | 283 /// to this label (branches that use this are considered forward branches). |
| 283 // The linked instructions form a linked list, of sorts, using the | 284 /// The linked instructions form a linked list, of sorts, using the |
| 284 // instruction's displacement field for the location of the next | 285 /// instruction's displacement field for the location of the next |
| 285 // instruction that is also linked to this label. | 286 /// instruction that is also linked to this label. |
| 286 intptr_t LinkPosition() const { | 287 intptr_t LinkPosition() const { |
| 287 assert(IsLinked()); | 288 assert(IsLinked()); |
| 288 return position_ - kWordSize; | 289 return position_ - kWordSize; |
| 289 } | 290 } |
| 290 | 291 |
| 291 // Returns the position of an earlier branch instruction which | 292 /// Returns the position of an earlier branch instruction which |
| 292 // assumes that this label is "near", and bumps iterator to the | 293 /// assumes that this label is "near", and bumps iterator to the |
| 293 // next near position. | 294 /// next near position. |
| 294 intptr_t NearPosition() { | 295 intptr_t NearPosition() { |
| 295 assert(HasNear()); | 296 assert(HasNear()); |
| 296 return unresolved_near_positions_[--num_unresolved_]; | 297 return unresolved_near_positions_[--num_unresolved_]; |
| 297 } | 298 } |
| 298 | 299 |
| 299 bool IsBound() const { return position_ < 0; } | 300 bool IsBound() const { return position_ < 0; } |
| 300 bool IsLinked() const { return position_ > 0; } | 301 bool IsLinked() const { return position_ > 0; } |
| 301 bool IsUnused() const { return (position_ == 0) && (num_unresolved_ == 0); } | 302 bool IsUnused() const { return (position_ == 0) && (num_unresolved_ == 0); } |
| 302 bool HasNear() const { return num_unresolved_ != 0; } | 303 bool HasNear() const { return num_unresolved_ != 0; } |
| 303 | 304 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 | 376 |
| 376 bool fixupIsPCRel(FixupKind Kind) const override { | 377 bool fixupIsPCRel(FixupKind Kind) const override { |
| 377 // Currently assuming this is the only PC-rel relocation type used. | 378 // Currently assuming this is the only PC-rel relocation type used. |
| 378 return Kind == llvm::ELF::R_386_PC32; | 379 return Kind == llvm::ELF::R_386_PC32; |
| 379 } | 380 } |
| 380 | 381 |
| 381 static bool classof(const Assembler *Asm) { | 382 static bool classof(const Assembler *Asm) { |
| 382 return Asm->getKind() == Asm_X8632; | 383 return Asm->getKind() == Asm_X8632; |
| 383 } | 384 } |
| 384 | 385 |
| 385 // Operations to emit GPR instructions (and dispatch on operand type). | 386 /// Operations to emit GPR instructions (and dispatch on operand type). |
| 386 typedef void (AssemblerX8632::*TypedEmitGPR)(Type, GPRRegister); | 387 typedef void (AssemblerX8632::*TypedEmitGPR)(Type, GPRRegister); |
| 387 typedef void (AssemblerX8632::*TypedEmitAddr)(Type, const Address &); | 388 typedef void (AssemblerX8632::*TypedEmitAddr)(Type, const Address &); |
| 388 struct GPREmitterOneOp { | 389 struct GPREmitterOneOp { |
| 389 TypedEmitGPR Reg; | 390 TypedEmitGPR Reg; |
| 390 TypedEmitAddr Addr; | 391 TypedEmitAddr Addr; |
| 391 }; | 392 }; |
| 392 | 393 |
| 393 typedef void (AssemblerX8632::*TypedEmitGPRGPR)(Type, GPRRegister, | 394 typedef void (AssemblerX8632::*TypedEmitGPRGPR)(Type, GPRRegister, |
| 394 GPRRegister); | 395 GPRRegister); |
| 395 typedef void (AssemblerX8632::*TypedEmitGPRAddr)(Type, GPRRegister, | 396 typedef void (AssemblerX8632::*TypedEmitGPRAddr)(Type, GPRRegister, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 421 | 422 |
| 422 typedef void (AssemblerX8632::*TypedEmitAddrGPR)(Type, const Address &, | 423 typedef void (AssemblerX8632::*TypedEmitAddrGPR)(Type, const Address &, |
| 423 GPRRegister); | 424 GPRRegister); |
| 424 typedef void (AssemblerX8632::*TypedEmitAddrImm)(Type, const Address &, | 425 typedef void (AssemblerX8632::*TypedEmitAddrImm)(Type, const Address &, |
| 425 const Immediate &); | 426 const Immediate &); |
| 426 struct GPREmitterAddrOp { | 427 struct GPREmitterAddrOp { |
| 427 TypedEmitAddrGPR AddrGPR; | 428 TypedEmitAddrGPR AddrGPR; |
| 428 TypedEmitAddrImm AddrImm; | 429 TypedEmitAddrImm AddrImm; |
| 429 }; | 430 }; |
| 430 | 431 |
| 431 // Operations to emit XMM instructions (and dispatch on operand type). | 432 /// Operations to emit XMM instructions (and dispatch on operand type). |
| 432 typedef void (AssemblerX8632::*TypedEmitXmmXmm)(Type, XmmRegister, | 433 typedef void (AssemblerX8632::*TypedEmitXmmXmm)(Type, XmmRegister, |
| 433 XmmRegister); | 434 XmmRegister); |
| 434 typedef void (AssemblerX8632::*TypedEmitXmmAddr)(Type, XmmRegister, | 435 typedef void (AssemblerX8632::*TypedEmitXmmAddr)(Type, XmmRegister, |
| 435 const Address &); | 436 const Address &); |
| 436 struct XmmEmitterRegOp { | 437 struct XmmEmitterRegOp { |
| 437 TypedEmitXmmXmm XmmXmm; | 438 TypedEmitXmmXmm XmmXmm; |
| 438 TypedEmitXmmAddr XmmAddr; | 439 TypedEmitXmmAddr XmmAddr; |
| 439 }; | 440 }; |
| 440 | 441 |
| 441 typedef void (AssemblerX8632::*EmitXmmXmm)(XmmRegister, XmmRegister); | 442 typedef void (AssemblerX8632::*EmitXmmXmm)(XmmRegister, XmmRegister); |
| 442 typedef void (AssemblerX8632::*EmitXmmAddr)(XmmRegister, const Address &); | 443 typedef void (AssemblerX8632::*EmitXmmAddr)(XmmRegister, const Address &); |
| 443 typedef void (AssemblerX8632::*EmitAddrXmm)(const Address &, XmmRegister); | 444 typedef void (AssemblerX8632::*EmitAddrXmm)(const Address &, XmmRegister); |
| 444 struct XmmEmitterMovOps { | 445 struct XmmEmitterMovOps { |
| 445 EmitXmmXmm XmmXmm; | 446 EmitXmmXmm XmmXmm; |
| 446 EmitXmmAddr XmmAddr; | 447 EmitXmmAddr XmmAddr; |
| 447 EmitAddrXmm AddrXmm; | 448 EmitAddrXmm AddrXmm; |
| 448 }; | 449 }; |
| 449 | 450 |
| 450 typedef void (AssemblerX8632::*TypedEmitXmmImm)(Type, XmmRegister, | 451 typedef void (AssemblerX8632::*TypedEmitXmmImm)(Type, XmmRegister, |
| 451 const Immediate &); | 452 const Immediate &); |
| 452 | 453 |
| 453 struct XmmEmitterShiftOp { | 454 struct XmmEmitterShiftOp { |
| 454 TypedEmitXmmXmm XmmXmm; | 455 TypedEmitXmmXmm XmmXmm; |
| 455 TypedEmitXmmAddr XmmAddr; | 456 TypedEmitXmmAddr XmmAddr; |
| 456 TypedEmitXmmImm XmmImm; | 457 TypedEmitXmmImm XmmImm; |
| 457 }; | 458 }; |
| 458 | 459 |
| 459 // Cross Xmm/GPR cast instructions. | 460 /// Cross Xmm/GPR cast instructions. |
| 460 template <typename DReg_t, typename SReg_t> struct CastEmitterRegOp { | 461 template <typename DReg_t, typename SReg_t> struct CastEmitterRegOp { |
| 461 typedef void (AssemblerX8632::*TypedEmitRegs)(Type, DReg_t, SReg_t); | 462 typedef void (AssemblerX8632::*TypedEmitRegs)(Type, DReg_t, SReg_t); |
| 462 typedef void (AssemblerX8632::*TypedEmitAddr)(Type, DReg_t, | 463 typedef void (AssemblerX8632::*TypedEmitAddr)(Type, DReg_t, |
| 463 const Address &); | 464 const Address &); |
| 464 | 465 |
| 465 TypedEmitRegs RegReg; | 466 TypedEmitRegs RegReg; |
| 466 TypedEmitAddr RegAddr; | 467 TypedEmitAddr RegAddr; |
| 467 }; | 468 }; |
| 468 | 469 |
| 469 // Three operand (potentially) cross Xmm/GPR instructions. | 470 /// Three operand (potentially) cross Xmm/GPR instructions. |
| 470 // The last operand must be an immediate. | 471 /// The last operand must be an immediate. |
| 471 template <typename DReg_t, typename SReg_t> struct ThreeOpImmEmitter { | 472 template <typename DReg_t, typename SReg_t> struct ThreeOpImmEmitter { |
| 472 typedef void (AssemblerX8632::*TypedEmitRegRegImm)(Type, DReg_t, SReg_t, | 473 typedef void (AssemblerX8632::*TypedEmitRegRegImm)(Type, DReg_t, SReg_t, |
| 473 const Immediate &); | 474 const Immediate &); |
| 474 typedef void (AssemblerX8632::*TypedEmitRegAddrImm)(Type, DReg_t, | 475 typedef void (AssemblerX8632::*TypedEmitRegAddrImm)(Type, DReg_t, |
| 475 const Address &, | 476 const Address &, |
| 476 const Immediate &); | 477 const Immediate &); |
| 477 | 478 |
| 478 TypedEmitRegRegImm RegRegImm; | 479 TypedEmitRegRegImm RegRegImm; |
| 479 TypedEmitRegAddrImm RegAddrImm; | 480 TypedEmitRegAddrImm RegAddrImm; |
| 480 }; | 481 }; |
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 810 void bsr(Type Ty, GPRRegister dst, GPRRegister src); | 811 void bsr(Type Ty, GPRRegister dst, GPRRegister src); |
| 811 void bsr(Type Ty, GPRRegister dst, const Address &src); | 812 void bsr(Type Ty, GPRRegister dst, const Address &src); |
| 812 | 813 |
| 813 void bswap(Type Ty, GPRRegister reg); | 814 void bswap(Type Ty, GPRRegister reg); |
| 814 | 815 |
| 815 void bt(GPRRegister base, GPRRegister offset); | 816 void bt(GPRRegister base, GPRRegister offset); |
| 816 | 817 |
| 817 void ret(); | 818 void ret(); |
| 818 void ret(const Immediate &imm); | 819 void ret(const Immediate &imm); |
| 819 | 820 |
| 820 // 'size' indicates size in bytes and must be in the range 1..8. | 821 /// 'size' indicates size in bytes and must be in the range 1..8. |
| 821 void nop(int size = 1); | 822 void nop(int size = 1); |
| 822 void int3(); | 823 void int3(); |
| 823 void hlt(); | 824 void hlt(); |
| 824 void ud2(); | 825 void ud2(); |
| 825 | 826 |
| 826 void j(CondX86::BrCond condition, Label *label, bool near = kFarJump); | 827 void j(CondX86::BrCond condition, Label *label, bool near = kFarJump); |
| 827 void j(CondX86::BrCond condition, const ConstantRelocatable *label); | 828 void j(CondX86::BrCond condition, const ConstantRelocatable *label); |
| 828 | 829 |
| 829 void jmp(GPRRegister reg); | 830 void jmp(GPRRegister reg); |
| 830 void jmp(Label *label, bool near = kFarJump); | 831 void jmp(Label *label, bool near = kFarJump); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 863 const Immediate &immediate); | 864 const Immediate &immediate); |
| 864 void emitLabel(Label *label, intptr_t instruction_size); | 865 void emitLabel(Label *label, intptr_t instruction_size); |
| 865 void emitLabelLink(Label *label); | 866 void emitLabelLink(Label *label); |
| 866 void emitNearLabelLink(Label *label); | 867 void emitNearLabelLink(Label *label); |
| 867 | 868 |
| 868 void emitGenericShift(int rm, Type Ty, GPRRegister reg, const Immediate &imm); | 869 void emitGenericShift(int rm, Type Ty, GPRRegister reg, const Immediate &imm); |
| 869 void emitGenericShift(int rm, Type Ty, const Operand &operand, | 870 void emitGenericShift(int rm, Type Ty, const Operand &operand, |
| 870 GPRRegister shifter); | 871 GPRRegister shifter); |
| 871 | 872 |
| 872 typedef std::vector<Label *> LabelVector; | 873 typedef std::vector<Label *> LabelVector; |
| 873 // A vector of pool-allocated x86 labels for CFG nodes. | 874 /// A vector of pool-allocated x86 labels for CFG nodes. |
| 874 LabelVector CfgNodeLabels; | 875 LabelVector CfgNodeLabels; |
| 875 // A vector of pool-allocated x86 labels for Local labels. | 876 /// A vector of pool-allocated x86 labels for Local labels. |
| 876 LabelVector LocalLabels; | 877 LabelVector LocalLabels; |
| 877 | 878 |
| 878 Label *GetOrCreateLabel(SizeT Number, LabelVector &Labels); | 879 Label *GetOrCreateLabel(SizeT Number, LabelVector &Labels); |
| 879 | 880 |
| 880 // The arith_int() methods factor out the commonality between the encodings of | 881 /// The arith_int() methods factor out the commonality between the encodings |
| 881 // add(), Or(), adc(), sbb(), And(), sub(), Xor(), and cmp(). The Tag | 882 /// of add(), Or(), adc(), sbb(), And(), sub(), Xor(), and cmp(). The Tag |
| 882 // parameter is statically asserted to be less than 8. | 883 /// parameter is statically asserted to be less than 8. |
| 883 template <uint32_t Tag> | 884 template <uint32_t Tag> |
| 884 void arith_int(Type Ty, GPRRegister reg, const Immediate &imm); | 885 void arith_int(Type Ty, GPRRegister reg, const Immediate &imm); |
| 885 | 886 |
| 886 template <uint32_t Tag> | 887 template <uint32_t Tag> |
| 887 void arith_int(Type Ty, GPRRegister reg0, GPRRegister reg1); | 888 void arith_int(Type Ty, GPRRegister reg0, GPRRegister reg1); |
| 888 | 889 |
| 889 template <uint32_t Tag> | 890 template <uint32_t Tag> |
| 890 void arith_int(Type Ty, GPRRegister reg, const Address &address); | 891 void arith_int(Type Ty, GPRRegister reg, const Address &address); |
| 891 | 892 |
| 892 template <uint32_t Tag> | 893 template <uint32_t Tag> |
| (...skipping 27 matching lines...) Expand all Loading... |
| 920 inline void AssemblerX8632::emitFixup(AssemblerFixup *fixup) { | 921 inline void AssemblerX8632::emitFixup(AssemblerFixup *fixup) { |
| 921 Buffer.emitFixup(fixup); | 922 Buffer.emitFixup(fixup); |
| 922 } | 923 } |
| 923 | 924 |
| 924 inline void AssemblerX8632::emitOperandSizeOverride() { emitUint8(0x66); } | 925 inline void AssemblerX8632::emitOperandSizeOverride() { emitUint8(0x66); } |
| 925 | 926 |
| 926 } // end of namespace X8632 | 927 } // end of namespace X8632 |
| 927 } // end of namespace Ice | 928 } // end of namespace Ice |
| 928 | 929 |
| 929 #endif // SUBZERO_SRC_ICEASSEMBLERX8632_H | 930 #endif // SUBZERO_SRC_ICEASSEMBLERX8632_H |
| OLD | NEW |