Remove VISIBILITY_HIDDEN from class/struct found inside anonymous namespaces.
[oota-llvm.git] / lib / Target / PowerPC / PPCCodeEmitter.cpp
1 //===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC32 -------*- C++ -*-=//
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 defines the PowerPC 32-bit CodeEmitter and associated machinery to
11 // JIT-compile bitcode to native PowerPC.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "PPCTargetMachine.h"
16 #include "PPCRelocations.h"
17 #include "PPC.h"
18 #include "llvm/Module.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/CodeGen/MachineCodeEmitter.h"
21 #include "llvm/CodeGen/JITCodeEmitter.h"
22 #include "llvm/CodeGen/ObjectCodeEmitter.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/CodeGen/Passes.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Target/TargetOptions.h"
32 using namespace llvm;
33
34 namespace {
35   class PPCCodeEmitter {
36     TargetMachine &TM;
37     MachineCodeEmitter &MCE;
38   public:
39     PPCCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce):
40         TM(tm), MCE(mce) {}
41
42     /// getBinaryCodeForInstr - This function, generated by the
43     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
44     /// machine instructions.
45
46     unsigned getBinaryCodeForInstr(const MachineInstr &MI);
47
48     /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
49
50     unsigned getMachineOpValue(const MachineInstr &MI,
51                                const MachineOperand &MO);
52
53     /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
54     /// its address in the function into this pointer.
55
56     void *MovePCtoLROffset;
57   };
58
59   template <class CodeEmitter>
60   class Emitter : public MachineFunctionPass, public PPCCodeEmitter {
61     TargetMachine &TM;
62     CodeEmitter &MCE;
63
64     void getAnalysisUsage(AnalysisUsage &AU) const {
65       AU.addRequired<MachineModuleInfo>();
66       MachineFunctionPass::getAnalysisUsage(AU);
67     }
68
69   public:
70     static char ID;
71     Emitter(TargetMachine &tm, CodeEmitter &mce)
72       : MachineFunctionPass(&ID), PPCCodeEmitter(tm, mce), TM(tm), MCE(mce) {}
73
74     const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
75
76     /// runOnMachineFunction - emits the given MachineFunction to memory
77     ///
78     bool runOnMachineFunction(MachineFunction &MF);
79
80     /// emitBasicBlock - emits the given MachineBasicBlock to memory
81     ///
82     void emitBasicBlock(MachineBasicBlock &MBB);
83
84     /// getValueBit - return the particular bit of Val
85     ///
86     unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
87   };
88
89   template <class CodeEmitter>
90     char Emitter<CodeEmitter>::ID = 0;
91 }
92
93 /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
94 /// to the specified MCE object.
95
96 FunctionPass *llvm::createPPCCodeEmitterPass(PPCTargetMachine &TM,
97                                              MachineCodeEmitter &MCE) {
98   return new Emitter<MachineCodeEmitter>(TM, MCE);
99 }
100
101 FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
102                                                 JITCodeEmitter &JCE) {
103   return new Emitter<JITCodeEmitter>(TM, JCE);
104 }
105
106 FunctionPass *llvm::createPPCObjectCodeEmitterPass(PPCTargetMachine &TM,
107                                                    ObjectCodeEmitter &OCE) {
108   return new Emitter<ObjectCodeEmitter>(TM, OCE);
109 }
110
111 template <class CodeEmitter>
112 bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
113   assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
114           MF.getTarget().getRelocationModel() != Reloc::Static) &&
115          "JIT relocation model must be set to static or default!");
116
117   MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
118   do {
119     MovePCtoLROffset = 0;
120     MCE.startFunction(MF);
121     for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
122       emitBasicBlock(*BB);
123   } while (MCE.finishFunction(MF));
124
125   return false;
126 }
127
128 template <class CodeEmitter>
129 void Emitter<CodeEmitter>::emitBasicBlock(MachineBasicBlock &MBB) {
130   MCE.StartMachineBasicBlock(&MBB);
131
132   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
133     const MachineInstr &MI = *I;
134     MCE.processDebugLoc(MI.getDebugLoc(), true);
135     switch (MI.getOpcode()) {
136     default:
137       MCE.emitWordBE(getBinaryCodeForInstr(MI));
138       break;
139     case TargetInstrInfo::DBG_LABEL:
140     case TargetInstrInfo::EH_LABEL:
141       MCE.emitLabel(MI.getOperand(0).getImm());
142       break;
143     case TargetInstrInfo::IMPLICIT_DEF:
144     case TargetInstrInfo::KILL:
145       break; // pseudo opcode, no side effects
146     case PPC::MovePCtoLR:
147     case PPC::MovePCtoLR8:
148       assert(TM.getRelocationModel() == Reloc::PIC_);
149       MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
150       MCE.emitWordBE(0x48000005);   // bl 1
151       break;
152     }
153     MCE.processDebugLoc(MI.getDebugLoc(), false);
154   }
155 }
156
157 unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
158                                            const MachineOperand &MO) {
159
160   unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
161                    // or things that get fixed up later by the JIT.
162   if (MO.isReg()) {
163     rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
164
165     // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
166     // register, not the register number directly.
167     if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
168         (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
169       rv = 0x80 >> rv;
170     }
171   } else if (MO.isImm()) {
172     rv = MO.getImm();
173   } else if (MO.isGlobal() || MO.isSymbol() ||
174              MO.isCPI() || MO.isJTI()) {
175     unsigned Reloc = 0;
176     if (MI.getOpcode() == PPC::BL_Darwin || MI.getOpcode() == PPC::BL8_Darwin ||
177         MI.getOpcode() == PPC::BL_SVR4 || MI.getOpcode() == PPC::BL8_ELF ||
178         MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8)
179       Reloc = PPC::reloc_pcrel_bx;
180     else {
181       if (TM.getRelocationModel() == Reloc::PIC_) {
182         assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
183       }
184       switch (MI.getOpcode()) {
185       default: MI.dump(); llvm_unreachable("Unknown instruction for relocation!");
186       case PPC::LIS:
187       case PPC::LIS8:
188       case PPC::ADDIS:
189       case PPC::ADDIS8:
190         Reloc = PPC::reloc_absolute_high;       // Pointer to symbol
191         break;
192       case PPC::LI:
193       case PPC::LI8:
194       case PPC::LA:
195       // Loads.
196       case PPC::LBZ:
197       case PPC::LBZ8:
198       case PPC::LHA:
199       case PPC::LHA8:
200       case PPC::LHZ:
201       case PPC::LHZ8:
202       case PPC::LWZ:
203       case PPC::LWZ8:
204       case PPC::LFS:
205       case PPC::LFD:
206
207       // Stores.
208       case PPC::STB:
209       case PPC::STB8:
210       case PPC::STH:
211       case PPC::STH8:
212       case PPC::STW:
213       case PPC::STW8:
214       case PPC::STFS:
215       case PPC::STFD:
216         Reloc = PPC::reloc_absolute_low;
217         break;
218
219       case PPC::LWA:
220       case PPC::LD:
221       case PPC::STD:
222       case PPC::STD_32:
223         Reloc = PPC::reloc_absolute_low_ix;
224         break;
225       }
226     }
227
228     MachineRelocation R;
229     if (MO.isGlobal()) {
230       R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
231                                    MO.getGlobal(), 0,
232                                    isa<Function>(MO.getGlobal()));
233     } else if (MO.isSymbol()) {
234       R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
235                                        Reloc, MO.getSymbolName(), 0);
236     } else if (MO.isCPI()) {
237       R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
238                                           Reloc, MO.getIndex(), 0);
239     } else {
240       assert(MO.isJTI());
241       R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
242                                           Reloc, MO.getIndex(), 0);
243     }
244
245     // If in PIC mode, we need to encode the negated address of the
246     // 'movepctolr' into the unrelocated field.  After relocation, we'll have
247     // &gv-&movepctolr-4 in the imm field.  Once &movepctolr is added to the imm
248     // field, we get &gv.  This doesn't happen for branch relocations, which are
249     // always implicitly pc relative.
250     if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){
251       assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
252       R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4);
253     }
254     MCE.addRelocation(R);
255
256   } else if (MO.isMBB()) {
257     unsigned Reloc = 0;
258     unsigned Opcode = MI.getOpcode();
259     if (Opcode == PPC::B || Opcode == PPC::BL_Darwin ||
260         Opcode == PPC::BLA_Darwin|| Opcode == PPC::BL_SVR4 ||
261         Opcode == PPC::BLA_SVR4)
262       Reloc = PPC::reloc_pcrel_bx;
263     else // BCC instruction
264       Reloc = PPC::reloc_pcrel_bcx;
265
266     MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
267                                                Reloc, MO.getMBB()));
268   } else {
269 #ifndef NDEBUG
270     errs() << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
271 #endif
272     llvm_unreachable(0);
273   }
274
275   return rv;
276 }
277
278 #include "PPCGenCodeEmitter.inc"