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