MipsCodeEmitter.cpp revision 5a96b3dad2f634c9081c8b2b6c2575441dc5a2bd
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 "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/Function.h"
34#include "llvm/PassManager.h"
35#include "llvm/Support/Debug.h"
36#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/raw_ostream.h"
38#ifndef NDEBUG
39#include <iomanip>
40#endif
41
42using namespace llvm;
43
44STATISTIC(NumEmitted, "Number of machine instructions emitted");
45
46namespace {
47
48class 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 getJumpTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
109
110    unsigned getBranchTargetOpValue(const MachineInstr &MI, 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;
114
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);
120
121    void emitGlobalAddressUnaligned(const GlobalValue *GV, unsigned Reloc,
122                                                            int Offset) const;
123  };
124}
125
126char MipsCodeEmitter::ID = 0;
127
128bool 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();
134  MJTEs = 0;
135  if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
136  JTI->Initialize(MF, IsPIC);
137  MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
138
139  do {
140    DEBUG(errs() << "JITTing function '"
141        << MF.getFunction()->getName() << "'\n");
142    MCE.startFunction(MF);
143
144    for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
145        MBB != E; ++MBB){
146      MCE.StartMachineBasicBlock(MBB);
147      for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
148          I != E; ++I)
149        emitInstruction(*I);
150    }
151  } while (MCE.finishFunction(MF));
152
153  return false;
154}
155
156unsigned 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)
164       && MI.isBranch())
165    return Mips::reloc_mips_branch;
166  if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
167    return Mips::reloc_mips_hi;
168  return Mips::reloc_mips_lo;
169}
170
171unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI,
172                                               unsigned OpNo) const {
173  // FIXME: implement
174  return 0;
175}
176
177unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
178                                                 unsigned OpNo) const {
179  // FIXME: implement
180  return 0;
181}
182
183unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI,
184                                         unsigned OpNo) const {
185  // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
186  assert(MI.getOperand(OpNo).isReg());
187  unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16;
188  return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
189}
190
191unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI,
192                                             unsigned OpNo) const {
193  // size is encoded as size-1.
194  return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
195}
196
197unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI,
198                                             unsigned OpNo) const {
199  // size is encoded as pos+size-1.
200  return getMachineOpValue(MI, MI.getOperand(OpNo-1)) +
201         getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
202}
203
204/// getMachineOpValue - Return binary encoding of operand. If the machine
205/// operand requires relocation, record the relocation and return zero.
206unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI,
207                                            const MachineOperand &MO) const {
208  if (MO.isReg())
209    return MipsRegisterInfo::getRegisterNumbering(MO.getReg());
210  else if (MO.isImm())
211    return static_cast<unsigned>(MO.getImm());
212  else if (MO.isGlobal()) {
213    if (MI.getOpcode() == Mips::ULW || MI.getOpcode() == Mips::USW ||
214          MI.getOpcode() == Mips::ULH || MI.getOpcode() == Mips::ULHu)
215      emitGlobalAddressUnaligned(MO.getGlobal(), getRelocation(MI, MO), 4);
216    else if (MI.getOpcode() == Mips::USH)
217      emitGlobalAddressUnaligned(MO.getGlobal(), getRelocation(MI, MO), 8);
218    else
219      emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
220  } else if (MO.isSymbol())
221    emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
222  else if (MO.isCPI())
223    emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
224  else if (MO.isJTI())
225    emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
226  else if (MO.isMBB())
227    emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
228  else
229    llvm_unreachable("Unable to encode MachineOperand!");
230  return 0;
231}
232
233void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
234                                        bool MayNeedFarStub) const {
235  MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
236                                             const_cast<GlobalValue *>(GV), 0,
237                                             MayNeedFarStub));
238}
239
240void MipsCodeEmitter::emitGlobalAddressUnaligned(const GlobalValue *GV,
241                                           unsigned Reloc, int Offset) const {
242  MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
243                             const_cast<GlobalValue *>(GV), 0, false));
244  MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset() + Offset,
245                      Reloc, const_cast<GlobalValue *>(GV), 0, false));
246}
247
248void MipsCodeEmitter::
249emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
250  MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
251                                                 Reloc, ES, 0, 0, false));
252}
253
254void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
255  MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
256                                                    Reloc, CPI, 0, false));
257}
258
259void MipsCodeEmitter::
260emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
261  MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
262                                                    Reloc, JTIndex, 0, false));
263}
264
265void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
266                                            unsigned Reloc) const {
267  MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
268                                             Reloc, BB));
269}
270
271int MipsCodeEmitter::emitUSW(const MachineInstr &MI) {
272  unsigned src = getMachineOpValue(MI, MI.getOperand(0));
273  unsigned base = getMachineOpValue(MI, MI.getOperand(1));
274  unsigned offset = getMachineOpValue(MI, MI.getOperand(2));
275  // swr src, offset(base)
276  // swl src, offset+3(base)
277  MCE.emitWordLE(
278    (0x2e << 26) | (base << 21) | (src << 16) | (offset & 0xffff));
279  MCE.emitWordLE(
280    (0x2a << 26) | (base << 21) | (src << 16) | ((offset+3) & 0xffff));
281  return 2;
282}
283
284int MipsCodeEmitter::emitULW(const MachineInstr &MI) {
285  unsigned dst = getMachineOpValue(MI, MI.getOperand(0));
286  unsigned base = getMachineOpValue(MI, MI.getOperand(1));
287  unsigned offset = getMachineOpValue(MI, MI.getOperand(2));
288  unsigned at = 1;
289  if (dst != base) {
290    // lwr dst, offset(base)
291    // lwl dst, offset+3(base)
292    MCE.emitWordLE(
293      (0x26 << 26) | (base << 21) | (dst << 16) | (offset & 0xffff));
294    MCE.emitWordLE(
295      (0x22 << 26) | (base << 21) | (dst << 16) | ((offset+3) & 0xffff));
296    return 2;
297  } else {
298    // lwr at, offset(base)
299    // lwl at, offset+3(base)
300    // addu dst, at, $zero
301    MCE.emitWordLE(
302      (0x26 << 26) | (base << 21) | (at << 16) | (offset & 0xffff));
303    MCE.emitWordLE(
304      (0x22 << 26) | (base << 21) | (at << 16) | ((offset+3) & 0xffff));
305    MCE.emitWordLE(
306      (0x0 << 26) | (at << 21) | (0x0 << 16) | (dst << 11) | (0x0 << 6) | 0x21);
307    return 3;
308  }
309}
310
311int MipsCodeEmitter::emitUSH(const MachineInstr &MI) {
312  unsigned src = getMachineOpValue(MI, MI.getOperand(0));
313  unsigned base = getMachineOpValue(MI, MI.getOperand(1));
314  unsigned offset = getMachineOpValue(MI, MI.getOperand(2));
315  unsigned at = 1;
316  // sb src, offset(base)
317  // srl at,src,8
318  // sb at, offset+1(base)
319  MCE.emitWordLE(
320    (0x28 << 26) | (base << 21) | (src << 16) | (offset & 0xffff));
321  MCE.emitWordLE(
322    (0x0 << 26) | (0x0 << 21) | (src << 16) | (at << 11) | (0x8 << 6) | 0x2);
323  MCE.emitWordLE(
324    (0x28 << 26) | (base << 21) | (at << 16) | ((offset+1) & 0xffff));
325  return 3;
326}
327
328int MipsCodeEmitter::emitULH(const MachineInstr &MI) {
329  unsigned dst = getMachineOpValue(MI, MI.getOperand(0));
330  unsigned base = getMachineOpValue(MI, MI.getOperand(1));
331  unsigned offset = getMachineOpValue(MI, MI.getOperand(2));
332  unsigned at = 1;
333  // lbu at, offset(base)
334  // lb dst, offset+1(base)
335  // sll dst,dst,8
336  // or dst,dst,at
337  MCE.emitWordLE(
338    (0x24 << 26) | (base << 21) | (at << 16) | (offset & 0xffff));
339  MCE.emitWordLE(
340    (0x20 << 26) | (base << 21) | (dst << 16) | ((offset+1) & 0xffff));
341  MCE.emitWordLE(
342    (0x0 << 26) | (0x0 << 21) | (dst << 16) | (dst << 11) | (0x8 << 6) | 0x0);
343  MCE.emitWordLE(
344    (0x0 << 26) | (dst << 21) | (at << 16) | (dst << 11) | (0x0 << 6) | 0x25);
345  return 4;
346}
347
348int MipsCodeEmitter::emitULHu(const MachineInstr &MI) {
349  unsigned dst = getMachineOpValue(MI, MI.getOperand(0));
350  unsigned base = getMachineOpValue(MI, MI.getOperand(1));
351  unsigned offset = getMachineOpValue(MI, MI.getOperand(2));
352  unsigned at = 1;
353  // lbu at, offset(base)
354  // lbu dst, offset+1(base)
355  // sll dst,dst,8
356  // or dst,dst,at
357  MCE.emitWordLE(
358    (0x24 << 26) | (base << 21) | (at << 16) | (offset & 0xffff));
359  MCE.emitWordLE(
360    (0x24 << 26) | (base << 21) | (dst << 16) | ((offset+1) & 0xffff));
361  MCE.emitWordLE(
362    (0x0 << 26) | (0x0 << 21) | (dst << 16) | (dst << 11) | (0x8 << 6) | 0x0);
363  MCE.emitWordLE(
364    (0x0 << 26) | (dst << 21) | (at << 16) | (dst << 11) | (0x0 << 6) | 0x25);
365  return 4;
366}
367
368void MipsCodeEmitter::emitInstruction(const MachineInstr &MI) {
369  DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI);
370
371  MCE.processDebugLoc(MI.getDebugLoc(), true);
372
373  // Skip pseudo instructions.
374  if ((MI.getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo)
375    return;
376
377
378  switch (MI.getOpcode()) {
379  case Mips::USW:
380    NumEmitted += emitUSW(MI);
381    break;
382  case Mips::ULW:
383    NumEmitted += emitULW(MI);
384    break;
385  case Mips::ULH:
386    NumEmitted += emitULH(MI);
387    break;
388  case Mips::ULHu:
389    NumEmitted += emitULHu(MI);
390    break;
391  case Mips::USH:
392    NumEmitted += emitUSH(MI);
393    break;
394
395  default:
396    emitWordLE(getBinaryCodeForInstr(MI));
397    ++NumEmitted;  // Keep track of the # of mi's emitted
398    break;
399  }
400
401  MCE.processDebugLoc(MI.getDebugLoc(), false);
402}
403
404void MipsCodeEmitter::emitWordLE(unsigned Word) {
405  DEBUG(errs() << "  0x";
406        errs().write_hex(Word) << "\n");
407  MCE.emitWordLE(Word);
408}
409
410/// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
411/// code to the specified MCE object.
412FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM,
413                                                 JITCodeEmitter &JCE) {
414  return new MipsCodeEmitter(TM, JCE);
415}
416
417#include "MipsGenCodeEmitter.inc"
418