Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 1 | //===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ARMMCCodeEmitter class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 2ac1902 | 2010-11-15 05:19:05 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "mccodeemitter" |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 15 | #include "ARM.h" |
Jim Grosbach | 42fac8e | 2010-10-11 23:16:21 +0000 | [diff] [blame] | 16 | #include "ARMAddressingModes.h" |
Jim Grosbach | 7093326 | 2010-11-04 01:12:30 +0000 | [diff] [blame] | 17 | #include "ARMFixupKinds.h" |
Jim Grosbach | d6d4b42 | 2010-10-07 22:12:50 +0000 | [diff] [blame] | 18 | #include "ARMInstrInfo.h" |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCCodeEmitter.h" |
| 20 | #include "llvm/MC/MCExpr.h" |
| 21 | #include "llvm/MC/MCInst.h" |
Jim Grosbach | d6d4b42 | 2010-10-07 22:12:50 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | using namespace llvm; |
| 25 | |
Jim Grosbach | 7093326 | 2010-11-04 01:12:30 +0000 | [diff] [blame] | 26 | STATISTIC(MCNumEmitted, "Number of MC instructions emitted."); |
| 27 | STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created."); |
Jim Grosbach | d6d4b42 | 2010-10-07 22:12:50 +0000 | [diff] [blame] | 28 | |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 29 | namespace { |
| 30 | class ARMMCCodeEmitter : public MCCodeEmitter { |
| 31 | ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT |
| 32 | void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT |
| 33 | const TargetMachine &TM; |
| 34 | const TargetInstrInfo &TII; |
| 35 | MCContext &Ctx; |
| 36 | |
| 37 | public: |
| 38 | ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx) |
| 39 | : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) { |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | ~ARMMCCodeEmitter() {} |
| 43 | |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 44 | unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; } |
Jim Grosbach | 7093326 | 2010-11-04 01:12:30 +0000 | [diff] [blame] | 45 | |
| 46 | const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const { |
| 47 | const static MCFixupKindInfo Infos[] = { |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 48 | // name offset bits flags |
| 49 | { "fixup_arm_pcrel_12", 2, 12, MCFixupKindInfo::FKF_IsPCRel }, |
| 50 | { "fixup_arm_vfp_pcrel_12", 3, 8, MCFixupKindInfo::FKF_IsPCRel }, |
| 51 | { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel }, |
Jim Grosbach | 7093326 | 2010-11-04 01:12:30 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | if (Kind < FirstTargetFixupKind) |
| 55 | return MCCodeEmitter::getFixupKindInfo(Kind); |
| 56 | |
| 57 | assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && |
| 58 | "Invalid kind!"); |
| 59 | return Infos[Kind - FirstTargetFixupKind]; |
| 60 | } |
Jim Grosbach | 0de6ab3 | 2010-10-12 17:11:26 +0000 | [diff] [blame] | 61 | unsigned getMachineSoImmOpValue(unsigned SoImm) const; |
| 62 | |
Jim Grosbach | 9af82ba | 2010-10-07 21:57:55 +0000 | [diff] [blame] | 63 | // getBinaryCodeForInstr - TableGen'erated function for getting the |
| 64 | // binary encoding for an instruction. |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 65 | unsigned getBinaryCodeForInstr(const MCInst &MI, |
| 66 | SmallVectorImpl<MCFixup> &Fixups) const; |
Jim Grosbach | 9af82ba | 2010-10-07 21:57:55 +0000 | [diff] [blame] | 67 | |
| 68 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 69 | /// operand requires relocation, record the relocation and return zero. |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 70 | unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO, |
| 71 | SmallVectorImpl<MCFixup> &Fixups) const; |
Jim Grosbach | 9af82ba | 2010-10-07 21:57:55 +0000 | [diff] [blame] | 72 | |
Jason W Kim | 837caa9 | 2010-11-18 23:37:15 +0000 | [diff] [blame] | 73 | /// getMovtImmOpValue - Return the encoding for the movw/movt pair |
| 74 | uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx, |
| 75 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 76 | |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 77 | bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 78 | unsigned &Reg, unsigned &Imm, |
| 79 | SmallVectorImpl<MCFixup> &Fixups) const; |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 80 | |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 81 | /// getBranchTargetOpValue - Return encoding info for 24-bit immediate |
| 82 | /// branch target. |
| 83 | uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, |
| 84 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 85 | |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 86 | /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' |
| 87 | /// operand. |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 88 | uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx, |
| 89 | SmallVectorImpl<MCFixup> &Fixups) const; |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 90 | |
Jim Grosbach | 54fea63 | 2010-11-09 17:20:53 +0000 | [diff] [blame] | 91 | /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm' |
| 92 | /// operand as needed by load/store instructions. |
| 93 | uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx, |
| 94 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 95 | |
Jim Grosbach | 5d5eb9e | 2010-11-10 23:38:36 +0000 | [diff] [blame] | 96 | /// getLdStmModeOpValue - Return encoding for load/store multiple mode. |
| 97 | uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx, |
| 98 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 99 | ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm(); |
| 100 | switch (Mode) { |
| 101 | default: assert(0 && "Unknown addressing sub-mode!"); |
| 102 | case ARM_AM::da: return 0; |
| 103 | case ARM_AM::ia: return 1; |
| 104 | case ARM_AM::db: return 2; |
| 105 | case ARM_AM::ib: return 3; |
| 106 | } |
| 107 | } |
Jim Grosbach | 99f53d1 | 2010-11-15 20:47:07 +0000 | [diff] [blame] | 108 | /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value. |
| 109 | /// |
| 110 | unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const { |
| 111 | switch (ShOpc) { |
| 112 | default: llvm_unreachable("Unknown shift opc!"); |
| 113 | case ARM_AM::no_shift: |
| 114 | case ARM_AM::lsl: return 0; |
| 115 | case ARM_AM::lsr: return 1; |
| 116 | case ARM_AM::asr: return 2; |
| 117 | case ARM_AM::ror: |
| 118 | case ARM_AM::rrx: return 3; |
| 119 | } |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | /// getAddrMode2OpValue - Return encoding for addrmode2 operands. |
| 124 | uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx, |
| 125 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 126 | |
| 127 | /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands. |
| 128 | uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx, |
| 129 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 130 | |
Jim Grosbach | 7eab97f | 2010-11-11 16:55:29 +0000 | [diff] [blame] | 131 | /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands. |
| 132 | uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx, |
| 133 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 134 | |
Jim Grosbach | 570a922 | 2010-11-11 01:09:40 +0000 | [diff] [blame] | 135 | /// getAddrMode3OpValue - Return encoding for addrmode3 operands. |
| 136 | uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx, |
| 137 | SmallVectorImpl<MCFixup> &Fixups) const; |
Jim Grosbach | 5d5eb9e | 2010-11-10 23:38:36 +0000 | [diff] [blame] | 138 | |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 139 | /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand. |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 140 | uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx, |
| 141 | SmallVectorImpl<MCFixup> &Fixups) const; |
Jim Grosbach | 3e55612 | 2010-10-26 22:37:02 +0000 | [diff] [blame] | 142 | |
Jim Grosbach | 08bd549 | 2010-10-12 23:00:24 +0000 | [diff] [blame] | 143 | /// getCCOutOpValue - Return encoding of the 's' bit. |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 144 | unsigned getCCOutOpValue(const MCInst &MI, unsigned Op, |
| 145 | SmallVectorImpl<MCFixup> &Fixups) const { |
Jim Grosbach | 08bd549 | 2010-10-12 23:00:24 +0000 | [diff] [blame] | 146 | // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or |
| 147 | // '1' respectively. |
| 148 | return MI.getOperand(Op).getReg() == ARM::CPSR; |
| 149 | } |
Jim Grosbach | ef324d7 | 2010-10-12 23:53:58 +0000 | [diff] [blame] | 150 | |
Jim Grosbach | 2a6a93d | 2010-10-12 23:18:08 +0000 | [diff] [blame] | 151 | /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value. |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 152 | unsigned getSOImmOpValue(const MCInst &MI, unsigned Op, |
| 153 | SmallVectorImpl<MCFixup> &Fixups) const { |
Jim Grosbach | 2a6a93d | 2010-10-12 23:18:08 +0000 | [diff] [blame] | 154 | unsigned SoImm = MI.getOperand(Op).getImm(); |
| 155 | int SoImmVal = ARM_AM::getSOImmVal(SoImm); |
| 156 | assert(SoImmVal != -1 && "Not a valid so_imm value!"); |
| 157 | |
| 158 | // Encode rotate_imm. |
| 159 | unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1) |
| 160 | << ARMII::SoRotImmShift; |
| 161 | |
| 162 | // Encode immed_8. |
| 163 | Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal); |
| 164 | return Binary; |
| 165 | } |
Owen Anderson | 5de6d84 | 2010-11-12 21:12:40 +0000 | [diff] [blame] | 166 | |
| 167 | /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value. |
| 168 | unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op, |
| 169 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 170 | unsigned SoImm = MI.getOperand(Op).getImm(); |
| 171 | unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm); |
| 172 | assert(Encoded != ~0U && "Not a Thumb2 so_imm value?"); |
| 173 | return Encoded; |
| 174 | } |
Jim Grosbach | 08bd549 | 2010-10-12 23:00:24 +0000 | [diff] [blame] | 175 | |
Jim Grosbach | ef324d7 | 2010-10-12 23:53:58 +0000 | [diff] [blame] | 176 | /// getSORegOpValue - Return an encoded so_reg shifted register value. |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 177 | unsigned getSORegOpValue(const MCInst &MI, unsigned Op, |
| 178 | SmallVectorImpl<MCFixup> &Fixups) const; |
Owen Anderson | 5de6d84 | 2010-11-12 21:12:40 +0000 | [diff] [blame] | 179 | unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op, |
| 180 | SmallVectorImpl<MCFixup> &Fixups) const; |
Jim Grosbach | ef324d7 | 2010-10-12 23:53:58 +0000 | [diff] [blame] | 181 | |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 182 | unsigned getRotImmOpValue(const MCInst &MI, unsigned Op, |
| 183 | SmallVectorImpl<MCFixup> &Fixups) const { |
Jim Grosbach | b35ad41 | 2010-10-13 19:56:10 +0000 | [diff] [blame] | 184 | switch (MI.getOperand(Op).getImm()) { |
| 185 | default: assert (0 && "Not a valid rot_imm value!"); |
| 186 | case 0: return 0; |
| 187 | case 8: return 1; |
| 188 | case 16: return 2; |
| 189 | case 24: return 3; |
| 190 | } |
| 191 | } |
| 192 | |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 193 | unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op, |
| 194 | SmallVectorImpl<MCFixup> &Fixups) const { |
Jim Grosbach | 8abe32a | 2010-10-15 17:15:16 +0000 | [diff] [blame] | 195 | return MI.getOperand(Op).getImm() - 1; |
| 196 | } |
Jim Grosbach | d8a11c2 | 2010-10-29 23:21:03 +0000 | [diff] [blame] | 197 | |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 198 | unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op, |
| 199 | SmallVectorImpl<MCFixup> &Fixups) const { |
Owen Anderson | 498ec20 | 2010-10-27 22:49:00 +0000 | [diff] [blame] | 200 | return 64 - MI.getOperand(Op).getImm(); |
| 201 | } |
Jim Grosbach | 8abe32a | 2010-10-15 17:15:16 +0000 | [diff] [blame] | 202 | |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 203 | unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op, |
| 204 | SmallVectorImpl<MCFixup> &Fixups) const; |
Jim Grosbach | 3fea19105 | 2010-10-21 22:03:21 +0000 | [diff] [blame] | 205 | |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 206 | unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op, |
| 207 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 208 | unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op, |
| 209 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 210 | unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op, |
| 211 | SmallVectorImpl<MCFixup> &Fixups) const; |
Jim Grosbach | 6b5252d | 2010-10-30 00:37:59 +0000 | [diff] [blame] | 212 | |
Owen Anderson | c7139a6 | 2010-11-11 19:07:48 +0000 | [diff] [blame] | 213 | unsigned NEONThumb2DataIPostEncoder(const MCInst &MI, |
| 214 | unsigned EncodedValue) const; |
Owen Anderson | 57dac88 | 2010-11-11 21:36:43 +0000 | [diff] [blame] | 215 | unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI, |
| 216 | unsigned EncodedValue) const; |
Owen Anderson | 8f14391 | 2010-11-11 23:12:55 +0000 | [diff] [blame] | 217 | unsigned NEONThumb2DupPostEncoder(const MCInst &MI, |
| 218 | unsigned EncodedValue) const; |
Owen Anderson | c7139a6 | 2010-11-11 19:07:48 +0000 | [diff] [blame] | 219 | |
Jim Grosbach | 7093326 | 2010-11-04 01:12:30 +0000 | [diff] [blame] | 220 | void EmitByte(unsigned char C, raw_ostream &OS) const { |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 221 | OS << (char)C; |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Jim Grosbach | 7093326 | 2010-11-04 01:12:30 +0000 | [diff] [blame] | 224 | void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const { |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 225 | // Output the constant in little endian byte order. |
| 226 | for (unsigned i = 0; i != Size; ++i) { |
Jim Grosbach | 7093326 | 2010-11-04 01:12:30 +0000 | [diff] [blame] | 227 | EmitByte(Val & 255, OS); |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 228 | Val >>= 8; |
| 229 | } |
| 230 | } |
| 231 | |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 232 | void EncodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 233 | SmallVectorImpl<MCFixup> &Fixups) const; |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | } // end anonymous namespace |
| 237 | |
Bill Wendling | 0800ce7 | 2010-11-02 22:53:11 +0000 | [diff] [blame] | 238 | MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM, |
| 239 | MCContext &Ctx) { |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 240 | return new ARMMCCodeEmitter(TM, Ctx); |
| 241 | } |
| 242 | |
Owen Anderson | 57dac88 | 2010-11-11 21:36:43 +0000 | [diff] [blame] | 243 | /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing |
Owen Anderson | c7139a6 | 2010-11-11 19:07:48 +0000 | [diff] [blame] | 244 | /// instructions, and rewrite them to their Thumb2 form if we are currently in |
| 245 | /// Thumb2 mode. |
| 246 | unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI, |
| 247 | unsigned EncodedValue) const { |
| 248 | const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>(); |
| 249 | if (Subtarget.isThumb2()) { |
| 250 | // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved |
| 251 | // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are |
| 252 | // set to 1111. |
| 253 | unsigned Bit24 = EncodedValue & 0x01000000; |
| 254 | unsigned Bit28 = Bit24 << 4; |
| 255 | EncodedValue &= 0xEFFFFFFF; |
| 256 | EncodedValue |= Bit28; |
| 257 | EncodedValue |= 0x0F000000; |
| 258 | } |
| 259 | |
| 260 | return EncodedValue; |
| 261 | } |
| 262 | |
Owen Anderson | 57dac88 | 2010-11-11 21:36:43 +0000 | [diff] [blame] | 263 | /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store |
| 264 | /// instructions, and rewrite them to their Thumb2 form if we are currently in |
| 265 | /// Thumb2 mode. |
| 266 | unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI, |
| 267 | unsigned EncodedValue) const { |
| 268 | const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>(); |
| 269 | if (Subtarget.isThumb2()) { |
| 270 | EncodedValue &= 0xF0FFFFFF; |
| 271 | EncodedValue |= 0x09000000; |
| 272 | } |
| 273 | |
| 274 | return EncodedValue; |
| 275 | } |
| 276 | |
Owen Anderson | 8f14391 | 2010-11-11 23:12:55 +0000 | [diff] [blame] | 277 | /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup |
| 278 | /// instructions, and rewrite them to their Thumb2 form if we are currently in |
| 279 | /// Thumb2 mode. |
| 280 | unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI, |
| 281 | unsigned EncodedValue) const { |
| 282 | const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>(); |
| 283 | if (Subtarget.isThumb2()) { |
| 284 | EncodedValue &= 0x00FFFFFF; |
| 285 | EncodedValue |= 0xEE000000; |
| 286 | } |
| 287 | |
| 288 | return EncodedValue; |
| 289 | } |
| 290 | |
| 291 | |
Owen Anderson | 57dac88 | 2010-11-11 21:36:43 +0000 | [diff] [blame] | 292 | |
Jim Grosbach | 56ac907 | 2010-10-08 21:45:55 +0000 | [diff] [blame] | 293 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 294 | /// operand requires relocation, record the relocation and return zero. |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 295 | unsigned ARMMCCodeEmitter:: |
| 296 | getMachineOpValue(const MCInst &MI, const MCOperand &MO, |
| 297 | SmallVectorImpl<MCFixup> &Fixups) const { |
Bill Wendling | bbbdcd4 | 2010-10-14 02:33:26 +0000 | [diff] [blame] | 298 | if (MO.isReg()) { |
Bill Wendling | 0800ce7 | 2010-11-02 22:53:11 +0000 | [diff] [blame] | 299 | unsigned Reg = MO.getReg(); |
| 300 | unsigned RegNo = getARMRegisterNumbering(Reg); |
Jim Grosbach | d8a11c2 | 2010-10-29 23:21:03 +0000 | [diff] [blame] | 301 | |
Owen Anderson | 90d4cf9 | 2010-10-21 20:49:13 +0000 | [diff] [blame] | 302 | // Q registers are encodes as 2x their register number. |
Bill Wendling | 0800ce7 | 2010-11-02 22:53:11 +0000 | [diff] [blame] | 303 | switch (Reg) { |
| 304 | default: |
| 305 | return RegNo; |
| 306 | case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3: |
| 307 | case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7: |
| 308 | case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11: |
| 309 | case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15: |
| 310 | return 2 * RegNo; |
Owen Anderson | 90d4cf9 | 2010-10-21 20:49:13 +0000 | [diff] [blame] | 311 | } |
Bill Wendling | bbbdcd4 | 2010-10-14 02:33:26 +0000 | [diff] [blame] | 312 | } else if (MO.isImm()) { |
Jim Grosbach | 56ac907 | 2010-10-08 21:45:55 +0000 | [diff] [blame] | 313 | return static_cast<unsigned>(MO.getImm()); |
Bill Wendling | bbbdcd4 | 2010-10-14 02:33:26 +0000 | [diff] [blame] | 314 | } else if (MO.isFPImm()) { |
| 315 | return static_cast<unsigned>(APFloat(MO.getFPImm()) |
| 316 | .bitcastToAPInt().getHiBits(32).getLimitedValue()); |
Jim Grosbach | 56ac907 | 2010-10-08 21:45:55 +0000 | [diff] [blame] | 317 | } |
Bill Wendling | 0800ce7 | 2010-11-02 22:53:11 +0000 | [diff] [blame] | 318 | |
Jim Grosbach | 817c1a6 | 2010-11-19 00:27:09 +0000 | [diff] [blame] | 319 | llvm_unreachable("Unable to encode MCOperand!"); |
Jim Grosbach | 56ac907 | 2010-10-08 21:45:55 +0000 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
Bill Wendling | 5df0e0a | 2010-11-02 22:31:46 +0000 | [diff] [blame] | 323 | /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand. |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 324 | bool ARMMCCodeEmitter:: |
| 325 | EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg, |
| 326 | unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const { |
Jim Grosbach | 3e55612 | 2010-10-26 22:37:02 +0000 | [diff] [blame] | 327 | const MCOperand &MO = MI.getOperand(OpIdx); |
| 328 | const MCOperand &MO1 = MI.getOperand(OpIdx + 1); |
Jim Grosbach | 9af3d1c | 2010-11-01 23:45:50 +0000 | [diff] [blame] | 329 | |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 330 | Reg = getARMRegisterNumbering(MO.getReg()); |
| 331 | |
| 332 | int32_t SImm = MO1.getImm(); |
| 333 | bool isAdd = true; |
Bill Wendling | 5df0e0a | 2010-11-02 22:31:46 +0000 | [diff] [blame] | 334 | |
Jim Grosbach | ab682a2 | 2010-10-28 18:34:10 +0000 | [diff] [blame] | 335 | // Special value for #-0 |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 336 | if (SImm == INT32_MIN) |
| 337 | SImm = 0; |
Bill Wendling | 5df0e0a | 2010-11-02 22:31:46 +0000 | [diff] [blame] | 338 | |
Jim Grosbach | ab682a2 | 2010-10-28 18:34:10 +0000 | [diff] [blame] | 339 | // Immediate is always encoded as positive. The 'U' bit controls add vs sub. |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 340 | if (SImm < 0) { |
| 341 | SImm = -SImm; |
| 342 | isAdd = false; |
| 343 | } |
Bill Wendling | 5df0e0a | 2010-11-02 22:31:46 +0000 | [diff] [blame] | 344 | |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 345 | Imm = SImm; |
| 346 | return isAdd; |
| 347 | } |
| 348 | |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 349 | /// getBranchTargetOpValue - Return encoding info for 24-bit immediate |
| 350 | /// branch target. |
| 351 | uint32_t ARMMCCodeEmitter:: |
| 352 | getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, |
| 353 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 354 | const MCOperand &MO = MI.getOperand(OpIdx); |
| 355 | |
| 356 | // If the destination is an immediate, we have nothing to do. |
| 357 | if (MO.isImm()) return MO.getImm(); |
| 358 | assert (MO.isExpr() && "Unexpected branch target type!"); |
| 359 | const MCExpr *Expr = MO.getExpr(); |
| 360 | MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch); |
| 361 | Fixups.push_back(MCFixup::Create(0, Expr, Kind)); |
| 362 | |
| 363 | // All of the information is in the fixup. |
| 364 | return 0; |
| 365 | } |
| 366 | |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 367 | /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand. |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 368 | uint32_t ARMMCCodeEmitter:: |
| 369 | getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx, |
| 370 | SmallVectorImpl<MCFixup> &Fixups) const { |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 371 | // {17-13} = reg |
| 372 | // {12} = (U)nsigned (add == '1', sub == '0') |
| 373 | // {11-0} = imm12 |
| 374 | unsigned Reg, Imm12; |
Jim Grosbach | 7093326 | 2010-11-04 01:12:30 +0000 | [diff] [blame] | 375 | bool isAdd = true; |
| 376 | // If The first operand isn't a register, we have a label reference. |
| 377 | const MCOperand &MO = MI.getOperand(OpIdx); |
| 378 | if (!MO.isReg()) { |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 379 | Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC. |
Jim Grosbach | 7093326 | 2010-11-04 01:12:30 +0000 | [diff] [blame] | 380 | Imm12 = 0; |
| 381 | |
| 382 | assert(MO.isExpr() && "Unexpected machine operand type!"); |
| 383 | const MCExpr *Expr = MO.getExpr(); |
| 384 | MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12); |
| 385 | Fixups.push_back(MCFixup::Create(0, Expr, Kind)); |
| 386 | |
| 387 | ++MCNumCPRelocations; |
| 388 | } else |
| 389 | isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups); |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 390 | |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 391 | uint32_t Binary = Imm12 & 0xfff; |
| 392 | // Immediate is always encoded as positive. The 'U' bit controls add vs sub. |
Jim Grosbach | ab682a2 | 2010-10-28 18:34:10 +0000 | [diff] [blame] | 393 | if (isAdd) |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 394 | Binary |= (1 << 12); |
| 395 | Binary |= (Reg << 13); |
| 396 | return Binary; |
| 397 | } |
| 398 | |
Jim Grosbach | 54fea63 | 2010-11-09 17:20:53 +0000 | [diff] [blame] | 399 | uint32_t ARMMCCodeEmitter:: |
Jason W Kim | 837caa9 | 2010-11-18 23:37:15 +0000 | [diff] [blame] | 400 | getMovtImmOpValue(const MCInst &MI, unsigned OpIdx, |
| 401 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 402 | // {20-16} = imm{15-12} |
| 403 | // {11-0} = imm{11-0} |
| 404 | const MCOperand &MO = MI.getOperand(OpIdx); |
| 405 | if (MO.isImm()) { |
| 406 | return static_cast<unsigned>(MO.getImm()); |
| 407 | } else if (const MCSymbolRefExpr *Expr = |
| 408 | dyn_cast<MCSymbolRefExpr>(MO.getExpr())) { |
| 409 | MCFixupKind Kind; |
| 410 | switch (Expr->getKind()) { |
Duncan Sands | 3d93893 | 2010-11-22 09:38:00 +0000 | [diff] [blame] | 411 | default: assert(0 && "Unsupported ARMFixup"); |
Jason W Kim | 837caa9 | 2010-11-18 23:37:15 +0000 | [diff] [blame] | 412 | case MCSymbolRefExpr::VK_ARM_HI16: |
| 413 | Kind = MCFixupKind(ARM::fixup_arm_movt_hi16); |
| 414 | break; |
| 415 | case MCSymbolRefExpr::VK_ARM_LO16: |
| 416 | Kind = MCFixupKind(ARM::fixup_arm_movw_lo16); |
| 417 | break; |
Jason W Kim | 837caa9 | 2010-11-18 23:37:15 +0000 | [diff] [blame] | 418 | } |
| 419 | Fixups.push_back(MCFixup::Create(0, Expr, Kind)); |
| 420 | return 0; |
Jim Grosbach | 817c1a6 | 2010-11-19 00:27:09 +0000 | [diff] [blame] | 421 | }; |
| 422 | llvm_unreachable("Unsupported MCExpr type in MCOperand!"); |
Jason W Kim | 837caa9 | 2010-11-18 23:37:15 +0000 | [diff] [blame] | 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | uint32_t ARMMCCodeEmitter:: |
Jim Grosbach | 54fea63 | 2010-11-09 17:20:53 +0000 | [diff] [blame] | 427 | getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx, |
| 428 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 429 | const MCOperand &MO = MI.getOperand(OpIdx); |
| 430 | const MCOperand &MO1 = MI.getOperand(OpIdx+1); |
| 431 | const MCOperand &MO2 = MI.getOperand(OpIdx+2); |
| 432 | unsigned Rn = getARMRegisterNumbering(MO.getReg()); |
| 433 | unsigned Rm = getARMRegisterNumbering(MO1.getReg()); |
Jim Grosbach | 54fea63 | 2010-11-09 17:20:53 +0000 | [diff] [blame] | 434 | unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()); |
| 435 | bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add; |
Jim Grosbach | 99f53d1 | 2010-11-15 20:47:07 +0000 | [diff] [blame] | 436 | ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm()); |
| 437 | unsigned SBits = getShiftOp(ShOp); |
Jim Grosbach | 54fea63 | 2010-11-09 17:20:53 +0000 | [diff] [blame] | 438 | |
| 439 | // {16-13} = Rn |
| 440 | // {12} = isAdd |
| 441 | // {11-0} = shifter |
| 442 | // {3-0} = Rm |
| 443 | // {4} = 0 |
| 444 | // {6-5} = type |
| 445 | // {11-7} = imm |
Jim Grosbach | 570a922 | 2010-11-11 01:09:40 +0000 | [diff] [blame] | 446 | uint32_t Binary = Rm; |
Jim Grosbach | 54fea63 | 2010-11-09 17:20:53 +0000 | [diff] [blame] | 447 | Binary |= Rn << 13; |
| 448 | Binary |= SBits << 5; |
| 449 | Binary |= ShImm << 7; |
| 450 | if (isAdd) |
| 451 | Binary |= 1 << 12; |
| 452 | return Binary; |
| 453 | } |
| 454 | |
Jim Grosbach | 570a922 | 2010-11-11 01:09:40 +0000 | [diff] [blame] | 455 | uint32_t ARMMCCodeEmitter:: |
Jim Grosbach | 99f53d1 | 2010-11-15 20:47:07 +0000 | [diff] [blame] | 456 | getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx, |
| 457 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 458 | // {17-14} Rn |
| 459 | // {13} 1 == imm12, 0 == Rm |
| 460 | // {12} isAdd |
| 461 | // {11-0} imm12/Rm |
| 462 | const MCOperand &MO = MI.getOperand(OpIdx); |
| 463 | unsigned Rn = getARMRegisterNumbering(MO.getReg()); |
| 464 | uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups); |
| 465 | Binary |= Rn << 14; |
| 466 | return Binary; |
| 467 | } |
| 468 | |
| 469 | uint32_t ARMMCCodeEmitter:: |
| 470 | getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx, |
| 471 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 472 | // {13} 1 == imm12, 0 == Rm |
| 473 | // {12} isAdd |
| 474 | // {11-0} imm12/Rm |
| 475 | const MCOperand &MO = MI.getOperand(OpIdx); |
| 476 | const MCOperand &MO1 = MI.getOperand(OpIdx+1); |
| 477 | unsigned Imm = MO1.getImm(); |
| 478 | bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add; |
| 479 | bool isReg = MO.getReg() != 0; |
| 480 | uint32_t Binary = ARM_AM::getAM2Offset(Imm); |
| 481 | // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12 |
| 482 | if (isReg) { |
| 483 | ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm); |
| 484 | Binary <<= 7; // Shift amount is bits [11:7] |
| 485 | Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5] |
| 486 | Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0] |
| 487 | } |
| 488 | return Binary | (isAdd << 12) | (isReg << 13); |
| 489 | } |
| 490 | |
| 491 | uint32_t ARMMCCodeEmitter:: |
Jim Grosbach | 7eab97f | 2010-11-11 16:55:29 +0000 | [diff] [blame] | 492 | getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx, |
| 493 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 494 | // {9} 1 == imm8, 0 == Rm |
| 495 | // {8} isAdd |
| 496 | // {7-4} imm7_4/zero |
| 497 | // {3-0} imm3_0/Rm |
| 498 | const MCOperand &MO = MI.getOperand(OpIdx); |
| 499 | const MCOperand &MO1 = MI.getOperand(OpIdx+1); |
| 500 | unsigned Imm = MO1.getImm(); |
| 501 | bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add; |
| 502 | bool isImm = MO.getReg() == 0; |
| 503 | uint32_t Imm8 = ARM_AM::getAM3Offset(Imm); |
| 504 | // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8 |
| 505 | if (!isImm) |
| 506 | Imm8 = getARMRegisterNumbering(MO.getReg()); |
| 507 | return Imm8 | (isAdd << 8) | (isImm << 9); |
| 508 | } |
| 509 | |
| 510 | uint32_t ARMMCCodeEmitter:: |
Jim Grosbach | 570a922 | 2010-11-11 01:09:40 +0000 | [diff] [blame] | 511 | getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx, |
| 512 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 513 | // {13} 1 == imm8, 0 == Rm |
| 514 | // {12-9} Rn |
| 515 | // {8} isAdd |
| 516 | // {7-4} imm7_4/zero |
| 517 | // {3-0} imm3_0/Rm |
| 518 | const MCOperand &MO = MI.getOperand(OpIdx); |
| 519 | const MCOperand &MO1 = MI.getOperand(OpIdx+1); |
| 520 | const MCOperand &MO2 = MI.getOperand(OpIdx+2); |
| 521 | unsigned Rn = getARMRegisterNumbering(MO.getReg()); |
| 522 | unsigned Imm = MO2.getImm(); |
| 523 | bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add; |
| 524 | bool isImm = MO1.getReg() == 0; |
| 525 | uint32_t Imm8 = ARM_AM::getAM3Offset(Imm); |
| 526 | // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8 |
| 527 | if (!isImm) |
| 528 | Imm8 = getARMRegisterNumbering(MO1.getReg()); |
| 529 | return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13); |
| 530 | } |
| 531 | |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 532 | /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand. |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 533 | uint32_t ARMMCCodeEmitter:: |
| 534 | getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx, |
| 535 | SmallVectorImpl<MCFixup> &Fixups) const { |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 536 | // {12-9} = reg |
| 537 | // {8} = (U)nsigned (add == '1', sub == '0') |
| 538 | // {7-0} = imm8 |
| 539 | unsigned Reg, Imm8; |
Jim Grosbach | 7093326 | 2010-11-04 01:12:30 +0000 | [diff] [blame] | 540 | // If The first operand isn't a register, we have a label reference. |
| 541 | const MCOperand &MO = MI.getOperand(OpIdx); |
| 542 | if (!MO.isReg()) { |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 543 | Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC. |
Jim Grosbach | 7093326 | 2010-11-04 01:12:30 +0000 | [diff] [blame] | 544 | Imm8 = 0; |
| 545 | |
| 546 | assert(MO.isExpr() && "Unexpected machine operand type!"); |
| 547 | const MCExpr *Expr = MO.getExpr(); |
| 548 | MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_vfp_pcrel_12); |
| 549 | Fixups.push_back(MCFixup::Create(0, Expr, Kind)); |
| 550 | |
| 551 | ++MCNumCPRelocations; |
| 552 | } else |
| 553 | EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups); |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 554 | |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 555 | uint32_t Binary = ARM_AM::getAM5Offset(Imm8); |
| 556 | // Immediate is always encoded as positive. The 'U' bit controls add vs sub. |
| 557 | if (ARM_AM::getAM5Op(Imm8) == ARM_AM::add) |
| 558 | Binary |= (1 << 8); |
| 559 | Binary |= (Reg << 9); |
Jim Grosbach | 3e55612 | 2010-10-26 22:37:02 +0000 | [diff] [blame] | 560 | return Binary; |
| 561 | } |
| 562 | |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 563 | unsigned ARMMCCodeEmitter:: |
| 564 | getSORegOpValue(const MCInst &MI, unsigned OpIdx, |
| 565 | SmallVectorImpl<MCFixup> &Fixups) const { |
Bill Wendling | 0800ce7 | 2010-11-02 22:53:11 +0000 | [diff] [blame] | 566 | // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be |
| 567 | // shifted. The second is either Rs, the amount to shift by, or reg0 in which |
| 568 | // case the imm contains the amount to shift by. |
Jim Grosbach | 35b2de0 | 2010-11-03 22:03:20 +0000 | [diff] [blame] | 569 | // |
Jim Grosbach | ef324d7 | 2010-10-12 23:53:58 +0000 | [diff] [blame] | 570 | // {3-0} = Rm. |
Bill Wendling | 0800ce7 | 2010-11-02 22:53:11 +0000 | [diff] [blame] | 571 | // {4} = 1 if reg shift, 0 if imm shift |
Jim Grosbach | ef324d7 | 2010-10-12 23:53:58 +0000 | [diff] [blame] | 572 | // {6-5} = type |
| 573 | // If reg shift: |
Jim Grosbach | ef324d7 | 2010-10-12 23:53:58 +0000 | [diff] [blame] | 574 | // {11-8} = Rs |
Bill Wendling | 0800ce7 | 2010-11-02 22:53:11 +0000 | [diff] [blame] | 575 | // {7} = 0 |
Jim Grosbach | ef324d7 | 2010-10-12 23:53:58 +0000 | [diff] [blame] | 576 | // else (imm shift) |
| 577 | // {11-7} = imm |
| 578 | |
| 579 | const MCOperand &MO = MI.getOperand(OpIdx); |
| 580 | const MCOperand &MO1 = MI.getOperand(OpIdx + 1); |
| 581 | const MCOperand &MO2 = MI.getOperand(OpIdx + 2); |
| 582 | ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm()); |
| 583 | |
| 584 | // Encode Rm. |
| 585 | unsigned Binary = getARMRegisterNumbering(MO.getReg()); |
| 586 | |
| 587 | // Encode the shift opcode. |
| 588 | unsigned SBits = 0; |
| 589 | unsigned Rs = MO1.getReg(); |
| 590 | if (Rs) { |
| 591 | // Set shift operand (bit[7:4]). |
| 592 | // LSL - 0001 |
| 593 | // LSR - 0011 |
| 594 | // ASR - 0101 |
| 595 | // ROR - 0111 |
| 596 | // RRX - 0110 and bit[11:8] clear. |
| 597 | switch (SOpc) { |
| 598 | default: llvm_unreachable("Unknown shift opc!"); |
| 599 | case ARM_AM::lsl: SBits = 0x1; break; |
| 600 | case ARM_AM::lsr: SBits = 0x3; break; |
| 601 | case ARM_AM::asr: SBits = 0x5; break; |
| 602 | case ARM_AM::ror: SBits = 0x7; break; |
| 603 | case ARM_AM::rrx: SBits = 0x6; break; |
| 604 | } |
| 605 | } else { |
| 606 | // Set shift operand (bit[6:4]). |
| 607 | // LSL - 000 |
| 608 | // LSR - 010 |
| 609 | // ASR - 100 |
| 610 | // ROR - 110 |
| 611 | switch (SOpc) { |
| 612 | default: llvm_unreachable("Unknown shift opc!"); |
| 613 | case ARM_AM::lsl: SBits = 0x0; break; |
| 614 | case ARM_AM::lsr: SBits = 0x2; break; |
| 615 | case ARM_AM::asr: SBits = 0x4; break; |
| 616 | case ARM_AM::ror: SBits = 0x6; break; |
| 617 | } |
| 618 | } |
Bill Wendling | 0800ce7 | 2010-11-02 22:53:11 +0000 | [diff] [blame] | 619 | |
Jim Grosbach | ef324d7 | 2010-10-12 23:53:58 +0000 | [diff] [blame] | 620 | Binary |= SBits << 4; |
| 621 | if (SOpc == ARM_AM::rrx) |
| 622 | return Binary; |
| 623 | |
| 624 | // Encode the shift operation Rs or shift_imm (except rrx). |
| 625 | if (Rs) { |
| 626 | // Encode Rs bit[11:8]. |
| 627 | assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0); |
| 628 | return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift); |
| 629 | } |
| 630 | |
| 631 | // Encode shift_imm bit[11:7]. |
| 632 | return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7; |
| 633 | } |
| 634 | |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 635 | unsigned ARMMCCodeEmitter:: |
Owen Anderson | 5de6d84 | 2010-11-12 21:12:40 +0000 | [diff] [blame] | 636 | getT2SORegOpValue(const MCInst &MI, unsigned OpIdx, |
| 637 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 638 | // Sub-operands are [reg, imm]. The first register is Rm, the reg to be |
| 639 | // shifted. The second is the amount to shift by. |
| 640 | // |
| 641 | // {3-0} = Rm. |
| 642 | // {4} = 0 |
| 643 | // {6-5} = type |
| 644 | // {11-7} = imm |
| 645 | |
| 646 | const MCOperand &MO = MI.getOperand(OpIdx); |
| 647 | const MCOperand &MO1 = MI.getOperand(OpIdx + 1); |
| 648 | ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm()); |
| 649 | |
| 650 | // Encode Rm. |
| 651 | unsigned Binary = getARMRegisterNumbering(MO.getReg()); |
| 652 | |
| 653 | // Encode the shift opcode. |
| 654 | unsigned SBits = 0; |
| 655 | // Set shift operand (bit[6:4]). |
| 656 | // LSL - 000 |
| 657 | // LSR - 010 |
| 658 | // ASR - 100 |
| 659 | // ROR - 110 |
| 660 | switch (SOpc) { |
| 661 | default: llvm_unreachable("Unknown shift opc!"); |
| 662 | case ARM_AM::lsl: SBits = 0x0; break; |
| 663 | case ARM_AM::lsr: SBits = 0x2; break; |
| 664 | case ARM_AM::asr: SBits = 0x4; break; |
| 665 | case ARM_AM::ror: SBits = 0x6; break; |
| 666 | } |
| 667 | |
| 668 | Binary |= SBits << 4; |
| 669 | if (SOpc == ARM_AM::rrx) |
| 670 | return Binary; |
| 671 | |
| 672 | // Encode shift_imm bit[11:7]. |
| 673 | return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7; |
| 674 | } |
| 675 | |
| 676 | unsigned ARMMCCodeEmitter:: |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 677 | getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op, |
| 678 | SmallVectorImpl<MCFixup> &Fixups) const { |
Jim Grosbach | 3fea19105 | 2010-10-21 22:03:21 +0000 | [diff] [blame] | 679 | // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the |
| 680 | // msb of the mask. |
| 681 | const MCOperand &MO = MI.getOperand(Op); |
| 682 | uint32_t v = ~MO.getImm(); |
| 683 | uint32_t lsb = CountTrailingZeros_32(v); |
| 684 | uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1; |
| 685 | assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!"); |
| 686 | return lsb | (msb << 5); |
| 687 | } |
| 688 | |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 689 | unsigned ARMMCCodeEmitter:: |
| 690 | getRegisterListOpValue(const MCInst &MI, unsigned Op, |
Bill Wendling | 5e559a2 | 2010-11-09 00:30:18 +0000 | [diff] [blame] | 691 | SmallVectorImpl<MCFixup> &Fixups) const { |
Bill Wendling | 6bc105a | 2010-11-17 00:45:23 +0000 | [diff] [blame] | 692 | // VLDM/VSTM: |
| 693 | // {12-8} = Vd |
| 694 | // {7-0} = Number of registers |
| 695 | // |
| 696 | // LDM/STM: |
| 697 | // {15-0} = Bitfield of GPRs. |
| 698 | unsigned Reg = MI.getOperand(Op).getReg(); |
| 699 | bool SPRRegs = ARM::SPRRegClass.contains(Reg); |
| 700 | bool DPRRegs = ARM::DPRRegClass.contains(Reg); |
| 701 | |
Bill Wendling | 5e559a2 | 2010-11-09 00:30:18 +0000 | [diff] [blame] | 702 | unsigned Binary = 0; |
Bill Wendling | 6bc105a | 2010-11-17 00:45:23 +0000 | [diff] [blame] | 703 | |
| 704 | if (SPRRegs || DPRRegs) { |
| 705 | // VLDM/VSTM |
| 706 | unsigned RegNo = getARMRegisterNumbering(Reg); |
| 707 | unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff; |
| 708 | Binary |= (RegNo & 0x1f) << 8; |
| 709 | if (SPRRegs) |
| 710 | Binary |= NumRegs; |
| 711 | else |
| 712 | Binary |= NumRegs * 2; |
| 713 | } else { |
| 714 | for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) { |
| 715 | unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg()); |
| 716 | Binary |= 1 << RegNo; |
| 717 | } |
Bill Wendling | 5e559a2 | 2010-11-09 00:30:18 +0000 | [diff] [blame] | 718 | } |
Bill Wendling | 6bc105a | 2010-11-17 00:45:23 +0000 | [diff] [blame] | 719 | |
Jim Grosbach | 6b5252d | 2010-10-30 00:37:59 +0000 | [diff] [blame] | 720 | return Binary; |
| 721 | } |
| 722 | |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 723 | unsigned ARMMCCodeEmitter:: |
| 724 | getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op, |
| 725 | SmallVectorImpl<MCFixup> &Fixups) const { |
Owen Anderson | d9aa7d3 | 2010-11-02 00:05:05 +0000 | [diff] [blame] | 726 | const MCOperand &Reg = MI.getOperand(Op); |
Bill Wendling | 0800ce7 | 2010-11-02 22:53:11 +0000 | [diff] [blame] | 727 | const MCOperand &Imm = MI.getOperand(Op + 1); |
Jim Grosbach | 35b2de0 | 2010-11-03 22:03:20 +0000 | [diff] [blame] | 728 | |
Owen Anderson | d9aa7d3 | 2010-11-02 00:05:05 +0000 | [diff] [blame] | 729 | unsigned RegNo = getARMRegisterNumbering(Reg.getReg()); |
Bill Wendling | 0800ce7 | 2010-11-02 22:53:11 +0000 | [diff] [blame] | 730 | unsigned Align = 0; |
| 731 | |
| 732 | switch (Imm.getImm()) { |
| 733 | default: break; |
| 734 | case 2: |
| 735 | case 4: |
| 736 | case 8: Align = 0x01; break; |
| 737 | case 16: Align = 0x02; break; |
| 738 | case 32: Align = 0x03; break; |
Owen Anderson | d9aa7d3 | 2010-11-02 00:05:05 +0000 | [diff] [blame] | 739 | } |
Bill Wendling | 0800ce7 | 2010-11-02 22:53:11 +0000 | [diff] [blame] | 740 | |
Owen Anderson | d9aa7d3 | 2010-11-02 00:05:05 +0000 | [diff] [blame] | 741 | return RegNo | (Align << 4); |
| 742 | } |
| 743 | |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 744 | unsigned ARMMCCodeEmitter:: |
| 745 | getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op, |
| 746 | SmallVectorImpl<MCFixup> &Fixups) const { |
Bill Wendling | 0800ce7 | 2010-11-02 22:53:11 +0000 | [diff] [blame] | 747 | const MCOperand &MO = MI.getOperand(Op); |
| 748 | if (MO.getReg() == 0) return 0x0D; |
| 749 | return MO.getReg(); |
Owen Anderson | cf667be | 2010-11-02 01:24:55 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 752 | void ARMMCCodeEmitter:: |
| 753 | EncodeInstruction(const MCInst &MI, raw_ostream &OS, |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 754 | SmallVectorImpl<MCFixup> &Fixups) const { |
Jim Grosbach | d6d4b42 | 2010-10-07 22:12:50 +0000 | [diff] [blame] | 755 | // Pseudo instructions don't get encoded. |
Bill Wendling | 7292e0a | 2010-11-02 22:44:12 +0000 | [diff] [blame] | 756 | const TargetInstrDesc &Desc = TII.get(MI.getOpcode()); |
Jim Grosbach | e50e6bc | 2010-11-11 23:41:09 +0000 | [diff] [blame] | 757 | uint64_t TSFlags = Desc.TSFlags; |
| 758 | if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo) |
Jim Grosbach | d6d4b42 | 2010-10-07 22:12:50 +0000 | [diff] [blame] | 759 | return; |
Jim Grosbach | e50e6bc | 2010-11-11 23:41:09 +0000 | [diff] [blame] | 760 | int Size; |
| 761 | // Basic size info comes from the TSFlags field. |
| 762 | switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) { |
| 763 | default: llvm_unreachable("Unexpected instruction size!"); |
| 764 | case ARMII::Size2Bytes: Size = 2; break; |
| 765 | case ARMII::Size4Bytes: Size = 4; break; |
| 766 | } |
| 767 | EmitConstant(getBinaryCodeForInstr(MI, Fixups), Size, OS); |
Bill Wendling | 7292e0a | 2010-11-02 22:44:12 +0000 | [diff] [blame] | 768 | ++MCNumEmitted; // Keep track of the # of mi's emitted. |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 769 | } |
Jim Grosbach | 9af82ba | 2010-10-07 21:57:55 +0000 | [diff] [blame] | 770 | |
Jim Grosbach | 806e80e | 2010-11-03 23:52:49 +0000 | [diff] [blame] | 771 | #include "ARMGenMCCodeEmitter.inc" |