MipsCodeEmitter.cpp revision 226633
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 "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" 36#ifndef NDEBUG 37#include <iomanip> 38#endif 39 40#include "llvm/CodeGen/MachineOperand.h" 41 42using namespace llvm; 43 44STATISTIC(NumEmitted, "Number of machine instructions emitted"); 45 46namespace { 47 48class MipsCodeEmitter : public MachineFunctionPass { 49 MipsJITInfo *JTI; 50 const MipsInstrInfo *II; 51 const TargetData *TD; 52 const MipsSubtarget *Subtarget; 53 TargetMachine &TM; 54 JITCodeEmitter &MCE; 55 const std::vector<MachineConstantPoolEntry> *MCPEs; 56 const std::vector<MachineJumpTableEntry> *MJTEs; 57 bool IsPIC; 58 59 void getAnalysisUsage(AnalysisUsage &AU) const { 60 AU.addRequired<MachineModuleInfo> (); 61 MachineFunctionPass::getAnalysisUsage(AU); 62 } 63 64 static char ID; 65 66 public: 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_) { 72 } 73 74 bool runOnMachineFunction(MachineFunction &MF); 75 76 virtual const char *getPassName() const { 77 return "Mips Machine Code Emitter"; 78 } 79 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; 84 85 void emitInstruction(const MachineInstr &MI); 86 87 private: 88 89 void emitWordLE(unsigned Word); 90 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; 99 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; 104 105 unsigned getRelocation(const MachineInstr &MI, 106 const MachineOperand &MO) const; 107 108 }; 109} 110 111char MipsCodeEmitter::ID = 0; 112 113bool 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(); 119 MJTEs = 0; 120 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables(); 121 JTI->Initialize(MF, IsPIC); 122 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ()); 123 124 do { 125 DEBUG(errs() << "JITTing function '" 126 << MF.getFunction()->getName() << "'\n"); 127 MCE.startFunction(MF); 128 129 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); 130 MBB != E; ++MBB){ 131 MCE.StartMachineBasicBlock(MBB); 132 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end(); 133 I != E; ++I) 134 emitInstruction(*I); 135 } 136 } while (MCE.finishFunction(MF)); 137 138 return false; 139} 140 141unsigned 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; 154} 155 156/// getMachineOpValue - Return binary encoding of operand. If the machine 157/// operand requires relocation, record the relocation and return zero. 158unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI, 159 const MachineOperand &MO) const { 160 if (MO.isReg()) 161 return MipsRegisterInfo::getRegisterNumbering(MO.getReg()); 162 else if (MO.isImm()) 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)); 168 else if (MO.isCPI()) 169 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO)); 170 else if (MO.isJTI()) 171 emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO)); 172 else if (MO.isMBB()) 173 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO)); 174 else 175 llvm_unreachable("Unable to encode MachineOperand!"); 176 return 0; 177} 178 179void 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)); 183} 184 185void MipsCodeEmitter:: 186emitExternalSymbolAddress(const char *ES, unsigned Reloc) const { 187 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(), 188 Reloc, ES, 0, 0, false)); 189} 190 191void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const { 192 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(), 193 Reloc, CPI, 0, false)); 194} 195 196void MipsCodeEmitter:: 197emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const { 198 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(), 199 Reloc, JTIndex, 0, false)); 200} 201 202void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB, 203 unsigned Reloc) const { 204 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(), 205 Reloc, BB)); 206} 207 208void MipsCodeEmitter::emitInstruction(const MachineInstr &MI) { 209 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI); 210 211 MCE.processDebugLoc(MI.getDebugLoc(), true); 212 213 // Skip pseudo instructions. 214 if ((MI.getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo) 215 return; 216 217 ++NumEmitted; // Keep track of the # of mi's emitted 218 219 switch (MI.getOpcode()) { 220 default: 221 emitWordLE(getBinaryCodeForInstr(MI)); 222 break; 223 } 224 225 MCE.processDebugLoc(MI.getDebugLoc(), false); 226} 227 228void MipsCodeEmitter::emitWordLE(unsigned Word) { 229 DEBUG(errs() << " 0x"; 230 errs().write_hex(Word) << "\n"); 231 MCE.emitWordLE(Word); 232} 233 234/// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips 235/// code to the specified MCE object. 236FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM, 237 JITCodeEmitter &JCE) { 238 return new MipsCodeEmitter(TM, JCE); 239} 240 241unsigned MipsCodeEmitter::getBinaryCodeForInstr(const MachineInstr &MI) const { 242 // this function will be automatically generated by the CodeEmitterGenerator 243 // using TableGen 244 return 0; 245} 246