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"
15 #include "MCTargetDesc/MBlazeBaseInfo.h"
16 #include "MCTargetDesc/MBlazeMCTargetDesc.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCInstrInfo.h"
21 #include "llvm/MC/MCSubtargetInfo.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/MC/MCFixup.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Support/raw_ostream.h"
28 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
31 class MBlazeMCCodeEmitter : public MCCodeEmitter {
32 MBlazeMCCodeEmitter(const MBlazeMCCodeEmitter &); // DO NOT IMPLEMENT
33 void operator=(const MBlazeMCCodeEmitter &); // DO NOT IMPLEMENT
34 const MCInstrInfo &MCII;
37 MBlazeMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
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 static unsigned GetMBlazeRegNum(const MCOperand &MO) {
56 // FIXME: getMBlazeRegisterNumbering() is sufficient?
57 assert(0 && "MBlazeMCCodeEmitter::GetMBlazeRegNum() not yet implemented.");
61 void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
62 // The MicroBlaze uses a bit reversed format so we need to reverse the
63 // order of the bits. Taken from:
64 // http://graphics.stanford.edu/~seander/bithacks.html
65 C = ((C * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32;
71 void EmitRawByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
76 void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
77 raw_ostream &OS) const {
78 assert(Size <= 8 && "size too big in emit constant");
80 for (unsigned i = 0; i != Size; ++i) {
81 EmitByte(Val & 255, CurByte, OS);
86 void EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const;
87 void EmitIMM(const MCInst &MI, unsigned &CurByte, raw_ostream &OS) const;
89 void EmitImmediate(const MCInst &MI, unsigned opNo, bool pcrel,
90 unsigned &CurByte, raw_ostream &OS,
91 SmallVectorImpl<MCFixup> &Fixups) const;
93 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
94 SmallVectorImpl<MCFixup> &Fixups) const;
97 } // end anonymous namespace
100 MCCodeEmitter *llvm::createMBlazeMCCodeEmitter(const MCInstrInfo &MCII,
101 const MCSubtargetInfo &STI,
103 return new MBlazeMCCodeEmitter(MCII, STI, Ctx);
106 /// getMachineOpValue - Return binary encoding of operand. If the machine
107 /// operand requires relocation, record the relocation and return zero.
108 unsigned MBlazeMCCodeEmitter::getMachineOpValue(const MCInst &MI,
109 const MCOperand &MO) const {
111 return getMBlazeRegisterNumbering(MO.getReg());
113 return static_cast<unsigned>(MO.getImm());
114 else if (MO.isExpr())
115 return 0; // The relocation has already been recorded at this point.
125 void MBlazeMCCodeEmitter::
126 EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const {
127 int32_t val = (int32_t)imm.getImm();
128 if (val > 32767 || val < -32768) {
129 EmitByte(0x0D, CurByte, OS);
130 EmitByte(0x00, CurByte, OS);
131 EmitRawByte((val >> 24) & 0xFF, CurByte, OS);
132 EmitRawByte((val >> 16) & 0xFF, CurByte, OS);
136 void MBlazeMCCodeEmitter::
137 EmitIMM(const MCInst &MI, unsigned &CurByte,raw_ostream &OS) const {
138 switch (MI.getOpcode()) {
141 case MBlaze::ADDIK32:
143 case MBlaze::BRLID32:
144 EmitByte(0x0D, CurByte, OS);
145 EmitByte(0x00, CurByte, OS);
146 EmitRawByte(0, CurByte, OS);
147 EmitRawByte(0, CurByte, OS);
151 void MBlazeMCCodeEmitter::
152 EmitImmediate(const MCInst &MI, unsigned opNo, bool pcrel, unsigned &CurByte,
153 raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups) const {
154 assert(MI.getNumOperands()>opNo && "Not enought operands for instruction");
156 MCOperand oper = MI.getOperand(opNo);
159 EmitIMM(oper, CurByte, OS);
160 } else if (oper.isExpr()) {
161 MCFixupKind FixupKind;
162 switch (MI.getOpcode()) {
164 FixupKind = pcrel ? FK_PCRel_2 : FK_Data_2;
165 Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
168 case MBlaze::ADDIK32:
169 case MBlaze::BRLID32:
170 FixupKind = pcrel ? FK_PCRel_4 : FK_Data_4;
171 Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
179 void MBlazeMCCodeEmitter::
180 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
181 SmallVectorImpl<MCFixup> &Fixups) const {
182 unsigned Opcode = MI.getOpcode();
183 const MCInstrDesc &Desc = MCII.get(Opcode);
184 uint64_t TSFlags = Desc.TSFlags;
185 // Keep track of the current byte being emitted.
186 unsigned CurByte = 0;
188 // Emit an IMM instruction if the instruction we are encoding requires it
189 EmitIMM(MI,CurByte,OS);
191 switch ((TSFlags & MBlazeII::FormMask)) {
193 case MBlazeII::FPseudo:
194 // Pseudo instructions don't get encoded.
197 EmitImmediate(MI, 2, false, CurByte, OS, Fixups);
200 EmitImmediate(MI, 1, false, CurByte, OS, Fixups);
203 EmitImmediate(MI, 1, true, CurByte, OS, Fixups);
206 EmitImmediate(MI, 1, true, CurByte, OS, Fixups);
208 EmitImmediate(MI, 0, true, CurByte, OS, Fixups);
212 ++MCNumEmitted; // Keep track of the # of mi's emitted
213 unsigned Value = getBinaryCodeForInstr(MI);
214 EmitConstant(Value, 4, CurByte, OS);
217 // FIXME: These #defines shouldn't be necessary. Instead, tblgen should
218 // be able to generate code emitter helpers for either variant, like it
219 // does for the AsmWriter.
220 #define MBlazeCodeEmitter MBlazeMCCodeEmitter
221 #define MachineInstr MCInst
222 #include "MBlazeGenCodeEmitter.inc"
223 #undef MBlazeCodeEmitter