blob: 13a80bdffde2cea611a8300f0f2eee1ce6c3c156 [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
Jim Grosbach3e556122010-10-26 22:37:02 +000052 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
53 /// operand.
54 unsigned getAddrModeImm12OpValue(const MCInst &MI, unsigned Op) const;
55
Jim Grosbach08bd5492010-10-12 23:00:24 +000056 /// 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
59 // '1' respectively.
60 return MI.getOperand(Op).getReg() == ARM::CPSR;
61 }
Jim Grosbachef324d72010-10-12 23:53:58 +000062
Jim Grosbach2a6a93d2010-10-12 23:18:08 +000063 /// 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!");
68
69 // Encode rotate_imm.
70 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
71 << ARMII::SoRotImmShift;
72
73 // Encode immed_8.
74 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
75 return Binary;
76 }
Jim Grosbach08bd5492010-10-12 23:00:24 +000077
Jim Grosbachef324d72010-10-12 23:53:58 +000078 /// getSORegOpValue - Return an encoded so_reg shifted register value.
79 unsigned getSORegOpValue(const MCInst &MI, unsigned Op) const;
80
Jim Grosbachb35ad412010-10-13 19:56:10 +000081 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op) const {
82 switch (MI.getOperand(Op).getImm()) {
83 default: assert (0 && "Not a valid rot_imm value!");
84 case 0: return 0;
85 case 8: return 1;
86 case 16: return 2;
87 case 24: return 3;
88 }
89 }
90
Jim Grosbach8abe32a2010-10-15 17:15:16 +000091 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op) const {
92 return MI.getOperand(Op).getImm() - 1;
93 }
94
Jim Grosbach3fea191052010-10-21 22:03:21 +000095 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op) const;
96
Jim Grosbach568eeed2010-09-17 18:46:17 +000097 unsigned getNumFixupKinds() const {
98 assert(0 && "ARMMCCodeEmitter::getNumFixupKinds() not yet implemented.");
Michael J. Spencer895dda62010-09-18 17:54:37 +000099 return 0;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000100 }
101
102 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
103 static MCFixupKindInfo rtn;
104 assert(0 && "ARMMCCodeEmitter::getFixupKindInfo() not yet implemented.");
105 return rtn;
106 }
107
Jim Grosbach568eeed2010-09-17 18:46:17 +0000108 void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
109 OS << (char)C;
110 ++CurByte;
111 }
112
113 void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
114 raw_ostream &OS) const {
115 // Output the constant in little endian byte order.
116 for (unsigned i = 0; i != Size; ++i) {
117 EmitByte(Val & 255, CurByte, OS);
118 Val >>= 8;
119 }
120 }
121
122 void EmitImmediate(const MCOperand &Disp,
123 unsigned ImmSize, MCFixupKind FixupKind,
124 unsigned &CurByte, raw_ostream &OS,
125 SmallVectorImpl<MCFixup> &Fixups,
126 int ImmOffset = 0) const;
127
128 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
129 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000130};
131
132} // end anonymous namespace
133
Jim Grosbach568eeed2010-09-17 18:46:17 +0000134MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &,
135 TargetMachine &TM,
136 MCContext &Ctx) {
137 return new ARMMCCodeEmitter(TM, Ctx);
138}
139
140void ARMMCCodeEmitter::
141EmitImmediate(const MCOperand &DispOp, unsigned Size, MCFixupKind FixupKind,
142 unsigned &CurByte, raw_ostream &OS,
143 SmallVectorImpl<MCFixup> &Fixups, int ImmOffset) const {
144 assert(0 && "ARMMCCodeEmitter::EmitImmediate() not yet implemented.");
145}
146
Jim Grosbach56ac9072010-10-08 21:45:55 +0000147/// getMachineOpValue - Return binary encoding of operand. If the machine
148/// operand requires relocation, record the relocation and return zero.
149unsigned ARMMCCodeEmitter::getMachineOpValue(const MCInst &MI,
150 const MCOperand &MO) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000151 if (MO.isReg()) {
Owen Anderson90d4cf92010-10-21 20:49:13 +0000152 unsigned regno = getARMRegisterNumbering(MO.getReg());
153
154 // Q registers are encodes as 2x their register number.
155 switch (MO.getReg()) {
156 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
157 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
158 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
159 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
160 return 2 * regno;
161 default:
162 return regno;
163 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000164 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000165 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000166 } else if (MO.isFPImm()) {
167 return static_cast<unsigned>(APFloat(MO.getFPImm())
168 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000169 } else {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000170#ifndef NDEBUG
171 errs() << MO;
172#endif
173 llvm_unreachable(0);
174 }
175 return 0;
176}
177
Jim Grosbach3e556122010-10-26 22:37:02 +0000178/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
179/// operand.
180unsigned ARMMCCodeEmitter::getAddrModeImm12OpValue(const MCInst &MI,
181 unsigned OpIdx) const {
182 // {17-13} = reg
183 // {12} = (U)nsigned (add == '1', sub == '0')
184 // {11-0} = imm12
185 const MCOperand &MO = MI.getOperand(OpIdx);
186 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
187 unsigned Reg = getARMRegisterNumbering(MO.getReg());
188 int32_t Imm12 = MO1.getImm();
189 uint32_t Binary;
190 Binary = Imm12 & 0xfff;
191 if (Imm12 >= 0)
192 Binary |= (1 << 12);
193 Binary |= (Reg << 13);
194 return Binary;
195}
196
Jim Grosbachef324d72010-10-12 23:53:58 +0000197unsigned ARMMCCodeEmitter::getSORegOpValue(const MCInst &MI,
198 unsigned OpIdx) const {
199 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg
200 // to be shifted. The second is either Rs, the amount to shift by, or
201 // reg0 in which case the imm contains the amount to shift by.
202 // {3-0} = Rm.
203 // {4} = 1 if reg shift, 0 if imm shift
204 // {6-5} = type
205 // If reg shift:
206 // {7} = 0
207 // {11-8} = Rs
208 // else (imm shift)
209 // {11-7} = imm
210
211 const MCOperand &MO = MI.getOperand(OpIdx);
212 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
213 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
214 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
215
216 // Encode Rm.
217 unsigned Binary = getARMRegisterNumbering(MO.getReg());
218
219 // Encode the shift opcode.
220 unsigned SBits = 0;
221 unsigned Rs = MO1.getReg();
222 if (Rs) {
223 // Set shift operand (bit[7:4]).
224 // LSL - 0001
225 // LSR - 0011
226 // ASR - 0101
227 // ROR - 0111
228 // RRX - 0110 and bit[11:8] clear.
229 switch (SOpc) {
230 default: llvm_unreachable("Unknown shift opc!");
231 case ARM_AM::lsl: SBits = 0x1; break;
232 case ARM_AM::lsr: SBits = 0x3; break;
233 case ARM_AM::asr: SBits = 0x5; break;
234 case ARM_AM::ror: SBits = 0x7; break;
235 case ARM_AM::rrx: SBits = 0x6; break;
236 }
237 } else {
238 // Set shift operand (bit[6:4]).
239 // LSL - 000
240 // LSR - 010
241 // ASR - 100
242 // ROR - 110
243 switch (SOpc) {
244 default: llvm_unreachable("Unknown shift opc!");
245 case ARM_AM::lsl: SBits = 0x0; break;
246 case ARM_AM::lsr: SBits = 0x2; break;
247 case ARM_AM::asr: SBits = 0x4; break;
248 case ARM_AM::ror: SBits = 0x6; break;
249 }
250 }
251 Binary |= SBits << 4;
252 if (SOpc == ARM_AM::rrx)
253 return Binary;
254
255 // Encode the shift operation Rs or shift_imm (except rrx).
256 if (Rs) {
257 // Encode Rs bit[11:8].
258 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
259 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
260 }
261
262 // Encode shift_imm bit[11:7].
263 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
264}
265
Jim Grosbach3fea191052010-10-21 22:03:21 +0000266unsigned ARMMCCodeEmitter::getBitfieldInvertedMaskOpValue(const MCInst &MI,
267 unsigned Op) const {
268 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
269 // msb of the mask.
270 const MCOperand &MO = MI.getOperand(Op);
271 uint32_t v = ~MO.getImm();
272 uint32_t lsb = CountTrailingZeros_32(v);
273 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
274 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
275 return lsb | (msb << 5);
276}
277
Jim Grosbach568eeed2010-09-17 18:46:17 +0000278void ARMMCCodeEmitter::
279EncodeInstruction(const MCInst &MI, raw_ostream &OS,
280 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000281 unsigned Opcode = MI.getOpcode();
282 const TargetInstrDesc &Desc = TII.get(Opcode);
283 uint64_t TSFlags = Desc.TSFlags;
Jim Grosbach58f38bf2010-10-08 00:39:21 +0000284 // Keep track of the current byte being emitted.
285 unsigned CurByte = 0;
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000286
287 // Pseudo instructions don't get encoded.
288 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
289 return;
290
291 ++MCNumEmitted; // Keep track of the # of mi's emitted
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000292 unsigned Value = getBinaryCodeForInstr(MI);
Jim Grosbach3e094132010-10-08 17:45:54 +0000293 switch (Opcode) {
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000294 default: break;
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000295 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000296 EmitConstant(Value, 4, CurByte, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000297}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000298
299// FIXME: These #defines shouldn't be necessary. Instead, tblgen should
300// be able to generate code emitter helpers for either variant, like it
301// does for the AsmWriter.
302#define ARMCodeEmitter ARMMCCodeEmitter
303#define MachineInstr MCInst
304#include "ARMGenCodeEmitter.inc"
305#undef ARMCodeEmitter
306#undef MachineInstr