blob: a20934dae20d41bf2071ffaa44c3427578605812 [file] [log] [blame]
Jim Grosbach568eeed2010-09-17 18:46:17 +00001//===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ARMMCCodeEmitter class.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2ac19022010-11-15 05:19:05 +000014#define DEBUG_TYPE "mccodeemitter"
Jim Grosbach568eeed2010-09-17 18:46:17 +000015#include "ARM.h"
Jim Grosbach42fac8e2010-10-11 23:16:21 +000016#include "ARMAddressingModes.h"
Jim Grosbach70933262010-11-04 01:12:30 +000017#include "ARMFixupKinds.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000018#include "ARMInstrInfo.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000019#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000022#include "llvm/ADT/Statistic.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000023#include "llvm/Support/raw_ostream.h"
24using namespace llvm;
25
Jim Grosbach70933262010-11-04 01:12:30 +000026STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
27STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
Jim Grosbachd6d4b422010-10-07 22:12:50 +000028
Jim Grosbach568eeed2010-09-17 18:46:17 +000029namespace {
30class ARMMCCodeEmitter : public MCCodeEmitter {
31 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
32 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
33 const TargetMachine &TM;
34 const TargetInstrInfo &TII;
35 MCContext &Ctx;
36
37public:
38 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
39 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000040 }
41
42 ~ARMMCCodeEmitter() {}
43
Jim Grosbachc466b932010-11-11 18:04:49 +000044 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
Jim Grosbach70933262010-11-04 01:12:30 +000045
46 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
47 const static MCFixupKindInfo Infos[] = {
Jim Grosbachc466b932010-11-11 18:04:49 +000048 // name offset bits flags
Jim Grosbach97dd28f2010-11-30 22:40:36 +000049 { "fixup_arm_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Anderson9d63d902010-12-01 19:18:46 +000050 { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachc466b932010-11-11 18:04:49 +000051 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Jason W Kim61db6292010-11-30 23:27:18 +000052 { "fixup_arm_movt_hi16", 0, 16, 0 },
53 { "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000054 };
55
56 if (Kind < FirstTargetFixupKind)
57 return MCCodeEmitter::getFixupKindInfo(Kind);
58
59 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
60 "Invalid kind!");
61 return Infos[Kind - FirstTargetFixupKind];
62 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000063 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
64
Jim Grosbach9af82ba2010-10-07 21:57:55 +000065 // getBinaryCodeForInstr - TableGen'erated function for getting the
66 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000067 unsigned getBinaryCodeForInstr(const MCInst &MI,
68 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000069
70 /// getMachineOpValue - Return binary encoding of operand. If the machine
71 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000072 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
73 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000074
Jason W Kim837caa92010-11-18 23:37:15 +000075 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
76 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
77 SmallVectorImpl<MCFixup> &Fixups) const;
78
Bill Wendling92b5a2e2010-11-03 01:49:29 +000079 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000080 unsigned &Reg, unsigned &Imm,
81 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000082
Jim Grosbachc466b932010-11-11 18:04:49 +000083 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
84 /// branch target.
85 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
86 SmallVectorImpl<MCFixup> &Fixups) const;
87
Jim Grosbach5d14f9b2010-12-01 19:47:31 +000088 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
89 /// ADR label target.
90 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
91 SmallVectorImpl<MCFixup> &Fixups) const;
92
Bill Wendling92b5a2e2010-11-03 01:49:29 +000093 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
94 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +000095 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
96 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000097
Owen Anderson9d63d902010-12-01 19:18:46 +000098 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
99 /// operand.
100 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
101 SmallVectorImpl<MCFixup> &Fixups) const;
102
103
Jim Grosbach54fea632010-11-09 17:20:53 +0000104 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
105 /// operand as needed by load/store instructions.
106 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
107 SmallVectorImpl<MCFixup> &Fixups) const;
108
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000109 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
110 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
111 SmallVectorImpl<MCFixup> &Fixups) const {
112 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
113 switch (Mode) {
114 default: assert(0 && "Unknown addressing sub-mode!");
115 case ARM_AM::da: return 0;
116 case ARM_AM::ia: return 1;
117 case ARM_AM::db: return 2;
118 case ARM_AM::ib: return 3;
119 }
120 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000121 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
122 ///
123 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
124 switch (ShOpc) {
125 default: llvm_unreachable("Unknown shift opc!");
126 case ARM_AM::no_shift:
127 case ARM_AM::lsl: return 0;
128 case ARM_AM::lsr: return 1;
129 case ARM_AM::asr: return 2;
130 case ARM_AM::ror:
131 case ARM_AM::rrx: return 3;
132 }
133 return 0;
134 }
135
136 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
137 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
138 SmallVectorImpl<MCFixup> &Fixups) const;
139
140 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
141 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
142 SmallVectorImpl<MCFixup> &Fixups) const;
143
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000144 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
145 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
146 SmallVectorImpl<MCFixup> &Fixups) const;
147
Jim Grosbach570a9222010-11-11 01:09:40 +0000148 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
149 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
150 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000151
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000152 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
153 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
154 SmallVectorImpl<MCFixup> &Fixups) const;
155
Bill Wendling1fd374e2010-11-30 22:57:21 +0000156 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
157 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
158 SmallVectorImpl<MCFixup> &Fixups) const;
159
160 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
161 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
162 SmallVectorImpl<MCFixup> &Fixups) const;
163
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000164 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000165 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
166 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000167
Jim Grosbach08bd5492010-10-12 23:00:24 +0000168 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000169 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
170 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000171 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
172 // '1' respectively.
173 return MI.getOperand(Op).getReg() == ARM::CPSR;
174 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000175
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000176 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000177 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
178 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000179 unsigned SoImm = MI.getOperand(Op).getImm();
180 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
181 assert(SoImmVal != -1 && "Not a valid so_imm value!");
182
183 // Encode rotate_imm.
184 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
185 << ARMII::SoRotImmShift;
186
187 // Encode immed_8.
188 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
189 return Binary;
190 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000191
192 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
193 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
194 SmallVectorImpl<MCFixup> &Fixups) const {
195 unsigned SoImm = MI.getOperand(Op).getImm();
196 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
197 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
198 return Encoded;
199 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000200
Owen Anderson75579f72010-11-29 22:44:32 +0000201 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
202 SmallVectorImpl<MCFixup> &Fixups) const;
203 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
204 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000205 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
206 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000207 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
208 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000209
Jim Grosbachef324d72010-10-12 23:53:58 +0000210 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000211 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
212 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000213 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
214 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000215
Jim Grosbach806e80e2010-11-03 23:52:49 +0000216 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
217 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000218 switch (MI.getOperand(Op).getImm()) {
219 default: assert (0 && "Not a valid rot_imm value!");
220 case 0: return 0;
221 case 8: return 1;
222 case 16: return 2;
223 case 24: return 3;
224 }
225 }
226
Jim Grosbach806e80e2010-11-03 23:52:49 +0000227 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
228 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000229 return MI.getOperand(Op).getImm() - 1;
230 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000231
Jim Grosbach806e80e2010-11-03 23:52:49 +0000232 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
233 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000234 return 64 - MI.getOperand(Op).getImm();
235 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000236
Jim Grosbach806e80e2010-11-03 23:52:49 +0000237 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
238 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000239
Jim Grosbach806e80e2010-11-03 23:52:49 +0000240 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
241 SmallVectorImpl<MCFixup> &Fixups) const;
242 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
243 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000244 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
245 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000246 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
247 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000248
Owen Andersonc7139a62010-11-11 19:07:48 +0000249 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
250 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000251 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000252 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000253 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000254 unsigned EncodedValue) const;
255
256 unsigned VFPThumb2PostEncoder(const MCInst &MI,
257 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000258
Jim Grosbach70933262010-11-04 01:12:30 +0000259 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000260 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000261 }
262
Jim Grosbach70933262010-11-04 01:12:30 +0000263 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000264 // Output the constant in little endian byte order.
265 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000266 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000267 Val >>= 8;
268 }
269 }
270
Jim Grosbach568eeed2010-09-17 18:46:17 +0000271 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
272 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000273};
274
275} // end anonymous namespace
276
Bill Wendling0800ce72010-11-02 22:53:11 +0000277MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
278 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000279 return new ARMMCCodeEmitter(TM, Ctx);
280}
281
Owen Anderson57dac882010-11-11 21:36:43 +0000282/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000283/// instructions, and rewrite them to their Thumb2 form if we are currently in
284/// Thumb2 mode.
285unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
286 unsigned EncodedValue) const {
287 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
288 if (Subtarget.isThumb2()) {
289 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
290 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
291 // set to 1111.
292 unsigned Bit24 = EncodedValue & 0x01000000;
293 unsigned Bit28 = Bit24 << 4;
294 EncodedValue &= 0xEFFFFFFF;
295 EncodedValue |= Bit28;
296 EncodedValue |= 0x0F000000;
297 }
298
299 return EncodedValue;
300}
301
Owen Anderson57dac882010-11-11 21:36:43 +0000302/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
303/// instructions, and rewrite them to their Thumb2 form if we are currently in
304/// Thumb2 mode.
305unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
306 unsigned EncodedValue) const {
307 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
308 if (Subtarget.isThumb2()) {
309 EncodedValue &= 0xF0FFFFFF;
310 EncodedValue |= 0x09000000;
311 }
312
313 return EncodedValue;
314}
315
Owen Anderson8f143912010-11-11 23:12:55 +0000316/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
317/// instructions, and rewrite them to their Thumb2 form if we are currently in
318/// Thumb2 mode.
319unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
320 unsigned EncodedValue) const {
321 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
322 if (Subtarget.isThumb2()) {
323 EncodedValue &= 0x00FFFFFF;
324 EncodedValue |= 0xEE000000;
325 }
326
327 return EncodedValue;
328}
329
Bill Wendlingcf590262010-12-01 21:54:50 +0000330/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
331/// them to their Thumb2 form if we are currently in Thumb2 mode.
332unsigned ARMMCCodeEmitter::
333VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
334 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
335 EncodedValue &= 0x0FFFFFFF;
336 EncodedValue |= 0xE0000000;
337 }
338 return EncodedValue;
339}
Owen Anderson57dac882010-11-11 21:36:43 +0000340
Jim Grosbach56ac9072010-10-08 21:45:55 +0000341/// getMachineOpValue - Return binary encoding of operand. If the machine
342/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000343unsigned ARMMCCodeEmitter::
344getMachineOpValue(const MCInst &MI, const MCOperand &MO,
345 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000346 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000347 unsigned Reg = MO.getReg();
348 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000349
Jim Grosbachb0708d22010-11-30 23:51:41 +0000350 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000351 switch (Reg) {
352 default:
353 return RegNo;
354 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
355 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
356 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
357 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
358 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000359 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000360 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000361 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000362 } else if (MO.isFPImm()) {
363 return static_cast<unsigned>(APFloat(MO.getFPImm())
364 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000365 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000366
Jim Grosbach817c1a62010-11-19 00:27:09 +0000367 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000368 return 0;
369}
370
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000371/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000372bool ARMMCCodeEmitter::
373EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
374 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000375 const MCOperand &MO = MI.getOperand(OpIdx);
376 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000377
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000378 Reg = getARMRegisterNumbering(MO.getReg());
379
380 int32_t SImm = MO1.getImm();
381 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000382
Jim Grosbachab682a22010-10-28 18:34:10 +0000383 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000384 if (SImm == INT32_MIN)
385 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000386
Jim Grosbachab682a22010-10-28 18:34:10 +0000387 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000388 if (SImm < 0) {
389 SImm = -SImm;
390 isAdd = false;
391 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000392
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000393 Imm = SImm;
394 return isAdd;
395}
396
Jim Grosbachc466b932010-11-11 18:04:49 +0000397/// getBranchTargetOpValue - Return encoding info for 24-bit immediate
398/// branch target.
399uint32_t ARMMCCodeEmitter::
400getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
401 SmallVectorImpl<MCFixup> &Fixups) const {
402 const MCOperand &MO = MI.getOperand(OpIdx);
403
404 // If the destination is an immediate, we have nothing to do.
405 if (MO.isImm()) return MO.getImm();
406 assert (MO.isExpr() && "Unexpected branch target type!");
407 const MCExpr *Expr = MO.getExpr();
408 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
409 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
410
411 // All of the information is in the fixup.
412 return 0;
413}
414
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000415/// getAdrLabelOpValue - Return encoding info for 12-bit immediate
416/// ADR label target.
417uint32_t ARMMCCodeEmitter::
418getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
419 SmallVectorImpl<MCFixup> &Fixups) const {
420 const MCOperand &MO = MI.getOperand(OpIdx);
421
422 // If the destination is an immediate, we have nothing to do.
423 if (MO.isImm()) return MO.getImm();
424 assert (MO.isExpr() && "Unexpected branch target type!");
425 const MCExpr *Expr = MO.getExpr();
426 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
427 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
428
429 // All of the information is in the fixup.
430 return 0;
431}
432
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000433/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000434uint32_t ARMMCCodeEmitter::
435getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
436 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000437 // {17-13} = reg
438 // {12} = (U)nsigned (add == '1', sub == '0')
439 // {11-0} = imm12
440 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000441 bool isAdd = true;
442 // If The first operand isn't a register, we have a label reference.
443 const MCOperand &MO = MI.getOperand(OpIdx);
444 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000445 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000446 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000447 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000448
449 assert(MO.isExpr() && "Unexpected machine operand type!");
450 const MCExpr *Expr = MO.getExpr();
451 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
452 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
453
454 ++MCNumCPRelocations;
455 } else
456 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000457
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000458 uint32_t Binary = Imm12 & 0xfff;
459 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000460 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000461 Binary |= (1 << 12);
462 Binary |= (Reg << 13);
463 return Binary;
464}
465
Owen Anderson9d63d902010-12-01 19:18:46 +0000466/// getT2AddrModeImm8s4OpValue - Return encoding info for
467/// 'reg +/- imm8<<2' operand.
468uint32_t ARMMCCodeEmitter::
469getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
470 SmallVectorImpl<MCFixup> &Fixups) const {
471 // {17-13} = reg
472 // {12} = (U)nsigned (add == '1', sub == '0')
473 // {11-0} = imm8
474 unsigned Reg, Imm8;
475 bool isAdd = true;
476 // If The first operand isn't a register, we have a label reference.
477 const MCOperand &MO = MI.getOperand(OpIdx);
478 if (!MO.isReg()) {
479 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
480 Imm8 = 0;
481 isAdd = false ; // 'U' bit is set as part of the fixup.
482
483 assert(MO.isExpr() && "Unexpected machine operand type!");
484 const MCExpr *Expr = MO.getExpr();
485 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
486 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
487
488 ++MCNumCPRelocations;
489 } else
490 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
491
492 uint32_t Binary = (Imm8 >> 2) & 0xff;
493 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
494 if (isAdd)
495 Binary |= (1 << 9);
496 Binary |= (Reg << 9);
497 return Binary;
498}
499
Jim Grosbach54fea632010-11-09 17:20:53 +0000500uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000501getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
502 SmallVectorImpl<MCFixup> &Fixups) const {
503 // {20-16} = imm{15-12}
504 // {11-0} = imm{11-0}
505 const MCOperand &MO = MI.getOperand(OpIdx);
506 if (MO.isImm()) {
507 return static_cast<unsigned>(MO.getImm());
508 } else if (const MCSymbolRefExpr *Expr =
509 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
510 MCFixupKind Kind;
511 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000512 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000513 case MCSymbolRefExpr::VK_ARM_HI16:
514 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
515 break;
516 case MCSymbolRefExpr::VK_ARM_LO16:
517 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
518 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000519 }
520 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
521 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000522 };
523 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000524 return 0;
525}
526
527uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000528getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
529 SmallVectorImpl<MCFixup> &Fixups) const {
530 const MCOperand &MO = MI.getOperand(OpIdx);
531 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
532 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
533 unsigned Rn = getARMRegisterNumbering(MO.getReg());
534 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000535 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
536 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000537 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
538 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000539
540 // {16-13} = Rn
541 // {12} = isAdd
542 // {11-0} = shifter
543 // {3-0} = Rm
544 // {4} = 0
545 // {6-5} = type
546 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000547 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000548 Binary |= Rn << 13;
549 Binary |= SBits << 5;
550 Binary |= ShImm << 7;
551 if (isAdd)
552 Binary |= 1 << 12;
553 return Binary;
554}
555
Jim Grosbach570a9222010-11-11 01:09:40 +0000556uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000557getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
558 SmallVectorImpl<MCFixup> &Fixups) const {
559 // {17-14} Rn
560 // {13} 1 == imm12, 0 == Rm
561 // {12} isAdd
562 // {11-0} imm12/Rm
563 const MCOperand &MO = MI.getOperand(OpIdx);
564 unsigned Rn = getARMRegisterNumbering(MO.getReg());
565 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
566 Binary |= Rn << 14;
567 return Binary;
568}
569
570uint32_t ARMMCCodeEmitter::
571getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
572 SmallVectorImpl<MCFixup> &Fixups) const {
573 // {13} 1 == imm12, 0 == Rm
574 // {12} isAdd
575 // {11-0} imm12/Rm
576 const MCOperand &MO = MI.getOperand(OpIdx);
577 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
578 unsigned Imm = MO1.getImm();
579 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
580 bool isReg = MO.getReg() != 0;
581 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
582 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
583 if (isReg) {
584 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
585 Binary <<= 7; // Shift amount is bits [11:7]
586 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
587 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
588 }
589 return Binary | (isAdd << 12) | (isReg << 13);
590}
591
592uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000593getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
594 SmallVectorImpl<MCFixup> &Fixups) const {
595 // {9} 1 == imm8, 0 == Rm
596 // {8} isAdd
597 // {7-4} imm7_4/zero
598 // {3-0} imm3_0/Rm
599 const MCOperand &MO = MI.getOperand(OpIdx);
600 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
601 unsigned Imm = MO1.getImm();
602 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
603 bool isImm = MO.getReg() == 0;
604 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
605 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
606 if (!isImm)
607 Imm8 = getARMRegisterNumbering(MO.getReg());
608 return Imm8 | (isAdd << 8) | (isImm << 9);
609}
610
611uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000612getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
613 SmallVectorImpl<MCFixup> &Fixups) const {
614 // {13} 1 == imm8, 0 == Rm
615 // {12-9} Rn
616 // {8} isAdd
617 // {7-4} imm7_4/zero
618 // {3-0} imm3_0/Rm
619 const MCOperand &MO = MI.getOperand(OpIdx);
620 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
621 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
622 unsigned Rn = getARMRegisterNumbering(MO.getReg());
623 unsigned Imm = MO2.getImm();
624 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
625 bool isImm = MO1.getReg() == 0;
626 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
627 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
628 if (!isImm)
629 Imm8 = getARMRegisterNumbering(MO1.getReg());
630 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
631}
632
Bill Wendling1fd374e2010-11-30 22:57:21 +0000633/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
634static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
635 unsigned Scale) {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000636 // [Rn, Rm]
637 // {5-3} = Rm
638 // {2-0} = Rn
639 //
640 // [Rn, #imm]
641 // {7-3} = imm5
642 // {2-0} = Rn
643 const MCOperand &MO = MI.getOperand(OpIdx);
644 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
645 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
646 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling1fd374e2010-11-30 22:57:21 +0000647 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000648 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
649 return (Rm << 3) | (Imm5 << 3) | Rn;
650}
651
Bill Wendling1fd374e2010-11-30 22:57:21 +0000652/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
653uint32_t ARMMCCodeEmitter::
654getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
655 SmallVectorImpl<MCFixup> &) const {
656 return getAddrModeSOpValue(MI, OpIdx, 4);
657}
658
659/// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
660uint32_t ARMMCCodeEmitter::
661getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
662 SmallVectorImpl<MCFixup> &) const {
663 return getAddrModeSOpValue(MI, OpIdx, 2);
664}
665
666/// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
667uint32_t ARMMCCodeEmitter::
668getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
669 SmallVectorImpl<MCFixup> &) const {
670 return getAddrModeSOpValue(MI, OpIdx, 1);
671}
672
Jim Grosbach5177f792010-12-01 21:09:40 +0000673/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000674uint32_t ARMMCCodeEmitter::
675getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
676 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000677 // {12-9} = reg
678 // {8} = (U)nsigned (add == '1', sub == '0')
679 // {7-0} = imm8
680 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000681 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000682 // If The first operand isn't a register, we have a label reference.
683 const MCOperand &MO = MI.getOperand(OpIdx);
684 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000685 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000686 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000687 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000688
689 assert(MO.isExpr() && "Unexpected machine operand type!");
690 const MCExpr *Expr = MO.getExpr();
Jim Grosbach5177f792010-12-01 21:09:40 +0000691 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000692 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
693
694 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000695 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000696 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000697 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
698 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000699
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000700 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
701 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000702 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000703 Binary |= (1 << 8);
704 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000705 return Binary;
706}
707
Jim Grosbach806e80e2010-11-03 23:52:49 +0000708unsigned ARMMCCodeEmitter::
709getSORegOpValue(const MCInst &MI, unsigned OpIdx,
710 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000711 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
712 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
713 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000714 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000715 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000716 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000717 // {6-5} = type
718 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000719 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000720 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000721 // else (imm shift)
722 // {11-7} = imm
723
724 const MCOperand &MO = MI.getOperand(OpIdx);
725 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
726 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
727 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
728
729 // Encode Rm.
730 unsigned Binary = getARMRegisterNumbering(MO.getReg());
731
732 // Encode the shift opcode.
733 unsigned SBits = 0;
734 unsigned Rs = MO1.getReg();
735 if (Rs) {
736 // Set shift operand (bit[7:4]).
737 // LSL - 0001
738 // LSR - 0011
739 // ASR - 0101
740 // ROR - 0111
741 // RRX - 0110 and bit[11:8] clear.
742 switch (SOpc) {
743 default: llvm_unreachable("Unknown shift opc!");
744 case ARM_AM::lsl: SBits = 0x1; break;
745 case ARM_AM::lsr: SBits = 0x3; break;
746 case ARM_AM::asr: SBits = 0x5; break;
747 case ARM_AM::ror: SBits = 0x7; break;
748 case ARM_AM::rrx: SBits = 0x6; break;
749 }
750 } else {
751 // Set shift operand (bit[6:4]).
752 // LSL - 000
753 // LSR - 010
754 // ASR - 100
755 // ROR - 110
756 switch (SOpc) {
757 default: llvm_unreachable("Unknown shift opc!");
758 case ARM_AM::lsl: SBits = 0x0; break;
759 case ARM_AM::lsr: SBits = 0x2; break;
760 case ARM_AM::asr: SBits = 0x4; break;
761 case ARM_AM::ror: SBits = 0x6; break;
762 }
763 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000764
Jim Grosbachef324d72010-10-12 23:53:58 +0000765 Binary |= SBits << 4;
766 if (SOpc == ARM_AM::rrx)
767 return Binary;
768
769 // Encode the shift operation Rs or shift_imm (except rrx).
770 if (Rs) {
771 // Encode Rs bit[11:8].
772 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
773 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
774 }
775
776 // Encode shift_imm bit[11:7].
777 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
778}
779
Jim Grosbach806e80e2010-11-03 23:52:49 +0000780unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000781getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
782 SmallVectorImpl<MCFixup> &Fixups) const {
783 const MCOperand &MO1 = MI.getOperand(OpNum);
784 const MCOperand &MO2 = MI.getOperand(OpNum+1);
785 const MCOperand &MO3 = MI.getOperand(OpNum+2);
786
787 // Encoded as [Rn, Rm, imm].
788 // FIXME: Needs fixup support.
789 unsigned Value = getARMRegisterNumbering(MO1.getReg());
790 Value <<= 4;
791 Value |= getARMRegisterNumbering(MO2.getReg());
792 Value <<= 2;
793 Value |= MO3.getImm();
794
795 return Value;
796}
797
798unsigned ARMMCCodeEmitter::
799getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
800 SmallVectorImpl<MCFixup> &Fixups) const {
801 const MCOperand &MO1 = MI.getOperand(OpNum);
802 const MCOperand &MO2 = MI.getOperand(OpNum+1);
803
804 // FIXME: Needs fixup support.
805 unsigned Value = getARMRegisterNumbering(MO1.getReg());
806
807 // Even though the immediate is 8 bits long, we need 9 bits in order
808 // to represent the (inverse of the) sign bit.
809 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000810 int32_t tmp = (int32_t)MO2.getImm();
811 if (tmp < 0)
812 tmp = abs(tmp);
813 else
814 Value |= 256; // Set the ADD bit
815 Value |= tmp & 255;
816 return Value;
817}
818
819unsigned ARMMCCodeEmitter::
820getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
821 SmallVectorImpl<MCFixup> &Fixups) const {
822 const MCOperand &MO1 = MI.getOperand(OpNum);
823
824 // FIXME: Needs fixup support.
825 unsigned Value = 0;
826 int32_t tmp = (int32_t)MO1.getImm();
827 if (tmp < 0)
828 tmp = abs(tmp);
829 else
830 Value |= 256; // Set the ADD bit
831 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000832 return Value;
833}
834
835unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000836getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
837 SmallVectorImpl<MCFixup> &Fixups) const {
838 const MCOperand &MO1 = MI.getOperand(OpNum);
839
840 // FIXME: Needs fixup support.
841 unsigned Value = 0;
842 int32_t tmp = (int32_t)MO1.getImm();
843 if (tmp < 0)
844 tmp = abs(tmp);
845 else
846 Value |= 4096; // Set the ADD bit
847 Value |= tmp & 4095;
848 return Value;
849}
850
851unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000852getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
853 SmallVectorImpl<MCFixup> &Fixups) const {
854 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
855 // shifted. The second is the amount to shift by.
856 //
857 // {3-0} = Rm.
858 // {4} = 0
859 // {6-5} = type
860 // {11-7} = imm
861
862 const MCOperand &MO = MI.getOperand(OpIdx);
863 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
864 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
865
866 // Encode Rm.
867 unsigned Binary = getARMRegisterNumbering(MO.getReg());
868
869 // Encode the shift opcode.
870 unsigned SBits = 0;
871 // Set shift operand (bit[6:4]).
872 // LSL - 000
873 // LSR - 010
874 // ASR - 100
875 // ROR - 110
876 switch (SOpc) {
877 default: llvm_unreachable("Unknown shift opc!");
878 case ARM_AM::lsl: SBits = 0x0; break;
879 case ARM_AM::lsr: SBits = 0x2; break;
880 case ARM_AM::asr: SBits = 0x4; break;
881 case ARM_AM::ror: SBits = 0x6; break;
882 }
883
884 Binary |= SBits << 4;
885 if (SOpc == ARM_AM::rrx)
886 return Binary;
887
888 // Encode shift_imm bit[11:7].
889 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
890}
891
892unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000893getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
894 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000895 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
896 // msb of the mask.
897 const MCOperand &MO = MI.getOperand(Op);
898 uint32_t v = ~MO.getImm();
899 uint32_t lsb = CountTrailingZeros_32(v);
900 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
901 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
902 return lsb | (msb << 5);
903}
904
Jim Grosbach806e80e2010-11-03 23:52:49 +0000905unsigned ARMMCCodeEmitter::
906getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000907 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +0000908 // VLDM/VSTM:
909 // {12-8} = Vd
910 // {7-0} = Number of registers
911 //
912 // LDM/STM:
913 // {15-0} = Bitfield of GPRs.
914 unsigned Reg = MI.getOperand(Op).getReg();
915 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
916 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
917
Bill Wendling5e559a22010-11-09 00:30:18 +0000918 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +0000919
920 if (SPRRegs || DPRRegs) {
921 // VLDM/VSTM
922 unsigned RegNo = getARMRegisterNumbering(Reg);
923 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
924 Binary |= (RegNo & 0x1f) << 8;
925 if (SPRRegs)
926 Binary |= NumRegs;
927 else
928 Binary |= NumRegs * 2;
929 } else {
930 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
931 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
932 Binary |= 1 << RegNo;
933 }
Bill Wendling5e559a22010-11-09 00:30:18 +0000934 }
Bill Wendling6bc105a2010-11-17 00:45:23 +0000935
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000936 return Binary;
937}
938
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000939/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
940/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000941unsigned ARMMCCodeEmitter::
942getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
943 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000944 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000945 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +0000946
Owen Andersond9aa7d32010-11-02 00:05:05 +0000947 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000948 unsigned Align = 0;
949
950 switch (Imm.getImm()) {
951 default: break;
952 case 2:
953 case 4:
954 case 8: Align = 0x01; break;
955 case 16: Align = 0x02; break;
956 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000957 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000958
Owen Andersond9aa7d32010-11-02 00:05:05 +0000959 return RegNo | (Align << 4);
960}
961
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000962/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
963/// alignment operand for use in VLD-dup instructions. This is the same as
964/// getAddrMode6AddressOpValue except for the alignment encoding, which is
965/// different for VLD4-dup.
966unsigned ARMMCCodeEmitter::
967getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
968 SmallVectorImpl<MCFixup> &Fixups) const {
969 const MCOperand &Reg = MI.getOperand(Op);
970 const MCOperand &Imm = MI.getOperand(Op + 1);
971
972 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
973 unsigned Align = 0;
974
975 switch (Imm.getImm()) {
976 default: break;
977 case 2:
978 case 4:
979 case 8: Align = 0x01; break;
980 case 16: Align = 0x03; break;
981 }
982
983 return RegNo | (Align << 4);
984}
985
Jim Grosbach806e80e2010-11-03 23:52:49 +0000986unsigned ARMMCCodeEmitter::
987getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
988 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000989 const MCOperand &MO = MI.getOperand(Op);
990 if (MO.getReg() == 0) return 0x0D;
991 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +0000992}
993
Jim Grosbach568eeed2010-09-17 18:46:17 +0000994void ARMMCCodeEmitter::
995EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000996 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000997 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +0000998 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000999 uint64_t TSFlags = Desc.TSFlags;
1000 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001001 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001002 int Size;
1003 // Basic size info comes from the TSFlags field.
1004 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1005 default: llvm_unreachable("Unexpected instruction size!");
1006 case ARMII::Size2Bytes: Size = 2; break;
1007 case ARMII::Size4Bytes: Size = 4; break;
1008 }
1009 EmitConstant(getBinaryCodeForInstr(MI, Fixups), Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001010 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001011}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001012
Jim Grosbach806e80e2010-11-03 23:52:49 +00001013#include "ARMGenMCCodeEmitter.inc"