blob: 12685ed17e3c5d5d8a476b4cf965d7b291ca53df [file] [log] [blame]
Andrew Lenharth0934ae02005-07-22 20:52:16 +00001//===-- AlphaJITInfo.cpp - Implement the JIT interfaces for the Alpha ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Andrew Lenharth0934ae02005-07-22 20:52:16 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the JIT interfaces for the Alpha target.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "jit"
15#include "AlphaJITInfo.h"
16#include "AlphaRelocations.h"
Nicolas Geoffray51cc3c12008-04-16 20:46:05 +000017#include "llvm/Function.h"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000018#include "llvm/CodeGen/JITCodeEmitter.h"
Andrew Lenharth0934ae02005-07-22 20:52:16 +000019#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000020#include "llvm/Support/ErrorHandling.h"
Chris Lattner893e1c92009-08-23 06:49:22 +000021#include "llvm/Support/raw_ostream.h"
Andrew Lenharth0934ae02005-07-22 20:52:16 +000022#include <cstdlib>
Andrew Lenharth0934ae02005-07-22 20:52:16 +000023using namespace llvm;
24
Andrew Lenhartha4433e12005-07-28 12:45:20 +000025#define BUILD_OFormatI(Op, RA, LIT, FUN, RC) \
26 ((Op << 26) | (RA << 21) | (LIT << 13) | (1 << 12) | (FUN << 5) | (RC))
27#define BUILD_OFormat(Op, RA, RB, FUN, RC) \
28 ((Op << 26) | (RA << 21) | (RB << 16) | (FUN << 5) | (RC))
29
Andrew Lenharth0934ae02005-07-22 20:52:16 +000030#define BUILD_LDA(RD, RS, IMM16) \
31 ((0x08 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 65535))
32#define BUILD_LDAH(RD, RS, IMM16) \
33 ((0x09 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 65535))
34
Andrew Lenharth0934ae02005-07-22 20:52:16 +000035#define BUILD_LDQ(RD, RS, IMM16) \
36 ((0x29 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 0xFFFF))
37
38#define BUILD_JMP(RD, RS, IMM16) \
Andrew Lenhartha4433e12005-07-28 12:45:20 +000039 ((0x1A << 26) | ((RD) << 21) | ((RS) << 16) | (0x00 << 14) | ((IMM16) & 0x3FFF))
40#define BUILD_JSR(RD, RS, IMM16) \
41 ((0x1A << 26) | ((RD) << 21) | ((RS) << 16) | (0x01 << 14) | ((IMM16) & 0x3FFF))
Andrew Lenharth0934ae02005-07-22 20:52:16 +000042
Andrew Lenhartha4433e12005-07-28 12:45:20 +000043#define BUILD_SLLi(RD, RS, IMM8) \
44 (BUILD_OFormatI(0x12, RS, IMM8, 0x39, RD))
45
46#define BUILD_ORi(RD, RS, IMM8) \
47 (BUILD_OFormatI(0x11, RS, IMM8, 0x20, RD))
48
49#define BUILD_OR(RD, RS, RT) \
50 (BUILD_OFormat(0x11, RS, RT, 0x20, RD))
51
52
53
54static void EmitBranchToAt(void *At, void *To) {
55 unsigned long Fn = (unsigned long)To;
56
57 unsigned *AtI = (unsigned*)At;
58
59 AtI[0] = BUILD_OR(0, 27, 27);
60
Chris Lattner893e1c92009-08-23 06:49:22 +000061 DEBUG(errs() << "Stub targeting " << To << "\n");
Andrew Lenhartha4433e12005-07-28 12:45:20 +000062
63 for (int x = 1; x <= 8; ++x) {
64 AtI[2*x - 1] = BUILD_SLLi(27,27,8);
65 unsigned d = (Fn >> (64 - 8 * x)) & 0x00FF;
Chris Lattner893e1c92009-08-23 06:49:22 +000066 //DEBUG(errs() << "outputing " << hex << d << dec << "\n");
Andrew Lenhartha4433e12005-07-28 12:45:20 +000067 AtI[2*x] = BUILD_ORi(27, 27, d);
68 }
69 AtI[17] = BUILD_JMP(31,27,0); //jump, preserving ra, and setting pv
70 AtI[18] = 0x00FFFFFF; //mark this as a stub
Andrew Lenharth0934ae02005-07-22 20:52:16 +000071}
72
73void AlphaJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
74 //FIXME
Torok Edwinc23197a2009-07-14 16:55:14 +000075 llvm_unreachable(0);
Andrew Lenharth0934ae02005-07-22 20:52:16 +000076}
77
78static TargetJITInfo::JITCompilerFn JITCompilerFunction;
79//static AlphaJITInfo* AlphaJTI;
80
81extern "C" {
Andrew Lenharth38396f82005-07-22 21:00:30 +000082#ifdef __alpha
Andrew Lenharth0934ae02005-07-22 20:52:16 +000083
Andrew Lenhartha4433e12005-07-28 12:45:20 +000084 void AlphaCompilationCallbackC(long* oldpv, void* CameFromStub)
Andrew Lenharth0934ae02005-07-22 20:52:16 +000085 {
Andrew Lenharth0934ae02005-07-22 20:52:16 +000086 void* Target = JITCompilerFunction(CameFromStub);
Jeff Cohen00b168892005-07-27 06:12:32 +000087
Andrew Lenharth0934ae02005-07-22 20:52:16 +000088 //rewrite the stub to an unconditional branch
Andrew Lenhartha4433e12005-07-28 12:45:20 +000089 if (((unsigned*)CameFromStub)[18] == 0x00FFFFFF) {
Chris Lattner893e1c92009-08-23 06:49:22 +000090 DEBUG(errs() << "Came from a stub, rewriting\n");
Andrew Lenhartha4433e12005-07-28 12:45:20 +000091 EmitBranchToAt(CameFromStub, Target);
92 } else {
Chris Lattner893e1c92009-08-23 06:49:22 +000093 DEBUG(errs() << "confused, didn't come from stub at " << CameFromStub
94 << " old jump vector " << oldpv
95 << " new jump vector " << Target << "\n");
Andrew Lenhartha4433e12005-07-28 12:45:20 +000096 }
Andrew Lenharth0934ae02005-07-22 20:52:16 +000097
98 //Change pv to new Target
Andrew Lenhartha4433e12005-07-28 12:45:20 +000099 *oldpv = (long)Target;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000100 }
101
102 void AlphaCompilationCallback(void);
103
104 asm(
105 ".text\n"
Dan Gohmanacbfc152010-04-30 22:38:11 +0000106 ".globl AlphaCompilationCallbackC\n"
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000107 ".align 4\n"
108 ".globl AlphaCompilationCallback\n"
109 ".ent AlphaCompilationCallback\n"
110"AlphaCompilationCallback:\n"
111 // //get JIT's GOT
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000112 "ldgp $29, 0($27)\n"
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000113 //Save args, callee saved, and perhaps others?
114 //args: $16-$21 $f16-$f21 (12)
115 //callee: $9-$14 $f2-$f9 (14)
116 //others: fp:$15 ra:$26 pv:$27 (3)
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000117 "lda $30, -232($30)\n"
118 "stq $16, 0($30)\n"
119 "stq $17, 8($30)\n"
120 "stq $18, 16($30)\n"
121 "stq $19, 24($30)\n"
122 "stq $20, 32($30)\n"
123 "stq $21, 40($30)\n"
124 "stt $f16, 48($30)\n"
125 "stt $f17, 56($30)\n"
126 "stt $f18, 64($30)\n"
127 "stt $f19, 72($30)\n"
128 "stt $f20, 80($30)\n"
129 "stt $f21, 88($30)\n"
130 "stq $9, 96($30)\n"
131 "stq $10, 104($30)\n"
132 "stq $11, 112($30)\n"
133 "stq $12, 120($30)\n"
134 "stq $13, 128($30)\n"
135 "stq $14, 136($30)\n"
136 "stt $f2, 144($30)\n"
137 "stt $f3, 152($30)\n"
138 "stt $f4, 160($30)\n"
139 "stt $f5, 168($30)\n"
140 "stt $f6, 176($30)\n"
141 "stt $f7, 184($30)\n"
142 "stt $f8, 192($30)\n"
143 "stt $f9, 200($30)\n"
144 "stq $15, 208($30)\n"
145 "stq $26, 216($30)\n"
146 "stq $27, 224($30)\n"
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000147
148 "addq $30, 224, $16\n" //pass the addr of saved pv as the first arg
149 "bis $0, $0, $17\n" //pass the roughly stub addr in second arg
150 "jsr $26, AlphaCompilationCallbackC\n" //call without saving ra
151
152 "ldq $16, 0($30)\n"
153 "ldq $17, 8($30)\n"
154 "ldq $18, 16($30)\n"
155 "ldq $19, 24($30)\n"
156 "ldq $20, 32($30)\n"
157 "ldq $21, 40($30)\n"
158 "ldt $f16, 48($30)\n"
159 "ldt $f17, 56($30)\n"
160 "ldt $f18, 64($30)\n"
161 "ldt $f19, 72($30)\n"
162 "ldt $f20, 80($30)\n"
163 "ldt $f21, 88($30)\n"
164 "ldq $9, 96($30)\n"
165 "ldq $10, 104($30)\n"
166 "ldq $11, 112($30)\n"
167 "ldq $12, 120($30)\n"
168 "ldq $13, 128($30)\n"
169 "ldq $14, 136($30)\n"
170 "ldt $f2, 144($30)\n"
171 "ldt $f3, 152($30)\n"
172 "ldt $f4, 160($30)\n"
173 "ldt $f5, 168($30)\n"
174 "ldt $f6, 176($30)\n"
175 "ldt $f7, 184($30)\n"
176 "ldt $f8, 192($30)\n"
177 "ldt $f9, 200($30)\n"
178 "ldq $15, 208($30)\n"
179 "ldq $26, 216($30)\n"
180 "ldq $27, 224($30)\n" //this was updated in the callback with the target
181
182 "lda $30, 232($30)\n" //restore sp
183 "jmp $31, ($27)\n" //jump to the new function
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000184 ".end AlphaCompilationCallback\n"
185 );
186#else
187 void AlphaCompilationCallback() {
Torok Edwinc23197a2009-07-14 16:55:14 +0000188 llvm_unreachable("Cannot call AlphaCompilationCallback() on a non-Alpha arch!");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000189 }
190#endif
191}
192
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000193TargetJITInfo::StubLayout AlphaJITInfo::getStubLayout() {
194 // The stub contains 19 4-byte instructions, aligned at 4 bytes:
195 // R0 = R27
196 // 8 x "R27 <<= 8; R27 |= 8-bits-of-Target" == 16 instructions
197 // JMP R27
198 // Magic number so the compilation callback can recognize the stub.
199 StubLayout Result = {19 * 4, 4};
200 return Result;
201}
202
Nicolas Geoffray51cc3c12008-04-16 20:46:05 +0000203void *AlphaJITInfo::emitFunctionStub(const Function* F, void *Fn,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000204 JITCodeEmitter &JCE) {
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000205 //assert(Fn == AlphaCompilationCallback && "Where are you going?\n");
206 //Do things in a stupid slow way!
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000207 void* Addr = (void*)(intptr_t)JCE.getCurrentPCValue();
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000208 for (int x = 0; x < 19; ++ x)
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000209 JCE.emitWordLE(0);
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000210 EmitBranchToAt(Addr, Fn);
Chris Lattner893e1c92009-08-23 06:49:22 +0000211 DEBUG(errs() << "Emitting Stub to " << Fn << " at [" << Addr << "]\n");
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000212 return Addr;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000213}
214
215TargetJITInfo::LazyResolverFn
216AlphaJITInfo::getLazyResolverFunction(JITCompilerFn F) {
217 JITCompilerFunction = F;
218 // setZerothGOTEntry((void*)AlphaCompilationCallback);
219 return AlphaCompilationCallback;
220}
221
222//These describe LDAx
223static const int IMM_LOW = -32768;
224static const int IMM_HIGH = 32767;
225static const int IMM_MULT = 65536;
226
227static long getUpper16(long l)
228{
229 long y = l / IMM_MULT;
230 if (l % IMM_MULT > IMM_HIGH)
231 ++y;
232 if (l % IMM_MULT < IMM_LOW)
233 --y;
234 assert((short)y == y && "displacement out of range");
235 return y;
236}
237
238static long getLower16(long l)
239{
240 long h = getUpper16(l);
241 long y = l - h * IMM_MULT;
242 assert(y == (short)y && "Displacement out of range");
243 return y;
244}
245
246void AlphaJITInfo::relocate(void *Function, MachineRelocation *MR,
247 unsigned NumRelocs, unsigned char* GOTBase) {
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000248 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
249 unsigned *RelocPos = (unsigned*)Function + MR->getMachineCodeOffset()/4;
250 long idx = 0;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000251 bool doCommon = true;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000252 switch ((Alpha::RelocationType)MR->getRelocationType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000253 default: llvm_unreachable("Unknown relocation type!");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000254 case Alpha::reloc_literal:
255 //This is a LDQl
256 idx = MR->getGOTIndex();
Chris Lattner893e1c92009-08-23 06:49:22 +0000257 DEBUG(errs() << "Literal relocation to slot " << idx);
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000258 idx = (idx - GOToffset) * 8;
Chris Lattner893e1c92009-08-23 06:49:22 +0000259 DEBUG(errs() << " offset " << idx << "\n");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000260 break;
261 case Alpha::reloc_gprellow:
262 idx = (unsigned char*)MR->getResultPointer() - &GOTBase[GOToffset * 8];
263 idx = getLower16(idx);
Chris Lattner893e1c92009-08-23 06:49:22 +0000264 DEBUG(errs() << "gprellow relocation offset " << idx << "\n");
265 DEBUG(errs() << " Pointer is " << (void*)MR->getResultPointer()
266 << " GOT is " << (void*)&GOTBase[GOToffset * 8] << "\n");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000267 break;
268 case Alpha::reloc_gprelhigh:
269 idx = (unsigned char*)MR->getResultPointer() - &GOTBase[GOToffset * 8];
270 idx = getUpper16(idx);
Chris Lattner893e1c92009-08-23 06:49:22 +0000271 DEBUG(errs() << "gprelhigh relocation offset " << idx << "\n");
272 DEBUG(errs() << " Pointer is " << (void*)MR->getResultPointer()
273 << " GOT is " << (void*)&GOTBase[GOToffset * 8] << "\n");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000274 break;
275 case Alpha::reloc_gpdist:
276 switch (*RelocPos >> 26) {
277 case 0x09: //LDAH
278 idx = &GOTBase[GOToffset * 8] - (unsigned char*)RelocPos;
279 idx = getUpper16(idx);
Chris Lattner893e1c92009-08-23 06:49:22 +0000280 DEBUG(errs() << "LDAH: " << idx << "\n");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000281 //add the relocation to the map
Chris Lattner0e9aa452006-01-01 22:20:31 +0000282 gpdistmap[std::make_pair(Function, MR->getConstantVal())] = RelocPos;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000283 break;
284 case 0x08: //LDA
Chris Lattner0e9aa452006-01-01 22:20:31 +0000285 assert(gpdistmap[std::make_pair(Function, MR->getConstantVal())] &&
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000286 "LDAg without seeing LDAHg");
Jeff Cohen00b168892005-07-27 06:12:32 +0000287 idx = &GOTBase[GOToffset * 8] -
Chris Lattner0e9aa452006-01-01 22:20:31 +0000288 (unsigned char*)gpdistmap[std::make_pair(Function, MR->getConstantVal())];
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000289 idx = getLower16(idx);
Chris Lattner893e1c92009-08-23 06:49:22 +0000290 DEBUG(errs() << "LDA: " << idx << "\n");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000291 break;
292 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000293 llvm_unreachable("Cannot handle gpdist yet");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000294 }
295 break;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000296 case Alpha::reloc_bsr: {
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000297 idx = (((unsigned char*)MR->getResultPointer() -
Andrew Lenharth98169be2005-07-28 18:14:47 +0000298 (unsigned char*)RelocPos) >> 2) + 1; //skip first 2 inst of fun
299 *RelocPos |= (idx & ((1 << 21)-1));
300 doCommon = false;
301 break;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000302 }
Andrew Lenharth98169be2005-07-28 18:14:47 +0000303 }
304 if (doCommon) {
305 short x = (short)idx;
306 assert(x == idx);
307 *(short*)RelocPos = x;
308 }
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000309 }
310}