blob: f6b8af87b546689b93a7b521e1562f89c6493dc8 [file] [log] [blame]
Jim Grosbach568eeed2010-09-17 18:46:17 +00001//===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ARMMCCodeEmitter class.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2ac19022010-11-15 05:19:05 +000014#define DEBUG_TYPE "mccodeemitter"
Evan Chengee04a6d2011-07-20 23:34:39 +000015#include "MCTargetDesc/ARMAddressingModes.h"
Evan Chengbe740292011-07-23 00:00:19 +000016#include "MCTargetDesc/ARMBaseInfo.h"
17#include "MCTargetDesc/ARMFixupKinds.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000018#include "MCTargetDesc/ARMMCExpr.h"
Evan Chengbe740292011-07-23 00:00:19 +000019#include "MCTargetDesc/ARMMCTargetDesc.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000020#include "llvm/MC/MCCodeEmitter.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000023#include "llvm/MC/MCInstrInfo.h"
Evan Chengbe740292011-07-23 00:00:19 +000024#include "llvm/MC/MCRegisterInfo.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000025#include "llvm/MC/MCSubtargetInfo.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000026#include "llvm/ADT/APFloat.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000027#include "llvm/ADT/Statistic.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000028#include "llvm/Support/raw_ostream.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000029
Jim Grosbach568eeed2010-09-17 18:46:17 +000030using namespace llvm;
31
Jim Grosbach70933262010-11-04 01:12:30 +000032STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
33STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
Jim Grosbachd6d4b422010-10-07 22:12:50 +000034
Jim Grosbach568eeed2010-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 Cheng59ee62d2011-07-11 03:57:24 +000039 const MCInstrInfo &MCII;
40 const MCSubtargetInfo &STI;
Jim Grosbach568eeed2010-09-17 18:46:17 +000041
42public:
Evan Cheng59ee62d2011-07-11 03:57:24 +000043 ARMMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
44 MCContext &ctx)
Evan Chengaf0a2e62011-07-11 21:24:15 +000045 : MCII(mcii), STI(sti) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000046 }
47
48 ~ARMMCCodeEmitter() {}
49
Evan Cheng59ee62d2011-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 Grosbach0de6ab32010-10-12 17:11:26 +000063 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
64
Jim Grosbach9af82ba2010-10-07 21:57:55 +000065 // getBinaryCodeForInstr - TableGen'erated function for getting the
66 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000067 unsigned getBinaryCodeForInstr(const MCInst &MI,
68 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-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 Grosbach806e80e2010-11-03 23:52:49 +000072 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
73 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000074
Evan Cheng75972122011-01-13 07:58:56 +000075 /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
Owen Anderson971b83b2011-02-08 22:39:40 +000076 /// the specified operand. This is used for operands with :lower16: and
Evan Cheng75972122011-01-13 07:58:56 +000077 /// :upper16: prefixes.
78 uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
79 SmallVectorImpl<MCFixup> &Fixups) const;
Jason W Kim837caa92010-11-18 23:37:15 +000080
Bill Wendling92b5a2e2010-11-03 01:49:29 +000081 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000082 unsigned &Reg, unsigned &Imm,
83 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000084
Jim Grosbach662a8162010-12-06 23:57:07 +000085 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +000086 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +000087 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
88 SmallVectorImpl<MCFixup> &Fixups) const;
89
Bill Wendling09aa3f02010-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 Grosbache2467172010-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 Grosbach01086452010-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 Grosbach027d6e82010-12-09 19:04:53 +0000103 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
104 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000105 SmallVectorImpl<MCFixup> &Fixups) const;
106
Jim Grosbachc466b932010-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 Andersonc2666002010-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 Anderson10096db2011-08-30 21:58:18 +0000116
Jason W Kim685c3502011-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 Andersonf1eab592011-08-26 23:32:08 +0000121 uint32_t getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
122 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersonc2666002010-12-13 19:31:11 +0000123
Jim Grosbach5d14f9b2010-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 Grosbachd40963c2010-12-14 22:28:03 +0000128 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersona838a252010-12-14 00:36:49 +0000130 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
131 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson971b83b2011-02-08 22:39:40 +0000132
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000133
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000134 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
135 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000136 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
137 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000138
Bill Wendlingf4caf692010-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 Anderson0f4b60d2010-12-10 22:11:13 +0000142
Owen Anderson9d63d902010-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;
147
148
Jim Grosbach54fea632010-11-09 17:20:53 +0000149 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
150 /// operand as needed by load/store instructions.
151 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
152 SmallVectorImpl<MCFixup> &Fixups) const;
153
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000154 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
155 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
156 SmallVectorImpl<MCFixup> &Fixups) const {
157 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
158 switch (Mode) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000159 default: assert(0 && "Unknown addressing sub-mode!");
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000160 case ARM_AM::da: return 0;
161 case ARM_AM::ia: return 1;
162 case ARM_AM::db: return 2;
163 case ARM_AM::ib: return 3;
164 }
165 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000166 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
167 ///
168 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
169 switch (ShOpc) {
170 default: llvm_unreachable("Unknown shift opc!");
171 case ARM_AM::no_shift:
172 case ARM_AM::lsl: return 0;
173 case ARM_AM::lsr: return 1;
174 case ARM_AM::asr: return 2;
175 case ARM_AM::ror:
176 case ARM_AM::rrx: return 3;
177 }
178 return 0;
179 }
180
181 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
182 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
183 SmallVectorImpl<MCFixup> &Fixups) const;
184
185 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
186 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
187 SmallVectorImpl<MCFixup> &Fixups) const;
188
Jim Grosbach7ce05792011-08-03 23:50:40 +0000189 /// getPostIdxRegOpValue - Return encoding for postidx_reg operands.
190 uint32_t getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
191 SmallVectorImpl<MCFixup> &Fixups) const;
192
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000193 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
194 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
195 SmallVectorImpl<MCFixup> &Fixups) const;
196
Jim Grosbach570a9222010-11-11 01:09:40 +0000197 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
198 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
199 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000200
Jim Grosbachd967cd02010-12-07 21:50:47 +0000201 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
202 /// operand.
203 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
204 SmallVectorImpl<MCFixup> &Fixups) const;
205
Bill Wendlingf4caf692010-12-14 03:36:38 +0000206 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
207 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000208 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000209
Bill Wendlingb8958b02010-12-08 01:57:09 +0000210 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
211 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
212 SmallVectorImpl<MCFixup> &Fixups) const;
213
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000214 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000215 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
216 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000217
Jim Grosbach08bd5492010-10-12 23:00:24 +0000218 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000219 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
220 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000221 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
222 // '1' respectively.
223 return MI.getOperand(Op).getReg() == ARM::CPSR;
224 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000225
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000226 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000227 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
228 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000229 unsigned SoImm = MI.getOperand(Op).getImm();
230 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
231 assert(SoImmVal != -1 && "Not a valid so_imm value!");
232
233 // Encode rotate_imm.
234 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
235 << ARMII::SoRotImmShift;
236
237 // Encode immed_8.
238 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
239 return Binary;
240 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000241
Owen Anderson5de6d842010-11-12 21:12:40 +0000242 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
243 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
244 SmallVectorImpl<MCFixup> &Fixups) const {
245 unsigned SoImm = MI.getOperand(Op).getImm();
246 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
247 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
248 return Encoded;
249 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000250
Owen Anderson75579f72010-11-29 22:44:32 +0000251 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
252 SmallVectorImpl<MCFixup> &Fixups) const;
253 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
254 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000255 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
256 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000257 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
258 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000259
Jim Grosbachef324d72010-10-12 23:53:58 +0000260 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Owen Anderson152d4a42011-07-21 23:38:37 +0000261 unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op,
262 SmallVectorImpl<MCFixup> &Fixups) const;
263 unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000264 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000265 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
266 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000267
Jim Grosbach806e80e2010-11-03 23:52:49 +0000268 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
269 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000270 return 64 - MI.getOperand(Op).getImm();
271 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000272
Jim Grosbach806e80e2010-11-03 23:52:49 +0000273 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
274 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000275
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +0000276 unsigned getMsbOpValue(const MCInst &MI, unsigned Op,
277 SmallVectorImpl<MCFixup> &Fixups) const;
278
Jim Grosbach806e80e2010-11-03 23:52:49 +0000279 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
280 SmallVectorImpl<MCFixup> &Fixups) const;
281 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
282 SmallVectorImpl<MCFixup> &Fixups) const;
Mon P Wang183c6272011-05-09 17:47:27 +0000283 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
284 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000285 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
286 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000287 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
288 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000289
Bill Wendling3116dce2011-03-07 23:38:41 +0000290 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
291 SmallVectorImpl<MCFixup> &Fixups) const;
292 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
293 SmallVectorImpl<MCFixup> &Fixups) const;
294 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
295 SmallVectorImpl<MCFixup> &Fixups) const;
296 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
297 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendlinga656b632011-03-01 01:00:59 +0000298
Owen Anderson6d746312011-08-08 20:42:17 +0000299 unsigned getThumbSRImmOpValue(const MCInst &MI, unsigned Op,
300 SmallVectorImpl<MCFixup> &Fixups) const;
301
Owen Andersonc7139a62010-11-11 19:07:48 +0000302 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
303 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000304 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000305 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000306 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000307 unsigned EncodedValue) const;
308
309 unsigned VFPThumb2PostEncoder(const MCInst &MI,
310 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000311
Jim Grosbach70933262010-11-04 01:12:30 +0000312 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000313 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000314 }
315
Jim Grosbach70933262010-11-04 01:12:30 +0000316 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000317 // Output the constant in little endian byte order.
318 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000319 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000320 Val >>= 8;
321 }
322 }
323
Jim Grosbach568eeed2010-09-17 18:46:17 +0000324 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
325 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000326};
327
328} // end anonymous namespace
329
Evan Cheng59ee62d2011-07-11 03:57:24 +0000330MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
331 const MCSubtargetInfo &STI,
Bill Wendling0800ce72010-11-02 22:53:11 +0000332 MCContext &Ctx) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000333 return new ARMMCCodeEmitter(MCII, STI, Ctx);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000334}
335
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000336/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
337/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000338/// Thumb2 mode.
339unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
340 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000341 if (isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000342 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000343 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
344 // set to 1111.
345 unsigned Bit24 = EncodedValue & 0x01000000;
346 unsigned Bit28 = Bit24 << 4;
347 EncodedValue &= 0xEFFFFFFF;
348 EncodedValue |= Bit28;
349 EncodedValue |= 0x0F000000;
350 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000351
Owen Andersonc7139a62010-11-11 19:07:48 +0000352 return EncodedValue;
353}
354
Owen Anderson57dac882010-11-11 21:36:43 +0000355/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000356/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000357/// Thumb2 mode.
358unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
359 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000360 if (isThumb2()) {
Owen Anderson57dac882010-11-11 21:36:43 +0000361 EncodedValue &= 0xF0FFFFFF;
362 EncodedValue |= 0x09000000;
363 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000364
Owen Anderson57dac882010-11-11 21:36:43 +0000365 return EncodedValue;
366}
367
Owen Anderson8f143912010-11-11 23:12:55 +0000368/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000369/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000370/// Thumb2 mode.
371unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
372 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000373 if (isThumb2()) {
Owen Anderson8f143912010-11-11 23:12:55 +0000374 EncodedValue &= 0x00FFFFFF;
375 EncodedValue |= 0xEE000000;
376 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000377
Owen Anderson8f143912010-11-11 23:12:55 +0000378 return EncodedValue;
379}
380
Bill Wendlingcf590262010-12-01 21:54:50 +0000381/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
382/// them to their Thumb2 form if we are currently in Thumb2 mode.
383unsigned ARMMCCodeEmitter::
384VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000385 if (isThumb2()) {
Bill Wendlingcf590262010-12-01 21:54:50 +0000386 EncodedValue &= 0x0FFFFFFF;
387 EncodedValue |= 0xE0000000;
388 }
389 return EncodedValue;
390}
Owen Anderson57dac882010-11-11 21:36:43 +0000391
Jim Grosbach56ac9072010-10-08 21:45:55 +0000392/// getMachineOpValue - Return binary encoding of operand. If the machine
393/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000394unsigned ARMMCCodeEmitter::
395getMachineOpValue(const MCInst &MI, const MCOperand &MO,
396 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000397 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000398 unsigned Reg = MO.getReg();
399 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000400
Jim Grosbachb0708d22010-11-30 23:51:41 +0000401 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000402 switch (Reg) {
403 default:
404 return RegNo;
405 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
406 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
407 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
408 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
409 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000410 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000411 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000412 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000413 } else if (MO.isFPImm()) {
414 return static_cast<unsigned>(APFloat(MO.getFPImm())
415 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000416 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000417
Jim Grosbach817c1a62010-11-19 00:27:09 +0000418 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000419 return 0;
420}
421
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000422/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000423bool ARMMCCodeEmitter::
424EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
425 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000426 const MCOperand &MO = MI.getOperand(OpIdx);
427 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000428
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000429 Reg = getARMRegisterNumbering(MO.getReg());
430
431 int32_t SImm = MO1.getImm();
432 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000433
Jim Grosbachab682a22010-10-28 18:34:10 +0000434 // Special value for #-0
Owen Anderson0da10cf2011-08-29 19:36:44 +0000435 if (SImm == INT32_MIN) {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000436 SImm = 0;
Owen Anderson0da10cf2011-08-29 19:36:44 +0000437 isAdd = false;
438 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000439
Jim Grosbachab682a22010-10-28 18:34:10 +0000440 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000441 if (SImm < 0) {
442 SImm = -SImm;
443 isAdd = false;
444 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000445
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000446 Imm = SImm;
447 return isAdd;
448}
449
Bill Wendlingdff2f712010-12-08 23:01:43 +0000450/// getBranchTargetOpValue - Helper function to get the branch target operand,
451/// which is either an immediate or requires a fixup.
452static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
453 unsigned FixupKind,
454 SmallVectorImpl<MCFixup> &Fixups) {
455 const MCOperand &MO = MI.getOperand(OpIdx);
456
457 // If the destination is an immediate, we have nothing to do.
458 if (MO.isImm()) return MO.getImm();
459 assert(MO.isExpr() && "Unexpected branch target type!");
460 const MCExpr *Expr = MO.getExpr();
461 MCFixupKind Kind = MCFixupKind(FixupKind);
462 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
463
464 // All of the information is in the fixup.
465 return 0;
466}
467
Owen Anderson559c2772011-08-31 18:30:20 +0000468// Thumb BL and BLX use a strange offset encoding where bits 22 and 21 are
469// determined by negating them and XOR'ing them with bit 23.
470static int32_t encodeThumbBLOffset(int32_t offset) {
471 offset >>= 1;
472 uint32_t S = (offset & 0x800000) >> 23;
473 uint32_t J1 = (offset & 0x400000) >> 22;
474 uint32_t J2 = (offset & 0x200000) >> 21;
475 J1 = (~J1 & 0x1);
476 J2 = (~J2 & 0x1);
477 J1 ^= S;
478 J2 ^= S;
479
480 offset &= ~0x600000;
481 offset |= J1 << 22;
482 offset |= J2 << 21;
483
484 return offset;
485}
486
Bill Wendlingdff2f712010-12-08 23:01:43 +0000487/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000488uint32_t ARMMCCodeEmitter::
489getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
490 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson559c2772011-08-31 18:30:20 +0000491 const MCOperand MO = MI.getOperand(OpIdx);
492 if (MO.isExpr())
493 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl,
494 Fixups);
495 return encodeThumbBLOffset(MO.getImm());
Jim Grosbach662a8162010-12-06 23:57:07 +0000496}
497
Bill Wendling09aa3f02010-12-09 00:39:08 +0000498/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
499/// BLX branch target.
500uint32_t ARMMCCodeEmitter::
501getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
502 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson559c2772011-08-31 18:30:20 +0000503 const MCOperand MO = MI.getOperand(OpIdx);
504 if (MO.isExpr())
505 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx,
506 Fixups);
507 return encodeThumbBLOffset(MO.getImm());
Bill Wendling09aa3f02010-12-09 00:39:08 +0000508}
509
Jim Grosbache2467172010-12-10 18:21:33 +0000510/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
511uint32_t ARMMCCodeEmitter::
512getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
513 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson391ac652011-08-30 22:03:20 +0000514 const MCOperand MO = MI.getOperand(OpIdx);
515 if (MO.isExpr())
Owen Anderson559c2772011-08-31 18:30:20 +0000516 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br,
517 Fixups);
Owen Anderson391ac652011-08-30 22:03:20 +0000518 return (MO.getImm() >> 1);
Jim Grosbache2467172010-12-10 18:21:33 +0000519}
520
Jim Grosbach01086452010-12-10 17:13:40 +0000521/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
522uint32_t ARMMCCodeEmitter::
523getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000524 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000525 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
526}
527
Jim Grosbach027d6e82010-12-09 19:04:53 +0000528/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000529uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000530getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000531 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson21df36c2011-08-30 22:15:17 +0000532 const MCOperand MO = MI.getOperand(OpIdx);
533 if (MO.isExpr())
534 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
535 return (MO.getImm() >> 1);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000536}
537
Jason W Kim685c3502011-02-04 19:47:15 +0000538/// Return true if this branch has a non-always predication
539static bool HasConditionalBranch(const MCInst &MI) {
540 int NumOp = MI.getNumOperands();
541 if (NumOp >= 2) {
542 for (int i = 0; i < NumOp-1; ++i) {
543 const MCOperand &MCOp1 = MI.getOperand(i);
544 const MCOperand &MCOp2 = MI.getOperand(i + 1);
Owen Anderson10096db2011-08-30 21:58:18 +0000545 if (MCOp1.isImm() && MCOp2.isReg() &&
Jason W Kim685c3502011-02-04 19:47:15 +0000546 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
Owen Anderson10096db2011-08-30 21:58:18 +0000547 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
Jason W Kim685c3502011-02-04 19:47:15 +0000548 return true;
549 }
550 }
551 }
552 return false;
553}
554
Bill Wendlingdff2f712010-12-08 23:01:43 +0000555/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
556/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000557uint32_t ARMMCCodeEmitter::
558getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000559 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000560 // FIXME: This really, really shouldn't use TargetMachine. We don't want
561 // coupling between MC and TM anywhere we can help it.
Evan Cheng59ee62d2011-07-11 03:57:24 +0000562 if (isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000563 return
564 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Jason W Kim685c3502011-02-04 19:47:15 +0000565 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000566}
567
Jason W Kim685c3502011-02-04 19:47:15 +0000568/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
569/// target.
570uint32_t ARMMCCodeEmitter::
571getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
572 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond7568e12011-08-26 22:54:51 +0000573 const MCOperand MO = MI.getOperand(OpIdx);
574 if (MO.isExpr()) {
Owen Anderson10096db2011-08-30 21:58:18 +0000575 if (HasConditionalBranch(MI))
Owen Andersond7568e12011-08-26 22:54:51 +0000576 return ::getBranchTargetOpValue(MI, OpIdx,
577 ARM::fixup_arm_condbranch, Fixups);
Owen Anderson10096db2011-08-30 21:58:18 +0000578 return ::getBranchTargetOpValue(MI, OpIdx,
Owen Andersond7568e12011-08-26 22:54:51 +0000579 ARM::fixup_arm_uncondbranch, Fixups);
580 }
581
582 return MO.getImm() >> 2;
Jason W Kim685c3502011-02-04 19:47:15 +0000583}
584
Owen Andersonf1eab592011-08-26 23:32:08 +0000585uint32_t ARMMCCodeEmitter::
586getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
587 SmallVectorImpl<MCFixup> &Fixups) const {
588 const MCOperand MO = MI.getOperand(OpIdx);
589 if (MO.isExpr()) {
Owen Anderson10096db2011-08-30 21:58:18 +0000590 if (HasConditionalBranch(MI))
Owen Andersonf1eab592011-08-26 23:32:08 +0000591 return ::getBranchTargetOpValue(MI, OpIdx,
592 ARM::fixup_arm_condbranch, Fixups);
Owen Anderson10096db2011-08-30 21:58:18 +0000593 return ::getBranchTargetOpValue(MI, OpIdx,
Owen Andersonf1eab592011-08-26 23:32:08 +0000594 ARM::fixup_arm_uncondbranch, Fixups);
595 }
Jason W Kim685c3502011-02-04 19:47:15 +0000596
Owen Andersonf1eab592011-08-26 23:32:08 +0000597 return MO.getImm() >> 1;
598}
Jason W Kim685c3502011-02-04 19:47:15 +0000599
Owen Andersonc2666002010-12-13 19:31:11 +0000600/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
601/// immediate branch target.
602uint32_t ARMMCCodeEmitter::
603getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
604 SmallVectorImpl<MCFixup> &Fixups) const {
605 unsigned Val =
606 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
607 bool I = (Val & 0x800000);
608 bool J1 = (Val & 0x400000);
609 bool J2 = (Val & 0x200000);
610 if (I ^ J1)
611 Val &= ~0x400000;
612 else
613 Val |= 0x400000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000614
Owen Andersonc2666002010-12-13 19:31:11 +0000615 if (I ^ J2)
616 Val &= ~0x200000;
617 else
618 Val |= 0x200000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000619
Owen Andersonc2666002010-12-13 19:31:11 +0000620 return Val;
621}
622
Bill Wendlingdff2f712010-12-08 23:01:43 +0000623/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
624/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000625uint32_t ARMMCCodeEmitter::
626getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
627 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson96425c82011-08-26 18:09:22 +0000628 const MCOperand MO = MI.getOperand(OpIdx);
629 if (MO.isExpr())
630 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
631 Fixups);
632 int32_t offset = MO.getImm();
633 uint32_t Val = 0x2000;
634 if (offset < 0) {
635 Val = 0x1000;
636 offset *= -1;
637 }
638 Val |= offset;
639 return Val;
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000640}
641
Owen Andersona838a252010-12-14 00:36:49 +0000642/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
643/// target.
644uint32_t ARMMCCodeEmitter::
645getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
646 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson96425c82011-08-26 18:09:22 +0000647 const MCOperand MO = MI.getOperand(OpIdx);
648 if (MO.isExpr())
649 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
650 Fixups);
651 return MO.getImm();
Owen Andersona838a252010-12-14 00:36:49 +0000652}
653
Jim Grosbachd40963c2010-12-14 22:28:03 +0000654/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
655/// target.
656uint32_t ARMMCCodeEmitter::
657getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
658 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson96425c82011-08-26 18:09:22 +0000659 const MCOperand MO = MI.getOperand(OpIdx);
660 if (MO.isExpr())
661 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
662 Fixups);
663 return MO.getImm();
Jim Grosbachd40963c2010-12-14 22:28:03 +0000664}
665
Bill Wendlingf4caf692010-12-14 03:36:38 +0000666/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
667/// operand.
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000668uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000669getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
670 SmallVectorImpl<MCFixup> &) const {
671 // [Rn, Rm]
672 // {5-3} = Rm
673 // {2-0} = Rn
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000674 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000675 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000676 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
677 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
678 return (Rm << 3) | Rn;
679}
680
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000681/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000682uint32_t ARMMCCodeEmitter::
683getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
684 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000685 // {17-13} = reg
686 // {12} = (U)nsigned (add == '1', sub == '0')
687 // {11-0} = imm12
688 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000689 bool isAdd = true;
690 // If The first operand isn't a register, we have a label reference.
691 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Anderson971b83b2011-02-08 22:39:40 +0000692 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000693 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000694 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000695 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000696
Owen Anderson971b83b2011-02-08 22:39:40 +0000697 assert(MO.isExpr() && "Unexpected machine operand type!");
698 const MCExpr *Expr = MO.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000699
Owen Andersond7b3f582010-12-09 01:51:07 +0000700 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000701 if (isThumb2())
Owen Andersond7b3f582010-12-09 01:51:07 +0000702 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
703 else
704 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000705 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
706
707 ++MCNumCPRelocations;
708 } else
709 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000710
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000711 uint32_t Binary = Imm12 & 0xfff;
712 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000713 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000714 Binary |= (1 << 12);
715 Binary |= (Reg << 13);
716 return Binary;
717}
718
Owen Anderson9d63d902010-12-01 19:18:46 +0000719/// getT2AddrModeImm8s4OpValue - Return encoding info for
720/// 'reg +/- imm8<<2' operand.
721uint32_t ARMMCCodeEmitter::
722getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
723 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000724 // {12-9} = reg
725 // {8} = (U)nsigned (add == '1', sub == '0')
726 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000727 unsigned Reg, Imm8;
728 bool isAdd = true;
729 // If The first operand isn't a register, we have a label reference.
730 const MCOperand &MO = MI.getOperand(OpIdx);
731 if (!MO.isReg()) {
732 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
733 Imm8 = 0;
734 isAdd = false ; // 'U' bit is set as part of the fixup.
735
736 assert(MO.isExpr() && "Unexpected machine operand type!");
737 const MCExpr *Expr = MO.getExpr();
738 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
739 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
740
741 ++MCNumCPRelocations;
742 } else
743 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
744
745 uint32_t Binary = (Imm8 >> 2) & 0xff;
746 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
747 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000748 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000749 Binary |= (Reg << 9);
750 return Binary;
751}
752
Jason W Kim86a97f22011-01-12 00:19:25 +0000753// FIXME: This routine assumes that a binary
754// expression will always result in a PCRel expression
755// In reality, its only true if one or more subexpressions
756// is itself a PCRel (i.e. "." in asm or some other pcrel construct)
757// but this is good enough for now.
758static bool EvaluateAsPCRel(const MCExpr *Expr) {
759 switch (Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000760 default: assert(0 && "Unexpected expression type");
Jason W Kim86a97f22011-01-12 00:19:25 +0000761 case MCExpr::SymbolRef: return false;
762 case MCExpr::Binary: return true;
Jason W Kim86a97f22011-01-12 00:19:25 +0000763 }
764}
765
Evan Cheng75972122011-01-13 07:58:56 +0000766uint32_t
767ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
768 SmallVectorImpl<MCFixup> &Fixups) const {
Jason W Kim837caa92010-11-18 23:37:15 +0000769 // {20-16} = imm{15-12}
770 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000771 const MCOperand &MO = MI.getOperand(OpIdx);
Evan Cheng75972122011-01-13 07:58:56 +0000772 if (MO.isImm())
773 // Hi / lo 16 bits already extracted during earlier passes.
Jason W Kim837caa92010-11-18 23:37:15 +0000774 return static_cast<unsigned>(MO.getImm());
Evan Cheng75972122011-01-13 07:58:56 +0000775
776 // Handle :upper16: and :lower16: assembly prefixes.
777 const MCExpr *E = MO.getExpr();
778 if (E->getKind() == MCExpr::Target) {
779 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
780 E = ARM16Expr->getSubExpr();
781
Jason W Kim837caa92010-11-18 23:37:15 +0000782 MCFixupKind Kind;
Evan Cheng75972122011-01-13 07:58:56 +0000783 switch (ARM16Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000784 default: assert(0 && "Unsupported ARMFixup");
Evan Cheng75972122011-01-13 07:58:56 +0000785 case ARMMCExpr::VK_ARM_HI16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000786 if (!isTargetDarwin() && EvaluateAsPCRel(E))
787 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000788 ? ARM::fixup_t2_movt_hi16_pcrel
789 : ARM::fixup_arm_movt_hi16_pcrel);
790 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000791 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000792 ? ARM::fixup_t2_movt_hi16
793 : ARM::fixup_arm_movt_hi16);
Jason W Kim837caa92010-11-18 23:37:15 +0000794 break;
Evan Cheng75972122011-01-13 07:58:56 +0000795 case ARMMCExpr::VK_ARM_LO16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000796 if (!isTargetDarwin() && EvaluateAsPCRel(E))
797 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000798 ? ARM::fixup_t2_movw_lo16_pcrel
799 : ARM::fixup_arm_movw_lo16_pcrel);
800 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000801 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000802 ? ARM::fixup_t2_movw_lo16
803 : ARM::fixup_arm_movw_lo16);
Jason W Kim837caa92010-11-18 23:37:15 +0000804 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000805 }
Evan Cheng75972122011-01-13 07:58:56 +0000806 Fixups.push_back(MCFixup::Create(0, E, Kind));
Jason W Kim837caa92010-11-18 23:37:15 +0000807 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000808 };
Evan Cheng75972122011-01-13 07:58:56 +0000809
Jim Grosbach817c1a62010-11-19 00:27:09 +0000810 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000811 return 0;
812}
813
814uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000815getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
816 SmallVectorImpl<MCFixup> &Fixups) const {
817 const MCOperand &MO = MI.getOperand(OpIdx);
818 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
819 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
820 unsigned Rn = getARMRegisterNumbering(MO.getReg());
821 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000822 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
823 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000824 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
825 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000826
827 // {16-13} = Rn
828 // {12} = isAdd
829 // {11-0} = shifter
830 // {3-0} = Rm
831 // {4} = 0
832 // {6-5} = type
833 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000834 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000835 Binary |= Rn << 13;
836 Binary |= SBits << 5;
837 Binary |= ShImm << 7;
838 if (isAdd)
839 Binary |= 1 << 12;
840 return Binary;
841}
842
Jim Grosbach570a9222010-11-11 01:09:40 +0000843uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000844getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
845 SmallVectorImpl<MCFixup> &Fixups) const {
846 // {17-14} Rn
847 // {13} 1 == imm12, 0 == Rm
848 // {12} isAdd
849 // {11-0} imm12/Rm
850 const MCOperand &MO = MI.getOperand(OpIdx);
851 unsigned Rn = getARMRegisterNumbering(MO.getReg());
852 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
853 Binary |= Rn << 14;
854 return Binary;
855}
856
857uint32_t ARMMCCodeEmitter::
858getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
859 SmallVectorImpl<MCFixup> &Fixups) const {
860 // {13} 1 == imm12, 0 == Rm
861 // {12} isAdd
862 // {11-0} imm12/Rm
863 const MCOperand &MO = MI.getOperand(OpIdx);
864 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
865 unsigned Imm = MO1.getImm();
866 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
867 bool isReg = MO.getReg() != 0;
868 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
869 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
870 if (isReg) {
871 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
872 Binary <<= 7; // Shift amount is bits [11:7]
873 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
874 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
875 }
876 return Binary | (isAdd << 12) | (isReg << 13);
877}
878
879uint32_t ARMMCCodeEmitter::
Jim Grosbach7ce05792011-08-03 23:50:40 +0000880getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
881 SmallVectorImpl<MCFixup> &Fixups) const {
882 // {4} isAdd
883 // {3-0} Rm
884 const MCOperand &MO = MI.getOperand(OpIdx);
885 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
Jim Grosbach16578b52011-08-05 16:11:38 +0000886 bool isAdd = MO1.getImm() != 0;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000887 return getARMRegisterNumbering(MO.getReg()) | (isAdd << 4);
888}
889
890uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000891getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
892 SmallVectorImpl<MCFixup> &Fixups) const {
893 // {9} 1 == imm8, 0 == Rm
894 // {8} isAdd
895 // {7-4} imm7_4/zero
896 // {3-0} imm3_0/Rm
897 const MCOperand &MO = MI.getOperand(OpIdx);
898 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
899 unsigned Imm = MO1.getImm();
900 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
901 bool isImm = MO.getReg() == 0;
902 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
903 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
904 if (!isImm)
905 Imm8 = getARMRegisterNumbering(MO.getReg());
906 return Imm8 | (isAdd << 8) | (isImm << 9);
907}
908
909uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000910getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
911 SmallVectorImpl<MCFixup> &Fixups) const {
912 // {13} 1 == imm8, 0 == Rm
913 // {12-9} Rn
914 // {8} isAdd
915 // {7-4} imm7_4/zero
916 // {3-0} imm3_0/Rm
917 const MCOperand &MO = MI.getOperand(OpIdx);
918 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
919 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
920 unsigned Rn = getARMRegisterNumbering(MO.getReg());
921 unsigned Imm = MO2.getImm();
922 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
923 bool isImm = MO1.getReg() == 0;
924 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
925 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
926 if (!isImm)
927 Imm8 = getARMRegisterNumbering(MO1.getReg());
928 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
929}
930
Bill Wendlingb8958b02010-12-08 01:57:09 +0000931/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000932uint32_t ARMMCCodeEmitter::
933getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
934 SmallVectorImpl<MCFixup> &Fixups) const {
935 // [SP, #imm]
936 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000937 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000938 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
939 "Unexpected base register!");
Bill Wendling7a905a82010-12-15 23:32:27 +0000940
Jim Grosbachd967cd02010-12-07 21:50:47 +0000941 // The immediate is already shifted for the implicit zeroes, so no change
942 // here.
943 return MO1.getImm() & 0xff;
944}
945
Bill Wendlingf4caf692010-12-14 03:36:38 +0000946/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000947uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000948getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000949 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000950 // [Rn, #imm]
951 // {7-3} = imm5
952 // {2-0} = Rn
953 const MCOperand &MO = MI.getOperand(OpIdx);
954 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000955 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Matt Beaumont-Gay656b3d22010-12-16 01:34:26 +0000956 unsigned Imm5 = MO1.getImm();
Bill Wendling272df512010-12-09 21:49:07 +0000957 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000958}
959
Bill Wendlingb8958b02010-12-08 01:57:09 +0000960/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
961uint32_t ARMMCCodeEmitter::
962getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
963 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersona7710ed2011-08-30 22:10:03 +0000964 const MCOperand MO = MI.getOperand(OpIdx);
965 if (MO.isExpr())
966 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
967 return (MO.getImm() >> 2);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000968}
969
Jim Grosbach5177f792010-12-01 21:09:40 +0000970/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000971uint32_t ARMMCCodeEmitter::
972getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
973 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000974 // {12-9} = reg
975 // {8} = (U)nsigned (add == '1', sub == '0')
976 // {7-0} = imm8
977 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000978 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000979 // If The first operand isn't a register, we have a label reference.
980 const MCOperand &MO = MI.getOperand(OpIdx);
981 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000982 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000983 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000984 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000985
986 assert(MO.isExpr() && "Unexpected machine operand type!");
987 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000988 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000989 if (isThumb2())
Owen Andersond8e351b2010-12-08 00:18:36 +0000990 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
991 else
992 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000993 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
994
995 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000996 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000997 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000998 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
999 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +00001000
Bill Wendling92b5a2e2010-11-03 01:49:29 +00001001 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
1002 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +00001003 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +00001004 Binary |= (1 << 8);
1005 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +00001006 return Binary;
1007}
1008
Jim Grosbach806e80e2010-11-03 23:52:49 +00001009unsigned ARMMCCodeEmitter::
Owen Anderson152d4a42011-07-21 23:38:37 +00001010getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001011 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001012 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
Owen Anderson354712c2011-07-28 17:56:55 +00001013 // shifted. The second is Rs, the amount to shift by, and the third specifies
1014 // the type of the shift.
Jim Grosbach35b2de02010-11-03 22:03:20 +00001015 //
Jim Grosbachef324d72010-10-12 23:53:58 +00001016 // {3-0} = Rm.
Owen Anderson354712c2011-07-28 17:56:55 +00001017 // {4} = 1
Jim Grosbachef324d72010-10-12 23:53:58 +00001018 // {6-5} = type
Owen Anderson354712c2011-07-28 17:56:55 +00001019 // {11-8} = Rs
1020 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +00001021
1022 const MCOperand &MO = MI.getOperand(OpIdx);
1023 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1024 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
1025 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
1026
1027 // Encode Rm.
1028 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1029
1030 // Encode the shift opcode.
1031 unsigned SBits = 0;
1032 unsigned Rs = MO1.getReg();
1033 if (Rs) {
1034 // Set shift operand (bit[7:4]).
1035 // LSL - 0001
1036 // LSR - 0011
1037 // ASR - 0101
1038 // ROR - 0111
Jim Grosbachef324d72010-10-12 23:53:58 +00001039 switch (SOpc) {
1040 default: llvm_unreachable("Unknown shift opc!");
1041 case ARM_AM::lsl: SBits = 0x1; break;
1042 case ARM_AM::lsr: SBits = 0x3; break;
1043 case ARM_AM::asr: SBits = 0x5; break;
1044 case ARM_AM::ror: SBits = 0x7; break;
Jim Grosbachef324d72010-10-12 23:53:58 +00001045 }
1046 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001047
Jim Grosbachef324d72010-10-12 23:53:58 +00001048 Binary |= SBits << 4;
Jim Grosbachef324d72010-10-12 23:53:58 +00001049
Owen Anderson354712c2011-07-28 17:56:55 +00001050 // Encode the shift operation Rs.
Owen Anderson152d4a42011-07-21 23:38:37 +00001051 // Encode Rs bit[11:8].
1052 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1053 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
1054}
1055
1056unsigned ARMMCCodeEmitter::
1057getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
1058 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson354712c2011-07-28 17:56:55 +00001059 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1060 // shifted. The second is the amount to shift by.
Owen Anderson152d4a42011-07-21 23:38:37 +00001061 //
1062 // {3-0} = Rm.
Owen Anderson354712c2011-07-28 17:56:55 +00001063 // {4} = 0
Owen Anderson152d4a42011-07-21 23:38:37 +00001064 // {6-5} = type
Owen Anderson354712c2011-07-28 17:56:55 +00001065 // {11-7} = imm
Owen Anderson152d4a42011-07-21 23:38:37 +00001066
1067 const MCOperand &MO = MI.getOperand(OpIdx);
1068 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1069 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1070
1071 // Encode Rm.
1072 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1073
1074 // Encode the shift opcode.
1075 unsigned SBits = 0;
1076
1077 // Set shift operand (bit[6:4]).
1078 // LSL - 000
1079 // LSR - 010
1080 // ASR - 100
1081 // ROR - 110
1082 // RRX - 110 and bit[11:8] clear.
1083 switch (SOpc) {
1084 default: llvm_unreachable("Unknown shift opc!");
1085 case ARM_AM::lsl: SBits = 0x0; break;
1086 case ARM_AM::lsr: SBits = 0x2; break;
1087 case ARM_AM::asr: SBits = 0x4; break;
1088 case ARM_AM::ror: SBits = 0x6; break;
1089 case ARM_AM::rrx:
1090 Binary |= 0x60;
1091 return Binary;
Jim Grosbachef324d72010-10-12 23:53:58 +00001092 }
1093
1094 // Encode shift_imm bit[11:7].
Owen Anderson152d4a42011-07-21 23:38:37 +00001095 Binary |= SBits << 4;
Owen Anderson3dac0be2011-08-11 18:41:59 +00001096 unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm());
1097 assert(Offset && "Offset must be in range 1-32!");
1098 if (Offset == 32) Offset = 0;
1099 return Binary | (Offset << 7);
Jim Grosbachef324d72010-10-12 23:53:58 +00001100}
1101
Owen Anderson152d4a42011-07-21 23:38:37 +00001102
Jim Grosbach806e80e2010-11-03 23:52:49 +00001103unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +00001104getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1105 SmallVectorImpl<MCFixup> &Fixups) const {
1106 const MCOperand &MO1 = MI.getOperand(OpNum);
1107 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001108 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1109
Owen Anderson75579f72010-11-29 22:44:32 +00001110 // Encoded as [Rn, Rm, imm].
1111 // FIXME: Needs fixup support.
1112 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1113 Value <<= 4;
1114 Value |= getARMRegisterNumbering(MO2.getReg());
1115 Value <<= 2;
1116 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001117
Owen Anderson75579f72010-11-29 22:44:32 +00001118 return Value;
1119}
1120
1121unsigned ARMMCCodeEmitter::
1122getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1123 SmallVectorImpl<MCFixup> &Fixups) const {
1124 const MCOperand &MO1 = MI.getOperand(OpNum);
1125 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1126
1127 // FIXME: Needs fixup support.
1128 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001129
Owen Anderson75579f72010-11-29 22:44:32 +00001130 // Even though the immediate is 8 bits long, we need 9 bits in order
1131 // to represent the (inverse of the) sign bit.
1132 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +00001133 int32_t tmp = (int32_t)MO2.getImm();
1134 if (tmp < 0)
1135 tmp = abs(tmp);
1136 else
1137 Value |= 256; // Set the ADD bit
1138 Value |= tmp & 255;
1139 return Value;
1140}
1141
1142unsigned ARMMCCodeEmitter::
1143getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1144 SmallVectorImpl<MCFixup> &Fixups) const {
1145 const MCOperand &MO1 = MI.getOperand(OpNum);
1146
1147 // FIXME: Needs fixup support.
1148 unsigned Value = 0;
1149 int32_t tmp = (int32_t)MO1.getImm();
1150 if (tmp < 0)
1151 tmp = abs(tmp);
1152 else
1153 Value |= 256; // Set the ADD bit
1154 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +00001155 return Value;
1156}
1157
1158unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +00001159getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1160 SmallVectorImpl<MCFixup> &Fixups) const {
1161 const MCOperand &MO1 = MI.getOperand(OpNum);
1162
1163 // FIXME: Needs fixup support.
1164 unsigned Value = 0;
1165 int32_t tmp = (int32_t)MO1.getImm();
1166 if (tmp < 0)
1167 tmp = abs(tmp);
1168 else
1169 Value |= 4096; // Set the ADD bit
1170 Value |= tmp & 4095;
1171 return Value;
1172}
1173
1174unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001175getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1176 SmallVectorImpl<MCFixup> &Fixups) const {
1177 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1178 // shifted. The second is the amount to shift by.
1179 //
1180 // {3-0} = Rm.
1181 // {4} = 0
1182 // {6-5} = type
1183 // {11-7} = imm
1184
1185 const MCOperand &MO = MI.getOperand(OpIdx);
1186 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1187 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1188
1189 // Encode Rm.
1190 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1191
1192 // Encode the shift opcode.
1193 unsigned SBits = 0;
1194 // Set shift operand (bit[6:4]).
1195 // LSL - 000
1196 // LSR - 010
1197 // ASR - 100
1198 // ROR - 110
1199 switch (SOpc) {
1200 default: llvm_unreachable("Unknown shift opc!");
1201 case ARM_AM::lsl: SBits = 0x0; break;
1202 case ARM_AM::lsr: SBits = 0x2; break;
1203 case ARM_AM::asr: SBits = 0x4; break;
1204 case ARM_AM::ror: SBits = 0x6; break;
1205 }
1206
1207 Binary |= SBits << 4;
1208 if (SOpc == ARM_AM::rrx)
1209 return Binary;
1210
1211 // Encode shift_imm bit[11:7].
1212 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1213}
1214
1215unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001216getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1217 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001218 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1219 // msb of the mask.
1220 const MCOperand &MO = MI.getOperand(Op);
1221 uint32_t v = ~MO.getImm();
1222 uint32_t lsb = CountTrailingZeros_32(v);
1223 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1224 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1225 return lsb | (msb << 5);
1226}
1227
Jim Grosbach806e80e2010-11-03 23:52:49 +00001228unsigned ARMMCCodeEmitter::
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +00001229getMsbOpValue(const MCInst &MI, unsigned Op,
1230 SmallVectorImpl<MCFixup> &Fixups) const {
1231 // MSB - 5 bits.
1232 uint32_t lsb = MI.getOperand(Op-1).getImm();
1233 uint32_t width = MI.getOperand(Op).getImm();
1234 uint32_t msb = lsb+width-1;
1235 assert (width != 0 && msb < 32 && "Illegal bit width!");
1236 return msb;
1237}
1238
1239unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001240getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001241 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001242 // VLDM/VSTM:
1243 // {12-8} = Vd
1244 // {7-0} = Number of registers
1245 //
1246 // LDM/STM:
1247 // {15-0} = Bitfield of GPRs.
1248 unsigned Reg = MI.getOperand(Op).getReg();
Evan Chengbe740292011-07-23 00:00:19 +00001249 bool SPRRegs = llvm::ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1250 bool DPRRegs = llvm::ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
Bill Wendling6bc105a2010-11-17 00:45:23 +00001251
Bill Wendling5e559a22010-11-09 00:30:18 +00001252 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001253
1254 if (SPRRegs || DPRRegs) {
1255 // VLDM/VSTM
1256 unsigned RegNo = getARMRegisterNumbering(Reg);
1257 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1258 Binary |= (RegNo & 0x1f) << 8;
1259 if (SPRRegs)
1260 Binary |= NumRegs;
1261 else
1262 Binary |= NumRegs * 2;
1263 } else {
1264 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1265 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1266 Binary |= 1 << RegNo;
1267 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001268 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001269
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001270 return Binary;
1271}
1272
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001273/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1274/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001275unsigned ARMMCCodeEmitter::
1276getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1277 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001278 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001279 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001280
Owen Andersond9aa7d32010-11-02 00:05:05 +00001281 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001282 unsigned Align = 0;
1283
1284 switch (Imm.getImm()) {
1285 default: break;
1286 case 2:
1287 case 4:
1288 case 8: Align = 0x01; break;
1289 case 16: Align = 0x02; break;
1290 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001291 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001292
Owen Andersond9aa7d32010-11-02 00:05:05 +00001293 return RegNo | (Align << 4);
1294}
1295
Mon P Wang183c6272011-05-09 17:47:27 +00001296/// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1297/// along with the alignment operand for use in VST1 and VLD1 with size 32.
1298unsigned ARMMCCodeEmitter::
1299getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1300 SmallVectorImpl<MCFixup> &Fixups) const {
1301 const MCOperand &Reg = MI.getOperand(Op);
1302 const MCOperand &Imm = MI.getOperand(Op + 1);
1303
1304 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1305 unsigned Align = 0;
1306
1307 switch (Imm.getImm()) {
1308 default: break;
1309 case 2:
1310 case 4:
1311 case 8:
1312 case 16: Align = 0x00; break;
1313 case 32: Align = 0x03; break;
1314 }
1315
1316 return RegNo | (Align << 4);
1317}
1318
1319
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001320/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1321/// alignment operand for use in VLD-dup instructions. This is the same as
1322/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1323/// different for VLD4-dup.
1324unsigned ARMMCCodeEmitter::
1325getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1326 SmallVectorImpl<MCFixup> &Fixups) const {
1327 const MCOperand &Reg = MI.getOperand(Op);
1328 const MCOperand &Imm = MI.getOperand(Op + 1);
1329
1330 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1331 unsigned Align = 0;
1332
1333 switch (Imm.getImm()) {
1334 default: break;
1335 case 2:
1336 case 4:
1337 case 8: Align = 0x01; break;
1338 case 16: Align = 0x03; break;
1339 }
1340
1341 return RegNo | (Align << 4);
1342}
1343
Jim Grosbach806e80e2010-11-03 23:52:49 +00001344unsigned ARMMCCodeEmitter::
1345getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1346 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001347 const MCOperand &MO = MI.getOperand(Op);
1348 if (MO.getReg() == 0) return 0x0D;
1349 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001350}
1351
Bill Wendlinga656b632011-03-01 01:00:59 +00001352unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001353getShiftRight8Imm(const MCInst &MI, unsigned Op,
1354 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001355 return 8 - MI.getOperand(Op).getImm();
1356}
1357
1358unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001359getShiftRight16Imm(const MCInst &MI, unsigned Op,
1360 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001361 return 16 - MI.getOperand(Op).getImm();
1362}
1363
1364unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001365getShiftRight32Imm(const MCInst &MI, unsigned Op,
1366 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001367 return 32 - MI.getOperand(Op).getImm();
1368}
1369
Bill Wendling3116dce2011-03-07 23:38:41 +00001370unsigned ARMMCCodeEmitter::
1371getShiftRight64Imm(const MCInst &MI, unsigned Op,
1372 SmallVectorImpl<MCFixup> &Fixups) const {
1373 return 64 - MI.getOperand(Op).getImm();
1374}
1375
Jim Grosbach568eeed2010-09-17 18:46:17 +00001376void ARMMCCodeEmitter::
1377EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001378 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001379 // Pseudo instructions don't get encoded.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001380 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001381 uint64_t TSFlags = Desc.TSFlags;
1382 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001383 return;
Owen Anderson16884412011-07-13 23:22:26 +00001384
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001385 int Size;
Owen Anderson16884412011-07-13 23:22:26 +00001386 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1387 Size = Desc.getSize();
1388 else
1389 llvm_unreachable("Unexpected instruction size!");
Owen Anderson10096db2011-08-30 21:58:18 +00001390
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001391 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
Evan Cheng75972122011-01-13 07:58:56 +00001392 // Thumb 32-bit wide instructions need to emit the high order halfword
1393 // first.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001394 if (isThumb() && Size == 4) {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001395 EmitConstant(Binary >> 16, 2, OS);
1396 EmitConstant(Binary & 0xffff, 2, OS);
1397 } else
1398 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001399 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001400}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001401
Jim Grosbach806e80e2010-11-03 23:52:49 +00001402#include "ARMGenMCCodeEmitter.inc"