Implement ELF object file writing support for the MBlaze backend. Its not perfect...
[oota-llvm.git] / lib / Target / MBlaze / MBlazeMCCodeEmitter.cpp
1 //===-- 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
14 #define DEBUG_TYPE "mccodeemitter"
15 #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"
25 using namespace llvm;
26
27 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
28
29 namespace {
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;
35   MCContext &Ctx;
36
37 public:
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       // name                offset  bits    flags
62       { "reloc_pcrel_4byte", 2,      4 * 8,  MCFixupKindInfo::FKF_IsPCRel },
63       { "reloc_pcrel_2byte", 2,      2 * 8,  MCFixupKindInfo::FKF_IsPCRel } };
64
65     if (Kind < FirstTargetFixupKind)
66       return MCCodeEmitter::getFixupKindInfo(Kind);
67
68     if (unsigned(Kind-FirstTargetFixupKind) < getNumFixupKinds())
69       return Infos[Kind - FirstTargetFixupKind];
70
71     assert(0 && "Invalid fixup kind.");
72     return Infos[0];
73   }
74
75   static unsigned GetMBlazeRegNum(const MCOperand &MO) {
76     // FIXME: getMBlazeRegisterNumbering() is sufficient?
77     assert(0 && "MBlazeMCCodeEmitter::GetMBlazeRegNum() not yet implemented.");
78     return 0;
79   }
80
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;
86
87     OS << (char)C;
88     ++CurByte;
89   }
90
91   void EmitRawByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
92     OS << (char)C;
93     ++CurByte;
94   }
95
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");
99
100     for (unsigned i = 0; i != Size; ++i) {
101       EmitByte(Val & 255, CurByte, OS);
102       Val >>= 8;
103     }
104   }
105
106   void EmitIMM(const MCOperand &imm, unsigned &CurByte, raw_ostream &OS) const;
107   void EmitIMM(const MCInst &MI, unsigned &CurByte, raw_ostream &OS) const;
108
109   void EmitImmediate(const MCInst &MI, unsigned opNo, bool pcrel,
110                      unsigned &CurByte, raw_ostream &OS,
111                      SmallVectorImpl<MCFixup> &Fixups) const;
112
113   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
114                          SmallVectorImpl<MCFixup> &Fixups) const;
115 };
116
117 } // end anonymous namespace
118
119
120 MCCodeEmitter *llvm::createMBlazeMCCodeEmitter(const Target &,
121                                                TargetMachine &TM,
122                                                MCContext &Ctx) {
123   return new MBlazeMCCodeEmitter(TM, Ctx);
124 }
125
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 {
130   if (MO.isReg())
131     return MBlazeRegisterInfo::getRegisterNumbering(MO.getReg());
132   else if (MO.isImm())
133     return static_cast<unsigned>(MO.getImm());
134   else if (MO.isExpr())
135       return 0; // The relocation has already been recorded at this point.
136   else {
137 #ifndef NDEBUG
138     errs() << MO;
139 #endif
140     llvm_unreachable(0);
141   }
142   return 0;
143 }
144
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);
153   }
154 }
155
156 void MBlazeMCCodeEmitter::
157 EmitIMM(const MCInst &MI, unsigned &CurByte,raw_ostream &OS) const {
158   switch (MI.getOpcode()) {
159   default: break;
160
161   case MBlaze::ADDI32:
162   case MBlaze::ORI32:
163   case MBlaze::BRLID32:
164     EmitByte(0x0D, CurByte, OS);
165     EmitByte(0x00, CurByte, OS);
166     EmitRawByte(0, CurByte, OS);
167     EmitRawByte(0, CurByte, OS);
168   }
169 }
170
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");
175
176   MCOperand oper = MI.getOperand(opNo);
177
178   if (oper.isImm()) {
179     EmitIMM(oper, CurByte, OS);
180   } else if (oper.isExpr()) {
181     MCFixupKind FixupKind;
182     switch (MI.getOpcode()) {
183     default:
184       FixupKind = pcrel ? MCFixupKind(MBlaze::reloc_pcrel_2byte) : FK_Data_2;
185       Fixups.push_back(MCFixup::Create(0,oper.getExpr(),FixupKind));
186       break;
187     case MBlaze::ORI32:
188     case MBlaze::ADDI32:
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));
192       break;
193     }
194   }
195 }
196
197
198
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;
207
208   // Emit an IMM instruction if the instruction we are encoding requires it
209   EmitIMM(MI,CurByte,OS);
210
211   switch ((TSFlags & MBlazeII::FormMask)) {
212   default: break;
213   case MBlazeII::FPseudo:
214     // Pseudo instructions don't get encoded.
215     return;
216   case MBlazeII::FRRI:
217     EmitImmediate(MI, 2, false, CurByte, OS, Fixups);
218     break;
219   case MBlazeII::FRIR:
220     EmitImmediate(MI, 1, false, CurByte, OS, Fixups);
221     break;
222   case MBlazeII::FCRI:
223     EmitImmediate(MI, 1, true, CurByte, OS, Fixups);
224     break;
225   case MBlazeII::FRCI:
226     EmitImmediate(MI, 1, true, CurByte, OS, Fixups);
227   case MBlazeII::FCCI:
228     EmitImmediate(MI, 0, true, CurByte, OS, Fixups);
229     break;
230   }
231
232   ++MCNumEmitted;  // Keep track of the # of mi's emitted
233   unsigned Value = getBinaryCodeForInstr(MI);
234   EmitConstant(Value, 4, CurByte, OS);
235 }
236
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
244 #undef MachineInstr