Jia Liu | 9f61011 | 2012-02-17 08:55:11 +0000 | [diff] [blame] | 1 | //===-- Mips/MipsCodeEmitter.cpp - Convert Mips Code to Machine Code ------===// |
Bruno Cardoso Lopes | d1d9c78 | 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 | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "MCTargetDesc/MipsBaseInfo.h" |
Bruno Cardoso Lopes | d1d9c78 | 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 | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Bruno Cardoso Lopes | d1d9c78 | 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" |
Akira Hatanaka | bf1af1a | 2013-02-11 22:35:40 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineOperand.h" |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Constants.h" |
| 33 | #include "llvm/IR/DerivedTypes.h" |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 34 | #include "llvm/PassManager.h" |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
| 36 | #include "llvm/Support/ErrorHandling.h" |
| 37 | #include "llvm/Support/raw_ostream.h" |
| 38 | #ifndef NDEBUG |
| 39 | #include <iomanip> |
| 40 | #endif |
| 41 | |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 44 | STATISTIC(NumEmitted, "Number of machine instructions emitted"); |
| 45 | |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 46 | namespace { |
| 47 | |
| 48 | class MipsCodeEmitter : public MachineFunctionPass { |
| 49 | MipsJITInfo *JTI; |
| 50 | const MipsInstrInfo *II; |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 51 | const DataLayout *TD; |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 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 | |
Akira Hatanaka | 3d38609 | 2013-02-11 22:03:52 +0000 | [diff] [blame] | 66 | public: |
| 67 | MipsCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce) |
Bill Wendling | ead89ef | 2013-06-07 07:04:14 +0000 | [diff] [blame] | 68 | : MachineFunctionPass(ID), JTI(0), II(0), TD(0), |
Akira Hatanaka | 3d38609 | 2013-02-11 22:03:52 +0000 | [diff] [blame] | 69 | TM(tm), MCE(mce), MCPEs(0), MJTEs(0), |
| 70 | IsPIC(TM.getRelocationModel() == Reloc::PIC_) {} |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 71 | |
Akira Hatanaka | 3d38609 | 2013-02-11 22:03:52 +0000 | [diff] [blame] | 72 | bool runOnMachineFunction(MachineFunction &MF); |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 73 | |
Akira Hatanaka | 3d38609 | 2013-02-11 22:03:52 +0000 | [diff] [blame] | 74 | virtual const char *getPassName() const { |
| 75 | return "Mips Machine Code Emitter"; |
| 76 | } |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 77 | |
Akira Hatanaka | 3d38609 | 2013-02-11 22:03:52 +0000 | [diff] [blame] | 78 | /// getBinaryCodeForInstr - This function, generated by the |
| 79 | /// CodeEmitterGenerator using TableGen, produces the binary encoding for |
| 80 | /// machine instructions. |
| 81 | uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const; |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 82 | |
Akira Hatanaka | bf1af1a | 2013-02-11 22:35:40 +0000 | [diff] [blame] | 83 | void emitInstruction(MachineBasicBlock::instr_iterator MI, |
| 84 | MachineBasicBlock &MBB); |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 85 | |
Akira Hatanaka | 3d38609 | 2013-02-11 22:03:52 +0000 | [diff] [blame] | 86 | private: |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 87 | |
Akira Hatanaka | 3d38609 | 2013-02-11 22:03:52 +0000 | [diff] [blame] | 88 | void emitWord(unsigned Word); |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 89 | |
Akira Hatanaka | 3d38609 | 2013-02-11 22:03:52 +0000 | [diff] [blame] | 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; |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 98 | |
Akira Hatanaka | 3d38609 | 2013-02-11 22:03:52 +0000 | [diff] [blame] | 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; |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 103 | |
Akira Hatanaka | 3d38609 | 2013-02-11 22:03:52 +0000 | [diff] [blame] | 104 | unsigned getRelocation(const MachineInstr &MI, |
| 105 | const MachineOperand &MO) const; |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 106 | |
Akira Hatanaka | 3d38609 | 2013-02-11 22:03:52 +0000 | [diff] [blame] | 107 | unsigned getJumpTargetOpValue(const MachineInstr &MI, unsigned OpNo) const; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 108 | |
Akira Hatanaka | 3d38609 | 2013-02-11 22:03:52 +0000 | [diff] [blame] | 109 | unsigned getBranchTargetOpValue(const MachineInstr &MI, unsigned OpNo) const; |
| 110 | unsigned getMemEncoding(const MachineInstr &MI, unsigned OpNo) const; |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 111 | unsigned getMemEncodingMMImm12(const MachineInstr &MI, unsigned OpNo) const; |
Akira Hatanaka | 3d38609 | 2013-02-11 22:03:52 +0000 | [diff] [blame] | 112 | unsigned getSizeExtEncoding(const MachineInstr &MI, unsigned OpNo) const; |
| 113 | unsigned getSizeInsEncoding(const MachineInstr &MI, unsigned OpNo) const; |
Bruno Cardoso Lopes | 71133fe | 2011-11-08 12:47:11 +0000 | [diff] [blame] | 114 | |
Akira Hatanaka | 3d38609 | 2013-02-11 22:03:52 +0000 | [diff] [blame] | 115 | void emitGlobalAddressUnaligned(const GlobalValue *GV, unsigned Reloc, |
| 116 | int Offset) const; |
Akira Hatanaka | bf1af1a | 2013-02-11 22:35:40 +0000 | [diff] [blame] | 117 | |
Akira Hatanaka | 2ffc573 | 2013-04-02 22:53:58 +0000 | [diff] [blame] | 118 | /// Expand pseudo instructions with accumulator register operands. |
Akira Hatanaka | 52f79fc | 2013-04-11 19:07:14 +0000 | [diff] [blame] | 119 | void expandACCInstr(MachineBasicBlock::instr_iterator MI, |
Akira Hatanaka | 2ffc573 | 2013-04-02 22:53:58 +0000 | [diff] [blame] | 120 | MachineBasicBlock &MBB, unsigned Opc) const; |
| 121 | |
Akira Hatanaka | bf1af1a | 2013-02-11 22:35:40 +0000 | [diff] [blame] | 122 | /// \brief Expand pseudo instruction. Return true if MI was expanded. |
| 123 | bool expandPseudos(MachineBasicBlock::instr_iterator &MI, |
| 124 | MachineBasicBlock &MBB) const; |
Akira Hatanaka | 3d38609 | 2013-02-11 22:03:52 +0000 | [diff] [blame] | 125 | }; |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 128 | char MipsCodeEmitter::ID = 0; |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 129 | |
| 130 | bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) { |
Dmitri Gribenko | 2e1df0e | 2013-01-14 22:08:37 +0000 | [diff] [blame] | 131 | MipsTargetMachine &Target = static_cast<MipsTargetMachine &>( |
| 132 | const_cast<TargetMachine &>(MF.getTarget())); |
| 133 | |
| 134 | JTI = Target.getJITInfo(); |
| 135 | II = Target.getInstrInfo(); |
| 136 | TD = Target.getDataLayout(); |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 137 | Subtarget = &TM.getSubtarget<MipsSubtarget> (); |
| 138 | MCPEs = &MF.getConstantPool()->getConstants(); |
| 139 | MJTEs = 0; |
| 140 | if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables(); |
Akira Hatanaka | 4c12850 | 2012-12-03 23:11:12 +0000 | [diff] [blame] | 141 | JTI->Initialize(MF, IsPIC, Subtarget->isLittle()); |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 142 | MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ()); |
| 143 | |
| 144 | do { |
| 145 | DEBUG(errs() << "JITTing function '" |
Craig Topper | a538d83 | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 146 | << MF.getName() << "'\n"); |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 147 | MCE.startFunction(MF); |
| 148 | |
| 149 | for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); |
| 150 | MBB != E; ++MBB){ |
| 151 | MCE.StartMachineBasicBlock(MBB); |
Akira Hatanaka | 9846239 | 2012-06-19 03:39:45 +0000 | [diff] [blame] | 152 | for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(), |
Akira Hatanaka | bf1af1a | 2013-02-11 22:35:40 +0000 | [diff] [blame] | 153 | E = MBB->instr_end(); I != E;) |
| 154 | emitInstruction(*I++, *MBB); |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 155 | } |
| 156 | } while (MCE.finishFunction(MF)); |
| 157 | |
| 158 | return false; |
| 159 | } |
| 160 | |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 161 | unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI, |
| 162 | const MachineOperand &MO) const { |
| 163 | // NOTE: This relocations are for static. |
| 164 | uint64_t TSFlags = MI.getDesc().TSFlags; |
| 165 | uint64_t Form = TSFlags & MipsII::FormMask; |
| 166 | if (Form == MipsII::FrmJ) |
| 167 | return Mips::reloc_mips_26; |
| 168 | if ((Form == MipsII::FrmI || Form == MipsII::FrmFI) |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 169 | && MI.isBranch()) |
Bruno Cardoso Lopes | d5b2834 | 2011-12-30 21:04:30 +0000 | [diff] [blame] | 170 | return Mips::reloc_mips_pc16; |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 171 | if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi) |
| 172 | return Mips::reloc_mips_hi; |
| 173 | return Mips::reloc_mips_lo; |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 176 | unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI, |
| 177 | unsigned OpNo) const { |
Bruno Cardoso Lopes | d5b2834 | 2011-12-30 21:04:30 +0000 | [diff] [blame] | 178 | MachineOperand MO = MI.getOperand(OpNo); |
| 179 | if (MO.isGlobal()) |
| 180 | emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true); |
| 181 | else if (MO.isSymbol()) |
| 182 | emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO)); |
| 183 | else if (MO.isMBB()) |
| 184 | emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO)); |
| 185 | else |
| 186 | llvm_unreachable("Unexpected jump target operand kind."); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI, |
| 191 | unsigned OpNo) const { |
Bruno Cardoso Lopes | d5b2834 | 2011-12-30 21:04:30 +0000 | [diff] [blame] | 192 | MachineOperand MO = MI.getOperand(OpNo); |
| 193 | emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO)); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
Bruno Cardoso Lopes | 2312a3a | 2011-10-18 17:50:36 +0000 | [diff] [blame] | 197 | unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI, |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 198 | unsigned OpNo) const { |
Bruno Cardoso Lopes | 2312a3a | 2011-10-18 17:50:36 +0000 | [diff] [blame] | 199 | // Base register is encoded in bits 20-16, offset is encoded in bits 15-0. |
| 200 | assert(MI.getOperand(OpNo).isReg()); |
| 201 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 202 | return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits; |
Bruno Cardoso Lopes | 2312a3a | 2011-10-18 17:50:36 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 205 | unsigned MipsCodeEmitter::getMemEncodingMMImm12(const MachineInstr &MI, |
| 206 | unsigned OpNo) const { |
| 207 | llvm_unreachable("Unimplemented function."); |
| 208 | return 0; |
| 209 | } |
| 210 | |
Bruno Cardoso Lopes | 2312a3a | 2011-10-18 17:50:36 +0000 | [diff] [blame] | 211 | unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI, |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 212 | unsigned OpNo) const { |
Bruno Cardoso Lopes | 2312a3a | 2011-10-18 17:50:36 +0000 | [diff] [blame] | 213 | // size is encoded as size-1. |
| 214 | return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1; |
| 215 | } |
| 216 | |
| 217 | unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI, |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 218 | unsigned OpNo) const { |
Bruno Cardoso Lopes | 2312a3a | 2011-10-18 17:50:36 +0000 | [diff] [blame] | 219 | // size is encoded as pos+size-1. |
| 220 | return getMachineOpValue(MI, MI.getOperand(OpNo-1)) + |
| 221 | getMachineOpValue(MI, MI.getOperand(OpNo)) - 1; |
| 222 | } |
| 223 | |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 224 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 225 | /// operand requires relocation, record the relocation and return zero. |
| 226 | unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI, |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 227 | const MachineOperand &MO) const { |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 228 | if (MO.isReg()) |
Akira Hatanaka | 5d6faed | 2012-12-10 20:04:40 +0000 | [diff] [blame] | 229 | return TM.getRegisterInfo()->getEncodingValue(MO.getReg()); |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 230 | else if (MO.isImm()) |
| 231 | return static_cast<unsigned>(MO.getImm()); |
Akira Hatanaka | 3e7ba76 | 2012-09-15 01:52:08 +0000 | [diff] [blame] | 232 | else if (MO.isGlobal()) |
| 233 | emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true); |
| 234 | else if (MO.isSymbol()) |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 235 | emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO)); |
| 236 | else if (MO.isCPI()) |
| 237 | emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO)); |
| 238 | else if (MO.isJTI()) |
| 239 | emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO)); |
| 240 | else if (MO.isMBB()) |
| 241 | emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO)); |
| 242 | else |
| 243 | llvm_unreachable("Unable to encode MachineOperand!"); |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc, |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 248 | bool MayNeedFarStub) const { |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 249 | MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc, |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 250 | const_cast<GlobalValue *>(GV), 0, |
| 251 | MayNeedFarStub)); |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Bruno Cardoso Lopes | 71133fe | 2011-11-08 12:47:11 +0000 | [diff] [blame] | 254 | void MipsCodeEmitter::emitGlobalAddressUnaligned(const GlobalValue *GV, |
| 255 | unsigned Reloc, int Offset) const { |
| 256 | MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc, |
| 257 | const_cast<GlobalValue *>(GV), 0, false)); |
| 258 | MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset() + Offset, |
| 259 | Reloc, const_cast<GlobalValue *>(GV), 0, false)); |
| 260 | } |
| 261 | |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 262 | void MipsCodeEmitter:: |
| 263 | emitExternalSymbolAddress(const char *ES, unsigned Reloc) const { |
| 264 | MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(), |
Akira Hatanaka | 45da9e2 | 2012-07-24 00:08:26 +0000 | [diff] [blame] | 265 | Reloc, ES, 0, 0)); |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const { |
| 269 | MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(), |
| 270 | Reloc, CPI, 0, false)); |
| 271 | } |
| 272 | |
| 273 | void MipsCodeEmitter:: |
| 274 | emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const { |
| 275 | MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(), |
| 276 | Reloc, JTIndex, 0, false)); |
| 277 | } |
| 278 | |
| 279 | void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB, |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 280 | unsigned Reloc) const { |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 281 | MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(), |
| 282 | Reloc, BB)); |
| 283 | } |
| 284 | |
Akira Hatanaka | bf1af1a | 2013-02-11 22:35:40 +0000 | [diff] [blame] | 285 | void MipsCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI, |
| 286 | MachineBasicBlock &MBB) { |
| 287 | DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI); |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 288 | |
Akira Hatanaka | bf1af1a | 2013-02-11 22:35:40 +0000 | [diff] [blame] | 289 | // Expand pseudo instruction. Skip if MI was not expanded. |
| 290 | if (((MI->getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo) && |
| 291 | !expandPseudos(MI, MBB)) |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 292 | return; |
| 293 | |
Akira Hatanaka | bf1af1a | 2013-02-11 22:35:40 +0000 | [diff] [blame] | 294 | MCE.processDebugLoc(MI->getDebugLoc(), true); |
| 295 | |
| 296 | emitWord(getBinaryCodeForInstr(*MI)); |
Akira Hatanaka | 3e7ba76 | 2012-09-15 01:52:08 +0000 | [diff] [blame] | 297 | ++NumEmitted; // Keep track of the # of mi's emitted |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 298 | |
Akira Hatanaka | bf1af1a | 2013-02-11 22:35:40 +0000 | [diff] [blame] | 299 | MCE.processDebugLoc(MI->getDebugLoc(), false); |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Akira Hatanaka | 4c12850 | 2012-12-03 23:11:12 +0000 | [diff] [blame] | 302 | void MipsCodeEmitter::emitWord(unsigned Word) { |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 303 | DEBUG(errs() << " 0x"; |
| 304 | errs().write_hex(Word) << "\n"); |
Akira Hatanaka | 4c12850 | 2012-12-03 23:11:12 +0000 | [diff] [blame] | 305 | if (Subtarget->isLittle()) |
| 306 | MCE.emitWordLE(Word); |
| 307 | else |
| 308 | MCE.emitWordBE(Word); |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Akira Hatanaka | 52f79fc | 2013-04-11 19:07:14 +0000 | [diff] [blame] | 311 | void MipsCodeEmitter::expandACCInstr(MachineBasicBlock::instr_iterator MI, |
Akira Hatanaka | 2ffc573 | 2013-04-02 22:53:58 +0000 | [diff] [blame] | 312 | MachineBasicBlock &MBB, |
| 313 | unsigned Opc) const { |
| 314 | // Expand "pseudomult $ac0, $t0, $t1" to "mult $t0, $t1". |
| 315 | BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Opc)) |
| 316 | .addReg(MI->getOperand(1).getReg()).addReg(MI->getOperand(2).getReg()); |
| 317 | } |
| 318 | |
Akira Hatanaka | bf1af1a | 2013-02-11 22:35:40 +0000 | [diff] [blame] | 319 | bool MipsCodeEmitter::expandPseudos(MachineBasicBlock::instr_iterator &MI, |
| 320 | MachineBasicBlock &MBB) const { |
| 321 | switch (MI->getOpcode()) { |
| 322 | case Mips::NOP: |
| 323 | BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::SLL), Mips::ZERO) |
| 324 | .addReg(Mips::ZERO).addImm(0); |
| 325 | break; |
Akira Hatanaka | 2c544d8 | 2013-09-06 23:40:15 +0000 | [diff] [blame] | 326 | case Mips::B: |
| 327 | BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BEQ)).addReg(Mips::ZERO) |
| 328 | .addReg(Mips::ZERO).addOperand(MI->getOperand(0)); |
| 329 | break; |
Akira Hatanaka | 79e38cd | 2013-09-06 23:52:46 +0000 | [diff] [blame] | 330 | case Mips::TRAP: |
| 331 | BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BREAK)).addImm(0) |
| 332 | .addImm(0); |
| 333 | break; |
Akira Hatanaka | bf1af1a | 2013-02-11 22:35:40 +0000 | [diff] [blame] | 334 | case Mips::JALRPseudo: |
| 335 | BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::JALR), Mips::RA) |
| 336 | .addReg(MI->getOperand(0).getReg()); |
| 337 | break; |
Akira Hatanaka | 2ffc573 | 2013-04-02 22:53:58 +0000 | [diff] [blame] | 338 | case Mips::PseudoMULT: |
| 339 | expandACCInstr(MI, MBB, Mips::MULT); |
| 340 | break; |
| 341 | case Mips::PseudoMULTu: |
| 342 | expandACCInstr(MI, MBB, Mips::MULTu); |
| 343 | break; |
| 344 | case Mips::PseudoSDIV: |
| 345 | expandACCInstr(MI, MBB, Mips::SDIV); |
| 346 | break; |
| 347 | case Mips::PseudoUDIV: |
| 348 | expandACCInstr(MI, MBB, Mips::UDIV); |
| 349 | break; |
| 350 | case Mips::PseudoMADD: |
| 351 | expandACCInstr(MI, MBB, Mips::MADD); |
| 352 | break; |
| 353 | case Mips::PseudoMADDU: |
| 354 | expandACCInstr(MI, MBB, Mips::MADDU); |
| 355 | break; |
| 356 | case Mips::PseudoMSUB: |
| 357 | expandACCInstr(MI, MBB, Mips::MSUB); |
| 358 | break; |
| 359 | case Mips::PseudoMSUBU: |
| 360 | expandACCInstr(MI, MBB, Mips::MSUBU); |
| 361 | break; |
Akira Hatanaka | bf1af1a | 2013-02-11 22:35:40 +0000 | [diff] [blame] | 362 | default: |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | (MI--)->eraseFromBundle(); |
| 367 | return true; |
| 368 | } |
| 369 | |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 370 | /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips |
| 371 | /// code to the specified MCE object. |
| 372 | FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM, |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 373 | JITCodeEmitter &JCE) { |
Bruno Cardoso Lopes | 483c269 | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 374 | return new MipsCodeEmitter(TM, JCE); |
| 375 | } |
| 376 | |
Bruno Cardoso Lopes | 2312a3a | 2011-10-18 17:50:36 +0000 | [diff] [blame] | 377 | #include "MipsGenCodeEmitter.inc" |