blob: 57439df15305f1764e2bdcee9fd8796c42f74a2d [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 getRotImmOpValue(const MCInst &MI, unsigned Op,
263 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000264 switch (MI.getOperand(Op).getImm()) {
265 default: assert (0 && "Not a valid rot_imm value!");
266 case 0: return 0;
267 case 8: return 1;
268 case 16: return 2;
269 case 24: return 3;
270 }
271 }
272
Jim Grosbach806e80e2010-11-03 23:52:49 +0000273 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
274 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000275 return MI.getOperand(Op).getImm() - 1;
276 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000277
Jim Grosbach806e80e2010-11-03 23:52:49 +0000278 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
279 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000280 return 64 - MI.getOperand(Op).getImm();
281 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000282
Jim Grosbach806e80e2010-11-03 23:52:49 +0000283 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
284 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000285
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +0000286 unsigned getMsbOpValue(const MCInst &MI, unsigned Op,
287 SmallVectorImpl<MCFixup> &Fixups) const;
288
Jim Grosbach806e80e2010-11-03 23:52:49 +0000289 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
290 SmallVectorImpl<MCFixup> &Fixups) const;
291 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
292 SmallVectorImpl<MCFixup> &Fixups) const;
Mon P Wang183c6272011-05-09 17:47:27 +0000293 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
294 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000295 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
296 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000297 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
298 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000299
Bill Wendling3116dce2011-03-07 23:38:41 +0000300 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
301 SmallVectorImpl<MCFixup> &Fixups) const;
302 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
303 SmallVectorImpl<MCFixup> &Fixups) const;
304 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
305 SmallVectorImpl<MCFixup> &Fixups) const;
306 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
307 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendlinga656b632011-03-01 01:00:59 +0000308
Owen Andersonc7139a62010-11-11 19:07:48 +0000309 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
310 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000311 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000312 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000313 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000314 unsigned EncodedValue) const;
315
316 unsigned VFPThumb2PostEncoder(const MCInst &MI,
317 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000318
Jim Grosbach70933262010-11-04 01:12:30 +0000319 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000320 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000321 }
322
Jim Grosbach70933262010-11-04 01:12:30 +0000323 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000324 // Output the constant in little endian byte order.
325 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000326 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000327 Val >>= 8;
328 }
329 }
330
Jim Grosbach568eeed2010-09-17 18:46:17 +0000331 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
332 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000333};
334
335} // end anonymous namespace
336
Evan Cheng59ee62d2011-07-11 03:57:24 +0000337MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
338 const MCSubtargetInfo &STI,
Bill Wendling0800ce72010-11-02 22:53:11 +0000339 MCContext &Ctx) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000340 return new ARMMCCodeEmitter(MCII, STI, Ctx);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000341}
342
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000343/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
344/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000345/// Thumb2 mode.
346unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
347 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000348 if (isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000349 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000350 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
351 // set to 1111.
352 unsigned Bit24 = EncodedValue & 0x01000000;
353 unsigned Bit28 = Bit24 << 4;
354 EncodedValue &= 0xEFFFFFFF;
355 EncodedValue |= Bit28;
356 EncodedValue |= 0x0F000000;
357 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000358
Owen Andersonc7139a62010-11-11 19:07:48 +0000359 return EncodedValue;
360}
361
Owen Anderson57dac882010-11-11 21:36:43 +0000362/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000363/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000364/// Thumb2 mode.
365unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
366 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000367 if (isThumb2()) {
Owen Anderson57dac882010-11-11 21:36:43 +0000368 EncodedValue &= 0xF0FFFFFF;
369 EncodedValue |= 0x09000000;
370 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000371
Owen Anderson57dac882010-11-11 21:36:43 +0000372 return EncodedValue;
373}
374
Owen Anderson8f143912010-11-11 23:12:55 +0000375/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000376/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000377/// Thumb2 mode.
378unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
379 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000380 if (isThumb2()) {
Owen Anderson8f143912010-11-11 23:12:55 +0000381 EncodedValue &= 0x00FFFFFF;
382 EncodedValue |= 0xEE000000;
383 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000384
Owen Anderson8f143912010-11-11 23:12:55 +0000385 return EncodedValue;
386}
387
Bill Wendlingcf590262010-12-01 21:54:50 +0000388/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
389/// them to their Thumb2 form if we are currently in Thumb2 mode.
390unsigned ARMMCCodeEmitter::
391VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000392 if (isThumb2()) {
Bill Wendlingcf590262010-12-01 21:54:50 +0000393 EncodedValue &= 0x0FFFFFFF;
394 EncodedValue |= 0xE0000000;
395 }
396 return EncodedValue;
397}
Owen Anderson57dac882010-11-11 21:36:43 +0000398
Jim Grosbach56ac9072010-10-08 21:45:55 +0000399/// getMachineOpValue - Return binary encoding of operand. If the machine
400/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000401unsigned ARMMCCodeEmitter::
402getMachineOpValue(const MCInst &MI, const MCOperand &MO,
403 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000404 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000405 unsigned Reg = MO.getReg();
406 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000407
Jim Grosbachb0708d22010-11-30 23:51:41 +0000408 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000409 switch (Reg) {
410 default:
411 return RegNo;
412 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
413 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
414 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
415 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
416 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000417 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000418 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000419 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000420 } else if (MO.isFPImm()) {
421 return static_cast<unsigned>(APFloat(MO.getFPImm())
422 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000423 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000424
Jim Grosbach817c1a62010-11-19 00:27:09 +0000425 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000426 return 0;
427}
428
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000429/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000430bool ARMMCCodeEmitter::
431EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
432 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000433 const MCOperand &MO = MI.getOperand(OpIdx);
434 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000435
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000436 Reg = getARMRegisterNumbering(MO.getReg());
437
438 int32_t SImm = MO1.getImm();
439 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000440
Jim Grosbachab682a22010-10-28 18:34:10 +0000441 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000442 if (SImm == INT32_MIN)
443 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000444
Jim Grosbachab682a22010-10-28 18:34:10 +0000445 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000446 if (SImm < 0) {
447 SImm = -SImm;
448 isAdd = false;
449 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000450
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000451 Imm = SImm;
452 return isAdd;
453}
454
Bill Wendlingdff2f712010-12-08 23:01:43 +0000455/// getBranchTargetOpValue - Helper function to get the branch target operand,
456/// which is either an immediate or requires a fixup.
457static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
458 unsigned FixupKind,
459 SmallVectorImpl<MCFixup> &Fixups) {
460 const MCOperand &MO = MI.getOperand(OpIdx);
461
462 // If the destination is an immediate, we have nothing to do.
463 if (MO.isImm()) return MO.getImm();
464 assert(MO.isExpr() && "Unexpected branch target type!");
465 const MCExpr *Expr = MO.getExpr();
466 MCFixupKind Kind = MCFixupKind(FixupKind);
467 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
468
469 // All of the information is in the fixup.
470 return 0;
471}
472
473/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000474uint32_t ARMMCCodeEmitter::
475getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
476 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000477 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000478}
479
Bill Wendling09aa3f02010-12-09 00:39:08 +0000480/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
481/// BLX branch target.
482uint32_t ARMMCCodeEmitter::
483getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
484 SmallVectorImpl<MCFixup> &Fixups) const {
485 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
486}
487
Jim Grosbache2467172010-12-10 18:21:33 +0000488/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
489uint32_t ARMMCCodeEmitter::
490getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
491 SmallVectorImpl<MCFixup> &Fixups) const {
492 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
493}
494
Jim Grosbach01086452010-12-10 17:13:40 +0000495/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
496uint32_t ARMMCCodeEmitter::
497getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000498 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000499 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
500}
501
Jim Grosbach027d6e82010-12-09 19:04:53 +0000502/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000503uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000504getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000505 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000506 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000507}
508
Jason W Kim685c3502011-02-04 19:47:15 +0000509/// Return true if this branch has a non-always predication
510static bool HasConditionalBranch(const MCInst &MI) {
511 int NumOp = MI.getNumOperands();
512 if (NumOp >= 2) {
513 for (int i = 0; i < NumOp-1; ++i) {
514 const MCOperand &MCOp1 = MI.getOperand(i);
515 const MCOperand &MCOp2 = MI.getOperand(i + 1);
516 if (MCOp1.isImm() && MCOp2.isReg() &&
517 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
518 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
519 return true;
520 }
521 }
522 }
523 return false;
524}
525
Bill Wendlingdff2f712010-12-08 23:01:43 +0000526/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
527/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000528uint32_t ARMMCCodeEmitter::
529getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000530 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000531 // FIXME: This really, really shouldn't use TargetMachine. We don't want
532 // coupling between MC and TM anywhere we can help it.
Evan Cheng59ee62d2011-07-11 03:57:24 +0000533 if (isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000534 return
535 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Jason W Kim685c3502011-02-04 19:47:15 +0000536 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000537}
538
Jason W Kim685c3502011-02-04 19:47:15 +0000539/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
540/// target.
541uint32_t ARMMCCodeEmitter::
542getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
543 SmallVectorImpl<MCFixup> &Fixups) const {
544 if (HasConditionalBranch(MI))
545 return ::getBranchTargetOpValue(MI, OpIdx,
546 ARM::fixup_arm_condbranch, Fixups);
547 return ::getBranchTargetOpValue(MI, OpIdx,
548 ARM::fixup_arm_uncondbranch, Fixups);
549}
550
551
552
553
Owen Andersonc2666002010-12-13 19:31:11 +0000554/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
555/// immediate branch target.
556uint32_t ARMMCCodeEmitter::
557getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
558 SmallVectorImpl<MCFixup> &Fixups) const {
559 unsigned Val =
560 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
561 bool I = (Val & 0x800000);
562 bool J1 = (Val & 0x400000);
563 bool J2 = (Val & 0x200000);
564 if (I ^ J1)
565 Val &= ~0x400000;
566 else
567 Val |= 0x400000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000568
Owen Andersonc2666002010-12-13 19:31:11 +0000569 if (I ^ J2)
570 Val &= ~0x200000;
571 else
572 Val |= 0x200000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000573
Owen Andersonc2666002010-12-13 19:31:11 +0000574 return Val;
575}
576
Bill Wendlingdff2f712010-12-08 23:01:43 +0000577/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
578/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000579uint32_t ARMMCCodeEmitter::
580getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
581 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000582 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
583 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
584 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000585}
586
Owen Andersona838a252010-12-14 00:36:49 +0000587/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
588/// target.
589uint32_t ARMMCCodeEmitter::
590getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
591 SmallVectorImpl<MCFixup> &Fixups) const {
592 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
593 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
594 Fixups);
595}
596
Jim Grosbachd40963c2010-12-14 22:28:03 +0000597/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
598/// target.
599uint32_t ARMMCCodeEmitter::
600getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
601 SmallVectorImpl<MCFixup> &Fixups) const {
602 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
603 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
604 Fixups);
605}
606
Bill Wendlingf4caf692010-12-14 03:36:38 +0000607/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
608/// operand.
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000609uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000610getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
611 SmallVectorImpl<MCFixup> &) const {
612 // [Rn, Rm]
613 // {5-3} = Rm
614 // {2-0} = Rn
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000615 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000616 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000617 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
618 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
619 return (Rm << 3) | Rn;
620}
621
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000622/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000623uint32_t ARMMCCodeEmitter::
624getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
625 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000626 // {17-13} = reg
627 // {12} = (U)nsigned (add == '1', sub == '0')
628 // {11-0} = imm12
629 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000630 bool isAdd = true;
631 // If The first operand isn't a register, we have a label reference.
632 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Anderson971b83b2011-02-08 22:39:40 +0000633 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000634 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000635 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000636 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000637
Owen Anderson971b83b2011-02-08 22:39:40 +0000638 assert(MO.isExpr() && "Unexpected machine operand type!");
639 const MCExpr *Expr = MO.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000640
Owen Andersond7b3f582010-12-09 01:51:07 +0000641 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000642 if (isThumb2())
Owen Andersond7b3f582010-12-09 01:51:07 +0000643 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
644 else
645 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000646 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
647
648 ++MCNumCPRelocations;
649 } else
650 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000651
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000652 uint32_t Binary = Imm12 & 0xfff;
653 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000654 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000655 Binary |= (1 << 12);
656 Binary |= (Reg << 13);
657 return Binary;
658}
659
Owen Anderson9d63d902010-12-01 19:18:46 +0000660/// getT2AddrModeImm8s4OpValue - Return encoding info for
661/// 'reg +/- imm8<<2' operand.
662uint32_t ARMMCCodeEmitter::
663getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
664 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000665 // {12-9} = reg
666 // {8} = (U)nsigned (add == '1', sub == '0')
667 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000668 unsigned Reg, Imm8;
669 bool isAdd = true;
670 // If The first operand isn't a register, we have a label reference.
671 const MCOperand &MO = MI.getOperand(OpIdx);
672 if (!MO.isReg()) {
673 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
674 Imm8 = 0;
675 isAdd = false ; // 'U' bit is set as part of the fixup.
676
677 assert(MO.isExpr() && "Unexpected machine operand type!");
678 const MCExpr *Expr = MO.getExpr();
679 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
680 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
681
682 ++MCNumCPRelocations;
683 } else
684 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
685
686 uint32_t Binary = (Imm8 >> 2) & 0xff;
687 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
688 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000689 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000690 Binary |= (Reg << 9);
691 return Binary;
692}
693
Jason W Kim86a97f22011-01-12 00:19:25 +0000694// FIXME: This routine assumes that a binary
695// expression will always result in a PCRel expression
696// In reality, its only true if one or more subexpressions
697// is itself a PCRel (i.e. "." in asm or some other pcrel construct)
698// but this is good enough for now.
699static bool EvaluateAsPCRel(const MCExpr *Expr) {
700 switch (Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000701 default: assert(0 && "Unexpected expression type");
Jason W Kim86a97f22011-01-12 00:19:25 +0000702 case MCExpr::SymbolRef: return false;
703 case MCExpr::Binary: return true;
Jason W Kim86a97f22011-01-12 00:19:25 +0000704 }
705}
706
Evan Cheng75972122011-01-13 07:58:56 +0000707uint32_t
708ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
709 SmallVectorImpl<MCFixup> &Fixups) const {
Jason W Kim837caa92010-11-18 23:37:15 +0000710 // {20-16} = imm{15-12}
711 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000712 const MCOperand &MO = MI.getOperand(OpIdx);
Evan Cheng75972122011-01-13 07:58:56 +0000713 if (MO.isImm())
714 // Hi / lo 16 bits already extracted during earlier passes.
Jason W Kim837caa92010-11-18 23:37:15 +0000715 return static_cast<unsigned>(MO.getImm());
Evan Cheng75972122011-01-13 07:58:56 +0000716
717 // Handle :upper16: and :lower16: assembly prefixes.
718 const MCExpr *E = MO.getExpr();
719 if (E->getKind() == MCExpr::Target) {
720 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
721 E = ARM16Expr->getSubExpr();
722
Jason W Kim837caa92010-11-18 23:37:15 +0000723 MCFixupKind Kind;
Evan Cheng75972122011-01-13 07:58:56 +0000724 switch (ARM16Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000725 default: assert(0 && "Unsupported ARMFixup");
Evan Cheng75972122011-01-13 07:58:56 +0000726 case ARMMCExpr::VK_ARM_HI16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000727 if (!isTargetDarwin() && EvaluateAsPCRel(E))
728 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000729 ? ARM::fixup_t2_movt_hi16_pcrel
730 : ARM::fixup_arm_movt_hi16_pcrel);
731 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000732 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000733 ? ARM::fixup_t2_movt_hi16
734 : ARM::fixup_arm_movt_hi16);
Jason W Kim837caa92010-11-18 23:37:15 +0000735 break;
Evan Cheng75972122011-01-13 07:58:56 +0000736 case ARMMCExpr::VK_ARM_LO16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000737 if (!isTargetDarwin() && EvaluateAsPCRel(E))
738 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000739 ? ARM::fixup_t2_movw_lo16_pcrel
740 : ARM::fixup_arm_movw_lo16_pcrel);
741 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000742 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000743 ? ARM::fixup_t2_movw_lo16
744 : ARM::fixup_arm_movw_lo16);
Jason W Kim837caa92010-11-18 23:37:15 +0000745 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000746 }
Evan Cheng75972122011-01-13 07:58:56 +0000747 Fixups.push_back(MCFixup::Create(0, E, Kind));
Jason W Kim837caa92010-11-18 23:37:15 +0000748 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000749 };
Evan Cheng75972122011-01-13 07:58:56 +0000750
Jim Grosbach817c1a62010-11-19 00:27:09 +0000751 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000752 return 0;
753}
754
755uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000756getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
757 SmallVectorImpl<MCFixup> &Fixups) const {
758 const MCOperand &MO = MI.getOperand(OpIdx);
759 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
760 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
761 unsigned Rn = getARMRegisterNumbering(MO.getReg());
762 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000763 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
764 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000765 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
766 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000767
768 // {16-13} = Rn
769 // {12} = isAdd
770 // {11-0} = shifter
771 // {3-0} = Rm
772 // {4} = 0
773 // {6-5} = type
774 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000775 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000776 Binary |= Rn << 13;
777 Binary |= SBits << 5;
778 Binary |= ShImm << 7;
779 if (isAdd)
780 Binary |= 1 << 12;
781 return Binary;
782}
783
Jim Grosbach570a9222010-11-11 01:09:40 +0000784uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000785getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
786 SmallVectorImpl<MCFixup> &Fixups) const {
787 // {17-14} Rn
788 // {13} 1 == imm12, 0 == Rm
789 // {12} isAdd
790 // {11-0} imm12/Rm
791 const MCOperand &MO = MI.getOperand(OpIdx);
792 unsigned Rn = getARMRegisterNumbering(MO.getReg());
793 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
794 Binary |= Rn << 14;
795 return Binary;
796}
797
798uint32_t ARMMCCodeEmitter::
799getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
800 SmallVectorImpl<MCFixup> &Fixups) const {
801 // {13} 1 == imm12, 0 == Rm
802 // {12} isAdd
803 // {11-0} imm12/Rm
804 const MCOperand &MO = MI.getOperand(OpIdx);
805 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
806 unsigned Imm = MO1.getImm();
807 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
808 bool isReg = MO.getReg() != 0;
809 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
810 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
811 if (isReg) {
812 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
813 Binary <<= 7; // Shift amount is bits [11:7]
814 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
815 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
816 }
817 return Binary | (isAdd << 12) | (isReg << 13);
818}
819
820uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000821getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
822 SmallVectorImpl<MCFixup> &Fixups) const {
823 // {9} 1 == imm8, 0 == Rm
824 // {8} isAdd
825 // {7-4} imm7_4/zero
826 // {3-0} imm3_0/Rm
827 const MCOperand &MO = MI.getOperand(OpIdx);
828 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
829 unsigned Imm = MO1.getImm();
830 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
831 bool isImm = MO.getReg() == 0;
832 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
833 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
834 if (!isImm)
835 Imm8 = getARMRegisterNumbering(MO.getReg());
836 return Imm8 | (isAdd << 8) | (isImm << 9);
837}
838
839uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000840getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
841 SmallVectorImpl<MCFixup> &Fixups) const {
842 // {13} 1 == imm8, 0 == Rm
843 // {12-9} Rn
844 // {8} isAdd
845 // {7-4} imm7_4/zero
846 // {3-0} imm3_0/Rm
847 const MCOperand &MO = MI.getOperand(OpIdx);
848 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
849 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
850 unsigned Rn = getARMRegisterNumbering(MO.getReg());
851 unsigned Imm = MO2.getImm();
852 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
853 bool isImm = MO1.getReg() == 0;
854 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
855 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
856 if (!isImm)
857 Imm8 = getARMRegisterNumbering(MO1.getReg());
858 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
859}
860
Bill Wendlingb8958b02010-12-08 01:57:09 +0000861/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000862uint32_t ARMMCCodeEmitter::
863getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
864 SmallVectorImpl<MCFixup> &Fixups) const {
865 // [SP, #imm]
866 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000867 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000868 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
869 "Unexpected base register!");
Bill Wendling7a905a82010-12-15 23:32:27 +0000870
Jim Grosbachd967cd02010-12-07 21:50:47 +0000871 // The immediate is already shifted for the implicit zeroes, so no change
872 // here.
873 return MO1.getImm() & 0xff;
874}
875
Bill Wendlingf4caf692010-12-14 03:36:38 +0000876/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000877uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000878getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000879 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000880 // [Rn, #imm]
881 // {7-3} = imm5
882 // {2-0} = Rn
883 const MCOperand &MO = MI.getOperand(OpIdx);
884 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000885 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Matt Beaumont-Gay656b3d22010-12-16 01:34:26 +0000886 unsigned Imm5 = MO1.getImm();
Bill Wendling272df512010-12-09 21:49:07 +0000887 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000888}
889
Bill Wendlingb8958b02010-12-08 01:57:09 +0000890/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
891uint32_t ARMMCCodeEmitter::
892getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
893 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000894 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000895}
896
Jim Grosbach5177f792010-12-01 21:09:40 +0000897/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000898uint32_t ARMMCCodeEmitter::
899getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
900 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000901 // {12-9} = reg
902 // {8} = (U)nsigned (add == '1', sub == '0')
903 // {7-0} = imm8
904 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000905 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000906 // If The first operand isn't a register, we have a label reference.
907 const MCOperand &MO = MI.getOperand(OpIdx);
908 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000909 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000910 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000911 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000912
913 assert(MO.isExpr() && "Unexpected machine operand type!");
914 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000915 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000916 if (isThumb2())
Owen Andersond8e351b2010-12-08 00:18:36 +0000917 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
918 else
919 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000920 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
921
922 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000923 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000924 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000925 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
926 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000927
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000928 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
929 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000930 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000931 Binary |= (1 << 8);
932 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000933 return Binary;
934}
935
Jim Grosbach806e80e2010-11-03 23:52:49 +0000936unsigned ARMMCCodeEmitter::
Owen Anderson152d4a42011-07-21 23:38:37 +0000937getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000938 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000939 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
940 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
941 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000942 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000943 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000944 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000945 // {6-5} = type
946 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000947 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000948 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000949 // else (imm shift)
950 // {11-7} = imm
951
952 const MCOperand &MO = MI.getOperand(OpIdx);
953 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
954 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
955 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
956
957 // Encode Rm.
958 unsigned Binary = getARMRegisterNumbering(MO.getReg());
959
960 // Encode the shift opcode.
961 unsigned SBits = 0;
962 unsigned Rs = MO1.getReg();
963 if (Rs) {
964 // Set shift operand (bit[7:4]).
965 // LSL - 0001
966 // LSR - 0011
967 // ASR - 0101
968 // ROR - 0111
Jim Grosbachef324d72010-10-12 23:53:58 +0000969 switch (SOpc) {
970 default: llvm_unreachable("Unknown shift opc!");
971 case ARM_AM::lsl: SBits = 0x1; break;
972 case ARM_AM::lsr: SBits = 0x3; break;
973 case ARM_AM::asr: SBits = 0x5; break;
974 case ARM_AM::ror: SBits = 0x7; break;
Jim Grosbachef324d72010-10-12 23:53:58 +0000975 }
976 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000977
Jim Grosbachef324d72010-10-12 23:53:58 +0000978 Binary |= SBits << 4;
Jim Grosbachef324d72010-10-12 23:53:58 +0000979
980 // Encode the shift operation Rs or shift_imm (except rrx).
Owen Anderson152d4a42011-07-21 23:38:37 +0000981 // Encode Rs bit[11:8].
982 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
983 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
984}
985
986unsigned ARMMCCodeEmitter::
987getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
988 SmallVectorImpl<MCFixup> &Fixups) const {
989 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
990 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
991 // case the imm contains the amount to shift by.
992 //
993 // {3-0} = Rm.
994 // {4} = 1 if reg shift, 0 if imm shift
995 // {6-5} = type
996 // If reg shift:
997 // {11-8} = Rs
998 // {7} = 0
999 // else (imm shift)
1000 // {11-7} = imm
1001
1002 const MCOperand &MO = MI.getOperand(OpIdx);
1003 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1004 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1005
1006 // Encode Rm.
1007 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1008
1009 // Encode the shift opcode.
1010 unsigned SBits = 0;
1011
1012 // Set shift operand (bit[6:4]).
1013 // LSL - 000
1014 // LSR - 010
1015 // ASR - 100
1016 // ROR - 110
1017 // RRX - 110 and bit[11:8] clear.
1018 switch (SOpc) {
1019 default: llvm_unreachable("Unknown shift opc!");
1020 case ARM_AM::lsl: SBits = 0x0; break;
1021 case ARM_AM::lsr: SBits = 0x2; break;
1022 case ARM_AM::asr: SBits = 0x4; break;
1023 case ARM_AM::ror: SBits = 0x6; break;
1024 case ARM_AM::rrx:
1025 Binary |= 0x60;
1026 return Binary;
Jim Grosbachef324d72010-10-12 23:53:58 +00001027 }
1028
1029 // Encode shift_imm bit[11:7].
Owen Anderson152d4a42011-07-21 23:38:37 +00001030 Binary |= SBits << 4;
1031 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
Jim Grosbachef324d72010-10-12 23:53:58 +00001032}
1033
Owen Anderson152d4a42011-07-21 23:38:37 +00001034
Jim Grosbach806e80e2010-11-03 23:52:49 +00001035unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +00001036getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1037 SmallVectorImpl<MCFixup> &Fixups) const {
1038 const MCOperand &MO1 = MI.getOperand(OpNum);
1039 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001040 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1041
Owen Anderson75579f72010-11-29 22:44:32 +00001042 // Encoded as [Rn, Rm, imm].
1043 // FIXME: Needs fixup support.
1044 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1045 Value <<= 4;
1046 Value |= getARMRegisterNumbering(MO2.getReg());
1047 Value <<= 2;
1048 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001049
Owen Anderson75579f72010-11-29 22:44:32 +00001050 return Value;
1051}
1052
1053unsigned ARMMCCodeEmitter::
1054getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1055 SmallVectorImpl<MCFixup> &Fixups) const {
1056 const MCOperand &MO1 = MI.getOperand(OpNum);
1057 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1058
1059 // FIXME: Needs fixup support.
1060 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001061
Owen Anderson75579f72010-11-29 22:44:32 +00001062 // Even though the immediate is 8 bits long, we need 9 bits in order
1063 // to represent the (inverse of the) sign bit.
1064 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +00001065 int32_t tmp = (int32_t)MO2.getImm();
1066 if (tmp < 0)
1067 tmp = abs(tmp);
1068 else
1069 Value |= 256; // Set the ADD bit
1070 Value |= tmp & 255;
1071 return Value;
1072}
1073
1074unsigned ARMMCCodeEmitter::
1075getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1076 SmallVectorImpl<MCFixup> &Fixups) const {
1077 const MCOperand &MO1 = MI.getOperand(OpNum);
1078
1079 // FIXME: Needs fixup support.
1080 unsigned Value = 0;
1081 int32_t tmp = (int32_t)MO1.getImm();
1082 if (tmp < 0)
1083 tmp = abs(tmp);
1084 else
1085 Value |= 256; // Set the ADD bit
1086 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +00001087 return Value;
1088}
1089
1090unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +00001091getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1092 SmallVectorImpl<MCFixup> &Fixups) const {
1093 const MCOperand &MO1 = MI.getOperand(OpNum);
1094
1095 // FIXME: Needs fixup support.
1096 unsigned Value = 0;
1097 int32_t tmp = (int32_t)MO1.getImm();
1098 if (tmp < 0)
1099 tmp = abs(tmp);
1100 else
1101 Value |= 4096; // Set the ADD bit
1102 Value |= tmp & 4095;
1103 return Value;
1104}
1105
1106unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001107getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1108 SmallVectorImpl<MCFixup> &Fixups) const {
1109 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1110 // shifted. The second is the amount to shift by.
1111 //
1112 // {3-0} = Rm.
1113 // {4} = 0
1114 // {6-5} = type
1115 // {11-7} = imm
1116
1117 const MCOperand &MO = MI.getOperand(OpIdx);
1118 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1119 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1120
1121 // Encode Rm.
1122 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1123
1124 // Encode the shift opcode.
1125 unsigned SBits = 0;
1126 // Set shift operand (bit[6:4]).
1127 // LSL - 000
1128 // LSR - 010
1129 // ASR - 100
1130 // ROR - 110
1131 switch (SOpc) {
1132 default: llvm_unreachable("Unknown shift opc!");
1133 case ARM_AM::lsl: SBits = 0x0; break;
1134 case ARM_AM::lsr: SBits = 0x2; break;
1135 case ARM_AM::asr: SBits = 0x4; break;
1136 case ARM_AM::ror: SBits = 0x6; break;
1137 }
1138
1139 Binary |= SBits << 4;
1140 if (SOpc == ARM_AM::rrx)
1141 return Binary;
1142
1143 // Encode shift_imm bit[11:7].
1144 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1145}
1146
1147unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001148getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1149 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001150 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1151 // msb of the mask.
1152 const MCOperand &MO = MI.getOperand(Op);
1153 uint32_t v = ~MO.getImm();
1154 uint32_t lsb = CountTrailingZeros_32(v);
1155 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1156 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1157 return lsb | (msb << 5);
1158}
1159
Jim Grosbach806e80e2010-11-03 23:52:49 +00001160unsigned ARMMCCodeEmitter::
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +00001161getMsbOpValue(const MCInst &MI, unsigned Op,
1162 SmallVectorImpl<MCFixup> &Fixups) const {
1163 // MSB - 5 bits.
1164 uint32_t lsb = MI.getOperand(Op-1).getImm();
1165 uint32_t width = MI.getOperand(Op).getImm();
1166 uint32_t msb = lsb+width-1;
1167 assert (width != 0 && msb < 32 && "Illegal bit width!");
1168 return msb;
1169}
1170
Evan Chengbe740292011-07-23 00:00:19 +00001171namespace llvm {
1172 // FIXME: TableGen this?
1173 extern MCRegisterClass ARMMCRegisterClasses[]; // In ARMGenRegisterInfo.inc.
1174}
1175
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +00001176unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001177getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001178 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001179 // VLDM/VSTM:
1180 // {12-8} = Vd
1181 // {7-0} = Number of registers
1182 //
1183 // LDM/STM:
1184 // {15-0} = Bitfield of GPRs.
1185 unsigned Reg = MI.getOperand(Op).getReg();
Evan Chengbe740292011-07-23 00:00:19 +00001186 bool SPRRegs = llvm::ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1187 bool DPRRegs = llvm::ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
Bill Wendling6bc105a2010-11-17 00:45:23 +00001188
Bill Wendling5e559a22010-11-09 00:30:18 +00001189 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001190
1191 if (SPRRegs || DPRRegs) {
1192 // VLDM/VSTM
1193 unsigned RegNo = getARMRegisterNumbering(Reg);
1194 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1195 Binary |= (RegNo & 0x1f) << 8;
1196 if (SPRRegs)
1197 Binary |= NumRegs;
1198 else
1199 Binary |= NumRegs * 2;
1200 } else {
1201 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1202 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1203 Binary |= 1 << RegNo;
1204 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001205 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001206
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001207 return Binary;
1208}
1209
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001210/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1211/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001212unsigned ARMMCCodeEmitter::
1213getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1214 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001215 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001216 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001217
Owen Andersond9aa7d32010-11-02 00:05:05 +00001218 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001219 unsigned Align = 0;
1220
1221 switch (Imm.getImm()) {
1222 default: break;
1223 case 2:
1224 case 4:
1225 case 8: Align = 0x01; break;
1226 case 16: Align = 0x02; break;
1227 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001228 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001229
Owen Andersond9aa7d32010-11-02 00:05:05 +00001230 return RegNo | (Align << 4);
1231}
1232
Mon P Wang183c6272011-05-09 17:47:27 +00001233/// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1234/// along with the alignment operand for use in VST1 and VLD1 with size 32.
1235unsigned ARMMCCodeEmitter::
1236getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1237 SmallVectorImpl<MCFixup> &Fixups) const {
1238 const MCOperand &Reg = MI.getOperand(Op);
1239 const MCOperand &Imm = MI.getOperand(Op + 1);
1240
1241 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1242 unsigned Align = 0;
1243
1244 switch (Imm.getImm()) {
1245 default: break;
1246 case 2:
1247 case 4:
1248 case 8:
1249 case 16: Align = 0x00; break;
1250 case 32: Align = 0x03; break;
1251 }
1252
1253 return RegNo | (Align << 4);
1254}
1255
1256
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001257/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1258/// alignment operand for use in VLD-dup instructions. This is the same as
1259/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1260/// different for VLD4-dup.
1261unsigned ARMMCCodeEmitter::
1262getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1263 SmallVectorImpl<MCFixup> &Fixups) const {
1264 const MCOperand &Reg = MI.getOperand(Op);
1265 const MCOperand &Imm = MI.getOperand(Op + 1);
1266
1267 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1268 unsigned Align = 0;
1269
1270 switch (Imm.getImm()) {
1271 default: break;
1272 case 2:
1273 case 4:
1274 case 8: Align = 0x01; break;
1275 case 16: Align = 0x03; break;
1276 }
1277
1278 return RegNo | (Align << 4);
1279}
1280
Jim Grosbach806e80e2010-11-03 23:52:49 +00001281unsigned ARMMCCodeEmitter::
1282getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1283 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001284 const MCOperand &MO = MI.getOperand(Op);
1285 if (MO.getReg() == 0) return 0x0D;
1286 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001287}
1288
Bill Wendlinga656b632011-03-01 01:00:59 +00001289unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001290getShiftRight8Imm(const MCInst &MI, unsigned Op,
1291 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001292 return 8 - MI.getOperand(Op).getImm();
1293}
1294
1295unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001296getShiftRight16Imm(const MCInst &MI, unsigned Op,
1297 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001298 return 16 - MI.getOperand(Op).getImm();
1299}
1300
1301unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001302getShiftRight32Imm(const MCInst &MI, unsigned Op,
1303 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001304 return 32 - MI.getOperand(Op).getImm();
1305}
1306
Bill Wendling3116dce2011-03-07 23:38:41 +00001307unsigned ARMMCCodeEmitter::
1308getShiftRight64Imm(const MCInst &MI, unsigned Op,
1309 SmallVectorImpl<MCFixup> &Fixups) const {
1310 return 64 - MI.getOperand(Op).getImm();
1311}
1312
Jim Grosbach568eeed2010-09-17 18:46:17 +00001313void ARMMCCodeEmitter::
1314EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001315 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001316 // Pseudo instructions don't get encoded.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001317 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001318 uint64_t TSFlags = Desc.TSFlags;
1319 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001320 return;
Owen Anderson16884412011-07-13 23:22:26 +00001321
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001322 int Size;
Owen Anderson16884412011-07-13 23:22:26 +00001323 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1324 Size = Desc.getSize();
1325 else
1326 llvm_unreachable("Unexpected instruction size!");
1327
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001328 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
Evan Cheng75972122011-01-13 07:58:56 +00001329 // Thumb 32-bit wide instructions need to emit the high order halfword
1330 // first.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001331 if (isThumb() && Size == 4) {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001332 EmitConstant(Binary >> 16, 2, OS);
1333 EmitConstant(Binary & 0xffff, 2, OS);
1334 } else
1335 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001336 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001337}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001338
Jim Grosbach806e80e2010-11-03 23:52:49 +00001339#include "ARMGenMCCodeEmitter.inc"