blob: 2caf548f888e1b583c555869cec83d37df2b0456 [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 Andersonc7139a62010-11-11 19:07:48 +0000297 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
298 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000299 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000300 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000301 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000302 unsigned EncodedValue) const;
303
304 unsigned VFPThumb2PostEncoder(const MCInst &MI,
305 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000306
Jim Grosbach70933262010-11-04 01:12:30 +0000307 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000308 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000309 }
310
Jim Grosbach70933262010-11-04 01:12:30 +0000311 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000312 // Output the constant in little endian byte order.
313 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000314 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000315 Val >>= 8;
316 }
317 }
318
Jim Grosbach568eeed2010-09-17 18:46:17 +0000319 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
320 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000321};
322
323} // end anonymous namespace
324
Evan Cheng59ee62d2011-07-11 03:57:24 +0000325MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
326 const MCSubtargetInfo &STI,
Bill Wendling0800ce72010-11-02 22:53:11 +0000327 MCContext &Ctx) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000328 return new ARMMCCodeEmitter(MCII, STI, Ctx);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000329}
330
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000331/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
332/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000333/// Thumb2 mode.
334unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
335 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000336 if (isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000337 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000338 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
339 // set to 1111.
340 unsigned Bit24 = EncodedValue & 0x01000000;
341 unsigned Bit28 = Bit24 << 4;
342 EncodedValue &= 0xEFFFFFFF;
343 EncodedValue |= Bit28;
344 EncodedValue |= 0x0F000000;
345 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000346
Owen Andersonc7139a62010-11-11 19:07:48 +0000347 return EncodedValue;
348}
349
Owen Anderson57dac882010-11-11 21:36:43 +0000350/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000351/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000352/// Thumb2 mode.
353unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
354 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000355 if (isThumb2()) {
Owen Anderson57dac882010-11-11 21:36:43 +0000356 EncodedValue &= 0xF0FFFFFF;
357 EncodedValue |= 0x09000000;
358 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000359
Owen Anderson57dac882010-11-11 21:36:43 +0000360 return EncodedValue;
361}
362
Owen Anderson8f143912010-11-11 23:12:55 +0000363/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000364/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000365/// Thumb2 mode.
366unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
367 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000368 if (isThumb2()) {
Owen Anderson8f143912010-11-11 23:12:55 +0000369 EncodedValue &= 0x00FFFFFF;
370 EncodedValue |= 0xEE000000;
371 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000372
Owen Anderson8f143912010-11-11 23:12:55 +0000373 return EncodedValue;
374}
375
Bill Wendlingcf590262010-12-01 21:54:50 +0000376/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
377/// them to their Thumb2 form if we are currently in Thumb2 mode.
378unsigned ARMMCCodeEmitter::
379VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000380 if (isThumb2()) {
Bill Wendlingcf590262010-12-01 21:54:50 +0000381 EncodedValue &= 0x0FFFFFFF;
382 EncodedValue |= 0xE0000000;
383 }
384 return EncodedValue;
385}
Owen Anderson57dac882010-11-11 21:36:43 +0000386
Jim Grosbach56ac9072010-10-08 21:45:55 +0000387/// getMachineOpValue - Return binary encoding of operand. If the machine
388/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000389unsigned ARMMCCodeEmitter::
390getMachineOpValue(const MCInst &MI, const MCOperand &MO,
391 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000392 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000393 unsigned Reg = MO.getReg();
394 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000395
Jim Grosbachb0708d22010-11-30 23:51:41 +0000396 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000397 switch (Reg) {
398 default:
399 return RegNo;
400 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
401 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
402 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
403 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
404 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000405 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000406 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000407 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000408 } else if (MO.isFPImm()) {
409 return static_cast<unsigned>(APFloat(MO.getFPImm())
410 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000411 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000412
Jim Grosbach817c1a62010-11-19 00:27:09 +0000413 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000414 return 0;
415}
416
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000417/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000418bool ARMMCCodeEmitter::
419EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
420 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000421 const MCOperand &MO = MI.getOperand(OpIdx);
422 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000423
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000424 Reg = getARMRegisterNumbering(MO.getReg());
425
426 int32_t SImm = MO1.getImm();
427 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000428
Jim Grosbachab682a22010-10-28 18:34:10 +0000429 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000430 if (SImm == INT32_MIN)
431 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000432
Jim Grosbachab682a22010-10-28 18:34:10 +0000433 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000434 if (SImm < 0) {
435 SImm = -SImm;
436 isAdd = false;
437 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000438
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000439 Imm = SImm;
440 return isAdd;
441}
442
Bill Wendlingdff2f712010-12-08 23:01:43 +0000443/// getBranchTargetOpValue - Helper function to get the branch target operand,
444/// which is either an immediate or requires a fixup.
445static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
446 unsigned FixupKind,
447 SmallVectorImpl<MCFixup> &Fixups) {
448 const MCOperand &MO = MI.getOperand(OpIdx);
449
450 // If the destination is an immediate, we have nothing to do.
451 if (MO.isImm()) return MO.getImm();
452 assert(MO.isExpr() && "Unexpected branch target type!");
453 const MCExpr *Expr = MO.getExpr();
454 MCFixupKind Kind = MCFixupKind(FixupKind);
455 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
456
457 // All of the information is in the fixup.
458 return 0;
459}
460
461/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000462uint32_t ARMMCCodeEmitter::
463getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
464 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000465 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000466}
467
Bill Wendling09aa3f02010-12-09 00:39:08 +0000468/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
469/// BLX branch target.
470uint32_t ARMMCCodeEmitter::
471getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
472 SmallVectorImpl<MCFixup> &Fixups) const {
473 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
474}
475
Jim Grosbache2467172010-12-10 18:21:33 +0000476/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
477uint32_t ARMMCCodeEmitter::
478getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
479 SmallVectorImpl<MCFixup> &Fixups) const {
480 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
481}
482
Jim Grosbach01086452010-12-10 17:13:40 +0000483/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
484uint32_t ARMMCCodeEmitter::
485getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000486 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000487 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
488}
489
Jim Grosbach027d6e82010-12-09 19:04:53 +0000490/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000491uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000492getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000493 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000494 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000495}
496
Jason W Kim685c3502011-02-04 19:47:15 +0000497/// Return true if this branch has a non-always predication
498static bool HasConditionalBranch(const MCInst &MI) {
499 int NumOp = MI.getNumOperands();
500 if (NumOp >= 2) {
501 for (int i = 0; i < NumOp-1; ++i) {
502 const MCOperand &MCOp1 = MI.getOperand(i);
503 const MCOperand &MCOp2 = MI.getOperand(i + 1);
504 if (MCOp1.isImm() && MCOp2.isReg() &&
505 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
506 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
507 return true;
508 }
509 }
510 }
511 return false;
512}
513
Bill Wendlingdff2f712010-12-08 23:01:43 +0000514/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
515/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000516uint32_t ARMMCCodeEmitter::
517getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000518 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000519 // FIXME: This really, really shouldn't use TargetMachine. We don't want
520 // coupling between MC and TM anywhere we can help it.
Evan Cheng59ee62d2011-07-11 03:57:24 +0000521 if (isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000522 return
523 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Jason W Kim685c3502011-02-04 19:47:15 +0000524 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000525}
526
Jason W Kim685c3502011-02-04 19:47:15 +0000527/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
528/// target.
529uint32_t ARMMCCodeEmitter::
530getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
531 SmallVectorImpl<MCFixup> &Fixups) const {
532 if (HasConditionalBranch(MI))
533 return ::getBranchTargetOpValue(MI, OpIdx,
534 ARM::fixup_arm_condbranch, Fixups);
535 return ::getBranchTargetOpValue(MI, OpIdx,
536 ARM::fixup_arm_uncondbranch, Fixups);
537}
538
539
540
541
Owen Andersonc2666002010-12-13 19:31:11 +0000542/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
543/// immediate branch target.
544uint32_t ARMMCCodeEmitter::
545getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
546 SmallVectorImpl<MCFixup> &Fixups) const {
547 unsigned Val =
548 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
549 bool I = (Val & 0x800000);
550 bool J1 = (Val & 0x400000);
551 bool J2 = (Val & 0x200000);
552 if (I ^ J1)
553 Val &= ~0x400000;
554 else
555 Val |= 0x400000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000556
Owen Andersonc2666002010-12-13 19:31:11 +0000557 if (I ^ J2)
558 Val &= ~0x200000;
559 else
560 Val |= 0x200000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000561
Owen Andersonc2666002010-12-13 19:31:11 +0000562 return Val;
563}
564
Bill Wendlingdff2f712010-12-08 23:01:43 +0000565/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
566/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000567uint32_t ARMMCCodeEmitter::
568getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
569 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000570 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
571 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
572 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000573}
574
Owen Andersona838a252010-12-14 00:36:49 +0000575/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
576/// target.
577uint32_t ARMMCCodeEmitter::
578getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
579 SmallVectorImpl<MCFixup> &Fixups) const {
580 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
581 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
582 Fixups);
583}
584
Jim Grosbachd40963c2010-12-14 22:28:03 +0000585/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
586/// target.
587uint32_t ARMMCCodeEmitter::
588getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
589 SmallVectorImpl<MCFixup> &Fixups) const {
590 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
591 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
592 Fixups);
593}
594
Bill Wendlingf4caf692010-12-14 03:36:38 +0000595/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
596/// operand.
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000597uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000598getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
599 SmallVectorImpl<MCFixup> &) const {
600 // [Rn, Rm]
601 // {5-3} = Rm
602 // {2-0} = Rn
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000603 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000604 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000605 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
606 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
607 return (Rm << 3) | Rn;
608}
609
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000610/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000611uint32_t ARMMCCodeEmitter::
612getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
613 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000614 // {17-13} = reg
615 // {12} = (U)nsigned (add == '1', sub == '0')
616 // {11-0} = imm12
617 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000618 bool isAdd = true;
619 // If The first operand isn't a register, we have a label reference.
620 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Anderson971b83b2011-02-08 22:39:40 +0000621 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000622 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000623 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000624 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000625
Owen Anderson971b83b2011-02-08 22:39:40 +0000626 assert(MO.isExpr() && "Unexpected machine operand type!");
627 const MCExpr *Expr = MO.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000628
Owen Andersond7b3f582010-12-09 01:51:07 +0000629 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000630 if (isThumb2())
Owen Andersond7b3f582010-12-09 01:51:07 +0000631 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
632 else
633 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000634 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
635
636 ++MCNumCPRelocations;
637 } else
638 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000639
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000640 uint32_t Binary = Imm12 & 0xfff;
641 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000642 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000643 Binary |= (1 << 12);
644 Binary |= (Reg << 13);
645 return Binary;
646}
647
Owen Anderson9d63d902010-12-01 19:18:46 +0000648/// getT2AddrModeImm8s4OpValue - Return encoding info for
649/// 'reg +/- imm8<<2' operand.
650uint32_t ARMMCCodeEmitter::
651getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
652 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000653 // {12-9} = reg
654 // {8} = (U)nsigned (add == '1', sub == '0')
655 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000656 unsigned Reg, Imm8;
657 bool isAdd = true;
658 // If The first operand isn't a register, we have a label reference.
659 const MCOperand &MO = MI.getOperand(OpIdx);
660 if (!MO.isReg()) {
661 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
662 Imm8 = 0;
663 isAdd = false ; // 'U' bit is set as part of the fixup.
664
665 assert(MO.isExpr() && "Unexpected machine operand type!");
666 const MCExpr *Expr = MO.getExpr();
667 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
668 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
669
670 ++MCNumCPRelocations;
671 } else
672 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
673
674 uint32_t Binary = (Imm8 >> 2) & 0xff;
675 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
676 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000677 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000678 Binary |= (Reg << 9);
679 return Binary;
680}
681
Jason W Kim86a97f22011-01-12 00:19:25 +0000682// FIXME: This routine assumes that a binary
683// expression will always result in a PCRel expression
684// In reality, its only true if one or more subexpressions
685// is itself a PCRel (i.e. "." in asm or some other pcrel construct)
686// but this is good enough for now.
687static bool EvaluateAsPCRel(const MCExpr *Expr) {
688 switch (Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000689 default: assert(0 && "Unexpected expression type");
Jason W Kim86a97f22011-01-12 00:19:25 +0000690 case MCExpr::SymbolRef: return false;
691 case MCExpr::Binary: return true;
Jason W Kim86a97f22011-01-12 00:19:25 +0000692 }
693}
694
Evan Cheng75972122011-01-13 07:58:56 +0000695uint32_t
696ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
697 SmallVectorImpl<MCFixup> &Fixups) const {
Jason W Kim837caa92010-11-18 23:37:15 +0000698 // {20-16} = imm{15-12}
699 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000700 const MCOperand &MO = MI.getOperand(OpIdx);
Evan Cheng75972122011-01-13 07:58:56 +0000701 if (MO.isImm())
702 // Hi / lo 16 bits already extracted during earlier passes.
Jason W Kim837caa92010-11-18 23:37:15 +0000703 return static_cast<unsigned>(MO.getImm());
Evan Cheng75972122011-01-13 07:58:56 +0000704
705 // Handle :upper16: and :lower16: assembly prefixes.
706 const MCExpr *E = MO.getExpr();
707 if (E->getKind() == MCExpr::Target) {
708 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
709 E = ARM16Expr->getSubExpr();
710
Jason W Kim837caa92010-11-18 23:37:15 +0000711 MCFixupKind Kind;
Evan Cheng75972122011-01-13 07:58:56 +0000712 switch (ARM16Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000713 default: assert(0 && "Unsupported ARMFixup");
Evan Cheng75972122011-01-13 07:58:56 +0000714 case ARMMCExpr::VK_ARM_HI16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000715 if (!isTargetDarwin() && EvaluateAsPCRel(E))
716 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000717 ? ARM::fixup_t2_movt_hi16_pcrel
718 : ARM::fixup_arm_movt_hi16_pcrel);
719 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000720 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000721 ? ARM::fixup_t2_movt_hi16
722 : ARM::fixup_arm_movt_hi16);
Jason W Kim837caa92010-11-18 23:37:15 +0000723 break;
Evan Cheng75972122011-01-13 07:58:56 +0000724 case ARMMCExpr::VK_ARM_LO16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000725 if (!isTargetDarwin() && EvaluateAsPCRel(E))
726 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000727 ? ARM::fixup_t2_movw_lo16_pcrel
728 : ARM::fixup_arm_movw_lo16_pcrel);
729 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000730 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000731 ? ARM::fixup_t2_movw_lo16
732 : ARM::fixup_arm_movw_lo16);
Jason W Kim837caa92010-11-18 23:37:15 +0000733 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000734 }
Evan Cheng75972122011-01-13 07:58:56 +0000735 Fixups.push_back(MCFixup::Create(0, E, Kind));
Jason W Kim837caa92010-11-18 23:37:15 +0000736 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000737 };
Evan Cheng75972122011-01-13 07:58:56 +0000738
Jim Grosbach817c1a62010-11-19 00:27:09 +0000739 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000740 return 0;
741}
742
743uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000744getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
745 SmallVectorImpl<MCFixup> &Fixups) const {
746 const MCOperand &MO = MI.getOperand(OpIdx);
747 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
748 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
749 unsigned Rn = getARMRegisterNumbering(MO.getReg());
750 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000751 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
752 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000753 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
754 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000755
756 // {16-13} = Rn
757 // {12} = isAdd
758 // {11-0} = shifter
759 // {3-0} = Rm
760 // {4} = 0
761 // {6-5} = type
762 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000763 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000764 Binary |= Rn << 13;
765 Binary |= SBits << 5;
766 Binary |= ShImm << 7;
767 if (isAdd)
768 Binary |= 1 << 12;
769 return Binary;
770}
771
Jim Grosbach570a9222010-11-11 01:09:40 +0000772uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000773getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
774 SmallVectorImpl<MCFixup> &Fixups) const {
775 // {17-14} Rn
776 // {13} 1 == imm12, 0 == Rm
777 // {12} isAdd
778 // {11-0} imm12/Rm
779 const MCOperand &MO = MI.getOperand(OpIdx);
780 unsigned Rn = getARMRegisterNumbering(MO.getReg());
781 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
782 Binary |= Rn << 14;
783 return Binary;
784}
785
786uint32_t ARMMCCodeEmitter::
787getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
788 SmallVectorImpl<MCFixup> &Fixups) const {
789 // {13} 1 == imm12, 0 == Rm
790 // {12} isAdd
791 // {11-0} imm12/Rm
792 const MCOperand &MO = MI.getOperand(OpIdx);
793 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
794 unsigned Imm = MO1.getImm();
795 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
796 bool isReg = MO.getReg() != 0;
797 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
798 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
799 if (isReg) {
800 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
801 Binary <<= 7; // Shift amount is bits [11:7]
802 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
803 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
804 }
805 return Binary | (isAdd << 12) | (isReg << 13);
806}
807
808uint32_t ARMMCCodeEmitter::
Jim Grosbach7ce05792011-08-03 23:50:40 +0000809getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
810 SmallVectorImpl<MCFixup> &Fixups) const {
811 // {4} isAdd
812 // {3-0} Rm
813 const MCOperand &MO = MI.getOperand(OpIdx);
814 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
Jim Grosbach16578b52011-08-05 16:11:38 +0000815 bool isAdd = MO1.getImm() != 0;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000816 return getARMRegisterNumbering(MO.getReg()) | (isAdd << 4);
817}
818
819uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000820getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
821 SmallVectorImpl<MCFixup> &Fixups) const {
822 // {9} 1 == imm8, 0 == Rm
823 // {8} isAdd
824 // {7-4} imm7_4/zero
825 // {3-0} imm3_0/Rm
826 const MCOperand &MO = MI.getOperand(OpIdx);
827 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
828 unsigned Imm = MO1.getImm();
829 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
830 bool isImm = MO.getReg() == 0;
831 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
832 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
833 if (!isImm)
834 Imm8 = getARMRegisterNumbering(MO.getReg());
835 return Imm8 | (isAdd << 8) | (isImm << 9);
836}
837
838uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000839getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
840 SmallVectorImpl<MCFixup> &Fixups) const {
841 // {13} 1 == imm8, 0 == Rm
842 // {12-9} Rn
843 // {8} isAdd
844 // {7-4} imm7_4/zero
845 // {3-0} imm3_0/Rm
846 const MCOperand &MO = MI.getOperand(OpIdx);
847 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
848 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
849 unsigned Rn = getARMRegisterNumbering(MO.getReg());
850 unsigned Imm = MO2.getImm();
851 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
852 bool isImm = MO1.getReg() == 0;
853 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
854 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
855 if (!isImm)
856 Imm8 = getARMRegisterNumbering(MO1.getReg());
857 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
858}
859
Bill Wendlingb8958b02010-12-08 01:57:09 +0000860/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000861uint32_t ARMMCCodeEmitter::
862getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
863 SmallVectorImpl<MCFixup> &Fixups) const {
864 // [SP, #imm]
865 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000866 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000867 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
868 "Unexpected base register!");
Bill Wendling7a905a82010-12-15 23:32:27 +0000869
Jim Grosbachd967cd02010-12-07 21:50:47 +0000870 // The immediate is already shifted for the implicit zeroes, so no change
871 // here.
872 return MO1.getImm() & 0xff;
873}
874
Bill Wendlingf4caf692010-12-14 03:36:38 +0000875/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000876uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000877getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000878 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000879 // [Rn, #imm]
880 // {7-3} = imm5
881 // {2-0} = Rn
882 const MCOperand &MO = MI.getOperand(OpIdx);
883 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000884 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Matt Beaumont-Gay656b3d22010-12-16 01:34:26 +0000885 unsigned Imm5 = MO1.getImm();
Bill Wendling272df512010-12-09 21:49:07 +0000886 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000887}
888
Bill Wendlingb8958b02010-12-08 01:57:09 +0000889/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
890uint32_t ARMMCCodeEmitter::
891getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
892 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000893 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000894}
895
Jim Grosbach5177f792010-12-01 21:09:40 +0000896/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000897uint32_t ARMMCCodeEmitter::
898getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
899 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000900 // {12-9} = reg
901 // {8} = (U)nsigned (add == '1', sub == '0')
902 // {7-0} = imm8
903 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000904 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000905 // If The first operand isn't a register, we have a label reference.
906 const MCOperand &MO = MI.getOperand(OpIdx);
907 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000908 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000909 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000910 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000911
912 assert(MO.isExpr() && "Unexpected machine operand type!");
913 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000914 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000915 if (isThumb2())
Owen Andersond8e351b2010-12-08 00:18:36 +0000916 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
917 else
918 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000919 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
920
921 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000922 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000923 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000924 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
925 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000926
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000927 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
928 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000929 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000930 Binary |= (1 << 8);
931 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000932 return Binary;
933}
934
Jim Grosbach806e80e2010-11-03 23:52:49 +0000935unsigned ARMMCCodeEmitter::
Owen Anderson152d4a42011-07-21 23:38:37 +0000936getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000937 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000938 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
Owen Anderson354712c2011-07-28 17:56:55 +0000939 // shifted. The second is Rs, the amount to shift by, and the third specifies
940 // the type of the shift.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000941 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000942 // {3-0} = Rm.
Owen Anderson354712c2011-07-28 17:56:55 +0000943 // {4} = 1
Jim Grosbachef324d72010-10-12 23:53:58 +0000944 // {6-5} = type
Owen Anderson354712c2011-07-28 17:56:55 +0000945 // {11-8} = Rs
946 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000947
948 const MCOperand &MO = MI.getOperand(OpIdx);
949 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
950 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
951 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
952
953 // Encode Rm.
954 unsigned Binary = getARMRegisterNumbering(MO.getReg());
955
956 // Encode the shift opcode.
957 unsigned SBits = 0;
958 unsigned Rs = MO1.getReg();
959 if (Rs) {
960 // Set shift operand (bit[7:4]).
961 // LSL - 0001
962 // LSR - 0011
963 // ASR - 0101
964 // ROR - 0111
Jim Grosbachef324d72010-10-12 23:53:58 +0000965 switch (SOpc) {
966 default: llvm_unreachable("Unknown shift opc!");
967 case ARM_AM::lsl: SBits = 0x1; break;
968 case ARM_AM::lsr: SBits = 0x3; break;
969 case ARM_AM::asr: SBits = 0x5; break;
970 case ARM_AM::ror: SBits = 0x7; break;
Jim Grosbachef324d72010-10-12 23:53:58 +0000971 }
972 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000973
Jim Grosbachef324d72010-10-12 23:53:58 +0000974 Binary |= SBits << 4;
Jim Grosbachef324d72010-10-12 23:53:58 +0000975
Owen Anderson354712c2011-07-28 17:56:55 +0000976 // Encode the shift operation Rs.
Owen Anderson152d4a42011-07-21 23:38:37 +0000977 // Encode Rs bit[11:8].
978 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
979 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
980}
981
982unsigned ARMMCCodeEmitter::
983getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
984 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson354712c2011-07-28 17:56:55 +0000985 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
986 // shifted. The second is the amount to shift by.
Owen Anderson152d4a42011-07-21 23:38:37 +0000987 //
988 // {3-0} = Rm.
Owen Anderson354712c2011-07-28 17:56:55 +0000989 // {4} = 0
Owen Anderson152d4a42011-07-21 23:38:37 +0000990 // {6-5} = type
Owen Anderson354712c2011-07-28 17:56:55 +0000991 // {11-7} = imm
Owen Anderson152d4a42011-07-21 23:38:37 +0000992
993 const MCOperand &MO = MI.getOperand(OpIdx);
994 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
995 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
996
997 // Encode Rm.
998 unsigned Binary = getARMRegisterNumbering(MO.getReg());
999
1000 // Encode the shift opcode.
1001 unsigned SBits = 0;
1002
1003 // Set shift operand (bit[6:4]).
1004 // LSL - 000
1005 // LSR - 010
1006 // ASR - 100
1007 // ROR - 110
1008 // RRX - 110 and bit[11:8] clear.
1009 switch (SOpc) {
1010 default: llvm_unreachable("Unknown shift opc!");
1011 case ARM_AM::lsl: SBits = 0x0; break;
1012 case ARM_AM::lsr: SBits = 0x2; break;
1013 case ARM_AM::asr: SBits = 0x4; break;
1014 case ARM_AM::ror: SBits = 0x6; break;
1015 case ARM_AM::rrx:
1016 Binary |= 0x60;
1017 return Binary;
Jim Grosbachef324d72010-10-12 23:53:58 +00001018 }
1019
1020 // Encode shift_imm bit[11:7].
Owen Anderson152d4a42011-07-21 23:38:37 +00001021 Binary |= SBits << 4;
1022 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
Jim Grosbachef324d72010-10-12 23:53:58 +00001023}
1024
Owen Anderson152d4a42011-07-21 23:38:37 +00001025
Jim Grosbach806e80e2010-11-03 23:52:49 +00001026unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +00001027getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1028 SmallVectorImpl<MCFixup> &Fixups) const {
1029 const MCOperand &MO1 = MI.getOperand(OpNum);
1030 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001031 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1032
Owen Anderson75579f72010-11-29 22:44:32 +00001033 // Encoded as [Rn, Rm, imm].
1034 // FIXME: Needs fixup support.
1035 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1036 Value <<= 4;
1037 Value |= getARMRegisterNumbering(MO2.getReg());
1038 Value <<= 2;
1039 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001040
Owen Anderson75579f72010-11-29 22:44:32 +00001041 return Value;
1042}
1043
1044unsigned ARMMCCodeEmitter::
1045getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1046 SmallVectorImpl<MCFixup> &Fixups) const {
1047 const MCOperand &MO1 = MI.getOperand(OpNum);
1048 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1049
1050 // FIXME: Needs fixup support.
1051 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001052
Owen Anderson75579f72010-11-29 22:44:32 +00001053 // Even though the immediate is 8 bits long, we need 9 bits in order
1054 // to represent the (inverse of the) sign bit.
1055 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +00001056 int32_t tmp = (int32_t)MO2.getImm();
1057 if (tmp < 0)
1058 tmp = abs(tmp);
1059 else
1060 Value |= 256; // Set the ADD bit
1061 Value |= tmp & 255;
1062 return Value;
1063}
1064
1065unsigned ARMMCCodeEmitter::
1066getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1067 SmallVectorImpl<MCFixup> &Fixups) const {
1068 const MCOperand &MO1 = MI.getOperand(OpNum);
1069
1070 // FIXME: Needs fixup support.
1071 unsigned Value = 0;
1072 int32_t tmp = (int32_t)MO1.getImm();
1073 if (tmp < 0)
1074 tmp = abs(tmp);
1075 else
1076 Value |= 256; // Set the ADD bit
1077 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +00001078 return Value;
1079}
1080
1081unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +00001082getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1083 SmallVectorImpl<MCFixup> &Fixups) const {
1084 const MCOperand &MO1 = MI.getOperand(OpNum);
1085
1086 // FIXME: Needs fixup support.
1087 unsigned Value = 0;
1088 int32_t tmp = (int32_t)MO1.getImm();
1089 if (tmp < 0)
1090 tmp = abs(tmp);
1091 else
1092 Value |= 4096; // Set the ADD bit
1093 Value |= tmp & 4095;
1094 return Value;
1095}
1096
1097unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001098getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1099 SmallVectorImpl<MCFixup> &Fixups) const {
1100 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1101 // shifted. The second is the amount to shift by.
1102 //
1103 // {3-0} = Rm.
1104 // {4} = 0
1105 // {6-5} = type
1106 // {11-7} = imm
1107
1108 const MCOperand &MO = MI.getOperand(OpIdx);
1109 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1110 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1111
1112 // Encode Rm.
1113 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1114
1115 // Encode the shift opcode.
1116 unsigned SBits = 0;
1117 // Set shift operand (bit[6:4]).
1118 // LSL - 000
1119 // LSR - 010
1120 // ASR - 100
1121 // ROR - 110
1122 switch (SOpc) {
1123 default: llvm_unreachable("Unknown shift opc!");
1124 case ARM_AM::lsl: SBits = 0x0; break;
1125 case ARM_AM::lsr: SBits = 0x2; break;
1126 case ARM_AM::asr: SBits = 0x4; break;
1127 case ARM_AM::ror: SBits = 0x6; break;
1128 }
1129
1130 Binary |= SBits << 4;
1131 if (SOpc == ARM_AM::rrx)
1132 return Binary;
1133
1134 // Encode shift_imm bit[11:7].
1135 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1136}
1137
1138unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001139getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1140 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001141 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1142 // msb of the mask.
1143 const MCOperand &MO = MI.getOperand(Op);
1144 uint32_t v = ~MO.getImm();
1145 uint32_t lsb = CountTrailingZeros_32(v);
1146 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1147 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1148 return lsb | (msb << 5);
1149}
1150
Jim Grosbach806e80e2010-11-03 23:52:49 +00001151unsigned ARMMCCodeEmitter::
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +00001152getMsbOpValue(const MCInst &MI, unsigned Op,
1153 SmallVectorImpl<MCFixup> &Fixups) const {
1154 // MSB - 5 bits.
1155 uint32_t lsb = MI.getOperand(Op-1).getImm();
1156 uint32_t width = MI.getOperand(Op).getImm();
1157 uint32_t msb = lsb+width-1;
1158 assert (width != 0 && msb < 32 && "Illegal bit width!");
1159 return msb;
1160}
1161
1162unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001163getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001164 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001165 // VLDM/VSTM:
1166 // {12-8} = Vd
1167 // {7-0} = Number of registers
1168 //
1169 // LDM/STM:
1170 // {15-0} = Bitfield of GPRs.
1171 unsigned Reg = MI.getOperand(Op).getReg();
Evan Chengbe740292011-07-23 00:00:19 +00001172 bool SPRRegs = llvm::ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1173 bool DPRRegs = llvm::ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
Bill Wendling6bc105a2010-11-17 00:45:23 +00001174
Bill Wendling5e559a22010-11-09 00:30:18 +00001175 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001176
1177 if (SPRRegs || DPRRegs) {
1178 // VLDM/VSTM
1179 unsigned RegNo = getARMRegisterNumbering(Reg);
1180 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1181 Binary |= (RegNo & 0x1f) << 8;
1182 if (SPRRegs)
1183 Binary |= NumRegs;
1184 else
1185 Binary |= NumRegs * 2;
1186 } else {
1187 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1188 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1189 Binary |= 1 << RegNo;
1190 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001191 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001192
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001193 return Binary;
1194}
1195
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001196/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1197/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001198unsigned ARMMCCodeEmitter::
1199getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1200 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001201 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001202 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001203
Owen Andersond9aa7d32010-11-02 00:05:05 +00001204 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001205 unsigned Align = 0;
1206
1207 switch (Imm.getImm()) {
1208 default: break;
1209 case 2:
1210 case 4:
1211 case 8: Align = 0x01; break;
1212 case 16: Align = 0x02; break;
1213 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001214 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001215
Owen Andersond9aa7d32010-11-02 00:05:05 +00001216 return RegNo | (Align << 4);
1217}
1218
Mon P Wang183c6272011-05-09 17:47:27 +00001219/// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1220/// along with the alignment operand for use in VST1 and VLD1 with size 32.
1221unsigned ARMMCCodeEmitter::
1222getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1223 SmallVectorImpl<MCFixup> &Fixups) const {
1224 const MCOperand &Reg = MI.getOperand(Op);
1225 const MCOperand &Imm = MI.getOperand(Op + 1);
1226
1227 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1228 unsigned Align = 0;
1229
1230 switch (Imm.getImm()) {
1231 default: break;
1232 case 2:
1233 case 4:
1234 case 8:
1235 case 16: Align = 0x00; break;
1236 case 32: Align = 0x03; break;
1237 }
1238
1239 return RegNo | (Align << 4);
1240}
1241
1242
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001243/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1244/// alignment operand for use in VLD-dup instructions. This is the same as
1245/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1246/// different for VLD4-dup.
1247unsigned ARMMCCodeEmitter::
1248getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1249 SmallVectorImpl<MCFixup> &Fixups) const {
1250 const MCOperand &Reg = MI.getOperand(Op);
1251 const MCOperand &Imm = MI.getOperand(Op + 1);
1252
1253 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1254 unsigned Align = 0;
1255
1256 switch (Imm.getImm()) {
1257 default: break;
1258 case 2:
1259 case 4:
1260 case 8: Align = 0x01; break;
1261 case 16: Align = 0x03; break;
1262 }
1263
1264 return RegNo | (Align << 4);
1265}
1266
Jim Grosbach806e80e2010-11-03 23:52:49 +00001267unsigned ARMMCCodeEmitter::
1268getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1269 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001270 const MCOperand &MO = MI.getOperand(Op);
1271 if (MO.getReg() == 0) return 0x0D;
1272 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001273}
1274
Bill Wendlinga656b632011-03-01 01:00:59 +00001275unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001276getShiftRight8Imm(const MCInst &MI, unsigned Op,
1277 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001278 return 8 - MI.getOperand(Op).getImm();
1279}
1280
1281unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001282getShiftRight16Imm(const MCInst &MI, unsigned Op,
1283 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001284 return 16 - MI.getOperand(Op).getImm();
1285}
1286
1287unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001288getShiftRight32Imm(const MCInst &MI, unsigned Op,
1289 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001290 return 32 - MI.getOperand(Op).getImm();
1291}
1292
Bill Wendling3116dce2011-03-07 23:38:41 +00001293unsigned ARMMCCodeEmitter::
1294getShiftRight64Imm(const MCInst &MI, unsigned Op,
1295 SmallVectorImpl<MCFixup> &Fixups) const {
1296 return 64 - MI.getOperand(Op).getImm();
1297}
1298
Jim Grosbach568eeed2010-09-17 18:46:17 +00001299void ARMMCCodeEmitter::
1300EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001301 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001302 // Pseudo instructions don't get encoded.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001303 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001304 uint64_t TSFlags = Desc.TSFlags;
1305 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001306 return;
Owen Anderson16884412011-07-13 23:22:26 +00001307
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001308 int Size;
Owen Anderson16884412011-07-13 23:22:26 +00001309 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1310 Size = Desc.getSize();
1311 else
1312 llvm_unreachable("Unexpected instruction size!");
1313
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001314 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
Evan Cheng75972122011-01-13 07:58:56 +00001315 // Thumb 32-bit wide instructions need to emit the high order halfword
1316 // first.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001317 if (isThumb() && Size == 4) {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001318 EmitConstant(Binary >> 16, 2, OS);
1319 EmitConstant(Binary & 0xffff, 2, OS);
1320 } else
1321 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001322 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001323}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001324
Jim Grosbach806e80e2010-11-03 23:52:49 +00001325#include "ARMGenMCCodeEmitter.inc"