blob: d6c3b502c30256cc6d457c9728209100a86a3799 [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"
Jim Grosbach568eeed2010-09-17 18:46:17 +000019#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000022#include "llvm/ADT/Statistic.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000023#include "llvm/Support/raw_ostream.h"
24using namespace llvm;
25
Jim Grosbach70933262010-11-04 01:12:30 +000026STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
27STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
Jim Grosbachd6d4b422010-10-07 22:12:50 +000028
Jim Grosbach568eeed2010-09-17 18:46:17 +000029namespace {
30class ARMMCCodeEmitter : public MCCodeEmitter {
31 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
32 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
33 const TargetMachine &TM;
34 const TargetInstrInfo &TII;
35 MCContext &Ctx;
36
37public:
38 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
39 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000040 }
41
42 ~ARMMCCodeEmitter() {}
43
Jim Grosbachc466b932010-11-11 18:04:49 +000044 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
Jim Grosbach70933262010-11-04 01:12:30 +000045
46 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
47 const static MCFixupKindInfo Infos[] = {
Jim Grosbachdff84b02010-12-02 00:28:45 +000048 // name off bits flags
49 { "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
50 { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersond8e351b2010-12-08 00:18:36 +000051 { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000052 { "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
53 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach662a8162010-12-06 23:57:07 +000054 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendlingb8958b02010-12-08 01:57:09 +000055 { "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000056 { "fixup_arm_movt_hi16", 0, 16, 0 },
57 { "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000058 };
59
60 if (Kind < FirstTargetFixupKind)
61 return MCCodeEmitter::getFixupKindInfo(Kind);
62
63 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
64 "Invalid kind!");
65 return Infos[Kind - FirstTargetFixupKind];
66 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000067 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
68
Jim Grosbach9af82ba2010-10-07 21:57:55 +000069 // getBinaryCodeForInstr - TableGen'erated function for getting the
70 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000071 unsigned getBinaryCodeForInstr(const MCInst &MI,
72 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000073
74 /// getMachineOpValue - Return binary encoding of operand. If the machine
75 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000076 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
77 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000078
Jason W Kim837caa92010-11-18 23:37:15 +000079 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
80 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
81 SmallVectorImpl<MCFixup> &Fixups) const;
82
Bill Wendling92b5a2e2010-11-03 01:49:29 +000083 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000084 unsigned &Reg, unsigned &Imm,
85 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000086
Jim Grosbach662a8162010-12-06 23:57:07 +000087 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
88 /// branch target.
89 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
90 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
Jim Grosbach5d14f9b2010-12-01 19:47:31 +000097 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
98 /// ADR label target.
99 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
100 SmallVectorImpl<MCFixup> &Fixups) const;
101
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000102 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
103 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000104 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
105 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000106
Owen Anderson9d63d902010-12-01 19:18:46 +0000107 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
108 /// operand.
109 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
110 SmallVectorImpl<MCFixup> &Fixups) const;
111
112
Jim Grosbach54fea632010-11-09 17:20:53 +0000113 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
114 /// operand as needed by load/store instructions.
115 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
116 SmallVectorImpl<MCFixup> &Fixups) const;
117
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000118 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
119 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
120 SmallVectorImpl<MCFixup> &Fixups) const {
121 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
122 switch (Mode) {
123 default: assert(0 && "Unknown addressing sub-mode!");
124 case ARM_AM::da: return 0;
125 case ARM_AM::ia: return 1;
126 case ARM_AM::db: return 2;
127 case ARM_AM::ib: return 3;
128 }
129 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000130 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
131 ///
132 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
133 switch (ShOpc) {
134 default: llvm_unreachable("Unknown shift opc!");
135 case ARM_AM::no_shift:
136 case ARM_AM::lsl: return 0;
137 case ARM_AM::lsr: return 1;
138 case ARM_AM::asr: return 2;
139 case ARM_AM::ror:
140 case ARM_AM::rrx: return 3;
141 }
142 return 0;
143 }
144
145 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
146 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
147 SmallVectorImpl<MCFixup> &Fixups) const;
148
149 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
150 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
151 SmallVectorImpl<MCFixup> &Fixups) const;
152
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000153 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
154 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
155 SmallVectorImpl<MCFixup> &Fixups) const;
156
Jim Grosbach570a9222010-11-11 01:09:40 +0000157 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
158 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
159 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000160
Jim Grosbachd967cd02010-12-07 21:50:47 +0000161 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
162 /// operand.
163 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
164 SmallVectorImpl<MCFixup> &Fixups) const;
165
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000166 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
167 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
168 SmallVectorImpl<MCFixup> &Fixups) const;
169
Bill Wendling1fd374e2010-11-30 22:57:21 +0000170 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
171 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
172 SmallVectorImpl<MCFixup> &Fixups) const;
173
174 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
175 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
176 SmallVectorImpl<MCFixup> &Fixups) const;
177
Bill Wendlingb8958b02010-12-08 01:57:09 +0000178 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
179 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
180 SmallVectorImpl<MCFixup> &Fixups) const;
181
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000182 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000183 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
184 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000185
Jim Grosbach08bd5492010-10-12 23:00:24 +0000186 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000187 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
188 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000189 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
190 // '1' respectively.
191 return MI.getOperand(Op).getReg() == ARM::CPSR;
192 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000193
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000194 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000195 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
196 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000197 unsigned SoImm = MI.getOperand(Op).getImm();
198 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
199 assert(SoImmVal != -1 && "Not a valid so_imm value!");
200
201 // Encode rotate_imm.
202 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
203 << ARMII::SoRotImmShift;
204
205 // Encode immed_8.
206 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
207 return Binary;
208 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000209
210 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
211 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
212 SmallVectorImpl<MCFixup> &Fixups) const {
213 unsigned SoImm = MI.getOperand(Op).getImm();
214 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
215 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
216 return Encoded;
217 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000218
Owen Anderson75579f72010-11-29 22:44:32 +0000219 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
220 SmallVectorImpl<MCFixup> &Fixups) const;
221 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
222 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000223 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
224 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000225 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
226 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000227
Jim Grosbachef324d72010-10-12 23:53:58 +0000228 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000229 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
230 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000231 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
232 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000233
Jim Grosbach806e80e2010-11-03 23:52:49 +0000234 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
235 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000236 switch (MI.getOperand(Op).getImm()) {
237 default: assert (0 && "Not a valid rot_imm value!");
238 case 0: return 0;
239 case 8: return 1;
240 case 16: return 2;
241 case 24: return 3;
242 }
243 }
244
Jim Grosbach806e80e2010-11-03 23:52:49 +0000245 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
246 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000247 return MI.getOperand(Op).getImm() - 1;
248 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000249
Jim Grosbach806e80e2010-11-03 23:52:49 +0000250 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
251 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000252 return 64 - MI.getOperand(Op).getImm();
253 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000254
Jim Grosbach806e80e2010-11-03 23:52:49 +0000255 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
256 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000257
Jim Grosbach806e80e2010-11-03 23:52:49 +0000258 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
259 SmallVectorImpl<MCFixup> &Fixups) const;
260 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
261 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000262 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
263 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000264 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
265 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000266
Owen Andersonc7139a62010-11-11 19:07:48 +0000267 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
268 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000269 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000270 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000271 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000272 unsigned EncodedValue) const;
273
274 unsigned VFPThumb2PostEncoder(const MCInst &MI,
275 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000276
Jim Grosbach70933262010-11-04 01:12:30 +0000277 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000278 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000279 }
280
Jim Grosbach70933262010-11-04 01:12:30 +0000281 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000282 // Output the constant in little endian byte order.
283 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000284 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000285 Val >>= 8;
286 }
287 }
288
Jim Grosbach568eeed2010-09-17 18:46:17 +0000289 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
290 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000291};
292
293} // end anonymous namespace
294
Bill Wendling0800ce72010-11-02 22:53:11 +0000295MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
296 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000297 return new ARMMCCodeEmitter(TM, Ctx);
298}
299
Owen Anderson57dac882010-11-11 21:36:43 +0000300/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000301/// instructions, and rewrite them to their Thumb2 form if we are currently in
302/// Thumb2 mode.
303unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
304 unsigned EncodedValue) const {
305 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
306 if (Subtarget.isThumb2()) {
307 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
308 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
309 // set to 1111.
310 unsigned Bit24 = EncodedValue & 0x01000000;
311 unsigned Bit28 = Bit24 << 4;
312 EncodedValue &= 0xEFFFFFFF;
313 EncodedValue |= Bit28;
314 EncodedValue |= 0x0F000000;
315 }
316
317 return EncodedValue;
318}
319
Owen Anderson57dac882010-11-11 21:36:43 +0000320/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
321/// instructions, and rewrite them to their Thumb2 form if we are currently in
322/// Thumb2 mode.
323unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
324 unsigned EncodedValue) const {
325 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
326 if (Subtarget.isThumb2()) {
327 EncodedValue &= 0xF0FFFFFF;
328 EncodedValue |= 0x09000000;
329 }
330
331 return EncodedValue;
332}
333
Owen Anderson8f143912010-11-11 23:12:55 +0000334/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
335/// instructions, and rewrite them to their Thumb2 form if we are currently in
336/// Thumb2 mode.
337unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
338 unsigned EncodedValue) const {
339 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
340 if (Subtarget.isThumb2()) {
341 EncodedValue &= 0x00FFFFFF;
342 EncodedValue |= 0xEE000000;
343 }
344
345 return EncodedValue;
346}
347
Bill Wendlingcf590262010-12-01 21:54:50 +0000348/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
349/// them to their Thumb2 form if we are currently in Thumb2 mode.
350unsigned ARMMCCodeEmitter::
351VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
352 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
353 EncodedValue &= 0x0FFFFFFF;
354 EncodedValue |= 0xE0000000;
355 }
356 return EncodedValue;
357}
Owen Anderson57dac882010-11-11 21:36:43 +0000358
Jim Grosbach56ac9072010-10-08 21:45:55 +0000359/// getMachineOpValue - Return binary encoding of operand. If the machine
360/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000361unsigned ARMMCCodeEmitter::
362getMachineOpValue(const MCInst &MI, const MCOperand &MO,
363 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000364 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000365 unsigned Reg = MO.getReg();
366 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000367
Jim Grosbachb0708d22010-11-30 23:51:41 +0000368 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000369 switch (Reg) {
370 default:
371 return RegNo;
372 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
373 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
374 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
375 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
376 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000377 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000378 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000379 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000380 } else if (MO.isFPImm()) {
381 return static_cast<unsigned>(APFloat(MO.getFPImm())
382 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000383 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000384
Jim Grosbach817c1a62010-11-19 00:27:09 +0000385 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000386 return 0;
387}
388
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000389/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000390bool ARMMCCodeEmitter::
391EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
392 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000393 const MCOperand &MO = MI.getOperand(OpIdx);
394 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000395
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000396 Reg = getARMRegisterNumbering(MO.getReg());
397
398 int32_t SImm = MO1.getImm();
399 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000400
Jim Grosbachab682a22010-10-28 18:34:10 +0000401 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000402 if (SImm == INT32_MIN)
403 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000404
Jim Grosbachab682a22010-10-28 18:34:10 +0000405 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000406 if (SImm < 0) {
407 SImm = -SImm;
408 isAdd = false;
409 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000410
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000411 Imm = SImm;
412 return isAdd;
413}
414
Jim Grosbach662a8162010-12-06 23:57:07 +0000415/// getThumbBLTargetOpValue - Return encoding info for immediate
416/// branch target.
417uint32_t ARMMCCodeEmitter::
418getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
419 SmallVectorImpl<MCFixup> &Fixups) const {
420 const MCOperand &MO = MI.getOperand(OpIdx);
421
422 // If the destination is an immediate, we have nothing to do.
423 if (MO.isImm()) return MO.getImm();
424 assert (MO.isExpr() && "Unexpected branch target type!");
425 const MCExpr *Expr = MO.getExpr();
426 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_thumb_bl);
427 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
428
429 // All of the information is in the fixup.
430 return 0;
431}
432
Jim Grosbachc466b932010-11-11 18:04:49 +0000433/// getBranchTargetOpValue - Return encoding info for 24-bit immediate
434/// branch target.
435uint32_t ARMMCCodeEmitter::
436getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
437 SmallVectorImpl<MCFixup> &Fixups) const {
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(ARM::fixup_arm_branch);
445 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
446
447 // All of the information is in the fixup.
448 return 0;
449}
450
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000451/// getAdrLabelOpValue - Return encoding info for 12-bit immediate
452/// ADR label target.
453uint32_t ARMMCCodeEmitter::
454getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
455 SmallVectorImpl<MCFixup> &Fixups) const {
456 const MCOperand &MO = MI.getOperand(OpIdx);
Jim Grosbachdff84b02010-12-02 00:28:45 +0000457 assert (MO.isExpr() && "Unexpected adr target type!");
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000458 const MCExpr *Expr = MO.getExpr();
Jim Grosbachdff84b02010-12-02 00:28:45 +0000459 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_adr_pcrel_12);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000460 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000461 // All of the information is in the fixup.
462 return 0;
463}
464
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000465/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000466uint32_t ARMMCCodeEmitter::
467getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
468 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000469 // {17-13} = reg
470 // {12} = (U)nsigned (add == '1', sub == '0')
471 // {11-0} = imm12
472 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000473 bool isAdd = true;
474 // If The first operand isn't a register, we have a label reference.
475 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000476 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
477 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000478 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000479 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000480 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000481
Owen Andersoneb6779c2010-12-07 00:45:21 +0000482 const MCExpr *Expr = 0;
483 if (!MO.isReg())
484 Expr = MO.getExpr();
485 else
486 Expr = MO2.getExpr();
487
Jim Grosbachdff84b02010-12-02 00:28:45 +0000488 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000489 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
490
491 ++MCNumCPRelocations;
492 } else
493 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000494
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000495 uint32_t Binary = Imm12 & 0xfff;
496 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000497 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000498 Binary |= (1 << 12);
499 Binary |= (Reg << 13);
500 return Binary;
501}
502
Owen Anderson9d63d902010-12-01 19:18:46 +0000503/// getT2AddrModeImm8s4OpValue - Return encoding info for
504/// 'reg +/- imm8<<2' operand.
505uint32_t ARMMCCodeEmitter::
506getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
507 SmallVectorImpl<MCFixup> &Fixups) const {
508 // {17-13} = reg
509 // {12} = (U)nsigned (add == '1', sub == '0')
510 // {11-0} = imm8
511 unsigned Reg, Imm8;
512 bool isAdd = true;
513 // If The first operand isn't a register, we have a label reference.
514 const MCOperand &MO = MI.getOperand(OpIdx);
515 if (!MO.isReg()) {
516 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
517 Imm8 = 0;
518 isAdd = false ; // 'U' bit is set as part of the fixup.
519
520 assert(MO.isExpr() && "Unexpected machine operand type!");
521 const MCExpr *Expr = MO.getExpr();
522 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
523 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
524
525 ++MCNumCPRelocations;
526 } else
527 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
528
529 uint32_t Binary = (Imm8 >> 2) & 0xff;
530 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
531 if (isAdd)
532 Binary |= (1 << 9);
533 Binary |= (Reg << 9);
534 return Binary;
535}
536
Jim Grosbach54fea632010-11-09 17:20:53 +0000537uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000538getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
539 SmallVectorImpl<MCFixup> &Fixups) const {
540 // {20-16} = imm{15-12}
541 // {11-0} = imm{11-0}
542 const MCOperand &MO = MI.getOperand(OpIdx);
543 if (MO.isImm()) {
544 return static_cast<unsigned>(MO.getImm());
545 } else if (const MCSymbolRefExpr *Expr =
546 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
547 MCFixupKind Kind;
548 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000549 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000550 case MCSymbolRefExpr::VK_ARM_HI16:
551 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
552 break;
553 case MCSymbolRefExpr::VK_ARM_LO16:
554 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
555 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000556 }
557 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
558 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000559 };
560 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000561 return 0;
562}
563
564uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000565getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
566 SmallVectorImpl<MCFixup> &Fixups) const {
567 const MCOperand &MO = MI.getOperand(OpIdx);
568 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
569 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
570 unsigned Rn = getARMRegisterNumbering(MO.getReg());
571 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000572 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
573 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000574 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
575 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000576
577 // {16-13} = Rn
578 // {12} = isAdd
579 // {11-0} = shifter
580 // {3-0} = Rm
581 // {4} = 0
582 // {6-5} = type
583 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000584 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000585 Binary |= Rn << 13;
586 Binary |= SBits << 5;
587 Binary |= ShImm << 7;
588 if (isAdd)
589 Binary |= 1 << 12;
590 return Binary;
591}
592
Jim Grosbach570a9222010-11-11 01:09:40 +0000593uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000594getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
595 SmallVectorImpl<MCFixup> &Fixups) const {
596 // {17-14} Rn
597 // {13} 1 == imm12, 0 == Rm
598 // {12} isAdd
599 // {11-0} imm12/Rm
600 const MCOperand &MO = MI.getOperand(OpIdx);
601 unsigned Rn = getARMRegisterNumbering(MO.getReg());
602 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
603 Binary |= Rn << 14;
604 return Binary;
605}
606
607uint32_t ARMMCCodeEmitter::
608getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
609 SmallVectorImpl<MCFixup> &Fixups) const {
610 // {13} 1 == imm12, 0 == Rm
611 // {12} isAdd
612 // {11-0} imm12/Rm
613 const MCOperand &MO = MI.getOperand(OpIdx);
614 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
615 unsigned Imm = MO1.getImm();
616 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
617 bool isReg = MO.getReg() != 0;
618 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
619 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
620 if (isReg) {
621 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
622 Binary <<= 7; // Shift amount is bits [11:7]
623 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
624 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
625 }
626 return Binary | (isAdd << 12) | (isReg << 13);
627}
628
629uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000630getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
631 SmallVectorImpl<MCFixup> &Fixups) const {
632 // {9} 1 == imm8, 0 == Rm
633 // {8} isAdd
634 // {7-4} imm7_4/zero
635 // {3-0} imm3_0/Rm
636 const MCOperand &MO = MI.getOperand(OpIdx);
637 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
638 unsigned Imm = MO1.getImm();
639 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
640 bool isImm = MO.getReg() == 0;
641 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
642 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
643 if (!isImm)
644 Imm8 = getARMRegisterNumbering(MO.getReg());
645 return Imm8 | (isAdd << 8) | (isImm << 9);
646}
647
648uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000649getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
650 SmallVectorImpl<MCFixup> &Fixups) const {
651 // {13} 1 == imm8, 0 == Rm
652 // {12-9} Rn
653 // {8} isAdd
654 // {7-4} imm7_4/zero
655 // {3-0} imm3_0/Rm
656 const MCOperand &MO = MI.getOperand(OpIdx);
657 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
658 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
659 unsigned Rn = getARMRegisterNumbering(MO.getReg());
660 unsigned Imm = MO2.getImm();
661 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
662 bool isImm = MO1.getReg() == 0;
663 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
664 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
665 if (!isImm)
666 Imm8 = getARMRegisterNumbering(MO1.getReg());
667 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
668}
669
Bill Wendlingb8958b02010-12-08 01:57:09 +0000670/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000671uint32_t ARMMCCodeEmitter::
672getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
673 SmallVectorImpl<MCFixup> &Fixups) const {
674 // [SP, #imm]
675 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000676 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000677#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
678 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
679 "Unexpected base register!");
680#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000681 // The immediate is already shifted for the implicit zeroes, so no change
682 // here.
683 return MO1.getImm() & 0xff;
684}
685
Bill Wendling1fd374e2010-11-30 22:57:21 +0000686/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
687static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
688 unsigned Scale) {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000689 // [Rn, Rm]
690 // {5-3} = Rm
691 // {2-0} = Rn
692 //
693 // [Rn, #imm]
694 // {7-3} = imm5
695 // {2-0} = Rn
696 const MCOperand &MO = MI.getOperand(OpIdx);
697 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
698 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
699 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling1fd374e2010-11-30 22:57:21 +0000700 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
Bill Wendling0bdf0c02010-12-03 00:53:22 +0000701
702 if (MO2.getReg() != 0)
703 // Is an immediate.
704 Imm5 = getARMRegisterNumbering(MO2.getReg());
705
706 return (Imm5 << 3) | Rn;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000707}
708
Bill Wendling1fd374e2010-11-30 22:57:21 +0000709/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
710uint32_t ARMMCCodeEmitter::
711getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
712 SmallVectorImpl<MCFixup> &) const {
713 return getAddrModeSOpValue(MI, OpIdx, 4);
714}
715
716/// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
717uint32_t ARMMCCodeEmitter::
718getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
719 SmallVectorImpl<MCFixup> &) const {
720 return getAddrModeSOpValue(MI, OpIdx, 2);
721}
722
723/// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
724uint32_t ARMMCCodeEmitter::
725getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
726 SmallVectorImpl<MCFixup> &) const {
727 return getAddrModeSOpValue(MI, OpIdx, 1);
728}
729
Bill Wendlingb8958b02010-12-08 01:57:09 +0000730/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
731uint32_t ARMMCCodeEmitter::
732getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
733 SmallVectorImpl<MCFixup> &Fixups) const {
734 const MCOperand &MO = MI.getOperand(OpIdx);
735
736 // If the destination is an immediate, we have nothing to do.
737 if (MO.isImm()) return MO.getImm();
738 assert (MO.isExpr() && "Unexpected branch target type!");
739 const MCExpr *Expr = MO.getExpr();
740 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_thumb_cp);
741 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
742
743 // All of the information is in the fixup.
744 return 0;
745}
746
Jim Grosbach5177f792010-12-01 21:09:40 +0000747/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000748uint32_t ARMMCCodeEmitter::
749getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
750 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000751 // {12-9} = reg
752 // {8} = (U)nsigned (add == '1', sub == '0')
753 // {7-0} = imm8
754 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000755 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000756 // If The first operand isn't a register, we have a label reference.
757 const MCOperand &MO = MI.getOperand(OpIdx);
758 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000759 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000760 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000761 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000762
763 assert(MO.isExpr() && "Unexpected machine operand type!");
764 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000765 MCFixupKind Kind;
766 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
767 if (Subtarget.isThumb2())
768 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
769 else
770 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000771 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
772
773 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000774 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000775 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000776 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
777 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000778
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000779 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
780 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000781 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000782 Binary |= (1 << 8);
783 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000784 return Binary;
785}
786
Jim Grosbach806e80e2010-11-03 23:52:49 +0000787unsigned ARMMCCodeEmitter::
788getSORegOpValue(const MCInst &MI, unsigned OpIdx,
789 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000790 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
791 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
792 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000793 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000794 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000795 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000796 // {6-5} = type
797 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000798 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000799 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000800 // else (imm shift)
801 // {11-7} = imm
802
803 const MCOperand &MO = MI.getOperand(OpIdx);
804 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
805 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
806 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
807
808 // Encode Rm.
809 unsigned Binary = getARMRegisterNumbering(MO.getReg());
810
811 // Encode the shift opcode.
812 unsigned SBits = 0;
813 unsigned Rs = MO1.getReg();
814 if (Rs) {
815 // Set shift operand (bit[7:4]).
816 // LSL - 0001
817 // LSR - 0011
818 // ASR - 0101
819 // ROR - 0111
820 // RRX - 0110 and bit[11:8] clear.
821 switch (SOpc) {
822 default: llvm_unreachable("Unknown shift opc!");
823 case ARM_AM::lsl: SBits = 0x1; break;
824 case ARM_AM::lsr: SBits = 0x3; break;
825 case ARM_AM::asr: SBits = 0x5; break;
826 case ARM_AM::ror: SBits = 0x7; break;
827 case ARM_AM::rrx: SBits = 0x6; break;
828 }
829 } else {
830 // Set shift operand (bit[6:4]).
831 // LSL - 000
832 // LSR - 010
833 // ASR - 100
834 // ROR - 110
835 switch (SOpc) {
836 default: llvm_unreachable("Unknown shift opc!");
837 case ARM_AM::lsl: SBits = 0x0; break;
838 case ARM_AM::lsr: SBits = 0x2; break;
839 case ARM_AM::asr: SBits = 0x4; break;
840 case ARM_AM::ror: SBits = 0x6; break;
841 }
842 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000843
Jim Grosbachef324d72010-10-12 23:53:58 +0000844 Binary |= SBits << 4;
845 if (SOpc == ARM_AM::rrx)
846 return Binary;
847
848 // Encode the shift operation Rs or shift_imm (except rrx).
849 if (Rs) {
850 // Encode Rs bit[11:8].
851 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
852 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
853 }
854
855 // Encode shift_imm bit[11:7].
856 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
857}
858
Jim Grosbach806e80e2010-11-03 23:52:49 +0000859unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000860getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
861 SmallVectorImpl<MCFixup> &Fixups) const {
862 const MCOperand &MO1 = MI.getOperand(OpNum);
863 const MCOperand &MO2 = MI.getOperand(OpNum+1);
864 const MCOperand &MO3 = MI.getOperand(OpNum+2);
865
866 // Encoded as [Rn, Rm, imm].
867 // FIXME: Needs fixup support.
868 unsigned Value = getARMRegisterNumbering(MO1.getReg());
869 Value <<= 4;
870 Value |= getARMRegisterNumbering(MO2.getReg());
871 Value <<= 2;
872 Value |= MO3.getImm();
873
874 return Value;
875}
876
877unsigned ARMMCCodeEmitter::
878getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
879 SmallVectorImpl<MCFixup> &Fixups) const {
880 const MCOperand &MO1 = MI.getOperand(OpNum);
881 const MCOperand &MO2 = MI.getOperand(OpNum+1);
882
883 // FIXME: Needs fixup support.
884 unsigned Value = getARMRegisterNumbering(MO1.getReg());
885
886 // Even though the immediate is 8 bits long, we need 9 bits in order
887 // to represent the (inverse of the) sign bit.
888 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000889 int32_t tmp = (int32_t)MO2.getImm();
890 if (tmp < 0)
891 tmp = abs(tmp);
892 else
893 Value |= 256; // Set the ADD bit
894 Value |= tmp & 255;
895 return Value;
896}
897
898unsigned ARMMCCodeEmitter::
899getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
900 SmallVectorImpl<MCFixup> &Fixups) const {
901 const MCOperand &MO1 = MI.getOperand(OpNum);
902
903 // FIXME: Needs fixup support.
904 unsigned Value = 0;
905 int32_t tmp = (int32_t)MO1.getImm();
906 if (tmp < 0)
907 tmp = abs(tmp);
908 else
909 Value |= 256; // Set the ADD bit
910 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000911 return Value;
912}
913
914unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000915getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
916 SmallVectorImpl<MCFixup> &Fixups) const {
917 const MCOperand &MO1 = MI.getOperand(OpNum);
918
919 // FIXME: Needs fixup support.
920 unsigned Value = 0;
921 int32_t tmp = (int32_t)MO1.getImm();
922 if (tmp < 0)
923 tmp = abs(tmp);
924 else
925 Value |= 4096; // Set the ADD bit
926 Value |= tmp & 4095;
927 return Value;
928}
929
930unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000931getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
932 SmallVectorImpl<MCFixup> &Fixups) const {
933 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
934 // shifted. The second is the amount to shift by.
935 //
936 // {3-0} = Rm.
937 // {4} = 0
938 // {6-5} = type
939 // {11-7} = imm
940
941 const MCOperand &MO = MI.getOperand(OpIdx);
942 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
943 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
944
945 // Encode Rm.
946 unsigned Binary = getARMRegisterNumbering(MO.getReg());
947
948 // Encode the shift opcode.
949 unsigned SBits = 0;
950 // Set shift operand (bit[6:4]).
951 // LSL - 000
952 // LSR - 010
953 // ASR - 100
954 // ROR - 110
955 switch (SOpc) {
956 default: llvm_unreachable("Unknown shift opc!");
957 case ARM_AM::lsl: SBits = 0x0; break;
958 case ARM_AM::lsr: SBits = 0x2; break;
959 case ARM_AM::asr: SBits = 0x4; break;
960 case ARM_AM::ror: SBits = 0x6; break;
961 }
962
963 Binary |= SBits << 4;
964 if (SOpc == ARM_AM::rrx)
965 return Binary;
966
967 // Encode shift_imm bit[11:7].
968 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
969}
970
971unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000972getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
973 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000974 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
975 // msb of the mask.
976 const MCOperand &MO = MI.getOperand(Op);
977 uint32_t v = ~MO.getImm();
978 uint32_t lsb = CountTrailingZeros_32(v);
979 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
980 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
981 return lsb | (msb << 5);
982}
983
Jim Grosbach806e80e2010-11-03 23:52:49 +0000984unsigned ARMMCCodeEmitter::
985getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000986 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +0000987 // VLDM/VSTM:
988 // {12-8} = Vd
989 // {7-0} = Number of registers
990 //
991 // LDM/STM:
992 // {15-0} = Bitfield of GPRs.
993 unsigned Reg = MI.getOperand(Op).getReg();
994 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
995 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
996
Bill Wendling5e559a22010-11-09 00:30:18 +0000997 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +0000998
999 if (SPRRegs || DPRRegs) {
1000 // VLDM/VSTM
1001 unsigned RegNo = getARMRegisterNumbering(Reg);
1002 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1003 Binary |= (RegNo & 0x1f) << 8;
1004 if (SPRRegs)
1005 Binary |= NumRegs;
1006 else
1007 Binary |= NumRegs * 2;
1008 } else {
1009 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1010 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1011 Binary |= 1 << RegNo;
1012 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001013 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001014
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001015 return Binary;
1016}
1017
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001018/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1019/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001020unsigned ARMMCCodeEmitter::
1021getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1022 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001023 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001024 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001025
Owen Andersond9aa7d32010-11-02 00:05:05 +00001026 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001027 unsigned Align = 0;
1028
1029 switch (Imm.getImm()) {
1030 default: break;
1031 case 2:
1032 case 4:
1033 case 8: Align = 0x01; break;
1034 case 16: Align = 0x02; break;
1035 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001036 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001037
Owen Andersond9aa7d32010-11-02 00:05:05 +00001038 return RegNo | (Align << 4);
1039}
1040
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001041/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1042/// alignment operand for use in VLD-dup instructions. This is the same as
1043/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1044/// different for VLD4-dup.
1045unsigned ARMMCCodeEmitter::
1046getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1047 SmallVectorImpl<MCFixup> &Fixups) const {
1048 const MCOperand &Reg = MI.getOperand(Op);
1049 const MCOperand &Imm = MI.getOperand(Op + 1);
1050
1051 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1052 unsigned Align = 0;
1053
1054 switch (Imm.getImm()) {
1055 default: break;
1056 case 2:
1057 case 4:
1058 case 8: Align = 0x01; break;
1059 case 16: Align = 0x03; break;
1060 }
1061
1062 return RegNo | (Align << 4);
1063}
1064
Jim Grosbach806e80e2010-11-03 23:52:49 +00001065unsigned ARMMCCodeEmitter::
1066getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1067 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001068 const MCOperand &MO = MI.getOperand(Op);
1069 if (MO.getReg() == 0) return 0x0D;
1070 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001071}
1072
Jim Grosbach568eeed2010-09-17 18:46:17 +00001073void ARMMCCodeEmitter::
1074EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001075 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001076 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001077 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001078 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001079 uint64_t TSFlags = Desc.TSFlags;
1080 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001081 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001082 int Size;
1083 // Basic size info comes from the TSFlags field.
1084 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1085 default: llvm_unreachable("Unexpected instruction size!");
1086 case ARMII::Size2Bytes: Size = 2; break;
1087 case ARMII::Size4Bytes: Size = 4; break;
1088 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001089 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1090 // Thumb 32-bit wide instructions need to be have the high order halfword
1091 // emitted first.
1092 if (Subtarget.isThumb() && Size == 4) {
1093 EmitConstant(Binary >> 16, 2, OS);
1094 EmitConstant(Binary & 0xffff, 2, OS);
1095 } else
1096 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001097 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001098}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001099
Jim Grosbach806e80e2010-11-03 23:52:49 +00001100#include "ARMGenMCCodeEmitter.inc"