1 //===-- Mips/MipsCodeEmitter.cpp - Convert Mips 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 contains the pass that transforms the Mips machine instructions
11 // into relocatable machine code.
13 //===---------------------------------------------------------------------===//
15 #define DEBUG_TYPE "jit"
17 #include "MipsInstrInfo.h"
18 #include "MipsRelocations.h"
19 #include "MipsSubtarget.h"
20 #include "MipsTargetMachine.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Function.h"
24 #include "llvm/PassManager.h"
25 #include "llvm/CodeGen/JITCodeEmitter.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/CodeGen/MachineJumpTableInfo.h"
30 #include "llvm/CodeGen/MachineModuleInfo.h"
31 #include "llvm/CodeGen/Passes.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/CodeGen/MachineOperand.h"
44 STATISTIC(NumEmitted, "Number of machine instructions emitted");
48 class MipsCodeEmitter : public MachineFunctionPass {
50 const MipsInstrInfo *II;
52 const MipsSubtarget *Subtarget;
55 const std::vector<MachineConstantPoolEntry> *MCPEs;
56 const std::vector<MachineJumpTableEntry> *MJTEs;
59 void getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.addRequired<MachineModuleInfo> ();
61 MachineFunctionPass::getAnalysisUsage(AU);
67 MipsCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce) :
68 MachineFunctionPass(ID), JTI(0),
69 II((const MipsInstrInfo *) tm.getInstrInfo()),
70 TD(tm.getTargetData()), TM(tm), MCE(mce), MCPEs(0), MJTEs(0),
71 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {
74 bool runOnMachineFunction(MachineFunction &MF);
76 virtual const char *getPassName() const {
77 return "Mips Machine Code Emitter";
80 /// getBinaryCodeForInstr - This function, generated by the
81 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
82 /// machine instructions.
83 unsigned getBinaryCodeForInstr(const MachineInstr &MI) const;
85 void emitInstruction(const MachineInstr &MI);
89 void emitWordLE(unsigned Word);
91 /// Routines that handle operands which add machine relocations which are
92 /// fixed up by the relocation stage.
93 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
94 bool MayNeedFarStub) const;
95 void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
96 void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
97 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const;
98 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc) const;
100 /// getMachineOpValue - Return binary encoding of operand. If the machine
101 /// operand requires relocation, record the relocation and return zero.
102 unsigned getMachineOpValue(const MachineInstr &MI,
103 const MachineOperand &MO) const;
105 unsigned getRelocation(const MachineInstr &MI,
106 const MachineOperand &MO) const;
111 char MipsCodeEmitter::ID = 0;
113 bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
114 JTI = ((MipsTargetMachine&) MF.getTarget()).getJITInfo();
115 II = ((const MipsTargetMachine&) MF.getTarget()).getInstrInfo();
116 TD = ((const MipsTargetMachine&) MF.getTarget()).getTargetData();
117 Subtarget = &TM.getSubtarget<MipsSubtarget> ();
118 MCPEs = &MF.getConstantPool()->getConstants();
120 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
121 JTI->Initialize(MF, IsPIC);
122 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
125 DEBUG(errs() << "JITTing function '"
126 << MF.getFunction()->getName() << "'\n");
127 MCE.startFunction(MF);
129 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
131 MCE.StartMachineBasicBlock(MBB);
132 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
136 } while (MCE.finishFunction(MF));
141 unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI,
142 const MachineOperand &MO) const {
143 // NOTE: This relocations are for static.
144 uint64_t TSFlags = MI.getDesc().TSFlags;
145 uint64_t Form = TSFlags & MipsII::FormMask;
146 if (Form == MipsII::FrmJ)
147 return Mips::reloc_mips_26;
148 if ((Form == MipsII::FrmI || Form == MipsII::FrmFI)
149 && MI.getDesc().isBranch())
150 return Mips::reloc_mips_branch;
151 if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
152 return Mips::reloc_mips_hi;
153 return Mips::reloc_mips_lo;
156 /// getMachineOpValue - Return binary encoding of operand. If the machine
157 /// operand requires relocation, record the relocation and return zero.
158 unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI,
159 const MachineOperand &MO) const {
161 return MipsRegisterInfo::getRegisterNumbering(MO.getReg());
163 return static_cast<unsigned>(MO.getImm());
164 else if (MO.isGlobal())
165 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
166 else if (MO.isSymbol())
167 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
169 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
171 emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
173 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
175 llvm_unreachable("Unable to encode MachineOperand!");
179 void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
180 bool MayNeedFarStub) const {
181 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
182 const_cast<GlobalValue *>(GV), 0, MayNeedFarStub));
185 void MipsCodeEmitter::
186 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
187 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
188 Reloc, ES, 0, 0, false));
191 void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
192 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
193 Reloc, CPI, 0, false));
196 void MipsCodeEmitter::
197 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
198 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
199 Reloc, JTIndex, 0, false));
202 void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
203 unsigned Reloc) const {
204 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
208 void MipsCodeEmitter::emitInstruction(const MachineInstr &MI) {
209 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
211 MCE.processDebugLoc(MI.getDebugLoc(), true);
213 // Skip pseudo instructions.
214 if ((MI.getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo)
217 ++NumEmitted; // Keep track of the # of mi's emitted
219 switch (MI.getOpcode()) {
221 emitWordLE(getBinaryCodeForInstr(MI));
225 MCE.processDebugLoc(MI.getDebugLoc(), false);
228 void MipsCodeEmitter::emitWordLE(unsigned Word) {
229 DEBUG(errs() << " 0x";
230 errs().write_hex(Word) << "\n");
231 MCE.emitWordLE(Word);
234 /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
235 /// code to the specified MCE object.
236 FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM,
237 JITCodeEmitter &JCE) {
238 return new MipsCodeEmitter(TM, JCE);
241 unsigned MipsCodeEmitter::getBinaryCodeForInstr(const MachineInstr &MI) const {
242 // this function will be automatically generated by the CodeEmitterGenerator