blob: d35a1c6fb33b782ab7b2714dcb328037a6361be8 [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,
252 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000253 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
254 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000255
Jim Grosbach70933262010-11-04 01:12:30 +0000256 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000257 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000258 }
259
Jim Grosbach70933262010-11-04 01:12:30 +0000260 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000261 // Output the constant in little endian byte order.
262 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000263 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000264 Val >>= 8;
265 }
266 }
267
Jim Grosbach568eeed2010-09-17 18:46:17 +0000268 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
269 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000270};
271
272} // end anonymous namespace
273
Bill Wendling0800ce72010-11-02 22:53:11 +0000274MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
275 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000276 return new ARMMCCodeEmitter(TM, Ctx);
277}
278
Owen Anderson57dac882010-11-11 21:36:43 +0000279/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000280/// instructions, and rewrite them to their Thumb2 form if we are currently in
281/// Thumb2 mode.
282unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
283 unsigned EncodedValue) const {
284 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
285 if (Subtarget.isThumb2()) {
286 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
287 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
288 // set to 1111.
289 unsigned Bit24 = EncodedValue & 0x01000000;
290 unsigned Bit28 = Bit24 << 4;
291 EncodedValue &= 0xEFFFFFFF;
292 EncodedValue |= Bit28;
293 EncodedValue |= 0x0F000000;
294 }
295
296 return EncodedValue;
297}
298
Owen Anderson57dac882010-11-11 21:36:43 +0000299/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
300/// instructions, and rewrite them to their Thumb2 form if we are currently in
301/// Thumb2 mode.
302unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
303 unsigned EncodedValue) const {
304 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
305 if (Subtarget.isThumb2()) {
306 EncodedValue &= 0xF0FFFFFF;
307 EncodedValue |= 0x09000000;
308 }
309
310 return EncodedValue;
311}
312
Owen Anderson8f143912010-11-11 23:12:55 +0000313/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
314/// instructions, and rewrite them to their Thumb2 form if we are currently in
315/// Thumb2 mode.
316unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
317 unsigned EncodedValue) const {
318 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
319 if (Subtarget.isThumb2()) {
320 EncodedValue &= 0x00FFFFFF;
321 EncodedValue |= 0xEE000000;
322 }
323
324 return EncodedValue;
325}
326
327
Owen Anderson57dac882010-11-11 21:36:43 +0000328
Jim Grosbach56ac9072010-10-08 21:45:55 +0000329/// getMachineOpValue - Return binary encoding of operand. If the machine
330/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000331unsigned ARMMCCodeEmitter::
332getMachineOpValue(const MCInst &MI, const MCOperand &MO,
333 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000334 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000335 unsigned Reg = MO.getReg();
336 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000337
Jim Grosbachb0708d22010-11-30 23:51:41 +0000338 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000339 switch (Reg) {
340 default:
341 return RegNo;
342 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
343 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
344 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
345 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
346 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000347 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000348 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000349 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000350 } else if (MO.isFPImm()) {
351 return static_cast<unsigned>(APFloat(MO.getFPImm())
352 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000353 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000354
Jim Grosbach817c1a62010-11-19 00:27:09 +0000355 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000356 return 0;
357}
358
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000359/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000360bool ARMMCCodeEmitter::
361EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
362 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000363 const MCOperand &MO = MI.getOperand(OpIdx);
364 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000365
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000366 Reg = getARMRegisterNumbering(MO.getReg());
367
368 int32_t SImm = MO1.getImm();
369 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000370
Jim Grosbachab682a22010-10-28 18:34:10 +0000371 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000372 if (SImm == INT32_MIN)
373 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000374
Jim Grosbachab682a22010-10-28 18:34:10 +0000375 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000376 if (SImm < 0) {
377 SImm = -SImm;
378 isAdd = false;
379 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000380
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000381 Imm = SImm;
382 return isAdd;
383}
384
Jim Grosbachc466b932010-11-11 18:04:49 +0000385/// getBranchTargetOpValue - Return encoding info for 24-bit immediate
386/// branch target.
387uint32_t ARMMCCodeEmitter::
388getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
389 SmallVectorImpl<MCFixup> &Fixups) const {
390 const MCOperand &MO = MI.getOperand(OpIdx);
391
392 // If the destination is an immediate, we have nothing to do.
393 if (MO.isImm()) return MO.getImm();
394 assert (MO.isExpr() && "Unexpected branch target type!");
395 const MCExpr *Expr = MO.getExpr();
396 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
397 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
398
399 // All of the information is in the fixup.
400 return 0;
401}
402
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000403/// getAdrLabelOpValue - Return encoding info for 12-bit immediate
404/// ADR label target.
405uint32_t ARMMCCodeEmitter::
406getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
407 SmallVectorImpl<MCFixup> &Fixups) const {
408 const MCOperand &MO = MI.getOperand(OpIdx);
409
410 // If the destination is an immediate, we have nothing to do.
411 if (MO.isImm()) return MO.getImm();
412 assert (MO.isExpr() && "Unexpected branch target type!");
413 const MCExpr *Expr = MO.getExpr();
414 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
415 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
416
417 // All of the information is in the fixup.
418 return 0;
419}
420
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000421/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000422uint32_t ARMMCCodeEmitter::
423getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
424 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000425 // {17-13} = reg
426 // {12} = (U)nsigned (add == '1', sub == '0')
427 // {11-0} = imm12
428 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000429 bool isAdd = true;
430 // If The first operand isn't a register, we have a label reference.
431 const MCOperand &MO = MI.getOperand(OpIdx);
432 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000433 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000434 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000435 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000436
437 assert(MO.isExpr() && "Unexpected machine operand type!");
438 const MCExpr *Expr = MO.getExpr();
439 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
440 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
441
442 ++MCNumCPRelocations;
443 } else
444 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000445
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000446 uint32_t Binary = Imm12 & 0xfff;
447 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000448 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000449 Binary |= (1 << 12);
450 Binary |= (Reg << 13);
451 return Binary;
452}
453
Owen Anderson9d63d902010-12-01 19:18:46 +0000454/// getT2AddrModeImm8s4OpValue - Return encoding info for
455/// 'reg +/- imm8<<2' operand.
456uint32_t ARMMCCodeEmitter::
457getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
458 SmallVectorImpl<MCFixup> &Fixups) const {
459 // {17-13} = reg
460 // {12} = (U)nsigned (add == '1', sub == '0')
461 // {11-0} = imm8
462 unsigned Reg, Imm8;
463 bool isAdd = true;
464 // If The first operand isn't a register, we have a label reference.
465 const MCOperand &MO = MI.getOperand(OpIdx);
466 if (!MO.isReg()) {
467 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
468 Imm8 = 0;
469 isAdd = false ; // 'U' bit is set as part of the fixup.
470
471 assert(MO.isExpr() && "Unexpected machine operand type!");
472 const MCExpr *Expr = MO.getExpr();
473 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
474 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
475
476 ++MCNumCPRelocations;
477 } else
478 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
479
480 uint32_t Binary = (Imm8 >> 2) & 0xff;
481 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
482 if (isAdd)
483 Binary |= (1 << 9);
484 Binary |= (Reg << 9);
485 return Binary;
486}
487
Jim Grosbach54fea632010-11-09 17:20:53 +0000488uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000489getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
490 SmallVectorImpl<MCFixup> &Fixups) const {
491 // {20-16} = imm{15-12}
492 // {11-0} = imm{11-0}
493 const MCOperand &MO = MI.getOperand(OpIdx);
494 if (MO.isImm()) {
495 return static_cast<unsigned>(MO.getImm());
496 } else if (const MCSymbolRefExpr *Expr =
497 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
498 MCFixupKind Kind;
499 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000500 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000501 case MCSymbolRefExpr::VK_ARM_HI16:
502 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
503 break;
504 case MCSymbolRefExpr::VK_ARM_LO16:
505 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
506 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000507 }
508 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
509 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000510 };
511 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000512 return 0;
513}
514
515uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000516getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
517 SmallVectorImpl<MCFixup> &Fixups) const {
518 const MCOperand &MO = MI.getOperand(OpIdx);
519 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
520 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
521 unsigned Rn = getARMRegisterNumbering(MO.getReg());
522 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000523 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
524 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000525 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
526 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000527
528 // {16-13} = Rn
529 // {12} = isAdd
530 // {11-0} = shifter
531 // {3-0} = Rm
532 // {4} = 0
533 // {6-5} = type
534 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000535 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000536 Binary |= Rn << 13;
537 Binary |= SBits << 5;
538 Binary |= ShImm << 7;
539 if (isAdd)
540 Binary |= 1 << 12;
541 return Binary;
542}
543
Jim Grosbach570a9222010-11-11 01:09:40 +0000544uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000545getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
546 SmallVectorImpl<MCFixup> &Fixups) const {
547 // {17-14} Rn
548 // {13} 1 == imm12, 0 == Rm
549 // {12} isAdd
550 // {11-0} imm12/Rm
551 const MCOperand &MO = MI.getOperand(OpIdx);
552 unsigned Rn = getARMRegisterNumbering(MO.getReg());
553 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
554 Binary |= Rn << 14;
555 return Binary;
556}
557
558uint32_t ARMMCCodeEmitter::
559getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
560 SmallVectorImpl<MCFixup> &Fixups) const {
561 // {13} 1 == imm12, 0 == Rm
562 // {12} isAdd
563 // {11-0} imm12/Rm
564 const MCOperand &MO = MI.getOperand(OpIdx);
565 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
566 unsigned Imm = MO1.getImm();
567 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
568 bool isReg = MO.getReg() != 0;
569 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
570 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
571 if (isReg) {
572 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
573 Binary <<= 7; // Shift amount is bits [11:7]
574 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
575 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
576 }
577 return Binary | (isAdd << 12) | (isReg << 13);
578}
579
580uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000581getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
582 SmallVectorImpl<MCFixup> &Fixups) const {
583 // {9} 1 == imm8, 0 == Rm
584 // {8} isAdd
585 // {7-4} imm7_4/zero
586 // {3-0} imm3_0/Rm
587 const MCOperand &MO = MI.getOperand(OpIdx);
588 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
589 unsigned Imm = MO1.getImm();
590 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
591 bool isImm = MO.getReg() == 0;
592 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
593 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
594 if (!isImm)
595 Imm8 = getARMRegisterNumbering(MO.getReg());
596 return Imm8 | (isAdd << 8) | (isImm << 9);
597}
598
599uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000600getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
601 SmallVectorImpl<MCFixup> &Fixups) const {
602 // {13} 1 == imm8, 0 == Rm
603 // {12-9} Rn
604 // {8} isAdd
605 // {7-4} imm7_4/zero
606 // {3-0} imm3_0/Rm
607 const MCOperand &MO = MI.getOperand(OpIdx);
608 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
609 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
610 unsigned Rn = getARMRegisterNumbering(MO.getReg());
611 unsigned Imm = MO2.getImm();
612 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
613 bool isImm = MO1.getReg() == 0;
614 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
615 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
616 if (!isImm)
617 Imm8 = getARMRegisterNumbering(MO1.getReg());
618 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
619}
620
Bill Wendling1fd374e2010-11-30 22:57:21 +0000621/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
622static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
623 unsigned Scale) {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000624 // [Rn, Rm]
625 // {5-3} = Rm
626 // {2-0} = Rn
627 //
628 // [Rn, #imm]
629 // {7-3} = imm5
630 // {2-0} = Rn
631 const MCOperand &MO = MI.getOperand(OpIdx);
632 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
633 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
634 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling1fd374e2010-11-30 22:57:21 +0000635 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000636 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
637 return (Rm << 3) | (Imm5 << 3) | Rn;
638}
639
Bill Wendling1fd374e2010-11-30 22:57:21 +0000640/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
641uint32_t ARMMCCodeEmitter::
642getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
643 SmallVectorImpl<MCFixup> &) const {
644 return getAddrModeSOpValue(MI, OpIdx, 4);
645}
646
647/// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
648uint32_t ARMMCCodeEmitter::
649getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
650 SmallVectorImpl<MCFixup> &) const {
651 return getAddrModeSOpValue(MI, OpIdx, 2);
652}
653
654/// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
655uint32_t ARMMCCodeEmitter::
656getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
657 SmallVectorImpl<MCFixup> &) const {
658 return getAddrModeSOpValue(MI, OpIdx, 1);
659}
660
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000661/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000662uint32_t ARMMCCodeEmitter::
663getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
664 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000665 // {12-9} = reg
666 // {8} = (U)nsigned (add == '1', sub == '0')
667 // {7-0} = imm8
668 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000669 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000670 // If The first operand isn't a register, we have a label reference.
671 const MCOperand &MO = MI.getOperand(OpIdx);
672 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000673 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000674 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000675 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000676
677 assert(MO.isExpr() && "Unexpected machine operand type!");
678 const MCExpr *Expr = MO.getExpr();
Owen Anderson9d63d902010-12-01 19:18:46 +0000679 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000680 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
681
682 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000683 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000684 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000685 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
686 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000687
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000688 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
689 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000690 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000691 Binary |= (1 << 8);
692 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000693 return Binary;
694}
695
Jim Grosbach806e80e2010-11-03 23:52:49 +0000696unsigned ARMMCCodeEmitter::
697getSORegOpValue(const MCInst &MI, unsigned OpIdx,
698 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000699 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
700 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
701 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000702 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000703 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000704 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000705 // {6-5} = type
706 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000707 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000708 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000709 // else (imm shift)
710 // {11-7} = imm
711
712 const MCOperand &MO = MI.getOperand(OpIdx);
713 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
714 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
715 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
716
717 // Encode Rm.
718 unsigned Binary = getARMRegisterNumbering(MO.getReg());
719
720 // Encode the shift opcode.
721 unsigned SBits = 0;
722 unsigned Rs = MO1.getReg();
723 if (Rs) {
724 // Set shift operand (bit[7:4]).
725 // LSL - 0001
726 // LSR - 0011
727 // ASR - 0101
728 // ROR - 0111
729 // RRX - 0110 and bit[11:8] clear.
730 switch (SOpc) {
731 default: llvm_unreachable("Unknown shift opc!");
732 case ARM_AM::lsl: SBits = 0x1; break;
733 case ARM_AM::lsr: SBits = 0x3; break;
734 case ARM_AM::asr: SBits = 0x5; break;
735 case ARM_AM::ror: SBits = 0x7; break;
736 case ARM_AM::rrx: SBits = 0x6; break;
737 }
738 } else {
739 // Set shift operand (bit[6:4]).
740 // LSL - 000
741 // LSR - 010
742 // ASR - 100
743 // ROR - 110
744 switch (SOpc) {
745 default: llvm_unreachable("Unknown shift opc!");
746 case ARM_AM::lsl: SBits = 0x0; break;
747 case ARM_AM::lsr: SBits = 0x2; break;
748 case ARM_AM::asr: SBits = 0x4; break;
749 case ARM_AM::ror: SBits = 0x6; break;
750 }
751 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000752
Jim Grosbachef324d72010-10-12 23:53:58 +0000753 Binary |= SBits << 4;
754 if (SOpc == ARM_AM::rrx)
755 return Binary;
756
757 // Encode the shift operation Rs or shift_imm (except rrx).
758 if (Rs) {
759 // Encode Rs bit[11:8].
760 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
761 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
762 }
763
764 // Encode shift_imm bit[11:7].
765 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
766}
767
Jim Grosbach806e80e2010-11-03 23:52:49 +0000768unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000769getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
770 SmallVectorImpl<MCFixup> &Fixups) const {
771 const MCOperand &MO1 = MI.getOperand(OpNum);
772 const MCOperand &MO2 = MI.getOperand(OpNum+1);
773 const MCOperand &MO3 = MI.getOperand(OpNum+2);
774
775 // Encoded as [Rn, Rm, imm].
776 // FIXME: Needs fixup support.
777 unsigned Value = getARMRegisterNumbering(MO1.getReg());
778 Value <<= 4;
779 Value |= getARMRegisterNumbering(MO2.getReg());
780 Value <<= 2;
781 Value |= MO3.getImm();
782
783 return Value;
784}
785
786unsigned ARMMCCodeEmitter::
787getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
788 SmallVectorImpl<MCFixup> &Fixups) const {
789 const MCOperand &MO1 = MI.getOperand(OpNum);
790 const MCOperand &MO2 = MI.getOperand(OpNum+1);
791
792 // FIXME: Needs fixup support.
793 unsigned Value = getARMRegisterNumbering(MO1.getReg());
794
795 // Even though the immediate is 8 bits long, we need 9 bits in order
796 // to represent the (inverse of the) sign bit.
797 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000798 int32_t tmp = (int32_t)MO2.getImm();
799 if (tmp < 0)
800 tmp = abs(tmp);
801 else
802 Value |= 256; // Set the ADD bit
803 Value |= tmp & 255;
804 return Value;
805}
806
807unsigned ARMMCCodeEmitter::
808getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
809 SmallVectorImpl<MCFixup> &Fixups) const {
810 const MCOperand &MO1 = MI.getOperand(OpNum);
811
812 // FIXME: Needs fixup support.
813 unsigned Value = 0;
814 int32_t tmp = (int32_t)MO1.getImm();
815 if (tmp < 0)
816 tmp = abs(tmp);
817 else
818 Value |= 256; // Set the ADD bit
819 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000820 return Value;
821}
822
823unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000824getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
825 SmallVectorImpl<MCFixup> &Fixups) const {
826 const MCOperand &MO1 = MI.getOperand(OpNum);
827
828 // FIXME: Needs fixup support.
829 unsigned Value = 0;
830 int32_t tmp = (int32_t)MO1.getImm();
831 if (tmp < 0)
832 tmp = abs(tmp);
833 else
834 Value |= 4096; // Set the ADD bit
835 Value |= tmp & 4095;
836 return Value;
837}
838
839unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000840getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
841 SmallVectorImpl<MCFixup> &Fixups) const {
842 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
843 // shifted. The second is the amount to shift by.
844 //
845 // {3-0} = Rm.
846 // {4} = 0
847 // {6-5} = type
848 // {11-7} = imm
849
850 const MCOperand &MO = MI.getOperand(OpIdx);
851 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
852 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
853
854 // Encode Rm.
855 unsigned Binary = getARMRegisterNumbering(MO.getReg());
856
857 // Encode the shift opcode.
858 unsigned SBits = 0;
859 // Set shift operand (bit[6:4]).
860 // LSL - 000
861 // LSR - 010
862 // ASR - 100
863 // ROR - 110
864 switch (SOpc) {
865 default: llvm_unreachable("Unknown shift opc!");
866 case ARM_AM::lsl: SBits = 0x0; break;
867 case ARM_AM::lsr: SBits = 0x2; break;
868 case ARM_AM::asr: SBits = 0x4; break;
869 case ARM_AM::ror: SBits = 0x6; break;
870 }
871
872 Binary |= SBits << 4;
873 if (SOpc == ARM_AM::rrx)
874 return Binary;
875
876 // Encode shift_imm bit[11:7].
877 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
878}
879
880unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000881getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
882 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000883 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
884 // msb of the mask.
885 const MCOperand &MO = MI.getOperand(Op);
886 uint32_t v = ~MO.getImm();
887 uint32_t lsb = CountTrailingZeros_32(v);
888 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
889 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
890 return lsb | (msb << 5);
891}
892
Jim Grosbach806e80e2010-11-03 23:52:49 +0000893unsigned ARMMCCodeEmitter::
894getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000895 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +0000896 // VLDM/VSTM:
897 // {12-8} = Vd
898 // {7-0} = Number of registers
899 //
900 // LDM/STM:
901 // {15-0} = Bitfield of GPRs.
902 unsigned Reg = MI.getOperand(Op).getReg();
903 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
904 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
905
Bill Wendling5e559a22010-11-09 00:30:18 +0000906 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +0000907
908 if (SPRRegs || DPRRegs) {
909 // VLDM/VSTM
910 unsigned RegNo = getARMRegisterNumbering(Reg);
911 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
912 Binary |= (RegNo & 0x1f) << 8;
913 if (SPRRegs)
914 Binary |= NumRegs;
915 else
916 Binary |= NumRegs * 2;
917 } else {
918 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
919 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
920 Binary |= 1 << RegNo;
921 }
Bill Wendling5e559a22010-11-09 00:30:18 +0000922 }
Bill Wendling6bc105a2010-11-17 00:45:23 +0000923
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000924 return Binary;
925}
926
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000927/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
928/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000929unsigned ARMMCCodeEmitter::
930getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
931 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000932 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000933 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +0000934
Owen Andersond9aa7d32010-11-02 00:05:05 +0000935 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000936 unsigned Align = 0;
937
938 switch (Imm.getImm()) {
939 default: break;
940 case 2:
941 case 4:
942 case 8: Align = 0x01; break;
943 case 16: Align = 0x02; break;
944 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000945 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000946
Owen Andersond9aa7d32010-11-02 00:05:05 +0000947 return RegNo | (Align << 4);
948}
949
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000950/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
951/// alignment operand for use in VLD-dup instructions. This is the same as
952/// getAddrMode6AddressOpValue except for the alignment encoding, which is
953/// different for VLD4-dup.
954unsigned ARMMCCodeEmitter::
955getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
956 SmallVectorImpl<MCFixup> &Fixups) const {
957 const MCOperand &Reg = MI.getOperand(Op);
958 const MCOperand &Imm = MI.getOperand(Op + 1);
959
960 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
961 unsigned Align = 0;
962
963 switch (Imm.getImm()) {
964 default: break;
965 case 2:
966 case 4:
967 case 8: Align = 0x01; break;
968 case 16: Align = 0x03; break;
969 }
970
971 return RegNo | (Align << 4);
972}
973
Jim Grosbach806e80e2010-11-03 23:52:49 +0000974unsigned ARMMCCodeEmitter::
975getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
976 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000977 const MCOperand &MO = MI.getOperand(Op);
978 if (MO.getReg() == 0) return 0x0D;
979 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +0000980}
981
Jim Grosbach568eeed2010-09-17 18:46:17 +0000982void ARMMCCodeEmitter::
983EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000984 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000985 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +0000986 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000987 uint64_t TSFlags = Desc.TSFlags;
988 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000989 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000990 int Size;
991 // Basic size info comes from the TSFlags field.
992 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
993 default: llvm_unreachable("Unexpected instruction size!");
994 case ARMII::Size2Bytes: Size = 2; break;
995 case ARMII::Size4Bytes: Size = 4; break;
996 }
997 EmitConstant(getBinaryCodeForInstr(MI, Fixups), Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +0000998 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +0000999}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001000
Jim Grosbach806e80e2010-11-03 23:52:49 +00001001#include "ARMGenMCCodeEmitter.inc"