blob: 48884a1d7182dc77fe26619997f1a3bf1e813098 [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"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000024#include "llvm/ADT/Statistic.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000025#include "llvm/Support/raw_ostream.h"
26using namespace llvm;
27
Jim Grosbach70933262010-11-04 01:12:30 +000028STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
29STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
Jim Grosbachd6d4b422010-10-07 22:12:50 +000030
Jim Grosbach568eeed2010-09-17 18:46:17 +000031namespace {
32class ARMMCCodeEmitter : public MCCodeEmitter {
33 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
34 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
35 const TargetMachine &TM;
36 const TargetInstrInfo &TII;
Evan Chengf3eb3bb2011-01-14 02:38:49 +000037 const ARMSubtarget *Subtarget;
Jim Grosbach568eeed2010-09-17 18:46:17 +000038 MCContext &Ctx;
39
40public:
41 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
Evan Chengf3eb3bb2011-01-14 02:38:49 +000042 : TM(tm), TII(*TM.getInstrInfo()),
43 Subtarget(&TM.getSubtarget<ARMSubtarget>()), Ctx(ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000044 }
45
46 ~ARMMCCodeEmitter() {}
47
Jim Grosbach0de6ab32010-10-12 17:11:26 +000048 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
49
Jim Grosbach9af82ba2010-10-07 21:57:55 +000050 // getBinaryCodeForInstr - TableGen'erated function for getting the
51 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000052 unsigned getBinaryCodeForInstr(const MCInst &MI,
53 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000054
55 /// getMachineOpValue - Return binary encoding of operand. If the machine
56 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000057 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
58 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000059
Evan Cheng75972122011-01-13 07:58:56 +000060 /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
Owen Anderson971b83b2011-02-08 22:39:40 +000061 /// the specified operand. This is used for operands with :lower16: and
Evan Cheng75972122011-01-13 07:58:56 +000062 /// :upper16: prefixes.
63 uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
64 SmallVectorImpl<MCFixup> &Fixups) const;
Jason W Kim837caa92010-11-18 23:37:15 +000065
Bill Wendling92b5a2e2010-11-03 01:49:29 +000066 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000067 unsigned &Reg, unsigned &Imm,
68 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000069
Jim Grosbach662a8162010-12-06 23:57:07 +000070 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +000071 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +000072 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
73 SmallVectorImpl<MCFixup> &Fixups) const;
74
Bill Wendling09aa3f02010-12-09 00:39:08 +000075 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
76 /// BLX branch target.
77 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
78 SmallVectorImpl<MCFixup> &Fixups) const;
79
Jim Grosbache2467172010-12-10 18:21:33 +000080 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
81 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
82 SmallVectorImpl<MCFixup> &Fixups) const;
83
Jim Grosbach01086452010-12-10 17:13:40 +000084 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
85 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
86 SmallVectorImpl<MCFixup> &Fixups) const;
87
Jim Grosbach027d6e82010-12-09 19:04:53 +000088 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
89 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +000090 SmallVectorImpl<MCFixup> &Fixups) const;
91
Jim Grosbachc466b932010-11-11 18:04:49 +000092 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
93 /// branch target.
94 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
95 SmallVectorImpl<MCFixup> &Fixups) const;
96
Owen Andersonc2666002010-12-13 19:31:11 +000097 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
98 /// immediate Thumb2 direct branch target.
99 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
100 SmallVectorImpl<MCFixup> &Fixups) const;
101
Jason W Kim685c3502011-02-04 19:47:15 +0000102 /// getARMBranchTargetOpValue - Return encoding info for 24-bit immediate
103 /// branch target.
104 uint32_t getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
105 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersonc2666002010-12-13 19:31:11 +0000106
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000107 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
108 /// ADR label target.
109 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
110 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachd40963c2010-12-14 22:28:03 +0000111 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
112 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersona838a252010-12-14 00:36:49 +0000113 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
114 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson971b83b2011-02-08 22:39:40 +0000115
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000116
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000117 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
118 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000119 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
120 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000121
Bill Wendlingf4caf692010-12-14 03:36:38 +0000122 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
123 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
124 SmallVectorImpl<MCFixup> &Fixups)const;
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000125
Owen Anderson9d63d902010-12-01 19:18:46 +0000126 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
127 /// operand.
128 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
130
131
Jim Grosbach54fea632010-11-09 17:20:53 +0000132 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
133 /// operand as needed by load/store instructions.
134 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
135 SmallVectorImpl<MCFixup> &Fixups) const;
136
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000137 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
138 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
139 SmallVectorImpl<MCFixup> &Fixups) const {
140 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
141 switch (Mode) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000142 default: assert(0 && "Unknown addressing sub-mode!");
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000143 case ARM_AM::da: return 0;
144 case ARM_AM::ia: return 1;
145 case ARM_AM::db: return 2;
146 case ARM_AM::ib: return 3;
147 }
148 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000149 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
150 ///
151 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
152 switch (ShOpc) {
153 default: llvm_unreachable("Unknown shift opc!");
154 case ARM_AM::no_shift:
155 case ARM_AM::lsl: return 0;
156 case ARM_AM::lsr: return 1;
157 case ARM_AM::asr: return 2;
158 case ARM_AM::ror:
159 case ARM_AM::rrx: return 3;
160 }
161 return 0;
162 }
163
164 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
165 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
166 SmallVectorImpl<MCFixup> &Fixups) const;
167
168 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
169 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
170 SmallVectorImpl<MCFixup> &Fixups) const;
171
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000172 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
173 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
174 SmallVectorImpl<MCFixup> &Fixups) const;
175
Jim Grosbach570a9222010-11-11 01:09:40 +0000176 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
177 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
178 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000179
Jim Grosbachd967cd02010-12-07 21:50:47 +0000180 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
181 /// operand.
182 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
183 SmallVectorImpl<MCFixup> &Fixups) const;
184
Bill Wendlingf4caf692010-12-14 03:36:38 +0000185 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
186 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000187 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000188
Bill Wendlingb8958b02010-12-08 01:57:09 +0000189 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
190 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
191 SmallVectorImpl<MCFixup> &Fixups) const;
192
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000193 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000194 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
195 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000196
Jim Grosbach08bd5492010-10-12 23:00:24 +0000197 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000198 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
199 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000200 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
201 // '1' respectively.
202 return MI.getOperand(Op).getReg() == ARM::CPSR;
203 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000204
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000205 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000206 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
207 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000208 unsigned SoImm = MI.getOperand(Op).getImm();
209 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
210 assert(SoImmVal != -1 && "Not a valid so_imm value!");
211
212 // Encode rotate_imm.
213 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
214 << ARMII::SoRotImmShift;
215
216 // Encode immed_8.
217 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
218 return Binary;
219 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000220
Owen Anderson5de6d842010-11-12 21:12:40 +0000221 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
222 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
223 SmallVectorImpl<MCFixup> &Fixups) const {
224 unsigned SoImm = MI.getOperand(Op).getImm();
225 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
226 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
227 return Encoded;
228 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000229
Owen Anderson75579f72010-11-29 22:44:32 +0000230 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
231 SmallVectorImpl<MCFixup> &Fixups) const;
232 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
233 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000234 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
235 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000236 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
237 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000238
Jim Grosbachef324d72010-10-12 23:53:58 +0000239 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000240 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
241 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000242 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
243 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000244
Jim Grosbach806e80e2010-11-03 23:52:49 +0000245 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
246 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000247 switch (MI.getOperand(Op).getImm()) {
248 default: assert (0 && "Not a valid rot_imm value!");
249 case 0: return 0;
250 case 8: return 1;
251 case 16: return 2;
252 case 24: return 3;
253 }
254 }
255
Jim Grosbach806e80e2010-11-03 23:52:49 +0000256 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
257 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000258 return MI.getOperand(Op).getImm() - 1;
259 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000260
Jim Grosbach806e80e2010-11-03 23:52:49 +0000261 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
262 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000263 return 64 - MI.getOperand(Op).getImm();
264 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000265
Jim Grosbach806e80e2010-11-03 23:52:49 +0000266 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
267 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000268
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +0000269 unsigned getMsbOpValue(const MCInst &MI, unsigned Op,
270 SmallVectorImpl<MCFixup> &Fixups) const;
271
Jim Grosbach806e80e2010-11-03 23:52:49 +0000272 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
273 SmallVectorImpl<MCFixup> &Fixups) const;
274 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
275 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000276 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
277 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000278 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
279 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000280
Bill Wendlinga656b632011-03-01 01:00:59 +0000281 unsigned getNarrowShiftRight16Imm(const MCInst &MI, unsigned Op,
282 SmallVectorImpl<MCFixup> &Fixups) const;
283 unsigned getNarrowShiftRight32Imm(const MCInst &MI, unsigned Op,
284 SmallVectorImpl<MCFixup> &Fixups) const;
285 unsigned getNarrowShiftRight64Imm(const MCInst &MI, unsigned Op,
286 SmallVectorImpl<MCFixup> &Fixups) const;
287
Owen Andersonc7139a62010-11-11 19:07:48 +0000288 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
289 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000290 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000291 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000292 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000293 unsigned EncodedValue) const;
294
295 unsigned VFPThumb2PostEncoder(const MCInst &MI,
296 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000297
Jim Grosbach70933262010-11-04 01:12:30 +0000298 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000299 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000300 }
301
Jim Grosbach70933262010-11-04 01:12:30 +0000302 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000303 // Output the constant in little endian byte order.
304 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000305 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000306 Val >>= 8;
307 }
308 }
309
Jim Grosbach568eeed2010-09-17 18:46:17 +0000310 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
311 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000312};
313
314} // end anonymous namespace
315
Bill Wendling0800ce72010-11-02 22:53:11 +0000316MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
317 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000318 return new ARMMCCodeEmitter(TM, Ctx);
319}
320
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000321/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
322/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000323/// Thumb2 mode.
324unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
325 unsigned EncodedValue) const {
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000326 if (Subtarget->isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000327 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000328 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
329 // set to 1111.
330 unsigned Bit24 = EncodedValue & 0x01000000;
331 unsigned Bit28 = Bit24 << 4;
332 EncodedValue &= 0xEFFFFFFF;
333 EncodedValue |= Bit28;
334 EncodedValue |= 0x0F000000;
335 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000336
Owen Andersonc7139a62010-11-11 19:07:48 +0000337 return EncodedValue;
338}
339
Owen Anderson57dac882010-11-11 21:36:43 +0000340/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000341/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000342/// Thumb2 mode.
343unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
344 unsigned EncodedValue) const {
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000345 if (Subtarget->isThumb2()) {
Owen Anderson57dac882010-11-11 21:36:43 +0000346 EncodedValue &= 0xF0FFFFFF;
347 EncodedValue |= 0x09000000;
348 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000349
Owen Anderson57dac882010-11-11 21:36:43 +0000350 return EncodedValue;
351}
352
Owen Anderson8f143912010-11-11 23:12:55 +0000353/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000354/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000355/// Thumb2 mode.
356unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
357 unsigned EncodedValue) const {
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000358 if (Subtarget->isThumb2()) {
Owen Anderson8f143912010-11-11 23:12:55 +0000359 EncodedValue &= 0x00FFFFFF;
360 EncodedValue |= 0xEE000000;
361 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000362
Owen Anderson8f143912010-11-11 23:12:55 +0000363 return EncodedValue;
364}
365
Bill Wendlingcf590262010-12-01 21:54:50 +0000366/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
367/// them to their Thumb2 form if we are currently in Thumb2 mode.
368unsigned ARMMCCodeEmitter::
369VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000370 if (Subtarget->isThumb2()) {
Bill Wendlingcf590262010-12-01 21:54:50 +0000371 EncodedValue &= 0x0FFFFFFF;
372 EncodedValue |= 0xE0000000;
373 }
374 return EncodedValue;
375}
Owen Anderson57dac882010-11-11 21:36:43 +0000376
Jim Grosbach56ac9072010-10-08 21:45:55 +0000377/// getMachineOpValue - Return binary encoding of operand. If the machine
378/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000379unsigned ARMMCCodeEmitter::
380getMachineOpValue(const MCInst &MI, const MCOperand &MO,
381 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000382 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000383 unsigned Reg = MO.getReg();
384 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000385
Jim Grosbachb0708d22010-11-30 23:51:41 +0000386 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000387 switch (Reg) {
388 default:
389 return RegNo;
390 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
391 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
392 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
393 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
394 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000395 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000396 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000397 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000398 } else if (MO.isFPImm()) {
399 return static_cast<unsigned>(APFloat(MO.getFPImm())
400 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000401 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000402
Jim Grosbach817c1a62010-11-19 00:27:09 +0000403 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000404 return 0;
405}
406
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000407/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000408bool ARMMCCodeEmitter::
409EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
410 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000411 const MCOperand &MO = MI.getOperand(OpIdx);
412 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000413
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000414 Reg = getARMRegisterNumbering(MO.getReg());
415
416 int32_t SImm = MO1.getImm();
417 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000418
Jim Grosbachab682a22010-10-28 18:34:10 +0000419 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000420 if (SImm == INT32_MIN)
421 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000422
Jim Grosbachab682a22010-10-28 18:34:10 +0000423 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000424 if (SImm < 0) {
425 SImm = -SImm;
426 isAdd = false;
427 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000428
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000429 Imm = SImm;
430 return isAdd;
431}
432
Bill Wendlingdff2f712010-12-08 23:01:43 +0000433/// getBranchTargetOpValue - Helper function to get the branch target operand,
434/// which is either an immediate or requires a fixup.
435static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
436 unsigned FixupKind,
437 SmallVectorImpl<MCFixup> &Fixups) {
438 const MCOperand &MO = MI.getOperand(OpIdx);
439
440 // If the destination is an immediate, we have nothing to do.
441 if (MO.isImm()) return MO.getImm();
442 assert(MO.isExpr() && "Unexpected branch target type!");
443 const MCExpr *Expr = MO.getExpr();
444 MCFixupKind Kind = MCFixupKind(FixupKind);
445 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
446
447 // All of the information is in the fixup.
448 return 0;
449}
450
451/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000452uint32_t ARMMCCodeEmitter::
453getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
454 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000455 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000456}
457
Bill Wendling09aa3f02010-12-09 00:39:08 +0000458/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
459/// BLX branch target.
460uint32_t ARMMCCodeEmitter::
461getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
462 SmallVectorImpl<MCFixup> &Fixups) const {
463 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
464}
465
Jim Grosbache2467172010-12-10 18:21:33 +0000466/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
467uint32_t ARMMCCodeEmitter::
468getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
469 SmallVectorImpl<MCFixup> &Fixups) const {
470 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
471}
472
Jim Grosbach01086452010-12-10 17:13:40 +0000473/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
474uint32_t ARMMCCodeEmitter::
475getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000476 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000477 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
478}
479
Jim Grosbach027d6e82010-12-09 19:04:53 +0000480/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000481uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000482getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000483 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000484 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000485}
486
Jason W Kim685c3502011-02-04 19:47:15 +0000487/// Return true if this branch has a non-always predication
488static bool HasConditionalBranch(const MCInst &MI) {
489 int NumOp = MI.getNumOperands();
490 if (NumOp >= 2) {
491 for (int i = 0; i < NumOp-1; ++i) {
492 const MCOperand &MCOp1 = MI.getOperand(i);
493 const MCOperand &MCOp2 = MI.getOperand(i + 1);
494 if (MCOp1.isImm() && MCOp2.isReg() &&
495 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
496 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
497 return true;
498 }
499 }
500 }
501 return false;
502}
503
Bill Wendlingdff2f712010-12-08 23:01:43 +0000504/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
505/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000506uint32_t ARMMCCodeEmitter::
507getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000508 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000509 // FIXME: This really, really shouldn't use TargetMachine. We don't want
510 // coupling between MC and TM anywhere we can help it.
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000511 if (Subtarget->isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000512 return
513 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Jason W Kim685c3502011-02-04 19:47:15 +0000514 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000515}
516
Jason W Kim685c3502011-02-04 19:47:15 +0000517/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
518/// target.
519uint32_t ARMMCCodeEmitter::
520getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
521 SmallVectorImpl<MCFixup> &Fixups) const {
522 if (HasConditionalBranch(MI))
523 return ::getBranchTargetOpValue(MI, OpIdx,
524 ARM::fixup_arm_condbranch, Fixups);
525 return ::getBranchTargetOpValue(MI, OpIdx,
526 ARM::fixup_arm_uncondbranch, Fixups);
527}
528
529
530
531
Owen Andersonc2666002010-12-13 19:31:11 +0000532/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
533/// immediate branch target.
534uint32_t ARMMCCodeEmitter::
535getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
536 SmallVectorImpl<MCFixup> &Fixups) const {
537 unsigned Val =
538 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
539 bool I = (Val & 0x800000);
540 bool J1 = (Val & 0x400000);
541 bool J2 = (Val & 0x200000);
542 if (I ^ J1)
543 Val &= ~0x400000;
544 else
545 Val |= 0x400000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000546
Owen Andersonc2666002010-12-13 19:31:11 +0000547 if (I ^ J2)
548 Val &= ~0x200000;
549 else
550 Val |= 0x200000;
Owen Anderson971b83b2011-02-08 22:39:40 +0000551
Owen Andersonc2666002010-12-13 19:31:11 +0000552 return Val;
553}
554
Bill Wendlingdff2f712010-12-08 23:01:43 +0000555/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
556/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000557uint32_t ARMMCCodeEmitter::
558getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
559 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000560 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
561 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
562 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000563}
564
Owen Andersona838a252010-12-14 00:36:49 +0000565/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
566/// target.
567uint32_t ARMMCCodeEmitter::
568getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
569 SmallVectorImpl<MCFixup> &Fixups) const {
570 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
571 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
572 Fixups);
573}
574
Jim Grosbachd40963c2010-12-14 22:28:03 +0000575/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
576/// target.
577uint32_t ARMMCCodeEmitter::
578getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
579 SmallVectorImpl<MCFixup> &Fixups) const {
580 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
581 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
582 Fixups);
583}
584
Bill Wendlingf4caf692010-12-14 03:36:38 +0000585/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
586/// operand.
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000587uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000588getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
589 SmallVectorImpl<MCFixup> &) const {
590 // [Rn, Rm]
591 // {5-3} = Rm
592 // {2-0} = Rn
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000593 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000594 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000595 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
596 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
597 return (Rm << 3) | Rn;
598}
599
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000600/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000601uint32_t ARMMCCodeEmitter::
602getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
603 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000604 // {17-13} = reg
605 // {12} = (U)nsigned (add == '1', sub == '0')
606 // {11-0} = imm12
607 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000608 bool isAdd = true;
609 // If The first operand isn't a register, we have a label reference.
610 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Anderson971b83b2011-02-08 22:39:40 +0000611 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000612 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000613 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000614 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000615
Owen Anderson971b83b2011-02-08 22:39:40 +0000616 assert(MO.isExpr() && "Unexpected machine operand type!");
617 const MCExpr *Expr = MO.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000618
Owen Andersond7b3f582010-12-09 01:51:07 +0000619 MCFixupKind Kind;
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000620 if (Subtarget->isThumb2())
Owen Andersond7b3f582010-12-09 01:51:07 +0000621 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
622 else
623 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000624 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
625
626 ++MCNumCPRelocations;
627 } else
628 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000629
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000630 uint32_t Binary = Imm12 & 0xfff;
631 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000632 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000633 Binary |= (1 << 12);
634 Binary |= (Reg << 13);
635 return Binary;
636}
637
Owen Anderson9d63d902010-12-01 19:18:46 +0000638/// getT2AddrModeImm8s4OpValue - Return encoding info for
639/// 'reg +/- imm8<<2' operand.
640uint32_t ARMMCCodeEmitter::
641getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
642 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000643 // {12-9} = reg
644 // {8} = (U)nsigned (add == '1', sub == '0')
645 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000646 unsigned Reg, Imm8;
647 bool isAdd = true;
648 // If The first operand isn't a register, we have a label reference.
649 const MCOperand &MO = MI.getOperand(OpIdx);
650 if (!MO.isReg()) {
651 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
652 Imm8 = 0;
653 isAdd = false ; // 'U' bit is set as part of the fixup.
654
655 assert(MO.isExpr() && "Unexpected machine operand type!");
656 const MCExpr *Expr = MO.getExpr();
657 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
658 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
659
660 ++MCNumCPRelocations;
661 } else
662 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
663
664 uint32_t Binary = (Imm8 >> 2) & 0xff;
665 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
666 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000667 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000668 Binary |= (Reg << 9);
669 return Binary;
670}
671
Jason W Kim86a97f22011-01-12 00:19:25 +0000672// FIXME: This routine assumes that a binary
673// expression will always result in a PCRel expression
674// In reality, its only true if one or more subexpressions
675// is itself a PCRel (i.e. "." in asm or some other pcrel construct)
676// but this is good enough for now.
677static bool EvaluateAsPCRel(const MCExpr *Expr) {
678 switch (Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000679 default: assert(0 && "Unexpected expression type");
Jason W Kim86a97f22011-01-12 00:19:25 +0000680 case MCExpr::SymbolRef: return false;
681 case MCExpr::Binary: return true;
Jason W Kim86a97f22011-01-12 00:19:25 +0000682 }
683}
684
Evan Cheng75972122011-01-13 07:58:56 +0000685uint32_t
686ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
687 SmallVectorImpl<MCFixup> &Fixups) const {
Jason W Kim837caa92010-11-18 23:37:15 +0000688 // {20-16} = imm{15-12}
689 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000690 const MCOperand &MO = MI.getOperand(OpIdx);
Evan Cheng75972122011-01-13 07:58:56 +0000691 if (MO.isImm())
692 // Hi / lo 16 bits already extracted during earlier passes.
Jason W Kim837caa92010-11-18 23:37:15 +0000693 return static_cast<unsigned>(MO.getImm());
Evan Cheng75972122011-01-13 07:58:56 +0000694
695 // Handle :upper16: and :lower16: assembly prefixes.
696 const MCExpr *E = MO.getExpr();
697 if (E->getKind() == MCExpr::Target) {
698 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
699 E = ARM16Expr->getSubExpr();
700
Jason W Kim837caa92010-11-18 23:37:15 +0000701 MCFixupKind Kind;
Evan Cheng75972122011-01-13 07:58:56 +0000702 switch (ARM16Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000703 default: assert(0 && "Unsupported ARMFixup");
Evan Cheng75972122011-01-13 07:58:56 +0000704 case ARMMCExpr::VK_ARM_HI16:
Owen Anderson971b83b2011-02-08 22:39:40 +0000705 if (!Subtarget->isTargetDarwin() && EvaluateAsPCRel(E))
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000706 Kind = MCFixupKind(Subtarget->isThumb2()
707 ? ARM::fixup_t2_movt_hi16_pcrel
708 : ARM::fixup_arm_movt_hi16_pcrel);
709 else
710 Kind = MCFixupKind(Subtarget->isThumb2()
711 ? ARM::fixup_t2_movt_hi16
712 : ARM::fixup_arm_movt_hi16);
Jason W Kim837caa92010-11-18 23:37:15 +0000713 break;
Evan Cheng75972122011-01-13 07:58:56 +0000714 case ARMMCExpr::VK_ARM_LO16:
Owen Anderson971b83b2011-02-08 22:39:40 +0000715 if (!Subtarget->isTargetDarwin() && EvaluateAsPCRel(E))
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000716 Kind = MCFixupKind(Subtarget->isThumb2()
717 ? ARM::fixup_t2_movw_lo16_pcrel
718 : ARM::fixup_arm_movw_lo16_pcrel);
719 else
720 Kind = MCFixupKind(Subtarget->isThumb2()
721 ? ARM::fixup_t2_movw_lo16
722 : ARM::fixup_arm_movw_lo16);
Jason W Kim837caa92010-11-18 23:37:15 +0000723 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000724 }
Evan Cheng75972122011-01-13 07:58:56 +0000725 Fixups.push_back(MCFixup::Create(0, E, Kind));
Jason W Kim837caa92010-11-18 23:37:15 +0000726 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000727 };
Evan Cheng75972122011-01-13 07:58:56 +0000728
Jim Grosbach817c1a62010-11-19 00:27:09 +0000729 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000730 return 0;
731}
732
733uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000734getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
735 SmallVectorImpl<MCFixup> &Fixups) const {
736 const MCOperand &MO = MI.getOperand(OpIdx);
737 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
738 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
739 unsigned Rn = getARMRegisterNumbering(MO.getReg());
740 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000741 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
742 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000743 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
744 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000745
746 // {16-13} = Rn
747 // {12} = isAdd
748 // {11-0} = shifter
749 // {3-0} = Rm
750 // {4} = 0
751 // {6-5} = type
752 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000753 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000754 Binary |= Rn << 13;
755 Binary |= SBits << 5;
756 Binary |= ShImm << 7;
757 if (isAdd)
758 Binary |= 1 << 12;
759 return Binary;
760}
761
Jim Grosbach570a9222010-11-11 01:09:40 +0000762uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000763getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
764 SmallVectorImpl<MCFixup> &Fixups) const {
765 // {17-14} Rn
766 // {13} 1 == imm12, 0 == Rm
767 // {12} isAdd
768 // {11-0} imm12/Rm
769 const MCOperand &MO = MI.getOperand(OpIdx);
770 unsigned Rn = getARMRegisterNumbering(MO.getReg());
771 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
772 Binary |= Rn << 14;
773 return Binary;
774}
775
776uint32_t ARMMCCodeEmitter::
777getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
778 SmallVectorImpl<MCFixup> &Fixups) const {
779 // {13} 1 == imm12, 0 == Rm
780 // {12} isAdd
781 // {11-0} imm12/Rm
782 const MCOperand &MO = MI.getOperand(OpIdx);
783 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
784 unsigned Imm = MO1.getImm();
785 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
786 bool isReg = MO.getReg() != 0;
787 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
788 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
789 if (isReg) {
790 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
791 Binary <<= 7; // Shift amount is bits [11:7]
792 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
793 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
794 }
795 return Binary | (isAdd << 12) | (isReg << 13);
796}
797
798uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000799getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
800 SmallVectorImpl<MCFixup> &Fixups) const {
801 // {9} 1 == imm8, 0 == Rm
802 // {8} isAdd
803 // {7-4} imm7_4/zero
804 // {3-0} imm3_0/Rm
805 const MCOperand &MO = MI.getOperand(OpIdx);
806 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
807 unsigned Imm = MO1.getImm();
808 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
809 bool isImm = MO.getReg() == 0;
810 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
811 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
812 if (!isImm)
813 Imm8 = getARMRegisterNumbering(MO.getReg());
814 return Imm8 | (isAdd << 8) | (isImm << 9);
815}
816
817uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000818getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
819 SmallVectorImpl<MCFixup> &Fixups) const {
820 // {13} 1 == imm8, 0 == Rm
821 // {12-9} Rn
822 // {8} isAdd
823 // {7-4} imm7_4/zero
824 // {3-0} imm3_0/Rm
825 const MCOperand &MO = MI.getOperand(OpIdx);
826 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
827 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
828 unsigned Rn = getARMRegisterNumbering(MO.getReg());
829 unsigned Imm = MO2.getImm();
830 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
831 bool isImm = MO1.getReg() == 0;
832 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
833 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
834 if (!isImm)
835 Imm8 = getARMRegisterNumbering(MO1.getReg());
836 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
837}
838
Bill Wendlingb8958b02010-12-08 01:57:09 +0000839/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000840uint32_t ARMMCCodeEmitter::
841getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
842 SmallVectorImpl<MCFixup> &Fixups) const {
843 // [SP, #imm]
844 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000845 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000846 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
847 "Unexpected base register!");
Bill Wendling7a905a82010-12-15 23:32:27 +0000848
Jim Grosbachd967cd02010-12-07 21:50:47 +0000849 // The immediate is already shifted for the implicit zeroes, so no change
850 // here.
851 return MO1.getImm() & 0xff;
852}
853
Bill Wendlingf4caf692010-12-14 03:36:38 +0000854/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000855uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000856getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000857 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000858 // [Rn, #imm]
859 // {7-3} = imm5
860 // {2-0} = Rn
861 const MCOperand &MO = MI.getOperand(OpIdx);
862 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000863 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Matt Beaumont-Gay656b3d22010-12-16 01:34:26 +0000864 unsigned Imm5 = MO1.getImm();
Bill Wendling272df512010-12-09 21:49:07 +0000865 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000866}
867
Bill Wendlingb8958b02010-12-08 01:57:09 +0000868/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
869uint32_t ARMMCCodeEmitter::
870getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
871 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000872 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000873}
874
Jim Grosbach5177f792010-12-01 21:09:40 +0000875/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000876uint32_t ARMMCCodeEmitter::
877getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
878 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000879 // {12-9} = reg
880 // {8} = (U)nsigned (add == '1', sub == '0')
881 // {7-0} = imm8
882 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000883 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000884 // If The first operand isn't a register, we have a label reference.
885 const MCOperand &MO = MI.getOperand(OpIdx);
886 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000887 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000888 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000889 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000890
891 assert(MO.isExpr() && "Unexpected machine operand type!");
892 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000893 MCFixupKind Kind;
Evan Chengf3eb3bb2011-01-14 02:38:49 +0000894 if (Subtarget->isThumb2())
Owen Andersond8e351b2010-12-08 00:18:36 +0000895 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
896 else
897 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000898 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
899
900 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000901 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000902 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000903 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
904 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000905
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000906 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
907 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000908 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000909 Binary |= (1 << 8);
910 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000911 return Binary;
912}
913
Jim Grosbach806e80e2010-11-03 23:52:49 +0000914unsigned ARMMCCodeEmitter::
915getSORegOpValue(const MCInst &MI, unsigned OpIdx,
916 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000917 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
918 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
919 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000920 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000921 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000922 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000923 // {6-5} = type
924 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000925 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000926 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000927 // else (imm shift)
928 // {11-7} = imm
929
930 const MCOperand &MO = MI.getOperand(OpIdx);
931 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
932 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
933 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
934
935 // Encode Rm.
936 unsigned Binary = getARMRegisterNumbering(MO.getReg());
937
938 // Encode the shift opcode.
939 unsigned SBits = 0;
940 unsigned Rs = MO1.getReg();
941 if (Rs) {
942 // Set shift operand (bit[7:4]).
943 // LSL - 0001
944 // LSR - 0011
945 // ASR - 0101
946 // ROR - 0111
947 // RRX - 0110 and bit[11:8] clear.
948 switch (SOpc) {
949 default: llvm_unreachable("Unknown shift opc!");
950 case ARM_AM::lsl: SBits = 0x1; break;
951 case ARM_AM::lsr: SBits = 0x3; break;
952 case ARM_AM::asr: SBits = 0x5; break;
953 case ARM_AM::ror: SBits = 0x7; break;
954 case ARM_AM::rrx: SBits = 0x6; break;
955 }
956 } else {
957 // Set shift operand (bit[6:4]).
958 // LSL - 000
959 // LSR - 010
960 // ASR - 100
961 // ROR - 110
962 switch (SOpc) {
963 default: llvm_unreachable("Unknown shift opc!");
964 case ARM_AM::lsl: SBits = 0x0; break;
965 case ARM_AM::lsr: SBits = 0x2; break;
966 case ARM_AM::asr: SBits = 0x4; break;
967 case ARM_AM::ror: SBits = 0x6; break;
968 }
969 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000970
Jim Grosbachef324d72010-10-12 23:53:58 +0000971 Binary |= SBits << 4;
972 if (SOpc == ARM_AM::rrx)
973 return Binary;
974
975 // Encode the shift operation Rs or shift_imm (except rrx).
976 if (Rs) {
977 // Encode Rs bit[11:8].
978 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
979 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
980 }
981
982 // Encode shift_imm bit[11:7].
983 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
984}
985
Jim Grosbach806e80e2010-11-03 23:52:49 +0000986unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000987getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
988 SmallVectorImpl<MCFixup> &Fixups) const {
989 const MCOperand &MO1 = MI.getOperand(OpNum);
990 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000991 const MCOperand &MO3 = MI.getOperand(OpNum+2);
992
Owen Anderson75579f72010-11-29 22:44:32 +0000993 // Encoded as [Rn, Rm, imm].
994 // FIXME: Needs fixup support.
995 unsigned Value = getARMRegisterNumbering(MO1.getReg());
996 Value <<= 4;
997 Value |= getARMRegisterNumbering(MO2.getReg());
998 Value <<= 2;
999 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001000
Owen Anderson75579f72010-11-29 22:44:32 +00001001 return Value;
1002}
1003
1004unsigned ARMMCCodeEmitter::
1005getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1006 SmallVectorImpl<MCFixup> &Fixups) const {
1007 const MCOperand &MO1 = MI.getOperand(OpNum);
1008 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1009
1010 // FIXME: Needs fixup support.
1011 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +00001012
Owen Anderson75579f72010-11-29 22:44:32 +00001013 // Even though the immediate is 8 bits long, we need 9 bits in order
1014 // to represent the (inverse of the) sign bit.
1015 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +00001016 int32_t tmp = (int32_t)MO2.getImm();
1017 if (tmp < 0)
1018 tmp = abs(tmp);
1019 else
1020 Value |= 256; // Set the ADD bit
1021 Value |= tmp & 255;
1022 return Value;
1023}
1024
1025unsigned ARMMCCodeEmitter::
1026getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1027 SmallVectorImpl<MCFixup> &Fixups) const {
1028 const MCOperand &MO1 = MI.getOperand(OpNum);
1029
1030 // FIXME: Needs fixup support.
1031 unsigned Value = 0;
1032 int32_t tmp = (int32_t)MO1.getImm();
1033 if (tmp < 0)
1034 tmp = abs(tmp);
1035 else
1036 Value |= 256; // Set the ADD bit
1037 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +00001038 return Value;
1039}
1040
1041unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +00001042getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1043 SmallVectorImpl<MCFixup> &Fixups) const {
1044 const MCOperand &MO1 = MI.getOperand(OpNum);
1045
1046 // FIXME: Needs fixup support.
1047 unsigned Value = 0;
1048 int32_t tmp = (int32_t)MO1.getImm();
1049 if (tmp < 0)
1050 tmp = abs(tmp);
1051 else
1052 Value |= 4096; // Set the ADD bit
1053 Value |= tmp & 4095;
1054 return Value;
1055}
1056
1057unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001058getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1059 SmallVectorImpl<MCFixup> &Fixups) const {
1060 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1061 // shifted. The second is the amount to shift by.
1062 //
1063 // {3-0} = Rm.
1064 // {4} = 0
1065 // {6-5} = type
1066 // {11-7} = imm
1067
1068 const MCOperand &MO = MI.getOperand(OpIdx);
1069 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1070 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1071
1072 // Encode Rm.
1073 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1074
1075 // Encode the shift opcode.
1076 unsigned SBits = 0;
1077 // Set shift operand (bit[6:4]).
1078 // LSL - 000
1079 // LSR - 010
1080 // ASR - 100
1081 // ROR - 110
1082 switch (SOpc) {
1083 default: llvm_unreachable("Unknown shift opc!");
1084 case ARM_AM::lsl: SBits = 0x0; break;
1085 case ARM_AM::lsr: SBits = 0x2; break;
1086 case ARM_AM::asr: SBits = 0x4; break;
1087 case ARM_AM::ror: SBits = 0x6; break;
1088 }
1089
1090 Binary |= SBits << 4;
1091 if (SOpc == ARM_AM::rrx)
1092 return Binary;
1093
1094 // Encode shift_imm bit[11:7].
1095 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1096}
1097
1098unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001099getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1100 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001101 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1102 // msb of the mask.
1103 const MCOperand &MO = MI.getOperand(Op);
1104 uint32_t v = ~MO.getImm();
1105 uint32_t lsb = CountTrailingZeros_32(v);
1106 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1107 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1108 return lsb | (msb << 5);
1109}
1110
Jim Grosbach806e80e2010-11-03 23:52:49 +00001111unsigned ARMMCCodeEmitter::
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +00001112getMsbOpValue(const MCInst &MI, unsigned Op,
1113 SmallVectorImpl<MCFixup> &Fixups) const {
1114 // MSB - 5 bits.
1115 uint32_t lsb = MI.getOperand(Op-1).getImm();
1116 uint32_t width = MI.getOperand(Op).getImm();
1117 uint32_t msb = lsb+width-1;
1118 assert (width != 0 && msb < 32 && "Illegal bit width!");
1119 return msb;
1120}
1121
1122unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001123getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001124 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001125 // VLDM/VSTM:
1126 // {12-8} = Vd
1127 // {7-0} = Number of registers
1128 //
1129 // LDM/STM:
1130 // {15-0} = Bitfield of GPRs.
1131 unsigned Reg = MI.getOperand(Op).getReg();
1132 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1133 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1134
Bill Wendling5e559a22010-11-09 00:30:18 +00001135 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001136
1137 if (SPRRegs || DPRRegs) {
1138 // VLDM/VSTM
1139 unsigned RegNo = getARMRegisterNumbering(Reg);
1140 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1141 Binary |= (RegNo & 0x1f) << 8;
1142 if (SPRRegs)
1143 Binary |= NumRegs;
1144 else
1145 Binary |= NumRegs * 2;
1146 } else {
1147 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1148 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1149 Binary |= 1 << RegNo;
1150 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001151 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001152
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001153 return Binary;
1154}
1155
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001156/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1157/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001158unsigned ARMMCCodeEmitter::
1159getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1160 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001161 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001162 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001163
Owen Andersond9aa7d32010-11-02 00:05:05 +00001164 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001165 unsigned Align = 0;
1166
1167 switch (Imm.getImm()) {
1168 default: break;
1169 case 2:
1170 case 4:
1171 case 8: Align = 0x01; break;
1172 case 16: Align = 0x02; break;
1173 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001174 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001175
Owen Andersond9aa7d32010-11-02 00:05:05 +00001176 return RegNo | (Align << 4);
1177}
1178
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001179/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1180/// alignment operand for use in VLD-dup instructions. This is the same as
1181/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1182/// different for VLD4-dup.
1183unsigned ARMMCCodeEmitter::
1184getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1185 SmallVectorImpl<MCFixup> &Fixups) const {
1186 const MCOperand &Reg = MI.getOperand(Op);
1187 const MCOperand &Imm = MI.getOperand(Op + 1);
1188
1189 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1190 unsigned Align = 0;
1191
1192 switch (Imm.getImm()) {
1193 default: break;
1194 case 2:
1195 case 4:
1196 case 8: Align = 0x01; break;
1197 case 16: Align = 0x03; break;
1198 }
1199
1200 return RegNo | (Align << 4);
1201}
1202
Jim Grosbach806e80e2010-11-03 23:52:49 +00001203unsigned ARMMCCodeEmitter::
1204getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1205 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001206 const MCOperand &MO = MI.getOperand(Op);
1207 if (MO.getReg() == 0) return 0x0D;
1208 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001209}
1210
Bill Wendlinga656b632011-03-01 01:00:59 +00001211unsigned ARMMCCodeEmitter::
1212getNarrowShiftRight16Imm(const MCInst &MI, unsigned Op,
1213 SmallVectorImpl<MCFixup> &Fixups) const {
1214 return 8 - MI.getOperand(Op).getImm();
1215}
1216
1217unsigned ARMMCCodeEmitter::
1218getNarrowShiftRight32Imm(const MCInst &MI, unsigned Op,
1219 SmallVectorImpl<MCFixup> &Fixups) const {
1220 return 16 - MI.getOperand(Op).getImm();
1221}
1222
1223unsigned ARMMCCodeEmitter::
1224getNarrowShiftRight64Imm(const MCInst &MI, unsigned Op,
1225 SmallVectorImpl<MCFixup> &Fixups) const {
1226 return 32 - MI.getOperand(Op).getImm();
1227}
1228
Jim Grosbach568eeed2010-09-17 18:46:17 +00001229void ARMMCCodeEmitter::
1230EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001231 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001232 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001233 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001234 uint64_t TSFlags = Desc.TSFlags;
1235 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001236 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001237 int Size;
1238 // Basic size info comes from the TSFlags field.
1239 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1240 default: llvm_unreachable("Unexpected instruction size!");
1241 case ARMII::Size2Bytes: Size = 2; break;
1242 case ARMII::Size4Bytes: Size = 4; break;
1243 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001244 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
Evan Cheng75972122011-01-13 07:58:56 +00001245 // Thumb 32-bit wide instructions need to emit the high order halfword
1246 // first.
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001247 if (Subtarget->isThumb() && Size == 4) {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001248 EmitConstant(Binary >> 16, 2, OS);
1249 EmitConstant(Binary & 0xffff, 2, OS);
1250 } else
1251 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001252 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001253}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001254
Jim Grosbach806e80e2010-11-03 23:52:49 +00001255#include "ARMGenMCCodeEmitter.inc"