1 //===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ARMMCCodeEmitter class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "arm-emitter"
16 #include "ARMAddressingModes.h"
17 #include "ARMInstrInfo.h"
18 #include "llvm/MC/MCCodeEmitter.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Support/raw_ostream.h"
25 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
28 class ARMMCCodeEmitter : public MCCodeEmitter {
29 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
30 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
31 const TargetMachine &TM;
32 const TargetInstrInfo &TII;
36 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
37 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
40 ~ARMMCCodeEmitter() {}
42 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
44 // getBinaryCodeForInstr - TableGen'erated function for getting the
45 // binary encoding for an instruction.
46 unsigned getBinaryCodeForInstr(const MCInst &MI) const;
48 /// getMachineOpValue - Return binary encoding of operand. If the machine
49 /// operand requires relocation, record the relocation and return zero.
50 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO) const;
52 /// getCCOutOpValue - Return encoding of the 's' bit.
53 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op) const {
54 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
56 return MI.getOperand(Op).getReg() == ARM::CPSR;
59 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
60 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op) const {
61 unsigned SoImm = MI.getOperand(Op).getImm();
62 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
63 assert(SoImmVal != -1 && "Not a valid so_imm value!");
66 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
67 << ARMII::SoRotImmShift;
70 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
74 /// getSORegOpValue - Return an encoded so_reg shifted register value.
75 unsigned getSORegOpValue(const MCInst &MI, unsigned Op) const;
77 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op) const {
78 switch (MI.getOperand(Op).getImm()) {
79 default: assert (0 && "Not a valid rot_imm value!");
87 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op) const {
88 return MI.getOperand(Op).getImm() - 1;
91 unsigned getNumFixupKinds() const {
92 assert(0 && "ARMMCCodeEmitter::getNumFixupKinds() not yet implemented.");
96 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
97 static MCFixupKindInfo rtn;
98 assert(0 && "ARMMCCodeEmitter::getFixupKindInfo() not yet implemented.");
102 void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
107 void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
108 raw_ostream &OS) const {
109 // Output the constant in little endian byte order.
110 for (unsigned i = 0; i != Size; ++i) {
111 EmitByte(Val & 255, CurByte, OS);
116 void EmitImmediate(const MCOperand &Disp,
117 unsigned ImmSize, MCFixupKind FixupKind,
118 unsigned &CurByte, raw_ostream &OS,
119 SmallVectorImpl<MCFixup> &Fixups,
120 int ImmOffset = 0) const;
122 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
123 SmallVectorImpl<MCFixup> &Fixups) const;
126 } // end anonymous namespace
128 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &,
131 return new ARMMCCodeEmitter(TM, Ctx);
134 void ARMMCCodeEmitter::
135 EmitImmediate(const MCOperand &DispOp, unsigned Size, MCFixupKind FixupKind,
136 unsigned &CurByte, raw_ostream &OS,
137 SmallVectorImpl<MCFixup> &Fixups, int ImmOffset) const {
138 assert(0 && "ARMMCCodeEmitter::EmitImmediate() not yet implemented.");
141 /// getMachineOpValue - Return binary encoding of operand. If the machine
142 /// operand requires relocation, record the relocation and return zero.
143 unsigned ARMMCCodeEmitter::getMachineOpValue(const MCInst &MI,
144 const MCOperand &MO) const {
146 return getARMRegisterNumbering(MO.getReg());
147 } else if (MO.isImm()) {
148 return static_cast<unsigned>(MO.getImm());
149 } else if (MO.isFPImm()) {
150 return static_cast<unsigned>(APFloat(MO.getFPImm())
151 .bitcastToAPInt().getHiBits(32).getLimitedValue());
161 unsigned ARMMCCodeEmitter::getSORegOpValue(const MCInst &MI,
162 unsigned OpIdx) const {
163 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg
164 // to be shifted. The second is either Rs, the amount to shift by, or
165 // reg0 in which case the imm contains the amount to shift by.
167 // {4} = 1 if reg shift, 0 if imm shift
175 const MCOperand &MO = MI.getOperand(OpIdx);
176 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
177 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
178 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
181 unsigned Binary = getARMRegisterNumbering(MO.getReg());
183 // Encode the shift opcode.
185 unsigned Rs = MO1.getReg();
187 // Set shift operand (bit[7:4]).
192 // RRX - 0110 and bit[11:8] clear.
194 default: llvm_unreachable("Unknown shift opc!");
195 case ARM_AM::lsl: SBits = 0x1; break;
196 case ARM_AM::lsr: SBits = 0x3; break;
197 case ARM_AM::asr: SBits = 0x5; break;
198 case ARM_AM::ror: SBits = 0x7; break;
199 case ARM_AM::rrx: SBits = 0x6; break;
202 // Set shift operand (bit[6:4]).
208 default: llvm_unreachable("Unknown shift opc!");
209 case ARM_AM::lsl: SBits = 0x0; break;
210 case ARM_AM::lsr: SBits = 0x2; break;
211 case ARM_AM::asr: SBits = 0x4; break;
212 case ARM_AM::ror: SBits = 0x6; break;
215 Binary |= SBits << 4;
216 if (SOpc == ARM_AM::rrx)
219 // Encode the shift operation Rs or shift_imm (except rrx).
221 // Encode Rs bit[11:8].
222 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
223 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
226 // Encode shift_imm bit[11:7].
227 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
230 void ARMMCCodeEmitter::
231 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
232 SmallVectorImpl<MCFixup> &Fixups) const {
233 unsigned Opcode = MI.getOpcode();
234 const TargetInstrDesc &Desc = TII.get(Opcode);
235 uint64_t TSFlags = Desc.TSFlags;
236 // Keep track of the current byte being emitted.
237 unsigned CurByte = 0;
239 // Pseudo instructions don't get encoded.
240 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
243 ++MCNumEmitted; // Keep track of the # of mi's emitted
244 unsigned Value = getBinaryCodeForInstr(MI);
248 EmitConstant(Value, 4, CurByte, OS);
251 // FIXME: These #defines shouldn't be necessary. Instead, tblgen should
252 // be able to generate code emitter helpers for either variant, like it
253 // does for the AsmWriter.
254 #define ARMCodeEmitter ARMMCCodeEmitter
255 #define MachineInstr MCInst
256 #include "ARMGenCodeEmitter.inc"
257 #undef ARMCodeEmitter