blob: 4b059197ff05ca622fbb81aa0b3b0c0a9960d63a [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 },
53 { "fixup_arm_movt_hi16", 0, 16, 0 },
54 { "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000055 };
56
57 if (Kind < FirstTargetFixupKind)
58 return MCCodeEmitter::getFixupKindInfo(Kind);
59
60 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
61 "Invalid kind!");
62 return Infos[Kind - FirstTargetFixupKind];
63 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000064 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
65
Jim Grosbach9af82ba2010-10-07 21:57:55 +000066 // getBinaryCodeForInstr - TableGen'erated function for getting the
67 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000068 unsigned getBinaryCodeForInstr(const MCInst &MI,
69 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000070
71 /// getMachineOpValue - Return binary encoding of operand. If the machine
72 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000073 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
74 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000075
Jason W Kim837caa92010-11-18 23:37:15 +000076 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
77 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
78 SmallVectorImpl<MCFixup> &Fixups) const;
79
Bill Wendling92b5a2e2010-11-03 01:49:29 +000080 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000081 unsigned &Reg, unsigned &Imm,
82 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000083
Jim Grosbachc466b932010-11-11 18:04:49 +000084 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
85 /// branch target.
86 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
87 SmallVectorImpl<MCFixup> &Fixups) const;
88
Jim Grosbach5d14f9b2010-12-01 19:47:31 +000089 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
90 /// ADR label target.
91 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
92 SmallVectorImpl<MCFixup> &Fixups) const;
93
Bill Wendling92b5a2e2010-11-03 01:49:29 +000094 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
95 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +000096 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
97 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000098
Owen Anderson9d63d902010-12-01 19:18:46 +000099 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
100 /// operand.
101 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
102 SmallVectorImpl<MCFixup> &Fixups) const;
103
104
Jim Grosbach54fea632010-11-09 17:20:53 +0000105 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
106 /// operand as needed by load/store instructions.
107 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
108 SmallVectorImpl<MCFixup> &Fixups) const;
109
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000110 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
111 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
112 SmallVectorImpl<MCFixup> &Fixups) const {
113 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
114 switch (Mode) {
115 default: assert(0 && "Unknown addressing sub-mode!");
116 case ARM_AM::da: return 0;
117 case ARM_AM::ia: return 1;
118 case ARM_AM::db: return 2;
119 case ARM_AM::ib: return 3;
120 }
121 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000122 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
123 ///
124 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
125 switch (ShOpc) {
126 default: llvm_unreachable("Unknown shift opc!");
127 case ARM_AM::no_shift:
128 case ARM_AM::lsl: return 0;
129 case ARM_AM::lsr: return 1;
130 case ARM_AM::asr: return 2;
131 case ARM_AM::ror:
132 case ARM_AM::rrx: return 3;
133 }
134 return 0;
135 }
136
137 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
138 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
139 SmallVectorImpl<MCFixup> &Fixups) const;
140
141 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
142 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
143 SmallVectorImpl<MCFixup> &Fixups) const;
144
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000145 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
146 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
147 SmallVectorImpl<MCFixup> &Fixups) const;
148
Jim Grosbach570a9222010-11-11 01:09:40 +0000149 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
150 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
151 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000152
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000153 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
154 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
155 SmallVectorImpl<MCFixup> &Fixups) const;
156
Bill Wendling1fd374e2010-11-30 22:57:21 +0000157 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
158 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
159 SmallVectorImpl<MCFixup> &Fixups) const;
160
161 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
162 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
163 SmallVectorImpl<MCFixup> &Fixups) const;
164
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000165 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000166 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
167 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000168
Jim Grosbach08bd5492010-10-12 23:00:24 +0000169 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000170 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
171 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000172 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
173 // '1' respectively.
174 return MI.getOperand(Op).getReg() == ARM::CPSR;
175 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000176
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000177 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000178 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
179 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000180 unsigned SoImm = MI.getOperand(Op).getImm();
181 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
182 assert(SoImmVal != -1 && "Not a valid so_imm value!");
183
184 // Encode rotate_imm.
185 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
186 << ARMII::SoRotImmShift;
187
188 // Encode immed_8.
189 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
190 return Binary;
191 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000192
193 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
194 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
195 SmallVectorImpl<MCFixup> &Fixups) const {
196 unsigned SoImm = MI.getOperand(Op).getImm();
197 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
198 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
199 return Encoded;
200 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000201
Owen Anderson75579f72010-11-29 22:44:32 +0000202 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
203 SmallVectorImpl<MCFixup> &Fixups) const;
204 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
205 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000206 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
207 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000208 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
209 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000210
Jim Grosbachef324d72010-10-12 23:53:58 +0000211 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000212 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
213 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000214 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
215 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000216
Jim Grosbach806e80e2010-11-03 23:52:49 +0000217 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
218 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000219 switch (MI.getOperand(Op).getImm()) {
220 default: assert (0 && "Not a valid rot_imm value!");
221 case 0: return 0;
222 case 8: return 1;
223 case 16: return 2;
224 case 24: return 3;
225 }
226 }
227
Jim Grosbach806e80e2010-11-03 23:52:49 +0000228 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
229 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000230 return MI.getOperand(Op).getImm() - 1;
231 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000232
Jim Grosbach806e80e2010-11-03 23:52:49 +0000233 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
234 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000235 return 64 - MI.getOperand(Op).getImm();
236 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000237
Jim Grosbach806e80e2010-11-03 23:52:49 +0000238 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
239 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000240
Jim Grosbach806e80e2010-11-03 23:52:49 +0000241 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
242 SmallVectorImpl<MCFixup> &Fixups) const;
243 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
244 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000245 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
246 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000247 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
248 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000249
Owen Andersonc7139a62010-11-11 19:07:48 +0000250 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
251 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000252 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000253 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000254 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000255 unsigned EncodedValue) const;
256
257 unsigned VFPThumb2PostEncoder(const MCInst &MI,
258 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000259
Jim Grosbach70933262010-11-04 01:12:30 +0000260 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000261 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000262 }
263
Jim Grosbach70933262010-11-04 01:12:30 +0000264 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000265 // Output the constant in little endian byte order.
266 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000267 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000268 Val >>= 8;
269 }
270 }
271
Jim Grosbach568eeed2010-09-17 18:46:17 +0000272 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
273 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000274};
275
276} // end anonymous namespace
277
Bill Wendling0800ce72010-11-02 22:53:11 +0000278MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
279 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000280 return new ARMMCCodeEmitter(TM, Ctx);
281}
282
Owen Anderson57dac882010-11-11 21:36:43 +0000283/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000284/// instructions, and rewrite them to their Thumb2 form if we are currently in
285/// Thumb2 mode.
286unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
287 unsigned EncodedValue) const {
288 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
289 if (Subtarget.isThumb2()) {
290 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
291 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
292 // set to 1111.
293 unsigned Bit24 = EncodedValue & 0x01000000;
294 unsigned Bit28 = Bit24 << 4;
295 EncodedValue &= 0xEFFFFFFF;
296 EncodedValue |= Bit28;
297 EncodedValue |= 0x0F000000;
298 }
299
300 return EncodedValue;
301}
302
Owen Anderson57dac882010-11-11 21:36:43 +0000303/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
304/// instructions, and rewrite them to their Thumb2 form if we are currently in
305/// Thumb2 mode.
306unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
307 unsigned EncodedValue) const {
308 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
309 if (Subtarget.isThumb2()) {
310 EncodedValue &= 0xF0FFFFFF;
311 EncodedValue |= 0x09000000;
312 }
313
314 return EncodedValue;
315}
316
Owen Anderson8f143912010-11-11 23:12:55 +0000317/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
318/// instructions, and rewrite them to their Thumb2 form if we are currently in
319/// Thumb2 mode.
320unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
321 unsigned EncodedValue) const {
322 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
323 if (Subtarget.isThumb2()) {
324 EncodedValue &= 0x00FFFFFF;
325 EncodedValue |= 0xEE000000;
326 }
327
328 return EncodedValue;
329}
330
Bill Wendlingcf590262010-12-01 21:54:50 +0000331/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
332/// them to their Thumb2 form if we are currently in Thumb2 mode.
333unsigned ARMMCCodeEmitter::
334VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
335 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
336 EncodedValue &= 0x0FFFFFFF;
337 EncodedValue |= 0xE0000000;
338 }
339 return EncodedValue;
340}
Owen Anderson57dac882010-11-11 21:36:43 +0000341
Jim Grosbach56ac9072010-10-08 21:45:55 +0000342/// getMachineOpValue - Return binary encoding of operand. If the machine
343/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000344unsigned ARMMCCodeEmitter::
345getMachineOpValue(const MCInst &MI, const MCOperand &MO,
346 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000347 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000348 unsigned Reg = MO.getReg();
349 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000350
Jim Grosbachb0708d22010-11-30 23:51:41 +0000351 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000352 switch (Reg) {
353 default:
354 return RegNo;
355 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
356 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
357 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
358 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
359 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000360 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000361 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000362 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000363 } else if (MO.isFPImm()) {
364 return static_cast<unsigned>(APFloat(MO.getFPImm())
365 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000366 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000367
Jim Grosbach817c1a62010-11-19 00:27:09 +0000368 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000369 return 0;
370}
371
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000372/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000373bool ARMMCCodeEmitter::
374EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
375 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000376 const MCOperand &MO = MI.getOperand(OpIdx);
377 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000378
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000379 Reg = getARMRegisterNumbering(MO.getReg());
380
381 int32_t SImm = MO1.getImm();
382 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000383
Jim Grosbachab682a22010-10-28 18:34:10 +0000384 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000385 if (SImm == INT32_MIN)
386 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000387
Jim Grosbachab682a22010-10-28 18:34:10 +0000388 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000389 if (SImm < 0) {
390 SImm = -SImm;
391 isAdd = false;
392 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000393
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000394 Imm = SImm;
395 return isAdd;
396}
397
Jim Grosbachc466b932010-11-11 18:04:49 +0000398/// getBranchTargetOpValue - Return encoding info for 24-bit immediate
399/// branch target.
400uint32_t ARMMCCodeEmitter::
401getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
402 SmallVectorImpl<MCFixup> &Fixups) const {
403 const MCOperand &MO = MI.getOperand(OpIdx);
404
405 // If the destination is an immediate, we have nothing to do.
406 if (MO.isImm()) return MO.getImm();
407 assert (MO.isExpr() && "Unexpected branch target type!");
408 const MCExpr *Expr = MO.getExpr();
409 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
410 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
411
412 // All of the information is in the fixup.
413 return 0;
414}
415
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000416/// getAdrLabelOpValue - Return encoding info for 12-bit immediate
417/// ADR label target.
418uint32_t ARMMCCodeEmitter::
419getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
420 SmallVectorImpl<MCFixup> &Fixups) const {
421 const MCOperand &MO = MI.getOperand(OpIdx);
Jim Grosbachdff84b02010-12-02 00:28:45 +0000422 assert (MO.isExpr() && "Unexpected adr target type!");
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000423 const MCExpr *Expr = MO.getExpr();
Jim Grosbachdff84b02010-12-02 00:28:45 +0000424 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_adr_pcrel_12);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000425 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000426 // All of the information is in the fixup.
427 return 0;
428}
429
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000430/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000431uint32_t ARMMCCodeEmitter::
432getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
433 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000434 // {17-13} = reg
435 // {12} = (U)nsigned (add == '1', sub == '0')
436 // {11-0} = imm12
437 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000438 bool isAdd = true;
439 // If The first operand isn't a register, we have a label reference.
440 const MCOperand &MO = MI.getOperand(OpIdx);
441 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000442 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000443 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000444 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000445
446 assert(MO.isExpr() && "Unexpected machine operand type!");
447 const MCExpr *Expr = MO.getExpr();
Jim Grosbachdff84b02010-12-02 00:28:45 +0000448 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000449 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
450
451 ++MCNumCPRelocations;
452 } else
453 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000454
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000455 uint32_t Binary = Imm12 & 0xfff;
456 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000457 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000458 Binary |= (1 << 12);
459 Binary |= (Reg << 13);
460 return Binary;
461}
462
Owen Anderson9d63d902010-12-01 19:18:46 +0000463/// getT2AddrModeImm8s4OpValue - Return encoding info for
464/// 'reg +/- imm8<<2' operand.
465uint32_t ARMMCCodeEmitter::
466getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
467 SmallVectorImpl<MCFixup> &Fixups) const {
468 // {17-13} = reg
469 // {12} = (U)nsigned (add == '1', sub == '0')
470 // {11-0} = imm8
471 unsigned Reg, Imm8;
472 bool isAdd = true;
473 // If The first operand isn't a register, we have a label reference.
474 const MCOperand &MO = MI.getOperand(OpIdx);
475 if (!MO.isReg()) {
476 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
477 Imm8 = 0;
478 isAdd = false ; // 'U' bit is set as part of the fixup.
479
480 assert(MO.isExpr() && "Unexpected machine operand type!");
481 const MCExpr *Expr = MO.getExpr();
482 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
483 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
484
485 ++MCNumCPRelocations;
486 } else
487 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
488
489 uint32_t Binary = (Imm8 >> 2) & 0xff;
490 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
491 if (isAdd)
492 Binary |= (1 << 9);
493 Binary |= (Reg << 9);
494 return Binary;
495}
496
Jim Grosbach54fea632010-11-09 17:20:53 +0000497uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000498getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
499 SmallVectorImpl<MCFixup> &Fixups) const {
500 // {20-16} = imm{15-12}
501 // {11-0} = imm{11-0}
502 const MCOperand &MO = MI.getOperand(OpIdx);
503 if (MO.isImm()) {
504 return static_cast<unsigned>(MO.getImm());
505 } else if (const MCSymbolRefExpr *Expr =
506 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
507 MCFixupKind Kind;
508 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000509 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000510 case MCSymbolRefExpr::VK_ARM_HI16:
511 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
512 break;
513 case MCSymbolRefExpr::VK_ARM_LO16:
514 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
515 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000516 }
517 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
518 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000519 };
520 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000521 return 0;
522}
523
524uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000525getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
526 SmallVectorImpl<MCFixup> &Fixups) const {
527 const MCOperand &MO = MI.getOperand(OpIdx);
528 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
529 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
530 unsigned Rn = getARMRegisterNumbering(MO.getReg());
531 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000532 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
533 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000534 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
535 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000536
537 // {16-13} = Rn
538 // {12} = isAdd
539 // {11-0} = shifter
540 // {3-0} = Rm
541 // {4} = 0
542 // {6-5} = type
543 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000544 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000545 Binary |= Rn << 13;
546 Binary |= SBits << 5;
547 Binary |= ShImm << 7;
548 if (isAdd)
549 Binary |= 1 << 12;
550 return Binary;
551}
552
Jim Grosbach570a9222010-11-11 01:09:40 +0000553uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000554getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
555 SmallVectorImpl<MCFixup> &Fixups) const {
556 // {17-14} Rn
557 // {13} 1 == imm12, 0 == Rm
558 // {12} isAdd
559 // {11-0} imm12/Rm
560 const MCOperand &MO = MI.getOperand(OpIdx);
561 unsigned Rn = getARMRegisterNumbering(MO.getReg());
562 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
563 Binary |= Rn << 14;
564 return Binary;
565}
566
567uint32_t ARMMCCodeEmitter::
568getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
569 SmallVectorImpl<MCFixup> &Fixups) const {
570 // {13} 1 == imm12, 0 == Rm
571 // {12} isAdd
572 // {11-0} imm12/Rm
573 const MCOperand &MO = MI.getOperand(OpIdx);
574 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
575 unsigned Imm = MO1.getImm();
576 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
577 bool isReg = MO.getReg() != 0;
578 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
579 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
580 if (isReg) {
581 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
582 Binary <<= 7; // Shift amount is bits [11:7]
583 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
584 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
585 }
586 return Binary | (isAdd << 12) | (isReg << 13);
587}
588
589uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000590getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
591 SmallVectorImpl<MCFixup> &Fixups) const {
592 // {9} 1 == imm8, 0 == Rm
593 // {8} isAdd
594 // {7-4} imm7_4/zero
595 // {3-0} imm3_0/Rm
596 const MCOperand &MO = MI.getOperand(OpIdx);
597 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
598 unsigned Imm = MO1.getImm();
599 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
600 bool isImm = MO.getReg() == 0;
601 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
602 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
603 if (!isImm)
604 Imm8 = getARMRegisterNumbering(MO.getReg());
605 return Imm8 | (isAdd << 8) | (isImm << 9);
606}
607
608uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000609getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
610 SmallVectorImpl<MCFixup> &Fixups) const {
611 // {13} 1 == imm8, 0 == Rm
612 // {12-9} Rn
613 // {8} isAdd
614 // {7-4} imm7_4/zero
615 // {3-0} imm3_0/Rm
616 const MCOperand &MO = MI.getOperand(OpIdx);
617 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
618 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
619 unsigned Rn = getARMRegisterNumbering(MO.getReg());
620 unsigned Imm = MO2.getImm();
621 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
622 bool isImm = MO1.getReg() == 0;
623 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
624 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
625 if (!isImm)
626 Imm8 = getARMRegisterNumbering(MO1.getReg());
627 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
628}
629
Bill Wendling1fd374e2010-11-30 22:57:21 +0000630/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
631static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
632 unsigned Scale) {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000633 // [Rn, Rm]
634 // {5-3} = Rm
635 // {2-0} = Rn
636 //
637 // [Rn, #imm]
638 // {7-3} = imm5
639 // {2-0} = Rn
640 const MCOperand &MO = MI.getOperand(OpIdx);
641 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
642 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
643 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling1fd374e2010-11-30 22:57:21 +0000644 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000645 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
646 return (Rm << 3) | (Imm5 << 3) | Rn;
647}
648
Bill Wendling1fd374e2010-11-30 22:57:21 +0000649/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
650uint32_t ARMMCCodeEmitter::
651getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
652 SmallVectorImpl<MCFixup> &) const {
653 return getAddrModeSOpValue(MI, OpIdx, 4);
654}
655
656/// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
657uint32_t ARMMCCodeEmitter::
658getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
659 SmallVectorImpl<MCFixup> &) const {
660 return getAddrModeSOpValue(MI, OpIdx, 2);
661}
662
663/// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
664uint32_t ARMMCCodeEmitter::
665getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
666 SmallVectorImpl<MCFixup> &) const {
667 return getAddrModeSOpValue(MI, OpIdx, 1);
668}
669
Jim Grosbach5177f792010-12-01 21:09:40 +0000670/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000671uint32_t ARMMCCodeEmitter::
672getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
673 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000674 // {12-9} = reg
675 // {8} = (U)nsigned (add == '1', sub == '0')
676 // {7-0} = imm8
677 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000678 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000679 // If The first operand isn't a register, we have a label reference.
680 const MCOperand &MO = MI.getOperand(OpIdx);
681 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000682 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000683 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000684 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000685
686 assert(MO.isExpr() && "Unexpected machine operand type!");
687 const MCExpr *Expr = MO.getExpr();
Jim Grosbach5177f792010-12-01 21:09:40 +0000688 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000689 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
690
691 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000692 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000693 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000694 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
695 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000696
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000697 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
698 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000699 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000700 Binary |= (1 << 8);
701 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000702 return Binary;
703}
704
Jim Grosbach806e80e2010-11-03 23:52:49 +0000705unsigned ARMMCCodeEmitter::
706getSORegOpValue(const MCInst &MI, unsigned OpIdx,
707 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000708 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
709 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
710 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000711 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000712 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000713 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000714 // {6-5} = type
715 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000716 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000717 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000718 // else (imm shift)
719 // {11-7} = imm
720
721 const MCOperand &MO = MI.getOperand(OpIdx);
722 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
723 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
724 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
725
726 // Encode Rm.
727 unsigned Binary = getARMRegisterNumbering(MO.getReg());
728
729 // Encode the shift opcode.
730 unsigned SBits = 0;
731 unsigned Rs = MO1.getReg();
732 if (Rs) {
733 // Set shift operand (bit[7:4]).
734 // LSL - 0001
735 // LSR - 0011
736 // ASR - 0101
737 // ROR - 0111
738 // RRX - 0110 and bit[11:8] clear.
739 switch (SOpc) {
740 default: llvm_unreachable("Unknown shift opc!");
741 case ARM_AM::lsl: SBits = 0x1; break;
742 case ARM_AM::lsr: SBits = 0x3; break;
743 case ARM_AM::asr: SBits = 0x5; break;
744 case ARM_AM::ror: SBits = 0x7; break;
745 case ARM_AM::rrx: SBits = 0x6; break;
746 }
747 } else {
748 // Set shift operand (bit[6:4]).
749 // LSL - 000
750 // LSR - 010
751 // ASR - 100
752 // ROR - 110
753 switch (SOpc) {
754 default: llvm_unreachable("Unknown shift opc!");
755 case ARM_AM::lsl: SBits = 0x0; break;
756 case ARM_AM::lsr: SBits = 0x2; break;
757 case ARM_AM::asr: SBits = 0x4; break;
758 case ARM_AM::ror: SBits = 0x6; break;
759 }
760 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000761
Jim Grosbachef324d72010-10-12 23:53:58 +0000762 Binary |= SBits << 4;
763 if (SOpc == ARM_AM::rrx)
764 return Binary;
765
766 // Encode the shift operation Rs or shift_imm (except rrx).
767 if (Rs) {
768 // Encode Rs bit[11:8].
769 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
770 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
771 }
772
773 // Encode shift_imm bit[11:7].
774 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
775}
776
Jim Grosbach806e80e2010-11-03 23:52:49 +0000777unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000778getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
779 SmallVectorImpl<MCFixup> &Fixups) const {
780 const MCOperand &MO1 = MI.getOperand(OpNum);
781 const MCOperand &MO2 = MI.getOperand(OpNum+1);
782 const MCOperand &MO3 = MI.getOperand(OpNum+2);
783
784 // Encoded as [Rn, Rm, imm].
785 // FIXME: Needs fixup support.
786 unsigned Value = getARMRegisterNumbering(MO1.getReg());
787 Value <<= 4;
788 Value |= getARMRegisterNumbering(MO2.getReg());
789 Value <<= 2;
790 Value |= MO3.getImm();
791
792 return Value;
793}
794
795unsigned ARMMCCodeEmitter::
796getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
797 SmallVectorImpl<MCFixup> &Fixups) const {
798 const MCOperand &MO1 = MI.getOperand(OpNum);
799 const MCOperand &MO2 = MI.getOperand(OpNum+1);
800
801 // FIXME: Needs fixup support.
802 unsigned Value = getARMRegisterNumbering(MO1.getReg());
803
804 // Even though the immediate is 8 bits long, we need 9 bits in order
805 // to represent the (inverse of the) sign bit.
806 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000807 int32_t tmp = (int32_t)MO2.getImm();
808 if (tmp < 0)
809 tmp = abs(tmp);
810 else
811 Value |= 256; // Set the ADD bit
812 Value |= tmp & 255;
813 return Value;
814}
815
816unsigned ARMMCCodeEmitter::
817getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
818 SmallVectorImpl<MCFixup> &Fixups) const {
819 const MCOperand &MO1 = MI.getOperand(OpNum);
820
821 // FIXME: Needs fixup support.
822 unsigned Value = 0;
823 int32_t tmp = (int32_t)MO1.getImm();
824 if (tmp < 0)
825 tmp = abs(tmp);
826 else
827 Value |= 256; // Set the ADD bit
828 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000829 return Value;
830}
831
832unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000833getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
834 SmallVectorImpl<MCFixup> &Fixups) const {
835 const MCOperand &MO1 = MI.getOperand(OpNum);
836
837 // FIXME: Needs fixup support.
838 unsigned Value = 0;
839 int32_t tmp = (int32_t)MO1.getImm();
840 if (tmp < 0)
841 tmp = abs(tmp);
842 else
843 Value |= 4096; // Set the ADD bit
844 Value |= tmp & 4095;
845 return Value;
846}
847
848unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000849getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
850 SmallVectorImpl<MCFixup> &Fixups) const {
851 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
852 // shifted. The second is the amount to shift by.
853 //
854 // {3-0} = Rm.
855 // {4} = 0
856 // {6-5} = type
857 // {11-7} = imm
858
859 const MCOperand &MO = MI.getOperand(OpIdx);
860 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
861 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
862
863 // Encode Rm.
864 unsigned Binary = getARMRegisterNumbering(MO.getReg());
865
866 // Encode the shift opcode.
867 unsigned SBits = 0;
868 // Set shift operand (bit[6:4]).
869 // LSL - 000
870 // LSR - 010
871 // ASR - 100
872 // ROR - 110
873 switch (SOpc) {
874 default: llvm_unreachable("Unknown shift opc!");
875 case ARM_AM::lsl: SBits = 0x0; break;
876 case ARM_AM::lsr: SBits = 0x2; break;
877 case ARM_AM::asr: SBits = 0x4; break;
878 case ARM_AM::ror: SBits = 0x6; break;
879 }
880
881 Binary |= SBits << 4;
882 if (SOpc == ARM_AM::rrx)
883 return Binary;
884
885 // Encode shift_imm bit[11:7].
886 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
887}
888
889unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000890getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
891 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000892 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
893 // msb of the mask.
894 const MCOperand &MO = MI.getOperand(Op);
895 uint32_t v = ~MO.getImm();
896 uint32_t lsb = CountTrailingZeros_32(v);
897 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
898 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
899 return lsb | (msb << 5);
900}
901
Jim Grosbach806e80e2010-11-03 23:52:49 +0000902unsigned ARMMCCodeEmitter::
903getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000904 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +0000905 // VLDM/VSTM:
906 // {12-8} = Vd
907 // {7-0} = Number of registers
908 //
909 // LDM/STM:
910 // {15-0} = Bitfield of GPRs.
911 unsigned Reg = MI.getOperand(Op).getReg();
912 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
913 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
914
Bill Wendling5e559a22010-11-09 00:30:18 +0000915 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +0000916
917 if (SPRRegs || DPRRegs) {
918 // VLDM/VSTM
919 unsigned RegNo = getARMRegisterNumbering(Reg);
920 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
921 Binary |= (RegNo & 0x1f) << 8;
922 if (SPRRegs)
923 Binary |= NumRegs;
924 else
925 Binary |= NumRegs * 2;
926 } else {
927 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
928 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
929 Binary |= 1 << RegNo;
930 }
Bill Wendling5e559a22010-11-09 00:30:18 +0000931 }
Bill Wendling6bc105a2010-11-17 00:45:23 +0000932
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000933 return Binary;
934}
935
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000936/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
937/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000938unsigned ARMMCCodeEmitter::
939getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
940 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000941 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000942 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +0000943
Owen Andersond9aa7d32010-11-02 00:05:05 +0000944 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000945 unsigned Align = 0;
946
947 switch (Imm.getImm()) {
948 default: break;
949 case 2:
950 case 4:
951 case 8: Align = 0x01; break;
952 case 16: Align = 0x02; break;
953 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000954 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000955
Owen Andersond9aa7d32010-11-02 00:05:05 +0000956 return RegNo | (Align << 4);
957}
958
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000959/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
960/// alignment operand for use in VLD-dup instructions. This is the same as
961/// getAddrMode6AddressOpValue except for the alignment encoding, which is
962/// different for VLD4-dup.
963unsigned ARMMCCodeEmitter::
964getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
965 SmallVectorImpl<MCFixup> &Fixups) const {
966 const MCOperand &Reg = MI.getOperand(Op);
967 const MCOperand &Imm = MI.getOperand(Op + 1);
968
969 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
970 unsigned Align = 0;
971
972 switch (Imm.getImm()) {
973 default: break;
974 case 2:
975 case 4:
976 case 8: Align = 0x01; break;
977 case 16: Align = 0x03; break;
978 }
979
980 return RegNo | (Align << 4);
981}
982
Jim Grosbach806e80e2010-11-03 23:52:49 +0000983unsigned ARMMCCodeEmitter::
984getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
985 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000986 const MCOperand &MO = MI.getOperand(Op);
987 if (MO.getReg() == 0) return 0x0D;
988 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +0000989}
990
Jim Grosbach568eeed2010-09-17 18:46:17 +0000991void ARMMCCodeEmitter::
992EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000993 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000994 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +0000995 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000996 uint64_t TSFlags = Desc.TSFlags;
997 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000998 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000999 int Size;
1000 // Basic size info comes from the TSFlags field.
1001 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1002 default: llvm_unreachable("Unexpected instruction size!");
1003 case ARMII::Size2Bytes: Size = 2; break;
1004 case ARMII::Size4Bytes: Size = 4; break;
1005 }
1006 EmitConstant(getBinaryCodeForInstr(MI, Fixups), Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001007 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001008}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001009
Jim Grosbach806e80e2010-11-03 23:52:49 +00001010#include "ARMGenMCCodeEmitter.inc"