Jia Liu | 8f5e8c1 | 2012-02-17 01:23:50 +0000 | [diff] [blame] | 1 | //== Mips/MipsCodeEmitter.cpp - Convert Mips Code to Machine Code -*- C++ -*-=// |
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" |
| 17 | #include "MipsInstrInfo.h" |
| 18 | #include "MipsRelocations.h" |
| 19 | #include "MipsSubtarget.h" |
| 20 | #include "MipsTargetMachine.h" |
Bruno Cardoso Lopes | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 21 | #include "MCTargetDesc/MipsBaseInfo.h" |
| 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" |
| 33 | #include "llvm/Function.h" |
| 34 | #include "llvm/PassManager.h" |
Bruno Cardoso Lopes | dca6cdd | 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 | dca6cdd | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 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), |
Bruno Cardoso Lopes | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 69 | II((const MipsInstrInfo *) tm.getInstrInfo()), |
| 70 | TD(tm.getTargetData()), TM(tm), MCE(mce), MCPEs(0), MJTEs(0), |
| 71 | IsPIC(TM.getRelocationModel() == Reloc::PIC_) { |
Bruno Cardoso Lopes | dca6cdd | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 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. |
Owen Anderson | 4f8dc7b | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 83 | uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const; |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 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 | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 94 | bool MayNeedFarStub) const; |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 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 | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 108 | unsigned getJumpTargetOpValue(const MachineInstr &MI, unsigned OpNo) const; |
| 109 | |
Akira Hatanaka | 8209968 | 2011-12-19 19:52:25 +0000 | [diff] [blame] | 110 | unsigned getBranchTargetOpValue(const MachineInstr &MI, |
| 111 | unsigned OpNo) const; |
Bruno Cardoso Lopes | c3f16b3 | 2011-10-18 17:50:36 +0000 | [diff] [blame] | 112 | unsigned getMemEncoding(const MachineInstr &MI, unsigned OpNo) const; |
| 113 | unsigned getSizeExtEncoding(const MachineInstr &MI, unsigned OpNo) const; |
| 114 | unsigned getSizeInsEncoding(const MachineInstr &MI, unsigned OpNo) const; |
Bruno Cardoso Lopes | ad6eef4 | 2011-11-08 12:47:11 +0000 | [diff] [blame] | 115 | |
| 116 | int emitULW(const MachineInstr &MI); |
| 117 | int emitUSW(const MachineInstr &MI); |
| 118 | int emitULH(const MachineInstr &MI); |
| 119 | int emitULHu(const MachineInstr &MI); |
| 120 | int emitUSH(const MachineInstr &MI); |
| 121 | |
| 122 | void emitGlobalAddressUnaligned(const GlobalValue *GV, unsigned Reloc, |
Akira Hatanaka | 8209968 | 2011-12-19 19:52:25 +0000 | [diff] [blame] | 123 | int Offset) const; |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 124 | }; |
Bruno Cardoso Lopes | dca6cdd | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 127 | char MipsCodeEmitter::ID = 0; |
Bruno Cardoso Lopes | dca6cdd | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 128 | |
| 129 | bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) { |
| 130 | JTI = ((MipsTargetMachine&) MF.getTarget()).getJITInfo(); |
| 131 | II = ((const MipsTargetMachine&) MF.getTarget()).getInstrInfo(); |
| 132 | TD = ((const MipsTargetMachine&) MF.getTarget()).getTargetData(); |
| 133 | Subtarget = &TM.getSubtarget<MipsSubtarget> (); |
| 134 | MCPEs = &MF.getConstantPool()->getConstants(); |
| 135 | MJTEs = 0; |
| 136 | if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables(); |
| 137 | JTI->Initialize(MF, IsPIC); |
| 138 | MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ()); |
| 139 | |
| 140 | do { |
| 141 | DEBUG(errs() << "JITTing function '" |
| 142 | << MF.getFunction()->getName() << "'\n"); |
| 143 | MCE.startFunction(MF); |
| 144 | |
| 145 | for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); |
| 146 | MBB != E; ++MBB){ |
| 147 | MCE.StartMachineBasicBlock(MBB); |
Evan Cheng | 7c2a4a3 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 148 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
Bruno Cardoso Lopes | dca6cdd | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 149 | I != E; ++I) |
| 150 | emitInstruction(*I); |
| 151 | } |
| 152 | } while (MCE.finishFunction(MF)); |
| 153 | |
| 154 | return false; |
| 155 | } |
| 156 | |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 157 | unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI, |
| 158 | const MachineOperand &MO) const { |
| 159 | // NOTE: This relocations are for static. |
| 160 | uint64_t TSFlags = MI.getDesc().TSFlags; |
| 161 | uint64_t Form = TSFlags & MipsII::FormMask; |
| 162 | if (Form == MipsII::FrmJ) |
| 163 | return Mips::reloc_mips_26; |
| 164 | if ((Form == MipsII::FrmI || Form == MipsII::FrmFI) |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 165 | && MI.isBranch()) |
Bruno Cardoso Lopes | 3aa035f | 2011-12-30 21:04:30 +0000 | [diff] [blame] | 166 | return Mips::reloc_mips_pc16; |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 167 | if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi) |
| 168 | return Mips::reloc_mips_hi; |
| 169 | return Mips::reloc_mips_lo; |
Bruno Cardoso Lopes | dca6cdd | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Bruno Cardoso Lopes | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 172 | unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI, |
| 173 | unsigned OpNo) const { |
Bruno Cardoso Lopes | 3aa035f | 2011-12-30 21:04:30 +0000 | [diff] [blame] | 174 | MachineOperand MO = MI.getOperand(OpNo); |
| 175 | if (MO.isGlobal()) |
| 176 | emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true); |
| 177 | else if (MO.isSymbol()) |
| 178 | emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO)); |
| 179 | else if (MO.isMBB()) |
| 180 | emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO)); |
| 181 | else |
| 182 | llvm_unreachable("Unexpected jump target operand kind."); |
Bruno Cardoso Lopes | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI, |
| 187 | unsigned OpNo) const { |
Bruno Cardoso Lopes | 3aa035f | 2011-12-30 21:04:30 +0000 | [diff] [blame] | 188 | MachineOperand MO = MI.getOperand(OpNo); |
| 189 | emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO)); |
Bruno Cardoso Lopes | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
Bruno Cardoso Lopes | c3f16b3 | 2011-10-18 17:50:36 +0000 | [diff] [blame] | 193 | unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI, |
Bruno Cardoso Lopes | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 194 | unsigned OpNo) const { |
Bruno Cardoso Lopes | c3f16b3 | 2011-10-18 17:50:36 +0000 | [diff] [blame] | 195 | // Base register is encoded in bits 20-16, offset is encoded in bits 15-0. |
| 196 | assert(MI.getOperand(OpNo).isReg()); |
| 197 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16; |
Bruno Cardoso Lopes | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 198 | return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits; |
Bruno Cardoso Lopes | c3f16b3 | 2011-10-18 17:50:36 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI, |
Bruno Cardoso Lopes | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 202 | unsigned OpNo) const { |
Bruno Cardoso Lopes | c3f16b3 | 2011-10-18 17:50:36 +0000 | [diff] [blame] | 203 | // size is encoded as size-1. |
| 204 | return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1; |
| 205 | } |
| 206 | |
| 207 | unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI, |
Bruno Cardoso Lopes | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 208 | unsigned OpNo) const { |
Bruno Cardoso Lopes | c3f16b3 | 2011-10-18 17:50:36 +0000 | [diff] [blame] | 209 | // size is encoded as pos+size-1. |
| 210 | return getMachineOpValue(MI, MI.getOperand(OpNo-1)) + |
| 211 | getMachineOpValue(MI, MI.getOperand(OpNo)) - 1; |
| 212 | } |
| 213 | |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 214 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 215 | /// operand requires relocation, record the relocation and return zero. |
| 216 | unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI, |
Bruno Cardoso Lopes | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 217 | const MachineOperand &MO) const { |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 218 | if (MO.isReg()) |
Bruno Cardoso Lopes | ce8524c | 2011-12-30 21:09:41 +0000 | [diff] [blame] | 219 | return getMipsRegisterNumbering(MO.getReg()); |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 220 | else if (MO.isImm()) |
| 221 | return static_cast<unsigned>(MO.getImm()); |
Bruno Cardoso Lopes | ad6eef4 | 2011-11-08 12:47:11 +0000 | [diff] [blame] | 222 | else if (MO.isGlobal()) { |
| 223 | if (MI.getOpcode() == Mips::ULW || MI.getOpcode() == Mips::USW || |
| 224 | MI.getOpcode() == Mips::ULH || MI.getOpcode() == Mips::ULHu) |
| 225 | emitGlobalAddressUnaligned(MO.getGlobal(), getRelocation(MI, MO), 4); |
| 226 | else if (MI.getOpcode() == Mips::USH) |
| 227 | emitGlobalAddressUnaligned(MO.getGlobal(), getRelocation(MI, MO), 8); |
| 228 | else |
| 229 | emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true); |
| 230 | } else if (MO.isSymbol()) |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 231 | emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO)); |
| 232 | else if (MO.isCPI()) |
| 233 | emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO)); |
| 234 | else if (MO.isJTI()) |
| 235 | emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO)); |
| 236 | else if (MO.isMBB()) |
| 237 | emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO)); |
| 238 | else |
| 239 | llvm_unreachable("Unable to encode MachineOperand!"); |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc, |
Bruno Cardoso Lopes | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 244 | bool MayNeedFarStub) const { |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 245 | MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc, |
Bruno Cardoso Lopes | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 246 | const_cast<GlobalValue *>(GV), 0, |
| 247 | MayNeedFarStub)); |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Bruno Cardoso Lopes | ad6eef4 | 2011-11-08 12:47:11 +0000 | [diff] [blame] | 250 | void MipsCodeEmitter::emitGlobalAddressUnaligned(const GlobalValue *GV, |
| 251 | unsigned Reloc, int Offset) const { |
| 252 | MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc, |
| 253 | const_cast<GlobalValue *>(GV), 0, false)); |
| 254 | MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset() + Offset, |
| 255 | Reloc, const_cast<GlobalValue *>(GV), 0, false)); |
| 256 | } |
| 257 | |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 258 | void MipsCodeEmitter:: |
| 259 | emitExternalSymbolAddress(const char *ES, unsigned Reloc) const { |
| 260 | MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(), |
| 261 | Reloc, ES, 0, 0, false)); |
| 262 | } |
| 263 | |
| 264 | void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const { |
| 265 | MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(), |
| 266 | Reloc, CPI, 0, false)); |
| 267 | } |
| 268 | |
| 269 | void MipsCodeEmitter:: |
| 270 | emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const { |
| 271 | MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(), |
| 272 | Reloc, JTIndex, 0, false)); |
| 273 | } |
| 274 | |
| 275 | void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB, |
Bruno Cardoso Lopes | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 276 | unsigned Reloc) const { |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 277 | MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(), |
| 278 | Reloc, BB)); |
| 279 | } |
| 280 | |
Bruno Cardoso Lopes | ad6eef4 | 2011-11-08 12:47:11 +0000 | [diff] [blame] | 281 | int MipsCodeEmitter::emitUSW(const MachineInstr &MI) { |
| 282 | unsigned src = getMachineOpValue(MI, MI.getOperand(0)); |
| 283 | unsigned base = getMachineOpValue(MI, MI.getOperand(1)); |
| 284 | unsigned offset = getMachineOpValue(MI, MI.getOperand(2)); |
| 285 | // swr src, offset(base) |
| 286 | // swl src, offset+3(base) |
| 287 | MCE.emitWordLE( |
| 288 | (0x2e << 26) | (base << 21) | (src << 16) | (offset & 0xffff)); |
| 289 | MCE.emitWordLE( |
| 290 | (0x2a << 26) | (base << 21) | (src << 16) | ((offset+3) & 0xffff)); |
| 291 | return 2; |
| 292 | } |
| 293 | |
| 294 | int MipsCodeEmitter::emitULW(const MachineInstr &MI) { |
| 295 | unsigned dst = getMachineOpValue(MI, MI.getOperand(0)); |
| 296 | unsigned base = getMachineOpValue(MI, MI.getOperand(1)); |
| 297 | unsigned offset = getMachineOpValue(MI, MI.getOperand(2)); |
| 298 | unsigned at = 1; |
| 299 | if (dst != base) { |
| 300 | // lwr dst, offset(base) |
| 301 | // lwl dst, offset+3(base) |
| 302 | MCE.emitWordLE( |
| 303 | (0x26 << 26) | (base << 21) | (dst << 16) | (offset & 0xffff)); |
| 304 | MCE.emitWordLE( |
| 305 | (0x22 << 26) | (base << 21) | (dst << 16) | ((offset+3) & 0xffff)); |
| 306 | return 2; |
| 307 | } else { |
| 308 | // lwr at, offset(base) |
| 309 | // lwl at, offset+3(base) |
| 310 | // addu dst, at, $zero |
| 311 | MCE.emitWordLE( |
| 312 | (0x26 << 26) | (base << 21) | (at << 16) | (offset & 0xffff)); |
| 313 | MCE.emitWordLE( |
| 314 | (0x22 << 26) | (base << 21) | (at << 16) | ((offset+3) & 0xffff)); |
| 315 | MCE.emitWordLE( |
| 316 | (0x0 << 26) | (at << 21) | (0x0 << 16) | (dst << 11) | (0x0 << 6) | 0x21); |
| 317 | return 3; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | int MipsCodeEmitter::emitUSH(const MachineInstr &MI) { |
| 322 | unsigned src = getMachineOpValue(MI, MI.getOperand(0)); |
| 323 | unsigned base = getMachineOpValue(MI, MI.getOperand(1)); |
| 324 | unsigned offset = getMachineOpValue(MI, MI.getOperand(2)); |
| 325 | unsigned at = 1; |
| 326 | // sb src, offset(base) |
| 327 | // srl at,src,8 |
| 328 | // sb at, offset+1(base) |
| 329 | MCE.emitWordLE( |
| 330 | (0x28 << 26) | (base << 21) | (src << 16) | (offset & 0xffff)); |
| 331 | MCE.emitWordLE( |
| 332 | (0x0 << 26) | (0x0 << 21) | (src << 16) | (at << 11) | (0x8 << 6) | 0x2); |
| 333 | MCE.emitWordLE( |
| 334 | (0x28 << 26) | (base << 21) | (at << 16) | ((offset+1) & 0xffff)); |
| 335 | return 3; |
| 336 | } |
| 337 | |
| 338 | int MipsCodeEmitter::emitULH(const MachineInstr &MI) { |
| 339 | unsigned dst = getMachineOpValue(MI, MI.getOperand(0)); |
| 340 | unsigned base = getMachineOpValue(MI, MI.getOperand(1)); |
| 341 | unsigned offset = getMachineOpValue(MI, MI.getOperand(2)); |
| 342 | unsigned at = 1; |
| 343 | // lbu at, offset(base) |
| 344 | // lb dst, offset+1(base) |
| 345 | // sll dst,dst,8 |
| 346 | // or dst,dst,at |
| 347 | MCE.emitWordLE( |
| 348 | (0x24 << 26) | (base << 21) | (at << 16) | (offset & 0xffff)); |
| 349 | MCE.emitWordLE( |
| 350 | (0x20 << 26) | (base << 21) | (dst << 16) | ((offset+1) & 0xffff)); |
| 351 | MCE.emitWordLE( |
| 352 | (0x0 << 26) | (0x0 << 21) | (dst << 16) | (dst << 11) | (0x8 << 6) | 0x0); |
| 353 | MCE.emitWordLE( |
| 354 | (0x0 << 26) | (dst << 21) | (at << 16) | (dst << 11) | (0x0 << 6) | 0x25); |
| 355 | return 4; |
| 356 | } |
| 357 | |
| 358 | int MipsCodeEmitter::emitULHu(const MachineInstr &MI) { |
| 359 | unsigned dst = getMachineOpValue(MI, MI.getOperand(0)); |
| 360 | unsigned base = getMachineOpValue(MI, MI.getOperand(1)); |
| 361 | unsigned offset = getMachineOpValue(MI, MI.getOperand(2)); |
| 362 | unsigned at = 1; |
| 363 | // lbu at, offset(base) |
| 364 | // lbu dst, offset+1(base) |
| 365 | // sll dst,dst,8 |
| 366 | // or dst,dst,at |
| 367 | MCE.emitWordLE( |
| 368 | (0x24 << 26) | (base << 21) | (at << 16) | (offset & 0xffff)); |
| 369 | MCE.emitWordLE( |
| 370 | (0x24 << 26) | (base << 21) | (dst << 16) | ((offset+1) & 0xffff)); |
| 371 | MCE.emitWordLE( |
| 372 | (0x0 << 26) | (0x0 << 21) | (dst << 16) | (dst << 11) | (0x8 << 6) | 0x0); |
| 373 | MCE.emitWordLE( |
| 374 | (0x0 << 26) | (dst << 21) | (at << 16) | (dst << 11) | (0x0 << 6) | 0x25); |
| 375 | return 4; |
| 376 | } |
| 377 | |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 378 | void MipsCodeEmitter::emitInstruction(const MachineInstr &MI) { |
| 379 | DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI); |
| 380 | |
| 381 | MCE.processDebugLoc(MI.getDebugLoc(), true); |
| 382 | |
| 383 | // Skip pseudo instructions. |
| 384 | if ((MI.getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo) |
| 385 | return; |
| 386 | |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 387 | |
| 388 | switch (MI.getOpcode()) { |
Bruno Cardoso Lopes | ad6eef4 | 2011-11-08 12:47:11 +0000 | [diff] [blame] | 389 | case Mips::USW: |
| 390 | NumEmitted += emitUSW(MI); |
| 391 | break; |
| 392 | case Mips::ULW: |
| 393 | NumEmitted += emitULW(MI); |
| 394 | break; |
| 395 | case Mips::ULH: |
| 396 | NumEmitted += emitULH(MI); |
| 397 | break; |
| 398 | case Mips::ULHu: |
| 399 | NumEmitted += emitULHu(MI); |
| 400 | break; |
| 401 | case Mips::USH: |
| 402 | NumEmitted += emitUSH(MI); |
| 403 | break; |
| 404 | |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 405 | default: |
| 406 | emitWordLE(getBinaryCodeForInstr(MI)); |
Bruno Cardoso Lopes | ad6eef4 | 2011-11-08 12:47:11 +0000 | [diff] [blame] | 407 | ++NumEmitted; // Keep track of the # of mi's emitted |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 408 | break; |
| 409 | } |
| 410 | |
| 411 | MCE.processDebugLoc(MI.getDebugLoc(), false); |
| 412 | } |
| 413 | |
| 414 | void MipsCodeEmitter::emitWordLE(unsigned Word) { |
| 415 | DEBUG(errs() << " 0x"; |
| 416 | errs().write_hex(Word) << "\n"); |
| 417 | MCE.emitWordLE(Word); |
| 418 | } |
| 419 | |
| 420 | /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips |
| 421 | /// code to the specified MCE object. |
| 422 | FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM, |
Bruno Cardoso Lopes | 47b92f3 | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 423 | JITCodeEmitter &JCE) { |
Bruno Cardoso Lopes | c4cc40c | 2011-09-14 03:00:41 +0000 | [diff] [blame] | 424 | return new MipsCodeEmitter(TM, JCE); |
| 425 | } |
| 426 | |
Bruno Cardoso Lopes | c3f16b3 | 2011-10-18 17:50:36 +0000 | [diff] [blame] | 427 | #include "MipsGenCodeEmitter.inc" |