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/MachineJumpTableInfo.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.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;
109 unsigned getBranchTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
110 unsigned getMemEncoding(const MachineInstr &MI, unsigned OpNo) const;
111 unsigned getMemEncodingMMImm12(const MachineInstr &MI, unsigned OpNo) const;
112 unsigned getSizeExtEncoding(const MachineInstr &MI, unsigned OpNo) const;
113 unsigned getSizeInsEncoding(const MachineInstr &MI, unsigned OpNo) const;
115 void emitGlobalAddressUnaligned(const GlobalValue *GV, unsigned Reloc,
118 /// Expand pseudo instructions with accumulator register operands.
119 void expandACCInstr(MachineBasicBlock::instr_iterator MI,
120 MachineBasicBlock &MBB, unsigned Opc) const;
122 /// \brief Expand pseudo instruction. Return true if MI was expanded.
123 bool expandPseudos(MachineBasicBlock::instr_iterator &MI,
124 MachineBasicBlock &MBB) const;
128 char MipsCodeEmitter::ID = 0;
130 bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
131 MipsTargetMachine &Target = static_cast<MipsTargetMachine &>(
132 const_cast<TargetMachine &>(MF.getTarget()));
134 JTI = Target.getJITInfo();
135 II = Target.getInstrInfo();
136 TD = Target.getDataLayout();
137 Subtarget = &TM.getSubtarget<MipsSubtarget> ();
138 MCPEs = &MF.getConstantPool()->getConstants();
140 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
141 JTI->Initialize(MF, IsPIC, Subtarget->isLittle());
142 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
145 DEBUG(errs() << "JITTing function '"
146 << MF.getName() << "'\n");
147 MCE.startFunction(MF);
149 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
151 MCE.StartMachineBasicBlock(MBB);
152 for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
153 E = MBB->instr_end(); I != E;)
154 emitInstruction(*I++, *MBB);
156 } while (MCE.finishFunction(MF));
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)
170 return Mips::reloc_mips_pc16;
171 if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
172 return Mips::reloc_mips_hi;
173 return Mips::reloc_mips_lo;
176 unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI,
177 unsigned OpNo) const {
178 MachineOperand MO = MI.getOperand(OpNo);
180 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
181 else if (MO.isSymbol())
182 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
184 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
186 llvm_unreachable("Unexpected jump target operand kind.");
190 unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
191 unsigned OpNo) const {
192 MachineOperand MO = MI.getOperand(OpNo);
193 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
197 unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI,
198 unsigned OpNo) const {
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;
202 return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
205 unsigned MipsCodeEmitter::getMemEncodingMMImm12(const MachineInstr &MI,
206 unsigned OpNo) const {
207 llvm_unreachable("Unimplemented function.");
211 unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI,
212 unsigned OpNo) const {
213 // size is encoded as size-1.
214 return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
217 unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI,
218 unsigned OpNo) const {
219 // size is encoded as pos+size-1.
220 return getMachineOpValue(MI, MI.getOperand(OpNo-1)) +
221 getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
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,
227 const MachineOperand &MO) const {
229 return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
231 return static_cast<unsigned>(MO.getImm());
232 else if (MO.isGlobal())
233 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
234 else if (MO.isSymbol())
235 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
237 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
239 emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
241 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
243 llvm_unreachable("Unable to encode MachineOperand!");
247 void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
248 bool MayNeedFarStub) const {
249 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
250 const_cast<GlobalValue *>(GV), 0,
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));
262 void MipsCodeEmitter::
263 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
264 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
268 void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
269 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
270 Reloc, CPI, 0, false));
273 void MipsCodeEmitter::
274 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
275 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
276 Reloc, JTIndex, 0, false));
279 void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
280 unsigned Reloc) const {
281 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
285 void MipsCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI,
286 MachineBasicBlock &MBB) {
287 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI);
289 // Expand pseudo instruction. Skip if MI was not expanded.
290 if (((MI->getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo) &&
291 !expandPseudos(MI, MBB))
294 MCE.processDebugLoc(MI->getDebugLoc(), true);
296 emitWord(getBinaryCodeForInstr(*MI));
297 ++NumEmitted; // Keep track of the # of mi's emitted
299 MCE.processDebugLoc(MI->getDebugLoc(), false);
302 void MipsCodeEmitter::emitWord(unsigned Word) {
303 DEBUG(errs() << " 0x";
304 errs().write_hex(Word) << "\n");
305 if (Subtarget->isLittle())
306 MCE.emitWordLE(Word);
308 MCE.emitWordBE(Word);
311 void MipsCodeEmitter::expandACCInstr(MachineBasicBlock::instr_iterator MI,
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());
319 bool MipsCodeEmitter::expandPseudos(MachineBasicBlock::instr_iterator &MI,
320 MachineBasicBlock &MBB) const {
321 switch (MI->getOpcode()) {
323 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::SLL), Mips::ZERO)
324 .addReg(Mips::ZERO).addImm(0);
327 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BEQ)).addReg(Mips::ZERO)
328 .addReg(Mips::ZERO).addOperand(MI->getOperand(0));
331 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BREAK)).addImm(0)
334 case Mips::JALRPseudo:
335 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::JALR), Mips::RA)
336 .addReg(MI->getOperand(0).getReg());
338 case Mips::PseudoMULT:
339 expandACCInstr(MI, MBB, Mips::MULT);
341 case Mips::PseudoMULTu:
342 expandACCInstr(MI, MBB, Mips::MULTu);
344 case Mips::PseudoSDIV:
345 expandACCInstr(MI, MBB, Mips::SDIV);
347 case Mips::PseudoUDIV:
348 expandACCInstr(MI, MBB, Mips::UDIV);
350 case Mips::PseudoMADD:
351 expandACCInstr(MI, MBB, Mips::MADD);
353 case Mips::PseudoMADDU:
354 expandACCInstr(MI, MBB, Mips::MADDU);
356 case Mips::PseudoMSUB:
357 expandACCInstr(MI, MBB, Mips::MSUB);
359 case Mips::PseudoMSUBU:
360 expandACCInstr(MI, MBB, Mips::MSUBU);
366 (MI--)->eraseFromBundle();
370 /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
371 /// code to the specified MCE object.
372 FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM,
373 JITCodeEmitter &JCE) {
374 return new MipsCodeEmitter(TM, JCE);
377 #include "MipsGenCodeEmitter.inc"