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