Bruno Cardoso Lopes | dca6cdd | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 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 | |
| 42 | using namespace llvm; |
| 43 | |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 44 | STATISTIC(NumEmitted, "Number of machine instructions emitted"); |
| 45 | |
Bruno Cardoso Lopes | dca6cdd | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 46 | namespace { |
| 47 | |
| 48 | class 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 | |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 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 | |
Bruno Cardoso Lopes | dca6cdd | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 85 | void emitInstruction(const MachineInstr &MI); |
| 86 | |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 87 | private: |
Bruno Cardoso Lopes | dca6cdd | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 88 | |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 89 | void emitWordLE(unsigned Word); |
| 90 | |
| 91 | /// Routines that handle operands which add machine relocations which are |
| 92 | /// fixed up by the relocation stage. |
Bruno Cardoso Lopes | dca6cdd | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 93 | void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc, |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 94 | bool MayNeedFarStub) const; |
| 95 | void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const; |
| 96 | void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const; |
Bruno Cardoso Lopes | dca6cdd | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 97 | void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const; |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 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 | |
Bruno Cardoso Lopes | c3f16b3 | 2011-10-18 17:50:36 +0000 | [diff] [blame^] | 108 | unsigned getMemEncoding(const MachineInstr &MI, unsigned OpNo) const; |
| 109 | unsigned getSizeExtEncoding(const MachineInstr &MI, unsigned OpNo) const; |
| 110 | unsigned getSizeInsEncoding(const MachineInstr &MI, unsigned OpNo) const; |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 111 | }; |
Bruno Cardoso Lopes | dca6cdd | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 114 | char MipsCodeEmitter::ID = 0; |
Bruno Cardoso Lopes | dca6cdd | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 115 | |
| 116 | bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) { |
| 117 | JTI = ((MipsTargetMachine&) MF.getTarget()).getJITInfo(); |
| 118 | II = ((const MipsTargetMachine&) MF.getTarget()).getInstrInfo(); |
| 119 | TD = ((const MipsTargetMachine&) MF.getTarget()).getTargetData(); |
| 120 | Subtarget = &TM.getSubtarget<MipsSubtarget> (); |
| 121 | MCPEs = &MF.getConstantPool()->getConstants(); |
| 122 | MJTEs = 0; |
| 123 | if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables(); |
| 124 | JTI->Initialize(MF, IsPIC); |
| 125 | MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ()); |
| 126 | |
| 127 | do { |
| 128 | DEBUG(errs() << "JITTing function '" |
| 129 | << MF.getFunction()->getName() << "'\n"); |
| 130 | MCE.startFunction(MF); |
| 131 | |
| 132 | for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); |
| 133 | MBB != E; ++MBB){ |
| 134 | MCE.StartMachineBasicBlock(MBB); |
| 135 | for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end(); |
| 136 | I != E; ++I) |
| 137 | emitInstruction(*I); |
| 138 | } |
| 139 | } while (MCE.finishFunction(MF)); |
| 140 | |
| 141 | return false; |
| 142 | } |
| 143 | |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 144 | unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI, |
| 145 | const MachineOperand &MO) const { |
| 146 | // NOTE: This relocations are for static. |
| 147 | uint64_t TSFlags = MI.getDesc().TSFlags; |
| 148 | uint64_t Form = TSFlags & MipsII::FormMask; |
| 149 | if (Form == MipsII::FrmJ) |
| 150 | return Mips::reloc_mips_26; |
| 151 | if ((Form == MipsII::FrmI || Form == MipsII::FrmFI) |
| 152 | && MI.getDesc().isBranch()) |
| 153 | return Mips::reloc_mips_branch; |
| 154 | if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi) |
| 155 | return Mips::reloc_mips_hi; |
| 156 | return Mips::reloc_mips_lo; |
Bruno Cardoso Lopes | dca6cdd | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Bruno Cardoso Lopes | c3f16b3 | 2011-10-18 17:50:36 +0000 | [diff] [blame^] | 159 | unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI, |
| 160 | unsigned OpNo) const { |
| 161 | // Base register is encoded in bits 20-16, offset is encoded in bits 15-0. |
| 162 | assert(MI.getOperand(OpNo).isReg()); |
| 163 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16; |
| 164 | return |
| 165 | (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits; |
| 166 | } |
| 167 | |
| 168 | unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI, |
| 169 | unsigned OpNo) const { |
| 170 | // size is encoded as size-1. |
| 171 | return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1; |
| 172 | } |
| 173 | |
| 174 | unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI, |
| 175 | unsigned OpNo) const { |
| 176 | // size is encoded as pos+size-1. |
| 177 | return getMachineOpValue(MI, MI.getOperand(OpNo-1)) + |
| 178 | getMachineOpValue(MI, MI.getOperand(OpNo)) - 1; |
| 179 | } |
| 180 | |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 181 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 182 | /// operand requires relocation, record the relocation and return zero. |
| 183 | unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI, |
| 184 | const MachineOperand &MO) const { |
| 185 | if (MO.isReg()) |
| 186 | return MipsRegisterInfo::getRegisterNumbering(MO.getReg()); |
| 187 | else if (MO.isImm()) |
| 188 | return static_cast<unsigned>(MO.getImm()); |
| 189 | else if (MO.isGlobal()) |
| 190 | emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true); |
| 191 | else if (MO.isSymbol()) |
| 192 | emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO)); |
| 193 | else if (MO.isCPI()) |
| 194 | emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO)); |
| 195 | else if (MO.isJTI()) |
| 196 | emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO)); |
| 197 | else if (MO.isMBB()) |
| 198 | emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO)); |
| 199 | else |
| 200 | llvm_unreachable("Unable to encode MachineOperand!"); |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc, |
| 205 | bool MayNeedFarStub) const { |
| 206 | MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc, |
| 207 | const_cast<GlobalValue *>(GV), 0, MayNeedFarStub)); |
| 208 | } |
| 209 | |
| 210 | void MipsCodeEmitter:: |
| 211 | emitExternalSymbolAddress(const char *ES, unsigned Reloc) const { |
| 212 | MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(), |
| 213 | Reloc, ES, 0, 0, false)); |
| 214 | } |
| 215 | |
| 216 | void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const { |
| 217 | MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(), |
| 218 | Reloc, CPI, 0, false)); |
| 219 | } |
| 220 | |
| 221 | void MipsCodeEmitter:: |
| 222 | emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const { |
| 223 | MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(), |
| 224 | Reloc, JTIndex, 0, false)); |
| 225 | } |
| 226 | |
| 227 | void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB, |
| 228 | unsigned Reloc) const { |
| 229 | MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(), |
| 230 | Reloc, BB)); |
| 231 | } |
| 232 | |
| 233 | void MipsCodeEmitter::emitInstruction(const MachineInstr &MI) { |
| 234 | DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI); |
| 235 | |
| 236 | MCE.processDebugLoc(MI.getDebugLoc(), true); |
| 237 | |
| 238 | // Skip pseudo instructions. |
| 239 | if ((MI.getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo) |
| 240 | return; |
| 241 | |
| 242 | ++NumEmitted; // Keep track of the # of mi's emitted |
| 243 | |
| 244 | switch (MI.getOpcode()) { |
| 245 | default: |
| 246 | emitWordLE(getBinaryCodeForInstr(MI)); |
| 247 | break; |
| 248 | } |
| 249 | |
| 250 | MCE.processDebugLoc(MI.getDebugLoc(), false); |
| 251 | } |
| 252 | |
| 253 | void MipsCodeEmitter::emitWordLE(unsigned Word) { |
| 254 | DEBUG(errs() << " 0x"; |
| 255 | errs().write_hex(Word) << "\n"); |
| 256 | MCE.emitWordLE(Word); |
| 257 | } |
| 258 | |
| 259 | /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips |
| 260 | /// code to the specified MCE object. |
| 261 | FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM, |
| 262 | JITCodeEmitter &JCE) { |
| 263 | return new MipsCodeEmitter(TM, JCE); |
| 264 | } |
| 265 | |
Bruno Cardoso Lopes | c3f16b3 | 2011-10-18 17:50:36 +0000 | [diff] [blame^] | 266 | #include "MipsGenCodeEmitter.inc" |