blob: bd799f233c07373141878b7e239353433f1bd9aa [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 Grosbach70933262010-11-04 01:12:30 +000044 unsigned getNumFixupKinds() const { return 2; }
45
46 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
47 const static MCFixupKindInfo Infos[] = {
48 { "fixup_arm_pcrel_12", 2, 12, MCFixupKindInfo::FKF_IsPCRel },
49 { "fixup_arm_vfp_pcrel_12", 3, 8, MCFixupKindInfo::FKF_IsPCRel },
50 };
51
52 if (Kind < FirstTargetFixupKind)
53 return MCCodeEmitter::getFixupKindInfo(Kind);
54
55 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
56 "Invalid kind!");
57 return Infos[Kind - FirstTargetFixupKind];
58 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000059 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
60
Jim Grosbach9af82ba2010-10-07 21:57:55 +000061 // getBinaryCodeForInstr - TableGen'erated function for getting the
62 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000063 unsigned getBinaryCodeForInstr(const MCInst &MI,
64 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000065
66 /// getMachineOpValue - Return binary encoding of operand. If the machine
67 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000068 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
69 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000070
Bill Wendling92b5a2e2010-11-03 01:49:29 +000071 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000072 unsigned &Reg, unsigned &Imm,
73 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000074
75 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
76 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +000077 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
78 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000079
Jim Grosbach54fea632010-11-09 17:20:53 +000080 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
81 /// operand as needed by load/store instructions.
82 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
83 SmallVectorImpl<MCFixup> &Fixups) const;
84
Bill Wendling92b5a2e2010-11-03 01:49:29 +000085 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +000086 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
87 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +000088
Jim Grosbach08bd5492010-10-12 23:00:24 +000089 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +000090 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
91 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +000092 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
93 // '1' respectively.
94 return MI.getOperand(Op).getReg() == ARM::CPSR;
95 }
Jim Grosbachef324d72010-10-12 23:53:58 +000096
Jim Grosbach2a6a93d2010-10-12 23:18:08 +000097 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +000098 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
99 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000100 unsigned SoImm = MI.getOperand(Op).getImm();
101 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
102 assert(SoImmVal != -1 && "Not a valid so_imm value!");
103
104 // Encode rotate_imm.
105 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
106 << ARMII::SoRotImmShift;
107
108 // Encode immed_8.
109 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
110 return Binary;
111 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000112
Jim Grosbachef324d72010-10-12 23:53:58 +0000113 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000114 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
115 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000116
Jim Grosbach806e80e2010-11-03 23:52:49 +0000117 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
118 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000119 switch (MI.getOperand(Op).getImm()) {
120 default: assert (0 && "Not a valid rot_imm value!");
121 case 0: return 0;
122 case 8: return 1;
123 case 16: return 2;
124 case 24: return 3;
125 }
126 }
127
Jim Grosbach806e80e2010-11-03 23:52:49 +0000128 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
129 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000130 return MI.getOperand(Op).getImm() - 1;
131 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000132
Jim Grosbach806e80e2010-11-03 23:52:49 +0000133 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
134 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000135 return 64 - MI.getOperand(Op).getImm();
136 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000137
Jim Grosbach806e80e2010-11-03 23:52:49 +0000138 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
139 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000140
Jim Grosbach806e80e2010-11-03 23:52:49 +0000141 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
142 SmallVectorImpl<MCFixup> &Fixups) const;
143 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
144 SmallVectorImpl<MCFixup> &Fixups) const;
145 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
146 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000147
Jim Grosbach70933262010-11-04 01:12:30 +0000148 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000149 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000150 }
151
Jim Grosbach70933262010-11-04 01:12:30 +0000152 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000153 // Output the constant in little endian byte order.
154 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000155 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000156 Val >>= 8;
157 }
158 }
159
Jim Grosbach568eeed2010-09-17 18:46:17 +0000160 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
161 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000162};
163
164} // end anonymous namespace
165
Bill Wendling0800ce72010-11-02 22:53:11 +0000166MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
167 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000168 return new ARMMCCodeEmitter(TM, Ctx);
169}
170
Jim Grosbach56ac9072010-10-08 21:45:55 +0000171/// getMachineOpValue - Return binary encoding of operand. If the machine
172/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000173unsigned ARMMCCodeEmitter::
174getMachineOpValue(const MCInst &MI, const MCOperand &MO,
175 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000176 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000177 unsigned Reg = MO.getReg();
178 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000179
Owen Anderson90d4cf92010-10-21 20:49:13 +0000180 // Q registers are encodes as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000181 switch (Reg) {
182 default:
183 return RegNo;
184 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
185 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
186 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
187 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
188 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000189 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000190 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000191 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000192 } else if (MO.isFPImm()) {
193 return static_cast<unsigned>(APFloat(MO.getFPImm())
194 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000195 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000196
197#ifndef NDEBUG
198 errs() << MO;
199#endif
200 llvm_unreachable(0);
Jim Grosbach56ac9072010-10-08 21:45:55 +0000201 return 0;
202}
203
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000204/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000205bool ARMMCCodeEmitter::
206EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
207 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000208 const MCOperand &MO = MI.getOperand(OpIdx);
209 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000210
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000211 Reg = getARMRegisterNumbering(MO.getReg());
212
213 int32_t SImm = MO1.getImm();
214 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000215
Jim Grosbachab682a22010-10-28 18:34:10 +0000216 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000217 if (SImm == INT32_MIN)
218 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000219
Jim Grosbachab682a22010-10-28 18:34:10 +0000220 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000221 if (SImm < 0) {
222 SImm = -SImm;
223 isAdd = false;
224 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000225
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000226 Imm = SImm;
227 return isAdd;
228}
229
230/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000231uint32_t ARMMCCodeEmitter::
232getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
233 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000234 // {17-13} = reg
235 // {12} = (U)nsigned (add == '1', sub == '0')
236 // {11-0} = imm12
237 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000238 bool isAdd = true;
239 // If The first operand isn't a register, we have a label reference.
240 const MCOperand &MO = MI.getOperand(OpIdx);
241 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000242 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000243 Imm12 = 0;
244
245 assert(MO.isExpr() && "Unexpected machine operand type!");
246 const MCExpr *Expr = MO.getExpr();
247 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
248 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
249
250 ++MCNumCPRelocations;
251 } else
252 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000253
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000254 uint32_t Binary = Imm12 & 0xfff;
255 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000256 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000257 Binary |= (1 << 12);
258 Binary |= (Reg << 13);
259 return Binary;
260}
261
Jim Grosbach54fea632010-11-09 17:20:53 +0000262uint32_t ARMMCCodeEmitter::
263getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
264 SmallVectorImpl<MCFixup> &Fixups) const {
265 const MCOperand &MO = MI.getOperand(OpIdx);
266 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
267 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
268 unsigned Rn = getARMRegisterNumbering(MO.getReg());
269 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
270 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
271 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
272 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
273 unsigned SBits;
274 // LSL - 00
275 // LSR - 01
276 // ASR - 10
277 // ROR - 11
278 switch (ShOp) {
279 default: llvm_unreachable("Unknown shift opc!");
Jim Grosbachd92354c2010-11-09 17:38:15 +0000280 case ARM_AM::no_shift:
281 assert(ShImm == 0 && "Non-zero shift amount with no shift type!");
282 // fall through
Jim Grosbach54fea632010-11-09 17:20:53 +0000283 case ARM_AM::lsl: SBits = 0x0; break;
284 case ARM_AM::lsr: SBits = 0x1; break;
285 case ARM_AM::asr: SBits = 0x2; break;
286 case ARM_AM::ror: SBits = 0x3; break;
287 }
288
289 // {16-13} = Rn
290 // {12} = isAdd
291 // {11-0} = shifter
292 // {3-0} = Rm
293 // {4} = 0
294 // {6-5} = type
295 // {11-7} = imm
296 int64_t Binary = Rm;
297 Binary |= Rn << 13;
298 Binary |= SBits << 5;
299 Binary |= ShImm << 7;
300 if (isAdd)
301 Binary |= 1 << 12;
302 return Binary;
303}
304
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000305/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000306uint32_t ARMMCCodeEmitter::
307getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
308 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000309 // {12-9} = reg
310 // {8} = (U)nsigned (add == '1', sub == '0')
311 // {7-0} = imm8
312 unsigned Reg, Imm8;
Jim Grosbach70933262010-11-04 01:12:30 +0000313 // If The first operand isn't a register, we have a label reference.
314 const MCOperand &MO = MI.getOperand(OpIdx);
315 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000316 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000317 Imm8 = 0;
318
319 assert(MO.isExpr() && "Unexpected machine operand type!");
320 const MCExpr *Expr = MO.getExpr();
321 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_vfp_pcrel_12);
322 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
323
324 ++MCNumCPRelocations;
325 } else
326 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000327
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000328 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
329 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
330 if (ARM_AM::getAM5Op(Imm8) == ARM_AM::add)
331 Binary |= (1 << 8);
332 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000333 return Binary;
334}
335
Jim Grosbach806e80e2010-11-03 23:52:49 +0000336unsigned ARMMCCodeEmitter::
337getSORegOpValue(const MCInst &MI, unsigned OpIdx,
338 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000339 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
340 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
341 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000342 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000343 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000344 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000345 // {6-5} = type
346 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000347 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000348 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000349 // else (imm shift)
350 // {11-7} = imm
351
352 const MCOperand &MO = MI.getOperand(OpIdx);
353 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
354 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
355 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
356
357 // Encode Rm.
358 unsigned Binary = getARMRegisterNumbering(MO.getReg());
359
360 // Encode the shift opcode.
361 unsigned SBits = 0;
362 unsigned Rs = MO1.getReg();
363 if (Rs) {
364 // Set shift operand (bit[7:4]).
365 // LSL - 0001
366 // LSR - 0011
367 // ASR - 0101
368 // ROR - 0111
369 // RRX - 0110 and bit[11:8] clear.
370 switch (SOpc) {
371 default: llvm_unreachable("Unknown shift opc!");
372 case ARM_AM::lsl: SBits = 0x1; break;
373 case ARM_AM::lsr: SBits = 0x3; break;
374 case ARM_AM::asr: SBits = 0x5; break;
375 case ARM_AM::ror: SBits = 0x7; break;
376 case ARM_AM::rrx: SBits = 0x6; break;
377 }
378 } else {
379 // Set shift operand (bit[6:4]).
380 // LSL - 000
381 // LSR - 010
382 // ASR - 100
383 // ROR - 110
384 switch (SOpc) {
385 default: llvm_unreachable("Unknown shift opc!");
386 case ARM_AM::lsl: SBits = 0x0; break;
387 case ARM_AM::lsr: SBits = 0x2; break;
388 case ARM_AM::asr: SBits = 0x4; break;
389 case ARM_AM::ror: SBits = 0x6; break;
390 }
391 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000392
Jim Grosbachef324d72010-10-12 23:53:58 +0000393 Binary |= SBits << 4;
394 if (SOpc == ARM_AM::rrx)
395 return Binary;
396
397 // Encode the shift operation Rs or shift_imm (except rrx).
398 if (Rs) {
399 // Encode Rs bit[11:8].
400 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
401 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
402 }
403
404 // Encode shift_imm bit[11:7].
405 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
406}
407
Jim Grosbach806e80e2010-11-03 23:52:49 +0000408unsigned ARMMCCodeEmitter::
409getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
410 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000411 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
412 // msb of the mask.
413 const MCOperand &MO = MI.getOperand(Op);
414 uint32_t v = ~MO.getImm();
415 uint32_t lsb = CountTrailingZeros_32(v);
416 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
417 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
418 return lsb | (msb << 5);
419}
420
Jim Grosbach806e80e2010-11-03 23:52:49 +0000421unsigned ARMMCCodeEmitter::
422getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000423 SmallVectorImpl<MCFixup> &Fixups) const {
424 // Convert a list of GPRs into a bitfield (R0 -> bit 0). For each
425 // register in the list, set the corresponding bit.
426 unsigned Binary = 0;
427 for (unsigned i = Op, e = MI.getNumOperands(); i < e; ++i) {
428 unsigned regno = getARMRegisterNumbering(MI.getOperand(i).getReg());
429 Binary |= 1 << regno;
430 }
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000431 return Binary;
432}
433
Jim Grosbach806e80e2010-11-03 23:52:49 +0000434unsigned ARMMCCodeEmitter::
435getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
436 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000437 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000438 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +0000439
Owen Andersond9aa7d32010-11-02 00:05:05 +0000440 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000441 unsigned Align = 0;
442
443 switch (Imm.getImm()) {
444 default: break;
445 case 2:
446 case 4:
447 case 8: Align = 0x01; break;
448 case 16: Align = 0x02; break;
449 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000450 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000451
Owen Andersond9aa7d32010-11-02 00:05:05 +0000452 return RegNo | (Align << 4);
453}
454
Jim Grosbach806e80e2010-11-03 23:52:49 +0000455unsigned ARMMCCodeEmitter::
456getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
457 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000458 const MCOperand &MO = MI.getOperand(Op);
459 if (MO.getReg() == 0) return 0x0D;
460 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +0000461}
462
Jim Grosbach568eeed2010-09-17 18:46:17 +0000463void ARMMCCodeEmitter::
464EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000465 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000466 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +0000467 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
468 if ((Desc.TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000469 return;
470
Jim Grosbach70933262010-11-04 01:12:30 +0000471 EmitConstant(getBinaryCodeForInstr(MI, Fixups), 4, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +0000472 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +0000473}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000474
Jim Grosbach806e80e2010-11-03 23:52:49 +0000475#include "ARMGenMCCodeEmitter.inc"