blob: e378a9cf68ed7cfe6bf437906160b048772e0f57 [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.
Owen Andersond845d9d2012-01-24 18:37:29 +000067 uint64_t getBinaryCodeForInstr(const MCInst &MI,
Jim Grosbach2eed7a12010-11-03 23:52:49 +000068 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;
Jim Grosbach7b811d32012-02-27 21:36:23 +0000121 uint32_t getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
122 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersonb205c022011-08-26 23:32:08 +0000123 uint32_t getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbach7b811d32012-02-27 21:36:23 +0000124 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson578074b2010-12-13 19:31:11 +0000125
Jim Grosbachdc35e062010-12-01 19:47:31 +0000126 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
127 /// ADR label target.
128 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000130 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
131 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6d375e52010-12-14 00:36:49 +0000132 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
133 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000134
Jim Grosbachdc35e062010-12-01 19:47:31 +0000135
Bill Wendlinge84eb992010-11-03 01:49:29 +0000136 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
137 /// operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000138 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
139 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendlinge84eb992010-11-03 01:49:29 +0000140
Bill Wendling092a7bd2010-12-14 03:36:38 +0000141 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
142 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
143 SmallVectorImpl<MCFixup> &Fixups)const;
Owen Andersonb0fa1272010-12-10 22:11:13 +0000144
Owen Anderson943fb602010-12-01 19:18:46 +0000145 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
146 /// operand.
147 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
148 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbacha05627e2011-09-09 18:37:27 +0000149
150 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for 'reg + imm8<<2'
151 /// operand.
152 uint32_t getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
153 SmallVectorImpl<MCFixup> &Fixups) const;
154
Jim Grosbach7db8d692011-09-08 22:07:06 +0000155 /// getT2Imm8s4OpValue - Return encoding info for '+/- imm8<<2'
156 /// operand.
157 uint32_t getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
158 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson943fb602010-12-01 19:18:46 +0000159
160
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000161 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
162 /// operand as needed by load/store instructions.
163 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
164 SmallVectorImpl<MCFixup> &Fixups) const;
165
Jim Grosbachcc4a4912010-11-10 23:38:36 +0000166 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
167 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
168 SmallVectorImpl<MCFixup> &Fixups) const {
169 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
170 switch (Mode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000171 default: llvm_unreachable("Unknown addressing sub-mode!");
Jim Grosbachcc4a4912010-11-10 23:38:36 +0000172 case ARM_AM::da: return 0;
173 case ARM_AM::ia: return 1;
174 case ARM_AM::db: return 2;
175 case ARM_AM::ib: return 3;
176 }
177 }
Jim Grosbach38b469e2010-11-15 20:47:07 +0000178 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
179 ///
180 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
181 switch (ShOpc) {
Jim Grosbach38b469e2010-11-15 20:47:07 +0000182 case ARM_AM::no_shift:
183 case ARM_AM::lsl: return 0;
184 case ARM_AM::lsr: return 1;
185 case ARM_AM::asr: return 2;
186 case ARM_AM::ror:
187 case ARM_AM::rrx: return 3;
188 }
David Blaikie46a9f012012-01-20 21:51:11 +0000189 llvm_unreachable("Invalid ShiftOpc!");
Jim Grosbach38b469e2010-11-15 20:47:07 +0000190 }
191
192 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
193 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
194 SmallVectorImpl<MCFixup> &Fixups) const;
195
196 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
197 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
198 SmallVectorImpl<MCFixup> &Fixups) const;
199
Jim Grosbachd3595712011-08-03 23:50:40 +0000200 /// getPostIdxRegOpValue - Return encoding for postidx_reg operands.
201 uint32_t getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
202 SmallVectorImpl<MCFixup> &Fixups) const;
203
Jim Grosbach68685e62010-11-11 16:55:29 +0000204 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
205 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
206 SmallVectorImpl<MCFixup> &Fixups) const;
207
Jim Grosbach607efcb2010-11-11 01:09:40 +0000208 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
209 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
210 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachcc4a4912010-11-10 23:38:36 +0000211
Jim Grosbach49bcd6f2010-12-07 21:50:47 +0000212 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
213 /// operand.
214 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
215 SmallVectorImpl<MCFixup> &Fixups) const;
216
Bill Wendling092a7bd2010-12-14 03:36:38 +0000217 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
218 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling03e75762010-12-15 08:51:02 +0000219 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendlinga9e3df72010-11-30 22:57:21 +0000220
Bill Wendling8a6449c2010-12-08 01:57:09 +0000221 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
222 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
223 SmallVectorImpl<MCFixup> &Fixups) const;
224
Bill Wendlinge84eb992010-11-03 01:49:29 +0000225 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000226 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
227 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000228
Jim Grosbachd9d31da2010-10-12 23:00:24 +0000229 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000230 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
231 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd9d31da2010-10-12 23:00:24 +0000232 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
233 // '1' respectively.
234 return MI.getOperand(Op).getReg() == ARM::CPSR;
235 }
Jim Grosbachefd53692010-10-12 23:53:58 +0000236
Jim Grosbach12e493a2010-10-12 23:18:08 +0000237 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000238 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
239 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach12e493a2010-10-12 23:18:08 +0000240 unsigned SoImm = MI.getOperand(Op).getImm();
241 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
242 assert(SoImmVal != -1 && "Not a valid so_imm value!");
243
244 // Encode rotate_imm.
245 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
246 << ARMII::SoRotImmShift;
247
248 // Encode immed_8.
249 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
250 return Binary;
251 }
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000252
Owen Anderson8fdd1722010-11-12 21:12:40 +0000253 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
254 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
255 SmallVectorImpl<MCFixup> &Fixups) const {
256 unsigned SoImm = MI.getOperand(Op).getImm();
257 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
258 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
259 return Encoded;
260 }
Jim Grosbachd9d31da2010-10-12 23:00:24 +0000261
Owen Anderson50d662b2010-11-29 22:44:32 +0000262 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
263 SmallVectorImpl<MCFixup> &Fixups) const;
264 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
265 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersone22c7322010-11-30 00:14:31 +0000266 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
267 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson299382e2010-11-30 19:19:31 +0000268 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
269 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson50d662b2010-11-29 22:44:32 +0000270
Jim Grosbachefd53692010-10-12 23:53:58 +0000271 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Owen Anderson04912702011-07-21 23:38:37 +0000272 unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op,
273 SmallVectorImpl<MCFixup> &Fixups) const;
274 unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op,
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000275 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson8fdd1722010-11-12 21:12:40 +0000276 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
277 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachefd53692010-10-12 23:53:58 +0000278
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000279 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
280 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfadb9512010-10-27 22:49:00 +0000281 return 64 - MI.getOperand(Op).getImm();
282 }
Jim Grosbach68a335e2010-10-15 17:15:16 +0000283
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000284 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
285 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5edb03e2010-10-21 22:03:21 +0000286
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000287 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
288 SmallVectorImpl<MCFixup> &Fixups) const;
289 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
290 SmallVectorImpl<MCFixup> &Fixups) const;
Mon P Wang92ff16b2011-05-09 17:47:27 +0000291 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
292 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson318ce7c2010-11-30 00:00:42 +0000293 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
294 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000295 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
296 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach74ef9e12010-10-30 00:37:59 +0000297
Bill Wendling77ad1dc2011-03-07 23:38:41 +0000298 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
299 SmallVectorImpl<MCFixup> &Fixups) const;
300 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
301 SmallVectorImpl<MCFixup> &Fixups) const;
302 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
303 SmallVectorImpl<MCFixup> &Fixups) const;
304 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
305 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling3b1459b2011-03-01 01:00:59 +0000306
Owen Andersonc4030382011-08-08 20:42:17 +0000307 unsigned getThumbSRImmOpValue(const MCInst &MI, unsigned Op,
308 SmallVectorImpl<MCFixup> &Fixups) const;
309
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000310 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
311 unsigned EncodedValue) const;
Owen Anderson99a8cb42010-11-11 21:36:43 +0000312 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendling87240d42010-12-01 21:54:50 +0000313 unsigned EncodedValue) const;
Owen Andersonce2250f2010-11-11 23:12:55 +0000314 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendling87240d42010-12-01 21:54:50 +0000315 unsigned EncodedValue) const;
316
317 unsigned VFPThumb2PostEncoder(const MCInst &MI,
318 unsigned EncodedValue) const;
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000319
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000320 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000321 OS << (char)C;
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000322 }
323
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000324 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000325 // Output the constant in little endian byte order.
326 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000327 EmitByte(Val & 255, OS);
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000328 Val >>= 8;
329 }
330 }
331
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000332 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
333 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000334};
335
336} // end anonymous namespace
337
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000338MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
339 const MCSubtargetInfo &STI,
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000340 MCContext &Ctx) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000341 return new ARMMCCodeEmitter(MCII, STI, Ctx);
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000342}
343
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000344/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
345/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000346/// Thumb2 mode.
347unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
348 unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000349 if (isThumb2()) {
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000350 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000351 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
352 // set to 1111.
353 unsigned Bit24 = EncodedValue & 0x01000000;
354 unsigned Bit28 = Bit24 << 4;
355 EncodedValue &= 0xEFFFFFFF;
356 EncodedValue |= Bit28;
357 EncodedValue |= 0x0F000000;
358 }
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000359
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000360 return EncodedValue;
361}
362
Owen Anderson99a8cb42010-11-11 21:36:43 +0000363/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000364/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson99a8cb42010-11-11 21:36:43 +0000365/// Thumb2 mode.
366unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
367 unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000368 if (isThumb2()) {
Owen Anderson99a8cb42010-11-11 21:36:43 +0000369 EncodedValue &= 0xF0FFFFFF;
370 EncodedValue |= 0x09000000;
371 }
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000372
Owen Anderson99a8cb42010-11-11 21:36:43 +0000373 return EncodedValue;
374}
375
Owen Andersonce2250f2010-11-11 23:12:55 +0000376/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000377/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonce2250f2010-11-11 23:12:55 +0000378/// Thumb2 mode.
379unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
380 unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000381 if (isThumb2()) {
Owen Andersonce2250f2010-11-11 23:12:55 +0000382 EncodedValue &= 0x00FFFFFF;
383 EncodedValue |= 0xEE000000;
384 }
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000385
Owen Andersonce2250f2010-11-11 23:12:55 +0000386 return EncodedValue;
387}
388
Bill Wendling87240d42010-12-01 21:54:50 +0000389/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
390/// them to their Thumb2 form if we are currently in Thumb2 mode.
391unsigned ARMMCCodeEmitter::
392VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000393 if (isThumb2()) {
Bill Wendling87240d42010-12-01 21:54:50 +0000394 EncodedValue &= 0x0FFFFFFF;
395 EncodedValue |= 0xE0000000;
396 }
397 return EncodedValue;
398}
Owen Anderson99a8cb42010-11-11 21:36:43 +0000399
Jim Grosbachc43c9302010-10-08 21:45:55 +0000400/// getMachineOpValue - Return binary encoding of operand. If the machine
401/// operand requires relocation, record the relocation and return zero.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000402unsigned ARMMCCodeEmitter::
403getMachineOpValue(const MCInst &MI, const MCOperand &MO,
404 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6f52f8a2010-10-14 02:33:26 +0000405 if (MO.isReg()) {
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000406 unsigned Reg = MO.getReg();
407 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbach96d82842010-10-29 23:21:03 +0000408
Jim Grosbachee48d2d2010-11-30 23:51:41 +0000409 // Q registers are encoded as 2x their register number.
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000410 switch (Reg) {
411 default:
412 return RegNo;
413 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
414 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
415 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
416 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
417 return 2 * RegNo;
Owen Anderson2bfa8ed2010-10-21 20:49:13 +0000418 }
Bill Wendling6f52f8a2010-10-14 02:33:26 +0000419 } else if (MO.isImm()) {
Jim Grosbachc43c9302010-10-08 21:45:55 +0000420 return static_cast<unsigned>(MO.getImm());
Bill Wendling6f52f8a2010-10-14 02:33:26 +0000421 } else if (MO.isFPImm()) {
422 return static_cast<unsigned>(APFloat(MO.getFPImm())
423 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbachc43c9302010-10-08 21:45:55 +0000424 }
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000425
Jim Grosbach2aeb8b92010-11-19 00:27:09 +0000426 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbachc43c9302010-10-08 21:45:55 +0000427}
428
Bill Wendling603bd8f2010-11-02 22:31:46 +0000429/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000430bool ARMMCCodeEmitter::
431EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
432 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000433 const MCOperand &MO = MI.getOperand(OpIdx);
434 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach2ba03aa2010-11-01 23:45:50 +0000435
Bill Wendlinge84eb992010-11-03 01:49:29 +0000436 Reg = getARMRegisterNumbering(MO.getReg());
437
438 int32_t SImm = MO1.getImm();
439 bool isAdd = true;
Bill Wendling603bd8f2010-11-02 22:31:46 +0000440
Jim Grosbach505607e2010-10-28 18:34:10 +0000441 // Special value for #-0
Owen Anderson967674d2011-08-29 19:36:44 +0000442 if (SImm == INT32_MIN) {
Bill Wendlinge84eb992010-11-03 01:49:29 +0000443 SImm = 0;
Owen Anderson967674d2011-08-29 19:36:44 +0000444 isAdd = false;
445 }
Bill Wendling603bd8f2010-11-02 22:31:46 +0000446
Jim Grosbach505607e2010-10-28 18:34:10 +0000447 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendlinge84eb992010-11-03 01:49:29 +0000448 if (SImm < 0) {
449 SImm = -SImm;
450 isAdd = false;
451 }
Bill Wendling603bd8f2010-11-02 22:31:46 +0000452
Bill Wendlinge84eb992010-11-03 01:49:29 +0000453 Imm = SImm;
454 return isAdd;
455}
456
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000457/// getBranchTargetOpValue - Helper function to get the branch target operand,
458/// which is either an immediate or requires a fixup.
459static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
460 unsigned FixupKind,
461 SmallVectorImpl<MCFixup> &Fixups) {
462 const MCOperand &MO = MI.getOperand(OpIdx);
463
464 // If the destination is an immediate, we have nothing to do.
465 if (MO.isImm()) return MO.getImm();
466 assert(MO.isExpr() && "Unexpected branch target type!");
467 const MCExpr *Expr = MO.getExpr();
468 MCFixupKind Kind = MCFixupKind(FixupKind);
Jim Grosbach5e5eabb2012-01-26 23:20:15 +0000469 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000470
471 // All of the information is in the fixup.
472 return 0;
473}
474
Owen Anderson5c160fd2011-08-31 18:30:20 +0000475// Thumb BL and BLX use a strange offset encoding where bits 22 and 21 are
476// determined by negating them and XOR'ing them with bit 23.
477static int32_t encodeThumbBLOffset(int32_t offset) {
478 offset >>= 1;
479 uint32_t S = (offset & 0x800000) >> 23;
480 uint32_t J1 = (offset & 0x400000) >> 22;
481 uint32_t J2 = (offset & 0x200000) >> 21;
482 J1 = (~J1 & 0x1);
483 J2 = (~J2 & 0x1);
484 J1 ^= S;
485 J2 ^= S;
486
487 offset &= ~0x600000;
488 offset |= J1 << 22;
489 offset |= J2 << 21;
490
491 return offset;
492}
493
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000494/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach9e199462010-12-06 23:57:07 +0000495uint32_t ARMMCCodeEmitter::
496getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
497 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson5c160fd2011-08-31 18:30:20 +0000498 const MCOperand MO = MI.getOperand(OpIdx);
499 if (MO.isExpr())
500 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl,
501 Fixups);
502 return encodeThumbBLOffset(MO.getImm());
Jim Grosbach9e199462010-12-06 23:57:07 +0000503}
504
Bill Wendling3392bfc2010-12-09 00:39:08 +0000505/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
506/// BLX branch target.
507uint32_t ARMMCCodeEmitter::
508getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
509 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson5c160fd2011-08-31 18:30:20 +0000510 const MCOperand MO = MI.getOperand(OpIdx);
511 if (MO.isExpr())
512 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx,
513 Fixups);
514 return encodeThumbBLOffset(MO.getImm());
Bill Wendling3392bfc2010-12-09 00:39:08 +0000515}
516
Jim Grosbache119da12010-12-10 18:21:33 +0000517/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
518uint32_t ARMMCCodeEmitter::
519getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
520 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson543c89f2011-08-30 22:03:20 +0000521 const MCOperand MO = MI.getOperand(OpIdx);
522 if (MO.isExpr())
Owen Anderson5c160fd2011-08-31 18:30:20 +0000523 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br,
524 Fixups);
Owen Anderson543c89f2011-08-30 22:03:20 +0000525 return (MO.getImm() >> 1);
Jim Grosbache119da12010-12-10 18:21:33 +0000526}
527
Jim Grosbach78485ad2010-12-10 17:13:40 +0000528/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
529uint32_t ARMMCCodeEmitter::
530getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache119da12010-12-10 18:21:33 +0000531 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona455a0b2011-08-31 20:26:14 +0000532 const MCOperand MO = MI.getOperand(OpIdx);
533 if (MO.isExpr())
534 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc,
535 Fixups);
536 return (MO.getImm() >> 1);
Jim Grosbach78485ad2010-12-10 17:13:40 +0000537}
538
Jim Grosbach62b68112010-12-09 19:04:53 +0000539/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000540uint32_t ARMMCCodeEmitter::
Jim Grosbach62b68112010-12-09 19:04:53 +0000541getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000542 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfdf3cd72011-08-30 22:15:17 +0000543 const MCOperand MO = MI.getOperand(OpIdx);
544 if (MO.isExpr())
545 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
546 return (MO.getImm() >> 1);
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000547}
548
Jason W Kimd2e2f562011-02-04 19:47:15 +0000549/// Return true if this branch has a non-always predication
550static bool HasConditionalBranch(const MCInst &MI) {
551 int NumOp = MI.getNumOperands();
552 if (NumOp >= 2) {
553 for (int i = 0; i < NumOp-1; ++i) {
554 const MCOperand &MCOp1 = MI.getOperand(i);
555 const MCOperand &MCOp2 = MI.getOperand(i + 1);
Owen Anderson1732c2e2011-08-30 21:58:18 +0000556 if (MCOp1.isImm() && MCOp2.isReg() &&
Jason W Kimd2e2f562011-02-04 19:47:15 +0000557 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
Owen Anderson1732c2e2011-08-30 21:58:18 +0000558 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
Jason W Kimd2e2f562011-02-04 19:47:15 +0000559 return true;
560 }
561 }
562 }
563 return false;
564}
565
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000566/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
567/// target.
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000568uint32_t ARMMCCodeEmitter::
569getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000570 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachaecdd872010-12-10 23:41:10 +0000571 // FIXME: This really, really shouldn't use TargetMachine. We don't want
572 // coupling between MC and TM anywhere we can help it.
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000573 if (isThumb2())
Owen Anderson578074b2010-12-13 19:31:11 +0000574 return
575 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Jason W Kimd2e2f562011-02-04 19:47:15 +0000576 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000577}
578
Jason W Kimd2e2f562011-02-04 19:47:15 +0000579/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
580/// target.
581uint32_t ARMMCCodeEmitter::
582getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
583 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson6c70e582011-08-26 22:54:51 +0000584 const MCOperand MO = MI.getOperand(OpIdx);
585 if (MO.isExpr()) {
Owen Anderson1732c2e2011-08-30 21:58:18 +0000586 if (HasConditionalBranch(MI))
Owen Anderson6c70e582011-08-26 22:54:51 +0000587 return ::getBranchTargetOpValue(MI, OpIdx,
588 ARM::fixup_arm_condbranch, Fixups);
Owen Anderson1732c2e2011-08-30 21:58:18 +0000589 return ::getBranchTargetOpValue(MI, OpIdx,
Owen Anderson6c70e582011-08-26 22:54:51 +0000590 ARM::fixup_arm_uncondbranch, Fixups);
591 }
592
593 return MO.getImm() >> 2;
Jason W Kimd2e2f562011-02-04 19:47:15 +0000594}
595
Owen Andersonb205c022011-08-26 23:32:08 +0000596uint32_t ARMMCCodeEmitter::
Jim Grosbach7b811d32012-02-27 21:36:23 +0000597getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
598 SmallVectorImpl<MCFixup> &Fixups) const {
599 const MCOperand MO = MI.getOperand(OpIdx);
600 if (MO.isExpr())
601 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_bl, Fixups);
602
603 return MO.getImm() >> 2;
604}
605
606uint32_t ARMMCCodeEmitter::
Owen Andersonb205c022011-08-26 23:32:08 +0000607getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
608 SmallVectorImpl<MCFixup> &Fixups) const {
609 const MCOperand MO = MI.getOperand(OpIdx);
Jim Grosbach7b811d32012-02-27 21:36:23 +0000610 if (MO.isExpr())
611 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_blx, Fixups);
Jason W Kimd2e2f562011-02-04 19:47:15 +0000612
Owen Andersonb205c022011-08-26 23:32:08 +0000613 return MO.getImm() >> 1;
614}
Jason W Kimd2e2f562011-02-04 19:47:15 +0000615
Owen Anderson578074b2010-12-13 19:31:11 +0000616/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
617/// immediate branch target.
618uint32_t ARMMCCodeEmitter::
619getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
620 SmallVectorImpl<MCFixup> &Fixups) const {
621 unsigned Val =
622 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
623 bool I = (Val & 0x800000);
624 bool J1 = (Val & 0x400000);
625 bool J2 = (Val & 0x200000);
626 if (I ^ J1)
627 Val &= ~0x400000;
628 else
629 Val |= 0x400000;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000630
Owen Anderson578074b2010-12-13 19:31:11 +0000631 if (I ^ J2)
632 Val &= ~0x200000;
633 else
634 Val |= 0x200000;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000635
Owen Anderson578074b2010-12-13 19:31:11 +0000636 return Val;
637}
638
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000639/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
640/// target.
Jim Grosbachdc35e062010-12-01 19:47:31 +0000641uint32_t ARMMCCodeEmitter::
642getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
643 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona01bcbf2011-08-26 18:09:22 +0000644 const MCOperand MO = MI.getOperand(OpIdx);
645 if (MO.isExpr())
646 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
647 Fixups);
648 int32_t offset = MO.getImm();
649 uint32_t Val = 0x2000;
650 if (offset < 0) {
651 Val = 0x1000;
652 offset *= -1;
653 }
654 Val |= offset;
655 return Val;
Jim Grosbachdc35e062010-12-01 19:47:31 +0000656}
657
Owen Anderson6d375e52010-12-14 00:36:49 +0000658/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
659/// target.
660uint32_t ARMMCCodeEmitter::
661getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
662 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona01bcbf2011-08-26 18:09:22 +0000663 const MCOperand MO = MI.getOperand(OpIdx);
664 if (MO.isExpr())
665 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
666 Fixups);
Owen Anderson5bfb0e02011-09-09 22:24:36 +0000667 int32_t Val = MO.getImm();
668 if (Val < 0) {
669 Val *= -1;
670 Val |= 0x1000;
671 }
672 return Val;
Owen Anderson6d375e52010-12-14 00:36:49 +0000673}
674
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000675/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
676/// target.
677uint32_t ARMMCCodeEmitter::
678getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
679 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona01bcbf2011-08-26 18:09:22 +0000680 const MCOperand MO = MI.getOperand(OpIdx);
681 if (MO.isExpr())
682 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
683 Fixups);
684 return MO.getImm();
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000685}
686
Bill Wendling092a7bd2010-12-14 03:36:38 +0000687/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
688/// operand.
Owen Andersonb0fa1272010-12-10 22:11:13 +0000689uint32_t ARMMCCodeEmitter::
Bill Wendling092a7bd2010-12-14 03:36:38 +0000690getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
691 SmallVectorImpl<MCFixup> &) const {
692 // [Rn, Rm]
693 // {5-3} = Rm
694 // {2-0} = Rn
Owen Andersonb0fa1272010-12-10 22:11:13 +0000695 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendling092a7bd2010-12-14 03:36:38 +0000696 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Andersonb0fa1272010-12-10 22:11:13 +0000697 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
698 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
699 return (Rm << 3) | Rn;
700}
701
Bill Wendlinge84eb992010-11-03 01:49:29 +0000702/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000703uint32_t ARMMCCodeEmitter::
704getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
705 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinge84eb992010-11-03 01:49:29 +0000706 // {17-13} = reg
707 // {12} = (U)nsigned (add == '1', sub == '0')
708 // {11-0} = imm12
709 unsigned Reg, Imm12;
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000710 bool isAdd = true;
711 // If The first operand isn't a register, we have a label reference.
712 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Anderson4ebf4712011-02-08 22:39:40 +0000713 if (!MO.isReg()) {
Jim Grosbach90987142010-11-09 01:37:15 +0000714 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000715 Imm12 = 0;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +0000716 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000717
Owen Anderson4a9eb5f2011-09-12 20:36:51 +0000718 if (MO.isExpr()) {
719 const MCExpr *Expr = MO.getExpr();
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000720
Owen Anderson4a9eb5f2011-09-12 20:36:51 +0000721 MCFixupKind Kind;
722 if (isThumb2())
723 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
724 else
725 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach5e5eabb2012-01-26 23:20:15 +0000726 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000727
Owen Anderson4a9eb5f2011-09-12 20:36:51 +0000728 ++MCNumCPRelocations;
729 } else {
730 Reg = ARM::PC;
731 int32_t Offset = MO.getImm();
Jim Grosbach94298a92012-01-18 22:46:46 +0000732 // FIXME: Handle #-0.
Owen Anderson4a9eb5f2011-09-12 20:36:51 +0000733 if (Offset < 0) {
734 Offset *= -1;
735 isAdd = false;
736 }
737 Imm12 = Offset;
738 }
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000739 } else
740 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendlinge84eb992010-11-03 01:49:29 +0000741
Bill Wendlinge84eb992010-11-03 01:49:29 +0000742 uint32_t Binary = Imm12 & 0xfff;
743 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach505607e2010-10-28 18:34:10 +0000744 if (isAdd)
Bill Wendlinge84eb992010-11-03 01:49:29 +0000745 Binary |= (1 << 12);
746 Binary |= (Reg << 13);
747 return Binary;
748}
749
Jim Grosbach7db8d692011-09-08 22:07:06 +0000750/// getT2Imm8s4OpValue - Return encoding info for
751/// '+/- imm8<<2' operand.
752uint32_t ARMMCCodeEmitter::
753getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
754 SmallVectorImpl<MCFixup> &Fixups) const {
755 // FIXME: The immediate operand should have already been encoded like this
756 // before ever getting here. The encoder method should just need to combine
757 // the MI operands for the register and the offset into a single
758 // representation for the complex operand in the .td file. This isn't just
759 // style, unfortunately. As-is, we can't represent the distinct encoding
760 // for #-0.
761
762 // {8} = (U)nsigned (add == '1', sub == '0')
763 // {7-0} = imm8
764 int32_t Imm8 = MI.getOperand(OpIdx).getImm();
765 bool isAdd = Imm8 >= 0;
766
767 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
768 if (Imm8 < 0)
769 Imm8 = -Imm8;
770
771 // Scaled by 4.
772 Imm8 /= 4;
773
774 uint32_t Binary = Imm8 & 0xff;
775 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
776 if (isAdd)
777 Binary |= (1 << 8);
778 return Binary;
779}
780
Owen Anderson943fb602010-12-01 19:18:46 +0000781/// getT2AddrModeImm8s4OpValue - Return encoding info for
782/// 'reg +/- imm8<<2' operand.
783uint32_t ARMMCCodeEmitter::
784getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
785 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbache69f7242010-12-10 21:05:07 +0000786 // {12-9} = reg
787 // {8} = (U)nsigned (add == '1', sub == '0')
788 // {7-0} = imm8
Owen Anderson943fb602010-12-01 19:18:46 +0000789 unsigned Reg, Imm8;
790 bool isAdd = true;
791 // If The first operand isn't a register, we have a label reference.
792 const MCOperand &MO = MI.getOperand(OpIdx);
793 if (!MO.isReg()) {
794 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
795 Imm8 = 0;
796 isAdd = false ; // 'U' bit is set as part of the fixup.
797
798 assert(MO.isExpr() && "Unexpected machine operand type!");
799 const MCExpr *Expr = MO.getExpr();
Jim Grosbach8648c102011-12-19 23:06:24 +0000800 MCFixupKind Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
Jim Grosbach5e5eabb2012-01-26 23:20:15 +0000801 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
Owen Anderson943fb602010-12-01 19:18:46 +0000802
803 ++MCNumCPRelocations;
804 } else
805 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
806
Jim Grosbach7db8d692011-09-08 22:07:06 +0000807 // FIXME: The immediate operand should have already been encoded like this
808 // before ever getting here. The encoder method should just need to combine
809 // the MI operands for the register and the offset into a single
810 // representation for the complex operand in the .td file. This isn't just
811 // style, unfortunately. As-is, we can't represent the distinct encoding
812 // for #-0.
Owen Anderson943fb602010-12-01 19:18:46 +0000813 uint32_t Binary = (Imm8 >> 2) & 0xff;
814 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
815 if (isAdd)
Jim Grosbache69f7242010-12-10 21:05:07 +0000816 Binary |= (1 << 8);
Owen Anderson943fb602010-12-01 19:18:46 +0000817 Binary |= (Reg << 9);
818 return Binary;
819}
820
Jim Grosbacha05627e2011-09-09 18:37:27 +0000821/// getT2AddrModeImm0_1020s4OpValue - Return encoding info for
822/// 'reg + imm8<<2' operand.
823uint32_t ARMMCCodeEmitter::
824getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
825 SmallVectorImpl<MCFixup> &Fixups) const {
826 // {11-8} = reg
827 // {7-0} = imm8
828 const MCOperand &MO = MI.getOperand(OpIdx);
829 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
830 unsigned Reg = getARMRegisterNumbering(MO.getReg());
831 unsigned Imm8 = MO1.getImm();
832 return (Reg << 8) | Imm8;
833}
834
Jason W Kim9c5b65d2011-01-12 00:19:25 +0000835// FIXME: This routine assumes that a binary
836// expression will always result in a PCRel expression
837// In reality, its only true if one or more subexpressions
838// is itself a PCRel (i.e. "." in asm or some other pcrel construct)
839// but this is good enough for now.
840static bool EvaluateAsPCRel(const MCExpr *Expr) {
841 switch (Expr->getKind()) {
Craig Toppere55c5562012-02-07 02:50:20 +0000842 default: llvm_unreachable("Unexpected expression type");
Jason W Kim9c5b65d2011-01-12 00:19:25 +0000843 case MCExpr::SymbolRef: return false;
844 case MCExpr::Binary: return true;
Jason W Kim9c5b65d2011-01-12 00:19:25 +0000845 }
846}
847
Evan Cheng965b3c72011-01-13 07:58:56 +0000848uint32_t
849ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
850 SmallVectorImpl<MCFixup> &Fixups) const {
Jason W Kim5a97bd82010-11-18 23:37:15 +0000851 // {20-16} = imm{15-12}
852 // {11-0} = imm{11-0}
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000853 const MCOperand &MO = MI.getOperand(OpIdx);
Evan Cheng965b3c72011-01-13 07:58:56 +0000854 if (MO.isImm())
855 // Hi / lo 16 bits already extracted during earlier passes.
Jason W Kim5a97bd82010-11-18 23:37:15 +0000856 return static_cast<unsigned>(MO.getImm());
Evan Cheng965b3c72011-01-13 07:58:56 +0000857
858 // Handle :upper16: and :lower16: assembly prefixes.
859 const MCExpr *E = MO.getExpr();
860 if (E->getKind() == MCExpr::Target) {
861 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
862 E = ARM16Expr->getSubExpr();
863
Jason W Kim5a97bd82010-11-18 23:37:15 +0000864 MCFixupKind Kind;
Evan Cheng965b3c72011-01-13 07:58:56 +0000865 switch (ARM16Expr->getKind()) {
Craig Toppere55c5562012-02-07 02:50:20 +0000866 default: llvm_unreachable("Unsupported ARMFixup");
Evan Cheng965b3c72011-01-13 07:58:56 +0000867 case ARMMCExpr::VK_ARM_HI16:
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000868 if (!isTargetDarwin() && EvaluateAsPCRel(E))
869 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000870 ? ARM::fixup_t2_movt_hi16_pcrel
871 : ARM::fixup_arm_movt_hi16_pcrel);
872 else
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000873 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000874 ? ARM::fixup_t2_movt_hi16
875 : ARM::fixup_arm_movt_hi16);
Jason W Kim5a97bd82010-11-18 23:37:15 +0000876 break;
Evan Cheng965b3c72011-01-13 07:58:56 +0000877 case ARMMCExpr::VK_ARM_LO16:
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000878 if (!isTargetDarwin() && EvaluateAsPCRel(E))
879 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000880 ? ARM::fixup_t2_movw_lo16_pcrel
881 : ARM::fixup_arm_movw_lo16_pcrel);
882 else
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000883 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000884 ? ARM::fixup_t2_movw_lo16
885 : ARM::fixup_arm_movw_lo16);
Jason W Kim5a97bd82010-11-18 23:37:15 +0000886 break;
Jason W Kim5a97bd82010-11-18 23:37:15 +0000887 }
Jim Grosbach5e5eabb2012-01-26 23:20:15 +0000888 Fixups.push_back(MCFixup::Create(0, E, Kind, MI.getLoc()));
Jason W Kim5a97bd82010-11-18 23:37:15 +0000889 return 0;
Jim Grosbach2aeb8b92010-11-19 00:27:09 +0000890 };
Evan Cheng965b3c72011-01-13 07:58:56 +0000891
Jim Grosbach2aeb8b92010-11-19 00:27:09 +0000892 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim5a97bd82010-11-18 23:37:15 +0000893}
894
895uint32_t ARMMCCodeEmitter::
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000896getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
897 SmallVectorImpl<MCFixup> &Fixups) const {
898 const MCOperand &MO = MI.getOperand(OpIdx);
899 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
900 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
901 unsigned Rn = getARMRegisterNumbering(MO.getReg());
902 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000903 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
904 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach38b469e2010-11-15 20:47:07 +0000905 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
906 unsigned SBits = getShiftOp(ShOp);
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000907
908 // {16-13} = Rn
909 // {12} = isAdd
910 // {11-0} = shifter
911 // {3-0} = Rm
912 // {4} = 0
913 // {6-5} = type
914 // {11-7} = imm
Jim Grosbach607efcb2010-11-11 01:09:40 +0000915 uint32_t Binary = Rm;
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000916 Binary |= Rn << 13;
917 Binary |= SBits << 5;
918 Binary |= ShImm << 7;
919 if (isAdd)
920 Binary |= 1 << 12;
921 return Binary;
922}
923
Jim Grosbach607efcb2010-11-11 01:09:40 +0000924uint32_t ARMMCCodeEmitter::
Jim Grosbach38b469e2010-11-15 20:47:07 +0000925getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
926 SmallVectorImpl<MCFixup> &Fixups) const {
927 // {17-14} Rn
928 // {13} 1 == imm12, 0 == Rm
929 // {12} isAdd
930 // {11-0} imm12/Rm
931 const MCOperand &MO = MI.getOperand(OpIdx);
932 unsigned Rn = getARMRegisterNumbering(MO.getReg());
933 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
934 Binary |= Rn << 14;
935 return Binary;
936}
937
938uint32_t ARMMCCodeEmitter::
939getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
940 SmallVectorImpl<MCFixup> &Fixups) const {
941 // {13} 1 == imm12, 0 == Rm
942 // {12} isAdd
943 // {11-0} imm12/Rm
944 const MCOperand &MO = MI.getOperand(OpIdx);
945 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
946 unsigned Imm = MO1.getImm();
947 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
948 bool isReg = MO.getReg() != 0;
949 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
950 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
951 if (isReg) {
952 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
953 Binary <<= 7; // Shift amount is bits [11:7]
954 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
955 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
956 }
957 return Binary | (isAdd << 12) | (isReg << 13);
958}
959
960uint32_t ARMMCCodeEmitter::
Jim Grosbachd3595712011-08-03 23:50:40 +0000961getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
962 SmallVectorImpl<MCFixup> &Fixups) const {
963 // {4} isAdd
964 // {3-0} Rm
965 const MCOperand &MO = MI.getOperand(OpIdx);
966 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
Jim Grosbacha70fbfd52011-08-05 16:11:38 +0000967 bool isAdd = MO1.getImm() != 0;
Jim Grosbachd3595712011-08-03 23:50:40 +0000968 return getARMRegisterNumbering(MO.getReg()) | (isAdd << 4);
969}
970
971uint32_t ARMMCCodeEmitter::
Jim Grosbach68685e62010-11-11 16:55:29 +0000972getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
973 SmallVectorImpl<MCFixup> &Fixups) const {
974 // {9} 1 == imm8, 0 == Rm
975 // {8} isAdd
976 // {7-4} imm7_4/zero
977 // {3-0} imm3_0/Rm
978 const MCOperand &MO = MI.getOperand(OpIdx);
979 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
980 unsigned Imm = MO1.getImm();
981 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
982 bool isImm = MO.getReg() == 0;
983 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
984 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
985 if (!isImm)
986 Imm8 = getARMRegisterNumbering(MO.getReg());
987 return Imm8 | (isAdd << 8) | (isImm << 9);
988}
989
990uint32_t ARMMCCodeEmitter::
Jim Grosbach607efcb2010-11-11 01:09:40 +0000991getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
992 SmallVectorImpl<MCFixup> &Fixups) const {
993 // {13} 1 == imm8, 0 == Rm
994 // {12-9} Rn
995 // {8} isAdd
996 // {7-4} imm7_4/zero
997 // {3-0} imm3_0/Rm
998 const MCOperand &MO = MI.getOperand(OpIdx);
999 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1000 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
Jim Grosbach8648c102011-12-19 23:06:24 +00001001
1002 // If The first operand isn't a register, we have a label reference.
1003 if (!MO.isReg()) {
1004 unsigned Rn = getARMRegisterNumbering(ARM::PC); // Rn is PC.
1005
1006 assert(MO.isExpr() && "Unexpected machine operand type!");
1007 const MCExpr *Expr = MO.getExpr();
1008 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10_unscaled);
Jim Grosbach5e5eabb2012-01-26 23:20:15 +00001009 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
Jim Grosbach8648c102011-12-19 23:06:24 +00001010
1011 ++MCNumCPRelocations;
1012 return (Rn << 9) | (1 << 13);
1013 }
Jim Grosbach607efcb2010-11-11 01:09:40 +00001014 unsigned Rn = getARMRegisterNumbering(MO.getReg());
1015 unsigned Imm = MO2.getImm();
1016 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1017 bool isImm = MO1.getReg() == 0;
1018 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1019 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1020 if (!isImm)
1021 Imm8 = getARMRegisterNumbering(MO1.getReg());
1022 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
1023}
1024
Bill Wendling8a6449c2010-12-08 01:57:09 +00001025/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbach49bcd6f2010-12-07 21:50:47 +00001026uint32_t ARMMCCodeEmitter::
1027getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
1028 SmallVectorImpl<MCFixup> &Fixups) const {
1029 // [SP, #imm]
1030 // {7-0} = imm8
Jim Grosbach49bcd6f2010-12-07 21:50:47 +00001031 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendling8a6449c2010-12-08 01:57:09 +00001032 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
1033 "Unexpected base register!");
Bill Wendling7d3bde92010-12-15 23:32:27 +00001034
Jim Grosbach49bcd6f2010-12-07 21:50:47 +00001035 // The immediate is already shifted for the implicit zeroes, so no change
1036 // here.
1037 return MO1.getImm() & 0xff;
1038}
1039
Bill Wendling092a7bd2010-12-14 03:36:38 +00001040/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling0c4838b2010-12-09 21:49:07 +00001041uint32_t ARMMCCodeEmitter::
Bill Wendling092a7bd2010-12-14 03:36:38 +00001042getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling03e75762010-12-15 08:51:02 +00001043 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling811c9362010-11-30 07:44:32 +00001044 // [Rn, #imm]
1045 // {7-3} = imm5
1046 // {2-0} = Rn
1047 const MCOperand &MO = MI.getOperand(OpIdx);
1048 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendling811c9362010-11-30 07:44:32 +00001049 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Matt Beaumont-Gaye9afc742010-12-16 01:34:26 +00001050 unsigned Imm5 = MO1.getImm();
Bill Wendling0c4838b2010-12-09 21:49:07 +00001051 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendlinga9e3df72010-11-30 22:57:21 +00001052}
1053
Bill Wendling8a6449c2010-12-08 01:57:09 +00001054/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
1055uint32_t ARMMCCodeEmitter::
1056getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
1057 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond16fb432011-08-30 22:10:03 +00001058 const MCOperand MO = MI.getOperand(OpIdx);
1059 if (MO.isExpr())
1060 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
1061 return (MO.getImm() >> 2);
Bill Wendling8a6449c2010-12-08 01:57:09 +00001062}
1063
Jim Grosbach30eb6c72010-12-01 21:09:40 +00001064/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001065uint32_t ARMMCCodeEmitter::
1066getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
1067 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinge84eb992010-11-03 01:49:29 +00001068 // {12-9} = reg
1069 // {8} = (U)nsigned (add == '1', sub == '0')
1070 // {7-0} = imm8
1071 unsigned Reg, Imm8;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001072 bool isAdd;
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001073 // If The first operand isn't a register, we have a label reference.
1074 const MCOperand &MO = MI.getOperand(OpIdx);
1075 if (!MO.isReg()) {
Jim Grosbach90987142010-11-09 01:37:15 +00001076 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001077 Imm8 = 0;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001078 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001079
1080 assert(MO.isExpr() && "Unexpected machine operand type!");
1081 const MCExpr *Expr = MO.getExpr();
Owen Anderson0f7142d2010-12-08 00:18:36 +00001082 MCFixupKind Kind;
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001083 if (isThumb2())
Owen Anderson0f7142d2010-12-08 00:18:36 +00001084 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
1085 else
1086 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach5e5eabb2012-01-26 23:20:15 +00001087 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001088
1089 ++MCNumCPRelocations;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001090 } else {
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001091 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001092 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
1093 }
Bill Wendlinge84eb992010-11-03 01:49:29 +00001094
Bill Wendlinge84eb992010-11-03 01:49:29 +00001095 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
1096 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001097 if (isAdd)
Bill Wendlinge84eb992010-11-03 01:49:29 +00001098 Binary |= (1 << 8);
1099 Binary |= (Reg << 9);
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001100 return Binary;
1101}
1102
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001103unsigned ARMMCCodeEmitter::
Owen Anderson04912702011-07-21 23:38:37 +00001104getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001105 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001106 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
Owen Anderson7c965e72011-07-28 17:56:55 +00001107 // shifted. The second is Rs, the amount to shift by, and the third specifies
1108 // the type of the shift.
Jim Grosbach49b0c452010-11-03 22:03:20 +00001109 //
Jim Grosbachefd53692010-10-12 23:53:58 +00001110 // {3-0} = Rm.
Owen Anderson7c965e72011-07-28 17:56:55 +00001111 // {4} = 1
Jim Grosbachefd53692010-10-12 23:53:58 +00001112 // {6-5} = type
Owen Anderson7c965e72011-07-28 17:56:55 +00001113 // {11-8} = Rs
1114 // {7} = 0
Jim Grosbachefd53692010-10-12 23:53:58 +00001115
1116 const MCOperand &MO = MI.getOperand(OpIdx);
1117 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1118 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
1119 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
1120
1121 // Encode Rm.
1122 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1123
1124 // Encode the shift opcode.
1125 unsigned SBits = 0;
1126 unsigned Rs = MO1.getReg();
1127 if (Rs) {
1128 // Set shift operand (bit[7:4]).
1129 // LSL - 0001
1130 // LSR - 0011
1131 // ASR - 0101
1132 // ROR - 0111
Jim Grosbachefd53692010-10-12 23:53:58 +00001133 switch (SOpc) {
1134 default: llvm_unreachable("Unknown shift opc!");
1135 case ARM_AM::lsl: SBits = 0x1; break;
1136 case ARM_AM::lsr: SBits = 0x3; break;
1137 case ARM_AM::asr: SBits = 0x5; break;
1138 case ARM_AM::ror: SBits = 0x7; break;
Jim Grosbachefd53692010-10-12 23:53:58 +00001139 }
1140 }
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001141
Jim Grosbachefd53692010-10-12 23:53:58 +00001142 Binary |= SBits << 4;
Jim Grosbachefd53692010-10-12 23:53:58 +00001143
Owen Anderson7c965e72011-07-28 17:56:55 +00001144 // Encode the shift operation Rs.
Owen Anderson04912702011-07-21 23:38:37 +00001145 // Encode Rs bit[11:8].
1146 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1147 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
1148}
1149
1150unsigned ARMMCCodeEmitter::
1151getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
1152 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson7c965e72011-07-28 17:56:55 +00001153 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1154 // shifted. The second is the amount to shift by.
Owen Anderson04912702011-07-21 23:38:37 +00001155 //
1156 // {3-0} = Rm.
Owen Anderson7c965e72011-07-28 17:56:55 +00001157 // {4} = 0
Owen Anderson04912702011-07-21 23:38:37 +00001158 // {6-5} = type
Owen Anderson7c965e72011-07-28 17:56:55 +00001159 // {11-7} = imm
Owen Anderson04912702011-07-21 23:38:37 +00001160
1161 const MCOperand &MO = MI.getOperand(OpIdx);
1162 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1163 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1164
1165 // Encode Rm.
1166 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1167
1168 // Encode the shift opcode.
1169 unsigned SBits = 0;
1170
1171 // Set shift operand (bit[6:4]).
1172 // LSL - 000
1173 // LSR - 010
1174 // ASR - 100
1175 // ROR - 110
1176 // RRX - 110 and bit[11:8] clear.
1177 switch (SOpc) {
1178 default: llvm_unreachable("Unknown shift opc!");
1179 case ARM_AM::lsl: SBits = 0x0; break;
1180 case ARM_AM::lsr: SBits = 0x2; break;
1181 case ARM_AM::asr: SBits = 0x4; break;
1182 case ARM_AM::ror: SBits = 0x6; break;
1183 case ARM_AM::rrx:
1184 Binary |= 0x60;
1185 return Binary;
Jim Grosbachefd53692010-10-12 23:53:58 +00001186 }
1187
1188 // Encode shift_imm bit[11:7].
Owen Anderson04912702011-07-21 23:38:37 +00001189 Binary |= SBits << 4;
Owen Andersone33c95d2011-08-11 18:41:59 +00001190 unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm());
1191 assert(Offset && "Offset must be in range 1-32!");
1192 if (Offset == 32) Offset = 0;
1193 return Binary | (Offset << 7);
Jim Grosbachefd53692010-10-12 23:53:58 +00001194}
1195
Owen Anderson04912702011-07-21 23:38:37 +00001196
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001197unsigned ARMMCCodeEmitter::
Owen Anderson50d662b2010-11-29 22:44:32 +00001198getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1199 SmallVectorImpl<MCFixup> &Fixups) const {
1200 const MCOperand &MO1 = MI.getOperand(OpNum);
1201 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbachc4a0c292010-12-10 21:57:34 +00001202 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1203
Owen Anderson50d662b2010-11-29 22:44:32 +00001204 // Encoded as [Rn, Rm, imm].
1205 // FIXME: Needs fixup support.
1206 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1207 Value <<= 4;
1208 Value |= getARMRegisterNumbering(MO2.getReg());
1209 Value <<= 2;
1210 Value |= MO3.getImm();
Jim Grosbachc4a0c292010-12-10 21:57:34 +00001211
Owen Anderson50d662b2010-11-29 22:44:32 +00001212 return Value;
1213}
1214
1215unsigned ARMMCCodeEmitter::
1216getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1217 SmallVectorImpl<MCFixup> &Fixups) const {
1218 const MCOperand &MO1 = MI.getOperand(OpNum);
1219 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1220
1221 // FIXME: Needs fixup support.
1222 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbachc4a0c292010-12-10 21:57:34 +00001223
Owen Anderson50d662b2010-11-29 22:44:32 +00001224 // Even though the immediate is 8 bits long, we need 9 bits in order
1225 // to represent the (inverse of the) sign bit.
1226 Value <<= 9;
Owen Andersone22c7322010-11-30 00:14:31 +00001227 int32_t tmp = (int32_t)MO2.getImm();
1228 if (tmp < 0)
1229 tmp = abs(tmp);
1230 else
1231 Value |= 256; // Set the ADD bit
1232 Value |= tmp & 255;
1233 return Value;
1234}
1235
1236unsigned ARMMCCodeEmitter::
1237getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1238 SmallVectorImpl<MCFixup> &Fixups) const {
1239 const MCOperand &MO1 = MI.getOperand(OpNum);
1240
1241 // FIXME: Needs fixup support.
1242 unsigned Value = 0;
1243 int32_t tmp = (int32_t)MO1.getImm();
1244 if (tmp < 0)
1245 tmp = abs(tmp);
1246 else
1247 Value |= 256; // Set the ADD bit
1248 Value |= tmp & 255;
Owen Anderson50d662b2010-11-29 22:44:32 +00001249 return Value;
1250}
1251
1252unsigned ARMMCCodeEmitter::
Owen Anderson299382e2010-11-30 19:19:31 +00001253getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1254 SmallVectorImpl<MCFixup> &Fixups) const {
1255 const MCOperand &MO1 = MI.getOperand(OpNum);
1256
1257 // FIXME: Needs fixup support.
1258 unsigned Value = 0;
1259 int32_t tmp = (int32_t)MO1.getImm();
1260 if (tmp < 0)
1261 tmp = abs(tmp);
1262 else
1263 Value |= 4096; // Set the ADD bit
1264 Value |= tmp & 4095;
1265 return Value;
1266}
1267
1268unsigned ARMMCCodeEmitter::
Owen Anderson8fdd1722010-11-12 21:12:40 +00001269getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1270 SmallVectorImpl<MCFixup> &Fixups) const {
1271 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1272 // shifted. The second is the amount to shift by.
1273 //
1274 // {3-0} = Rm.
1275 // {4} = 0
1276 // {6-5} = type
1277 // {11-7} = imm
1278
1279 const MCOperand &MO = MI.getOperand(OpIdx);
1280 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1281 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1282
1283 // Encode Rm.
1284 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1285
1286 // Encode the shift opcode.
1287 unsigned SBits = 0;
1288 // Set shift operand (bit[6:4]).
1289 // LSL - 000
1290 // LSR - 010
1291 // ASR - 100
1292 // ROR - 110
1293 switch (SOpc) {
1294 default: llvm_unreachable("Unknown shift opc!");
1295 case ARM_AM::lsl: SBits = 0x0; break;
1296 case ARM_AM::lsr: SBits = 0x2; break;
1297 case ARM_AM::asr: SBits = 0x4; break;
Owen Andersonc3c60a02011-09-13 17:34:32 +00001298 case ARM_AM::rrx: // FALLTHROUGH
Owen Anderson8fdd1722010-11-12 21:12:40 +00001299 case ARM_AM::ror: SBits = 0x6; break;
1300 }
1301
1302 Binary |= SBits << 4;
1303 if (SOpc == ARM_AM::rrx)
1304 return Binary;
1305
1306 // Encode shift_imm bit[11:7].
1307 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1308}
1309
1310unsigned ARMMCCodeEmitter::
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001311getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1312 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach5edb03e2010-10-21 22:03:21 +00001313 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1314 // msb of the mask.
1315 const MCOperand &MO = MI.getOperand(Op);
1316 uint32_t v = ~MO.getImm();
1317 uint32_t lsb = CountTrailingZeros_32(v);
1318 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1319 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1320 return lsb | (msb << 5);
1321}
1322
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001323unsigned ARMMCCodeEmitter::
1324getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling1b83ed52010-11-09 00:30:18 +00001325 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling345b48f2010-11-17 00:45:23 +00001326 // VLDM/VSTM:
1327 // {12-8} = Vd
1328 // {7-0} = Number of registers
1329 //
1330 // LDM/STM:
1331 // {15-0} = Bitfield of GPRs.
1332 unsigned Reg = MI.getOperand(Op).getReg();
Craig Topperf6e7e122012-03-27 07:21:54 +00001333 bool SPRRegs = ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1334 bool DPRRegs = ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
Bill Wendling345b48f2010-11-17 00:45:23 +00001335
Bill Wendling1b83ed52010-11-09 00:30:18 +00001336 unsigned Binary = 0;
Bill Wendling345b48f2010-11-17 00:45:23 +00001337
1338 if (SPRRegs || DPRRegs) {
1339 // VLDM/VSTM
1340 unsigned RegNo = getARMRegisterNumbering(Reg);
1341 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1342 Binary |= (RegNo & 0x1f) << 8;
1343 if (SPRRegs)
1344 Binary |= NumRegs;
1345 else
1346 Binary |= NumRegs * 2;
1347 } else {
1348 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1349 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1350 Binary |= 1 << RegNo;
1351 }
Bill Wendling1b83ed52010-11-09 00:30:18 +00001352 }
Bill Wendling345b48f2010-11-17 00:45:23 +00001353
Jim Grosbach74ef9e12010-10-30 00:37:59 +00001354 return Binary;
1355}
1356
Bob Wilson318ce7c2010-11-30 00:00:42 +00001357/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1358/// with the alignment operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001359unsigned ARMMCCodeEmitter::
1360getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1361 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonad402342010-11-02 00:05:05 +00001362 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001363 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach49b0c452010-11-03 22:03:20 +00001364
Owen Andersonad402342010-11-02 00:05:05 +00001365 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001366 unsigned Align = 0;
1367
1368 switch (Imm.getImm()) {
1369 default: break;
1370 case 2:
1371 case 4:
1372 case 8: Align = 0x01; break;
1373 case 16: Align = 0x02; break;
1374 case 32: Align = 0x03; break;
Owen Andersonad402342010-11-02 00:05:05 +00001375 }
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001376
Owen Andersonad402342010-11-02 00:05:05 +00001377 return RegNo | (Align << 4);
1378}
1379
Mon P Wang92ff16b2011-05-09 17:47:27 +00001380/// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1381/// along with the alignment operand for use in VST1 and VLD1 with size 32.
1382unsigned ARMMCCodeEmitter::
1383getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1384 SmallVectorImpl<MCFixup> &Fixups) const {
1385 const MCOperand &Reg = MI.getOperand(Op);
1386 const MCOperand &Imm = MI.getOperand(Op + 1);
1387
1388 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1389 unsigned Align = 0;
1390
1391 switch (Imm.getImm()) {
1392 default: break;
Mon P Wang92ff16b2011-05-09 17:47:27 +00001393 case 8:
Jim Grosbachcef98cd2011-12-19 18:31:43 +00001394 case 16:
1395 case 32: // Default '0' value for invalid alignments of 8, 16, 32 bytes.
1396 case 2: Align = 0x00; break;
1397 case 4: Align = 0x03; break;
Mon P Wang92ff16b2011-05-09 17:47:27 +00001398 }
1399
1400 return RegNo | (Align << 4);
1401}
1402
1403
Bob Wilson318ce7c2010-11-30 00:00:42 +00001404/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1405/// alignment operand for use in VLD-dup instructions. This is the same as
1406/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1407/// different for VLD4-dup.
1408unsigned ARMMCCodeEmitter::
1409getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1410 SmallVectorImpl<MCFixup> &Fixups) const {
1411 const MCOperand &Reg = MI.getOperand(Op);
1412 const MCOperand &Imm = MI.getOperand(Op + 1);
1413
1414 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1415 unsigned Align = 0;
1416
1417 switch (Imm.getImm()) {
1418 default: break;
1419 case 2:
1420 case 4:
1421 case 8: Align = 0x01; break;
1422 case 16: Align = 0x03; break;
1423 }
1424
1425 return RegNo | (Align << 4);
1426}
1427
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001428unsigned ARMMCCodeEmitter::
1429getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1430 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001431 const MCOperand &MO = MI.getOperand(Op);
1432 if (MO.getReg() == 0) return 0x0D;
Jim Grosbach81c90032011-12-02 22:01:25 +00001433 return getARMRegisterNumbering(MO.getReg());
Owen Anderson526ffd52010-11-02 01:24:55 +00001434}
1435
Bill Wendling3b1459b2011-03-01 01:00:59 +00001436unsigned ARMMCCodeEmitter::
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001437getShiftRight8Imm(const MCInst &MI, unsigned Op,
1438 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling3b1459b2011-03-01 01:00:59 +00001439 return 8 - MI.getOperand(Op).getImm();
1440}
1441
1442unsigned ARMMCCodeEmitter::
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001443getShiftRight16Imm(const MCInst &MI, unsigned Op,
1444 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling3b1459b2011-03-01 01:00:59 +00001445 return 16 - MI.getOperand(Op).getImm();
1446}
1447
1448unsigned ARMMCCodeEmitter::
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001449getShiftRight32Imm(const MCInst &MI, unsigned Op,
1450 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling3b1459b2011-03-01 01:00:59 +00001451 return 32 - MI.getOperand(Op).getImm();
1452}
1453
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001454unsigned ARMMCCodeEmitter::
1455getShiftRight64Imm(const MCInst &MI, unsigned Op,
1456 SmallVectorImpl<MCFixup> &Fixups) const {
1457 return 64 - MI.getOperand(Op).getImm();
1458}
1459
Jim Grosbach1287f4f2010-09-17 18:46:17 +00001460void ARMMCCodeEmitter::
1461EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001462 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach91029092010-10-07 22:12:50 +00001463 // Pseudo instructions don't get encoded.
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001464 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Jim Grosbach20b6fd72010-11-11 23:41:09 +00001465 uint64_t TSFlags = Desc.TSFlags;
1466 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbach91029092010-10-07 22:12:50 +00001467 return;
Owen Anderson651b2302011-07-13 23:22:26 +00001468
Jim Grosbach20b6fd72010-11-11 23:41:09 +00001469 int Size;
Owen Anderson651b2302011-07-13 23:22:26 +00001470 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1471 Size = Desc.getSize();
1472 else
1473 llvm_unreachable("Unexpected instruction size!");
Owen Anderson1732c2e2011-08-30 21:58:18 +00001474
Jim Grosbach567ebd0c2010-12-03 22:31:40 +00001475 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
Evan Cheng965b3c72011-01-13 07:58:56 +00001476 // Thumb 32-bit wide instructions need to emit the high order halfword
1477 // first.
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001478 if (isThumb() && Size == 4) {
Jim Grosbach567ebd0c2010-12-03 22:31:40 +00001479 EmitConstant(Binary >> 16, 2, OS);
1480 EmitConstant(Binary & 0xffff, 2, OS);
1481 } else
1482 EmitConstant(Binary, Size, OS);
Bill Wendling91da9ab2010-11-02 22:44:12 +00001483 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach1287f4f2010-09-17 18:46:17 +00001484}
Jim Grosbach8aed3862010-10-07 21:57:55 +00001485
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001486#include "ARMGenMCCodeEmitter.inc"