blob: 6844cc71b73fa177e594575211dce0440594177a [file] [log] [blame]
Jim Grosbach568eeed2010-09-17 18:46:17 +00001//===-- 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 Grosbach42fac8e2010-10-11 23:16:21 +000016#include "ARMAddressingModes.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000017#include "ARMInstrInfo.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000018#include "llvm/MC/MCCodeEmitter.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCInst.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000021#include "llvm/ADT/Statistic.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000022#include "llvm/Support/raw_ostream.h"
23using namespace llvm;
24
Jim Grosbachd6d4b422010-10-07 22:12:50 +000025STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
26
Jim Grosbach568eeed2010-09-17 18:46:17 +000027namespace {
28class 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
35public:
36 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
37 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000038 }
39
40 ~ARMMCCodeEmitter() {}
41
Jim Grosbach0de6ab32010-10-12 17:11:26 +000042 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
43
Jim Grosbach9af82ba2010-10-07 21:57:55 +000044 // getBinaryCodeForInstr - TableGen'erated function for getting the
45 // binary encoding for an instruction.
Jim Grosbachbade37b2010-10-08 00:21:28 +000046 unsigned getBinaryCodeForInstr(const MCInst &MI) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000047
48 /// getMachineOpValue - Return binary encoding of operand. If the machine
49 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach3e094132010-10-08 17:45:54 +000050 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000051
Bill Wendling5df0e0a2010-11-02 22:31:46 +000052 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
53 uint32_t getAddrModeImmOpValue(const MCInst &MI, unsigned Op) const;
Jim Grosbach3e556122010-10-26 22:37:02 +000054
Jim Grosbach08bd5492010-10-12 23:00:24 +000055 /// getCCOutOpValue - Return encoding of the 's' bit.
56 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op) const {
57 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
58 // '1' respectively.
59 return MI.getOperand(Op).getReg() == ARM::CPSR;
60 }
Jim Grosbachef324d72010-10-12 23:53:58 +000061
Jim Grosbach2a6a93d2010-10-12 23:18:08 +000062 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
63 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op) const {
64 unsigned SoImm = MI.getOperand(Op).getImm();
65 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
66 assert(SoImmVal != -1 && "Not a valid so_imm value!");
67
68 // Encode rotate_imm.
69 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
70 << ARMII::SoRotImmShift;
71
72 // Encode immed_8.
73 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
74 return Binary;
75 }
Jim Grosbach08bd5492010-10-12 23:00:24 +000076
Jim Grosbachef324d72010-10-12 23:53:58 +000077 /// getSORegOpValue - Return an encoded so_reg shifted register value.
78 unsigned getSORegOpValue(const MCInst &MI, unsigned Op) const;
79
Jim Grosbachb35ad412010-10-13 19:56:10 +000080 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op) const {
81 switch (MI.getOperand(Op).getImm()) {
82 default: assert (0 && "Not a valid rot_imm value!");
83 case 0: return 0;
84 case 8: return 1;
85 case 16: return 2;
86 case 24: return 3;
87 }
88 }
89
Jim Grosbach8abe32a2010-10-15 17:15:16 +000090 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op) const {
91 return MI.getOperand(Op).getImm() - 1;
92 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +000093
Jim Grosbach0d2d2e92010-10-29 23:19:55 +000094 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op) const {
Owen Anderson498ec202010-10-27 22:49:00 +000095 return 64 - MI.getOperand(Op).getImm();
96 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +000097
Jim Grosbach3fea191052010-10-21 22:03:21 +000098 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op) const;
99
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000100 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op) const;
Owen Andersona2b50b32010-11-02 22:28:01 +0000101 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op) const;
102 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000103
Jim Grosbach568eeed2010-09-17 18:46:17 +0000104 unsigned getNumFixupKinds() const {
105 assert(0 && "ARMMCCodeEmitter::getNumFixupKinds() not yet implemented.");
Michael J. Spencer895dda62010-09-18 17:54:37 +0000106 return 0;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000107 }
108
109 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
110 static MCFixupKindInfo rtn;
111 assert(0 && "ARMMCCodeEmitter::getFixupKindInfo() not yet implemented.");
112 return rtn;
113 }
114
Jim Grosbach568eeed2010-09-17 18:46:17 +0000115 void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
116 OS << (char)C;
117 ++CurByte;
118 }
119
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);
125 Val >>= 8;
126 }
127 }
128
Jim Grosbach568eeed2010-09-17 18:46:17 +0000129 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
130 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000131};
132
133} // end anonymous namespace
134
Bill Wendling0800ce72010-11-02 22:53:11 +0000135MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
136 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000137 return new ARMMCCodeEmitter(TM, Ctx);
138}
139
Jim Grosbach56ac9072010-10-08 21:45:55 +0000140/// getMachineOpValue - Return binary encoding of operand. If the machine
141/// operand requires relocation, record the relocation and return zero.
142unsigned ARMMCCodeEmitter::getMachineOpValue(const MCInst &MI,
143 const MCOperand &MO) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000144 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000145 unsigned Reg = MO.getReg();
146 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000147
Owen Anderson90d4cf92010-10-21 20:49:13 +0000148 // Q registers are encodes as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000149 switch (Reg) {
150 default:
151 return RegNo;
152 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
153 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
154 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
155 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
156 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000157 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000158 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000159 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000160 } else if (MO.isFPImm()) {
161 return static_cast<unsigned>(APFloat(MO.getFPImm())
162 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000163 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000164
165#ifndef NDEBUG
166 errs() << MO;
167#endif
168 llvm_unreachable(0);
Jim Grosbach56ac9072010-10-08 21:45:55 +0000169 return 0;
170}
171
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000172/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
173uint32_t ARMMCCodeEmitter::getAddrModeImmOpValue(const MCInst &MI,
174 unsigned OpIdx) const {
175 // {20-17} = reg
176 // {16} = (U)nsigned (add == '1', sub == '0')
177 // {15-0} = imm
Jim Grosbach3e556122010-10-26 22:37:02 +0000178 const MCOperand &MO = MI.getOperand(OpIdx);
179 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000180 uint32_t Binary = 0;
181
182 // If The first operand isn't a register, we have a label reference.
183 if (!MO.isReg()) {
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000184 Binary |= ARM::PC << 17; // Rn is PC.
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000185 // FIXME: Add a fixup referencing the label.
186 return Binary;
187 }
188
Jim Grosbach3e556122010-10-26 22:37:02 +0000189 unsigned Reg = getARMRegisterNumbering(MO.getReg());
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000190 int32_t Imm = MO1.getImm();
191 bool isAdd = Imm >= 0;
192
Jim Grosbachab682a22010-10-28 18:34:10 +0000193 // Special value for #-0
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000194 if (Imm == INT32_MIN)
195 Imm = 0;
196
Jim Grosbachab682a22010-10-28 18:34:10 +0000197 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000198 if (Imm < 0) Imm = -Imm;
199
200 Binary = Imm & 0xffff;
Jim Grosbachab682a22010-10-28 18:34:10 +0000201 if (isAdd)
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000202 Binary |= (1 << 16);
203 Binary |= (Reg << 17);
Jim Grosbach3e556122010-10-26 22:37:02 +0000204 return Binary;
205}
206
Jim Grosbachef324d72010-10-12 23:53:58 +0000207unsigned ARMMCCodeEmitter::getSORegOpValue(const MCInst &MI,
208 unsigned OpIdx) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000209 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
210 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
211 // case the imm contains the amount to shift by.
212 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000213 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000214 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000215 // {6-5} = type
216 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000217 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000218 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000219 // else (imm shift)
220 // {11-7} = imm
221
222 const MCOperand &MO = MI.getOperand(OpIdx);
223 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
224 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
225 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
226
227 // Encode Rm.
228 unsigned Binary = getARMRegisterNumbering(MO.getReg());
229
230 // Encode the shift opcode.
231 unsigned SBits = 0;
232 unsigned Rs = MO1.getReg();
233 if (Rs) {
234 // Set shift operand (bit[7:4]).
235 // LSL - 0001
236 // LSR - 0011
237 // ASR - 0101
238 // ROR - 0111
239 // RRX - 0110 and bit[11:8] clear.
240 switch (SOpc) {
241 default: llvm_unreachable("Unknown shift opc!");
242 case ARM_AM::lsl: SBits = 0x1; break;
243 case ARM_AM::lsr: SBits = 0x3; break;
244 case ARM_AM::asr: SBits = 0x5; break;
245 case ARM_AM::ror: SBits = 0x7; break;
246 case ARM_AM::rrx: SBits = 0x6; break;
247 }
248 } else {
249 // Set shift operand (bit[6:4]).
250 // LSL - 000
251 // LSR - 010
252 // ASR - 100
253 // ROR - 110
254 switch (SOpc) {
255 default: llvm_unreachable("Unknown shift opc!");
256 case ARM_AM::lsl: SBits = 0x0; break;
257 case ARM_AM::lsr: SBits = 0x2; break;
258 case ARM_AM::asr: SBits = 0x4; break;
259 case ARM_AM::ror: SBits = 0x6; break;
260 }
261 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000262
Jim Grosbachef324d72010-10-12 23:53:58 +0000263 Binary |= SBits << 4;
264 if (SOpc == ARM_AM::rrx)
265 return Binary;
266
267 // Encode the shift operation Rs or shift_imm (except rrx).
268 if (Rs) {
269 // Encode Rs bit[11:8].
270 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
271 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
272 }
273
274 // Encode shift_imm bit[11:7].
275 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
276}
277
Jim Grosbach3fea191052010-10-21 22:03:21 +0000278unsigned ARMMCCodeEmitter::getBitfieldInvertedMaskOpValue(const MCInst &MI,
279 unsigned Op) const {
280 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
281 // msb of the mask.
282 const MCOperand &MO = MI.getOperand(Op);
283 uint32_t v = ~MO.getImm();
284 uint32_t lsb = CountTrailingZeros_32(v);
285 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
286 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
287 return lsb | (msb << 5);
288}
289
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000290unsigned ARMMCCodeEmitter::getRegisterListOpValue(const MCInst &MI,
291 unsigned Op) const {
292 // Convert a list of GPRs into a bitfield (R0 -> bit 0). For each
293 // register in the list, set the corresponding bit.
294 unsigned Binary = 0;
Jim Grosbach4b5236c2010-10-30 01:40:16 +0000295 for (unsigned i = Op, e = MI.getNumOperands(); i < e; ++i) {
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000296 unsigned regno = getARMRegisterNumbering(MI.getOperand(i).getReg());
297 Binary |= 1 << regno;
298 }
299 return Binary;
300}
301
Owen Andersona2b50b32010-11-02 22:28:01 +0000302unsigned ARMMCCodeEmitter::getAddrMode6AddressOpValue(const MCInst &MI,
Owen Andersond9aa7d32010-11-02 00:05:05 +0000303 unsigned Op) const {
304 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000305 const MCOperand &Imm = MI.getOperand(Op + 1);
Owen Andersond9aa7d32010-11-02 00:05:05 +0000306
307 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000308 unsigned Align = 0;
309
310 switch (Imm.getImm()) {
311 default: break;
312 case 2:
313 case 4:
314 case 8: Align = 0x01; break;
315 case 16: Align = 0x02; break;
316 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000317 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000318
Owen Andersond9aa7d32010-11-02 00:05:05 +0000319 return RegNo | (Align << 4);
320}
321
Owen Andersona2b50b32010-11-02 22:28:01 +0000322unsigned ARMMCCodeEmitter::getAddrMode6OffsetOpValue(const MCInst &MI,
Owen Andersoncf667be2010-11-02 01:24:55 +0000323 unsigned Op) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000324 const MCOperand &MO = MI.getOperand(Op);
325 if (MO.getReg() == 0) return 0x0D;
326 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +0000327}
328
Jim Grosbach568eeed2010-09-17 18:46:17 +0000329void ARMMCCodeEmitter::
330EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Bill Wendlingd3a124d2010-11-02 22:46:04 +0000331 SmallVectorImpl<MCFixup> &) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000332 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +0000333 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
334 if ((Desc.TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000335 return;
336
Bill Wendling7292e0a2010-11-02 22:44:12 +0000337 // Keep track of the current byte being emitted.
338 unsigned CurByte = 0;
339 EmitConstant(getBinaryCodeForInstr(MI), 4, CurByte, OS);
340 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +0000341}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000342
343// FIXME: These #defines shouldn't be necessary. Instead, tblgen should
344// be able to generate code emitter helpers for either variant, like it
345// does for the AsmWriter.
346#define ARMCodeEmitter ARMMCCodeEmitter
347#define MachineInstr MCInst
348#include "ARMGenCodeEmitter.inc"
349#undef ARMCodeEmitter
350#undef MachineInstr