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 // name offset bits flags
62 { "reloc_pcrel_4byte", 2, 4 * 8, MCFixupKindInfo::FKF_IsPCRel },
63 { "reloc_pcrel_2byte", 2, 2 * 8, MCFixupKindInfo::FKF_IsPCRel } };
65 if (Kind < FirstTargetFixupKind)
66 return MCCodeEmitter::getFixupKindInfo(Kind);
68 if (unsigned(Kind-FirstTargetFixupKind) < getNumFixupKinds())
69 return Infos[Kind - FirstTargetFixupKind];
71 assert(0 && "Invalid fixup kind.");
75 static unsigned GetMBlazeRegNum(const MCOperand &MO) {
76 // FIXME: getMBlazeRegisterNumbering() is sufficient?
77 assert(0 && "MBlazeMCCodeEmitter::GetMBlazeRegNum() not yet implemented.");
81 void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
82 // The MicroBlaze uses a bit reversed format so we need to reverse the
83 // order of the bits. Taken from:
84 // http://graphics.stanford.edu/~seander/bithacks.html
85 C = ((C * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32;
91 void EmitRawByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
96 void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
97 raw_ostream &OS) const {
98 assert(Size <= 8 && "size too big in emit constant");
100 for (unsigned i = 0; i != Size; ++i) {
101 EmitByte(Val & 255, CurByte, OS);
106 void EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const;
107 void EmitIMM(const MCInst &MI, unsigned &CurByte, raw_ostream &OS) const;
109 void EmitImmediate(const MCInst &MI, unsigned opNo, bool pcrel,
110 unsigned &CurByte, raw_ostream &OS,
111 SmallVectorImpl<MCFixup> &Fixups) const;
113 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
114 SmallVectorImpl<MCFixup> &Fixups) const;
117 } // end anonymous namespace
120 MCCodeEmitter *llvm::createMBlazeMCCodeEmitter(const Target &,
123 return new MBlazeMCCodeEmitter(TM, Ctx);
126 /// getMachineOpValue - Return binary encoding of operand. If the machine
127 /// operand requires relocation, record the relocation and return zero.
128 unsigned MBlazeMCCodeEmitter::getMachineOpValue(const MCInst &MI,
129 const MCOperand &MO) const {
131 return MBlazeRegisterInfo::getRegisterNumbering(MO.getReg());
133 return static_cast<unsigned>(MO.getImm());
134 else if (MO.isExpr())
135 return 0; // The relocation has already been recorded at this point.
145 void MBlazeMCCodeEmitter::
146 EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const {
147 int32_t val = (int32_t)imm.getImm();
148 if (val > 32767 || val < -32768) {
149 EmitByte(0x0D, CurByte, OS);
150 EmitByte(0x00, CurByte, OS);
151 EmitRawByte((val >> 24) & 0xFF, CurByte, OS);
152 EmitRawByte((val >> 16) & 0xFF, CurByte, OS);
156 void MBlazeMCCodeEmitter::
157 EmitIMM(const MCInst &MI, unsigned &CurByte,raw_ostream &OS) const {
158 switch (MI.getOpcode()) {
163 case MBlaze::BRLID32:
164 EmitByte(0x0D, CurByte, OS);
165 EmitByte(0x00, CurByte, OS);
166 EmitRawByte(0, CurByte, OS);
167 EmitRawByte(0, CurByte, OS);
171 void MBlazeMCCodeEmitter::
172 EmitImmediate(const MCInst &MI, unsigned opNo, bool pcrel, unsigned &CurByte,
173 raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups) const {
174 assert(MI.getNumOperands()>opNo && "Not enought operands for instruction");
176 MCOperand oper = MI.getOperand(opNo);
179 EmitIMM(oper, CurByte, OS);
180 } else if (oper.isExpr()) {
181 MCFixupKind FixupKind;
182 switch (MI.getOpcode()) {
184 FixupKind = pcrel ? MCFixupKind(MBlaze::reloc_pcrel_2byte) : FK_Data_2;
185 Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
189 case MBlaze::BRLID32:
190 FixupKind = pcrel ? MCFixupKind(MBlaze::reloc_pcrel_4byte) : FK_Data_4;
191 Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
199 void MBlazeMCCodeEmitter::
200 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
201 SmallVectorImpl<MCFixup> &Fixups) const {
202 unsigned Opcode = MI.getOpcode();
203 const TargetInstrDesc &Desc = TII.get(Opcode);
204 uint64_t TSFlags = Desc.TSFlags;
205 // Keep track of the current byte being emitted.
206 unsigned CurByte = 0;
208 // Emit an IMM instruction if the instruction we are encoding requires it
209 EmitIMM(MI,CurByte,OS);
211 switch ((TSFlags & MBlazeII::FormMask)) {
213 case MBlazeII::FPseudo:
214 // Pseudo instructions don't get encoded.
217 EmitImmediate(MI, 2, false, CurByte, OS, Fixups);
220 EmitImmediate(MI, 1, false, CurByte, OS, Fixups);
223 EmitImmediate(MI, 1, true, CurByte, OS, Fixups);
226 EmitImmediate(MI, 1, true, CurByte, OS, Fixups);
228 EmitImmediate(MI, 0, true, CurByte, OS, Fixups);
232 ++MCNumEmitted; // Keep track of the # of mi's emitted
233 unsigned Value = getBinaryCodeForInstr(MI);
234 EmitConstant(Value, 4, CurByte, OS);
237 // FIXME: These #defines shouldn't be necessary. Instead, tblgen should
238 // be able to generate code emitter helpers for either variant, like it
239 // does for the AsmWriter.
240 #define MBlazeCodeEmitter MBlazeMCCodeEmitter
241 #define MachineInstr MCInst
242 #include "MBlazeGenCodeEmitter.inc"
243 #undef MBlazeCodeEmitter