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/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineOperand.h"
30 #include "llvm/CodeGen/Passes.h"
31 #include "llvm/IR/Constants.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/PassManager.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h"
43 STATISTIC(NumEmitted, "Number of machine instructions emitted");
47 class MipsCodeEmitter : public MachineFunctionPass {
49 const MipsInstrInfo *II;
51 const MipsSubtarget *Subtarget;
54 const std::vector<MachineConstantPoolEntry> *MCPEs;
55 const std::vector<MachineJumpTableEntry> *MJTEs;
58 void getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.addRequired<MachineModuleInfo> ();
60 MachineFunctionPass::getAnalysisUsage(AU);
66 MipsCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce) :
67 MachineFunctionPass(ID), JTI(0),
68 II((const MipsInstrInfo *) tm.getInstrInfo()),
69 TD(tm.getDataLayout()), TM(tm), MCE(mce), MCPEs(0), MJTEs(0),
70 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(const MachineInstr &MI);
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,
110 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,
120 char MipsCodeEmitter::ID = 0;
122 bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
123 JTI = const_cast<MipsTargetMachine&>(
124 static_cast<const MipsTargetMachine&>(MF.getTarget())).getJITInfo();
125 II = ((const MipsTargetMachine&) MF.getTarget()).getInstrInfo();
126 TD = ((const MipsTargetMachine&) MF.getTarget()).getDataLayout();
127 Subtarget = &TM.getSubtarget<MipsSubtarget> ();
128 MCPEs = &MF.getConstantPool()->getConstants();
130 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
131 JTI->Initialize(MF, IsPIC, Subtarget->isLittle());
132 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
135 DEBUG(errs() << "JITTing function '"
136 << MF.getName() << "'\n");
137 MCE.startFunction(MF);
139 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
141 MCE.StartMachineBasicBlock(MBB);
142 for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
143 E = MBB->instr_end(); I != E; ++I)
146 } while (MCE.finishFunction(MF));
151 unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI,
152 const MachineOperand &MO) const {
153 // NOTE: This relocations are for static.
154 uint64_t TSFlags = MI.getDesc().TSFlags;
155 uint64_t Form = TSFlags & MipsII::FormMask;
156 if (Form == MipsII::FrmJ)
157 return Mips::reloc_mips_26;
158 if ((Form == MipsII::FrmI || Form == MipsII::FrmFI)
160 return Mips::reloc_mips_pc16;
161 if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
162 return Mips::reloc_mips_hi;
163 return Mips::reloc_mips_lo;
166 unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI,
167 unsigned OpNo) const {
168 MachineOperand MO = MI.getOperand(OpNo);
170 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
171 else if (MO.isSymbol())
172 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
174 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
176 llvm_unreachable("Unexpected jump target operand kind.");
180 unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
181 unsigned OpNo) const {
182 MachineOperand MO = MI.getOperand(OpNo);
183 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
187 unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI,
188 unsigned OpNo) const {
189 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
190 assert(MI.getOperand(OpNo).isReg());
191 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16;
192 return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
195 unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI,
196 unsigned OpNo) const {
197 // size is encoded as size-1.
198 return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
201 unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI,
202 unsigned OpNo) const {
203 // size is encoded as pos+size-1.
204 return getMachineOpValue(MI, MI.getOperand(OpNo-1)) +
205 getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
208 /// getMachineOpValue - Return binary encoding of operand. If the machine
209 /// operand requires relocation, record the relocation and return zero.
210 unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI,
211 const MachineOperand &MO) const {
213 return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
215 return static_cast<unsigned>(MO.getImm());
216 else if (MO.isGlobal())
217 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
218 else if (MO.isSymbol())
219 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
221 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
223 emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
225 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
227 llvm_unreachable("Unable to encode MachineOperand!");
231 void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
232 bool MayNeedFarStub) const {
233 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
234 const_cast<GlobalValue *>(GV), 0,
238 void MipsCodeEmitter::emitGlobalAddressUnaligned(const GlobalValue *GV,
239 unsigned Reloc, int Offset) const {
240 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
241 const_cast<GlobalValue *>(GV), 0, false));
242 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset() + Offset,
243 Reloc, const_cast<GlobalValue *>(GV), 0, false));
246 void MipsCodeEmitter::
247 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
248 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
252 void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
253 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
254 Reloc, CPI, 0, false));
257 void MipsCodeEmitter::
258 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
259 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
260 Reloc, JTIndex, 0, false));
263 void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
264 unsigned Reloc) const {
265 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
269 void MipsCodeEmitter::emitInstruction(const MachineInstr &MI) {
270 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
272 MCE.processDebugLoc(MI.getDebugLoc(), true);
274 // Skip pseudo instructions.
275 if ((MI.getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo)
278 emitWord(getBinaryCodeForInstr(MI));
279 ++NumEmitted; // Keep track of the # of mi's emitted
281 MCE.processDebugLoc(MI.getDebugLoc(), false);
284 void MipsCodeEmitter::emitWord(unsigned Word) {
285 DEBUG(errs() << " 0x";
286 errs().write_hex(Word) << "\n");
287 if (Subtarget->isLittle())
288 MCE.emitWordLE(Word);
290 MCE.emitWordBE(Word);
293 /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
294 /// code to the specified MCE object.
295 FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM,
296 JITCodeEmitter &JCE) {
297 return new MipsCodeEmitter(TM, JCE);
300 #include "MipsGenCodeEmitter.inc"