Initial implementation of exiting CompilationCallback
[oota-llvm.git] / lib / Target / PowerPC / PPCJITInfo.cpp
1 //===-- PPC32JITInfo.cpp - Implement the JIT interfaces for the PowerPC ---===//
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 implements the JIT interfaces for the 32-bit PowerPC target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "jit"
15 #include "PPC32JITInfo.h"
16 #include "PPC32Relocations.h"
17 #include "llvm/CodeGen/MachineCodeEmitter.h"
18 #include "llvm/Config/alloca.h"
19 using namespace llvm;
20
21 static TargetJITInfo::JITCompilerFn JITCompilerFunction;
22
23 #define BUILD_ADDIS(RD,RS,IMM16) \
24   ((15 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 65535))
25 #define BUILD_ORI(RD,RS,UIMM16) \
26   ((24 << 26) | ((RS) << 21) | ((RD) << 16) | ((UIMM16) & 65535))
27 #define BUILD_MTSPR(RS,SPR)      \
28   ((31 << 26) | ((RS) << 21) | ((SPR) << 16) | (467 << 1))
29 #define BUILD_BCCTRx(BO,BI,LINK) \
30   ((19 << 26) | ((BO) << 21) | ((BI) << 16) | (528 << 1) | ((LINK) & 1))
31
32 // Pseudo-ops
33 #define BUILD_LIS(RD,IMM16)    BUILD_ADDIS(RD,0,IMM16)
34 #define BUILD_MTCTR(RS)        BUILD_MTSPR(RS,9)
35 #define BUILD_BCTR(LINK)       BUILD_BCCTRx(20,0,LINK)
36
37
38 static void EmitBranchToAt(void *At, void *To, bool isCall) {
39   intptr_t Addr = (intptr_t)To;
40
41   // FIXME: should special case the short branch case.
42   unsigned *AtI = (unsigned*)At;
43
44   AtI[0] = BUILD_LIS(12, Addr >> 16);   // lis r12, hi16(address)
45   AtI[1] = BUILD_ORI(12, 12, Addr);     // ori r12, r12, low16(address)
46   AtI[2] = BUILD_MTCTR(12);             // mtctr r12
47   AtI[3] = BUILD_BCTR(isCall);          // bctr/bctrl
48 }
49
50 static void CompilationCallback() {
51   // FIXME: Should save R3-R10 and F1-F13 onto the stack, just like the Sparc
52   // version does.
53   //int IntRegs[8];
54   //uint64_t FPRegs[13];
55   unsigned *CameFromStub = (unsigned*)__builtin_return_address(0);
56   unsigned *CameFromOrig = (unsigned*)__builtin_return_address(1);
57   unsigned *CCStackPtr   = (unsigned*)__builtin_frame_address(0);
58 //unsigned *StubStackPtr = (unsigned*)__builtin_frame_address(1);
59   unsigned *OrigStackPtr = (unsigned*)__builtin_frame_address(2);
60
61   // Adjust pointer to the branch, not the return address.
62   --CameFromStub;
63
64   void *Target = JITCompilerFunction(CameFromStub);
65
66   // Check to see if CameFromOrig[-1] is a 'bl' instruction, and if we can
67   // rewrite it to branch directly to the destination.  If so, rewrite it so it
68   // does not need to go through the stub anymore.
69   unsigned CameFromOrigInst = CameFromOrig[-1];
70   if ((CameFromOrigInst >> 26) == 18) {     // Direct call.
71     intptr_t Offset = ((intptr_t)Target-(intptr_t)CameFromOrig) >> 2;
72     if (Offset >= -(1 << 23) && Offset < (1 << 23)) {   // In range?
73       // FIXME: hasn't been tested at all.
74       // Clear the original target out:
75       CameFromOrigInst &= (63 << 26) | 3;
76       CameFromOrigInst |= Offset << 2;
77       CameFromOrig[-1] = CameFromOrigInst;
78     }
79   }
80   
81   // Locate the start of the stub.  If this is a short call, adjust backwards
82   // the short amount, otherwise the full amount.
83   bool isShortStub = (*CameFromStub >> 26) == 18;
84   CameFromStub -= isShortStub ? 2 : 6;
85
86   // Rewrite the stub with an unconditional branch to the target, for any users
87   // who took the address of the stub.
88   EmitBranchToAt(CameFromStub, Target, false);
89
90   // Change the SP so that we pop two stack frames off when we return.
91   *CCStackPtr = (intptr_t)OrigStackPtr;
92
93   // Put the address of the stub and the LR value that originally came into the
94   // stub in a place that is easy to get on the stack after we restore all regs.
95   CCStackPtr[2] = (intptr_t)CameFromStub;
96   CCStackPtr[1] = (intptr_t)CameFromOrig;
97
98   // FIXME: Need to restore the registers from IntRegs/FPRegs.
99
100   // Note, this is not a standard epilog!
101 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
102   __asm__ __volatile__ ("lwz r0,4(r1)\n"   // Get CameFromOrig (LR into stub)
103                         "mtlr r0\n"        // Put it in the LR register
104                         "lwz r0,8(r1)\n"   // Get "CameFromStub"
105                         "mtctr r0\n"       // Put it into the CTR register
106                         "lwz r1,0(r1)\n"   // Pop two frames off
107                         "bctr\n" ::);      // Return to stub!
108 #endif
109 }
110
111
112
113 TargetJITInfo::LazyResolverFn 
114 PPC32JITInfo::getLazyResolverFunction(JITCompilerFn Fn) {
115   JITCompilerFunction = Fn;
116   return CompilationCallback;
117 }
118
119 void *PPC32JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
120   // If this is just a call to an external function, emit a branch instead of a
121   // call.  The code is the same except for one bit of the last instruction.
122   if (Fn != CompilationCallback) {
123     MCE.startFunctionStub(4*4);
124     void *Addr = (void*)(intptr_t)MCE.getCurrentPCValue();
125     MCE.emitWord(0);
126     MCE.emitWord(0);
127     MCE.emitWord(0);
128     MCE.emitWord(0);
129     EmitBranchToAt(Addr, Fn, false);
130     return MCE.finishFunctionStub(0);
131   }
132
133   MCE.startFunctionStub(4*7);
134   MCE.emitWord(0x9421ffe0);     // stwu    r1,-32(r1)
135   MCE.emitWord(0x7d6802a6);     // mflr r11
136   MCE.emitWord(0x91610028);     // stw r11, 40(r1)
137   void *Addr = (void*)(intptr_t)MCE.getCurrentPCValue();
138   MCE.emitWord(0);
139   MCE.emitWord(0);
140   MCE.emitWord(0);
141   MCE.emitWord(0);
142   EmitBranchToAt(Addr, Fn, true/*is call*/);
143   return MCE.finishFunctionStub(0);
144 }
145
146
147 void PPC32JITInfo::relocate(void *Function, MachineRelocation *MR,
148                             unsigned NumRelocs) {
149   for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
150     unsigned *RelocPos = (unsigned*)Function + MR->getMachineCodeOffset()/4;
151     intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
152     switch ((PPC::RelocationType)MR->getRelocationType()) {
153     default: assert(0 && "Unknown relocation type!");
154     case PPC::reloc_pcrel_bx:
155       // PC-relative relocation for b and bl instructions.
156       ResultPtr = (ResultPtr-(intptr_t)RelocPos) >> 2;
157       assert(ResultPtr >= -(1 << 23) && ResultPtr < (1 << 23) &&
158              "Relocation out of range!");
159       *RelocPos |= (ResultPtr & ((1 << 24)-1))  << 2;
160       break;
161     case PPC::reloc_absolute_loadhi:   // Relocate high bits into addis
162     case PPC::reloc_absolute_la:       // Relocate low bits into addi
163       ResultPtr += MR->getConstantVal();
164
165       if (MR->getRelocationType() == PPC::reloc_absolute_loadhi) {
166         // If the low part will have a carry (really a borrow) from the low
167         // 16-bits into the high 16, add a bit to borrow from.
168         if (((int)ResultPtr << 16) < 0)
169           ResultPtr += 1 << 16;
170         ResultPtr >>= 16;
171       }
172
173       // Do the addition then mask, so the addition does not overflow the 16-bit
174       // immediate section of the instruction.
175       unsigned LowBits  = (*RelocPos + ResultPtr) & 65535;
176       unsigned HighBits = *RelocPos & ~65535;
177       *RelocPos = LowBits | HighBits;  // Slam into low 16-bits
178       break;
179     }
180   }
181 }
182
183 void PPC32JITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
184   EmitBranchToAt(Old, New, false);
185 }