blob: 19dc89b37b0642ea9e579d0e26f49d35008bf123 [file] [log] [blame]
Wesley Peck4e9141f2010-10-21 03:57:26 +00001//===-- MBlazeMCCodeEmitter.cpp - Convert MBlaze code to machine code -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the MBlazeMCCodeEmitter class.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2ac19022010-11-15 05:19:05 +000014#define DEBUG_TYPE "mccodeemitter"
Wesley Peck4e9141f2010-10-21 03:57:26 +000015#include "MBlaze.h"
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"
25using namespace llvm;
26
27STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
28
29namespace {
30class 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;
35 MCContext &Ctx;
36
37public:
38 MBlazeMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
39 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
40 }
41
42 ~MBlazeMCCodeEmitter() {}
43
44 // getBinaryCodeForInstr - TableGen'erated function for getting the
45 // binary encoding for an instruction.
46 unsigned getBinaryCodeForInstr(const MCInst &MI) const;
47
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));
53 }
54
55 unsigned getNumFixupKinds() const {
56 return 2;
57 }
58
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 } };
63
64 if (Kind < FirstTargetFixupKind)
65 return MCCodeEmitter::getFixupKindInfo(Kind);
66
67 if (unsigned(Kind-FirstTargetFixupKind) < getNumFixupKinds())
68 return Infos[Kind - FirstTargetFixupKind];
69
70 assert(0 && "Invalid fixup kind.");
71 return Infos[0];
72 }
73
74 static unsigned GetMBlazeRegNum(const MCOperand &MO) {
75 // FIXME: getMBlazeRegisterNumbering() is sufficient?
76 assert(0 && "MBlazeMCCodeEmitter::GetMBlazeRegNum() not yet implemented.");
77 return 0;
78 }
79
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;
85
86 OS << (char)C;
87 ++CurByte;
88 }
89
90 void EmitRawByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
91 OS << (char)C;
92 ++CurByte;
93 }
94
95 void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
96 raw_ostream &OS) const {
Wesley Peck0a67d922010-11-08 19:40:01 +000097 assert(Size <= 8 && "size too big in emit constant");
Wesley Peck4e9141f2010-10-21 03:57:26 +000098
99 for (unsigned i = 0; i != Size; ++i) {
100 EmitByte(Val & 255, CurByte, OS);
101 Val >>= 8;
102 }
103 }
104
105 void EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const;
Wesley Peck0a67d922010-11-08 19:40:01 +0000106 void EmitIMM(const MCInst &MI, unsigned op, unsigned &CurByte,
Wesley Pecka0603832010-10-27 00:23:01 +0000107 raw_ostream &OS) const;
Wesley Peck4e9141f2010-10-21 03:57:26 +0000108
109 void EmitImmediate(const MCInst &MI,
110 unsigned opNo, MCFixupKind FixupKind,
111 unsigned &CurByte, raw_ostream &OS,
112 SmallVectorImpl<MCFixup> &Fixups) const;
113
114 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
115 SmallVectorImpl<MCFixup> &Fixups) const;
116};
117
118} // end anonymous namespace
119
120
121MCCodeEmitter *llvm::createMBlazeMCCodeEmitter(const Target &,
122 TargetMachine &TM,
123 MCContext &Ctx) {
124 return new MBlazeMCCodeEmitter(TM, Ctx);
125}
126
127/// getMachineOpValue - Return binary encoding of operand. If the machine
128/// operand requires relocation, record the relocation and return zero.
129unsigned MBlazeMCCodeEmitter::getMachineOpValue(const MCInst &MI,
130 const MCOperand &MO) const {
131 if (MO.isReg())
132 return MBlazeRegisterInfo::getRegisterNumbering(MO.getReg());
133 else if (MO.isImm())
134 return static_cast<unsigned>(MO.getImm());
Wesley Peck0a67d922010-11-08 19:40:01 +0000135 else if (MO.isExpr())
Wesley Peck4e9141f2010-10-21 03:57:26 +0000136 return 0; // The relocation has already been recorded at this point.
137 else {
138#ifndef NDEBUG
139 errs() << MO;
140#endif
141 llvm_unreachable(0);
142 }
143 return 0;
144}
145
146void MBlazeMCCodeEmitter::
147EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const {
148 int32_t val = (int32_t)imm.getImm();
Wesley Peckef5b3902010-11-11 21:40:53 +0000149 if (val > 32767 || val < -32768) {
Wesley Peck4e9141f2010-10-21 03:57:26 +0000150 EmitByte(0x0D, CurByte, OS);
151 EmitByte(0x00, CurByte, OS);
152 EmitRawByte((val >> 24) & 0xFF, CurByte, OS);
153 EmitRawByte((val >> 16) & 0xFF, CurByte, OS);
154 }
155}
156
157void MBlazeMCCodeEmitter::
Wesley Peck0a67d922010-11-08 19:40:01 +0000158EmitIMM(const MCInst &MI, unsigned op, unsigned &CurByte,
Wesley Pecka0603832010-10-27 00:23:01 +0000159 raw_ostream &OS) const {
160 MCOperand mcop = MI.getOperand(op);
161 if (mcop.isExpr()) {
162 EmitByte(0x0D, CurByte, OS);
163 EmitByte(0x00, CurByte, OS);
164 EmitRawByte(0, CurByte, OS);
165 EmitRawByte(0, CurByte, OS);
166 }
167}
168
169void MBlazeMCCodeEmitter::
Wesley Peck4e9141f2010-10-21 03:57:26 +0000170EmitImmediate(const MCInst &MI, unsigned opNo, MCFixupKind FixupKind,
171 unsigned &CurByte, raw_ostream &OS,
172 SmallVectorImpl<MCFixup> &Fixups) const {
Wesley Peck0a67d922010-11-08 19:40:01 +0000173 assert(MI.getNumOperands()>opNo && "Not enought operands for instruction");
Wesley Peck4e9141f2010-10-21 03:57:26 +0000174
175 MCOperand oper = MI.getOperand(opNo);
176 if (oper.isImm()) {
Wesley Peck0a67d922010-11-08 19:40:01 +0000177 EmitIMM(oper, CurByte, OS);
Wesley Peck4e9141f2010-10-21 03:57:26 +0000178 } else if (oper.isExpr()) {
179 Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
180 }
181}
182
Wesley Pecka0603832010-10-27 00:23:01 +0000183
184
Wesley Peck4e9141f2010-10-21 03:57:26 +0000185void MBlazeMCCodeEmitter::
186EncodeInstruction(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;
193
194 switch ((TSFlags & MBlazeII::FormMask)) {
195 default: break;
Wesley Pecka0603832010-10-27 00:23:01 +0000196 case MBlazeII::FPseudo:
Wesley Peck4e9141f2010-10-21 03:57:26 +0000197 // Pseudo instructions don't get encoded.
198 return;
199
Wesley Pecka0603832010-10-27 00:23:01 +0000200 case MBlazeII::FRRI:
Wesley Peck0a67d922010-11-08 19:40:01 +0000201 EmitImmediate(MI, 2, FK_Data_4, CurByte, OS, Fixups);
Wesley Peck4e9141f2010-10-21 03:57:26 +0000202 break;
203
Wesley Pecka0603832010-10-27 00:23:01 +0000204 case MBlazeII::FRIR:
Wesley Peck0a67d922010-11-08 19:40:01 +0000205 EmitImmediate(MI, 1, FK_Data_4, CurByte, OS, Fixups);
Wesley Peck4e9141f2010-10-21 03:57:26 +0000206 break;
207
Wesley Pecka0603832010-10-27 00:23:01 +0000208 case MBlazeII::FCRI:
Wesley Peck0a67d922010-11-08 19:40:01 +0000209 EmitImmediate(MI, 1, MCFixupKind(MBlaze::reloc_pcrel_2byte), CurByte, OS,
210 Fixups);
Wesley Peck4e9141f2010-10-21 03:57:26 +0000211 break;
212
Wesley Pecka0603832010-10-27 00:23:01 +0000213 case MBlazeII::FRCI:
Wesley Peck0a67d922010-11-08 19:40:01 +0000214 EmitImmediate(MI, 1, MCFixupKind(MBlaze::reloc_pcrel_4byte), CurByte, OS,
215 Fixups);
Wesley Pecka0603832010-10-27 00:23:01 +0000216
217 case MBlazeII::FCCI:
Wesley Peck0a67d922010-11-08 19:40:01 +0000218 EmitImmediate(MI, 0, MCFixupKind(MBlaze::reloc_pcrel_4byte), CurByte, OS,
219 Fixups);
Wesley Peck4e9141f2010-10-21 03:57:26 +0000220 break;
221 }
222
223 ++MCNumEmitted; // Keep track of the # of mi's emitted
224 unsigned Value = getBinaryCodeForInstr(MI);
225 switch (Opcode) {
226 default:
227 EmitConstant(Value, 4, CurByte, OS);
228 break;
229
Wesley Pecka0603832010-10-27 00:23:01 +0000230 case MBlaze::BRLID:
231 case MBlaze::BRALID:
232 EmitIMM(MI,1,CurByte,OS);
233 EmitConstant(Value, 4, CurByte, OS);
234 break;
235
Wesley Peck4e9141f2010-10-21 03:57:26 +0000236 case MBlaze::BRI:
237 case MBlaze::BRAI:
238 case MBlaze::BRID:
239 case MBlaze::BRAID:
Wesley Pecka0603832010-10-27 00:23:01 +0000240 EmitIMM(MI,0,CurByte,OS);
Wesley Peck4e9141f2010-10-21 03:57:26 +0000241 EmitConstant(Value, 4, CurByte, OS);
242 break;
243 }
244}
245
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
253#undef MachineInstr