blob: 87f633e49def32310351874c006837118ce79d07 [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;
Owen Andersonb205c022011-08-26 23:32:08 +0000121 uint32_t getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
122 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson578074b2010-12-13 19:31:11 +0000123
Jim Grosbachdc35e062010-12-01 19:47:31 +0000124 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
125 /// ADR label target.
126 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
127 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000128 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6d375e52010-12-14 00:36:49 +0000130 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
131 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000132
Jim Grosbachdc35e062010-12-01 19:47:31 +0000133
Bill Wendlinge84eb992010-11-03 01:49:29 +0000134 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
135 /// operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000136 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
137 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendlinge84eb992010-11-03 01:49:29 +0000138
Bill Wendling092a7bd2010-12-14 03:36:38 +0000139 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
140 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
141 SmallVectorImpl<MCFixup> &Fixups)const;
Owen Andersonb0fa1272010-12-10 22:11:13 +0000142
Owen Anderson943fb602010-12-01 19:18:46 +0000143 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
144 /// operand.
145 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
146 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbacha05627e2011-09-09 18:37:27 +0000147
148 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for 'reg + imm8<<2'
149 /// operand.
150 uint32_t getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
151 SmallVectorImpl<MCFixup> &Fixups) const;
152
Jim Grosbach7db8d692011-09-08 22:07:06 +0000153 /// getT2Imm8s4OpValue - Return encoding info for '+/- imm8<<2'
154 /// operand.
155 uint32_t getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
156 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson943fb602010-12-01 19:18:46 +0000157
158
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000159 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
160 /// operand as needed by load/store instructions.
161 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
162 SmallVectorImpl<MCFixup> &Fixups) const;
163
Jim Grosbachcc4a4912010-11-10 23:38:36 +0000164 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
165 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
166 SmallVectorImpl<MCFixup> &Fixups) const {
167 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
168 switch (Mode) {
Matt Beaumont-Gay3077bb62011-01-12 18:02:55 +0000169 default: assert(0 && "Unknown addressing sub-mode!");
Jim Grosbachcc4a4912010-11-10 23:38:36 +0000170 case ARM_AM::da: return 0;
171 case ARM_AM::ia: return 1;
172 case ARM_AM::db: return 2;
173 case ARM_AM::ib: return 3;
174 }
175 }
Jim Grosbach38b469e2010-11-15 20:47:07 +0000176 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
177 ///
178 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
179 switch (ShOpc) {
Jim Grosbach38b469e2010-11-15 20:47:07 +0000180 case ARM_AM::no_shift:
181 case ARM_AM::lsl: return 0;
182 case ARM_AM::lsr: return 1;
183 case ARM_AM::asr: return 2;
184 case ARM_AM::ror:
185 case ARM_AM::rrx: return 3;
186 }
David Blaikie46a9f012012-01-20 21:51:11 +0000187 llvm_unreachable("Invalid ShiftOpc!");
Jim Grosbach38b469e2010-11-15 20:47:07 +0000188 }
189
190 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
191 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
192 SmallVectorImpl<MCFixup> &Fixups) const;
193
194 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
195 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
196 SmallVectorImpl<MCFixup> &Fixups) const;
197
Jim Grosbachd3595712011-08-03 23:50:40 +0000198 /// getPostIdxRegOpValue - Return encoding for postidx_reg operands.
199 uint32_t getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
200 SmallVectorImpl<MCFixup> &Fixups) const;
201
Jim Grosbach68685e62010-11-11 16:55:29 +0000202 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
203 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
204 SmallVectorImpl<MCFixup> &Fixups) const;
205
Jim Grosbach607efcb2010-11-11 01:09:40 +0000206 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
207 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
208 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachcc4a4912010-11-10 23:38:36 +0000209
Jim Grosbach49bcd6f2010-12-07 21:50:47 +0000210 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
211 /// operand.
212 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
213 SmallVectorImpl<MCFixup> &Fixups) const;
214
Bill Wendling092a7bd2010-12-14 03:36:38 +0000215 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
216 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling03e75762010-12-15 08:51:02 +0000217 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendlinga9e3df72010-11-30 22:57:21 +0000218
Bill Wendling8a6449c2010-12-08 01:57:09 +0000219 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
220 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
221 SmallVectorImpl<MCFixup> &Fixups) const;
222
Bill Wendlinge84eb992010-11-03 01:49:29 +0000223 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000224 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
225 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000226
Jim Grosbachd9d31da2010-10-12 23:00:24 +0000227 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000228 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
229 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd9d31da2010-10-12 23:00:24 +0000230 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
231 // '1' respectively.
232 return MI.getOperand(Op).getReg() == ARM::CPSR;
233 }
Jim Grosbachefd53692010-10-12 23:53:58 +0000234
Jim Grosbach12e493a2010-10-12 23:18:08 +0000235 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000236 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
237 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach12e493a2010-10-12 23:18:08 +0000238 unsigned SoImm = MI.getOperand(Op).getImm();
239 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
240 assert(SoImmVal != -1 && "Not a valid so_imm value!");
241
242 // Encode rotate_imm.
243 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
244 << ARMII::SoRotImmShift;
245
246 // Encode immed_8.
247 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
248 return Binary;
249 }
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000250
Owen Anderson8fdd1722010-11-12 21:12:40 +0000251 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
252 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
253 SmallVectorImpl<MCFixup> &Fixups) const {
254 unsigned SoImm = MI.getOperand(Op).getImm();
255 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
256 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
257 return Encoded;
258 }
Jim Grosbachd9d31da2010-10-12 23:00:24 +0000259
Owen Anderson50d662b2010-11-29 22:44:32 +0000260 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
261 SmallVectorImpl<MCFixup> &Fixups) const;
262 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
263 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersone22c7322010-11-30 00:14:31 +0000264 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
265 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson299382e2010-11-30 19:19:31 +0000266 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
267 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson50d662b2010-11-29 22:44:32 +0000268
Jim Grosbachefd53692010-10-12 23:53:58 +0000269 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Owen Anderson04912702011-07-21 23:38:37 +0000270 unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op,
271 SmallVectorImpl<MCFixup> &Fixups) const;
272 unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op,
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000273 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson8fdd1722010-11-12 21:12:40 +0000274 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
275 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachefd53692010-10-12 23:53:58 +0000276
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000277 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
278 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfadb9512010-10-27 22:49:00 +0000279 return 64 - MI.getOperand(Op).getImm();
280 }
Jim Grosbach68a335e2010-10-15 17:15:16 +0000281
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000282 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
283 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5edb03e2010-10-21 22:03:21 +0000284
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000285 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
286 SmallVectorImpl<MCFixup> &Fixups) const;
287 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
288 SmallVectorImpl<MCFixup> &Fixups) const;
Mon P Wang92ff16b2011-05-09 17:47:27 +0000289 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
290 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson318ce7c2010-11-30 00:00:42 +0000291 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
292 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000293 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
294 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach74ef9e12010-10-30 00:37:59 +0000295
Bill Wendling77ad1dc2011-03-07 23:38:41 +0000296 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
297 SmallVectorImpl<MCFixup> &Fixups) const;
298 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
299 SmallVectorImpl<MCFixup> &Fixups) const;
300 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
301 SmallVectorImpl<MCFixup> &Fixups) const;
302 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
303 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling3b1459b2011-03-01 01:00:59 +0000304
Owen Andersonc4030382011-08-08 20:42:17 +0000305 unsigned getThumbSRImmOpValue(const MCInst &MI, unsigned Op,
306 SmallVectorImpl<MCFixup> &Fixups) const;
307
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000308 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
309 unsigned EncodedValue) const;
Owen Anderson99a8cb42010-11-11 21:36:43 +0000310 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendling87240d42010-12-01 21:54:50 +0000311 unsigned EncodedValue) const;
Owen Andersonce2250f2010-11-11 23:12:55 +0000312 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendling87240d42010-12-01 21:54:50 +0000313 unsigned EncodedValue) const;
314
315 unsigned VFPThumb2PostEncoder(const MCInst &MI,
316 unsigned EncodedValue) const;
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000317
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000318 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000319 OS << (char)C;
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000320 }
321
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000322 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000323 // Output the constant in little endian byte order.
324 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000325 EmitByte(Val & 255, OS);
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000326 Val >>= 8;
327 }
328 }
329
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000330 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
331 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000332};
333
334} // end anonymous namespace
335
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000336MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
337 const MCSubtargetInfo &STI,
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000338 MCContext &Ctx) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000339 return new ARMMCCodeEmitter(MCII, STI, Ctx);
Jim Grosbach1287f4f2010-09-17 18:46:17 +0000340}
341
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000342/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
343/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000344/// Thumb2 mode.
345unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
346 unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000347 if (isThumb2()) {
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000348 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000349 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
350 // set to 1111.
351 unsigned Bit24 = EncodedValue & 0x01000000;
352 unsigned Bit28 = Bit24 << 4;
353 EncodedValue &= 0xEFFFFFFF;
354 EncodedValue |= Bit28;
355 EncodedValue |= 0x0F000000;
356 }
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000357
Owen Anderson7ffe3b32010-11-11 19:07:48 +0000358 return EncodedValue;
359}
360
Owen Anderson99a8cb42010-11-11 21:36:43 +0000361/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000362/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson99a8cb42010-11-11 21:36:43 +0000363/// Thumb2 mode.
364unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
365 unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000366 if (isThumb2()) {
Owen Anderson99a8cb42010-11-11 21:36:43 +0000367 EncodedValue &= 0xF0FFFFFF;
368 EncodedValue |= 0x09000000;
369 }
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000370
Owen Anderson99a8cb42010-11-11 21:36:43 +0000371 return EncodedValue;
372}
373
Owen Andersonce2250f2010-11-11 23:12:55 +0000374/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000375/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonce2250f2010-11-11 23:12:55 +0000376/// Thumb2 mode.
377unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
378 unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000379 if (isThumb2()) {
Owen Andersonce2250f2010-11-11 23:12:55 +0000380 EncodedValue &= 0x00FFFFFF;
381 EncodedValue |= 0xEE000000;
382 }
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000383
Owen Andersonce2250f2010-11-11 23:12:55 +0000384 return EncodedValue;
385}
386
Bill Wendling87240d42010-12-01 21:54:50 +0000387/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
388/// them to their Thumb2 form if we are currently in Thumb2 mode.
389unsigned ARMMCCodeEmitter::
390VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000391 if (isThumb2()) {
Bill Wendling87240d42010-12-01 21:54:50 +0000392 EncodedValue &= 0x0FFFFFFF;
393 EncodedValue |= 0xE0000000;
394 }
395 return EncodedValue;
396}
Owen Anderson99a8cb42010-11-11 21:36:43 +0000397
Jim Grosbachc43c9302010-10-08 21:45:55 +0000398/// getMachineOpValue - Return binary encoding of operand. If the machine
399/// operand requires relocation, record the relocation and return zero.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000400unsigned ARMMCCodeEmitter::
401getMachineOpValue(const MCInst &MI, const MCOperand &MO,
402 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6f52f8a2010-10-14 02:33:26 +0000403 if (MO.isReg()) {
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000404 unsigned Reg = MO.getReg();
405 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbach96d82842010-10-29 23:21:03 +0000406
Jim Grosbachee48d2d2010-11-30 23:51:41 +0000407 // Q registers are encoded as 2x their register number.
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000408 switch (Reg) {
409 default:
410 return RegNo;
411 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
412 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
413 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
414 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
415 return 2 * RegNo;
Owen Anderson2bfa8ed2010-10-21 20:49:13 +0000416 }
Bill Wendling6f52f8a2010-10-14 02:33:26 +0000417 } else if (MO.isImm()) {
Jim Grosbachc43c9302010-10-08 21:45:55 +0000418 return static_cast<unsigned>(MO.getImm());
Bill Wendling6f52f8a2010-10-14 02:33:26 +0000419 } else if (MO.isFPImm()) {
420 return static_cast<unsigned>(APFloat(MO.getFPImm())
421 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbachc43c9302010-10-08 21:45:55 +0000422 }
Bill Wendlingf9eebb52010-11-02 22:53:11 +0000423
Jim Grosbach2aeb8b92010-11-19 00:27:09 +0000424 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbachc43c9302010-10-08 21:45:55 +0000425}
426
Bill Wendling603bd8f2010-11-02 22:31:46 +0000427/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000428bool ARMMCCodeEmitter::
429EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
430 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000431 const MCOperand &MO = MI.getOperand(OpIdx);
432 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach2ba03aa2010-11-01 23:45:50 +0000433
Bill Wendlinge84eb992010-11-03 01:49:29 +0000434 Reg = getARMRegisterNumbering(MO.getReg());
435
436 int32_t SImm = MO1.getImm();
437 bool isAdd = true;
Bill Wendling603bd8f2010-11-02 22:31:46 +0000438
Jim Grosbach505607e2010-10-28 18:34:10 +0000439 // Special value for #-0
Owen Anderson967674d2011-08-29 19:36:44 +0000440 if (SImm == INT32_MIN) {
Bill Wendlinge84eb992010-11-03 01:49:29 +0000441 SImm = 0;
Owen Anderson967674d2011-08-29 19:36:44 +0000442 isAdd = false;
443 }
Bill Wendling603bd8f2010-11-02 22:31:46 +0000444
Jim Grosbach505607e2010-10-28 18:34:10 +0000445 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendlinge84eb992010-11-03 01:49:29 +0000446 if (SImm < 0) {
447 SImm = -SImm;
448 isAdd = false;
449 }
Bill Wendling603bd8f2010-11-02 22:31:46 +0000450
Bill Wendlinge84eb992010-11-03 01:49:29 +0000451 Imm = SImm;
452 return isAdd;
453}
454
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000455/// getBranchTargetOpValue - Helper function to get the branch target operand,
456/// which is either an immediate or requires a fixup.
457static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
458 unsigned FixupKind,
459 SmallVectorImpl<MCFixup> &Fixups) {
460 const MCOperand &MO = MI.getOperand(OpIdx);
461
462 // If the destination is an immediate, we have nothing to do.
463 if (MO.isImm()) return MO.getImm();
464 assert(MO.isExpr() && "Unexpected branch target type!");
465 const MCExpr *Expr = MO.getExpr();
466 MCFixupKind Kind = MCFixupKind(FixupKind);
467 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
468
469 // All of the information is in the fixup.
470 return 0;
471}
472
Owen Anderson5c160fd2011-08-31 18:30:20 +0000473// Thumb BL and BLX use a strange offset encoding where bits 22 and 21 are
474// determined by negating them and XOR'ing them with bit 23.
475static int32_t encodeThumbBLOffset(int32_t offset) {
476 offset >>= 1;
477 uint32_t S = (offset & 0x800000) >> 23;
478 uint32_t J1 = (offset & 0x400000) >> 22;
479 uint32_t J2 = (offset & 0x200000) >> 21;
480 J1 = (~J1 & 0x1);
481 J2 = (~J2 & 0x1);
482 J1 ^= S;
483 J2 ^= S;
484
485 offset &= ~0x600000;
486 offset |= J1 << 22;
487 offset |= J2 << 21;
488
489 return offset;
490}
491
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000492/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach9e199462010-12-06 23:57:07 +0000493uint32_t ARMMCCodeEmitter::
494getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
495 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson5c160fd2011-08-31 18:30:20 +0000496 const MCOperand MO = MI.getOperand(OpIdx);
497 if (MO.isExpr())
498 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl,
499 Fixups);
500 return encodeThumbBLOffset(MO.getImm());
Jim Grosbach9e199462010-12-06 23:57:07 +0000501}
502
Bill Wendling3392bfc2010-12-09 00:39:08 +0000503/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
504/// BLX branch target.
505uint32_t ARMMCCodeEmitter::
506getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
507 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson5c160fd2011-08-31 18:30:20 +0000508 const MCOperand MO = MI.getOperand(OpIdx);
509 if (MO.isExpr())
510 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx,
511 Fixups);
512 return encodeThumbBLOffset(MO.getImm());
Bill Wendling3392bfc2010-12-09 00:39:08 +0000513}
514
Jim Grosbache119da12010-12-10 18:21:33 +0000515/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
516uint32_t ARMMCCodeEmitter::
517getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
518 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson543c89f2011-08-30 22:03:20 +0000519 const MCOperand MO = MI.getOperand(OpIdx);
520 if (MO.isExpr())
Owen Anderson5c160fd2011-08-31 18:30:20 +0000521 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br,
522 Fixups);
Owen Anderson543c89f2011-08-30 22:03:20 +0000523 return (MO.getImm() >> 1);
Jim Grosbache119da12010-12-10 18:21:33 +0000524}
525
Jim Grosbach78485ad2010-12-10 17:13:40 +0000526/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
527uint32_t ARMMCCodeEmitter::
528getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache119da12010-12-10 18:21:33 +0000529 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona455a0b2011-08-31 20:26:14 +0000530 const MCOperand MO = MI.getOperand(OpIdx);
531 if (MO.isExpr())
532 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc,
533 Fixups);
534 return (MO.getImm() >> 1);
Jim Grosbach78485ad2010-12-10 17:13:40 +0000535}
536
Jim Grosbach62b68112010-12-09 19:04:53 +0000537/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000538uint32_t ARMMCCodeEmitter::
Jim Grosbach62b68112010-12-09 19:04:53 +0000539getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000540 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfdf3cd72011-08-30 22:15:17 +0000541 const MCOperand MO = MI.getOperand(OpIdx);
542 if (MO.isExpr())
543 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
544 return (MO.getImm() >> 1);
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000545}
546
Jason W Kimd2e2f562011-02-04 19:47:15 +0000547/// Return true if this branch has a non-always predication
548static bool HasConditionalBranch(const MCInst &MI) {
549 int NumOp = MI.getNumOperands();
550 if (NumOp >= 2) {
551 for (int i = 0; i < NumOp-1; ++i) {
552 const MCOperand &MCOp1 = MI.getOperand(i);
553 const MCOperand &MCOp2 = MI.getOperand(i + 1);
Owen Anderson1732c2e2011-08-30 21:58:18 +0000554 if (MCOp1.isImm() && MCOp2.isReg() &&
Jason W Kimd2e2f562011-02-04 19:47:15 +0000555 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
Owen Anderson1732c2e2011-08-30 21:58:18 +0000556 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
Jason W Kimd2e2f562011-02-04 19:47:15 +0000557 return true;
558 }
559 }
560 }
561 return false;
562}
563
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000564/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
565/// target.
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000566uint32_t ARMMCCodeEmitter::
567getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000568 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachaecdd872010-12-10 23:41:10 +0000569 // FIXME: This really, really shouldn't use TargetMachine. We don't want
570 // coupling between MC and TM anywhere we can help it.
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000571 if (isThumb2())
Owen Anderson578074b2010-12-13 19:31:11 +0000572 return
573 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Jason W Kimd2e2f562011-02-04 19:47:15 +0000574 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000575}
576
Jason W Kimd2e2f562011-02-04 19:47:15 +0000577/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
578/// target.
579uint32_t ARMMCCodeEmitter::
580getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
581 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson6c70e582011-08-26 22:54:51 +0000582 const MCOperand MO = MI.getOperand(OpIdx);
583 if (MO.isExpr()) {
Owen Anderson1732c2e2011-08-30 21:58:18 +0000584 if (HasConditionalBranch(MI))
Owen Anderson6c70e582011-08-26 22:54:51 +0000585 return ::getBranchTargetOpValue(MI, OpIdx,
586 ARM::fixup_arm_condbranch, Fixups);
Owen Anderson1732c2e2011-08-30 21:58:18 +0000587 return ::getBranchTargetOpValue(MI, OpIdx,
Owen Anderson6c70e582011-08-26 22:54:51 +0000588 ARM::fixup_arm_uncondbranch, Fixups);
589 }
590
591 return MO.getImm() >> 2;
Jason W Kimd2e2f562011-02-04 19:47:15 +0000592}
593
Owen Andersonb205c022011-08-26 23:32:08 +0000594uint32_t ARMMCCodeEmitter::
595getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
596 SmallVectorImpl<MCFixup> &Fixups) const {
597 const MCOperand MO = MI.getOperand(OpIdx);
598 if (MO.isExpr()) {
Owen Anderson1732c2e2011-08-30 21:58:18 +0000599 if (HasConditionalBranch(MI))
Owen Andersonb205c022011-08-26 23:32:08 +0000600 return ::getBranchTargetOpValue(MI, OpIdx,
601 ARM::fixup_arm_condbranch, Fixups);
Owen Anderson1732c2e2011-08-30 21:58:18 +0000602 return ::getBranchTargetOpValue(MI, OpIdx,
Owen Andersonb205c022011-08-26 23:32:08 +0000603 ARM::fixup_arm_uncondbranch, Fixups);
604 }
Jason W Kimd2e2f562011-02-04 19:47:15 +0000605
Owen Andersonb205c022011-08-26 23:32:08 +0000606 return MO.getImm() >> 1;
607}
Jason W Kimd2e2f562011-02-04 19:47:15 +0000608
Owen Anderson578074b2010-12-13 19:31:11 +0000609/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
610/// immediate branch target.
611uint32_t ARMMCCodeEmitter::
612getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
613 SmallVectorImpl<MCFixup> &Fixups) const {
614 unsigned Val =
615 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
616 bool I = (Val & 0x800000);
617 bool J1 = (Val & 0x400000);
618 bool J2 = (Val & 0x200000);
619 if (I ^ J1)
620 Val &= ~0x400000;
621 else
622 Val |= 0x400000;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000623
Owen Anderson578074b2010-12-13 19:31:11 +0000624 if (I ^ J2)
625 Val &= ~0x200000;
626 else
627 Val |= 0x200000;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000628
Owen Anderson578074b2010-12-13 19:31:11 +0000629 return Val;
630}
631
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000632/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
633/// target.
Jim Grosbachdc35e062010-12-01 19:47:31 +0000634uint32_t ARMMCCodeEmitter::
635getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
636 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona01bcbf2011-08-26 18:09:22 +0000637 const MCOperand MO = MI.getOperand(OpIdx);
638 if (MO.isExpr())
639 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
640 Fixups);
641 int32_t offset = MO.getImm();
642 uint32_t Val = 0x2000;
643 if (offset < 0) {
644 Val = 0x1000;
645 offset *= -1;
646 }
647 Val |= offset;
648 return Val;
Jim Grosbachdc35e062010-12-01 19:47:31 +0000649}
650
Owen Anderson6d375e52010-12-14 00:36:49 +0000651/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
652/// target.
653uint32_t ARMMCCodeEmitter::
654getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
655 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona01bcbf2011-08-26 18:09:22 +0000656 const MCOperand MO = MI.getOperand(OpIdx);
657 if (MO.isExpr())
658 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
659 Fixups);
Owen Anderson5bfb0e02011-09-09 22:24:36 +0000660 int32_t Val = MO.getImm();
661 if (Val < 0) {
662 Val *= -1;
663 Val |= 0x1000;
664 }
665 return Val;
Owen Anderson6d375e52010-12-14 00:36:49 +0000666}
667
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000668/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
669/// target.
670uint32_t ARMMCCodeEmitter::
671getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
672 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona01bcbf2011-08-26 18:09:22 +0000673 const MCOperand MO = MI.getOperand(OpIdx);
674 if (MO.isExpr())
675 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
676 Fixups);
677 return MO.getImm();
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000678}
679
Bill Wendling092a7bd2010-12-14 03:36:38 +0000680/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
681/// operand.
Owen Andersonb0fa1272010-12-10 22:11:13 +0000682uint32_t ARMMCCodeEmitter::
Bill Wendling092a7bd2010-12-14 03:36:38 +0000683getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
684 SmallVectorImpl<MCFixup> &) const {
685 // [Rn, Rm]
686 // {5-3} = Rm
687 // {2-0} = Rn
Owen Andersonb0fa1272010-12-10 22:11:13 +0000688 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendling092a7bd2010-12-14 03:36:38 +0000689 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Andersonb0fa1272010-12-10 22:11:13 +0000690 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
691 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
692 return (Rm << 3) | Rn;
693}
694
Bill Wendlinge84eb992010-11-03 01:49:29 +0000695/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +0000696uint32_t ARMMCCodeEmitter::
697getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
698 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinge84eb992010-11-03 01:49:29 +0000699 // {17-13} = reg
700 // {12} = (U)nsigned (add == '1', sub == '0')
701 // {11-0} = imm12
702 unsigned Reg, Imm12;
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000703 bool isAdd = true;
704 // If The first operand isn't a register, we have a label reference.
705 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Anderson4ebf4712011-02-08 22:39:40 +0000706 if (!MO.isReg()) {
Jim Grosbach90987142010-11-09 01:37:15 +0000707 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000708 Imm12 = 0;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +0000709 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000710
Owen Anderson4a9eb5f2011-09-12 20:36:51 +0000711 if (MO.isExpr()) {
712 const MCExpr *Expr = MO.getExpr();
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000713
Owen Anderson4a9eb5f2011-09-12 20:36:51 +0000714 MCFixupKind Kind;
715 if (isThumb2())
716 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
717 else
718 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
719 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000720
Owen Anderson4a9eb5f2011-09-12 20:36:51 +0000721 ++MCNumCPRelocations;
722 } else {
723 Reg = ARM::PC;
724 int32_t Offset = MO.getImm();
Jim Grosbach94298a92012-01-18 22:46:46 +0000725 // FIXME: Handle #-0.
Owen Anderson4a9eb5f2011-09-12 20:36:51 +0000726 if (Offset < 0) {
727 Offset *= -1;
728 isAdd = false;
729 }
730 Imm12 = Offset;
731 }
Jim Grosbach0fb841f2010-11-04 01:12:30 +0000732 } else
733 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendlinge84eb992010-11-03 01:49:29 +0000734
Bill Wendlinge84eb992010-11-03 01:49:29 +0000735 uint32_t Binary = Imm12 & 0xfff;
736 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach505607e2010-10-28 18:34:10 +0000737 if (isAdd)
Bill Wendlinge84eb992010-11-03 01:49:29 +0000738 Binary |= (1 << 12);
739 Binary |= (Reg << 13);
740 return Binary;
741}
742
Jim Grosbach7db8d692011-09-08 22:07:06 +0000743/// getT2Imm8s4OpValue - Return encoding info for
744/// '+/- imm8<<2' operand.
745uint32_t ARMMCCodeEmitter::
746getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
747 SmallVectorImpl<MCFixup> &Fixups) const {
748 // FIXME: The immediate operand should have already been encoded like this
749 // before ever getting here. The encoder method should just need to combine
750 // the MI operands for the register and the offset into a single
751 // representation for the complex operand in the .td file. This isn't just
752 // style, unfortunately. As-is, we can't represent the distinct encoding
753 // for #-0.
754
755 // {8} = (U)nsigned (add == '1', sub == '0')
756 // {7-0} = imm8
757 int32_t Imm8 = MI.getOperand(OpIdx).getImm();
758 bool isAdd = Imm8 >= 0;
759
760 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
761 if (Imm8 < 0)
762 Imm8 = -Imm8;
763
764 // Scaled by 4.
765 Imm8 /= 4;
766
767 uint32_t Binary = Imm8 & 0xff;
768 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
769 if (isAdd)
770 Binary |= (1 << 8);
771 return Binary;
772}
773
Owen Anderson943fb602010-12-01 19:18:46 +0000774/// getT2AddrModeImm8s4OpValue - Return encoding info for
775/// 'reg +/- imm8<<2' operand.
776uint32_t ARMMCCodeEmitter::
777getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
778 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbache69f7242010-12-10 21:05:07 +0000779 // {12-9} = reg
780 // {8} = (U)nsigned (add == '1', sub == '0')
781 // {7-0} = imm8
Owen Anderson943fb602010-12-01 19:18:46 +0000782 unsigned Reg, Imm8;
783 bool isAdd = true;
784 // If The first operand isn't a register, we have a label reference.
785 const MCOperand &MO = MI.getOperand(OpIdx);
786 if (!MO.isReg()) {
787 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
788 Imm8 = 0;
789 isAdd = false ; // 'U' bit is set as part of the fixup.
790
791 assert(MO.isExpr() && "Unexpected machine operand type!");
792 const MCExpr *Expr = MO.getExpr();
Jim Grosbach8648c102011-12-19 23:06:24 +0000793 MCFixupKind Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
Owen Anderson943fb602010-12-01 19:18:46 +0000794 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
795
796 ++MCNumCPRelocations;
797 } else
798 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
799
Jim Grosbach7db8d692011-09-08 22:07:06 +0000800 // FIXME: The immediate operand should have already been encoded like this
801 // before ever getting here. The encoder method should just need to combine
802 // the MI operands for the register and the offset into a single
803 // representation for the complex operand in the .td file. This isn't just
804 // style, unfortunately. As-is, we can't represent the distinct encoding
805 // for #-0.
Owen Anderson943fb602010-12-01 19:18:46 +0000806 uint32_t Binary = (Imm8 >> 2) & 0xff;
807 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
808 if (isAdd)
Jim Grosbache69f7242010-12-10 21:05:07 +0000809 Binary |= (1 << 8);
Owen Anderson943fb602010-12-01 19:18:46 +0000810 Binary |= (Reg << 9);
811 return Binary;
812}
813
Jim Grosbacha05627e2011-09-09 18:37:27 +0000814/// getT2AddrModeImm0_1020s4OpValue - Return encoding info for
815/// 'reg + imm8<<2' operand.
816uint32_t ARMMCCodeEmitter::
817getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
818 SmallVectorImpl<MCFixup> &Fixups) const {
819 // {11-8} = reg
820 // {7-0} = imm8
821 const MCOperand &MO = MI.getOperand(OpIdx);
822 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
823 unsigned Reg = getARMRegisterNumbering(MO.getReg());
824 unsigned Imm8 = MO1.getImm();
825 return (Reg << 8) | Imm8;
826}
827
Jason W Kim9c5b65d2011-01-12 00:19:25 +0000828// FIXME: This routine assumes that a binary
829// expression will always result in a PCRel expression
830// In reality, its only true if one or more subexpressions
831// is itself a PCRel (i.e. "." in asm or some other pcrel construct)
832// but this is good enough for now.
833static bool EvaluateAsPCRel(const MCExpr *Expr) {
834 switch (Expr->getKind()) {
Matt Beaumont-Gay3077bb62011-01-12 18:02:55 +0000835 default: assert(0 && "Unexpected expression type");
Jason W Kim9c5b65d2011-01-12 00:19:25 +0000836 case MCExpr::SymbolRef: return false;
837 case MCExpr::Binary: return true;
Jason W Kim9c5b65d2011-01-12 00:19:25 +0000838 }
839}
840
Evan Cheng965b3c72011-01-13 07:58:56 +0000841uint32_t
842ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
843 SmallVectorImpl<MCFixup> &Fixups) const {
Jason W Kim5a97bd82010-11-18 23:37:15 +0000844 // {20-16} = imm{15-12}
845 // {11-0} = imm{11-0}
Jim Grosbachc4a0c292010-12-10 21:57:34 +0000846 const MCOperand &MO = MI.getOperand(OpIdx);
Evan Cheng965b3c72011-01-13 07:58:56 +0000847 if (MO.isImm())
848 // Hi / lo 16 bits already extracted during earlier passes.
Jason W Kim5a97bd82010-11-18 23:37:15 +0000849 return static_cast<unsigned>(MO.getImm());
Evan Cheng965b3c72011-01-13 07:58:56 +0000850
851 // Handle :upper16: and :lower16: assembly prefixes.
852 const MCExpr *E = MO.getExpr();
853 if (E->getKind() == MCExpr::Target) {
854 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
855 E = ARM16Expr->getSubExpr();
856
Jason W Kim5a97bd82010-11-18 23:37:15 +0000857 MCFixupKind Kind;
Evan Cheng965b3c72011-01-13 07:58:56 +0000858 switch (ARM16Expr->getKind()) {
Matt Beaumont-Gay3077bb62011-01-12 18:02:55 +0000859 default: assert(0 && "Unsupported ARMFixup");
Evan Cheng965b3c72011-01-13 07:58:56 +0000860 case ARMMCExpr::VK_ARM_HI16:
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000861 if (!isTargetDarwin() && EvaluateAsPCRel(E))
862 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000863 ? ARM::fixup_t2_movt_hi16_pcrel
864 : ARM::fixup_arm_movt_hi16_pcrel);
865 else
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000866 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000867 ? ARM::fixup_t2_movt_hi16
868 : ARM::fixup_arm_movt_hi16);
Jason W Kim5a97bd82010-11-18 23:37:15 +0000869 break;
Evan Cheng965b3c72011-01-13 07:58:56 +0000870 case ARMMCExpr::VK_ARM_LO16:
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000871 if (!isTargetDarwin() && EvaluateAsPCRel(E))
872 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000873 ? ARM::fixup_t2_movw_lo16_pcrel
874 : ARM::fixup_arm_movw_lo16_pcrel);
875 else
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000876 Kind = MCFixupKind(isThumb2()
Evan Chengd4a5c052011-01-14 02:38:49 +0000877 ? ARM::fixup_t2_movw_lo16
878 : ARM::fixup_arm_movw_lo16);
Jason W Kim5a97bd82010-11-18 23:37:15 +0000879 break;
Jason W Kim5a97bd82010-11-18 23:37:15 +0000880 }
Evan Cheng965b3c72011-01-13 07:58:56 +0000881 Fixups.push_back(MCFixup::Create(0, E, Kind));
Jason W Kim5a97bd82010-11-18 23:37:15 +0000882 return 0;
Jim Grosbach2aeb8b92010-11-19 00:27:09 +0000883 };
Evan Cheng965b3c72011-01-13 07:58:56 +0000884
Jim Grosbach2aeb8b92010-11-19 00:27:09 +0000885 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim5a97bd82010-11-18 23:37:15 +0000886}
887
888uint32_t ARMMCCodeEmitter::
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000889getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
890 SmallVectorImpl<MCFixup> &Fixups) const {
891 const MCOperand &MO = MI.getOperand(OpIdx);
892 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
893 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
894 unsigned Rn = getARMRegisterNumbering(MO.getReg());
895 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000896 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
897 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach38b469e2010-11-15 20:47:07 +0000898 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
899 unsigned SBits = getShiftOp(ShOp);
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000900
901 // {16-13} = Rn
902 // {12} = isAdd
903 // {11-0} = shifter
904 // {3-0} = Rm
905 // {4} = 0
906 // {6-5} = type
907 // {11-7} = imm
Jim Grosbach607efcb2010-11-11 01:09:40 +0000908 uint32_t Binary = Rm;
Jim Grosbachdbfb5ed2010-11-09 17:20:53 +0000909 Binary |= Rn << 13;
910 Binary |= SBits << 5;
911 Binary |= ShImm << 7;
912 if (isAdd)
913 Binary |= 1 << 12;
914 return Binary;
915}
916
Jim Grosbach607efcb2010-11-11 01:09:40 +0000917uint32_t ARMMCCodeEmitter::
Jim Grosbach38b469e2010-11-15 20:47:07 +0000918getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
919 SmallVectorImpl<MCFixup> &Fixups) const {
920 // {17-14} Rn
921 // {13} 1 == imm12, 0 == Rm
922 // {12} isAdd
923 // {11-0} imm12/Rm
924 const MCOperand &MO = MI.getOperand(OpIdx);
925 unsigned Rn = getARMRegisterNumbering(MO.getReg());
926 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
927 Binary |= Rn << 14;
928 return Binary;
929}
930
931uint32_t ARMMCCodeEmitter::
932getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
933 SmallVectorImpl<MCFixup> &Fixups) const {
934 // {13} 1 == imm12, 0 == Rm
935 // {12} isAdd
936 // {11-0} imm12/Rm
937 const MCOperand &MO = MI.getOperand(OpIdx);
938 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
939 unsigned Imm = MO1.getImm();
940 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
941 bool isReg = MO.getReg() != 0;
942 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
943 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
944 if (isReg) {
945 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
946 Binary <<= 7; // Shift amount is bits [11:7]
947 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
948 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
949 }
950 return Binary | (isAdd << 12) | (isReg << 13);
951}
952
953uint32_t ARMMCCodeEmitter::
Jim Grosbachd3595712011-08-03 23:50:40 +0000954getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
955 SmallVectorImpl<MCFixup> &Fixups) const {
956 // {4} isAdd
957 // {3-0} Rm
958 const MCOperand &MO = MI.getOperand(OpIdx);
959 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
Jim Grosbacha70fbfd52011-08-05 16:11:38 +0000960 bool isAdd = MO1.getImm() != 0;
Jim Grosbachd3595712011-08-03 23:50:40 +0000961 return getARMRegisterNumbering(MO.getReg()) | (isAdd << 4);
962}
963
964uint32_t ARMMCCodeEmitter::
Jim Grosbach68685e62010-11-11 16:55:29 +0000965getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
966 SmallVectorImpl<MCFixup> &Fixups) const {
967 // {9} 1 == imm8, 0 == Rm
968 // {8} isAdd
969 // {7-4} imm7_4/zero
970 // {3-0} imm3_0/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::getAM3Op(Imm) == ARM_AM::add;
975 bool isImm = MO.getReg() == 0;
976 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
977 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
978 if (!isImm)
979 Imm8 = getARMRegisterNumbering(MO.getReg());
980 return Imm8 | (isAdd << 8) | (isImm << 9);
981}
982
983uint32_t ARMMCCodeEmitter::
Jim Grosbach607efcb2010-11-11 01:09:40 +0000984getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
985 SmallVectorImpl<MCFixup> &Fixups) const {
986 // {13} 1 == imm8, 0 == Rm
987 // {12-9} Rn
988 // {8} isAdd
989 // {7-4} imm7_4/zero
990 // {3-0} imm3_0/Rm
991 const MCOperand &MO = MI.getOperand(OpIdx);
992 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
993 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
Jim Grosbach8648c102011-12-19 23:06:24 +0000994
995 // If The first operand isn't a register, we have a label reference.
996 if (!MO.isReg()) {
997 unsigned Rn = getARMRegisterNumbering(ARM::PC); // Rn is PC.
998
999 assert(MO.isExpr() && "Unexpected machine operand type!");
1000 const MCExpr *Expr = MO.getExpr();
1001 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10_unscaled);
1002 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
1003
1004 ++MCNumCPRelocations;
1005 return (Rn << 9) | (1 << 13);
1006 }
Jim Grosbach607efcb2010-11-11 01:09:40 +00001007 unsigned Rn = getARMRegisterNumbering(MO.getReg());
1008 unsigned Imm = MO2.getImm();
1009 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1010 bool isImm = MO1.getReg() == 0;
1011 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1012 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1013 if (!isImm)
1014 Imm8 = getARMRegisterNumbering(MO1.getReg());
1015 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
1016}
1017
Bill Wendling8a6449c2010-12-08 01:57:09 +00001018/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbach49bcd6f2010-12-07 21:50:47 +00001019uint32_t ARMMCCodeEmitter::
1020getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
1021 SmallVectorImpl<MCFixup> &Fixups) const {
1022 // [SP, #imm]
1023 // {7-0} = imm8
Jim Grosbach49bcd6f2010-12-07 21:50:47 +00001024 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendling8a6449c2010-12-08 01:57:09 +00001025 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
1026 "Unexpected base register!");
Bill Wendling7d3bde92010-12-15 23:32:27 +00001027
Jim Grosbach49bcd6f2010-12-07 21:50:47 +00001028 // The immediate is already shifted for the implicit zeroes, so no change
1029 // here.
1030 return MO1.getImm() & 0xff;
1031}
1032
Bill Wendling092a7bd2010-12-14 03:36:38 +00001033/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling0c4838b2010-12-09 21:49:07 +00001034uint32_t ARMMCCodeEmitter::
Bill Wendling092a7bd2010-12-14 03:36:38 +00001035getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling03e75762010-12-15 08:51:02 +00001036 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling811c9362010-11-30 07:44:32 +00001037 // [Rn, #imm]
1038 // {7-3} = imm5
1039 // {2-0} = Rn
1040 const MCOperand &MO = MI.getOperand(OpIdx);
1041 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendling811c9362010-11-30 07:44:32 +00001042 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Matt Beaumont-Gaye9afc742010-12-16 01:34:26 +00001043 unsigned Imm5 = MO1.getImm();
Bill Wendling0c4838b2010-12-09 21:49:07 +00001044 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendlinga9e3df72010-11-30 22:57:21 +00001045}
1046
Bill Wendling8a6449c2010-12-08 01:57:09 +00001047/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
1048uint32_t ARMMCCodeEmitter::
1049getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
1050 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond16fb432011-08-30 22:10:03 +00001051 const MCOperand MO = MI.getOperand(OpIdx);
1052 if (MO.isExpr())
1053 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
1054 return (MO.getImm() >> 2);
Bill Wendling8a6449c2010-12-08 01:57:09 +00001055}
1056
Jim Grosbach30eb6c72010-12-01 21:09:40 +00001057/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001058uint32_t ARMMCCodeEmitter::
1059getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
1060 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinge84eb992010-11-03 01:49:29 +00001061 // {12-9} = reg
1062 // {8} = (U)nsigned (add == '1', sub == '0')
1063 // {7-0} = imm8
1064 unsigned Reg, Imm8;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001065 bool isAdd;
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001066 // If The first operand isn't a register, we have a label reference.
1067 const MCOperand &MO = MI.getOperand(OpIdx);
1068 if (!MO.isReg()) {
Jim Grosbach90987142010-11-09 01:37:15 +00001069 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001070 Imm8 = 0;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001071 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001072
1073 assert(MO.isExpr() && "Unexpected machine operand type!");
1074 const MCExpr *Expr = MO.getExpr();
Owen Anderson0f7142d2010-12-08 00:18:36 +00001075 MCFixupKind Kind;
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001076 if (isThumb2())
Owen Anderson0f7142d2010-12-08 00:18:36 +00001077 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
1078 else
1079 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001080 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
1081
1082 ++MCNumCPRelocations;
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001083 } else {
Jim Grosbach0fb841f2010-11-04 01:12:30 +00001084 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001085 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
1086 }
Bill Wendlinge84eb992010-11-03 01:49:29 +00001087
Bill Wendlinge84eb992010-11-03 01:49:29 +00001088 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
1089 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach2d3e5c12010-11-30 22:40:36 +00001090 if (isAdd)
Bill Wendlinge84eb992010-11-03 01:49:29 +00001091 Binary |= (1 << 8);
1092 Binary |= (Reg << 9);
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001093 return Binary;
1094}
1095
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001096unsigned ARMMCCodeEmitter::
Owen Anderson04912702011-07-21 23:38:37 +00001097getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001098 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001099 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
Owen Anderson7c965e72011-07-28 17:56:55 +00001100 // shifted. The second is Rs, the amount to shift by, and the third specifies
1101 // the type of the shift.
Jim Grosbach49b0c452010-11-03 22:03:20 +00001102 //
Jim Grosbachefd53692010-10-12 23:53:58 +00001103 // {3-0} = Rm.
Owen Anderson7c965e72011-07-28 17:56:55 +00001104 // {4} = 1
Jim Grosbachefd53692010-10-12 23:53:58 +00001105 // {6-5} = type
Owen Anderson7c965e72011-07-28 17:56:55 +00001106 // {11-8} = Rs
1107 // {7} = 0
Jim Grosbachefd53692010-10-12 23:53:58 +00001108
1109 const MCOperand &MO = MI.getOperand(OpIdx);
1110 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1111 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
1112 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
1113
1114 // Encode Rm.
1115 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1116
1117 // Encode the shift opcode.
1118 unsigned SBits = 0;
1119 unsigned Rs = MO1.getReg();
1120 if (Rs) {
1121 // Set shift operand (bit[7:4]).
1122 // LSL - 0001
1123 // LSR - 0011
1124 // ASR - 0101
1125 // ROR - 0111
Jim Grosbachefd53692010-10-12 23:53:58 +00001126 switch (SOpc) {
1127 default: llvm_unreachable("Unknown shift opc!");
1128 case ARM_AM::lsl: SBits = 0x1; break;
1129 case ARM_AM::lsr: SBits = 0x3; break;
1130 case ARM_AM::asr: SBits = 0x5; break;
1131 case ARM_AM::ror: SBits = 0x7; break;
Jim Grosbachefd53692010-10-12 23:53:58 +00001132 }
1133 }
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001134
Jim Grosbachefd53692010-10-12 23:53:58 +00001135 Binary |= SBits << 4;
Jim Grosbachefd53692010-10-12 23:53:58 +00001136
Owen Anderson7c965e72011-07-28 17:56:55 +00001137 // Encode the shift operation Rs.
Owen Anderson04912702011-07-21 23:38:37 +00001138 // Encode Rs bit[11:8].
1139 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1140 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
1141}
1142
1143unsigned ARMMCCodeEmitter::
1144getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
1145 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson7c965e72011-07-28 17:56:55 +00001146 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1147 // shifted. The second is the amount to shift by.
Owen Anderson04912702011-07-21 23:38:37 +00001148 //
1149 // {3-0} = Rm.
Owen Anderson7c965e72011-07-28 17:56:55 +00001150 // {4} = 0
Owen Anderson04912702011-07-21 23:38:37 +00001151 // {6-5} = type
Owen Anderson7c965e72011-07-28 17:56:55 +00001152 // {11-7} = imm
Owen Anderson04912702011-07-21 23:38:37 +00001153
1154 const MCOperand &MO = MI.getOperand(OpIdx);
1155 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1156 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1157
1158 // Encode Rm.
1159 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1160
1161 // Encode the shift opcode.
1162 unsigned SBits = 0;
1163
1164 // Set shift operand (bit[6:4]).
1165 // LSL - 000
1166 // LSR - 010
1167 // ASR - 100
1168 // ROR - 110
1169 // RRX - 110 and bit[11:8] clear.
1170 switch (SOpc) {
1171 default: llvm_unreachable("Unknown shift opc!");
1172 case ARM_AM::lsl: SBits = 0x0; break;
1173 case ARM_AM::lsr: SBits = 0x2; break;
1174 case ARM_AM::asr: SBits = 0x4; break;
1175 case ARM_AM::ror: SBits = 0x6; break;
1176 case ARM_AM::rrx:
1177 Binary |= 0x60;
1178 return Binary;
Jim Grosbachefd53692010-10-12 23:53:58 +00001179 }
1180
1181 // Encode shift_imm bit[11:7].
Owen Anderson04912702011-07-21 23:38:37 +00001182 Binary |= SBits << 4;
Owen Andersone33c95d2011-08-11 18:41:59 +00001183 unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm());
1184 assert(Offset && "Offset must be in range 1-32!");
1185 if (Offset == 32) Offset = 0;
1186 return Binary | (Offset << 7);
Jim Grosbachefd53692010-10-12 23:53:58 +00001187}
1188
Owen Anderson04912702011-07-21 23:38:37 +00001189
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001190unsigned ARMMCCodeEmitter::
Owen Anderson50d662b2010-11-29 22:44:32 +00001191getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1192 SmallVectorImpl<MCFixup> &Fixups) const {
1193 const MCOperand &MO1 = MI.getOperand(OpNum);
1194 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbachc4a0c292010-12-10 21:57:34 +00001195 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1196
Owen Anderson50d662b2010-11-29 22:44:32 +00001197 // Encoded as [Rn, Rm, imm].
1198 // FIXME: Needs fixup support.
1199 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1200 Value <<= 4;
1201 Value |= getARMRegisterNumbering(MO2.getReg());
1202 Value <<= 2;
1203 Value |= MO3.getImm();
Jim Grosbachc4a0c292010-12-10 21:57:34 +00001204
Owen Anderson50d662b2010-11-29 22:44:32 +00001205 return Value;
1206}
1207
1208unsigned ARMMCCodeEmitter::
1209getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1210 SmallVectorImpl<MCFixup> &Fixups) const {
1211 const MCOperand &MO1 = MI.getOperand(OpNum);
1212 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1213
1214 // FIXME: Needs fixup support.
1215 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbachc4a0c292010-12-10 21:57:34 +00001216
Owen Anderson50d662b2010-11-29 22:44:32 +00001217 // Even though the immediate is 8 bits long, we need 9 bits in order
1218 // to represent the (inverse of the) sign bit.
1219 Value <<= 9;
Owen Andersone22c7322010-11-30 00:14:31 +00001220 int32_t tmp = (int32_t)MO2.getImm();
1221 if (tmp < 0)
1222 tmp = abs(tmp);
1223 else
1224 Value |= 256; // Set the ADD bit
1225 Value |= tmp & 255;
1226 return Value;
1227}
1228
1229unsigned ARMMCCodeEmitter::
1230getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1231 SmallVectorImpl<MCFixup> &Fixups) const {
1232 const MCOperand &MO1 = MI.getOperand(OpNum);
1233
1234 // FIXME: Needs fixup support.
1235 unsigned Value = 0;
1236 int32_t tmp = (int32_t)MO1.getImm();
1237 if (tmp < 0)
1238 tmp = abs(tmp);
1239 else
1240 Value |= 256; // Set the ADD bit
1241 Value |= tmp & 255;
Owen Anderson50d662b2010-11-29 22:44:32 +00001242 return Value;
1243}
1244
1245unsigned ARMMCCodeEmitter::
Owen Anderson299382e2010-11-30 19:19:31 +00001246getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1247 SmallVectorImpl<MCFixup> &Fixups) const {
1248 const MCOperand &MO1 = MI.getOperand(OpNum);
1249
1250 // FIXME: Needs fixup support.
1251 unsigned Value = 0;
1252 int32_t tmp = (int32_t)MO1.getImm();
1253 if (tmp < 0)
1254 tmp = abs(tmp);
1255 else
1256 Value |= 4096; // Set the ADD bit
1257 Value |= tmp & 4095;
1258 return Value;
1259}
1260
1261unsigned ARMMCCodeEmitter::
Owen Anderson8fdd1722010-11-12 21:12:40 +00001262getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1263 SmallVectorImpl<MCFixup> &Fixups) const {
1264 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1265 // shifted. The second is the amount to shift by.
1266 //
1267 // {3-0} = Rm.
1268 // {4} = 0
1269 // {6-5} = type
1270 // {11-7} = imm
1271
1272 const MCOperand &MO = MI.getOperand(OpIdx);
1273 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1274 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1275
1276 // Encode Rm.
1277 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1278
1279 // Encode the shift opcode.
1280 unsigned SBits = 0;
1281 // Set shift operand (bit[6:4]).
1282 // LSL - 000
1283 // LSR - 010
1284 // ASR - 100
1285 // ROR - 110
1286 switch (SOpc) {
1287 default: llvm_unreachable("Unknown shift opc!");
1288 case ARM_AM::lsl: SBits = 0x0; break;
1289 case ARM_AM::lsr: SBits = 0x2; break;
1290 case ARM_AM::asr: SBits = 0x4; break;
Owen Andersonc3c60a02011-09-13 17:34:32 +00001291 case ARM_AM::rrx: // FALLTHROUGH
Owen Anderson8fdd1722010-11-12 21:12:40 +00001292 case ARM_AM::ror: SBits = 0x6; break;
1293 }
1294
1295 Binary |= SBits << 4;
1296 if (SOpc == ARM_AM::rrx)
1297 return Binary;
1298
1299 // Encode shift_imm bit[11:7].
1300 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1301}
1302
1303unsigned ARMMCCodeEmitter::
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001304getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1305 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach5edb03e2010-10-21 22:03:21 +00001306 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1307 // msb of the mask.
1308 const MCOperand &MO = MI.getOperand(Op);
1309 uint32_t v = ~MO.getImm();
1310 uint32_t lsb = CountTrailingZeros_32(v);
1311 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1312 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1313 return lsb | (msb << 5);
1314}
1315
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001316unsigned ARMMCCodeEmitter::
1317getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling1b83ed52010-11-09 00:30:18 +00001318 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling345b48f2010-11-17 00:45:23 +00001319 // VLDM/VSTM:
1320 // {12-8} = Vd
1321 // {7-0} = Number of registers
1322 //
1323 // LDM/STM:
1324 // {15-0} = Bitfield of GPRs.
1325 unsigned Reg = MI.getOperand(Op).getReg();
Evan Chengad5f4852011-07-23 00:00:19 +00001326 bool SPRRegs = llvm::ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1327 bool DPRRegs = llvm::ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
Bill Wendling345b48f2010-11-17 00:45:23 +00001328
Bill Wendling1b83ed52010-11-09 00:30:18 +00001329 unsigned Binary = 0;
Bill Wendling345b48f2010-11-17 00:45:23 +00001330
1331 if (SPRRegs || DPRRegs) {
1332 // VLDM/VSTM
1333 unsigned RegNo = getARMRegisterNumbering(Reg);
1334 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1335 Binary |= (RegNo & 0x1f) << 8;
1336 if (SPRRegs)
1337 Binary |= NumRegs;
1338 else
1339 Binary |= NumRegs * 2;
1340 } else {
1341 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1342 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1343 Binary |= 1 << RegNo;
1344 }
Bill Wendling1b83ed52010-11-09 00:30:18 +00001345 }
Bill Wendling345b48f2010-11-17 00:45:23 +00001346
Jim Grosbach74ef9e12010-10-30 00:37:59 +00001347 return Binary;
1348}
1349
Bob Wilson318ce7c2010-11-30 00:00:42 +00001350/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1351/// with the alignment operand.
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001352unsigned ARMMCCodeEmitter::
1353getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1354 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonad402342010-11-02 00:05:05 +00001355 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001356 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach49b0c452010-11-03 22:03:20 +00001357
Owen Andersonad402342010-11-02 00:05:05 +00001358 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001359 unsigned Align = 0;
1360
1361 switch (Imm.getImm()) {
1362 default: break;
1363 case 2:
1364 case 4:
1365 case 8: Align = 0x01; break;
1366 case 16: Align = 0x02; break;
1367 case 32: Align = 0x03; break;
Owen Andersonad402342010-11-02 00:05:05 +00001368 }
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001369
Owen Andersonad402342010-11-02 00:05:05 +00001370 return RegNo | (Align << 4);
1371}
1372
Mon P Wang92ff16b2011-05-09 17:47:27 +00001373/// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1374/// along with the alignment operand for use in VST1 and VLD1 with size 32.
1375unsigned ARMMCCodeEmitter::
1376getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1377 SmallVectorImpl<MCFixup> &Fixups) const {
1378 const MCOperand &Reg = MI.getOperand(Op);
1379 const MCOperand &Imm = MI.getOperand(Op + 1);
1380
1381 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1382 unsigned Align = 0;
1383
1384 switch (Imm.getImm()) {
1385 default: break;
Mon P Wang92ff16b2011-05-09 17:47:27 +00001386 case 8:
Jim Grosbachcef98cd2011-12-19 18:31:43 +00001387 case 16:
1388 case 32: // Default '0' value for invalid alignments of 8, 16, 32 bytes.
1389 case 2: Align = 0x00; break;
1390 case 4: Align = 0x03; break;
Mon P Wang92ff16b2011-05-09 17:47:27 +00001391 }
1392
1393 return RegNo | (Align << 4);
1394}
1395
1396
Bob Wilson318ce7c2010-11-30 00:00:42 +00001397/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1398/// alignment operand for use in VLD-dup instructions. This is the same as
1399/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1400/// different for VLD4-dup.
1401unsigned ARMMCCodeEmitter::
1402getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1403 SmallVectorImpl<MCFixup> &Fixups) const {
1404 const MCOperand &Reg = MI.getOperand(Op);
1405 const MCOperand &Imm = MI.getOperand(Op + 1);
1406
1407 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1408 unsigned Align = 0;
1409
1410 switch (Imm.getImm()) {
1411 default: break;
1412 case 2:
1413 case 4:
1414 case 8: Align = 0x01; break;
1415 case 16: Align = 0x03; break;
1416 }
1417
1418 return RegNo | (Align << 4);
1419}
1420
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001421unsigned ARMMCCodeEmitter::
1422getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1423 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingf9eebb52010-11-02 22:53:11 +00001424 const MCOperand &MO = MI.getOperand(Op);
1425 if (MO.getReg() == 0) return 0x0D;
Jim Grosbach81c90032011-12-02 22:01:25 +00001426 return getARMRegisterNumbering(MO.getReg());
Owen Anderson526ffd52010-11-02 01:24:55 +00001427}
1428
Bill Wendling3b1459b2011-03-01 01:00:59 +00001429unsigned ARMMCCodeEmitter::
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001430getShiftRight8Imm(const MCInst &MI, unsigned Op,
1431 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling3b1459b2011-03-01 01:00:59 +00001432 return 8 - MI.getOperand(Op).getImm();
1433}
1434
1435unsigned ARMMCCodeEmitter::
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001436getShiftRight16Imm(const MCInst &MI, unsigned Op,
1437 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling3b1459b2011-03-01 01:00:59 +00001438 return 16 - MI.getOperand(Op).getImm();
1439}
1440
1441unsigned ARMMCCodeEmitter::
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001442getShiftRight32Imm(const MCInst &MI, unsigned Op,
1443 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling3b1459b2011-03-01 01:00:59 +00001444 return 32 - MI.getOperand(Op).getImm();
1445}
1446
Bill Wendling77ad1dc2011-03-07 23:38:41 +00001447unsigned ARMMCCodeEmitter::
1448getShiftRight64Imm(const MCInst &MI, unsigned Op,
1449 SmallVectorImpl<MCFixup> &Fixups) const {
1450 return 64 - MI.getOperand(Op).getImm();
1451}
1452
Jim Grosbach1287f4f2010-09-17 18:46:17 +00001453void ARMMCCodeEmitter::
1454EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001455 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach91029092010-10-07 22:12:50 +00001456 // Pseudo instructions don't get encoded.
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001457 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Jim Grosbach20b6fd72010-11-11 23:41:09 +00001458 uint64_t TSFlags = Desc.TSFlags;
1459 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbach91029092010-10-07 22:12:50 +00001460 return;
Owen Anderson651b2302011-07-13 23:22:26 +00001461
Jim Grosbach20b6fd72010-11-11 23:41:09 +00001462 int Size;
Owen Anderson651b2302011-07-13 23:22:26 +00001463 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1464 Size = Desc.getSize();
1465 else
1466 llvm_unreachable("Unexpected instruction size!");
Owen Anderson1732c2e2011-08-30 21:58:18 +00001467
Jim Grosbach567ebd0c2010-12-03 22:31:40 +00001468 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
Evan Cheng965b3c72011-01-13 07:58:56 +00001469 // Thumb 32-bit wide instructions need to emit the high order halfword
1470 // first.
Evan Chengc5e6d2f2011-07-11 03:57:24 +00001471 if (isThumb() && Size == 4) {
Jim Grosbach567ebd0c2010-12-03 22:31:40 +00001472 EmitConstant(Binary >> 16, 2, OS);
1473 EmitConstant(Binary & 0xffff, 2, OS);
1474 } else
1475 EmitConstant(Binary, Size, OS);
Bill Wendling91da9ab2010-11-02 22:44:12 +00001476 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach1287f4f2010-09-17 18:46:17 +00001477}
Jim Grosbach8aed3862010-10-07 21:57:55 +00001478
Jim Grosbach2eed7a12010-11-03 23:52:49 +00001479#include "ARMGenMCCodeEmitter.inc"