blob: 434eb28c698b3de3072a8504224c100dfd7dcbee [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;
116
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 Andersonc2666002010-12-13 19:31:11 +0000121
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000122 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
123 /// ADR label target.
124 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
125 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachd40963c2010-12-14 22:28:03 +0000126 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
127 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersona838a252010-12-14 00:36:49 +0000128 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson971b83b2011-02-08 22:39:40 +0000130
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000131
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000132 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
133 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000134 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
135 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000136
Bill Wendlingf4caf692010-12-14 03:36:38 +0000137 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
138 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
139 SmallVectorImpl<MCFixup> &Fixups)const;
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000140
Owen Anderson9d63d902010-12-01 19:18:46 +0000141 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
142 /// operand.
143 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
144 SmallVectorImpl<MCFixup> &Fixups) const;
145
146
Jim Grosbach54fea632010-11-09 17:20:53 +0000147 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
148 /// operand as needed by load/store instructions.
149 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
150 SmallVectorImpl<MCFixup> &Fixups) const;
151
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000152 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
153 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
154 SmallVectorImpl<MCFixup> &Fixups) const {
155 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
156 switch (Mode) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000157 default: assert(0 && "Unknown addressing sub-mode!");
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000158 case ARM_AM::da: return 0;
159 case ARM_AM::ia: return 1;
160 case ARM_AM::db: return 2;
161 case ARM_AM::ib: return 3;
162 }
163 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000164 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
165 ///
166 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
167 switch (ShOpc) {
168 default: llvm_unreachable("Unknown shift opc!");
169 case ARM_AM::no_shift:
170 case ARM_AM::lsl: return 0;
171 case ARM_AM::lsr: return 1;
172 case ARM_AM::asr: return 2;
173 case ARM_AM::ror:
174 case ARM_AM::rrx: return 3;
175 }
176 return 0;
177 }
178
179 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
180 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
181 SmallVectorImpl<MCFixup> &Fixups) const;
182
183 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
184 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
185 SmallVectorImpl<MCFixup> &Fixups) const;
186
Jim Grosbach7ce05792011-08-03 23:50:40 +0000187 /// getPostIdxRegOpValue - Return encoding for postidx_reg operands.
188 uint32_t getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
189 SmallVectorImpl<MCFixup> &Fixups) const;
190
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000191 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
192 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
193 SmallVectorImpl<MCFixup> &Fixups) const;
194
Jim Grosbach570a9222010-11-11 01:09:40 +0000195 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
196 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
197 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000198
Jim Grosbachd967cd02010-12-07 21:50:47 +0000199 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
200 /// operand.
201 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
202 SmallVectorImpl<MCFixup> &Fixups) const;
203
Bill Wendlingf4caf692010-12-14 03:36:38 +0000204 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
205 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000206 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000207
Bill Wendlingb8958b02010-12-08 01:57:09 +0000208 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
209 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
210 SmallVectorImpl<MCFixup> &Fixups) const;
211
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000212 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000213 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
214 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000215
Jim Grosbach08bd5492010-10-12 23:00:24 +0000216 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000217 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
218 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000219 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
220 // '1' respectively.
221 return MI.getOperand(Op).getReg() == ARM::CPSR;
222 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000223
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000224 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000225 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
226 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000227 unsigned SoImm = MI.getOperand(Op).getImm();
228 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
229 assert(SoImmVal != -1 && "Not a valid so_imm value!");
230
231 // Encode rotate_imm.
232 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
233 << ARMII::SoRotImmShift;
234
235 // Encode immed_8.
236 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
237 return Binary;
238 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000239
Owen Anderson5de6d842010-11-12 21:12:40 +0000240 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
241 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
242 SmallVectorImpl<MCFixup> &Fixups) const {
243 unsigned SoImm = MI.getOperand(Op).getImm();
244 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
245 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
246 return Encoded;
247 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000248
Owen Anderson75579f72010-11-29 22:44:32 +0000249 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
250 SmallVectorImpl<MCFixup> &Fixups) const;
251 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
252 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000253 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
254 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000255 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
256 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000257
Jim Grosbachef324d72010-10-12 23:53:58 +0000258 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Owen Anderson152d4a42011-07-21 23:38:37 +0000259 unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op,
260 SmallVectorImpl<MCFixup> &Fixups) const;
261 unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000262 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000263 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
264 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000265
Jim Grosbach806e80e2010-11-03 23:52:49 +0000266 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
267 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000268 return 64 - MI.getOperand(Op).getImm();
269 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000270
Jim Grosbach806e80e2010-11-03 23:52:49 +0000271 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
272 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000273
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +0000274 unsigned getMsbOpValue(const MCInst &MI, unsigned Op,
275 SmallVectorImpl<MCFixup> &Fixups) const;
276
Jim Grosbach806e80e2010-11-03 23:52:49 +0000277 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
278 SmallVectorImpl<MCFixup> &Fixups) const;
279 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
280 SmallVectorImpl<MCFixup> &Fixups) const;
Mon P Wang183c6272011-05-09 17:47:27 +0000281 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
282 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000283 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
284 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000285 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
286 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000287
Bill Wendling3116dce2011-03-07 23:38:41 +0000288 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
289 SmallVectorImpl<MCFixup> &Fixups) const;
290 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
291 SmallVectorImpl<MCFixup> &Fixups) const;
292 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
293 SmallVectorImpl<MCFixup> &Fixups) const;
294 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
295 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendlinga656b632011-03-01 01:00:59 +0000296
Owen Anderson6d746312011-08-08 20:42:17 +0000297 unsigned getThumbSRImmOpValue(const MCInst &MI, unsigned Op,
298 SmallVectorImpl<MCFixup> &Fixups) const;
299
Owen Andersonc7139a62010-11-11 19:07:48 +0000300 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
301 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000302 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000303 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000304 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000305 unsigned EncodedValue) const;
306
307 unsigned VFPThumb2PostEncoder(const MCInst &MI,
308 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000309
Jim Grosbach70933262010-11-04 01:12:30 +0000310 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000311 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000312 }
313
Jim Grosbach70933262010-11-04 01:12:30 +0000314 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000315 // Output the constant in little endian byte order.
316 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000317 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000318 Val >>= 8;
319 }
320 }
321
Jim Grosbach568eeed2010-09-17 18:46:17 +0000322 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
323 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000324};
325
326} // end anonymous namespace
327
Evan Cheng59ee62d2011-07-11 03:57:24 +0000328MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
329 const MCSubtargetInfo &STI,
Bill Wendling0800ce72010-11-02 22:53:11 +0000330 MCContext &Ctx) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000331 return new ARMMCCodeEmitter(MCII, STI, Ctx);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000332}
333
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000334/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
335/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000336/// Thumb2 mode.
337unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
338 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000339 if (isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000340 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000341 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
342 // set to 1111.
343 unsigned Bit24 = EncodedValue & 0x01000000;
344 unsigned Bit28 = Bit24 << 4;
345 EncodedValue &= 0xEFFFFFFF;
346 EncodedValue |= Bit28;
347 EncodedValue |= 0x0F000000;
348 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000349
Owen Andersonc7139a62010-11-11 19:07:48 +0000350 return EncodedValue;
351}
352
Owen Anderson57dac882010-11-11 21:36:43 +0000353/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000354/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000355/// Thumb2 mode.
356unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
357 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000358 if (isThumb2()) {
Owen Anderson57dac882010-11-11 21:36:43 +0000359 EncodedValue &= 0xF0FFFFFF;
360 EncodedValue |= 0x09000000;
361 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000362
Owen Anderson57dac882010-11-11 21:36:43 +0000363 return EncodedValue;
364}
365
Owen Anderson8f143912010-11-11 23:12:55 +0000366/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000367/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000368/// Thumb2 mode.
369unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
370 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000371 if (isThumb2()) {
Owen Anderson8f143912010-11-11 23:12:55 +0000372 EncodedValue &= 0x00FFFFFF;
373 EncodedValue |= 0xEE000000;
374 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000375
Owen Anderson8f143912010-11-11 23:12:55 +0000376 return EncodedValue;
377}
378
Bill Wendlingcf590262010-12-01 21:54:50 +0000379/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
380/// them to their Thumb2 form if we are currently in Thumb2 mode.
381unsigned ARMMCCodeEmitter::
382VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000383 if (isThumb2()) {
Bill Wendlingcf590262010-12-01 21:54:50 +0000384 EncodedValue &= 0x0FFFFFFF;
385 EncodedValue |= 0xE0000000;
386 }
387 return EncodedValue;
388}
Owen Anderson57dac882010-11-11 21:36:43 +0000389
Jim Grosbach56ac9072010-10-08 21:45:55 +0000390/// getMachineOpValue - Return binary encoding of operand. If the machine
391/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000392unsigned ARMMCCodeEmitter::
393getMachineOpValue(const MCInst &MI, const MCOperand &MO,
394 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000395 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000396 unsigned Reg = MO.getReg();
397 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000398
Jim Grosbachb0708d22010-11-30 23:51:41 +0000399 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000400 switch (Reg) {
401 default:
402 return RegNo;
403 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
404 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
405 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
406 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
407 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000408 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000409 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000410 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000411 } else if (MO.isFPImm()) {
412 return static_cast<unsigned>(APFloat(MO.getFPImm())
413 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000414 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000415
Jim Grosbach817c1a62010-11-19 00:27:09 +0000416 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000417 return 0;
418}
419
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000420/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000421bool ARMMCCodeEmitter::
422EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
423 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000424 const MCOperand &MO = MI.getOperand(OpIdx);
425 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000426
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000427 Reg = getARMRegisterNumbering(MO.getReg());
428
429 int32_t SImm = MO1.getImm();
430 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000431
Jim Grosbachab682a22010-10-28 18:34:10 +0000432 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000433 if (SImm == INT32_MIN)
434 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000435
Jim Grosbachab682a22010-10-28 18:34:10 +0000436 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000437 if (SImm < 0) {
438 SImm = -SImm;
439 isAdd = false;
440 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000441
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000442 Imm = SImm;
443 return isAdd;
444}
445
Bill Wendlingdff2f712010-12-08 23:01:43 +0000446/// getBranchTargetOpValue - Helper function to get the branch target operand,
447/// which is either an immediate or requires a fixup.
448static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
449 unsigned FixupKind,
450 SmallVectorImpl<MCFixup> &Fixups) {
451 const MCOperand &MO = MI.getOperand(OpIdx);
452
453 // If the destination is an immediate, we have nothing to do.
454 if (MO.isImm()) return MO.getImm();
455 assert(MO.isExpr() && "Unexpected branch target type!");
456 const MCExpr *Expr = MO.getExpr();
457 MCFixupKind Kind = MCFixupKind(FixupKind);
458 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
459
460 // All of the information is in the fixup.
461 return 0;
462}
463
464/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000465uint32_t ARMMCCodeEmitter::
466getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
467 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000468 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000469}
470
Bill Wendling09aa3f02010-12-09 00:39:08 +0000471/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
472/// BLX branch target.
473uint32_t ARMMCCodeEmitter::
474getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
475 SmallVectorImpl<MCFixup> &Fixups) const {
476 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
477}
478
Jim Grosbache2467172010-12-10 18:21:33 +0000479/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
480uint32_t ARMMCCodeEmitter::
481getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
482 SmallVectorImpl<MCFixup> &Fixups) const {
483 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
484}
485
Jim Grosbach01086452010-12-10 17:13:40 +0000486/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
487uint32_t ARMMCCodeEmitter::
488getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000489 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000490 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
491}
492
Jim Grosbach027d6e82010-12-09 19:04:53 +0000493/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000494uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000495getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000496 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000497 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000498}
499
Jason W Kim685c3502011-02-04 19:47:15 +0000500/// Return true if this branch has a non-always predication
501static bool HasConditionalBranch(const MCInst &MI) {
502 int NumOp = MI.getNumOperands();
503 if (NumOp >= 2) {
504 for (int i = 0; i < NumOp-1; ++i) {
505 const MCOperand &MCOp1 = MI.getOperand(i);
506 const MCOperand &MCOp2 = MI.getOperand(i + 1);
507 if (MCOp1.isImm() && MCOp2.isReg() &&
508 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
509 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
510 return true;
511 }
512 }
513 }
514 return false;
515}
516
Bill Wendlingdff2f712010-12-08 23:01:43 +0000517/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
518/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000519uint32_t ARMMCCodeEmitter::
520getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000521 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000522 // FIXME: This really, really shouldn't use TargetMachine. We don't want
523 // coupling between MC and TM anywhere we can help it.
Evan Cheng59ee62d2011-07-11 03:57:24 +0000524 if (isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000525 return
526 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Jason W Kim685c3502011-02-04 19:47:15 +0000527 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000528}
529
Jason W Kim685c3502011-02-04 19:47:15 +0000530/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
531/// target.
532uint32_t ARMMCCodeEmitter::
533getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
534 SmallVectorImpl<MCFixup> &Fixups) const {
535 if (HasConditionalBranch(MI))
536 return ::getBranchTargetOpValue(MI, OpIdx,
537 ARM::fixup_arm_condbranch, Fixups);
538 return ::getBranchTargetOpValue(MI, OpIdx,
539 ARM::fixup_arm_uncondbranch, Fixups);
540}
541
542
543
544
Owen Andersonc2666002010-12-13 19:31:11 +0000545/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
546/// immediate branch target.
547uint32_t ARMMCCodeEmitter::
548getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
549 SmallVectorImpl<MCFixup> &Fixups) const {
550 unsigned Val =
551 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
552 bool I = (Val & 0x800000);
553 bool J1 = (Val & 0x400000);
554 bool J2 = (Val & 0x200000);
555 if (I ^ J1)
556 Val &= ~0x400000;
557 else
558 Val |= 0x400000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000559
Owen Andersonc2666002010-12-13 19:31:11 +0000560 if (I ^ J2)
561 Val &= ~0x200000;
562 else
563 Val |= 0x200000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000564
Owen Andersonc2666002010-12-13 19:31:11 +0000565 return Val;
566}
567
Bill Wendlingdff2f712010-12-08 23:01:43 +0000568/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
569/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000570uint32_t ARMMCCodeEmitter::
571getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
572 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson96425c82011-08-26 18:09:22 +0000573 const MCOperand MO = MI.getOperand(OpIdx);
574 if (MO.isExpr())
575 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
576 Fixups);
577 int32_t offset = MO.getImm();
578 uint32_t Val = 0x2000;
579 if (offset < 0) {
580 Val = 0x1000;
581 offset *= -1;
582 }
583 Val |= offset;
584 return Val;
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000585}
586
Owen Andersona838a252010-12-14 00:36:49 +0000587/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
588/// target.
589uint32_t ARMMCCodeEmitter::
590getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
591 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson96425c82011-08-26 18:09:22 +0000592 const MCOperand MO = MI.getOperand(OpIdx);
593 if (MO.isExpr())
594 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
595 Fixups);
596 return MO.getImm();
Owen Andersona838a252010-12-14 00:36:49 +0000597}
598
Jim Grosbachd40963c2010-12-14 22:28:03 +0000599/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
600/// target.
601uint32_t ARMMCCodeEmitter::
602getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
603 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson96425c82011-08-26 18:09:22 +0000604 const MCOperand MO = MI.getOperand(OpIdx);
605 if (MO.isExpr())
606 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
607 Fixups);
608 return MO.getImm();
Jim Grosbachd40963c2010-12-14 22:28:03 +0000609}
610
Bill Wendlingf4caf692010-12-14 03:36:38 +0000611/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
612/// operand.
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000613uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000614getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
615 SmallVectorImpl<MCFixup> &) const {
616 // [Rn, Rm]
617 // {5-3} = Rm
618 // {2-0} = Rn
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000619 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000620 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000621 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
622 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
623 return (Rm << 3) | Rn;
624}
625
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000626/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000627uint32_t ARMMCCodeEmitter::
628getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
629 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000630 // {17-13} = reg
631 // {12} = (U)nsigned (add == '1', sub == '0')
632 // {11-0} = imm12
633 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000634 bool isAdd = true;
635 // If The first operand isn't a register, we have a label reference.
636 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Anderson971b83b2011-02-08 22:39:40 +0000637 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000638 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000639 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000640 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000641
Owen Anderson971b83b2011-02-08 22:39:40 +0000642 assert(MO.isExpr() && "Unexpected machine operand type!");
643 const MCExpr *Expr = MO.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000644
Owen Andersond7b3f582010-12-09 01:51:07 +0000645 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000646 if (isThumb2())
Owen Andersond7b3f582010-12-09 01:51:07 +0000647 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
648 else
649 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000650 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
651
652 ++MCNumCPRelocations;
653 } else
654 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000655
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000656 uint32_t Binary = Imm12 & 0xfff;
657 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000658 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000659 Binary |= (1 << 12);
660 Binary |= (Reg << 13);
661 return Binary;
662}
663
Owen Anderson9d63d902010-12-01 19:18:46 +0000664/// getT2AddrModeImm8s4OpValue - Return encoding info for
665/// 'reg +/- imm8<<2' operand.
666uint32_t ARMMCCodeEmitter::
667getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
668 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000669 // {12-9} = reg
670 // {8} = (U)nsigned (add == '1', sub == '0')
671 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000672 unsigned Reg, Imm8;
673 bool isAdd = true;
674 // If The first operand isn't a register, we have a label reference.
675 const MCOperand &MO = MI.getOperand(OpIdx);
676 if (!MO.isReg()) {
677 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
678 Imm8 = 0;
679 isAdd = false ; // 'U' bit is set as part of the fixup.
680
681 assert(MO.isExpr() && "Unexpected machine operand type!");
682 const MCExpr *Expr = MO.getExpr();
683 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
684 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
685
686 ++MCNumCPRelocations;
687 } else
688 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
689
690 uint32_t Binary = (Imm8 >> 2) & 0xff;
691 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
692 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000693 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000694 Binary |= (Reg << 9);
695 return Binary;
696}
697
Jason W Kim86a97f22011-01-12 00:19:25 +0000698// FIXME: This routine assumes that a binary
699// expression will always result in a PCRel expression
700// In reality, its only true if one or more subexpressions
701// is itself a PCRel (i.e. "." in asm or some other pcrel construct)
702// but this is good enough for now.
703static bool EvaluateAsPCRel(const MCExpr *Expr) {
704 switch (Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000705 default: assert(0 && "Unexpected expression type");
Jason W Kim86a97f22011-01-12 00:19:25 +0000706 case MCExpr::SymbolRef: return false;
707 case MCExpr::Binary: return true;
Jason W Kim86a97f22011-01-12 00:19:25 +0000708 }
709}
710
Evan Cheng75972122011-01-13 07:58:56 +0000711uint32_t
712ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
713 SmallVectorImpl<MCFixup> &Fixups) const {
Jason W Kim837caa92010-11-18 23:37:15 +0000714 // {20-16} = imm{15-12}
715 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000716 const MCOperand &MO = MI.getOperand(OpIdx);
Evan Cheng75972122011-01-13 07:58:56 +0000717 if (MO.isImm())
718 // Hi / lo 16 bits already extracted during earlier passes.
Jason W Kim837caa92010-11-18 23:37:15 +0000719 return static_cast<unsigned>(MO.getImm());
Evan Cheng75972122011-01-13 07:58:56 +0000720
721 // Handle :upper16: and :lower16: assembly prefixes.
722 const MCExpr *E = MO.getExpr();
723 if (E->getKind() == MCExpr::Target) {
724 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
725 E = ARM16Expr->getSubExpr();
726
Jason W Kim837caa92010-11-18 23:37:15 +0000727 MCFixupKind Kind;
Evan Cheng75972122011-01-13 07:58:56 +0000728 switch (ARM16Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000729 default: assert(0 && "Unsupported ARMFixup");
Evan Cheng75972122011-01-13 07:58:56 +0000730 case ARMMCExpr::VK_ARM_HI16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000731 if (!isTargetDarwin() && EvaluateAsPCRel(E))
732 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000733 ? ARM::fixup_t2_movt_hi16_pcrel
734 : ARM::fixup_arm_movt_hi16_pcrel);
735 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000736 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000737 ? ARM::fixup_t2_movt_hi16
738 : ARM::fixup_arm_movt_hi16);
Jason W Kim837caa92010-11-18 23:37:15 +0000739 break;
Evan Cheng75972122011-01-13 07:58:56 +0000740 case ARMMCExpr::VK_ARM_LO16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000741 if (!isTargetDarwin() && EvaluateAsPCRel(E))
742 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000743 ? ARM::fixup_t2_movw_lo16_pcrel
744 : ARM::fixup_arm_movw_lo16_pcrel);
745 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000746 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000747 ? ARM::fixup_t2_movw_lo16
748 : ARM::fixup_arm_movw_lo16);
Jason W Kim837caa92010-11-18 23:37:15 +0000749 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000750 }
Evan Cheng75972122011-01-13 07:58:56 +0000751 Fixups.push_back(MCFixup::Create(0, E, Kind));
Jason W Kim837caa92010-11-18 23:37:15 +0000752 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000753 };
Evan Cheng75972122011-01-13 07:58:56 +0000754
Jim Grosbach817c1a62010-11-19 00:27:09 +0000755 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000756 return 0;
757}
758
759uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000760getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
761 SmallVectorImpl<MCFixup> &Fixups) const {
762 const MCOperand &MO = MI.getOperand(OpIdx);
763 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
764 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
765 unsigned Rn = getARMRegisterNumbering(MO.getReg());
766 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000767 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
768 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000769 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
770 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000771
772 // {16-13} = Rn
773 // {12} = isAdd
774 // {11-0} = shifter
775 // {3-0} = Rm
776 // {4} = 0
777 // {6-5} = type
778 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000779 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000780 Binary |= Rn << 13;
781 Binary |= SBits << 5;
782 Binary |= ShImm << 7;
783 if (isAdd)
784 Binary |= 1 << 12;
785 return Binary;
786}
787
Jim Grosbach570a9222010-11-11 01:09:40 +0000788uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000789getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
790 SmallVectorImpl<MCFixup> &Fixups) const {
791 // {17-14} Rn
792 // {13} 1 == imm12, 0 == Rm
793 // {12} isAdd
794 // {11-0} imm12/Rm
795 const MCOperand &MO = MI.getOperand(OpIdx);
796 unsigned Rn = getARMRegisterNumbering(MO.getReg());
797 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
798 Binary |= Rn << 14;
799 return Binary;
800}
801
802uint32_t ARMMCCodeEmitter::
803getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
804 SmallVectorImpl<MCFixup> &Fixups) const {
805 // {13} 1 == imm12, 0 == Rm
806 // {12} isAdd
807 // {11-0} imm12/Rm
808 const MCOperand &MO = MI.getOperand(OpIdx);
809 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
810 unsigned Imm = MO1.getImm();
811 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
812 bool isReg = MO.getReg() != 0;
813 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
814 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
815 if (isReg) {
816 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
817 Binary <<= 7; // Shift amount is bits [11:7]
818 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
819 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
820 }
821 return Binary | (isAdd << 12) | (isReg << 13);
822}
823
824uint32_t ARMMCCodeEmitter::
Jim Grosbach7ce05792011-08-03 23:50:40 +0000825getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
826 SmallVectorImpl<MCFixup> &Fixups) const {
827 // {4} isAdd
828 // {3-0} Rm
829 const MCOperand &MO = MI.getOperand(OpIdx);
830 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
Jim Grosbach16578b52011-08-05 16:11:38 +0000831 bool isAdd = MO1.getImm() != 0;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000832 return getARMRegisterNumbering(MO.getReg()) | (isAdd << 4);
833}
834
835uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000836getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
837 SmallVectorImpl<MCFixup> &Fixups) const {
838 // {9} 1 == imm8, 0 == Rm
839 // {8} isAdd
840 // {7-4} imm7_4/zero
841 // {3-0} imm3_0/Rm
842 const MCOperand &MO = MI.getOperand(OpIdx);
843 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
844 unsigned Imm = MO1.getImm();
845 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
846 bool isImm = MO.getReg() == 0;
847 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
848 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
849 if (!isImm)
850 Imm8 = getARMRegisterNumbering(MO.getReg());
851 return Imm8 | (isAdd << 8) | (isImm << 9);
852}
853
854uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000855getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
856 SmallVectorImpl<MCFixup> &Fixups) const {
857 // {13} 1 == imm8, 0 == Rm
858 // {12-9} Rn
859 // {8} isAdd
860 // {7-4} imm7_4/zero
861 // {3-0} imm3_0/Rm
862 const MCOperand &MO = MI.getOperand(OpIdx);
863 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
864 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
865 unsigned Rn = getARMRegisterNumbering(MO.getReg());
866 unsigned Imm = MO2.getImm();
867 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
868 bool isImm = MO1.getReg() == 0;
869 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
870 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
871 if (!isImm)
872 Imm8 = getARMRegisterNumbering(MO1.getReg());
873 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
874}
875
Bill Wendlingb8958b02010-12-08 01:57:09 +0000876/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000877uint32_t ARMMCCodeEmitter::
878getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
879 SmallVectorImpl<MCFixup> &Fixups) const {
880 // [SP, #imm]
881 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000882 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000883 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
884 "Unexpected base register!");
Bill Wendling7a905a82010-12-15 23:32:27 +0000885
Jim Grosbachd967cd02010-12-07 21:50:47 +0000886 // The immediate is already shifted for the implicit zeroes, so no change
887 // here.
888 return MO1.getImm() & 0xff;
889}
890
Bill Wendlingf4caf692010-12-14 03:36:38 +0000891/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000892uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000893getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000894 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000895 // [Rn, #imm]
896 // {7-3} = imm5
897 // {2-0} = Rn
898 const MCOperand &MO = MI.getOperand(OpIdx);
899 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000900 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Matt Beaumont-Gay656b3d22010-12-16 01:34:26 +0000901 unsigned Imm5 = MO1.getImm();
Bill Wendling272df512010-12-09 21:49:07 +0000902 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000903}
904
Bill Wendlingb8958b02010-12-08 01:57:09 +0000905/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
906uint32_t ARMMCCodeEmitter::
907getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
908 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000909 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000910}
911
Jim Grosbach5177f792010-12-01 21:09:40 +0000912/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000913uint32_t ARMMCCodeEmitter::
914getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
915 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000916 // {12-9} = reg
917 // {8} = (U)nsigned (add == '1', sub == '0')
918 // {7-0} = imm8
919 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000920 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000921 // If The first operand isn't a register, we have a label reference.
922 const MCOperand &MO = MI.getOperand(OpIdx);
923 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000924 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000925 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000926 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000927
928 assert(MO.isExpr() && "Unexpected machine operand type!");
929 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000930 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000931 if (isThumb2())
Owen Andersond8e351b2010-12-08 00:18:36 +0000932 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
933 else
934 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000935 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
936
937 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000938 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000939 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000940 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
941 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000942
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000943 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
944 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000945 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000946 Binary |= (1 << 8);
947 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000948 return Binary;
949}
950
Jim Grosbach806e80e2010-11-03 23:52:49 +0000951unsigned ARMMCCodeEmitter::
Owen Anderson152d4a42011-07-21 23:38:37 +0000952getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000953 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000954 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
Owen Anderson354712c2011-07-28 17:56:55 +0000955 // shifted. The second is Rs, the amount to shift by, and the third specifies
956 // the type of the shift.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000957 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000958 // {3-0} = Rm.
Owen Anderson354712c2011-07-28 17:56:55 +0000959 // {4} = 1
Jim Grosbachef324d72010-10-12 23:53:58 +0000960 // {6-5} = type
Owen Anderson354712c2011-07-28 17:56:55 +0000961 // {11-8} = Rs
962 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000963
964 const MCOperand &MO = MI.getOperand(OpIdx);
965 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
966 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
967 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
968
969 // Encode Rm.
970 unsigned Binary = getARMRegisterNumbering(MO.getReg());
971
972 // Encode the shift opcode.
973 unsigned SBits = 0;
974 unsigned Rs = MO1.getReg();
975 if (Rs) {
976 // Set shift operand (bit[7:4]).
977 // LSL - 0001
978 // LSR - 0011
979 // ASR - 0101
980 // ROR - 0111
Jim Grosbachef324d72010-10-12 23:53:58 +0000981 switch (SOpc) {
982 default: llvm_unreachable("Unknown shift opc!");
983 case ARM_AM::lsl: SBits = 0x1; break;
984 case ARM_AM::lsr: SBits = 0x3; break;
985 case ARM_AM::asr: SBits = 0x5; break;
986 case ARM_AM::ror: SBits = 0x7; break;
Jim Grosbachef324d72010-10-12 23:53:58 +0000987 }
988 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000989
Jim Grosbachef324d72010-10-12 23:53:58 +0000990 Binary |= SBits << 4;
Jim Grosbachef324d72010-10-12 23:53:58 +0000991
Owen Anderson354712c2011-07-28 17:56:55 +0000992 // Encode the shift operation Rs.
Owen Anderson152d4a42011-07-21 23:38:37 +0000993 // Encode Rs bit[11:8].
994 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
995 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
996}
997
998unsigned ARMMCCodeEmitter::
999getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
1000 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson354712c2011-07-28 17:56:55 +00001001 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1002 // shifted. The second is the amount to shift by.
Owen Anderson152d4a42011-07-21 23:38:37 +00001003 //
1004 // {3-0} = Rm.
Owen Anderson354712c2011-07-28 17:56:55 +00001005 // {4} = 0
Owen Anderson152d4a42011-07-21 23:38:37 +00001006 // {6-5} = type
Owen Anderson354712c2011-07-28 17:56:55 +00001007 // {11-7} = imm
Owen Anderson152d4a42011-07-21 23:38:37 +00001008
1009 const MCOperand &MO = MI.getOperand(OpIdx);
1010 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1011 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1012
1013 // Encode Rm.
1014 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1015
1016 // Encode the shift opcode.
1017 unsigned SBits = 0;
1018
1019 // Set shift operand (bit[6:4]).
1020 // LSL - 000
1021 // LSR - 010
1022 // ASR - 100
1023 // ROR - 110
1024 // RRX - 110 and bit[11:8] clear.
1025 switch (SOpc) {
1026 default: llvm_unreachable("Unknown shift opc!");
1027 case ARM_AM::lsl: SBits = 0x0; break;
1028 case ARM_AM::lsr: SBits = 0x2; break;
1029 case ARM_AM::asr: SBits = 0x4; break;
1030 case ARM_AM::ror: SBits = 0x6; break;
1031 case ARM_AM::rrx:
1032 Binary |= 0x60;
1033 return Binary;
Jim Grosbachef324d72010-10-12 23:53:58 +00001034 }
1035
1036 // Encode shift_imm bit[11:7].
Owen Anderson152d4a42011-07-21 23:38:37 +00001037 Binary |= SBits << 4;
Owen Anderson3dac0be2011-08-11 18:41:59 +00001038 unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm());
1039 assert(Offset && "Offset must be in range 1-32!");
1040 if (Offset == 32) Offset = 0;
1041 return Binary | (Offset << 7);
Jim Grosbachef324d72010-10-12 23:53:58 +00001042}
1043
Owen Anderson152d4a42011-07-21 23:38:37 +00001044
Jim Grosbach806e80e2010-11-03 23:52:49 +00001045unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +00001046getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1047 SmallVectorImpl<MCFixup> &Fixups) const {
1048 const MCOperand &MO1 = MI.getOperand(OpNum);
1049 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001050 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1051
Owen Anderson75579f72010-11-29 22:44:32 +00001052 // Encoded as [Rn, Rm, imm].
1053 // FIXME: Needs fixup support.
1054 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1055 Value <<= 4;
1056 Value |= getARMRegisterNumbering(MO2.getReg());
1057 Value <<= 2;
1058 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001059
Owen Anderson75579f72010-11-29 22:44:32 +00001060 return Value;
1061}
1062
1063unsigned ARMMCCodeEmitter::
1064getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1065 SmallVectorImpl<MCFixup> &Fixups) const {
1066 const MCOperand &MO1 = MI.getOperand(OpNum);
1067 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1068
1069 // FIXME: Needs fixup support.
1070 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001071
Owen Anderson75579f72010-11-29 22:44:32 +00001072 // Even though the immediate is 8 bits long, we need 9 bits in order
1073 // to represent the (inverse of the) sign bit.
1074 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +00001075 int32_t tmp = (int32_t)MO2.getImm();
1076 if (tmp < 0)
1077 tmp = abs(tmp);
1078 else
1079 Value |= 256; // Set the ADD bit
1080 Value |= tmp & 255;
1081 return Value;
1082}
1083
1084unsigned ARMMCCodeEmitter::
1085getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1086 SmallVectorImpl<MCFixup> &Fixups) const {
1087 const MCOperand &MO1 = MI.getOperand(OpNum);
1088
1089 // FIXME: Needs fixup support.
1090 unsigned Value = 0;
1091 int32_t tmp = (int32_t)MO1.getImm();
1092 if (tmp < 0)
1093 tmp = abs(tmp);
1094 else
1095 Value |= 256; // Set the ADD bit
1096 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +00001097 return Value;
1098}
1099
1100unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +00001101getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1102 SmallVectorImpl<MCFixup> &Fixups) const {
1103 const MCOperand &MO1 = MI.getOperand(OpNum);
1104
1105 // FIXME: Needs fixup support.
1106 unsigned Value = 0;
1107 int32_t tmp = (int32_t)MO1.getImm();
1108 if (tmp < 0)
1109 tmp = abs(tmp);
1110 else
1111 Value |= 4096; // Set the ADD bit
1112 Value |= tmp & 4095;
1113 return Value;
1114}
1115
1116unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001117getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1118 SmallVectorImpl<MCFixup> &Fixups) const {
1119 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1120 // shifted. The second is the amount to shift by.
1121 //
1122 // {3-0} = Rm.
1123 // {4} = 0
1124 // {6-5} = type
1125 // {11-7} = imm
1126
1127 const MCOperand &MO = MI.getOperand(OpIdx);
1128 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1129 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1130
1131 // Encode Rm.
1132 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1133
1134 // Encode the shift opcode.
1135 unsigned SBits = 0;
1136 // Set shift operand (bit[6:4]).
1137 // LSL - 000
1138 // LSR - 010
1139 // ASR - 100
1140 // ROR - 110
1141 switch (SOpc) {
1142 default: llvm_unreachable("Unknown shift opc!");
1143 case ARM_AM::lsl: SBits = 0x0; break;
1144 case ARM_AM::lsr: SBits = 0x2; break;
1145 case ARM_AM::asr: SBits = 0x4; break;
1146 case ARM_AM::ror: SBits = 0x6; break;
1147 }
1148
1149 Binary |= SBits << 4;
1150 if (SOpc == ARM_AM::rrx)
1151 return Binary;
1152
1153 // Encode shift_imm bit[11:7].
1154 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1155}
1156
1157unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001158getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1159 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001160 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1161 // msb of the mask.
1162 const MCOperand &MO = MI.getOperand(Op);
1163 uint32_t v = ~MO.getImm();
1164 uint32_t lsb = CountTrailingZeros_32(v);
1165 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1166 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1167 return lsb | (msb << 5);
1168}
1169
Jim Grosbach806e80e2010-11-03 23:52:49 +00001170unsigned ARMMCCodeEmitter::
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +00001171getMsbOpValue(const MCInst &MI, unsigned Op,
1172 SmallVectorImpl<MCFixup> &Fixups) const {
1173 // MSB - 5 bits.
1174 uint32_t lsb = MI.getOperand(Op-1).getImm();
1175 uint32_t width = MI.getOperand(Op).getImm();
1176 uint32_t msb = lsb+width-1;
1177 assert (width != 0 && msb < 32 && "Illegal bit width!");
1178 return msb;
1179}
1180
1181unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001182getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001183 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001184 // VLDM/VSTM:
1185 // {12-8} = Vd
1186 // {7-0} = Number of registers
1187 //
1188 // LDM/STM:
1189 // {15-0} = Bitfield of GPRs.
1190 unsigned Reg = MI.getOperand(Op).getReg();
Evan Chengbe740292011-07-23 00:00:19 +00001191 bool SPRRegs = llvm::ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1192 bool DPRRegs = llvm::ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
Bill Wendling6bc105a2010-11-17 00:45:23 +00001193
Bill Wendling5e559a22010-11-09 00:30:18 +00001194 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001195
1196 if (SPRRegs || DPRRegs) {
1197 // VLDM/VSTM
1198 unsigned RegNo = getARMRegisterNumbering(Reg);
1199 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1200 Binary |= (RegNo & 0x1f) << 8;
1201 if (SPRRegs)
1202 Binary |= NumRegs;
1203 else
1204 Binary |= NumRegs * 2;
1205 } else {
1206 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1207 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1208 Binary |= 1 << RegNo;
1209 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001210 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001211
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001212 return Binary;
1213}
1214
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001215/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1216/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001217unsigned ARMMCCodeEmitter::
1218getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1219 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001220 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001221 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001222
Owen Andersond9aa7d32010-11-02 00:05:05 +00001223 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001224 unsigned Align = 0;
1225
1226 switch (Imm.getImm()) {
1227 default: break;
1228 case 2:
1229 case 4:
1230 case 8: Align = 0x01; break;
1231 case 16: Align = 0x02; break;
1232 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001233 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001234
Owen Andersond9aa7d32010-11-02 00:05:05 +00001235 return RegNo | (Align << 4);
1236}
1237
Mon P Wang183c6272011-05-09 17:47:27 +00001238/// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1239/// along with the alignment operand for use in VST1 and VLD1 with size 32.
1240unsigned ARMMCCodeEmitter::
1241getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1242 SmallVectorImpl<MCFixup> &Fixups) const {
1243 const MCOperand &Reg = MI.getOperand(Op);
1244 const MCOperand &Imm = MI.getOperand(Op + 1);
1245
1246 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1247 unsigned Align = 0;
1248
1249 switch (Imm.getImm()) {
1250 default: break;
1251 case 2:
1252 case 4:
1253 case 8:
1254 case 16: Align = 0x00; break;
1255 case 32: Align = 0x03; break;
1256 }
1257
1258 return RegNo | (Align << 4);
1259}
1260
1261
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001262/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1263/// alignment operand for use in VLD-dup instructions. This is the same as
1264/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1265/// different for VLD4-dup.
1266unsigned ARMMCCodeEmitter::
1267getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1268 SmallVectorImpl<MCFixup> &Fixups) const {
1269 const MCOperand &Reg = MI.getOperand(Op);
1270 const MCOperand &Imm = MI.getOperand(Op + 1);
1271
1272 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1273 unsigned Align = 0;
1274
1275 switch (Imm.getImm()) {
1276 default: break;
1277 case 2:
1278 case 4:
1279 case 8: Align = 0x01; break;
1280 case 16: Align = 0x03; break;
1281 }
1282
1283 return RegNo | (Align << 4);
1284}
1285
Jim Grosbach806e80e2010-11-03 23:52:49 +00001286unsigned ARMMCCodeEmitter::
1287getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1288 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001289 const MCOperand &MO = MI.getOperand(Op);
1290 if (MO.getReg() == 0) return 0x0D;
1291 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001292}
1293
Bill Wendlinga656b632011-03-01 01:00:59 +00001294unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001295getShiftRight8Imm(const MCInst &MI, unsigned Op,
1296 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001297 return 8 - MI.getOperand(Op).getImm();
1298}
1299
1300unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001301getShiftRight16Imm(const MCInst &MI, unsigned Op,
1302 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001303 return 16 - MI.getOperand(Op).getImm();
1304}
1305
1306unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001307getShiftRight32Imm(const MCInst &MI, unsigned Op,
1308 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001309 return 32 - MI.getOperand(Op).getImm();
1310}
1311
Bill Wendling3116dce2011-03-07 23:38:41 +00001312unsigned ARMMCCodeEmitter::
1313getShiftRight64Imm(const MCInst &MI, unsigned Op,
1314 SmallVectorImpl<MCFixup> &Fixups) const {
1315 return 64 - MI.getOperand(Op).getImm();
1316}
1317
Jim Grosbach568eeed2010-09-17 18:46:17 +00001318void ARMMCCodeEmitter::
1319EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001320 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001321 // Pseudo instructions don't get encoded.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001322 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001323 uint64_t TSFlags = Desc.TSFlags;
1324 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001325 return;
Owen Anderson16884412011-07-13 23:22:26 +00001326
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001327 int Size;
Owen Anderson16884412011-07-13 23:22:26 +00001328 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1329 Size = Desc.getSize();
1330 else
1331 llvm_unreachable("Unexpected instruction size!");
1332
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001333 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
Evan Cheng75972122011-01-13 07:58:56 +00001334 // Thumb 32-bit wide instructions need to emit the high order halfword
1335 // first.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001336 if (isThumb() && Size == 4) {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001337 EmitConstant(Binary >> 16, 2, OS);
1338 EmitConstant(Binary & 0xffff, 2, OS);
1339 } else
1340 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001341 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001342}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001343
Jim Grosbach806e80e2010-11-03 23:52:49 +00001344#include "ARMGenMCCodeEmitter.inc"