1 //===-- MBlazeMCCodeEmitter.cpp - Convert MBlaze 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 MBlazeMCCodeEmitter class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "mblaze-emitter"
16 #include "MBlazeInstrInfo.h"
17 #include "MBlazeFixupKinds.h"
18 #include "llvm/MC/MCCodeEmitter.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/MC/MCFixup.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Support/raw_ostream.h"
27 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
30 class MBlazeMCCodeEmitter : public MCCodeEmitter {
31 MBlazeMCCodeEmitter(const MBlazeMCCodeEmitter &); // DO NOT IMPLEMENT
32 void operator=(const MBlazeMCCodeEmitter &); // DO NOT IMPLEMENT
33 const TargetMachine &TM;
34 const TargetInstrInfo &TII;
38 MBlazeMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
39 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
42 ~MBlazeMCCodeEmitter() {}
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;
51 unsigned getMachineOpValue(const MCInst &MI, unsigned OpIdx) const {
52 return getMachineOpValue(MI, MI.getOperand(OpIdx));
55 unsigned getNumFixupKinds() const {
59 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
60 const static MCFixupKindInfo Infos[] = {
61 { "reloc_pcrel_4byte", 0, 4 * 8, MCFixupKindInfo::FKF_IsPCRel },
62 { "reloc_pcrel_2byte", 0, 2 * 8, MCFixupKindInfo::FKF_IsPCRel } };
64 if (Kind < FirstTargetFixupKind)
65 return MCCodeEmitter::getFixupKindInfo(Kind);
67 if (unsigned(Kind-FirstTargetFixupKind) < getNumFixupKinds())
68 return Infos[Kind - FirstTargetFixupKind];
70 assert(0 && "Invalid fixup kind.");
74 static unsigned GetMBlazeRegNum(const MCOperand &MO) {
75 // FIXME: getMBlazeRegisterNumbering() is sufficient?
76 assert(0 && "MBlazeMCCodeEmitter::GetMBlazeRegNum() not yet implemented.");
80 void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
81 // The MicroBlaze uses a bit reversed format so we need to reverse the
82 // order of the bits. Taken from:
83 // http://graphics.stanford.edu/~seander/bithacks.html
84 C = ((C * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32;
90 void EmitRawByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
95 void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
96 raw_ostream &OS) const {
97 assert(Size <= 8 && "size too big in emit constant" );
99 for (unsigned i = 0; i != Size; ++i) {
100 EmitByte(Val & 255, CurByte, OS);
105 void EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const;
107 void EmitImmediate(const MCInst &MI,
108 unsigned opNo, MCFixupKind FixupKind,
109 unsigned &CurByte, raw_ostream &OS,
110 SmallVectorImpl<MCFixup> &Fixups) const;
112 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
113 SmallVectorImpl<MCFixup> &Fixups) const;
116 } // end anonymous namespace
119 MCCodeEmitter *llvm::createMBlazeMCCodeEmitter(const Target &,
122 return new MBlazeMCCodeEmitter(TM, Ctx);
125 /// getMachineOpValue - Return binary encoding of operand. If the machine
126 /// operand requires relocation, record the relocation and return zero.
127 unsigned MBlazeMCCodeEmitter::getMachineOpValue(const MCInst &MI,
128 const MCOperand &MO) const {
130 return MBlazeRegisterInfo::getRegisterNumbering(MO.getReg());
132 return static_cast<unsigned>(MO.getImm());
133 else if (MO.isExpr() )
134 return 0; // The relocation has already been recorded at this point.
144 void MBlazeMCCodeEmitter::
145 EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const {
146 int32_t val = (int32_t)imm.getImm();
147 if (val > 32767 || val < -32678 ) {
148 EmitByte(0x0D, CurByte, OS);
149 EmitByte(0x00, CurByte, OS);
150 EmitRawByte((val >> 24) & 0xFF, CurByte, OS);
151 EmitRawByte((val >> 16) & 0xFF, CurByte, OS);
155 void MBlazeMCCodeEmitter::
156 EmitImmediate(const MCInst &MI, unsigned opNo, MCFixupKind FixupKind,
157 unsigned &CurByte, raw_ostream &OS,
158 SmallVectorImpl<MCFixup> &Fixups) const {
159 assert( MI.getNumOperands()>opNo && "Not enought operands for instruction" );
161 MCOperand oper = MI.getOperand(opNo);
163 EmitIMM( oper, CurByte, OS );
164 } else if (oper.isExpr()) {
165 Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
169 void MBlazeMCCodeEmitter::
170 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
171 SmallVectorImpl<MCFixup> &Fixups) const {
172 unsigned Opcode = MI.getOpcode();
173 const TargetInstrDesc &Desc = TII.get(Opcode);
174 uint64_t TSFlags = Desc.TSFlags;
175 // Keep track of the current byte being emitted.
176 unsigned CurByte = 0;
178 switch ((TSFlags & MBlazeII::FormMask)) {
180 case MBlazeII::Pseudo:
181 // Pseudo instructions don't get encoded.
184 case MBlazeII::RegRegImm:
185 EmitImmediate( MI, 2, FK_Data_4, CurByte, OS, Fixups );
188 case MBlazeII::RegImmReg:
189 EmitImmediate( MI, 1, FK_Data_4, CurByte, OS, Fixups );
192 case MBlazeII::RegImm:
193 EmitImmediate( MI, 1, MCFixupKind(MBlaze::reloc_pcrel_2byte), CurByte, OS,
198 EmitImmediate( MI, 0, MCFixupKind(MBlaze::reloc_pcrel_4byte), CurByte, OS,
203 ++MCNumEmitted; // Keep track of the # of mi's emitted
204 unsigned Value = getBinaryCodeForInstr(MI);
207 EmitConstant(Value, 4, CurByte, OS);
216 MCOperand op = MI.getOperand(0);
218 EmitByte(0x0D, CurByte, OS);
219 EmitByte(0x00, CurByte, OS);
220 EmitRawByte(0, CurByte, OS);
221 EmitRawByte(0, CurByte, OS);
223 EmitConstant(Value, 4, CurByte, OS);
228 // FIXME: These #defines shouldn't be necessary. Instead, tblgen should
229 // be able to generate code emitter helpers for either variant, like it
230 // does for the AsmWriter.
231 #define MBlazeCodeEmitter MBlazeMCCodeEmitter
232 #define MachineInstr MCInst
233 #include "MBlazeGenCodeEmitter.inc"
234 #undef MBlazeCodeEmitter