This patch handles unaligned loads and stores in Mips JIT. Mips backend
[oota-llvm.git] / lib / Target / Mips / MipsCodeEmitter.cpp
1 //===-- Mips/MipsCodeEmitter.cpp - Convert Mips code to machine code -----===//
2 //
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"
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"
36 #ifndef NDEBUG
37 #include <iomanip>
38 #endif
39
40 #include "llvm/CodeGen/MachineOperand.h"
41
42 using namespace llvm;
43
44 STATISTIC(NumEmitted, "Number of machine instructions emitted");
45
46 namespace {
47
48 class MipsCodeEmitter : public MachineFunctionPass {
49   MipsJITInfo *JTI;
50   const MipsInstrInfo *II;
51   const TargetData *TD;
52   const MipsSubtarget *Subtarget;
53   TargetMachine &TM;
54   JITCodeEmitter &MCE;
55   const std::vector<MachineConstantPoolEntry> *MCPEs;
56   const std::vector<MachineJumpTableEntry> *MJTEs;
57   bool IsPIC;
58
59   void getAnalysisUsage(AnalysisUsage &AU) const {
60     AU.addRequired<MachineModuleInfo> ();
61     MachineFunctionPass::getAnalysisUsage(AU);
62   }
63
64   static char ID;
65
66   public:
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_) {
72     }
73
74     bool runOnMachineFunction(MachineFunction &MF);
75
76     virtual const char *getPassName() const {
77       return "Mips Machine Code Emitter";
78     }
79
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;
84
85     void emitInstruction(const MachineInstr &MI);
86
87   private:
88
89     void emitWordLE(unsigned Word);
90
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;
99
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;
104
105     unsigned getRelocation(const MachineInstr &MI,
106                            const MachineOperand &MO) const;
107
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;
111
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);
117
118     void emitGlobalAddressUnaligned(const GlobalValue *GV, unsigned Reloc,
119                                                             int Offset) const;
120   };
121 }
122
123 char MipsCodeEmitter::ID = 0;
124
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();
131   MJTEs = 0;
132   if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
133   JTI->Initialize(MF, IsPIC);
134   MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
135
136   do {
137     DEBUG(errs() << "JITTing function '"
138         << MF.getFunction()->getName() << "'\n");
139     MCE.startFunction(MF);
140
141     for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
142         MBB != E; ++MBB){
143       MCE.StartMachineBasicBlock(MBB);
144       for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
145           I != E; ++I)
146         emitInstruction(*I);
147     }
148   } while (MCE.finishFunction(MF));
149
150   return false;
151 }
152
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;
166 }
167
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;
173   return
174     (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
175 }
176
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;
181 }
182
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;
188 }
189
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 {
194   if (MO.isReg())
195     return MipsRegisterInfo::getRegisterNumbering(MO.getReg());
196   else if (MO.isImm())
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);
204     else
205       emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
206   } else if (MO.isSymbol())
207     emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
208   else if (MO.isCPI())
209     emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
210   else if (MO.isJTI())
211     emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
212   else if (MO.isMBB())
213     emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
214   else
215     llvm_unreachable("Unable to encode MachineOperand!");
216   return 0;
217 }
218
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));
223 }
224
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));
231 }
232
233 void MipsCodeEmitter::
234 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
235   MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
236                                                  Reloc, ES, 0, 0, false));
237 }
238
239 void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
240   MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
241                                                     Reloc, CPI, 0, false));
242 }
243
244 void MipsCodeEmitter::
245 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
246   MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
247                                                     Reloc, JTIndex, 0, false));
248 }
249
250 void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
251                                            unsigned Reloc) const {
252   MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
253                                              Reloc, BB));
254 }
255
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)
262   MCE.emitWordLE(
263     (0x2e << 26) | (base << 21) | (src << 16) | (offset & 0xffff));
264   MCE.emitWordLE(
265     (0x2a << 26) | (base << 21) | (src << 16) | ((offset+3) & 0xffff));
266   return 2;
267 }
268
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));
273   unsigned at = 1;
274   if (dst != base) {
275     // lwr dst, offset(base)
276     // lwl dst, offset+3(base)
277     MCE.emitWordLE(
278       (0x26 << 26) | (base << 21) | (dst << 16) | (offset & 0xffff));
279     MCE.emitWordLE(
280       (0x22 << 26) | (base << 21) | (dst << 16) | ((offset+3) & 0xffff));
281     return 2;
282   } else {
283     // lwr at, offset(base)
284     // lwl at, offset+3(base)
285     // addu dst, at, $zero
286     MCE.emitWordLE(
287       (0x26 << 26) | (base << 21) | (at << 16) | (offset & 0xffff));
288     MCE.emitWordLE(
289       (0x22 << 26) | (base << 21) | (at << 16) | ((offset+3) & 0xffff));
290     MCE.emitWordLE(
291       (0x0 << 26) | (at << 21) | (0x0 << 16) | (dst << 11) | (0x0 << 6) | 0x21);
292     return 3;
293   }
294 }
295
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));
300   unsigned at = 1;
301   // sb src, offset(base)
302   // srl at,src,8
303   // sb at, offset+1(base)
304   MCE.emitWordLE(
305     (0x28 << 26) | (base << 21) | (src << 16) | (offset & 0xffff));
306   MCE.emitWordLE(
307     (0x0 << 26) | (0x0 << 21) | (src << 16) | (at << 11) | (0x8 << 6) | 0x2);
308   MCE.emitWordLE(
309     (0x28 << 26) | (base << 21) | (at << 16) | ((offset+1) & 0xffff));
310   return 3;
311 }
312
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));
317   unsigned at = 1;
318   // lbu at, offset(base)
319   // lb dst, offset+1(base)
320   // sll dst,dst,8
321   // or dst,dst,at
322   MCE.emitWordLE(
323     (0x24 << 26) | (base << 21) | (at << 16) | (offset & 0xffff));
324   MCE.emitWordLE(
325     (0x20 << 26) | (base << 21) | (dst << 16) | ((offset+1) & 0xffff));
326   MCE.emitWordLE(
327     (0x0 << 26) | (0x0 << 21) | (dst << 16) | (dst << 11) | (0x8 << 6) | 0x0);
328   MCE.emitWordLE(
329     (0x0 << 26) | (dst << 21) | (at << 16) | (dst << 11) | (0x0 << 6) | 0x25);
330   return 4;
331 }
332
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));
337   unsigned at = 1;
338   // lbu at, offset(base)
339   // lbu dst, offset+1(base)
340   // sll dst,dst,8
341   // or dst,dst,at
342   MCE.emitWordLE(
343     (0x24 << 26) | (base << 21) | (at << 16) | (offset & 0xffff));
344   MCE.emitWordLE(
345     (0x24 << 26) | (base << 21) | (dst << 16) | ((offset+1) & 0xffff));
346   MCE.emitWordLE(
347     (0x0 << 26) | (0x0 << 21) | (dst << 16) | (dst << 11) | (0x8 << 6) | 0x0);
348   MCE.emitWordLE(
349     (0x0 << 26) | (dst << 21) | (at << 16) | (dst << 11) | (0x0 << 6) | 0x25);
350   return 4;
351 }
352
353 void MipsCodeEmitter::emitInstruction(const MachineInstr &MI) {
354   DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
355
356   MCE.processDebugLoc(MI.getDebugLoc(), true);
357
358   // Skip pseudo instructions.
359   if ((MI.getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo)
360     return;
361
362
363   switch (MI.getOpcode()) {
364   case Mips::USW:
365     NumEmitted += emitUSW(MI);
366     break;
367   case Mips::ULW:
368     NumEmitted += emitULW(MI);
369     break;
370   case Mips::ULH:
371     NumEmitted += emitULH(MI);
372     break;
373   case Mips::ULHu:
374     NumEmitted += emitULHu(MI);
375     break;
376   case Mips::USH:
377     NumEmitted += emitUSH(MI);
378     break;
379
380   default:
381     emitWordLE(getBinaryCodeForInstr(MI));
382     ++NumEmitted;  // Keep track of the # of mi's emitted
383     break;
384   }
385
386   MCE.processDebugLoc(MI.getDebugLoc(), false);
387 }
388
389 void MipsCodeEmitter::emitWordLE(unsigned Word) {
390   DEBUG(errs() << "  0x";
391         errs().write_hex(Word) << "\n");
392   MCE.emitWordLE(Word);
393 }
394
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);
400 }
401
402 #include "MipsGenCodeEmitter.inc"