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;
108 unsigned getJumpTargetOpValueMM(const MachineInstr &MI, unsigned OpNo) const;
110 unsigned getBranchTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
111 unsigned getMemEncoding(const MachineInstr &MI, unsigned OpNo) const;
112 unsigned getMemEncodingMMImm12(const MachineInstr &MI, unsigned OpNo) const;
113 unsigned getSizeExtEncoding(const MachineInstr &MI, unsigned OpNo) const;
114 unsigned getSizeInsEncoding(const MachineInstr &MI, unsigned OpNo) const;
116 void emitGlobalAddressUnaligned(const GlobalValue *GV, unsigned Reloc,
119 /// Expand pseudo instructions with accumulator register operands.
120 void expandACCInstr(MachineBasicBlock::instr_iterator MI,
121 MachineBasicBlock &MBB, unsigned Opc) const;
123 /// \brief Expand pseudo instruction. Return true if MI was expanded.
124 bool expandPseudos(MachineBasicBlock::instr_iterator &MI,
125 MachineBasicBlock &MBB) const;
129 char MipsCodeEmitter::ID = 0;
131 bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
132 MipsTargetMachine &Target = static_cast<MipsTargetMachine &>(
133 const_cast<TargetMachine &>(MF.getTarget()));
135 JTI = Target.getJITInfo();
136 II = Target.getInstrInfo();
137 TD = Target.getDataLayout();
138 Subtarget = &TM.getSubtarget<MipsSubtarget> ();
139 MCPEs = &MF.getConstantPool()->getConstants();
141 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
142 JTI->Initialize(MF, IsPIC, Subtarget->isLittle());
143 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
146 DEBUG(errs() << "JITTing function '"
147 << MF.getName() << "'\n");
148 MCE.startFunction(MF);
150 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
152 MCE.StartMachineBasicBlock(MBB);
153 for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
154 E = MBB->instr_end(); I != E;)
155 emitInstruction(*I++, *MBB);
157 } while (MCE.finishFunction(MF));
162 unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI,
163 const MachineOperand &MO) const {
164 // NOTE: This relocations are for static.
165 uint64_t TSFlags = MI.getDesc().TSFlags;
166 uint64_t Form = TSFlags & MipsII::FormMask;
167 if (Form == MipsII::FrmJ)
168 return Mips::reloc_mips_26;
169 if ((Form == MipsII::FrmI || Form == MipsII::FrmFI)
171 return Mips::reloc_mips_pc16;
172 if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
173 return Mips::reloc_mips_hi;
174 return Mips::reloc_mips_lo;
177 unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI,
178 unsigned OpNo) const {
179 MachineOperand MO = MI.getOperand(OpNo);
181 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
182 else if (MO.isSymbol())
183 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
185 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
187 llvm_unreachable("Unexpected jump target operand kind.");
191 unsigned MipsCodeEmitter::getJumpTargetOpValueMM(const MachineInstr &MI,
192 unsigned OpNo) const {
193 llvm_unreachable("Unimplemented function.");
197 unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
198 unsigned OpNo) const {
199 MachineOperand MO = MI.getOperand(OpNo);
200 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
204 unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI,
205 unsigned OpNo) const {
206 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
207 assert(MI.getOperand(OpNo).isReg());
208 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16;
209 return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
212 unsigned MipsCodeEmitter::getMemEncodingMMImm12(const MachineInstr &MI,
213 unsigned OpNo) const {
214 llvm_unreachable("Unimplemented function.");
218 unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI,
219 unsigned OpNo) const {
220 // size is encoded as size-1.
221 return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
224 unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI,
225 unsigned OpNo) const {
226 // size is encoded as pos+size-1.
227 return getMachineOpValue(MI, MI.getOperand(OpNo-1)) +
228 getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
231 /// getMachineOpValue - Return binary encoding of operand. If the machine
232 /// operand requires relocation, record the relocation and return zero.
233 unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI,
234 const MachineOperand &MO) const {
236 return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
238 return static_cast<unsigned>(MO.getImm());
239 else if (MO.isGlobal())
240 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
241 else if (MO.isSymbol())
242 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
244 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
246 emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
248 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
250 llvm_unreachable("Unable to encode MachineOperand!");
254 void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
255 bool MayNeedFarStub) const {
256 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
257 const_cast<GlobalValue *>(GV), 0,
261 void MipsCodeEmitter::emitGlobalAddressUnaligned(const GlobalValue *GV,
262 unsigned Reloc, int Offset) const {
263 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
264 const_cast<GlobalValue *>(GV), 0, false));
265 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset() + Offset,
266 Reloc, const_cast<GlobalValue *>(GV), 0, false));
269 void MipsCodeEmitter::
270 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
271 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
275 void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
276 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
277 Reloc, CPI, 0, false));
280 void MipsCodeEmitter::
281 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
282 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
283 Reloc, JTIndex, 0, false));
286 void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
287 unsigned Reloc) const {
288 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
292 void MipsCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI,
293 MachineBasicBlock &MBB) {
294 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI);
296 // Expand pseudo instruction. Skip if MI was not expanded.
297 if (((MI->getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo) &&
298 !expandPseudos(MI, MBB))
301 MCE.processDebugLoc(MI->getDebugLoc(), true);
303 emitWord(getBinaryCodeForInstr(*MI));
304 ++NumEmitted; // Keep track of the # of mi's emitted
306 MCE.processDebugLoc(MI->getDebugLoc(), false);
309 void MipsCodeEmitter::emitWord(unsigned Word) {
310 DEBUG(errs() << " 0x";
311 errs().write_hex(Word) << "\n");
312 if (Subtarget->isLittle())
313 MCE.emitWordLE(Word);
315 MCE.emitWordBE(Word);
318 void MipsCodeEmitter::expandACCInstr(MachineBasicBlock::instr_iterator MI,
319 MachineBasicBlock &MBB,
320 unsigned Opc) const {
321 // Expand "pseudomult $ac0, $t0, $t1" to "mult $t0, $t1".
322 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Opc))
323 .addReg(MI->getOperand(1).getReg()).addReg(MI->getOperand(2).getReg());
326 bool MipsCodeEmitter::expandPseudos(MachineBasicBlock::instr_iterator &MI,
327 MachineBasicBlock &MBB) const {
328 switch (MI->getOpcode()) {
330 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::SLL), Mips::ZERO)
331 .addReg(Mips::ZERO).addImm(0);
334 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BEQ)).addReg(Mips::ZERO)
335 .addReg(Mips::ZERO).addOperand(MI->getOperand(0));
338 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BREAK)).addImm(0)
341 case Mips::JALRPseudo:
342 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::JALR), Mips::RA)
343 .addReg(MI->getOperand(0).getReg());
345 case Mips::PseudoMULT:
346 expandACCInstr(MI, MBB, Mips::MULT);
348 case Mips::PseudoMULTu:
349 expandACCInstr(MI, MBB, Mips::MULTu);
351 case Mips::PseudoSDIV:
352 expandACCInstr(MI, MBB, Mips::SDIV);
354 case Mips::PseudoUDIV:
355 expandACCInstr(MI, MBB, Mips::UDIV);
357 case Mips::PseudoMADD:
358 expandACCInstr(MI, MBB, Mips::MADD);
360 case Mips::PseudoMADDU:
361 expandACCInstr(MI, MBB, Mips::MADDU);
363 case Mips::PseudoMSUB:
364 expandACCInstr(MI, MBB, Mips::MSUB);
366 case Mips::PseudoMSUBU:
367 expandACCInstr(MI, MBB, Mips::MSUBU);
373 (MI--)->eraseFromBundle();
377 /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
378 /// code to the specified MCE object.
379 FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM,
380 JITCodeEmitter &JCE) {
381 return new MipsCodeEmitter(TM, JCE);
384 #include "MipsGenCodeEmitter.inc"