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