blob: b0113476fa8f7cc4df227bca8d938c67945876c6 [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 },
51 { "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
52 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach662a8162010-12-06 23:57:07 +000053 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000054 { "fixup_arm_movt_hi16", 0, 16, 0 },
55 { "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000056 };
57
58 if (Kind < FirstTargetFixupKind)
59 return MCCodeEmitter::getFixupKindInfo(Kind);
60
61 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
62 "Invalid kind!");
63 return Infos[Kind - FirstTargetFixupKind];
64 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000065 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
66
Jim Grosbach9af82ba2010-10-07 21:57:55 +000067 // getBinaryCodeForInstr - TableGen'erated function for getting the
68 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000069 unsigned getBinaryCodeForInstr(const MCInst &MI,
70 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000071
72 /// getMachineOpValue - Return binary encoding of operand. If the machine
73 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000074 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
75 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000076
Jason W Kim837caa92010-11-18 23:37:15 +000077 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
78 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
79 SmallVectorImpl<MCFixup> &Fixups) const;
80
Bill Wendling92b5a2e2010-11-03 01:49:29 +000081 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000082 unsigned &Reg, unsigned &Imm,
83 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000084
Jim Grosbach662a8162010-12-06 23:57:07 +000085 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
86 /// branch target.
87 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
88 SmallVectorImpl<MCFixup> &Fixups) const;
89
Jim Grosbachc466b932010-11-11 18:04:49 +000090 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
91 /// branch target.
92 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
93 SmallVectorImpl<MCFixup> &Fixups) const;
94
Jim Grosbach5d14f9b2010-12-01 19:47:31 +000095 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
96 /// ADR label target.
97 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
98 SmallVectorImpl<MCFixup> &Fixups) const;
99
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000100 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
101 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000102 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
103 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000104
Owen Anderson9d63d902010-12-01 19:18:46 +0000105 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
106 /// operand.
107 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
108 SmallVectorImpl<MCFixup> &Fixups) const;
109
110
Jim Grosbach54fea632010-11-09 17:20:53 +0000111 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
112 /// operand as needed by load/store instructions.
113 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
114 SmallVectorImpl<MCFixup> &Fixups) const;
115
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000116 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
117 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
118 SmallVectorImpl<MCFixup> &Fixups) const {
119 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
120 switch (Mode) {
121 default: assert(0 && "Unknown addressing sub-mode!");
122 case ARM_AM::da: return 0;
123 case ARM_AM::ia: return 1;
124 case ARM_AM::db: return 2;
125 case ARM_AM::ib: return 3;
126 }
127 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000128 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
129 ///
130 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
131 switch (ShOpc) {
132 default: llvm_unreachable("Unknown shift opc!");
133 case ARM_AM::no_shift:
134 case ARM_AM::lsl: return 0;
135 case ARM_AM::lsr: return 1;
136 case ARM_AM::asr: return 2;
137 case ARM_AM::ror:
138 case ARM_AM::rrx: return 3;
139 }
140 return 0;
141 }
142
143 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
144 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
145 SmallVectorImpl<MCFixup> &Fixups) const;
146
147 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
148 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
149 SmallVectorImpl<MCFixup> &Fixups) const;
150
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000151 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
152 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
153 SmallVectorImpl<MCFixup> &Fixups) const;
154
Jim Grosbach570a9222010-11-11 01:09:40 +0000155 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
156 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
157 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000158
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000159 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
160 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
161 SmallVectorImpl<MCFixup> &Fixups) const;
162
Bill Wendling1fd374e2010-11-30 22:57:21 +0000163 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
164 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
165 SmallVectorImpl<MCFixup> &Fixups) const;
166
167 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
168 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
169 SmallVectorImpl<MCFixup> &Fixups) const;
170
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000171 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000172 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
173 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000174
Jim Grosbach08bd5492010-10-12 23:00:24 +0000175 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000176 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
177 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000178 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
179 // '1' respectively.
180 return MI.getOperand(Op).getReg() == ARM::CPSR;
181 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000182
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000183 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000184 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
185 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000186 unsigned SoImm = MI.getOperand(Op).getImm();
187 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
188 assert(SoImmVal != -1 && "Not a valid so_imm value!");
189
190 // Encode rotate_imm.
191 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
192 << ARMII::SoRotImmShift;
193
194 // Encode immed_8.
195 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
196 return Binary;
197 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000198
199 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
200 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
201 SmallVectorImpl<MCFixup> &Fixups) const {
202 unsigned SoImm = MI.getOperand(Op).getImm();
203 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
204 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
205 return Encoded;
206 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000207
Owen Anderson75579f72010-11-29 22:44:32 +0000208 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
209 SmallVectorImpl<MCFixup> &Fixups) const;
210 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
211 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000212 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
213 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000214 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
215 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000216
Jim Grosbachef324d72010-10-12 23:53:58 +0000217 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000218 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
219 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000220 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
221 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000222
Jim Grosbach806e80e2010-11-03 23:52:49 +0000223 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
224 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000225 switch (MI.getOperand(Op).getImm()) {
226 default: assert (0 && "Not a valid rot_imm value!");
227 case 0: return 0;
228 case 8: return 1;
229 case 16: return 2;
230 case 24: return 3;
231 }
232 }
233
Jim Grosbach806e80e2010-11-03 23:52:49 +0000234 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
235 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000236 return MI.getOperand(Op).getImm() - 1;
237 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000238
Jim Grosbach806e80e2010-11-03 23:52:49 +0000239 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
240 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000241 return 64 - MI.getOperand(Op).getImm();
242 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000243
Jim Grosbach806e80e2010-11-03 23:52:49 +0000244 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
245 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000246
Jim Grosbach806e80e2010-11-03 23:52:49 +0000247 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
248 SmallVectorImpl<MCFixup> &Fixups) const;
249 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
250 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000251 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
252 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000253 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
254 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000255
Owen Andersonc7139a62010-11-11 19:07:48 +0000256 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
257 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000258 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000259 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000260 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000261 unsigned EncodedValue) const;
262
263 unsigned VFPThumb2PostEncoder(const MCInst &MI,
264 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000265
Jim Grosbach70933262010-11-04 01:12:30 +0000266 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000267 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000268 }
269
Jim Grosbach70933262010-11-04 01:12:30 +0000270 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000271 // Output the constant in little endian byte order.
272 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000273 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000274 Val >>= 8;
275 }
276 }
277
Jim Grosbach568eeed2010-09-17 18:46:17 +0000278 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
279 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000280};
281
282} // end anonymous namespace
283
Bill Wendling0800ce72010-11-02 22:53:11 +0000284MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
285 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000286 return new ARMMCCodeEmitter(TM, Ctx);
287}
288
Owen Anderson57dac882010-11-11 21:36:43 +0000289/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000290/// instructions, and rewrite them to their Thumb2 form if we are currently in
291/// Thumb2 mode.
292unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
293 unsigned EncodedValue) const {
294 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
295 if (Subtarget.isThumb2()) {
296 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
297 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
298 // set to 1111.
299 unsigned Bit24 = EncodedValue & 0x01000000;
300 unsigned Bit28 = Bit24 << 4;
301 EncodedValue &= 0xEFFFFFFF;
302 EncodedValue |= Bit28;
303 EncodedValue |= 0x0F000000;
304 }
305
306 return EncodedValue;
307}
308
Owen Anderson57dac882010-11-11 21:36:43 +0000309/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
310/// instructions, and rewrite them to their Thumb2 form if we are currently in
311/// Thumb2 mode.
312unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
313 unsigned EncodedValue) const {
314 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
315 if (Subtarget.isThumb2()) {
316 EncodedValue &= 0xF0FFFFFF;
317 EncodedValue |= 0x09000000;
318 }
319
320 return EncodedValue;
321}
322
Owen Anderson8f143912010-11-11 23:12:55 +0000323/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
324/// instructions, and rewrite them to their Thumb2 form if we are currently in
325/// Thumb2 mode.
326unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
327 unsigned EncodedValue) const {
328 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
329 if (Subtarget.isThumb2()) {
330 EncodedValue &= 0x00FFFFFF;
331 EncodedValue |= 0xEE000000;
332 }
333
334 return EncodedValue;
335}
336
Bill Wendlingcf590262010-12-01 21:54:50 +0000337/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
338/// them to their Thumb2 form if we are currently in Thumb2 mode.
339unsigned ARMMCCodeEmitter::
340VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
341 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
342 EncodedValue &= 0x0FFFFFFF;
343 EncodedValue |= 0xE0000000;
344 }
345 return EncodedValue;
346}
Owen Anderson57dac882010-11-11 21:36:43 +0000347
Jim Grosbach56ac9072010-10-08 21:45:55 +0000348/// getMachineOpValue - Return binary encoding of operand. If the machine
349/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000350unsigned ARMMCCodeEmitter::
351getMachineOpValue(const MCInst &MI, const MCOperand &MO,
352 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000353 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000354 unsigned Reg = MO.getReg();
355 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000356
Jim Grosbachb0708d22010-11-30 23:51:41 +0000357 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000358 switch (Reg) {
359 default:
360 return RegNo;
361 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
362 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
363 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
364 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
365 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000366 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000367 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000368 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000369 } else if (MO.isFPImm()) {
370 return static_cast<unsigned>(APFloat(MO.getFPImm())
371 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000372 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000373
Jim Grosbach817c1a62010-11-19 00:27:09 +0000374 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000375 return 0;
376}
377
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000378/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000379bool ARMMCCodeEmitter::
380EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
381 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000382 const MCOperand &MO = MI.getOperand(OpIdx);
383 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000384
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000385 Reg = getARMRegisterNumbering(MO.getReg());
386
387 int32_t SImm = MO1.getImm();
388 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000389
Jim Grosbachab682a22010-10-28 18:34:10 +0000390 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000391 if (SImm == INT32_MIN)
392 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000393
Jim Grosbachab682a22010-10-28 18:34:10 +0000394 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000395 if (SImm < 0) {
396 SImm = -SImm;
397 isAdd = false;
398 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000399
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000400 Imm = SImm;
401 return isAdd;
402}
403
Jim Grosbach662a8162010-12-06 23:57:07 +0000404/// getThumbBLTargetOpValue - Return encoding info for immediate
405/// branch target.
406uint32_t ARMMCCodeEmitter::
407getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
408 SmallVectorImpl<MCFixup> &Fixups) const {
409 const MCOperand &MO = MI.getOperand(OpIdx);
410
411 // If the destination is an immediate, we have nothing to do.
412 if (MO.isImm()) return MO.getImm();
413 assert (MO.isExpr() && "Unexpected branch target type!");
414 const MCExpr *Expr = MO.getExpr();
415 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_thumb_bl);
416 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
417
418 // All of the information is in the fixup.
419 return 0;
420}
421
Jim Grosbachc466b932010-11-11 18:04:49 +0000422/// getBranchTargetOpValue - Return encoding info for 24-bit immediate
423/// branch target.
424uint32_t ARMMCCodeEmitter::
425getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
426 SmallVectorImpl<MCFixup> &Fixups) const {
427 const MCOperand &MO = MI.getOperand(OpIdx);
428
429 // If the destination is an immediate, we have nothing to do.
430 if (MO.isImm()) return MO.getImm();
431 assert (MO.isExpr() && "Unexpected branch target type!");
432 const MCExpr *Expr = MO.getExpr();
433 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
434 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
435
436 // All of the information is in the fixup.
437 return 0;
438}
439
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000440/// getAdrLabelOpValue - Return encoding info for 12-bit immediate
441/// ADR label target.
442uint32_t ARMMCCodeEmitter::
443getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
444 SmallVectorImpl<MCFixup> &Fixups) const {
445 const MCOperand &MO = MI.getOperand(OpIdx);
Jim Grosbachdff84b02010-12-02 00:28:45 +0000446 assert (MO.isExpr() && "Unexpected adr target type!");
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000447 const MCExpr *Expr = MO.getExpr();
Jim Grosbachdff84b02010-12-02 00:28:45 +0000448 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_adr_pcrel_12);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000449 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000450 // All of the information is in the fixup.
451 return 0;
452}
453
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000454/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000455uint32_t ARMMCCodeEmitter::
456getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
457 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000458 // {17-13} = reg
459 // {12} = (U)nsigned (add == '1', sub == '0')
460 // {11-0} = imm12
461 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000462 bool isAdd = true;
463 // If The first operand isn't a register, we have a label reference.
464 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000465 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
466 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000467 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000468 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000469 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000470
Owen Andersoneb6779c2010-12-07 00:45:21 +0000471 const MCExpr *Expr = 0;
472 if (!MO.isReg())
473 Expr = MO.getExpr();
474 else
475 Expr = MO2.getExpr();
476
Jim Grosbachdff84b02010-12-02 00:28:45 +0000477 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000478 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
479
480 ++MCNumCPRelocations;
481 } else
482 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000483
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000484 uint32_t Binary = Imm12 & 0xfff;
485 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000486 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000487 Binary |= (1 << 12);
488 Binary |= (Reg << 13);
489 return Binary;
490}
491
Owen Anderson9d63d902010-12-01 19:18:46 +0000492/// getT2AddrModeImm8s4OpValue - Return encoding info for
493/// 'reg +/- imm8<<2' operand.
494uint32_t ARMMCCodeEmitter::
495getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
496 SmallVectorImpl<MCFixup> &Fixups) const {
497 // {17-13} = reg
498 // {12} = (U)nsigned (add == '1', sub == '0')
499 // {11-0} = imm8
500 unsigned Reg, Imm8;
501 bool isAdd = true;
502 // If The first operand isn't a register, we have a label reference.
503 const MCOperand &MO = MI.getOperand(OpIdx);
504 if (!MO.isReg()) {
505 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
506 Imm8 = 0;
507 isAdd = false ; // 'U' bit is set as part of the fixup.
508
509 assert(MO.isExpr() && "Unexpected machine operand type!");
510 const MCExpr *Expr = MO.getExpr();
511 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
512 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
513
514 ++MCNumCPRelocations;
515 } else
516 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
517
518 uint32_t Binary = (Imm8 >> 2) & 0xff;
519 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
520 if (isAdd)
521 Binary |= (1 << 9);
522 Binary |= (Reg << 9);
523 return Binary;
524}
525
Jim Grosbach54fea632010-11-09 17:20:53 +0000526uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000527getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
528 SmallVectorImpl<MCFixup> &Fixups) const {
529 // {20-16} = imm{15-12}
530 // {11-0} = imm{11-0}
531 const MCOperand &MO = MI.getOperand(OpIdx);
532 if (MO.isImm()) {
533 return static_cast<unsigned>(MO.getImm());
534 } else if (const MCSymbolRefExpr *Expr =
535 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
536 MCFixupKind Kind;
537 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000538 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000539 case MCSymbolRefExpr::VK_ARM_HI16:
540 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
541 break;
542 case MCSymbolRefExpr::VK_ARM_LO16:
543 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
544 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000545 }
546 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
547 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000548 };
549 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000550 return 0;
551}
552
553uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000554getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
555 SmallVectorImpl<MCFixup> &Fixups) const {
556 const MCOperand &MO = MI.getOperand(OpIdx);
557 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
558 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
559 unsigned Rn = getARMRegisterNumbering(MO.getReg());
560 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000561 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
562 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000563 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
564 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000565
566 // {16-13} = Rn
567 // {12} = isAdd
568 // {11-0} = shifter
569 // {3-0} = Rm
570 // {4} = 0
571 // {6-5} = type
572 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000573 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000574 Binary |= Rn << 13;
575 Binary |= SBits << 5;
576 Binary |= ShImm << 7;
577 if (isAdd)
578 Binary |= 1 << 12;
579 return Binary;
580}
581
Jim Grosbach570a9222010-11-11 01:09:40 +0000582uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000583getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
584 SmallVectorImpl<MCFixup> &Fixups) const {
585 // {17-14} Rn
586 // {13} 1 == imm12, 0 == Rm
587 // {12} isAdd
588 // {11-0} imm12/Rm
589 const MCOperand &MO = MI.getOperand(OpIdx);
590 unsigned Rn = getARMRegisterNumbering(MO.getReg());
591 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
592 Binary |= Rn << 14;
593 return Binary;
594}
595
596uint32_t ARMMCCodeEmitter::
597getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
598 SmallVectorImpl<MCFixup> &Fixups) const {
599 // {13} 1 == imm12, 0 == Rm
600 // {12} isAdd
601 // {11-0} imm12/Rm
602 const MCOperand &MO = MI.getOperand(OpIdx);
603 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
604 unsigned Imm = MO1.getImm();
605 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
606 bool isReg = MO.getReg() != 0;
607 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
608 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
609 if (isReg) {
610 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
611 Binary <<= 7; // Shift amount is bits [11:7]
612 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
613 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
614 }
615 return Binary | (isAdd << 12) | (isReg << 13);
616}
617
618uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000619getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
620 SmallVectorImpl<MCFixup> &Fixups) const {
621 // {9} 1 == imm8, 0 == Rm
622 // {8} isAdd
623 // {7-4} imm7_4/zero
624 // {3-0} imm3_0/Rm
625 const MCOperand &MO = MI.getOperand(OpIdx);
626 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
627 unsigned Imm = MO1.getImm();
628 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
629 bool isImm = MO.getReg() == 0;
630 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
631 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
632 if (!isImm)
633 Imm8 = getARMRegisterNumbering(MO.getReg());
634 return Imm8 | (isAdd << 8) | (isImm << 9);
635}
636
637uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000638getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
639 SmallVectorImpl<MCFixup> &Fixups) const {
640 // {13} 1 == imm8, 0 == Rm
641 // {12-9} Rn
642 // {8} isAdd
643 // {7-4} imm7_4/zero
644 // {3-0} imm3_0/Rm
645 const MCOperand &MO = MI.getOperand(OpIdx);
646 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
647 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
648 unsigned Rn = getARMRegisterNumbering(MO.getReg());
649 unsigned Imm = MO2.getImm();
650 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
651 bool isImm = MO1.getReg() == 0;
652 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
653 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
654 if (!isImm)
655 Imm8 = getARMRegisterNumbering(MO1.getReg());
656 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
657}
658
Bill Wendling1fd374e2010-11-30 22:57:21 +0000659/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
660static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
661 unsigned Scale) {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000662 // [Rn, Rm]
663 // {5-3} = Rm
664 // {2-0} = Rn
665 //
666 // [Rn, #imm]
667 // {7-3} = imm5
668 // {2-0} = Rn
669 const MCOperand &MO = MI.getOperand(OpIdx);
670 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
671 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
672 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling1fd374e2010-11-30 22:57:21 +0000673 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
Bill Wendling0bdf0c02010-12-03 00:53:22 +0000674
675 if (MO2.getReg() != 0)
676 // Is an immediate.
677 Imm5 = getARMRegisterNumbering(MO2.getReg());
678
679 return (Imm5 << 3) | Rn;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000680}
681
Bill Wendling1fd374e2010-11-30 22:57:21 +0000682/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
683uint32_t ARMMCCodeEmitter::
684getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
685 SmallVectorImpl<MCFixup> &) const {
686 return getAddrModeSOpValue(MI, OpIdx, 4);
687}
688
689/// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
690uint32_t ARMMCCodeEmitter::
691getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
692 SmallVectorImpl<MCFixup> &) const {
693 return getAddrModeSOpValue(MI, OpIdx, 2);
694}
695
696/// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
697uint32_t ARMMCCodeEmitter::
698getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
699 SmallVectorImpl<MCFixup> &) const {
700 return getAddrModeSOpValue(MI, OpIdx, 1);
701}
702
Jim Grosbach5177f792010-12-01 21:09:40 +0000703/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000704uint32_t ARMMCCodeEmitter::
705getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
706 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000707 // {12-9} = reg
708 // {8} = (U)nsigned (add == '1', sub == '0')
709 // {7-0} = imm8
710 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000711 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000712 // If The first operand isn't a register, we have a label reference.
713 const MCOperand &MO = MI.getOperand(OpIdx);
714 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000715 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000716 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000717 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000718
719 assert(MO.isExpr() && "Unexpected machine operand type!");
720 const MCExpr *Expr = MO.getExpr();
Jim Grosbach5177f792010-12-01 21:09:40 +0000721 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000722 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
723
724 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000725 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000726 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000727 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
728 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000729
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000730 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
731 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000732 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000733 Binary |= (1 << 8);
734 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000735 return Binary;
736}
737
Jim Grosbach806e80e2010-11-03 23:52:49 +0000738unsigned ARMMCCodeEmitter::
739getSORegOpValue(const MCInst &MI, unsigned OpIdx,
740 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000741 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
742 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
743 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000744 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000745 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000746 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000747 // {6-5} = type
748 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000749 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000750 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000751 // else (imm shift)
752 // {11-7} = imm
753
754 const MCOperand &MO = MI.getOperand(OpIdx);
755 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
756 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
757 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
758
759 // Encode Rm.
760 unsigned Binary = getARMRegisterNumbering(MO.getReg());
761
762 // Encode the shift opcode.
763 unsigned SBits = 0;
764 unsigned Rs = MO1.getReg();
765 if (Rs) {
766 // Set shift operand (bit[7:4]).
767 // LSL - 0001
768 // LSR - 0011
769 // ASR - 0101
770 // ROR - 0111
771 // RRX - 0110 and bit[11:8] clear.
772 switch (SOpc) {
773 default: llvm_unreachable("Unknown shift opc!");
774 case ARM_AM::lsl: SBits = 0x1; break;
775 case ARM_AM::lsr: SBits = 0x3; break;
776 case ARM_AM::asr: SBits = 0x5; break;
777 case ARM_AM::ror: SBits = 0x7; break;
778 case ARM_AM::rrx: SBits = 0x6; break;
779 }
780 } else {
781 // Set shift operand (bit[6:4]).
782 // LSL - 000
783 // LSR - 010
784 // ASR - 100
785 // ROR - 110
786 switch (SOpc) {
787 default: llvm_unreachable("Unknown shift opc!");
788 case ARM_AM::lsl: SBits = 0x0; break;
789 case ARM_AM::lsr: SBits = 0x2; break;
790 case ARM_AM::asr: SBits = 0x4; break;
791 case ARM_AM::ror: SBits = 0x6; break;
792 }
793 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000794
Jim Grosbachef324d72010-10-12 23:53:58 +0000795 Binary |= SBits << 4;
796 if (SOpc == ARM_AM::rrx)
797 return Binary;
798
799 // Encode the shift operation Rs or shift_imm (except rrx).
800 if (Rs) {
801 // Encode Rs bit[11:8].
802 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
803 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
804 }
805
806 // Encode shift_imm bit[11:7].
807 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
808}
809
Jim Grosbach806e80e2010-11-03 23:52:49 +0000810unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000811getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
812 SmallVectorImpl<MCFixup> &Fixups) const {
813 const MCOperand &MO1 = MI.getOperand(OpNum);
814 const MCOperand &MO2 = MI.getOperand(OpNum+1);
815 const MCOperand &MO3 = MI.getOperand(OpNum+2);
816
817 // Encoded as [Rn, Rm, imm].
818 // FIXME: Needs fixup support.
819 unsigned Value = getARMRegisterNumbering(MO1.getReg());
820 Value <<= 4;
821 Value |= getARMRegisterNumbering(MO2.getReg());
822 Value <<= 2;
823 Value |= MO3.getImm();
824
825 return Value;
826}
827
828unsigned ARMMCCodeEmitter::
829getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
830 SmallVectorImpl<MCFixup> &Fixups) const {
831 const MCOperand &MO1 = MI.getOperand(OpNum);
832 const MCOperand &MO2 = MI.getOperand(OpNum+1);
833
834 // FIXME: Needs fixup support.
835 unsigned Value = getARMRegisterNumbering(MO1.getReg());
836
837 // Even though the immediate is 8 bits long, we need 9 bits in order
838 // to represent the (inverse of the) sign bit.
839 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000840 int32_t tmp = (int32_t)MO2.getImm();
841 if (tmp < 0)
842 tmp = abs(tmp);
843 else
844 Value |= 256; // Set the ADD bit
845 Value |= tmp & 255;
846 return Value;
847}
848
849unsigned ARMMCCodeEmitter::
850getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
851 SmallVectorImpl<MCFixup> &Fixups) const {
852 const MCOperand &MO1 = MI.getOperand(OpNum);
853
854 // FIXME: Needs fixup support.
855 unsigned Value = 0;
856 int32_t tmp = (int32_t)MO1.getImm();
857 if (tmp < 0)
858 tmp = abs(tmp);
859 else
860 Value |= 256; // Set the ADD bit
861 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000862 return Value;
863}
864
865unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000866getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
867 SmallVectorImpl<MCFixup> &Fixups) const {
868 const MCOperand &MO1 = MI.getOperand(OpNum);
869
870 // FIXME: Needs fixup support.
871 unsigned Value = 0;
872 int32_t tmp = (int32_t)MO1.getImm();
873 if (tmp < 0)
874 tmp = abs(tmp);
875 else
876 Value |= 4096; // Set the ADD bit
877 Value |= tmp & 4095;
878 return Value;
879}
880
881unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000882getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
883 SmallVectorImpl<MCFixup> &Fixups) const {
884 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
885 // shifted. The second is the amount to shift by.
886 //
887 // {3-0} = Rm.
888 // {4} = 0
889 // {6-5} = type
890 // {11-7} = imm
891
892 const MCOperand &MO = MI.getOperand(OpIdx);
893 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
894 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
895
896 // Encode Rm.
897 unsigned Binary = getARMRegisterNumbering(MO.getReg());
898
899 // Encode the shift opcode.
900 unsigned SBits = 0;
901 // Set shift operand (bit[6:4]).
902 // LSL - 000
903 // LSR - 010
904 // ASR - 100
905 // ROR - 110
906 switch (SOpc) {
907 default: llvm_unreachable("Unknown shift opc!");
908 case ARM_AM::lsl: SBits = 0x0; break;
909 case ARM_AM::lsr: SBits = 0x2; break;
910 case ARM_AM::asr: SBits = 0x4; break;
911 case ARM_AM::ror: SBits = 0x6; break;
912 }
913
914 Binary |= SBits << 4;
915 if (SOpc == ARM_AM::rrx)
916 return Binary;
917
918 // Encode shift_imm bit[11:7].
919 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
920}
921
922unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000923getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
924 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000925 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
926 // msb of the mask.
927 const MCOperand &MO = MI.getOperand(Op);
928 uint32_t v = ~MO.getImm();
929 uint32_t lsb = CountTrailingZeros_32(v);
930 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
931 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
932 return lsb | (msb << 5);
933}
934
Jim Grosbach806e80e2010-11-03 23:52:49 +0000935unsigned ARMMCCodeEmitter::
936getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000937 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +0000938 // VLDM/VSTM:
939 // {12-8} = Vd
940 // {7-0} = Number of registers
941 //
942 // LDM/STM:
943 // {15-0} = Bitfield of GPRs.
944 unsigned Reg = MI.getOperand(Op).getReg();
945 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
946 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
947
Bill Wendling5e559a22010-11-09 00:30:18 +0000948 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +0000949
950 if (SPRRegs || DPRRegs) {
951 // VLDM/VSTM
952 unsigned RegNo = getARMRegisterNumbering(Reg);
953 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
954 Binary |= (RegNo & 0x1f) << 8;
955 if (SPRRegs)
956 Binary |= NumRegs;
957 else
958 Binary |= NumRegs * 2;
959 } else {
960 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
961 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
962 Binary |= 1 << RegNo;
963 }
Bill Wendling5e559a22010-11-09 00:30:18 +0000964 }
Bill Wendling6bc105a2010-11-17 00:45:23 +0000965
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000966 return Binary;
967}
968
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000969/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
970/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000971unsigned ARMMCCodeEmitter::
972getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
973 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000974 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000975 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +0000976
Owen Andersond9aa7d32010-11-02 00:05:05 +0000977 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000978 unsigned Align = 0;
979
980 switch (Imm.getImm()) {
981 default: break;
982 case 2:
983 case 4:
984 case 8: Align = 0x01; break;
985 case 16: Align = 0x02; break;
986 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000987 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000988
Owen Andersond9aa7d32010-11-02 00:05:05 +0000989 return RegNo | (Align << 4);
990}
991
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000992/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
993/// alignment operand for use in VLD-dup instructions. This is the same as
994/// getAddrMode6AddressOpValue except for the alignment encoding, which is
995/// different for VLD4-dup.
996unsigned ARMMCCodeEmitter::
997getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
998 SmallVectorImpl<MCFixup> &Fixups) const {
999 const MCOperand &Reg = MI.getOperand(Op);
1000 const MCOperand &Imm = MI.getOperand(Op + 1);
1001
1002 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1003 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 = 0x03; break;
1011 }
1012
1013 return RegNo | (Align << 4);
1014}
1015
Jim Grosbach806e80e2010-11-03 23:52:49 +00001016unsigned ARMMCCodeEmitter::
1017getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1018 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001019 const MCOperand &MO = MI.getOperand(Op);
1020 if (MO.getReg() == 0) return 0x0D;
1021 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001022}
1023
Jim Grosbach568eeed2010-09-17 18:46:17 +00001024void ARMMCCodeEmitter::
1025EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001026 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001027 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001028 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001029 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001030 uint64_t TSFlags = Desc.TSFlags;
1031 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001032 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001033 int Size;
1034 // Basic size info comes from the TSFlags field.
1035 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1036 default: llvm_unreachable("Unexpected instruction size!");
1037 case ARMII::Size2Bytes: Size = 2; break;
1038 case ARMII::Size4Bytes: Size = 4; break;
1039 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001040 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1041 // Thumb 32-bit wide instructions need to be have the high order halfword
1042 // emitted first.
1043 if (Subtarget.isThumb() && Size == 4) {
1044 EmitConstant(Binary >> 16, 2, OS);
1045 EmitConstant(Binary & 0xffff, 2, OS);
1046 } else
1047 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001048 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001049}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001050
Jim Grosbach806e80e2010-11-03 23:52:49 +00001051#include "ARMGenMCCodeEmitter.inc"