blob: ba1f6d30ca2659503552a096441d31f17885ce94 [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 Grosbachdff84b02010-12-02 00:28:45 +000048 // name off bits flags
49 { "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
50 { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersond8e351b2010-12-08 00:18:36 +000051 { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000052 { "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
53 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach662a8162010-12-06 23:57:07 +000054 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000055 { "fixup_arm_movt_hi16", 0, 16, 0 },
56 { "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000057 };
58
59 if (Kind < FirstTargetFixupKind)
60 return MCCodeEmitter::getFixupKindInfo(Kind);
61
62 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
63 "Invalid kind!");
64 return Infos[Kind - FirstTargetFixupKind];
65 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000066 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
67
Jim Grosbach9af82ba2010-10-07 21:57:55 +000068 // getBinaryCodeForInstr - TableGen'erated function for getting the
69 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000070 unsigned getBinaryCodeForInstr(const MCInst &MI,
71 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000072
73 /// getMachineOpValue - Return binary encoding of operand. If the machine
74 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000075 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
76 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000077
Jason W Kim837caa92010-11-18 23:37:15 +000078 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
79 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
80 SmallVectorImpl<MCFixup> &Fixups) const;
81
Bill Wendling92b5a2e2010-11-03 01:49:29 +000082 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000083 unsigned &Reg, unsigned &Imm,
84 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000085
Jim Grosbach662a8162010-12-06 23:57:07 +000086 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
87 /// branch target.
88 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
89 SmallVectorImpl<MCFixup> &Fixups) const;
90
Jim Grosbachc466b932010-11-11 18:04:49 +000091 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
92 /// branch target.
93 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
94 SmallVectorImpl<MCFixup> &Fixups) const;
95
Jim Grosbach5d14f9b2010-12-01 19:47:31 +000096 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
97 /// ADR label target.
98 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
99 SmallVectorImpl<MCFixup> &Fixups) const;
100
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000101 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
102 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000103 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
104 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000105
Owen Anderson9d63d902010-12-01 19:18:46 +0000106 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
107 /// operand.
108 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
109 SmallVectorImpl<MCFixup> &Fixups) const;
110
111
Jim Grosbach54fea632010-11-09 17:20:53 +0000112 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
113 /// operand as needed by load/store instructions.
114 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
115 SmallVectorImpl<MCFixup> &Fixups) const;
116
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000117 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
118 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
119 SmallVectorImpl<MCFixup> &Fixups) const {
120 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
121 switch (Mode) {
122 default: assert(0 && "Unknown addressing sub-mode!");
123 case ARM_AM::da: return 0;
124 case ARM_AM::ia: return 1;
125 case ARM_AM::db: return 2;
126 case ARM_AM::ib: return 3;
127 }
128 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000129 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
130 ///
131 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
132 switch (ShOpc) {
133 default: llvm_unreachable("Unknown shift opc!");
134 case ARM_AM::no_shift:
135 case ARM_AM::lsl: return 0;
136 case ARM_AM::lsr: return 1;
137 case ARM_AM::asr: return 2;
138 case ARM_AM::ror:
139 case ARM_AM::rrx: return 3;
140 }
141 return 0;
142 }
143
144 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
145 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
146 SmallVectorImpl<MCFixup> &Fixups) const;
147
148 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
149 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
150 SmallVectorImpl<MCFixup> &Fixups) const;
151
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000152 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
153 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
154 SmallVectorImpl<MCFixup> &Fixups) const;
155
Jim Grosbach570a9222010-11-11 01:09:40 +0000156 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
157 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
158 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000159
Jim Grosbachd967cd02010-12-07 21:50:47 +0000160 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
161 /// operand.
162 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
163 SmallVectorImpl<MCFixup> &Fixups) const;
164
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000165 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
166 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
167 SmallVectorImpl<MCFixup> &Fixups) const;
168
Bill Wendling1fd374e2010-11-30 22:57:21 +0000169 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
170 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
171 SmallVectorImpl<MCFixup> &Fixups) const;
172
173 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
174 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
175 SmallVectorImpl<MCFixup> &Fixups) const;
176
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000177 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000178 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
179 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000180
Jim Grosbach08bd5492010-10-12 23:00:24 +0000181 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000182 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
183 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000184 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
185 // '1' respectively.
186 return MI.getOperand(Op).getReg() == ARM::CPSR;
187 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000188
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000189 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000190 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
191 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000192 unsigned SoImm = MI.getOperand(Op).getImm();
193 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
194 assert(SoImmVal != -1 && "Not a valid so_imm value!");
195
196 // Encode rotate_imm.
197 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
198 << ARMII::SoRotImmShift;
199
200 // Encode immed_8.
201 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
202 return Binary;
203 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000204
205 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
206 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
207 SmallVectorImpl<MCFixup> &Fixups) const {
208 unsigned SoImm = MI.getOperand(Op).getImm();
209 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
210 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
211 return Encoded;
212 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000213
Owen Anderson75579f72010-11-29 22:44:32 +0000214 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
215 SmallVectorImpl<MCFixup> &Fixups) const;
216 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
217 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000218 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
219 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000220 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
221 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000222
Jim Grosbachef324d72010-10-12 23:53:58 +0000223 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000224 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
225 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000226 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
227 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000228
Jim Grosbach806e80e2010-11-03 23:52:49 +0000229 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
230 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000231 switch (MI.getOperand(Op).getImm()) {
232 default: assert (0 && "Not a valid rot_imm value!");
233 case 0: return 0;
234 case 8: return 1;
235 case 16: return 2;
236 case 24: return 3;
237 }
238 }
239
Jim Grosbach806e80e2010-11-03 23:52:49 +0000240 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
241 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000242 return MI.getOperand(Op).getImm() - 1;
243 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000244
Jim Grosbach806e80e2010-11-03 23:52:49 +0000245 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
246 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000247 return 64 - MI.getOperand(Op).getImm();
248 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000249
Jim Grosbach806e80e2010-11-03 23:52:49 +0000250 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
251 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000252
Jim Grosbach806e80e2010-11-03 23:52:49 +0000253 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
254 SmallVectorImpl<MCFixup> &Fixups) const;
255 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
256 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000257 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
258 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000259 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
260 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000261
Owen Andersonc7139a62010-11-11 19:07:48 +0000262 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
263 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000264 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000265 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000266 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000267 unsigned EncodedValue) const;
268
269 unsigned VFPThumb2PostEncoder(const MCInst &MI,
270 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000271
Jim Grosbach70933262010-11-04 01:12:30 +0000272 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000273 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000274 }
275
Jim Grosbach70933262010-11-04 01:12:30 +0000276 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000277 // Output the constant in little endian byte order.
278 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000279 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000280 Val >>= 8;
281 }
282 }
283
Jim Grosbach568eeed2010-09-17 18:46:17 +0000284 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
285 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000286};
287
288} // end anonymous namespace
289
Bill Wendling0800ce72010-11-02 22:53:11 +0000290MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
291 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000292 return new ARMMCCodeEmitter(TM, Ctx);
293}
294
Owen Anderson57dac882010-11-11 21:36:43 +0000295/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000296/// instructions, and rewrite them to their Thumb2 form if we are currently in
297/// Thumb2 mode.
298unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
299 unsigned EncodedValue) const {
300 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
301 if (Subtarget.isThumb2()) {
302 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
303 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
304 // set to 1111.
305 unsigned Bit24 = EncodedValue & 0x01000000;
306 unsigned Bit28 = Bit24 << 4;
307 EncodedValue &= 0xEFFFFFFF;
308 EncodedValue |= Bit28;
309 EncodedValue |= 0x0F000000;
310 }
311
312 return EncodedValue;
313}
314
Owen Anderson57dac882010-11-11 21:36:43 +0000315/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
316/// instructions, and rewrite them to their Thumb2 form if we are currently in
317/// Thumb2 mode.
318unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
319 unsigned EncodedValue) const {
320 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
321 if (Subtarget.isThumb2()) {
322 EncodedValue &= 0xF0FFFFFF;
323 EncodedValue |= 0x09000000;
324 }
325
326 return EncodedValue;
327}
328
Owen Anderson8f143912010-11-11 23:12:55 +0000329/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
330/// instructions, and rewrite them to their Thumb2 form if we are currently in
331/// Thumb2 mode.
332unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
333 unsigned EncodedValue) const {
334 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
335 if (Subtarget.isThumb2()) {
336 EncodedValue &= 0x00FFFFFF;
337 EncodedValue |= 0xEE000000;
338 }
339
340 return EncodedValue;
341}
342
Bill Wendlingcf590262010-12-01 21:54:50 +0000343/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
344/// them to their Thumb2 form if we are currently in Thumb2 mode.
345unsigned ARMMCCodeEmitter::
346VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
347 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
348 EncodedValue &= 0x0FFFFFFF;
349 EncodedValue |= 0xE0000000;
350 }
351 return EncodedValue;
352}
Owen Anderson57dac882010-11-11 21:36:43 +0000353
Jim Grosbach56ac9072010-10-08 21:45:55 +0000354/// getMachineOpValue - Return binary encoding of operand. If the machine
355/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000356unsigned ARMMCCodeEmitter::
357getMachineOpValue(const MCInst &MI, const MCOperand &MO,
358 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000359 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000360 unsigned Reg = MO.getReg();
361 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000362
Jim Grosbachb0708d22010-11-30 23:51:41 +0000363 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000364 switch (Reg) {
365 default:
366 return RegNo;
367 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
368 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
369 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
370 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
371 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000372 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000373 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000374 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000375 } else if (MO.isFPImm()) {
376 return static_cast<unsigned>(APFloat(MO.getFPImm())
377 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000378 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000379
Jim Grosbach817c1a62010-11-19 00:27:09 +0000380 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000381 return 0;
382}
383
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000384/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000385bool ARMMCCodeEmitter::
386EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
387 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000388 const MCOperand &MO = MI.getOperand(OpIdx);
389 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000390
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000391 Reg = getARMRegisterNumbering(MO.getReg());
392
393 int32_t SImm = MO1.getImm();
394 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000395
Jim Grosbachab682a22010-10-28 18:34:10 +0000396 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000397 if (SImm == INT32_MIN)
398 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000399
Jim Grosbachab682a22010-10-28 18:34:10 +0000400 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000401 if (SImm < 0) {
402 SImm = -SImm;
403 isAdd = false;
404 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000405
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000406 Imm = SImm;
407 return isAdd;
408}
409
Jim Grosbach662a8162010-12-06 23:57:07 +0000410/// getThumbBLTargetOpValue - Return encoding info for immediate
411/// branch target.
412uint32_t ARMMCCodeEmitter::
413getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
414 SmallVectorImpl<MCFixup> &Fixups) const {
415 const MCOperand &MO = MI.getOperand(OpIdx);
416
417 // If the destination is an immediate, we have nothing to do.
418 if (MO.isImm()) return MO.getImm();
419 assert (MO.isExpr() && "Unexpected branch target type!");
420 const MCExpr *Expr = MO.getExpr();
421 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_thumb_bl);
422 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
423
424 // All of the information is in the fixup.
425 return 0;
426}
427
Jim Grosbachc466b932010-11-11 18:04:49 +0000428/// getBranchTargetOpValue - Return encoding info for 24-bit immediate
429/// branch target.
430uint32_t ARMMCCodeEmitter::
431getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
432 SmallVectorImpl<MCFixup> &Fixups) const {
433 const MCOperand &MO = MI.getOperand(OpIdx);
434
435 // If the destination is an immediate, we have nothing to do.
436 if (MO.isImm()) return MO.getImm();
437 assert (MO.isExpr() && "Unexpected branch target type!");
438 const MCExpr *Expr = MO.getExpr();
439 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
440 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
441
442 // All of the information is in the fixup.
443 return 0;
444}
445
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000446/// getAdrLabelOpValue - Return encoding info for 12-bit immediate
447/// ADR label target.
448uint32_t ARMMCCodeEmitter::
449getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
450 SmallVectorImpl<MCFixup> &Fixups) const {
451 const MCOperand &MO = MI.getOperand(OpIdx);
Jim Grosbachdff84b02010-12-02 00:28:45 +0000452 assert (MO.isExpr() && "Unexpected adr target type!");
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000453 const MCExpr *Expr = MO.getExpr();
Jim Grosbachdff84b02010-12-02 00:28:45 +0000454 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_adr_pcrel_12);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000455 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000456 // All of the information is in the fixup.
457 return 0;
458}
459
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000460/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000461uint32_t ARMMCCodeEmitter::
462getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
463 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000464 // {17-13} = reg
465 // {12} = (U)nsigned (add == '1', sub == '0')
466 // {11-0} = imm12
467 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000468 bool isAdd = true;
469 // If The first operand isn't a register, we have a label reference.
470 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000471 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
472 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000473 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000474 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000475 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000476
Owen Andersoneb6779c2010-12-07 00:45:21 +0000477 const MCExpr *Expr = 0;
478 if (!MO.isReg())
479 Expr = MO.getExpr();
480 else
481 Expr = MO2.getExpr();
482
Jim Grosbachdff84b02010-12-02 00:28:45 +0000483 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000484 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
485
486 ++MCNumCPRelocations;
487 } else
488 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000489
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000490 uint32_t Binary = Imm12 & 0xfff;
491 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000492 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000493 Binary |= (1 << 12);
494 Binary |= (Reg << 13);
495 return Binary;
496}
497
Owen Anderson9d63d902010-12-01 19:18:46 +0000498/// getT2AddrModeImm8s4OpValue - Return encoding info for
499/// 'reg +/- imm8<<2' operand.
500uint32_t ARMMCCodeEmitter::
501getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
502 SmallVectorImpl<MCFixup> &Fixups) const {
503 // {17-13} = reg
504 // {12} = (U)nsigned (add == '1', sub == '0')
505 // {11-0} = imm8
506 unsigned Reg, Imm8;
507 bool isAdd = true;
508 // If The first operand isn't a register, we have a label reference.
509 const MCOperand &MO = MI.getOperand(OpIdx);
510 if (!MO.isReg()) {
511 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
512 Imm8 = 0;
513 isAdd = false ; // 'U' bit is set as part of the fixup.
514
515 assert(MO.isExpr() && "Unexpected machine operand type!");
516 const MCExpr *Expr = MO.getExpr();
517 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
518 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
519
520 ++MCNumCPRelocations;
521 } else
522 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
523
524 uint32_t Binary = (Imm8 >> 2) & 0xff;
525 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
526 if (isAdd)
527 Binary |= (1 << 9);
528 Binary |= (Reg << 9);
529 return Binary;
530}
531
Jim Grosbach54fea632010-11-09 17:20:53 +0000532uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000533getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
534 SmallVectorImpl<MCFixup> &Fixups) const {
535 // {20-16} = imm{15-12}
536 // {11-0} = imm{11-0}
537 const MCOperand &MO = MI.getOperand(OpIdx);
538 if (MO.isImm()) {
539 return static_cast<unsigned>(MO.getImm());
540 } else if (const MCSymbolRefExpr *Expr =
541 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
542 MCFixupKind Kind;
543 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000544 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000545 case MCSymbolRefExpr::VK_ARM_HI16:
546 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
547 break;
548 case MCSymbolRefExpr::VK_ARM_LO16:
549 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
550 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000551 }
552 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
553 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000554 };
555 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000556 return 0;
557}
558
559uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000560getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
561 SmallVectorImpl<MCFixup> &Fixups) const {
562 const MCOperand &MO = MI.getOperand(OpIdx);
563 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
564 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
565 unsigned Rn = getARMRegisterNumbering(MO.getReg());
566 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000567 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
568 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000569 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
570 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000571
572 // {16-13} = Rn
573 // {12} = isAdd
574 // {11-0} = shifter
575 // {3-0} = Rm
576 // {4} = 0
577 // {6-5} = type
578 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000579 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000580 Binary |= Rn << 13;
581 Binary |= SBits << 5;
582 Binary |= ShImm << 7;
583 if (isAdd)
584 Binary |= 1 << 12;
585 return Binary;
586}
587
Jim Grosbach570a9222010-11-11 01:09:40 +0000588uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000589getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
590 SmallVectorImpl<MCFixup> &Fixups) const {
591 // {17-14} Rn
592 // {13} 1 == imm12, 0 == Rm
593 // {12} isAdd
594 // {11-0} imm12/Rm
595 const MCOperand &MO = MI.getOperand(OpIdx);
596 unsigned Rn = getARMRegisterNumbering(MO.getReg());
597 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
598 Binary |= Rn << 14;
599 return Binary;
600}
601
602uint32_t ARMMCCodeEmitter::
603getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
604 SmallVectorImpl<MCFixup> &Fixups) const {
605 // {13} 1 == imm12, 0 == Rm
606 // {12} isAdd
607 // {11-0} imm12/Rm
608 const MCOperand &MO = MI.getOperand(OpIdx);
609 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
610 unsigned Imm = MO1.getImm();
611 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
612 bool isReg = MO.getReg() != 0;
613 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
614 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
615 if (isReg) {
616 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
617 Binary <<= 7; // Shift amount is bits [11:7]
618 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
619 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
620 }
621 return Binary | (isAdd << 12) | (isReg << 13);
622}
623
624uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000625getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
626 SmallVectorImpl<MCFixup> &Fixups) const {
627 // {9} 1 == imm8, 0 == Rm
628 // {8} isAdd
629 // {7-4} imm7_4/zero
630 // {3-0} imm3_0/Rm
631 const MCOperand &MO = MI.getOperand(OpIdx);
632 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
633 unsigned Imm = MO1.getImm();
634 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
635 bool isImm = MO.getReg() == 0;
636 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
637 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
638 if (!isImm)
639 Imm8 = getARMRegisterNumbering(MO.getReg());
640 return Imm8 | (isAdd << 8) | (isImm << 9);
641}
642
643uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000644getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
645 SmallVectorImpl<MCFixup> &Fixups) const {
646 // {13} 1 == imm8, 0 == Rm
647 // {12-9} Rn
648 // {8} isAdd
649 // {7-4} imm7_4/zero
650 // {3-0} imm3_0/Rm
651 const MCOperand &MO = MI.getOperand(OpIdx);
652 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
653 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
654 unsigned Rn = getARMRegisterNumbering(MO.getReg());
655 unsigned Imm = MO2.getImm();
656 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
657 bool isImm = MO1.getReg() == 0;
658 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
659 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
660 if (!isImm)
661 Imm8 = getARMRegisterNumbering(MO1.getReg());
662 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
663}
664
Jim Grosbachd967cd02010-12-07 21:50:47 +0000665/// getAddrModeThumbSPOpValue- Encode the t_addrmode_sp operands.
666uint32_t ARMMCCodeEmitter::
667getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
668 SmallVectorImpl<MCFixup> &Fixups) const {
669 // [SP, #imm]
670 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000671 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Matt Beaumont-Gay2bf315f2010-12-07 23:26:21 +0000672 assert (MI.getOperand(OpIdx).getReg() == ARM::SP &&
673 "Unexpected base register!");
Jim Grosbachd967cd02010-12-07 21:50:47 +0000674 // The immediate is already shifted for the implicit zeroes, so no change
675 // here.
676 return MO1.getImm() & 0xff;
677}
678
Bill Wendling1fd374e2010-11-30 22:57:21 +0000679/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
680static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
681 unsigned Scale) {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000682 // [Rn, Rm]
683 // {5-3} = Rm
684 // {2-0} = Rn
685 //
686 // [Rn, #imm]
687 // {7-3} = imm5
688 // {2-0} = Rn
689 const MCOperand &MO = MI.getOperand(OpIdx);
690 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
691 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
692 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling1fd374e2010-11-30 22:57:21 +0000693 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
Bill Wendling0bdf0c02010-12-03 00:53:22 +0000694
695 if (MO2.getReg() != 0)
696 // Is an immediate.
697 Imm5 = getARMRegisterNumbering(MO2.getReg());
698
699 return (Imm5 << 3) | Rn;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000700}
701
Bill Wendling1fd374e2010-11-30 22:57:21 +0000702/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
703uint32_t ARMMCCodeEmitter::
704getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
705 SmallVectorImpl<MCFixup> &) const {
706 return getAddrModeSOpValue(MI, OpIdx, 4);
707}
708
709/// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
710uint32_t ARMMCCodeEmitter::
711getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
712 SmallVectorImpl<MCFixup> &) const {
713 return getAddrModeSOpValue(MI, OpIdx, 2);
714}
715
716/// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
717uint32_t ARMMCCodeEmitter::
718getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
719 SmallVectorImpl<MCFixup> &) const {
720 return getAddrModeSOpValue(MI, OpIdx, 1);
721}
722
Jim Grosbach5177f792010-12-01 21:09:40 +0000723/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000724uint32_t ARMMCCodeEmitter::
725getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
726 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000727 // {12-9} = reg
728 // {8} = (U)nsigned (add == '1', sub == '0')
729 // {7-0} = imm8
730 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000731 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000732 // If The first operand isn't a register, we have a label reference.
733 const MCOperand &MO = MI.getOperand(OpIdx);
734 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000735 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000736 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000737 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000738
739 assert(MO.isExpr() && "Unexpected machine operand type!");
740 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000741 MCFixupKind Kind;
742 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
743 if (Subtarget.isThumb2())
744 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
745 else
746 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000747 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
748
749 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000750 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000751 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000752 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
753 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000754
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000755 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
756 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000757 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000758 Binary |= (1 << 8);
759 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000760 return Binary;
761}
762
Jim Grosbach806e80e2010-11-03 23:52:49 +0000763unsigned ARMMCCodeEmitter::
764getSORegOpValue(const MCInst &MI, unsigned OpIdx,
765 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000766 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
767 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
768 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000769 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000770 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000771 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000772 // {6-5} = type
773 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000774 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000775 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000776 // else (imm shift)
777 // {11-7} = imm
778
779 const MCOperand &MO = MI.getOperand(OpIdx);
780 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
781 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
782 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
783
784 // Encode Rm.
785 unsigned Binary = getARMRegisterNumbering(MO.getReg());
786
787 // Encode the shift opcode.
788 unsigned SBits = 0;
789 unsigned Rs = MO1.getReg();
790 if (Rs) {
791 // Set shift operand (bit[7:4]).
792 // LSL - 0001
793 // LSR - 0011
794 // ASR - 0101
795 // ROR - 0111
796 // RRX - 0110 and bit[11:8] clear.
797 switch (SOpc) {
798 default: llvm_unreachable("Unknown shift opc!");
799 case ARM_AM::lsl: SBits = 0x1; break;
800 case ARM_AM::lsr: SBits = 0x3; break;
801 case ARM_AM::asr: SBits = 0x5; break;
802 case ARM_AM::ror: SBits = 0x7; break;
803 case ARM_AM::rrx: SBits = 0x6; break;
804 }
805 } else {
806 // Set shift operand (bit[6:4]).
807 // LSL - 000
808 // LSR - 010
809 // ASR - 100
810 // ROR - 110
811 switch (SOpc) {
812 default: llvm_unreachable("Unknown shift opc!");
813 case ARM_AM::lsl: SBits = 0x0; break;
814 case ARM_AM::lsr: SBits = 0x2; break;
815 case ARM_AM::asr: SBits = 0x4; break;
816 case ARM_AM::ror: SBits = 0x6; break;
817 }
818 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000819
Jim Grosbachef324d72010-10-12 23:53:58 +0000820 Binary |= SBits << 4;
821 if (SOpc == ARM_AM::rrx)
822 return Binary;
823
824 // Encode the shift operation Rs or shift_imm (except rrx).
825 if (Rs) {
826 // Encode Rs bit[11:8].
827 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
828 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
829 }
830
831 // Encode shift_imm bit[11:7].
832 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
833}
834
Jim Grosbach806e80e2010-11-03 23:52:49 +0000835unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000836getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
837 SmallVectorImpl<MCFixup> &Fixups) const {
838 const MCOperand &MO1 = MI.getOperand(OpNum);
839 const MCOperand &MO2 = MI.getOperand(OpNum+1);
840 const MCOperand &MO3 = MI.getOperand(OpNum+2);
841
842 // Encoded as [Rn, Rm, imm].
843 // FIXME: Needs fixup support.
844 unsigned Value = getARMRegisterNumbering(MO1.getReg());
845 Value <<= 4;
846 Value |= getARMRegisterNumbering(MO2.getReg());
847 Value <<= 2;
848 Value |= MO3.getImm();
849
850 return Value;
851}
852
853unsigned ARMMCCodeEmitter::
854getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
855 SmallVectorImpl<MCFixup> &Fixups) const {
856 const MCOperand &MO1 = MI.getOperand(OpNum);
857 const MCOperand &MO2 = MI.getOperand(OpNum+1);
858
859 // FIXME: Needs fixup support.
860 unsigned Value = getARMRegisterNumbering(MO1.getReg());
861
862 // Even though the immediate is 8 bits long, we need 9 bits in order
863 // to represent the (inverse of the) sign bit.
864 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000865 int32_t tmp = (int32_t)MO2.getImm();
866 if (tmp < 0)
867 tmp = abs(tmp);
868 else
869 Value |= 256; // Set the ADD bit
870 Value |= tmp & 255;
871 return Value;
872}
873
874unsigned ARMMCCodeEmitter::
875getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
876 SmallVectorImpl<MCFixup> &Fixups) const {
877 const MCOperand &MO1 = MI.getOperand(OpNum);
878
879 // FIXME: Needs fixup support.
880 unsigned Value = 0;
881 int32_t tmp = (int32_t)MO1.getImm();
882 if (tmp < 0)
883 tmp = abs(tmp);
884 else
885 Value |= 256; // Set the ADD bit
886 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000887 return Value;
888}
889
890unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000891getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
892 SmallVectorImpl<MCFixup> &Fixups) const {
893 const MCOperand &MO1 = MI.getOperand(OpNum);
894
895 // FIXME: Needs fixup support.
896 unsigned Value = 0;
897 int32_t tmp = (int32_t)MO1.getImm();
898 if (tmp < 0)
899 tmp = abs(tmp);
900 else
901 Value |= 4096; // Set the ADD bit
902 Value |= tmp & 4095;
903 return Value;
904}
905
906unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000907getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
908 SmallVectorImpl<MCFixup> &Fixups) const {
909 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
910 // shifted. The second is the amount to shift by.
911 //
912 // {3-0} = Rm.
913 // {4} = 0
914 // {6-5} = type
915 // {11-7} = imm
916
917 const MCOperand &MO = MI.getOperand(OpIdx);
918 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
919 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
920
921 // Encode Rm.
922 unsigned Binary = getARMRegisterNumbering(MO.getReg());
923
924 // Encode the shift opcode.
925 unsigned SBits = 0;
926 // Set shift operand (bit[6:4]).
927 // LSL - 000
928 // LSR - 010
929 // ASR - 100
930 // ROR - 110
931 switch (SOpc) {
932 default: llvm_unreachable("Unknown shift opc!");
933 case ARM_AM::lsl: SBits = 0x0; break;
934 case ARM_AM::lsr: SBits = 0x2; break;
935 case ARM_AM::asr: SBits = 0x4; break;
936 case ARM_AM::ror: SBits = 0x6; break;
937 }
938
939 Binary |= SBits << 4;
940 if (SOpc == ARM_AM::rrx)
941 return Binary;
942
943 // Encode shift_imm bit[11:7].
944 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
945}
946
947unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000948getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
949 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000950 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
951 // msb of the mask.
952 const MCOperand &MO = MI.getOperand(Op);
953 uint32_t v = ~MO.getImm();
954 uint32_t lsb = CountTrailingZeros_32(v);
955 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
956 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
957 return lsb | (msb << 5);
958}
959
Jim Grosbach806e80e2010-11-03 23:52:49 +0000960unsigned ARMMCCodeEmitter::
961getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000962 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +0000963 // VLDM/VSTM:
964 // {12-8} = Vd
965 // {7-0} = Number of registers
966 //
967 // LDM/STM:
968 // {15-0} = Bitfield of GPRs.
969 unsigned Reg = MI.getOperand(Op).getReg();
970 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
971 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
972
Bill Wendling5e559a22010-11-09 00:30:18 +0000973 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +0000974
975 if (SPRRegs || DPRRegs) {
976 // VLDM/VSTM
977 unsigned RegNo = getARMRegisterNumbering(Reg);
978 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
979 Binary |= (RegNo & 0x1f) << 8;
980 if (SPRRegs)
981 Binary |= NumRegs;
982 else
983 Binary |= NumRegs * 2;
984 } else {
985 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
986 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
987 Binary |= 1 << RegNo;
988 }
Bill Wendling5e559a22010-11-09 00:30:18 +0000989 }
Bill Wendling6bc105a2010-11-17 00:45:23 +0000990
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000991 return Binary;
992}
993
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000994/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
995/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000996unsigned ARMMCCodeEmitter::
997getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
998 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000999 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001000 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001001
Owen Andersond9aa7d32010-11-02 00:05:05 +00001002 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001003 unsigned Align = 0;
1004
1005 switch (Imm.getImm()) {
1006 default: break;
1007 case 2:
1008 case 4:
1009 case 8: Align = 0x01; break;
1010 case 16: Align = 0x02; break;
1011 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001012 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001013
Owen Andersond9aa7d32010-11-02 00:05:05 +00001014 return RegNo | (Align << 4);
1015}
1016
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001017/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1018/// alignment operand for use in VLD-dup instructions. This is the same as
1019/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1020/// different for VLD4-dup.
1021unsigned ARMMCCodeEmitter::
1022getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1023 SmallVectorImpl<MCFixup> &Fixups) const {
1024 const MCOperand &Reg = MI.getOperand(Op);
1025 const MCOperand &Imm = MI.getOperand(Op + 1);
1026
1027 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1028 unsigned Align = 0;
1029
1030 switch (Imm.getImm()) {
1031 default: break;
1032 case 2:
1033 case 4:
1034 case 8: Align = 0x01; break;
1035 case 16: Align = 0x03; break;
1036 }
1037
1038 return RegNo | (Align << 4);
1039}
1040
Jim Grosbach806e80e2010-11-03 23:52:49 +00001041unsigned ARMMCCodeEmitter::
1042getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1043 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001044 const MCOperand &MO = MI.getOperand(Op);
1045 if (MO.getReg() == 0) return 0x0D;
1046 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001047}
1048
Jim Grosbach568eeed2010-09-17 18:46:17 +00001049void ARMMCCodeEmitter::
1050EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001051 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001052 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001053 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001054 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001055 uint64_t TSFlags = Desc.TSFlags;
1056 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001057 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001058 int Size;
1059 // Basic size info comes from the TSFlags field.
1060 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1061 default: llvm_unreachable("Unexpected instruction size!");
1062 case ARMII::Size2Bytes: Size = 2; break;
1063 case ARMII::Size4Bytes: Size = 4; break;
1064 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001065 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1066 // Thumb 32-bit wide instructions need to be have the high order halfword
1067 // emitted first.
1068 if (Subtarget.isThumb() && Size == 4) {
1069 EmitConstant(Binary >> 16, 2, OS);
1070 EmitConstant(Binary & 0xffff, 2, OS);
1071 } else
1072 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001073 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001074}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001075
Jim Grosbach806e80e2010-11-03 23:52:49 +00001076#include "ARMGenMCCodeEmitter.inc"