1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
//===-- AlphaJITInfo.cpp - Implement the JIT interfaces for the Alpha ---===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the JIT interfaces for the Alpha target.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "jit"
#include "AlphaJITInfo.h"
#include "AlphaRelocations.h"
#include "llvm/CodeGen/MachineCodeEmitter.h"
#include "llvm/Config/alloca.h"
#include "llvm/Support/Debug.h"
#include <cstdlib>
#include <iostream>
#include <map>
using namespace std;
using namespace llvm;
#define BUILD_LDA(RD, RS, IMM16) \
((0x08 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 65535))
#define BUILD_LDAH(RD, RS, IMM16) \
((0x09 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 65535))
#define MERGE_PARTS(HH, HL, LH, LL) \
(((HH * 65536 + HL) << 32) + (LH * 65536 + LL))
#define BUILD_LDQ(RD, RS, IMM16) \
((0x29 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 0xFFFF))
#define BUILD_JMP(RD, RS, IMM16) \
((0x1A << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 0xFFFF))
static void EmitBranchToAt(void *At, void *To, bool isCall) {
//FIXME
assert(0);
}
void AlphaJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
//FIXME
assert(0);
}
static TargetJITInfo::JITCompilerFn JITCompilerFunction;
//static AlphaJITInfo* AlphaJTI;
extern "C" {
#ifdef __alpha
void AlphaCompilationCallbackC(long* oldsp)
{
void* CameFromStub = (void*)*(oldsp - 1);
void* CameFromOrig = (void*)*(oldsp - 2);
void* Target = JITCompilerFunction(CameFromStub);
//rewrite the stub to an unconditional branch
EmitBranchToAt(CameFromStub, Target, false);
//Change pv to new Target
*(oldsp - 1) = (long)Target;
//special epilog
register long* RSP asm ("$0") = oldsp;
__asm__ __volatile__ (
"ldq $16, 0($0)\n"
"ldq $17, 8($0)\n"
"ldq $18, 16($0)\n"
"ldq $19, 24($0)\n"
"ldq $20, 32($0)\n"
"ldq $21, 40($0)\n"
"ldt $f16, 48($0)\n"
"ldt $f17, 56($0)\n"
"ldt $f18, 64($0)\n"
"ldt $f19, 72($0)\n"
"ldt $f20, 80($0)\n"
"ldt $f21, 88($0)\n"
"ldq $9, 96($0)\n"
"ldq $10, 104($0)\n"
"ldq $11, 112($0)\n"
"ldq $12, 120($0)\n"
"ldq $13, 128($0)\n"
"ldq $14, 136($0)\n"
"ldt $f2, 144($0)\n"
"ldt $f3, 152($0)\n"
"ldt $f4, 160($0)\n"
"ldt $f5, 168($0)\n"
"ldt $f6, 176($0)\n"
"ldt $f7, 184($0)\n"
"ldt $f8, 192($0)\n"
"ldt $f9, 200($0)\n"
"ldq $15, 208($0)\n"
"ldq $26, 216($0)\n"
"ldq $27, 224($0)\n"
"bis $30, $0, $0\n" //restore sp
"jmp $31, ($27)\n" //jump to the new function
"and $0, $31, $31\n" //dummy use of r0
);
}
void AlphaCompilationCallback(void);
asm(
".text\n"
".globl AlphaComilationCallbackC\n"
".align 4\n"
".globl AlphaCompilationCallback\n"
".ent AlphaCompilationCallback\n"
"AlphaCompilationCallback:\n"
// //get JIT's GOT
// "ldgp\n"
//Save args, callee saved, and perhaps others?
//args: $16-$21 $f16-$f21 (12)
//callee: $9-$14 $f2-$f9 (14)
//others: fp:$15 ra:$26 pv:$27 (3)
"bis $0, $30, $30\n" //0 = sp
"lda $30, -232($30)\n"
"stq $16, 0($30)\n"
"stq $17, 8($30)\n"
"stq $18, 16($30)\n"
"stq $19, 24($30)\n"
"stq $20, 32($30)\n"
"stq $21, 40($30)\n"
"stt $f16, 48($30)\n"
"stt $f17, 56($30)\n"
"stt $f18, 64($30)\n"
"stt $f19, 72($30)\n"
"stt $f20, 80($30)\n"
"stt $f21, 88($30)\n"
"stq $9, 96($30)\n"
"stq $10, 104($30)\n"
"stq $11, 112($30)\n"
"stq $12, 120($30)\n"
"stq $13, 128($30)\n"
"stq $14, 136($30)\n"
"stt $f2, 144($30)\n"
"stt $f3, 152($30)\n"
"stt $f4, 160($30)\n"
"stt $f5, 168($30)\n"
"stt $f6, 176($30)\n"
"stt $f7, 184($30)\n"
"stt $f8, 192($30)\n"
"stt $f9, 200($30)\n"
"stq $15, 208($30)\n"
"stq $26, 216($30)\n"
"stq $27, 224($30)\n"
"bis $16, $0, $0\n" //pass the old sp as the first arg
"bsr $31, AlphaCompilationCallbackC\n"
".end AlphaCompilationCallback\n"
);
#else
void AlphaCompilationCallback() {
std::cerr << "Cannot call AlphaCompilationCallback() on a non-Alpha arch!\n";
abort();
}
#endif
}
void *AlphaJITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
// // If this is just a call to an external function, emit a branch instead of a
// // call. This means looking up Fn and storing that in R27 so as to appear to
// // have called there originally
// if (Fn != AlphaCompilationCallback) {
// int idx = AlphaJTI->getNewGOTEntry(Fn);
// //R27 = ldq idx(R29)
// //R31 = JMP R27, 0
// MCE.startFunctionStub(2*4);
// void *Addr = (void*)(intptr_t)MCE.getCurrentPCValue();
// MCE.emitWord(BUILD_LDQ(27, 29, idx << 3));
// MCE.emitWord(BUILD_JMP(31, 27, 0));
// return MCE.finishFunctionStub(0);
// }
assert(0 && "Need to be able to jump to this guy too");
}
TargetJITInfo::LazyResolverFn
AlphaJITInfo::getLazyResolverFunction(JITCompilerFn F) {
JITCompilerFunction = F;
// setZerothGOTEntry((void*)AlphaCompilationCallback);
return AlphaCompilationCallback;
}
//These describe LDAx
static const int IMM_LOW = -32768;
static const int IMM_HIGH = 32767;
static const int IMM_MULT = 65536;
static long getUpper16(long l)
{
long y = l / IMM_MULT;
if (l % IMM_MULT > IMM_HIGH)
++y;
if (l % IMM_MULT < IMM_LOW)
--y;
assert((short)y == y && "displacement out of range");
return y;
}
static long getLower16(long l)
{
long h = getUpper16(l);
long y = l - h * IMM_MULT;
assert(y == (short)y && "Displacement out of range");
return y;
}
void AlphaJITInfo::relocate(void *Function, MachineRelocation *MR,
unsigned NumRelocs, unsigned char* GOTBase) {
//because gpdist are paired and relative to the pc of the first inst,
//we need to have some state
static map<pair<void*, int>, void*> gpdistmap;
for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
unsigned *RelocPos = (unsigned*)Function + MR->getMachineCodeOffset()/4;
long idx = 0;
switch ((Alpha::RelocationType)MR->getRelocationType()) {
default: assert(0 && "Unknown relocation type!");
case Alpha::reloc_literal:
//This is a LDQl
idx = MR->getGOTIndex();
DEBUG(std::cerr << "Literal relocation to slot " << idx);
idx = (idx - GOToffset) * 8;
DEBUG(std::cerr << " offset " << idx << "\n");
break;
case Alpha::reloc_gprellow:
idx = (unsigned char*)MR->getResultPointer() - &GOTBase[GOToffset * 8];
idx = getLower16(idx);
DEBUG(std::cerr << "gprellow relocation offset " << idx << "\n");
DEBUG(std::cerr << " Pointer is " << (void*)MR->getResultPointer()
<< " GOT is " << (void*)&GOTBase[GOToffset * 8] << "\n");
break;
case Alpha::reloc_gprelhigh:
idx = (unsigned char*)MR->getResultPointer() - &GOTBase[GOToffset * 8];
idx = getUpper16(idx);
DEBUG(std::cerr << "gprelhigh relocation offset " << idx << "\n");
DEBUG(std::cerr << " Pointer is " << (void*)MR->getResultPointer()
<< " GOT is " << (void*)&GOTBase[GOToffset * 8] << "\n");
break;
case Alpha::reloc_gpdist:
switch (*RelocPos >> 26) {
case 0x09: //LDAH
idx = &GOTBase[GOToffset * 8] - (unsigned char*)RelocPos;
idx = getUpper16(idx);
DEBUG(std::cerr << "LDAH: " << idx << "\n");
//add the relocation to the map
gpdistmap[make_pair(Function, MR->getConstantVal())] = RelocPos;
break;
case 0x08: //LDA
assert(gpdistmap[make_pair(Function, MR->getConstantVal())] &&
"LDAg without seeing LDAHg");
idx = &GOTBase[GOToffset * 8] -
(unsigned char*)gpdistmap[make_pair(Function, MR->getConstantVal())];
idx = getLower16(idx);
DEBUG(std::cerr << "LDA: " << idx << "\n");
break;
default:
assert(0 && "Cannot handle gpdist yet");
}
break;
}
short x = (short)idx;
assert(x == idx);
*(short*)RelocPos = x;
}
}
|