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 "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Function.h"
24 #include "llvm/PassManager.h"
25 #include "llvm/CodeGen/JITCodeEmitter.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/CodeGen/MachineJumpTableInfo.h"
30 #include "llvm/CodeGen/MachineModuleInfo.h"
31 #include "llvm/CodeGen/Passes.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/CodeGen/MachineOperand.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()),
70 TD(tm.getTargetData()), TM(tm), MCE(mce), MCPEs(0), MJTEs(0),
71 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {
74 bool runOnMachineFunction(MachineFunction &MF);
76 virtual const char *getPassName() const {
77 return "Mips Machine Code Emitter";
80 /// getBinaryCodeForInstr - This function, generated by the
81 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
82 /// machine instructions.
83 unsigned getBinaryCodeForInstr(const MachineInstr &MI) const;
85 void emitInstruction(const MachineInstr &MI);
89 void emitWordLE(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 getMemEncoding(const MachineInstr &MI, unsigned OpNo) const;
109 unsigned getSizeExtEncoding(const MachineInstr &MI, unsigned OpNo) const;
110 unsigned getSizeInsEncoding(const MachineInstr &MI, unsigned OpNo) const;
112 int emitULW(const MachineInstr &MI);
113 int emitUSW(const MachineInstr &MI);
114 int emitULH(const MachineInstr &MI);
115 int emitULHu(const MachineInstr &MI);
116 int emitUSH(const MachineInstr &MI);
118 void emitGlobalAddressUnaligned(const GlobalValue *GV, unsigned Reloc,
123 char MipsCodeEmitter::ID = 0;
125 bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
126 JTI = ((MipsTargetMachine&) MF.getTarget()).getJITInfo();
127 II = ((const MipsTargetMachine&) MF.getTarget()).getInstrInfo();
128 TD = ((const MipsTargetMachine&) MF.getTarget()).getTargetData();
129 Subtarget = &TM.getSubtarget<MipsSubtarget> ();
130 MCPEs = &MF.getConstantPool()->getConstants();
132 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
133 JTI->Initialize(MF, IsPIC);
134 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
137 DEBUG(errs() << "JITTing function '"
138 << MF.getFunction()->getName() << "'\n");
139 MCE.startFunction(MF);
141 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
143 MCE.StartMachineBasicBlock(MBB);
144 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
148 } while (MCE.finishFunction(MF));
153 unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI,
154 const MachineOperand &MO) const {
155 // NOTE: This relocations are for static.
156 uint64_t TSFlags = MI.getDesc().TSFlags;
157 uint64_t Form = TSFlags & MipsII::FormMask;
158 if (Form == MipsII::FrmJ)
159 return Mips::reloc_mips_26;
160 if ((Form == MipsII::FrmI || Form == MipsII::FrmFI)
161 && MI.getDesc().isBranch())
162 return Mips::reloc_mips_branch;
163 if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
164 return Mips::reloc_mips_hi;
165 return Mips::reloc_mips_lo;
168 unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI,
169 unsigned OpNo) const {
170 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
171 assert(MI.getOperand(OpNo).isReg());
172 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16;
174 (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
177 unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI,
178 unsigned OpNo) const {
179 // size is encoded as size-1.
180 return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
183 unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI,
184 unsigned OpNo) const {
185 // size is encoded as pos+size-1.
186 return getMachineOpValue(MI, MI.getOperand(OpNo-1)) +
187 getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
190 /// getMachineOpValue - Return binary encoding of operand. If the machine
191 /// operand requires relocation, record the relocation and return zero.
192 unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI,
193 const MachineOperand &MO) const {
195 return MipsRegisterInfo::getRegisterNumbering(MO.getReg());
197 return static_cast<unsigned>(MO.getImm());
198 else if (MO.isGlobal()) {
199 if (MI.getOpcode() == Mips::ULW || MI.getOpcode() == Mips::USW ||
200 MI.getOpcode() == Mips::ULH || MI.getOpcode() == Mips::ULHu)
201 emitGlobalAddressUnaligned(MO.getGlobal(), getRelocation(MI, MO), 4);
202 else if (MI.getOpcode() == Mips::USH)
203 emitGlobalAddressUnaligned(MO.getGlobal(), getRelocation(MI, MO), 8);
205 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
206 } else if (MO.isSymbol())
207 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
209 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
211 emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
213 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
215 llvm_unreachable("Unable to encode MachineOperand!");
219 void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
220 bool MayNeedFarStub) const {
221 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
222 const_cast<GlobalValue *>(GV), 0, MayNeedFarStub));
225 void MipsCodeEmitter::emitGlobalAddressUnaligned(const GlobalValue *GV,
226 unsigned Reloc, int Offset) const {
227 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
228 const_cast<GlobalValue *>(GV), 0, false));
229 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset() + Offset,
230 Reloc, const_cast<GlobalValue *>(GV), 0, false));
233 void MipsCodeEmitter::
234 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
235 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
236 Reloc, ES, 0, 0, false));
239 void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
240 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
241 Reloc, CPI, 0, false));
244 void MipsCodeEmitter::
245 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
246 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
247 Reloc, JTIndex, 0, false));
250 void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
251 unsigned Reloc) const {
252 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
256 int MipsCodeEmitter::emitUSW(const MachineInstr &MI) {
257 unsigned src = getMachineOpValue(MI, MI.getOperand(0));
258 unsigned base = getMachineOpValue(MI, MI.getOperand(1));
259 unsigned offset = getMachineOpValue(MI, MI.getOperand(2));
260 // swr src, offset(base)
261 // swl src, offset+3(base)
263 (0x2e << 26) | (base << 21) | (src << 16) | (offset & 0xffff));
265 (0x2a << 26) | (base << 21) | (src << 16) | ((offset+3) & 0xffff));
269 int MipsCodeEmitter::emitULW(const MachineInstr &MI) {
270 unsigned dst = getMachineOpValue(MI, MI.getOperand(0));
271 unsigned base = getMachineOpValue(MI, MI.getOperand(1));
272 unsigned offset = getMachineOpValue(MI, MI.getOperand(2));
275 // lwr dst, offset(base)
276 // lwl dst, offset+3(base)
278 (0x26 << 26) | (base << 21) | (dst << 16) | (offset & 0xffff));
280 (0x22 << 26) | (base << 21) | (dst << 16) | ((offset+3) & 0xffff));
283 // lwr at, offset(base)
284 // lwl at, offset+3(base)
285 // addu dst, at, $zero
287 (0x26 << 26) | (base << 21) | (at << 16) | (offset & 0xffff));
289 (0x22 << 26) | (base << 21) | (at << 16) | ((offset+3) & 0xffff));
291 (0x0 << 26) | (at << 21) | (0x0 << 16) | (dst << 11) | (0x0 << 6) | 0x21);
296 int MipsCodeEmitter::emitUSH(const MachineInstr &MI) {
297 unsigned src = getMachineOpValue(MI, MI.getOperand(0));
298 unsigned base = getMachineOpValue(MI, MI.getOperand(1));
299 unsigned offset = getMachineOpValue(MI, MI.getOperand(2));
301 // sb src, offset(base)
303 // sb at, offset+1(base)
305 (0x28 << 26) | (base << 21) | (src << 16) | (offset & 0xffff));
307 (0x0 << 26) | (0x0 << 21) | (src << 16) | (at << 11) | (0x8 << 6) | 0x2);
309 (0x28 << 26) | (base << 21) | (at << 16) | ((offset+1) & 0xffff));
313 int MipsCodeEmitter::emitULH(const MachineInstr &MI) {
314 unsigned dst = getMachineOpValue(MI, MI.getOperand(0));
315 unsigned base = getMachineOpValue(MI, MI.getOperand(1));
316 unsigned offset = getMachineOpValue(MI, MI.getOperand(2));
318 // lbu at, offset(base)
319 // lb dst, offset+1(base)
323 (0x24 << 26) | (base << 21) | (at << 16) | (offset & 0xffff));
325 (0x20 << 26) | (base << 21) | (dst << 16) | ((offset+1) & 0xffff));
327 (0x0 << 26) | (0x0 << 21) | (dst << 16) | (dst << 11) | (0x8 << 6) | 0x0);
329 (0x0 << 26) | (dst << 21) | (at << 16) | (dst << 11) | (0x0 << 6) | 0x25);
333 int MipsCodeEmitter::emitULHu(const MachineInstr &MI) {
334 unsigned dst = getMachineOpValue(MI, MI.getOperand(0));
335 unsigned base = getMachineOpValue(MI, MI.getOperand(1));
336 unsigned offset = getMachineOpValue(MI, MI.getOperand(2));
338 // lbu at, offset(base)
339 // lbu dst, offset+1(base)
343 (0x24 << 26) | (base << 21) | (at << 16) | (offset & 0xffff));
345 (0x24 << 26) | (base << 21) | (dst << 16) | ((offset+1) & 0xffff));
347 (0x0 << 26) | (0x0 << 21) | (dst << 16) | (dst << 11) | (0x8 << 6) | 0x0);
349 (0x0 << 26) | (dst << 21) | (at << 16) | (dst << 11) | (0x0 << 6) | 0x25);
353 void MipsCodeEmitter::emitInstruction(const MachineInstr &MI) {
354 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
356 MCE.processDebugLoc(MI.getDebugLoc(), true);
358 // Skip pseudo instructions.
359 if ((MI.getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo)
363 switch (MI.getOpcode()) {
365 NumEmitted += emitUSW(MI);
368 NumEmitted += emitULW(MI);
371 NumEmitted += emitULH(MI);
374 NumEmitted += emitULHu(MI);
377 NumEmitted += emitUSH(MI);
381 emitWordLE(getBinaryCodeForInstr(MI));
382 ++NumEmitted; // Keep track of the # of mi's emitted
386 MCE.processDebugLoc(MI.getDebugLoc(), false);
389 void MipsCodeEmitter::emitWordLE(unsigned Word) {
390 DEBUG(errs() << " 0x";
391 errs().write_hex(Word) << "\n");
392 MCE.emitWordLE(Word);
395 /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
396 /// code to the specified MCE object.
397 FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM,
398 JITCodeEmitter &JCE) {
399 return new MipsCodeEmitter(TM, JCE);
402 #include "MipsGenCodeEmitter.inc"