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