[SparcV9] Add support for parsing branch instructions with prediction.
[oota-llvm.git] / lib / Target / Sparc / SparcCodeEmitter.cpp
1 //===-- Sparc/SparcCodeEmitter.cpp - Convert Sparc 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 Sparc machine instructions
11 // into relocatable machine code.
12 //
13 //===---------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "jit"
16 #include "Sparc.h"
17 #include "MCTargetDesc/SparcMCExpr.h"
18 #include "SparcRelocations.h"
19 #include "SparcTargetMachine.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/CodeGen/JITCodeEmitter.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/Support/Debug.h"
25
26 using namespace llvm;
27
28 STATISTIC(NumEmitted, "Number of machine instructions emitted");
29
30 namespace {
31
32 class SparcCodeEmitter : public MachineFunctionPass {
33   SparcJITInfo *JTI;
34   const SparcInstrInfo *II;
35   const DataLayout *TD;
36   const SparcSubtarget *Subtarget;
37   TargetMachine &TM;
38   JITCodeEmitter &MCE;
39   const std::vector<MachineConstantPoolEntry> *MCPEs;
40   bool IsPIC;
41
42   void getAnalysisUsage(AnalysisUsage &AU) const {
43     AU.addRequired<MachineModuleInfo> ();
44     MachineFunctionPass::getAnalysisUsage(AU);
45   }
46
47   static char ID;
48
49 public:
50   SparcCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
51     : MachineFunctionPass(ID), JTI(0), II(0), TD(0),
52       TM(tm), MCE(mce), MCPEs(0),
53       IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
54
55   bool runOnMachineFunction(MachineFunction &MF);
56
57   virtual const char *getPassName() const {
58     return "Sparc Machine Code Emitter";
59   }
60
61   /// getBinaryCodeForInstr - This function, generated by the
62   /// CodeEmitterGenerator using TableGen, produces the binary encoding for
63   /// machine instructions.
64   uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const;
65
66   void emitInstruction(MachineBasicBlock::instr_iterator MI,
67                        MachineBasicBlock &MBB);
68
69 private:
70   /// getMachineOpValue - Return binary encoding of operand. If the machine
71   /// operand requires relocation, record the relocation and return zero.
72   unsigned getMachineOpValue(const MachineInstr &MI,
73                              const MachineOperand &MO) const;
74
75   unsigned getCallTargetOpValue(const MachineInstr &MI,
76                                 unsigned) const;
77   unsigned getBranchTargetOpValue(const MachineInstr &MI,
78                                   unsigned) const;
79   unsigned getBranchPredTargetOpValue(const MachineInstr &MI,
80                                       unsigned) const;
81
82   void emitWord(unsigned Word);
83
84   unsigned getRelocation(const MachineInstr &MI,
85                          const MachineOperand &MO) const;
86
87   void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc) const;
88   void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
89   void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
90   void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc) const;
91 };
92 }  // end anonymous namespace.
93
94 char SparcCodeEmitter::ID = 0;
95
96 bool SparcCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
97   SparcTargetMachine &Target = static_cast<SparcTargetMachine &>(
98                                 const_cast<TargetMachine &>(MF.getTarget()));
99
100   JTI = Target.getJITInfo();
101   II = Target.getInstrInfo();
102   TD = Target.getDataLayout();
103   Subtarget = &TM.getSubtarget<SparcSubtarget> ();
104   MCPEs = &MF.getConstantPool()->getConstants();
105   JTI->Initialize(MF, IsPIC);
106   MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
107
108   do {
109     DEBUG(errs() << "JITTing function '"
110         << MF.getName() << "'\n");
111     MCE.startFunction(MF);
112
113     for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
114         MBB != E; ++MBB){
115       MCE.StartMachineBasicBlock(MBB);
116       for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
117            E = MBB->instr_end(); I != E;)
118         emitInstruction(*I++, *MBB);
119     }
120   } while (MCE.finishFunction(MF));
121
122   return false;
123 }
124
125 void SparcCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI,
126                                       MachineBasicBlock &MBB) {
127   DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI);
128
129   MCE.processDebugLoc(MI->getDebugLoc(), true);
130
131   ++NumEmitted;
132
133   switch (MI->getOpcode()) {
134   default: {
135     emitWord(getBinaryCodeForInstr(*MI));
136     break;
137   }
138   case TargetOpcode::INLINEASM: {
139     // We allow inline assembler nodes with empty bodies - they can
140     // implicitly define registers, which is ok for JIT.
141     if (MI->getOperand(0).getSymbolName()[0]) {
142       report_fatal_error("JIT does not support inline asm!");
143     }
144     break;
145   }
146   case TargetOpcode::PROLOG_LABEL:
147   case TargetOpcode::EH_LABEL: {
148     MCE.emitLabel(MI->getOperand(0).getMCSymbol());
149     break;
150   }
151   case TargetOpcode::IMPLICIT_DEF:
152   case TargetOpcode::KILL: {
153     // Do nothing.
154     break;
155   }
156   case SP::GETPCX: {
157     report_fatal_error("JIT does not support pseudo instruction GETPCX yet!");
158     break;
159   }
160   }
161
162   MCE.processDebugLoc(MI->getDebugLoc(), false);
163 }
164
165 void SparcCodeEmitter::emitWord(unsigned Word) {
166   DEBUG(errs() << "  0x";
167         errs().write_hex(Word) << "\n");
168   MCE.emitWordBE(Word);
169 }
170
171 /// getMachineOpValue - Return binary encoding of operand. If the machine
172 /// operand requires relocation, record the relocation and return zero.
173 unsigned SparcCodeEmitter::getMachineOpValue(const MachineInstr &MI,
174                                              const MachineOperand &MO) const {
175   if (MO.isReg())
176     return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
177   else if (MO.isImm())
178     return static_cast<unsigned>(MO.getImm());
179   else if (MO.isGlobal())
180     emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO));
181   else if (MO.isSymbol())
182     emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
183   else if (MO.isCPI())
184     emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
185   else if (MO.isMBB())
186     emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
187   else
188     llvm_unreachable("Unable to encode MachineOperand!");
189   return 0;
190 }
191 unsigned SparcCodeEmitter::getCallTargetOpValue(const MachineInstr &MI,
192                                                 unsigned opIdx) const {
193   const MachineOperand MO = MI.getOperand(opIdx);
194   return getMachineOpValue(MI, MO);
195 }
196
197 unsigned SparcCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
198                                                   unsigned opIdx) const {
199   const MachineOperand MO = MI.getOperand(opIdx);
200   return getMachineOpValue(MI, MO);
201 }
202
203 unsigned SparcCodeEmitter::getBranchPredTargetOpValue(const MachineInstr &MI,
204                                                       unsigned opIdx) const {
205   const MachineOperand MO = MI.getOperand(opIdx);
206   return getMachineOpValue(MI, MO);
207 }
208
209 unsigned SparcCodeEmitter::getRelocation(const MachineInstr &MI,
210                                          const MachineOperand &MO) const {
211
212   unsigned TF = MO.getTargetFlags();
213   switch (TF) {
214   default:
215   case SparcMCExpr::VK_Sparc_None:  break;
216   case SparcMCExpr::VK_Sparc_LO:    return SP::reloc_sparc_lo;
217   case SparcMCExpr::VK_Sparc_HI:    return SP::reloc_sparc_hi;
218   case SparcMCExpr::VK_Sparc_H44:   return SP::reloc_sparc_h44;
219   case SparcMCExpr::VK_Sparc_M44:   return SP::reloc_sparc_m44;
220   case SparcMCExpr::VK_Sparc_L44:   return SP::reloc_sparc_l44;
221   case SparcMCExpr::VK_Sparc_HH:    return SP::reloc_sparc_hh;
222   case SparcMCExpr::VK_Sparc_HM:    return SP::reloc_sparc_hm;
223   }
224
225   unsigned Opc = MI.getOpcode();
226   switch (Opc) {
227   default: break;
228   case SP::CALL:    return SP::reloc_sparc_pc30;
229   case SP::BA:
230   case SP::BCOND:
231   case SP::FBCOND:  return SP::reloc_sparc_pc22;
232   case SP::BPXCC:   return SP::reloc_sparc_pc19;
233   }
234   llvm_unreachable("unknown reloc!");
235 }
236
237 void SparcCodeEmitter::emitGlobalAddress(const GlobalValue *GV,
238                                         unsigned Reloc) const {
239   MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
240                                              const_cast<GlobalValue *>(GV), 0,
241                                              true));
242 }
243
244 void SparcCodeEmitter::
245 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
246   MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
247                                                  Reloc, ES, 0, 0));
248 }
249
250 void SparcCodeEmitter::
251 emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
252   MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
253                                                     Reloc, CPI, 0, false));
254 }
255
256 void SparcCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
257                                             unsigned Reloc) const {
258   MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
259                                              Reloc, BB));
260 }
261
262
263 /// createSparcJITCodeEmitterPass - Return a pass that emits the collected Sparc
264 /// code to the specified MCE object.
265 FunctionPass *llvm::createSparcJITCodeEmitterPass(SparcTargetMachine &TM,
266                                                  JITCodeEmitter &JCE) {
267   return new SparcCodeEmitter(TM, JCE);
268 }
269
270 #include "SparcGenCodeEmitter.inc"