1 //===-- SparcJITInfo.cpp - Implement the Sparc JIT Interface --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the JIT interfaces for the Sparc target.
12 //===----------------------------------------------------------------------===//
13 #define DEBUG_TYPE "jit"
14 #include "SparcJITInfo.h"
15 #include "SparcRelocations.h"
16 #include "llvm/CodeGen/JITCodeEmitter.h"
17 #include "llvm/Support/Memory.h"
21 /// JITCompilerFunction - This contains the address of the JIT function used to
22 /// compile a function lazily.
23 static TargetJITInfo::JITCompilerFn JITCompilerFunction;
25 extern "C" void SparcCompilationCallback();
28 #if defined (__sparc__)
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
42 "\t restore %o0, 0, %g1\n"
46 "\t.size SparcCompilationCallback, .-SparcCompilationCallback"
50 void SparcCompilationCallback() {
52 "Cannot call SparcCompilationCallback() on a non-sparc arch!");
57 #define HI(Val) (((unsigned)(Val)) >> 10)
58 #define LO(Val) (((unsigned)(Val)) & 0x3FF)
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)
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);
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)
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;
80 sys::Memory::InvalidateInstructionCache((void*) StubAddr, 12);
81 return (void*)StubAddr;
84 void SparcJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
85 assert(0 && "FIXME: Implement SparcJITInfo::replaceMachineCodeForFunction");
89 TargetJITInfo::StubLayout SparcJITInfo::getStubLayout() {
90 // The stub contains 3 4-byte instructions, aligned at 4 bytes. See
91 // emitFunctionStub for details.
93 StubLayout Result = { 3*4, 4 };
97 void *SparcJITInfo::emitFunctionStub(const Function *F, void *Fn,
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.");
105 intptr_t EmittedAddr;
106 if (Fn != (void*)(intptr_t)SparcCompilationCallback)
107 EmittedAddr = (intptr_t)Fn;
109 EmittedAddr = (intptr_t)SparcCompilationCallback;
111 // sethi %hi(EmittedAddr), %g1
112 // jmp %g1+%lo(EmittedAddr), %g1
115 JCE.emitWordBE(SETHI_INST(HI(EmittedAddr), 1));
116 JCE.emitWordBE(JMP_INST(1, LO(EmittedAddr), 1));
117 JCE.emitWordBE(NOP_INST);
119 sys::Memory::InvalidateInstructionCache(Addr, 12);
120 if (!sys::Memory::setRangeExecutable(Addr, 12))
121 llvm_unreachable("ERROR: Unable to mark stub executable.");
126 TargetJITInfo::LazyResolverFn
127 SparcJITInfo::getLazyResolverFunction(JITCompilerFn F) {
128 JITCompilerFunction = F;
129 return SparcCompilationCallback;
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();
141 switch ((SP::RelocationType) MR->getRelocationType()) {
142 case SP::reloc_sparc_hi:
143 ResultPtr = (ResultPtr >> 10) & 0x3fffff;
146 case SP::reloc_sparc_lo:
147 ResultPtr = (ResultPtr & 0x3ff);
150 case SP::reloc_sparc_pc30:
151 ResultPtr = ((ResultPtr - (intptr_t)RelocPos) >> 2) & 0x3fffffff;
154 case SP::reloc_sparc_pc22:
155 ResultPtr = ((ResultPtr - (intptr_t)RelocPos) >> 2) & 0x3fffff;
158 case SP::reloc_sparc_pc19:
159 ResultPtr = ((ResultPtr - (intptr_t)RelocPos) >> 2) & 0x7ffff;
162 *((unsigned*) RelocPos) |= (unsigned) ResultPtr;