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 | |
| 14 | #define DEBUG_TYPE "arm-emitter" |
| 15 | #include "ARM.h" |
Jim Grosbach | 42fac8e | 2010-10-11 23:16:21 +0000 | [diff] [blame] | 16 | #include "ARMAddressingModes.h" |
Jim Grosbach | d6d4b42 | 2010-10-07 22:12:50 +0000 | [diff] [blame] | 17 | #include "ARMInstrInfo.h" |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCCodeEmitter.h" |
| 19 | #include "llvm/MC/MCExpr.h" |
| 20 | #include "llvm/MC/MCInst.h" |
Jim Grosbach | d6d4b42 | 2010-10-07 22:12:50 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | using namespace llvm; |
| 24 | |
Jim Grosbach | d6d4b42 | 2010-10-07 22:12:50 +0000 | [diff] [blame] | 25 | STATISTIC(MCNumEmitted, "Number of MC instructions emitted"); |
| 26 | |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | class ARMMCCodeEmitter : public MCCodeEmitter { |
| 29 | ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT |
| 30 | void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT |
| 31 | const TargetMachine &TM; |
| 32 | const TargetInstrInfo &TII; |
| 33 | MCContext &Ctx; |
| 34 | |
| 35 | public: |
| 36 | ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx) |
| 37 | : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) { |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | ~ARMMCCodeEmitter() {} |
| 41 | |
Jim Grosbach | 0de6ab3 | 2010-10-12 17:11:26 +0000 | [diff] [blame] | 42 | unsigned getMachineSoImmOpValue(unsigned SoImm) const; |
| 43 | |
Jim Grosbach | 9af82ba | 2010-10-07 21:57:55 +0000 | [diff] [blame] | 44 | // getBinaryCodeForInstr - TableGen'erated function for getting the |
| 45 | // binary encoding for an instruction. |
Jim Grosbach | bade37b | 2010-10-08 00:21:28 +0000 | [diff] [blame] | 46 | unsigned getBinaryCodeForInstr(const MCInst &MI) const; |
Jim Grosbach | 9af82ba | 2010-10-07 21:57:55 +0000 | [diff] [blame] | 47 | |
| 48 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 49 | /// operand requires relocation, record the relocation and return zero. |
Jim Grosbach | 3e09413 | 2010-10-08 17:45:54 +0000 | [diff] [blame] | 50 | unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO) const; |
Jim Grosbach | 9af82ba | 2010-10-07 21:57:55 +0000 | [diff] [blame] | 51 | |
Jim Grosbach | 08bd549 | 2010-10-12 23:00:24 +0000 | [diff] [blame] | 52 | /// getCCOutOpValue - Return encoding of the 's' bit. |
| 53 | unsigned getCCOutOpValue(const MCInst &MI, unsigned Op) const { |
| 54 | // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or |
| 55 | // '1' respectively. |
| 56 | return MI.getOperand(Op).getReg() == ARM::CPSR; |
| 57 | } |
Jim Grosbach | ef324d7 | 2010-10-12 23:53:58 +0000 | [diff] [blame^] | 58 | |
Jim Grosbach | 2a6a93d | 2010-10-12 23:18:08 +0000 | [diff] [blame] | 59 | /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value. |
| 60 | unsigned getSOImmOpValue(const MCInst &MI, unsigned Op) const { |
| 61 | unsigned SoImm = MI.getOperand(Op).getImm(); |
| 62 | int SoImmVal = ARM_AM::getSOImmVal(SoImm); |
| 63 | assert(SoImmVal != -1 && "Not a valid so_imm value!"); |
| 64 | |
| 65 | // Encode rotate_imm. |
| 66 | unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1) |
| 67 | << ARMII::SoRotImmShift; |
| 68 | |
| 69 | // Encode immed_8. |
| 70 | Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal); |
| 71 | return Binary; |
| 72 | } |
Jim Grosbach | 08bd549 | 2010-10-12 23:00:24 +0000 | [diff] [blame] | 73 | |
Jim Grosbach | ef324d7 | 2010-10-12 23:53:58 +0000 | [diff] [blame^] | 74 | /// getSORegOpValue - Return an encoded so_reg shifted register value. |
| 75 | unsigned getSORegOpValue(const MCInst &MI, unsigned Op) const; |
| 76 | |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 77 | unsigned getNumFixupKinds() const { |
| 78 | assert(0 && "ARMMCCodeEmitter::getNumFixupKinds() not yet implemented."); |
Michael J. Spencer | 895dda6 | 2010-09-18 17:54:37 +0000 | [diff] [blame] | 79 | return 0; |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const { |
| 83 | static MCFixupKindInfo rtn; |
| 84 | assert(0 && "ARMMCCodeEmitter::getFixupKindInfo() not yet implemented."); |
| 85 | return rtn; |
| 86 | } |
| 87 | |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 88 | void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const { |
| 89 | OS << (char)C; |
| 90 | ++CurByte; |
| 91 | } |
| 92 | |
| 93 | void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte, |
| 94 | raw_ostream &OS) const { |
| 95 | // Output the constant in little endian byte order. |
| 96 | for (unsigned i = 0; i != Size; ++i) { |
| 97 | EmitByte(Val & 255, CurByte, OS); |
| 98 | Val >>= 8; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void EmitImmediate(const MCOperand &Disp, |
| 103 | unsigned ImmSize, MCFixupKind FixupKind, |
| 104 | unsigned &CurByte, raw_ostream &OS, |
| 105 | SmallVectorImpl<MCFixup> &Fixups, |
| 106 | int ImmOffset = 0) const; |
| 107 | |
| 108 | void EncodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 109 | SmallVectorImpl<MCFixup> &Fixups) const; |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | } // end anonymous namespace |
| 113 | |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 114 | MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, |
| 115 | TargetMachine &TM, |
| 116 | MCContext &Ctx) { |
| 117 | return new ARMMCCodeEmitter(TM, Ctx); |
| 118 | } |
| 119 | |
| 120 | void ARMMCCodeEmitter:: |
| 121 | EmitImmediate(const MCOperand &DispOp, unsigned Size, MCFixupKind FixupKind, |
| 122 | unsigned &CurByte, raw_ostream &OS, |
| 123 | SmallVectorImpl<MCFixup> &Fixups, int ImmOffset) const { |
| 124 | assert(0 && "ARMMCCodeEmitter::EmitImmediate() not yet implemented."); |
| 125 | } |
| 126 | |
Jim Grosbach | 56ac907 | 2010-10-08 21:45:55 +0000 | [diff] [blame] | 127 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 128 | /// operand requires relocation, record the relocation and return zero. |
| 129 | unsigned ARMMCCodeEmitter::getMachineOpValue(const MCInst &MI, |
| 130 | const MCOperand &MO) const { |
| 131 | if (MO.isReg()) |
Jim Grosbach | 56ac907 | 2010-10-08 21:45:55 +0000 | [diff] [blame] | 132 | return getARMRegisterNumbering(MO.getReg()); |
Jim Grosbach | 0de6ab3 | 2010-10-12 17:11:26 +0000 | [diff] [blame] | 133 | else if (MO.isImm()) { |
Jim Grosbach | 56ac907 | 2010-10-08 21:45:55 +0000 | [diff] [blame] | 134 | return static_cast<unsigned>(MO.getImm()); |
Jim Grosbach | 0de6ab3 | 2010-10-12 17:11:26 +0000 | [diff] [blame] | 135 | } else { |
Jim Grosbach | 56ac907 | 2010-10-08 21:45:55 +0000 | [diff] [blame] | 136 | #ifndef NDEBUG |
| 137 | errs() << MO; |
| 138 | #endif |
| 139 | llvm_unreachable(0); |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
Jim Grosbach | ef324d7 | 2010-10-12 23:53:58 +0000 | [diff] [blame^] | 144 | |
| 145 | unsigned ARMMCCodeEmitter::getSORegOpValue(const MCInst &MI, |
| 146 | unsigned OpIdx) const { |
| 147 | // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg |
| 148 | // to be shifted. The second is either Rs, the amount to shift by, or |
| 149 | // reg0 in which case the imm contains the amount to shift by. |
| 150 | // {3-0} = Rm. |
| 151 | // {4} = 1 if reg shift, 0 if imm shift |
| 152 | // {6-5} = type |
| 153 | // If reg shift: |
| 154 | // {7} = 0 |
| 155 | // {11-8} = Rs |
| 156 | // else (imm shift) |
| 157 | // {11-7} = imm |
| 158 | |
| 159 | const MCOperand &MO = MI.getOperand(OpIdx); |
| 160 | const MCOperand &MO1 = MI.getOperand(OpIdx + 1); |
| 161 | const MCOperand &MO2 = MI.getOperand(OpIdx + 2); |
| 162 | ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm()); |
| 163 | |
| 164 | // Encode Rm. |
| 165 | unsigned Binary = getARMRegisterNumbering(MO.getReg()); |
| 166 | |
| 167 | // Encode the shift opcode. |
| 168 | unsigned SBits = 0; |
| 169 | unsigned Rs = MO1.getReg(); |
| 170 | if (Rs) { |
| 171 | // Set shift operand (bit[7:4]). |
| 172 | // LSL - 0001 |
| 173 | // LSR - 0011 |
| 174 | // ASR - 0101 |
| 175 | // ROR - 0111 |
| 176 | // RRX - 0110 and bit[11:8] clear. |
| 177 | switch (SOpc) { |
| 178 | default: llvm_unreachable("Unknown shift opc!"); |
| 179 | case ARM_AM::lsl: SBits = 0x1; break; |
| 180 | case ARM_AM::lsr: SBits = 0x3; break; |
| 181 | case ARM_AM::asr: SBits = 0x5; break; |
| 182 | case ARM_AM::ror: SBits = 0x7; break; |
| 183 | case ARM_AM::rrx: SBits = 0x6; break; |
| 184 | } |
| 185 | } else { |
| 186 | // Set shift operand (bit[6:4]). |
| 187 | // LSL - 000 |
| 188 | // LSR - 010 |
| 189 | // ASR - 100 |
| 190 | // ROR - 110 |
| 191 | switch (SOpc) { |
| 192 | default: llvm_unreachable("Unknown shift opc!"); |
| 193 | case ARM_AM::lsl: SBits = 0x0; break; |
| 194 | case ARM_AM::lsr: SBits = 0x2; break; |
| 195 | case ARM_AM::asr: SBits = 0x4; break; |
| 196 | case ARM_AM::ror: SBits = 0x6; break; |
| 197 | } |
| 198 | } |
| 199 | Binary |= SBits << 4; |
| 200 | if (SOpc == ARM_AM::rrx) |
| 201 | return Binary; |
| 202 | |
| 203 | // Encode the shift operation Rs or shift_imm (except rrx). |
| 204 | if (Rs) { |
| 205 | // Encode Rs bit[11:8]. |
| 206 | assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0); |
| 207 | return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift); |
| 208 | } |
| 209 | |
| 210 | // Encode shift_imm bit[11:7]. |
| 211 | return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7; |
| 212 | } |
| 213 | |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 214 | void ARMMCCodeEmitter:: |
| 215 | EncodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 216 | SmallVectorImpl<MCFixup> &Fixups) const { |
Jim Grosbach | d6d4b42 | 2010-10-07 22:12:50 +0000 | [diff] [blame] | 217 | unsigned Opcode = MI.getOpcode(); |
| 218 | const TargetInstrDesc &Desc = TII.get(Opcode); |
| 219 | uint64_t TSFlags = Desc.TSFlags; |
Jim Grosbach | 58f38bf | 2010-10-08 00:39:21 +0000 | [diff] [blame] | 220 | // Keep track of the current byte being emitted. |
| 221 | unsigned CurByte = 0; |
Jim Grosbach | d6d4b42 | 2010-10-07 22:12:50 +0000 | [diff] [blame] | 222 | |
| 223 | // Pseudo instructions don't get encoded. |
| 224 | if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo) |
| 225 | return; |
| 226 | |
| 227 | ++MCNumEmitted; // Keep track of the # of mi's emitted |
Jim Grosbach | 0de6ab3 | 2010-10-12 17:11:26 +0000 | [diff] [blame] | 228 | unsigned Value = getBinaryCodeForInstr(MI); |
Jim Grosbach | 3e09413 | 2010-10-08 17:45:54 +0000 | [diff] [blame] | 229 | switch (Opcode) { |
Jim Grosbach | 0de6ab3 | 2010-10-12 17:11:26 +0000 | [diff] [blame] | 230 | default: break; |
Jim Grosbach | d6d4b42 | 2010-10-07 22:12:50 +0000 | [diff] [blame] | 231 | } |
Jim Grosbach | 0de6ab3 | 2010-10-12 17:11:26 +0000 | [diff] [blame] | 232 | EmitConstant(Value, 4, CurByte, OS); |
Jim Grosbach | 568eeed | 2010-09-17 18:46:17 +0000 | [diff] [blame] | 233 | } |
Jim Grosbach | 9af82ba | 2010-10-07 21:57:55 +0000 | [diff] [blame] | 234 | |
| 235 | // FIXME: These #defines shouldn't be necessary. Instead, tblgen should |
| 236 | // be able to generate code emitter helpers for either variant, like it |
| 237 | // does for the AsmWriter. |
| 238 | #define ARMCodeEmitter ARMMCCodeEmitter |
| 239 | #define MachineInstr MCInst |
| 240 | #include "ARMGenCodeEmitter.inc" |
| 241 | #undef ARMCodeEmitter |
| 242 | #undef MachineInstr |