blob: ebcab8b8d33cd4127b3f8f96fe2f97c2bf78aa90 [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 },
Owen Andersonfb20d892010-12-09 00:27:41 +000054 { "fixup_t2_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach662a8162010-12-06 23:57:07 +000055 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendlingdff2f712010-12-08 23:01:43 +000056 { "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendlingb8958b02010-12-08 01:57:09 +000057 { "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000058 { "fixup_arm_movt_hi16", 0, 16, 0 },
59 { "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000060 };
61
62 if (Kind < FirstTargetFixupKind)
63 return MCCodeEmitter::getFixupKindInfo(Kind);
64
65 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
66 "Invalid kind!");
67 return Infos[Kind - FirstTargetFixupKind];
68 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000069 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
70
Jim Grosbach9af82ba2010-10-07 21:57:55 +000071 // getBinaryCodeForInstr - TableGen'erated function for getting the
72 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000073 unsigned getBinaryCodeForInstr(const MCInst &MI,
74 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000075
76 /// getMachineOpValue - Return binary encoding of operand. If the machine
77 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000078 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
79 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000080
Jason W Kim837caa92010-11-18 23:37:15 +000081 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
82 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
83 SmallVectorImpl<MCFixup> &Fixups) const;
84
Bill Wendling92b5a2e2010-11-03 01:49:29 +000085 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000086 unsigned &Reg, unsigned &Imm,
87 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000088
Jim Grosbach662a8162010-12-06 23:57:07 +000089 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
90 /// branch target.
91 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
92 SmallVectorImpl<MCFixup> &Fixups) const;
93
Bill Wendlingdff2f712010-12-08 23:01:43 +000094 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
95 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
96 SmallVectorImpl<MCFixup> &Fixups) const;
97
Jim Grosbachc466b932010-11-11 18:04:49 +000098 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
99 /// branch target.
100 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
101 SmallVectorImpl<MCFixup> &Fixups) const;
102
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000103 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
104 /// ADR label target.
105 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
106 SmallVectorImpl<MCFixup> &Fixups) const;
107
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000108 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
109 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000110 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
111 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000112
Owen Anderson9d63d902010-12-01 19:18:46 +0000113 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
114 /// operand.
115 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
116 SmallVectorImpl<MCFixup> &Fixups) const;
117
118
Jim Grosbach54fea632010-11-09 17:20:53 +0000119 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
120 /// operand as needed by load/store instructions.
121 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
122 SmallVectorImpl<MCFixup> &Fixups) const;
123
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000124 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
125 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
126 SmallVectorImpl<MCFixup> &Fixups) const {
127 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
128 switch (Mode) {
129 default: assert(0 && "Unknown addressing sub-mode!");
130 case ARM_AM::da: return 0;
131 case ARM_AM::ia: return 1;
132 case ARM_AM::db: return 2;
133 case ARM_AM::ib: return 3;
134 }
135 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000136 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
137 ///
138 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
139 switch (ShOpc) {
140 default: llvm_unreachable("Unknown shift opc!");
141 case ARM_AM::no_shift:
142 case ARM_AM::lsl: return 0;
143 case ARM_AM::lsr: return 1;
144 case ARM_AM::asr: return 2;
145 case ARM_AM::ror:
146 case ARM_AM::rrx: return 3;
147 }
148 return 0;
149 }
150
151 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
152 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
153 SmallVectorImpl<MCFixup> &Fixups) const;
154
155 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
156 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
157 SmallVectorImpl<MCFixup> &Fixups) const;
158
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000159 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
160 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
161 SmallVectorImpl<MCFixup> &Fixups) const;
162
Jim Grosbach570a9222010-11-11 01:09:40 +0000163 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
164 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
165 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000166
Jim Grosbachd967cd02010-12-07 21:50:47 +0000167 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
168 /// operand.
169 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
170 SmallVectorImpl<MCFixup> &Fixups) const;
171
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000172 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
173 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
174 SmallVectorImpl<MCFixup> &Fixups) const;
175
Bill Wendling1fd374e2010-11-30 22:57:21 +0000176 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
177 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
178 SmallVectorImpl<MCFixup> &Fixups) const;
179
180 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
181 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
182 SmallVectorImpl<MCFixup> &Fixups) const;
183
Bill Wendlingb8958b02010-12-08 01:57:09 +0000184 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
185 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
186 SmallVectorImpl<MCFixup> &Fixups) const;
187
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000188 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000189 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
190 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000191
Jim Grosbach08bd5492010-10-12 23:00:24 +0000192 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000193 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
194 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000195 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
196 // '1' respectively.
197 return MI.getOperand(Op).getReg() == ARM::CPSR;
198 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000199
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000200 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000201 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
202 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000203 unsigned SoImm = MI.getOperand(Op).getImm();
204 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
205 assert(SoImmVal != -1 && "Not a valid so_imm value!");
206
207 // Encode rotate_imm.
208 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
209 << ARMII::SoRotImmShift;
210
211 // Encode immed_8.
212 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
213 return Binary;
214 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000215
216 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
217 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
218 SmallVectorImpl<MCFixup> &Fixups) const {
219 unsigned SoImm = MI.getOperand(Op).getImm();
220 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
221 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
222 return Encoded;
223 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000224
Owen Anderson75579f72010-11-29 22:44:32 +0000225 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
226 SmallVectorImpl<MCFixup> &Fixups) const;
227 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
228 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000229 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
230 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000231 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
232 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000233
Jim Grosbachef324d72010-10-12 23:53:58 +0000234 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000235 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
236 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000237 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
238 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000239
Jim Grosbach806e80e2010-11-03 23:52:49 +0000240 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
241 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000242 switch (MI.getOperand(Op).getImm()) {
243 default: assert (0 && "Not a valid rot_imm value!");
244 case 0: return 0;
245 case 8: return 1;
246 case 16: return 2;
247 case 24: return 3;
248 }
249 }
250
Jim Grosbach806e80e2010-11-03 23:52:49 +0000251 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
252 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000253 return MI.getOperand(Op).getImm() - 1;
254 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000255
Jim Grosbach806e80e2010-11-03 23:52:49 +0000256 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
257 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000258 return 64 - MI.getOperand(Op).getImm();
259 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000260
Jim Grosbach806e80e2010-11-03 23:52:49 +0000261 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
262 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000263
Jim Grosbach806e80e2010-11-03 23:52:49 +0000264 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
265 SmallVectorImpl<MCFixup> &Fixups) const;
266 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
267 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000268 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
269 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000270 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
271 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000272
Owen Andersonc7139a62010-11-11 19:07:48 +0000273 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
274 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000275 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000276 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000277 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000278 unsigned EncodedValue) const;
279
280 unsigned VFPThumb2PostEncoder(const MCInst &MI,
281 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000282
Jim Grosbach70933262010-11-04 01:12:30 +0000283 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000284 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000285 }
286
Jim Grosbach70933262010-11-04 01:12:30 +0000287 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000288 // Output the constant in little endian byte order.
289 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000290 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000291 Val >>= 8;
292 }
293 }
294
Jim Grosbach568eeed2010-09-17 18:46:17 +0000295 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
296 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000297};
298
299} // end anonymous namespace
300
Bill Wendling0800ce72010-11-02 22:53:11 +0000301MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
302 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000303 return new ARMMCCodeEmitter(TM, Ctx);
304}
305
Owen Anderson57dac882010-11-11 21:36:43 +0000306/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000307/// instructions, and rewrite them to their Thumb2 form if we are currently in
308/// Thumb2 mode.
309unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
310 unsigned EncodedValue) const {
311 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
312 if (Subtarget.isThumb2()) {
313 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
314 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
315 // set to 1111.
316 unsigned Bit24 = EncodedValue & 0x01000000;
317 unsigned Bit28 = Bit24 << 4;
318 EncodedValue &= 0xEFFFFFFF;
319 EncodedValue |= Bit28;
320 EncodedValue |= 0x0F000000;
321 }
322
323 return EncodedValue;
324}
325
Owen Anderson57dac882010-11-11 21:36:43 +0000326/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
327/// instructions, and rewrite them to their Thumb2 form if we are currently in
328/// Thumb2 mode.
329unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
330 unsigned EncodedValue) const {
331 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
332 if (Subtarget.isThumb2()) {
333 EncodedValue &= 0xF0FFFFFF;
334 EncodedValue |= 0x09000000;
335 }
336
337 return EncodedValue;
338}
339
Owen Anderson8f143912010-11-11 23:12:55 +0000340/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
341/// instructions, and rewrite them to their Thumb2 form if we are currently in
342/// Thumb2 mode.
343unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
344 unsigned EncodedValue) const {
345 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
346 if (Subtarget.isThumb2()) {
347 EncodedValue &= 0x00FFFFFF;
348 EncodedValue |= 0xEE000000;
349 }
350
351 return EncodedValue;
352}
353
Bill Wendlingcf590262010-12-01 21:54:50 +0000354/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
355/// them to their Thumb2 form if we are currently in Thumb2 mode.
356unsigned ARMMCCodeEmitter::
357VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
358 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
359 EncodedValue &= 0x0FFFFFFF;
360 EncodedValue |= 0xE0000000;
361 }
362 return EncodedValue;
363}
Owen Anderson57dac882010-11-11 21:36:43 +0000364
Jim Grosbach56ac9072010-10-08 21:45:55 +0000365/// getMachineOpValue - Return binary encoding of operand. If the machine
366/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000367unsigned ARMMCCodeEmitter::
368getMachineOpValue(const MCInst &MI, const MCOperand &MO,
369 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000370 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000371 unsigned Reg = MO.getReg();
372 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000373
Jim Grosbachb0708d22010-11-30 23:51:41 +0000374 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000375 switch (Reg) {
376 default:
377 return RegNo;
378 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
379 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
380 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
381 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
382 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000383 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000384 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000385 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000386 } else if (MO.isFPImm()) {
387 return static_cast<unsigned>(APFloat(MO.getFPImm())
388 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000389 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000390
Jim Grosbach817c1a62010-11-19 00:27:09 +0000391 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000392 return 0;
393}
394
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000395/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000396bool ARMMCCodeEmitter::
397EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
398 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000399 const MCOperand &MO = MI.getOperand(OpIdx);
400 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000401
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000402 Reg = getARMRegisterNumbering(MO.getReg());
403
404 int32_t SImm = MO1.getImm();
405 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000406
Jim Grosbachab682a22010-10-28 18:34:10 +0000407 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000408 if (SImm == INT32_MIN)
409 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000410
Jim Grosbachab682a22010-10-28 18:34:10 +0000411 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000412 if (SImm < 0) {
413 SImm = -SImm;
414 isAdd = false;
415 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000416
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000417 Imm = SImm;
418 return isAdd;
419}
420
Bill Wendlingdff2f712010-12-08 23:01:43 +0000421/// getBranchTargetOpValue - Helper function to get the branch target operand,
422/// which is either an immediate or requires a fixup.
423static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
424 unsigned FixupKind,
425 SmallVectorImpl<MCFixup> &Fixups) {
426 const MCOperand &MO = MI.getOperand(OpIdx);
427
428 // If the destination is an immediate, we have nothing to do.
429 if (MO.isImm()) return MO.getImm();
430 assert(MO.isExpr() && "Unexpected branch target type!");
431 const MCExpr *Expr = MO.getExpr();
432 MCFixupKind Kind = MCFixupKind(FixupKind);
433 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
434
435 // All of the information is in the fixup.
436 return 0;
437}
438
439/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000440uint32_t ARMMCCodeEmitter::
441getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
442 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000443 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000444}
445
Bill Wendlingdff2f712010-12-08 23:01:43 +0000446/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
447uint32_t ARMMCCodeEmitter::
448getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
449 SmallVectorImpl<MCFixup> &Fixups) const {
450 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
451}
452
453/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
454/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000455uint32_t ARMMCCodeEmitter::
456getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000457 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfb20d892010-12-09 00:27:41 +0000458 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
459 if (Subtarget.isThumb2())
460 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_branch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000461 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000462}
463
Bill Wendlingdff2f712010-12-08 23:01:43 +0000464/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
465/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000466uint32_t ARMMCCodeEmitter::
467getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
468 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000469 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
470 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
471 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000472}
473
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000474/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000475uint32_t ARMMCCodeEmitter::
476getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
477 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000478 // {17-13} = reg
479 // {12} = (U)nsigned (add == '1', sub == '0')
480 // {11-0} = imm12
481 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000482 bool isAdd = true;
483 // If The first operand isn't a register, we have a label reference.
484 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000485 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
486 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000487 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000488 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000489 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000490
Owen Andersoneb6779c2010-12-07 00:45:21 +0000491 const MCExpr *Expr = 0;
492 if (!MO.isReg())
493 Expr = MO.getExpr();
494 else
495 Expr = MO2.getExpr();
496
Jim Grosbachdff84b02010-12-02 00:28:45 +0000497 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000498 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
499
500 ++MCNumCPRelocations;
501 } else
502 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000503
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000504 uint32_t Binary = Imm12 & 0xfff;
505 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000506 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000507 Binary |= (1 << 12);
508 Binary |= (Reg << 13);
509 return Binary;
510}
511
Owen Anderson9d63d902010-12-01 19:18:46 +0000512/// getT2AddrModeImm8s4OpValue - Return encoding info for
513/// 'reg +/- imm8<<2' operand.
514uint32_t ARMMCCodeEmitter::
515getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
516 SmallVectorImpl<MCFixup> &Fixups) const {
517 // {17-13} = reg
518 // {12} = (U)nsigned (add == '1', sub == '0')
519 // {11-0} = imm8
520 unsigned Reg, Imm8;
521 bool isAdd = true;
522 // If The first operand isn't a register, we have a label reference.
523 const MCOperand &MO = MI.getOperand(OpIdx);
524 if (!MO.isReg()) {
525 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
526 Imm8 = 0;
527 isAdd = false ; // 'U' bit is set as part of the fixup.
528
529 assert(MO.isExpr() && "Unexpected machine operand type!");
530 const MCExpr *Expr = MO.getExpr();
531 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
532 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
533
534 ++MCNumCPRelocations;
535 } else
536 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
537
538 uint32_t Binary = (Imm8 >> 2) & 0xff;
539 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
540 if (isAdd)
541 Binary |= (1 << 9);
542 Binary |= (Reg << 9);
543 return Binary;
544}
545
Jim Grosbach54fea632010-11-09 17:20:53 +0000546uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000547getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
548 SmallVectorImpl<MCFixup> &Fixups) const {
549 // {20-16} = imm{15-12}
550 // {11-0} = imm{11-0}
551 const MCOperand &MO = MI.getOperand(OpIdx);
552 if (MO.isImm()) {
553 return static_cast<unsigned>(MO.getImm());
554 } else if (const MCSymbolRefExpr *Expr =
555 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
556 MCFixupKind Kind;
557 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000558 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000559 case MCSymbolRefExpr::VK_ARM_HI16:
560 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
561 break;
562 case MCSymbolRefExpr::VK_ARM_LO16:
563 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
564 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000565 }
566 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
567 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000568 };
569 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000570 return 0;
571}
572
573uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000574getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
575 SmallVectorImpl<MCFixup> &Fixups) const {
576 const MCOperand &MO = MI.getOperand(OpIdx);
577 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
578 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
579 unsigned Rn = getARMRegisterNumbering(MO.getReg());
580 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000581 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
582 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000583 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
584 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000585
586 // {16-13} = Rn
587 // {12} = isAdd
588 // {11-0} = shifter
589 // {3-0} = Rm
590 // {4} = 0
591 // {6-5} = type
592 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000593 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000594 Binary |= Rn << 13;
595 Binary |= SBits << 5;
596 Binary |= ShImm << 7;
597 if (isAdd)
598 Binary |= 1 << 12;
599 return Binary;
600}
601
Jim Grosbach570a9222010-11-11 01:09:40 +0000602uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000603getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
604 SmallVectorImpl<MCFixup> &Fixups) const {
605 // {17-14} Rn
606 // {13} 1 == imm12, 0 == Rm
607 // {12} isAdd
608 // {11-0} imm12/Rm
609 const MCOperand &MO = MI.getOperand(OpIdx);
610 unsigned Rn = getARMRegisterNumbering(MO.getReg());
611 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
612 Binary |= Rn << 14;
613 return Binary;
614}
615
616uint32_t ARMMCCodeEmitter::
617getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
618 SmallVectorImpl<MCFixup> &Fixups) const {
619 // {13} 1 == imm12, 0 == Rm
620 // {12} isAdd
621 // {11-0} imm12/Rm
622 const MCOperand &MO = MI.getOperand(OpIdx);
623 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
624 unsigned Imm = MO1.getImm();
625 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
626 bool isReg = MO.getReg() != 0;
627 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
628 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
629 if (isReg) {
630 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
631 Binary <<= 7; // Shift amount is bits [11:7]
632 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
633 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
634 }
635 return Binary | (isAdd << 12) | (isReg << 13);
636}
637
638uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000639getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
640 SmallVectorImpl<MCFixup> &Fixups) const {
641 // {9} 1 == imm8, 0 == Rm
642 // {8} isAdd
643 // {7-4} imm7_4/zero
644 // {3-0} imm3_0/Rm
645 const MCOperand &MO = MI.getOperand(OpIdx);
646 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
647 unsigned Imm = MO1.getImm();
648 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
649 bool isImm = MO.getReg() == 0;
650 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
651 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
652 if (!isImm)
653 Imm8 = getARMRegisterNumbering(MO.getReg());
654 return Imm8 | (isAdd << 8) | (isImm << 9);
655}
656
657uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000658getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
659 SmallVectorImpl<MCFixup> &Fixups) const {
660 // {13} 1 == imm8, 0 == Rm
661 // {12-9} Rn
662 // {8} isAdd
663 // {7-4} imm7_4/zero
664 // {3-0} imm3_0/Rm
665 const MCOperand &MO = MI.getOperand(OpIdx);
666 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
667 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
668 unsigned Rn = getARMRegisterNumbering(MO.getReg());
669 unsigned Imm = MO2.getImm();
670 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
671 bool isImm = MO1.getReg() == 0;
672 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
673 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
674 if (!isImm)
675 Imm8 = getARMRegisterNumbering(MO1.getReg());
676 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
677}
678
Bill Wendlingb8958b02010-12-08 01:57:09 +0000679/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000680uint32_t ARMMCCodeEmitter::
681getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
682 SmallVectorImpl<MCFixup> &Fixups) const {
683 // [SP, #imm]
684 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000685 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000686#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
687 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
688 "Unexpected base register!");
689#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000690 // The immediate is already shifted for the implicit zeroes, so no change
691 // here.
692 return MO1.getImm() & 0xff;
693}
694
Bill Wendling1fd374e2010-11-30 22:57:21 +0000695/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
696static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
697 unsigned Scale) {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000698 // [Rn, Rm]
699 // {5-3} = Rm
700 // {2-0} = Rn
701 //
702 // [Rn, #imm]
703 // {7-3} = imm5
704 // {2-0} = Rn
705 const MCOperand &MO = MI.getOperand(OpIdx);
706 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
707 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
708 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling1fd374e2010-11-30 22:57:21 +0000709 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
Bill Wendling0bdf0c02010-12-03 00:53:22 +0000710
711 if (MO2.getReg() != 0)
712 // Is an immediate.
713 Imm5 = getARMRegisterNumbering(MO2.getReg());
714
715 return (Imm5 << 3) | Rn;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000716}
717
Bill Wendling1fd374e2010-11-30 22:57:21 +0000718/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
719uint32_t ARMMCCodeEmitter::
720getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
721 SmallVectorImpl<MCFixup> &) const {
722 return getAddrModeSOpValue(MI, OpIdx, 4);
723}
724
725/// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
726uint32_t ARMMCCodeEmitter::
727getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
728 SmallVectorImpl<MCFixup> &) const {
729 return getAddrModeSOpValue(MI, OpIdx, 2);
730}
731
732/// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
733uint32_t ARMMCCodeEmitter::
734getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
735 SmallVectorImpl<MCFixup> &) const {
736 return getAddrModeSOpValue(MI, OpIdx, 1);
737}
738
Bill Wendlingb8958b02010-12-08 01:57:09 +0000739/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
740uint32_t ARMMCCodeEmitter::
741getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
742 SmallVectorImpl<MCFixup> &Fixups) const {
743 const MCOperand &MO = MI.getOperand(OpIdx);
744
745 // If the destination is an immediate, we have nothing to do.
746 if (MO.isImm()) return MO.getImm();
747 assert (MO.isExpr() && "Unexpected branch target type!");
748 const MCExpr *Expr = MO.getExpr();
749 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_thumb_cp);
750 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
751
752 // All of the information is in the fixup.
753 return 0;
754}
755
Jim Grosbach5177f792010-12-01 21:09:40 +0000756/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000757uint32_t ARMMCCodeEmitter::
758getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
759 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000760 // {12-9} = reg
761 // {8} = (U)nsigned (add == '1', sub == '0')
762 // {7-0} = imm8
763 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000764 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000765 // If The first operand isn't a register, we have a label reference.
766 const MCOperand &MO = MI.getOperand(OpIdx);
767 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000768 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000769 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000770 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000771
772 assert(MO.isExpr() && "Unexpected machine operand type!");
773 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000774 MCFixupKind Kind;
775 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
776 if (Subtarget.isThumb2())
777 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
778 else
779 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000780 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
781
782 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000783 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000784 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000785 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
786 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000787
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000788 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
789 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000790 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000791 Binary |= (1 << 8);
792 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000793 return Binary;
794}
795
Jim Grosbach806e80e2010-11-03 23:52:49 +0000796unsigned ARMMCCodeEmitter::
797getSORegOpValue(const MCInst &MI, unsigned OpIdx,
798 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000799 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
800 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
801 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000802 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000803 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000804 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000805 // {6-5} = type
806 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000807 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000808 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000809 // else (imm shift)
810 // {11-7} = imm
811
812 const MCOperand &MO = MI.getOperand(OpIdx);
813 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
814 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
815 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
816
817 // Encode Rm.
818 unsigned Binary = getARMRegisterNumbering(MO.getReg());
819
820 // Encode the shift opcode.
821 unsigned SBits = 0;
822 unsigned Rs = MO1.getReg();
823 if (Rs) {
824 // Set shift operand (bit[7:4]).
825 // LSL - 0001
826 // LSR - 0011
827 // ASR - 0101
828 // ROR - 0111
829 // RRX - 0110 and bit[11:8] clear.
830 switch (SOpc) {
831 default: llvm_unreachable("Unknown shift opc!");
832 case ARM_AM::lsl: SBits = 0x1; break;
833 case ARM_AM::lsr: SBits = 0x3; break;
834 case ARM_AM::asr: SBits = 0x5; break;
835 case ARM_AM::ror: SBits = 0x7; break;
836 case ARM_AM::rrx: SBits = 0x6; break;
837 }
838 } else {
839 // Set shift operand (bit[6:4]).
840 // LSL - 000
841 // LSR - 010
842 // ASR - 100
843 // ROR - 110
844 switch (SOpc) {
845 default: llvm_unreachable("Unknown shift opc!");
846 case ARM_AM::lsl: SBits = 0x0; break;
847 case ARM_AM::lsr: SBits = 0x2; break;
848 case ARM_AM::asr: SBits = 0x4; break;
849 case ARM_AM::ror: SBits = 0x6; break;
850 }
851 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000852
Jim Grosbachef324d72010-10-12 23:53:58 +0000853 Binary |= SBits << 4;
854 if (SOpc == ARM_AM::rrx)
855 return Binary;
856
857 // Encode the shift operation Rs or shift_imm (except rrx).
858 if (Rs) {
859 // Encode Rs bit[11:8].
860 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
861 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
862 }
863
864 // Encode shift_imm bit[11:7].
865 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
866}
867
Jim Grosbach806e80e2010-11-03 23:52:49 +0000868unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000869getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
870 SmallVectorImpl<MCFixup> &Fixups) const {
871 const MCOperand &MO1 = MI.getOperand(OpNum);
872 const MCOperand &MO2 = MI.getOperand(OpNum+1);
873 const MCOperand &MO3 = MI.getOperand(OpNum+2);
874
875 // Encoded as [Rn, Rm, imm].
876 // FIXME: Needs fixup support.
877 unsigned Value = getARMRegisterNumbering(MO1.getReg());
878 Value <<= 4;
879 Value |= getARMRegisterNumbering(MO2.getReg());
880 Value <<= 2;
881 Value |= MO3.getImm();
882
883 return Value;
884}
885
886unsigned ARMMCCodeEmitter::
887getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
888 SmallVectorImpl<MCFixup> &Fixups) const {
889 const MCOperand &MO1 = MI.getOperand(OpNum);
890 const MCOperand &MO2 = MI.getOperand(OpNum+1);
891
892 // FIXME: Needs fixup support.
893 unsigned Value = getARMRegisterNumbering(MO1.getReg());
894
895 // Even though the immediate is 8 bits long, we need 9 bits in order
896 // to represent the (inverse of the) sign bit.
897 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000898 int32_t tmp = (int32_t)MO2.getImm();
899 if (tmp < 0)
900 tmp = abs(tmp);
901 else
902 Value |= 256; // Set the ADD bit
903 Value |= tmp & 255;
904 return Value;
905}
906
907unsigned ARMMCCodeEmitter::
908getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
909 SmallVectorImpl<MCFixup> &Fixups) const {
910 const MCOperand &MO1 = MI.getOperand(OpNum);
911
912 // FIXME: Needs fixup support.
913 unsigned Value = 0;
914 int32_t tmp = (int32_t)MO1.getImm();
915 if (tmp < 0)
916 tmp = abs(tmp);
917 else
918 Value |= 256; // Set the ADD bit
919 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000920 return Value;
921}
922
923unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000924getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
925 SmallVectorImpl<MCFixup> &Fixups) const {
926 const MCOperand &MO1 = MI.getOperand(OpNum);
927
928 // FIXME: Needs fixup support.
929 unsigned Value = 0;
930 int32_t tmp = (int32_t)MO1.getImm();
931 if (tmp < 0)
932 tmp = abs(tmp);
933 else
934 Value |= 4096; // Set the ADD bit
935 Value |= tmp & 4095;
936 return Value;
937}
938
939unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000940getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
941 SmallVectorImpl<MCFixup> &Fixups) const {
942 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
943 // shifted. The second is the amount to shift by.
944 //
945 // {3-0} = Rm.
946 // {4} = 0
947 // {6-5} = type
948 // {11-7} = imm
949
950 const MCOperand &MO = MI.getOperand(OpIdx);
951 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
952 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
953
954 // Encode Rm.
955 unsigned Binary = getARMRegisterNumbering(MO.getReg());
956
957 // Encode the shift opcode.
958 unsigned SBits = 0;
959 // Set shift operand (bit[6:4]).
960 // LSL - 000
961 // LSR - 010
962 // ASR - 100
963 // ROR - 110
964 switch (SOpc) {
965 default: llvm_unreachable("Unknown shift opc!");
966 case ARM_AM::lsl: SBits = 0x0; break;
967 case ARM_AM::lsr: SBits = 0x2; break;
968 case ARM_AM::asr: SBits = 0x4; break;
969 case ARM_AM::ror: SBits = 0x6; break;
970 }
971
972 Binary |= SBits << 4;
973 if (SOpc == ARM_AM::rrx)
974 return Binary;
975
976 // Encode shift_imm bit[11:7].
977 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
978}
979
980unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000981getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
982 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000983 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
984 // msb of the mask.
985 const MCOperand &MO = MI.getOperand(Op);
986 uint32_t v = ~MO.getImm();
987 uint32_t lsb = CountTrailingZeros_32(v);
988 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
989 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
990 return lsb | (msb << 5);
991}
992
Jim Grosbach806e80e2010-11-03 23:52:49 +0000993unsigned ARMMCCodeEmitter::
994getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000995 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +0000996 // VLDM/VSTM:
997 // {12-8} = Vd
998 // {7-0} = Number of registers
999 //
1000 // LDM/STM:
1001 // {15-0} = Bitfield of GPRs.
1002 unsigned Reg = MI.getOperand(Op).getReg();
1003 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1004 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1005
Bill Wendling5e559a22010-11-09 00:30:18 +00001006 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001007
1008 if (SPRRegs || DPRRegs) {
1009 // VLDM/VSTM
1010 unsigned RegNo = getARMRegisterNumbering(Reg);
1011 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1012 Binary |= (RegNo & 0x1f) << 8;
1013 if (SPRRegs)
1014 Binary |= NumRegs;
1015 else
1016 Binary |= NumRegs * 2;
1017 } else {
1018 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1019 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1020 Binary |= 1 << RegNo;
1021 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001022 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001023
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001024 return Binary;
1025}
1026
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001027/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1028/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001029unsigned ARMMCCodeEmitter::
1030getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1031 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001032 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001033 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001034
Owen Andersond9aa7d32010-11-02 00:05:05 +00001035 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001036 unsigned Align = 0;
1037
1038 switch (Imm.getImm()) {
1039 default: break;
1040 case 2:
1041 case 4:
1042 case 8: Align = 0x01; break;
1043 case 16: Align = 0x02; break;
1044 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001045 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001046
Owen Andersond9aa7d32010-11-02 00:05:05 +00001047 return RegNo | (Align << 4);
1048}
1049
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001050/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1051/// alignment operand for use in VLD-dup instructions. This is the same as
1052/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1053/// different for VLD4-dup.
1054unsigned ARMMCCodeEmitter::
1055getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1056 SmallVectorImpl<MCFixup> &Fixups) const {
1057 const MCOperand &Reg = MI.getOperand(Op);
1058 const MCOperand &Imm = MI.getOperand(Op + 1);
1059
1060 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1061 unsigned Align = 0;
1062
1063 switch (Imm.getImm()) {
1064 default: break;
1065 case 2:
1066 case 4:
1067 case 8: Align = 0x01; break;
1068 case 16: Align = 0x03; break;
1069 }
1070
1071 return RegNo | (Align << 4);
1072}
1073
Jim Grosbach806e80e2010-11-03 23:52:49 +00001074unsigned ARMMCCodeEmitter::
1075getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1076 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001077 const MCOperand &MO = MI.getOperand(Op);
1078 if (MO.getReg() == 0) return 0x0D;
1079 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001080}
1081
Jim Grosbach568eeed2010-09-17 18:46:17 +00001082void ARMMCCodeEmitter::
1083EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001084 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001085 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001086 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001087 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001088 uint64_t TSFlags = Desc.TSFlags;
1089 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001090 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001091 int Size;
1092 // Basic size info comes from the TSFlags field.
1093 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1094 default: llvm_unreachable("Unexpected instruction size!");
1095 case ARMII::Size2Bytes: Size = 2; break;
1096 case ARMII::Size4Bytes: Size = 4; break;
1097 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001098 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1099 // Thumb 32-bit wide instructions need to be have the high order halfword
1100 // emitted first.
1101 if (Subtarget.isThumb() && Size == 4) {
1102 EmitConstant(Binary >> 16, 2, OS);
1103 EmitConstant(Binary & 0xffff, 2, OS);
1104 } else
1105 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001106 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001107}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001108
Jim Grosbach806e80e2010-11-03 23:52:49 +00001109#include "ARMGenMCCodeEmitter.inc"