blob: e7fb788b3c71347fb560c1b4895d16a7b2a8da08 [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"
Wesley Peck4e9141f2010-10-21 03:57:26 +000017#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"
24using namespace llvm;
25
26STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
27
28namespace {
29class MBlazeMCCodeEmitter : public MCCodeEmitter {
30 MBlazeMCCodeEmitter(const MBlazeMCCodeEmitter &); // DO NOT IMPLEMENT
31 void operator=(const MBlazeMCCodeEmitter &); // DO NOT IMPLEMENT
32 const TargetMachine &TM;
33 const TargetInstrInfo &TII;
34 MCContext &Ctx;
35
36public:
37 MBlazeMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
38 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
39 }
40
41 ~MBlazeMCCodeEmitter() {}
42
43 // getBinaryCodeForInstr - TableGen'erated function for getting the
44 // binary encoding for an instruction.
45 unsigned getBinaryCodeForInstr(const MCInst &MI) const;
46
47 /// getMachineOpValue - Return binary encoding of operand. If the machine
48 /// operand requires relocation, record the relocation and return zero.
49 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO) const;
50 unsigned getMachineOpValue(const MCInst &MI, unsigned OpIdx) const {
51 return getMachineOpValue(MI, MI.getOperand(OpIdx));
52 }
53
54 unsigned getNumFixupKinds() const {
55 return 2;
56 }
57
Wesley Peck4e9141f2010-10-21 03:57:26 +000058 static unsigned GetMBlazeRegNum(const MCOperand &MO) {
59 // FIXME: getMBlazeRegisterNumbering() is sufficient?
60 assert(0 && "MBlazeMCCodeEmitter::GetMBlazeRegNum() not yet implemented.");
61 return 0;
62 }
63
64 void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
65 // The MicroBlaze uses a bit reversed format so we need to reverse the
66 // order of the bits. Taken from:
67 // http://graphics.stanford.edu/~seander/bithacks.html
68 C = ((C * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32;
69
70 OS << (char)C;
71 ++CurByte;
72 }
73
74 void EmitRawByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
75 OS << (char)C;
76 ++CurByte;
77 }
78
79 void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
80 raw_ostream &OS) const {
Wesley Peck0a67d922010-11-08 19:40:01 +000081 assert(Size <= 8 && "size too big in emit constant");
Wesley Peck4e9141f2010-10-21 03:57:26 +000082
83 for (unsigned i = 0; i != Size; ++i) {
84 EmitByte(Val & 255, CurByte, OS);
85 Val >>= 8;
86 }
87 }
88
89 void EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const;
Wesley Peck4b047132010-11-21 22:06:28 +000090 void EmitIMM(const MCInst &MI, unsigned &CurByte, raw_ostream &OS) const;
Wesley Peck4e9141f2010-10-21 03:57:26 +000091
Wesley Peck4b047132010-11-21 22:06:28 +000092 void EmitImmediate(const MCInst &MI, unsigned opNo, bool pcrel,
Wesley Peck4e9141f2010-10-21 03:57:26 +000093 unsigned &CurByte, raw_ostream &OS,
94 SmallVectorImpl<MCFixup> &Fixups) const;
95
96 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
97 SmallVectorImpl<MCFixup> &Fixups) const;
98};
99
100} // end anonymous namespace
101
102
103MCCodeEmitter *llvm::createMBlazeMCCodeEmitter(const Target &,
104 TargetMachine &TM,
105 MCContext &Ctx) {
106 return new MBlazeMCCodeEmitter(TM, Ctx);
107}
108
109/// getMachineOpValue - Return binary encoding of operand. If the machine
110/// operand requires relocation, record the relocation and return zero.
111unsigned MBlazeMCCodeEmitter::getMachineOpValue(const MCInst &MI,
112 const MCOperand &MO) const {
113 if (MO.isReg())
114 return MBlazeRegisterInfo::getRegisterNumbering(MO.getReg());
115 else if (MO.isImm())
116 return static_cast<unsigned>(MO.getImm());
Wesley Peck0a67d922010-11-08 19:40:01 +0000117 else if (MO.isExpr())
Wesley Peck4e9141f2010-10-21 03:57:26 +0000118 return 0; // The relocation has already been recorded at this point.
119 else {
120#ifndef NDEBUG
121 errs() << MO;
122#endif
123 llvm_unreachable(0);
124 }
125 return 0;
126}
127
128void MBlazeMCCodeEmitter::
129EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const {
130 int32_t val = (int32_t)imm.getImm();
Wesley Peckef5b3902010-11-11 21:40:53 +0000131 if (val > 32767 || val < -32768) {
Wesley Peck4e9141f2010-10-21 03:57:26 +0000132 EmitByte(0x0D, CurByte, OS);
133 EmitByte(0x00, CurByte, OS);
134 EmitRawByte((val >> 24) & 0xFF, CurByte, OS);
135 EmitRawByte((val >> 16) & 0xFF, CurByte, OS);
136 }
137}
138
139void MBlazeMCCodeEmitter::
Wesley Peck4b047132010-11-21 22:06:28 +0000140EmitIMM(const MCInst &MI, unsigned &CurByte,raw_ostream &OS) const {
141 switch (MI.getOpcode()) {
142 default: break;
143
144 case MBlaze::ADDI32:
145 case MBlaze::ORI32:
146 case MBlaze::BRLID32:
147 EmitByte(0x0D, CurByte, OS);
148 EmitByte(0x00, CurByte, OS);
149 EmitRawByte(0, CurByte, OS);
150 EmitRawByte(0, CurByte, OS);
151 }
Wesley Pecka0603832010-10-27 00:23:01 +0000152}
153
154void MBlazeMCCodeEmitter::
Wesley Peck4b047132010-11-21 22:06:28 +0000155EmitImmediate(const MCInst &MI, unsigned opNo, bool pcrel, unsigned &CurByte,
156 raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups) const {
Wesley Peck0a67d922010-11-08 19:40:01 +0000157 assert(MI.getNumOperands()>opNo && "Not enought operands for instruction");
Wesley Peck4e9141f2010-10-21 03:57:26 +0000158
159 MCOperand oper = MI.getOperand(opNo);
Wesley Peck4b047132010-11-21 22:06:28 +0000160
Wesley Peck4e9141f2010-10-21 03:57:26 +0000161 if (oper.isImm()) {
Wesley Peck4b047132010-11-21 22:06:28 +0000162 EmitIMM(oper, CurByte, OS);
Wesley Peck4e9141f2010-10-21 03:57:26 +0000163 } else if (oper.isExpr()) {
Wesley Peck4b047132010-11-21 22:06:28 +0000164 MCFixupKind FixupKind;
165 switch (MI.getOpcode()) {
166 default:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +0000167 FixupKind = pcrel ? FK_PCRel_2 : FK_Data_2;
Wesley Peck4e9141f2010-10-21 03:57:26 +0000168 Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
Wesley Peck4b047132010-11-21 22:06:28 +0000169 break;
170 case MBlaze::ORI32:
171 case MBlaze::ADDI32:
172 case MBlaze::BRLID32:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +0000173 FixupKind = pcrel ? FK_PCRel_4 : FK_Data_4;
Wesley Peck4b047132010-11-21 22:06:28 +0000174 Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
175 break;
176 }
Wesley Peck4e9141f2010-10-21 03:57:26 +0000177 }
178}
179
Wesley Pecka0603832010-10-27 00:23:01 +0000180
181
Wesley Peck4e9141f2010-10-21 03:57:26 +0000182void MBlazeMCCodeEmitter::
183EncodeInstruction(const MCInst &MI, raw_ostream &OS,
184 SmallVectorImpl<MCFixup> &Fixups) const {
185 unsigned Opcode = MI.getOpcode();
186 const TargetInstrDesc &Desc = TII.get(Opcode);
187 uint64_t TSFlags = Desc.TSFlags;
188 // Keep track of the current byte being emitted.
189 unsigned CurByte = 0;
190
Wesley Peck4b047132010-11-21 22:06:28 +0000191 // Emit an IMM instruction if the instruction we are encoding requires it
192 EmitIMM(MI,CurByte,OS);
193
Wesley Peck4e9141f2010-10-21 03:57:26 +0000194 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;
Wesley Pecka0603832010-10-27 00:23:01 +0000199 case MBlazeII::FRRI:
Wesley Peck4b047132010-11-21 22:06:28 +0000200 EmitImmediate(MI, 2, false, CurByte, OS, Fixups);
Wesley Peck4e9141f2010-10-21 03:57:26 +0000201 break;
Wesley Pecka0603832010-10-27 00:23:01 +0000202 case MBlazeII::FRIR:
Wesley Peck4b047132010-11-21 22:06:28 +0000203 EmitImmediate(MI, 1, false, CurByte, OS, Fixups);
Wesley Peck4e9141f2010-10-21 03:57:26 +0000204 break;
Wesley Pecka0603832010-10-27 00:23:01 +0000205 case MBlazeII::FCRI:
Wesley Peck4b047132010-11-21 22:06:28 +0000206 EmitImmediate(MI, 1, true, CurByte, OS, Fixups);
Wesley Peck4e9141f2010-10-21 03:57:26 +0000207 break;
Wesley Pecka0603832010-10-27 00:23:01 +0000208 case MBlazeII::FRCI:
Wesley Peck4b047132010-11-21 22:06:28 +0000209 EmitImmediate(MI, 1, true, CurByte, OS, Fixups);
Wesley Pecka0603832010-10-27 00:23:01 +0000210 case MBlazeII::FCCI:
Wesley Peck4b047132010-11-21 22:06:28 +0000211 EmitImmediate(MI, 0, true, CurByte, OS, Fixups);
Wesley Peck4e9141f2010-10-21 03:57:26 +0000212 break;
213 }
214
215 ++MCNumEmitted; // Keep track of the # of mi's emitted
216 unsigned Value = getBinaryCodeForInstr(MI);
Wesley Peck4b047132010-11-21 22:06:28 +0000217 EmitConstant(Value, 4, CurByte, OS);
Wesley Peck4e9141f2010-10-21 03:57:26 +0000218}
219
220// FIXME: These #defines shouldn't be necessary. Instead, tblgen should
221// be able to generate code emitter helpers for either variant, like it
222// does for the AsmWriter.
223#define MBlazeCodeEmitter MBlazeMCCodeEmitter
224#define MachineInstr MCInst
225#include "MBlazeGenCodeEmitter.inc"
226#undef MBlazeCodeEmitter
227#undef MachineInstr