Improve r172471: avoid all those extra casts on the lines nearby
[oota-llvm.git] / lib / Target / Mips / MipsCodeEmitter.cpp
1 //===-- Mips/MipsCodeEmitter.cpp - Convert Mips 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 contains the pass that transforms the Mips machine instructions
11 // into relocatable machine code.
12 //
13 //===---------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "jit"
16 #include "Mips.h"
17 #include "MCTargetDesc/MipsBaseInfo.h"
18 #include "MipsInstrInfo.h"
19 #include "MipsRelocations.h"
20 #include "MipsSubtarget.h"
21 #include "MipsTargetMachine.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/CodeGen/JITCodeEmitter.h"
24 #include "llvm/CodeGen/MachineConstantPool.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/CodeGen/MachineJumpTableInfo.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineOperand.h"
30 #include "llvm/CodeGen/Passes.h"
31 #include "llvm/IR/Constants.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/PassManager.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h"
37 #ifndef NDEBUG
38 #include <iomanip>
39 #endif
40
41 using namespace llvm;
42
43 STATISTIC(NumEmitted, "Number of machine instructions emitted");
44
45 namespace {
46
47 class MipsCodeEmitter : public MachineFunctionPass {
48   MipsJITInfo *JTI;
49   const MipsInstrInfo *II;
50   const DataLayout *TD;
51   const MipsSubtarget *Subtarget;
52   TargetMachine &TM;
53   JITCodeEmitter &MCE;
54   const std::vector<MachineConstantPoolEntry> *MCPEs;
55   const std::vector<MachineJumpTableEntry> *MJTEs;
56   bool IsPIC;
57
58   void getAnalysisUsage(AnalysisUsage &AU) const {
59     AU.addRequired<MachineModuleInfo> ();
60     MachineFunctionPass::getAnalysisUsage(AU);
61   }
62
63   static char ID;
64
65   public:
66     MipsCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce) :
67       MachineFunctionPass(ID), JTI(0),
68       II((const MipsInstrInfo *) tm.getInstrInfo()),
69       TD(tm.getDataLayout()), TM(tm), MCE(mce), MCPEs(0), MJTEs(0),
70       IsPIC(TM.getRelocationModel() == Reloc::PIC_) {
71     }
72
73     bool runOnMachineFunction(MachineFunction &MF);
74
75     virtual const char *getPassName() const {
76       return "Mips Machine Code Emitter";
77     }
78
79     /// getBinaryCodeForInstr - This function, generated by the
80     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
81     /// machine instructions.
82     uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const;
83
84     void emitInstruction(const MachineInstr &MI);
85
86   private:
87
88     void emitWord(unsigned Word);
89
90     /// Routines that handle operands which add machine relocations which are
91     /// fixed up by the relocation stage.
92     void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
93                            bool MayNeedFarStub) const;
94     void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
95     void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
96     void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const;
97     void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc) const;
98
99     /// getMachineOpValue - Return binary encoding of operand. If the machine
100     /// operand requires relocation, record the relocation and return zero.
101     unsigned getMachineOpValue(const MachineInstr &MI,
102                                const MachineOperand &MO) const;
103
104     unsigned getRelocation(const MachineInstr &MI,
105                            const MachineOperand &MO) const;
106
107     unsigned getJumpTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
108
109     unsigned getBranchTargetOpValue(const MachineInstr &MI,
110                                     unsigned OpNo) const;
111     unsigned getMemEncoding(const MachineInstr &MI, unsigned OpNo) const;
112     unsigned getSizeExtEncoding(const MachineInstr &MI, unsigned OpNo) const;
113     unsigned getSizeInsEncoding(const MachineInstr &MI, unsigned OpNo) const;
114
115     void emitGlobalAddressUnaligned(const GlobalValue *GV, unsigned Reloc,
116                                     int Offset) const;
117   };
118 }
119
120 char MipsCodeEmitter::ID = 0;
121
122 bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
123   MipsTargetMachine &Target = static_cast<MipsTargetMachine &>(
124                                 const_cast<TargetMachine &>(MF.getTarget()));
125
126   JTI = Target.getJITInfo();
127   II = Target.getInstrInfo();
128   TD = Target.getDataLayout();
129   Subtarget = &TM.getSubtarget<MipsSubtarget> ();
130   MCPEs = &MF.getConstantPool()->getConstants();
131   MJTEs = 0;
132   if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
133   JTI->Initialize(MF, IsPIC, Subtarget->isLittle());
134   MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
135
136   do {
137     DEBUG(errs() << "JITTing function '"
138         << MF.getName() << "'\n");
139     MCE.startFunction(MF);
140
141     for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
142         MBB != E; ++MBB){
143       MCE.StartMachineBasicBlock(MBB);
144       for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
145            E = MBB->instr_end(); I != E; ++I)
146         emitInstruction(*I);
147     }
148   } while (MCE.finishFunction(MF));
149
150   return false;
151 }
152
153 unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI,
154                                         const MachineOperand &MO) const {
155   // NOTE: This relocations are for static.
156   uint64_t TSFlags = MI.getDesc().TSFlags;
157   uint64_t Form = TSFlags & MipsII::FormMask;
158   if (Form == MipsII::FrmJ)
159     return Mips::reloc_mips_26;
160   if ((Form == MipsII::FrmI || Form == MipsII::FrmFI)
161        && MI.isBranch())
162     return Mips::reloc_mips_pc16;
163   if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
164     return Mips::reloc_mips_hi;
165   return Mips::reloc_mips_lo;
166 }
167
168 unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI,
169                                                unsigned OpNo) const {
170   MachineOperand MO = MI.getOperand(OpNo);
171   if (MO.isGlobal())
172     emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
173   else if (MO.isSymbol())
174     emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
175   else if (MO.isMBB())
176     emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
177   else
178     llvm_unreachable("Unexpected jump target operand kind.");
179   return 0;
180 }
181
182 unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
183                                                  unsigned OpNo) const {
184   MachineOperand MO = MI.getOperand(OpNo);
185   emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
186   return 0;
187 }
188
189 unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI,
190                                          unsigned OpNo) const {
191   // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
192   assert(MI.getOperand(OpNo).isReg());
193   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16;
194   return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
195 }
196
197 unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI,
198                                              unsigned OpNo) const {
199   // size is encoded as size-1.
200   return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
201 }
202
203 unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI,
204                                              unsigned OpNo) const {
205   // size is encoded as pos+size-1.
206   return getMachineOpValue(MI, MI.getOperand(OpNo-1)) +
207          getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
208 }
209
210 /// getMachineOpValue - Return binary encoding of operand. If the machine
211 /// operand requires relocation, record the relocation and return zero.
212 unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI,
213                                             const MachineOperand &MO) const {
214   if (MO.isReg())
215     return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
216   else if (MO.isImm())
217     return static_cast<unsigned>(MO.getImm());
218   else if (MO.isGlobal())
219     emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
220   else if (MO.isSymbol())
221     emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
222   else if (MO.isCPI())
223     emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
224   else if (MO.isJTI())
225     emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
226   else if (MO.isMBB())
227     emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
228   else
229     llvm_unreachable("Unable to encode MachineOperand!");
230   return 0;
231 }
232
233 void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
234                                         bool MayNeedFarStub) const {
235   MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
236                                              const_cast<GlobalValue *>(GV), 0,
237                                              MayNeedFarStub));
238 }
239
240 void MipsCodeEmitter::emitGlobalAddressUnaligned(const GlobalValue *GV,
241                                            unsigned Reloc, int Offset) const {
242   MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
243                              const_cast<GlobalValue *>(GV), 0, false));
244   MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset() + Offset,
245                       Reloc, const_cast<GlobalValue *>(GV), 0, false));
246 }
247
248 void MipsCodeEmitter::
249 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
250   MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
251                                                  Reloc, ES, 0, 0));
252 }
253
254 void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
255   MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
256                                                     Reloc, CPI, 0, false));
257 }
258
259 void MipsCodeEmitter::
260 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
261   MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
262                                                     Reloc, JTIndex, 0, false));
263 }
264
265 void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
266                                             unsigned Reloc) const {
267   MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
268                                              Reloc, BB));
269 }
270
271 void MipsCodeEmitter::emitInstruction(const MachineInstr &MI) {
272   DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
273
274   MCE.processDebugLoc(MI.getDebugLoc(), true);
275
276   // Skip pseudo instructions.
277   if ((MI.getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo)
278     return;
279
280   emitWord(getBinaryCodeForInstr(MI));
281   ++NumEmitted;  // Keep track of the # of mi's emitted
282
283   MCE.processDebugLoc(MI.getDebugLoc(), false);
284 }
285
286 void MipsCodeEmitter::emitWord(unsigned Word) {
287   DEBUG(errs() << "  0x";
288         errs().write_hex(Word) << "\n");
289   if (Subtarget->isLittle())
290     MCE.emitWordLE(Word);
291   else
292     MCE.emitWordBE(Word);
293 }
294
295 /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
296 /// code to the specified MCE object.
297 FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM,
298                                                  JITCodeEmitter &JCE) {
299   return new MipsCodeEmitter(TM, JCE);
300 }
301
302 #include "MipsGenCodeEmitter.inc"