blob: 89c0d2d058bb4666a1f19763aeb74dd0c73ea01c [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 Grosbach70933262010-11-04 01:12:30 +000017#include "ARMFixupKinds.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000018#include "ARMInstrInfo.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000019#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000022#include "llvm/ADT/Statistic.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000023#include "llvm/Support/raw_ostream.h"
24using namespace llvm;
25
Jim Grosbach70933262010-11-04 01:12:30 +000026STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
27STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
Jim Grosbachd6d4b422010-10-07 22:12:50 +000028
Jim Grosbach568eeed2010-09-17 18:46:17 +000029namespace {
30class 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
37public:
38 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
39 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000040 }
41
42 ~ARMMCCodeEmitter() {}
43
Jim Grosbach70933262010-11-04 01:12:30 +000044 unsigned getNumFixupKinds() const { return 2; }
45
46 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
47 const static MCFixupKindInfo Infos[] = {
48 { "fixup_arm_pcrel_12", 2, 12, MCFixupKindInfo::FKF_IsPCRel },
49 { "fixup_arm_vfp_pcrel_12", 3, 8, MCFixupKindInfo::FKF_IsPCRel },
50 };
51
52 if (Kind < FirstTargetFixupKind)
53 return MCCodeEmitter::getFixupKindInfo(Kind);
54
55 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
56 "Invalid kind!");
57 return Infos[Kind - FirstTargetFixupKind];
58 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000059 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
60
Jim Grosbach9af82ba2010-10-07 21:57:55 +000061 // getBinaryCodeForInstr - TableGen'erated function for getting the
62 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000063 unsigned getBinaryCodeForInstr(const MCInst &MI,
64 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000065
66 /// getMachineOpValue - Return binary encoding of operand. If the machine
67 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000068 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
69 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000070
Bill Wendling92b5a2e2010-11-03 01:49:29 +000071 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000072 unsigned &Reg, unsigned &Imm,
73 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000074
75 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
76 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +000077 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
78 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000079
Jim Grosbach54fea632010-11-09 17:20:53 +000080 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
81 /// operand as needed by load/store instructions.
82 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
83 SmallVectorImpl<MCFixup> &Fixups) const;
84
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +000085 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
86 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
87 SmallVectorImpl<MCFixup> &Fixups) const {
88 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
89 switch (Mode) {
90 default: assert(0 && "Unknown addressing sub-mode!");
91 case ARM_AM::da: return 0;
92 case ARM_AM::ia: return 1;
93 case ARM_AM::db: return 2;
94 case ARM_AM::ib: return 3;
95 }
96 }
Jim Grosbach7eab97f2010-11-11 16:55:29 +000097 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
98 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
99 SmallVectorImpl<MCFixup> &Fixups) const;
100
Jim Grosbach570a9222010-11-11 01:09:40 +0000101 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
102 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
103 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000104
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000105 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000106 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
107 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000108
Jim Grosbach08bd5492010-10-12 23:00:24 +0000109 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000110 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
111 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000112 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
113 // '1' respectively.
114 return MI.getOperand(Op).getReg() == ARM::CPSR;
115 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000116
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000117 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000118 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
119 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000120 unsigned SoImm = MI.getOperand(Op).getImm();
121 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
122 assert(SoImmVal != -1 && "Not a valid so_imm value!");
123
124 // Encode rotate_imm.
125 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
126 << ARMII::SoRotImmShift;
127
128 // Encode immed_8.
129 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
130 return Binary;
131 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000132
Jim Grosbachef324d72010-10-12 23:53:58 +0000133 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000134 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
135 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000136
Jim Grosbach806e80e2010-11-03 23:52:49 +0000137 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
138 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000139 switch (MI.getOperand(Op).getImm()) {
140 default: assert (0 && "Not a valid rot_imm value!");
141 case 0: return 0;
142 case 8: return 1;
143 case 16: return 2;
144 case 24: return 3;
145 }
146 }
147
Jim Grosbach806e80e2010-11-03 23:52:49 +0000148 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
149 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000150 return MI.getOperand(Op).getImm() - 1;
151 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000152
Jim Grosbach806e80e2010-11-03 23:52:49 +0000153 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
154 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000155 return 64 - MI.getOperand(Op).getImm();
156 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000157
Jim Grosbach806e80e2010-11-03 23:52:49 +0000158 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
159 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000160
Jim Grosbach806e80e2010-11-03 23:52:49 +0000161 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
162 SmallVectorImpl<MCFixup> &Fixups) const;
163 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
164 SmallVectorImpl<MCFixup> &Fixups) const;
165 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
166 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000167
Jim Grosbach70933262010-11-04 01:12:30 +0000168 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000169 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000170 }
171
Jim Grosbach70933262010-11-04 01:12:30 +0000172 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000173 // Output the constant in little endian byte order.
174 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000175 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000176 Val >>= 8;
177 }
178 }
179
Jim Grosbach568eeed2010-09-17 18:46:17 +0000180 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
181 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000182};
183
184} // end anonymous namespace
185
Bill Wendling0800ce72010-11-02 22:53:11 +0000186MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
187 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000188 return new ARMMCCodeEmitter(TM, Ctx);
189}
190
Jim Grosbach56ac9072010-10-08 21:45:55 +0000191/// getMachineOpValue - Return binary encoding of operand. If the machine
192/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000193unsigned ARMMCCodeEmitter::
194getMachineOpValue(const MCInst &MI, const MCOperand &MO,
195 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000196 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000197 unsigned Reg = MO.getReg();
198 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000199
Owen Anderson90d4cf92010-10-21 20:49:13 +0000200 // Q registers are encodes as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000201 switch (Reg) {
202 default:
203 return RegNo;
204 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
205 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
206 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
207 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
208 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000209 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000210 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000211 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000212 } else if (MO.isFPImm()) {
213 return static_cast<unsigned>(APFloat(MO.getFPImm())
214 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000215 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000216
217#ifndef NDEBUG
218 errs() << MO;
219#endif
220 llvm_unreachable(0);
Jim Grosbach56ac9072010-10-08 21:45:55 +0000221 return 0;
222}
223
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000224/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000225bool ARMMCCodeEmitter::
226EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
227 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000228 const MCOperand &MO = MI.getOperand(OpIdx);
229 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000230
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000231 Reg = getARMRegisterNumbering(MO.getReg());
232
233 int32_t SImm = MO1.getImm();
234 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000235
Jim Grosbachab682a22010-10-28 18:34:10 +0000236 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000237 if (SImm == INT32_MIN)
238 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000239
Jim Grosbachab682a22010-10-28 18:34:10 +0000240 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000241 if (SImm < 0) {
242 SImm = -SImm;
243 isAdd = false;
244 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000245
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000246 Imm = SImm;
247 return isAdd;
248}
249
250/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000251uint32_t ARMMCCodeEmitter::
252getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
253 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000254 // {17-13} = reg
255 // {12} = (U)nsigned (add == '1', sub == '0')
256 // {11-0} = imm12
257 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000258 bool isAdd = true;
259 // If The first operand isn't a register, we have a label reference.
260 const MCOperand &MO = MI.getOperand(OpIdx);
261 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000262 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000263 Imm12 = 0;
264
265 assert(MO.isExpr() && "Unexpected machine operand type!");
266 const MCExpr *Expr = MO.getExpr();
267 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
268 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
269
270 ++MCNumCPRelocations;
271 } else
272 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000273
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000274 uint32_t Binary = Imm12 & 0xfff;
275 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000276 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000277 Binary |= (1 << 12);
278 Binary |= (Reg << 13);
279 return Binary;
280}
281
Jim Grosbach54fea632010-11-09 17:20:53 +0000282uint32_t ARMMCCodeEmitter::
283getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
284 SmallVectorImpl<MCFixup> &Fixups) const {
285 const MCOperand &MO = MI.getOperand(OpIdx);
286 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
287 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
288 unsigned Rn = getARMRegisterNumbering(MO.getReg());
289 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
290 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
291 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
292 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
293 unsigned SBits;
294 // LSL - 00
295 // LSR - 01
296 // ASR - 10
297 // ROR - 11
298 switch (ShOp) {
299 default: llvm_unreachable("Unknown shift opc!");
Jim Grosbachd92354c2010-11-09 17:38:15 +0000300 case ARM_AM::no_shift:
301 assert(ShImm == 0 && "Non-zero shift amount with no shift type!");
302 // fall through
Jim Grosbach54fea632010-11-09 17:20:53 +0000303 case ARM_AM::lsl: SBits = 0x0; break;
304 case ARM_AM::lsr: SBits = 0x1; break;
305 case ARM_AM::asr: SBits = 0x2; break;
306 case ARM_AM::ror: SBits = 0x3; break;
307 }
308
309 // {16-13} = Rn
310 // {12} = isAdd
311 // {11-0} = shifter
312 // {3-0} = Rm
313 // {4} = 0
314 // {6-5} = type
315 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000316 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000317 Binary |= Rn << 13;
318 Binary |= SBits << 5;
319 Binary |= ShImm << 7;
320 if (isAdd)
321 Binary |= 1 << 12;
322 return Binary;
323}
324
Jim Grosbach570a9222010-11-11 01:09:40 +0000325uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000326getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
327 SmallVectorImpl<MCFixup> &Fixups) const {
328 // {9} 1 == imm8, 0 == Rm
329 // {8} isAdd
330 // {7-4} imm7_4/zero
331 // {3-0} imm3_0/Rm
332 const MCOperand &MO = MI.getOperand(OpIdx);
333 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
334 unsigned Imm = MO1.getImm();
335 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
336 bool isImm = MO.getReg() == 0;
337 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
338 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
339 if (!isImm)
340 Imm8 = getARMRegisterNumbering(MO.getReg());
341 return Imm8 | (isAdd << 8) | (isImm << 9);
342}
343
344uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000345getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
346 SmallVectorImpl<MCFixup> &Fixups) const {
347 // {13} 1 == imm8, 0 == Rm
348 // {12-9} Rn
349 // {8} isAdd
350 // {7-4} imm7_4/zero
351 // {3-0} imm3_0/Rm
352 const MCOperand &MO = MI.getOperand(OpIdx);
353 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
354 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
355 unsigned Rn = getARMRegisterNumbering(MO.getReg());
356 unsigned Imm = MO2.getImm();
357 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
358 bool isImm = MO1.getReg() == 0;
359 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
360 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
361 if (!isImm)
362 Imm8 = getARMRegisterNumbering(MO1.getReg());
363 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
364}
365
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000366/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000367uint32_t ARMMCCodeEmitter::
368getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
369 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000370 // {12-9} = reg
371 // {8} = (U)nsigned (add == '1', sub == '0')
372 // {7-0} = imm8
373 unsigned Reg, Imm8;
Jim Grosbach70933262010-11-04 01:12:30 +0000374 // If The first operand isn't a register, we have a label reference.
375 const MCOperand &MO = MI.getOperand(OpIdx);
376 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000377 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000378 Imm8 = 0;
379
380 assert(MO.isExpr() && "Unexpected machine operand type!");
381 const MCExpr *Expr = MO.getExpr();
382 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_vfp_pcrel_12);
383 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
384
385 ++MCNumCPRelocations;
386 } else
387 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000388
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000389 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
390 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
391 if (ARM_AM::getAM5Op(Imm8) == ARM_AM::add)
392 Binary |= (1 << 8);
393 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000394 return Binary;
395}
396
Jim Grosbach806e80e2010-11-03 23:52:49 +0000397unsigned ARMMCCodeEmitter::
398getSORegOpValue(const MCInst &MI, unsigned OpIdx,
399 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000400 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
401 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
402 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000403 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000404 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000405 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000406 // {6-5} = type
407 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000408 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000409 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000410 // else (imm shift)
411 // {11-7} = imm
412
413 const MCOperand &MO = MI.getOperand(OpIdx);
414 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
415 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
416 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
417
418 // Encode Rm.
419 unsigned Binary = getARMRegisterNumbering(MO.getReg());
420
421 // Encode the shift opcode.
422 unsigned SBits = 0;
423 unsigned Rs = MO1.getReg();
424 if (Rs) {
425 // Set shift operand (bit[7:4]).
426 // LSL - 0001
427 // LSR - 0011
428 // ASR - 0101
429 // ROR - 0111
430 // RRX - 0110 and bit[11:8] clear.
431 switch (SOpc) {
432 default: llvm_unreachable("Unknown shift opc!");
433 case ARM_AM::lsl: SBits = 0x1; break;
434 case ARM_AM::lsr: SBits = 0x3; break;
435 case ARM_AM::asr: SBits = 0x5; break;
436 case ARM_AM::ror: SBits = 0x7; break;
437 case ARM_AM::rrx: SBits = 0x6; break;
438 }
439 } else {
440 // Set shift operand (bit[6:4]).
441 // LSL - 000
442 // LSR - 010
443 // ASR - 100
444 // ROR - 110
445 switch (SOpc) {
446 default: llvm_unreachable("Unknown shift opc!");
447 case ARM_AM::lsl: SBits = 0x0; break;
448 case ARM_AM::lsr: SBits = 0x2; break;
449 case ARM_AM::asr: SBits = 0x4; break;
450 case ARM_AM::ror: SBits = 0x6; break;
451 }
452 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000453
Jim Grosbachef324d72010-10-12 23:53:58 +0000454 Binary |= SBits << 4;
455 if (SOpc == ARM_AM::rrx)
456 return Binary;
457
458 // Encode the shift operation Rs or shift_imm (except rrx).
459 if (Rs) {
460 // Encode Rs bit[11:8].
461 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
462 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
463 }
464
465 // Encode shift_imm bit[11:7].
466 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
467}
468
Jim Grosbach806e80e2010-11-03 23:52:49 +0000469unsigned ARMMCCodeEmitter::
470getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
471 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000472 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
473 // msb of the mask.
474 const MCOperand &MO = MI.getOperand(Op);
475 uint32_t v = ~MO.getImm();
476 uint32_t lsb = CountTrailingZeros_32(v);
477 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
478 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
479 return lsb | (msb << 5);
480}
481
Jim Grosbach806e80e2010-11-03 23:52:49 +0000482unsigned ARMMCCodeEmitter::
483getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000484 SmallVectorImpl<MCFixup> &Fixups) const {
485 // Convert a list of GPRs into a bitfield (R0 -> bit 0). For each
486 // register in the list, set the corresponding bit.
487 unsigned Binary = 0;
488 for (unsigned i = Op, e = MI.getNumOperands(); i < e; ++i) {
489 unsigned regno = getARMRegisterNumbering(MI.getOperand(i).getReg());
490 Binary |= 1 << regno;
491 }
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000492 return Binary;
493}
494
Jim Grosbach806e80e2010-11-03 23:52:49 +0000495unsigned ARMMCCodeEmitter::
496getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
497 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000498 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000499 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +0000500
Owen Andersond9aa7d32010-11-02 00:05:05 +0000501 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000502 unsigned Align = 0;
503
504 switch (Imm.getImm()) {
505 default: break;
506 case 2:
507 case 4:
508 case 8: Align = 0x01; break;
509 case 16: Align = 0x02; break;
510 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000511 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000512
Owen Andersond9aa7d32010-11-02 00:05:05 +0000513 return RegNo | (Align << 4);
514}
515
Jim Grosbach806e80e2010-11-03 23:52:49 +0000516unsigned ARMMCCodeEmitter::
517getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
518 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000519 const MCOperand &MO = MI.getOperand(Op);
520 if (MO.getReg() == 0) return 0x0D;
521 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +0000522}
523
Jim Grosbach568eeed2010-09-17 18:46:17 +0000524void ARMMCCodeEmitter::
525EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000526 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000527 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +0000528 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
529 if ((Desc.TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000530 return;
531
Jim Grosbach70933262010-11-04 01:12:30 +0000532 EmitConstant(getBinaryCodeForInstr(MI, Fixups), 4, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +0000533 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +0000534}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000535
Jim Grosbach806e80e2010-11-03 23:52:49 +0000536#include "ARMGenMCCodeEmitter.inc"