Venkatraman Govindaraju | 2ea4c28 | 2013-10-08 07:15:22 +0000 | [diff] [blame] | 1 | //===-- SparcJITInfo.cpp - Implement the Sparc JIT Interface --------------===// |
| 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 implements the JIT interfaces for the Sparc target. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #define DEBUG_TYPE "jit" |
| 14 | #include "SparcJITInfo.h" |
| 15 | #include "SparcRelocations.h" |
Venkatraman Govindaraju | 2ea4c28 | 2013-10-08 07:15:22 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/JITCodeEmitter.h" |
| 17 | #include "llvm/Support/Memory.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | /// JITCompilerFunction - This contains the address of the JIT function used to |
| 22 | /// compile a function lazily. |
| 23 | static TargetJITInfo::JITCompilerFn JITCompilerFunction; |
| 24 | |
| 25 | extern "C" void SparcCompilationCallback(); |
| 26 | |
| 27 | extern "C" { |
| 28 | #if defined (__sparc__) |
| 29 | asm( |
| 30 | ".text\n" |
| 31 | "\t.align 4\n" |
| 32 | "\t.global SparcCompilationCallback\n" |
| 33 | "\t.type SparcCompilationCallback, #function\n" |
| 34 | "SparcCompilationCallback:\n" |
| 35 | // Save current register window. |
| 36 | "\tsave %sp, -192, %sp\n" |
| 37 | // stubaddr+4 is in %g1. |
| 38 | "\tcall SparcCompilationCallbackC\n" |
| 39 | "\t sub %g1, 4, %o0\n" |
| 40 | // restore original register window and |
| 41 | // copy %o0 to %g1 |
| 42 | "\t restore %o0, 0, %g1\n" |
| 43 | // call the new stub |
| 44 | "\tjmp %g1\n" |
| 45 | "\t nop\n" |
| 46 | "\t.size SparcCompilationCallback, .-SparcCompilationCallback" |
| 47 | ); |
| 48 | |
| 49 | #else |
| 50 | void SparcCompilationCallback() { |
| 51 | llvm_unreachable( |
| 52 | "Cannot call SparcCompilationCallback() on a non-sparc arch!"); |
| 53 | } |
| 54 | #endif |
| 55 | } |
| 56 | |
| 57 | #define HI(Val) (((unsigned)(Val)) >> 10) |
| 58 | #define LO(Val) (((unsigned)(Val)) & 0x3FF) |
| 59 | |
| 60 | #define SETHI_INST(imm, rd) (0x01000000 | ((rd) << 25) | ((imm) & 0x3FFFFF)) |
| 61 | #define JMP_INST(rs1, imm, rd) (0x80000000 | ((rd) << 25) | (0x38 << 19) \ |
| 62 | | ((rs1) << 14) | (1 << 13) | ((imm) & 0x1FFF)) |
| 63 | #define NOP_INST SETHI_INST(0, 0) |
| 64 | |
| 65 | extern "C" void *SparcCompilationCallbackC(intptr_t StubAddr) { |
| 66 | // Get the address of the compiled code for this function. |
| 67 | intptr_t NewVal = (intptr_t) JITCompilerFunction((void*) StubAddr); |
| 68 | |
| 69 | // Rewrite the function stub so that we don't end up here every time we |
| 70 | // execute the call. We're replacing the first three instructions of the |
| 71 | // stub with code that jumps to the compiled function: |
| 72 | // sethi %hi(NewVal), %g1 |
| 73 | // jmp %g1+%lo(NewVal) |
| 74 | // nop |
| 75 | |
| 76 | *(intptr_t *)(StubAddr) = SETHI_INST(HI(NewVal), 1); |
| 77 | *(intptr_t *)(StubAddr + 4) = JMP_INST(1, LO(NewVal), 0); |
| 78 | *(intptr_t *)(StubAddr + 8) = NOP_INST; |
| 79 | |
| 80 | sys::Memory::InvalidateInstructionCache((void*) StubAddr, 12); |
| 81 | return (void*)StubAddr; |
| 82 | } |
| 83 | |
| 84 | void SparcJITInfo::replaceMachineCodeForFunction(void *Old, void *New) { |
| 85 | assert(0 && "FIXME: Implement SparcJITInfo::replaceMachineCodeForFunction"); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | TargetJITInfo::StubLayout SparcJITInfo::getStubLayout() { |
| 90 | // The stub contains 3 4-byte instructions, aligned at 4 bytes. See |
| 91 | // emitFunctionStub for details. |
| 92 | |
| 93 | StubLayout Result = { 3*4, 4 }; |
| 94 | return Result; |
| 95 | } |
| 96 | |
| 97 | void *SparcJITInfo::emitFunctionStub(const Function *F, void *Fn, |
| 98 | JITCodeEmitter &JCE) |
| 99 | { |
| 100 | JCE.emitAlignment(4); |
| 101 | void *Addr = (void*) (JCE.getCurrentPCValue()); |
| 102 | if (!sys::Memory::setRangeWritable(Addr, 12)) |
| 103 | llvm_unreachable("ERROR: Unable to mark stub writable."); |
| 104 | |
| 105 | intptr_t EmittedAddr; |
| 106 | if (Fn != (void*)(intptr_t)SparcCompilationCallback) |
| 107 | EmittedAddr = (intptr_t)Fn; |
| 108 | else |
| 109 | EmittedAddr = (intptr_t)SparcCompilationCallback; |
| 110 | |
| 111 | // sethi %hi(EmittedAddr), %g1 |
| 112 | // jmp %g1+%lo(EmittedAddr), %g1 |
| 113 | // nop |
| 114 | |
| 115 | JCE.emitWordBE(SETHI_INST(HI(EmittedAddr), 1)); |
| 116 | JCE.emitWordBE(JMP_INST(1, LO(EmittedAddr), 1)); |
| 117 | JCE.emitWordBE(NOP_INST); |
| 118 | |
| 119 | sys::Memory::InvalidateInstructionCache(Addr, 12); |
| 120 | if (!sys::Memory::setRangeExecutable(Addr, 12)) |
| 121 | llvm_unreachable("ERROR: Unable to mark stub executable."); |
| 122 | |
| 123 | return Addr; |
| 124 | } |
| 125 | |
| 126 | TargetJITInfo::LazyResolverFn |
| 127 | SparcJITInfo::getLazyResolverFunction(JITCompilerFn F) { |
| 128 | JITCompilerFunction = F; |
| 129 | return SparcCompilationCallback; |
| 130 | } |
| 131 | |
| 132 | /// relocate - Before the JIT can run a block of code that has been emitted, |
| 133 | /// it must rewrite the code to contain the actual addresses of any |
| 134 | /// referenced global symbols. |
| 135 | void SparcJITInfo::relocate(void *Function, MachineRelocation *MR, |
| 136 | unsigned NumRelocs, unsigned char *GOTBase) { |
| 137 | for (unsigned i = 0; i != NumRelocs; ++i, ++MR) { |
| 138 | void *RelocPos = (char*) Function + MR->getMachineCodeOffset(); |
| 139 | intptr_t ResultPtr = (intptr_t) MR->getResultPointer(); |
| 140 | |
| 141 | switch ((SP::RelocationType) MR->getRelocationType()) { |
Venkatraman Govindaraju | 2ea4c28 | 2013-10-08 07:15:22 +0000 | [diff] [blame] | 142 | case SP::reloc_sparc_hi: |
| 143 | ResultPtr = (ResultPtr >> 10) & 0x3fffff; |
| 144 | break; |
| 145 | |
| 146 | case SP::reloc_sparc_lo: |
| 147 | ResultPtr = (ResultPtr & 0x3ff); |
| 148 | break; |
| 149 | |
| 150 | case SP::reloc_sparc_pc30: |
| 151 | ResultPtr = ((ResultPtr - (intptr_t)RelocPos) >> 2) & 0x3fffffff; |
| 152 | break; |
| 153 | |
| 154 | case SP::reloc_sparc_pc22: |
| 155 | ResultPtr = ((ResultPtr - (intptr_t)RelocPos) >> 2) & 0x3fffff; |
| 156 | break; |
| 157 | |
| 158 | case SP::reloc_sparc_pc19: |
| 159 | ResultPtr = ((ResultPtr - (intptr_t)RelocPos) >> 2) & 0x7ffff; |
| 160 | break; |
| 161 | } |
| 162 | *((unsigned*) RelocPos) |= (unsigned) ResultPtr; |
| 163 | } |
| 164 | } |