blob: ef9fac436c748ced9f21c8383be02f7de0ce109a [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 Wendlingdff2f712010-12-08 23:01:43 +000055 { "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendlingb8958b02010-12-08 01:57:09 +000056 { "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000057 { "fixup_arm_movt_hi16", 0, 16, 0 },
58 { "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000059 };
60
61 if (Kind < FirstTargetFixupKind)
62 return MCCodeEmitter::getFixupKindInfo(Kind);
63
64 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
65 "Invalid kind!");
66 return Infos[Kind - FirstTargetFixupKind];
67 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000068 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
69
Jim Grosbach9af82ba2010-10-07 21:57:55 +000070 // getBinaryCodeForInstr - TableGen'erated function for getting the
71 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000072 unsigned getBinaryCodeForInstr(const MCInst &MI,
73 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000074
75 /// getMachineOpValue - Return binary encoding of operand. If the machine
76 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000077 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
78 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000079
Jason W Kim837caa92010-11-18 23:37:15 +000080 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
81 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
82 SmallVectorImpl<MCFixup> &Fixups) const;
83
Bill Wendling92b5a2e2010-11-03 01:49:29 +000084 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000085 unsigned &Reg, unsigned &Imm,
86 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000087
Jim Grosbach662a8162010-12-06 23:57:07 +000088 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
89 /// branch target.
90 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
91 SmallVectorImpl<MCFixup> &Fixups) const;
92
Bill Wendlingdff2f712010-12-08 23:01:43 +000093 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
94 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
95 SmallVectorImpl<MCFixup> &Fixups) const;
96
Jim Grosbachc466b932010-11-11 18:04:49 +000097 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
98 /// branch target.
99 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
100 SmallVectorImpl<MCFixup> &Fixups) const;
101
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000102 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
103 /// ADR label target.
104 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
105 SmallVectorImpl<MCFixup> &Fixups) const;
106
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000107 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
108 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000109 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
110 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000111
Owen Anderson9d63d902010-12-01 19:18:46 +0000112 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
113 /// operand.
114 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
115 SmallVectorImpl<MCFixup> &Fixups) const;
116
117
Jim Grosbach54fea632010-11-09 17:20:53 +0000118 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
119 /// operand as needed by load/store instructions.
120 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
121 SmallVectorImpl<MCFixup> &Fixups) const;
122
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000123 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
124 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
125 SmallVectorImpl<MCFixup> &Fixups) const {
126 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
127 switch (Mode) {
128 default: assert(0 && "Unknown addressing sub-mode!");
129 case ARM_AM::da: return 0;
130 case ARM_AM::ia: return 1;
131 case ARM_AM::db: return 2;
132 case ARM_AM::ib: return 3;
133 }
134 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000135 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
136 ///
137 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
138 switch (ShOpc) {
139 default: llvm_unreachable("Unknown shift opc!");
140 case ARM_AM::no_shift:
141 case ARM_AM::lsl: return 0;
142 case ARM_AM::lsr: return 1;
143 case ARM_AM::asr: return 2;
144 case ARM_AM::ror:
145 case ARM_AM::rrx: return 3;
146 }
147 return 0;
148 }
149
150 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
151 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
152 SmallVectorImpl<MCFixup> &Fixups) const;
153
154 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
155 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
156 SmallVectorImpl<MCFixup> &Fixups) const;
157
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000158 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
159 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
160 SmallVectorImpl<MCFixup> &Fixups) const;
161
Jim Grosbach570a9222010-11-11 01:09:40 +0000162 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
163 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
164 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000165
Jim Grosbachd967cd02010-12-07 21:50:47 +0000166 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
167 /// operand.
168 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
169 SmallVectorImpl<MCFixup> &Fixups) const;
170
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000171 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
172 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
173 SmallVectorImpl<MCFixup> &Fixups) const;
174
Bill Wendling1fd374e2010-11-30 22:57:21 +0000175 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
176 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
177 SmallVectorImpl<MCFixup> &Fixups) const;
178
179 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
180 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
181 SmallVectorImpl<MCFixup> &Fixups) const;
182
Bill Wendlingb8958b02010-12-08 01:57:09 +0000183 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
184 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
185 SmallVectorImpl<MCFixup> &Fixups) const;
186
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000187 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000188 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
189 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000190
Jim Grosbach08bd5492010-10-12 23:00:24 +0000191 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000192 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
193 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000194 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
195 // '1' respectively.
196 return MI.getOperand(Op).getReg() == ARM::CPSR;
197 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000198
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000199 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000200 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
201 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000202 unsigned SoImm = MI.getOperand(Op).getImm();
203 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
204 assert(SoImmVal != -1 && "Not a valid so_imm value!");
205
206 // Encode rotate_imm.
207 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
208 << ARMII::SoRotImmShift;
209
210 // Encode immed_8.
211 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
212 return Binary;
213 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000214
215 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
216 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
217 SmallVectorImpl<MCFixup> &Fixups) const {
218 unsigned SoImm = MI.getOperand(Op).getImm();
219 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
220 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
221 return Encoded;
222 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000223
Owen Anderson75579f72010-11-29 22:44:32 +0000224 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
225 SmallVectorImpl<MCFixup> &Fixups) const;
226 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
227 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000228 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
229 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000230 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
231 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000232
Jim Grosbachef324d72010-10-12 23:53:58 +0000233 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000234 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
235 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000236 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
237 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000238
Jim Grosbach806e80e2010-11-03 23:52:49 +0000239 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
240 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000241 switch (MI.getOperand(Op).getImm()) {
242 default: assert (0 && "Not a valid rot_imm value!");
243 case 0: return 0;
244 case 8: return 1;
245 case 16: return 2;
246 case 24: return 3;
247 }
248 }
249
Jim Grosbach806e80e2010-11-03 23:52:49 +0000250 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
251 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000252 return MI.getOperand(Op).getImm() - 1;
253 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000254
Jim Grosbach806e80e2010-11-03 23:52:49 +0000255 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
256 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000257 return 64 - MI.getOperand(Op).getImm();
258 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000259
Jim Grosbach806e80e2010-11-03 23:52:49 +0000260 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
261 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000262
Jim Grosbach806e80e2010-11-03 23:52:49 +0000263 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
264 SmallVectorImpl<MCFixup> &Fixups) const;
265 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
266 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000267 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
268 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000269 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
270 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000271
Owen Andersonc7139a62010-11-11 19:07:48 +0000272 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
273 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000274 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000275 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000276 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000277 unsigned EncodedValue) const;
278
279 unsigned VFPThumb2PostEncoder(const MCInst &MI,
280 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000281
Jim Grosbach70933262010-11-04 01:12:30 +0000282 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000283 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000284 }
285
Jim Grosbach70933262010-11-04 01:12:30 +0000286 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000287 // Output the constant in little endian byte order.
288 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000289 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000290 Val >>= 8;
291 }
292 }
293
Jim Grosbach568eeed2010-09-17 18:46:17 +0000294 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
295 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000296};
297
298} // end anonymous namespace
299
Bill Wendling0800ce72010-11-02 22:53:11 +0000300MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
301 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000302 return new ARMMCCodeEmitter(TM, Ctx);
303}
304
Owen Anderson57dac882010-11-11 21:36:43 +0000305/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000306/// instructions, and rewrite them to their Thumb2 form if we are currently in
307/// Thumb2 mode.
308unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
309 unsigned EncodedValue) const {
310 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
311 if (Subtarget.isThumb2()) {
312 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
313 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
314 // set to 1111.
315 unsigned Bit24 = EncodedValue & 0x01000000;
316 unsigned Bit28 = Bit24 << 4;
317 EncodedValue &= 0xEFFFFFFF;
318 EncodedValue |= Bit28;
319 EncodedValue |= 0x0F000000;
320 }
321
322 return EncodedValue;
323}
324
Owen Anderson57dac882010-11-11 21:36:43 +0000325/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
326/// instructions, and rewrite them to their Thumb2 form if we are currently in
327/// Thumb2 mode.
328unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
329 unsigned EncodedValue) const {
330 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
331 if (Subtarget.isThumb2()) {
332 EncodedValue &= 0xF0FFFFFF;
333 EncodedValue |= 0x09000000;
334 }
335
336 return EncodedValue;
337}
338
Owen Anderson8f143912010-11-11 23:12:55 +0000339/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
340/// instructions, and rewrite them to their Thumb2 form if we are currently in
341/// Thumb2 mode.
342unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
343 unsigned EncodedValue) const {
344 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
345 if (Subtarget.isThumb2()) {
346 EncodedValue &= 0x00FFFFFF;
347 EncodedValue |= 0xEE000000;
348 }
349
350 return EncodedValue;
351}
352
Bill Wendlingcf590262010-12-01 21:54:50 +0000353/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
354/// them to their Thumb2 form if we are currently in Thumb2 mode.
355unsigned ARMMCCodeEmitter::
356VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
357 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
358 EncodedValue &= 0x0FFFFFFF;
359 EncodedValue |= 0xE0000000;
360 }
361 return EncodedValue;
362}
Owen Anderson57dac882010-11-11 21:36:43 +0000363
Jim Grosbach56ac9072010-10-08 21:45:55 +0000364/// getMachineOpValue - Return binary encoding of operand. If the machine
365/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000366unsigned ARMMCCodeEmitter::
367getMachineOpValue(const MCInst &MI, const MCOperand &MO,
368 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000369 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000370 unsigned Reg = MO.getReg();
371 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000372
Jim Grosbachb0708d22010-11-30 23:51:41 +0000373 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000374 switch (Reg) {
375 default:
376 return RegNo;
377 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
378 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
379 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
380 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
381 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000382 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000383 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000384 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000385 } else if (MO.isFPImm()) {
386 return static_cast<unsigned>(APFloat(MO.getFPImm())
387 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000388 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000389
Jim Grosbach817c1a62010-11-19 00:27:09 +0000390 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000391 return 0;
392}
393
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000394/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000395bool ARMMCCodeEmitter::
396EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
397 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000398 const MCOperand &MO = MI.getOperand(OpIdx);
399 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000400
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000401 Reg = getARMRegisterNumbering(MO.getReg());
402
403 int32_t SImm = MO1.getImm();
404 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000405
Jim Grosbachab682a22010-10-28 18:34:10 +0000406 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000407 if (SImm == INT32_MIN)
408 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000409
Jim Grosbachab682a22010-10-28 18:34:10 +0000410 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000411 if (SImm < 0) {
412 SImm = -SImm;
413 isAdd = false;
414 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000415
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000416 Imm = SImm;
417 return isAdd;
418}
419
Bill Wendlingdff2f712010-12-08 23:01:43 +0000420/// getBranchTargetOpValue - Helper function to get the branch target operand,
421/// which is either an immediate or requires a fixup.
422static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
423 unsigned FixupKind,
424 SmallVectorImpl<MCFixup> &Fixups) {
425 const MCOperand &MO = MI.getOperand(OpIdx);
426
427 // If the destination is an immediate, we have nothing to do.
428 if (MO.isImm()) return MO.getImm();
429 assert(MO.isExpr() && "Unexpected branch target type!");
430 const MCExpr *Expr = MO.getExpr();
431 MCFixupKind Kind = MCFixupKind(FixupKind);
432 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
433
434 // All of the information is in the fixup.
435 return 0;
436}
437
438/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000439uint32_t ARMMCCodeEmitter::
440getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
441 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000442 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000443}
444
Bill Wendlingdff2f712010-12-08 23:01:43 +0000445/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
446uint32_t ARMMCCodeEmitter::
447getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
448 SmallVectorImpl<MCFixup> &Fixups) const {
449 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
450}
451
452/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
453/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000454uint32_t ARMMCCodeEmitter::
455getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000456 SmallVectorImpl<MCFixup> &Fixups) const {
457 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000458}
459
Bill Wendlingdff2f712010-12-08 23:01:43 +0000460/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
461/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000462uint32_t ARMMCCodeEmitter::
463getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
464 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000465 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
466 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
467 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000468}
469
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000470/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000471uint32_t ARMMCCodeEmitter::
472getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
473 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000474 // {17-13} = reg
475 // {12} = (U)nsigned (add == '1', sub == '0')
476 // {11-0} = imm12
477 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000478 bool isAdd = true;
479 // If The first operand isn't a register, we have a label reference.
480 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000481 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
482 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000483 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000484 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000485 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000486
Owen Andersoneb6779c2010-12-07 00:45:21 +0000487 const MCExpr *Expr = 0;
488 if (!MO.isReg())
489 Expr = MO.getExpr();
490 else
491 Expr = MO2.getExpr();
492
Jim Grosbachdff84b02010-12-02 00:28:45 +0000493 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000494 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
495
496 ++MCNumCPRelocations;
497 } else
498 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000499
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000500 uint32_t Binary = Imm12 & 0xfff;
501 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000502 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000503 Binary |= (1 << 12);
504 Binary |= (Reg << 13);
505 return Binary;
506}
507
Owen Anderson9d63d902010-12-01 19:18:46 +0000508/// getT2AddrModeImm8s4OpValue - Return encoding info for
509/// 'reg +/- imm8<<2' operand.
510uint32_t ARMMCCodeEmitter::
511getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
512 SmallVectorImpl<MCFixup> &Fixups) const {
513 // {17-13} = reg
514 // {12} = (U)nsigned (add == '1', sub == '0')
515 // {11-0} = imm8
516 unsigned Reg, Imm8;
517 bool isAdd = true;
518 // If The first operand isn't a register, we have a label reference.
519 const MCOperand &MO = MI.getOperand(OpIdx);
520 if (!MO.isReg()) {
521 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
522 Imm8 = 0;
523 isAdd = false ; // 'U' bit is set as part of the fixup.
524
525 assert(MO.isExpr() && "Unexpected machine operand type!");
526 const MCExpr *Expr = MO.getExpr();
527 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
528 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
529
530 ++MCNumCPRelocations;
531 } else
532 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
533
534 uint32_t Binary = (Imm8 >> 2) & 0xff;
535 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
536 if (isAdd)
537 Binary |= (1 << 9);
538 Binary |= (Reg << 9);
539 return Binary;
540}
541
Jim Grosbach54fea632010-11-09 17:20:53 +0000542uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000543getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
544 SmallVectorImpl<MCFixup> &Fixups) const {
545 // {20-16} = imm{15-12}
546 // {11-0} = imm{11-0}
547 const MCOperand &MO = MI.getOperand(OpIdx);
548 if (MO.isImm()) {
549 return static_cast<unsigned>(MO.getImm());
550 } else if (const MCSymbolRefExpr *Expr =
551 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
552 MCFixupKind Kind;
553 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000554 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000555 case MCSymbolRefExpr::VK_ARM_HI16:
556 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
557 break;
558 case MCSymbolRefExpr::VK_ARM_LO16:
559 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
560 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000561 }
562 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
563 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000564 };
565 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000566 return 0;
567}
568
569uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000570getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
571 SmallVectorImpl<MCFixup> &Fixups) const {
572 const MCOperand &MO = MI.getOperand(OpIdx);
573 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
574 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
575 unsigned Rn = getARMRegisterNumbering(MO.getReg());
576 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000577 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
578 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000579 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
580 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000581
582 // {16-13} = Rn
583 // {12} = isAdd
584 // {11-0} = shifter
585 // {3-0} = Rm
586 // {4} = 0
587 // {6-5} = type
588 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000589 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000590 Binary |= Rn << 13;
591 Binary |= SBits << 5;
592 Binary |= ShImm << 7;
593 if (isAdd)
594 Binary |= 1 << 12;
595 return Binary;
596}
597
Jim Grosbach570a9222010-11-11 01:09:40 +0000598uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000599getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
600 SmallVectorImpl<MCFixup> &Fixups) const {
601 // {17-14} Rn
602 // {13} 1 == imm12, 0 == Rm
603 // {12} isAdd
604 // {11-0} imm12/Rm
605 const MCOperand &MO = MI.getOperand(OpIdx);
606 unsigned Rn = getARMRegisterNumbering(MO.getReg());
607 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
608 Binary |= Rn << 14;
609 return Binary;
610}
611
612uint32_t ARMMCCodeEmitter::
613getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
614 SmallVectorImpl<MCFixup> &Fixups) const {
615 // {13} 1 == imm12, 0 == Rm
616 // {12} isAdd
617 // {11-0} imm12/Rm
618 const MCOperand &MO = MI.getOperand(OpIdx);
619 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
620 unsigned Imm = MO1.getImm();
621 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
622 bool isReg = MO.getReg() != 0;
623 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
624 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
625 if (isReg) {
626 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
627 Binary <<= 7; // Shift amount is bits [11:7]
628 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
629 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
630 }
631 return Binary | (isAdd << 12) | (isReg << 13);
632}
633
634uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000635getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
636 SmallVectorImpl<MCFixup> &Fixups) const {
637 // {9} 1 == imm8, 0 == Rm
638 // {8} isAdd
639 // {7-4} imm7_4/zero
640 // {3-0} imm3_0/Rm
641 const MCOperand &MO = MI.getOperand(OpIdx);
642 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
643 unsigned Imm = MO1.getImm();
644 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
645 bool isImm = MO.getReg() == 0;
646 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
647 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
648 if (!isImm)
649 Imm8 = getARMRegisterNumbering(MO.getReg());
650 return Imm8 | (isAdd << 8) | (isImm << 9);
651}
652
653uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000654getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
655 SmallVectorImpl<MCFixup> &Fixups) const {
656 // {13} 1 == imm8, 0 == Rm
657 // {12-9} Rn
658 // {8} isAdd
659 // {7-4} imm7_4/zero
660 // {3-0} imm3_0/Rm
661 const MCOperand &MO = MI.getOperand(OpIdx);
662 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
663 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
664 unsigned Rn = getARMRegisterNumbering(MO.getReg());
665 unsigned Imm = MO2.getImm();
666 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
667 bool isImm = MO1.getReg() == 0;
668 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
669 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
670 if (!isImm)
671 Imm8 = getARMRegisterNumbering(MO1.getReg());
672 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
673}
674
Bill Wendlingb8958b02010-12-08 01:57:09 +0000675/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000676uint32_t ARMMCCodeEmitter::
677getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
678 SmallVectorImpl<MCFixup> &Fixups) const {
679 // [SP, #imm]
680 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000681 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000682#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
683 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
684 "Unexpected base register!");
685#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000686 // The immediate is already shifted for the implicit zeroes, so no change
687 // here.
688 return MO1.getImm() & 0xff;
689}
690
Bill Wendling1fd374e2010-11-30 22:57:21 +0000691/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
692static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
693 unsigned Scale) {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000694 // [Rn, Rm]
695 // {5-3} = Rm
696 // {2-0} = Rn
697 //
698 // [Rn, #imm]
699 // {7-3} = imm5
700 // {2-0} = Rn
701 const MCOperand &MO = MI.getOperand(OpIdx);
702 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
703 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
704 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling1fd374e2010-11-30 22:57:21 +0000705 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
Bill Wendling0bdf0c02010-12-03 00:53:22 +0000706
707 if (MO2.getReg() != 0)
708 // Is an immediate.
709 Imm5 = getARMRegisterNumbering(MO2.getReg());
710
711 return (Imm5 << 3) | Rn;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000712}
713
Bill Wendling1fd374e2010-11-30 22:57:21 +0000714/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
715uint32_t ARMMCCodeEmitter::
716getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
717 SmallVectorImpl<MCFixup> &) const {
718 return getAddrModeSOpValue(MI, OpIdx, 4);
719}
720
721/// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
722uint32_t ARMMCCodeEmitter::
723getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
724 SmallVectorImpl<MCFixup> &) const {
725 return getAddrModeSOpValue(MI, OpIdx, 2);
726}
727
728/// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
729uint32_t ARMMCCodeEmitter::
730getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
731 SmallVectorImpl<MCFixup> &) const {
732 return getAddrModeSOpValue(MI, OpIdx, 1);
733}
734
Bill Wendlingb8958b02010-12-08 01:57:09 +0000735/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
736uint32_t ARMMCCodeEmitter::
737getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
738 SmallVectorImpl<MCFixup> &Fixups) const {
739 const MCOperand &MO = MI.getOperand(OpIdx);
740
741 // If the destination is an immediate, we have nothing to do.
742 if (MO.isImm()) return MO.getImm();
743 assert (MO.isExpr() && "Unexpected branch target type!");
744 const MCExpr *Expr = MO.getExpr();
745 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_thumb_cp);
746 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
747
748 // All of the information is in the fixup.
749 return 0;
750}
751
Jim Grosbach5177f792010-12-01 21:09:40 +0000752/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000753uint32_t ARMMCCodeEmitter::
754getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
755 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000756 // {12-9} = reg
757 // {8} = (U)nsigned (add == '1', sub == '0')
758 // {7-0} = imm8
759 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000760 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000761 // If The first operand isn't a register, we have a label reference.
762 const MCOperand &MO = MI.getOperand(OpIdx);
763 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000764 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000765 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000766 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000767
768 assert(MO.isExpr() && "Unexpected machine operand type!");
769 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000770 MCFixupKind Kind;
771 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
772 if (Subtarget.isThumb2())
773 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
774 else
775 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000776 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
777
778 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000779 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000780 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000781 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
782 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000783
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000784 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
785 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000786 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000787 Binary |= (1 << 8);
788 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000789 return Binary;
790}
791
Jim Grosbach806e80e2010-11-03 23:52:49 +0000792unsigned ARMMCCodeEmitter::
793getSORegOpValue(const MCInst &MI, unsigned OpIdx,
794 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000795 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
796 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
797 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000798 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000799 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000800 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000801 // {6-5} = type
802 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000803 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000804 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000805 // else (imm shift)
806 // {11-7} = imm
807
808 const MCOperand &MO = MI.getOperand(OpIdx);
809 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
810 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
811 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
812
813 // Encode Rm.
814 unsigned Binary = getARMRegisterNumbering(MO.getReg());
815
816 // Encode the shift opcode.
817 unsigned SBits = 0;
818 unsigned Rs = MO1.getReg();
819 if (Rs) {
820 // Set shift operand (bit[7:4]).
821 // LSL - 0001
822 // LSR - 0011
823 // ASR - 0101
824 // ROR - 0111
825 // RRX - 0110 and bit[11:8] clear.
826 switch (SOpc) {
827 default: llvm_unreachable("Unknown shift opc!");
828 case ARM_AM::lsl: SBits = 0x1; break;
829 case ARM_AM::lsr: SBits = 0x3; break;
830 case ARM_AM::asr: SBits = 0x5; break;
831 case ARM_AM::ror: SBits = 0x7; break;
832 case ARM_AM::rrx: SBits = 0x6; break;
833 }
834 } else {
835 // Set shift operand (bit[6:4]).
836 // LSL - 000
837 // LSR - 010
838 // ASR - 100
839 // ROR - 110
840 switch (SOpc) {
841 default: llvm_unreachable("Unknown shift opc!");
842 case ARM_AM::lsl: SBits = 0x0; break;
843 case ARM_AM::lsr: SBits = 0x2; break;
844 case ARM_AM::asr: SBits = 0x4; break;
845 case ARM_AM::ror: SBits = 0x6; break;
846 }
847 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000848
Jim Grosbachef324d72010-10-12 23:53:58 +0000849 Binary |= SBits << 4;
850 if (SOpc == ARM_AM::rrx)
851 return Binary;
852
853 // Encode the shift operation Rs or shift_imm (except rrx).
854 if (Rs) {
855 // Encode Rs bit[11:8].
856 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
857 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
858 }
859
860 // Encode shift_imm bit[11:7].
861 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
862}
863
Jim Grosbach806e80e2010-11-03 23:52:49 +0000864unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000865getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
866 SmallVectorImpl<MCFixup> &Fixups) const {
867 const MCOperand &MO1 = MI.getOperand(OpNum);
868 const MCOperand &MO2 = MI.getOperand(OpNum+1);
869 const MCOperand &MO3 = MI.getOperand(OpNum+2);
870
871 // Encoded as [Rn, Rm, imm].
872 // FIXME: Needs fixup support.
873 unsigned Value = getARMRegisterNumbering(MO1.getReg());
874 Value <<= 4;
875 Value |= getARMRegisterNumbering(MO2.getReg());
876 Value <<= 2;
877 Value |= MO3.getImm();
878
879 return Value;
880}
881
882unsigned ARMMCCodeEmitter::
883getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
884 SmallVectorImpl<MCFixup> &Fixups) const {
885 const MCOperand &MO1 = MI.getOperand(OpNum);
886 const MCOperand &MO2 = MI.getOperand(OpNum+1);
887
888 // FIXME: Needs fixup support.
889 unsigned Value = getARMRegisterNumbering(MO1.getReg());
890
891 // Even though the immediate is 8 bits long, we need 9 bits in order
892 // to represent the (inverse of the) sign bit.
893 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000894 int32_t tmp = (int32_t)MO2.getImm();
895 if (tmp < 0)
896 tmp = abs(tmp);
897 else
898 Value |= 256; // Set the ADD bit
899 Value |= tmp & 255;
900 return Value;
901}
902
903unsigned ARMMCCodeEmitter::
904getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
905 SmallVectorImpl<MCFixup> &Fixups) const {
906 const MCOperand &MO1 = MI.getOperand(OpNum);
907
908 // FIXME: Needs fixup support.
909 unsigned Value = 0;
910 int32_t tmp = (int32_t)MO1.getImm();
911 if (tmp < 0)
912 tmp = abs(tmp);
913 else
914 Value |= 256; // Set the ADD bit
915 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000916 return Value;
917}
918
919unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000920getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
921 SmallVectorImpl<MCFixup> &Fixups) const {
922 const MCOperand &MO1 = MI.getOperand(OpNum);
923
924 // FIXME: Needs fixup support.
925 unsigned Value = 0;
926 int32_t tmp = (int32_t)MO1.getImm();
927 if (tmp < 0)
928 tmp = abs(tmp);
929 else
930 Value |= 4096; // Set the ADD bit
931 Value |= tmp & 4095;
932 return Value;
933}
934
935unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000936getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
937 SmallVectorImpl<MCFixup> &Fixups) const {
938 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
939 // shifted. The second is the amount to shift by.
940 //
941 // {3-0} = Rm.
942 // {4} = 0
943 // {6-5} = type
944 // {11-7} = imm
945
946 const MCOperand &MO = MI.getOperand(OpIdx);
947 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
948 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
949
950 // Encode Rm.
951 unsigned Binary = getARMRegisterNumbering(MO.getReg());
952
953 // Encode the shift opcode.
954 unsigned SBits = 0;
955 // Set shift operand (bit[6:4]).
956 // LSL - 000
957 // LSR - 010
958 // ASR - 100
959 // ROR - 110
960 switch (SOpc) {
961 default: llvm_unreachable("Unknown shift opc!");
962 case ARM_AM::lsl: SBits = 0x0; break;
963 case ARM_AM::lsr: SBits = 0x2; break;
964 case ARM_AM::asr: SBits = 0x4; break;
965 case ARM_AM::ror: SBits = 0x6; break;
966 }
967
968 Binary |= SBits << 4;
969 if (SOpc == ARM_AM::rrx)
970 return Binary;
971
972 // Encode shift_imm bit[11:7].
973 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
974}
975
976unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000977getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
978 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000979 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
980 // msb of the mask.
981 const MCOperand &MO = MI.getOperand(Op);
982 uint32_t v = ~MO.getImm();
983 uint32_t lsb = CountTrailingZeros_32(v);
984 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
985 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
986 return lsb | (msb << 5);
987}
988
Jim Grosbach806e80e2010-11-03 23:52:49 +0000989unsigned ARMMCCodeEmitter::
990getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000991 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +0000992 // VLDM/VSTM:
993 // {12-8} = Vd
994 // {7-0} = Number of registers
995 //
996 // LDM/STM:
997 // {15-0} = Bitfield of GPRs.
998 unsigned Reg = MI.getOperand(Op).getReg();
999 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1000 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1001
Bill Wendling5e559a22010-11-09 00:30:18 +00001002 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001003
1004 if (SPRRegs || DPRRegs) {
1005 // VLDM/VSTM
1006 unsigned RegNo = getARMRegisterNumbering(Reg);
1007 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1008 Binary |= (RegNo & 0x1f) << 8;
1009 if (SPRRegs)
1010 Binary |= NumRegs;
1011 else
1012 Binary |= NumRegs * 2;
1013 } else {
1014 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1015 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1016 Binary |= 1 << RegNo;
1017 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001018 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001019
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001020 return Binary;
1021}
1022
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001023/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1024/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001025unsigned ARMMCCodeEmitter::
1026getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1027 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001028 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001029 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001030
Owen Andersond9aa7d32010-11-02 00:05:05 +00001031 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001032 unsigned Align = 0;
1033
1034 switch (Imm.getImm()) {
1035 default: break;
1036 case 2:
1037 case 4:
1038 case 8: Align = 0x01; break;
1039 case 16: Align = 0x02; break;
1040 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001041 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001042
Owen Andersond9aa7d32010-11-02 00:05:05 +00001043 return RegNo | (Align << 4);
1044}
1045
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001046/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1047/// alignment operand for use in VLD-dup instructions. This is the same as
1048/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1049/// different for VLD4-dup.
1050unsigned ARMMCCodeEmitter::
1051getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1052 SmallVectorImpl<MCFixup> &Fixups) const {
1053 const MCOperand &Reg = MI.getOperand(Op);
1054 const MCOperand &Imm = MI.getOperand(Op + 1);
1055
1056 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1057 unsigned Align = 0;
1058
1059 switch (Imm.getImm()) {
1060 default: break;
1061 case 2:
1062 case 4:
1063 case 8: Align = 0x01; break;
1064 case 16: Align = 0x03; break;
1065 }
1066
1067 return RegNo | (Align << 4);
1068}
1069
Jim Grosbach806e80e2010-11-03 23:52:49 +00001070unsigned ARMMCCodeEmitter::
1071getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1072 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001073 const MCOperand &MO = MI.getOperand(Op);
1074 if (MO.getReg() == 0) return 0x0D;
1075 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001076}
1077
Jim Grosbach568eeed2010-09-17 18:46:17 +00001078void ARMMCCodeEmitter::
1079EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001080 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001081 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001082 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001083 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001084 uint64_t TSFlags = Desc.TSFlags;
1085 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001086 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001087 int Size;
1088 // Basic size info comes from the TSFlags field.
1089 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1090 default: llvm_unreachable("Unexpected instruction size!");
1091 case ARMII::Size2Bytes: Size = 2; break;
1092 case ARMII::Size4Bytes: Size = 4; break;
1093 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001094 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1095 // Thumb 32-bit wide instructions need to be have the high order halfword
1096 // emitted first.
1097 if (Subtarget.isThumb() && Size == 4) {
1098 EmitConstant(Binary >> 16, 2, OS);
1099 EmitConstant(Binary & 0xffff, 2, OS);
1100 } else
1101 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001102 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001103}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001104
Jim Grosbach806e80e2010-11-03 23:52:49 +00001105#include "ARMGenMCCodeEmitter.inc"