blob: 43eb5e32b3d59e690e56fcdc56c9c89c4d5e9698 [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 Grosbach70933262010-11-04 01:12:30 +000016#include "ARMFixupKinds.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000017#include "ARMInstrInfo.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000018#include "MCTargetDesc/ARMAddressingModes.h"
19#include "MCTargetDesc/ARMMCExpr.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000020#include "llvm/MC/MCCodeEmitter.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000023#include "llvm/MC/MCInstrInfo.h"
24#include "llvm/MC/MCSubtargetInfo.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000025#include "llvm/ADT/APFloat.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
Jim Grosbach568eeed2010-09-17 18:46:17 +000029using namespace llvm;
30
Jim Grosbach70933262010-11-04 01:12:30 +000031STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
32STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
Jim Grosbachd6d4b422010-10-07 22:12:50 +000033
Jim Grosbach568eeed2010-09-17 18:46:17 +000034namespace {
35class ARMMCCodeEmitter : public MCCodeEmitter {
36 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
37 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
Evan Cheng59ee62d2011-07-11 03:57:24 +000038 const MCInstrInfo &MCII;
39 const MCSubtargetInfo &STI;
Jim Grosbach568eeed2010-09-17 18:46:17 +000040
41public:
Evan Cheng59ee62d2011-07-11 03:57:24 +000042 ARMMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
43 MCContext &ctx)
Evan Chengaf0a2e62011-07-11 21:24:15 +000044 : MCII(mcii), STI(sti) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000045 }
46
47 ~ARMMCCodeEmitter() {}
48
Evan Cheng59ee62d2011-07-11 03:57:24 +000049 bool isThumb() const {
50 // FIXME: Can tablegen auto-generate this?
51 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
52 }
53 bool isThumb2() const {
54 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) != 0;
55 }
56 bool isTargetDarwin() const {
57 Triple TT(STI.getTargetTriple());
58 Triple::OSType OS = TT.getOS();
59 return OS == Triple::Darwin || OS == Triple::MacOSX || OS == Triple::IOS;
60 }
61
Jim Grosbach0de6ab32010-10-12 17:11:26 +000062 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
63
Jim Grosbach9af82ba2010-10-07 21:57:55 +000064 // getBinaryCodeForInstr - TableGen'erated function for getting the
65 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000066 unsigned getBinaryCodeForInstr(const MCInst &MI,
67 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000068
69 /// getMachineOpValue - Return binary encoding of operand. If the machine
70 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000071 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
72 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000073
Evan Cheng75972122011-01-13 07:58:56 +000074 /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
Owen Anderson971b83b2011-02-08 22:39:40 +000075 /// the specified operand. This is used for operands with :lower16: and
Evan Cheng75972122011-01-13 07:58:56 +000076 /// :upper16: prefixes.
77 uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
78 SmallVectorImpl<MCFixup> &Fixups) const;
Jason W Kim837caa92010-11-18 23:37:15 +000079
Bill Wendling92b5a2e2010-11-03 01:49:29 +000080 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000081 unsigned &Reg, unsigned &Imm,
82 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000083
Jim Grosbach662a8162010-12-06 23:57:07 +000084 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +000085 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +000086 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
87 SmallVectorImpl<MCFixup> &Fixups) const;
88
Bill Wendling09aa3f02010-12-09 00:39:08 +000089 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
90 /// BLX branch target.
91 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
92 SmallVectorImpl<MCFixup> &Fixups) const;
93
Jim Grosbache2467172010-12-10 18:21:33 +000094 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
95 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
96 SmallVectorImpl<MCFixup> &Fixups) const;
97
Jim Grosbach01086452010-12-10 17:13:40 +000098 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
99 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
100 SmallVectorImpl<MCFixup> &Fixups) const;
101
Jim Grosbach027d6e82010-12-09 19:04:53 +0000102 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
103 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000104 SmallVectorImpl<MCFixup> &Fixups) const;
105
Jim Grosbachc466b932010-11-11 18:04:49 +0000106 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
107 /// branch target.
108 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
109 SmallVectorImpl<MCFixup> &Fixups) const;
110
Owen Andersonc2666002010-12-13 19:31:11 +0000111 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
112 /// immediate Thumb2 direct branch target.
113 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
114 SmallVectorImpl<MCFixup> &Fixups) const;
115
Jason W Kim685c3502011-02-04 19:47:15 +0000116 /// getARMBranchTargetOpValue - Return encoding info for 24-bit immediate
117 /// branch target.
118 uint32_t getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
119 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersonc2666002010-12-13 19:31:11 +0000120
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000121 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
122 /// ADR label target.
123 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
124 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachd40963c2010-12-14 22:28:03 +0000125 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
126 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersona838a252010-12-14 00:36:49 +0000127 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
128 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson971b83b2011-02-08 22:39:40 +0000129
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000130
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000131 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
132 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000133 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
134 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000135
Bill Wendlingf4caf692010-12-14 03:36:38 +0000136 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
137 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
138 SmallVectorImpl<MCFixup> &Fixups)const;
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000139
Owen Anderson9d63d902010-12-01 19:18:46 +0000140 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
141 /// operand.
142 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
143 SmallVectorImpl<MCFixup> &Fixups) const;
144
145
Jim Grosbach54fea632010-11-09 17:20:53 +0000146 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
147 /// operand as needed by load/store instructions.
148 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
149 SmallVectorImpl<MCFixup> &Fixups) const;
150
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000151 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
152 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
153 SmallVectorImpl<MCFixup> &Fixups) const {
154 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
155 switch (Mode) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000156 default: assert(0 && "Unknown addressing sub-mode!");
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000157 case ARM_AM::da: return 0;
158 case ARM_AM::ia: return 1;
159 case ARM_AM::db: return 2;
160 case ARM_AM::ib: return 3;
161 }
162 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000163 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
164 ///
165 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
166 switch (ShOpc) {
167 default: llvm_unreachable("Unknown shift opc!");
168 case ARM_AM::no_shift:
169 case ARM_AM::lsl: return 0;
170 case ARM_AM::lsr: return 1;
171 case ARM_AM::asr: return 2;
172 case ARM_AM::ror:
173 case ARM_AM::rrx: return 3;
174 }
175 return 0;
176 }
177
178 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
179 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
180 SmallVectorImpl<MCFixup> &Fixups) const;
181
182 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
183 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
184 SmallVectorImpl<MCFixup> &Fixups) const;
185
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000186 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
187 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
188 SmallVectorImpl<MCFixup> &Fixups) const;
189
Jim Grosbach570a9222010-11-11 01:09:40 +0000190 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
191 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
192 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000193
Jim Grosbachd967cd02010-12-07 21:50:47 +0000194 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
195 /// operand.
196 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
197 SmallVectorImpl<MCFixup> &Fixups) const;
198
Bill Wendlingf4caf692010-12-14 03:36:38 +0000199 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
200 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000201 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000202
Bill Wendlingb8958b02010-12-08 01:57:09 +0000203 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
204 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
205 SmallVectorImpl<MCFixup> &Fixups) const;
206
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000207 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000208 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
209 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000210
Jim Grosbach08bd5492010-10-12 23:00:24 +0000211 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000212 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
213 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000214 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
215 // '1' respectively.
216 return MI.getOperand(Op).getReg() == ARM::CPSR;
217 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000218
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000219 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000220 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
221 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000222 unsigned SoImm = MI.getOperand(Op).getImm();
223 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
224 assert(SoImmVal != -1 && "Not a valid so_imm value!");
225
226 // Encode rotate_imm.
227 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
228 << ARMII::SoRotImmShift;
229
230 // Encode immed_8.
231 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
232 return Binary;
233 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000234
Owen Anderson5de6d842010-11-12 21:12:40 +0000235 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
236 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
237 SmallVectorImpl<MCFixup> &Fixups) const {
238 unsigned SoImm = MI.getOperand(Op).getImm();
239 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
240 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
241 return Encoded;
242 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000243
Owen Anderson75579f72010-11-29 22:44:32 +0000244 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
245 SmallVectorImpl<MCFixup> &Fixups) const;
246 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
247 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000248 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
249 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000250 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
251 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000252
Jim Grosbachef324d72010-10-12 23:53:58 +0000253 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Owen Anderson152d4a42011-07-21 23:38:37 +0000254 unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op,
255 SmallVectorImpl<MCFixup> &Fixups) const;
256 unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000257 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000258 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
259 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000260
Jim Grosbach806e80e2010-11-03 23:52:49 +0000261 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
262 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000263 switch (MI.getOperand(Op).getImm()) {
264 default: assert (0 && "Not a valid rot_imm value!");
265 case 0: return 0;
266 case 8: return 1;
267 case 16: return 2;
268 case 24: return 3;
269 }
270 }
271
Jim Grosbach806e80e2010-11-03 23:52:49 +0000272 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
273 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000274 return MI.getOperand(Op).getImm() - 1;
275 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000276
Jim Grosbach806e80e2010-11-03 23:52:49 +0000277 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
278 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000279 return 64 - MI.getOperand(Op).getImm();
280 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000281
Jim Grosbach806e80e2010-11-03 23:52:49 +0000282 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
283 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000284
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +0000285 unsigned getMsbOpValue(const MCInst &MI, unsigned Op,
286 SmallVectorImpl<MCFixup> &Fixups) const;
287
Bruno Cardoso Lopes895c1e22011-05-31 03:33:27 +0000288 unsigned getSsatBitPosValue(const MCInst &MI, unsigned Op,
289 SmallVectorImpl<MCFixup> &Fixups) const;
290
Jim Grosbach806e80e2010-11-03 23:52:49 +0000291 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
292 SmallVectorImpl<MCFixup> &Fixups) const;
293 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
294 SmallVectorImpl<MCFixup> &Fixups) const;
Mon P Wang183c6272011-05-09 17:47:27 +0000295 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
296 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000297 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
298 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000299 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
300 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000301
Bill Wendling3116dce2011-03-07 23:38:41 +0000302 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
303 SmallVectorImpl<MCFixup> &Fixups) const;
304 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
305 SmallVectorImpl<MCFixup> &Fixups) const;
306 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
307 SmallVectorImpl<MCFixup> &Fixups) const;
308 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
309 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendlinga656b632011-03-01 01:00:59 +0000310
Owen Andersonc7139a62010-11-11 19:07:48 +0000311 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
312 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000313 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000314 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000315 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000316 unsigned EncodedValue) const;
317
318 unsigned VFPThumb2PostEncoder(const MCInst &MI,
319 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000320
Jim Grosbach70933262010-11-04 01:12:30 +0000321 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000322 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000323 }
324
Jim Grosbach70933262010-11-04 01:12:30 +0000325 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000326 // Output the constant in little endian byte order.
327 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000328 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000329 Val >>= 8;
330 }
331 }
332
Jim Grosbach568eeed2010-09-17 18:46:17 +0000333 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
334 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000335};
336
337} // end anonymous namespace
338
Evan Cheng59ee62d2011-07-11 03:57:24 +0000339MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
340 const MCSubtargetInfo &STI,
Bill Wendling0800ce72010-11-02 22:53:11 +0000341 MCContext &Ctx) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000342 return new ARMMCCodeEmitter(MCII, STI, Ctx);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000343}
344
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000345/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
346/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000347/// Thumb2 mode.
348unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
349 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000350 if (isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000351 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000352 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
353 // set to 1111.
354 unsigned Bit24 = EncodedValue & 0x01000000;
355 unsigned Bit28 = Bit24 << 4;
356 EncodedValue &= 0xEFFFFFFF;
357 EncodedValue |= Bit28;
358 EncodedValue |= 0x0F000000;
359 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000360
Owen Andersonc7139a62010-11-11 19:07:48 +0000361 return EncodedValue;
362}
363
Owen Anderson57dac882010-11-11 21:36:43 +0000364/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000365/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000366/// Thumb2 mode.
367unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
368 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000369 if (isThumb2()) {
Owen Anderson57dac882010-11-11 21:36:43 +0000370 EncodedValue &= 0xF0FFFFFF;
371 EncodedValue |= 0x09000000;
372 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000373
Owen Anderson57dac882010-11-11 21:36:43 +0000374 return EncodedValue;
375}
376
Owen Anderson8f143912010-11-11 23:12:55 +0000377/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000378/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000379/// Thumb2 mode.
380unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
381 unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000382 if (isThumb2()) {
Owen Anderson8f143912010-11-11 23:12:55 +0000383 EncodedValue &= 0x00FFFFFF;
384 EncodedValue |= 0xEE000000;
385 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000386
Owen Anderson8f143912010-11-11 23:12:55 +0000387 return EncodedValue;
388}
389
Bill Wendlingcf590262010-12-01 21:54:50 +0000390/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
391/// them to their Thumb2 form if we are currently in Thumb2 mode.
392unsigned ARMMCCodeEmitter::
393VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000394 if (isThumb2()) {
Bill Wendlingcf590262010-12-01 21:54:50 +0000395 EncodedValue &= 0x0FFFFFFF;
396 EncodedValue |= 0xE0000000;
397 }
398 return EncodedValue;
399}
Owen Anderson57dac882010-11-11 21:36:43 +0000400
Jim Grosbach56ac9072010-10-08 21:45:55 +0000401/// getMachineOpValue - Return binary encoding of operand. If the machine
402/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000403unsigned ARMMCCodeEmitter::
404getMachineOpValue(const MCInst &MI, const MCOperand &MO,
405 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000406 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000407 unsigned Reg = MO.getReg();
408 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000409
Jim Grosbachb0708d22010-11-30 23:51:41 +0000410 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000411 switch (Reg) {
412 default:
413 return RegNo;
414 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
415 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
416 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
417 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
418 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000419 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000420 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000421 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000422 } else if (MO.isFPImm()) {
423 return static_cast<unsigned>(APFloat(MO.getFPImm())
424 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000425 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000426
Jim Grosbach817c1a62010-11-19 00:27:09 +0000427 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000428 return 0;
429}
430
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000431/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000432bool ARMMCCodeEmitter::
433EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
434 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000435 const MCOperand &MO = MI.getOperand(OpIdx);
436 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000437
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000438 Reg = getARMRegisterNumbering(MO.getReg());
439
440 int32_t SImm = MO1.getImm();
441 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000442
Jim Grosbachab682a22010-10-28 18:34:10 +0000443 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000444 if (SImm == INT32_MIN)
445 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000446
Jim Grosbachab682a22010-10-28 18:34:10 +0000447 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000448 if (SImm < 0) {
449 SImm = -SImm;
450 isAdd = false;
451 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000452
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000453 Imm = SImm;
454 return isAdd;
455}
456
Bill Wendlingdff2f712010-12-08 23:01:43 +0000457/// getBranchTargetOpValue - Helper function to get the branch target operand,
458/// which is either an immediate or requires a fixup.
459static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
460 unsigned FixupKind,
461 SmallVectorImpl<MCFixup> &Fixups) {
462 const MCOperand &MO = MI.getOperand(OpIdx);
463
464 // If the destination is an immediate, we have nothing to do.
465 if (MO.isImm()) return MO.getImm();
466 assert(MO.isExpr() && "Unexpected branch target type!");
467 const MCExpr *Expr = MO.getExpr();
468 MCFixupKind Kind = MCFixupKind(FixupKind);
469 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
470
471 // All of the information is in the fixup.
472 return 0;
473}
474
475/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000476uint32_t ARMMCCodeEmitter::
477getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
478 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000479 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000480}
481
Bill Wendling09aa3f02010-12-09 00:39:08 +0000482/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
483/// BLX branch target.
484uint32_t ARMMCCodeEmitter::
485getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
486 SmallVectorImpl<MCFixup> &Fixups) const {
487 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
488}
489
Jim Grosbache2467172010-12-10 18:21:33 +0000490/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
491uint32_t ARMMCCodeEmitter::
492getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
493 SmallVectorImpl<MCFixup> &Fixups) const {
494 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
495}
496
Jim Grosbach01086452010-12-10 17:13:40 +0000497/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
498uint32_t ARMMCCodeEmitter::
499getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000500 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000501 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
502}
503
Jim Grosbach027d6e82010-12-09 19:04:53 +0000504/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000505uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000506getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000507 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000508 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000509}
510
Jason W Kim685c3502011-02-04 19:47:15 +0000511/// Return true if this branch has a non-always predication
512static bool HasConditionalBranch(const MCInst &MI) {
513 int NumOp = MI.getNumOperands();
514 if (NumOp >= 2) {
515 for (int i = 0; i < NumOp-1; ++i) {
516 const MCOperand &MCOp1 = MI.getOperand(i);
517 const MCOperand &MCOp2 = MI.getOperand(i + 1);
518 if (MCOp1.isImm() && MCOp2.isReg() &&
519 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
520 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
521 return true;
522 }
523 }
524 }
525 return false;
526}
527
Bill Wendlingdff2f712010-12-08 23:01:43 +0000528/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
529/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000530uint32_t ARMMCCodeEmitter::
531getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000532 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000533 // FIXME: This really, really shouldn't use TargetMachine. We don't want
534 // coupling between MC and TM anywhere we can help it.
Evan Cheng59ee62d2011-07-11 03:57:24 +0000535 if (isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000536 return
537 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Jason W Kim685c3502011-02-04 19:47:15 +0000538 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000539}
540
Jason W Kim685c3502011-02-04 19:47:15 +0000541/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
542/// target.
543uint32_t ARMMCCodeEmitter::
544getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
545 SmallVectorImpl<MCFixup> &Fixups) const {
546 if (HasConditionalBranch(MI))
547 return ::getBranchTargetOpValue(MI, OpIdx,
548 ARM::fixup_arm_condbranch, Fixups);
549 return ::getBranchTargetOpValue(MI, OpIdx,
550 ARM::fixup_arm_uncondbranch, Fixups);
551}
552
553
554
555
Owen Andersonc2666002010-12-13 19:31:11 +0000556/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
557/// immediate branch target.
558uint32_t ARMMCCodeEmitter::
559getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
560 SmallVectorImpl<MCFixup> &Fixups) const {
561 unsigned Val =
562 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
563 bool I = (Val & 0x800000);
564 bool J1 = (Val & 0x400000);
565 bool J2 = (Val & 0x200000);
566 if (I ^ J1)
567 Val &= ~0x400000;
568 else
569 Val |= 0x400000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000570
Owen Andersonc2666002010-12-13 19:31:11 +0000571 if (I ^ J2)
572 Val &= ~0x200000;
573 else
574 Val |= 0x200000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000575
Owen Andersonc2666002010-12-13 19:31:11 +0000576 return Val;
577}
578
Bill Wendlingdff2f712010-12-08 23:01:43 +0000579/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
580/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000581uint32_t ARMMCCodeEmitter::
582getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
583 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000584 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
585 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
586 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000587}
588
Owen Andersona838a252010-12-14 00:36:49 +0000589/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
590/// target.
591uint32_t ARMMCCodeEmitter::
592getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
593 SmallVectorImpl<MCFixup> &Fixups) const {
594 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
595 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
596 Fixups);
597}
598
Jim Grosbachd40963c2010-12-14 22:28:03 +0000599/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
600/// target.
601uint32_t ARMMCCodeEmitter::
602getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
603 SmallVectorImpl<MCFixup> &Fixups) const {
604 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
605 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
606 Fixups);
607}
608
Bill Wendlingf4caf692010-12-14 03:36:38 +0000609/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
610/// operand.
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000611uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000612getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
613 SmallVectorImpl<MCFixup> &) const {
614 // [Rn, Rm]
615 // {5-3} = Rm
616 // {2-0} = Rn
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000617 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000618 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000619 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
620 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
621 return (Rm << 3) | Rn;
622}
623
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000624/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000625uint32_t ARMMCCodeEmitter::
626getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
627 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000628 // {17-13} = reg
629 // {12} = (U)nsigned (add == '1', sub == '0')
630 // {11-0} = imm12
631 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000632 bool isAdd = true;
633 // If The first operand isn't a register, we have a label reference.
634 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Anderson971b83b2011-02-08 22:39:40 +0000635 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000636 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000637 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000638 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000639
Owen Anderson971b83b2011-02-08 22:39:40 +0000640 assert(MO.isExpr() && "Unexpected machine operand type!");
641 const MCExpr *Expr = MO.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000642
Owen Andersond7b3f582010-12-09 01:51:07 +0000643 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000644 if (isThumb2())
Owen Andersond7b3f582010-12-09 01:51:07 +0000645 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
646 else
647 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000648 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
649
650 ++MCNumCPRelocations;
651 } else
652 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000653
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000654 uint32_t Binary = Imm12 & 0xfff;
655 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000656 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000657 Binary |= (1 << 12);
658 Binary |= (Reg << 13);
659 return Binary;
660}
661
Owen Anderson9d63d902010-12-01 19:18:46 +0000662/// getT2AddrModeImm8s4OpValue - Return encoding info for
663/// 'reg +/- imm8<<2' operand.
664uint32_t ARMMCCodeEmitter::
665getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
666 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000667 // {12-9} = reg
668 // {8} = (U)nsigned (add == '1', sub == '0')
669 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000670 unsigned Reg, Imm8;
671 bool isAdd = true;
672 // If The first operand isn't a register, we have a label reference.
673 const MCOperand &MO = MI.getOperand(OpIdx);
674 if (!MO.isReg()) {
675 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
676 Imm8 = 0;
677 isAdd = false ; // 'U' bit is set as part of the fixup.
678
679 assert(MO.isExpr() && "Unexpected machine operand type!");
680 const MCExpr *Expr = MO.getExpr();
681 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
682 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
683
684 ++MCNumCPRelocations;
685 } else
686 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
687
688 uint32_t Binary = (Imm8 >> 2) & 0xff;
689 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
690 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000691 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000692 Binary |= (Reg << 9);
693 return Binary;
694}
695
Jason W Kim86a97f22011-01-12 00:19:25 +0000696// FIXME: This routine assumes that a binary
697// expression will always result in a PCRel expression
698// In reality, its only true if one or more subexpressions
699// is itself a PCRel (i.e. "." in asm or some other pcrel construct)
700// but this is good enough for now.
701static bool EvaluateAsPCRel(const MCExpr *Expr) {
702 switch (Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000703 default: assert(0 && "Unexpected expression type");
Jason W Kim86a97f22011-01-12 00:19:25 +0000704 case MCExpr::SymbolRef: return false;
705 case MCExpr::Binary: return true;
Jason W Kim86a97f22011-01-12 00:19:25 +0000706 }
707}
708
Evan Cheng75972122011-01-13 07:58:56 +0000709uint32_t
710ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
711 SmallVectorImpl<MCFixup> &Fixups) const {
Jason W Kim837caa92010-11-18 23:37:15 +0000712 // {20-16} = imm{15-12}
713 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000714 const MCOperand &MO = MI.getOperand(OpIdx);
Evan Cheng75972122011-01-13 07:58:56 +0000715 if (MO.isImm())
716 // Hi / lo 16 bits already extracted during earlier passes.
Jason W Kim837caa92010-11-18 23:37:15 +0000717 return static_cast<unsigned>(MO.getImm());
Evan Cheng75972122011-01-13 07:58:56 +0000718
719 // Handle :upper16: and :lower16: assembly prefixes.
720 const MCExpr *E = MO.getExpr();
721 if (E->getKind() == MCExpr::Target) {
722 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
723 E = ARM16Expr->getSubExpr();
724
Jason W Kim837caa92010-11-18 23:37:15 +0000725 MCFixupKind Kind;
Evan Cheng75972122011-01-13 07:58:56 +0000726 switch (ARM16Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000727 default: assert(0 && "Unsupported ARMFixup");
Evan Cheng75972122011-01-13 07:58:56 +0000728 case ARMMCExpr::VK_ARM_HI16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000729 if (!isTargetDarwin() && EvaluateAsPCRel(E))
730 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000731 ? ARM::fixup_t2_movt_hi16_pcrel
732 : ARM::fixup_arm_movt_hi16_pcrel);
733 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000734 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000735 ? ARM::fixup_t2_movt_hi16
736 : ARM::fixup_arm_movt_hi16);
Jason W Kim837caa92010-11-18 23:37:15 +0000737 break;
Evan Cheng75972122011-01-13 07:58:56 +0000738 case ARMMCExpr::VK_ARM_LO16:
Evan Cheng59ee62d2011-07-11 03:57:24 +0000739 if (!isTargetDarwin() && EvaluateAsPCRel(E))
740 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000741 ? ARM::fixup_t2_movw_lo16_pcrel
742 : ARM::fixup_arm_movw_lo16_pcrel);
743 else
Evan Cheng59ee62d2011-07-11 03:57:24 +0000744 Kind = MCFixupKind(isThumb2()
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000745 ? ARM::fixup_t2_movw_lo16
746 : ARM::fixup_arm_movw_lo16);
Jason W Kim837caa92010-11-18 23:37:15 +0000747 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000748 }
Evan Cheng75972122011-01-13 07:58:56 +0000749 Fixups.push_back(MCFixup::Create(0, E, Kind));
Jason W Kim837caa92010-11-18 23:37:15 +0000750 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000751 };
Evan Cheng75972122011-01-13 07:58:56 +0000752
Jim Grosbach817c1a62010-11-19 00:27:09 +0000753 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000754 return 0;
755}
756
757uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000758getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
759 SmallVectorImpl<MCFixup> &Fixups) const {
760 const MCOperand &MO = MI.getOperand(OpIdx);
761 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
762 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
763 unsigned Rn = getARMRegisterNumbering(MO.getReg());
764 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000765 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
766 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000767 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
768 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000769
770 // {16-13} = Rn
771 // {12} = isAdd
772 // {11-0} = shifter
773 // {3-0} = Rm
774 // {4} = 0
775 // {6-5} = type
776 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000777 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000778 Binary |= Rn << 13;
779 Binary |= SBits << 5;
780 Binary |= ShImm << 7;
781 if (isAdd)
782 Binary |= 1 << 12;
783 return Binary;
784}
785
Jim Grosbach570a9222010-11-11 01:09:40 +0000786uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000787getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
788 SmallVectorImpl<MCFixup> &Fixups) const {
789 // {17-14} Rn
790 // {13} 1 == imm12, 0 == Rm
791 // {12} isAdd
792 // {11-0} imm12/Rm
793 const MCOperand &MO = MI.getOperand(OpIdx);
794 unsigned Rn = getARMRegisterNumbering(MO.getReg());
795 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
796 Binary |= Rn << 14;
797 return Binary;
798}
799
800uint32_t ARMMCCodeEmitter::
801getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
802 SmallVectorImpl<MCFixup> &Fixups) const {
803 // {13} 1 == imm12, 0 == Rm
804 // {12} isAdd
805 // {11-0} imm12/Rm
806 const MCOperand &MO = MI.getOperand(OpIdx);
807 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
808 unsigned Imm = MO1.getImm();
809 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
810 bool isReg = MO.getReg() != 0;
811 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
812 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
813 if (isReg) {
814 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
815 Binary <<= 7; // Shift amount is bits [11:7]
816 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
817 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
818 }
819 return Binary | (isAdd << 12) | (isReg << 13);
820}
821
822uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000823getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
824 SmallVectorImpl<MCFixup> &Fixups) const {
825 // {9} 1 == imm8, 0 == Rm
826 // {8} isAdd
827 // {7-4} imm7_4/zero
828 // {3-0} imm3_0/Rm
829 const MCOperand &MO = MI.getOperand(OpIdx);
830 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
831 unsigned Imm = MO1.getImm();
832 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
833 bool isImm = MO.getReg() == 0;
834 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
835 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
836 if (!isImm)
837 Imm8 = getARMRegisterNumbering(MO.getReg());
838 return Imm8 | (isAdd << 8) | (isImm << 9);
839}
840
841uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000842getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
843 SmallVectorImpl<MCFixup> &Fixups) const {
844 // {13} 1 == imm8, 0 == Rm
845 // {12-9} Rn
846 // {8} isAdd
847 // {7-4} imm7_4/zero
848 // {3-0} imm3_0/Rm
849 const MCOperand &MO = MI.getOperand(OpIdx);
850 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
851 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
852 unsigned Rn = getARMRegisterNumbering(MO.getReg());
853 unsigned Imm = MO2.getImm();
854 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
855 bool isImm = MO1.getReg() == 0;
856 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
857 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
858 if (!isImm)
859 Imm8 = getARMRegisterNumbering(MO1.getReg());
860 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
861}
862
Bill Wendlingb8958b02010-12-08 01:57:09 +0000863/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000864uint32_t ARMMCCodeEmitter::
865getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
866 SmallVectorImpl<MCFixup> &Fixups) const {
867 // [SP, #imm]
868 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000869 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000870 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
871 "Unexpected base register!");
Bill Wendling7a905a82010-12-15 23:32:27 +0000872
Jim Grosbachd967cd02010-12-07 21:50:47 +0000873 // The immediate is already shifted for the implicit zeroes, so no change
874 // here.
875 return MO1.getImm() & 0xff;
876}
877
Bill Wendlingf4caf692010-12-14 03:36:38 +0000878/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000879uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000880getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000881 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000882 // [Rn, #imm]
883 // {7-3} = imm5
884 // {2-0} = Rn
885 const MCOperand &MO = MI.getOperand(OpIdx);
886 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000887 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Matt Beaumont-Gay656b3d22010-12-16 01:34:26 +0000888 unsigned Imm5 = MO1.getImm();
Bill Wendling272df512010-12-09 21:49:07 +0000889 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000890}
891
Bill Wendlingb8958b02010-12-08 01:57:09 +0000892/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
893uint32_t ARMMCCodeEmitter::
894getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
895 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000896 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000897}
898
Jim Grosbach5177f792010-12-01 21:09:40 +0000899/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000900uint32_t ARMMCCodeEmitter::
901getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
902 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000903 // {12-9} = reg
904 // {8} = (U)nsigned (add == '1', sub == '0')
905 // {7-0} = imm8
906 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000907 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000908 // If The first operand isn't a register, we have a label reference.
909 const MCOperand &MO = MI.getOperand(OpIdx);
910 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000911 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000912 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000913 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000914
915 assert(MO.isExpr() && "Unexpected machine operand type!");
916 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000917 MCFixupKind Kind;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000918 if (isThumb2())
Owen Andersond8e351b2010-12-08 00:18:36 +0000919 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
920 else
921 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000922 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
923
924 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000925 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000926 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000927 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
928 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000929
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000930 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
931 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000932 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000933 Binary |= (1 << 8);
934 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000935 return Binary;
936}
937
Jim Grosbach806e80e2010-11-03 23:52:49 +0000938unsigned ARMMCCodeEmitter::
Owen Anderson152d4a42011-07-21 23:38:37 +0000939getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000940 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000941 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
942 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
943 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000944 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000945 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000946 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000947 // {6-5} = type
948 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000949 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000950 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000951 // else (imm shift)
952 // {11-7} = imm
953
954 const MCOperand &MO = MI.getOperand(OpIdx);
955 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
956 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
957 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
958
959 // Encode Rm.
960 unsigned Binary = getARMRegisterNumbering(MO.getReg());
961
962 // Encode the shift opcode.
963 unsigned SBits = 0;
964 unsigned Rs = MO1.getReg();
965 if (Rs) {
966 // Set shift operand (bit[7:4]).
967 // LSL - 0001
968 // LSR - 0011
969 // ASR - 0101
970 // ROR - 0111
Jim Grosbachef324d72010-10-12 23:53:58 +0000971 switch (SOpc) {
972 default: llvm_unreachable("Unknown shift opc!");
973 case ARM_AM::lsl: SBits = 0x1; break;
974 case ARM_AM::lsr: SBits = 0x3; break;
975 case ARM_AM::asr: SBits = 0x5; break;
976 case ARM_AM::ror: SBits = 0x7; break;
Jim Grosbachef324d72010-10-12 23:53:58 +0000977 }
978 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000979
Jim Grosbachef324d72010-10-12 23:53:58 +0000980 Binary |= SBits << 4;
Jim Grosbachef324d72010-10-12 23:53:58 +0000981
982 // Encode the shift operation Rs or shift_imm (except rrx).
Owen Anderson152d4a42011-07-21 23:38:37 +0000983 // Encode Rs bit[11:8].
984 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
985 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
986}
987
988unsigned ARMMCCodeEmitter::
989getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
990 SmallVectorImpl<MCFixup> &Fixups) const {
991 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
992 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
993 // case the imm contains the amount to shift by.
994 //
995 // {3-0} = Rm.
996 // {4} = 1 if reg shift, 0 if imm shift
997 // {6-5} = type
998 // If reg shift:
999 // {11-8} = Rs
1000 // {7} = 0
1001 // else (imm shift)
1002 // {11-7} = imm
1003
1004 const MCOperand &MO = MI.getOperand(OpIdx);
1005 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1006 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1007
1008 // Encode Rm.
1009 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1010
1011 // Encode the shift opcode.
1012 unsigned SBits = 0;
1013
1014 // Set shift operand (bit[6:4]).
1015 // LSL - 000
1016 // LSR - 010
1017 // ASR - 100
1018 // ROR - 110
1019 // RRX - 110 and bit[11:8] clear.
1020 switch (SOpc) {
1021 default: llvm_unreachable("Unknown shift opc!");
1022 case ARM_AM::lsl: SBits = 0x0; break;
1023 case ARM_AM::lsr: SBits = 0x2; break;
1024 case ARM_AM::asr: SBits = 0x4; break;
1025 case ARM_AM::ror: SBits = 0x6; break;
1026 case ARM_AM::rrx:
1027 Binary |= 0x60;
1028 return Binary;
Jim Grosbachef324d72010-10-12 23:53:58 +00001029 }
1030
1031 // Encode shift_imm bit[11:7].
Owen Anderson152d4a42011-07-21 23:38:37 +00001032 Binary |= SBits << 4;
1033 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
Jim Grosbachef324d72010-10-12 23:53:58 +00001034}
1035
Owen Anderson152d4a42011-07-21 23:38:37 +00001036
Jim Grosbach806e80e2010-11-03 23:52:49 +00001037unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +00001038getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1039 SmallVectorImpl<MCFixup> &Fixups) const {
1040 const MCOperand &MO1 = MI.getOperand(OpNum);
1041 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001042 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1043
Owen Anderson75579f72010-11-29 22:44:32 +00001044 // Encoded as [Rn, Rm, imm].
1045 // FIXME: Needs fixup support.
1046 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1047 Value <<= 4;
1048 Value |= getARMRegisterNumbering(MO2.getReg());
1049 Value <<= 2;
1050 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001051
Owen Anderson75579f72010-11-29 22:44:32 +00001052 return Value;
1053}
1054
1055unsigned ARMMCCodeEmitter::
1056getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1057 SmallVectorImpl<MCFixup> &Fixups) const {
1058 const MCOperand &MO1 = MI.getOperand(OpNum);
1059 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1060
1061 // FIXME: Needs fixup support.
1062 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001063
Owen Anderson75579f72010-11-29 22:44:32 +00001064 // Even though the immediate is 8 bits long, we need 9 bits in order
1065 // to represent the (inverse of the) sign bit.
1066 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +00001067 int32_t tmp = (int32_t)MO2.getImm();
1068 if (tmp < 0)
1069 tmp = abs(tmp);
1070 else
1071 Value |= 256; // Set the ADD bit
1072 Value |= tmp & 255;
1073 return Value;
1074}
1075
1076unsigned ARMMCCodeEmitter::
1077getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1078 SmallVectorImpl<MCFixup> &Fixups) const {
1079 const MCOperand &MO1 = MI.getOperand(OpNum);
1080
1081 // FIXME: Needs fixup support.
1082 unsigned Value = 0;
1083 int32_t tmp = (int32_t)MO1.getImm();
1084 if (tmp < 0)
1085 tmp = abs(tmp);
1086 else
1087 Value |= 256; // Set the ADD bit
1088 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +00001089 return Value;
1090}
1091
1092unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +00001093getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1094 SmallVectorImpl<MCFixup> &Fixups) const {
1095 const MCOperand &MO1 = MI.getOperand(OpNum);
1096
1097 // FIXME: Needs fixup support.
1098 unsigned Value = 0;
1099 int32_t tmp = (int32_t)MO1.getImm();
1100 if (tmp < 0)
1101 tmp = abs(tmp);
1102 else
1103 Value |= 4096; // Set the ADD bit
1104 Value |= tmp & 4095;
1105 return Value;
1106}
1107
1108unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001109getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1110 SmallVectorImpl<MCFixup> &Fixups) const {
1111 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1112 // shifted. The second is the amount to shift by.
1113 //
1114 // {3-0} = Rm.
1115 // {4} = 0
1116 // {6-5} = type
1117 // {11-7} = imm
1118
1119 const MCOperand &MO = MI.getOperand(OpIdx);
1120 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1121 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1122
1123 // Encode Rm.
1124 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1125
1126 // Encode the shift opcode.
1127 unsigned SBits = 0;
1128 // Set shift operand (bit[6:4]).
1129 // LSL - 000
1130 // LSR - 010
1131 // ASR - 100
1132 // ROR - 110
1133 switch (SOpc) {
1134 default: llvm_unreachable("Unknown shift opc!");
1135 case ARM_AM::lsl: SBits = 0x0; break;
1136 case ARM_AM::lsr: SBits = 0x2; break;
1137 case ARM_AM::asr: SBits = 0x4; break;
1138 case ARM_AM::ror: SBits = 0x6; break;
1139 }
1140
1141 Binary |= SBits << 4;
1142 if (SOpc == ARM_AM::rrx)
1143 return Binary;
1144
1145 // Encode shift_imm bit[11:7].
1146 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1147}
1148
1149unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001150getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1151 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001152 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1153 // msb of the mask.
1154 const MCOperand &MO = MI.getOperand(Op);
1155 uint32_t v = ~MO.getImm();
1156 uint32_t lsb = CountTrailingZeros_32(v);
1157 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1158 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1159 return lsb | (msb << 5);
1160}
1161
Jim Grosbach806e80e2010-11-03 23:52:49 +00001162unsigned ARMMCCodeEmitter::
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +00001163getMsbOpValue(const MCInst &MI, unsigned Op,
1164 SmallVectorImpl<MCFixup> &Fixups) const {
1165 // MSB - 5 bits.
1166 uint32_t lsb = MI.getOperand(Op-1).getImm();
1167 uint32_t width = MI.getOperand(Op).getImm();
1168 uint32_t msb = lsb+width-1;
1169 assert (width != 0 && msb < 32 && "Illegal bit width!");
1170 return msb;
1171}
1172
1173unsigned ARMMCCodeEmitter::
Bruno Cardoso Lopes895c1e22011-05-31 03:33:27 +00001174getSsatBitPosValue(const MCInst &MI, unsigned Op,
1175 SmallVectorImpl<MCFixup> &Fixups) const {
1176 // For ssat instructions, the bit position should be encoded decremented by 1
1177 return MI.getOperand(Op).getImm()-1;
1178}
1179
1180unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001181getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001182 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001183 // VLDM/VSTM:
1184 // {12-8} = Vd
1185 // {7-0} = Number of registers
1186 //
1187 // LDM/STM:
1188 // {15-0} = Bitfield of GPRs.
1189 unsigned Reg = MI.getOperand(Op).getReg();
1190 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1191 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1192
Bill Wendling5e559a22010-11-09 00:30:18 +00001193 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001194
1195 if (SPRRegs || DPRRegs) {
1196 // VLDM/VSTM
1197 unsigned RegNo = getARMRegisterNumbering(Reg);
1198 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1199 Binary |= (RegNo & 0x1f) << 8;
1200 if (SPRRegs)
1201 Binary |= NumRegs;
1202 else
1203 Binary |= NumRegs * 2;
1204 } else {
1205 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1206 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1207 Binary |= 1 << RegNo;
1208 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001209 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001210
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001211 return Binary;
1212}
1213
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001214/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1215/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001216unsigned ARMMCCodeEmitter::
1217getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1218 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001219 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001220 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001221
Owen Andersond9aa7d32010-11-02 00:05:05 +00001222 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001223 unsigned Align = 0;
1224
1225 switch (Imm.getImm()) {
1226 default: break;
1227 case 2:
1228 case 4:
1229 case 8: Align = 0x01; break;
1230 case 16: Align = 0x02; break;
1231 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001232 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001233
Owen Andersond9aa7d32010-11-02 00:05:05 +00001234 return RegNo | (Align << 4);
1235}
1236
Mon P Wang183c6272011-05-09 17:47:27 +00001237/// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1238/// along with the alignment operand for use in VST1 and VLD1 with size 32.
1239unsigned ARMMCCodeEmitter::
1240getAddrMode6OneLane32AddressOpValue(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:
1253 case 16: Align = 0x00; break;
1254 case 32: Align = 0x03; break;
1255 }
1256
1257 return RegNo | (Align << 4);
1258}
1259
1260
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001261/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1262/// alignment operand for use in VLD-dup instructions. This is the same as
1263/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1264/// different for VLD4-dup.
1265unsigned ARMMCCodeEmitter::
1266getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1267 SmallVectorImpl<MCFixup> &Fixups) const {
1268 const MCOperand &Reg = MI.getOperand(Op);
1269 const MCOperand &Imm = MI.getOperand(Op + 1);
1270
1271 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1272 unsigned Align = 0;
1273
1274 switch (Imm.getImm()) {
1275 default: break;
1276 case 2:
1277 case 4:
1278 case 8: Align = 0x01; break;
1279 case 16: Align = 0x03; break;
1280 }
1281
1282 return RegNo | (Align << 4);
1283}
1284
Jim Grosbach806e80e2010-11-03 23:52:49 +00001285unsigned ARMMCCodeEmitter::
1286getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1287 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001288 const MCOperand &MO = MI.getOperand(Op);
1289 if (MO.getReg() == 0) return 0x0D;
1290 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001291}
1292
Bill Wendlinga656b632011-03-01 01:00:59 +00001293unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001294getShiftRight8Imm(const MCInst &MI, unsigned Op,
1295 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001296 return 8 - MI.getOperand(Op).getImm();
1297}
1298
1299unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001300getShiftRight16Imm(const MCInst &MI, unsigned Op,
1301 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001302 return 16 - MI.getOperand(Op).getImm();
1303}
1304
1305unsigned ARMMCCodeEmitter::
Bill Wendling3116dce2011-03-07 23:38:41 +00001306getShiftRight32Imm(const MCInst &MI, unsigned Op,
1307 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlinga656b632011-03-01 01:00:59 +00001308 return 32 - MI.getOperand(Op).getImm();
1309}
1310
Bill Wendling3116dce2011-03-07 23:38:41 +00001311unsigned ARMMCCodeEmitter::
1312getShiftRight64Imm(const MCInst &MI, unsigned Op,
1313 SmallVectorImpl<MCFixup> &Fixups) const {
1314 return 64 - MI.getOperand(Op).getImm();
1315}
1316
Jim Grosbach568eeed2010-09-17 18:46:17 +00001317void ARMMCCodeEmitter::
1318EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001319 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001320 // Pseudo instructions don't get encoded.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001321 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001322 uint64_t TSFlags = Desc.TSFlags;
1323 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001324 return;
Owen Anderson16884412011-07-13 23:22:26 +00001325
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001326 int Size;
Owen Anderson16884412011-07-13 23:22:26 +00001327 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1328 Size = Desc.getSize();
1329 else
1330 llvm_unreachable("Unexpected instruction size!");
1331
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001332 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
Evan Cheng75972122011-01-13 07:58:56 +00001333 // Thumb 32-bit wide instructions need to emit the high order halfword
1334 // first.
Evan Cheng59ee62d2011-07-11 03:57:24 +00001335 if (isThumb() && Size == 4) {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001336 EmitConstant(Binary >> 16, 2, OS);
1337 EmitConstant(Binary & 0xffff, 2, OS);
1338 } else
1339 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001340 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001341}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001342
Jim Grosbach806e80e2010-11-03 23:52:49 +00001343#include "ARMGenMCCodeEmitter.inc"