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 uint64_t 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());
115 return 0; // The relocation has already been recorded at this point.
122 void MBlazeMCCodeEmitter::
123 EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const {
124 int32_t val = (int32_t)imm.getImm();
125 if (val > 32767 || val < -32768) {
126 EmitByte(0x0D, CurByte, OS);
127 EmitByte(0x00, CurByte, OS);
128 EmitRawByte((val >> 24) & 0xFF, CurByte, OS);
129 EmitRawByte((val >> 16) & 0xFF, CurByte, OS);
133 void MBlazeMCCodeEmitter::
134 EmitIMM(const MCInst &MI, unsigned &CurByte,raw_ostream &OS) const {
135 switch (MI.getOpcode()) {
138 case MBlaze::ADDIK32:
140 case MBlaze::BRLID32:
141 EmitByte(0x0D, CurByte, OS);
142 EmitByte(0x00, CurByte, OS);
143 EmitRawByte(0, CurByte, OS);
144 EmitRawByte(0, CurByte, OS);
148 void MBlazeMCCodeEmitter::
149 EmitImmediate(const MCInst &MI, unsigned opNo, bool pcrel, unsigned &CurByte,
150 raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups) const {
151 assert(MI.getNumOperands()>opNo && "Not enought operands for instruction");
153 MCOperand oper = MI.getOperand(opNo);
156 EmitIMM(oper, CurByte, OS);
157 } else if (oper.isExpr()) {
158 MCFixupKind FixupKind;
159 switch (MI.getOpcode()) {
161 FixupKind = pcrel ? FK_PCRel_2 : FK_Data_2;
162 Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
165 case MBlaze::ADDIK32:
166 case MBlaze::BRLID32:
167 FixupKind = pcrel ? FK_PCRel_4 : FK_Data_4;
168 Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
176 void MBlazeMCCodeEmitter::
177 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
178 SmallVectorImpl<MCFixup> &Fixups) const {
179 unsigned Opcode = MI.getOpcode();
180 const MCInstrDesc &Desc = MCII.get(Opcode);
181 uint64_t TSFlags = Desc.TSFlags;
182 // Keep track of the current byte being emitted.
183 unsigned CurByte = 0;
185 // Emit an IMM instruction if the instruction we are encoding requires it
186 EmitIMM(MI,CurByte,OS);
188 switch ((TSFlags & MBlazeII::FormMask)) {
190 case MBlazeII::FPseudo:
191 // Pseudo instructions don't get encoded.
194 EmitImmediate(MI, 2, false, CurByte, OS, Fixups);
197 EmitImmediate(MI, 1, false, CurByte, OS, Fixups);
200 EmitImmediate(MI, 1, true, CurByte, OS, Fixups);
203 EmitImmediate(MI, 1, true, CurByte, OS, Fixups);
205 EmitImmediate(MI, 0, true, CurByte, OS, Fixups);
209 ++MCNumEmitted; // Keep track of the # of mi's emitted
210 unsigned Value = getBinaryCodeForInstr(MI);
211 EmitConstant(Value, 4, CurByte, OS);
214 // FIXME: These #defines shouldn't be necessary. Instead, tblgen should
215 // be able to generate code emitter helpers for either variant, like it
216 // does for the AsmWriter.
217 #define MBlazeCodeEmitter MBlazeMCCodeEmitter
218 #define MachineInstr MCInst
219 #include "MBlazeGenCodeEmitter.inc"
220 #undef MBlazeCodeEmitter