blob: df3a14f3a8e62adde4acf4d554c8fb275a9d0eae [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"
Jim Grosbach568eeed2010-09-17 18:46:17 +000015#include "ARM.h"
Jim Grosbach42fac8e2010-10-11 23:16:21 +000016#include "ARMAddressingModes.h"
Jim Grosbach70933262010-11-04 01:12:30 +000017#include "ARMFixupKinds.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000018#include "ARMInstrInfo.h"
Evan Cheng75972122011-01-13 07:58:56 +000019#include "ARMMCExpr.h"
Evan Chengf3eb3bb2011-01-14 02:38:49 +000020#include "ARMSubtarget.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000021#include "llvm/MC/MCCodeEmitter.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000024#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MC/MCSubtargetInfo.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000026#include "llvm/ADT/Statistic.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000027#include "llvm/Support/raw_ostream.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000028
29#define GET_SUBTARGETINFO_ENUM
30#include "ARMGenSubtargetInfo.inc"
31
Jim Grosbach568eeed2010-09-17 18:46:17 +000032using namespace llvm;
33
Jim Grosbach70933262010-11-04 01:12:30 +000034STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
35STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
Jim Grosbachd6d4b422010-10-07 22:12:50 +000036
Jim Grosbach568eeed2010-09-17 18:46:17 +000037namespace {
38class ARMMCCodeEmitter : public MCCodeEmitter {
39 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
40 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
Evan Cheng59ee62d2011-07-11 03:57:24 +000041 const MCInstrInfo &MCII;
42 const MCSubtargetInfo &STI;
Jim Grosbach568eeed2010-09-17 18:46:17 +000043
44public:
Evan Cheng59ee62d2011-07-11 03:57:24 +000045 ARMMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
46 MCContext &ctx)
Evan Chengaf0a2e62011-07-11 21:24:15 +000047 : MCII(mcii), STI(sti) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000048 }
49
50 ~ARMMCCodeEmitter() {}
51
Evan Cheng59ee62d2011-07-11 03:57:24 +000052 bool isThumb() const {
53 // FIXME: Can tablegen auto-generate this?
54 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
55 }
56 bool isThumb2() const {
57 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) != 0;
58 }
59 bool isTargetDarwin() const {
60 Triple TT(STI.getTargetTriple());
61 Triple::OSType OS = TT.getOS();
62 return OS == Triple::Darwin || OS == Triple::MacOSX || OS == Triple::IOS;
63 }
64
Jim Grosbach0de6ab32010-10-12 17:11:26 +000065 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
66
Jim Grosbach9af82ba2010-10-07 21:57:55 +000067 // getBinaryCodeForInstr - TableGen'erated function for getting the
68 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000069 unsigned getBinaryCodeForInstr(const MCInst &MI,
70 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000071
72 /// getMachineOpValue - Return binary encoding of operand. If the machine
73 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000074 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
75 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000076
Evan Cheng75972122011-01-13 07:58:56 +000077 /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
Owen Anderson971b83b2011-02-08 22:39:40 +000078 /// the specified operand. This is used for operands with :lower16: and
Evan Cheng75972122011-01-13 07:58:56 +000079 /// :upper16: prefixes.
80 uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
81 SmallVectorImpl<MCFixup> &Fixups) const;
Jason W Kim837caa92010-11-18 23:37:15 +000082
Bill Wendling92b5a2e2010-11-03 01:49:29 +000083 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000084 unsigned &Reg, unsigned &Imm,
85 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000086
Jim Grosbach662a8162010-12-06 23:57:07 +000087 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +000088 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +000089 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
90 SmallVectorImpl<MCFixup> &Fixups) const;
91
Bill Wendling09aa3f02010-12-09 00:39:08 +000092 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
93 /// BLX branch target.
94 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
95 SmallVectorImpl<MCFixup> &Fixups) const;
96
Jim Grosbache2467172010-12-10 18:21:33 +000097 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
98 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
99 SmallVectorImpl<MCFixup> &Fixups) const;
100
Jim Grosbach01086452010-12-10 17:13:40 +0000101 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
102 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
103 SmallVectorImpl<MCFixup> &Fixups) const;
104
Jim Grosbach027d6e82010-12-09 19:04:53 +0000105 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
106 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000107 SmallVectorImpl<MCFixup> &Fixups) const;
108
Jim Grosbachc466b932010-11-11 18:04:49 +0000109 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
110 /// branch target.
111 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
112 SmallVectorImpl<MCFixup> &Fixups) const;
113
Owen Andersonc2666002010-12-13 19:31:11 +0000114 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
115 /// immediate Thumb2 direct branch target.
116 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
117 SmallVectorImpl<MCFixup> &Fixups) const;
118
Jason W Kim685c3502011-02-04 19:47:15 +0000119 /// getARMBranchTargetOpValue - Return encoding info for 24-bit immediate
120 /// branch target.
121 uint32_t getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
122 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersonc2666002010-12-13 19:31:11 +0000123
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000124 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
125 /// ADR label target.
126 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
127 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachd40963c2010-12-14 22:28:03 +0000128 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersona838a252010-12-14 00:36:49 +0000130 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
131 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson971b83b2011-02-08 22:39:40 +0000132
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000133
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000134 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
135 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000136 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
137 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000138
Bill Wendlingf4caf692010-12-14 03:36:38 +0000139 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
140 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
141 SmallVectorImpl<MCFixup> &Fixups)const;
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000142
Owen Anderson9d63d902010-12-01 19:18:46 +0000143 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
144 /// operand.
145 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
146 SmallVectorImpl<MCFixup> &Fixups) const;
147
148
Jim Grosbach54fea632010-11-09 17:20:53 +0000149 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
150 /// operand as needed by load/store instructions.
151 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
152 SmallVectorImpl<MCFixup> &Fixups) const;
153
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000154 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
155 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
156 SmallVectorImpl<MCFixup> &Fixups) const {
157 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
158 switch (Mode) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000159 default: assert(0 && "Unknown addressing sub-mode!");
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000160 case ARM_AM::da: return 0;
161 case ARM_AM::ia: return 1;
162 case ARM_AM::db: return 2;
163 case ARM_AM::ib: return 3;
164 }
165 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000166 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
167 ///
168 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
169 switch (ShOpc) {
170 default: llvm_unreachable("Unknown shift opc!");
171 case ARM_AM::no_shift:
172 case ARM_AM::lsl: return 0;
173 case ARM_AM::lsr: return 1;
174 case ARM_AM::asr: return 2;
175 case ARM_AM::ror:
176 case ARM_AM::rrx: return 3;
177 }
178 return 0;
179 }
180
181 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
182 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
183 SmallVectorImpl<MCFixup> &Fixups) const;
184
185 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
186 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
187 SmallVectorImpl<MCFixup> &Fixups) const;
188
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000189 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
190 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
191 SmallVectorImpl<MCFixup> &Fixups) const;
192
Jim Grosbach570a9222010-11-11 01:09:40 +0000193 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
194 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
195 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000196
Jim Grosbachd967cd02010-12-07 21:50:47 +0000197 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
198 /// operand.
199 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
200 SmallVectorImpl<MCFixup> &Fixups) const;
201
Bill Wendlingf4caf692010-12-14 03:36:38 +0000202 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
203 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000204 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000205
Bill Wendlingb8958b02010-12-08 01:57:09 +0000206 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
207 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
208 SmallVectorImpl<MCFixup> &Fixups) const;
209
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000210 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000211 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
212 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000213
Jim Grosbach08bd5492010-10-12 23:00:24 +0000214 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000215 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
216 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000217 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
218 // '1' respectively.
219 return MI.getOperand(Op).getReg() == ARM::CPSR;
220 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000221
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000222 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000223 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
224 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000225 unsigned SoImm = MI.getOperand(Op).getImm();
226 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
227 assert(SoImmVal != -1 && "Not a valid so_imm value!");
228
229 // Encode rotate_imm.
230 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
231 << ARMII::SoRotImmShift;
232
233 // Encode immed_8.
234 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
235 return Binary;
236 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000237
Owen Anderson5de6d842010-11-12 21:12:40 +0000238 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
239 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
240 SmallVectorImpl<MCFixup> &Fixups) const {
241 unsigned SoImm = MI.getOperand(Op).getImm();
242 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
243 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
244 return Encoded;
245 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000246
Owen Anderson75579f72010-11-29 22:44:32 +0000247 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
248 SmallVectorImpl<MCFixup> &Fixups) const;
249 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
250 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000251 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
252 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000253 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
254 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000255
Jim Grosbachef324d72010-10-12 23:53:58 +0000256 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000257 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
258 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
Bruno Cardoso Lopes895c1e22011-05-31 03:33:27 +0000289 unsigned getSsatBitPosValue(const MCInst &MI, unsigned Op,
290 SmallVectorImpl<MCFixup> &Fixups) const;
291
Jim Grosbach806e80e2010-11-03 23:52:49 +0000292 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
293 SmallVectorImpl<MCFixup> &Fixups) const;
294 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
295 SmallVectorImpl<MCFixup> &Fixups) const;
Mon P Wang183c6272011-05-09 17:47:27 +0000296 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
297 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000298 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
299 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000300 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
301 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000302
Bill Wendling3116dce2011-03-07 23:38:41 +0000303 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
304 SmallVectorImpl<MCFixup> &Fixups) const;
305 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
306 SmallVectorImpl<MCFixup> &Fixups) const;
307 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
308 SmallVectorImpl<MCFixup> &Fixups) const;
309 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
310 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendlinga656b632011-03-01 01:00:59 +0000311
Owen Andersonc7139a62010-11-11 19:07:48 +0000312 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
313 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000314 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000315 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000316 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000317 unsigned EncodedValue) const;
318
319 unsigned VFPThumb2PostEncoder(const MCInst &MI,
320 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000321
Jim Grosbach70933262010-11-04 01:12:30 +0000322 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000323 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000324 }
325
Jim Grosbach70933262010-11-04 01:12:30 +0000326 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000327 // Output the constant in little endian byte order.
328 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000329 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000330 Val >>= 8;
331 }
332 }
333
Jim Grosbach568eeed2010-09-17 18:46:17 +0000334 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
335 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000336};
337
338} // end anonymous namespace
339
Evan Cheng59ee62d2011-07-11 03:57:24 +0000340MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
341 const MCSubtargetInfo &STI,
Bill Wendling0800ce72010-11-02 22:53:11 +0000342 MCContext &Ctx) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000343 return new ARMMCCodeEmitter(MCII, STI, Ctx);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000344}
345
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000346/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
347/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000348/// Thumb2 mode.
349unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
350 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000351 if (isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000352 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000353 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
354 // set to 1111.
355 unsigned Bit24 = EncodedValue & 0x01000000;
356 unsigned Bit28 = Bit24 << 4;
357 EncodedValue &= 0xEFFFFFFF;
358 EncodedValue |= Bit28;
359 EncodedValue |= 0x0F000000;
360 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000361
Owen Andersonc7139a62010-11-11 19:07:48 +0000362 return EncodedValue;
363}
364
Owen Anderson57dac882010-11-11 21:36:43 +0000365/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000366/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000367/// Thumb2 mode.
368unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
369 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000370 if (isThumb2()) {
Owen Anderson57dac882010-11-11 21:36:43 +0000371 EncodedValue &= 0xF0FFFFFF;
372 EncodedValue |= 0x09000000;
373 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000374
Owen Anderson57dac882010-11-11 21:36:43 +0000375 return EncodedValue;
376}
377
Owen Anderson8f143912010-11-11 23:12:55 +0000378/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000379/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000380/// Thumb2 mode.
381unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
382 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000383 if (isThumb2()) {
Owen Anderson8f143912010-11-11 23:12:55 +0000384 EncodedValue &= 0x00FFFFFF;
385 EncodedValue |= 0xEE000000;
386 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000387
Owen Anderson8f143912010-11-11 23:12:55 +0000388 return EncodedValue;
389}
390
Bill Wendlingcf590262010-12-01 21:54:50 +0000391/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
392/// them to their Thumb2 form if we are currently in Thumb2 mode.
393unsigned ARMMCCodeEmitter::
394VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000395 if (isThumb2()) {
Bill Wendlingcf590262010-12-01 21:54:50 +0000396 EncodedValue &= 0x0FFFFFFF;
397 EncodedValue |= 0xE0000000;
398 }
399 return EncodedValue;
400}
Owen Anderson57dac882010-11-11 21:36:43 +0000401
Jim Grosbach56ac9072010-10-08 21:45:55 +0000402/// getMachineOpValue - Return binary encoding of operand. If the machine
403/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000404unsigned ARMMCCodeEmitter::
405getMachineOpValue(const MCInst &MI, const MCOperand &MO,
406 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000407 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000408 unsigned Reg = MO.getReg();
409 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000410
Jim Grosbachb0708d22010-11-30 23:51:41 +0000411 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000412 switch (Reg) {
413 default:
414 return RegNo;
415 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
416 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
417 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
418 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
419 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000420 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000421 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000422 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000423 } else if (MO.isFPImm()) {
424 return static_cast<unsigned>(APFloat(MO.getFPImm())
425 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000426 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000427
Jim Grosbach817c1a62010-11-19 00:27:09 +0000428 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000429 return 0;
430}
431
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000432/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000433bool ARMMCCodeEmitter::
434EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
435 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000436 const MCOperand &MO = MI.getOperand(OpIdx);
437 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000438
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000439 Reg = getARMRegisterNumbering(MO.getReg());
440
441 int32_t SImm = MO1.getImm();
442 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000443
Jim Grosbachab682a22010-10-28 18:34:10 +0000444 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000445 if (SImm == INT32_MIN)
446 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000447
Jim Grosbachab682a22010-10-28 18:34:10 +0000448 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000449 if (SImm < 0) {
450 SImm = -SImm;
451 isAdd = false;
452 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000453
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000454 Imm = SImm;
455 return isAdd;
456}
457
Bill Wendlingdff2f712010-12-08 23:01:43 +0000458/// getBranchTargetOpValue - Helper function to get the branch target operand,
459/// which is either an immediate or requires a fixup.
460static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
461 unsigned FixupKind,
462 SmallVectorImpl<MCFixup> &Fixups) {
463 const MCOperand &MO = MI.getOperand(OpIdx);
464
465 // If the destination is an immediate, we have nothing to do.
466 if (MO.isImm()) return MO.getImm();
467 assert(MO.isExpr() && "Unexpected branch target type!");
468 const MCExpr *Expr = MO.getExpr();
469 MCFixupKind Kind = MCFixupKind(FixupKind);
470 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
471
472 // All of the information is in the fixup.
473 return 0;
474}
475
476/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000477uint32_t ARMMCCodeEmitter::
478getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
479 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000480 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000481}
482
Bill Wendling09aa3f02010-12-09 00:39:08 +0000483/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
484/// BLX branch target.
485uint32_t ARMMCCodeEmitter::
486getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
487 SmallVectorImpl<MCFixup> &Fixups) const {
488 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
489}
490
Jim Grosbache2467172010-12-10 18:21:33 +0000491/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
492uint32_t ARMMCCodeEmitter::
493getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
494 SmallVectorImpl<MCFixup> &Fixups) const {
495 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
496}
497
Jim Grosbach01086452010-12-10 17:13:40 +0000498/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
499uint32_t ARMMCCodeEmitter::
500getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000501 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000502 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
503}
504
Jim Grosbach027d6e82010-12-09 19:04:53 +0000505/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000506uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000507getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000508 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000509 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000510}
511
Jason W Kim685c3502011-02-04 19:47:15 +0000512/// Return true if this branch has a non-always predication
513static bool HasConditionalBranch(const MCInst &MI) {
514 int NumOp = MI.getNumOperands();
515 if (NumOp >= 2) {
516 for (int i = 0; i < NumOp-1; ++i) {
517 const MCOperand &MCOp1 = MI.getOperand(i);
518 const MCOperand &MCOp2 = MI.getOperand(i + 1);
519 if (MCOp1.isImm() && MCOp2.isReg() &&
520 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
521 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
522 return true;
523 }
524 }
525 }
526 return false;
527}
528
Bill Wendlingdff2f712010-12-08 23:01:43 +0000529/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
530/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000531uint32_t ARMMCCodeEmitter::
532getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000533 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000534 // FIXME: This really, really shouldn't use TargetMachine. We don't want
535 // coupling between MC and TM anywhere we can help it.
Evan Cheng59ee62d2011-07-11 03:57:24 +0000536 if (isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000537 return
538 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Jason W Kim685c3502011-02-04 19:47:15 +0000539 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000540}
541
Jason W Kim685c3502011-02-04 19:47:15 +0000542/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
543/// target.
544uint32_t ARMMCCodeEmitter::
545getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
546 SmallVectorImpl<MCFixup> &Fixups) const {
547 if (HasConditionalBranch(MI))
548 return ::getBranchTargetOpValue(MI, OpIdx,
549 ARM::fixup_arm_condbranch, Fixups);
550 return ::getBranchTargetOpValue(MI, OpIdx,
551 ARM::fixup_arm_uncondbranch, Fixups);
552}
553
554
555
556
Owen Andersonc2666002010-12-13 19:31:11 +0000557/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
558/// immediate branch target.
559uint32_t ARMMCCodeEmitter::
560getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
561 SmallVectorImpl<MCFixup> &Fixups) const {
562 unsigned Val =
563 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
564 bool I = (Val & 0x800000);
565 bool J1 = (Val & 0x400000);
566 bool J2 = (Val & 0x200000);
567 if (I ^ J1)
568 Val &= ~0x400000;
569 else
570 Val |= 0x400000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000571
Owen Andersonc2666002010-12-13 19:31:11 +0000572 if (I ^ J2)
573 Val &= ~0x200000;
574 else
575 Val |= 0x200000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000576
Owen Andersonc2666002010-12-13 19:31:11 +0000577 return Val;
578}
579
Bill Wendlingdff2f712010-12-08 23:01:43 +0000580/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
581/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000582uint32_t ARMMCCodeEmitter::
583getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
584 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000585 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
586 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
587 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000588}
589
Owen Andersona838a252010-12-14 00:36:49 +0000590/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
591/// target.
592uint32_t ARMMCCodeEmitter::
593getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
594 SmallVectorImpl<MCFixup> &Fixups) const {
595 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
596 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
597 Fixups);
598}
599
Jim Grosbachd40963c2010-12-14 22:28:03 +0000600/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
601/// target.
602uint32_t ARMMCCodeEmitter::
603getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
604 SmallVectorImpl<MCFixup> &Fixups) const {
605 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
606 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
607 Fixups);
608}
609
Bill Wendlingf4caf692010-12-14 03:36:38 +0000610/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
611/// operand.
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000612uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000613getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
614 SmallVectorImpl<MCFixup> &) const {
615 // [Rn, Rm]
616 // {5-3} = Rm
617 // {2-0} = Rn
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000618 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000619 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000620 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
621 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
622 return (Rm << 3) | Rn;
623}
624
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000625/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000626uint32_t ARMMCCodeEmitter::
627getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
628 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000629 // {17-13} = reg
630 // {12} = (U)nsigned (add == '1', sub == '0')
631 // {11-0} = imm12
632 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000633 bool isAdd = true;
634 // If The first operand isn't a register, we have a label reference.
635 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Anderson971b83b2011-02-08 22:39:40 +0000636 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000637 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000638 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000639 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000640
Owen Anderson971b83b2011-02-08 22:39:40 +0000641 assert(MO.isExpr() && "Unexpected machine operand type!");
642 const MCExpr *Expr = MO.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000643
Owen Andersond7b3f582010-12-09 01:51:07 +0000644 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000645 if (isThumb2())
Owen Andersond7b3f582010-12-09 01:51:07 +0000646 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
647 else
648 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000649 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
650
651 ++MCNumCPRelocations;
652 } else
653 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000654
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000655 uint32_t Binary = Imm12 & 0xfff;
656 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000657 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000658 Binary |= (1 << 12);
659 Binary |= (Reg << 13);
660 return Binary;
661}
662
Owen Anderson9d63d902010-12-01 19:18:46 +0000663/// getT2AddrModeImm8s4OpValue - Return encoding info for
664/// 'reg +/- imm8<<2' operand.
665uint32_t ARMMCCodeEmitter::
666getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
667 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000668 // {12-9} = reg
669 // {8} = (U)nsigned (add == '1', sub == '0')
670 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000671 unsigned Reg, Imm8;
672 bool isAdd = true;
673 // If The first operand isn't a register, we have a label reference.
674 const MCOperand &MO = MI.getOperand(OpIdx);
675 if (!MO.isReg()) {
676 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
677 Imm8 = 0;
678 isAdd = false ; // 'U' bit is set as part of the fixup.
679
680 assert(MO.isExpr() && "Unexpected machine operand type!");
681 const MCExpr *Expr = MO.getExpr();
682 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
683 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
684
685 ++MCNumCPRelocations;
686 } else
687 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
688
689 uint32_t Binary = (Imm8 >> 2) & 0xff;
690 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
691 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000692 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000693 Binary |= (Reg << 9);
694 return Binary;
695}
696
Jason W Kim86a97f22011-01-12 00:19:25 +0000697// FIXME: This routine assumes that a binary
698// expression will always result in a PCRel expression
699// In reality, its only true if one or more subexpressions
700// is itself a PCRel (i.e. "." in asm or some other pcrel construct)
701// but this is good enough for now.
702static bool EvaluateAsPCRel(const MCExpr *Expr) {
703 switch (Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000704 default: assert(0 && "Unexpected expression type");
Jason W Kim86a97f22011-01-12 00:19:25 +0000705 case MCExpr::SymbolRef: return false;
706 case MCExpr::Binary: return true;
Jason W Kim86a97f22011-01-12 00:19:25 +0000707 }
708}
709
Evan Cheng75972122011-01-13 07:58:56 +0000710uint32_t
711ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
712 SmallVectorImpl<MCFixup> &Fixups) const {
Jason W Kim837caa92010-11-18 23:37:15 +0000713 // {20-16} = imm{15-12}
714 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000715 const MCOperand &MO = MI.getOperand(OpIdx);
Evan Cheng75972122011-01-13 07:58:56 +0000716 if (MO.isImm())
717 // Hi / lo 16 bits already extracted during earlier passes.
Jason W Kim837caa92010-11-18 23:37:15 +0000718 return static_cast<unsigned>(MO.getImm());
Evan Cheng75972122011-01-13 07:58:56 +0000719
720 // Handle :upper16: and :lower16: assembly prefixes.
721 const MCExpr *E = MO.getExpr();
722 if (E->getKind() == MCExpr::Target) {
723 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
724 E = ARM16Expr->getSubExpr();
725
Jason W Kim837caa92010-11-18 23:37:15 +0000726 MCFixupKind Kind;
Evan Cheng75972122011-01-13 07:58:56 +0000727 switch (ARM16Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000728 default: assert(0 && "Unsupported ARMFixup");
Evan Cheng75972122011-01-13 07:58:56 +0000729 case ARMMCExpr::VK_ARM_HI16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000730 if (!isTargetDarwin() && EvaluateAsPCRel(E))
731 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000732 ? ARM::fixup_t2_movt_hi16_pcrel
733 : ARM::fixup_arm_movt_hi16_pcrel);
734 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000735 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000736 ? ARM::fixup_t2_movt_hi16
737 : ARM::fixup_arm_movt_hi16);
Jason W Kim837caa92010-11-18 23:37:15 +0000738 break;
Evan Cheng75972122011-01-13 07:58:56 +0000739 case ARMMCExpr::VK_ARM_LO16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000740 if (!isTargetDarwin() && EvaluateAsPCRel(E))
741 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000742 ? ARM::fixup_t2_movw_lo16_pcrel
743 : ARM::fixup_arm_movw_lo16_pcrel);
744 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000745 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000746 ? ARM::fixup_t2_movw_lo16
747 : ARM::fixup_arm_movw_lo16);
Jason W Kim837caa92010-11-18 23:37:15 +0000748 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000749 }
Evan Cheng75972122011-01-13 07:58:56 +0000750 Fixups.push_back(MCFixup::Create(0, E, Kind));
Jason W Kim837caa92010-11-18 23:37:15 +0000751 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000752 };
Evan Cheng75972122011-01-13 07:58:56 +0000753
Jim Grosbach817c1a62010-11-19 00:27:09 +0000754 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000755 return 0;
756}
757
758uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000759getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
760 SmallVectorImpl<MCFixup> &Fixups) const {
761 const MCOperand &MO = MI.getOperand(OpIdx);
762 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
763 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
764 unsigned Rn = getARMRegisterNumbering(MO.getReg());
765 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000766 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
767 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000768 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
769 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000770
771 // {16-13} = Rn
772 // {12} = isAdd
773 // {11-0} = shifter
774 // {3-0} = Rm
775 // {4} = 0
776 // {6-5} = type
777 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000778 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000779 Binary |= Rn << 13;
780 Binary |= SBits << 5;
781 Binary |= ShImm << 7;
782 if (isAdd)
783 Binary |= 1 << 12;
784 return Binary;
785}
786
Jim Grosbach570a9222010-11-11 01:09:40 +0000787uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000788getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
789 SmallVectorImpl<MCFixup> &Fixups) const {
790 // {17-14} Rn
791 // {13} 1 == imm12, 0 == Rm
792 // {12} isAdd
793 // {11-0} imm12/Rm
794 const MCOperand &MO = MI.getOperand(OpIdx);
795 unsigned Rn = getARMRegisterNumbering(MO.getReg());
796 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
797 Binary |= Rn << 14;
798 return Binary;
799}
800
801uint32_t ARMMCCodeEmitter::
802getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
803 SmallVectorImpl<MCFixup> &Fixups) const {
804 // {13} 1 == imm12, 0 == Rm
805 // {12} isAdd
806 // {11-0} imm12/Rm
807 const MCOperand &MO = MI.getOperand(OpIdx);
808 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
809 unsigned Imm = MO1.getImm();
810 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
811 bool isReg = MO.getReg() != 0;
812 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
813 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
814 if (isReg) {
815 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
816 Binary <<= 7; // Shift amount is bits [11:7]
817 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
818 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
819 }
820 return Binary | (isAdd << 12) | (isReg << 13);
821}
822
823uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000824getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
825 SmallVectorImpl<MCFixup> &Fixups) const {
826 // {9} 1 == imm8, 0 == Rm
827 // {8} isAdd
828 // {7-4} imm7_4/zero
829 // {3-0} imm3_0/Rm
830 const MCOperand &MO = MI.getOperand(OpIdx);
831 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
832 unsigned Imm = MO1.getImm();
833 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
834 bool isImm = MO.getReg() == 0;
835 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
836 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
837 if (!isImm)
838 Imm8 = getARMRegisterNumbering(MO.getReg());
839 return Imm8 | (isAdd << 8) | (isImm << 9);
840}
841
842uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000843getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
844 SmallVectorImpl<MCFixup> &Fixups) const {
845 // {13} 1 == imm8, 0 == Rm
846 // {12-9} Rn
847 // {8} isAdd
848 // {7-4} imm7_4/zero
849 // {3-0} imm3_0/Rm
850 const MCOperand &MO = MI.getOperand(OpIdx);
851 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
852 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
853 unsigned Rn = getARMRegisterNumbering(MO.getReg());
854 unsigned Imm = MO2.getImm();
855 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
856 bool isImm = MO1.getReg() == 0;
857 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
858 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
859 if (!isImm)
860 Imm8 = getARMRegisterNumbering(MO1.getReg());
861 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
862}
863
Bill Wendlingb8958b02010-12-08 01:57:09 +0000864/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000865uint32_t ARMMCCodeEmitter::
866getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
867 SmallVectorImpl<MCFixup> &Fixups) const {
868 // [SP, #imm]
869 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000870 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000871 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
872 "Unexpected base register!");
Bill Wendling7a905a82010-12-15 23:32:27 +0000873
Jim Grosbachd967cd02010-12-07 21:50:47 +0000874 // The immediate is already shifted for the implicit zeroes, so no change
875 // here.
876 return MO1.getImm() & 0xff;
877}
878
Bill Wendlingf4caf692010-12-14 03:36:38 +0000879/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000880uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000881getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000882 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000883 // [Rn, #imm]
884 // {7-3} = imm5
885 // {2-0} = Rn
886 const MCOperand &MO = MI.getOperand(OpIdx);
887 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000888 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Matt Beaumont-Gay656b3d22010-12-16 01:34:26 +0000889 unsigned Imm5 = MO1.getImm();
Bill Wendling272df512010-12-09 21:49:07 +0000890 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000891}
892
Bill Wendlingb8958b02010-12-08 01:57:09 +0000893/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
894uint32_t ARMMCCodeEmitter::
895getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
896 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000897 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000898}
899
Jim Grosbach5177f792010-12-01 21:09:40 +0000900/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000901uint32_t ARMMCCodeEmitter::
902getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
903 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000904 // {12-9} = reg
905 // {8} = (U)nsigned (add == '1', sub == '0')
906 // {7-0} = imm8
907 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000908 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000909 // If The first operand isn't a register, we have a label reference.
910 const MCOperand &MO = MI.getOperand(OpIdx);
911 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000912 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000913 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000914 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000915
916 assert(MO.isExpr() && "Unexpected machine operand type!");
917 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000918 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000919 if (isThumb2())
Owen Andersond8e351b2010-12-08 00:18:36 +0000920 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
921 else
922 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000923 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
924
925 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000926 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000927 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000928 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
929 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000930
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000931 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
932 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000933 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000934 Binary |= (1 << 8);
935 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000936 return Binary;
937}
938
Jim Grosbach806e80e2010-11-03 23:52:49 +0000939unsigned ARMMCCodeEmitter::
940getSORegOpValue(const MCInst &MI, unsigned OpIdx,
941 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000942 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
943 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
944 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000945 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000946 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000947 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000948 // {6-5} = type
949 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000950 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000951 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000952 // else (imm shift)
953 // {11-7} = imm
954
955 const MCOperand &MO = MI.getOperand(OpIdx);
956 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
957 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
958 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
959
960 // Encode Rm.
961 unsigned Binary = getARMRegisterNumbering(MO.getReg());
962
963 // Encode the shift opcode.
964 unsigned SBits = 0;
965 unsigned Rs = MO1.getReg();
966 if (Rs) {
967 // Set shift operand (bit[7:4]).
968 // LSL - 0001
969 // LSR - 0011
970 // ASR - 0101
971 // ROR - 0111
972 // RRX - 0110 and bit[11:8] clear.
973 switch (SOpc) {
974 default: llvm_unreachable("Unknown shift opc!");
975 case ARM_AM::lsl: SBits = 0x1; break;
976 case ARM_AM::lsr: SBits = 0x3; break;
977 case ARM_AM::asr: SBits = 0x5; break;
978 case ARM_AM::ror: SBits = 0x7; break;
979 case ARM_AM::rrx: SBits = 0x6; break;
980 }
981 } else {
982 // Set shift operand (bit[6:4]).
983 // LSL - 000
984 // LSR - 010
985 // ASR - 100
986 // ROR - 110
987 switch (SOpc) {
988 default: llvm_unreachable("Unknown shift opc!");
989 case ARM_AM::lsl: SBits = 0x0; break;
990 case ARM_AM::lsr: SBits = 0x2; break;
991 case ARM_AM::asr: SBits = 0x4; break;
992 case ARM_AM::ror: SBits = 0x6; break;
993 }
994 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000995
Jim Grosbachef324d72010-10-12 23:53:58 +0000996 Binary |= SBits << 4;
997 if (SOpc == ARM_AM::rrx)
998 return Binary;
999
1000 // Encode the shift operation Rs or shift_imm (except rrx).
1001 if (Rs) {
1002 // Encode Rs bit[11:8].
1003 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1004 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
1005 }
1006
1007 // Encode shift_imm bit[11:7].
1008 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
1009}
1010
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::
Bruno Cardoso Lopes895c1e22011-05-31 03:33:27 +00001148getSsatBitPosValue(const MCInst &MI, unsigned Op,
1149 SmallVectorImpl<MCFixup> &Fixups) const {
1150 // For ssat instructions, the bit position should be encoded decremented by 1
1151 return MI.getOperand(Op).getImm()-1;
1152}
1153
1154unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001155getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001156 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001157 // VLDM/VSTM:
1158 // {12-8} = Vd
1159 // {7-0} = Number of registers
1160 //
1161 // LDM/STM:
1162 // {15-0} = Bitfield of GPRs.
1163 unsigned Reg = MI.getOperand(Op).getReg();
1164 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1165 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1166
Bill Wendling5e559a22010-11-09 00:30:18 +00001167 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001168
1169 if (SPRRegs || DPRRegs) {
1170 // VLDM/VSTM
1171 unsigned RegNo = getARMRegisterNumbering(Reg);
1172 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1173 Binary |= (RegNo & 0x1f) << 8;
1174 if (SPRRegs)
1175 Binary |= NumRegs;
1176 else
1177 Binary |= NumRegs * 2;
1178 } else {
1179 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1180 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1181 Binary |= 1 << RegNo;
1182 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001183 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001184
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001185 return Binary;
1186}
1187
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001188/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1189/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001190unsigned ARMMCCodeEmitter::
1191getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1192 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001193 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001194 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001195
Owen Andersond9aa7d32010-11-02 00:05:05 +00001196 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001197 unsigned Align = 0;
1198
1199 switch (Imm.getImm()) {
1200 default: break;
1201 case 2:
1202 case 4:
1203 case 8: Align = 0x01; break;
1204 case 16: Align = 0x02; break;
1205 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001206 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001207
Owen Andersond9aa7d32010-11-02 00:05:05 +00001208 return RegNo | (Align << 4);
1209}
1210
Mon P Wang183c6272011-05-09 17:47:27 +00001211/// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1212/// along with the alignment operand for use in VST1 and VLD1 with size 32.
1213unsigned ARMMCCodeEmitter::
1214getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1215 SmallVectorImpl<MCFixup> &Fixups) const {
1216 const MCOperand &Reg = MI.getOperand(Op);
1217 const MCOperand &Imm = MI.getOperand(Op + 1);
1218
1219 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1220 unsigned Align = 0;
1221
1222 switch (Imm.getImm()) {
1223 default: break;
1224 case 2:
1225 case 4:
1226 case 8:
1227 case 16: Align = 0x00; break;
1228 case 32: Align = 0x03; break;
1229 }
1230
1231 return RegNo | (Align << 4);
1232}
1233
1234
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001235/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1236/// alignment operand for use in VLD-dup instructions. This is the same as
1237/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1238/// different for VLD4-dup.
1239unsigned ARMMCCodeEmitter::
1240getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1241 SmallVectorImpl<MCFixup> &Fixups) const {
1242 const MCOperand &Reg = MI.getOperand(Op);
1243 const MCOperand &Imm = MI.getOperand(Op + 1);
1244
1245 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1246 unsigned Align = 0;
1247
1248 switch (Imm.getImm()) {
1249 default: break;
1250 case 2:
1251 case 4:
1252 case 8: Align = 0x01; break;
1253 case 16: Align = 0x03; break;
1254 }
1255
1256 return RegNo | (Align << 4);
1257}
1258
Jim Grosbach806e80e2010-11-03 23:52:49 +00001259unsigned ARMMCCodeEmitter::
1260getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1261 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001262 const MCOperand &MO = MI.getOperand(Op);
1263 if (MO.getReg() == 0) return 0x0D;
1264 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001265}
1266
Bill Wendlinga656b632011-03-01 01:00:59 +00001267unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001268getShiftRight8Imm(const MCInst &MI, unsigned Op,
1269 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001270 return 8 - MI.getOperand(Op).getImm();
1271}
1272
1273unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001274getShiftRight16Imm(const MCInst &MI, unsigned Op,
1275 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001276 return 16 - MI.getOperand(Op).getImm();
1277}
1278
1279unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001280getShiftRight32Imm(const MCInst &MI, unsigned Op,
1281 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001282 return 32 - MI.getOperand(Op).getImm();
1283}
1284
Bill Wendling3116dce2011-03-07 23:38:41 +00001285unsigned ARMMCCodeEmitter::
1286getShiftRight64Imm(const MCInst &MI, unsigned Op,
1287 SmallVectorImpl<MCFixup> &Fixups) const {
1288 return 64 - MI.getOperand(Op).getImm();
1289}
1290
Jim Grosbach568eeed2010-09-17 18:46:17 +00001291void ARMMCCodeEmitter::
1292EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001293 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001294 // Pseudo instructions don't get encoded.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001295 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001296 uint64_t TSFlags = Desc.TSFlags;
1297 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001298 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001299 int Size;
1300 // Basic size info comes from the TSFlags field.
1301 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1302 default: llvm_unreachable("Unexpected instruction size!");
1303 case ARMII::Size2Bytes: Size = 2; break;
1304 case ARMII::Size4Bytes: Size = 4; break;
1305 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001306 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
Evan Cheng75972122011-01-13 07:58:56 +00001307 // Thumb 32-bit wide instructions need to emit the high order halfword
1308 // first.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001309 if (isThumb() && Size == 4) {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001310 EmitConstant(Binary >> 16, 2, OS);
1311 EmitConstant(Binary & 0xffff, 2, OS);
1312 } else
1313 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001314 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001315}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001316
Jim Grosbach806e80e2010-11-03 23:52:49 +00001317#include "ARMGenMCCodeEmitter.inc"