1 //===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ARMMCCodeEmitter class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "arm-emitter"
16 #include "ARMAddressingModes.h"
17 #include "ARMInstrInfo.h"
18 #include "llvm/MC/MCCodeEmitter.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Support/raw_ostream.h"
25 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
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;
36 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
37 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
40 ~ARMMCCodeEmitter() {}
42 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
44 // getBinaryCodeForInstr - TableGen'erated function for getting the
45 // binary encoding for an instruction.
46 unsigned getBinaryCodeForInstr(const MCInst &MI) const;
48 /// getMachineOpValue - Return binary encoding of operand. If the machine
49 /// operand requires relocation, record the relocation and return zero.
50 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO) const;
52 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
54 unsigned getAddrModeImm12OpValue(const MCInst &MI, unsigned Op) const;
56 /// getCCOutOpValue - Return encoding of the 's' bit.
57 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op) const {
58 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
60 return MI.getOperand(Op).getReg() == ARM::CPSR;
63 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
64 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op) const {
65 unsigned SoImm = MI.getOperand(Op).getImm();
66 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
67 assert(SoImmVal != -1 && "Not a valid so_imm value!");
70 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
71 << ARMII::SoRotImmShift;
74 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
78 /// getSORegOpValue - Return an encoded so_reg shifted register value.
79 unsigned getSORegOpValue(const MCInst &MI, unsigned Op) const;
81 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op) const {
82 switch (MI.getOperand(Op).getImm()) {
83 default: assert (0 && "Not a valid rot_imm value!");
91 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op) const {
92 return MI.getOperand(Op).getImm() - 1;
95 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op) const {
96 return 64 - MI.getOperand(Op).getImm();
99 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op) const;
101 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op) const;
104 unsigned getNumFixupKinds() const {
105 assert(0 && "ARMMCCodeEmitter::getNumFixupKinds() not yet implemented.");
109 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
110 static MCFixupKindInfo rtn;
111 assert(0 && "ARMMCCodeEmitter::getFixupKindInfo() not yet implemented.");
115 void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
120 void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
121 raw_ostream &OS) const {
122 // Output the constant in little endian byte order.
123 for (unsigned i = 0; i != Size; ++i) {
124 EmitByte(Val & 255, CurByte, OS);
129 void EmitImmediate(const MCOperand &Disp,
130 unsigned ImmSize, MCFixupKind FixupKind,
131 unsigned &CurByte, raw_ostream &OS,
132 SmallVectorImpl<MCFixup> &Fixups,
133 int ImmOffset = 0) const;
135 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
136 SmallVectorImpl<MCFixup> &Fixups) const;
139 } // end anonymous namespace
141 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &,
144 return new ARMMCCodeEmitter(TM, Ctx);
147 void ARMMCCodeEmitter::
148 EmitImmediate(const MCOperand &DispOp, unsigned Size, MCFixupKind FixupKind,
149 unsigned &CurByte, raw_ostream &OS,
150 SmallVectorImpl<MCFixup> &Fixups, int ImmOffset) const {
151 assert(0 && "ARMMCCodeEmitter::EmitImmediate() not yet implemented.");
154 /// getMachineOpValue - Return binary encoding of operand. If the machine
155 /// operand requires relocation, record the relocation and return zero.
156 unsigned ARMMCCodeEmitter::getMachineOpValue(const MCInst &MI,
157 const MCOperand &MO) const {
159 unsigned regno = getARMRegisterNumbering(MO.getReg());
161 // Q registers are encodes as 2x their register number.
162 switch (MO.getReg()) {
163 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
164 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
165 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
166 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
171 } else if (MO.isImm()) {
172 return static_cast<unsigned>(MO.getImm());
173 } else if (MO.isFPImm()) {
174 return static_cast<unsigned>(APFloat(MO.getFPImm())
175 .bitcastToAPInt().getHiBits(32).getLimitedValue());
185 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
187 unsigned ARMMCCodeEmitter::getAddrModeImm12OpValue(const MCInst &MI,
188 unsigned OpIdx) const {
190 // {12} = (U)nsigned (add == '1', sub == '0')
192 const MCOperand &MO = MI.getOperand(OpIdx);
193 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
194 unsigned Reg = getARMRegisterNumbering(MO.getReg());
195 int32_t Imm12 = MO1.getImm();
196 bool isAdd = Imm12 >= 0;
197 // Special value for #-0
198 if (Imm12 == INT32_MIN)
200 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
203 uint32_t Binary = Imm12 & 0xfff;
206 Binary |= (Reg << 13);
210 unsigned ARMMCCodeEmitter::getSORegOpValue(const MCInst &MI,
211 unsigned OpIdx) const {
212 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg
213 // to be shifted. The second is either Rs, the amount to shift by, or
214 // reg0 in which case the imm contains the amount to shift by.
216 // {4} = 1 if reg shift, 0 if imm shift
224 const MCOperand &MO = MI.getOperand(OpIdx);
225 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
226 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
227 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
230 unsigned Binary = getARMRegisterNumbering(MO.getReg());
232 // Encode the shift opcode.
234 unsigned Rs = MO1.getReg();
236 // Set shift operand (bit[7:4]).
241 // RRX - 0110 and bit[11:8] clear.
243 default: llvm_unreachable("Unknown shift opc!");
244 case ARM_AM::lsl: SBits = 0x1; break;
245 case ARM_AM::lsr: SBits = 0x3; break;
246 case ARM_AM::asr: SBits = 0x5; break;
247 case ARM_AM::ror: SBits = 0x7; break;
248 case ARM_AM::rrx: SBits = 0x6; break;
251 // Set shift operand (bit[6:4]).
257 default: llvm_unreachable("Unknown shift opc!");
258 case ARM_AM::lsl: SBits = 0x0; break;
259 case ARM_AM::lsr: SBits = 0x2; break;
260 case ARM_AM::asr: SBits = 0x4; break;
261 case ARM_AM::ror: SBits = 0x6; break;
264 Binary |= SBits << 4;
265 if (SOpc == ARM_AM::rrx)
268 // Encode the shift operation Rs or shift_imm (except rrx).
270 // Encode Rs bit[11:8].
271 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
272 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
275 // Encode shift_imm bit[11:7].
276 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
279 unsigned ARMMCCodeEmitter::getBitfieldInvertedMaskOpValue(const MCInst &MI,
281 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
283 const MCOperand &MO = MI.getOperand(Op);
284 uint32_t v = ~MO.getImm();
285 uint32_t lsb = CountTrailingZeros_32(v);
286 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
287 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
288 return lsb | (msb << 5);
291 unsigned ARMMCCodeEmitter::getRegisterListOpValue(const MCInst &MI,
293 // Convert a list of GPRs into a bitfield (R0 -> bit 0). For each
294 // register in the list, set the corresponding bit.
296 for (unsigned i = Op; i < MI.getNumOperands(); ++i) {
297 unsigned regno = getARMRegisterNumbering(MI.getOperand(i).getReg());
298 Binary |= 1 << regno;
303 void ARMMCCodeEmitter::
304 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
305 SmallVectorImpl<MCFixup> &Fixups) const {
306 unsigned Opcode = MI.getOpcode();
307 const TargetInstrDesc &Desc = TII.get(Opcode);
308 uint64_t TSFlags = Desc.TSFlags;
309 // Keep track of the current byte being emitted.
310 unsigned CurByte = 0;
312 // Pseudo instructions don't get encoded.
313 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
316 ++MCNumEmitted; // Keep track of the # of mi's emitted
317 unsigned Value = getBinaryCodeForInstr(MI);
321 EmitConstant(Value, 4, CurByte, OS);
324 // FIXME: These #defines shouldn't be necessary. Instead, tblgen should
325 // be able to generate code emitter helpers for either variant, like it
326 // does for the AsmWriter.
327 #define ARMCodeEmitter ARMMCCodeEmitter
328 #define MachineInstr MCInst
329 #include "ARMGenCodeEmitter.inc"
330 #undef ARMCodeEmitter