1 //===-- Mips/MipsCodeEmitter.cpp - Convert Mips Code to Machine Code ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===---------------------------------------------------------------------===//
10 // This file contains the pass that transforms the Mips machine instructions
11 // into relocatable machine code.
13 //===---------------------------------------------------------------------===//
15 #define DEBUG_TYPE "jit"
17 #include "MCTargetDesc/MipsBaseInfo.h"
18 #include "MipsInstrInfo.h"
19 #include "MipsRelocations.h"
20 #include "MipsSubtarget.h"
21 #include "MipsTargetMachine.h"
22 #include "llvm/ADT/Statistic.h"
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/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineJumpTableInfo.h"
29 #include "llvm/CodeGen/MachineModuleInfo.h"
30 #include "llvm/CodeGen/MachineOperand.h"
31 #include "llvm/CodeGen/Passes.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/DerivedTypes.h"
34 #include "llvm/PassManager.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/raw_ostream.h"
44 STATISTIC(NumEmitted, "Number of machine instructions emitted");
48 class MipsCodeEmitter : public MachineFunctionPass {
50 const MipsInstrInfo *II;
52 const MipsSubtarget *Subtarget;
55 const std::vector<MachineConstantPoolEntry> *MCPEs;
56 const std::vector<MachineJumpTableEntry> *MJTEs;
59 void getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.addRequired<MachineModuleInfo> ();
61 MachineFunctionPass::getAnalysisUsage(AU);
67 MipsCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
68 : MachineFunctionPass(ID), JTI(0), II(0), TD(0),
69 TM(tm), MCE(mce), MCPEs(0), MJTEs(0),
70 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
72 bool runOnMachineFunction(MachineFunction &MF);
74 virtual const char *getPassName() const {
75 return "Mips Machine Code Emitter";
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;
83 void emitInstruction(MachineBasicBlock::instr_iterator MI,
84 MachineBasicBlock &MBB);
88 void emitWord(unsigned Word);
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;
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;
104 unsigned getRelocation(const MachineInstr &MI,
105 const MachineOperand &MO) const;
107 unsigned getJumpTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
108 unsigned getJumpTargetOpValueMM(const MachineInstr &MI, unsigned OpNo) const;
109 unsigned getBranchTargetOpValueMM(const MachineInstr &MI,
110 unsigned OpNo) const;
112 unsigned getBranchTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
113 unsigned getMemEncoding(const MachineInstr &MI, unsigned OpNo) const;
114 unsigned getMemEncodingMMImm12(const MachineInstr &MI, unsigned OpNo) const;
115 unsigned getMSAMemEncoding(const MachineInstr &MI, unsigned OpNo) const;
116 unsigned getSizeExtEncoding(const MachineInstr &MI, unsigned OpNo) const;
117 unsigned getSizeInsEncoding(const MachineInstr &MI, unsigned OpNo) const;
118 unsigned getLSAImmEncoding(const MachineInstr &MI, unsigned OpNo) const;
120 void emitGlobalAddressUnaligned(const GlobalValue *GV, unsigned Reloc,
123 /// Expand pseudo instructions with accumulator register operands.
124 void expandACCInstr(MachineBasicBlock::instr_iterator MI,
125 MachineBasicBlock &MBB, unsigned Opc) const;
127 /// \brief Expand pseudo instruction. Return true if MI was expanded.
128 bool expandPseudos(MachineBasicBlock::instr_iterator &MI,
129 MachineBasicBlock &MBB) const;
133 char MipsCodeEmitter::ID = 0;
135 bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
136 MipsTargetMachine &Target = static_cast<MipsTargetMachine &>(
137 const_cast<TargetMachine &>(MF.getTarget()));
139 JTI = Target.getJITInfo();
140 II = Target.getInstrInfo();
141 TD = Target.getDataLayout();
142 Subtarget = &TM.getSubtarget<MipsSubtarget> ();
143 MCPEs = &MF.getConstantPool()->getConstants();
145 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
146 JTI->Initialize(MF, IsPIC, Subtarget->isLittle());
147 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
150 DEBUG(errs() << "JITTing function '"
151 << MF.getName() << "'\n");
152 MCE.startFunction(MF);
154 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
156 MCE.StartMachineBasicBlock(MBB);
157 for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
158 E = MBB->instr_end(); I != E;)
159 emitInstruction(*I++, *MBB);
161 } while (MCE.finishFunction(MF));
166 unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI,
167 const MachineOperand &MO) const {
168 // NOTE: This relocations are for static.
169 uint64_t TSFlags = MI.getDesc().TSFlags;
170 uint64_t Form = TSFlags & MipsII::FormMask;
171 if (Form == MipsII::FrmJ)
172 return Mips::reloc_mips_26;
173 if ((Form == MipsII::FrmI || Form == MipsII::FrmFI)
175 return Mips::reloc_mips_pc16;
176 if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
177 return Mips::reloc_mips_hi;
178 return Mips::reloc_mips_lo;
181 unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI,
182 unsigned OpNo) const {
183 MachineOperand MO = MI.getOperand(OpNo);
185 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
186 else if (MO.isSymbol())
187 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
189 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
191 llvm_unreachable("Unexpected jump target operand kind.");
195 unsigned MipsCodeEmitter::getJumpTargetOpValueMM(const MachineInstr &MI,
196 unsigned OpNo) const {
197 llvm_unreachable("Unimplemented function.");
201 unsigned MipsCodeEmitter::getBranchTargetOpValueMM(const MachineInstr &MI,
202 unsigned OpNo) const {
203 llvm_unreachable("Unimplemented function.");
207 unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
208 unsigned OpNo) const {
209 MachineOperand MO = MI.getOperand(OpNo);
210 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
214 unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI,
215 unsigned OpNo) const {
216 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
217 assert(MI.getOperand(OpNo).isReg());
218 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16;
219 return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
222 unsigned MipsCodeEmitter::getMemEncodingMMImm12(const MachineInstr &MI,
223 unsigned OpNo) const {
224 llvm_unreachable("Unimplemented function.");
228 unsigned MipsCodeEmitter::getMSAMemEncoding(const MachineInstr &MI,
229 unsigned OpNo) const {
230 llvm_unreachable("Unimplemented function.");
234 unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI,
235 unsigned OpNo) const {
236 // size is encoded as size-1.
237 return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
240 unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI,
241 unsigned OpNo) const {
242 // size is encoded as pos+size-1.
243 return getMachineOpValue(MI, MI.getOperand(OpNo-1)) +
244 getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
247 unsigned MipsCodeEmitter::getLSAImmEncoding(const MachineInstr &MI,
248 unsigned OpNo) const {
249 llvm_unreachable("Unimplemented function.");
253 /// getMachineOpValue - Return binary encoding of operand. If the machine
254 /// operand requires relocation, record the relocation and return zero.
255 unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI,
256 const MachineOperand &MO) const {
258 return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
260 return static_cast<unsigned>(MO.getImm());
261 else if (MO.isGlobal())
262 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
263 else if (MO.isSymbol())
264 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
266 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
268 emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
270 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
272 llvm_unreachable("Unable to encode MachineOperand!");
276 void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
277 bool MayNeedFarStub) const {
278 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
279 const_cast<GlobalValue *>(GV), 0,
283 void MipsCodeEmitter::emitGlobalAddressUnaligned(const GlobalValue *GV,
284 unsigned Reloc, int Offset) const {
285 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
286 const_cast<GlobalValue *>(GV), 0, false));
287 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset() + Offset,
288 Reloc, const_cast<GlobalValue *>(GV), 0, false));
291 void MipsCodeEmitter::
292 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
293 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
297 void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
298 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
299 Reloc, CPI, 0, false));
302 void MipsCodeEmitter::
303 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
304 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
305 Reloc, JTIndex, 0, false));
308 void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
309 unsigned Reloc) const {
310 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
314 void MipsCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI,
315 MachineBasicBlock &MBB) {
316 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI);
318 // Expand pseudo instruction. Skip if MI was not expanded.
319 if (((MI->getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo) &&
320 !expandPseudos(MI, MBB))
323 MCE.processDebugLoc(MI->getDebugLoc(), true);
325 emitWord(getBinaryCodeForInstr(*MI));
326 ++NumEmitted; // Keep track of the # of mi's emitted
328 MCE.processDebugLoc(MI->getDebugLoc(), false);
331 void MipsCodeEmitter::emitWord(unsigned Word) {
332 DEBUG(errs() << " 0x";
333 errs().write_hex(Word) << "\n");
334 if (Subtarget->isLittle())
335 MCE.emitWordLE(Word);
337 MCE.emitWordBE(Word);
340 void MipsCodeEmitter::expandACCInstr(MachineBasicBlock::instr_iterator MI,
341 MachineBasicBlock &MBB,
342 unsigned Opc) const {
343 // Expand "pseudomult $ac0, $t0, $t1" to "mult $t0, $t1".
344 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Opc))
345 .addReg(MI->getOperand(1).getReg()).addReg(MI->getOperand(2).getReg());
348 bool MipsCodeEmitter::expandPseudos(MachineBasicBlock::instr_iterator &MI,
349 MachineBasicBlock &MBB) const {
350 switch (MI->getOpcode()) {
352 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::SLL), Mips::ZERO)
353 .addReg(Mips::ZERO).addImm(0);
356 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BEQ)).addReg(Mips::ZERO)
357 .addReg(Mips::ZERO).addOperand(MI->getOperand(0));
360 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BREAK)).addImm(0)
363 case Mips::JALRPseudo:
364 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::JALR), Mips::RA)
365 .addReg(MI->getOperand(0).getReg());
367 case Mips::PseudoMULT:
368 expandACCInstr(MI, MBB, Mips::MULT);
370 case Mips::PseudoMULTu:
371 expandACCInstr(MI, MBB, Mips::MULTu);
373 case Mips::PseudoSDIV:
374 expandACCInstr(MI, MBB, Mips::SDIV);
376 case Mips::PseudoUDIV:
377 expandACCInstr(MI, MBB, Mips::UDIV);
379 case Mips::PseudoMADD:
380 expandACCInstr(MI, MBB, Mips::MADD);
382 case Mips::PseudoMADDU:
383 expandACCInstr(MI, MBB, Mips::MADDU);
385 case Mips::PseudoMSUB:
386 expandACCInstr(MI, MBB, Mips::MSUB);
388 case Mips::PseudoMSUBU:
389 expandACCInstr(MI, MBB, Mips::MSUBU);
395 (MI--)->eraseFromBundle();
399 /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
400 /// code to the specified MCE object.
401 FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM,
402 JITCodeEmitter &JCE) {
403 return new MipsCodeEmitter(TM, JCE);
406 #include "MipsGenCodeEmitter.inc"