blob: 33174a75d7c25714b221eb8294defaed97b16cff [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/Config/alloca.h"
20#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattner893e1c92009-08-23 06:49:22 +000022#include "llvm/Support/raw_ostream.h"
Andrew Lenharth0934ae02005-07-22 20:52:16 +000023#include <cstdlib>
Andrew Lenharth0934ae02005-07-22 20:52:16 +000024using namespace llvm;
25
Andrew Lenhartha4433e12005-07-28 12:45:20 +000026#define BUILD_OFormatI(Op, RA, LIT, FUN, RC) \
27 ((Op << 26) | (RA << 21) | (LIT << 13) | (1 << 12) | (FUN << 5) | (RC))
28#define BUILD_OFormat(Op, RA, RB, FUN, RC) \
29 ((Op << 26) | (RA << 21) | (RB << 16) | (FUN << 5) | (RC))
30
Andrew Lenharth0934ae02005-07-22 20:52:16 +000031#define BUILD_LDA(RD, RS, IMM16) \
32 ((0x08 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 65535))
33#define BUILD_LDAH(RD, RS, IMM16) \
34 ((0x09 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 65535))
35
Andrew Lenharth0934ae02005-07-22 20:52:16 +000036#define BUILD_LDQ(RD, RS, IMM16) \
37 ((0x29 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 0xFFFF))
38
39#define BUILD_JMP(RD, RS, IMM16) \
Andrew Lenhartha4433e12005-07-28 12:45:20 +000040 ((0x1A << 26) | ((RD) << 21) | ((RS) << 16) | (0x00 << 14) | ((IMM16) & 0x3FFF))
41#define BUILD_JSR(RD, RS, IMM16) \
42 ((0x1A << 26) | ((RD) << 21) | ((RS) << 16) | (0x01 << 14) | ((IMM16) & 0x3FFF))
Andrew Lenharth0934ae02005-07-22 20:52:16 +000043
Andrew Lenhartha4433e12005-07-28 12:45:20 +000044#define BUILD_SLLi(RD, RS, IMM8) \
45 (BUILD_OFormatI(0x12, RS, IMM8, 0x39, RD))
46
47#define BUILD_ORi(RD, RS, IMM8) \
48 (BUILD_OFormatI(0x11, RS, IMM8, 0x20, RD))
49
50#define BUILD_OR(RD, RS, RT) \
51 (BUILD_OFormat(0x11, RS, RT, 0x20, RD))
52
53
54
55static void EmitBranchToAt(void *At, void *To) {
56 unsigned long Fn = (unsigned long)To;
57
58 unsigned *AtI = (unsigned*)At;
59
60 AtI[0] = BUILD_OR(0, 27, 27);
61
Chris Lattner893e1c92009-08-23 06:49:22 +000062 DEBUG(errs() << "Stub targeting " << To << "\n");
Andrew Lenhartha4433e12005-07-28 12:45:20 +000063
64 for (int x = 1; x <= 8; ++x) {
65 AtI[2*x - 1] = BUILD_SLLi(27,27,8);
66 unsigned d = (Fn >> (64 - 8 * x)) & 0x00FF;
Chris Lattner893e1c92009-08-23 06:49:22 +000067 //DEBUG(errs() << "outputing " << hex << d << dec << "\n");
Andrew Lenhartha4433e12005-07-28 12:45:20 +000068 AtI[2*x] = BUILD_ORi(27, 27, d);
69 }
70 AtI[17] = BUILD_JMP(31,27,0); //jump, preserving ra, and setting pv
71 AtI[18] = 0x00FFFFFF; //mark this as a stub
Andrew Lenharth0934ae02005-07-22 20:52:16 +000072}
73
74void AlphaJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
75 //FIXME
Torok Edwinc23197a2009-07-14 16:55:14 +000076 llvm_unreachable(0);
Andrew Lenharth0934ae02005-07-22 20:52:16 +000077}
78
79static TargetJITInfo::JITCompilerFn JITCompilerFunction;
80//static AlphaJITInfo* AlphaJTI;
81
82extern "C" {
Andrew Lenharth38396f82005-07-22 21:00:30 +000083#ifdef __alpha
Andrew Lenharth0934ae02005-07-22 20:52:16 +000084
Andrew Lenhartha4433e12005-07-28 12:45:20 +000085 void AlphaCompilationCallbackC(long* oldpv, void* CameFromStub)
Andrew Lenharth0934ae02005-07-22 20:52:16 +000086 {
Andrew Lenharth0934ae02005-07-22 20:52:16 +000087 void* Target = JITCompilerFunction(CameFromStub);
Jeff Cohen00b168892005-07-27 06:12:32 +000088
Andrew Lenharth0934ae02005-07-22 20:52:16 +000089 //rewrite the stub to an unconditional branch
Andrew Lenhartha4433e12005-07-28 12:45:20 +000090 if (((unsigned*)CameFromStub)[18] == 0x00FFFFFF) {
Chris Lattner893e1c92009-08-23 06:49:22 +000091 DEBUG(errs() << "Came from a stub, rewriting\n");
Andrew Lenhartha4433e12005-07-28 12:45:20 +000092 EmitBranchToAt(CameFromStub, Target);
93 } else {
Chris Lattner893e1c92009-08-23 06:49:22 +000094 DEBUG(errs() << "confused, didn't come from stub at " << CameFromStub
95 << " old jump vector " << oldpv
96 << " new jump vector " << Target << "\n");
Andrew Lenhartha4433e12005-07-28 12:45:20 +000097 }
Andrew Lenharth0934ae02005-07-22 20:52:16 +000098
99 //Change pv to new Target
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000100 *oldpv = (long)Target;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000101 }
102
103 void AlphaCompilationCallback(void);
104
105 asm(
106 ".text\n"
107 ".globl AlphaComilationCallbackC\n"
108 ".align 4\n"
109 ".globl AlphaCompilationCallback\n"
110 ".ent AlphaCompilationCallback\n"
111"AlphaCompilationCallback:\n"
112 // //get JIT's GOT
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000113 "ldgp $29, 0($27)\n"
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000114 //Save args, callee saved, and perhaps others?
115 //args: $16-$21 $f16-$f21 (12)
116 //callee: $9-$14 $f2-$f9 (14)
117 //others: fp:$15 ra:$26 pv:$27 (3)
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000118 "lda $30, -232($30)\n"
119 "stq $16, 0($30)\n"
120 "stq $17, 8($30)\n"
121 "stq $18, 16($30)\n"
122 "stq $19, 24($30)\n"
123 "stq $20, 32($30)\n"
124 "stq $21, 40($30)\n"
125 "stt $f16, 48($30)\n"
126 "stt $f17, 56($30)\n"
127 "stt $f18, 64($30)\n"
128 "stt $f19, 72($30)\n"
129 "stt $f20, 80($30)\n"
130 "stt $f21, 88($30)\n"
131 "stq $9, 96($30)\n"
132 "stq $10, 104($30)\n"
133 "stq $11, 112($30)\n"
134 "stq $12, 120($30)\n"
135 "stq $13, 128($30)\n"
136 "stq $14, 136($30)\n"
137 "stt $f2, 144($30)\n"
138 "stt $f3, 152($30)\n"
139 "stt $f4, 160($30)\n"
140 "stt $f5, 168($30)\n"
141 "stt $f6, 176($30)\n"
142 "stt $f7, 184($30)\n"
143 "stt $f8, 192($30)\n"
144 "stt $f9, 200($30)\n"
145 "stq $15, 208($30)\n"
146 "stq $26, 216($30)\n"
147 "stq $27, 224($30)\n"
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000148
149 "addq $30, 224, $16\n" //pass the addr of saved pv as the first arg
150 "bis $0, $0, $17\n" //pass the roughly stub addr in second arg
151 "jsr $26, AlphaCompilationCallbackC\n" //call without saving ra
152
153 "ldq $16, 0($30)\n"
154 "ldq $17, 8($30)\n"
155 "ldq $18, 16($30)\n"
156 "ldq $19, 24($30)\n"
157 "ldq $20, 32($30)\n"
158 "ldq $21, 40($30)\n"
159 "ldt $f16, 48($30)\n"
160 "ldt $f17, 56($30)\n"
161 "ldt $f18, 64($30)\n"
162 "ldt $f19, 72($30)\n"
163 "ldt $f20, 80($30)\n"
164 "ldt $f21, 88($30)\n"
165 "ldq $9, 96($30)\n"
166 "ldq $10, 104($30)\n"
167 "ldq $11, 112($30)\n"
168 "ldq $12, 120($30)\n"
169 "ldq $13, 128($30)\n"
170 "ldq $14, 136($30)\n"
171 "ldt $f2, 144($30)\n"
172 "ldt $f3, 152($30)\n"
173 "ldt $f4, 160($30)\n"
174 "ldt $f5, 168($30)\n"
175 "ldt $f6, 176($30)\n"
176 "ldt $f7, 184($30)\n"
177 "ldt $f8, 192($30)\n"
178 "ldt $f9, 200($30)\n"
179 "ldq $15, 208($30)\n"
180 "ldq $26, 216($30)\n"
181 "ldq $27, 224($30)\n" //this was updated in the callback with the target
182
183 "lda $30, 232($30)\n" //restore sp
184 "jmp $31, ($27)\n" //jump to the new function
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000185 ".end AlphaCompilationCallback\n"
186 );
187#else
188 void AlphaCompilationCallback() {
Torok Edwinc23197a2009-07-14 16:55:14 +0000189 llvm_unreachable("Cannot call AlphaCompilationCallback() on a non-Alpha arch!");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000190 }
191#endif
192}
193
Nicolas Geoffray51cc3c12008-04-16 20:46:05 +0000194void *AlphaJITInfo::emitFunctionStub(const Function* F, void *Fn,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000195 JITCodeEmitter &JCE) {
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000196 //assert(Fn == AlphaCompilationCallback && "Where are you going?\n");
197 //Do things in a stupid slow way!
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000198 JCE.startGVStub(F, 19*4);
199 void* Addr = (void*)(intptr_t)JCE.getCurrentPCValue();
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000200 for (int x = 0; x < 19; ++ x)
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000201 JCE.emitWordLE(0);
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000202 EmitBranchToAt(Addr, Fn);
Chris Lattner893e1c92009-08-23 06:49:22 +0000203 DEBUG(errs() << "Emitting Stub to " << Fn << " at [" << Addr << "]\n");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000204 return JCE.finishGVStub(F);
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000205}
206
207TargetJITInfo::LazyResolverFn
208AlphaJITInfo::getLazyResolverFunction(JITCompilerFn F) {
209 JITCompilerFunction = F;
210 // setZerothGOTEntry((void*)AlphaCompilationCallback);
211 return AlphaCompilationCallback;
212}
213
214//These describe LDAx
215static const int IMM_LOW = -32768;
216static const int IMM_HIGH = 32767;
217static const int IMM_MULT = 65536;
218
219static long getUpper16(long l)
220{
221 long y = l / IMM_MULT;
222 if (l % IMM_MULT > IMM_HIGH)
223 ++y;
224 if (l % IMM_MULT < IMM_LOW)
225 --y;
226 assert((short)y == y && "displacement out of range");
227 return y;
228}
229
230static long getLower16(long l)
231{
232 long h = getUpper16(l);
233 long y = l - h * IMM_MULT;
234 assert(y == (short)y && "Displacement out of range");
235 return y;
236}
237
238void AlphaJITInfo::relocate(void *Function, MachineRelocation *MR,
239 unsigned NumRelocs, unsigned char* GOTBase) {
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000240 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
241 unsigned *RelocPos = (unsigned*)Function + MR->getMachineCodeOffset()/4;
242 long idx = 0;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000243 bool doCommon = true;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000244 switch ((Alpha::RelocationType)MR->getRelocationType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000245 default: llvm_unreachable("Unknown relocation type!");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000246 case Alpha::reloc_literal:
247 //This is a LDQl
248 idx = MR->getGOTIndex();
Chris Lattner893e1c92009-08-23 06:49:22 +0000249 DEBUG(errs() << "Literal relocation to slot " << idx);
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000250 idx = (idx - GOToffset) * 8;
Chris Lattner893e1c92009-08-23 06:49:22 +0000251 DEBUG(errs() << " offset " << idx << "\n");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000252 break;
253 case Alpha::reloc_gprellow:
254 idx = (unsigned char*)MR->getResultPointer() - &GOTBase[GOToffset * 8];
255 idx = getLower16(idx);
Chris Lattner893e1c92009-08-23 06:49:22 +0000256 DEBUG(errs() << "gprellow relocation offset " << idx << "\n");
257 DEBUG(errs() << " Pointer is " << (void*)MR->getResultPointer()
258 << " GOT is " << (void*)&GOTBase[GOToffset * 8] << "\n");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000259 break;
260 case Alpha::reloc_gprelhigh:
261 idx = (unsigned char*)MR->getResultPointer() - &GOTBase[GOToffset * 8];
262 idx = getUpper16(idx);
Chris Lattner893e1c92009-08-23 06:49:22 +0000263 DEBUG(errs() << "gprelhigh relocation offset " << idx << "\n");
264 DEBUG(errs() << " Pointer is " << (void*)MR->getResultPointer()
265 << " GOT is " << (void*)&GOTBase[GOToffset * 8] << "\n");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000266 break;
267 case Alpha::reloc_gpdist:
268 switch (*RelocPos >> 26) {
269 case 0x09: //LDAH
270 idx = &GOTBase[GOToffset * 8] - (unsigned char*)RelocPos;
271 idx = getUpper16(idx);
Chris Lattner893e1c92009-08-23 06:49:22 +0000272 DEBUG(errs() << "LDAH: " << idx << "\n");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000273 //add the relocation to the map
Chris Lattner0e9aa452006-01-01 22:20:31 +0000274 gpdistmap[std::make_pair(Function, MR->getConstantVal())] = RelocPos;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000275 break;
276 case 0x08: //LDA
Chris Lattner0e9aa452006-01-01 22:20:31 +0000277 assert(gpdistmap[std::make_pair(Function, MR->getConstantVal())] &&
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000278 "LDAg without seeing LDAHg");
Jeff Cohen00b168892005-07-27 06:12:32 +0000279 idx = &GOTBase[GOToffset * 8] -
Chris Lattner0e9aa452006-01-01 22:20:31 +0000280 (unsigned char*)gpdistmap[std::make_pair(Function, MR->getConstantVal())];
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000281 idx = getLower16(idx);
Chris Lattner893e1c92009-08-23 06:49:22 +0000282 DEBUG(errs() << "LDA: " << idx << "\n");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000283 break;
284 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000285 llvm_unreachable("Cannot handle gpdist yet");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000286 }
287 break;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000288 case Alpha::reloc_bsr: {
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000289 idx = (((unsigned char*)MR->getResultPointer() -
Andrew Lenharth98169be2005-07-28 18:14:47 +0000290 (unsigned char*)RelocPos) >> 2) + 1; //skip first 2 inst of fun
291 *RelocPos |= (idx & ((1 << 21)-1));
292 doCommon = false;
293 break;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000294 }
Andrew Lenharth98169be2005-07-28 18:14:47 +0000295 }
296 if (doCommon) {
297 short x = (short)idx;
298 assert(x == idx);
299 *(short*)RelocPos = x;
300 }
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000301 }
302}