blob: c02d85bfd421972944d1452029490c2ba5248fee [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[] = {
Bill Wendling1591b292010-12-10 22:37:19 +000048// This table *must* be in the order that the fixup_* kinds are defined in
49// ARMFixupKinds.h.
50//
51// Name Offset (bits) Size (bits) Flags
52{ "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
53{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
Owen Anderson05018c22010-12-09 20:27:52 +000054 MCFixupKindInfo::FKF_IsAligned},
Bill Wendling1591b292010-12-10 22:37:19 +000055{ "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersone2e0f582010-12-10 22:46:47 +000056{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
57 MCFixupKindInfo::FKF_IsPCRel },
Bill Wendling1591b292010-12-10 22:37:19 +000058{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
59{ "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
60{ "fixup_t2_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
Owen Andersonfe7fac72010-12-09 21:34:47 +000061 MCFixupKindInfo::FKF_IsAligned},
Bill Wendling1591b292010-12-10 22:37:19 +000062{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
63{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
64{ "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
65{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
66{ "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
67{ "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
68{ "fixup_arm_movt_hi16", 0, 16, 0 },
69{ "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000070 };
71
72 if (Kind < FirstTargetFixupKind)
73 return MCCodeEmitter::getFixupKindInfo(Kind);
74
75 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
76 "Invalid kind!");
77 return Infos[Kind - FirstTargetFixupKind];
78 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000079 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
80
Jim Grosbach9af82ba2010-10-07 21:57:55 +000081 // getBinaryCodeForInstr - TableGen'erated function for getting the
82 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000083 unsigned getBinaryCodeForInstr(const MCInst &MI,
84 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000085
86 /// getMachineOpValue - Return binary encoding of operand. If the machine
87 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000088 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
89 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000090
Jason W Kim837caa92010-11-18 23:37:15 +000091 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
92 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
93 SmallVectorImpl<MCFixup> &Fixups) const;
94
Bill Wendling92b5a2e2010-11-03 01:49:29 +000095 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000096 unsigned &Reg, unsigned &Imm,
97 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000098
Jim Grosbach662a8162010-12-06 23:57:07 +000099 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +0000100 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000101 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
102 SmallVectorImpl<MCFixup> &Fixups) const;
103
Bill Wendling09aa3f02010-12-09 00:39:08 +0000104 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
105 /// BLX branch target.
106 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
107 SmallVectorImpl<MCFixup> &Fixups) const;
108
Jim Grosbache2467172010-12-10 18:21:33 +0000109 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
110 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
111 SmallVectorImpl<MCFixup> &Fixups) const;
112
Jim Grosbach01086452010-12-10 17:13:40 +0000113 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
114 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
115 SmallVectorImpl<MCFixup> &Fixups) const;
116
Jim Grosbach027d6e82010-12-09 19:04:53 +0000117 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
118 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000119 SmallVectorImpl<MCFixup> &Fixups) const;
120
Jim Grosbachc466b932010-11-11 18:04:49 +0000121 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
122 /// branch target.
123 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
124 SmallVectorImpl<MCFixup> &Fixups) const;
125
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000126 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
127 /// ADR label target.
128 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
130
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000131 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
132 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000133 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
134 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000135
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000136 /// getTAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
137 uint32_t getTAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
138 SmallVectorImpl<MCFixup> &Fixups) const;
139
Owen Anderson9d63d902010-12-01 19:18:46 +0000140 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
141 /// operand.
142 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
143 SmallVectorImpl<MCFixup> &Fixups) const;
144
145
Jim Grosbach54fea632010-11-09 17:20:53 +0000146 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
147 /// operand as needed by load/store instructions.
148 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
149 SmallVectorImpl<MCFixup> &Fixups) const;
150
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000151 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
152 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
153 SmallVectorImpl<MCFixup> &Fixups) const {
154 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
155 switch (Mode) {
156 default: assert(0 && "Unknown addressing sub-mode!");
157 case ARM_AM::da: return 0;
158 case ARM_AM::ia: return 1;
159 case ARM_AM::db: return 2;
160 case ARM_AM::ib: return 3;
161 }
162 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000163 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
164 ///
165 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
166 switch (ShOpc) {
167 default: llvm_unreachable("Unknown shift opc!");
168 case ARM_AM::no_shift:
169 case ARM_AM::lsl: return 0;
170 case ARM_AM::lsr: return 1;
171 case ARM_AM::asr: return 2;
172 case ARM_AM::ror:
173 case ARM_AM::rrx: return 3;
174 }
175 return 0;
176 }
177
178 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
179 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
180 SmallVectorImpl<MCFixup> &Fixups) const;
181
182 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
183 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
184 SmallVectorImpl<MCFixup> &Fixups) const;
185
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000186 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
187 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
188 SmallVectorImpl<MCFixup> &Fixups) const;
189
Jim Grosbach570a9222010-11-11 01:09:40 +0000190 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
191 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
192 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000193
Jim Grosbachd967cd02010-12-07 21:50:47 +0000194 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
195 /// operand.
196 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
197 SmallVectorImpl<MCFixup> &Fixups) const;
198
Bill Wendling272df512010-12-09 21:49:07 +0000199 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
200 uint32_t getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
201 SmallVectorImpl<MCFixup> &) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000202
Bill Wendlingb8958b02010-12-08 01:57:09 +0000203 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
204 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
205 SmallVectorImpl<MCFixup> &Fixups) const;
206
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000207 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000208 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
209 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000210
Jim Grosbach08bd5492010-10-12 23:00:24 +0000211 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000212 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
213 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000214 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
215 // '1' respectively.
216 return MI.getOperand(Op).getReg() == ARM::CPSR;
217 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000218
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000219 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000220 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
221 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000222 unsigned SoImm = MI.getOperand(Op).getImm();
223 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
224 assert(SoImmVal != -1 && "Not a valid so_imm value!");
225
226 // Encode rotate_imm.
227 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
228 << ARMII::SoRotImmShift;
229
230 // Encode immed_8.
231 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
232 return Binary;
233 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000234
Owen Anderson5de6d842010-11-12 21:12:40 +0000235 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
236 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
237 SmallVectorImpl<MCFixup> &Fixups) const {
238 unsigned SoImm = MI.getOperand(Op).getImm();
239 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
240 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
241 return Encoded;
242 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000243
Owen Anderson75579f72010-11-29 22:44:32 +0000244 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
245 SmallVectorImpl<MCFixup> &Fixups) const;
246 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
247 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000248 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
249 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000250 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
251 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000252
Jim Grosbachef324d72010-10-12 23:53:58 +0000253 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000254 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
255 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000256 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
257 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000258
Jim Grosbach806e80e2010-11-03 23:52:49 +0000259 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
260 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000261 switch (MI.getOperand(Op).getImm()) {
262 default: assert (0 && "Not a valid rot_imm value!");
263 case 0: return 0;
264 case 8: return 1;
265 case 16: return 2;
266 case 24: return 3;
267 }
268 }
269
Jim Grosbach806e80e2010-11-03 23:52:49 +0000270 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
271 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000272 return MI.getOperand(Op).getImm() - 1;
273 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000274
Jim Grosbach806e80e2010-11-03 23:52:49 +0000275 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
276 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000277 return 64 - MI.getOperand(Op).getImm();
278 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000279
Jim Grosbach806e80e2010-11-03 23:52:49 +0000280 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
281 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000282
Jim Grosbach806e80e2010-11-03 23:52:49 +0000283 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
284 SmallVectorImpl<MCFixup> &Fixups) const;
285 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
286 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000287 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
288 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000289 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
290 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000291
Owen Andersonc7139a62010-11-11 19:07:48 +0000292 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
293 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000294 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000295 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000296 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000297 unsigned EncodedValue) const;
298
299 unsigned VFPThumb2PostEncoder(const MCInst &MI,
300 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000301
Jim Grosbach70933262010-11-04 01:12:30 +0000302 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000303 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000304 }
305
Jim Grosbach70933262010-11-04 01:12:30 +0000306 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000307 // Output the constant in little endian byte order.
308 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000309 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000310 Val >>= 8;
311 }
312 }
313
Jim Grosbach568eeed2010-09-17 18:46:17 +0000314 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
315 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000316};
317
318} // end anonymous namespace
319
Bill Wendling0800ce72010-11-02 22:53:11 +0000320MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
321 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000322 return new ARMMCCodeEmitter(TM, Ctx);
323}
324
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000325/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
326/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000327/// Thumb2 mode.
328unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
329 unsigned EncodedValue) const {
330 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
331 if (Subtarget.isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000332 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000333 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
334 // set to 1111.
335 unsigned Bit24 = EncodedValue & 0x01000000;
336 unsigned Bit28 = Bit24 << 4;
337 EncodedValue &= 0xEFFFFFFF;
338 EncodedValue |= Bit28;
339 EncodedValue |= 0x0F000000;
340 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000341
Owen Andersonc7139a62010-11-11 19:07:48 +0000342 return EncodedValue;
343}
344
Owen Anderson57dac882010-11-11 21:36:43 +0000345/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000346/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000347/// Thumb2 mode.
348unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
349 unsigned EncodedValue) const {
350 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
351 if (Subtarget.isThumb2()) {
352 EncodedValue &= 0xF0FFFFFF;
353 EncodedValue |= 0x09000000;
354 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000355
Owen Anderson57dac882010-11-11 21:36:43 +0000356 return EncodedValue;
357}
358
Owen Anderson8f143912010-11-11 23:12:55 +0000359/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000360/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000361/// Thumb2 mode.
362unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
363 unsigned EncodedValue) const {
364 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
365 if (Subtarget.isThumb2()) {
366 EncodedValue &= 0x00FFFFFF;
367 EncodedValue |= 0xEE000000;
368 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000369
Owen Anderson8f143912010-11-11 23:12:55 +0000370 return EncodedValue;
371}
372
Bill Wendlingcf590262010-12-01 21:54:50 +0000373/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
374/// them to their Thumb2 form if we are currently in Thumb2 mode.
375unsigned ARMMCCodeEmitter::
376VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
377 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
378 EncodedValue &= 0x0FFFFFFF;
379 EncodedValue |= 0xE0000000;
380 }
381 return EncodedValue;
382}
Owen Anderson57dac882010-11-11 21:36:43 +0000383
Jim Grosbach56ac9072010-10-08 21:45:55 +0000384/// getMachineOpValue - Return binary encoding of operand. If the machine
385/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000386unsigned ARMMCCodeEmitter::
387getMachineOpValue(const MCInst &MI, const MCOperand &MO,
388 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000389 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000390 unsigned Reg = MO.getReg();
391 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000392
Jim Grosbachb0708d22010-11-30 23:51:41 +0000393 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000394 switch (Reg) {
395 default:
396 return RegNo;
397 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
398 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
399 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
400 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
401 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000402 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000403 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000404 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000405 } else if (MO.isFPImm()) {
406 return static_cast<unsigned>(APFloat(MO.getFPImm())
407 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000408 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000409
Jim Grosbach817c1a62010-11-19 00:27:09 +0000410 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000411 return 0;
412}
413
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000414/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000415bool ARMMCCodeEmitter::
416EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
417 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000418 const MCOperand &MO = MI.getOperand(OpIdx);
419 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000420
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000421 Reg = getARMRegisterNumbering(MO.getReg());
422
423 int32_t SImm = MO1.getImm();
424 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000425
Jim Grosbachab682a22010-10-28 18:34:10 +0000426 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000427 if (SImm == INT32_MIN)
428 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000429
Jim Grosbachab682a22010-10-28 18:34:10 +0000430 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000431 if (SImm < 0) {
432 SImm = -SImm;
433 isAdd = false;
434 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000435
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000436 Imm = SImm;
437 return isAdd;
438}
439
Bill Wendlingdff2f712010-12-08 23:01:43 +0000440/// getBranchTargetOpValue - Helper function to get the branch target operand,
441/// which is either an immediate or requires a fixup.
442static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
443 unsigned FixupKind,
444 SmallVectorImpl<MCFixup> &Fixups) {
445 const MCOperand &MO = MI.getOperand(OpIdx);
446
447 // If the destination is an immediate, we have nothing to do.
448 if (MO.isImm()) return MO.getImm();
449 assert(MO.isExpr() && "Unexpected branch target type!");
450 const MCExpr *Expr = MO.getExpr();
451 MCFixupKind Kind = MCFixupKind(FixupKind);
452 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
453
454 // All of the information is in the fixup.
455 return 0;
456}
457
458/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000459uint32_t ARMMCCodeEmitter::
460getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
461 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000462 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000463}
464
Bill Wendling09aa3f02010-12-09 00:39:08 +0000465/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
466/// BLX branch target.
467uint32_t ARMMCCodeEmitter::
468getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
469 SmallVectorImpl<MCFixup> &Fixups) const {
470 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
471}
472
Jim Grosbache2467172010-12-10 18:21:33 +0000473/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
474uint32_t ARMMCCodeEmitter::
475getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
476 SmallVectorImpl<MCFixup> &Fixups) const {
477 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
478}
479
Jim Grosbach01086452010-12-10 17:13:40 +0000480/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
481uint32_t ARMMCCodeEmitter::
482getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000483 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000484 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
485}
486
Jim Grosbach027d6e82010-12-09 19:04:53 +0000487/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000488uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000489getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000490 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000491 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000492}
493
494/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
495/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000496uint32_t ARMMCCodeEmitter::
497getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000498 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfb20d892010-12-09 00:27:41 +0000499 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
500 if (Subtarget.isThumb2())
501 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_branch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000502 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000503}
504
Bill Wendlingdff2f712010-12-08 23:01:43 +0000505/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
506/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000507uint32_t ARMMCCodeEmitter::
508getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
509 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000510 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
511 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
512 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000513}
514
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000515/// getTAddrModeRegRegOpValue - Return encoding info for 'reg + reg' operand.
516uint32_t ARMMCCodeEmitter::
517getTAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
518 SmallVectorImpl<MCFixup> &Fixups) const {
519 const MCOperand &MO1 = MI.getOperand(OpIdx);
520 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
521 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
522 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
523 return (Rm << 3) | Rn;
524}
525
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000526/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000527uint32_t ARMMCCodeEmitter::
528getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
529 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000530 // {17-13} = reg
531 // {12} = (U)nsigned (add == '1', sub == '0')
532 // {11-0} = imm12
533 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000534 bool isAdd = true;
535 // If The first operand isn't a register, we have a label reference.
536 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000537 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
538 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000539 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000540 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000541 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000542
Owen Andersoneb6779c2010-12-07 00:45:21 +0000543 const MCExpr *Expr = 0;
544 if (!MO.isReg())
545 Expr = MO.getExpr();
546 else
547 Expr = MO2.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000548
Owen Andersond7b3f582010-12-09 01:51:07 +0000549 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
550 MCFixupKind Kind;
551 if (Subtarget.isThumb2())
552 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
553 else
554 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000555 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
556
557 ++MCNumCPRelocations;
558 } else
559 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000560
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000561 uint32_t Binary = Imm12 & 0xfff;
562 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000563 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000564 Binary |= (1 << 12);
565 Binary |= (Reg << 13);
566 return Binary;
567}
568
Owen Anderson9d63d902010-12-01 19:18:46 +0000569/// getT2AddrModeImm8s4OpValue - Return encoding info for
570/// 'reg +/- imm8<<2' operand.
571uint32_t ARMMCCodeEmitter::
572getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
573 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000574 // {12-9} = reg
575 // {8} = (U)nsigned (add == '1', sub == '0')
576 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000577 unsigned Reg, Imm8;
578 bool isAdd = true;
579 // If The first operand isn't a register, we have a label reference.
580 const MCOperand &MO = MI.getOperand(OpIdx);
581 if (!MO.isReg()) {
582 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
583 Imm8 = 0;
584 isAdd = false ; // 'U' bit is set as part of the fixup.
585
586 assert(MO.isExpr() && "Unexpected machine operand type!");
587 const MCExpr *Expr = MO.getExpr();
588 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
589 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
590
591 ++MCNumCPRelocations;
592 } else
593 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
594
595 uint32_t Binary = (Imm8 >> 2) & 0xff;
596 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
597 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000598 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000599 Binary |= (Reg << 9);
600 return Binary;
601}
602
Jim Grosbach54fea632010-11-09 17:20:53 +0000603uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000604getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
605 SmallVectorImpl<MCFixup> &Fixups) const {
606 // {20-16} = imm{15-12}
607 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000608 const MCOperand &MO = MI.getOperand(OpIdx);
Jason W Kim837caa92010-11-18 23:37:15 +0000609 if (MO.isImm()) {
610 return static_cast<unsigned>(MO.getImm());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000611 } else if (const MCSymbolRefExpr *Expr =
Jason W Kim837caa92010-11-18 23:37:15 +0000612 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
613 MCFixupKind Kind;
614 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000615 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000616 case MCSymbolRefExpr::VK_ARM_HI16:
617 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
618 break;
619 case MCSymbolRefExpr::VK_ARM_LO16:
620 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
621 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000622 }
623 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
624 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000625 };
626 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000627 return 0;
628}
629
630uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000631getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
632 SmallVectorImpl<MCFixup> &Fixups) const {
633 const MCOperand &MO = MI.getOperand(OpIdx);
634 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
635 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
636 unsigned Rn = getARMRegisterNumbering(MO.getReg());
637 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000638 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
639 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000640 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
641 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000642
643 // {16-13} = Rn
644 // {12} = isAdd
645 // {11-0} = shifter
646 // {3-0} = Rm
647 // {4} = 0
648 // {6-5} = type
649 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000650 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000651 Binary |= Rn << 13;
652 Binary |= SBits << 5;
653 Binary |= ShImm << 7;
654 if (isAdd)
655 Binary |= 1 << 12;
656 return Binary;
657}
658
Jim Grosbach570a9222010-11-11 01:09:40 +0000659uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000660getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
661 SmallVectorImpl<MCFixup> &Fixups) const {
662 // {17-14} Rn
663 // {13} 1 == imm12, 0 == Rm
664 // {12} isAdd
665 // {11-0} imm12/Rm
666 const MCOperand &MO = MI.getOperand(OpIdx);
667 unsigned Rn = getARMRegisterNumbering(MO.getReg());
668 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
669 Binary |= Rn << 14;
670 return Binary;
671}
672
673uint32_t ARMMCCodeEmitter::
674getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
675 SmallVectorImpl<MCFixup> &Fixups) const {
676 // {13} 1 == imm12, 0 == Rm
677 // {12} isAdd
678 // {11-0} imm12/Rm
679 const MCOperand &MO = MI.getOperand(OpIdx);
680 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
681 unsigned Imm = MO1.getImm();
682 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
683 bool isReg = MO.getReg() != 0;
684 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
685 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
686 if (isReg) {
687 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
688 Binary <<= 7; // Shift amount is bits [11:7]
689 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
690 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
691 }
692 return Binary | (isAdd << 12) | (isReg << 13);
693}
694
695uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000696getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
697 SmallVectorImpl<MCFixup> &Fixups) const {
698 // {9} 1 == imm8, 0 == Rm
699 // {8} isAdd
700 // {7-4} imm7_4/zero
701 // {3-0} imm3_0/Rm
702 const MCOperand &MO = MI.getOperand(OpIdx);
703 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
704 unsigned Imm = MO1.getImm();
705 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
706 bool isImm = MO.getReg() == 0;
707 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
708 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
709 if (!isImm)
710 Imm8 = getARMRegisterNumbering(MO.getReg());
711 return Imm8 | (isAdd << 8) | (isImm << 9);
712}
713
714uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000715getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
716 SmallVectorImpl<MCFixup> &Fixups) const {
717 // {13} 1 == imm8, 0 == Rm
718 // {12-9} Rn
719 // {8} isAdd
720 // {7-4} imm7_4/zero
721 // {3-0} imm3_0/Rm
722 const MCOperand &MO = MI.getOperand(OpIdx);
723 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
724 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
725 unsigned Rn = getARMRegisterNumbering(MO.getReg());
726 unsigned Imm = MO2.getImm();
727 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
728 bool isImm = MO1.getReg() == 0;
729 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
730 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
731 if (!isImm)
732 Imm8 = getARMRegisterNumbering(MO1.getReg());
733 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
734}
735
Bill Wendlingb8958b02010-12-08 01:57:09 +0000736/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000737uint32_t ARMMCCodeEmitter::
738getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
739 SmallVectorImpl<MCFixup> &Fixups) const {
740 // [SP, #imm]
741 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000742 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000743#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
744 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
745 "Unexpected base register!");
746#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000747 // The immediate is already shifted for the implicit zeroes, so no change
748 // here.
749 return MO1.getImm() & 0xff;
750}
751
Bill Wendling1fd374e2010-11-30 22:57:21 +0000752/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000753uint32_t ARMMCCodeEmitter::
754getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
755 SmallVectorImpl<MCFixup> &) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000756 // [Rn, Rm]
757 // {5-3} = Rm
758 // {2-0} = Rn
759 //
760 // [Rn, #imm]
761 // {7-3} = imm5
762 // {2-0} = Rn
763 const MCOperand &MO = MI.getOperand(OpIdx);
764 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
765 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
766 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling272df512010-12-09 21:49:07 +0000767 unsigned Imm5 = MO1.getImm();
Bill Wendling0bdf0c02010-12-03 00:53:22 +0000768
769 if (MO2.getReg() != 0)
770 // Is an immediate.
771 Imm5 = getARMRegisterNumbering(MO2.getReg());
772
Bill Wendling272df512010-12-09 21:49:07 +0000773 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000774}
775
Bill Wendlingb8958b02010-12-08 01:57:09 +0000776/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
777uint32_t ARMMCCodeEmitter::
778getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
779 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000780 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000781}
782
Jim Grosbach5177f792010-12-01 21:09:40 +0000783/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000784uint32_t ARMMCCodeEmitter::
785getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
786 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000787 // {12-9} = reg
788 // {8} = (U)nsigned (add == '1', sub == '0')
789 // {7-0} = imm8
790 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000791 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000792 // If The first operand isn't a register, we have a label reference.
793 const MCOperand &MO = MI.getOperand(OpIdx);
794 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000795 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000796 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000797 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000798
799 assert(MO.isExpr() && "Unexpected machine operand type!");
800 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000801 MCFixupKind Kind;
802 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
803 if (Subtarget.isThumb2())
804 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
805 else
806 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000807 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
808
809 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000810 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000811 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000812 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
813 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000814
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000815 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
816 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000817 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000818 Binary |= (1 << 8);
819 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000820 return Binary;
821}
822
Jim Grosbach806e80e2010-11-03 23:52:49 +0000823unsigned ARMMCCodeEmitter::
824getSORegOpValue(const MCInst &MI, unsigned OpIdx,
825 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000826 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
827 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
828 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000829 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000830 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000831 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000832 // {6-5} = type
833 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000834 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000835 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000836 // else (imm shift)
837 // {11-7} = imm
838
839 const MCOperand &MO = MI.getOperand(OpIdx);
840 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
841 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
842 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
843
844 // Encode Rm.
845 unsigned Binary = getARMRegisterNumbering(MO.getReg());
846
847 // Encode the shift opcode.
848 unsigned SBits = 0;
849 unsigned Rs = MO1.getReg();
850 if (Rs) {
851 // Set shift operand (bit[7:4]).
852 // LSL - 0001
853 // LSR - 0011
854 // ASR - 0101
855 // ROR - 0111
856 // RRX - 0110 and bit[11:8] clear.
857 switch (SOpc) {
858 default: llvm_unreachable("Unknown shift opc!");
859 case ARM_AM::lsl: SBits = 0x1; break;
860 case ARM_AM::lsr: SBits = 0x3; break;
861 case ARM_AM::asr: SBits = 0x5; break;
862 case ARM_AM::ror: SBits = 0x7; break;
863 case ARM_AM::rrx: SBits = 0x6; break;
864 }
865 } else {
866 // Set shift operand (bit[6:4]).
867 // LSL - 000
868 // LSR - 010
869 // ASR - 100
870 // ROR - 110
871 switch (SOpc) {
872 default: llvm_unreachable("Unknown shift opc!");
873 case ARM_AM::lsl: SBits = 0x0; break;
874 case ARM_AM::lsr: SBits = 0x2; break;
875 case ARM_AM::asr: SBits = 0x4; break;
876 case ARM_AM::ror: SBits = 0x6; break;
877 }
878 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000879
Jim Grosbachef324d72010-10-12 23:53:58 +0000880 Binary |= SBits << 4;
881 if (SOpc == ARM_AM::rrx)
882 return Binary;
883
884 // Encode the shift operation Rs or shift_imm (except rrx).
885 if (Rs) {
886 // Encode Rs bit[11:8].
887 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
888 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
889 }
890
891 // Encode shift_imm bit[11:7].
892 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
893}
894
Jim Grosbach806e80e2010-11-03 23:52:49 +0000895unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000896getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
897 SmallVectorImpl<MCFixup> &Fixups) const {
898 const MCOperand &MO1 = MI.getOperand(OpNum);
899 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000900 const MCOperand &MO3 = MI.getOperand(OpNum+2);
901
Owen Anderson75579f72010-11-29 22:44:32 +0000902 // Encoded as [Rn, Rm, imm].
903 // FIXME: Needs fixup support.
904 unsigned Value = getARMRegisterNumbering(MO1.getReg());
905 Value <<= 4;
906 Value |= getARMRegisterNumbering(MO2.getReg());
907 Value <<= 2;
908 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000909
Owen Anderson75579f72010-11-29 22:44:32 +0000910 return Value;
911}
912
913unsigned ARMMCCodeEmitter::
914getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
915 SmallVectorImpl<MCFixup> &Fixups) const {
916 const MCOperand &MO1 = MI.getOperand(OpNum);
917 const MCOperand &MO2 = MI.getOperand(OpNum+1);
918
919 // FIXME: Needs fixup support.
920 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000921
Owen Anderson75579f72010-11-29 22:44:32 +0000922 // Even though the immediate is 8 bits long, we need 9 bits in order
923 // to represent the (inverse of the) sign bit.
924 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000925 int32_t tmp = (int32_t)MO2.getImm();
926 if (tmp < 0)
927 tmp = abs(tmp);
928 else
929 Value |= 256; // Set the ADD bit
930 Value |= tmp & 255;
931 return Value;
932}
933
934unsigned ARMMCCodeEmitter::
935getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
936 SmallVectorImpl<MCFixup> &Fixups) const {
937 const MCOperand &MO1 = MI.getOperand(OpNum);
938
939 // FIXME: Needs fixup support.
940 unsigned Value = 0;
941 int32_t tmp = (int32_t)MO1.getImm();
942 if (tmp < 0)
943 tmp = abs(tmp);
944 else
945 Value |= 256; // Set the ADD bit
946 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000947 return Value;
948}
949
950unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000951getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
952 SmallVectorImpl<MCFixup> &Fixups) const {
953 const MCOperand &MO1 = MI.getOperand(OpNum);
954
955 // FIXME: Needs fixup support.
956 unsigned Value = 0;
957 int32_t tmp = (int32_t)MO1.getImm();
958 if (tmp < 0)
959 tmp = abs(tmp);
960 else
961 Value |= 4096; // Set the ADD bit
962 Value |= tmp & 4095;
963 return Value;
964}
965
966unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000967getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
968 SmallVectorImpl<MCFixup> &Fixups) const {
969 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
970 // shifted. The second is the amount to shift by.
971 //
972 // {3-0} = Rm.
973 // {4} = 0
974 // {6-5} = type
975 // {11-7} = imm
976
977 const MCOperand &MO = MI.getOperand(OpIdx);
978 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
979 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
980
981 // Encode Rm.
982 unsigned Binary = getARMRegisterNumbering(MO.getReg());
983
984 // Encode the shift opcode.
985 unsigned SBits = 0;
986 // Set shift operand (bit[6:4]).
987 // LSL - 000
988 // LSR - 010
989 // ASR - 100
990 // ROR - 110
991 switch (SOpc) {
992 default: llvm_unreachable("Unknown shift opc!");
993 case ARM_AM::lsl: SBits = 0x0; break;
994 case ARM_AM::lsr: SBits = 0x2; break;
995 case ARM_AM::asr: SBits = 0x4; break;
996 case ARM_AM::ror: SBits = 0x6; break;
997 }
998
999 Binary |= SBits << 4;
1000 if (SOpc == ARM_AM::rrx)
1001 return Binary;
1002
1003 // Encode shift_imm bit[11:7].
1004 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1005}
1006
1007unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001008getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1009 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001010 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1011 // msb of the mask.
1012 const MCOperand &MO = MI.getOperand(Op);
1013 uint32_t v = ~MO.getImm();
1014 uint32_t lsb = CountTrailingZeros_32(v);
1015 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1016 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1017 return lsb | (msb << 5);
1018}
1019
Jim Grosbach806e80e2010-11-03 23:52:49 +00001020unsigned ARMMCCodeEmitter::
1021getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001022 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001023 // VLDM/VSTM:
1024 // {12-8} = Vd
1025 // {7-0} = Number of registers
1026 //
1027 // LDM/STM:
1028 // {15-0} = Bitfield of GPRs.
1029 unsigned Reg = MI.getOperand(Op).getReg();
1030 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1031 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1032
Bill Wendling5e559a22010-11-09 00:30:18 +00001033 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001034
1035 if (SPRRegs || DPRRegs) {
1036 // VLDM/VSTM
1037 unsigned RegNo = getARMRegisterNumbering(Reg);
1038 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1039 Binary |= (RegNo & 0x1f) << 8;
1040 if (SPRRegs)
1041 Binary |= NumRegs;
1042 else
1043 Binary |= NumRegs * 2;
1044 } else {
1045 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1046 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1047 Binary |= 1 << RegNo;
1048 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001049 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001050
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001051 return Binary;
1052}
1053
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001054/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1055/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001056unsigned ARMMCCodeEmitter::
1057getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1058 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001059 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001060 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001061
Owen Andersond9aa7d32010-11-02 00:05:05 +00001062 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001063 unsigned Align = 0;
1064
1065 switch (Imm.getImm()) {
1066 default: break;
1067 case 2:
1068 case 4:
1069 case 8: Align = 0x01; break;
1070 case 16: Align = 0x02; break;
1071 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001072 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001073
Owen Andersond9aa7d32010-11-02 00:05:05 +00001074 return RegNo | (Align << 4);
1075}
1076
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001077/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1078/// alignment operand for use in VLD-dup instructions. This is the same as
1079/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1080/// different for VLD4-dup.
1081unsigned ARMMCCodeEmitter::
1082getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1083 SmallVectorImpl<MCFixup> &Fixups) const {
1084 const MCOperand &Reg = MI.getOperand(Op);
1085 const MCOperand &Imm = MI.getOperand(Op + 1);
1086
1087 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1088 unsigned Align = 0;
1089
1090 switch (Imm.getImm()) {
1091 default: break;
1092 case 2:
1093 case 4:
1094 case 8: Align = 0x01; break;
1095 case 16: Align = 0x03; break;
1096 }
1097
1098 return RegNo | (Align << 4);
1099}
1100
Jim Grosbach806e80e2010-11-03 23:52:49 +00001101unsigned ARMMCCodeEmitter::
1102getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1103 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001104 const MCOperand &MO = MI.getOperand(Op);
1105 if (MO.getReg() == 0) return 0x0D;
1106 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001107}
1108
Jim Grosbach568eeed2010-09-17 18:46:17 +00001109void ARMMCCodeEmitter::
1110EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001111 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001112 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001113 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001114 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001115 uint64_t TSFlags = Desc.TSFlags;
1116 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001117 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001118 int Size;
1119 // Basic size info comes from the TSFlags field.
1120 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1121 default: llvm_unreachable("Unexpected instruction size!");
1122 case ARMII::Size2Bytes: Size = 2; break;
1123 case ARMII::Size4Bytes: Size = 4; break;
1124 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001125 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1126 // Thumb 32-bit wide instructions need to be have the high order halfword
1127 // emitted first.
1128 if (Subtarget.isThumb() && Size == 4) {
1129 EmitConstant(Binary >> 16, 2, OS);
1130 EmitConstant(Binary & 0xffff, 2, OS);
1131 } else
1132 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001133 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001134}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001135
Jim Grosbach806e80e2010-11-03 23:52:49 +00001136#include "ARMGenMCCodeEmitter.inc"