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 "mccodeemitter"
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;
106 void EmitIMM(const MCInst &MI, unsigned op, unsigned &CurByte,
107 raw_ostream &OS) const;
109 void EmitImmediate(const MCInst &MI,
110 unsigned opNo, MCFixupKind FixupKind,
111 unsigned &CurByte, raw_ostream &OS,
112 SmallVectorImpl<MCFixup> &Fixups) const;
114 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
115 SmallVectorImpl<MCFixup> &Fixups) const;
118 } // end anonymous namespace
121 MCCodeEmitter *llvm::createMBlazeMCCodeEmitter(const Target &,
124 return new MBlazeMCCodeEmitter(TM, Ctx);
127 /// getMachineOpValue - Return binary encoding of operand. If the machine
128 /// operand requires relocation, record the relocation and return zero.
129 unsigned MBlazeMCCodeEmitter::getMachineOpValue(const MCInst &MI,
130 const MCOperand &MO) const {
132 return MBlazeRegisterInfo::getRegisterNumbering(MO.getReg());
134 return static_cast<unsigned>(MO.getImm());
135 else if (MO.isExpr())
136 return 0; // The relocation has already been recorded at this point.
146 void MBlazeMCCodeEmitter::
147 EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const {
148 int32_t val = (int32_t)imm.getImm();
149 if (val > 32767 || val < -32768) {
150 EmitByte(0x0D, CurByte, OS);
151 EmitByte(0x00, CurByte, OS);
152 EmitRawByte((val >> 24) & 0xFF, CurByte, OS);
153 EmitRawByte((val >> 16) & 0xFF, CurByte, OS);
157 void MBlazeMCCodeEmitter::
158 EmitIMM(const MCInst &MI, unsigned op, unsigned &CurByte,
159 raw_ostream &OS) const {
160 MCOperand mcop = MI.getOperand(op);
162 EmitByte(0x0D, CurByte, OS);
163 EmitByte(0x00, CurByte, OS);
164 EmitRawByte(0, CurByte, OS);
165 EmitRawByte(0, CurByte, OS);
169 void MBlazeMCCodeEmitter::
170 EmitImmediate(const MCInst &MI, unsigned opNo, MCFixupKind FixupKind,
171 unsigned &CurByte, raw_ostream &OS,
172 SmallVectorImpl<MCFixup> &Fixups) const {
173 assert(MI.getNumOperands()>opNo && "Not enought operands for instruction");
175 MCOperand oper = MI.getOperand(opNo);
177 EmitIMM(oper, CurByte, OS);
178 } else if (oper.isExpr()) {
179 Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
185 void MBlazeMCCodeEmitter::
186 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
187 SmallVectorImpl<MCFixup> &Fixups) const {
188 unsigned Opcode = MI.getOpcode();
189 const TargetInstrDesc &Desc = TII.get(Opcode);
190 uint64_t TSFlags = Desc.TSFlags;
191 // Keep track of the current byte being emitted.
192 unsigned CurByte = 0;
194 switch ((TSFlags & MBlazeII::FormMask)) {
196 case MBlazeII::FPseudo:
197 // Pseudo instructions don't get encoded.
201 EmitImmediate(MI, 2, FK_Data_4, CurByte, OS, Fixups);
205 EmitImmediate(MI, 1, FK_Data_4, CurByte, OS, Fixups);
209 EmitImmediate(MI, 1, MCFixupKind(MBlaze::reloc_pcrel_2byte), CurByte, OS,
214 EmitImmediate(MI, 1, MCFixupKind(MBlaze::reloc_pcrel_4byte), CurByte, OS,
218 EmitImmediate(MI, 0, MCFixupKind(MBlaze::reloc_pcrel_4byte), CurByte, OS,
223 ++MCNumEmitted; // Keep track of the # of mi's emitted
224 unsigned Value = getBinaryCodeForInstr(MI);
227 EmitConstant(Value, 4, CurByte, OS);
232 EmitIMM(MI,1,CurByte,OS);
233 EmitConstant(Value, 4, CurByte, OS);
240 EmitIMM(MI,0,CurByte,OS);
241 EmitConstant(Value, 4, CurByte, OS);
246 // FIXME: These #defines shouldn't be necessary. Instead, tblgen should
247 // be able to generate code emitter helpers for either variant, like it
248 // does for the AsmWriter.
249 #define MBlazeCodeEmitter MBlazeMCCodeEmitter
250 #define MachineInstr MCInst
251 #include "MBlazeGenCodeEmitter.inc"
252 #undef MBlazeCodeEmitter