LLVM API Documentation
00001 //===-- PPCJITInfo.cpp - Implement the JIT interfaces for the PowerPC -----===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the JIT interfaces for the 32-bit PowerPC target. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #define DEBUG_TYPE "jit" 00015 #include "PPCJITInfo.h" 00016 #include "PPCRelocations.h" 00017 #include "PPCTargetMachine.h" 00018 #include "llvm/Function.h" 00019 #include "llvm/System/Memory.h" 00020 #include "llvm/Support/Debug.h" 00021 #include "llvm/Support/ErrorHandling.h" 00022 #include "llvm/Support/raw_ostream.h" 00023 using namespace llvm; 00024 00025 static TargetJITInfo::JITCompilerFn JITCompilerFunction; 00026 00027 #define BUILD_ADDIS(RD,RS,IMM16) \ 00028 ((15 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 65535)) 00029 #define BUILD_ORI(RD,RS,UIMM16) \ 00030 ((24 << 26) | ((RS) << 21) | ((RD) << 16) | ((UIMM16) & 65535)) 00031 #define BUILD_ORIS(RD,RS,UIMM16) \ 00032 ((25 << 26) | ((RS) << 21) | ((RD) << 16) | ((UIMM16) & 65535)) 00033 #define BUILD_RLDICR(RD,RS,SH,ME) \ 00034 ((30 << 26) | ((RS) << 21) | ((RD) << 16) | (((SH) & 31) << 11) | \ 00035 (((ME) & 63) << 6) | (1 << 2) | ((((SH) >> 5) & 1) << 1)) 00036 #define BUILD_MTSPR(RS,SPR) \ 00037 ((31 << 26) | ((RS) << 21) | ((SPR) << 16) | (467 << 1)) 00038 #define BUILD_BCCTRx(BO,BI,LINK) \ 00039 ((19 << 26) | ((BO) << 21) | ((BI) << 16) | (528 << 1) | ((LINK) & 1)) 00040 #define BUILD_B(TARGET, LINK) \ 00041 ((18 << 26) | (((TARGET) & 0x00FFFFFF) << 2) | ((LINK) & 1)) 00042 00043 // Pseudo-ops 00044 #define BUILD_LIS(RD,IMM16) BUILD_ADDIS(RD,0,IMM16) 00045 #define BUILD_SLDI(RD,RS,IMM6) BUILD_RLDICR(RD,RS,IMM6,63-IMM6) 00046 #define BUILD_MTCTR(RS) BUILD_MTSPR(RS,9) 00047 #define BUILD_BCTR(LINK) BUILD_BCCTRx(20,0,LINK) 00048 00049 static void EmitBranchToAt(uint64_t At, uint64_t To, bool isCall, bool is64Bit){ 00050 intptr_t Offset = ((intptr_t)To - (intptr_t)At) >> 2; 00051 unsigned *AtI = (unsigned*)(intptr_t)At; 00052 00053 if (Offset >= -(1 << 23) && Offset < (1 << 23)) { // In range? 00054 AtI[0] = BUILD_B(Offset, isCall); // b/bl target 00055 } else if (!is64Bit) { 00056 AtI[0] = BUILD_LIS(12, To >> 16); // lis r12, hi16(address) 00057 AtI[1] = BUILD_ORI(12, 12, To); // ori r12, r12, lo16(address) 00058 AtI[2] = BUILD_MTCTR(12); // mtctr r12 00059 AtI[3] = BUILD_BCTR(isCall); // bctr/bctrl 00060 } else { 00061 AtI[0] = BUILD_LIS(12, To >> 48); // lis r12, hi16(address) 00062 AtI[1] = BUILD_ORI(12, 12, To >> 32); // ori r12, r12, lo16(address) 00063 AtI[2] = BUILD_SLDI(12, 12, 32); // sldi r12, r12, 32 00064 AtI[3] = BUILD_ORIS(12, 12, To >> 16); // oris r12, r12, hi16(address) 00065 AtI[4] = BUILD_ORI(12, 12, To); // ori r12, r12, lo16(address) 00066 AtI[5] = BUILD_MTCTR(12); // mtctr r12 00067 AtI[6] = BUILD_BCTR(isCall); // bctr/bctrl 00068 } 00069 } 00070 00071 extern "C" void PPC32CompilationCallback(); 00072 extern "C" void PPC64CompilationCallback(); 00073 00074 #if (defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)) && \ 00075 !(defined(__ppc64__) || defined(__FreeBSD__)) 00076 // CompilationCallback stub - We can't use a C function with inline assembly in 00077 // it, because we the prolog/epilog inserted by GCC won't work for us. Instead, 00078 // write our own wrapper, which does things our way, so we have complete control 00079 // over register saving and restoring. 00080 asm( 00081 ".text\n" 00082 ".align 2\n" 00083 ".globl _PPC32CompilationCallback\n" 00084 "_PPC32CompilationCallback:\n" 00085 // Make space for 8 ints r[3-10] and 13 doubles f[1-13] and the 00086 // FIXME: need to save v[0-19] for altivec? 00087 // FIXME: could shrink frame 00088 // Set up a proper stack frame 00089 // FIXME Layout 00090 // PowerPC64 ABI linkage - 24 bytes 00091 // parameters - 32 bytes 00092 // 13 double registers - 104 bytes 00093 // 8 int registers - 32 bytes 00094 "mflr r0\n" 00095 "stw r0, 8(r1)\n" 00096 "stwu r1, -208(r1)\n" 00097 // Save all int arg registers 00098 "stw r10, 204(r1)\n" "stw r9, 200(r1)\n" 00099 "stw r8, 196(r1)\n" "stw r7, 192(r1)\n" 00100 "stw r6, 188(r1)\n" "stw r5, 184(r1)\n" 00101 "stw r4, 180(r1)\n" "stw r3, 176(r1)\n" 00102 // Save all call-clobbered FP regs. 00103 "stfd f13, 168(r1)\n" "stfd f12, 160(r1)\n" 00104 "stfd f11, 152(r1)\n" "stfd f10, 144(r1)\n" 00105 "stfd f9, 136(r1)\n" "stfd f8, 128(r1)\n" 00106 "stfd f7, 120(r1)\n" "stfd f6, 112(r1)\n" 00107 "stfd f5, 104(r1)\n" "stfd f4, 96(r1)\n" 00108 "stfd f3, 88(r1)\n" "stfd f2, 80(r1)\n" 00109 "stfd f1, 72(r1)\n" 00110 // Arguments to Compilation Callback: 00111 // r3 - our lr (address of the call instruction in stub plus 4) 00112 // r4 - stub's lr (address of instruction that called the stub plus 4) 00113 // r5 - is64Bit - always 0. 00114 "mr r3, r0\n" 00115 "lwz r2, 208(r1)\n" // stub's frame 00116 "lwz r4, 8(r2)\n" // stub's lr 00117 "li r5, 0\n" // 0 == 32 bit 00118 "bl _PPCCompilationCallbackC\n" 00119 "mtctr r3\n" 00120 // Restore all int arg registers 00121 "lwz r10, 204(r1)\n" "lwz r9, 200(r1)\n" 00122 "lwz r8, 196(r1)\n" "lwz r7, 192(r1)\n" 00123 "lwz r6, 188(r1)\n" "lwz r5, 184(r1)\n" 00124 "lwz r4, 180(r1)\n" "lwz r3, 176(r1)\n" 00125 // Restore all FP arg registers 00126 "lfd f13, 168(r1)\n" "lfd f12, 160(r1)\n" 00127 "lfd f11, 152(r1)\n" "lfd f10, 144(r1)\n" 00128 "lfd f9, 136(r1)\n" "lfd f8, 128(r1)\n" 00129 "lfd f7, 120(r1)\n" "lfd f6, 112(r1)\n" 00130 "lfd f5, 104(r1)\n" "lfd f4, 96(r1)\n" 00131 "lfd f3, 88(r1)\n" "lfd f2, 80(r1)\n" 00132 "lfd f1, 72(r1)\n" 00133 // Pop 3 frames off the stack and branch to target 00134 "lwz r1, 208(r1)\n" 00135 "lwz r2, 8(r1)\n" 00136 "mtlr r2\n" 00137 "bctr\n" 00138 ); 00139 00140 #elif defined(__PPC__) && !defined(__ppc64__) 00141 // Linux & FreeBSD / PPC 32 support 00142 00143 // CompilationCallback stub - We can't use a C function with inline assembly in 00144 // it, because we the prolog/epilog inserted by GCC won't work for us. Instead, 00145 // write our own wrapper, which does things our way, so we have complete control 00146 // over register saving and restoring. 00147 asm( 00148 ".text\n" 00149 ".align 2\n" 00150 ".globl PPC32CompilationCallback\n" 00151 "PPC32CompilationCallback:\n" 00152 // Make space for 8 ints r[3-10] and 8 doubles f[1-8] and the 00153 // FIXME: need to save v[0-19] for altivec? 00154 // FIXME: could shrink frame 00155 // Set up a proper stack frame 00156 // FIXME Layout 00157 // 8 double registers - 64 bytes 00158 // 8 int registers - 32 bytes 00159 "mflr 0\n" 00160 "stw 0, 4(1)\n" 00161 "stwu 1, -104(1)\n" 00162 // Save all int arg registers 00163 "stw 10, 100(1)\n" "stw 9, 96(1)\n" 00164 "stw 8, 92(1)\n" "stw 7, 88(1)\n" 00165 "stw 6, 84(1)\n" "stw 5, 80(1)\n" 00166 "stw 4, 76(1)\n" "stw 3, 72(1)\n" 00167 // Save all call-clobbered FP regs. 00168 "stfd 8, 64(1)\n" 00169 "stfd 7, 56(1)\n" "stfd 6, 48(1)\n" 00170 "stfd 5, 40(1)\n" "stfd 4, 32(1)\n" 00171 "stfd 3, 24(1)\n" "stfd 2, 16(1)\n" 00172 "stfd 1, 8(1)\n" 00173 // Arguments to Compilation Callback: 00174 // r3 - our lr (address of the call instruction in stub plus 4) 00175 // r4 - stub's lr (address of instruction that called the stub plus 4) 00176 // r5 - is64Bit - always 0. 00177 "mr 3, 0\n" 00178 "lwz 5, 104(1)\n" // stub's frame 00179 "lwz 4, 4(5)\n" // stub's lr 00180 "li 5, 0\n" // 0 == 32 bit 00181 "bl PPCCompilationCallbackC\n" 00182 "mtctr 3\n" 00183 // Restore all int arg registers 00184 "lwz 10, 100(1)\n" "lwz 9, 96(1)\n" 00185 "lwz 8, 92(1)\n" "lwz 7, 88(1)\n" 00186 "lwz 6, 84(1)\n" "lwz 5, 80(1)\n" 00187 "lwz 4, 76(1)\n" "lwz 3, 72(1)\n" 00188 // Restore all FP arg registers 00189 "lfd 8, 64(1)\n" 00190 "lfd 7, 56(1)\n" "lfd 6, 48(1)\n" 00191 "lfd 5, 40(1)\n" "lfd 4, 32(1)\n" 00192 "lfd 3, 24(1)\n" "lfd 2, 16(1)\n" 00193 "lfd 1, 8(1)\n" 00194 // Pop 3 frames off the stack and branch to target 00195 "lwz 1, 104(1)\n" 00196 "lwz 0, 4(1)\n" 00197 "mtlr 0\n" 00198 "bctr\n" 00199 ); 00200 #else 00201 void PPC32CompilationCallback() { 00202 llvm_unreachable("This is not a power pc, you can't execute this!"); 00203 } 00204 #endif 00205 00206 #if (defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)) && \ 00207 defined(__ppc64__) 00208 asm( 00209 ".text\n" 00210 ".align 2\n" 00211 ".globl _PPC64CompilationCallback\n" 00212 "_PPC64CompilationCallback:\n" 00213 // Make space for 8 ints r[3-10] and 13 doubles f[1-13] and the 00214 // FIXME: need to save v[0-19] for altivec? 00215 // Set up a proper stack frame 00216 // Layout 00217 // PowerPC64 ABI linkage - 48 bytes 00218 // parameters - 64 bytes 00219 // 13 double registers - 104 bytes 00220 // 8 int registers - 64 bytes 00221 "mflr r0\n" 00222 "std r0, 16(r1)\n" 00223 "stdu r1, -280(r1)\n" 00224 // Save all int arg registers 00225 "std r10, 272(r1)\n" "std r9, 264(r1)\n" 00226 "std r8, 256(r1)\n" "std r7, 248(r1)\n" 00227 "std r6, 240(r1)\n" "std r5, 232(r1)\n" 00228 "std r4, 224(r1)\n" "std r3, 216(r1)\n" 00229 // Save all call-clobbered FP regs. 00230 "stfd f13, 208(r1)\n" "stfd f12, 200(r1)\n" 00231 "stfd f11, 192(r1)\n" "stfd f10, 184(r1)\n" 00232 "stfd f9, 176(r1)\n" "stfd f8, 168(r1)\n" 00233 "stfd f7, 160(r1)\n" "stfd f6, 152(r1)\n" 00234 "stfd f5, 144(r1)\n" "stfd f4, 136(r1)\n" 00235 "stfd f3, 128(r1)\n" "stfd f2, 120(r1)\n" 00236 "stfd f1, 112(r1)\n" 00237 // Arguments to Compilation Callback: 00238 // r3 - our lr (address of the call instruction in stub plus 4) 00239 // r4 - stub's lr (address of instruction that called the stub plus 4) 00240 // r5 - is64Bit - always 1. 00241 "mr r3, r0\n" 00242 "ld r2, 280(r1)\n" // stub's frame 00243 "ld r4, 16(r2)\n" // stub's lr 00244 "li r5, 1\n" // 1 == 64 bit 00245 "bl _PPCCompilationCallbackC\n" 00246 "mtctr r3\n" 00247 // Restore all int arg registers 00248 "ld r10, 272(r1)\n" "ld r9, 264(r1)\n" 00249 "ld r8, 256(r1)\n" "ld r7, 248(r1)\n" 00250 "ld r6, 240(r1)\n" "ld r5, 232(r1)\n" 00251 "ld r4, 224(r1)\n" "ld r3, 216(r1)\n" 00252 // Restore all FP arg registers 00253 "lfd f13, 208(r1)\n" "lfd f12, 200(r1)\n" 00254 "lfd f11, 192(r1)\n" "lfd f10, 184(r1)\n" 00255 "lfd f9, 176(r1)\n" "lfd f8, 168(r1)\n" 00256 "lfd f7, 160(r1)\n" "lfd f6, 152(r1)\n" 00257 "lfd f5, 144(r1)\n" "lfd f4, 136(r1)\n" 00258 "lfd f3, 128(r1)\n" "lfd f2, 120(r1)\n" 00259 "lfd f1, 112(r1)\n" 00260 // Pop 3 frames off the stack and branch to target 00261 "ld r1, 280(r1)\n" 00262 "ld r2, 16(r1)\n" 00263 "mtlr r2\n" 00264 "bctr\n" 00265 ); 00266 #else 00267 void PPC64CompilationCallback() { 00268 llvm_unreachable("This is not a power pc, you can't execute this!"); 00269 } 00270 #endif 00271 00272 extern "C" void *PPCCompilationCallbackC(unsigned *StubCallAddrPlus4, 00273 unsigned *OrigCallAddrPlus4, 00274 bool is64Bit) { 00275 // Adjust the pointer to the address of the call instruction in the stub 00276 // emitted by emitFunctionStub, rather than the instruction after it. 00277 unsigned *StubCallAddr = StubCallAddrPlus4 - 1; 00278 unsigned *OrigCallAddr = OrigCallAddrPlus4 - 1; 00279 00280 void *Target = JITCompilerFunction(StubCallAddr); 00281 00282 // Check to see if *OrigCallAddr is a 'bl' instruction, and if we can rewrite 00283 // it to branch directly to the destination. If so, rewrite it so it does not 00284 // need to go through the stub anymore. 00285 unsigned OrigCallInst = *OrigCallAddr; 00286 if ((OrigCallInst >> 26) == 18) { // Direct call. 00287 intptr_t Offset = ((intptr_t)Target - (intptr_t)OrigCallAddr) >> 2; 00288 00289 if (Offset >= -(1 << 23) && Offset < (1 << 23)) { // In range? 00290 // Clear the original target out. 00291 OrigCallInst &= (63 << 26) | 3; 00292 // Fill in the new target. 00293 OrigCallInst |= (Offset & ((1 << 24)-1)) << 2; 00294 // Replace the call. 00295 *OrigCallAddr = OrigCallInst; 00296 } 00297 } 00298 00299 // Assert that we are coming from a stub that was created with our 00300 // emitFunctionStub. 00301 if ((*StubCallAddr >> 26) == 18) 00302 StubCallAddr -= 3; 00303 else { 00304 assert((*StubCallAddr >> 26) == 19 && "Call in stub is not indirect!"); 00305 StubCallAddr -= is64Bit ? 9 : 6; 00306 } 00307 00308 // Rewrite the stub with an unconditional branch to the target, for any users 00309 // who took the address of the stub. 00310 EmitBranchToAt((intptr_t)StubCallAddr, (intptr_t)Target, false, is64Bit); 00311 00312 // Put the address of the target function to call and the address to return to 00313 // after calling the target function in a place that is easy to get on the 00314 // stack after we restore all regs. 00315 return Target; 00316 } 00317 00318 00319 00320 TargetJITInfo::LazyResolverFn 00321 PPCJITInfo::getLazyResolverFunction(JITCompilerFn Fn) { 00322 JITCompilerFunction = Fn; 00323 return is64Bit ? PPC64CompilationCallback : PPC32CompilationCallback; 00324 } 00325 00326 #if (defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)) && \ 00327 defined(__APPLE__) 00328 extern "C" void sys_icache_invalidate(const void *Addr, size_t len); 00329 #endif 00330 00331 void *PPCJITInfo::emitFunctionStub(const Function* F, void *Fn, 00332 JITCodeEmitter &JCE) { 00333 // If this is just a call to an external function, emit a branch instead of a 00334 // call. The code is the same except for one bit of the last instruction. 00335 if (Fn != (void*)(intptr_t)PPC32CompilationCallback && 00336 Fn != (void*)(intptr_t)PPC64CompilationCallback) { 00337 JCE.startGVStub(F, 7*4); 00338 intptr_t Addr = (intptr_t)JCE.getCurrentPCValue(); 00339 JCE.emitWordBE(0); 00340 JCE.emitWordBE(0); 00341 JCE.emitWordBE(0); 00342 JCE.emitWordBE(0); 00343 JCE.emitWordBE(0); 00344 JCE.emitWordBE(0); 00345 JCE.emitWordBE(0); 00346 EmitBranchToAt(Addr, (intptr_t)Fn, false, is64Bit); 00347 sys::Memory::InvalidateInstructionCache((void*)Addr, 7*4); 00348 return JCE.finishGVStub(F); 00349 } 00350 00351 JCE.startGVStub(F, 10*4); 00352 intptr_t Addr = (intptr_t)JCE.getCurrentPCValue(); 00353 if (is64Bit) { 00354 JCE.emitWordBE(0xf821ffb1); // stdu r1,-80(r1) 00355 JCE.emitWordBE(0x7d6802a6); // mflr r11 00356 JCE.emitWordBE(0xf9610060); // std r11, 96(r1) 00357 } else if (TM.getSubtargetImpl()->isDarwinABI()){ 00358 JCE.emitWordBE(0x9421ffe0); // stwu r1,-32(r1) 00359 JCE.emitWordBE(0x7d6802a6); // mflr r11 00360 JCE.emitWordBE(0x91610028); // stw r11, 40(r1) 00361 } else { 00362 JCE.emitWordBE(0x9421ffe0); // stwu r1,-32(r1) 00363 JCE.emitWordBE(0x7d6802a6); // mflr r11 00364 JCE.emitWordBE(0x91610024); // stw r11, 36(r1) 00365 } 00366 intptr_t BranchAddr = (intptr_t)JCE.getCurrentPCValue(); 00367 JCE.emitWordBE(0); 00368 JCE.emitWordBE(0); 00369 JCE.emitWordBE(0); 00370 JCE.emitWordBE(0); 00371 JCE.emitWordBE(0); 00372 JCE.emitWordBE(0); 00373 JCE.emitWordBE(0); 00374 EmitBranchToAt(BranchAddr, (intptr_t)Fn, true, is64Bit); 00375 sys::Memory::InvalidateInstructionCache((void*)Addr, 10*4); 00376 return JCE.finishGVStub(F); 00377 } 00378 00379 00380 void PPCJITInfo::relocate(void *Function, MachineRelocation *MR, 00381 unsigned NumRelocs, unsigned char* GOTBase) { 00382 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) { 00383 unsigned *RelocPos = (unsigned*)Function + MR->getMachineCodeOffset()/4; 00384 intptr_t ResultPtr = (intptr_t)MR->getResultPointer(); 00385 switch ((PPC::RelocationType)MR->getRelocationType()) { 00386 default: llvm_unreachable("Unknown relocation type!"); 00387 case PPC::reloc_pcrel_bx: 00388 // PC-relative relocation for b and bl instructions. 00389 ResultPtr = (ResultPtr-(intptr_t)RelocPos) >> 2; 00390 assert(ResultPtr >= -(1 << 23) && ResultPtr < (1 << 23) && 00391 "Relocation out of range!"); 00392 *RelocPos |= (ResultPtr & ((1 << 24)-1)) << 2; 00393 break; 00394 case PPC::reloc_pcrel_bcx: 00395 // PC-relative relocation for BLT,BLE,BEQ,BGE,BGT,BNE, or other 00396 // bcx instructions. 00397 ResultPtr = (ResultPtr-(intptr_t)RelocPos) >> 2; 00398 assert(ResultPtr >= -(1 << 13) && ResultPtr < (1 << 13) && 00399 "Relocation out of range!"); 00400 *RelocPos |= (ResultPtr & ((1 << 14)-1)) << 2; 00401 break; 00402 case PPC::reloc_absolute_high: // high bits of ref -> low 16 of instr 00403 case PPC::reloc_absolute_low: { // low bits of ref -> low 16 of instr 00404 ResultPtr += MR->getConstantVal(); 00405 00406 // If this is a high-part access, get the high-part. 00407 if (MR->getRelocationType() == PPC::reloc_absolute_high) { 00408 // If the low part will have a carry (really a borrow) from the low 00409 // 16-bits into the high 16, add a bit to borrow from. 00410 if (((int)ResultPtr << 16) < 0) 00411 ResultPtr += 1 << 16; 00412 ResultPtr >>= 16; 00413 } 00414 00415 // Do the addition then mask, so the addition does not overflow the 16-bit 00416 // immediate section of the instruction. 00417 unsigned LowBits = (*RelocPos + ResultPtr) & 65535; 00418 unsigned HighBits = *RelocPos & ~65535; 00419 *RelocPos = LowBits | HighBits; // Slam into low 16-bits 00420 break; 00421 } 00422 case PPC::reloc_absolute_low_ix: { // low bits of ref -> low 14 of instr 00423 ResultPtr += MR->getConstantVal(); 00424 // Do the addition then mask, so the addition does not overflow the 16-bit 00425 // immediate section of the instruction. 00426 unsigned LowBits = (*RelocPos + ResultPtr) & 0xFFFC; 00427 unsigned HighBits = *RelocPos & 0xFFFF0003; 00428 *RelocPos = LowBits | HighBits; // Slam into low 14-bits. 00429 break; 00430 } 00431 } 00432 } 00433 } 00434 00435 void PPCJITInfo::replaceMachineCodeForFunction(void *Old, void *New) { 00436 EmitBranchToAt((intptr_t)Old, (intptr_t)New, false, is64Bit); 00437 }