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 unsigned getNumFixupKinds() const {
53 assert(0 && "ARMMCCodeEmitter::getNumFixupKinds() not yet implemented.");
57 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
58 static MCFixupKindInfo rtn;
59 assert(0 && "ARMMCCodeEmitter::getFixupKindInfo() not yet implemented.");
63 void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
68 void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
69 raw_ostream &OS) const {
70 // Output the constant in little endian byte order.
71 for (unsigned i = 0; i != Size; ++i) {
72 EmitByte(Val & 255, CurByte, OS);
77 void EmitImmediate(const MCOperand &Disp,
78 unsigned ImmSize, MCFixupKind FixupKind,
79 unsigned &CurByte, raw_ostream &OS,
80 SmallVectorImpl<MCFixup> &Fixups,
81 int ImmOffset = 0) const;
83 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
84 SmallVectorImpl<MCFixup> &Fixups) const;
87 } // end anonymous namespace
89 unsigned ARMMCCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) const {
90 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
91 assert(SoImmVal != -1 && "Not a valid so_imm value!");
94 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
95 << ARMII::SoRotImmShift;
98 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
102 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &,
105 return new ARMMCCodeEmitter(TM, Ctx);
108 void ARMMCCodeEmitter::
109 EmitImmediate(const MCOperand &DispOp, unsigned Size, MCFixupKind FixupKind,
110 unsigned &CurByte, raw_ostream &OS,
111 SmallVectorImpl<MCFixup> &Fixups, int ImmOffset) const {
112 assert(0 && "ARMMCCodeEmitter::EmitImmediate() not yet implemented.");
115 /// getMachineOpValue - Return binary encoding of operand. If the machine
116 /// operand requires relocation, record the relocation and return zero.
117 unsigned ARMMCCodeEmitter::getMachineOpValue(const MCInst &MI,
118 const MCOperand &MO) const {
120 return getARMRegisterNumbering(MO.getReg());
121 else if (MO.isImm()) {
122 return static_cast<unsigned>(MO.getImm());
132 void ARMMCCodeEmitter::
133 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
134 SmallVectorImpl<MCFixup> &Fixups) const {
135 unsigned Opcode = MI.getOpcode();
136 const TargetInstrDesc &Desc = TII.get(Opcode);
137 uint64_t TSFlags = Desc.TSFlags;
138 // Keep track of the current byte being emitted.
139 unsigned CurByte = 0;
141 // Pseudo instructions don't get encoded.
142 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
145 ++MCNumEmitted; // Keep track of the # of mi's emitted
146 // FIXME: TableGen doesn't deal well with operands that expand to multiple
147 // machine instruction operands, so for now we'll fix those up here.
148 // Similarly, operands that are encoded as other than their literal
150 unsigned Value = getBinaryCodeForInstr(MI);
155 if (MI.getOperand(4).getReg() == ARM::CPSR)
156 Value |= 1 << ARMII::S_BitShift;
157 // The shifted immediate value.
158 Value |= getMachineSoImmOpValue((unsigned)MI.getOperand(1).getImm());
167 if (MI.getOperand(5).getReg() == ARM::CPSR)
168 Value |= 1 << ARMII::S_BitShift;
169 // The shifted immediate value.
170 Value |= getMachineSoImmOpValue((unsigned)MI.getOperand(2).getImm());
179 if (MI.getOperand(7).getReg() == ARM::CPSR)
180 Value |= 1 << ARMII::S_BitShift;
181 // The so_reg operand needs the shift ammount encoded.
182 unsigned ShVal = MI.getOperand(4).getImm();
183 unsigned ShType = ARM_AM::getShiftOpcEncoding(ARM_AM::getSORegShOp(ShVal));
184 unsigned ShAmt = ARM_AM::getSORegOffset(ShVal);
185 Value |= ShType << ARMII::ShiftTypeShift;
186 Value |= ShAmt << ARMII::ShiftShift;
190 EmitConstant(Value, 4, CurByte, OS);
193 // FIXME: These #defines shouldn't be necessary. Instead, tblgen should
194 // be able to generate code emitter helpers for either variant, like it
195 // does for the AsmWriter.
196 #define ARMCodeEmitter ARMMCCodeEmitter
197 #define MachineInstr MCInst
198 #include "ARMGenCodeEmitter.inc"
199 #undef ARMCodeEmitter