blob: f0cb95f39b131a4ec26b69b4a676418573cca9a9 [file] [log] [blame]
Jim Grosbach1287f4f2010-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 Lattner63274cb2010-11-15 05:19:05 +000014#define DEBUG_TYPE "mccodeemitter"
Evan Chenga20cde32011-07-20 23:34:39 +000015#include "MCTargetDesc/ARMAddressingModes.h"
Evan Chengad5f4852011-07-23 00:00:19 +000016#include "MCTargetDesc/ARMBaseInfo.h"
17#include "MCTargetDesc/ARMFixupKinds.h"
Evan Chenga20cde32011-07-20 23:34:39 +000018#include "MCTargetDesc/ARMMCExpr.h"
Evan Chengad5f4852011-07-23 00:00:19 +000019#include "MCTargetDesc/ARMMCTargetDesc.h"
Jim Grosbach1287f4f2010-09-17 18:46:17 +000020#include "llvm/MC/MCCodeEmitter.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000023#include "llvm/MC/MCInstrInfo.h"
Evan Chengad5f4852011-07-23 00:00:19 +000024#include "llvm/MC/MCRegisterInfo.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000025#include "llvm/MC/MCSubtargetInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000026#include "llvm/ADT/APFloat.h"
Jim Grosbach91029092010-10-07 22:12:50 +000027#include "llvm/ADT/Statistic.h"
Jim Grosbach1287f4f2010-09-17 18:46:17 +000028#include "llvm/Support/raw_ostream.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000029
Jim Grosbach1287f4f2010-09-17 18:46:17 +000030using namespace llvm;
31
Jim Grosbach0fb841f2010-11-04 01:12:30 +000032STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
33STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
Jim Grosbach91029092010-10-07 22:12:50 +000034
Jim Grosbach1287f4f2010-09-17 18:46:17 +000035namespace {
36class ARMMCCodeEmitter : public MCCodeEmitter {
37 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
38 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
Evan Chengc5e6d2f2011-07-11 03:57:24 +000039 const MCInstrInfo &MCII;
40 const MCSubtargetInfo &STI;
Jim Grosbach1287f4f2010-09-17 18:46:17 +000041
42public:
Evan Chengc5e6d2f2011-07-11 03:57:24 +000043 ARMMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
44 MCContext &ctx)
Evan Cheng58a98142011-07-11 21:24:15 +000045 : MCII(mcii), STI(sti) {
Jim Grosbach1287f4f2010-09-17 18:46:17 +000046 }
47
48 ~ARMMCCodeEmitter() {}
49
Evan Chengc5e6d2f2011-07-11 03:57:24 +000050 bool isThumb() const {
51 // FIXME: Can tablegen auto-generate this?
52 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
53 }
54 bool isThumb2() const {
55 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) != 0;
56 }
57 bool isTargetDarwin() const {
58 Triple TT(STI.getTargetTriple());
59 Triple::OSType OS = TT.getOS();
60 return OS == Triple::Darwin || OS == Triple::MacOSX || OS == Triple::IOS;
61 }
62
Jim Grosbach6fead932010-10-12 17:11:26 +000063 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
64
Jim Grosbach8aed3862010-10-07 21:57:55 +000065 // getBinaryCodeForInstr - TableGen'erated function for getting the
66 // binary encoding for an instruction.
Jim Grosbach2eed7a12010-11-03 23:52:49 +000067 unsigned getBinaryCodeForInstr(const MCInst &MI,
68 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach8aed3862010-10-07 21:57:55 +000069
70 /// getMachineOpValue - Return binary encoding of operand. If the machine
71 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach2eed7a12010-11-03 23:52:49 +000072 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
73 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach8aed3862010-10-07 21:57:55 +000074
Evan Cheng965b3c72011-01-13 07:58:56 +000075 /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
Owen Anderson4ebf4712011-02-08 22:39:40 +000076 /// the specified operand. This is used for operands with :lower16: and
Evan Cheng965b3c72011-01-13 07:58:56 +000077 /// :upper16: prefixes.
78 uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
79 SmallVectorImpl<MCFixup> &Fixups) const;
Jason W Kim5a97bd82010-11-18 23:37:15 +000080
Bill Wendlinge84eb992010-11-03 01:49:29 +000081 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach2eed7a12010-11-03 23:52:49 +000082 unsigned &Reg, unsigned &Imm,
83 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendlinge84eb992010-11-03 01:49:29 +000084
Jim Grosbach9e199462010-12-06 23:57:07 +000085 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling3392bfc2010-12-09 00:39:08 +000086 /// BL branch target.
Jim Grosbach9e199462010-12-06 23:57:07 +000087 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
88 SmallVectorImpl<MCFixup> &Fixups) const;
89
Bill Wendling3392bfc2010-12-09 00:39:08 +000090 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
91 /// BLX branch target.
92 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
93 SmallVectorImpl<MCFixup> &Fixups) const;
94
Jim Grosbache119da12010-12-10 18:21:33 +000095 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
96 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
97 SmallVectorImpl<MCFixup> &Fixups) const;
98
Jim Grosbach78485ad2010-12-10 17:13:40 +000099 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
100 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
101 SmallVectorImpl<MCFixup> &Fixups) const;
102
Jim Grosbach62b68112010-12-09 19:04:53 +0000103 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
104 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000105 SmallVectorImpl<MCFixup> &Fixups) const;
106
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000107 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
108 /// branch target.
109 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
110 SmallVectorImpl<MCFixup> &Fixups) const;
111
Owen Anderson578074b2010-12-13 19:31:11 +0000112 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
113 /// immediate Thumb2 direct branch target.
114 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
115 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson1732c2e2011-08-30 21:58:18 +0000116
Jason W Kimd2e2f562011-02-04 19:47:15 +0000117 /// getARMBranchTargetOpValue - Return encoding info for 24-bit immediate
118 /// branch target.
119 uint32_t getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
120 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersonb205c022011-08-26 23:32:08 +0000121 uint32_t getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
122 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson578074b2010-12-13 19:31:11 +0000123
Jim Grosbachdc35e062010-12-01 19:47:31 +0000124 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
125 /// ADR label target.
126 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
127 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000128 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6d375e52010-12-14 00:36:49 +0000130 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
131 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000132
Jim Grosbachdc35e062010-12-01 19:47:31 +0000133
Bill Wendlinge84eb992010-11-03 01:49:29 +0000134 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
135 /// operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000136 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
137 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendlinge84eb992010-11-03 01:49:29 +0000138
Bill Wendling092a7bd2010-12-14 03:36:38 +0000139 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
140 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
141 SmallVectorImpl<MCFixup> &Fixups)const;
Owen Andersonb0fa1272010-12-10 22:11:13 +0000142
Owen Anderson943fb602010-12-01 19:18:46 +0000143 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
144 /// operand.
145 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
146 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbacha05627e2011-09-09 18:37:27 +0000147
148 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for 'reg + imm8<<2'
149 /// operand.
150 uint32_t getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
151 SmallVectorImpl<MCFixup> &Fixups) const;
152
Jim Grosbach7db8d692011-09-08 22:07:06 +0000153 /// getT2Imm8s4OpValue - Return encoding info for '+/- imm8<<2'
154 /// operand.
155 uint32_t getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
156 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson943fb602010-12-01 19:18:46 +0000157
158
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000159 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
160 /// operand as needed by load/store instructions.
161 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
162 SmallVectorImpl<MCFixup> &Fixups) const;
163
Jim Grosbachcc4a4912010-11-10 23:38:36 +0000164 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
165 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
166 SmallVectorImpl<MCFixup> &Fixups) const {
167 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
168 switch (Mode) {
Matt Beaumont-Gay3077bb62011-01-12 18:02:55 +0000169 default: assert(0 && "Unknown addressing sub-mode!");
Jim Grosbachcc4a4912010-11-10 23:38:36 +0000170 case ARM_AM::da: return 0;
171 case ARM_AM::ia: return 1;
172 case ARM_AM::db: return 2;
173 case ARM_AM::ib: return 3;
174 }
175 }
Jim Grosbach38b469e2010-11-15 20:47:07 +0000176 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
177 ///
178 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
179 switch (ShOpc) {
180 default: llvm_unreachable("Unknown shift opc!");
181 case ARM_AM::no_shift:
182 case ARM_AM::lsl: return 0;
183 case ARM_AM::lsr: return 1;
184 case ARM_AM::asr: return 2;
185 case ARM_AM::ror:
186 case ARM_AM::rrx: return 3;
187 }
188 return 0;
189 }
190
191 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
192 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
193 SmallVectorImpl<MCFixup> &Fixups) const;
194
195 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
196 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
197 SmallVectorImpl<MCFixup> &Fixups) const;
198
Jim Grosbachd3595712011-08-03 23:50:40 +0000199 /// getPostIdxRegOpValue - Return encoding for postidx_reg operands.
200 uint32_t getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
201 SmallVectorImpl<MCFixup> &Fixups) const;
202
Jim Grosbach68685e62010-11-11 16:55:29 +0000203 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
204 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
205 SmallVectorImpl<MCFixup> &Fixups) const;
206
Jim Grosbach607efcb2010-11-11 01:09:40 +0000207 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
208 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
209 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachcc4a4912010-11-10 23:38:36 +0000210
Jim Grosbach49bcd6f2010-12-07 21:50:47 +0000211 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
212 /// operand.
213 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
214 SmallVectorImpl<MCFixup> &Fixups) const;
215
Bill Wendling092a7bd2010-12-14 03:36:38 +0000216 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
217 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling03e75762010-12-15 08:51:02 +0000218 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendlinga9e3df72010-11-30 22:57:21 +0000219
Bill Wendling8a6449c2010-12-08 01:57:09 +0000220 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
221 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
222 SmallVectorImpl<MCFixup> &Fixups) const;
223
Bill Wendlinge84eb992010-11-03 01:49:29 +0000224 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000225 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
226 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000227
Jim Grosbachd9d31da2010-10-12 23:00:24 +0000228 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000229 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
230 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd9d31da2010-10-12 23:00:24 +0000231 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
232 // '1' respectively.
233 return MI.getOperand(Op).getReg() == ARM::CPSR;
234 }
Jim Grosbachefd53692010-10-12 23:53:58 +0000235
Jim Grosbach12e493a2010-10-12 23:18:08 +0000236 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000237 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
238 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach12e493a2010-10-12 23:18:08 +0000239 unsigned SoImm = MI.getOperand(Op).getImm();
240 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
241 assert(SoImmVal != -1 && "Not a valid so_imm value!");
242
243 // Encode rotate_imm.
244 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
245 << ARMII::SoRotImmShift;
246
247 // Encode immed_8.
248 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
249 return Binary;
250 }
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000251
Owen Anderson8fdd1722010-11-12 21:12:40 +0000252 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
253 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
254 SmallVectorImpl<MCFixup> &Fixups) const {
255 unsigned SoImm = MI.getOperand(Op).getImm();
256 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
257 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
258 return Encoded;
259 }
Jim Grosbachd9d31da2010-10-12 23:00:24 +0000260
Owen Anderson50d662b2010-11-29 22:44:32 +0000261 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
262 SmallVectorImpl<MCFixup> &Fixups) const;
263 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
264 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersone22c7322010-11-30 00:14:31 +0000265 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
266 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson299382e2010-11-30 19:19:31 +0000267 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
268 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson50d662b2010-11-29 22:44:32 +0000269
Jim Grosbachefd53692010-10-12 23:53:58 +0000270 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Owen Anderson04912702011-07-21 23:38:37 +0000271 unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op,
272 SmallVectorImpl<MCFixup> &Fixups) const;
273 unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op,
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000274 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson8fdd1722010-11-12 21:12:40 +0000275 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
276 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachefd53692010-10-12 23:53:58 +0000277
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000278 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
279 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfadb9512010-10-27 22:49:00 +0000280 return 64 - MI.getOperand(Op).getImm();
281 }
Jim Grosbach68a335e2010-10-15 17:15:16 +0000282
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000283 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
284 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5edb03e2010-10-21 22:03:21 +0000285
Bruno Cardoso Lopes7f639c12011-01-18 20:45:56 +0000286 unsigned getMsbOpValue(const MCInst &MI, unsigned Op,
287 SmallVectorImpl<MCFixup> &Fixups) const;
288
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000289 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
290 SmallVectorImpl<MCFixup> &Fixups) const;
291 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
292 SmallVectorImpl<MCFixup> &Fixups) const;
Mon P Wang92ff16b2011-05-09 17:47:27 +0000293 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
294 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson318ce7c2010-11-30 00:00:42 +0000295 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
296 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000297 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
298 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach74ef9e12010-10-30 00:37:59 +0000299
Bill Wendling77ad1dc2011-03-07 23:38:41 +0000300 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
301 SmallVectorImpl<MCFixup> &Fixups) const;
302 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
303 SmallVectorImpl<MCFixup> &Fixups) const;
304 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
305 SmallVectorImpl<MCFixup> &Fixups) const;
306 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
307 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling3b1459b2011-03-01 01:00:59 +0000308
Owen Andersonc4030382011-08-08 20:42:17 +0000309 unsigned getThumbSRImmOpValue(const MCInst &MI, unsigned Op,
310 SmallVectorImpl<MCFixup> &Fixups) const;
311
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000312 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
313 unsigned EncodedValue) const;
Owen Anderson99a8cb42010-11-11 21:36:43 +0000314 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendling87240d42010-12-01 21:54:50 +0000315 unsigned EncodedValue) const;
Owen Andersonce2250f2010-11-11 23:12:55 +0000316 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendling87240d42010-12-01 21:54:50 +0000317 unsigned EncodedValue) const;
318
319 unsigned VFPThumb2PostEncoder(const MCInst &MI,
320 unsigned EncodedValue) const;
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000321
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000322 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000323 OS << (char)C;
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000324 }
325
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000326 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000327 // Output the constant in little endian byte order.
328 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000329 EmitByte(Val & 255, OS);
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000330 Val >>= 8;
331 }
332 }
333
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000334 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
335 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000336};
337
338} // end anonymous namespace
339
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000340MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
341 const MCSubtargetInfo &STI,
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000342 MCContext &Ctx) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000343 return new ARMMCCodeEmitter(MCII, STI, Ctx);
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000344}
345
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000346/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
347/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000348/// Thumb2 mode.
349unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
350 unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000351 if (isThumb2()) {
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000352 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000353 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
354 // set to 1111.
355 unsigned Bit24 = EncodedValue & 0x01000000;
356 unsigned Bit28 = Bit24 << 4;
357 EncodedValue &= 0xEFFFFFFF;
358 EncodedValue |= Bit28;
359 EncodedValue |= 0x0F000000;
360 }
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000361
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000362 return EncodedValue;
363}
364
Owen Anderson99a8cb42010-11-11 21:36:43 +0000365/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000366/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson99a8cb42010-11-11 21:36:43 +0000367/// Thumb2 mode.
368unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
369 unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000370 if (isThumb2()) {
Owen Anderson99a8cb42010-11-11 21:36:43 +0000371 EncodedValue &= 0xF0FFFFFF;
372 EncodedValue |= 0x09000000;
373 }
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000374
Owen Anderson99a8cb42010-11-11 21:36:43 +0000375 return EncodedValue;
376}
377
Owen Andersonce2250f2010-11-11 23:12:55 +0000378/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000379/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonce2250f2010-11-11 23:12:55 +0000380/// Thumb2 mode.
381unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
382 unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000383 if (isThumb2()) {
Owen Andersonce2250f2010-11-11 23:12:55 +0000384 EncodedValue &= 0x00FFFFFF;
385 EncodedValue |= 0xEE000000;
386 }
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000387
Owen Andersonce2250f2010-11-11 23:12:55 +0000388 return EncodedValue;
389}
390
Bill Wendling87240d42010-12-01 21:54:50 +0000391/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
392/// them to their Thumb2 form if we are currently in Thumb2 mode.
393unsigned ARMMCCodeEmitter::
394VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000395 if (isThumb2()) {
Bill Wendling87240d42010-12-01 21:54:50 +0000396 EncodedValue &= 0x0FFFFFFF;
397 EncodedValue |= 0xE0000000;
398 }
399 return EncodedValue;
400}
Owen Anderson99a8cb42010-11-11 21:36:43 +0000401
Jim Grosbachc43c9302010-10-08 21:45:55 +0000402/// getMachineOpValue - Return binary encoding of operand. If the machine
403/// operand requires relocation, record the relocation and return zero.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000404unsigned ARMMCCodeEmitter::
405getMachineOpValue(const MCInst &MI, const MCOperand &MO,
406 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6f52f8a2010-10-14 02:33:26 +0000407 if (MO.isReg()) {
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000408 unsigned Reg = MO.getReg();
409 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbach96d82842010-10-29 23:21:03 +0000410
Jim Grosbachee48d2d2010-11-30 23:51:41 +0000411 // Q registers are encoded as 2x their register number.
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000412 switch (Reg) {
413 default:
414 return RegNo;
415 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
416 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
417 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
418 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
419 return 2 * RegNo;
Owen Anderson2bfa8ed2010-10-21 20:49:13 +0000420 }
Bill Wendling6f52f8a2010-10-14 02:33:26 +0000421 } else if (MO.isImm()) {
Jim Grosbachc43c9302010-10-08 21:45:55 +0000422 return static_cast<unsigned>(MO.getImm());
Bill Wendling6f52f8a2010-10-14 02:33:26 +0000423 } else if (MO.isFPImm()) {
424 return static_cast<unsigned>(APFloat(MO.getFPImm())
425 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbachc43c9302010-10-08 21:45:55 +0000426 }
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000427
Jim Grosbach2aeb8b92010-11-19 00:27:09 +0000428 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbachc43c9302010-10-08 21:45:55 +0000429 return 0;
430}
431
Bill Wendling603bd8f2010-11-02 22:31:46 +0000432/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000433bool ARMMCCodeEmitter::
434EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
435 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000436 const MCOperand &MO = MI.getOperand(OpIdx);
437 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach2ba03aa2010-11-01 23:45:50 +0000438
Bill Wendlinge84eb992010-11-03 01:49:29 +0000439 Reg = getARMRegisterNumbering(MO.getReg());
440
441 int32_t SImm = MO1.getImm();
442 bool isAdd = true;
Bill Wendling603bd8f2010-11-02 22:31:46 +0000443
Jim Grosbach505607e2010-10-28 18:34:10 +0000444 // Special value for #-0
Owen Anderson967674d2011-08-29 19:36:44 +0000445 if (SImm == INT32_MIN) {
Bill Wendlinge84eb992010-11-03 01:49:29 +0000446 SImm = 0;
Owen Anderson967674d2011-08-29 19:36:44 +0000447 isAdd = false;
448 }
Bill Wendling603bd8f2010-11-02 22:31:46 +0000449
Jim Grosbach505607e2010-10-28 18:34:10 +0000450 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendlinge84eb992010-11-03 01:49:29 +0000451 if (SImm < 0) {
452 SImm = -SImm;
453 isAdd = false;
454 }
Bill Wendling603bd8f2010-11-02 22:31:46 +0000455
Bill Wendlinge84eb992010-11-03 01:49:29 +0000456 Imm = SImm;
457 return isAdd;
458}
459
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000460/// getBranchTargetOpValue - Helper function to get the branch target operand,
461/// which is either an immediate or requires a fixup.
462static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
463 unsigned FixupKind,
464 SmallVectorImpl<MCFixup> &Fixups) {
465 const MCOperand &MO = MI.getOperand(OpIdx);
466
467 // If the destination is an immediate, we have nothing to do.
468 if (MO.isImm()) return MO.getImm();
469 assert(MO.isExpr() && "Unexpected branch target type!");
470 const MCExpr *Expr = MO.getExpr();
471 MCFixupKind Kind = MCFixupKind(FixupKind);
472 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
473
474 // All of the information is in the fixup.
475 return 0;
476}
477
Owen Anderson5c160fd2011-08-31 18:30:20 +0000478// Thumb BL and BLX use a strange offset encoding where bits 22 and 21 are
479// determined by negating them and XOR'ing them with bit 23.
480static int32_t encodeThumbBLOffset(int32_t offset) {
481 offset >>= 1;
482 uint32_t S = (offset & 0x800000) >> 23;
483 uint32_t J1 = (offset & 0x400000) >> 22;
484 uint32_t J2 = (offset & 0x200000) >> 21;
485 J1 = (~J1 & 0x1);
486 J2 = (~J2 & 0x1);
487 J1 ^= S;
488 J2 ^= S;
489
490 offset &= ~0x600000;
491 offset |= J1 << 22;
492 offset |= J2 << 21;
493
494 return offset;
495}
496
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000497/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach9e199462010-12-06 23:57:07 +0000498uint32_t ARMMCCodeEmitter::
499getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
500 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson5c160fd2011-08-31 18:30:20 +0000501 const MCOperand MO = MI.getOperand(OpIdx);
502 if (MO.isExpr())
503 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl,
504 Fixups);
505 return encodeThumbBLOffset(MO.getImm());
Jim Grosbach9e199462010-12-06 23:57:07 +0000506}
507
Bill Wendling3392bfc2010-12-09 00:39:08 +0000508/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
509/// BLX branch target.
510uint32_t ARMMCCodeEmitter::
511getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
512 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson5c160fd2011-08-31 18:30:20 +0000513 const MCOperand MO = MI.getOperand(OpIdx);
514 if (MO.isExpr())
515 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx,
516 Fixups);
517 return encodeThumbBLOffset(MO.getImm());
Bill Wendling3392bfc2010-12-09 00:39:08 +0000518}
519
Jim Grosbache119da12010-12-10 18:21:33 +0000520/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
521uint32_t ARMMCCodeEmitter::
522getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
523 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson543c89f2011-08-30 22:03:20 +0000524 const MCOperand MO = MI.getOperand(OpIdx);
525 if (MO.isExpr())
Owen Anderson5c160fd2011-08-31 18:30:20 +0000526 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br,
527 Fixups);
Owen Anderson543c89f2011-08-30 22:03:20 +0000528 return (MO.getImm() >> 1);
Jim Grosbache119da12010-12-10 18:21:33 +0000529}
530
Jim Grosbach78485ad2010-12-10 17:13:40 +0000531/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
532uint32_t ARMMCCodeEmitter::
533getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache119da12010-12-10 18:21:33 +0000534 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona455a0b2011-08-31 20:26:14 +0000535 const MCOperand MO = MI.getOperand(OpIdx);
536 if (MO.isExpr())
537 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc,
538 Fixups);
539 return (MO.getImm() >> 1);
Jim Grosbach78485ad2010-12-10 17:13:40 +0000540}
541
Jim Grosbach62b68112010-12-09 19:04:53 +0000542/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000543uint32_t ARMMCCodeEmitter::
Jim Grosbach62b68112010-12-09 19:04:53 +0000544getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000545 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfdf3cd72011-08-30 22:15:17 +0000546 const MCOperand MO = MI.getOperand(OpIdx);
547 if (MO.isExpr())
548 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
549 return (MO.getImm() >> 1);
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000550}
551
Jason W Kimd2e2f562011-02-04 19:47:15 +0000552/// Return true if this branch has a non-always predication
553static bool HasConditionalBranch(const MCInst &MI) {
554 int NumOp = MI.getNumOperands();
555 if (NumOp >= 2) {
556 for (int i = 0; i < NumOp-1; ++i) {
557 const MCOperand &MCOp1 = MI.getOperand(i);
558 const MCOperand &MCOp2 = MI.getOperand(i + 1);
Owen Anderson1732c2e2011-08-30 21:58:18 +0000559 if (MCOp1.isImm() && MCOp2.isReg() &&
Jason W Kimd2e2f562011-02-04 19:47:15 +0000560 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
Owen Anderson1732c2e2011-08-30 21:58:18 +0000561 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
Jason W Kimd2e2f562011-02-04 19:47:15 +0000562 return true;
563 }
564 }
565 }
566 return false;
567}
568
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000569/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
570/// target.
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000571uint32_t ARMMCCodeEmitter::
572getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000573 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachaecdd872010-12-10 23:41:10 +0000574 // FIXME: This really, really shouldn't use TargetMachine. We don't want
575 // coupling between MC and TM anywhere we can help it.
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000576 if (isThumb2())
Owen Anderson578074b2010-12-13 19:31:11 +0000577 return
578 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Jason W Kimd2e2f562011-02-04 19:47:15 +0000579 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000580}
581
Jason W Kimd2e2f562011-02-04 19:47:15 +0000582/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
583/// target.
584uint32_t ARMMCCodeEmitter::
585getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
586 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson6c70e582011-08-26 22:54:51 +0000587 const MCOperand MO = MI.getOperand(OpIdx);
588 if (MO.isExpr()) {
Owen Anderson1732c2e2011-08-30 21:58:18 +0000589 if (HasConditionalBranch(MI))
Owen Anderson6c70e582011-08-26 22:54:51 +0000590 return ::getBranchTargetOpValue(MI, OpIdx,
591 ARM::fixup_arm_condbranch, Fixups);
Owen Anderson1732c2e2011-08-30 21:58:18 +0000592 return ::getBranchTargetOpValue(MI, OpIdx,
Owen Anderson6c70e582011-08-26 22:54:51 +0000593 ARM::fixup_arm_uncondbranch, Fixups);
594 }
595
596 return MO.getImm() >> 2;
Jason W Kimd2e2f562011-02-04 19:47:15 +0000597}
598
Owen Andersonb205c022011-08-26 23:32:08 +0000599uint32_t ARMMCCodeEmitter::
600getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
601 SmallVectorImpl<MCFixup> &Fixups) const {
602 const MCOperand MO = MI.getOperand(OpIdx);
603 if (MO.isExpr()) {
Owen Anderson1732c2e2011-08-30 21:58:18 +0000604 if (HasConditionalBranch(MI))
Owen Andersonb205c022011-08-26 23:32:08 +0000605 return ::getBranchTargetOpValue(MI, OpIdx,
606 ARM::fixup_arm_condbranch, Fixups);
Owen Anderson1732c2e2011-08-30 21:58:18 +0000607 return ::getBranchTargetOpValue(MI, OpIdx,
Owen Andersonb205c022011-08-26 23:32:08 +0000608 ARM::fixup_arm_uncondbranch, Fixups);
609 }
Jason W Kimd2e2f562011-02-04 19:47:15 +0000610
Owen Andersonb205c022011-08-26 23:32:08 +0000611 return MO.getImm() >> 1;
612}
Jason W Kimd2e2f562011-02-04 19:47:15 +0000613
Owen Anderson578074b2010-12-13 19:31:11 +0000614/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
615/// immediate branch target.
616uint32_t ARMMCCodeEmitter::
617getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
618 SmallVectorImpl<MCFixup> &Fixups) const {
619 unsigned Val =
620 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
621 bool I = (Val & 0x800000);
622 bool J1 = (Val & 0x400000);
623 bool J2 = (Val & 0x200000);
624 if (I ^ J1)
625 Val &= ~0x400000;
626 else
627 Val |= 0x400000;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000628
Owen Anderson578074b2010-12-13 19:31:11 +0000629 if (I ^ J2)
630 Val &= ~0x200000;
631 else
632 Val |= 0x200000;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000633
Owen Anderson578074b2010-12-13 19:31:11 +0000634 return Val;
635}
636
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000637/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
638/// target.
Jim Grosbachdc35e062010-12-01 19:47:31 +0000639uint32_t ARMMCCodeEmitter::
640getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
641 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona01bcbf2011-08-26 18:09:22 +0000642 const MCOperand MO = MI.getOperand(OpIdx);
643 if (MO.isExpr())
644 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
645 Fixups);
646 int32_t offset = MO.getImm();
647 uint32_t Val = 0x2000;
648 if (offset < 0) {
649 Val = 0x1000;
650 offset *= -1;
651 }
652 Val |= offset;
653 return Val;
Jim Grosbachdc35e062010-12-01 19:47:31 +0000654}
655
Owen Anderson6d375e52010-12-14 00:36:49 +0000656/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
657/// target.
658uint32_t ARMMCCodeEmitter::
659getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
660 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona01bcbf2011-08-26 18:09:22 +0000661 const MCOperand MO = MI.getOperand(OpIdx);
662 if (MO.isExpr())
663 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
664 Fixups);
Owen Anderson5bfb0e02011-09-09 22:24:36 +0000665 int32_t Val = MO.getImm();
666 if (Val < 0) {
667 Val *= -1;
668 Val |= 0x1000;
669 }
670 return Val;
Owen Anderson6d375e52010-12-14 00:36:49 +0000671}
672
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000673/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
674/// target.
675uint32_t ARMMCCodeEmitter::
676getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
677 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona01bcbf2011-08-26 18:09:22 +0000678 const MCOperand MO = MI.getOperand(OpIdx);
679 if (MO.isExpr())
680 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
681 Fixups);
682 return MO.getImm();
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000683}
684
Bill Wendling092a7bd2010-12-14 03:36:38 +0000685/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
686/// operand.
Owen Andersonb0fa1272010-12-10 22:11:13 +0000687uint32_t ARMMCCodeEmitter::
Bill Wendling092a7bd2010-12-14 03:36:38 +0000688getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
689 SmallVectorImpl<MCFixup> &) const {
690 // [Rn, Rm]
691 // {5-3} = Rm
692 // {2-0} = Rn
Owen Andersonb0fa1272010-12-10 22:11:13 +0000693 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendling092a7bd2010-12-14 03:36:38 +0000694 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Andersonb0fa1272010-12-10 22:11:13 +0000695 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
696 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
697 return (Rm << 3) | Rn;
698}
699
Bill Wendlinge84eb992010-11-03 01:49:29 +0000700/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000701uint32_t ARMMCCodeEmitter::
702getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
703 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinge84eb992010-11-03 01:49:29 +0000704 // {17-13} = reg
705 // {12} = (U)nsigned (add == '1', sub == '0')
706 // {11-0} = imm12
707 unsigned Reg, Imm12;
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000708 bool isAdd = true;
709 // If The first operand isn't a register, we have a label reference.
710 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Anderson4ebf4712011-02-08 22:39:40 +0000711 if (!MO.isReg()) {
Jim Grosbach90987142010-11-09 01:37:15 +0000712 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000713 Imm12 = 0;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +0000714 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000715
Owen Anderson4a9eb5f2011-09-12 20:36:51 +0000716 if (MO.isExpr()) {
717 const MCExpr *Expr = MO.getExpr();
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000718
Owen Anderson4a9eb5f2011-09-12 20:36:51 +0000719 MCFixupKind Kind;
720 if (isThumb2())
721 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
722 else
723 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
724 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000725
Owen Anderson4a9eb5f2011-09-12 20:36:51 +0000726 ++MCNumCPRelocations;
727 } else {
728 Reg = ARM::PC;
729 int32_t Offset = MO.getImm();
730 if (Offset < 0) {
731 Offset *= -1;
732 isAdd = false;
733 }
734 Imm12 = Offset;
735 }
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000736 } else
737 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendlinge84eb992010-11-03 01:49:29 +0000738
Bill Wendlinge84eb992010-11-03 01:49:29 +0000739 uint32_t Binary = Imm12 & 0xfff;
740 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach505607e2010-10-28 18:34:10 +0000741 if (isAdd)
Bill Wendlinge84eb992010-11-03 01:49:29 +0000742 Binary |= (1 << 12);
743 Binary |= (Reg << 13);
744 return Binary;
745}
746
Jim Grosbach7db8d692011-09-08 22:07:06 +0000747/// getT2Imm8s4OpValue - Return encoding info for
748/// '+/- imm8<<2' operand.
749uint32_t ARMMCCodeEmitter::
750getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
751 SmallVectorImpl<MCFixup> &Fixups) const {
752 // FIXME: The immediate operand should have already been encoded like this
753 // before ever getting here. The encoder method should just need to combine
754 // the MI operands for the register and the offset into a single
755 // representation for the complex operand in the .td file. This isn't just
756 // style, unfortunately. As-is, we can't represent the distinct encoding
757 // for #-0.
758
759 // {8} = (U)nsigned (add == '1', sub == '0')
760 // {7-0} = imm8
761 int32_t Imm8 = MI.getOperand(OpIdx).getImm();
762 bool isAdd = Imm8 >= 0;
763
764 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
765 if (Imm8 < 0)
766 Imm8 = -Imm8;
767
768 // Scaled by 4.
769 Imm8 /= 4;
770
771 uint32_t Binary = Imm8 & 0xff;
772 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
773 if (isAdd)
774 Binary |= (1 << 8);
775 return Binary;
776}
777
Owen Anderson943fb602010-12-01 19:18:46 +0000778/// getT2AddrModeImm8s4OpValue - Return encoding info for
779/// 'reg +/- imm8<<2' operand.
780uint32_t ARMMCCodeEmitter::
781getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
782 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbache69f7242010-12-10 21:05:07 +0000783 // {12-9} = reg
784 // {8} = (U)nsigned (add == '1', sub == '0')
785 // {7-0} = imm8
Owen Anderson943fb602010-12-01 19:18:46 +0000786 unsigned Reg, Imm8;
787 bool isAdd = true;
788 // If The first operand isn't a register, we have a label reference.
789 const MCOperand &MO = MI.getOperand(OpIdx);
790 if (!MO.isReg()) {
791 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
792 Imm8 = 0;
793 isAdd = false ; // 'U' bit is set as part of the fixup.
794
795 assert(MO.isExpr() && "Unexpected machine operand type!");
796 const MCExpr *Expr = MO.getExpr();
797 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
798 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
799
800 ++MCNumCPRelocations;
801 } else
802 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
803
Jim Grosbach7db8d692011-09-08 22:07:06 +0000804 // FIXME: The immediate operand should have already been encoded like this
805 // before ever getting here. The encoder method should just need to combine
806 // the MI operands for the register and the offset into a single
807 // representation for the complex operand in the .td file. This isn't just
808 // style, unfortunately. As-is, we can't represent the distinct encoding
809 // for #-0.
Owen Anderson943fb602010-12-01 19:18:46 +0000810 uint32_t Binary = (Imm8 >> 2) & 0xff;
811 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
812 if (isAdd)
Jim Grosbache69f7242010-12-10 21:05:07 +0000813 Binary |= (1 << 8);
Owen Anderson943fb602010-12-01 19:18:46 +0000814 Binary |= (Reg << 9);
815 return Binary;
816}
817
Jim Grosbacha05627e2011-09-09 18:37:27 +0000818/// getT2AddrModeImm0_1020s4OpValue - Return encoding info for
819/// 'reg + imm8<<2' operand.
820uint32_t ARMMCCodeEmitter::
821getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
822 SmallVectorImpl<MCFixup> &Fixups) const {
823 // {11-8} = reg
824 // {7-0} = imm8
825 const MCOperand &MO = MI.getOperand(OpIdx);
826 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
827 unsigned Reg = getARMRegisterNumbering(MO.getReg());
828 unsigned Imm8 = MO1.getImm();
829 return (Reg << 8) | Imm8;
830}
831
Jason W Kim9c5b65d2011-01-12 00:19:25 +0000832// FIXME: This routine assumes that a binary
833// expression will always result in a PCRel expression
834// In reality, its only true if one or more subexpressions
835// is itself a PCRel (i.e. "." in asm or some other pcrel construct)
836// but this is good enough for now.
837static bool EvaluateAsPCRel(const MCExpr *Expr) {
838 switch (Expr->getKind()) {
Matt Beaumont-Gay3077bb62011-01-12 18:02:55 +0000839 default: assert(0 && "Unexpected expression type");
Jason W Kim9c5b65d2011-01-12 00:19:25 +0000840 case MCExpr::SymbolRef: return false;
841 case MCExpr::Binary: return true;
Jason W Kim9c5b65d2011-01-12 00:19:25 +0000842 }
843}
844
Evan Cheng965b3c72011-01-13 07:58:56 +0000845uint32_t
846ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
847 SmallVectorImpl<MCFixup> &Fixups) const {
Jason W Kim5a97bd82010-11-18 23:37:15 +0000848 // {20-16} = imm{15-12}
849 // {11-0} = imm{11-0}
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000850 const MCOperand &MO = MI.getOperand(OpIdx);
Evan Cheng965b3c72011-01-13 07:58:56 +0000851 if (MO.isImm())
852 // Hi / lo 16 bits already extracted during earlier passes.
Jason W Kim5a97bd82010-11-18 23:37:15 +0000853 return static_cast<unsigned>(MO.getImm());
Evan Cheng965b3c72011-01-13 07:58:56 +0000854
855 // Handle :upper16: and :lower16: assembly prefixes.
856 const MCExpr *E = MO.getExpr();
857 if (E->getKind() == MCExpr::Target) {
858 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
859 E = ARM16Expr->getSubExpr();
860
Jason W Kim5a97bd82010-11-18 23:37:15 +0000861 MCFixupKind Kind;
Evan Cheng965b3c72011-01-13 07:58:56 +0000862 switch (ARM16Expr->getKind()) {
Matt Beaumont-Gay3077bb62011-01-12 18:02:55 +0000863 default: assert(0 && "Unsupported ARMFixup");
Evan Cheng965b3c72011-01-13 07:58:56 +0000864 case ARMMCExpr::VK_ARM_HI16:
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000865 if (!isTargetDarwin() && EvaluateAsPCRel(E))
866 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000867 ? ARM::fixup_t2_movt_hi16_pcrel
868 : ARM::fixup_arm_movt_hi16_pcrel);
869 else
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000870 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000871 ? ARM::fixup_t2_movt_hi16
872 : ARM::fixup_arm_movt_hi16);
Jason W Kim5a97bd82010-11-18 23:37:15 +0000873 break;
Evan Cheng965b3c72011-01-13 07:58:56 +0000874 case ARMMCExpr::VK_ARM_LO16:
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000875 if (!isTargetDarwin() && EvaluateAsPCRel(E))
876 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000877 ? ARM::fixup_t2_movw_lo16_pcrel
878 : ARM::fixup_arm_movw_lo16_pcrel);
879 else
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000880 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000881 ? ARM::fixup_t2_movw_lo16
882 : ARM::fixup_arm_movw_lo16);
Jason W Kim5a97bd82010-11-18 23:37:15 +0000883 break;
Jason W Kim5a97bd82010-11-18 23:37:15 +0000884 }
Evan Cheng965b3c72011-01-13 07:58:56 +0000885 Fixups.push_back(MCFixup::Create(0, E, Kind));
Jason W Kim5a97bd82010-11-18 23:37:15 +0000886 return 0;
Jim Grosbach2aeb8b92010-11-19 00:27:09 +0000887 };
Evan Cheng965b3c72011-01-13 07:58:56 +0000888
Jim Grosbach2aeb8b92010-11-19 00:27:09 +0000889 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim5a97bd82010-11-18 23:37:15 +0000890 return 0;
891}
892
893uint32_t ARMMCCodeEmitter::
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000894getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
895 SmallVectorImpl<MCFixup> &Fixups) const {
896 const MCOperand &MO = MI.getOperand(OpIdx);
897 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
898 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
899 unsigned Rn = getARMRegisterNumbering(MO.getReg());
900 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000901 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
902 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach38b469e2010-11-15 20:47:07 +0000903 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
904 unsigned SBits = getShiftOp(ShOp);
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000905
906 // {16-13} = Rn
907 // {12} = isAdd
908 // {11-0} = shifter
909 // {3-0} = Rm
910 // {4} = 0
911 // {6-5} = type
912 // {11-7} = imm
Jim Grosbach607efcb2010-11-11 01:09:40 +0000913 uint32_t Binary = Rm;
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000914 Binary |= Rn << 13;
915 Binary |= SBits << 5;
916 Binary |= ShImm << 7;
917 if (isAdd)
918 Binary |= 1 << 12;
919 return Binary;
920}
921
Jim Grosbach607efcb2010-11-11 01:09:40 +0000922uint32_t ARMMCCodeEmitter::
Jim Grosbach38b469e2010-11-15 20:47:07 +0000923getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
924 SmallVectorImpl<MCFixup> &Fixups) const {
925 // {17-14} Rn
926 // {13} 1 == imm12, 0 == Rm
927 // {12} isAdd
928 // {11-0} imm12/Rm
929 const MCOperand &MO = MI.getOperand(OpIdx);
930 unsigned Rn = getARMRegisterNumbering(MO.getReg());
931 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
932 Binary |= Rn << 14;
933 return Binary;
934}
935
936uint32_t ARMMCCodeEmitter::
937getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
938 SmallVectorImpl<MCFixup> &Fixups) const {
939 // {13} 1 == imm12, 0 == Rm
940 // {12} isAdd
941 // {11-0} imm12/Rm
942 const MCOperand &MO = MI.getOperand(OpIdx);
943 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
944 unsigned Imm = MO1.getImm();
945 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
946 bool isReg = MO.getReg() != 0;
947 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
948 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
949 if (isReg) {
950 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
951 Binary <<= 7; // Shift amount is bits [11:7]
952 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
953 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
954 }
955 return Binary | (isAdd << 12) | (isReg << 13);
956}
957
958uint32_t ARMMCCodeEmitter::
Jim Grosbachd3595712011-08-03 23:50:40 +0000959getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
960 SmallVectorImpl<MCFixup> &Fixups) const {
961 // {4} isAdd
962 // {3-0} Rm
963 const MCOperand &MO = MI.getOperand(OpIdx);
964 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
Jim Grosbacha70fbfd52011-08-05 16:11:38 +0000965 bool isAdd = MO1.getImm() != 0;
Jim Grosbachd3595712011-08-03 23:50:40 +0000966 return getARMRegisterNumbering(MO.getReg()) | (isAdd << 4);
967}
968
969uint32_t ARMMCCodeEmitter::
Jim Grosbach68685e62010-11-11 16:55:29 +0000970getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
971 SmallVectorImpl<MCFixup> &Fixups) const {
972 // {9} 1 == imm8, 0 == Rm
973 // {8} isAdd
974 // {7-4} imm7_4/zero
975 // {3-0} imm3_0/Rm
976 const MCOperand &MO = MI.getOperand(OpIdx);
977 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
978 unsigned Imm = MO1.getImm();
979 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
980 bool isImm = MO.getReg() == 0;
981 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
982 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
983 if (!isImm)
984 Imm8 = getARMRegisterNumbering(MO.getReg());
985 return Imm8 | (isAdd << 8) | (isImm << 9);
986}
987
988uint32_t ARMMCCodeEmitter::
Jim Grosbach607efcb2010-11-11 01:09:40 +0000989getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
990 SmallVectorImpl<MCFixup> &Fixups) const {
991 // {13} 1 == imm8, 0 == Rm
992 // {12-9} Rn
993 // {8} isAdd
994 // {7-4} imm7_4/zero
995 // {3-0} imm3_0/Rm
996 const MCOperand &MO = MI.getOperand(OpIdx);
997 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
998 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
999 unsigned Rn = getARMRegisterNumbering(MO.getReg());
1000 unsigned Imm = MO2.getImm();
1001 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1002 bool isImm = MO1.getReg() == 0;
1003 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1004 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1005 if (!isImm)
1006 Imm8 = getARMRegisterNumbering(MO1.getReg());
1007 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
1008}
1009
Bill Wendling8a6449c2010-12-08 01:57:09 +00001010/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbach49bcd6f2010-12-07 21:50:47 +00001011uint32_t ARMMCCodeEmitter::
1012getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
1013 SmallVectorImpl<MCFixup> &Fixups) const {
1014 // [SP, #imm]
1015 // {7-0} = imm8
Jim Grosbach49bcd6f2010-12-07 21:50:47 +00001016 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendling8a6449c2010-12-08 01:57:09 +00001017 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
1018 "Unexpected base register!");
Bill Wendling7d3bde92010-12-15 23:32:27 +00001019
Jim Grosbach49bcd6f2010-12-07 21:50:47 +00001020 // The immediate is already shifted for the implicit zeroes, so no change
1021 // here.
1022 return MO1.getImm() & 0xff;
1023}
1024
Bill Wendling092a7bd2010-12-14 03:36:38 +00001025/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling0c4838b2010-12-09 21:49:07 +00001026uint32_t ARMMCCodeEmitter::
Bill Wendling092a7bd2010-12-14 03:36:38 +00001027getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling03e75762010-12-15 08:51:02 +00001028 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling811c9362010-11-30 07:44:32 +00001029 // [Rn, #imm]
1030 // {7-3} = imm5
1031 // {2-0} = Rn
1032 const MCOperand &MO = MI.getOperand(OpIdx);
1033 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendling811c9362010-11-30 07:44:32 +00001034 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Matt Beaumont-Gaye9afc742010-12-16 01:34:26 +00001035 unsigned Imm5 = MO1.getImm();
Bill Wendling0c4838b2010-12-09 21:49:07 +00001036 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendlinga9e3df72010-11-30 22:57:21 +00001037}
1038
Bill Wendling8a6449c2010-12-08 01:57:09 +00001039/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
1040uint32_t ARMMCCodeEmitter::
1041getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
1042 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond16fb432011-08-30 22:10:03 +00001043 const MCOperand MO = MI.getOperand(OpIdx);
1044 if (MO.isExpr())
1045 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
1046 return (MO.getImm() >> 2);
Bill Wendling8a6449c2010-12-08 01:57:09 +00001047}
1048
Jim Grosbach30eb6c72010-12-01 21:09:40 +00001049/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001050uint32_t ARMMCCodeEmitter::
1051getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
1052 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinge84eb992010-11-03 01:49:29 +00001053 // {12-9} = reg
1054 // {8} = (U)nsigned (add == '1', sub == '0')
1055 // {7-0} = imm8
1056 unsigned Reg, Imm8;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001057 bool isAdd;
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001058 // If The first operand isn't a register, we have a label reference.
1059 const MCOperand &MO = MI.getOperand(OpIdx);
1060 if (!MO.isReg()) {
Jim Grosbach90987142010-11-09 01:37:15 +00001061 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001062 Imm8 = 0;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001063 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001064
1065 assert(MO.isExpr() && "Unexpected machine operand type!");
1066 const MCExpr *Expr = MO.getExpr();
Owen Anderson0f7142d2010-12-08 00:18:36 +00001067 MCFixupKind Kind;
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001068 if (isThumb2())
Owen Anderson0f7142d2010-12-08 00:18:36 +00001069 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
1070 else
1071 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001072 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
1073
1074 ++MCNumCPRelocations;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001075 } else {
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001076 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001077 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
1078 }
Bill Wendlinge84eb992010-11-03 01:49:29 +00001079
Bill Wendlinge84eb992010-11-03 01:49:29 +00001080 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
1081 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001082 if (isAdd)
Bill Wendlinge84eb992010-11-03 01:49:29 +00001083 Binary |= (1 << 8);
1084 Binary |= (Reg << 9);
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001085 return Binary;
1086}
1087
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001088unsigned ARMMCCodeEmitter::
Owen Anderson04912702011-07-21 23:38:37 +00001089getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001090 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001091 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
Owen Anderson7c965e72011-07-28 17:56:55 +00001092 // shifted. The second is Rs, the amount to shift by, and the third specifies
1093 // the type of the shift.
Jim Grosbach49b0c452010-11-03 22:03:20 +00001094 //
Jim Grosbachefd53692010-10-12 23:53:58 +00001095 // {3-0} = Rm.
Owen Anderson7c965e72011-07-28 17:56:55 +00001096 // {4} = 1
Jim Grosbachefd53692010-10-12 23:53:58 +00001097 // {6-5} = type
Owen Anderson7c965e72011-07-28 17:56:55 +00001098 // {11-8} = Rs
1099 // {7} = 0
Jim Grosbachefd53692010-10-12 23:53:58 +00001100
1101 const MCOperand &MO = MI.getOperand(OpIdx);
1102 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1103 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
1104 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
1105
1106 // Encode Rm.
1107 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1108
1109 // Encode the shift opcode.
1110 unsigned SBits = 0;
1111 unsigned Rs = MO1.getReg();
1112 if (Rs) {
1113 // Set shift operand (bit[7:4]).
1114 // LSL - 0001
1115 // LSR - 0011
1116 // ASR - 0101
1117 // ROR - 0111
Jim Grosbachefd53692010-10-12 23:53:58 +00001118 switch (SOpc) {
1119 default: llvm_unreachable("Unknown shift opc!");
1120 case ARM_AM::lsl: SBits = 0x1; break;
1121 case ARM_AM::lsr: SBits = 0x3; break;
1122 case ARM_AM::asr: SBits = 0x5; break;
1123 case ARM_AM::ror: SBits = 0x7; break;
Jim Grosbachefd53692010-10-12 23:53:58 +00001124 }
1125 }
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001126
Jim Grosbachefd53692010-10-12 23:53:58 +00001127 Binary |= SBits << 4;
Jim Grosbachefd53692010-10-12 23:53:58 +00001128
Owen Anderson7c965e72011-07-28 17:56:55 +00001129 // Encode the shift operation Rs.
Owen Anderson04912702011-07-21 23:38:37 +00001130 // Encode Rs bit[11:8].
1131 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1132 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
1133}
1134
1135unsigned ARMMCCodeEmitter::
1136getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
1137 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson7c965e72011-07-28 17:56:55 +00001138 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1139 // shifted. The second is the amount to shift by.
Owen Anderson04912702011-07-21 23:38:37 +00001140 //
1141 // {3-0} = Rm.
Owen Anderson7c965e72011-07-28 17:56:55 +00001142 // {4} = 0
Owen Anderson04912702011-07-21 23:38:37 +00001143 // {6-5} = type
Owen Anderson7c965e72011-07-28 17:56:55 +00001144 // {11-7} = imm
Owen Anderson04912702011-07-21 23:38:37 +00001145
1146 const MCOperand &MO = MI.getOperand(OpIdx);
1147 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1148 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1149
1150 // Encode Rm.
1151 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1152
1153 // Encode the shift opcode.
1154 unsigned SBits = 0;
1155
1156 // Set shift operand (bit[6:4]).
1157 // LSL - 000
1158 // LSR - 010
1159 // ASR - 100
1160 // ROR - 110
1161 // RRX - 110 and bit[11:8] clear.
1162 switch (SOpc) {
1163 default: llvm_unreachable("Unknown shift opc!");
1164 case ARM_AM::lsl: SBits = 0x0; break;
1165 case ARM_AM::lsr: SBits = 0x2; break;
1166 case ARM_AM::asr: SBits = 0x4; break;
1167 case ARM_AM::ror: SBits = 0x6; break;
1168 case ARM_AM::rrx:
1169 Binary |= 0x60;
1170 return Binary;
Jim Grosbachefd53692010-10-12 23:53:58 +00001171 }
1172
1173 // Encode shift_imm bit[11:7].
Owen Anderson04912702011-07-21 23:38:37 +00001174 Binary |= SBits << 4;
Owen Andersone33c95d2011-08-11 18:41:59 +00001175 unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm());
1176 assert(Offset && "Offset must be in range 1-32!");
1177 if (Offset == 32) Offset = 0;
1178 return Binary | (Offset << 7);
Jim Grosbachefd53692010-10-12 23:53:58 +00001179}
1180
Owen Anderson04912702011-07-21 23:38:37 +00001181
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001182unsigned ARMMCCodeEmitter::
Owen Anderson50d662b2010-11-29 22:44:32 +00001183getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1184 SmallVectorImpl<MCFixup> &Fixups) const {
1185 const MCOperand &MO1 = MI.getOperand(OpNum);
1186 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbachc4a0c292010-12-10 21:57:34 +00001187 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1188
Owen Anderson50d662b2010-11-29 22:44:32 +00001189 // Encoded as [Rn, Rm, imm].
1190 // FIXME: Needs fixup support.
1191 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1192 Value <<= 4;
1193 Value |= getARMRegisterNumbering(MO2.getReg());
1194 Value <<= 2;
1195 Value |= MO3.getImm();
Jim Grosbachc4a0c292010-12-10 21:57:34 +00001196
Owen Anderson50d662b2010-11-29 22:44:32 +00001197 return Value;
1198}
1199
1200unsigned ARMMCCodeEmitter::
1201getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1202 SmallVectorImpl<MCFixup> &Fixups) const {
1203 const MCOperand &MO1 = MI.getOperand(OpNum);
1204 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1205
1206 // FIXME: Needs fixup support.
1207 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbachc4a0c292010-12-10 21:57:34 +00001208
Owen Anderson50d662b2010-11-29 22:44:32 +00001209 // Even though the immediate is 8 bits long, we need 9 bits in order
1210 // to represent the (inverse of the) sign bit.
1211 Value <<= 9;
Owen Andersone22c7322010-11-30 00:14:31 +00001212 int32_t tmp = (int32_t)MO2.getImm();
1213 if (tmp < 0)
1214 tmp = abs(tmp);
1215 else
1216 Value |= 256; // Set the ADD bit
1217 Value |= tmp & 255;
1218 return Value;
1219}
1220
1221unsigned ARMMCCodeEmitter::
1222getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1223 SmallVectorImpl<MCFixup> &Fixups) const {
1224 const MCOperand &MO1 = MI.getOperand(OpNum);
1225
1226 // FIXME: Needs fixup support.
1227 unsigned Value = 0;
1228 int32_t tmp = (int32_t)MO1.getImm();
1229 if (tmp < 0)
1230 tmp = abs(tmp);
1231 else
1232 Value |= 256; // Set the ADD bit
1233 Value |= tmp & 255;
Owen Anderson50d662b2010-11-29 22:44:32 +00001234 return Value;
1235}
1236
1237unsigned ARMMCCodeEmitter::
Owen Anderson299382e2010-11-30 19:19:31 +00001238getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1239 SmallVectorImpl<MCFixup> &Fixups) const {
1240 const MCOperand &MO1 = MI.getOperand(OpNum);
1241
1242 // FIXME: Needs fixup support.
1243 unsigned Value = 0;
1244 int32_t tmp = (int32_t)MO1.getImm();
1245 if (tmp < 0)
1246 tmp = abs(tmp);
1247 else
1248 Value |= 4096; // Set the ADD bit
1249 Value |= tmp & 4095;
1250 return Value;
1251}
1252
1253unsigned ARMMCCodeEmitter::
Owen Anderson8fdd1722010-11-12 21:12:40 +00001254getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1255 SmallVectorImpl<MCFixup> &Fixups) const {
1256 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1257 // shifted. The second is the amount to shift by.
1258 //
1259 // {3-0} = Rm.
1260 // {4} = 0
1261 // {6-5} = type
1262 // {11-7} = imm
1263
1264 const MCOperand &MO = MI.getOperand(OpIdx);
1265 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1266 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1267
1268 // Encode Rm.
1269 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1270
1271 // Encode the shift opcode.
1272 unsigned SBits = 0;
1273 // Set shift operand (bit[6:4]).
1274 // LSL - 000
1275 // LSR - 010
1276 // ASR - 100
1277 // ROR - 110
1278 switch (SOpc) {
1279 default: llvm_unreachable("Unknown shift opc!");
1280 case ARM_AM::lsl: SBits = 0x0; break;
1281 case ARM_AM::lsr: SBits = 0x2; break;
1282 case ARM_AM::asr: SBits = 0x4; break;
1283 case ARM_AM::ror: SBits = 0x6; break;
1284 }
1285
1286 Binary |= SBits << 4;
1287 if (SOpc == ARM_AM::rrx)
1288 return Binary;
1289
1290 // Encode shift_imm bit[11:7].
1291 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1292}
1293
1294unsigned ARMMCCodeEmitter::
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001295getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1296 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach5edb03e2010-10-21 22:03:21 +00001297 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1298 // msb of the mask.
1299 const MCOperand &MO = MI.getOperand(Op);
1300 uint32_t v = ~MO.getImm();
1301 uint32_t lsb = CountTrailingZeros_32(v);
1302 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1303 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1304 return lsb | (msb << 5);
1305}
1306
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001307unsigned ARMMCCodeEmitter::
Bruno Cardoso Lopes7f639c12011-01-18 20:45:56 +00001308getMsbOpValue(const MCInst &MI, unsigned Op,
1309 SmallVectorImpl<MCFixup> &Fixups) const {
1310 // MSB - 5 bits.
1311 uint32_t lsb = MI.getOperand(Op-1).getImm();
1312 uint32_t width = MI.getOperand(Op).getImm();
1313 uint32_t msb = lsb+width-1;
1314 assert (width != 0 && msb < 32 && "Illegal bit width!");
1315 return msb;
1316}
1317
1318unsigned ARMMCCodeEmitter::
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001319getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling1b83ed52010-11-09 00:30:18 +00001320 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling345b48f2010-11-17 00:45:23 +00001321 // VLDM/VSTM:
1322 // {12-8} = Vd
1323 // {7-0} = Number of registers
1324 //
1325 // LDM/STM:
1326 // {15-0} = Bitfield of GPRs.
1327 unsigned Reg = MI.getOperand(Op).getReg();
Evan Chengad5f4852011-07-23 00:00:19 +00001328 bool SPRRegs = llvm::ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1329 bool DPRRegs = llvm::ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
Bill Wendling345b48f2010-11-17 00:45:23 +00001330
Bill Wendling1b83ed52010-11-09 00:30:18 +00001331 unsigned Binary = 0;
Bill Wendling345b48f2010-11-17 00:45:23 +00001332
1333 if (SPRRegs || DPRRegs) {
1334 // VLDM/VSTM
1335 unsigned RegNo = getARMRegisterNumbering(Reg);
1336 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1337 Binary |= (RegNo & 0x1f) << 8;
1338 if (SPRRegs)
1339 Binary |= NumRegs;
1340 else
1341 Binary |= NumRegs * 2;
1342 } else {
1343 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1344 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1345 Binary |= 1 << RegNo;
1346 }
Bill Wendling1b83ed52010-11-09 00:30:18 +00001347 }
Bill Wendling345b48f2010-11-17 00:45:23 +00001348
Jim Grosbach74ef9e12010-10-30 00:37:59 +00001349 return Binary;
1350}
1351
Bob Wilson318ce7c2010-11-30 00:00:42 +00001352/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1353/// with the alignment operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001354unsigned ARMMCCodeEmitter::
1355getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1356 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonad402342010-11-02 00:05:05 +00001357 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001358 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach49b0c452010-11-03 22:03:20 +00001359
Owen Andersonad402342010-11-02 00:05:05 +00001360 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001361 unsigned Align = 0;
1362
1363 switch (Imm.getImm()) {
1364 default: break;
1365 case 2:
1366 case 4:
1367 case 8: Align = 0x01; break;
1368 case 16: Align = 0x02; break;
1369 case 32: Align = 0x03; break;
Owen Andersonad402342010-11-02 00:05:05 +00001370 }
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001371
Owen Andersonad402342010-11-02 00:05:05 +00001372 return RegNo | (Align << 4);
1373}
1374
Mon P Wang92ff16b2011-05-09 17:47:27 +00001375/// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1376/// along with the alignment operand for use in VST1 and VLD1 with size 32.
1377unsigned ARMMCCodeEmitter::
1378getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1379 SmallVectorImpl<MCFixup> &Fixups) const {
1380 const MCOperand &Reg = MI.getOperand(Op);
1381 const MCOperand &Imm = MI.getOperand(Op + 1);
1382
1383 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1384 unsigned Align = 0;
1385
1386 switch (Imm.getImm()) {
1387 default: break;
1388 case 2:
1389 case 4:
1390 case 8:
1391 case 16: Align = 0x00; break;
1392 case 32: Align = 0x03; break;
1393 }
1394
1395 return RegNo | (Align << 4);
1396}
1397
1398
Bob Wilson318ce7c2010-11-30 00:00:42 +00001399/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1400/// alignment operand for use in VLD-dup instructions. This is the same as
1401/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1402/// different for VLD4-dup.
1403unsigned ARMMCCodeEmitter::
1404getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1405 SmallVectorImpl<MCFixup> &Fixups) const {
1406 const MCOperand &Reg = MI.getOperand(Op);
1407 const MCOperand &Imm = MI.getOperand(Op + 1);
1408
1409 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1410 unsigned Align = 0;
1411
1412 switch (Imm.getImm()) {
1413 default: break;
1414 case 2:
1415 case 4:
1416 case 8: Align = 0x01; break;
1417 case 16: Align = 0x03; break;
1418 }
1419
1420 return RegNo | (Align << 4);
1421}
1422
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001423unsigned ARMMCCodeEmitter::
1424getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1425 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001426 const MCOperand &MO = MI.getOperand(Op);
1427 if (MO.getReg() == 0) return 0x0D;
1428 return MO.getReg();
Owen Anderson526ffd52010-11-02 01:24:55 +00001429}
1430
Bill Wendling3b1459b2011-03-01 01:00:59 +00001431unsigned ARMMCCodeEmitter::
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001432getShiftRight8Imm(const MCInst &MI, unsigned Op,
1433 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling3b1459b2011-03-01 01:00:59 +00001434 return 8 - MI.getOperand(Op).getImm();
1435}
1436
1437unsigned ARMMCCodeEmitter::
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001438getShiftRight16Imm(const MCInst &MI, unsigned Op,
1439 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling3b1459b2011-03-01 01:00:59 +00001440 return 16 - MI.getOperand(Op).getImm();
1441}
1442
1443unsigned ARMMCCodeEmitter::
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001444getShiftRight32Imm(const MCInst &MI, unsigned Op,
1445 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling3b1459b2011-03-01 01:00:59 +00001446 return 32 - MI.getOperand(Op).getImm();
1447}
1448
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001449unsigned ARMMCCodeEmitter::
1450getShiftRight64Imm(const MCInst &MI, unsigned Op,
1451 SmallVectorImpl<MCFixup> &Fixups) const {
1452 return 64 - MI.getOperand(Op).getImm();
1453}
1454
Jim Grosbach1287f4f2010-09-17 18:46:17 +00001455void ARMMCCodeEmitter::
1456EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001457 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach91029092010-10-07 22:12:50 +00001458 // Pseudo instructions don't get encoded.
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001459 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Jim Grosbach20b6fd72010-11-11 23:41:09 +00001460 uint64_t TSFlags = Desc.TSFlags;
1461 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbach91029092010-10-07 22:12:50 +00001462 return;
Owen Anderson651b2302011-07-13 23:22:26 +00001463
Jim Grosbach20b6fd72010-11-11 23:41:09 +00001464 int Size;
Owen Anderson651b2302011-07-13 23:22:26 +00001465 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1466 Size = Desc.getSize();
1467 else
1468 llvm_unreachable("Unexpected instruction size!");
Owen Anderson1732c2e2011-08-30 21:58:18 +00001469
Jim Grosbach567ebd0c2010-12-03 22:31:40 +00001470 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
Evan Cheng965b3c72011-01-13 07:58:56 +00001471 // Thumb 32-bit wide instructions need to emit the high order halfword
1472 // first.
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001473 if (isThumb() && Size == 4) {
Jim Grosbach567ebd0c2010-12-03 22:31:40 +00001474 EmitConstant(Binary >> 16, 2, OS);
1475 EmitConstant(Binary & 0xffff, 2, OS);
1476 } else
1477 EmitConstant(Binary, Size, OS);
Bill Wendling91da9ab2010-11-02 22:44:12 +00001478 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach1287f4f2010-09-17 18:46:17 +00001479}
Jim Grosbach8aed3862010-10-07 21:57:55 +00001480
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001481#include "ARMGenMCCodeEmitter.inc"