Chris Lattner | 9b3d989 | 2004-11-23 06:02:06 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 9b3d989 | 2004-11-23 06:02:06 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 7c83dc2 | 2004-11-23 06:27:02 +0000 | [diff] [blame] | 38 | static void EmitBranchToAt(void *At, void *To, bool isCall) { |
Chris Lattner | 9b3d989 | 2004-11-23 06:02:06 +0000 | [diff] [blame] | 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 |
Chris Lattner | 7c83dc2 | 2004-11-23 06:27:02 +0000 | [diff] [blame] | 47 | AtI[3] = BUILD_BCTR(isCall); // bctr/bctrl |
Chris Lattner | 9b3d989 | 2004-11-23 06:02:06 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Chris Lattner | e61198b | 2004-11-23 06:55:05 +0000 | [diff] [blame^] | 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 | |
| 58 | // Adjust our pointers to the branches, not the return addresses. |
| 59 | --CameFromStub; --CameFromOrig; |
| 60 | |
| 61 | void *Target = JITCompilerFunction(CameFromStub); |
| 62 | |
| 63 | // Check to see if CameFromOrig[-1] is a 'bl' instruction, and if we can |
| 64 | // rewrite it to branch directly to the destination. If so, rewrite it so it |
| 65 | // does not need to go through the stub anymore. |
| 66 | unsigned CameFromOrigInst = *CameFromOrig; |
| 67 | if ((CameFromOrigInst >> 26) == 18) { // Direct call. |
| 68 | intptr_t Offset = ((intptr_t)Target-(intptr_t)CameFromOrig) >> 2; |
| 69 | if (Offset >= -(1 << 23) && Offset < (1 << 23)) { // In range? |
| 70 | // FIXME: hasn't been tested at all. |
| 71 | // Clear the original target out: |
| 72 | CameFromOrigInst &= (63 << 26) | 3; |
| 73 | CameFromOrigInst |= Offset << 2; |
| 74 | *CameFromOrig = CameFromOrigInst; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Locate the start of the stub. If this is a short call, adjust backwards |
| 79 | // the short amount, otherwise the full amount. |
| 80 | bool isShortStub = (*CameFromStub >> 26) == 18; |
| 81 | CameFromStub -= isShortStub ? 3 : 7; |
| 82 | |
| 83 | // Rewrite the stub with an unconditional branch to the target, for any users |
| 84 | // who took the address of the stub. |
| 85 | EmitBranchToAt(CameFromStub, Target, false); |
| 86 | |
| 87 | |
| 88 | // FIXME: Need to restore the registers from IntRegs/FPRegs. |
| 89 | |
| 90 | // FIXME: Need to pop two frames off of the stack and return to a place where |
| 91 | // we magically reexecute the call, or jump directly to the caller. This |
| 92 | // requires inline asm majik. |
| 93 | assert(0 && "CompilationCallback not finished yet!"); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | |
| 98 | TargetJITInfo::LazyResolverFn |
| 99 | PPC32JITInfo::getLazyResolverFunction(JITCompilerFn Fn) { |
| 100 | JITCompilerFunction = Fn; |
| 101 | return CompilationCallback; |
| 102 | } |
| 103 | |
Chris Lattner | 9b3d989 | 2004-11-23 06:02:06 +0000 | [diff] [blame] | 104 | void *PPC32JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) { |
| 105 | // If this is just a call to an external function, emit a branch instead of a |
| 106 | // call. The code is the same except for one bit of the last instruction. |
| 107 | if (Fn != CompilationCallback) { |
| 108 | MCE.startFunctionStub(4*4); |
| 109 | void *Addr = (void*)(intptr_t)MCE.getCurrentPCValue(); |
| 110 | MCE.emitWord(0); |
| 111 | MCE.emitWord(0); |
| 112 | MCE.emitWord(0); |
| 113 | MCE.emitWord(0); |
Chris Lattner | 7c83dc2 | 2004-11-23 06:27:02 +0000 | [diff] [blame] | 114 | EmitBranchToAt(Addr, Fn, false); |
Chris Lattner | 9b3d989 | 2004-11-23 06:02:06 +0000 | [diff] [blame] | 115 | return MCE.finishFunctionStub(0); |
| 116 | } |
| 117 | |
Chris Lattner | 7c83dc2 | 2004-11-23 06:27:02 +0000 | [diff] [blame] | 118 | MCE.startFunctionStub(4*7); |
| 119 | MCE.emitWord(0x9421ffe0); // stwu r1,-32(r1) |
| 120 | MCE.emitWord(0x7d6802a6); // mflr r11 |
| 121 | MCE.emitWord(0x91610028); // stw r11, 40(r1) |
| 122 | void *Addr = (void*)(intptr_t)MCE.getCurrentPCValue(); |
| 123 | MCE.emitWord(0); |
| 124 | MCE.emitWord(0); |
| 125 | MCE.emitWord(0); |
| 126 | MCE.emitWord(0); |
| 127 | EmitBranchToAt(Addr, Fn, true/*is call*/); |
Chris Lattner | 9b3d989 | 2004-11-23 06:02:06 +0000 | [diff] [blame] | 128 | return MCE.finishFunctionStub(0); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | void PPC32JITInfo::relocate(void *Function, MachineRelocation *MR, |
| 133 | unsigned NumRelocs) { |
| 134 | for (unsigned i = 0; i != NumRelocs; ++i, ++MR) { |
| 135 | unsigned *RelocPos = (unsigned*)Function + MR->getMachineCodeOffset()/4; |
| 136 | intptr_t ResultPtr = (intptr_t)MR->getResultPointer(); |
| 137 | switch ((PPC::RelocationType)MR->getRelocationType()) { |
| 138 | default: assert(0 && "Unknown relocation type!"); |
| 139 | case PPC::reloc_pcrel_bx: |
| 140 | // PC-relative relocation for b and bl instructions. |
| 141 | ResultPtr = (ResultPtr-(intptr_t)RelocPos) >> 2; |
| 142 | assert(ResultPtr >= -(1 << 23) && ResultPtr < (1 << 23) && |
| 143 | "Relocation out of range!"); |
| 144 | *RelocPos |= (ResultPtr & ((1 << 24)-1)) << 2; |
| 145 | break; |
| 146 | case PPC::reloc_absolute_loadhi: // Relocate high bits into addis |
| 147 | case PPC::reloc_absolute_la: // Relocate low bits into addi |
| 148 | ResultPtr += MR->getConstantVal(); |
| 149 | |
| 150 | if (MR->getRelocationType() == PPC::reloc_absolute_loadhi) { |
| 151 | // If the low part will have a carry (really a borrow) from the low |
| 152 | // 16-bits into the high 16, add a bit to borrow from. |
| 153 | if (((int)ResultPtr << 16) < 0) |
| 154 | ResultPtr += 1 << 16; |
| 155 | ResultPtr >>= 16; |
| 156 | } |
| 157 | |
| 158 | // Do the addition then mask, so the addition does not overflow the 16-bit |
| 159 | // immediate section of the instruction. |
| 160 | unsigned LowBits = (*RelocPos + ResultPtr) & 65535; |
| 161 | unsigned HighBits = *RelocPos & ~65535; |
| 162 | *RelocPos = LowBits | HighBits; // Slam into low 16-bits |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void PPC32JITInfo::replaceMachineCodeForFunction(void *Old, void *New) { |
Chris Lattner | 7c83dc2 | 2004-11-23 06:27:02 +0000 | [diff] [blame] | 169 | EmitBranchToAt(Old, New, false); |
Chris Lattner | 9b3d989 | 2004-11-23 06:02:06 +0000 | [diff] [blame] | 170 | } |