blob: 8cf31e70cb4c7fdd8054d118049d48b5e21dd5e0 [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 Grosbachc466b932010-11-11 18:04:49 +000048 // name offset bits flags
49 { "fixup_arm_pcrel_12", 2, 12, MCFixupKindInfo::FKF_IsPCRel },
50 { "fixup_arm_vfp_pcrel_12", 3, 8, MCFixupKindInfo::FKF_IsPCRel },
51 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach70933262010-11-04 01:12:30 +000052 };
53
54 if (Kind < FirstTargetFixupKind)
55 return MCCodeEmitter::getFixupKindInfo(Kind);
56
57 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
58 "Invalid kind!");
59 return Infos[Kind - FirstTargetFixupKind];
60 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000061 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
62
Jim Grosbach9af82ba2010-10-07 21:57:55 +000063 // getBinaryCodeForInstr - TableGen'erated function for getting the
64 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000065 unsigned getBinaryCodeForInstr(const MCInst &MI,
66 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000067
68 /// getMachineOpValue - Return binary encoding of operand. If the machine
69 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000070 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
71 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000072
Jason W Kim837caa92010-11-18 23:37:15 +000073 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
74 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
75 SmallVectorImpl<MCFixup> &Fixups) const;
76
Bill Wendling92b5a2e2010-11-03 01:49:29 +000077 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000078 unsigned &Reg, unsigned &Imm,
79 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000080
Jim Grosbachc466b932010-11-11 18:04:49 +000081 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
82 /// branch target.
83 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
84 SmallVectorImpl<MCFixup> &Fixups) const;
85
Bill Wendling92b5a2e2010-11-03 01:49:29 +000086 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
87 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +000088 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
89 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000090
Jim Grosbach54fea632010-11-09 17:20:53 +000091 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
92 /// operand as needed by load/store instructions.
93 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
94 SmallVectorImpl<MCFixup> &Fixups) const;
95
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +000096 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
97 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
98 SmallVectorImpl<MCFixup> &Fixups) const {
99 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
100 switch (Mode) {
101 default: assert(0 && "Unknown addressing sub-mode!");
102 case ARM_AM::da: return 0;
103 case ARM_AM::ia: return 1;
104 case ARM_AM::db: return 2;
105 case ARM_AM::ib: return 3;
106 }
107 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000108 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
109 ///
110 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
111 switch (ShOpc) {
112 default: llvm_unreachable("Unknown shift opc!");
113 case ARM_AM::no_shift:
114 case ARM_AM::lsl: return 0;
115 case ARM_AM::lsr: return 1;
116 case ARM_AM::asr: return 2;
117 case ARM_AM::ror:
118 case ARM_AM::rrx: return 3;
119 }
120 return 0;
121 }
122
123 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
124 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
125 SmallVectorImpl<MCFixup> &Fixups) const;
126
127 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
128 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
130
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000131 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
132 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
133 SmallVectorImpl<MCFixup> &Fixups) const;
134
Jim Grosbach570a9222010-11-11 01:09:40 +0000135 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
136 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
137 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000138
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000139 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000140 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
141 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000142
Jim Grosbach08bd5492010-10-12 23:00:24 +0000143 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000144 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
145 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000146 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
147 // '1' respectively.
148 return MI.getOperand(Op).getReg() == ARM::CPSR;
149 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000150
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000151 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000152 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
153 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000154 unsigned SoImm = MI.getOperand(Op).getImm();
155 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
156 assert(SoImmVal != -1 && "Not a valid so_imm value!");
157
158 // Encode rotate_imm.
159 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
160 << ARMII::SoRotImmShift;
161
162 // Encode immed_8.
163 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
164 return Binary;
165 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000166
167 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
168 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
169 SmallVectorImpl<MCFixup> &Fixups) const {
170 unsigned SoImm = MI.getOperand(Op).getImm();
171 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
172 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
173 return Encoded;
174 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000175
Jim Grosbachef324d72010-10-12 23:53:58 +0000176 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000177 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
178 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000179 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
180 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000181
Jim Grosbach806e80e2010-11-03 23:52:49 +0000182 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
183 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000184 switch (MI.getOperand(Op).getImm()) {
185 default: assert (0 && "Not a valid rot_imm value!");
186 case 0: return 0;
187 case 8: return 1;
188 case 16: return 2;
189 case 24: return 3;
190 }
191 }
192
Jim Grosbach806e80e2010-11-03 23:52:49 +0000193 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
194 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000195 return MI.getOperand(Op).getImm() - 1;
196 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000197
Jim Grosbach806e80e2010-11-03 23:52:49 +0000198 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
199 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000200 return 64 - MI.getOperand(Op).getImm();
201 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000202
Jim Grosbach806e80e2010-11-03 23:52:49 +0000203 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
204 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000205
Jim Grosbach806e80e2010-11-03 23:52:49 +0000206 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
207 SmallVectorImpl<MCFixup> &Fixups) const;
208 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
209 SmallVectorImpl<MCFixup> &Fixups) const;
210 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
211 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000212
Owen Andersonc7139a62010-11-11 19:07:48 +0000213 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
214 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000215 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
216 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000217 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
218 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000219
Jim Grosbach70933262010-11-04 01:12:30 +0000220 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000221 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000222 }
223
Jim Grosbach70933262010-11-04 01:12:30 +0000224 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000225 // Output the constant in little endian byte order.
226 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000227 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000228 Val >>= 8;
229 }
230 }
231
Jim Grosbach568eeed2010-09-17 18:46:17 +0000232 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
233 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000234};
235
236} // end anonymous namespace
237
Bill Wendling0800ce72010-11-02 22:53:11 +0000238MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
239 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000240 return new ARMMCCodeEmitter(TM, Ctx);
241}
242
Owen Anderson57dac882010-11-11 21:36:43 +0000243/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000244/// instructions, and rewrite them to their Thumb2 form if we are currently in
245/// Thumb2 mode.
246unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
247 unsigned EncodedValue) const {
248 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
249 if (Subtarget.isThumb2()) {
250 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
251 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
252 // set to 1111.
253 unsigned Bit24 = EncodedValue & 0x01000000;
254 unsigned Bit28 = Bit24 << 4;
255 EncodedValue &= 0xEFFFFFFF;
256 EncodedValue |= Bit28;
257 EncodedValue |= 0x0F000000;
258 }
259
260 return EncodedValue;
261}
262
Owen Anderson57dac882010-11-11 21:36:43 +0000263/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
264/// instructions, and rewrite them to their Thumb2 form if we are currently in
265/// Thumb2 mode.
266unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
267 unsigned EncodedValue) const {
268 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
269 if (Subtarget.isThumb2()) {
270 EncodedValue &= 0xF0FFFFFF;
271 EncodedValue |= 0x09000000;
272 }
273
274 return EncodedValue;
275}
276
Owen Anderson8f143912010-11-11 23:12:55 +0000277/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
278/// instructions, and rewrite them to their Thumb2 form if we are currently in
279/// Thumb2 mode.
280unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
281 unsigned EncodedValue) const {
282 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
283 if (Subtarget.isThumb2()) {
284 EncodedValue &= 0x00FFFFFF;
285 EncodedValue |= 0xEE000000;
286 }
287
288 return EncodedValue;
289}
290
291
Owen Anderson57dac882010-11-11 21:36:43 +0000292
Jim Grosbach56ac9072010-10-08 21:45:55 +0000293/// getMachineOpValue - Return binary encoding of operand. If the machine
294/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000295unsigned ARMMCCodeEmitter::
296getMachineOpValue(const MCInst &MI, const MCOperand &MO,
297 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000298 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000299 unsigned Reg = MO.getReg();
300 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000301
Owen Anderson90d4cf92010-10-21 20:49:13 +0000302 // Q registers are encodes as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000303 switch (Reg) {
304 default:
305 return RegNo;
306 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
307 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
308 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
309 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
310 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000311 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000312 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000313 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000314 } else if (MO.isFPImm()) {
315 return static_cast<unsigned>(APFloat(MO.getFPImm())
316 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000317 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000318
319#ifndef NDEBUG
320 errs() << MO;
321#endif
322 llvm_unreachable(0);
Jim Grosbach56ac9072010-10-08 21:45:55 +0000323 return 0;
324}
325
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000326/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000327bool ARMMCCodeEmitter::
328EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
329 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000330 const MCOperand &MO = MI.getOperand(OpIdx);
331 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000332
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000333 Reg = getARMRegisterNumbering(MO.getReg());
334
335 int32_t SImm = MO1.getImm();
336 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000337
Jim Grosbachab682a22010-10-28 18:34:10 +0000338 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000339 if (SImm == INT32_MIN)
340 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000341
Jim Grosbachab682a22010-10-28 18:34:10 +0000342 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000343 if (SImm < 0) {
344 SImm = -SImm;
345 isAdd = false;
346 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000347
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000348 Imm = SImm;
349 return isAdd;
350}
351
Jim Grosbachc466b932010-11-11 18:04:49 +0000352/// getBranchTargetOpValue - Return encoding info for 24-bit immediate
353/// branch target.
354uint32_t ARMMCCodeEmitter::
355getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
356 SmallVectorImpl<MCFixup> &Fixups) const {
357 const MCOperand &MO = MI.getOperand(OpIdx);
358
359 // If the destination is an immediate, we have nothing to do.
360 if (MO.isImm()) return MO.getImm();
361 assert (MO.isExpr() && "Unexpected branch target type!");
362 const MCExpr *Expr = MO.getExpr();
363 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
364 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
365
366 // All of the information is in the fixup.
367 return 0;
368}
369
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000370/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000371uint32_t ARMMCCodeEmitter::
372getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
373 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000374 // {17-13} = reg
375 // {12} = (U)nsigned (add == '1', sub == '0')
376 // {11-0} = imm12
377 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000378 bool isAdd = true;
379 // If The first operand isn't a register, we have a label reference.
380 const MCOperand &MO = MI.getOperand(OpIdx);
381 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000382 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000383 Imm12 = 0;
384
385 assert(MO.isExpr() && "Unexpected machine operand type!");
386 const MCExpr *Expr = MO.getExpr();
387 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
388 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
389
390 ++MCNumCPRelocations;
391 } else
392 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000393
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000394 uint32_t Binary = Imm12 & 0xfff;
395 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000396 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000397 Binary |= (1 << 12);
398 Binary |= (Reg << 13);
399 return Binary;
400}
401
Jim Grosbach54fea632010-11-09 17:20:53 +0000402uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000403getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
404 SmallVectorImpl<MCFixup> &Fixups) const {
405 // {20-16} = imm{15-12}
406 // {11-0} = imm{11-0}
407 const MCOperand &MO = MI.getOperand(OpIdx);
408 if (MO.isImm()) {
409 return static_cast<unsigned>(MO.getImm());
410 } else if (const MCSymbolRefExpr *Expr =
411 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
412 MCFixupKind Kind;
413 switch (Expr->getKind()) {
414 case MCSymbolRefExpr::VK_ARM_HI16:
415 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
416 break;
417 case MCSymbolRefExpr::VK_ARM_LO16:
418 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
419 break;
420 default: assert(0 && "Unsupported ARMFixup"); break;
421 }
422 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
423 return 0;
Bill Wendlingd1fadd82010-11-19 00:05:15 +0000424 }
425 llvm_unreachable("Unsupported MCExpr type in MCOperand");
Jason W Kim837caa92010-11-18 23:37:15 +0000426 return 0;
427}
428
429uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000430getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
431 SmallVectorImpl<MCFixup> &Fixups) const {
432 const MCOperand &MO = MI.getOperand(OpIdx);
433 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
434 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
435 unsigned Rn = getARMRegisterNumbering(MO.getReg());
436 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000437 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
438 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000439 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
440 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000441
442 // {16-13} = Rn
443 // {12} = isAdd
444 // {11-0} = shifter
445 // {3-0} = Rm
446 // {4} = 0
447 // {6-5} = type
448 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000449 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000450 Binary |= Rn << 13;
451 Binary |= SBits << 5;
452 Binary |= ShImm << 7;
453 if (isAdd)
454 Binary |= 1 << 12;
455 return Binary;
456}
457
Jim Grosbach570a9222010-11-11 01:09:40 +0000458uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000459getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
460 SmallVectorImpl<MCFixup> &Fixups) const {
461 // {17-14} Rn
462 // {13} 1 == imm12, 0 == Rm
463 // {12} isAdd
464 // {11-0} imm12/Rm
465 const MCOperand &MO = MI.getOperand(OpIdx);
466 unsigned Rn = getARMRegisterNumbering(MO.getReg());
467 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
468 Binary |= Rn << 14;
469 return Binary;
470}
471
472uint32_t ARMMCCodeEmitter::
473getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
474 SmallVectorImpl<MCFixup> &Fixups) const {
475 // {13} 1 == imm12, 0 == Rm
476 // {12} isAdd
477 // {11-0} imm12/Rm
478 const MCOperand &MO = MI.getOperand(OpIdx);
479 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
480 unsigned Imm = MO1.getImm();
481 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
482 bool isReg = MO.getReg() != 0;
483 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
484 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
485 if (isReg) {
486 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
487 Binary <<= 7; // Shift amount is bits [11:7]
488 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
489 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
490 }
491 return Binary | (isAdd << 12) | (isReg << 13);
492}
493
494uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000495getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
496 SmallVectorImpl<MCFixup> &Fixups) const {
497 // {9} 1 == imm8, 0 == Rm
498 // {8} isAdd
499 // {7-4} imm7_4/zero
500 // {3-0} imm3_0/Rm
501 const MCOperand &MO = MI.getOperand(OpIdx);
502 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
503 unsigned Imm = MO1.getImm();
504 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
505 bool isImm = MO.getReg() == 0;
506 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
507 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
508 if (!isImm)
509 Imm8 = getARMRegisterNumbering(MO.getReg());
510 return Imm8 | (isAdd << 8) | (isImm << 9);
511}
512
513uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000514getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
515 SmallVectorImpl<MCFixup> &Fixups) const {
516 // {13} 1 == imm8, 0 == Rm
517 // {12-9} Rn
518 // {8} isAdd
519 // {7-4} imm7_4/zero
520 // {3-0} imm3_0/Rm
521 const MCOperand &MO = MI.getOperand(OpIdx);
522 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
523 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
524 unsigned Rn = getARMRegisterNumbering(MO.getReg());
525 unsigned Imm = MO2.getImm();
526 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
527 bool isImm = MO1.getReg() == 0;
528 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
529 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
530 if (!isImm)
531 Imm8 = getARMRegisterNumbering(MO1.getReg());
532 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
533}
534
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000535/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000536uint32_t ARMMCCodeEmitter::
537getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
538 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000539 // {12-9} = reg
540 // {8} = (U)nsigned (add == '1', sub == '0')
541 // {7-0} = imm8
542 unsigned Reg, Imm8;
Jim Grosbach70933262010-11-04 01:12:30 +0000543 // If The first operand isn't a register, we have a label reference.
544 const MCOperand &MO = MI.getOperand(OpIdx);
545 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000546 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000547 Imm8 = 0;
548
549 assert(MO.isExpr() && "Unexpected machine operand type!");
550 const MCExpr *Expr = MO.getExpr();
551 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_vfp_pcrel_12);
552 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
553
554 ++MCNumCPRelocations;
555 } else
556 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000557
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000558 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
559 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
560 if (ARM_AM::getAM5Op(Imm8) == ARM_AM::add)
561 Binary |= (1 << 8);
562 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000563 return Binary;
564}
565
Jim Grosbach806e80e2010-11-03 23:52:49 +0000566unsigned ARMMCCodeEmitter::
567getSORegOpValue(const MCInst &MI, unsigned OpIdx,
568 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000569 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
570 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
571 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000572 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000573 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000574 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000575 // {6-5} = type
576 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000577 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000578 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000579 // else (imm shift)
580 // {11-7} = imm
581
582 const MCOperand &MO = MI.getOperand(OpIdx);
583 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
584 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
585 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
586
587 // Encode Rm.
588 unsigned Binary = getARMRegisterNumbering(MO.getReg());
589
590 // Encode the shift opcode.
591 unsigned SBits = 0;
592 unsigned Rs = MO1.getReg();
593 if (Rs) {
594 // Set shift operand (bit[7:4]).
595 // LSL - 0001
596 // LSR - 0011
597 // ASR - 0101
598 // ROR - 0111
599 // RRX - 0110 and bit[11:8] clear.
600 switch (SOpc) {
601 default: llvm_unreachable("Unknown shift opc!");
602 case ARM_AM::lsl: SBits = 0x1; break;
603 case ARM_AM::lsr: SBits = 0x3; break;
604 case ARM_AM::asr: SBits = 0x5; break;
605 case ARM_AM::ror: SBits = 0x7; break;
606 case ARM_AM::rrx: SBits = 0x6; break;
607 }
608 } else {
609 // Set shift operand (bit[6:4]).
610 // LSL - 000
611 // LSR - 010
612 // ASR - 100
613 // ROR - 110
614 switch (SOpc) {
615 default: llvm_unreachable("Unknown shift opc!");
616 case ARM_AM::lsl: SBits = 0x0; break;
617 case ARM_AM::lsr: SBits = 0x2; break;
618 case ARM_AM::asr: SBits = 0x4; break;
619 case ARM_AM::ror: SBits = 0x6; break;
620 }
621 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000622
Jim Grosbachef324d72010-10-12 23:53:58 +0000623 Binary |= SBits << 4;
624 if (SOpc == ARM_AM::rrx)
625 return Binary;
626
627 // Encode the shift operation Rs or shift_imm (except rrx).
628 if (Rs) {
629 // Encode Rs bit[11:8].
630 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
631 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
632 }
633
634 // Encode shift_imm bit[11:7].
635 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
636}
637
Jim Grosbach806e80e2010-11-03 23:52:49 +0000638unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000639getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
640 SmallVectorImpl<MCFixup> &Fixups) const {
641 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
642 // shifted. The second is the amount to shift by.
643 //
644 // {3-0} = Rm.
645 // {4} = 0
646 // {6-5} = type
647 // {11-7} = imm
648
649 const MCOperand &MO = MI.getOperand(OpIdx);
650 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
651 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
652
653 // Encode Rm.
654 unsigned Binary = getARMRegisterNumbering(MO.getReg());
655
656 // Encode the shift opcode.
657 unsigned SBits = 0;
658 // Set shift operand (bit[6:4]).
659 // LSL - 000
660 // LSR - 010
661 // ASR - 100
662 // ROR - 110
663 switch (SOpc) {
664 default: llvm_unreachable("Unknown shift opc!");
665 case ARM_AM::lsl: SBits = 0x0; break;
666 case ARM_AM::lsr: SBits = 0x2; break;
667 case ARM_AM::asr: SBits = 0x4; break;
668 case ARM_AM::ror: SBits = 0x6; break;
669 }
670
671 Binary |= SBits << 4;
672 if (SOpc == ARM_AM::rrx)
673 return Binary;
674
675 // Encode shift_imm bit[11:7].
676 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
677}
678
679unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000680getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
681 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000682 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
683 // msb of the mask.
684 const MCOperand &MO = MI.getOperand(Op);
685 uint32_t v = ~MO.getImm();
686 uint32_t lsb = CountTrailingZeros_32(v);
687 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
688 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
689 return lsb | (msb << 5);
690}
691
Jim Grosbach806e80e2010-11-03 23:52:49 +0000692unsigned ARMMCCodeEmitter::
693getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000694 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +0000695 // VLDM/VSTM:
696 // {12-8} = Vd
697 // {7-0} = Number of registers
698 //
699 // LDM/STM:
700 // {15-0} = Bitfield of GPRs.
701 unsigned Reg = MI.getOperand(Op).getReg();
702 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
703 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
704
Bill Wendling5e559a22010-11-09 00:30:18 +0000705 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +0000706
707 if (SPRRegs || DPRRegs) {
708 // VLDM/VSTM
709 unsigned RegNo = getARMRegisterNumbering(Reg);
710 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
711 Binary |= (RegNo & 0x1f) << 8;
712 if (SPRRegs)
713 Binary |= NumRegs;
714 else
715 Binary |= NumRegs * 2;
716 } else {
717 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
718 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
719 Binary |= 1 << RegNo;
720 }
Bill Wendling5e559a22010-11-09 00:30:18 +0000721 }
Bill Wendling6bc105a2010-11-17 00:45:23 +0000722
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000723 return Binary;
724}
725
Jim Grosbach806e80e2010-11-03 23:52:49 +0000726unsigned ARMMCCodeEmitter::
727getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
728 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000729 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000730 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +0000731
Owen Andersond9aa7d32010-11-02 00:05:05 +0000732 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000733 unsigned Align = 0;
734
735 switch (Imm.getImm()) {
736 default: break;
737 case 2:
738 case 4:
739 case 8: Align = 0x01; break;
740 case 16: Align = 0x02; break;
741 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000742 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000743
Owen Andersond9aa7d32010-11-02 00:05:05 +0000744 return RegNo | (Align << 4);
745}
746
Jim Grosbach806e80e2010-11-03 23:52:49 +0000747unsigned ARMMCCodeEmitter::
748getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
749 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000750 const MCOperand &MO = MI.getOperand(Op);
751 if (MO.getReg() == 0) return 0x0D;
752 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +0000753}
754
Jim Grosbach568eeed2010-09-17 18:46:17 +0000755void ARMMCCodeEmitter::
756EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000757 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000758 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +0000759 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000760 uint64_t TSFlags = Desc.TSFlags;
761 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000762 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000763 int Size;
764 // Basic size info comes from the TSFlags field.
765 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
766 default: llvm_unreachable("Unexpected instruction size!");
767 case ARMII::Size2Bytes: Size = 2; break;
768 case ARMII::Size4Bytes: Size = 4; break;
769 }
770 EmitConstant(getBinaryCodeForInstr(MI, Fixups), Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +0000771 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +0000772}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000773
Jim Grosbach806e80e2010-11-03 23:52:49 +0000774#include "ARMGenMCCodeEmitter.inc"