1 //===-- AlphaJITInfo.cpp - Implement the JIT interfaces for the Alpha ---===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the JIT interfaces for the Alpha target.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "jit"
15 #include "AlphaJITInfo.h"
16 #include "AlphaRelocations.h"
17 #include "llvm/CodeGen/MachineCodeEmitter.h"
18 #include "llvm/Config/alloca.h"
19 #include "llvm/Support/Debug.h"
26 #define BUILD_LDA(RD, RS, IMM16) \
27 ((0x08 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 65535))
28 #define BUILD_LDAH(RD, RS, IMM16) \
29 ((0x09 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 65535))
31 #define MERGE_PARTS(HH, HL, LH, LL) \
32 (((HH * 65536 + HL) << 32) + (LH * 65536 + LL))
34 #define BUILD_LDQ(RD, RS, IMM16) \
35 ((0x29 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 0xFFFF))
37 #define BUILD_JMP(RD, RS, IMM16) \
38 ((0x1A << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 0xFFFF))
40 static void EmitBranchToAt(void *At, void *To, bool isCall) {
45 void AlphaJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
50 static TargetJITInfo::JITCompilerFn JITCompilerFunction;
51 //static AlphaJITInfo* AlphaJTI;
56 void AlphaCompilationCallbackC(long* oldsp)
58 void* CameFromStub = (void*)*(oldsp - 1);
59 void* CameFromOrig = (void*)*(oldsp - 2);
61 void* Target = JITCompilerFunction(CameFromStub);
63 //rewrite the stub to an unconditional branch
64 EmitBranchToAt(CameFromStub, Target, false);
66 //Change pv to new Target
67 *(oldsp - 1) = (long)Target;
70 register long* RSP asm ("$0") = oldsp;
71 __asm__ __volatile__ (
101 "bis $30, $0, $0\n" //restore sp
102 "jmp $31, ($27)\n" //jump to the new function
103 "and $0, $31, $31\n" //dummy use of r0
107 void AlphaCompilationCallback(void);
111 ".globl AlphaComilationCallbackC\n"
113 ".globl AlphaCompilationCallback\n"
114 ".ent AlphaCompilationCallback\n"
115 "AlphaCompilationCallback:\n"
118 //Save args, callee saved, and perhaps others?
119 //args: $16-$21 $f16-$f21 (12)
120 //callee: $9-$14 $f2-$f9 (14)
121 //others: fp:$15 ra:$26 pv:$27 (3)
122 "bis $0, $30, $30\n" //0 = sp
123 "lda $30, -232($30)\n"
130 "stt $f16, 48($30)\n"
131 "stt $f17, 56($30)\n"
132 "stt $f18, 64($30)\n"
133 "stt $f19, 72($30)\n"
134 "stt $f20, 80($30)\n"
135 "stt $f21, 88($30)\n"
137 "stq $10, 104($30)\n"
138 "stq $11, 112($30)\n"
139 "stq $12, 120($30)\n"
140 "stq $13, 128($30)\n"
141 "stq $14, 136($30)\n"
142 "stt $f2, 144($30)\n"
143 "stt $f3, 152($30)\n"
144 "stt $f4, 160($30)\n"
145 "stt $f5, 168($30)\n"
146 "stt $f6, 176($30)\n"
147 "stt $f7, 184($30)\n"
148 "stt $f8, 192($30)\n"
149 "stt $f9, 200($30)\n"
150 "stq $15, 208($30)\n"
151 "stq $26, 216($30)\n"
152 "stq $27, 224($30)\n"
153 "bis $16, $0, $0\n" //pass the old sp as the first arg
154 "bsr $31, AlphaCompilationCallbackC\n"
155 ".end AlphaCompilationCallback\n"
158 void AlphaCompilationCallback() {
159 std::cerr << "Cannot call AlphaCompilationCallback() on a non-Alpha arch!\n";
165 void *AlphaJITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
166 // // If this is just a call to an external function, emit a branch instead of a
167 // // call. This means looking up Fn and storing that in R27 so as to appear to
168 // // have called there originally
169 // if (Fn != AlphaCompilationCallback) {
170 // int idx = AlphaJTI->getNewGOTEntry(Fn);
171 // //R27 = ldq idx(R29)
172 // //R31 = JMP R27, 0
173 // MCE.startFunctionStub(2*4);
174 // void *Addr = (void*)(intptr_t)MCE.getCurrentPCValue();
175 // MCE.emitWord(BUILD_LDQ(27, 29, idx << 3));
176 // MCE.emitWord(BUILD_JMP(31, 27, 0));
177 // return MCE.finishFunctionStub(0);
180 assert(0 && "Need to be able to jump to this guy too");
183 TargetJITInfo::LazyResolverFn
184 AlphaJITInfo::getLazyResolverFunction(JITCompilerFn F) {
185 JITCompilerFunction = F;
186 // setZerothGOTEntry((void*)AlphaCompilationCallback);
187 return AlphaCompilationCallback;
190 //These describe LDAx
191 static const int IMM_LOW = -32768;
192 static const int IMM_HIGH = 32767;
193 static const int IMM_MULT = 65536;
195 static long getUpper16(long l)
197 long y = l / IMM_MULT;
198 if (l % IMM_MULT > IMM_HIGH)
200 if (l % IMM_MULT < IMM_LOW)
202 assert((short)y == y && "displacement out of range");
206 static long getLower16(long l)
208 long h = getUpper16(l);
209 long y = l - h * IMM_MULT;
210 assert(y == (short)y && "Displacement out of range");
214 void AlphaJITInfo::relocate(void *Function, MachineRelocation *MR,
215 unsigned NumRelocs, unsigned char* GOTBase) {
216 //because gpdist are paired and relative to the pc of the first inst,
217 //we need to have some state
219 static map<pair<void*, int>, void*> gpdistmap;
221 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
222 unsigned *RelocPos = (unsigned*)Function + MR->getMachineCodeOffset()/4;
224 switch ((Alpha::RelocationType)MR->getRelocationType()) {
225 default: assert(0 && "Unknown relocation type!");
226 case Alpha::reloc_literal:
228 idx = MR->getGOTIndex();
229 DEBUG(std::cerr << "Literal relocation to slot " << idx);
230 idx = (idx - GOToffset) * 8;
231 DEBUG(std::cerr << " offset " << idx << "\n");
233 case Alpha::reloc_gprellow:
234 idx = (unsigned char*)MR->getResultPointer() - &GOTBase[GOToffset * 8];
235 idx = getLower16(idx);
236 DEBUG(std::cerr << "gprellow relocation offset " << idx << "\n");
237 DEBUG(std::cerr << " Pointer is " << (void*)MR->getResultPointer()
238 << " GOT is " << (void*)&GOTBase[GOToffset * 8] << "\n");
240 case Alpha::reloc_gprelhigh:
241 idx = (unsigned char*)MR->getResultPointer() - &GOTBase[GOToffset * 8];
242 idx = getUpper16(idx);
243 DEBUG(std::cerr << "gprelhigh relocation offset " << idx << "\n");
244 DEBUG(std::cerr << " Pointer is " << (void*)MR->getResultPointer()
245 << " GOT is " << (void*)&GOTBase[GOToffset * 8] << "\n");
247 case Alpha::reloc_gpdist:
248 switch (*RelocPos >> 26) {
250 idx = &GOTBase[GOToffset * 8] - (unsigned char*)RelocPos;
251 idx = getUpper16(idx);
252 DEBUG(std::cerr << "LDAH: " << idx << "\n");
253 //add the relocation to the map
254 gpdistmap[make_pair(Function, MR->getConstantVal())] = RelocPos;
257 assert(gpdistmap[make_pair(Function, MR->getConstantVal())] &&
258 "LDAg without seeing LDAHg");
259 idx = &GOTBase[GOToffset * 8] -
260 (unsigned char*)gpdistmap[make_pair(Function, MR->getConstantVal())];
261 idx = getLower16(idx);
262 DEBUG(std::cerr << "LDA: " << idx << "\n");
265 assert(0 && "Cannot handle gpdist yet");
269 short x = (short)idx;
271 *(short*)RelocPos = x;