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 "MipsInstrInfo.h"
18 #include "MipsRelocations.h"
19 #include "MipsSubtarget.h"
20 #include "MipsTargetMachine.h"
21 #include "MCTargetDesc/MipsBaseInfo.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/Constants.h"
32 #include "llvm/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.getTargetData()), 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 emitWordLE(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 int emitULW(const MachineInstr &MI);
116 int emitUSW(const MachineInstr &MI);
117 int emitULH(const MachineInstr &MI);
118 int emitULHu(const MachineInstr &MI);
119 int emitUSH(const MachineInstr &MI);
121 void emitGlobalAddressUnaligned(const GlobalValue *GV, unsigned Reloc,
126 char MipsCodeEmitter::ID = 0;
128 bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
129 JTI = ((MipsTargetMachine&) MF.getTarget()).getJITInfo();
130 II = ((const MipsTargetMachine&) MF.getTarget()).getInstrInfo();
131 TD = ((const MipsTargetMachine&) MF.getTarget()).getTargetData();
132 Subtarget = &TM.getSubtarget<MipsSubtarget> ();
133 MCPEs = &MF.getConstantPool()->getConstants();
135 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
136 JTI->Initialize(MF, IsPIC);
137 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
140 DEBUG(errs() << "JITTing function '"
141 << MF.getName() << "'\n");
142 MCE.startFunction(MF);
144 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
146 MCE.StartMachineBasicBlock(MBB);
147 for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
148 E = MBB->instr_end(); I != E; ++I)
151 } while (MCE.finishFunction(MF));
156 unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI,
157 const MachineOperand &MO) const {
158 // NOTE: This relocations are for static.
159 uint64_t TSFlags = MI.getDesc().TSFlags;
160 uint64_t Form = TSFlags & MipsII::FormMask;
161 if (Form == MipsII::FrmJ)
162 return Mips::reloc_mips_26;
163 if ((Form == MipsII::FrmI || Form == MipsII::FrmFI)
165 return Mips::reloc_mips_pc16;
166 if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
167 return Mips::reloc_mips_hi;
168 return Mips::reloc_mips_lo;
171 unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI,
172 unsigned OpNo) const {
173 MachineOperand MO = MI.getOperand(OpNo);
175 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
176 else if (MO.isSymbol())
177 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
179 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
181 llvm_unreachable("Unexpected jump target operand kind.");
185 unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
186 unsigned OpNo) const {
187 MachineOperand MO = MI.getOperand(OpNo);
188 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
192 unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI,
193 unsigned OpNo) const {
194 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
195 assert(MI.getOperand(OpNo).isReg());
196 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16;
197 return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
200 unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI,
201 unsigned OpNo) const {
202 // size is encoded as size-1.
203 return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
206 unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI,
207 unsigned OpNo) const {
208 // size is encoded as pos+size-1.
209 return getMachineOpValue(MI, MI.getOperand(OpNo-1)) +
210 getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
213 /// getMachineOpValue - Return binary encoding of operand. If the machine
214 /// operand requires relocation, record the relocation and return zero.
215 unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI,
216 const MachineOperand &MO) const {
218 return getMipsRegisterNumbering(MO.getReg());
220 return static_cast<unsigned>(MO.getImm());
221 else if (MO.isGlobal())
222 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
223 else if (MO.isSymbol())
224 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
226 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
228 emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
230 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
232 llvm_unreachable("Unable to encode MachineOperand!");
236 void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
237 bool MayNeedFarStub) const {
238 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
239 const_cast<GlobalValue *>(GV), 0,
243 void MipsCodeEmitter::emitGlobalAddressUnaligned(const GlobalValue *GV,
244 unsigned Reloc, int Offset) const {
245 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
246 const_cast<GlobalValue *>(GV), 0, false));
247 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset() + Offset,
248 Reloc, const_cast<GlobalValue *>(GV), 0, false));
251 void MipsCodeEmitter::
252 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
253 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
257 void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
258 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
259 Reloc, CPI, 0, false));
262 void MipsCodeEmitter::
263 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
264 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
265 Reloc, JTIndex, 0, false));
268 void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
269 unsigned Reloc) const {
270 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
274 int MipsCodeEmitter::emitUSW(const MachineInstr &MI) {
275 unsigned src = getMachineOpValue(MI, MI.getOperand(0));
276 unsigned base = getMachineOpValue(MI, MI.getOperand(1));
277 unsigned offset = getMachineOpValue(MI, MI.getOperand(2));
278 // swr src, offset(base)
279 // swl src, offset+3(base)
281 (0x2e << 26) | (base << 21) | (src << 16) | (offset & 0xffff));
283 (0x2a << 26) | (base << 21) | (src << 16) | ((offset+3) & 0xffff));
287 int MipsCodeEmitter::emitULW(const MachineInstr &MI) {
288 unsigned dst = getMachineOpValue(MI, MI.getOperand(0));
289 unsigned base = getMachineOpValue(MI, MI.getOperand(1));
290 unsigned offset = getMachineOpValue(MI, MI.getOperand(2));
293 // lwr dst, offset(base)
294 // lwl dst, offset+3(base)
296 (0x26 << 26) | (base << 21) | (dst << 16) | (offset & 0xffff));
298 (0x22 << 26) | (base << 21) | (dst << 16) | ((offset+3) & 0xffff));
301 // lwr at, offset(base)
302 // lwl at, offset+3(base)
303 // addu dst, at, $zero
305 (0x26 << 26) | (base << 21) | (at << 16) | (offset & 0xffff));
307 (0x22 << 26) | (base << 21) | (at << 16) | ((offset+3) & 0xffff));
309 (0x0 << 26) | (at << 21) | (0x0 << 16) | (dst << 11) | (0x0 << 6) | 0x21);
314 int MipsCodeEmitter::emitUSH(const MachineInstr &MI) {
315 unsigned src = getMachineOpValue(MI, MI.getOperand(0));
316 unsigned base = getMachineOpValue(MI, MI.getOperand(1));
317 unsigned offset = getMachineOpValue(MI, MI.getOperand(2));
319 // sb src, offset(base)
321 // sb at, offset+1(base)
323 (0x28 << 26) | (base << 21) | (src << 16) | (offset & 0xffff));
325 (0x0 << 26) | (0x0 << 21) | (src << 16) | (at << 11) | (0x8 << 6) | 0x2);
327 (0x28 << 26) | (base << 21) | (at << 16) | ((offset+1) & 0xffff));
331 int MipsCodeEmitter::emitULH(const MachineInstr &MI) {
332 unsigned dst = getMachineOpValue(MI, MI.getOperand(0));
333 unsigned base = getMachineOpValue(MI, MI.getOperand(1));
334 unsigned offset = getMachineOpValue(MI, MI.getOperand(2));
336 // lbu at, offset(base)
337 // lb dst, offset+1(base)
341 (0x24 << 26) | (base << 21) | (at << 16) | (offset & 0xffff));
343 (0x20 << 26) | (base << 21) | (dst << 16) | ((offset+1) & 0xffff));
345 (0x0 << 26) | (0x0 << 21) | (dst << 16) | (dst << 11) | (0x8 << 6) | 0x0);
347 (0x0 << 26) | (dst << 21) | (at << 16) | (dst << 11) | (0x0 << 6) | 0x25);
351 int MipsCodeEmitter::emitULHu(const MachineInstr &MI) {
352 unsigned dst = getMachineOpValue(MI, MI.getOperand(0));
353 unsigned base = getMachineOpValue(MI, MI.getOperand(1));
354 unsigned offset = getMachineOpValue(MI, MI.getOperand(2));
356 // lbu at, offset(base)
357 // lbu dst, offset+1(base)
361 (0x24 << 26) | (base << 21) | (at << 16) | (offset & 0xffff));
363 (0x24 << 26) | (base << 21) | (dst << 16) | ((offset+1) & 0xffff));
365 (0x0 << 26) | (0x0 << 21) | (dst << 16) | (dst << 11) | (0x8 << 6) | 0x0);
367 (0x0 << 26) | (dst << 21) | (at << 16) | (dst << 11) | (0x0 << 6) | 0x25);
371 void MipsCodeEmitter::emitInstruction(const MachineInstr &MI) {
372 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
374 MCE.processDebugLoc(MI.getDebugLoc(), true);
376 // Skip pseudo instructions.
377 if ((MI.getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo)
380 emitWordLE(getBinaryCodeForInstr(MI));
381 ++NumEmitted; // Keep track of the # of mi's emitted
383 MCE.processDebugLoc(MI.getDebugLoc(), false);
386 void MipsCodeEmitter::emitWordLE(unsigned Word) {
387 DEBUG(errs() << " 0x";
388 errs().write_hex(Word) << "\n");
389 MCE.emitWordLE(Word);
392 /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
393 /// code to the specified MCE object.
394 FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM,
395 JITCodeEmitter &JCE) {
396 return new MipsCodeEmitter(TM, JCE);
399 #include "MipsGenCodeEmitter.inc"