blob: 4d8791c9989d6b09aeec1179984fafff92bb3f16 [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 Andersonfb20d892010-12-09 00:27:41 +000056 { "fixup_t2_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach662a8162010-12-06 23:57:07 +000057 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendling09aa3f02010-12-09 00:39:08 +000058 { "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachb492a7c2010-12-09 19:50:12 +000059 { "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendlingb8958b02010-12-08 01:57:09 +000060 { "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000061 { "fixup_arm_movt_hi16", 0, 16, 0 },
62 { "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000063 };
64
65 if (Kind < FirstTargetFixupKind)
66 return MCCodeEmitter::getFixupKindInfo(Kind);
67
68 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
69 "Invalid kind!");
70 return Infos[Kind - FirstTargetFixupKind];
71 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000072 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
73
Jim Grosbach9af82ba2010-10-07 21:57:55 +000074 // getBinaryCodeForInstr - TableGen'erated function for getting the
75 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000076 unsigned getBinaryCodeForInstr(const MCInst &MI,
77 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000078
79 /// getMachineOpValue - Return binary encoding of operand. If the machine
80 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000081 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
82 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000083
Jason W Kim837caa92010-11-18 23:37:15 +000084 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
85 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
86 SmallVectorImpl<MCFixup> &Fixups) const;
87
Bill Wendling92b5a2e2010-11-03 01:49:29 +000088 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000089 unsigned &Reg, unsigned &Imm,
90 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000091
Jim Grosbach662a8162010-12-06 23:57:07 +000092 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +000093 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +000094 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
95 SmallVectorImpl<MCFixup> &Fixups) const;
96
Bill Wendling09aa3f02010-12-09 00:39:08 +000097 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
98 /// BLX branch target.
99 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
100 SmallVectorImpl<MCFixup> &Fixups) const;
101
Jim Grosbach027d6e82010-12-09 19:04:53 +0000102 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
103 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000104 SmallVectorImpl<MCFixup> &Fixups) const;
105
Jim Grosbachc466b932010-11-11 18:04:49 +0000106 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
107 /// branch target.
108 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
109 SmallVectorImpl<MCFixup> &Fixups) const;
110
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000111 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
112 /// ADR label target.
113 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
114 SmallVectorImpl<MCFixup> &Fixups) const;
115
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000116 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
117 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000118 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
119 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000120
Owen Anderson9d63d902010-12-01 19:18:46 +0000121 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
122 /// operand.
123 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
124 SmallVectorImpl<MCFixup> &Fixups) const;
125
126
Jim Grosbach54fea632010-11-09 17:20:53 +0000127 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
128 /// operand as needed by load/store instructions.
129 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
130 SmallVectorImpl<MCFixup> &Fixups) const;
131
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000132 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
133 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
134 SmallVectorImpl<MCFixup> &Fixups) const {
135 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
136 switch (Mode) {
137 default: assert(0 && "Unknown addressing sub-mode!");
138 case ARM_AM::da: return 0;
139 case ARM_AM::ia: return 1;
140 case ARM_AM::db: return 2;
141 case ARM_AM::ib: return 3;
142 }
143 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000144 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
145 ///
146 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
147 switch (ShOpc) {
148 default: llvm_unreachable("Unknown shift opc!");
149 case ARM_AM::no_shift:
150 case ARM_AM::lsl: return 0;
151 case ARM_AM::lsr: return 1;
152 case ARM_AM::asr: return 2;
153 case ARM_AM::ror:
154 case ARM_AM::rrx: return 3;
155 }
156 return 0;
157 }
158
159 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
160 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
161 SmallVectorImpl<MCFixup> &Fixups) const;
162
163 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
164 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
165 SmallVectorImpl<MCFixup> &Fixups) const;
166
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000167 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
168 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
169 SmallVectorImpl<MCFixup> &Fixups) const;
170
Jim Grosbach570a9222010-11-11 01:09:40 +0000171 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
172 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
173 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000174
Jim Grosbachd967cd02010-12-07 21:50:47 +0000175 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
176 /// operand.
177 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
178 SmallVectorImpl<MCFixup> &Fixups) const;
179
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000180 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
181 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
182 SmallVectorImpl<MCFixup> &Fixups) const;
183
Bill Wendling1fd374e2010-11-30 22:57:21 +0000184 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
185 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
186 SmallVectorImpl<MCFixup> &Fixups) const;
187
188 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
189 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
190 SmallVectorImpl<MCFixup> &Fixups) const;
191
Bill Wendlingb8958b02010-12-08 01:57:09 +0000192 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
193 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
194 SmallVectorImpl<MCFixup> &Fixups) const;
195
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000196 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000197 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
198 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000199
Jim Grosbach08bd5492010-10-12 23:00:24 +0000200 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000201 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
202 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000203 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
204 // '1' respectively.
205 return MI.getOperand(Op).getReg() == ARM::CPSR;
206 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000207
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000208 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000209 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
210 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000211 unsigned SoImm = MI.getOperand(Op).getImm();
212 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
213 assert(SoImmVal != -1 && "Not a valid so_imm value!");
214
215 // Encode rotate_imm.
216 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
217 << ARMII::SoRotImmShift;
218
219 // Encode immed_8.
220 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
221 return Binary;
222 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000223
224 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
225 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
226 SmallVectorImpl<MCFixup> &Fixups) const {
227 unsigned SoImm = MI.getOperand(Op).getImm();
228 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
229 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
230 return Encoded;
231 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000232
Owen Anderson75579f72010-11-29 22:44:32 +0000233 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
234 SmallVectorImpl<MCFixup> &Fixups) const;
235 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
236 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000237 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
238 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000239 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
240 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000241
Jim Grosbachef324d72010-10-12 23:53:58 +0000242 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000243 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
244 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000245 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
246 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000247
Jim Grosbach806e80e2010-11-03 23:52:49 +0000248 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
249 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000250 switch (MI.getOperand(Op).getImm()) {
251 default: assert (0 && "Not a valid rot_imm value!");
252 case 0: return 0;
253 case 8: return 1;
254 case 16: return 2;
255 case 24: return 3;
256 }
257 }
258
Jim Grosbach806e80e2010-11-03 23:52:49 +0000259 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
260 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000261 return MI.getOperand(Op).getImm() - 1;
262 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000263
Jim Grosbach806e80e2010-11-03 23:52:49 +0000264 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
265 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000266 return 64 - MI.getOperand(Op).getImm();
267 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000268
Jim Grosbach806e80e2010-11-03 23:52:49 +0000269 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
270 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000271
Jim Grosbach806e80e2010-11-03 23:52:49 +0000272 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
273 SmallVectorImpl<MCFixup> &Fixups) const;
274 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
275 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000276 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
277 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000278 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
279 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000280
Owen Andersonc7139a62010-11-11 19:07:48 +0000281 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
282 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000283 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000284 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000285 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000286 unsigned EncodedValue) const;
287
288 unsigned VFPThumb2PostEncoder(const MCInst &MI,
289 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000290
Jim Grosbach70933262010-11-04 01:12:30 +0000291 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000292 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000293 }
294
Jim Grosbach70933262010-11-04 01:12:30 +0000295 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000296 // Output the constant in little endian byte order.
297 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000298 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000299 Val >>= 8;
300 }
301 }
302
Jim Grosbach568eeed2010-09-17 18:46:17 +0000303 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
304 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000305};
306
307} // end anonymous namespace
308
Bill Wendling0800ce72010-11-02 22:53:11 +0000309MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
310 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000311 return new ARMMCCodeEmitter(TM, Ctx);
312}
313
Owen Anderson57dac882010-11-11 21:36:43 +0000314/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000315/// instructions, and rewrite them to their Thumb2 form if we are currently in
316/// Thumb2 mode.
317unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
318 unsigned EncodedValue) const {
319 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
320 if (Subtarget.isThumb2()) {
321 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
322 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
323 // set to 1111.
324 unsigned Bit24 = EncodedValue & 0x01000000;
325 unsigned Bit28 = Bit24 << 4;
326 EncodedValue &= 0xEFFFFFFF;
327 EncodedValue |= Bit28;
328 EncodedValue |= 0x0F000000;
329 }
330
331 return EncodedValue;
332}
333
Owen Anderson57dac882010-11-11 21:36:43 +0000334/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
335/// instructions, and rewrite them to their Thumb2 form if we are currently in
336/// Thumb2 mode.
337unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
338 unsigned EncodedValue) const {
339 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
340 if (Subtarget.isThumb2()) {
341 EncodedValue &= 0xF0FFFFFF;
342 EncodedValue |= 0x09000000;
343 }
344
345 return EncodedValue;
346}
347
Owen Anderson8f143912010-11-11 23:12:55 +0000348/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
349/// instructions, and rewrite them to their Thumb2 form if we are currently in
350/// Thumb2 mode.
351unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
352 unsigned EncodedValue) const {
353 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
354 if (Subtarget.isThumb2()) {
355 EncodedValue &= 0x00FFFFFF;
356 EncodedValue |= 0xEE000000;
357 }
358
359 return EncodedValue;
360}
361
Bill Wendlingcf590262010-12-01 21:54:50 +0000362/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
363/// them to their Thumb2 form if we are currently in Thumb2 mode.
364unsigned ARMMCCodeEmitter::
365VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
366 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
367 EncodedValue &= 0x0FFFFFFF;
368 EncodedValue |= 0xE0000000;
369 }
370 return EncodedValue;
371}
Owen Anderson57dac882010-11-11 21:36:43 +0000372
Jim Grosbach56ac9072010-10-08 21:45:55 +0000373/// getMachineOpValue - Return binary encoding of operand. If the machine
374/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000375unsigned ARMMCCodeEmitter::
376getMachineOpValue(const MCInst &MI, const MCOperand &MO,
377 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000378 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000379 unsigned Reg = MO.getReg();
380 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000381
Jim Grosbachb0708d22010-11-30 23:51:41 +0000382 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000383 switch (Reg) {
384 default:
385 return RegNo;
386 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
387 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
388 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
389 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
390 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000391 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000392 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000393 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000394 } else if (MO.isFPImm()) {
395 return static_cast<unsigned>(APFloat(MO.getFPImm())
396 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000397 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000398
Jim Grosbach817c1a62010-11-19 00:27:09 +0000399 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000400 return 0;
401}
402
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000403/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000404bool ARMMCCodeEmitter::
405EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
406 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000407 const MCOperand &MO = MI.getOperand(OpIdx);
408 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000409
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000410 Reg = getARMRegisterNumbering(MO.getReg());
411
412 int32_t SImm = MO1.getImm();
413 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000414
Jim Grosbachab682a22010-10-28 18:34:10 +0000415 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000416 if (SImm == INT32_MIN)
417 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000418
Jim Grosbachab682a22010-10-28 18:34:10 +0000419 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000420 if (SImm < 0) {
421 SImm = -SImm;
422 isAdd = false;
423 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000424
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000425 Imm = SImm;
426 return isAdd;
427}
428
Bill Wendlingdff2f712010-12-08 23:01:43 +0000429/// getBranchTargetOpValue - Helper function to get the branch target operand,
430/// which is either an immediate or requires a fixup.
431static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
432 unsigned FixupKind,
433 SmallVectorImpl<MCFixup> &Fixups) {
434 const MCOperand &MO = MI.getOperand(OpIdx);
435
436 // If the destination is an immediate, we have nothing to do.
437 if (MO.isImm()) return MO.getImm();
438 assert(MO.isExpr() && "Unexpected branch target type!");
439 const MCExpr *Expr = MO.getExpr();
440 MCFixupKind Kind = MCFixupKind(FixupKind);
441 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
442
443 // All of the information is in the fixup.
444 return 0;
445}
446
447/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000448uint32_t ARMMCCodeEmitter::
449getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
450 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000451 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000452}
453
Bill Wendling09aa3f02010-12-09 00:39:08 +0000454/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
455/// BLX branch target.
456uint32_t ARMMCCodeEmitter::
457getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
458 SmallVectorImpl<MCFixup> &Fixups) const {
459 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
460}
461
Jim Grosbach027d6e82010-12-09 19:04:53 +0000462/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000463uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000464getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000465 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000466 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000467}
468
469/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
470/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000471uint32_t ARMMCCodeEmitter::
472getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000473 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfb20d892010-12-09 00:27:41 +0000474 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
475 if (Subtarget.isThumb2())
476 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_branch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000477 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000478}
479
Bill Wendlingdff2f712010-12-08 23:01:43 +0000480/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
481/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000482uint32_t ARMMCCodeEmitter::
483getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
484 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000485 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
486 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
487 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000488}
489
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000490/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000491uint32_t ARMMCCodeEmitter::
492getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
493 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000494 // {17-13} = reg
495 // {12} = (U)nsigned (add == '1', sub == '0')
496 // {11-0} = imm12
497 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000498 bool isAdd = true;
499 // If The first operand isn't a register, we have a label reference.
500 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000501 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
502 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000503 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000504 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000505 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000506
Owen Andersoneb6779c2010-12-07 00:45:21 +0000507 const MCExpr *Expr = 0;
508 if (!MO.isReg())
509 Expr = MO.getExpr();
510 else
511 Expr = MO2.getExpr();
512
Owen Andersond7b3f582010-12-09 01:51:07 +0000513 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
514 MCFixupKind Kind;
515 if (Subtarget.isThumb2())
516 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
517 else
518 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000519 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
520
521 ++MCNumCPRelocations;
522 } else
523 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000524
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000525 uint32_t Binary = Imm12 & 0xfff;
526 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000527 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000528 Binary |= (1 << 12);
529 Binary |= (Reg << 13);
530 return Binary;
531}
532
Owen Anderson9d63d902010-12-01 19:18:46 +0000533/// getT2AddrModeImm8s4OpValue - Return encoding info for
534/// 'reg +/- imm8<<2' operand.
535uint32_t ARMMCCodeEmitter::
536getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
537 SmallVectorImpl<MCFixup> &Fixups) const {
538 // {17-13} = reg
539 // {12} = (U)nsigned (add == '1', sub == '0')
540 // {11-0} = imm8
541 unsigned Reg, Imm8;
542 bool isAdd = true;
543 // If The first operand isn't a register, we have a label reference.
544 const MCOperand &MO = MI.getOperand(OpIdx);
545 if (!MO.isReg()) {
546 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
547 Imm8 = 0;
548 isAdd = false ; // 'U' bit is set as part of the fixup.
549
550 assert(MO.isExpr() && "Unexpected machine operand type!");
551 const MCExpr *Expr = MO.getExpr();
552 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
553 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
554
555 ++MCNumCPRelocations;
556 } else
557 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
558
559 uint32_t Binary = (Imm8 >> 2) & 0xff;
560 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
561 if (isAdd)
562 Binary |= (1 << 9);
563 Binary |= (Reg << 9);
564 return Binary;
565}
566
Jim Grosbach54fea632010-11-09 17:20:53 +0000567uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000568getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
569 SmallVectorImpl<MCFixup> &Fixups) const {
570 // {20-16} = imm{15-12}
571 // {11-0} = imm{11-0}
572 const MCOperand &MO = MI.getOperand(OpIdx);
573 if (MO.isImm()) {
574 return static_cast<unsigned>(MO.getImm());
575 } else if (const MCSymbolRefExpr *Expr =
576 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
577 MCFixupKind Kind;
578 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000579 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000580 case MCSymbolRefExpr::VK_ARM_HI16:
581 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
582 break;
583 case MCSymbolRefExpr::VK_ARM_LO16:
584 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
585 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000586 }
587 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
588 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000589 };
590 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000591 return 0;
592}
593
594uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000595getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
596 SmallVectorImpl<MCFixup> &Fixups) const {
597 const MCOperand &MO = MI.getOperand(OpIdx);
598 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
599 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
600 unsigned Rn = getARMRegisterNumbering(MO.getReg());
601 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000602 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
603 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000604 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
605 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000606
607 // {16-13} = Rn
608 // {12} = isAdd
609 // {11-0} = shifter
610 // {3-0} = Rm
611 // {4} = 0
612 // {6-5} = type
613 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000614 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000615 Binary |= Rn << 13;
616 Binary |= SBits << 5;
617 Binary |= ShImm << 7;
618 if (isAdd)
619 Binary |= 1 << 12;
620 return Binary;
621}
622
Jim Grosbach570a9222010-11-11 01:09:40 +0000623uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000624getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
625 SmallVectorImpl<MCFixup> &Fixups) const {
626 // {17-14} Rn
627 // {13} 1 == imm12, 0 == Rm
628 // {12} isAdd
629 // {11-0} imm12/Rm
630 const MCOperand &MO = MI.getOperand(OpIdx);
631 unsigned Rn = getARMRegisterNumbering(MO.getReg());
632 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
633 Binary |= Rn << 14;
634 return Binary;
635}
636
637uint32_t ARMMCCodeEmitter::
638getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
639 SmallVectorImpl<MCFixup> &Fixups) const {
640 // {13} 1 == imm12, 0 == Rm
641 // {12} isAdd
642 // {11-0} imm12/Rm
643 const MCOperand &MO = MI.getOperand(OpIdx);
644 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
645 unsigned Imm = MO1.getImm();
646 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
647 bool isReg = MO.getReg() != 0;
648 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
649 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
650 if (isReg) {
651 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
652 Binary <<= 7; // Shift amount is bits [11:7]
653 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
654 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
655 }
656 return Binary | (isAdd << 12) | (isReg << 13);
657}
658
659uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000660getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
661 SmallVectorImpl<MCFixup> &Fixups) const {
662 // {9} 1 == imm8, 0 == Rm
663 // {8} isAdd
664 // {7-4} imm7_4/zero
665 // {3-0} imm3_0/Rm
666 const MCOperand &MO = MI.getOperand(OpIdx);
667 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
668 unsigned Imm = MO1.getImm();
669 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
670 bool isImm = MO.getReg() == 0;
671 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
672 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
673 if (!isImm)
674 Imm8 = getARMRegisterNumbering(MO.getReg());
675 return Imm8 | (isAdd << 8) | (isImm << 9);
676}
677
678uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000679getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
680 SmallVectorImpl<MCFixup> &Fixups) const {
681 // {13} 1 == imm8, 0 == Rm
682 // {12-9} Rn
683 // {8} isAdd
684 // {7-4} imm7_4/zero
685 // {3-0} imm3_0/Rm
686 const MCOperand &MO = MI.getOperand(OpIdx);
687 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
688 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
689 unsigned Rn = getARMRegisterNumbering(MO.getReg());
690 unsigned Imm = MO2.getImm();
691 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
692 bool isImm = MO1.getReg() == 0;
693 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
694 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
695 if (!isImm)
696 Imm8 = getARMRegisterNumbering(MO1.getReg());
697 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
698}
699
Bill Wendlingb8958b02010-12-08 01:57:09 +0000700/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000701uint32_t ARMMCCodeEmitter::
702getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
703 SmallVectorImpl<MCFixup> &Fixups) const {
704 // [SP, #imm]
705 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000706 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000707#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
708 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
709 "Unexpected base register!");
710#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000711 // The immediate is already shifted for the implicit zeroes, so no change
712 // here.
713 return MO1.getImm() & 0xff;
714}
715
Bill Wendling1fd374e2010-11-30 22:57:21 +0000716/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
717static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
718 unsigned Scale) {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000719 // [Rn, Rm]
720 // {5-3} = Rm
721 // {2-0} = Rn
722 //
723 // [Rn, #imm]
724 // {7-3} = imm5
725 // {2-0} = Rn
726 const MCOperand &MO = MI.getOperand(OpIdx);
727 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
728 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
729 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling1fd374e2010-11-30 22:57:21 +0000730 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
Bill Wendling0bdf0c02010-12-03 00:53:22 +0000731
732 if (MO2.getReg() != 0)
733 // Is an immediate.
734 Imm5 = getARMRegisterNumbering(MO2.getReg());
735
736 return (Imm5 << 3) | Rn;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000737}
738
Bill Wendling1fd374e2010-11-30 22:57:21 +0000739/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
740uint32_t ARMMCCodeEmitter::
741getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
742 SmallVectorImpl<MCFixup> &) const {
743 return getAddrModeSOpValue(MI, OpIdx, 4);
744}
745
746/// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
747uint32_t ARMMCCodeEmitter::
748getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
749 SmallVectorImpl<MCFixup> &) const {
750 return getAddrModeSOpValue(MI, OpIdx, 2);
751}
752
753/// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
754uint32_t ARMMCCodeEmitter::
755getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
756 SmallVectorImpl<MCFixup> &) const {
757 return getAddrModeSOpValue(MI, OpIdx, 1);
758}
759
Bill Wendlingb8958b02010-12-08 01:57:09 +0000760/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
761uint32_t ARMMCCodeEmitter::
762getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
763 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000764 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000765}
766
Jim Grosbach5177f792010-12-01 21:09:40 +0000767/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000768uint32_t ARMMCCodeEmitter::
769getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
770 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000771 // {12-9} = reg
772 // {8} = (U)nsigned (add == '1', sub == '0')
773 // {7-0} = imm8
774 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000775 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000776 // If The first operand isn't a register, we have a label reference.
777 const MCOperand &MO = MI.getOperand(OpIdx);
778 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000779 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000780 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000781 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000782
783 assert(MO.isExpr() && "Unexpected machine operand type!");
784 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000785 MCFixupKind Kind;
786 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
787 if (Subtarget.isThumb2())
788 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
789 else
790 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000791 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
792
793 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000794 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000795 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000796 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
797 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000798
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000799 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
800 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000801 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000802 Binary |= (1 << 8);
803 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000804 return Binary;
805}
806
Jim Grosbach806e80e2010-11-03 23:52:49 +0000807unsigned ARMMCCodeEmitter::
808getSORegOpValue(const MCInst &MI, unsigned OpIdx,
809 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000810 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
811 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
812 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000813 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000814 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000815 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000816 // {6-5} = type
817 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000818 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000819 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000820 // else (imm shift)
821 // {11-7} = imm
822
823 const MCOperand &MO = MI.getOperand(OpIdx);
824 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
825 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
826 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
827
828 // Encode Rm.
829 unsigned Binary = getARMRegisterNumbering(MO.getReg());
830
831 // Encode the shift opcode.
832 unsigned SBits = 0;
833 unsigned Rs = MO1.getReg();
834 if (Rs) {
835 // Set shift operand (bit[7:4]).
836 // LSL - 0001
837 // LSR - 0011
838 // ASR - 0101
839 // ROR - 0111
840 // RRX - 0110 and bit[11:8] clear.
841 switch (SOpc) {
842 default: llvm_unreachable("Unknown shift opc!");
843 case ARM_AM::lsl: SBits = 0x1; break;
844 case ARM_AM::lsr: SBits = 0x3; break;
845 case ARM_AM::asr: SBits = 0x5; break;
846 case ARM_AM::ror: SBits = 0x7; break;
847 case ARM_AM::rrx: SBits = 0x6; break;
848 }
849 } else {
850 // Set shift operand (bit[6:4]).
851 // LSL - 000
852 // LSR - 010
853 // ASR - 100
854 // ROR - 110
855 switch (SOpc) {
856 default: llvm_unreachable("Unknown shift opc!");
857 case ARM_AM::lsl: SBits = 0x0; break;
858 case ARM_AM::lsr: SBits = 0x2; break;
859 case ARM_AM::asr: SBits = 0x4; break;
860 case ARM_AM::ror: SBits = 0x6; break;
861 }
862 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000863
Jim Grosbachef324d72010-10-12 23:53:58 +0000864 Binary |= SBits << 4;
865 if (SOpc == ARM_AM::rrx)
866 return Binary;
867
868 // Encode the shift operation Rs or shift_imm (except rrx).
869 if (Rs) {
870 // Encode Rs bit[11:8].
871 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
872 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
873 }
874
875 // Encode shift_imm bit[11:7].
876 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
877}
878
Jim Grosbach806e80e2010-11-03 23:52:49 +0000879unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000880getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
881 SmallVectorImpl<MCFixup> &Fixups) const {
882 const MCOperand &MO1 = MI.getOperand(OpNum);
883 const MCOperand &MO2 = MI.getOperand(OpNum+1);
884 const MCOperand &MO3 = MI.getOperand(OpNum+2);
885
886 // Encoded as [Rn, Rm, imm].
887 // FIXME: Needs fixup support.
888 unsigned Value = getARMRegisterNumbering(MO1.getReg());
889 Value <<= 4;
890 Value |= getARMRegisterNumbering(MO2.getReg());
891 Value <<= 2;
892 Value |= MO3.getImm();
893
894 return Value;
895}
896
897unsigned ARMMCCodeEmitter::
898getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
899 SmallVectorImpl<MCFixup> &Fixups) const {
900 const MCOperand &MO1 = MI.getOperand(OpNum);
901 const MCOperand &MO2 = MI.getOperand(OpNum+1);
902
903 // FIXME: Needs fixup support.
904 unsigned Value = getARMRegisterNumbering(MO1.getReg());
905
906 // Even though the immediate is 8 bits long, we need 9 bits in order
907 // to represent the (inverse of the) sign bit.
908 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000909 int32_t tmp = (int32_t)MO2.getImm();
910 if (tmp < 0)
911 tmp = abs(tmp);
912 else
913 Value |= 256; // Set the ADD bit
914 Value |= tmp & 255;
915 return Value;
916}
917
918unsigned ARMMCCodeEmitter::
919getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
920 SmallVectorImpl<MCFixup> &Fixups) const {
921 const MCOperand &MO1 = MI.getOperand(OpNum);
922
923 // FIXME: Needs fixup support.
924 unsigned Value = 0;
925 int32_t tmp = (int32_t)MO1.getImm();
926 if (tmp < 0)
927 tmp = abs(tmp);
928 else
929 Value |= 256; // Set the ADD bit
930 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000931 return Value;
932}
933
934unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000935getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
936 SmallVectorImpl<MCFixup> &Fixups) const {
937 const MCOperand &MO1 = MI.getOperand(OpNum);
938
939 // FIXME: Needs fixup support.
940 unsigned Value = 0;
941 int32_t tmp = (int32_t)MO1.getImm();
942 if (tmp < 0)
943 tmp = abs(tmp);
944 else
945 Value |= 4096; // Set the ADD bit
946 Value |= tmp & 4095;
947 return Value;
948}
949
950unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000951getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
952 SmallVectorImpl<MCFixup> &Fixups) const {
953 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
954 // shifted. The second is the amount to shift by.
955 //
956 // {3-0} = Rm.
957 // {4} = 0
958 // {6-5} = type
959 // {11-7} = imm
960
961 const MCOperand &MO = MI.getOperand(OpIdx);
962 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
963 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
964
965 // Encode Rm.
966 unsigned Binary = getARMRegisterNumbering(MO.getReg());
967
968 // Encode the shift opcode.
969 unsigned SBits = 0;
970 // Set shift operand (bit[6:4]).
971 // LSL - 000
972 // LSR - 010
973 // ASR - 100
974 // ROR - 110
975 switch (SOpc) {
976 default: llvm_unreachable("Unknown shift opc!");
977 case ARM_AM::lsl: SBits = 0x0; break;
978 case ARM_AM::lsr: SBits = 0x2; break;
979 case ARM_AM::asr: SBits = 0x4; break;
980 case ARM_AM::ror: SBits = 0x6; break;
981 }
982
983 Binary |= SBits << 4;
984 if (SOpc == ARM_AM::rrx)
985 return Binary;
986
987 // Encode shift_imm bit[11:7].
988 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
989}
990
991unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000992getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
993 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000994 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
995 // msb of the mask.
996 const MCOperand &MO = MI.getOperand(Op);
997 uint32_t v = ~MO.getImm();
998 uint32_t lsb = CountTrailingZeros_32(v);
999 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1000 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1001 return lsb | (msb << 5);
1002}
1003
Jim Grosbach806e80e2010-11-03 23:52:49 +00001004unsigned ARMMCCodeEmitter::
1005getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001006 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001007 // VLDM/VSTM:
1008 // {12-8} = Vd
1009 // {7-0} = Number of registers
1010 //
1011 // LDM/STM:
1012 // {15-0} = Bitfield of GPRs.
1013 unsigned Reg = MI.getOperand(Op).getReg();
1014 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1015 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1016
Bill Wendling5e559a22010-11-09 00:30:18 +00001017 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001018
1019 if (SPRRegs || DPRRegs) {
1020 // VLDM/VSTM
1021 unsigned RegNo = getARMRegisterNumbering(Reg);
1022 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1023 Binary |= (RegNo & 0x1f) << 8;
1024 if (SPRRegs)
1025 Binary |= NumRegs;
1026 else
1027 Binary |= NumRegs * 2;
1028 } else {
1029 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1030 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1031 Binary |= 1 << RegNo;
1032 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001033 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001034
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001035 return Binary;
1036}
1037
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001038/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1039/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001040unsigned ARMMCCodeEmitter::
1041getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1042 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001043 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001044 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001045
Owen Andersond9aa7d32010-11-02 00:05:05 +00001046 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001047 unsigned Align = 0;
1048
1049 switch (Imm.getImm()) {
1050 default: break;
1051 case 2:
1052 case 4:
1053 case 8: Align = 0x01; break;
1054 case 16: Align = 0x02; break;
1055 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001056 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001057
Owen Andersond9aa7d32010-11-02 00:05:05 +00001058 return RegNo | (Align << 4);
1059}
1060
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001061/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1062/// alignment operand for use in VLD-dup instructions. This is the same as
1063/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1064/// different for VLD4-dup.
1065unsigned ARMMCCodeEmitter::
1066getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1067 SmallVectorImpl<MCFixup> &Fixups) const {
1068 const MCOperand &Reg = MI.getOperand(Op);
1069 const MCOperand &Imm = MI.getOperand(Op + 1);
1070
1071 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1072 unsigned Align = 0;
1073
1074 switch (Imm.getImm()) {
1075 default: break;
1076 case 2:
1077 case 4:
1078 case 8: Align = 0x01; break;
1079 case 16: Align = 0x03; break;
1080 }
1081
1082 return RegNo | (Align << 4);
1083}
1084
Jim Grosbach806e80e2010-11-03 23:52:49 +00001085unsigned ARMMCCodeEmitter::
1086getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1087 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001088 const MCOperand &MO = MI.getOperand(Op);
1089 if (MO.getReg() == 0) return 0x0D;
1090 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001091}
1092
Jim Grosbach568eeed2010-09-17 18:46:17 +00001093void ARMMCCodeEmitter::
1094EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001095 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001096 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001097 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001098 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001099 uint64_t TSFlags = Desc.TSFlags;
1100 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001101 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001102 int Size;
1103 // Basic size info comes from the TSFlags field.
1104 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1105 default: llvm_unreachable("Unexpected instruction size!");
1106 case ARMII::Size2Bytes: Size = 2; break;
1107 case ARMII::Size4Bytes: Size = 4; break;
1108 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001109 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1110 // Thumb 32-bit wide instructions need to be have the high order halfword
1111 // emitted first.
1112 if (Subtarget.isThumb() && Size == 4) {
1113 EmitConstant(Binary >> 16, 2, OS);
1114 EmitConstant(Binary & 0xffff, 2, OS);
1115 } else
1116 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001117 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001118}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001119
Jim Grosbach806e80e2010-11-03 23:52:49 +00001120#include "ARMGenMCCodeEmitter.inc"