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),
69 II((const MipsInstrInfo *) tm.getInstrInfo()), TD(tm.getDataLayout()),
70 TM(tm), MCE(mce), MCPEs(0), MJTEs(0),
71 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
73 bool runOnMachineFunction(MachineFunction &MF);
75 virtual const char *getPassName() const {
76 return "Mips Machine Code Emitter";
79 /// getBinaryCodeForInstr - This function, generated by the
80 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
81 /// machine instructions.
82 uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const;
84 void emitInstruction(MachineBasicBlock::instr_iterator MI,
85 MachineBasicBlock &MBB);
89 void emitWord(unsigned Word);
91 /// Routines that handle operands which add machine relocations which are
92 /// fixed up by the relocation stage.
93 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
94 bool MayNeedFarStub) const;
95 void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
96 void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
97 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const;
98 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc) const;
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;
105 unsigned getRelocation(const MachineInstr &MI,
106 const MachineOperand &MO) const;
108 unsigned getJumpTargetOpValue(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 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 /// \brief Expand pseudo instruction. Return true if MI was expanded.
119 bool expandPseudos(MachineBasicBlock::instr_iterator &MI,
120 MachineBasicBlock &MBB) const;
124 char MipsCodeEmitter::ID = 0;
126 bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
127 MipsTargetMachine &Target = static_cast<MipsTargetMachine &>(
128 const_cast<TargetMachine &>(MF.getTarget()));
130 JTI = Target.getJITInfo();
131 II = Target.getInstrInfo();
132 TD = Target.getDataLayout();
133 Subtarget = &TM.getSubtarget<MipsSubtarget> ();
134 MCPEs = &MF.getConstantPool()->getConstants();
136 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
137 JTI->Initialize(MF, IsPIC, Subtarget->isLittle());
138 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
141 DEBUG(errs() << "JITTing function '"
142 << MF.getName() << "'\n");
143 MCE.startFunction(MF);
145 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
147 MCE.StartMachineBasicBlock(MBB);
148 for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
149 E = MBB->instr_end(); I != E;)
150 emitInstruction(*I++, *MBB);
152 } while (MCE.finishFunction(MF));
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)
166 return Mips::reloc_mips_pc16;
167 if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
168 return Mips::reloc_mips_hi;
169 return Mips::reloc_mips_lo;
172 unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI,
173 unsigned OpNo) const {
174 MachineOperand MO = MI.getOperand(OpNo);
176 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
177 else if (MO.isSymbol())
178 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
180 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
182 llvm_unreachable("Unexpected jump target operand kind.");
186 unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
187 unsigned OpNo) const {
188 MachineOperand MO = MI.getOperand(OpNo);
189 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
193 unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI,
194 unsigned OpNo) const {
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;
198 return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
201 unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI,
202 unsigned OpNo) const {
203 // size is encoded as size-1.
204 return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
207 unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI,
208 unsigned OpNo) const {
209 // size is encoded as pos+size-1.
210 return getMachineOpValue(MI, MI.getOperand(OpNo-1)) +
211 getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
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,
217 const MachineOperand &MO) const {
219 return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
221 return static_cast<unsigned>(MO.getImm());
222 else if (MO.isGlobal())
223 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
224 else if (MO.isSymbol())
225 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
227 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
229 emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
231 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
233 llvm_unreachable("Unable to encode MachineOperand!");
237 void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
238 bool MayNeedFarStub) const {
239 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
240 const_cast<GlobalValue *>(GV), 0,
244 void MipsCodeEmitter::emitGlobalAddressUnaligned(const GlobalValue *GV,
245 unsigned Reloc, int Offset) const {
246 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
247 const_cast<GlobalValue *>(GV), 0, false));
248 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset() + Offset,
249 Reloc, const_cast<GlobalValue *>(GV), 0, false));
252 void MipsCodeEmitter::
253 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
254 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
258 void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
259 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
260 Reloc, CPI, 0, false));
263 void MipsCodeEmitter::
264 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
265 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
266 Reloc, JTIndex, 0, false));
269 void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
270 unsigned Reloc) const {
271 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
275 void MipsCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI,
276 MachineBasicBlock &MBB) {
277 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI);
279 // Expand pseudo instruction. Skip if MI was not expanded.
280 if (((MI->getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo) &&
281 !expandPseudos(MI, MBB))
284 MCE.processDebugLoc(MI->getDebugLoc(), true);
286 emitWord(getBinaryCodeForInstr(*MI));
287 ++NumEmitted; // Keep track of the # of mi's emitted
289 MCE.processDebugLoc(MI->getDebugLoc(), false);
292 void MipsCodeEmitter::emitWord(unsigned Word) {
293 DEBUG(errs() << " 0x";
294 errs().write_hex(Word) << "\n");
295 if (Subtarget->isLittle())
296 MCE.emitWordLE(Word);
298 MCE.emitWordBE(Word);
301 bool MipsCodeEmitter::expandPseudos(MachineBasicBlock::instr_iterator &MI,
302 MachineBasicBlock &MBB) const {
303 switch (MI->getOpcode()) {
305 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::SLL), Mips::ZERO)
306 .addReg(Mips::ZERO).addImm(0);
308 case Mips::JALRPseudo:
309 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::JALR), Mips::RA)
310 .addReg(MI->getOperand(0).getReg());
316 (MI--)->eraseFromBundle();
320 /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
321 /// code to the specified MCE object.
322 FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM,
323 JITCodeEmitter &JCE) {
324 return new MipsCodeEmitter(TM, JCE);
327 #include "MipsGenCodeEmitter.inc"