blob: ab3bfd970c6c71dc2b02dd1093daa76beabd0ebe [file] [log] [blame]
Jia Liuc5707112012-02-17 08:55:11 +00001//===-- Mips/MipsCodeEmitter.cpp - Convert Mips Code to Machine Code ------===//
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===---------------------------------------------------------------------===//
9//
10// This file contains the pass that transforms the Mips machine instructions
11// into relocatable machine code.
12//
13//===---------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "jit"
16#include "Mips.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "MCTargetDesc/MipsBaseInfo.h"
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000018#include "MipsInstrInfo.h"
19#include "MipsRelocations.h"
20#include "MipsSubtarget.h"
21#include "MipsTargetMachine.h"
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +000022#include "llvm/ADT/Statistic.h"
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000023#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"
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +000029#include "llvm/CodeGen/MachineOperand.h"
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000030#include "llvm/CodeGen/Passes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000031#include "llvm/IR/Constants.h"
32#include "llvm/IR/DerivedTypes.h"
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +000033#include "llvm/PassManager.h"
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/raw_ostream.h"
37#ifndef NDEBUG
38#include <iomanip>
39#endif
40
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000041using namespace llvm;
42
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +000043STATISTIC(NumEmitted, "Number of machine instructions emitted");
44
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000045namespace {
46
47class MipsCodeEmitter : public MachineFunctionPass {
48 MipsJITInfo *JTI;
49 const MipsInstrInfo *II;
Micah Villmow3574eca2012-10-08 16:38:25 +000050 const DataLayout *TD;
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000051 const MipsSubtarget *Subtarget;
52 TargetMachine &TM;
53 JITCodeEmitter &MCE;
54 const std::vector<MachineConstantPoolEntry> *MCPEs;
55 const std::vector<MachineJumpTableEntry> *MJTEs;
56 bool IsPIC;
57
58 void getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.addRequired<MachineModuleInfo> ();
60 MachineFunctionPass::getAnalysisUsage(AU);
61 }
62
63 static char ID;
64
Akira Hatanaka1b235a22013-02-11 22:03:52 +000065public:
66 MipsCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
67 : MachineFunctionPass(ID), JTI(0),
68 II((const MipsInstrInfo *) tm.getInstrInfo()), TD(tm.getDataLayout()),
69 TM(tm), MCE(mce), MCPEs(0), MJTEs(0),
70 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000071
Akira Hatanaka1b235a22013-02-11 22:03:52 +000072 bool runOnMachineFunction(MachineFunction &MF);
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000073
Akira Hatanaka1b235a22013-02-11 22:03:52 +000074 virtual const char *getPassName() const {
75 return "Mips Machine Code Emitter";
76 }
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000077
Akira Hatanaka1b235a22013-02-11 22:03:52 +000078 /// 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;
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +000082
Akira Hatanaka1b235a22013-02-11 22:03:52 +000083 void emitInstruction(const MachineInstr &MI);
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000084
Akira Hatanaka1b235a22013-02-11 22:03:52 +000085private:
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000086
Akira Hatanaka1b235a22013-02-11 22:03:52 +000087 void emitWord(unsigned Word);
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +000088
Akira Hatanaka1b235a22013-02-11 22:03:52 +000089 /// Routines that handle operands which add machine relocations which are
90 /// fixed up by the relocation stage.
91 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
92 bool MayNeedFarStub) const;
93 void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
94 void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
95 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const;
96 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc) const;
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +000097
Akira Hatanaka1b235a22013-02-11 22:03:52 +000098 /// getMachineOpValue - Return binary encoding of operand. If the machine
99 /// operand requires relocation, record the relocation and return zero.
100 unsigned getMachineOpValue(const MachineInstr &MI,
101 const MachineOperand &MO) const;
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000102
Akira Hatanaka1b235a22013-02-11 22:03:52 +0000103 unsigned getRelocation(const MachineInstr &MI,
104 const MachineOperand &MO) const;
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000105
Akira Hatanaka1b235a22013-02-11 22:03:52 +0000106 unsigned getJumpTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000107
Akira Hatanaka1b235a22013-02-11 22:03:52 +0000108 unsigned getBranchTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
109 unsigned getMemEncoding(const MachineInstr &MI, unsigned OpNo) const;
110 unsigned getSizeExtEncoding(const MachineInstr &MI, unsigned OpNo) const;
111 unsigned getSizeInsEncoding(const MachineInstr &MI, unsigned OpNo) const;
Bruno Cardoso Lopesad6eef42011-11-08 12:47:11 +0000112
Akira Hatanaka1b235a22013-02-11 22:03:52 +0000113 void emitGlobalAddressUnaligned(const GlobalValue *GV, unsigned Reloc,
114 int Offset) const;
115};
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000116}
117
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000118char MipsCodeEmitter::ID = 0;
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000119
120bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Dmitri Gribenko953cbfc2013-01-14 22:08:37 +0000121 MipsTargetMachine &Target = static_cast<MipsTargetMachine &>(
122 const_cast<TargetMachine &>(MF.getTarget()));
123
124 JTI = Target.getJITInfo();
125 II = Target.getInstrInfo();
126 TD = Target.getDataLayout();
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000127 Subtarget = &TM.getSubtarget<MipsSubtarget> ();
128 MCPEs = &MF.getConstantPool()->getConstants();
129 MJTEs = 0;
130 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
Akira Hatanakaff0b2cf2012-12-03 23:11:12 +0000131 JTI->Initialize(MF, IsPIC, Subtarget->isLittle());
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000132 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
133
134 do {
135 DEBUG(errs() << "JITTing function '"
Craig Topper96601ca2012-08-22 06:07:19 +0000136 << MF.getName() << "'\n");
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000137 MCE.startFunction(MF);
138
139 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
140 MBB != E; ++MBB){
141 MCE.StartMachineBasicBlock(MBB);
Akira Hatanaka226ae402012-06-19 03:39:45 +0000142 for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
143 E = MBB->instr_end(); I != E; ++I)
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000144 emitInstruction(*I);
145 }
146 } while (MCE.finishFunction(MF));
147
148 return false;
149}
150
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000151unsigned 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)
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000159 && MI.isBranch())
Bruno Cardoso Lopes3aa035f2011-12-30 21:04:30 +0000160 return Mips::reloc_mips_pc16;
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000161 if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
162 return Mips::reloc_mips_hi;
163 return Mips::reloc_mips_lo;
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000164}
165
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000166unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI,
167 unsigned OpNo) const {
Bruno Cardoso Lopes3aa035f2011-12-30 21:04:30 +0000168 MachineOperand MO = MI.getOperand(OpNo);
169 if (MO.isGlobal())
170 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
171 else if (MO.isSymbol())
172 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
173 else if (MO.isMBB())
174 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
175 else
176 llvm_unreachable("Unexpected jump target operand kind.");
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000177 return 0;
178}
179
180unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
181 unsigned OpNo) const {
Bruno Cardoso Lopes3aa035f2011-12-30 21:04:30 +0000182 MachineOperand MO = MI.getOperand(OpNo);
183 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000184 return 0;
185}
186
Bruno Cardoso Lopesc3f16b32011-10-18 17:50:36 +0000187unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000188 unsigned OpNo) const {
Bruno Cardoso Lopesc3f16b32011-10-18 17:50:36 +0000189 // 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;
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000192 return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
Bruno Cardoso Lopesc3f16b32011-10-18 17:50:36 +0000193}
194
195unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000196 unsigned OpNo) const {
Bruno Cardoso Lopesc3f16b32011-10-18 17:50:36 +0000197 // size is encoded as size-1.
198 return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
199}
200
201unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000202 unsigned OpNo) const {
Bruno Cardoso Lopesc3f16b32011-10-18 17:50:36 +0000203 // size is encoded as pos+size-1.
204 return getMachineOpValue(MI, MI.getOperand(OpNo-1)) +
205 getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
206}
207
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000208/// getMachineOpValue - Return binary encoding of operand. If the machine
209/// operand requires relocation, record the relocation and return zero.
210unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000211 const MachineOperand &MO) const {
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000212 if (MO.isReg())
Akira Hatanakae8068692012-12-10 20:04:40 +0000213 return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000214 else if (MO.isImm())
215 return static_cast<unsigned>(MO.getImm());
Akira Hatanaka5a7dd432012-09-15 01:52:08 +0000216 else if (MO.isGlobal())
217 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
218 else if (MO.isSymbol())
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000219 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
220 else if (MO.isCPI())
221 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
222 else if (MO.isJTI())
223 emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
224 else if (MO.isMBB())
225 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
226 else
227 llvm_unreachable("Unable to encode MachineOperand!");
228 return 0;
229}
230
231void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000232 bool MayNeedFarStub) const {
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000233 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000234 const_cast<GlobalValue *>(GV), 0,
235 MayNeedFarStub));
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000236}
237
Bruno Cardoso Lopesad6eef42011-11-08 12:47:11 +0000238void 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));
244}
245
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000246void MipsCodeEmitter::
247emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
248 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Akira Hatanakab5713452012-07-24 00:08:26 +0000249 Reloc, ES, 0, 0));
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000250}
251
252void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
253 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
254 Reloc, CPI, 0, false));
255}
256
257void MipsCodeEmitter::
258emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
259 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
260 Reloc, JTIndex, 0, false));
261}
262
263void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000264 unsigned Reloc) const {
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000265 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
266 Reloc, BB));
267}
268
269void MipsCodeEmitter::emitInstruction(const MachineInstr &MI) {
270 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
271
272 MCE.processDebugLoc(MI.getDebugLoc(), true);
273
274 // Skip pseudo instructions.
275 if ((MI.getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo)
276 return;
277
Akira Hatanakaff0b2cf2012-12-03 23:11:12 +0000278 emitWord(getBinaryCodeForInstr(MI));
Akira Hatanaka5a7dd432012-09-15 01:52:08 +0000279 ++NumEmitted; // Keep track of the # of mi's emitted
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000280
281 MCE.processDebugLoc(MI.getDebugLoc(), false);
282}
283
Akira Hatanakaff0b2cf2012-12-03 23:11:12 +0000284void MipsCodeEmitter::emitWord(unsigned Word) {
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000285 DEBUG(errs() << " 0x";
286 errs().write_hex(Word) << "\n");
Akira Hatanakaff0b2cf2012-12-03 23:11:12 +0000287 if (Subtarget->isLittle())
288 MCE.emitWordLE(Word);
289 else
290 MCE.emitWordBE(Word);
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000291}
292
293/// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
294/// code to the specified MCE object.
295FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000296 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000297 return new MipsCodeEmitter(TM, JCE);
298}
299
Bruno Cardoso Lopesc3f16b32011-10-18 17:50:36 +0000300#include "MipsGenCodeEmitter.inc"