blob: 73b0874491d38faf48510440eb958340ddeb36bc [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 },
56{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
57{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
58{ "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
59{ "fixup_t2_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
Owen Andersonfe7fac72010-12-09 21:34:47 +000060 MCFixupKindInfo::FKF_IsAligned},
Bill Wendling1591b292010-12-10 22:37:19 +000061{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
62{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
63{ "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
64{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
65{ "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
66{ "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
67{ "fixup_arm_movt_hi16", 0, 16, 0 },
68{ "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000069 };
70
71 if (Kind < FirstTargetFixupKind)
72 return MCCodeEmitter::getFixupKindInfo(Kind);
73
74 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
75 "Invalid kind!");
76 return Infos[Kind - FirstTargetFixupKind];
77 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000078 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
79
Jim Grosbach9af82ba2010-10-07 21:57:55 +000080 // getBinaryCodeForInstr - TableGen'erated function for getting the
81 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000082 unsigned getBinaryCodeForInstr(const MCInst &MI,
83 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000084
85 /// getMachineOpValue - Return binary encoding of operand. If the machine
86 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000087 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
88 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000089
Jason W Kim837caa92010-11-18 23:37:15 +000090 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
91 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
92 SmallVectorImpl<MCFixup> &Fixups) const;
93
Bill Wendling92b5a2e2010-11-03 01:49:29 +000094 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000095 unsigned &Reg, unsigned &Imm,
96 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000097
Jim Grosbach662a8162010-12-06 23:57:07 +000098 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +000099 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000100 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
101 SmallVectorImpl<MCFixup> &Fixups) const;
102
Bill Wendling09aa3f02010-12-09 00:39:08 +0000103 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
104 /// BLX branch target.
105 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
106 SmallVectorImpl<MCFixup> &Fixups) const;
107
Jim Grosbache2467172010-12-10 18:21:33 +0000108 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
109 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
110 SmallVectorImpl<MCFixup> &Fixups) const;
111
Jim Grosbach01086452010-12-10 17:13:40 +0000112 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
113 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
114 SmallVectorImpl<MCFixup> &Fixups) const;
115
Jim Grosbach027d6e82010-12-09 19:04:53 +0000116 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
117 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000118 SmallVectorImpl<MCFixup> &Fixups) const;
119
Jim Grosbachc466b932010-11-11 18:04:49 +0000120 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
121 /// branch target.
122 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
123 SmallVectorImpl<MCFixup> &Fixups) const;
124
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000125 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
126 /// ADR label target.
127 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
128 SmallVectorImpl<MCFixup> &Fixups) const;
129
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000130 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
131 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000132 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
133 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000134
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000135 /// getTAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
136 uint32_t getTAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
137 SmallVectorImpl<MCFixup> &Fixups) const;
138
Owen Anderson9d63d902010-12-01 19:18:46 +0000139 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
140 /// operand.
141 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
142 SmallVectorImpl<MCFixup> &Fixups) const;
143
144
Jim Grosbach54fea632010-11-09 17:20:53 +0000145 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
146 /// operand as needed by load/store instructions.
147 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
148 SmallVectorImpl<MCFixup> &Fixups) const;
149
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000150 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
151 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
152 SmallVectorImpl<MCFixup> &Fixups) const {
153 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
154 switch (Mode) {
155 default: assert(0 && "Unknown addressing sub-mode!");
156 case ARM_AM::da: return 0;
157 case ARM_AM::ia: return 1;
158 case ARM_AM::db: return 2;
159 case ARM_AM::ib: return 3;
160 }
161 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000162 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
163 ///
164 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
165 switch (ShOpc) {
166 default: llvm_unreachable("Unknown shift opc!");
167 case ARM_AM::no_shift:
168 case ARM_AM::lsl: return 0;
169 case ARM_AM::lsr: return 1;
170 case ARM_AM::asr: return 2;
171 case ARM_AM::ror:
172 case ARM_AM::rrx: return 3;
173 }
174 return 0;
175 }
176
177 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
178 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
179 SmallVectorImpl<MCFixup> &Fixups) const;
180
181 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
182 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
183 SmallVectorImpl<MCFixup> &Fixups) const;
184
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000185 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
186 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
187 SmallVectorImpl<MCFixup> &Fixups) const;
188
Jim Grosbach570a9222010-11-11 01:09:40 +0000189 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
190 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
191 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000192
Jim Grosbachd967cd02010-12-07 21:50:47 +0000193 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
194 /// operand.
195 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
196 SmallVectorImpl<MCFixup> &Fixups) const;
197
Bill Wendling272df512010-12-09 21:49:07 +0000198 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
199 uint32_t getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
200 SmallVectorImpl<MCFixup> &) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000201
Bill Wendlingb8958b02010-12-08 01:57:09 +0000202 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
203 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
204 SmallVectorImpl<MCFixup> &Fixups) const;
205
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000206 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000207 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
208 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000209
Jim Grosbach08bd5492010-10-12 23:00:24 +0000210 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000211 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
212 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000213 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
214 // '1' respectively.
215 return MI.getOperand(Op).getReg() == ARM::CPSR;
216 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000217
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000218 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000219 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
220 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000221 unsigned SoImm = MI.getOperand(Op).getImm();
222 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
223 assert(SoImmVal != -1 && "Not a valid so_imm value!");
224
225 // Encode rotate_imm.
226 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
227 << ARMII::SoRotImmShift;
228
229 // Encode immed_8.
230 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
231 return Binary;
232 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000233
Owen Anderson5de6d842010-11-12 21:12:40 +0000234 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
235 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
236 SmallVectorImpl<MCFixup> &Fixups) const {
237 unsigned SoImm = MI.getOperand(Op).getImm();
238 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
239 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
240 return Encoded;
241 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000242
Owen Anderson75579f72010-11-29 22:44:32 +0000243 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
244 SmallVectorImpl<MCFixup> &Fixups) const;
245 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
246 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000247 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
248 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000249 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
250 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000251
Jim Grosbachef324d72010-10-12 23:53:58 +0000252 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000253 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
254 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000255 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
256 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000257
Jim Grosbach806e80e2010-11-03 23:52:49 +0000258 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
259 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000260 switch (MI.getOperand(Op).getImm()) {
261 default: assert (0 && "Not a valid rot_imm value!");
262 case 0: return 0;
263 case 8: return 1;
264 case 16: return 2;
265 case 24: return 3;
266 }
267 }
268
Jim Grosbach806e80e2010-11-03 23:52:49 +0000269 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
270 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000271 return MI.getOperand(Op).getImm() - 1;
272 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000273
Jim Grosbach806e80e2010-11-03 23:52:49 +0000274 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
275 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000276 return 64 - MI.getOperand(Op).getImm();
277 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000278
Jim Grosbach806e80e2010-11-03 23:52:49 +0000279 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
280 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000281
Jim Grosbach806e80e2010-11-03 23:52:49 +0000282 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
283 SmallVectorImpl<MCFixup> &Fixups) const;
284 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
285 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000286 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
287 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000288 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
289 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000290
Owen Andersonc7139a62010-11-11 19:07:48 +0000291 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
292 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000293 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000294 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000295 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000296 unsigned EncodedValue) const;
297
298 unsigned VFPThumb2PostEncoder(const MCInst &MI,
299 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000300
Jim Grosbach70933262010-11-04 01:12:30 +0000301 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000302 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000303 }
304
Jim Grosbach70933262010-11-04 01:12:30 +0000305 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000306 // Output the constant in little endian byte order.
307 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000308 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000309 Val >>= 8;
310 }
311 }
312
Jim Grosbach568eeed2010-09-17 18:46:17 +0000313 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
314 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000315};
316
317} // end anonymous namespace
318
Bill Wendling0800ce72010-11-02 22:53:11 +0000319MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
320 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000321 return new ARMMCCodeEmitter(TM, Ctx);
322}
323
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000324/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
325/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000326/// Thumb2 mode.
327unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
328 unsigned EncodedValue) const {
329 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
330 if (Subtarget.isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000331 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000332 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
333 // set to 1111.
334 unsigned Bit24 = EncodedValue & 0x01000000;
335 unsigned Bit28 = Bit24 << 4;
336 EncodedValue &= 0xEFFFFFFF;
337 EncodedValue |= Bit28;
338 EncodedValue |= 0x0F000000;
339 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000340
Owen Andersonc7139a62010-11-11 19:07:48 +0000341 return EncodedValue;
342}
343
Owen Anderson57dac882010-11-11 21:36:43 +0000344/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000345/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000346/// Thumb2 mode.
347unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
348 unsigned EncodedValue) const {
349 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
350 if (Subtarget.isThumb2()) {
351 EncodedValue &= 0xF0FFFFFF;
352 EncodedValue |= 0x09000000;
353 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000354
Owen Anderson57dac882010-11-11 21:36:43 +0000355 return EncodedValue;
356}
357
Owen Anderson8f143912010-11-11 23:12:55 +0000358/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000359/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000360/// Thumb2 mode.
361unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
362 unsigned EncodedValue) const {
363 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
364 if (Subtarget.isThumb2()) {
365 EncodedValue &= 0x00FFFFFF;
366 EncodedValue |= 0xEE000000;
367 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000368
Owen Anderson8f143912010-11-11 23:12:55 +0000369 return EncodedValue;
370}
371
Bill Wendlingcf590262010-12-01 21:54:50 +0000372/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
373/// them to their Thumb2 form if we are currently in Thumb2 mode.
374unsigned ARMMCCodeEmitter::
375VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
376 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
377 EncodedValue &= 0x0FFFFFFF;
378 EncodedValue |= 0xE0000000;
379 }
380 return EncodedValue;
381}
Owen Anderson57dac882010-11-11 21:36:43 +0000382
Jim Grosbach56ac9072010-10-08 21:45:55 +0000383/// getMachineOpValue - Return binary encoding of operand. If the machine
384/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000385unsigned ARMMCCodeEmitter::
386getMachineOpValue(const MCInst &MI, const MCOperand &MO,
387 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000388 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000389 unsigned Reg = MO.getReg();
390 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000391
Jim Grosbachb0708d22010-11-30 23:51:41 +0000392 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000393 switch (Reg) {
394 default:
395 return RegNo;
396 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
397 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
398 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
399 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
400 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000401 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000402 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000403 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000404 } else if (MO.isFPImm()) {
405 return static_cast<unsigned>(APFloat(MO.getFPImm())
406 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000407 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000408
Jim Grosbach817c1a62010-11-19 00:27:09 +0000409 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000410 return 0;
411}
412
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000413/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000414bool ARMMCCodeEmitter::
415EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
416 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000417 const MCOperand &MO = MI.getOperand(OpIdx);
418 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000419
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000420 Reg = getARMRegisterNumbering(MO.getReg());
421
422 int32_t SImm = MO1.getImm();
423 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000424
Jim Grosbachab682a22010-10-28 18:34:10 +0000425 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000426 if (SImm == INT32_MIN)
427 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000428
Jim Grosbachab682a22010-10-28 18:34:10 +0000429 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000430 if (SImm < 0) {
431 SImm = -SImm;
432 isAdd = false;
433 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000434
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000435 Imm = SImm;
436 return isAdd;
437}
438
Bill Wendlingdff2f712010-12-08 23:01:43 +0000439/// getBranchTargetOpValue - Helper function to get the branch target operand,
440/// which is either an immediate or requires a fixup.
441static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
442 unsigned FixupKind,
443 SmallVectorImpl<MCFixup> &Fixups) {
444 const MCOperand &MO = MI.getOperand(OpIdx);
445
446 // If the destination is an immediate, we have nothing to do.
447 if (MO.isImm()) return MO.getImm();
448 assert(MO.isExpr() && "Unexpected branch target type!");
449 const MCExpr *Expr = MO.getExpr();
450 MCFixupKind Kind = MCFixupKind(FixupKind);
451 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
452
453 // All of the information is in the fixup.
454 return 0;
455}
456
457/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000458uint32_t ARMMCCodeEmitter::
459getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
460 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000461 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000462}
463
Bill Wendling09aa3f02010-12-09 00:39:08 +0000464/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
465/// BLX branch target.
466uint32_t ARMMCCodeEmitter::
467getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
468 SmallVectorImpl<MCFixup> &Fixups) const {
469 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
470}
471
Jim Grosbache2467172010-12-10 18:21:33 +0000472/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
473uint32_t ARMMCCodeEmitter::
474getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
475 SmallVectorImpl<MCFixup> &Fixups) const {
476 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
477}
478
Jim Grosbach01086452010-12-10 17:13:40 +0000479/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
480uint32_t ARMMCCodeEmitter::
481getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000482 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000483 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
484}
485
Jim Grosbach027d6e82010-12-09 19:04:53 +0000486/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000487uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000488getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000489 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000490 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000491}
492
493/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
494/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000495uint32_t ARMMCCodeEmitter::
496getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000497 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfb20d892010-12-09 00:27:41 +0000498 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
499 if (Subtarget.isThumb2())
500 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_branch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000501 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000502}
503
Bill Wendlingdff2f712010-12-08 23:01:43 +0000504/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
505/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000506uint32_t ARMMCCodeEmitter::
507getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
508 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000509 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
510 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
511 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000512}
513
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000514/// getTAddrModeRegRegOpValue - Return encoding info for 'reg + reg' operand.
515uint32_t ARMMCCodeEmitter::
516getTAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
517 SmallVectorImpl<MCFixup> &Fixups) const {
518 const MCOperand &MO1 = MI.getOperand(OpIdx);
519 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
520 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
521 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
522 return (Rm << 3) | Rn;
523}
524
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000525/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000526uint32_t ARMMCCodeEmitter::
527getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
528 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000529 // {17-13} = reg
530 // {12} = (U)nsigned (add == '1', sub == '0')
531 // {11-0} = imm12
532 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000533 bool isAdd = true;
534 // If The first operand isn't a register, we have a label reference.
535 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000536 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
537 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000538 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000539 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000540 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000541
Owen Andersoneb6779c2010-12-07 00:45:21 +0000542 const MCExpr *Expr = 0;
543 if (!MO.isReg())
544 Expr = MO.getExpr();
545 else
546 Expr = MO2.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000547
Owen Andersond7b3f582010-12-09 01:51:07 +0000548 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
549 MCFixupKind Kind;
550 if (Subtarget.isThumb2())
551 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
552 else
553 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000554 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
555
556 ++MCNumCPRelocations;
557 } else
558 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000559
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000560 uint32_t Binary = Imm12 & 0xfff;
561 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000562 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000563 Binary |= (1 << 12);
564 Binary |= (Reg << 13);
565 return Binary;
566}
567
Owen Anderson9d63d902010-12-01 19:18:46 +0000568/// getT2AddrModeImm8s4OpValue - Return encoding info for
569/// 'reg +/- imm8<<2' operand.
570uint32_t ARMMCCodeEmitter::
571getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
572 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000573 // {12-9} = reg
574 // {8} = (U)nsigned (add == '1', sub == '0')
575 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000576 unsigned Reg, Imm8;
577 bool isAdd = true;
578 // If The first operand isn't a register, we have a label reference.
579 const MCOperand &MO = MI.getOperand(OpIdx);
580 if (!MO.isReg()) {
581 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
582 Imm8 = 0;
583 isAdd = false ; // 'U' bit is set as part of the fixup.
584
585 assert(MO.isExpr() && "Unexpected machine operand type!");
586 const MCExpr *Expr = MO.getExpr();
587 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
588 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
589
590 ++MCNumCPRelocations;
591 } else
592 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
593
594 uint32_t Binary = (Imm8 >> 2) & 0xff;
595 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
596 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000597 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000598 Binary |= (Reg << 9);
599 return Binary;
600}
601
Jim Grosbach54fea632010-11-09 17:20:53 +0000602uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000603getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
604 SmallVectorImpl<MCFixup> &Fixups) const {
605 // {20-16} = imm{15-12}
606 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000607 const MCOperand &MO = MI.getOperand(OpIdx);
Jason W Kim837caa92010-11-18 23:37:15 +0000608 if (MO.isImm()) {
609 return static_cast<unsigned>(MO.getImm());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000610 } else if (const MCSymbolRefExpr *Expr =
Jason W Kim837caa92010-11-18 23:37:15 +0000611 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
612 MCFixupKind Kind;
613 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000614 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000615 case MCSymbolRefExpr::VK_ARM_HI16:
616 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
617 break;
618 case MCSymbolRefExpr::VK_ARM_LO16:
619 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
620 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000621 }
622 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
623 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000624 };
625 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000626 return 0;
627}
628
629uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000630getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
631 SmallVectorImpl<MCFixup> &Fixups) const {
632 const MCOperand &MO = MI.getOperand(OpIdx);
633 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
634 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
635 unsigned Rn = getARMRegisterNumbering(MO.getReg());
636 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000637 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
638 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000639 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
640 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000641
642 // {16-13} = Rn
643 // {12} = isAdd
644 // {11-0} = shifter
645 // {3-0} = Rm
646 // {4} = 0
647 // {6-5} = type
648 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000649 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000650 Binary |= Rn << 13;
651 Binary |= SBits << 5;
652 Binary |= ShImm << 7;
653 if (isAdd)
654 Binary |= 1 << 12;
655 return Binary;
656}
657
Jim Grosbach570a9222010-11-11 01:09:40 +0000658uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000659getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
660 SmallVectorImpl<MCFixup> &Fixups) const {
661 // {17-14} Rn
662 // {13} 1 == imm12, 0 == Rm
663 // {12} isAdd
664 // {11-0} imm12/Rm
665 const MCOperand &MO = MI.getOperand(OpIdx);
666 unsigned Rn = getARMRegisterNumbering(MO.getReg());
667 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
668 Binary |= Rn << 14;
669 return Binary;
670}
671
672uint32_t ARMMCCodeEmitter::
673getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
674 SmallVectorImpl<MCFixup> &Fixups) const {
675 // {13} 1 == imm12, 0 == Rm
676 // {12} isAdd
677 // {11-0} imm12/Rm
678 const MCOperand &MO = MI.getOperand(OpIdx);
679 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
680 unsigned Imm = MO1.getImm();
681 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
682 bool isReg = MO.getReg() != 0;
683 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
684 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
685 if (isReg) {
686 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
687 Binary <<= 7; // Shift amount is bits [11:7]
688 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
689 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
690 }
691 return Binary | (isAdd << 12) | (isReg << 13);
692}
693
694uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000695getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
696 SmallVectorImpl<MCFixup> &Fixups) const {
697 // {9} 1 == imm8, 0 == Rm
698 // {8} isAdd
699 // {7-4} imm7_4/zero
700 // {3-0} imm3_0/Rm
701 const MCOperand &MO = MI.getOperand(OpIdx);
702 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
703 unsigned Imm = MO1.getImm();
704 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
705 bool isImm = MO.getReg() == 0;
706 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
707 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
708 if (!isImm)
709 Imm8 = getARMRegisterNumbering(MO.getReg());
710 return Imm8 | (isAdd << 8) | (isImm << 9);
711}
712
713uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000714getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
715 SmallVectorImpl<MCFixup> &Fixups) const {
716 // {13} 1 == imm8, 0 == Rm
717 // {12-9} Rn
718 // {8} isAdd
719 // {7-4} imm7_4/zero
720 // {3-0} imm3_0/Rm
721 const MCOperand &MO = MI.getOperand(OpIdx);
722 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
723 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
724 unsigned Rn = getARMRegisterNumbering(MO.getReg());
725 unsigned Imm = MO2.getImm();
726 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
727 bool isImm = MO1.getReg() == 0;
728 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
729 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
730 if (!isImm)
731 Imm8 = getARMRegisterNumbering(MO1.getReg());
732 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
733}
734
Bill Wendlingb8958b02010-12-08 01:57:09 +0000735/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000736uint32_t ARMMCCodeEmitter::
737getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
738 SmallVectorImpl<MCFixup> &Fixups) const {
739 // [SP, #imm]
740 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000741 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000742#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
743 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
744 "Unexpected base register!");
745#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000746 // The immediate is already shifted for the implicit zeroes, so no change
747 // here.
748 return MO1.getImm() & 0xff;
749}
750
Bill Wendling1fd374e2010-11-30 22:57:21 +0000751/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000752uint32_t ARMMCCodeEmitter::
753getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
754 SmallVectorImpl<MCFixup> &) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000755 // [Rn, Rm]
756 // {5-3} = Rm
757 // {2-0} = Rn
758 //
759 // [Rn, #imm]
760 // {7-3} = imm5
761 // {2-0} = Rn
762 const MCOperand &MO = MI.getOperand(OpIdx);
763 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
764 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
765 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling272df512010-12-09 21:49:07 +0000766 unsigned Imm5 = MO1.getImm();
Bill Wendling0bdf0c02010-12-03 00:53:22 +0000767
768 if (MO2.getReg() != 0)
769 // Is an immediate.
770 Imm5 = getARMRegisterNumbering(MO2.getReg());
771
Bill Wendling272df512010-12-09 21:49:07 +0000772 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000773}
774
Bill Wendlingb8958b02010-12-08 01:57:09 +0000775/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
776uint32_t ARMMCCodeEmitter::
777getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
778 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000779 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000780}
781
Jim Grosbach5177f792010-12-01 21:09:40 +0000782/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000783uint32_t ARMMCCodeEmitter::
784getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
785 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000786 // {12-9} = reg
787 // {8} = (U)nsigned (add == '1', sub == '0')
788 // {7-0} = imm8
789 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000790 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000791 // If The first operand isn't a register, we have a label reference.
792 const MCOperand &MO = MI.getOperand(OpIdx);
793 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000794 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000795 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000796 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000797
798 assert(MO.isExpr() && "Unexpected machine operand type!");
799 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000800 MCFixupKind Kind;
801 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
802 if (Subtarget.isThumb2())
803 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
804 else
805 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000806 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
807
808 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000809 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000810 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000811 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
812 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000813
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000814 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
815 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000816 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000817 Binary |= (1 << 8);
818 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000819 return Binary;
820}
821
Jim Grosbach806e80e2010-11-03 23:52:49 +0000822unsigned ARMMCCodeEmitter::
823getSORegOpValue(const MCInst &MI, unsigned OpIdx,
824 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000825 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
826 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
827 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000828 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000829 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000830 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000831 // {6-5} = type
832 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000833 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000834 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000835 // else (imm shift)
836 // {11-7} = imm
837
838 const MCOperand &MO = MI.getOperand(OpIdx);
839 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
840 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
841 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
842
843 // Encode Rm.
844 unsigned Binary = getARMRegisterNumbering(MO.getReg());
845
846 // Encode the shift opcode.
847 unsigned SBits = 0;
848 unsigned Rs = MO1.getReg();
849 if (Rs) {
850 // Set shift operand (bit[7:4]).
851 // LSL - 0001
852 // LSR - 0011
853 // ASR - 0101
854 // ROR - 0111
855 // RRX - 0110 and bit[11:8] clear.
856 switch (SOpc) {
857 default: llvm_unreachable("Unknown shift opc!");
858 case ARM_AM::lsl: SBits = 0x1; break;
859 case ARM_AM::lsr: SBits = 0x3; break;
860 case ARM_AM::asr: SBits = 0x5; break;
861 case ARM_AM::ror: SBits = 0x7; break;
862 case ARM_AM::rrx: SBits = 0x6; break;
863 }
864 } else {
865 // Set shift operand (bit[6:4]).
866 // LSL - 000
867 // LSR - 010
868 // ASR - 100
869 // ROR - 110
870 switch (SOpc) {
871 default: llvm_unreachable("Unknown shift opc!");
872 case ARM_AM::lsl: SBits = 0x0; break;
873 case ARM_AM::lsr: SBits = 0x2; break;
874 case ARM_AM::asr: SBits = 0x4; break;
875 case ARM_AM::ror: SBits = 0x6; break;
876 }
877 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000878
Jim Grosbachef324d72010-10-12 23:53:58 +0000879 Binary |= SBits << 4;
880 if (SOpc == ARM_AM::rrx)
881 return Binary;
882
883 // Encode the shift operation Rs or shift_imm (except rrx).
884 if (Rs) {
885 // Encode Rs bit[11:8].
886 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
887 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
888 }
889
890 // Encode shift_imm bit[11:7].
891 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
892}
893
Jim Grosbach806e80e2010-11-03 23:52:49 +0000894unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000895getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
896 SmallVectorImpl<MCFixup> &Fixups) const {
897 const MCOperand &MO1 = MI.getOperand(OpNum);
898 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000899 const MCOperand &MO3 = MI.getOperand(OpNum+2);
900
Owen Anderson75579f72010-11-29 22:44:32 +0000901 // Encoded as [Rn, Rm, imm].
902 // FIXME: Needs fixup support.
903 unsigned Value = getARMRegisterNumbering(MO1.getReg());
904 Value <<= 4;
905 Value |= getARMRegisterNumbering(MO2.getReg());
906 Value <<= 2;
907 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000908
Owen Anderson75579f72010-11-29 22:44:32 +0000909 return Value;
910}
911
912unsigned ARMMCCodeEmitter::
913getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
914 SmallVectorImpl<MCFixup> &Fixups) const {
915 const MCOperand &MO1 = MI.getOperand(OpNum);
916 const MCOperand &MO2 = MI.getOperand(OpNum+1);
917
918 // FIXME: Needs fixup support.
919 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000920
Owen Anderson75579f72010-11-29 22:44:32 +0000921 // Even though the immediate is 8 bits long, we need 9 bits in order
922 // to represent the (inverse of the) sign bit.
923 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000924 int32_t tmp = (int32_t)MO2.getImm();
925 if (tmp < 0)
926 tmp = abs(tmp);
927 else
928 Value |= 256; // Set the ADD bit
929 Value |= tmp & 255;
930 return Value;
931}
932
933unsigned ARMMCCodeEmitter::
934getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
935 SmallVectorImpl<MCFixup> &Fixups) const {
936 const MCOperand &MO1 = MI.getOperand(OpNum);
937
938 // FIXME: Needs fixup support.
939 unsigned Value = 0;
940 int32_t tmp = (int32_t)MO1.getImm();
941 if (tmp < 0)
942 tmp = abs(tmp);
943 else
944 Value |= 256; // Set the ADD bit
945 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000946 return Value;
947}
948
949unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000950getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
951 SmallVectorImpl<MCFixup> &Fixups) const {
952 const MCOperand &MO1 = MI.getOperand(OpNum);
953
954 // FIXME: Needs fixup support.
955 unsigned Value = 0;
956 int32_t tmp = (int32_t)MO1.getImm();
957 if (tmp < 0)
958 tmp = abs(tmp);
959 else
960 Value |= 4096; // Set the ADD bit
961 Value |= tmp & 4095;
962 return Value;
963}
964
965unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000966getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
967 SmallVectorImpl<MCFixup> &Fixups) const {
968 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
969 // shifted. The second is the amount to shift by.
970 //
971 // {3-0} = Rm.
972 // {4} = 0
973 // {6-5} = type
974 // {11-7} = imm
975
976 const MCOperand &MO = MI.getOperand(OpIdx);
977 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
978 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
979
980 // Encode Rm.
981 unsigned Binary = getARMRegisterNumbering(MO.getReg());
982
983 // Encode the shift opcode.
984 unsigned SBits = 0;
985 // Set shift operand (bit[6:4]).
986 // LSL - 000
987 // LSR - 010
988 // ASR - 100
989 // ROR - 110
990 switch (SOpc) {
991 default: llvm_unreachable("Unknown shift opc!");
992 case ARM_AM::lsl: SBits = 0x0; break;
993 case ARM_AM::lsr: SBits = 0x2; break;
994 case ARM_AM::asr: SBits = 0x4; break;
995 case ARM_AM::ror: SBits = 0x6; break;
996 }
997
998 Binary |= SBits << 4;
999 if (SOpc == ARM_AM::rrx)
1000 return Binary;
1001
1002 // Encode shift_imm bit[11:7].
1003 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1004}
1005
1006unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001007getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1008 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001009 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1010 // msb of the mask.
1011 const MCOperand &MO = MI.getOperand(Op);
1012 uint32_t v = ~MO.getImm();
1013 uint32_t lsb = CountTrailingZeros_32(v);
1014 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1015 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1016 return lsb | (msb << 5);
1017}
1018
Jim Grosbach806e80e2010-11-03 23:52:49 +00001019unsigned ARMMCCodeEmitter::
1020getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001021 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001022 // VLDM/VSTM:
1023 // {12-8} = Vd
1024 // {7-0} = Number of registers
1025 //
1026 // LDM/STM:
1027 // {15-0} = Bitfield of GPRs.
1028 unsigned Reg = MI.getOperand(Op).getReg();
1029 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1030 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1031
Bill Wendling5e559a22010-11-09 00:30:18 +00001032 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001033
1034 if (SPRRegs || DPRRegs) {
1035 // VLDM/VSTM
1036 unsigned RegNo = getARMRegisterNumbering(Reg);
1037 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1038 Binary |= (RegNo & 0x1f) << 8;
1039 if (SPRRegs)
1040 Binary |= NumRegs;
1041 else
1042 Binary |= NumRegs * 2;
1043 } else {
1044 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1045 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1046 Binary |= 1 << RegNo;
1047 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001048 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001049
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001050 return Binary;
1051}
1052
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001053/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1054/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001055unsigned ARMMCCodeEmitter::
1056getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1057 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001058 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001059 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001060
Owen Andersond9aa7d32010-11-02 00:05:05 +00001061 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001062 unsigned Align = 0;
1063
1064 switch (Imm.getImm()) {
1065 default: break;
1066 case 2:
1067 case 4:
1068 case 8: Align = 0x01; break;
1069 case 16: Align = 0x02; break;
1070 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001071 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001072
Owen Andersond9aa7d32010-11-02 00:05:05 +00001073 return RegNo | (Align << 4);
1074}
1075
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001076/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1077/// alignment operand for use in VLD-dup instructions. This is the same as
1078/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1079/// different for VLD4-dup.
1080unsigned ARMMCCodeEmitter::
1081getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1082 SmallVectorImpl<MCFixup> &Fixups) const {
1083 const MCOperand &Reg = MI.getOperand(Op);
1084 const MCOperand &Imm = MI.getOperand(Op + 1);
1085
1086 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1087 unsigned Align = 0;
1088
1089 switch (Imm.getImm()) {
1090 default: break;
1091 case 2:
1092 case 4:
1093 case 8: Align = 0x01; break;
1094 case 16: Align = 0x03; break;
1095 }
1096
1097 return RegNo | (Align << 4);
1098}
1099
Jim Grosbach806e80e2010-11-03 23:52:49 +00001100unsigned ARMMCCodeEmitter::
1101getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1102 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001103 const MCOperand &MO = MI.getOperand(Op);
1104 if (MO.getReg() == 0) return 0x0D;
1105 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001106}
1107
Jim Grosbach568eeed2010-09-17 18:46:17 +00001108void ARMMCCodeEmitter::
1109EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001110 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001111 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001112 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001113 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001114 uint64_t TSFlags = Desc.TSFlags;
1115 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001116 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001117 int Size;
1118 // Basic size info comes from the TSFlags field.
1119 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1120 default: llvm_unreachable("Unexpected instruction size!");
1121 case ARMII::Size2Bytes: Size = 2; break;
1122 case ARMII::Size4Bytes: Size = 4; break;
1123 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001124 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1125 // Thumb 32-bit wide instructions need to be have the high order halfword
1126 // emitted first.
1127 if (Subtarget.isThumb() && Size == 4) {
1128 EmitConstant(Binary >> 16, 2, OS);
1129 EmitConstant(Binary & 0xffff, 2, OS);
1130 } else
1131 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001132 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001133}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001134
Jim Grosbach806e80e2010-11-03 23:52:49 +00001135#include "ARMGenMCCodeEmitter.inc"