blob: 424b06d7fa8bcf44d7a0b74f54567b851daec49b [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,
Jim Grosbachc3b04272012-05-15 17:35:52 +0000339 const MCRegisterInfo &MRI,
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000340 const MCSubtargetInfo &STI,
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000341 MCContext &Ctx) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000342 return new ARMMCCodeEmitter(MCII, STI, Ctx);
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000343}
344
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000345/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
346/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000347/// Thumb2 mode.
348unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
349 unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000350 if (isThumb2()) {
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000351 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000352 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
353 // set to 1111.
354 unsigned Bit24 = EncodedValue & 0x01000000;
355 unsigned Bit28 = Bit24 << 4;
356 EncodedValue &= 0xEFFFFFFF;
357 EncodedValue |= Bit28;
358 EncodedValue |= 0x0F000000;
359 }
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000360
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000361 return EncodedValue;
362}
363
Owen Anderson99a8cb42010-11-11 21:36:43 +0000364/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000365/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson99a8cb42010-11-11 21:36:43 +0000366/// Thumb2 mode.
367unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
368 unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000369 if (isThumb2()) {
Owen Anderson99a8cb42010-11-11 21:36:43 +0000370 EncodedValue &= 0xF0FFFFFF;
371 EncodedValue |= 0x09000000;
372 }
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000373
Owen Anderson99a8cb42010-11-11 21:36:43 +0000374 return EncodedValue;
375}
376
Owen Andersonce2250f2010-11-11 23:12:55 +0000377/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000378/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonce2250f2010-11-11 23:12:55 +0000379/// Thumb2 mode.
380unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
381 unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000382 if (isThumb2()) {
Owen Andersonce2250f2010-11-11 23:12:55 +0000383 EncodedValue &= 0x00FFFFFF;
384 EncodedValue |= 0xEE000000;
385 }
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000386
Owen Andersonce2250f2010-11-11 23:12:55 +0000387 return EncodedValue;
388}
389
Bill Wendling87240d42010-12-01 21:54:50 +0000390/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
391/// them to their Thumb2 form if we are currently in Thumb2 mode.
392unsigned ARMMCCodeEmitter::
393VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000394 if (isThumb2()) {
Bill Wendling87240d42010-12-01 21:54:50 +0000395 EncodedValue &= 0x0FFFFFFF;
396 EncodedValue |= 0xE0000000;
397 }
398 return EncodedValue;
399}
Owen Anderson99a8cb42010-11-11 21:36:43 +0000400
Jim Grosbachc43c9302010-10-08 21:45:55 +0000401/// getMachineOpValue - Return binary encoding of operand. If the machine
402/// operand requires relocation, record the relocation and return zero.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000403unsigned ARMMCCodeEmitter::
404getMachineOpValue(const MCInst &MI, const MCOperand &MO,
405 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6f52f8a2010-10-14 02:33:26 +0000406 if (MO.isReg()) {
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000407 unsigned Reg = MO.getReg();
408 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbach96d82842010-10-29 23:21:03 +0000409
Jim Grosbachee48d2d2010-11-30 23:51:41 +0000410 // Q registers are encoded as 2x their register number.
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000411 switch (Reg) {
412 default:
413 return RegNo;
414 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
415 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
416 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
417 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
418 return 2 * RegNo;
Owen Anderson2bfa8ed2010-10-21 20:49:13 +0000419 }
Bill Wendling6f52f8a2010-10-14 02:33:26 +0000420 } else if (MO.isImm()) {
Jim Grosbachc43c9302010-10-08 21:45:55 +0000421 return static_cast<unsigned>(MO.getImm());
Bill Wendling6f52f8a2010-10-14 02:33:26 +0000422 } else if (MO.isFPImm()) {
423 return static_cast<unsigned>(APFloat(MO.getFPImm())
424 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbachc43c9302010-10-08 21:45:55 +0000425 }
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000426
Jim Grosbach2aeb8b92010-11-19 00:27:09 +0000427 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbachc43c9302010-10-08 21:45:55 +0000428}
429
Bill Wendling603bd8f2010-11-02 22:31:46 +0000430/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000431bool ARMMCCodeEmitter::
432EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
433 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000434 const MCOperand &MO = MI.getOperand(OpIdx);
435 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach2ba03aa2010-11-01 23:45:50 +0000436
Bill Wendlinge84eb992010-11-03 01:49:29 +0000437 Reg = getARMRegisterNumbering(MO.getReg());
438
439 int32_t SImm = MO1.getImm();
440 bool isAdd = true;
Bill Wendling603bd8f2010-11-02 22:31:46 +0000441
Jim Grosbach505607e2010-10-28 18:34:10 +0000442 // Special value for #-0
Owen Anderson967674d2011-08-29 19:36:44 +0000443 if (SImm == INT32_MIN) {
Bill Wendlinge84eb992010-11-03 01:49:29 +0000444 SImm = 0;
Owen Anderson967674d2011-08-29 19:36:44 +0000445 isAdd = false;
446 }
Bill Wendling603bd8f2010-11-02 22:31:46 +0000447
Jim Grosbach505607e2010-10-28 18:34:10 +0000448 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendlinge84eb992010-11-03 01:49:29 +0000449 if (SImm < 0) {
450 SImm = -SImm;
451 isAdd = false;
452 }
Bill Wendling603bd8f2010-11-02 22:31:46 +0000453
Bill Wendlinge84eb992010-11-03 01:49:29 +0000454 Imm = SImm;
455 return isAdd;
456}
457
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000458/// getBranchTargetOpValue - Helper function to get the branch target operand,
459/// which is either an immediate or requires a fixup.
460static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
461 unsigned FixupKind,
462 SmallVectorImpl<MCFixup> &Fixups) {
463 const MCOperand &MO = MI.getOperand(OpIdx);
464
465 // If the destination is an immediate, we have nothing to do.
466 if (MO.isImm()) return MO.getImm();
467 assert(MO.isExpr() && "Unexpected branch target type!");
468 const MCExpr *Expr = MO.getExpr();
469 MCFixupKind Kind = MCFixupKind(FixupKind);
Jim Grosbach5e5eabb2012-01-26 23:20:15 +0000470 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000471
472 // All of the information is in the fixup.
473 return 0;
474}
475
Owen Anderson5c160fd2011-08-31 18:30:20 +0000476// Thumb BL and BLX use a strange offset encoding where bits 22 and 21 are
477// determined by negating them and XOR'ing them with bit 23.
478static int32_t encodeThumbBLOffset(int32_t offset) {
479 offset >>= 1;
480 uint32_t S = (offset & 0x800000) >> 23;
481 uint32_t J1 = (offset & 0x400000) >> 22;
482 uint32_t J2 = (offset & 0x200000) >> 21;
483 J1 = (~J1 & 0x1);
484 J2 = (~J2 & 0x1);
485 J1 ^= S;
486 J2 ^= S;
487
488 offset &= ~0x600000;
489 offset |= J1 << 22;
490 offset |= J2 << 21;
491
492 return offset;
493}
494
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000495/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach9e199462010-12-06 23:57:07 +0000496uint32_t ARMMCCodeEmitter::
497getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
498 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson5c160fd2011-08-31 18:30:20 +0000499 const MCOperand MO = MI.getOperand(OpIdx);
500 if (MO.isExpr())
501 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl,
502 Fixups);
503 return encodeThumbBLOffset(MO.getImm());
Jim Grosbach9e199462010-12-06 23:57:07 +0000504}
505
Bill Wendling3392bfc2010-12-09 00:39:08 +0000506/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
507/// BLX branch target.
508uint32_t ARMMCCodeEmitter::
509getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
510 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson5c160fd2011-08-31 18:30:20 +0000511 const MCOperand MO = MI.getOperand(OpIdx);
512 if (MO.isExpr())
513 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx,
514 Fixups);
515 return encodeThumbBLOffset(MO.getImm());
Bill Wendling3392bfc2010-12-09 00:39:08 +0000516}
517
Jim Grosbache119da12010-12-10 18:21:33 +0000518/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
519uint32_t ARMMCCodeEmitter::
520getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
521 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson543c89f2011-08-30 22:03:20 +0000522 const MCOperand MO = MI.getOperand(OpIdx);
523 if (MO.isExpr())
Owen Anderson5c160fd2011-08-31 18:30:20 +0000524 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br,
525 Fixups);
Owen Anderson543c89f2011-08-30 22:03:20 +0000526 return (MO.getImm() >> 1);
Jim Grosbache119da12010-12-10 18:21:33 +0000527}
528
Jim Grosbach78485ad2010-12-10 17:13:40 +0000529/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
530uint32_t ARMMCCodeEmitter::
531getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache119da12010-12-10 18:21:33 +0000532 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona455a0b2011-08-31 20:26:14 +0000533 const MCOperand MO = MI.getOperand(OpIdx);
534 if (MO.isExpr())
535 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc,
536 Fixups);
537 return (MO.getImm() >> 1);
Jim Grosbach78485ad2010-12-10 17:13:40 +0000538}
539
Jim Grosbach62b68112010-12-09 19:04:53 +0000540/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000541uint32_t ARMMCCodeEmitter::
Jim Grosbach62b68112010-12-09 19:04:53 +0000542getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000543 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfdf3cd72011-08-30 22:15:17 +0000544 const MCOperand MO = MI.getOperand(OpIdx);
545 if (MO.isExpr())
546 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
547 return (MO.getImm() >> 1);
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000548}
549
Jason W Kimd2e2f562011-02-04 19:47:15 +0000550/// Return true if this branch has a non-always predication
551static bool HasConditionalBranch(const MCInst &MI) {
552 int NumOp = MI.getNumOperands();
553 if (NumOp >= 2) {
554 for (int i = 0; i < NumOp-1; ++i) {
555 const MCOperand &MCOp1 = MI.getOperand(i);
556 const MCOperand &MCOp2 = MI.getOperand(i + 1);
Owen Anderson1732c2e2011-08-30 21:58:18 +0000557 if (MCOp1.isImm() && MCOp2.isReg() &&
Jason W Kimd2e2f562011-02-04 19:47:15 +0000558 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
Owen Anderson1732c2e2011-08-30 21:58:18 +0000559 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
Jason W Kimd2e2f562011-02-04 19:47:15 +0000560 return true;
561 }
562 }
563 }
564 return false;
565}
566
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000567/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
568/// target.
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000569uint32_t ARMMCCodeEmitter::
570getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000571 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachaecdd872010-12-10 23:41:10 +0000572 // FIXME: This really, really shouldn't use TargetMachine. We don't want
573 // coupling between MC and TM anywhere we can help it.
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000574 if (isThumb2())
Owen Anderson578074b2010-12-13 19:31:11 +0000575 return
576 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Jason W Kimd2e2f562011-02-04 19:47:15 +0000577 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000578}
579
Jason W Kimd2e2f562011-02-04 19:47:15 +0000580/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
581/// target.
582uint32_t ARMMCCodeEmitter::
583getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
584 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson6c70e582011-08-26 22:54:51 +0000585 const MCOperand MO = MI.getOperand(OpIdx);
586 if (MO.isExpr()) {
Owen Anderson1732c2e2011-08-30 21:58:18 +0000587 if (HasConditionalBranch(MI))
Owen Anderson6c70e582011-08-26 22:54:51 +0000588 return ::getBranchTargetOpValue(MI, OpIdx,
589 ARM::fixup_arm_condbranch, Fixups);
Owen Anderson1732c2e2011-08-30 21:58:18 +0000590 return ::getBranchTargetOpValue(MI, OpIdx,
Owen Anderson6c70e582011-08-26 22:54:51 +0000591 ARM::fixup_arm_uncondbranch, Fixups);
592 }
593
594 return MO.getImm() >> 2;
Jason W Kimd2e2f562011-02-04 19:47:15 +0000595}
596
Owen Andersonb205c022011-08-26 23:32:08 +0000597uint32_t ARMMCCodeEmitter::
Jim Grosbach7b811d32012-02-27 21:36:23 +0000598getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
599 SmallVectorImpl<MCFixup> &Fixups) const {
600 const MCOperand MO = MI.getOperand(OpIdx);
James Molloyfb5cd602012-03-30 09:15:32 +0000601 if (MO.isExpr()) {
602 if (HasConditionalBranch(MI))
603 return ::getBranchTargetOpValue(MI, OpIdx,
604 ARM::fixup_arm_condbl, Fixups);
605 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_uncondbl, Fixups);
606 }
Jim Grosbach7b811d32012-02-27 21:36:23 +0000607
608 return MO.getImm() >> 2;
609}
610
611uint32_t ARMMCCodeEmitter::
Owen Andersonb205c022011-08-26 23:32:08 +0000612getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
613 SmallVectorImpl<MCFixup> &Fixups) const {
614 const MCOperand MO = MI.getOperand(OpIdx);
Jim Grosbach7b811d32012-02-27 21:36:23 +0000615 if (MO.isExpr())
616 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_blx, Fixups);
Jason W Kimd2e2f562011-02-04 19:47:15 +0000617
Owen Andersonb205c022011-08-26 23:32:08 +0000618 return MO.getImm() >> 1;
619}
Jason W Kimd2e2f562011-02-04 19:47:15 +0000620
Owen Anderson578074b2010-12-13 19:31:11 +0000621/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
622/// immediate branch target.
623uint32_t ARMMCCodeEmitter::
624getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
625 SmallVectorImpl<MCFixup> &Fixups) const {
626 unsigned Val =
627 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
628 bool I = (Val & 0x800000);
629 bool J1 = (Val & 0x400000);
630 bool J2 = (Val & 0x200000);
631 if (I ^ J1)
632 Val &= ~0x400000;
633 else
634 Val |= 0x400000;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000635
Owen Anderson578074b2010-12-13 19:31:11 +0000636 if (I ^ J2)
637 Val &= ~0x200000;
638 else
639 Val |= 0x200000;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000640
Owen Anderson578074b2010-12-13 19:31:11 +0000641 return Val;
642}
643
Jiangning Liu10dd40e2012-08-02 08:13:13 +0000644/// getAdrLabelOpValue - Return encoding info for 12-bit shifted-immediate
645/// ADR label target.
Jim Grosbachdc35e062010-12-01 19:47:31 +0000646uint32_t ARMMCCodeEmitter::
647getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
648 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona01bcbf2011-08-26 18:09:22 +0000649 const MCOperand MO = MI.getOperand(OpIdx);
650 if (MO.isExpr())
651 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
652 Fixups);
653 int32_t offset = MO.getImm();
654 uint32_t Val = 0x2000;
Jiangning Liu10dd40e2012-08-02 08:13:13 +0000655
656 if (offset == INT32_MIN) {
657 Val = 0x1000;
658 offset = 0;
659 } else if (offset < 0) {
Owen Andersona01bcbf2011-08-26 18:09:22 +0000660 Val = 0x1000;
661 offset *= -1;
662 }
Jiangning Liu10dd40e2012-08-02 08:13:13 +0000663
664 int SoImmVal = ARM_AM::getSOImmVal(offset);
665 assert(SoImmVal != -1 && "Not a valid so_imm value!");
666
667 Val |= SoImmVal;
Owen Andersona01bcbf2011-08-26 18:09:22 +0000668 return Val;
Jim Grosbachdc35e062010-12-01 19:47:31 +0000669}
670
Jiangning Liu10dd40e2012-08-02 08:13:13 +0000671/// getT2AdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
Owen Anderson6d375e52010-12-14 00:36:49 +0000672/// target.
673uint32_t ARMMCCodeEmitter::
674getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
675 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona01bcbf2011-08-26 18:09:22 +0000676 const MCOperand MO = MI.getOperand(OpIdx);
677 if (MO.isExpr())
678 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
679 Fixups);
Owen Anderson5bfb0e02011-09-09 22:24:36 +0000680 int32_t Val = MO.getImm();
Jiangning Liu10dd40e2012-08-02 08:13:13 +0000681 if (Val == INT32_MIN)
682 Val = 0x1000;
683 else if (Val < 0) {
Owen Anderson5bfb0e02011-09-09 22:24:36 +0000684 Val *= -1;
685 Val |= 0x1000;
686 }
687 return Val;
Owen Anderson6d375e52010-12-14 00:36:49 +0000688}
689
Jiangning Liu10dd40e2012-08-02 08:13:13 +0000690/// getThumbAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000691/// target.
692uint32_t ARMMCCodeEmitter::
693getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
694 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona01bcbf2011-08-26 18:09:22 +0000695 const MCOperand MO = MI.getOperand(OpIdx);
696 if (MO.isExpr())
697 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
698 Fixups);
699 return MO.getImm();
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000700}
701
Bill Wendling092a7bd2010-12-14 03:36:38 +0000702/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
703/// operand.
Owen Andersonb0fa1272010-12-10 22:11:13 +0000704uint32_t ARMMCCodeEmitter::
Bill Wendling092a7bd2010-12-14 03:36:38 +0000705getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
706 SmallVectorImpl<MCFixup> &) const {
707 // [Rn, Rm]
708 // {5-3} = Rm
709 // {2-0} = Rn
Owen Andersonb0fa1272010-12-10 22:11:13 +0000710 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendling092a7bd2010-12-14 03:36:38 +0000711 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Andersonb0fa1272010-12-10 22:11:13 +0000712 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
713 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
714 return (Rm << 3) | Rn;
715}
716
Bill Wendlinge84eb992010-11-03 01:49:29 +0000717/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000718uint32_t ARMMCCodeEmitter::
719getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
720 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinge84eb992010-11-03 01:49:29 +0000721 // {17-13} = reg
722 // {12} = (U)nsigned (add == '1', sub == '0')
723 // {11-0} = imm12
724 unsigned Reg, Imm12;
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000725 bool isAdd = true;
726 // If The first operand isn't a register, we have a label reference.
727 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Anderson4ebf4712011-02-08 22:39:40 +0000728 if (!MO.isReg()) {
Jim Grosbach90987142010-11-09 01:37:15 +0000729 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000730 Imm12 = 0;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +0000731 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000732
Owen Anderson4a9eb5f2011-09-12 20:36:51 +0000733 if (MO.isExpr()) {
734 const MCExpr *Expr = MO.getExpr();
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000735
Owen Anderson4a9eb5f2011-09-12 20:36:51 +0000736 MCFixupKind Kind;
737 if (isThumb2())
738 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
739 else
740 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach5e5eabb2012-01-26 23:20:15 +0000741 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000742
Owen Anderson4a9eb5f2011-09-12 20:36:51 +0000743 ++MCNumCPRelocations;
744 } else {
745 Reg = ARM::PC;
746 int32_t Offset = MO.getImm();
Jim Grosbach94298a92012-01-18 22:46:46 +0000747 // FIXME: Handle #-0.
Owen Anderson4a9eb5f2011-09-12 20:36:51 +0000748 if (Offset < 0) {
749 Offset *= -1;
750 isAdd = false;
751 }
752 Imm12 = Offset;
753 }
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000754 } else
755 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendlinge84eb992010-11-03 01:49:29 +0000756
Bill Wendlinge84eb992010-11-03 01:49:29 +0000757 uint32_t Binary = Imm12 & 0xfff;
758 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach505607e2010-10-28 18:34:10 +0000759 if (isAdd)
Bill Wendlinge84eb992010-11-03 01:49:29 +0000760 Binary |= (1 << 12);
761 Binary |= (Reg << 13);
762 return Binary;
763}
764
Jim Grosbach7db8d692011-09-08 22:07:06 +0000765/// getT2Imm8s4OpValue - Return encoding info for
766/// '+/- imm8<<2' operand.
767uint32_t ARMMCCodeEmitter::
768getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
769 SmallVectorImpl<MCFixup> &Fixups) const {
770 // FIXME: The immediate operand should have already been encoded like this
771 // before ever getting here. The encoder method should just need to combine
772 // the MI operands for the register and the offset into a single
773 // representation for the complex operand in the .td file. This isn't just
774 // style, unfortunately. As-is, we can't represent the distinct encoding
775 // for #-0.
776
777 // {8} = (U)nsigned (add == '1', sub == '0')
778 // {7-0} = imm8
779 int32_t Imm8 = MI.getOperand(OpIdx).getImm();
780 bool isAdd = Imm8 >= 0;
781
782 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
783 if (Imm8 < 0)
784 Imm8 = -Imm8;
785
786 // Scaled by 4.
787 Imm8 /= 4;
788
789 uint32_t Binary = Imm8 & 0xff;
790 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
791 if (isAdd)
792 Binary |= (1 << 8);
793 return Binary;
794}
795
Owen Anderson943fb602010-12-01 19:18:46 +0000796/// getT2AddrModeImm8s4OpValue - Return encoding info for
797/// 'reg +/- imm8<<2' operand.
798uint32_t ARMMCCodeEmitter::
799getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
800 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbache69f7242010-12-10 21:05:07 +0000801 // {12-9} = reg
802 // {8} = (U)nsigned (add == '1', sub == '0')
803 // {7-0} = imm8
Owen Anderson943fb602010-12-01 19:18:46 +0000804 unsigned Reg, Imm8;
805 bool isAdd = true;
806 // If The first operand isn't a register, we have a label reference.
807 const MCOperand &MO = MI.getOperand(OpIdx);
808 if (!MO.isReg()) {
809 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
810 Imm8 = 0;
811 isAdd = false ; // 'U' bit is set as part of the fixup.
812
813 assert(MO.isExpr() && "Unexpected machine operand type!");
814 const MCExpr *Expr = MO.getExpr();
Jim Grosbach8648c102011-12-19 23:06:24 +0000815 MCFixupKind Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
Jim Grosbach5e5eabb2012-01-26 23:20:15 +0000816 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
Owen Anderson943fb602010-12-01 19:18:46 +0000817
818 ++MCNumCPRelocations;
819 } else
820 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
821
Jim Grosbach7db8d692011-09-08 22:07:06 +0000822 // FIXME: The immediate operand should have already been encoded like this
823 // before ever getting here. The encoder method should just need to combine
824 // the MI operands for the register and the offset into a single
825 // representation for the complex operand in the .td file. This isn't just
826 // style, unfortunately. As-is, we can't represent the distinct encoding
827 // for #-0.
Owen Anderson943fb602010-12-01 19:18:46 +0000828 uint32_t Binary = (Imm8 >> 2) & 0xff;
829 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
830 if (isAdd)
Jim Grosbache69f7242010-12-10 21:05:07 +0000831 Binary |= (1 << 8);
Owen Anderson943fb602010-12-01 19:18:46 +0000832 Binary |= (Reg << 9);
833 return Binary;
834}
835
Jim Grosbacha05627e2011-09-09 18:37:27 +0000836/// getT2AddrModeImm0_1020s4OpValue - Return encoding info for
837/// 'reg + imm8<<2' operand.
838uint32_t ARMMCCodeEmitter::
839getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
840 SmallVectorImpl<MCFixup> &Fixups) const {
841 // {11-8} = reg
842 // {7-0} = imm8
843 const MCOperand &MO = MI.getOperand(OpIdx);
844 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
845 unsigned Reg = getARMRegisterNumbering(MO.getReg());
846 unsigned Imm8 = MO1.getImm();
847 return (Reg << 8) | Imm8;
848}
849
Jason W Kim9c5b65d2011-01-12 00:19:25 +0000850// FIXME: This routine assumes that a binary
851// expression will always result in a PCRel expression
852// In reality, its only true if one or more subexpressions
853// is itself a PCRel (i.e. "." in asm or some other pcrel construct)
854// but this is good enough for now.
855static bool EvaluateAsPCRel(const MCExpr *Expr) {
856 switch (Expr->getKind()) {
Craig Toppere55c5562012-02-07 02:50:20 +0000857 default: llvm_unreachable("Unexpected expression type");
Jason W Kim9c5b65d2011-01-12 00:19:25 +0000858 case MCExpr::SymbolRef: return false;
859 case MCExpr::Binary: return true;
Jason W Kim9c5b65d2011-01-12 00:19:25 +0000860 }
861}
862
Evan Cheng965b3c72011-01-13 07:58:56 +0000863uint32_t
864ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
865 SmallVectorImpl<MCFixup> &Fixups) const {
Jason W Kim5a97bd82010-11-18 23:37:15 +0000866 // {20-16} = imm{15-12}
867 // {11-0} = imm{11-0}
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000868 const MCOperand &MO = MI.getOperand(OpIdx);
Evan Cheng965b3c72011-01-13 07:58:56 +0000869 if (MO.isImm())
870 // Hi / lo 16 bits already extracted during earlier passes.
Jason W Kim5a97bd82010-11-18 23:37:15 +0000871 return static_cast<unsigned>(MO.getImm());
Evan Cheng965b3c72011-01-13 07:58:56 +0000872
873 // Handle :upper16: and :lower16: assembly prefixes.
874 const MCExpr *E = MO.getExpr();
Jim Grosbach70bed4f2012-05-01 20:43:21 +0000875 MCFixupKind Kind;
Evan Cheng965b3c72011-01-13 07:58:56 +0000876 if (E->getKind() == MCExpr::Target) {
877 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
878 E = ARM16Expr->getSubExpr();
879
Evan Cheng965b3c72011-01-13 07:58:56 +0000880 switch (ARM16Expr->getKind()) {
Craig Toppere55c5562012-02-07 02:50:20 +0000881 default: llvm_unreachable("Unsupported ARMFixup");
Evan Cheng965b3c72011-01-13 07:58:56 +0000882 case ARMMCExpr::VK_ARM_HI16:
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000883 if (!isTargetDarwin() && EvaluateAsPCRel(E))
884 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000885 ? ARM::fixup_t2_movt_hi16_pcrel
886 : ARM::fixup_arm_movt_hi16_pcrel);
887 else
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000888 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000889 ? ARM::fixup_t2_movt_hi16
890 : ARM::fixup_arm_movt_hi16);
Jason W Kim5a97bd82010-11-18 23:37:15 +0000891 break;
Evan Cheng965b3c72011-01-13 07:58:56 +0000892 case ARMMCExpr::VK_ARM_LO16:
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000893 if (!isTargetDarwin() && EvaluateAsPCRel(E))
894 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000895 ? ARM::fixup_t2_movw_lo16_pcrel
896 : ARM::fixup_arm_movw_lo16_pcrel);
897 else
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000898 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000899 ? ARM::fixup_t2_movw_lo16
900 : ARM::fixup_arm_movw_lo16);
Jason W Kim5a97bd82010-11-18 23:37:15 +0000901 break;
Jason W Kim5a97bd82010-11-18 23:37:15 +0000902 }
Jim Grosbach5e5eabb2012-01-26 23:20:15 +0000903 Fixups.push_back(MCFixup::Create(0, E, Kind, MI.getLoc()));
Jason W Kim5a97bd82010-11-18 23:37:15 +0000904 return 0;
Jim Grosbach70bed4f2012-05-01 20:43:21 +0000905 }
906 // If the expression doesn't have :upper16: or :lower16: on it,
907 // it's just a plain immediate expression, and those evaluate to
908 // the lower 16 bits of the expression regardless of whether
909 // we have a movt or a movw.
910 if (!isTargetDarwin() && EvaluateAsPCRel(E))
911 Kind = MCFixupKind(isThumb2()
912 ? ARM::fixup_t2_movw_lo16_pcrel
913 : ARM::fixup_arm_movw_lo16_pcrel);
914 else
915 Kind = MCFixupKind(isThumb2()
916 ? ARM::fixup_t2_movw_lo16
917 : ARM::fixup_arm_movw_lo16);
918 Fixups.push_back(MCFixup::Create(0, E, Kind, MI.getLoc()));
919 return 0;
Jason W Kim5a97bd82010-11-18 23:37:15 +0000920}
921
922uint32_t ARMMCCodeEmitter::
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000923getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
924 SmallVectorImpl<MCFixup> &Fixups) const {
925 const MCOperand &MO = MI.getOperand(OpIdx);
926 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
927 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
928 unsigned Rn = getARMRegisterNumbering(MO.getReg());
929 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000930 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
931 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach38b469e2010-11-15 20:47:07 +0000932 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
933 unsigned SBits = getShiftOp(ShOp);
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000934
935 // {16-13} = Rn
936 // {12} = isAdd
937 // {11-0} = shifter
938 // {3-0} = Rm
939 // {4} = 0
940 // {6-5} = type
941 // {11-7} = imm
Jim Grosbach607efcb2010-11-11 01:09:40 +0000942 uint32_t Binary = Rm;
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000943 Binary |= Rn << 13;
944 Binary |= SBits << 5;
945 Binary |= ShImm << 7;
946 if (isAdd)
947 Binary |= 1 << 12;
948 return Binary;
949}
950
Jim Grosbach607efcb2010-11-11 01:09:40 +0000951uint32_t ARMMCCodeEmitter::
Jim Grosbach38b469e2010-11-15 20:47:07 +0000952getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
953 SmallVectorImpl<MCFixup> &Fixups) const {
954 // {17-14} Rn
955 // {13} 1 == imm12, 0 == Rm
956 // {12} isAdd
957 // {11-0} imm12/Rm
958 const MCOperand &MO = MI.getOperand(OpIdx);
959 unsigned Rn = getARMRegisterNumbering(MO.getReg());
960 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
961 Binary |= Rn << 14;
962 return Binary;
963}
964
965uint32_t ARMMCCodeEmitter::
966getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
967 SmallVectorImpl<MCFixup> &Fixups) const {
968 // {13} 1 == imm12, 0 == Rm
969 // {12} isAdd
970 // {11-0} imm12/Rm
971 const MCOperand &MO = MI.getOperand(OpIdx);
972 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
973 unsigned Imm = MO1.getImm();
974 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
975 bool isReg = MO.getReg() != 0;
976 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
977 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
978 if (isReg) {
979 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
980 Binary <<= 7; // Shift amount is bits [11:7]
981 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
982 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
983 }
984 return Binary | (isAdd << 12) | (isReg << 13);
985}
986
987uint32_t ARMMCCodeEmitter::
Jim Grosbachd3595712011-08-03 23:50:40 +0000988getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
989 SmallVectorImpl<MCFixup> &Fixups) const {
990 // {4} isAdd
991 // {3-0} Rm
992 const MCOperand &MO = MI.getOperand(OpIdx);
993 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
Jim Grosbacha70fbfd52011-08-05 16:11:38 +0000994 bool isAdd = MO1.getImm() != 0;
Jim Grosbachd3595712011-08-03 23:50:40 +0000995 return getARMRegisterNumbering(MO.getReg()) | (isAdd << 4);
996}
997
998uint32_t ARMMCCodeEmitter::
Jim Grosbach68685e62010-11-11 16:55:29 +0000999getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
1000 SmallVectorImpl<MCFixup> &Fixups) const {
1001 // {9} 1 == imm8, 0 == Rm
1002 // {8} isAdd
1003 // {7-4} imm7_4/zero
1004 // {3-0} imm3_0/Rm
1005 const MCOperand &MO = MI.getOperand(OpIdx);
1006 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1007 unsigned Imm = MO1.getImm();
1008 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1009 bool isImm = MO.getReg() == 0;
1010 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1011 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1012 if (!isImm)
1013 Imm8 = getARMRegisterNumbering(MO.getReg());
1014 return Imm8 | (isAdd << 8) | (isImm << 9);
1015}
1016
1017uint32_t ARMMCCodeEmitter::
Jim Grosbach607efcb2010-11-11 01:09:40 +00001018getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
1019 SmallVectorImpl<MCFixup> &Fixups) const {
1020 // {13} 1 == imm8, 0 == Rm
1021 // {12-9} Rn
1022 // {8} isAdd
1023 // {7-4} imm7_4/zero
1024 // {3-0} imm3_0/Rm
1025 const MCOperand &MO = MI.getOperand(OpIdx);
1026 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1027 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
Jim Grosbach8648c102011-12-19 23:06:24 +00001028
1029 // If The first operand isn't a register, we have a label reference.
1030 if (!MO.isReg()) {
1031 unsigned Rn = getARMRegisterNumbering(ARM::PC); // Rn is PC.
1032
1033 assert(MO.isExpr() && "Unexpected machine operand type!");
1034 const MCExpr *Expr = MO.getExpr();
1035 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10_unscaled);
Jim Grosbach5e5eabb2012-01-26 23:20:15 +00001036 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
Jim Grosbach8648c102011-12-19 23:06:24 +00001037
1038 ++MCNumCPRelocations;
1039 return (Rn << 9) | (1 << 13);
1040 }
Jim Grosbach607efcb2010-11-11 01:09:40 +00001041 unsigned Rn = getARMRegisterNumbering(MO.getReg());
1042 unsigned Imm = MO2.getImm();
1043 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1044 bool isImm = MO1.getReg() == 0;
1045 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1046 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1047 if (!isImm)
1048 Imm8 = getARMRegisterNumbering(MO1.getReg());
1049 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
1050}
1051
Bill Wendling8a6449c2010-12-08 01:57:09 +00001052/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbach49bcd6f2010-12-07 21:50:47 +00001053uint32_t ARMMCCodeEmitter::
1054getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
1055 SmallVectorImpl<MCFixup> &Fixups) const {
1056 // [SP, #imm]
1057 // {7-0} = imm8
Jim Grosbach49bcd6f2010-12-07 21:50:47 +00001058 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendling8a6449c2010-12-08 01:57:09 +00001059 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
1060 "Unexpected base register!");
Bill Wendling7d3bde92010-12-15 23:32:27 +00001061
Jim Grosbach49bcd6f2010-12-07 21:50:47 +00001062 // The immediate is already shifted for the implicit zeroes, so no change
1063 // here.
1064 return MO1.getImm() & 0xff;
1065}
1066
Bill Wendling092a7bd2010-12-14 03:36:38 +00001067/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling0c4838b2010-12-09 21:49:07 +00001068uint32_t ARMMCCodeEmitter::
Bill Wendling092a7bd2010-12-14 03:36:38 +00001069getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling03e75762010-12-15 08:51:02 +00001070 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling811c9362010-11-30 07:44:32 +00001071 // [Rn, #imm]
1072 // {7-3} = imm5
1073 // {2-0} = Rn
1074 const MCOperand &MO = MI.getOperand(OpIdx);
1075 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendling811c9362010-11-30 07:44:32 +00001076 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Matt Beaumont-Gaye9afc742010-12-16 01:34:26 +00001077 unsigned Imm5 = MO1.getImm();
Bill Wendling0c4838b2010-12-09 21:49:07 +00001078 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendlinga9e3df72010-11-30 22:57:21 +00001079}
1080
Bill Wendling8a6449c2010-12-08 01:57:09 +00001081/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
1082uint32_t ARMMCCodeEmitter::
1083getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
1084 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond16fb432011-08-30 22:10:03 +00001085 const MCOperand MO = MI.getOperand(OpIdx);
1086 if (MO.isExpr())
1087 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
1088 return (MO.getImm() >> 2);
Bill Wendling8a6449c2010-12-08 01:57:09 +00001089}
1090
Jim Grosbach30eb6c72010-12-01 21:09:40 +00001091/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001092uint32_t ARMMCCodeEmitter::
1093getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
1094 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinge84eb992010-11-03 01:49:29 +00001095 // {12-9} = reg
1096 // {8} = (U)nsigned (add == '1', sub == '0')
1097 // {7-0} = imm8
1098 unsigned Reg, Imm8;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001099 bool isAdd;
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001100 // If The first operand isn't a register, we have a label reference.
1101 const MCOperand &MO = MI.getOperand(OpIdx);
1102 if (!MO.isReg()) {
Jim Grosbach90987142010-11-09 01:37:15 +00001103 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001104 Imm8 = 0;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001105 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001106
1107 assert(MO.isExpr() && "Unexpected machine operand type!");
1108 const MCExpr *Expr = MO.getExpr();
Owen Anderson0f7142d2010-12-08 00:18:36 +00001109 MCFixupKind Kind;
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001110 if (isThumb2())
Owen Anderson0f7142d2010-12-08 00:18:36 +00001111 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
1112 else
1113 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach5e5eabb2012-01-26 23:20:15 +00001114 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001115
1116 ++MCNumCPRelocations;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001117 } else {
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001118 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001119 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
1120 }
Bill Wendlinge84eb992010-11-03 01:49:29 +00001121
Bill Wendlinge84eb992010-11-03 01:49:29 +00001122 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
1123 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001124 if (isAdd)
Bill Wendlinge84eb992010-11-03 01:49:29 +00001125 Binary |= (1 << 8);
1126 Binary |= (Reg << 9);
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001127 return Binary;
1128}
1129
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001130unsigned ARMMCCodeEmitter::
Owen Anderson04912702011-07-21 23:38:37 +00001131getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001132 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001133 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
Owen Anderson7c965e72011-07-28 17:56:55 +00001134 // shifted. The second is Rs, the amount to shift by, and the third specifies
1135 // the type of the shift.
Jim Grosbach49b0c452010-11-03 22:03:20 +00001136 //
Jim Grosbachefd53692010-10-12 23:53:58 +00001137 // {3-0} = Rm.
Owen Anderson7c965e72011-07-28 17:56:55 +00001138 // {4} = 1
Jim Grosbachefd53692010-10-12 23:53:58 +00001139 // {6-5} = type
Owen Anderson7c965e72011-07-28 17:56:55 +00001140 // {11-8} = Rs
1141 // {7} = 0
Jim Grosbachefd53692010-10-12 23:53:58 +00001142
1143 const MCOperand &MO = MI.getOperand(OpIdx);
1144 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1145 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
1146 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
1147
1148 // Encode Rm.
1149 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1150
1151 // Encode the shift opcode.
1152 unsigned SBits = 0;
1153 unsigned Rs = MO1.getReg();
1154 if (Rs) {
1155 // Set shift operand (bit[7:4]).
1156 // LSL - 0001
1157 // LSR - 0011
1158 // ASR - 0101
1159 // ROR - 0111
Jim Grosbachefd53692010-10-12 23:53:58 +00001160 switch (SOpc) {
1161 default: llvm_unreachable("Unknown shift opc!");
1162 case ARM_AM::lsl: SBits = 0x1; break;
1163 case ARM_AM::lsr: SBits = 0x3; break;
1164 case ARM_AM::asr: SBits = 0x5; break;
1165 case ARM_AM::ror: SBits = 0x7; break;
Jim Grosbachefd53692010-10-12 23:53:58 +00001166 }
1167 }
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001168
Jim Grosbachefd53692010-10-12 23:53:58 +00001169 Binary |= SBits << 4;
Jim Grosbachefd53692010-10-12 23:53:58 +00001170
Owen Anderson7c965e72011-07-28 17:56:55 +00001171 // Encode the shift operation Rs.
Owen Anderson04912702011-07-21 23:38:37 +00001172 // Encode Rs bit[11:8].
1173 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1174 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
1175}
1176
1177unsigned ARMMCCodeEmitter::
1178getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
1179 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson7c965e72011-07-28 17:56:55 +00001180 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1181 // shifted. The second is the amount to shift by.
Owen Anderson04912702011-07-21 23:38:37 +00001182 //
1183 // {3-0} = Rm.
Owen Anderson7c965e72011-07-28 17:56:55 +00001184 // {4} = 0
Owen Anderson04912702011-07-21 23:38:37 +00001185 // {6-5} = type
Owen Anderson7c965e72011-07-28 17:56:55 +00001186 // {11-7} = imm
Owen Anderson04912702011-07-21 23:38:37 +00001187
1188 const MCOperand &MO = MI.getOperand(OpIdx);
1189 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1190 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1191
1192 // Encode Rm.
1193 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1194
1195 // Encode the shift opcode.
1196 unsigned SBits = 0;
1197
1198 // Set shift operand (bit[6:4]).
1199 // LSL - 000
1200 // LSR - 010
1201 // ASR - 100
1202 // ROR - 110
1203 // RRX - 110 and bit[11:8] clear.
1204 switch (SOpc) {
1205 default: llvm_unreachable("Unknown shift opc!");
1206 case ARM_AM::lsl: SBits = 0x0; break;
1207 case ARM_AM::lsr: SBits = 0x2; break;
1208 case ARM_AM::asr: SBits = 0x4; break;
1209 case ARM_AM::ror: SBits = 0x6; break;
1210 case ARM_AM::rrx:
1211 Binary |= 0x60;
1212 return Binary;
Jim Grosbachefd53692010-10-12 23:53:58 +00001213 }
1214
1215 // Encode shift_imm bit[11:7].
Owen Anderson04912702011-07-21 23:38:37 +00001216 Binary |= SBits << 4;
Owen Andersone33c95d2011-08-11 18:41:59 +00001217 unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm());
Richard Bartonba5b0cc2012-04-25 18:00:18 +00001218 assert(Offset < 32 && "Offset must be in range 0-31!");
Owen Andersone33c95d2011-08-11 18:41:59 +00001219 return Binary | (Offset << 7);
Jim Grosbachefd53692010-10-12 23:53:58 +00001220}
1221
Owen Anderson04912702011-07-21 23:38:37 +00001222
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001223unsigned ARMMCCodeEmitter::
Owen Anderson50d662b2010-11-29 22:44:32 +00001224getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1225 SmallVectorImpl<MCFixup> &Fixups) const {
1226 const MCOperand &MO1 = MI.getOperand(OpNum);
1227 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbachc4a0c292010-12-10 21:57:34 +00001228 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1229
Owen Anderson50d662b2010-11-29 22:44:32 +00001230 // Encoded as [Rn, Rm, imm].
1231 // FIXME: Needs fixup support.
1232 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1233 Value <<= 4;
1234 Value |= getARMRegisterNumbering(MO2.getReg());
1235 Value <<= 2;
1236 Value |= MO3.getImm();
Jim Grosbachc4a0c292010-12-10 21:57:34 +00001237
Owen Anderson50d662b2010-11-29 22:44:32 +00001238 return Value;
1239}
1240
1241unsigned ARMMCCodeEmitter::
1242getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1243 SmallVectorImpl<MCFixup> &Fixups) const {
1244 const MCOperand &MO1 = MI.getOperand(OpNum);
1245 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1246
1247 // FIXME: Needs fixup support.
1248 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbachc4a0c292010-12-10 21:57:34 +00001249
Owen Anderson50d662b2010-11-29 22:44:32 +00001250 // Even though the immediate is 8 bits long, we need 9 bits in order
1251 // to represent the (inverse of the) sign bit.
1252 Value <<= 9;
Owen Andersone22c7322010-11-30 00:14:31 +00001253 int32_t tmp = (int32_t)MO2.getImm();
1254 if (tmp < 0)
1255 tmp = abs(tmp);
1256 else
1257 Value |= 256; // Set the ADD bit
1258 Value |= tmp & 255;
1259 return Value;
1260}
1261
1262unsigned ARMMCCodeEmitter::
1263getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1264 SmallVectorImpl<MCFixup> &Fixups) const {
1265 const MCOperand &MO1 = MI.getOperand(OpNum);
1266
1267 // FIXME: Needs fixup support.
1268 unsigned Value = 0;
1269 int32_t tmp = (int32_t)MO1.getImm();
1270 if (tmp < 0)
1271 tmp = abs(tmp);
1272 else
1273 Value |= 256; // Set the ADD bit
1274 Value |= tmp & 255;
Owen Anderson50d662b2010-11-29 22:44:32 +00001275 return Value;
1276}
1277
1278unsigned ARMMCCodeEmitter::
Owen Anderson299382e2010-11-30 19:19:31 +00001279getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1280 SmallVectorImpl<MCFixup> &Fixups) const {
1281 const MCOperand &MO1 = MI.getOperand(OpNum);
1282
1283 // FIXME: Needs fixup support.
1284 unsigned Value = 0;
1285 int32_t tmp = (int32_t)MO1.getImm();
1286 if (tmp < 0)
1287 tmp = abs(tmp);
1288 else
1289 Value |= 4096; // Set the ADD bit
1290 Value |= tmp & 4095;
1291 return Value;
1292}
1293
1294unsigned ARMMCCodeEmitter::
Owen Anderson8fdd1722010-11-12 21:12:40 +00001295getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1296 SmallVectorImpl<MCFixup> &Fixups) const {
1297 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1298 // shifted. The second is the amount to shift by.
1299 //
1300 // {3-0} = Rm.
1301 // {4} = 0
1302 // {6-5} = type
1303 // {11-7} = imm
1304
1305 const MCOperand &MO = MI.getOperand(OpIdx);
1306 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1307 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1308
1309 // Encode Rm.
1310 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1311
1312 // Encode the shift opcode.
1313 unsigned SBits = 0;
1314 // Set shift operand (bit[6:4]).
1315 // LSL - 000
1316 // LSR - 010
1317 // ASR - 100
1318 // ROR - 110
1319 switch (SOpc) {
1320 default: llvm_unreachable("Unknown shift opc!");
1321 case ARM_AM::lsl: SBits = 0x0; break;
1322 case ARM_AM::lsr: SBits = 0x2; break;
1323 case ARM_AM::asr: SBits = 0x4; break;
Owen Andersonc3c60a02011-09-13 17:34:32 +00001324 case ARM_AM::rrx: // FALLTHROUGH
Owen Anderson8fdd1722010-11-12 21:12:40 +00001325 case ARM_AM::ror: SBits = 0x6; break;
1326 }
1327
1328 Binary |= SBits << 4;
1329 if (SOpc == ARM_AM::rrx)
1330 return Binary;
1331
1332 // Encode shift_imm bit[11:7].
1333 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1334}
1335
1336unsigned ARMMCCodeEmitter::
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001337getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1338 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach5edb03e2010-10-21 22:03:21 +00001339 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1340 // msb of the mask.
1341 const MCOperand &MO = MI.getOperand(Op);
1342 uint32_t v = ~MO.getImm();
1343 uint32_t lsb = CountTrailingZeros_32(v);
1344 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1345 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1346 return lsb | (msb << 5);
1347}
1348
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001349unsigned ARMMCCodeEmitter::
1350getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling1b83ed52010-11-09 00:30:18 +00001351 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling345b48f2010-11-17 00:45:23 +00001352 // VLDM/VSTM:
1353 // {12-8} = Vd
1354 // {7-0} = Number of registers
1355 //
1356 // LDM/STM:
1357 // {15-0} = Bitfield of GPRs.
1358 unsigned Reg = MI.getOperand(Op).getReg();
Craig Topperf6e7e122012-03-27 07:21:54 +00001359 bool SPRRegs = ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1360 bool DPRRegs = ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
Bill Wendling345b48f2010-11-17 00:45:23 +00001361
Bill Wendling1b83ed52010-11-09 00:30:18 +00001362 unsigned Binary = 0;
Bill Wendling345b48f2010-11-17 00:45:23 +00001363
1364 if (SPRRegs || DPRRegs) {
1365 // VLDM/VSTM
1366 unsigned RegNo = getARMRegisterNumbering(Reg);
1367 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1368 Binary |= (RegNo & 0x1f) << 8;
1369 if (SPRRegs)
1370 Binary |= NumRegs;
1371 else
1372 Binary |= NumRegs * 2;
1373 } else {
1374 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1375 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1376 Binary |= 1 << RegNo;
1377 }
Bill Wendling1b83ed52010-11-09 00:30:18 +00001378 }
Bill Wendling345b48f2010-11-17 00:45:23 +00001379
Jim Grosbach74ef9e12010-10-30 00:37:59 +00001380 return Binary;
1381}
1382
Bob Wilson318ce7c2010-11-30 00:00:42 +00001383/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1384/// with the alignment operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001385unsigned ARMMCCodeEmitter::
1386getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1387 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonad402342010-11-02 00:05:05 +00001388 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001389 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach49b0c452010-11-03 22:03:20 +00001390
Owen Andersonad402342010-11-02 00:05:05 +00001391 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001392 unsigned Align = 0;
1393
1394 switch (Imm.getImm()) {
1395 default: break;
1396 case 2:
1397 case 4:
1398 case 8: Align = 0x01; break;
1399 case 16: Align = 0x02; break;
1400 case 32: Align = 0x03; break;
Owen Andersonad402342010-11-02 00:05:05 +00001401 }
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001402
Owen Andersonad402342010-11-02 00:05:05 +00001403 return RegNo | (Align << 4);
1404}
1405
Mon P Wang92ff16b2011-05-09 17:47:27 +00001406/// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1407/// along with the alignment operand for use in VST1 and VLD1 with size 32.
1408unsigned ARMMCCodeEmitter::
1409getAddrMode6OneLane32AddressOpValue(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;
Mon P Wang92ff16b2011-05-09 17:47:27 +00001419 case 8:
Jim Grosbachcef98cd2011-12-19 18:31:43 +00001420 case 16:
1421 case 32: // Default '0' value for invalid alignments of 8, 16, 32 bytes.
1422 case 2: Align = 0x00; break;
1423 case 4: Align = 0x03; break;
Mon P Wang92ff16b2011-05-09 17:47:27 +00001424 }
1425
1426 return RegNo | (Align << 4);
1427}
1428
1429
Bob Wilson318ce7c2010-11-30 00:00:42 +00001430/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1431/// alignment operand for use in VLD-dup instructions. This is the same as
1432/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1433/// different for VLD4-dup.
1434unsigned ARMMCCodeEmitter::
1435getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1436 SmallVectorImpl<MCFixup> &Fixups) const {
1437 const MCOperand &Reg = MI.getOperand(Op);
1438 const MCOperand &Imm = MI.getOperand(Op + 1);
1439
1440 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1441 unsigned Align = 0;
1442
1443 switch (Imm.getImm()) {
1444 default: break;
1445 case 2:
1446 case 4:
1447 case 8: Align = 0x01; break;
1448 case 16: Align = 0x03; break;
1449 }
1450
1451 return RegNo | (Align << 4);
1452}
1453
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001454unsigned ARMMCCodeEmitter::
1455getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1456 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001457 const MCOperand &MO = MI.getOperand(Op);
1458 if (MO.getReg() == 0) return 0x0D;
Jim Grosbach81c90032011-12-02 22:01:25 +00001459 return getARMRegisterNumbering(MO.getReg());
Owen Anderson526ffd52010-11-02 01:24:55 +00001460}
1461
Bill Wendling3b1459b2011-03-01 01:00:59 +00001462unsigned ARMMCCodeEmitter::
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001463getShiftRight8Imm(const MCInst &MI, unsigned Op,
1464 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling3b1459b2011-03-01 01:00:59 +00001465 return 8 - MI.getOperand(Op).getImm();
1466}
1467
1468unsigned ARMMCCodeEmitter::
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001469getShiftRight16Imm(const MCInst &MI, unsigned Op,
1470 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling3b1459b2011-03-01 01:00:59 +00001471 return 16 - MI.getOperand(Op).getImm();
1472}
1473
1474unsigned ARMMCCodeEmitter::
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001475getShiftRight32Imm(const MCInst &MI, unsigned Op,
1476 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling3b1459b2011-03-01 01:00:59 +00001477 return 32 - MI.getOperand(Op).getImm();
1478}
1479
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001480unsigned ARMMCCodeEmitter::
1481getShiftRight64Imm(const MCInst &MI, unsigned Op,
1482 SmallVectorImpl<MCFixup> &Fixups) const {
1483 return 64 - MI.getOperand(Op).getImm();
1484}
1485
Jim Grosbach1287f4f2010-09-17 18:46:17 +00001486void ARMMCCodeEmitter::
1487EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001488 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach91029092010-10-07 22:12:50 +00001489 // Pseudo instructions don't get encoded.
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001490 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Jim Grosbach20b6fd72010-11-11 23:41:09 +00001491 uint64_t TSFlags = Desc.TSFlags;
1492 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbach91029092010-10-07 22:12:50 +00001493 return;
Owen Anderson651b2302011-07-13 23:22:26 +00001494
Jim Grosbach20b6fd72010-11-11 23:41:09 +00001495 int Size;
Owen Anderson651b2302011-07-13 23:22:26 +00001496 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1497 Size = Desc.getSize();
1498 else
1499 llvm_unreachable("Unexpected instruction size!");
Owen Anderson1732c2e2011-08-30 21:58:18 +00001500
Jim Grosbach567ebd0c2010-12-03 22:31:40 +00001501 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
Evan Cheng965b3c72011-01-13 07:58:56 +00001502 // Thumb 32-bit wide instructions need to emit the high order halfword
1503 // first.
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001504 if (isThumb() && Size == 4) {
Jim Grosbach567ebd0c2010-12-03 22:31:40 +00001505 EmitConstant(Binary >> 16, 2, OS);
1506 EmitConstant(Binary & 0xffff, 2, OS);
1507 } else
1508 EmitConstant(Binary, Size, OS);
Bill Wendling91da9ab2010-11-02 22:44:12 +00001509 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach1287f4f2010-09-17 18:46:17 +00001510}
Jim Grosbach8aed3862010-10-07 21:57:55 +00001511
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001512#include "ARMGenMCCodeEmitter.inc"