Add the Object Code Emitter class. Original patch by Aaron Gray, I did some
[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/Target/TargetOptions.h"
30 using namespace llvm;
31
32 namespace {
33   class PPCCodeEmitter {
34     TargetMachine &TM;
35     MachineCodeEmitter &MCE;
36   public:
37     PPCCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce):
38         TM(tm), MCE(mce) {}
39
40     /// getBinaryCodeForInstr - This function, generated by the
41     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
42     /// machine instructions.
43
44     unsigned getBinaryCodeForInstr(const MachineInstr &MI);
45
46     /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
47
48     unsigned getMachineOpValue(const MachineInstr &MI,
49                                const MachineOperand &MO);
50
51     /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
52     /// its address in the function into this pointer.
53
54     void *MovePCtoLROffset;
55   };
56
57   template <class CodeEmitter>
58   class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass,
59       public PPCCodeEmitter
60   {
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     switch (MI.getOpcode()) {
135     default:
136       MCE.emitWordBE(getBinaryCodeForInstr(MI));
137       break;
138     case TargetInstrInfo::DBG_LABEL:
139     case TargetInstrInfo::EH_LABEL:
140       MCE.emitLabel(MI.getOperand(0).getImm());
141       break;
142     case TargetInstrInfo::IMPLICIT_DEF:
143       break; // pseudo opcode, no side effects
144     case PPC::MovePCtoLR:
145     case PPC::MovePCtoLR8:
146       assert(TM.getRelocationModel() == Reloc::PIC_);
147       MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
148       MCE.emitWordBE(0x48000005);   // bl 1
149       break;
150     }
151   }
152 }
153
154 unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
155                                            const MachineOperand &MO) {
156
157   unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
158                    // or things that get fixed up later by the JIT.
159   if (MO.isReg()) {
160     rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
161
162     // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
163     // register, not the register number directly.
164     if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
165         (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
166       rv = 0x80 >> rv;
167     }
168   } else if (MO.isImm()) {
169     rv = MO.getImm();
170   } else if (MO.isGlobal() || MO.isSymbol() ||
171              MO.isCPI() || MO.isJTI()) {
172     unsigned Reloc = 0;
173     if (MI.getOpcode() == PPC::BL_Darwin || MI.getOpcode() == PPC::BL8_Darwin ||
174         MI.getOpcode() == PPC::BL_SVR4 || MI.getOpcode() == PPC::BL8_ELF ||
175         MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8)
176       Reloc = PPC::reloc_pcrel_bx;
177     else {
178       if (TM.getRelocationModel() == Reloc::PIC_) {
179         assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
180       }
181       switch (MI.getOpcode()) {
182       default: MI.dump(); assert(0 && "Unknown instruction for relocation!");
183       case PPC::LIS:
184       case PPC::LIS8:
185       case PPC::ADDIS:
186       case PPC::ADDIS8:
187         Reloc = PPC::reloc_absolute_high;       // Pointer to symbol
188         break;
189       case PPC::LI:
190       case PPC::LI8:
191       case PPC::LA:
192       // Loads.
193       case PPC::LBZ:
194       case PPC::LBZ8:
195       case PPC::LHA:
196       case PPC::LHA8:
197       case PPC::LHZ:
198       case PPC::LHZ8:
199       case PPC::LWZ:
200       case PPC::LWZ8:
201       case PPC::LFS:
202       case PPC::LFD:
203       
204       // Stores.
205       case PPC::STB:
206       case PPC::STB8:
207       case PPC::STH:
208       case PPC::STH8:
209       case PPC::STW:
210       case PPC::STW8:
211       case PPC::STFS:
212       case PPC::STFD:
213         Reloc = PPC::reloc_absolute_low;
214         break;
215
216       case PPC::LWA:
217       case PPC::LD:
218       case PPC::STD:
219       case PPC::STD_32:
220         Reloc = PPC::reloc_absolute_low_ix;
221         break;
222       }
223     }
224     
225     MachineRelocation R;
226     if (MO.isGlobal()) {
227       R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
228                                    MO.getGlobal(), 0,
229                                    isa<Function>(MO.getGlobal()));
230     } else if (MO.isSymbol()) {
231       R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
232                                        Reloc, MO.getSymbolName(), 0);
233     } else if (MO.isCPI()) {
234       R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
235                                           Reloc, MO.getIndex(), 0);
236     } else {
237       assert(MO.isJTI());
238       R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
239                                           Reloc, MO.getIndex(), 0);
240     }
241     
242     // If in PIC mode, we need to encode the negated address of the
243     // 'movepctolr' into the unrelocated field.  After relocation, we'll have
244     // &gv-&movepctolr-4 in the imm field.  Once &movepctolr is added to the imm
245     // field, we get &gv.  This doesn't happen for branch relocations, which are
246     // always implicitly pc relative.
247     if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){
248       assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
249       R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4);
250     }
251     MCE.addRelocation(R);
252     
253   } else if (MO.isMBB()) {
254     unsigned Reloc = 0;
255     unsigned Opcode = MI.getOpcode();
256     if (Opcode == PPC::B || Opcode == PPC::BL_Darwin ||
257         Opcode == PPC::BLA_Darwin|| Opcode == PPC::BL_SVR4 ||
258         Opcode == PPC::BLA_SVR4)
259       Reloc = PPC::reloc_pcrel_bx;
260     else // BCC instruction
261       Reloc = PPC::reloc_pcrel_bcx;
262
263     MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
264                                                Reloc, MO.getMBB()));
265   } else {
266     cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
267     abort();
268   }
269
270   return rv;
271 }
272
273 #include "PPCGenCodeEmitter.inc"
274