blob: b156755bb501c181cb1400d41c88c39d91e56231 [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
Chris Lattner2ac19022010-11-15 05:19:05 +000014#define DEBUG_TYPE "mccodeemitter"
Jim Grosbach568eeed2010-09-17 18:46:17 +000015#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 Grosbachc466b932010-11-11 18:04:49 +000044 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
Jim Grosbach70933262010-11-04 01:12:30 +000045
46 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
47 const static MCFixupKindInfo Infos[] = {
Jim Grosbachc466b932010-11-11 18:04:49 +000048 // name offset bits flags
49 { "fixup_arm_pcrel_12", 2, 12, MCFixupKindInfo::FKF_IsPCRel },
50 { "fixup_arm_vfp_pcrel_12", 3, 8, MCFixupKindInfo::FKF_IsPCRel },
51 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach70933262010-11-04 01:12:30 +000052 };
53
54 if (Kind < FirstTargetFixupKind)
55 return MCCodeEmitter::getFixupKindInfo(Kind);
56
57 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
58 "Invalid kind!");
59 return Infos[Kind - FirstTargetFixupKind];
60 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000061 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
62
Jim Grosbach9af82ba2010-10-07 21:57:55 +000063 // getBinaryCodeForInstr - TableGen'erated function for getting the
64 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000065 unsigned getBinaryCodeForInstr(const MCInst &MI,
66 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000067
68 /// getMachineOpValue - Return binary encoding of operand. If the machine
69 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000070 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
71 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000072
Bill Wendling92b5a2e2010-11-03 01:49:29 +000073 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000074 unsigned &Reg, unsigned &Imm,
75 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000076
Jim Grosbachc466b932010-11-11 18:04:49 +000077 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
78 /// branch target.
79 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
80 SmallVectorImpl<MCFixup> &Fixups) const;
81
Bill Wendling92b5a2e2010-11-03 01:49:29 +000082 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
83 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +000084 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
85 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000086
Jim Grosbach54fea632010-11-09 17:20:53 +000087 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
88 /// operand as needed by load/store instructions.
89 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
90 SmallVectorImpl<MCFixup> &Fixups) const;
91
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +000092 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
93 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
94 SmallVectorImpl<MCFixup> &Fixups) const {
95 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
96 switch (Mode) {
97 default: assert(0 && "Unknown addressing sub-mode!");
98 case ARM_AM::da: return 0;
99 case ARM_AM::ia: return 1;
100 case ARM_AM::db: return 2;
101 case ARM_AM::ib: return 3;
102 }
103 }
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000104 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
105 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
106 SmallVectorImpl<MCFixup> &Fixups) const;
107
Jim Grosbach570a9222010-11-11 01:09:40 +0000108 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
109 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
110 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000111
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000112 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000113 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
114 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000115
Jim Grosbach08bd5492010-10-12 23:00:24 +0000116 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000117 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
118 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000119 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
120 // '1' respectively.
121 return MI.getOperand(Op).getReg() == ARM::CPSR;
122 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000123
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000124 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000125 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
126 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000127 unsigned SoImm = MI.getOperand(Op).getImm();
128 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
129 assert(SoImmVal != -1 && "Not a valid so_imm value!");
130
131 // Encode rotate_imm.
132 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
133 << ARMII::SoRotImmShift;
134
135 // Encode immed_8.
136 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
137 return Binary;
138 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000139
140 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
141 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
142 SmallVectorImpl<MCFixup> &Fixups) const {
143 unsigned SoImm = MI.getOperand(Op).getImm();
144 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
145 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
146 return Encoded;
147 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000148
Jim Grosbachef324d72010-10-12 23:53:58 +0000149 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000150 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
151 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000152 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
153 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000154
Jim Grosbach806e80e2010-11-03 23:52:49 +0000155 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
156 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000157 switch (MI.getOperand(Op).getImm()) {
158 default: assert (0 && "Not a valid rot_imm value!");
159 case 0: return 0;
160 case 8: return 1;
161 case 16: return 2;
162 case 24: return 3;
163 }
164 }
165
Jim Grosbach806e80e2010-11-03 23:52:49 +0000166 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
167 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000168 return MI.getOperand(Op).getImm() - 1;
169 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000170
Jim Grosbach806e80e2010-11-03 23:52:49 +0000171 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
172 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000173 return 64 - MI.getOperand(Op).getImm();
174 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000175
Jim Grosbach806e80e2010-11-03 23:52:49 +0000176 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
177 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000178
Jim Grosbach806e80e2010-11-03 23:52:49 +0000179 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
180 SmallVectorImpl<MCFixup> &Fixups) const;
181 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
182 SmallVectorImpl<MCFixup> &Fixups) const;
183 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
184 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000185
Owen Andersonc7139a62010-11-11 19:07:48 +0000186 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
187 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000188 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
189 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000190 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
191 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000192
Jim Grosbach70933262010-11-04 01:12:30 +0000193 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000194 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000195 }
196
Jim Grosbach70933262010-11-04 01:12:30 +0000197 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000198 // Output the constant in little endian byte order.
199 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000200 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000201 Val >>= 8;
202 }
203 }
204
Jim Grosbach568eeed2010-09-17 18:46:17 +0000205 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
206 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000207};
208
209} // end anonymous namespace
210
Bill Wendling0800ce72010-11-02 22:53:11 +0000211MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
212 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000213 return new ARMMCCodeEmitter(TM, Ctx);
214}
215
Owen Anderson57dac882010-11-11 21:36:43 +0000216/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000217/// instructions, and rewrite them to their Thumb2 form if we are currently in
218/// Thumb2 mode.
219unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
220 unsigned EncodedValue) const {
221 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
222 if (Subtarget.isThumb2()) {
223 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
224 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
225 // set to 1111.
226 unsigned Bit24 = EncodedValue & 0x01000000;
227 unsigned Bit28 = Bit24 << 4;
228 EncodedValue &= 0xEFFFFFFF;
229 EncodedValue |= Bit28;
230 EncodedValue |= 0x0F000000;
231 }
232
233 return EncodedValue;
234}
235
Owen Anderson57dac882010-11-11 21:36:43 +0000236/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
237/// instructions, and rewrite them to their Thumb2 form if we are currently in
238/// Thumb2 mode.
239unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
240 unsigned EncodedValue) const {
241 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
242 if (Subtarget.isThumb2()) {
243 EncodedValue &= 0xF0FFFFFF;
244 EncodedValue |= 0x09000000;
245 }
246
247 return EncodedValue;
248}
249
Owen Anderson8f143912010-11-11 23:12:55 +0000250/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
251/// instructions, and rewrite them to their Thumb2 form if we are currently in
252/// Thumb2 mode.
253unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
254 unsigned EncodedValue) const {
255 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
256 if (Subtarget.isThumb2()) {
257 EncodedValue &= 0x00FFFFFF;
258 EncodedValue |= 0xEE000000;
259 }
260
261 return EncodedValue;
262}
263
264
Owen Anderson57dac882010-11-11 21:36:43 +0000265
Jim Grosbach56ac9072010-10-08 21:45:55 +0000266/// getMachineOpValue - Return binary encoding of operand. If the machine
267/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000268unsigned ARMMCCodeEmitter::
269getMachineOpValue(const MCInst &MI, const MCOperand &MO,
270 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000271 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000272 unsigned Reg = MO.getReg();
273 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000274
Owen Anderson90d4cf92010-10-21 20:49:13 +0000275 // Q registers are encodes as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000276 switch (Reg) {
277 default:
278 return RegNo;
279 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
280 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
281 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
282 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
283 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000284 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000285 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000286 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000287 } else if (MO.isFPImm()) {
288 return static_cast<unsigned>(APFloat(MO.getFPImm())
289 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000290 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000291
292#ifndef NDEBUG
293 errs() << MO;
294#endif
295 llvm_unreachable(0);
Jim Grosbach56ac9072010-10-08 21:45:55 +0000296 return 0;
297}
298
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000299/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000300bool ARMMCCodeEmitter::
301EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
302 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000303 const MCOperand &MO = MI.getOperand(OpIdx);
304 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000305
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000306 Reg = getARMRegisterNumbering(MO.getReg());
307
308 int32_t SImm = MO1.getImm();
309 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000310
Jim Grosbachab682a22010-10-28 18:34:10 +0000311 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000312 if (SImm == INT32_MIN)
313 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000314
Jim Grosbachab682a22010-10-28 18:34:10 +0000315 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000316 if (SImm < 0) {
317 SImm = -SImm;
318 isAdd = false;
319 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000320
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000321 Imm = SImm;
322 return isAdd;
323}
324
Jim Grosbachc466b932010-11-11 18:04:49 +0000325/// getBranchTargetOpValue - Return encoding info for 24-bit immediate
326/// branch target.
327uint32_t ARMMCCodeEmitter::
328getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
329 SmallVectorImpl<MCFixup> &Fixups) const {
330 const MCOperand &MO = MI.getOperand(OpIdx);
331
332 // If the destination is an immediate, we have nothing to do.
333 if (MO.isImm()) return MO.getImm();
334 assert (MO.isExpr() && "Unexpected branch target type!");
335 const MCExpr *Expr = MO.getExpr();
336 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
337 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
338
339 // All of the information is in the fixup.
340 return 0;
341}
342
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000343/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000344uint32_t ARMMCCodeEmitter::
345getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
346 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000347 // {17-13} = reg
348 // {12} = (U)nsigned (add == '1', sub == '0')
349 // {11-0} = imm12
350 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000351 bool isAdd = true;
352 // If The first operand isn't a register, we have a label reference.
353 const MCOperand &MO = MI.getOperand(OpIdx);
354 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000355 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000356 Imm12 = 0;
357
358 assert(MO.isExpr() && "Unexpected machine operand type!");
359 const MCExpr *Expr = MO.getExpr();
360 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
361 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
362
363 ++MCNumCPRelocations;
364 } else
365 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000366
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000367 uint32_t Binary = Imm12 & 0xfff;
368 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000369 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000370 Binary |= (1 << 12);
371 Binary |= (Reg << 13);
372 return Binary;
373}
374
Jim Grosbach54fea632010-11-09 17:20:53 +0000375uint32_t ARMMCCodeEmitter::
376getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
377 SmallVectorImpl<MCFixup> &Fixups) const {
378 const MCOperand &MO = MI.getOperand(OpIdx);
379 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
380 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
381 unsigned Rn = getARMRegisterNumbering(MO.getReg());
382 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
383 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
384 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
385 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
386 unsigned SBits;
387 // LSL - 00
388 // LSR - 01
389 // ASR - 10
390 // ROR - 11
391 switch (ShOp) {
392 default: llvm_unreachable("Unknown shift opc!");
Jim Grosbachd92354c2010-11-09 17:38:15 +0000393 case ARM_AM::no_shift:
394 assert(ShImm == 0 && "Non-zero shift amount with no shift type!");
395 // fall through
Jim Grosbach54fea632010-11-09 17:20:53 +0000396 case ARM_AM::lsl: SBits = 0x0; break;
397 case ARM_AM::lsr: SBits = 0x1; break;
398 case ARM_AM::asr: SBits = 0x2; break;
399 case ARM_AM::ror: SBits = 0x3; break;
400 }
401
402 // {16-13} = Rn
403 // {12} = isAdd
404 // {11-0} = shifter
405 // {3-0} = Rm
406 // {4} = 0
407 // {6-5} = type
408 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000409 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000410 Binary |= Rn << 13;
411 Binary |= SBits << 5;
412 Binary |= ShImm << 7;
413 if (isAdd)
414 Binary |= 1 << 12;
415 return Binary;
416}
417
Jim Grosbach570a9222010-11-11 01:09:40 +0000418uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000419getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
420 SmallVectorImpl<MCFixup> &Fixups) const {
421 // {9} 1 == imm8, 0 == Rm
422 // {8} isAdd
423 // {7-4} imm7_4/zero
424 // {3-0} imm3_0/Rm
425 const MCOperand &MO = MI.getOperand(OpIdx);
426 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
427 unsigned Imm = MO1.getImm();
428 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
429 bool isImm = MO.getReg() == 0;
430 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
431 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
432 if (!isImm)
433 Imm8 = getARMRegisterNumbering(MO.getReg());
434 return Imm8 | (isAdd << 8) | (isImm << 9);
435}
436
437uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000438getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
439 SmallVectorImpl<MCFixup> &Fixups) const {
440 // {13} 1 == imm8, 0 == Rm
441 // {12-9} Rn
442 // {8} isAdd
443 // {7-4} imm7_4/zero
444 // {3-0} imm3_0/Rm
445 const MCOperand &MO = MI.getOperand(OpIdx);
446 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
447 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
448 unsigned Rn = getARMRegisterNumbering(MO.getReg());
449 unsigned Imm = MO2.getImm();
450 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
451 bool isImm = MO1.getReg() == 0;
452 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
453 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
454 if (!isImm)
455 Imm8 = getARMRegisterNumbering(MO1.getReg());
456 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
457}
458
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000459/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000460uint32_t ARMMCCodeEmitter::
461getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
462 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000463 // {12-9} = reg
464 // {8} = (U)nsigned (add == '1', sub == '0')
465 // {7-0} = imm8
466 unsigned Reg, Imm8;
Jim Grosbach70933262010-11-04 01:12:30 +0000467 // If The first operand isn't a register, we have a label reference.
468 const MCOperand &MO = MI.getOperand(OpIdx);
469 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000470 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000471 Imm8 = 0;
472
473 assert(MO.isExpr() && "Unexpected machine operand type!");
474 const MCExpr *Expr = MO.getExpr();
475 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_vfp_pcrel_12);
476 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
477
478 ++MCNumCPRelocations;
479 } else
480 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000481
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000482 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
483 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
484 if (ARM_AM::getAM5Op(Imm8) == ARM_AM::add)
485 Binary |= (1 << 8);
486 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000487 return Binary;
488}
489
Jim Grosbach806e80e2010-11-03 23:52:49 +0000490unsigned ARMMCCodeEmitter::
491getSORegOpValue(const MCInst &MI, unsigned OpIdx,
492 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000493 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
494 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
495 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000496 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000497 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000498 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000499 // {6-5} = type
500 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000501 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000502 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000503 // else (imm shift)
504 // {11-7} = imm
505
506 const MCOperand &MO = MI.getOperand(OpIdx);
507 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
508 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
509 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
510
511 // Encode Rm.
512 unsigned Binary = getARMRegisterNumbering(MO.getReg());
513
514 // Encode the shift opcode.
515 unsigned SBits = 0;
516 unsigned Rs = MO1.getReg();
517 if (Rs) {
518 // Set shift operand (bit[7:4]).
519 // LSL - 0001
520 // LSR - 0011
521 // ASR - 0101
522 // ROR - 0111
523 // RRX - 0110 and bit[11:8] clear.
524 switch (SOpc) {
525 default: llvm_unreachable("Unknown shift opc!");
526 case ARM_AM::lsl: SBits = 0x1; break;
527 case ARM_AM::lsr: SBits = 0x3; break;
528 case ARM_AM::asr: SBits = 0x5; break;
529 case ARM_AM::ror: SBits = 0x7; break;
530 case ARM_AM::rrx: SBits = 0x6; break;
531 }
532 } else {
533 // Set shift operand (bit[6:4]).
534 // LSL - 000
535 // LSR - 010
536 // ASR - 100
537 // ROR - 110
538 switch (SOpc) {
539 default: llvm_unreachable("Unknown shift opc!");
540 case ARM_AM::lsl: SBits = 0x0; break;
541 case ARM_AM::lsr: SBits = 0x2; break;
542 case ARM_AM::asr: SBits = 0x4; break;
543 case ARM_AM::ror: SBits = 0x6; break;
544 }
545 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000546
Jim Grosbachef324d72010-10-12 23:53:58 +0000547 Binary |= SBits << 4;
548 if (SOpc == ARM_AM::rrx)
549 return Binary;
550
551 // Encode the shift operation Rs or shift_imm (except rrx).
552 if (Rs) {
553 // Encode Rs bit[11:8].
554 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
555 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
556 }
557
558 // Encode shift_imm bit[11:7].
559 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
560}
561
Jim Grosbach806e80e2010-11-03 23:52:49 +0000562unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000563getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
564 SmallVectorImpl<MCFixup> &Fixups) const {
565 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
566 // shifted. The second is the amount to shift by.
567 //
568 // {3-0} = Rm.
569 // {4} = 0
570 // {6-5} = type
571 // {11-7} = imm
572
573 const MCOperand &MO = MI.getOperand(OpIdx);
574 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
575 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
576
577 // Encode Rm.
578 unsigned Binary = getARMRegisterNumbering(MO.getReg());
579
580 // Encode the shift opcode.
581 unsigned SBits = 0;
582 // Set shift operand (bit[6:4]).
583 // LSL - 000
584 // LSR - 010
585 // ASR - 100
586 // ROR - 110
587 switch (SOpc) {
588 default: llvm_unreachable("Unknown shift opc!");
589 case ARM_AM::lsl: SBits = 0x0; break;
590 case ARM_AM::lsr: SBits = 0x2; break;
591 case ARM_AM::asr: SBits = 0x4; break;
592 case ARM_AM::ror: SBits = 0x6; break;
593 }
594
595 Binary |= SBits << 4;
596 if (SOpc == ARM_AM::rrx)
597 return Binary;
598
599 // Encode shift_imm bit[11:7].
600 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
601}
602
603unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000604getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
605 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000606 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
607 // msb of the mask.
608 const MCOperand &MO = MI.getOperand(Op);
609 uint32_t v = ~MO.getImm();
610 uint32_t lsb = CountTrailingZeros_32(v);
611 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
612 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
613 return lsb | (msb << 5);
614}
615
Jim Grosbach806e80e2010-11-03 23:52:49 +0000616unsigned ARMMCCodeEmitter::
617getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000618 SmallVectorImpl<MCFixup> &Fixups) const {
619 // Convert a list of GPRs into a bitfield (R0 -> bit 0). For each
620 // register in the list, set the corresponding bit.
621 unsigned Binary = 0;
622 for (unsigned i = Op, e = MI.getNumOperands(); i < e; ++i) {
623 unsigned regno = getARMRegisterNumbering(MI.getOperand(i).getReg());
624 Binary |= 1 << regno;
625 }
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000626 return Binary;
627}
628
Jim Grosbach806e80e2010-11-03 23:52:49 +0000629unsigned ARMMCCodeEmitter::
630getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
631 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000632 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000633 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +0000634
Owen Andersond9aa7d32010-11-02 00:05:05 +0000635 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000636 unsigned Align = 0;
637
638 switch (Imm.getImm()) {
639 default: break;
640 case 2:
641 case 4:
642 case 8: Align = 0x01; break;
643 case 16: Align = 0x02; break;
644 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000645 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000646
Owen Andersond9aa7d32010-11-02 00:05:05 +0000647 return RegNo | (Align << 4);
648}
649
Jim Grosbach806e80e2010-11-03 23:52:49 +0000650unsigned ARMMCCodeEmitter::
651getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
652 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000653 const MCOperand &MO = MI.getOperand(Op);
654 if (MO.getReg() == 0) return 0x0D;
655 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +0000656}
657
Jim Grosbach568eeed2010-09-17 18:46:17 +0000658void ARMMCCodeEmitter::
659EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000660 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000661 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +0000662 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000663 uint64_t TSFlags = Desc.TSFlags;
664 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000665 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000666 int Size;
667 // Basic size info comes from the TSFlags field.
668 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
669 default: llvm_unreachable("Unexpected instruction size!");
670 case ARMII::Size2Bytes: Size = 2; break;
671 case ARMII::Size4Bytes: Size = 4; break;
672 }
673 EmitConstant(getBinaryCodeForInstr(MI, Fixups), Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +0000674 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +0000675}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000676
Jim Grosbach806e80e2010-11-03 23:52:49 +0000677#include "ARMGenMCCodeEmitter.inc"