blob: b92e02d047bd43f92e97dafdbf5a032604ee14f9 [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 MCContext &Ctx;
44
45public:
Evan Cheng59ee62d2011-07-11 03:57:24 +000046 ARMMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
47 MCContext &ctx)
48 : MCII(mcii), STI(sti), Ctx(ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000049 }
50
51 ~ARMMCCodeEmitter() {}
52
Evan Cheng59ee62d2011-07-11 03:57:24 +000053 bool isThumb() const {
54 // FIXME: Can tablegen auto-generate this?
55 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
56 }
57 bool isThumb2() const {
58 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) != 0;
59 }
60 bool isTargetDarwin() const {
61 Triple TT(STI.getTargetTriple());
62 Triple::OSType OS = TT.getOS();
63 return OS == Triple::Darwin || OS == Triple::MacOSX || OS == Triple::IOS;
64 }
65
Jim Grosbach0de6ab32010-10-12 17:11:26 +000066 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
67
Jim Grosbach9af82ba2010-10-07 21:57:55 +000068 // getBinaryCodeForInstr - TableGen'erated function for getting the
69 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000070 unsigned getBinaryCodeForInstr(const MCInst &MI,
71 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000072
73 /// getMachineOpValue - Return binary encoding of operand. If the machine
74 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000075 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
76 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000077
Evan Cheng75972122011-01-13 07:58:56 +000078 /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
Owen Anderson971b83b2011-02-08 22:39:40 +000079 /// the specified operand. This is used for operands with :lower16: and
Evan Cheng75972122011-01-13 07:58:56 +000080 /// :upper16: prefixes.
81 uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
82 SmallVectorImpl<MCFixup> &Fixups) const;
Jason W Kim837caa92010-11-18 23:37:15 +000083
Bill Wendling92b5a2e2010-11-03 01:49:29 +000084 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000085 unsigned &Reg, unsigned &Imm,
86 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000087
Jim Grosbach662a8162010-12-06 23:57:07 +000088 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +000089 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +000090 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
91 SmallVectorImpl<MCFixup> &Fixups) const;
92
Bill Wendling09aa3f02010-12-09 00:39:08 +000093 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
94 /// BLX branch target.
95 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
96 SmallVectorImpl<MCFixup> &Fixups) const;
97
Jim Grosbache2467172010-12-10 18:21:33 +000098 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
99 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
100 SmallVectorImpl<MCFixup> &Fixups) const;
101
Jim Grosbach01086452010-12-10 17:13:40 +0000102 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
103 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
104 SmallVectorImpl<MCFixup> &Fixups) const;
105
Jim Grosbach027d6e82010-12-09 19:04:53 +0000106 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
107 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000108 SmallVectorImpl<MCFixup> &Fixups) const;
109
Jim Grosbachc466b932010-11-11 18:04:49 +0000110 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
111 /// branch target.
112 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
113 SmallVectorImpl<MCFixup> &Fixups) const;
114
Owen Andersonc2666002010-12-13 19:31:11 +0000115 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
116 /// immediate Thumb2 direct branch target.
117 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
118 SmallVectorImpl<MCFixup> &Fixups) const;
119
Jason W Kim685c3502011-02-04 19:47:15 +0000120 /// getARMBranchTargetOpValue - Return encoding info for 24-bit immediate
121 /// branch target.
122 uint32_t getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
123 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersonc2666002010-12-13 19:31:11 +0000124
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000125 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
126 /// ADR label target.
127 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
128 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachd40963c2010-12-14 22:28:03 +0000129 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
130 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersona838a252010-12-14 00:36:49 +0000131 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
132 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson971b83b2011-02-08 22:39:40 +0000133
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000134
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000135 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
136 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000137 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
138 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000139
Bill Wendlingf4caf692010-12-14 03:36:38 +0000140 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
141 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
142 SmallVectorImpl<MCFixup> &Fixups)const;
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000143
Owen Anderson9d63d902010-12-01 19:18:46 +0000144 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
145 /// operand.
146 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
147 SmallVectorImpl<MCFixup> &Fixups) const;
148
149
Jim Grosbach54fea632010-11-09 17:20:53 +0000150 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
151 /// operand as needed by load/store instructions.
152 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
153 SmallVectorImpl<MCFixup> &Fixups) const;
154
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000155 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
156 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
157 SmallVectorImpl<MCFixup> &Fixups) const {
158 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
159 switch (Mode) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000160 default: assert(0 && "Unknown addressing sub-mode!");
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000161 case ARM_AM::da: return 0;
162 case ARM_AM::ia: return 1;
163 case ARM_AM::db: return 2;
164 case ARM_AM::ib: return 3;
165 }
166 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000167 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
168 ///
169 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
170 switch (ShOpc) {
171 default: llvm_unreachable("Unknown shift opc!");
172 case ARM_AM::no_shift:
173 case ARM_AM::lsl: return 0;
174 case ARM_AM::lsr: return 1;
175 case ARM_AM::asr: return 2;
176 case ARM_AM::ror:
177 case ARM_AM::rrx: return 3;
178 }
179 return 0;
180 }
181
182 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
183 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
184 SmallVectorImpl<MCFixup> &Fixups) const;
185
186 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
187 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
188 SmallVectorImpl<MCFixup> &Fixups) const;
189
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000190 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
191 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
192 SmallVectorImpl<MCFixup> &Fixups) const;
193
Jim Grosbach570a9222010-11-11 01:09:40 +0000194 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
195 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
196 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000197
Jim Grosbachd967cd02010-12-07 21:50:47 +0000198 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
199 /// operand.
200 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
201 SmallVectorImpl<MCFixup> &Fixups) const;
202
Bill Wendlingf4caf692010-12-14 03:36:38 +0000203 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
204 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000205 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000206
Bill Wendlingb8958b02010-12-08 01:57:09 +0000207 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
208 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
209 SmallVectorImpl<MCFixup> &Fixups) const;
210
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000211 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000212 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
213 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000214
Jim Grosbach08bd5492010-10-12 23:00:24 +0000215 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000216 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
217 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000218 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
219 // '1' respectively.
220 return MI.getOperand(Op).getReg() == ARM::CPSR;
221 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000222
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000223 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000224 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
225 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000226 unsigned SoImm = MI.getOperand(Op).getImm();
227 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
228 assert(SoImmVal != -1 && "Not a valid so_imm value!");
229
230 // Encode rotate_imm.
231 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
232 << ARMII::SoRotImmShift;
233
234 // Encode immed_8.
235 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
236 return Binary;
237 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000238
Owen Anderson5de6d842010-11-12 21:12:40 +0000239 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
240 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
241 SmallVectorImpl<MCFixup> &Fixups) const {
242 unsigned SoImm = MI.getOperand(Op).getImm();
243 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
244 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
245 return Encoded;
246 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000247
Owen Anderson75579f72010-11-29 22:44:32 +0000248 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
249 SmallVectorImpl<MCFixup> &Fixups) const;
250 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
251 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000252 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
253 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000254 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
255 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000256
Jim Grosbachef324d72010-10-12 23:53:58 +0000257 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000258 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
259 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000260 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
261 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000262
Jim Grosbach806e80e2010-11-03 23:52:49 +0000263 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
264 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000265 switch (MI.getOperand(Op).getImm()) {
266 default: assert (0 && "Not a valid rot_imm value!");
267 case 0: return 0;
268 case 8: return 1;
269 case 16: return 2;
270 case 24: return 3;
271 }
272 }
273
Jim Grosbach806e80e2010-11-03 23:52:49 +0000274 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
275 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000276 return MI.getOperand(Op).getImm() - 1;
277 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000278
Jim Grosbach806e80e2010-11-03 23:52:49 +0000279 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
280 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000281 return 64 - MI.getOperand(Op).getImm();
282 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000283
Jim Grosbach806e80e2010-11-03 23:52:49 +0000284 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
285 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000286
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +0000287 unsigned getMsbOpValue(const MCInst &MI, unsigned Op,
288 SmallVectorImpl<MCFixup> &Fixups) const;
289
Bruno Cardoso Lopes895c1e22011-05-31 03:33:27 +0000290 unsigned getSsatBitPosValue(const MCInst &MI, unsigned Op,
291 SmallVectorImpl<MCFixup> &Fixups) const;
292
Jim Grosbach806e80e2010-11-03 23:52:49 +0000293 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
294 SmallVectorImpl<MCFixup> &Fixups) const;
295 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
296 SmallVectorImpl<MCFixup> &Fixups) const;
Mon P Wang183c6272011-05-09 17:47:27 +0000297 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
298 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000299 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
300 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000301 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
302 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000303
Bill Wendling3116dce2011-03-07 23:38:41 +0000304 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
305 SmallVectorImpl<MCFixup> &Fixups) const;
306 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
307 SmallVectorImpl<MCFixup> &Fixups) const;
308 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
309 SmallVectorImpl<MCFixup> &Fixups) const;
310 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
311 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendlinga656b632011-03-01 01:00:59 +0000312
Owen Andersonc7139a62010-11-11 19:07:48 +0000313 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
314 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000315 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000316 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000317 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000318 unsigned EncodedValue) const;
319
320 unsigned VFPThumb2PostEncoder(const MCInst &MI,
321 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000322
Jim Grosbach70933262010-11-04 01:12:30 +0000323 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000324 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000325 }
326
Jim Grosbach70933262010-11-04 01:12:30 +0000327 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000328 // Output the constant in little endian byte order.
329 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000330 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000331 Val >>= 8;
332 }
333 }
334
Jim Grosbach568eeed2010-09-17 18:46:17 +0000335 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
336 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000337};
338
339} // end anonymous namespace
340
Evan Cheng59ee62d2011-07-11 03:57:24 +0000341MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
342 const MCSubtargetInfo &STI,
Bill Wendling0800ce72010-11-02 22:53:11 +0000343 MCContext &Ctx) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000344 return new ARMMCCodeEmitter(MCII, STI, Ctx);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000345}
346
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000347/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
348/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000349/// Thumb2 mode.
350unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
351 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000352 if (isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000353 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000354 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
355 // set to 1111.
356 unsigned Bit24 = EncodedValue & 0x01000000;
357 unsigned Bit28 = Bit24 << 4;
358 EncodedValue &= 0xEFFFFFFF;
359 EncodedValue |= Bit28;
360 EncodedValue |= 0x0F000000;
361 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000362
Owen Andersonc7139a62010-11-11 19:07:48 +0000363 return EncodedValue;
364}
365
Owen Anderson57dac882010-11-11 21:36:43 +0000366/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000367/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000368/// Thumb2 mode.
369unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
370 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000371 if (isThumb2()) {
Owen Anderson57dac882010-11-11 21:36:43 +0000372 EncodedValue &= 0xF0FFFFFF;
373 EncodedValue |= 0x09000000;
374 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000375
Owen Anderson57dac882010-11-11 21:36:43 +0000376 return EncodedValue;
377}
378
Owen Anderson8f143912010-11-11 23:12:55 +0000379/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000380/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000381/// Thumb2 mode.
382unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
383 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000384 if (isThumb2()) {
Owen Anderson8f143912010-11-11 23:12:55 +0000385 EncodedValue &= 0x00FFFFFF;
386 EncodedValue |= 0xEE000000;
387 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000388
Owen Anderson8f143912010-11-11 23:12:55 +0000389 return EncodedValue;
390}
391
Bill Wendlingcf590262010-12-01 21:54:50 +0000392/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
393/// them to their Thumb2 form if we are currently in Thumb2 mode.
394unsigned ARMMCCodeEmitter::
395VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000396 if (isThumb2()) {
Bill Wendlingcf590262010-12-01 21:54:50 +0000397 EncodedValue &= 0x0FFFFFFF;
398 EncodedValue |= 0xE0000000;
399 }
400 return EncodedValue;
401}
Owen Anderson57dac882010-11-11 21:36:43 +0000402
Jim Grosbach56ac9072010-10-08 21:45:55 +0000403/// getMachineOpValue - Return binary encoding of operand. If the machine
404/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000405unsigned ARMMCCodeEmitter::
406getMachineOpValue(const MCInst &MI, const MCOperand &MO,
407 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000408 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000409 unsigned Reg = MO.getReg();
410 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000411
Jim Grosbachb0708d22010-11-30 23:51:41 +0000412 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000413 switch (Reg) {
414 default:
415 return RegNo;
416 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
417 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
418 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
419 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
420 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000421 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000422 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000423 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000424 } else if (MO.isFPImm()) {
425 return static_cast<unsigned>(APFloat(MO.getFPImm())
426 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000427 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000428
Jim Grosbach817c1a62010-11-19 00:27:09 +0000429 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000430 return 0;
431}
432
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000433/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000434bool ARMMCCodeEmitter::
435EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
436 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000437 const MCOperand &MO = MI.getOperand(OpIdx);
438 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000439
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000440 Reg = getARMRegisterNumbering(MO.getReg());
441
442 int32_t SImm = MO1.getImm();
443 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000444
Jim Grosbachab682a22010-10-28 18:34:10 +0000445 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000446 if (SImm == INT32_MIN)
447 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000448
Jim Grosbachab682a22010-10-28 18:34:10 +0000449 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000450 if (SImm < 0) {
451 SImm = -SImm;
452 isAdd = false;
453 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000454
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000455 Imm = SImm;
456 return isAdd;
457}
458
Bill Wendlingdff2f712010-12-08 23:01:43 +0000459/// getBranchTargetOpValue - Helper function to get the branch target operand,
460/// which is either an immediate or requires a fixup.
461static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
462 unsigned FixupKind,
463 SmallVectorImpl<MCFixup> &Fixups) {
464 const MCOperand &MO = MI.getOperand(OpIdx);
465
466 // If the destination is an immediate, we have nothing to do.
467 if (MO.isImm()) return MO.getImm();
468 assert(MO.isExpr() && "Unexpected branch target type!");
469 const MCExpr *Expr = MO.getExpr();
470 MCFixupKind Kind = MCFixupKind(FixupKind);
471 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
472
473 // All of the information is in the fixup.
474 return 0;
475}
476
477/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000478uint32_t ARMMCCodeEmitter::
479getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
480 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000481 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000482}
483
Bill Wendling09aa3f02010-12-09 00:39:08 +0000484/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
485/// BLX branch target.
486uint32_t ARMMCCodeEmitter::
487getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
488 SmallVectorImpl<MCFixup> &Fixups) const {
489 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
490}
491
Jim Grosbache2467172010-12-10 18:21:33 +0000492/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
493uint32_t ARMMCCodeEmitter::
494getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
495 SmallVectorImpl<MCFixup> &Fixups) const {
496 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
497}
498
Jim Grosbach01086452010-12-10 17:13:40 +0000499/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
500uint32_t ARMMCCodeEmitter::
501getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000502 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000503 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
504}
505
Jim Grosbach027d6e82010-12-09 19:04:53 +0000506/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000507uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000508getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000509 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000510 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000511}
512
Jason W Kim685c3502011-02-04 19:47:15 +0000513/// Return true if this branch has a non-always predication
514static bool HasConditionalBranch(const MCInst &MI) {
515 int NumOp = MI.getNumOperands();
516 if (NumOp >= 2) {
517 for (int i = 0; i < NumOp-1; ++i) {
518 const MCOperand &MCOp1 = MI.getOperand(i);
519 const MCOperand &MCOp2 = MI.getOperand(i + 1);
520 if (MCOp1.isImm() && MCOp2.isReg() &&
521 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
522 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
523 return true;
524 }
525 }
526 }
527 return false;
528}
529
Bill Wendlingdff2f712010-12-08 23:01:43 +0000530/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
531/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000532uint32_t ARMMCCodeEmitter::
533getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000534 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000535 // FIXME: This really, really shouldn't use TargetMachine. We don't want
536 // coupling between MC and TM anywhere we can help it.
Evan Cheng59ee62d2011-07-11 03:57:24 +0000537 if (isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000538 return
539 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Jason W Kim685c3502011-02-04 19:47:15 +0000540 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000541}
542
Jason W Kim685c3502011-02-04 19:47:15 +0000543/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
544/// target.
545uint32_t ARMMCCodeEmitter::
546getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
547 SmallVectorImpl<MCFixup> &Fixups) const {
548 if (HasConditionalBranch(MI))
549 return ::getBranchTargetOpValue(MI, OpIdx,
550 ARM::fixup_arm_condbranch, Fixups);
551 return ::getBranchTargetOpValue(MI, OpIdx,
552 ARM::fixup_arm_uncondbranch, Fixups);
553}
554
555
556
557
Owen Andersonc2666002010-12-13 19:31:11 +0000558/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
559/// immediate branch target.
560uint32_t ARMMCCodeEmitter::
561getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
562 SmallVectorImpl<MCFixup> &Fixups) const {
563 unsigned Val =
564 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
565 bool I = (Val & 0x800000);
566 bool J1 = (Val & 0x400000);
567 bool J2 = (Val & 0x200000);
568 if (I ^ J1)
569 Val &= ~0x400000;
570 else
571 Val |= 0x400000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000572
Owen Andersonc2666002010-12-13 19:31:11 +0000573 if (I ^ J2)
574 Val &= ~0x200000;
575 else
576 Val |= 0x200000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000577
Owen Andersonc2666002010-12-13 19:31:11 +0000578 return Val;
579}
580
Bill Wendlingdff2f712010-12-08 23:01:43 +0000581/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
582/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000583uint32_t ARMMCCodeEmitter::
584getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
585 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000586 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
587 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
588 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000589}
590
Owen Andersona838a252010-12-14 00:36:49 +0000591/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
592/// target.
593uint32_t ARMMCCodeEmitter::
594getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
595 SmallVectorImpl<MCFixup> &Fixups) const {
596 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
597 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
598 Fixups);
599}
600
Jim Grosbachd40963c2010-12-14 22:28:03 +0000601/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
602/// target.
603uint32_t ARMMCCodeEmitter::
604getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
605 SmallVectorImpl<MCFixup> &Fixups) const {
606 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
607 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
608 Fixups);
609}
610
Bill Wendlingf4caf692010-12-14 03:36:38 +0000611/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
612/// operand.
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000613uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000614getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
615 SmallVectorImpl<MCFixup> &) const {
616 // [Rn, Rm]
617 // {5-3} = Rm
618 // {2-0} = Rn
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000619 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000620 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000621 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
622 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
623 return (Rm << 3) | Rn;
624}
625
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000626/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000627uint32_t ARMMCCodeEmitter::
628getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
629 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000630 // {17-13} = reg
631 // {12} = (U)nsigned (add == '1', sub == '0')
632 // {11-0} = imm12
633 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000634 bool isAdd = true;
635 // If The first operand isn't a register, we have a label reference.
636 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Anderson971b83b2011-02-08 22:39:40 +0000637 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000638 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000639 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000640 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000641
Owen Anderson971b83b2011-02-08 22:39:40 +0000642 assert(MO.isExpr() && "Unexpected machine operand type!");
643 const MCExpr *Expr = MO.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000644
Owen Andersond7b3f582010-12-09 01:51:07 +0000645 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000646 if (isThumb2())
Owen Andersond7b3f582010-12-09 01:51:07 +0000647 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
648 else
649 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000650 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
651
652 ++MCNumCPRelocations;
653 } else
654 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000655
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000656 uint32_t Binary = Imm12 & 0xfff;
657 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000658 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000659 Binary |= (1 << 12);
660 Binary |= (Reg << 13);
661 return Binary;
662}
663
Owen Anderson9d63d902010-12-01 19:18:46 +0000664/// getT2AddrModeImm8s4OpValue - Return encoding info for
665/// 'reg +/- imm8<<2' operand.
666uint32_t ARMMCCodeEmitter::
667getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
668 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000669 // {12-9} = reg
670 // {8} = (U)nsigned (add == '1', sub == '0')
671 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000672 unsigned Reg, Imm8;
673 bool isAdd = true;
674 // If The first operand isn't a register, we have a label reference.
675 const MCOperand &MO = MI.getOperand(OpIdx);
676 if (!MO.isReg()) {
677 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
678 Imm8 = 0;
679 isAdd = false ; // 'U' bit is set as part of the fixup.
680
681 assert(MO.isExpr() && "Unexpected machine operand type!");
682 const MCExpr *Expr = MO.getExpr();
683 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
684 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
685
686 ++MCNumCPRelocations;
687 } else
688 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
689
690 uint32_t Binary = (Imm8 >> 2) & 0xff;
691 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
692 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000693 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000694 Binary |= (Reg << 9);
695 return Binary;
696}
697
Jason W Kim86a97f22011-01-12 00:19:25 +0000698// FIXME: This routine assumes that a binary
699// expression will always result in a PCRel expression
700// In reality, its only true if one or more subexpressions
701// is itself a PCRel (i.e. "." in asm or some other pcrel construct)
702// but this is good enough for now.
703static bool EvaluateAsPCRel(const MCExpr *Expr) {
704 switch (Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000705 default: assert(0 && "Unexpected expression type");
Jason W Kim86a97f22011-01-12 00:19:25 +0000706 case MCExpr::SymbolRef: return false;
707 case MCExpr::Binary: return true;
Jason W Kim86a97f22011-01-12 00:19:25 +0000708 }
709}
710
Evan Cheng75972122011-01-13 07:58:56 +0000711uint32_t
712ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
713 SmallVectorImpl<MCFixup> &Fixups) const {
Jason W Kim837caa92010-11-18 23:37:15 +0000714 // {20-16} = imm{15-12}
715 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000716 const MCOperand &MO = MI.getOperand(OpIdx);
Evan Cheng75972122011-01-13 07:58:56 +0000717 if (MO.isImm())
718 // Hi / lo 16 bits already extracted during earlier passes.
Jason W Kim837caa92010-11-18 23:37:15 +0000719 return static_cast<unsigned>(MO.getImm());
Evan Cheng75972122011-01-13 07:58:56 +0000720
721 // Handle :upper16: and :lower16: assembly prefixes.
722 const MCExpr *E = MO.getExpr();
723 if (E->getKind() == MCExpr::Target) {
724 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
725 E = ARM16Expr->getSubExpr();
726
Jason W Kim837caa92010-11-18 23:37:15 +0000727 MCFixupKind Kind;
Evan Cheng75972122011-01-13 07:58:56 +0000728 switch (ARM16Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000729 default: assert(0 && "Unsupported ARMFixup");
Evan Cheng75972122011-01-13 07:58:56 +0000730 case ARMMCExpr::VK_ARM_HI16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000731 if (!isTargetDarwin() && EvaluateAsPCRel(E))
732 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000733 ? ARM::fixup_t2_movt_hi16_pcrel
734 : ARM::fixup_arm_movt_hi16_pcrel);
735 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000736 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000737 ? ARM::fixup_t2_movt_hi16
738 : ARM::fixup_arm_movt_hi16);
Jason W Kim837caa92010-11-18 23:37:15 +0000739 break;
Evan Cheng75972122011-01-13 07:58:56 +0000740 case ARMMCExpr::VK_ARM_LO16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000741 if (!isTargetDarwin() && EvaluateAsPCRel(E))
742 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000743 ? ARM::fixup_t2_movw_lo16_pcrel
744 : ARM::fixup_arm_movw_lo16_pcrel);
745 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000746 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000747 ? ARM::fixup_t2_movw_lo16
748 : ARM::fixup_arm_movw_lo16);
Jason W Kim837caa92010-11-18 23:37:15 +0000749 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000750 }
Evan Cheng75972122011-01-13 07:58:56 +0000751 Fixups.push_back(MCFixup::Create(0, E, Kind));
Jason W Kim837caa92010-11-18 23:37:15 +0000752 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000753 };
Evan Cheng75972122011-01-13 07:58:56 +0000754
Jim Grosbach817c1a62010-11-19 00:27:09 +0000755 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000756 return 0;
757}
758
759uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000760getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
761 SmallVectorImpl<MCFixup> &Fixups) const {
762 const MCOperand &MO = MI.getOperand(OpIdx);
763 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
764 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
765 unsigned Rn = getARMRegisterNumbering(MO.getReg());
766 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000767 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
768 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000769 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
770 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000771
772 // {16-13} = Rn
773 // {12} = isAdd
774 // {11-0} = shifter
775 // {3-0} = Rm
776 // {4} = 0
777 // {6-5} = type
778 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000779 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000780 Binary |= Rn << 13;
781 Binary |= SBits << 5;
782 Binary |= ShImm << 7;
783 if (isAdd)
784 Binary |= 1 << 12;
785 return Binary;
786}
787
Jim Grosbach570a9222010-11-11 01:09:40 +0000788uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000789getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
790 SmallVectorImpl<MCFixup> &Fixups) const {
791 // {17-14} Rn
792 // {13} 1 == imm12, 0 == Rm
793 // {12} isAdd
794 // {11-0} imm12/Rm
795 const MCOperand &MO = MI.getOperand(OpIdx);
796 unsigned Rn = getARMRegisterNumbering(MO.getReg());
797 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
798 Binary |= Rn << 14;
799 return Binary;
800}
801
802uint32_t ARMMCCodeEmitter::
803getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
804 SmallVectorImpl<MCFixup> &Fixups) const {
805 // {13} 1 == imm12, 0 == Rm
806 // {12} isAdd
807 // {11-0} imm12/Rm
808 const MCOperand &MO = MI.getOperand(OpIdx);
809 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
810 unsigned Imm = MO1.getImm();
811 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
812 bool isReg = MO.getReg() != 0;
813 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
814 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
815 if (isReg) {
816 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
817 Binary <<= 7; // Shift amount is bits [11:7]
818 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
819 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
820 }
821 return Binary | (isAdd << 12) | (isReg << 13);
822}
823
824uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000825getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
826 SmallVectorImpl<MCFixup> &Fixups) const {
827 // {9} 1 == imm8, 0 == Rm
828 // {8} isAdd
829 // {7-4} imm7_4/zero
830 // {3-0} imm3_0/Rm
831 const MCOperand &MO = MI.getOperand(OpIdx);
832 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
833 unsigned Imm = MO1.getImm();
834 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
835 bool isImm = MO.getReg() == 0;
836 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
837 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
838 if (!isImm)
839 Imm8 = getARMRegisterNumbering(MO.getReg());
840 return Imm8 | (isAdd << 8) | (isImm << 9);
841}
842
843uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000844getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
845 SmallVectorImpl<MCFixup> &Fixups) const {
846 // {13} 1 == imm8, 0 == Rm
847 // {12-9} Rn
848 // {8} isAdd
849 // {7-4} imm7_4/zero
850 // {3-0} imm3_0/Rm
851 const MCOperand &MO = MI.getOperand(OpIdx);
852 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
853 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
854 unsigned Rn = getARMRegisterNumbering(MO.getReg());
855 unsigned Imm = MO2.getImm();
856 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
857 bool isImm = MO1.getReg() == 0;
858 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
859 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
860 if (!isImm)
861 Imm8 = getARMRegisterNumbering(MO1.getReg());
862 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
863}
864
Bill Wendlingb8958b02010-12-08 01:57:09 +0000865/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000866uint32_t ARMMCCodeEmitter::
867getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
868 SmallVectorImpl<MCFixup> &Fixups) const {
869 // [SP, #imm]
870 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000871 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000872 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
873 "Unexpected base register!");
Bill Wendling7a905a82010-12-15 23:32:27 +0000874
Jim Grosbachd967cd02010-12-07 21:50:47 +0000875 // The immediate is already shifted for the implicit zeroes, so no change
876 // here.
877 return MO1.getImm() & 0xff;
878}
879
Bill Wendlingf4caf692010-12-14 03:36:38 +0000880/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000881uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000882getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000883 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000884 // [Rn, #imm]
885 // {7-3} = imm5
886 // {2-0} = Rn
887 const MCOperand &MO = MI.getOperand(OpIdx);
888 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000889 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Matt Beaumont-Gay656b3d22010-12-16 01:34:26 +0000890 unsigned Imm5 = MO1.getImm();
Bill Wendling272df512010-12-09 21:49:07 +0000891 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000892}
893
Bill Wendlingb8958b02010-12-08 01:57:09 +0000894/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
895uint32_t ARMMCCodeEmitter::
896getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
897 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000898 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000899}
900
Jim Grosbach5177f792010-12-01 21:09:40 +0000901/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000902uint32_t ARMMCCodeEmitter::
903getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
904 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000905 // {12-9} = reg
906 // {8} = (U)nsigned (add == '1', sub == '0')
907 // {7-0} = imm8
908 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000909 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000910 // If The first operand isn't a register, we have a label reference.
911 const MCOperand &MO = MI.getOperand(OpIdx);
912 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000913 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000914 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000915 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000916
917 assert(MO.isExpr() && "Unexpected machine operand type!");
918 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000919 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000920 if (isThumb2())
Owen Andersond8e351b2010-12-08 00:18:36 +0000921 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
922 else
923 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000924 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
925
926 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000927 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000928 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000929 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
930 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000931
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000932 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
933 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000934 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000935 Binary |= (1 << 8);
936 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000937 return Binary;
938}
939
Jim Grosbach806e80e2010-11-03 23:52:49 +0000940unsigned ARMMCCodeEmitter::
941getSORegOpValue(const MCInst &MI, unsigned OpIdx,
942 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000943 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
944 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
945 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000946 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000947 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000948 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000949 // {6-5} = type
950 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000951 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000952 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000953 // else (imm shift)
954 // {11-7} = imm
955
956 const MCOperand &MO = MI.getOperand(OpIdx);
957 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
958 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
959 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
960
961 // Encode Rm.
962 unsigned Binary = getARMRegisterNumbering(MO.getReg());
963
964 // Encode the shift opcode.
965 unsigned SBits = 0;
966 unsigned Rs = MO1.getReg();
967 if (Rs) {
968 // Set shift operand (bit[7:4]).
969 // LSL - 0001
970 // LSR - 0011
971 // ASR - 0101
972 // ROR - 0111
973 // RRX - 0110 and bit[11:8] clear.
974 switch (SOpc) {
975 default: llvm_unreachable("Unknown shift opc!");
976 case ARM_AM::lsl: SBits = 0x1; break;
977 case ARM_AM::lsr: SBits = 0x3; break;
978 case ARM_AM::asr: SBits = 0x5; break;
979 case ARM_AM::ror: SBits = 0x7; break;
980 case ARM_AM::rrx: SBits = 0x6; break;
981 }
982 } else {
983 // Set shift operand (bit[6:4]).
984 // LSL - 000
985 // LSR - 010
986 // ASR - 100
987 // ROR - 110
988 switch (SOpc) {
989 default: llvm_unreachable("Unknown shift opc!");
990 case ARM_AM::lsl: SBits = 0x0; break;
991 case ARM_AM::lsr: SBits = 0x2; break;
992 case ARM_AM::asr: SBits = 0x4; break;
993 case ARM_AM::ror: SBits = 0x6; break;
994 }
995 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000996
Jim Grosbachef324d72010-10-12 23:53:58 +0000997 Binary |= SBits << 4;
998 if (SOpc == ARM_AM::rrx)
999 return Binary;
1000
1001 // Encode the shift operation Rs or shift_imm (except rrx).
1002 if (Rs) {
1003 // Encode Rs bit[11:8].
1004 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1005 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
1006 }
1007
1008 // Encode shift_imm bit[11:7].
1009 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
1010}
1011
Jim Grosbach806e80e2010-11-03 23:52:49 +00001012unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +00001013getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1014 SmallVectorImpl<MCFixup> &Fixups) const {
1015 const MCOperand &MO1 = MI.getOperand(OpNum);
1016 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001017 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1018
Owen Anderson75579f72010-11-29 22:44:32 +00001019 // Encoded as [Rn, Rm, imm].
1020 // FIXME: Needs fixup support.
1021 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1022 Value <<= 4;
1023 Value |= getARMRegisterNumbering(MO2.getReg());
1024 Value <<= 2;
1025 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001026
Owen Anderson75579f72010-11-29 22:44:32 +00001027 return Value;
1028}
1029
1030unsigned ARMMCCodeEmitter::
1031getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1032 SmallVectorImpl<MCFixup> &Fixups) const {
1033 const MCOperand &MO1 = MI.getOperand(OpNum);
1034 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1035
1036 // FIXME: Needs fixup support.
1037 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001038
Owen Anderson75579f72010-11-29 22:44:32 +00001039 // Even though the immediate is 8 bits long, we need 9 bits in order
1040 // to represent the (inverse of the) sign bit.
1041 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +00001042 int32_t tmp = (int32_t)MO2.getImm();
1043 if (tmp < 0)
1044 tmp = abs(tmp);
1045 else
1046 Value |= 256; // Set the ADD bit
1047 Value |= tmp & 255;
1048 return Value;
1049}
1050
1051unsigned ARMMCCodeEmitter::
1052getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1053 SmallVectorImpl<MCFixup> &Fixups) const {
1054 const MCOperand &MO1 = MI.getOperand(OpNum);
1055
1056 // FIXME: Needs fixup support.
1057 unsigned Value = 0;
1058 int32_t tmp = (int32_t)MO1.getImm();
1059 if (tmp < 0)
1060 tmp = abs(tmp);
1061 else
1062 Value |= 256; // Set the ADD bit
1063 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +00001064 return Value;
1065}
1066
1067unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +00001068getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1069 SmallVectorImpl<MCFixup> &Fixups) const {
1070 const MCOperand &MO1 = MI.getOperand(OpNum);
1071
1072 // FIXME: Needs fixup support.
1073 unsigned Value = 0;
1074 int32_t tmp = (int32_t)MO1.getImm();
1075 if (tmp < 0)
1076 tmp = abs(tmp);
1077 else
1078 Value |= 4096; // Set the ADD bit
1079 Value |= tmp & 4095;
1080 return Value;
1081}
1082
1083unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001084getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1085 SmallVectorImpl<MCFixup> &Fixups) const {
1086 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1087 // shifted. The second is the amount to shift by.
1088 //
1089 // {3-0} = Rm.
1090 // {4} = 0
1091 // {6-5} = type
1092 // {11-7} = imm
1093
1094 const MCOperand &MO = MI.getOperand(OpIdx);
1095 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1096 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1097
1098 // Encode Rm.
1099 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1100
1101 // Encode the shift opcode.
1102 unsigned SBits = 0;
1103 // Set shift operand (bit[6:4]).
1104 // LSL - 000
1105 // LSR - 010
1106 // ASR - 100
1107 // ROR - 110
1108 switch (SOpc) {
1109 default: llvm_unreachable("Unknown shift opc!");
1110 case ARM_AM::lsl: SBits = 0x0; break;
1111 case ARM_AM::lsr: SBits = 0x2; break;
1112 case ARM_AM::asr: SBits = 0x4; break;
1113 case ARM_AM::ror: SBits = 0x6; break;
1114 }
1115
1116 Binary |= SBits << 4;
1117 if (SOpc == ARM_AM::rrx)
1118 return Binary;
1119
1120 // Encode shift_imm bit[11:7].
1121 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1122}
1123
1124unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001125getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1126 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001127 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1128 // msb of the mask.
1129 const MCOperand &MO = MI.getOperand(Op);
1130 uint32_t v = ~MO.getImm();
1131 uint32_t lsb = CountTrailingZeros_32(v);
1132 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1133 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1134 return lsb | (msb << 5);
1135}
1136
Jim Grosbach806e80e2010-11-03 23:52:49 +00001137unsigned ARMMCCodeEmitter::
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +00001138getMsbOpValue(const MCInst &MI, unsigned Op,
1139 SmallVectorImpl<MCFixup> &Fixups) const {
1140 // MSB - 5 bits.
1141 uint32_t lsb = MI.getOperand(Op-1).getImm();
1142 uint32_t width = MI.getOperand(Op).getImm();
1143 uint32_t msb = lsb+width-1;
1144 assert (width != 0 && msb < 32 && "Illegal bit width!");
1145 return msb;
1146}
1147
1148unsigned ARMMCCodeEmitter::
Bruno Cardoso Lopes895c1e22011-05-31 03:33:27 +00001149getSsatBitPosValue(const MCInst &MI, unsigned Op,
1150 SmallVectorImpl<MCFixup> &Fixups) const {
1151 // For ssat instructions, the bit position should be encoded decremented by 1
1152 return MI.getOperand(Op).getImm()-1;
1153}
1154
1155unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001156getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001157 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001158 // VLDM/VSTM:
1159 // {12-8} = Vd
1160 // {7-0} = Number of registers
1161 //
1162 // LDM/STM:
1163 // {15-0} = Bitfield of GPRs.
1164 unsigned Reg = MI.getOperand(Op).getReg();
1165 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1166 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1167
Bill Wendling5e559a22010-11-09 00:30:18 +00001168 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001169
1170 if (SPRRegs || DPRRegs) {
1171 // VLDM/VSTM
1172 unsigned RegNo = getARMRegisterNumbering(Reg);
1173 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1174 Binary |= (RegNo & 0x1f) << 8;
1175 if (SPRRegs)
1176 Binary |= NumRegs;
1177 else
1178 Binary |= NumRegs * 2;
1179 } else {
1180 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1181 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1182 Binary |= 1 << RegNo;
1183 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001184 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001185
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001186 return Binary;
1187}
1188
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001189/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1190/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001191unsigned ARMMCCodeEmitter::
1192getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1193 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001194 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001195 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001196
Owen Andersond9aa7d32010-11-02 00:05:05 +00001197 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001198 unsigned Align = 0;
1199
1200 switch (Imm.getImm()) {
1201 default: break;
1202 case 2:
1203 case 4:
1204 case 8: Align = 0x01; break;
1205 case 16: Align = 0x02; break;
1206 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001207 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001208
Owen Andersond9aa7d32010-11-02 00:05:05 +00001209 return RegNo | (Align << 4);
1210}
1211
Mon P Wang183c6272011-05-09 17:47:27 +00001212/// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1213/// along with the alignment operand for use in VST1 and VLD1 with size 32.
1214unsigned ARMMCCodeEmitter::
1215getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1216 SmallVectorImpl<MCFixup> &Fixups) const {
1217 const MCOperand &Reg = MI.getOperand(Op);
1218 const MCOperand &Imm = MI.getOperand(Op + 1);
1219
1220 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1221 unsigned Align = 0;
1222
1223 switch (Imm.getImm()) {
1224 default: break;
1225 case 2:
1226 case 4:
1227 case 8:
1228 case 16: Align = 0x00; break;
1229 case 32: Align = 0x03; break;
1230 }
1231
1232 return RegNo | (Align << 4);
1233}
1234
1235
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001236/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1237/// alignment operand for use in VLD-dup instructions. This is the same as
1238/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1239/// different for VLD4-dup.
1240unsigned ARMMCCodeEmitter::
1241getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1242 SmallVectorImpl<MCFixup> &Fixups) const {
1243 const MCOperand &Reg = MI.getOperand(Op);
1244 const MCOperand &Imm = MI.getOperand(Op + 1);
1245
1246 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1247 unsigned Align = 0;
1248
1249 switch (Imm.getImm()) {
1250 default: break;
1251 case 2:
1252 case 4:
1253 case 8: Align = 0x01; break;
1254 case 16: Align = 0x03; break;
1255 }
1256
1257 return RegNo | (Align << 4);
1258}
1259
Jim Grosbach806e80e2010-11-03 23:52:49 +00001260unsigned ARMMCCodeEmitter::
1261getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1262 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001263 const MCOperand &MO = MI.getOperand(Op);
1264 if (MO.getReg() == 0) return 0x0D;
1265 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001266}
1267
Bill Wendlinga656b632011-03-01 01:00:59 +00001268unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001269getShiftRight8Imm(const MCInst &MI, unsigned Op,
1270 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001271 return 8 - MI.getOperand(Op).getImm();
1272}
1273
1274unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001275getShiftRight16Imm(const MCInst &MI, unsigned Op,
1276 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001277 return 16 - MI.getOperand(Op).getImm();
1278}
1279
1280unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001281getShiftRight32Imm(const MCInst &MI, unsigned Op,
1282 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001283 return 32 - MI.getOperand(Op).getImm();
1284}
1285
Bill Wendling3116dce2011-03-07 23:38:41 +00001286unsigned ARMMCCodeEmitter::
1287getShiftRight64Imm(const MCInst &MI, unsigned Op,
1288 SmallVectorImpl<MCFixup> &Fixups) const {
1289 return 64 - MI.getOperand(Op).getImm();
1290}
1291
Jim Grosbach568eeed2010-09-17 18:46:17 +00001292void ARMMCCodeEmitter::
1293EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001294 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001295 // Pseudo instructions don't get encoded.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001296 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001297 uint64_t TSFlags = Desc.TSFlags;
1298 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001299 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001300 int Size;
1301 // Basic size info comes from the TSFlags field.
1302 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1303 default: llvm_unreachable("Unexpected instruction size!");
1304 case ARMII::Size2Bytes: Size = 2; break;
1305 case ARMII::Size4Bytes: Size = 4; break;
1306 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001307 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
Evan Cheng75972122011-01-13 07:58:56 +00001308 // Thumb 32-bit wide instructions need to emit the high order halfword
1309 // first.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001310 if (isThumb() && Size == 4) {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001311 EmitConstant(Binary >> 16, 2, OS);
1312 EmitConstant(Binary & 0xffff, 2, OS);
1313 } else
1314 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001315 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001316}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001317
Jim Grosbach806e80e2010-11-03 23:52:49 +00001318#include "ARMGenMCCodeEmitter.inc"