blob: 0207491970c55ddb7dbbc4869eae94adb70a5650 [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
14#define DEBUG_TYPE "arm-emitter"
15#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
Bill Wendling92b5a2e2010-11-03 01:49:29 +000073 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000074 unsigned &Reg, unsigned &Imm,
75 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000076
Jim Grosbachc466b932010-11-11 18:04:49 +000077 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
78 /// branch target.
79 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
80 SmallVectorImpl<MCFixup> &Fixups) const;
81
Bill Wendling92b5a2e2010-11-03 01:49:29 +000082 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
83 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +000084 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
85 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000086
Jim Grosbach54fea632010-11-09 17:20:53 +000087 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
88 /// operand as needed by load/store instructions.
89 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
90 SmallVectorImpl<MCFixup> &Fixups) const;
91
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +000092 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
93 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
94 SmallVectorImpl<MCFixup> &Fixups) const {
95 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
96 switch (Mode) {
97 default: assert(0 && "Unknown addressing sub-mode!");
98 case ARM_AM::da: return 0;
99 case ARM_AM::ia: return 1;
100 case ARM_AM::db: return 2;
101 case ARM_AM::ib: return 3;
102 }
103 }
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000104 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
105 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
106 SmallVectorImpl<MCFixup> &Fixups) const;
107
Jim Grosbach570a9222010-11-11 01:09:40 +0000108 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
109 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
110 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000111
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000112 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000113 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
114 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000115
Jim Grosbach08bd5492010-10-12 23:00:24 +0000116 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000117 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
118 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000119 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
120 // '1' respectively.
121 return MI.getOperand(Op).getReg() == ARM::CPSR;
122 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000123
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000124 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000125 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
126 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000127 unsigned SoImm = MI.getOperand(Op).getImm();
128 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
129 assert(SoImmVal != -1 && "Not a valid so_imm value!");
130
131 // Encode rotate_imm.
132 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
133 << ARMII::SoRotImmShift;
134
135 // Encode immed_8.
136 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
137 return Binary;
138 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000139
Jim Grosbachef324d72010-10-12 23:53:58 +0000140 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000141 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
142 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000143
Jim Grosbach806e80e2010-11-03 23:52:49 +0000144 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
145 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000146 switch (MI.getOperand(Op).getImm()) {
147 default: assert (0 && "Not a valid rot_imm value!");
148 case 0: return 0;
149 case 8: return 1;
150 case 16: return 2;
151 case 24: return 3;
152 }
153 }
154
Jim Grosbach806e80e2010-11-03 23:52:49 +0000155 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
156 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000157 return MI.getOperand(Op).getImm() - 1;
158 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000159
Jim Grosbach806e80e2010-11-03 23:52:49 +0000160 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
161 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000162 return 64 - MI.getOperand(Op).getImm();
163 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000164
Jim Grosbach806e80e2010-11-03 23:52:49 +0000165 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
166 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000167
Jim Grosbach806e80e2010-11-03 23:52:49 +0000168 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
169 SmallVectorImpl<MCFixup> &Fixups) const;
170 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
171 SmallVectorImpl<MCFixup> &Fixups) const;
172 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
173 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000174
Jim Grosbach70933262010-11-04 01:12:30 +0000175 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000176 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000177 }
178
Jim Grosbach70933262010-11-04 01:12:30 +0000179 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000180 // Output the constant in little endian byte order.
181 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000182 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000183 Val >>= 8;
184 }
185 }
186
Jim Grosbach568eeed2010-09-17 18:46:17 +0000187 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
188 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000189};
190
191} // end anonymous namespace
192
Bill Wendling0800ce72010-11-02 22:53:11 +0000193MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
194 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000195 return new ARMMCCodeEmitter(TM, Ctx);
196}
197
Jim Grosbach56ac9072010-10-08 21:45:55 +0000198/// getMachineOpValue - Return binary encoding of operand. If the machine
199/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000200unsigned ARMMCCodeEmitter::
201getMachineOpValue(const MCInst &MI, const MCOperand &MO,
202 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000203 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000204 unsigned Reg = MO.getReg();
205 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000206
Owen Anderson90d4cf92010-10-21 20:49:13 +0000207 // Q registers are encodes as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000208 switch (Reg) {
209 default:
210 return RegNo;
211 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
212 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
213 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
214 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
215 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000216 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000217 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000218 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000219 } else if (MO.isFPImm()) {
220 return static_cast<unsigned>(APFloat(MO.getFPImm())
221 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000222 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000223
224#ifndef NDEBUG
225 errs() << MO;
226#endif
227 llvm_unreachable(0);
Jim Grosbach56ac9072010-10-08 21:45:55 +0000228 return 0;
229}
230
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000231/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000232bool ARMMCCodeEmitter::
233EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
234 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000235 const MCOperand &MO = MI.getOperand(OpIdx);
236 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000237
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000238 Reg = getARMRegisterNumbering(MO.getReg());
239
240 int32_t SImm = MO1.getImm();
241 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000242
Jim Grosbachab682a22010-10-28 18:34:10 +0000243 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000244 if (SImm == INT32_MIN)
245 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000246
Jim Grosbachab682a22010-10-28 18:34:10 +0000247 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000248 if (SImm < 0) {
249 SImm = -SImm;
250 isAdd = false;
251 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000252
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000253 Imm = SImm;
254 return isAdd;
255}
256
Jim Grosbachc466b932010-11-11 18:04:49 +0000257/// getBranchTargetOpValue - Return encoding info for 24-bit immediate
258/// branch target.
259uint32_t ARMMCCodeEmitter::
260getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
261 SmallVectorImpl<MCFixup> &Fixups) const {
262 const MCOperand &MO = MI.getOperand(OpIdx);
263
264 // If the destination is an immediate, we have nothing to do.
265 if (MO.isImm()) return MO.getImm();
266 assert (MO.isExpr() && "Unexpected branch target type!");
267 const MCExpr *Expr = MO.getExpr();
268 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
269 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
270
271 // All of the information is in the fixup.
272 return 0;
273}
274
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000275/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000276uint32_t ARMMCCodeEmitter::
277getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
278 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000279 // {17-13} = reg
280 // {12} = (U)nsigned (add == '1', sub == '0')
281 // {11-0} = imm12
282 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000283 bool isAdd = true;
284 // If The first operand isn't a register, we have a label reference.
285 const MCOperand &MO = MI.getOperand(OpIdx);
286 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000287 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000288 Imm12 = 0;
289
290 assert(MO.isExpr() && "Unexpected machine operand type!");
291 const MCExpr *Expr = MO.getExpr();
292 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
293 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
294
295 ++MCNumCPRelocations;
296 } else
297 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000298
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000299 uint32_t Binary = Imm12 & 0xfff;
300 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000301 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000302 Binary |= (1 << 12);
303 Binary |= (Reg << 13);
304 return Binary;
305}
306
Jim Grosbach54fea632010-11-09 17:20:53 +0000307uint32_t ARMMCCodeEmitter::
308getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
309 SmallVectorImpl<MCFixup> &Fixups) const {
310 const MCOperand &MO = MI.getOperand(OpIdx);
311 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
312 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
313 unsigned Rn = getARMRegisterNumbering(MO.getReg());
314 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
315 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
316 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
317 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
318 unsigned SBits;
319 // LSL - 00
320 // LSR - 01
321 // ASR - 10
322 // ROR - 11
323 switch (ShOp) {
324 default: llvm_unreachable("Unknown shift opc!");
Jim Grosbachd92354c2010-11-09 17:38:15 +0000325 case ARM_AM::no_shift:
326 assert(ShImm == 0 && "Non-zero shift amount with no shift type!");
327 // fall through
Jim Grosbach54fea632010-11-09 17:20:53 +0000328 case ARM_AM::lsl: SBits = 0x0; break;
329 case ARM_AM::lsr: SBits = 0x1; break;
330 case ARM_AM::asr: SBits = 0x2; break;
331 case ARM_AM::ror: SBits = 0x3; break;
332 }
333
334 // {16-13} = Rn
335 // {12} = isAdd
336 // {11-0} = shifter
337 // {3-0} = Rm
338 // {4} = 0
339 // {6-5} = type
340 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000341 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000342 Binary |= Rn << 13;
343 Binary |= SBits << 5;
344 Binary |= ShImm << 7;
345 if (isAdd)
346 Binary |= 1 << 12;
347 return Binary;
348}
349
Jim Grosbach570a9222010-11-11 01:09:40 +0000350uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000351getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
352 SmallVectorImpl<MCFixup> &Fixups) const {
353 // {9} 1 == imm8, 0 == Rm
354 // {8} isAdd
355 // {7-4} imm7_4/zero
356 // {3-0} imm3_0/Rm
357 const MCOperand &MO = MI.getOperand(OpIdx);
358 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
359 unsigned Imm = MO1.getImm();
360 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
361 bool isImm = MO.getReg() == 0;
362 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
363 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
364 if (!isImm)
365 Imm8 = getARMRegisterNumbering(MO.getReg());
366 return Imm8 | (isAdd << 8) | (isImm << 9);
367}
368
369uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000370getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
371 SmallVectorImpl<MCFixup> &Fixups) const {
372 // {13} 1 == imm8, 0 == Rm
373 // {12-9} Rn
374 // {8} isAdd
375 // {7-4} imm7_4/zero
376 // {3-0} imm3_0/Rm
377 const MCOperand &MO = MI.getOperand(OpIdx);
378 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
379 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
380 unsigned Rn = getARMRegisterNumbering(MO.getReg());
381 unsigned Imm = MO2.getImm();
382 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
383 bool isImm = MO1.getReg() == 0;
384 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
385 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
386 if (!isImm)
387 Imm8 = getARMRegisterNumbering(MO1.getReg());
388 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
389}
390
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000391/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000392uint32_t ARMMCCodeEmitter::
393getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
394 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000395 // {12-9} = reg
396 // {8} = (U)nsigned (add == '1', sub == '0')
397 // {7-0} = imm8
398 unsigned Reg, Imm8;
Jim Grosbach70933262010-11-04 01:12:30 +0000399 // If The first operand isn't a register, we have a label reference.
400 const MCOperand &MO = MI.getOperand(OpIdx);
401 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000402 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000403 Imm8 = 0;
404
405 assert(MO.isExpr() && "Unexpected machine operand type!");
406 const MCExpr *Expr = MO.getExpr();
407 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_vfp_pcrel_12);
408 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
409
410 ++MCNumCPRelocations;
411 } else
412 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000413
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000414 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
415 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
416 if (ARM_AM::getAM5Op(Imm8) == ARM_AM::add)
417 Binary |= (1 << 8);
418 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000419 return Binary;
420}
421
Jim Grosbach806e80e2010-11-03 23:52:49 +0000422unsigned ARMMCCodeEmitter::
423getSORegOpValue(const MCInst &MI, unsigned OpIdx,
424 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000425 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
426 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
427 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000428 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000429 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000430 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000431 // {6-5} = type
432 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000433 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000434 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000435 // else (imm shift)
436 // {11-7} = imm
437
438 const MCOperand &MO = MI.getOperand(OpIdx);
439 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
440 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
441 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
442
443 // Encode Rm.
444 unsigned Binary = getARMRegisterNumbering(MO.getReg());
445
446 // Encode the shift opcode.
447 unsigned SBits = 0;
448 unsigned Rs = MO1.getReg();
449 if (Rs) {
450 // Set shift operand (bit[7:4]).
451 // LSL - 0001
452 // LSR - 0011
453 // ASR - 0101
454 // ROR - 0111
455 // RRX - 0110 and bit[11:8] clear.
456 switch (SOpc) {
457 default: llvm_unreachable("Unknown shift opc!");
458 case ARM_AM::lsl: SBits = 0x1; break;
459 case ARM_AM::lsr: SBits = 0x3; break;
460 case ARM_AM::asr: SBits = 0x5; break;
461 case ARM_AM::ror: SBits = 0x7; break;
462 case ARM_AM::rrx: SBits = 0x6; break;
463 }
464 } else {
465 // Set shift operand (bit[6:4]).
466 // LSL - 000
467 // LSR - 010
468 // ASR - 100
469 // ROR - 110
470 switch (SOpc) {
471 default: llvm_unreachable("Unknown shift opc!");
472 case ARM_AM::lsl: SBits = 0x0; break;
473 case ARM_AM::lsr: SBits = 0x2; break;
474 case ARM_AM::asr: SBits = 0x4; break;
475 case ARM_AM::ror: SBits = 0x6; break;
476 }
477 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000478
Jim Grosbachef324d72010-10-12 23:53:58 +0000479 Binary |= SBits << 4;
480 if (SOpc == ARM_AM::rrx)
481 return Binary;
482
483 // Encode the shift operation Rs or shift_imm (except rrx).
484 if (Rs) {
485 // Encode Rs bit[11:8].
486 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
487 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
488 }
489
490 // Encode shift_imm bit[11:7].
491 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
492}
493
Jim Grosbach806e80e2010-11-03 23:52:49 +0000494unsigned ARMMCCodeEmitter::
495getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
496 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000497 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
498 // msb of the mask.
499 const MCOperand &MO = MI.getOperand(Op);
500 uint32_t v = ~MO.getImm();
501 uint32_t lsb = CountTrailingZeros_32(v);
502 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
503 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
504 return lsb | (msb << 5);
505}
506
Jim Grosbach806e80e2010-11-03 23:52:49 +0000507unsigned ARMMCCodeEmitter::
508getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000509 SmallVectorImpl<MCFixup> &Fixups) const {
510 // Convert a list of GPRs into a bitfield (R0 -> bit 0). For each
511 // register in the list, set the corresponding bit.
512 unsigned Binary = 0;
513 for (unsigned i = Op, e = MI.getNumOperands(); i < e; ++i) {
514 unsigned regno = getARMRegisterNumbering(MI.getOperand(i).getReg());
515 Binary |= 1 << regno;
516 }
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000517 return Binary;
518}
519
Jim Grosbach806e80e2010-11-03 23:52:49 +0000520unsigned ARMMCCodeEmitter::
521getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
522 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000523 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000524 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +0000525
Owen Andersond9aa7d32010-11-02 00:05:05 +0000526 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000527 unsigned Align = 0;
528
529 switch (Imm.getImm()) {
530 default: break;
531 case 2:
532 case 4:
533 case 8: Align = 0x01; break;
534 case 16: Align = 0x02; break;
535 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000536 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000537
Owen Andersond9aa7d32010-11-02 00:05:05 +0000538 return RegNo | (Align << 4);
539}
540
Jim Grosbach806e80e2010-11-03 23:52:49 +0000541unsigned ARMMCCodeEmitter::
542getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
543 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000544 const MCOperand &MO = MI.getOperand(Op);
545 if (MO.getReg() == 0) return 0x0D;
546 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +0000547}
548
Jim Grosbach568eeed2010-09-17 18:46:17 +0000549void ARMMCCodeEmitter::
550EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000551 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000552 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +0000553 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
554 if ((Desc.TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000555 return;
556
Jim Grosbach70933262010-11-04 01:12:30 +0000557 EmitConstant(getBinaryCodeForInstr(MI, Fixups), 4, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +0000558 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +0000559}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000560
Jim Grosbach806e80e2010-11-03 23:52:49 +0000561#include "ARMGenMCCodeEmitter.inc"