blob: 60be7f740cecc837166ffd4aab284e8f02ebbda4 [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 Grosbach7eab97f2010-11-11 16:55:29 +0000187 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
188 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
189 SmallVectorImpl<MCFixup> &Fixups) const;
190
Jim Grosbach570a9222010-11-11 01:09:40 +0000191 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
192 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
193 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000194
Jim Grosbachd967cd02010-12-07 21:50:47 +0000195 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
196 /// operand.
197 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
198 SmallVectorImpl<MCFixup> &Fixups) const;
199
Bill Wendlingf4caf692010-12-14 03:36:38 +0000200 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
201 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000202 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000203
Bill Wendlingb8958b02010-12-08 01:57:09 +0000204 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
205 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
206 SmallVectorImpl<MCFixup> &Fixups) const;
207
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000208 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000209 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
210 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000211
Jim Grosbach08bd5492010-10-12 23:00:24 +0000212 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000213 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
214 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000215 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
216 // '1' respectively.
217 return MI.getOperand(Op).getReg() == ARM::CPSR;
218 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000219
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000220 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000221 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
222 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000223 unsigned SoImm = MI.getOperand(Op).getImm();
224 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
225 assert(SoImmVal != -1 && "Not a valid so_imm value!");
226
227 // Encode rotate_imm.
228 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
229 << ARMII::SoRotImmShift;
230
231 // Encode immed_8.
232 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
233 return Binary;
234 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000235
Owen Anderson5de6d842010-11-12 21:12:40 +0000236 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
237 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
238 SmallVectorImpl<MCFixup> &Fixups) const {
239 unsigned SoImm = MI.getOperand(Op).getImm();
240 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
241 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
242 return Encoded;
243 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000244
Owen Anderson75579f72010-11-29 22:44:32 +0000245 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
246 SmallVectorImpl<MCFixup> &Fixups) const;
247 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
248 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000249 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
250 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000251 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
252 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000253
Jim Grosbachef324d72010-10-12 23:53:58 +0000254 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Owen Anderson152d4a42011-07-21 23:38:37 +0000255 unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op,
256 SmallVectorImpl<MCFixup> &Fixups) const;
257 unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000258 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000259 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
260 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000261
Jim Grosbach806e80e2010-11-03 23:52:49 +0000262 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
263 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000264 return 64 - MI.getOperand(Op).getImm();
265 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000266
Jim Grosbach806e80e2010-11-03 23:52:49 +0000267 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
268 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000269
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +0000270 unsigned getMsbOpValue(const MCInst &MI, unsigned Op,
271 SmallVectorImpl<MCFixup> &Fixups) const;
272
Jim Grosbach806e80e2010-11-03 23:52:49 +0000273 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
274 SmallVectorImpl<MCFixup> &Fixups) const;
275 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
276 SmallVectorImpl<MCFixup> &Fixups) const;
Mon P Wang183c6272011-05-09 17:47:27 +0000277 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
278 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000279 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
280 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000281 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
282 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000283
Bill Wendling3116dce2011-03-07 23:38:41 +0000284 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
285 SmallVectorImpl<MCFixup> &Fixups) const;
286 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
287 SmallVectorImpl<MCFixup> &Fixups) const;
288 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
289 SmallVectorImpl<MCFixup> &Fixups) const;
290 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
291 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendlinga656b632011-03-01 01:00:59 +0000292
Owen Andersonc7139a62010-11-11 19:07:48 +0000293 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
294 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000295 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000296 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000297 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000298 unsigned EncodedValue) const;
299
300 unsigned VFPThumb2PostEncoder(const MCInst &MI,
301 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000302
Jim Grosbach70933262010-11-04 01:12:30 +0000303 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000304 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000305 }
306
Jim Grosbach70933262010-11-04 01:12:30 +0000307 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000308 // Output the constant in little endian byte order.
309 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000310 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000311 Val >>= 8;
312 }
313 }
314
Jim Grosbach568eeed2010-09-17 18:46:17 +0000315 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
316 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000317};
318
319} // end anonymous namespace
320
Evan Cheng59ee62d2011-07-11 03:57:24 +0000321MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
322 const MCSubtargetInfo &STI,
Bill Wendling0800ce72010-11-02 22:53:11 +0000323 MCContext &Ctx) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000324 return new ARMMCCodeEmitter(MCII, STI, Ctx);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000325}
326
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000327/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
328/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000329/// Thumb2 mode.
330unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
331 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000332 if (isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000333 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000334 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
335 // set to 1111.
336 unsigned Bit24 = EncodedValue & 0x01000000;
337 unsigned Bit28 = Bit24 << 4;
338 EncodedValue &= 0xEFFFFFFF;
339 EncodedValue |= Bit28;
340 EncodedValue |= 0x0F000000;
341 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000342
Owen Andersonc7139a62010-11-11 19:07:48 +0000343 return EncodedValue;
344}
345
Owen Anderson57dac882010-11-11 21:36:43 +0000346/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000347/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000348/// Thumb2 mode.
349unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
350 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000351 if (isThumb2()) {
Owen Anderson57dac882010-11-11 21:36:43 +0000352 EncodedValue &= 0xF0FFFFFF;
353 EncodedValue |= 0x09000000;
354 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000355
Owen Anderson57dac882010-11-11 21:36:43 +0000356 return EncodedValue;
357}
358
Owen Anderson8f143912010-11-11 23:12:55 +0000359/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000360/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000361/// Thumb2 mode.
362unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
363 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000364 if (isThumb2()) {
Owen Anderson8f143912010-11-11 23:12:55 +0000365 EncodedValue &= 0x00FFFFFF;
366 EncodedValue |= 0xEE000000;
367 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000368
Owen Anderson8f143912010-11-11 23:12:55 +0000369 return EncodedValue;
370}
371
Bill Wendlingcf590262010-12-01 21:54:50 +0000372/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
373/// them to their Thumb2 form if we are currently in Thumb2 mode.
374unsigned ARMMCCodeEmitter::
375VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000376 if (isThumb2()) {
Bill Wendlingcf590262010-12-01 21:54:50 +0000377 EncodedValue &= 0x0FFFFFFF;
378 EncodedValue |= 0xE0000000;
379 }
380 return EncodedValue;
381}
Owen Anderson57dac882010-11-11 21:36:43 +0000382
Jim Grosbach56ac9072010-10-08 21:45:55 +0000383/// getMachineOpValue - Return binary encoding of operand. If the machine
384/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000385unsigned ARMMCCodeEmitter::
386getMachineOpValue(const MCInst &MI, const MCOperand &MO,
387 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000388 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000389 unsigned Reg = MO.getReg();
390 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000391
Jim Grosbachb0708d22010-11-30 23:51:41 +0000392 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000393 switch (Reg) {
394 default:
395 return RegNo;
396 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
397 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
398 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
399 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
400 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000401 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000402 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000403 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000404 } else if (MO.isFPImm()) {
405 return static_cast<unsigned>(APFloat(MO.getFPImm())
406 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000407 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000408
Jim Grosbach817c1a62010-11-19 00:27:09 +0000409 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000410 return 0;
411}
412
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000413/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000414bool ARMMCCodeEmitter::
415EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
416 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000417 const MCOperand &MO = MI.getOperand(OpIdx);
418 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000419
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000420 Reg = getARMRegisterNumbering(MO.getReg());
421
422 int32_t SImm = MO1.getImm();
423 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000424
Jim Grosbachab682a22010-10-28 18:34:10 +0000425 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000426 if (SImm == INT32_MIN)
427 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000428
Jim Grosbachab682a22010-10-28 18:34:10 +0000429 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000430 if (SImm < 0) {
431 SImm = -SImm;
432 isAdd = false;
433 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000434
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000435 Imm = SImm;
436 return isAdd;
437}
438
Bill Wendlingdff2f712010-12-08 23:01:43 +0000439/// getBranchTargetOpValue - Helper function to get the branch target operand,
440/// which is either an immediate or requires a fixup.
441static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
442 unsigned FixupKind,
443 SmallVectorImpl<MCFixup> &Fixups) {
444 const MCOperand &MO = MI.getOperand(OpIdx);
445
446 // If the destination is an immediate, we have nothing to do.
447 if (MO.isImm()) return MO.getImm();
448 assert(MO.isExpr() && "Unexpected branch target type!");
449 const MCExpr *Expr = MO.getExpr();
450 MCFixupKind Kind = MCFixupKind(FixupKind);
451 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
452
453 // All of the information is in the fixup.
454 return 0;
455}
456
457/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000458uint32_t ARMMCCodeEmitter::
459getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
460 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000461 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000462}
463
Bill Wendling09aa3f02010-12-09 00:39:08 +0000464/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
465/// BLX branch target.
466uint32_t ARMMCCodeEmitter::
467getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
468 SmallVectorImpl<MCFixup> &Fixups) const {
469 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
470}
471
Jim Grosbache2467172010-12-10 18:21:33 +0000472/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
473uint32_t ARMMCCodeEmitter::
474getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
475 SmallVectorImpl<MCFixup> &Fixups) const {
476 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
477}
478
Jim Grosbach01086452010-12-10 17:13:40 +0000479/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
480uint32_t ARMMCCodeEmitter::
481getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000482 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000483 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
484}
485
Jim Grosbach027d6e82010-12-09 19:04:53 +0000486/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000487uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000488getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000489 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000490 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000491}
492
Jason W Kim685c3502011-02-04 19:47:15 +0000493/// Return true if this branch has a non-always predication
494static bool HasConditionalBranch(const MCInst &MI) {
495 int NumOp = MI.getNumOperands();
496 if (NumOp >= 2) {
497 for (int i = 0; i < NumOp-1; ++i) {
498 const MCOperand &MCOp1 = MI.getOperand(i);
499 const MCOperand &MCOp2 = MI.getOperand(i + 1);
500 if (MCOp1.isImm() && MCOp2.isReg() &&
501 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
502 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
503 return true;
504 }
505 }
506 }
507 return false;
508}
509
Bill Wendlingdff2f712010-12-08 23:01:43 +0000510/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
511/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000512uint32_t ARMMCCodeEmitter::
513getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000514 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000515 // FIXME: This really, really shouldn't use TargetMachine. We don't want
516 // coupling between MC and TM anywhere we can help it.
Evan Cheng59ee62d2011-07-11 03:57:24 +0000517 if (isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000518 return
519 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Jason W Kim685c3502011-02-04 19:47:15 +0000520 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000521}
522
Jason W Kim685c3502011-02-04 19:47:15 +0000523/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
524/// target.
525uint32_t ARMMCCodeEmitter::
526getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
527 SmallVectorImpl<MCFixup> &Fixups) const {
528 if (HasConditionalBranch(MI))
529 return ::getBranchTargetOpValue(MI, OpIdx,
530 ARM::fixup_arm_condbranch, Fixups);
531 return ::getBranchTargetOpValue(MI, OpIdx,
532 ARM::fixup_arm_uncondbranch, Fixups);
533}
534
535
536
537
Owen Andersonc2666002010-12-13 19:31:11 +0000538/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
539/// immediate branch target.
540uint32_t ARMMCCodeEmitter::
541getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
542 SmallVectorImpl<MCFixup> &Fixups) const {
543 unsigned Val =
544 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
545 bool I = (Val & 0x800000);
546 bool J1 = (Val & 0x400000);
547 bool J2 = (Val & 0x200000);
548 if (I ^ J1)
549 Val &= ~0x400000;
550 else
551 Val |= 0x400000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000552
Owen Andersonc2666002010-12-13 19:31:11 +0000553 if (I ^ J2)
554 Val &= ~0x200000;
555 else
556 Val |= 0x200000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000557
Owen Andersonc2666002010-12-13 19:31:11 +0000558 return Val;
559}
560
Bill Wendlingdff2f712010-12-08 23:01:43 +0000561/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
562/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000563uint32_t ARMMCCodeEmitter::
564getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
565 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000566 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
567 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
568 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000569}
570
Owen Andersona838a252010-12-14 00:36:49 +0000571/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
572/// target.
573uint32_t ARMMCCodeEmitter::
574getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
575 SmallVectorImpl<MCFixup> &Fixups) const {
576 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
577 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
578 Fixups);
579}
580
Jim Grosbachd40963c2010-12-14 22:28:03 +0000581/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
582/// target.
583uint32_t ARMMCCodeEmitter::
584getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
585 SmallVectorImpl<MCFixup> &Fixups) const {
586 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
587 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
588 Fixups);
589}
590
Bill Wendlingf4caf692010-12-14 03:36:38 +0000591/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
592/// operand.
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000593uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000594getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
595 SmallVectorImpl<MCFixup> &) const {
596 // [Rn, Rm]
597 // {5-3} = Rm
598 // {2-0} = Rn
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000599 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000600 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000601 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
602 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
603 return (Rm << 3) | Rn;
604}
605
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000606/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000607uint32_t ARMMCCodeEmitter::
608getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
609 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000610 // {17-13} = reg
611 // {12} = (U)nsigned (add == '1', sub == '0')
612 // {11-0} = imm12
613 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000614 bool isAdd = true;
615 // If The first operand isn't a register, we have a label reference.
616 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Anderson971b83b2011-02-08 22:39:40 +0000617 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000618 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000619 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000620 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000621
Owen Anderson971b83b2011-02-08 22:39:40 +0000622 assert(MO.isExpr() && "Unexpected machine operand type!");
623 const MCExpr *Expr = MO.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000624
Owen Andersond7b3f582010-12-09 01:51:07 +0000625 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000626 if (isThumb2())
Owen Andersond7b3f582010-12-09 01:51:07 +0000627 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
628 else
629 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000630 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
631
632 ++MCNumCPRelocations;
633 } else
634 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000635
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000636 uint32_t Binary = Imm12 & 0xfff;
637 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000638 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000639 Binary |= (1 << 12);
640 Binary |= (Reg << 13);
641 return Binary;
642}
643
Owen Anderson9d63d902010-12-01 19:18:46 +0000644/// getT2AddrModeImm8s4OpValue - Return encoding info for
645/// 'reg +/- imm8<<2' operand.
646uint32_t ARMMCCodeEmitter::
647getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
648 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000649 // {12-9} = reg
650 // {8} = (U)nsigned (add == '1', sub == '0')
651 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000652 unsigned Reg, Imm8;
653 bool isAdd = true;
654 // If The first operand isn't a register, we have a label reference.
655 const MCOperand &MO = MI.getOperand(OpIdx);
656 if (!MO.isReg()) {
657 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
658 Imm8 = 0;
659 isAdd = false ; // 'U' bit is set as part of the fixup.
660
661 assert(MO.isExpr() && "Unexpected machine operand type!");
662 const MCExpr *Expr = MO.getExpr();
663 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
664 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
665
666 ++MCNumCPRelocations;
667 } else
668 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
669
670 uint32_t Binary = (Imm8 >> 2) & 0xff;
671 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
672 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000673 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000674 Binary |= (Reg << 9);
675 return Binary;
676}
677
Jason W Kim86a97f22011-01-12 00:19:25 +0000678// FIXME: This routine assumes that a binary
679// expression will always result in a PCRel expression
680// In reality, its only true if one or more subexpressions
681// is itself a PCRel (i.e. "." in asm or some other pcrel construct)
682// but this is good enough for now.
683static bool EvaluateAsPCRel(const MCExpr *Expr) {
684 switch (Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000685 default: assert(0 && "Unexpected expression type");
Jason W Kim86a97f22011-01-12 00:19:25 +0000686 case MCExpr::SymbolRef: return false;
687 case MCExpr::Binary: return true;
Jason W Kim86a97f22011-01-12 00:19:25 +0000688 }
689}
690
Evan Cheng75972122011-01-13 07:58:56 +0000691uint32_t
692ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
693 SmallVectorImpl<MCFixup> &Fixups) const {
Jason W Kim837caa92010-11-18 23:37:15 +0000694 // {20-16} = imm{15-12}
695 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000696 const MCOperand &MO = MI.getOperand(OpIdx);
Evan Cheng75972122011-01-13 07:58:56 +0000697 if (MO.isImm())
698 // Hi / lo 16 bits already extracted during earlier passes.
Jason W Kim837caa92010-11-18 23:37:15 +0000699 return static_cast<unsigned>(MO.getImm());
Evan Cheng75972122011-01-13 07:58:56 +0000700
701 // Handle :upper16: and :lower16: assembly prefixes.
702 const MCExpr *E = MO.getExpr();
703 if (E->getKind() == MCExpr::Target) {
704 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
705 E = ARM16Expr->getSubExpr();
706
Jason W Kim837caa92010-11-18 23:37:15 +0000707 MCFixupKind Kind;
Evan Cheng75972122011-01-13 07:58:56 +0000708 switch (ARM16Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000709 default: assert(0 && "Unsupported ARMFixup");
Evan Cheng75972122011-01-13 07:58:56 +0000710 case ARMMCExpr::VK_ARM_HI16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000711 if (!isTargetDarwin() && EvaluateAsPCRel(E))
712 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000713 ? ARM::fixup_t2_movt_hi16_pcrel
714 : ARM::fixup_arm_movt_hi16_pcrel);
715 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000716 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000717 ? ARM::fixup_t2_movt_hi16
718 : ARM::fixup_arm_movt_hi16);
Jason W Kim837caa92010-11-18 23:37:15 +0000719 break;
Evan Cheng75972122011-01-13 07:58:56 +0000720 case ARMMCExpr::VK_ARM_LO16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000721 if (!isTargetDarwin() && EvaluateAsPCRel(E))
722 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000723 ? ARM::fixup_t2_movw_lo16_pcrel
724 : ARM::fixup_arm_movw_lo16_pcrel);
725 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000726 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000727 ? ARM::fixup_t2_movw_lo16
728 : ARM::fixup_arm_movw_lo16);
Jason W Kim837caa92010-11-18 23:37:15 +0000729 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000730 }
Evan Cheng75972122011-01-13 07:58:56 +0000731 Fixups.push_back(MCFixup::Create(0, E, Kind));
Jason W Kim837caa92010-11-18 23:37:15 +0000732 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000733 };
Evan Cheng75972122011-01-13 07:58:56 +0000734
Jim Grosbach817c1a62010-11-19 00:27:09 +0000735 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000736 return 0;
737}
738
739uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000740getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
741 SmallVectorImpl<MCFixup> &Fixups) const {
742 const MCOperand &MO = MI.getOperand(OpIdx);
743 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
744 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
745 unsigned Rn = getARMRegisterNumbering(MO.getReg());
746 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000747 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
748 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000749 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
750 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000751
752 // {16-13} = Rn
753 // {12} = isAdd
754 // {11-0} = shifter
755 // {3-0} = Rm
756 // {4} = 0
757 // {6-5} = type
758 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000759 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000760 Binary |= Rn << 13;
761 Binary |= SBits << 5;
762 Binary |= ShImm << 7;
763 if (isAdd)
764 Binary |= 1 << 12;
765 return Binary;
766}
767
Jim Grosbach570a9222010-11-11 01:09:40 +0000768uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000769getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
770 SmallVectorImpl<MCFixup> &Fixups) const {
771 // {17-14} Rn
772 // {13} 1 == imm12, 0 == Rm
773 // {12} isAdd
774 // {11-0} imm12/Rm
775 const MCOperand &MO = MI.getOperand(OpIdx);
776 unsigned Rn = getARMRegisterNumbering(MO.getReg());
777 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
778 Binary |= Rn << 14;
779 return Binary;
780}
781
782uint32_t ARMMCCodeEmitter::
783getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
784 SmallVectorImpl<MCFixup> &Fixups) const {
785 // {13} 1 == imm12, 0 == Rm
786 // {12} isAdd
787 // {11-0} imm12/Rm
788 const MCOperand &MO = MI.getOperand(OpIdx);
789 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
790 unsigned Imm = MO1.getImm();
791 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
792 bool isReg = MO.getReg() != 0;
793 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
794 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
795 if (isReg) {
796 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
797 Binary <<= 7; // Shift amount is bits [11:7]
798 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
799 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
800 }
801 return Binary | (isAdd << 12) | (isReg << 13);
802}
803
804uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000805getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
806 SmallVectorImpl<MCFixup> &Fixups) const {
807 // {9} 1 == imm8, 0 == Rm
808 // {8} isAdd
809 // {7-4} imm7_4/zero
810 // {3-0} imm3_0/Rm
811 const MCOperand &MO = MI.getOperand(OpIdx);
812 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
813 unsigned Imm = MO1.getImm();
814 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
815 bool isImm = MO.getReg() == 0;
816 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
817 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
818 if (!isImm)
819 Imm8 = getARMRegisterNumbering(MO.getReg());
820 return Imm8 | (isAdd << 8) | (isImm << 9);
821}
822
823uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000824getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
825 SmallVectorImpl<MCFixup> &Fixups) const {
826 // {13} 1 == imm8, 0 == Rm
827 // {12-9} Rn
828 // {8} isAdd
829 // {7-4} imm7_4/zero
830 // {3-0} imm3_0/Rm
831 const MCOperand &MO = MI.getOperand(OpIdx);
832 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
833 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
834 unsigned Rn = getARMRegisterNumbering(MO.getReg());
835 unsigned Imm = MO2.getImm();
836 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
837 bool isImm = MO1.getReg() == 0;
838 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
839 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
840 if (!isImm)
841 Imm8 = getARMRegisterNumbering(MO1.getReg());
842 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
843}
844
Bill Wendlingb8958b02010-12-08 01:57:09 +0000845/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000846uint32_t ARMMCCodeEmitter::
847getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
848 SmallVectorImpl<MCFixup> &Fixups) const {
849 // [SP, #imm]
850 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000851 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000852 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
853 "Unexpected base register!");
Bill Wendling7a905a82010-12-15 23:32:27 +0000854
Jim Grosbachd967cd02010-12-07 21:50:47 +0000855 // The immediate is already shifted for the implicit zeroes, so no change
856 // here.
857 return MO1.getImm() & 0xff;
858}
859
Bill Wendlingf4caf692010-12-14 03:36:38 +0000860/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000861uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000862getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000863 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000864 // [Rn, #imm]
865 // {7-3} = imm5
866 // {2-0} = Rn
867 const MCOperand &MO = MI.getOperand(OpIdx);
868 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000869 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Matt Beaumont-Gay656b3d22010-12-16 01:34:26 +0000870 unsigned Imm5 = MO1.getImm();
Bill Wendling272df512010-12-09 21:49:07 +0000871 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000872}
873
Bill Wendlingb8958b02010-12-08 01:57:09 +0000874/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
875uint32_t ARMMCCodeEmitter::
876getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
877 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000878 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000879}
880
Jim Grosbach5177f792010-12-01 21:09:40 +0000881/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000882uint32_t ARMMCCodeEmitter::
883getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
884 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000885 // {12-9} = reg
886 // {8} = (U)nsigned (add == '1', sub == '0')
887 // {7-0} = imm8
888 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000889 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000890 // If The first operand isn't a register, we have a label reference.
891 const MCOperand &MO = MI.getOperand(OpIdx);
892 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000893 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000894 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000895 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000896
897 assert(MO.isExpr() && "Unexpected machine operand type!");
898 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000899 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000900 if (isThumb2())
Owen Andersond8e351b2010-12-08 00:18:36 +0000901 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
902 else
903 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000904 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
905
906 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000907 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000908 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000909 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
910 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000911
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000912 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
913 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000914 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000915 Binary |= (1 << 8);
916 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000917 return Binary;
918}
919
Jim Grosbach806e80e2010-11-03 23:52:49 +0000920unsigned ARMMCCodeEmitter::
Owen Anderson152d4a42011-07-21 23:38:37 +0000921getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000922 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000923 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
Owen Anderson354712c2011-07-28 17:56:55 +0000924 // shifted. The second is Rs, the amount to shift by, and the third specifies
925 // the type of the shift.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000926 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000927 // {3-0} = Rm.
Owen Anderson354712c2011-07-28 17:56:55 +0000928 // {4} = 1
Jim Grosbachef324d72010-10-12 23:53:58 +0000929 // {6-5} = type
Owen Anderson354712c2011-07-28 17:56:55 +0000930 // {11-8} = Rs
931 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000932
933 const MCOperand &MO = MI.getOperand(OpIdx);
934 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
935 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
936 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
937
938 // Encode Rm.
939 unsigned Binary = getARMRegisterNumbering(MO.getReg());
940
941 // Encode the shift opcode.
942 unsigned SBits = 0;
943 unsigned Rs = MO1.getReg();
944 if (Rs) {
945 // Set shift operand (bit[7:4]).
946 // LSL - 0001
947 // LSR - 0011
948 // ASR - 0101
949 // ROR - 0111
Jim Grosbachef324d72010-10-12 23:53:58 +0000950 switch (SOpc) {
951 default: llvm_unreachable("Unknown shift opc!");
952 case ARM_AM::lsl: SBits = 0x1; break;
953 case ARM_AM::lsr: SBits = 0x3; break;
954 case ARM_AM::asr: SBits = 0x5; break;
955 case ARM_AM::ror: SBits = 0x7; break;
Jim Grosbachef324d72010-10-12 23:53:58 +0000956 }
957 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000958
Jim Grosbachef324d72010-10-12 23:53:58 +0000959 Binary |= SBits << 4;
Jim Grosbachef324d72010-10-12 23:53:58 +0000960
Owen Anderson354712c2011-07-28 17:56:55 +0000961 // Encode the shift operation Rs.
Owen Anderson152d4a42011-07-21 23:38:37 +0000962 // Encode Rs bit[11:8].
963 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
964 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
965}
966
967unsigned ARMMCCodeEmitter::
968getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
969 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson354712c2011-07-28 17:56:55 +0000970 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
971 // shifted. The second is the amount to shift by.
Owen Anderson152d4a42011-07-21 23:38:37 +0000972 //
973 // {3-0} = Rm.
Owen Anderson354712c2011-07-28 17:56:55 +0000974 // {4} = 0
Owen Anderson152d4a42011-07-21 23:38:37 +0000975 // {6-5} = type
Owen Anderson354712c2011-07-28 17:56:55 +0000976 // {11-7} = imm
Owen Anderson152d4a42011-07-21 23:38:37 +0000977
978 const MCOperand &MO = MI.getOperand(OpIdx);
979 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
980 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
981
982 // Encode Rm.
983 unsigned Binary = getARMRegisterNumbering(MO.getReg());
984
985 // Encode the shift opcode.
986 unsigned SBits = 0;
987
988 // Set shift operand (bit[6:4]).
989 // LSL - 000
990 // LSR - 010
991 // ASR - 100
992 // ROR - 110
993 // RRX - 110 and bit[11:8] clear.
994 switch (SOpc) {
995 default: llvm_unreachable("Unknown shift opc!");
996 case ARM_AM::lsl: SBits = 0x0; break;
997 case ARM_AM::lsr: SBits = 0x2; break;
998 case ARM_AM::asr: SBits = 0x4; break;
999 case ARM_AM::ror: SBits = 0x6; break;
1000 case ARM_AM::rrx:
1001 Binary |= 0x60;
1002 return Binary;
Jim Grosbachef324d72010-10-12 23:53:58 +00001003 }
1004
1005 // Encode shift_imm bit[11:7].
Owen Anderson152d4a42011-07-21 23:38:37 +00001006 Binary |= SBits << 4;
1007 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
Jim Grosbachef324d72010-10-12 23:53:58 +00001008}
1009
Owen Anderson152d4a42011-07-21 23:38:37 +00001010
Jim Grosbach806e80e2010-11-03 23:52:49 +00001011unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +00001012getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1013 SmallVectorImpl<MCFixup> &Fixups) const {
1014 const MCOperand &MO1 = MI.getOperand(OpNum);
1015 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001016 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1017
Owen Anderson75579f72010-11-29 22:44:32 +00001018 // Encoded as [Rn, Rm, imm].
1019 // FIXME: Needs fixup support.
1020 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1021 Value <<= 4;
1022 Value |= getARMRegisterNumbering(MO2.getReg());
1023 Value <<= 2;
1024 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001025
Owen Anderson75579f72010-11-29 22:44:32 +00001026 return Value;
1027}
1028
1029unsigned ARMMCCodeEmitter::
1030getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1031 SmallVectorImpl<MCFixup> &Fixups) const {
1032 const MCOperand &MO1 = MI.getOperand(OpNum);
1033 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1034
1035 // FIXME: Needs fixup support.
1036 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001037
Owen Anderson75579f72010-11-29 22:44:32 +00001038 // Even though the immediate is 8 bits long, we need 9 bits in order
1039 // to represent the (inverse of the) sign bit.
1040 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +00001041 int32_t tmp = (int32_t)MO2.getImm();
1042 if (tmp < 0)
1043 tmp = abs(tmp);
1044 else
1045 Value |= 256; // Set the ADD bit
1046 Value |= tmp & 255;
1047 return Value;
1048}
1049
1050unsigned ARMMCCodeEmitter::
1051getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1052 SmallVectorImpl<MCFixup> &Fixups) const {
1053 const MCOperand &MO1 = MI.getOperand(OpNum);
1054
1055 // FIXME: Needs fixup support.
1056 unsigned Value = 0;
1057 int32_t tmp = (int32_t)MO1.getImm();
1058 if (tmp < 0)
1059 tmp = abs(tmp);
1060 else
1061 Value |= 256; // Set the ADD bit
1062 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +00001063 return Value;
1064}
1065
1066unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +00001067getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1068 SmallVectorImpl<MCFixup> &Fixups) const {
1069 const MCOperand &MO1 = MI.getOperand(OpNum);
1070
1071 // FIXME: Needs fixup support.
1072 unsigned Value = 0;
1073 int32_t tmp = (int32_t)MO1.getImm();
1074 if (tmp < 0)
1075 tmp = abs(tmp);
1076 else
1077 Value |= 4096; // Set the ADD bit
1078 Value |= tmp & 4095;
1079 return Value;
1080}
1081
1082unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001083getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1084 SmallVectorImpl<MCFixup> &Fixups) const {
1085 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1086 // shifted. The second is the amount to shift by.
1087 //
1088 // {3-0} = Rm.
1089 // {4} = 0
1090 // {6-5} = type
1091 // {11-7} = imm
1092
1093 const MCOperand &MO = MI.getOperand(OpIdx);
1094 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1095 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1096
1097 // Encode Rm.
1098 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1099
1100 // Encode the shift opcode.
1101 unsigned SBits = 0;
1102 // Set shift operand (bit[6:4]).
1103 // LSL - 000
1104 // LSR - 010
1105 // ASR - 100
1106 // ROR - 110
1107 switch (SOpc) {
1108 default: llvm_unreachable("Unknown shift opc!");
1109 case ARM_AM::lsl: SBits = 0x0; break;
1110 case ARM_AM::lsr: SBits = 0x2; break;
1111 case ARM_AM::asr: SBits = 0x4; break;
1112 case ARM_AM::ror: SBits = 0x6; break;
1113 }
1114
1115 Binary |= SBits << 4;
1116 if (SOpc == ARM_AM::rrx)
1117 return Binary;
1118
1119 // Encode shift_imm bit[11:7].
1120 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1121}
1122
1123unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001124getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1125 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001126 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1127 // msb of the mask.
1128 const MCOperand &MO = MI.getOperand(Op);
1129 uint32_t v = ~MO.getImm();
1130 uint32_t lsb = CountTrailingZeros_32(v);
1131 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1132 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1133 return lsb | (msb << 5);
1134}
1135
Jim Grosbach806e80e2010-11-03 23:52:49 +00001136unsigned ARMMCCodeEmitter::
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +00001137getMsbOpValue(const MCInst &MI, unsigned Op,
1138 SmallVectorImpl<MCFixup> &Fixups) const {
1139 // MSB - 5 bits.
1140 uint32_t lsb = MI.getOperand(Op-1).getImm();
1141 uint32_t width = MI.getOperand(Op).getImm();
1142 uint32_t msb = lsb+width-1;
1143 assert (width != 0 && msb < 32 && "Illegal bit width!");
1144 return msb;
1145}
1146
1147unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001148getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001149 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001150 // VLDM/VSTM:
1151 // {12-8} = Vd
1152 // {7-0} = Number of registers
1153 //
1154 // LDM/STM:
1155 // {15-0} = Bitfield of GPRs.
1156 unsigned Reg = MI.getOperand(Op).getReg();
Evan Chengbe740292011-07-23 00:00:19 +00001157 bool SPRRegs = llvm::ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1158 bool DPRRegs = llvm::ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
Bill Wendling6bc105a2010-11-17 00:45:23 +00001159
Bill Wendling5e559a22010-11-09 00:30:18 +00001160 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001161
1162 if (SPRRegs || DPRRegs) {
1163 // VLDM/VSTM
1164 unsigned RegNo = getARMRegisterNumbering(Reg);
1165 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1166 Binary |= (RegNo & 0x1f) << 8;
1167 if (SPRRegs)
1168 Binary |= NumRegs;
1169 else
1170 Binary |= NumRegs * 2;
1171 } else {
1172 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1173 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1174 Binary |= 1 << RegNo;
1175 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001176 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001177
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001178 return Binary;
1179}
1180
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001181/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1182/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001183unsigned ARMMCCodeEmitter::
1184getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1185 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001186 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001187 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001188
Owen Andersond9aa7d32010-11-02 00:05:05 +00001189 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001190 unsigned Align = 0;
1191
1192 switch (Imm.getImm()) {
1193 default: break;
1194 case 2:
1195 case 4:
1196 case 8: Align = 0x01; break;
1197 case 16: Align = 0x02; break;
1198 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001199 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001200
Owen Andersond9aa7d32010-11-02 00:05:05 +00001201 return RegNo | (Align << 4);
1202}
1203
Mon P Wang183c6272011-05-09 17:47:27 +00001204/// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1205/// along with the alignment operand for use in VST1 and VLD1 with size 32.
1206unsigned ARMMCCodeEmitter::
1207getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1208 SmallVectorImpl<MCFixup> &Fixups) const {
1209 const MCOperand &Reg = MI.getOperand(Op);
1210 const MCOperand &Imm = MI.getOperand(Op + 1);
1211
1212 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1213 unsigned Align = 0;
1214
1215 switch (Imm.getImm()) {
1216 default: break;
1217 case 2:
1218 case 4:
1219 case 8:
1220 case 16: Align = 0x00; break;
1221 case 32: Align = 0x03; break;
1222 }
1223
1224 return RegNo | (Align << 4);
1225}
1226
1227
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001228/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1229/// alignment operand for use in VLD-dup instructions. This is the same as
1230/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1231/// different for VLD4-dup.
1232unsigned ARMMCCodeEmitter::
1233getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1234 SmallVectorImpl<MCFixup> &Fixups) const {
1235 const MCOperand &Reg = MI.getOperand(Op);
1236 const MCOperand &Imm = MI.getOperand(Op + 1);
1237
1238 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1239 unsigned Align = 0;
1240
1241 switch (Imm.getImm()) {
1242 default: break;
1243 case 2:
1244 case 4:
1245 case 8: Align = 0x01; break;
1246 case 16: Align = 0x03; break;
1247 }
1248
1249 return RegNo | (Align << 4);
1250}
1251
Jim Grosbach806e80e2010-11-03 23:52:49 +00001252unsigned ARMMCCodeEmitter::
1253getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1254 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001255 const MCOperand &MO = MI.getOperand(Op);
1256 if (MO.getReg() == 0) return 0x0D;
1257 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001258}
1259
Bill Wendlinga656b632011-03-01 01:00:59 +00001260unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001261getShiftRight8Imm(const MCInst &MI, unsigned Op,
1262 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001263 return 8 - MI.getOperand(Op).getImm();
1264}
1265
1266unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001267getShiftRight16Imm(const MCInst &MI, unsigned Op,
1268 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001269 return 16 - MI.getOperand(Op).getImm();
1270}
1271
1272unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001273getShiftRight32Imm(const MCInst &MI, unsigned Op,
1274 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001275 return 32 - MI.getOperand(Op).getImm();
1276}
1277
Bill Wendling3116dce2011-03-07 23:38:41 +00001278unsigned ARMMCCodeEmitter::
1279getShiftRight64Imm(const MCInst &MI, unsigned Op,
1280 SmallVectorImpl<MCFixup> &Fixups) const {
1281 return 64 - MI.getOperand(Op).getImm();
1282}
1283
Jim Grosbach568eeed2010-09-17 18:46:17 +00001284void ARMMCCodeEmitter::
1285EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001286 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001287 // Pseudo instructions don't get encoded.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001288 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001289 uint64_t TSFlags = Desc.TSFlags;
1290 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001291 return;
Owen Anderson16884412011-07-13 23:22:26 +00001292
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001293 int Size;
Owen Anderson16884412011-07-13 23:22:26 +00001294 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1295 Size = Desc.getSize();
1296 else
1297 llvm_unreachable("Unexpected instruction size!");
1298
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001299 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
Evan Cheng75972122011-01-13 07:58:56 +00001300 // Thumb 32-bit wide instructions need to emit the high order halfword
1301 // first.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001302 if (isThumb() && Size == 4) {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001303 EmitConstant(Binary >> 16, 2, OS);
1304 EmitConstant(Binary & 0xffff, 2, OS);
1305 } else
1306 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001307 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001308}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001309
Jim Grosbach806e80e2010-11-03 23:52:49 +00001310#include "ARMGenMCCodeEmitter.inc"