blob: a2829c9f2817897a907463bd7ea4f7c1340c0189 [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
49 { "fixup_arm_pcrel_12", 2, 12, MCFixupKindInfo::FKF_IsPCRel },
50 { "fixup_arm_vfp_pcrel_12", 3, 8, MCFixupKindInfo::FKF_IsPCRel },
51 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach70933262010-11-04 01:12:30 +000052 };
53
54 if (Kind < FirstTargetFixupKind)
55 return MCCodeEmitter::getFixupKindInfo(Kind);
56
57 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
58 "Invalid kind!");
59 return Infos[Kind - FirstTargetFixupKind];
60 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000061 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
62
Jim Grosbach9af82ba2010-10-07 21:57:55 +000063 // getBinaryCodeForInstr - TableGen'erated function for getting the
64 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000065 unsigned getBinaryCodeForInstr(const MCInst &MI,
66 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000067
68 /// getMachineOpValue - Return binary encoding of operand. If the machine
69 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000070 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
71 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000072
Jason W Kim837caa92010-11-18 23:37:15 +000073 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
74 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
75 SmallVectorImpl<MCFixup> &Fixups) const;
76
Bill Wendling92b5a2e2010-11-03 01:49:29 +000077 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000078 unsigned &Reg, unsigned &Imm,
79 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000080
Jim Grosbachc466b932010-11-11 18:04:49 +000081 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
82 /// branch target.
83 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
84 SmallVectorImpl<MCFixup> &Fixups) const;
85
Bill Wendling92b5a2e2010-11-03 01:49:29 +000086 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
87 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +000088 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
89 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000090
Jim Grosbach54fea632010-11-09 17:20:53 +000091 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
92 /// operand as needed by load/store instructions.
93 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
94 SmallVectorImpl<MCFixup> &Fixups) const;
95
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +000096 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
97 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
98 SmallVectorImpl<MCFixup> &Fixups) const {
99 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
100 switch (Mode) {
101 default: assert(0 && "Unknown addressing sub-mode!");
102 case ARM_AM::da: return 0;
103 case ARM_AM::ia: return 1;
104 case ARM_AM::db: return 2;
105 case ARM_AM::ib: return 3;
106 }
107 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000108 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
109 ///
110 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
111 switch (ShOpc) {
112 default: llvm_unreachable("Unknown shift opc!");
113 case ARM_AM::no_shift:
114 case ARM_AM::lsl: return 0;
115 case ARM_AM::lsr: return 1;
116 case ARM_AM::asr: return 2;
117 case ARM_AM::ror:
118 case ARM_AM::rrx: return 3;
119 }
120 return 0;
121 }
122
123 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
124 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
125 SmallVectorImpl<MCFixup> &Fixups) const;
126
127 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
128 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
130
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000131 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
132 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
133 SmallVectorImpl<MCFixup> &Fixups) const;
134
Jim Grosbach570a9222010-11-11 01:09:40 +0000135 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
136 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
137 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000138
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000139 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
140 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
141 SmallVectorImpl<MCFixup> &Fixups) const;
142
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000143 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000144 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
145 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000146
Jim Grosbach08bd5492010-10-12 23:00:24 +0000147 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000148 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
149 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000150 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
151 // '1' respectively.
152 return MI.getOperand(Op).getReg() == ARM::CPSR;
153 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000154
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000155 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000156 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
157 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000158 unsigned SoImm = MI.getOperand(Op).getImm();
159 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
160 assert(SoImmVal != -1 && "Not a valid so_imm value!");
161
162 // Encode rotate_imm.
163 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
164 << ARMII::SoRotImmShift;
165
166 // Encode immed_8.
167 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
168 return Binary;
169 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000170
171 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
172 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
173 SmallVectorImpl<MCFixup> &Fixups) const {
174 unsigned SoImm = MI.getOperand(Op).getImm();
175 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
176 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
177 return Encoded;
178 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000179
Owen Anderson75579f72010-11-29 22:44:32 +0000180 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
181 SmallVectorImpl<MCFixup> &Fixups) const;
182 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
183 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000184 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
185 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000186 unsigned getT2AddrModeImm12OpValue(const MCInst &MI, unsigned OpNum,
187 SmallVectorImpl<MCFixup> &Fixups) const;
188
Jim Grosbachef324d72010-10-12 23:53:58 +0000189 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000190 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
191 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000192 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
193 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000194
Jim Grosbach806e80e2010-11-03 23:52:49 +0000195 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
196 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000197 switch (MI.getOperand(Op).getImm()) {
198 default: assert (0 && "Not a valid rot_imm value!");
199 case 0: return 0;
200 case 8: return 1;
201 case 16: return 2;
202 case 24: return 3;
203 }
204 }
205
Jim Grosbach806e80e2010-11-03 23:52:49 +0000206 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
207 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000208 return MI.getOperand(Op).getImm() - 1;
209 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000210
Jim Grosbach806e80e2010-11-03 23:52:49 +0000211 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
212 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000213 return 64 - MI.getOperand(Op).getImm();
214 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000215
Jim Grosbach806e80e2010-11-03 23:52:49 +0000216 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
217 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000218
Jim Grosbach806e80e2010-11-03 23:52:49 +0000219 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
220 SmallVectorImpl<MCFixup> &Fixups) const;
221 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
222 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000223 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
224 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000225 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
226 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000227
Owen Andersonc7139a62010-11-11 19:07:48 +0000228 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
229 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000230 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
231 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000232 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
233 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000234
Jim Grosbach70933262010-11-04 01:12:30 +0000235 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000236 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000237 }
238
Jim Grosbach70933262010-11-04 01:12:30 +0000239 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000240 // Output the constant in little endian byte order.
241 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000242 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000243 Val >>= 8;
244 }
245 }
246
Jim Grosbach568eeed2010-09-17 18:46:17 +0000247 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
248 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000249};
250
251} // end anonymous namespace
252
Bill Wendling0800ce72010-11-02 22:53:11 +0000253MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
254 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000255 return new ARMMCCodeEmitter(TM, Ctx);
256}
257
Owen Anderson57dac882010-11-11 21:36:43 +0000258/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000259/// instructions, and rewrite them to their Thumb2 form if we are currently in
260/// Thumb2 mode.
261unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
262 unsigned EncodedValue) const {
263 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
264 if (Subtarget.isThumb2()) {
265 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
266 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
267 // set to 1111.
268 unsigned Bit24 = EncodedValue & 0x01000000;
269 unsigned Bit28 = Bit24 << 4;
270 EncodedValue &= 0xEFFFFFFF;
271 EncodedValue |= Bit28;
272 EncodedValue |= 0x0F000000;
273 }
274
275 return EncodedValue;
276}
277
Owen Anderson57dac882010-11-11 21:36:43 +0000278/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
279/// instructions, and rewrite them to their Thumb2 form if we are currently in
280/// Thumb2 mode.
281unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
282 unsigned EncodedValue) const {
283 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
284 if (Subtarget.isThumb2()) {
285 EncodedValue &= 0xF0FFFFFF;
286 EncodedValue |= 0x09000000;
287 }
288
289 return EncodedValue;
290}
291
Owen Anderson8f143912010-11-11 23:12:55 +0000292/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
293/// instructions, and rewrite them to their Thumb2 form if we are currently in
294/// Thumb2 mode.
295unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
296 unsigned EncodedValue) const {
297 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
298 if (Subtarget.isThumb2()) {
299 EncodedValue &= 0x00FFFFFF;
300 EncodedValue |= 0xEE000000;
301 }
302
303 return EncodedValue;
304}
305
306
Owen Anderson57dac882010-11-11 21:36:43 +0000307
Jim Grosbach56ac9072010-10-08 21:45:55 +0000308/// getMachineOpValue - Return binary encoding of operand. If the machine
309/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000310unsigned ARMMCCodeEmitter::
311getMachineOpValue(const MCInst &MI, const MCOperand &MO,
312 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000313 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000314 unsigned Reg = MO.getReg();
315 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000316
Owen Anderson90d4cf92010-10-21 20:49:13 +0000317 // Q registers are encodes as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000318 switch (Reg) {
319 default:
320 return RegNo;
321 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
322 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
323 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
324 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
325 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000326 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000327 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000328 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000329 } else if (MO.isFPImm()) {
330 return static_cast<unsigned>(APFloat(MO.getFPImm())
331 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000332 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000333
Jim Grosbach817c1a62010-11-19 00:27:09 +0000334 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000335 return 0;
336}
337
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000338/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000339bool ARMMCCodeEmitter::
340EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
341 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000342 const MCOperand &MO = MI.getOperand(OpIdx);
343 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000344
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000345 Reg = getARMRegisterNumbering(MO.getReg());
346
347 int32_t SImm = MO1.getImm();
348 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000349
Jim Grosbachab682a22010-10-28 18:34:10 +0000350 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000351 if (SImm == INT32_MIN)
352 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000353
Jim Grosbachab682a22010-10-28 18:34:10 +0000354 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000355 if (SImm < 0) {
356 SImm = -SImm;
357 isAdd = false;
358 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000359
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000360 Imm = SImm;
361 return isAdd;
362}
363
Jim Grosbachc466b932010-11-11 18:04:49 +0000364/// getBranchTargetOpValue - Return encoding info for 24-bit immediate
365/// branch target.
366uint32_t ARMMCCodeEmitter::
367getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
368 SmallVectorImpl<MCFixup> &Fixups) const {
369 const MCOperand &MO = MI.getOperand(OpIdx);
370
371 // If the destination is an immediate, we have nothing to do.
372 if (MO.isImm()) return MO.getImm();
373 assert (MO.isExpr() && "Unexpected branch target type!");
374 const MCExpr *Expr = MO.getExpr();
375 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
376 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
377
378 // All of the information is in the fixup.
379 return 0;
380}
381
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000382/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000383uint32_t ARMMCCodeEmitter::
384getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
385 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000386 // {17-13} = reg
387 // {12} = (U)nsigned (add == '1', sub == '0')
388 // {11-0} = imm12
389 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000390 bool isAdd = true;
391 // If The first operand isn't a register, we have a label reference.
392 const MCOperand &MO = MI.getOperand(OpIdx);
393 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000394 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000395 Imm12 = 0;
396
397 assert(MO.isExpr() && "Unexpected machine operand type!");
398 const MCExpr *Expr = MO.getExpr();
399 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
400 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
401
402 ++MCNumCPRelocations;
403 } else
404 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000405
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000406 uint32_t Binary = Imm12 & 0xfff;
407 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000408 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000409 Binary |= (1 << 12);
410 Binary |= (Reg << 13);
411 return Binary;
412}
413
Jim Grosbach54fea632010-11-09 17:20:53 +0000414uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000415getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
416 SmallVectorImpl<MCFixup> &Fixups) const {
417 // {20-16} = imm{15-12}
418 // {11-0} = imm{11-0}
419 const MCOperand &MO = MI.getOperand(OpIdx);
420 if (MO.isImm()) {
421 return static_cast<unsigned>(MO.getImm());
422 } else if (const MCSymbolRefExpr *Expr =
423 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
424 MCFixupKind Kind;
425 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000426 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000427 case MCSymbolRefExpr::VK_ARM_HI16:
428 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
429 break;
430 case MCSymbolRefExpr::VK_ARM_LO16:
431 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
432 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000433 }
434 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
435 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000436 };
437 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000438 return 0;
439}
440
441uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000442getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
443 SmallVectorImpl<MCFixup> &Fixups) const {
444 const MCOperand &MO = MI.getOperand(OpIdx);
445 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
446 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
447 unsigned Rn = getARMRegisterNumbering(MO.getReg());
448 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000449 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
450 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000451 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
452 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000453
454 // {16-13} = Rn
455 // {12} = isAdd
456 // {11-0} = shifter
457 // {3-0} = Rm
458 // {4} = 0
459 // {6-5} = type
460 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000461 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000462 Binary |= Rn << 13;
463 Binary |= SBits << 5;
464 Binary |= ShImm << 7;
465 if (isAdd)
466 Binary |= 1 << 12;
467 return Binary;
468}
469
Jim Grosbach570a9222010-11-11 01:09:40 +0000470uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000471getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
472 SmallVectorImpl<MCFixup> &Fixups) const {
473 // {17-14} Rn
474 // {13} 1 == imm12, 0 == Rm
475 // {12} isAdd
476 // {11-0} imm12/Rm
477 const MCOperand &MO = MI.getOperand(OpIdx);
478 unsigned Rn = getARMRegisterNumbering(MO.getReg());
479 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
480 Binary |= Rn << 14;
481 return Binary;
482}
483
484uint32_t ARMMCCodeEmitter::
485getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
486 SmallVectorImpl<MCFixup> &Fixups) const {
487 // {13} 1 == imm12, 0 == Rm
488 // {12} isAdd
489 // {11-0} imm12/Rm
490 const MCOperand &MO = MI.getOperand(OpIdx);
491 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
492 unsigned Imm = MO1.getImm();
493 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
494 bool isReg = MO.getReg() != 0;
495 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
496 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
497 if (isReg) {
498 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
499 Binary <<= 7; // Shift amount is bits [11:7]
500 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
501 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
502 }
503 return Binary | (isAdd << 12) | (isReg << 13);
504}
505
506uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000507getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
508 SmallVectorImpl<MCFixup> &Fixups) const {
509 // {9} 1 == imm8, 0 == Rm
510 // {8} isAdd
511 // {7-4} imm7_4/zero
512 // {3-0} imm3_0/Rm
513 const MCOperand &MO = MI.getOperand(OpIdx);
514 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
515 unsigned Imm = MO1.getImm();
516 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
517 bool isImm = MO.getReg() == 0;
518 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
519 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
520 if (!isImm)
521 Imm8 = getARMRegisterNumbering(MO.getReg());
522 return Imm8 | (isAdd << 8) | (isImm << 9);
523}
524
525uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000526getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
527 SmallVectorImpl<MCFixup> &Fixups) const {
528 // {13} 1 == imm8, 0 == Rm
529 // {12-9} Rn
530 // {8} isAdd
531 // {7-4} imm7_4/zero
532 // {3-0} imm3_0/Rm
533 const MCOperand &MO = MI.getOperand(OpIdx);
534 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
535 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
536 unsigned Rn = getARMRegisterNumbering(MO.getReg());
537 unsigned Imm = MO2.getImm();
538 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
539 bool isImm = MO1.getReg() == 0;
540 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
541 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
542 if (!isImm)
543 Imm8 = getARMRegisterNumbering(MO1.getReg());
544 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
545}
546
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000547/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
548uint32_t ARMMCCodeEmitter::
549getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
550 SmallVectorImpl<MCFixup> &Fixups) const {
551 // [Rn, Rm]
552 // {5-3} = Rm
553 // {2-0} = Rn
554 //
555 // [Rn, #imm]
556 // {7-3} = imm5
557 // {2-0} = Rn
558 const MCOperand &MO = MI.getOperand(OpIdx);
559 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
560 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
561 unsigned Rn = getARMRegisterNumbering(MO.getReg());
562 unsigned Imm5 = MO1.getImm();
563 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
564 return (Rm << 3) | (Imm5 << 3) | Rn;
565}
566
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000567/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000568uint32_t ARMMCCodeEmitter::
569getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
570 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000571 // {12-9} = reg
572 // {8} = (U)nsigned (add == '1', sub == '0')
573 // {7-0} = imm8
574 unsigned Reg, Imm8;
Jim Grosbach70933262010-11-04 01:12:30 +0000575 // If The first operand isn't a register, we have a label reference.
576 const MCOperand &MO = MI.getOperand(OpIdx);
577 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000578 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000579 Imm8 = 0;
580
581 assert(MO.isExpr() && "Unexpected machine operand type!");
582 const MCExpr *Expr = MO.getExpr();
583 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_vfp_pcrel_12);
584 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
585
586 ++MCNumCPRelocations;
587 } else
588 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000589
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000590 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
591 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
592 if (ARM_AM::getAM5Op(Imm8) == ARM_AM::add)
593 Binary |= (1 << 8);
594 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000595 return Binary;
596}
597
Jim Grosbach806e80e2010-11-03 23:52:49 +0000598unsigned ARMMCCodeEmitter::
599getSORegOpValue(const MCInst &MI, unsigned OpIdx,
600 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000601 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
602 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
603 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000604 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000605 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000606 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000607 // {6-5} = type
608 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000609 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000610 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000611 // else (imm shift)
612 // {11-7} = imm
613
614 const MCOperand &MO = MI.getOperand(OpIdx);
615 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
616 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
617 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
618
619 // Encode Rm.
620 unsigned Binary = getARMRegisterNumbering(MO.getReg());
621
622 // Encode the shift opcode.
623 unsigned SBits = 0;
624 unsigned Rs = MO1.getReg();
625 if (Rs) {
626 // Set shift operand (bit[7:4]).
627 // LSL - 0001
628 // LSR - 0011
629 // ASR - 0101
630 // ROR - 0111
631 // RRX - 0110 and bit[11:8] clear.
632 switch (SOpc) {
633 default: llvm_unreachable("Unknown shift opc!");
634 case ARM_AM::lsl: SBits = 0x1; break;
635 case ARM_AM::lsr: SBits = 0x3; break;
636 case ARM_AM::asr: SBits = 0x5; break;
637 case ARM_AM::ror: SBits = 0x7; break;
638 case ARM_AM::rrx: SBits = 0x6; break;
639 }
640 } else {
641 // Set shift operand (bit[6:4]).
642 // LSL - 000
643 // LSR - 010
644 // ASR - 100
645 // ROR - 110
646 switch (SOpc) {
647 default: llvm_unreachable("Unknown shift opc!");
648 case ARM_AM::lsl: SBits = 0x0; break;
649 case ARM_AM::lsr: SBits = 0x2; break;
650 case ARM_AM::asr: SBits = 0x4; break;
651 case ARM_AM::ror: SBits = 0x6; break;
652 }
653 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000654
Jim Grosbachef324d72010-10-12 23:53:58 +0000655 Binary |= SBits << 4;
656 if (SOpc == ARM_AM::rrx)
657 return Binary;
658
659 // Encode the shift operation Rs or shift_imm (except rrx).
660 if (Rs) {
661 // Encode Rs bit[11:8].
662 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
663 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
664 }
665
666 // Encode shift_imm bit[11:7].
667 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
668}
669
Jim Grosbach806e80e2010-11-03 23:52:49 +0000670unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000671getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
672 SmallVectorImpl<MCFixup> &Fixups) const {
673 const MCOperand &MO1 = MI.getOperand(OpNum);
674 const MCOperand &MO2 = MI.getOperand(OpNum+1);
675 const MCOperand &MO3 = MI.getOperand(OpNum+2);
676
677 // Encoded as [Rn, Rm, imm].
678 // FIXME: Needs fixup support.
679 unsigned Value = getARMRegisterNumbering(MO1.getReg());
680 Value <<= 4;
681 Value |= getARMRegisterNumbering(MO2.getReg());
682 Value <<= 2;
683 Value |= MO3.getImm();
684
685 return Value;
686}
687
688unsigned ARMMCCodeEmitter::
689getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
690 SmallVectorImpl<MCFixup> &Fixups) const {
691 const MCOperand &MO1 = MI.getOperand(OpNum);
692 const MCOperand &MO2 = MI.getOperand(OpNum+1);
693
694 // FIXME: Needs fixup support.
695 unsigned Value = getARMRegisterNumbering(MO1.getReg());
696
697 // Even though the immediate is 8 bits long, we need 9 bits in order
698 // to represent the (inverse of the) sign bit.
699 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000700 int32_t tmp = (int32_t)MO2.getImm();
701 if (tmp < 0)
702 tmp = abs(tmp);
703 else
704 Value |= 256; // Set the ADD bit
705 Value |= tmp & 255;
706 return Value;
707}
708
709unsigned ARMMCCodeEmitter::
710getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
711 SmallVectorImpl<MCFixup> &Fixups) const {
712 const MCOperand &MO1 = MI.getOperand(OpNum);
713
714 // FIXME: Needs fixup support.
715 unsigned Value = 0;
716 int32_t tmp = (int32_t)MO1.getImm();
717 if (tmp < 0)
718 tmp = abs(tmp);
719 else
720 Value |= 256; // Set the ADD bit
721 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000722 return Value;
723}
724
725unsigned ARMMCCodeEmitter::
726getT2AddrModeImm12OpValue(const MCInst &MI, unsigned OpNum,
727 SmallVectorImpl<MCFixup> &Fixups) const {
728 const MCOperand &MO1 = MI.getOperand(OpNum);
729 const MCOperand &MO2 = MI.getOperand(OpNum+1);
730
731 // FIXME: Needs fixup support.
732 unsigned Value = getARMRegisterNumbering(MO1.getReg());
733 Value <<= 12;
734 Value |= MO2.getImm() & 4095;
735 return Value;
736}
737
738unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000739getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
740 SmallVectorImpl<MCFixup> &Fixups) const {
741 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
742 // shifted. The second is the amount to shift by.
743 //
744 // {3-0} = Rm.
745 // {4} = 0
746 // {6-5} = type
747 // {11-7} = imm
748
749 const MCOperand &MO = MI.getOperand(OpIdx);
750 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
751 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
752
753 // Encode Rm.
754 unsigned Binary = getARMRegisterNumbering(MO.getReg());
755
756 // Encode the shift opcode.
757 unsigned SBits = 0;
758 // Set shift operand (bit[6:4]).
759 // LSL - 000
760 // LSR - 010
761 // ASR - 100
762 // ROR - 110
763 switch (SOpc) {
764 default: llvm_unreachable("Unknown shift opc!");
765 case ARM_AM::lsl: SBits = 0x0; break;
766 case ARM_AM::lsr: SBits = 0x2; break;
767 case ARM_AM::asr: SBits = 0x4; break;
768 case ARM_AM::ror: SBits = 0x6; break;
769 }
770
771 Binary |= SBits << 4;
772 if (SOpc == ARM_AM::rrx)
773 return Binary;
774
775 // Encode shift_imm bit[11:7].
776 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
777}
778
779unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000780getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
781 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000782 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
783 // msb of the mask.
784 const MCOperand &MO = MI.getOperand(Op);
785 uint32_t v = ~MO.getImm();
786 uint32_t lsb = CountTrailingZeros_32(v);
787 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
788 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
789 return lsb | (msb << 5);
790}
791
Jim Grosbach806e80e2010-11-03 23:52:49 +0000792unsigned ARMMCCodeEmitter::
793getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000794 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +0000795 // VLDM/VSTM:
796 // {12-8} = Vd
797 // {7-0} = Number of registers
798 //
799 // LDM/STM:
800 // {15-0} = Bitfield of GPRs.
801 unsigned Reg = MI.getOperand(Op).getReg();
802 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
803 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
804
Bill Wendling5e559a22010-11-09 00:30:18 +0000805 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +0000806
807 if (SPRRegs || DPRRegs) {
808 // VLDM/VSTM
809 unsigned RegNo = getARMRegisterNumbering(Reg);
810 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
811 Binary |= (RegNo & 0x1f) << 8;
812 if (SPRRegs)
813 Binary |= NumRegs;
814 else
815 Binary |= NumRegs * 2;
816 } else {
817 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
818 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
819 Binary |= 1 << RegNo;
820 }
Bill Wendling5e559a22010-11-09 00:30:18 +0000821 }
Bill Wendling6bc105a2010-11-17 00:45:23 +0000822
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000823 return Binary;
824}
825
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000826/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
827/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000828unsigned ARMMCCodeEmitter::
829getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
830 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000831 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000832 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +0000833
Owen Andersond9aa7d32010-11-02 00:05:05 +0000834 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000835 unsigned Align = 0;
836
837 switch (Imm.getImm()) {
838 default: break;
839 case 2:
840 case 4:
841 case 8: Align = 0x01; break;
842 case 16: Align = 0x02; break;
843 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000844 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000845
Owen Andersond9aa7d32010-11-02 00:05:05 +0000846 return RegNo | (Align << 4);
847}
848
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000849/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
850/// alignment operand for use in VLD-dup instructions. This is the same as
851/// getAddrMode6AddressOpValue except for the alignment encoding, which is
852/// different for VLD4-dup.
853unsigned ARMMCCodeEmitter::
854getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
855 SmallVectorImpl<MCFixup> &Fixups) const {
856 const MCOperand &Reg = MI.getOperand(Op);
857 const MCOperand &Imm = MI.getOperand(Op + 1);
858
859 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
860 unsigned Align = 0;
861
862 switch (Imm.getImm()) {
863 default: break;
864 case 2:
865 case 4:
866 case 8: Align = 0x01; break;
867 case 16: Align = 0x03; break;
868 }
869
870 return RegNo | (Align << 4);
871}
872
Jim Grosbach806e80e2010-11-03 23:52:49 +0000873unsigned ARMMCCodeEmitter::
874getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
875 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000876 const MCOperand &MO = MI.getOperand(Op);
877 if (MO.getReg() == 0) return 0x0D;
878 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +0000879}
880
Jim Grosbach568eeed2010-09-17 18:46:17 +0000881void ARMMCCodeEmitter::
882EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000883 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000884 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +0000885 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000886 uint64_t TSFlags = Desc.TSFlags;
887 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000888 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000889 int Size;
890 // Basic size info comes from the TSFlags field.
891 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
892 default: llvm_unreachable("Unexpected instruction size!");
893 case ARMII::Size2Bytes: Size = 2; break;
894 case ARMII::Size4Bytes: Size = 4; break;
895 }
896 EmitConstant(getBinaryCodeForInstr(MI, Fixups), Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +0000897 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +0000898}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000899
Jim Grosbach806e80e2010-11-03 23:52:49 +0000900#include "ARMGenMCCodeEmitter.inc"