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 "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/MC/MCFixup.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Support/raw_ostream.h"
26 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
29 class MBlazeMCCodeEmitter : public MCCodeEmitter {
30 MBlazeMCCodeEmitter(const MBlazeMCCodeEmitter &); // DO NOT IMPLEMENT
31 void operator=(const MBlazeMCCodeEmitter &); // DO NOT IMPLEMENT
32 const MCInstrInfo &MCII;
35 MBlazeMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
40 ~MBlazeMCCodeEmitter() {}
42 // getBinaryCodeForInstr - TableGen'erated function for getting the
43 // binary encoding for an instruction.
44 unsigned getBinaryCodeForInstr(const MCInst &MI) const;
46 /// getMachineOpValue - Return binary encoding of operand. If the machine
47 /// operand requires relocation, record the relocation and return zero.
48 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO) const;
49 unsigned getMachineOpValue(const MCInst &MI, unsigned OpIdx) const {
50 return getMachineOpValue(MI, MI.getOperand(OpIdx));
53 static unsigned GetMBlazeRegNum(const MCOperand &MO) {
54 // FIXME: getMBlazeRegisterNumbering() is sufficient?
55 assert(0 && "MBlazeMCCodeEmitter::GetMBlazeRegNum() not yet implemented.");
59 void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
60 // The MicroBlaze uses a bit reversed format so we need to reverse the
61 // order of the bits. Taken from:
62 // http://graphics.stanford.edu/~seander/bithacks.html
63 C = ((C * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32;
69 void EmitRawByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
74 void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
75 raw_ostream &OS) const {
76 assert(Size <= 8 && "size too big in emit constant");
78 for (unsigned i = 0; i != Size; ++i) {
79 EmitByte(Val & 255, CurByte, OS);
84 void EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const;
85 void EmitIMM(const MCInst &MI, unsigned &CurByte, raw_ostream &OS) const;
87 void EmitImmediate(const MCInst &MI, unsigned opNo, bool pcrel,
88 unsigned &CurByte, raw_ostream &OS,
89 SmallVectorImpl<MCFixup> &Fixups) const;
91 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
92 SmallVectorImpl<MCFixup> &Fixups) const;
95 } // end anonymous namespace
98 MCCodeEmitter *llvm::createMBlazeMCCodeEmitter(const MCInstrInfo &MCII,
99 const MCSubtargetInfo &STI,
101 return new MBlazeMCCodeEmitter(MCII, STI, Ctx);
104 /// getMachineOpValue - Return binary encoding of operand. If the machine
105 /// operand requires relocation, record the relocation and return zero.
106 unsigned MBlazeMCCodeEmitter::getMachineOpValue(const MCInst &MI,
107 const MCOperand &MO) const {
109 return MBlazeRegisterInfo::getRegisterNumbering(MO.getReg());
111 return static_cast<unsigned>(MO.getImm());
112 else if (MO.isExpr())
113 return 0; // The relocation has already been recorded at this point.
123 void MBlazeMCCodeEmitter::
124 EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const {
125 int32_t val = (int32_t)imm.getImm();
126 if (val > 32767 || val < -32768) {
127 EmitByte(0x0D, CurByte, OS);
128 EmitByte(0x00, CurByte, OS);
129 EmitRawByte((val >> 24) & 0xFF, CurByte, OS);
130 EmitRawByte((val >> 16) & 0xFF, CurByte, OS);
134 void MBlazeMCCodeEmitter::
135 EmitIMM(const MCInst &MI, unsigned &CurByte,raw_ostream &OS) const {
136 switch (MI.getOpcode()) {
139 case MBlaze::ADDIK32:
141 case MBlaze::BRLID32:
142 EmitByte(0x0D, CurByte, OS);
143 EmitByte(0x00, CurByte, OS);
144 EmitRawByte(0, CurByte, OS);
145 EmitRawByte(0, CurByte, OS);
149 void MBlazeMCCodeEmitter::
150 EmitImmediate(const MCInst &MI, unsigned opNo, bool pcrel, unsigned &CurByte,
151 raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups) const {
152 assert(MI.getNumOperands()>opNo && "Not enought operands for instruction");
154 MCOperand oper = MI.getOperand(opNo);
157 EmitIMM(oper, CurByte, OS);
158 } else if (oper.isExpr()) {
159 MCFixupKind FixupKind;
160 switch (MI.getOpcode()) {
162 FixupKind = pcrel ? FK_PCRel_2 : FK_Data_2;
163 Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
166 case MBlaze::ADDIK32:
167 case MBlaze::BRLID32:
168 FixupKind = pcrel ? FK_PCRel_4 : FK_Data_4;
169 Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
177 void MBlazeMCCodeEmitter::
178 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
179 SmallVectorImpl<MCFixup> &Fixups) const {
180 unsigned Opcode = MI.getOpcode();
181 const MCInstrDesc &Desc = MCII.get(Opcode);
182 uint64_t TSFlags = Desc.TSFlags;
183 // Keep track of the current byte being emitted.
184 unsigned CurByte = 0;
186 // Emit an IMM instruction if the instruction we are encoding requires it
187 EmitIMM(MI,CurByte,OS);
189 switch ((TSFlags & MBlazeII::FormMask)) {
191 case MBlazeII::FPseudo:
192 // Pseudo instructions don't get encoded.
195 EmitImmediate(MI, 2, false, CurByte, OS, Fixups);
198 EmitImmediate(MI, 1, false, CurByte, OS, Fixups);
201 EmitImmediate(MI, 1, true, CurByte, OS, Fixups);
204 EmitImmediate(MI, 1, true, CurByte, OS, Fixups);
206 EmitImmediate(MI, 0, true, CurByte, OS, Fixups);
210 ++MCNumEmitted; // Keep track of the # of mi's emitted
211 unsigned Value = getBinaryCodeForInstr(MI);
212 EmitConstant(Value, 4, CurByte, OS);
215 // FIXME: These #defines shouldn't be necessary. Instead, tblgen should
216 // be able to generate code emitter helpers for either variant, like it
217 // does for the AsmWriter.
218 #define MBlazeCodeEmitter MBlazeMCCodeEmitter
219 #define MachineInstr MCInst
220 #include "MBlazeGenCodeEmitter.inc"
221 #undef MBlazeCodeEmitter