blob: f8802d1d56e9d7bfa8ad80883ed139aa0eacd9d6 [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 },
Owen Anderson05018c22010-12-09 20:27:52 +000050 { "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
51 MCFixupKindInfo::FKF_IsAligned},
Jim Grosbachdff84b02010-12-02 00:28:45 +000052 { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersond8e351b2010-12-08 00:18:36 +000053 { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000054 { "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
55 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersonfe7fac72010-12-09 21:34:47 +000056 { "fixup_t2_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
57 MCFixupKindInfo::FKF_IsAligned},
Jim Grosbach662a8162010-12-06 23:57:07 +000058 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendling09aa3f02010-12-09 00:39:08 +000059 { "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachb492a7c2010-12-09 19:50:12 +000060 { "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendlingb8958b02010-12-08 01:57:09 +000061 { "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000062 { "fixup_arm_movt_hi16", 0, 16, 0 },
63 { "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000064 };
65
66 if (Kind < FirstTargetFixupKind)
67 return MCCodeEmitter::getFixupKindInfo(Kind);
68
69 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
70 "Invalid kind!");
71 return Infos[Kind - FirstTargetFixupKind];
72 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000073 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
74
Jim Grosbach9af82ba2010-10-07 21:57:55 +000075 // getBinaryCodeForInstr - TableGen'erated function for getting the
76 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000077 unsigned getBinaryCodeForInstr(const MCInst &MI,
78 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000079
80 /// getMachineOpValue - Return binary encoding of operand. If the machine
81 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000082 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
83 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000084
Jason W Kim837caa92010-11-18 23:37:15 +000085 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
86 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
87 SmallVectorImpl<MCFixup> &Fixups) const;
88
Bill Wendling92b5a2e2010-11-03 01:49:29 +000089 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000090 unsigned &Reg, unsigned &Imm,
91 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000092
Jim Grosbach662a8162010-12-06 23:57:07 +000093 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +000094 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +000095 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
96 SmallVectorImpl<MCFixup> &Fixups) const;
97
Bill Wendling09aa3f02010-12-09 00:39:08 +000098 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
99 /// BLX branch target.
100 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
101 SmallVectorImpl<MCFixup> &Fixups) const;
102
Jim Grosbach027d6e82010-12-09 19:04:53 +0000103 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
104 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000105 SmallVectorImpl<MCFixup> &Fixups) const;
106
Jim Grosbachc466b932010-11-11 18:04:49 +0000107 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
108 /// branch target.
109 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
110 SmallVectorImpl<MCFixup> &Fixups) const;
111
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000112 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
113 /// ADR label target.
114 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
115 SmallVectorImpl<MCFixup> &Fixups) const;
116
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000117 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
118 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000119 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
120 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000121
Owen Anderson9d63d902010-12-01 19:18:46 +0000122 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
123 /// operand.
124 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
125 SmallVectorImpl<MCFixup> &Fixups) const;
126
127
Jim Grosbach54fea632010-11-09 17:20:53 +0000128 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
129 /// operand as needed by load/store instructions.
130 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
131 SmallVectorImpl<MCFixup> &Fixups) const;
132
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000133 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
134 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
135 SmallVectorImpl<MCFixup> &Fixups) const {
136 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
137 switch (Mode) {
138 default: assert(0 && "Unknown addressing sub-mode!");
139 case ARM_AM::da: return 0;
140 case ARM_AM::ia: return 1;
141 case ARM_AM::db: return 2;
142 case ARM_AM::ib: return 3;
143 }
144 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000145 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
146 ///
147 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
148 switch (ShOpc) {
149 default: llvm_unreachable("Unknown shift opc!");
150 case ARM_AM::no_shift:
151 case ARM_AM::lsl: return 0;
152 case ARM_AM::lsr: return 1;
153 case ARM_AM::asr: return 2;
154 case ARM_AM::ror:
155 case ARM_AM::rrx: return 3;
156 }
157 return 0;
158 }
159
160 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
161 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
162 SmallVectorImpl<MCFixup> &Fixups) const;
163
164 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
165 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
166 SmallVectorImpl<MCFixup> &Fixups) const;
167
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000168 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
169 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
170 SmallVectorImpl<MCFixup> &Fixups) const;
171
Jim Grosbach570a9222010-11-11 01:09:40 +0000172 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
173 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
174 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000175
Jim Grosbachd967cd02010-12-07 21:50:47 +0000176 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
177 /// operand.
178 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
179 SmallVectorImpl<MCFixup> &Fixups) const;
180
Bill Wendling272df512010-12-09 21:49:07 +0000181 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
182 uint32_t getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
183 SmallVectorImpl<MCFixup> &) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000184
Bill Wendlingb8958b02010-12-08 01:57:09 +0000185 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
186 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
187 SmallVectorImpl<MCFixup> &Fixups) const;
188
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000189 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000190 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
191 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000192
Jim Grosbach08bd5492010-10-12 23:00:24 +0000193 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000194 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
195 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000196 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
197 // '1' respectively.
198 return MI.getOperand(Op).getReg() == ARM::CPSR;
199 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000200
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000201 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000202 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
203 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000204 unsigned SoImm = MI.getOperand(Op).getImm();
205 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
206 assert(SoImmVal != -1 && "Not a valid so_imm value!");
207
208 // Encode rotate_imm.
209 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
210 << ARMII::SoRotImmShift;
211
212 // Encode immed_8.
213 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
214 return Binary;
215 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000216
217 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
218 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
219 SmallVectorImpl<MCFixup> &Fixups) const {
220 unsigned SoImm = MI.getOperand(Op).getImm();
221 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
222 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
223 return Encoded;
224 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000225
Owen Anderson75579f72010-11-29 22:44:32 +0000226 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
227 SmallVectorImpl<MCFixup> &Fixups) const;
228 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
229 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000230 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
231 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000232 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
233 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000234
Jim Grosbachef324d72010-10-12 23:53:58 +0000235 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000236 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
237 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000238 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
239 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000240
Jim Grosbach806e80e2010-11-03 23:52:49 +0000241 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
242 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000243 switch (MI.getOperand(Op).getImm()) {
244 default: assert (0 && "Not a valid rot_imm value!");
245 case 0: return 0;
246 case 8: return 1;
247 case 16: return 2;
248 case 24: return 3;
249 }
250 }
251
Jim Grosbach806e80e2010-11-03 23:52:49 +0000252 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
253 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000254 return MI.getOperand(Op).getImm() - 1;
255 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000256
Jim Grosbach806e80e2010-11-03 23:52:49 +0000257 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
258 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000259 return 64 - MI.getOperand(Op).getImm();
260 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000261
Jim Grosbach806e80e2010-11-03 23:52:49 +0000262 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
263 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000264
Jim Grosbach806e80e2010-11-03 23:52:49 +0000265 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
266 SmallVectorImpl<MCFixup> &Fixups) const;
267 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
268 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000269 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
270 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000271 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
272 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000273
Owen Andersonc7139a62010-11-11 19:07:48 +0000274 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
275 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000276 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000277 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000278 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000279 unsigned EncodedValue) const;
280
281 unsigned VFPThumb2PostEncoder(const MCInst &MI,
282 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000283
Jim Grosbach70933262010-11-04 01:12:30 +0000284 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000285 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000286 }
287
Jim Grosbach70933262010-11-04 01:12:30 +0000288 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000289 // Output the constant in little endian byte order.
290 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000291 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000292 Val >>= 8;
293 }
294 }
295
Jim Grosbach568eeed2010-09-17 18:46:17 +0000296 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
297 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000298};
299
300} // end anonymous namespace
301
Bill Wendling0800ce72010-11-02 22:53:11 +0000302MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
303 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000304 return new ARMMCCodeEmitter(TM, Ctx);
305}
306
Owen Anderson57dac882010-11-11 21:36:43 +0000307/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000308/// instructions, and rewrite them to their Thumb2 form if we are currently in
309/// Thumb2 mode.
310unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
311 unsigned EncodedValue) const {
312 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
313 if (Subtarget.isThumb2()) {
314 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
315 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
316 // set to 1111.
317 unsigned Bit24 = EncodedValue & 0x01000000;
318 unsigned Bit28 = Bit24 << 4;
319 EncodedValue &= 0xEFFFFFFF;
320 EncodedValue |= Bit28;
321 EncodedValue |= 0x0F000000;
322 }
323
324 return EncodedValue;
325}
326
Owen Anderson57dac882010-11-11 21:36:43 +0000327/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
328/// instructions, and rewrite them to their Thumb2 form if we are currently in
329/// Thumb2 mode.
330unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
331 unsigned EncodedValue) const {
332 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
333 if (Subtarget.isThumb2()) {
334 EncodedValue &= 0xF0FFFFFF;
335 EncodedValue |= 0x09000000;
336 }
337
338 return EncodedValue;
339}
340
Owen Anderson8f143912010-11-11 23:12:55 +0000341/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
342/// instructions, and rewrite them to their Thumb2 form if we are currently in
343/// Thumb2 mode.
344unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
345 unsigned EncodedValue) const {
346 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
347 if (Subtarget.isThumb2()) {
348 EncodedValue &= 0x00FFFFFF;
349 EncodedValue |= 0xEE000000;
350 }
351
352 return EncodedValue;
353}
354
Bill Wendlingcf590262010-12-01 21:54:50 +0000355/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
356/// them to their Thumb2 form if we are currently in Thumb2 mode.
357unsigned ARMMCCodeEmitter::
358VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
359 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
360 EncodedValue &= 0x0FFFFFFF;
361 EncodedValue |= 0xE0000000;
362 }
363 return EncodedValue;
364}
Owen Anderson57dac882010-11-11 21:36:43 +0000365
Jim Grosbach56ac9072010-10-08 21:45:55 +0000366/// getMachineOpValue - Return binary encoding of operand. If the machine
367/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000368unsigned ARMMCCodeEmitter::
369getMachineOpValue(const MCInst &MI, const MCOperand &MO,
370 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000371 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000372 unsigned Reg = MO.getReg();
373 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000374
Jim Grosbachb0708d22010-11-30 23:51:41 +0000375 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000376 switch (Reg) {
377 default:
378 return RegNo;
379 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
380 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
381 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
382 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
383 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000384 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000385 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000386 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000387 } else if (MO.isFPImm()) {
388 return static_cast<unsigned>(APFloat(MO.getFPImm())
389 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000390 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000391
Jim Grosbach817c1a62010-11-19 00:27:09 +0000392 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000393 return 0;
394}
395
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000396/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000397bool ARMMCCodeEmitter::
398EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
399 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000400 const MCOperand &MO = MI.getOperand(OpIdx);
401 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000402
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000403 Reg = getARMRegisterNumbering(MO.getReg());
404
405 int32_t SImm = MO1.getImm();
406 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000407
Jim Grosbachab682a22010-10-28 18:34:10 +0000408 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000409 if (SImm == INT32_MIN)
410 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000411
Jim Grosbachab682a22010-10-28 18:34:10 +0000412 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000413 if (SImm < 0) {
414 SImm = -SImm;
415 isAdd = false;
416 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000417
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000418 Imm = SImm;
419 return isAdd;
420}
421
Bill Wendlingdff2f712010-12-08 23:01:43 +0000422/// getBranchTargetOpValue - Helper function to get the branch target operand,
423/// which is either an immediate or requires a fixup.
424static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
425 unsigned FixupKind,
426 SmallVectorImpl<MCFixup> &Fixups) {
427 const MCOperand &MO = MI.getOperand(OpIdx);
428
429 // If the destination is an immediate, we have nothing to do.
430 if (MO.isImm()) return MO.getImm();
431 assert(MO.isExpr() && "Unexpected branch target type!");
432 const MCExpr *Expr = MO.getExpr();
433 MCFixupKind Kind = MCFixupKind(FixupKind);
434 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
435
436 // All of the information is in the fixup.
437 return 0;
438}
439
440/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000441uint32_t ARMMCCodeEmitter::
442getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
443 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000444 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000445}
446
Bill Wendling09aa3f02010-12-09 00:39:08 +0000447/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
448/// BLX branch target.
449uint32_t ARMMCCodeEmitter::
450getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
451 SmallVectorImpl<MCFixup> &Fixups) const {
452 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
453}
454
Jim Grosbach027d6e82010-12-09 19:04:53 +0000455/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000456uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000457getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000458 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000459 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000460}
461
462/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
463/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000464uint32_t ARMMCCodeEmitter::
465getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000466 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfb20d892010-12-09 00:27:41 +0000467 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
468 if (Subtarget.isThumb2())
469 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_branch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000470 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000471}
472
Bill Wendlingdff2f712010-12-08 23:01:43 +0000473/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
474/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000475uint32_t ARMMCCodeEmitter::
476getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
477 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000478 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
479 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
480 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000481}
482
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000483/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000484uint32_t ARMMCCodeEmitter::
485getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
486 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000487 // {17-13} = reg
488 // {12} = (U)nsigned (add == '1', sub == '0')
489 // {11-0} = imm12
490 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000491 bool isAdd = true;
492 // If The first operand isn't a register, we have a label reference.
493 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000494 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
495 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000496 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000497 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000498 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000499
Owen Andersoneb6779c2010-12-07 00:45:21 +0000500 const MCExpr *Expr = 0;
501 if (!MO.isReg())
502 Expr = MO.getExpr();
503 else
504 Expr = MO2.getExpr();
505
Owen Andersond7b3f582010-12-09 01:51:07 +0000506 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
507 MCFixupKind Kind;
508 if (Subtarget.isThumb2())
509 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
510 else
511 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000512 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
513
514 ++MCNumCPRelocations;
515 } else
516 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000517
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000518 uint32_t Binary = Imm12 & 0xfff;
519 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000520 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000521 Binary |= (1 << 12);
522 Binary |= (Reg << 13);
523 return Binary;
524}
525
Owen Anderson9d63d902010-12-01 19:18:46 +0000526/// getT2AddrModeImm8s4OpValue - Return encoding info for
527/// 'reg +/- imm8<<2' operand.
528uint32_t ARMMCCodeEmitter::
529getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
530 SmallVectorImpl<MCFixup> &Fixups) const {
531 // {17-13} = reg
532 // {12} = (U)nsigned (add == '1', sub == '0')
533 // {11-0} = imm8
534 unsigned Reg, Imm8;
535 bool isAdd = true;
536 // If The first operand isn't a register, we have a label reference.
537 const MCOperand &MO = MI.getOperand(OpIdx);
538 if (!MO.isReg()) {
539 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
540 Imm8 = 0;
541 isAdd = false ; // 'U' bit is set as part of the fixup.
542
543 assert(MO.isExpr() && "Unexpected machine operand type!");
544 const MCExpr *Expr = MO.getExpr();
545 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
546 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
547
548 ++MCNumCPRelocations;
549 } else
550 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
551
552 uint32_t Binary = (Imm8 >> 2) & 0xff;
553 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
554 if (isAdd)
555 Binary |= (1 << 9);
556 Binary |= (Reg << 9);
557 return Binary;
558}
559
Jim Grosbach54fea632010-11-09 17:20:53 +0000560uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000561getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
562 SmallVectorImpl<MCFixup> &Fixups) const {
563 // {20-16} = imm{15-12}
564 // {11-0} = imm{11-0}
565 const MCOperand &MO = MI.getOperand(OpIdx);
566 if (MO.isImm()) {
567 return static_cast<unsigned>(MO.getImm());
568 } else if (const MCSymbolRefExpr *Expr =
569 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
570 MCFixupKind Kind;
571 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000572 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000573 case MCSymbolRefExpr::VK_ARM_HI16:
574 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
575 break;
576 case MCSymbolRefExpr::VK_ARM_LO16:
577 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
578 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000579 }
580 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
581 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000582 };
583 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000584 return 0;
585}
586
587uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000588getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
589 SmallVectorImpl<MCFixup> &Fixups) const {
590 const MCOperand &MO = MI.getOperand(OpIdx);
591 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
592 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
593 unsigned Rn = getARMRegisterNumbering(MO.getReg());
594 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000595 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
596 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000597 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
598 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000599
600 // {16-13} = Rn
601 // {12} = isAdd
602 // {11-0} = shifter
603 // {3-0} = Rm
604 // {4} = 0
605 // {6-5} = type
606 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000607 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000608 Binary |= Rn << 13;
609 Binary |= SBits << 5;
610 Binary |= ShImm << 7;
611 if (isAdd)
612 Binary |= 1 << 12;
613 return Binary;
614}
615
Jim Grosbach570a9222010-11-11 01:09:40 +0000616uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000617getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
618 SmallVectorImpl<MCFixup> &Fixups) const {
619 // {17-14} Rn
620 // {13} 1 == imm12, 0 == Rm
621 // {12} isAdd
622 // {11-0} imm12/Rm
623 const MCOperand &MO = MI.getOperand(OpIdx);
624 unsigned Rn = getARMRegisterNumbering(MO.getReg());
625 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
626 Binary |= Rn << 14;
627 return Binary;
628}
629
630uint32_t ARMMCCodeEmitter::
631getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
632 SmallVectorImpl<MCFixup> &Fixups) const {
633 // {13} 1 == imm12, 0 == Rm
634 // {12} isAdd
635 // {11-0} imm12/Rm
636 const MCOperand &MO = MI.getOperand(OpIdx);
637 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
638 unsigned Imm = MO1.getImm();
639 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
640 bool isReg = MO.getReg() != 0;
641 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
642 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
643 if (isReg) {
644 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
645 Binary <<= 7; // Shift amount is bits [11:7]
646 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
647 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
648 }
649 return Binary | (isAdd << 12) | (isReg << 13);
650}
651
652uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000653getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
654 SmallVectorImpl<MCFixup> &Fixups) const {
655 // {9} 1 == imm8, 0 == Rm
656 // {8} isAdd
657 // {7-4} imm7_4/zero
658 // {3-0} imm3_0/Rm
659 const MCOperand &MO = MI.getOperand(OpIdx);
660 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
661 unsigned Imm = MO1.getImm();
662 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
663 bool isImm = MO.getReg() == 0;
664 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
665 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
666 if (!isImm)
667 Imm8 = getARMRegisterNumbering(MO.getReg());
668 return Imm8 | (isAdd << 8) | (isImm << 9);
669}
670
671uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000672getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
673 SmallVectorImpl<MCFixup> &Fixups) const {
674 // {13} 1 == imm8, 0 == Rm
675 // {12-9} Rn
676 // {8} isAdd
677 // {7-4} imm7_4/zero
678 // {3-0} imm3_0/Rm
679 const MCOperand &MO = MI.getOperand(OpIdx);
680 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
681 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
682 unsigned Rn = getARMRegisterNumbering(MO.getReg());
683 unsigned Imm = MO2.getImm();
684 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
685 bool isImm = MO1.getReg() == 0;
686 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
687 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
688 if (!isImm)
689 Imm8 = getARMRegisterNumbering(MO1.getReg());
690 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
691}
692
Bill Wendlingb8958b02010-12-08 01:57:09 +0000693/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000694uint32_t ARMMCCodeEmitter::
695getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
696 SmallVectorImpl<MCFixup> &Fixups) const {
697 // [SP, #imm]
698 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000699 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000700#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
701 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
702 "Unexpected base register!");
703#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000704 // The immediate is already shifted for the implicit zeroes, so no change
705 // here.
706 return MO1.getImm() & 0xff;
707}
708
Bill Wendling1fd374e2010-11-30 22:57:21 +0000709/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000710uint32_t ARMMCCodeEmitter::
711getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
712 SmallVectorImpl<MCFixup> &) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000713 // [Rn, Rm]
714 // {5-3} = Rm
715 // {2-0} = Rn
716 //
717 // [Rn, #imm]
718 // {7-3} = imm5
719 // {2-0} = Rn
720 const MCOperand &MO = MI.getOperand(OpIdx);
721 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
722 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
723 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling272df512010-12-09 21:49:07 +0000724 unsigned Imm5 = MO1.getImm();
Bill Wendling0bdf0c02010-12-03 00:53:22 +0000725
726 if (MO2.getReg() != 0)
727 // Is an immediate.
728 Imm5 = getARMRegisterNumbering(MO2.getReg());
729
Bill Wendling272df512010-12-09 21:49:07 +0000730 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000731}
732
Bill Wendlingb8958b02010-12-08 01:57:09 +0000733/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
734uint32_t ARMMCCodeEmitter::
735getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
736 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000737 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000738}
739
Jim Grosbach5177f792010-12-01 21:09:40 +0000740/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000741uint32_t ARMMCCodeEmitter::
742getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
743 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000744 // {12-9} = reg
745 // {8} = (U)nsigned (add == '1', sub == '0')
746 // {7-0} = imm8
747 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000748 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000749 // If The first operand isn't a register, we have a label reference.
750 const MCOperand &MO = MI.getOperand(OpIdx);
751 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000752 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000753 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000754 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000755
756 assert(MO.isExpr() && "Unexpected machine operand type!");
757 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000758 MCFixupKind Kind;
759 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
760 if (Subtarget.isThumb2())
761 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
762 else
763 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000764 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
765
766 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000767 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000768 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000769 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
770 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000771
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000772 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
773 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000774 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000775 Binary |= (1 << 8);
776 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000777 return Binary;
778}
779
Jim Grosbach806e80e2010-11-03 23:52:49 +0000780unsigned ARMMCCodeEmitter::
781getSORegOpValue(const MCInst &MI, unsigned OpIdx,
782 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000783 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
784 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
785 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000786 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000787 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000788 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000789 // {6-5} = type
790 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000791 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000792 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000793 // else (imm shift)
794 // {11-7} = imm
795
796 const MCOperand &MO = MI.getOperand(OpIdx);
797 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
798 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
799 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
800
801 // Encode Rm.
802 unsigned Binary = getARMRegisterNumbering(MO.getReg());
803
804 // Encode the shift opcode.
805 unsigned SBits = 0;
806 unsigned Rs = MO1.getReg();
807 if (Rs) {
808 // Set shift operand (bit[7:4]).
809 // LSL - 0001
810 // LSR - 0011
811 // ASR - 0101
812 // ROR - 0111
813 // RRX - 0110 and bit[11:8] clear.
814 switch (SOpc) {
815 default: llvm_unreachable("Unknown shift opc!");
816 case ARM_AM::lsl: SBits = 0x1; break;
817 case ARM_AM::lsr: SBits = 0x3; break;
818 case ARM_AM::asr: SBits = 0x5; break;
819 case ARM_AM::ror: SBits = 0x7; break;
820 case ARM_AM::rrx: SBits = 0x6; break;
821 }
822 } else {
823 // Set shift operand (bit[6:4]).
824 // LSL - 000
825 // LSR - 010
826 // ASR - 100
827 // ROR - 110
828 switch (SOpc) {
829 default: llvm_unreachable("Unknown shift opc!");
830 case ARM_AM::lsl: SBits = 0x0; break;
831 case ARM_AM::lsr: SBits = 0x2; break;
832 case ARM_AM::asr: SBits = 0x4; break;
833 case ARM_AM::ror: SBits = 0x6; break;
834 }
835 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000836
Jim Grosbachef324d72010-10-12 23:53:58 +0000837 Binary |= SBits << 4;
838 if (SOpc == ARM_AM::rrx)
839 return Binary;
840
841 // Encode the shift operation Rs or shift_imm (except rrx).
842 if (Rs) {
843 // Encode Rs bit[11:8].
844 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
845 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
846 }
847
848 // Encode shift_imm bit[11:7].
849 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
850}
851
Jim Grosbach806e80e2010-11-03 23:52:49 +0000852unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000853getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
854 SmallVectorImpl<MCFixup> &Fixups) const {
855 const MCOperand &MO1 = MI.getOperand(OpNum);
856 const MCOperand &MO2 = MI.getOperand(OpNum+1);
857 const MCOperand &MO3 = MI.getOperand(OpNum+2);
858
859 // Encoded as [Rn, Rm, imm].
860 // FIXME: Needs fixup support.
861 unsigned Value = getARMRegisterNumbering(MO1.getReg());
862 Value <<= 4;
863 Value |= getARMRegisterNumbering(MO2.getReg());
864 Value <<= 2;
865 Value |= MO3.getImm();
866
867 return Value;
868}
869
870unsigned ARMMCCodeEmitter::
871getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
872 SmallVectorImpl<MCFixup> &Fixups) const {
873 const MCOperand &MO1 = MI.getOperand(OpNum);
874 const MCOperand &MO2 = MI.getOperand(OpNum+1);
875
876 // FIXME: Needs fixup support.
877 unsigned Value = getARMRegisterNumbering(MO1.getReg());
878
879 // Even though the immediate is 8 bits long, we need 9 bits in order
880 // to represent the (inverse of the) sign bit.
881 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000882 int32_t tmp = (int32_t)MO2.getImm();
883 if (tmp < 0)
884 tmp = abs(tmp);
885 else
886 Value |= 256; // Set the ADD bit
887 Value |= tmp & 255;
888 return Value;
889}
890
891unsigned ARMMCCodeEmitter::
892getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
893 SmallVectorImpl<MCFixup> &Fixups) const {
894 const MCOperand &MO1 = MI.getOperand(OpNum);
895
896 // FIXME: Needs fixup support.
897 unsigned Value = 0;
898 int32_t tmp = (int32_t)MO1.getImm();
899 if (tmp < 0)
900 tmp = abs(tmp);
901 else
902 Value |= 256; // Set the ADD bit
903 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000904 return Value;
905}
906
907unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000908getT2AddrModeImm12OffsetOpValue(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 |= 4096; // Set the ADD bit
919 Value |= tmp & 4095;
920 return Value;
921}
922
923unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000924getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
925 SmallVectorImpl<MCFixup> &Fixups) const {
926 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
927 // shifted. The second is the amount to shift by.
928 //
929 // {3-0} = Rm.
930 // {4} = 0
931 // {6-5} = type
932 // {11-7} = imm
933
934 const MCOperand &MO = MI.getOperand(OpIdx);
935 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
936 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
937
938 // Encode Rm.
939 unsigned Binary = getARMRegisterNumbering(MO.getReg());
940
941 // Encode the shift opcode.
942 unsigned SBits = 0;
943 // Set shift operand (bit[6:4]).
944 // LSL - 000
945 // LSR - 010
946 // ASR - 100
947 // ROR - 110
948 switch (SOpc) {
949 default: llvm_unreachable("Unknown shift opc!");
950 case ARM_AM::lsl: SBits = 0x0; break;
951 case ARM_AM::lsr: SBits = 0x2; break;
952 case ARM_AM::asr: SBits = 0x4; break;
953 case ARM_AM::ror: SBits = 0x6; break;
954 }
955
956 Binary |= SBits << 4;
957 if (SOpc == ARM_AM::rrx)
958 return Binary;
959
960 // Encode shift_imm bit[11:7].
961 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
962}
963
964unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000965getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
966 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000967 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
968 // msb of the mask.
969 const MCOperand &MO = MI.getOperand(Op);
970 uint32_t v = ~MO.getImm();
971 uint32_t lsb = CountTrailingZeros_32(v);
972 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
973 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
974 return lsb | (msb << 5);
975}
976
Jim Grosbach806e80e2010-11-03 23:52:49 +0000977unsigned ARMMCCodeEmitter::
978getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000979 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +0000980 // VLDM/VSTM:
981 // {12-8} = Vd
982 // {7-0} = Number of registers
983 //
984 // LDM/STM:
985 // {15-0} = Bitfield of GPRs.
986 unsigned Reg = MI.getOperand(Op).getReg();
987 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
988 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
989
Bill Wendling5e559a22010-11-09 00:30:18 +0000990 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +0000991
992 if (SPRRegs || DPRRegs) {
993 // VLDM/VSTM
994 unsigned RegNo = getARMRegisterNumbering(Reg);
995 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
996 Binary |= (RegNo & 0x1f) << 8;
997 if (SPRRegs)
998 Binary |= NumRegs;
999 else
1000 Binary |= NumRegs * 2;
1001 } else {
1002 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1003 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1004 Binary |= 1 << RegNo;
1005 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001006 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001007
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001008 return Binary;
1009}
1010
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001011/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1012/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001013unsigned ARMMCCodeEmitter::
1014getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1015 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001016 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001017 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001018
Owen Andersond9aa7d32010-11-02 00:05:05 +00001019 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001020 unsigned Align = 0;
1021
1022 switch (Imm.getImm()) {
1023 default: break;
1024 case 2:
1025 case 4:
1026 case 8: Align = 0x01; break;
1027 case 16: Align = 0x02; break;
1028 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001029 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001030
Owen Andersond9aa7d32010-11-02 00:05:05 +00001031 return RegNo | (Align << 4);
1032}
1033
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001034/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1035/// alignment operand for use in VLD-dup instructions. This is the same as
1036/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1037/// different for VLD4-dup.
1038unsigned ARMMCCodeEmitter::
1039getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1040 SmallVectorImpl<MCFixup> &Fixups) const {
1041 const MCOperand &Reg = MI.getOperand(Op);
1042 const MCOperand &Imm = MI.getOperand(Op + 1);
1043
1044 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1045 unsigned Align = 0;
1046
1047 switch (Imm.getImm()) {
1048 default: break;
1049 case 2:
1050 case 4:
1051 case 8: Align = 0x01; break;
1052 case 16: Align = 0x03; break;
1053 }
1054
1055 return RegNo | (Align << 4);
1056}
1057
Jim Grosbach806e80e2010-11-03 23:52:49 +00001058unsigned ARMMCCodeEmitter::
1059getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1060 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001061 const MCOperand &MO = MI.getOperand(Op);
1062 if (MO.getReg() == 0) return 0x0D;
1063 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001064}
1065
Jim Grosbach568eeed2010-09-17 18:46:17 +00001066void ARMMCCodeEmitter::
1067EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001068 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001069 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001070 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001071 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001072 uint64_t TSFlags = Desc.TSFlags;
1073 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001074 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001075 int Size;
1076 // Basic size info comes from the TSFlags field.
1077 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1078 default: llvm_unreachable("Unexpected instruction size!");
1079 case ARMII::Size2Bytes: Size = 2; break;
1080 case ARMII::Size4Bytes: Size = 4; break;
1081 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001082 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1083 // Thumb 32-bit wide instructions need to be have the high order halfword
1084 // emitted first.
1085 if (Subtarget.isThumb() && Size == 4) {
1086 EmitConstant(Binary >> 16, 2, OS);
1087 EmitConstant(Binary & 0xffff, 2, OS);
1088 } else
1089 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001090 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001091}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001092
Jim Grosbach806e80e2010-11-03 23:52:49 +00001093#include "ARMGenMCCodeEmitter.inc"