blob: 296a5c9ce3646d4a8ba6e2058ca7d344a79e69c9 [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
80 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +000081 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
82 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +000083
Jim Grosbach08bd5492010-10-12 23:00:24 +000084 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +000085 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
86 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +000087 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
88 // '1' respectively.
89 return MI.getOperand(Op).getReg() == ARM::CPSR;
90 }
Jim Grosbachef324d72010-10-12 23:53:58 +000091
Jim Grosbach2a6a93d2010-10-12 23:18:08 +000092 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +000093 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
94 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +000095 unsigned SoImm = MI.getOperand(Op).getImm();
96 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
97 assert(SoImmVal != -1 && "Not a valid so_imm value!");
98
99 // Encode rotate_imm.
100 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
101 << ARMII::SoRotImmShift;
102
103 // Encode immed_8.
104 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
105 return Binary;
106 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000107
Jim Grosbachef324d72010-10-12 23:53:58 +0000108 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000109 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
110 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000111
Jim Grosbach806e80e2010-11-03 23:52:49 +0000112 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
113 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000114 switch (MI.getOperand(Op).getImm()) {
115 default: assert (0 && "Not a valid rot_imm value!");
116 case 0: return 0;
117 case 8: return 1;
118 case 16: return 2;
119 case 24: return 3;
120 }
121 }
122
Jim Grosbach806e80e2010-11-03 23:52:49 +0000123 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
124 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000125 return MI.getOperand(Op).getImm() - 1;
126 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000127
Jim Grosbach806e80e2010-11-03 23:52:49 +0000128 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
129 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000130 return 64 - MI.getOperand(Op).getImm();
131 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000132
Jim Grosbach806e80e2010-11-03 23:52:49 +0000133 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
134 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000135
Jim Grosbach806e80e2010-11-03 23:52:49 +0000136 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
137 SmallVectorImpl<MCFixup> &Fixups) const;
138 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
139 SmallVectorImpl<MCFixup> &Fixups) const;
140 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
141 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000142
Jim Grosbach70933262010-11-04 01:12:30 +0000143 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000144 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000145 }
146
Jim Grosbach70933262010-11-04 01:12:30 +0000147 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000148 // Output the constant in little endian byte order.
149 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000150 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000151 Val >>= 8;
152 }
153 }
154
Jim Grosbach568eeed2010-09-17 18:46:17 +0000155 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
156 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000157};
158
159} // end anonymous namespace
160
Bill Wendling0800ce72010-11-02 22:53:11 +0000161MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
162 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000163 return new ARMMCCodeEmitter(TM, Ctx);
164}
165
Jim Grosbach56ac9072010-10-08 21:45:55 +0000166/// getMachineOpValue - Return binary encoding of operand. If the machine
167/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000168unsigned ARMMCCodeEmitter::
169getMachineOpValue(const MCInst &MI, const MCOperand &MO,
170 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000171 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000172 unsigned Reg = MO.getReg();
173 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000174
Owen Anderson90d4cf92010-10-21 20:49:13 +0000175 // Q registers are encodes as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000176 switch (Reg) {
177 default:
178 return RegNo;
179 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
180 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
181 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
182 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
183 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000184 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000185 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000186 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000187 } else if (MO.isFPImm()) {
188 return static_cast<unsigned>(APFloat(MO.getFPImm())
189 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000190 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000191
192#ifndef NDEBUG
193 errs() << MO;
194#endif
195 llvm_unreachable(0);
Jim Grosbach56ac9072010-10-08 21:45:55 +0000196 return 0;
197}
198
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000199/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000200bool ARMMCCodeEmitter::
201EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
202 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000203 const MCOperand &MO = MI.getOperand(OpIdx);
204 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000205
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000206 Reg = getARMRegisterNumbering(MO.getReg());
207
208 int32_t SImm = MO1.getImm();
209 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000210
Jim Grosbachab682a22010-10-28 18:34:10 +0000211 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000212 if (SImm == INT32_MIN)
213 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000214
Jim Grosbachab682a22010-10-28 18:34:10 +0000215 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000216 if (SImm < 0) {
217 SImm = -SImm;
218 isAdd = false;
219 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000220
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000221 Imm = SImm;
222 return isAdd;
223}
224
225/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000226uint32_t ARMMCCodeEmitter::
227getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
228 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000229 // {17-13} = reg
230 // {12} = (U)nsigned (add == '1', sub == '0')
231 // {11-0} = imm12
232 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000233 bool isAdd = true;
234 // If The first operand isn't a register, we have a label reference.
235 const MCOperand &MO = MI.getOperand(OpIdx);
236 if (!MO.isReg()) {
237 Reg = ARM::PC; // Rn is PC.
238 Imm12 = 0;
239
240 assert(MO.isExpr() && "Unexpected machine operand type!");
241 const MCExpr *Expr = MO.getExpr();
242 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
243 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
244
245 ++MCNumCPRelocations;
246 } else
247 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000248
249 if (Reg == ARM::PC)
250 return ARM::PC << 13; // Rn is PC;
251
252 uint32_t Binary = Imm12 & 0xfff;
253 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000254 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000255 Binary |= (1 << 12);
256 Binary |= (Reg << 13);
257 return Binary;
258}
259
260/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000261uint32_t ARMMCCodeEmitter::
262getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
263 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000264 // {12-9} = reg
265 // {8} = (U)nsigned (add == '1', sub == '0')
266 // {7-0} = imm8
267 unsigned Reg, Imm8;
Jim Grosbach70933262010-11-04 01:12:30 +0000268 // If The first operand isn't a register, we have a label reference.
269 const MCOperand &MO = MI.getOperand(OpIdx);
270 if (!MO.isReg()) {
271 Reg = ARM::PC; // Rn is PC.
272 Imm8 = 0;
273
274 assert(MO.isExpr() && "Unexpected machine operand type!");
275 const MCExpr *Expr = MO.getExpr();
276 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_vfp_pcrel_12);
277 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
278
279 ++MCNumCPRelocations;
280 } else
281 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000282
283 if (Reg == ARM::PC)
Bill Wendlingcdbbec42010-11-03 04:57:44 +0000284 return ARM::PC << 9; // Rn is PC;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000285
286 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
287 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
288 if (ARM_AM::getAM5Op(Imm8) == ARM_AM::add)
289 Binary |= (1 << 8);
290 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000291 return Binary;
292}
293
Jim Grosbach806e80e2010-11-03 23:52:49 +0000294unsigned ARMMCCodeEmitter::
295getSORegOpValue(const MCInst &MI, unsigned OpIdx,
296 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000297 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
298 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
299 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000300 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000301 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000302 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000303 // {6-5} = type
304 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000305 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000306 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000307 // else (imm shift)
308 // {11-7} = imm
309
310 const MCOperand &MO = MI.getOperand(OpIdx);
311 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
312 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
313 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
314
315 // Encode Rm.
316 unsigned Binary = getARMRegisterNumbering(MO.getReg());
317
318 // Encode the shift opcode.
319 unsigned SBits = 0;
320 unsigned Rs = MO1.getReg();
321 if (Rs) {
322 // Set shift operand (bit[7:4]).
323 // LSL - 0001
324 // LSR - 0011
325 // ASR - 0101
326 // ROR - 0111
327 // RRX - 0110 and bit[11:8] clear.
328 switch (SOpc) {
329 default: llvm_unreachable("Unknown shift opc!");
330 case ARM_AM::lsl: SBits = 0x1; break;
331 case ARM_AM::lsr: SBits = 0x3; break;
332 case ARM_AM::asr: SBits = 0x5; break;
333 case ARM_AM::ror: SBits = 0x7; break;
334 case ARM_AM::rrx: SBits = 0x6; break;
335 }
336 } else {
337 // Set shift operand (bit[6:4]).
338 // LSL - 000
339 // LSR - 010
340 // ASR - 100
341 // ROR - 110
342 switch (SOpc) {
343 default: llvm_unreachable("Unknown shift opc!");
344 case ARM_AM::lsl: SBits = 0x0; break;
345 case ARM_AM::lsr: SBits = 0x2; break;
346 case ARM_AM::asr: SBits = 0x4; break;
347 case ARM_AM::ror: SBits = 0x6; break;
348 }
349 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000350
Jim Grosbachef324d72010-10-12 23:53:58 +0000351 Binary |= SBits << 4;
352 if (SOpc == ARM_AM::rrx)
353 return Binary;
354
355 // Encode the shift operation Rs or shift_imm (except rrx).
356 if (Rs) {
357 // Encode Rs bit[11:8].
358 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
359 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
360 }
361
362 // Encode shift_imm bit[11:7].
363 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
364}
365
Jim Grosbach806e80e2010-11-03 23:52:49 +0000366unsigned ARMMCCodeEmitter::
367getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
368 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000369 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
370 // msb of the mask.
371 const MCOperand &MO = MI.getOperand(Op);
372 uint32_t v = ~MO.getImm();
373 uint32_t lsb = CountTrailingZeros_32(v);
374 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
375 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
376 return lsb | (msb << 5);
377}
378
Jim Grosbach806e80e2010-11-03 23:52:49 +0000379unsigned ARMMCCodeEmitter::
380getRegisterListOpValue(const MCInst &MI, unsigned Op,
381 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000382 // Convert a list of GPRs into a bitfield (R0 -> bit 0). For each
383 // register in the list, set the corresponding bit.
384 unsigned Binary = 0;
Jim Grosbach4b5236c2010-10-30 01:40:16 +0000385 for (unsigned i = Op, e = MI.getNumOperands(); i < e; ++i) {
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000386 unsigned regno = getARMRegisterNumbering(MI.getOperand(i).getReg());
387 Binary |= 1 << regno;
388 }
389 return Binary;
390}
391
Jim Grosbach806e80e2010-11-03 23:52:49 +0000392unsigned ARMMCCodeEmitter::
393getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
394 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000395 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000396 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +0000397
Owen Andersond9aa7d32010-11-02 00:05:05 +0000398 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000399 unsigned Align = 0;
400
401 switch (Imm.getImm()) {
402 default: break;
403 case 2:
404 case 4:
405 case 8: Align = 0x01; break;
406 case 16: Align = 0x02; break;
407 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000408 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000409
Owen Andersond9aa7d32010-11-02 00:05:05 +0000410 return RegNo | (Align << 4);
411}
412
Jim Grosbach806e80e2010-11-03 23:52:49 +0000413unsigned ARMMCCodeEmitter::
414getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
415 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000416 const MCOperand &MO = MI.getOperand(Op);
417 if (MO.getReg() == 0) return 0x0D;
418 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +0000419}
420
Jim Grosbach568eeed2010-09-17 18:46:17 +0000421void ARMMCCodeEmitter::
422EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000423 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000424 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +0000425 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
426 if ((Desc.TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000427 return;
428
Jim Grosbach70933262010-11-04 01:12:30 +0000429 EmitConstant(getBinaryCodeForInstr(MI, Fixups), 4, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +0000430 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +0000431}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000432
Jim Grosbach806e80e2010-11-03 23:52:49 +0000433#include "ARMGenMCCodeEmitter.inc"