2005-07-22 20:52:16 +00:00
|
|
|
//===-- 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 <map>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2005-07-28 12:45:20 +00:00
|
|
|
#define BUILD_OFormatI(Op, RA, LIT, FUN, RC) \
|
|
|
|
((Op << 26) | (RA << 21) | (LIT << 13) | (1 << 12) | (FUN << 5) | (RC))
|
|
|
|
#define BUILD_OFormat(Op, RA, RB, FUN, RC) \
|
|
|
|
((Op << 26) | (RA << 21) | (RB << 16) | (FUN << 5) | (RC))
|
|
|
|
|
2005-07-22 20:52:16 +00:00
|
|
|
#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 BUILD_LDQ(RD, RS, IMM16) \
|
|
|
|
((0x29 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 0xFFFF))
|
|
|
|
|
|
|
|
#define BUILD_JMP(RD, RS, IMM16) \
|
2005-07-28 12:45:20 +00:00
|
|
|
((0x1A << 26) | ((RD) << 21) | ((RS) << 16) | (0x00 << 14) | ((IMM16) & 0x3FFF))
|
|
|
|
#define BUILD_JSR(RD, RS, IMM16) \
|
|
|
|
((0x1A << 26) | ((RD) << 21) | ((RS) << 16) | (0x01 << 14) | ((IMM16) & 0x3FFF))
|
2005-07-22 20:52:16 +00:00
|
|
|
|
2005-07-28 12:45:20 +00:00
|
|
|
#define BUILD_SLLi(RD, RS, IMM8) \
|
|
|
|
(BUILD_OFormatI(0x12, RS, IMM8, 0x39, RD))
|
|
|
|
|
|
|
|
#define BUILD_ORi(RD, RS, IMM8) \
|
|
|
|
(BUILD_OFormatI(0x11, RS, IMM8, 0x20, RD))
|
|
|
|
|
|
|
|
#define BUILD_OR(RD, RS, RT) \
|
|
|
|
(BUILD_OFormat(0x11, RS, RT, 0x20, RD))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void EmitBranchToAt(void *At, void *To) {
|
|
|
|
unsigned long Fn = (unsigned long)To;
|
|
|
|
|
|
|
|
unsigned *AtI = (unsigned*)At;
|
|
|
|
|
|
|
|
AtI[0] = BUILD_OR(0, 27, 27);
|
|
|
|
|
2006-12-07 22:21:48 +00:00
|
|
|
DOUT << "Stub targeting " << To << "\n";
|
2005-07-28 12:45:20 +00:00
|
|
|
|
|
|
|
for (int x = 1; x <= 8; ++x) {
|
|
|
|
AtI[2*x - 1] = BUILD_SLLi(27,27,8);
|
|
|
|
unsigned d = (Fn >> (64 - 8 * x)) & 0x00FF;
|
2006-12-07 22:21:48 +00:00
|
|
|
//DOUT << "outputing " << hex << d << dec << "\n";
|
2005-07-28 12:45:20 +00:00
|
|
|
AtI[2*x] = BUILD_ORi(27, 27, d);
|
|
|
|
}
|
|
|
|
AtI[17] = BUILD_JMP(31,27,0); //jump, preserving ra, and setting pv
|
|
|
|
AtI[18] = 0x00FFFFFF; //mark this as a stub
|
2005-07-22 20:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlphaJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
|
|
|
|
//FIXME
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static TargetJITInfo::JITCompilerFn JITCompilerFunction;
|
|
|
|
//static AlphaJITInfo* AlphaJTI;
|
|
|
|
|
|
|
|
extern "C" {
|
2005-07-22 21:00:30 +00:00
|
|
|
#ifdef __alpha
|
2005-07-22 20:52:16 +00:00
|
|
|
|
2005-07-28 12:45:20 +00:00
|
|
|
void AlphaCompilationCallbackC(long* oldpv, void* CameFromStub)
|
2005-07-22 20:52:16 +00:00
|
|
|
{
|
|
|
|
void* Target = JITCompilerFunction(CameFromStub);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-22 20:52:16 +00:00
|
|
|
//rewrite the stub to an unconditional branch
|
2005-07-28 12:45:20 +00:00
|
|
|
if (((unsigned*)CameFromStub)[18] == 0x00FFFFFF) {
|
2006-12-07 22:21:48 +00:00
|
|
|
DOUT << "Came from a stub, rewriting\n";
|
2005-07-28 12:45:20 +00:00
|
|
|
EmitBranchToAt(CameFromStub, Target);
|
|
|
|
} else {
|
2006-12-07 22:21:48 +00:00
|
|
|
DOUT << "confused, didn't come from stub at " << CameFromStub
|
|
|
|
<< " old jump vector " << oldpv
|
|
|
|
<< " new jump vector " << Target << "\n";
|
2005-07-28 12:45:20 +00:00
|
|
|
}
|
2005-07-22 20:52:16 +00:00
|
|
|
|
|
|
|
//Change pv to new Target
|
2005-07-28 12:45:20 +00:00
|
|
|
*oldpv = (long)Target;
|
2005-07-22 20:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlphaCompilationCallback(void);
|
|
|
|
|
|
|
|
asm(
|
|
|
|
".text\n"
|
|
|
|
".globl AlphaComilationCallbackC\n"
|
|
|
|
".align 4\n"
|
|
|
|
".globl AlphaCompilationCallback\n"
|
|
|
|
".ent AlphaCompilationCallback\n"
|
|
|
|
"AlphaCompilationCallback:\n"
|
|
|
|
// //get JIT's GOT
|
2005-07-28 12:45:20 +00:00
|
|
|
"ldgp $29, 0($27)\n"
|
2005-07-22 20:52:16 +00:00
|
|
|
//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)
|
|
|
|
"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"
|
2005-07-28 12:45:20 +00:00
|
|
|
|
|
|
|
"addq $30, 224, $16\n" //pass the addr of saved pv as the first arg
|
|
|
|
"bis $0, $0, $17\n" //pass the roughly stub addr in second arg
|
|
|
|
"jsr $26, AlphaCompilationCallbackC\n" //call without saving ra
|
|
|
|
|
|
|
|
"ldq $16, 0($30)\n"
|
|
|
|
"ldq $17, 8($30)\n"
|
|
|
|
"ldq $18, 16($30)\n"
|
|
|
|
"ldq $19, 24($30)\n"
|
|
|
|
"ldq $20, 32($30)\n"
|
|
|
|
"ldq $21, 40($30)\n"
|
|
|
|
"ldt $f16, 48($30)\n"
|
|
|
|
"ldt $f17, 56($30)\n"
|
|
|
|
"ldt $f18, 64($30)\n"
|
|
|
|
"ldt $f19, 72($30)\n"
|
|
|
|
"ldt $f20, 80($30)\n"
|
|
|
|
"ldt $f21, 88($30)\n"
|
|
|
|
"ldq $9, 96($30)\n"
|
|
|
|
"ldq $10, 104($30)\n"
|
|
|
|
"ldq $11, 112($30)\n"
|
|
|
|
"ldq $12, 120($30)\n"
|
|
|
|
"ldq $13, 128($30)\n"
|
|
|
|
"ldq $14, 136($30)\n"
|
|
|
|
"ldt $f2, 144($30)\n"
|
|
|
|
"ldt $f3, 152($30)\n"
|
|
|
|
"ldt $f4, 160($30)\n"
|
|
|
|
"ldt $f5, 168($30)\n"
|
|
|
|
"ldt $f6, 176($30)\n"
|
|
|
|
"ldt $f7, 184($30)\n"
|
|
|
|
"ldt $f8, 192($30)\n"
|
|
|
|
"ldt $f9, 200($30)\n"
|
|
|
|
"ldq $15, 208($30)\n"
|
|
|
|
"ldq $26, 216($30)\n"
|
|
|
|
"ldq $27, 224($30)\n" //this was updated in the callback with the target
|
|
|
|
|
|
|
|
"lda $30, 232($30)\n" //restore sp
|
|
|
|
"jmp $31, ($27)\n" //jump to the new function
|
2005-07-22 20:52:16 +00:00
|
|
|
".end AlphaCompilationCallback\n"
|
|
|
|
);
|
|
|
|
#else
|
|
|
|
void AlphaCompilationCallback() {
|
2006-12-07 22:21:48 +00:00
|
|
|
cerr << "Cannot call AlphaCompilationCallback() on a non-Alpha arch!\n";
|
2005-07-22 20:52:16 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void *AlphaJITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
|
2005-07-28 12:45:20 +00:00
|
|
|
//assert(Fn == AlphaCompilationCallback && "Where are you going?\n");
|
|
|
|
//Do things in a stupid slow way!
|
|
|
|
MCE.startFunctionStub(19*4);
|
|
|
|
void* Addr = (void*)(intptr_t)MCE.getCurrentPCValue();
|
|
|
|
for (int x = 0; x < 19; ++ x)
|
2006-05-02 19:14:47 +00:00
|
|
|
MCE.emitWordLE(0);
|
2005-07-28 12:45:20 +00:00
|
|
|
EmitBranchToAt(Addr, Fn);
|
2006-12-07 22:21:48 +00:00
|
|
|
DOUT << "Emitting Stub to " << Fn << " at [" << Addr << "]\n";
|
2005-07-28 12:45:20 +00:00
|
|
|
return MCE.finishFunctionStub(0);
|
2005-07-22 20:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2006-01-01 22:20:31 +00:00
|
|
|
static std::map<std::pair<void*, int>, void*> gpdistmap;
|
2005-07-22 20:52:16 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
|
|
|
|
unsigned *RelocPos = (unsigned*)Function + MR->getMachineCodeOffset()/4;
|
|
|
|
long idx = 0;
|
2005-07-28 18:14:47 +00:00
|
|
|
bool doCommon = true;
|
2005-07-22 20:52:16 +00:00
|
|
|
switch ((Alpha::RelocationType)MR->getRelocationType()) {
|
|
|
|
default: assert(0 && "Unknown relocation type!");
|
|
|
|
case Alpha::reloc_literal:
|
|
|
|
//This is a LDQl
|
|
|
|
idx = MR->getGOTIndex();
|
2006-12-07 22:21:48 +00:00
|
|
|
DOUT << "Literal relocation to slot " << idx;
|
2005-07-22 20:52:16 +00:00
|
|
|
idx = (idx - GOToffset) * 8;
|
2006-12-07 22:21:48 +00:00
|
|
|
DOUT << " offset " << idx << "\n";
|
2005-07-22 20:52:16 +00:00
|
|
|
break;
|
|
|
|
case Alpha::reloc_gprellow:
|
|
|
|
idx = (unsigned char*)MR->getResultPointer() - &GOTBase[GOToffset * 8];
|
|
|
|
idx = getLower16(idx);
|
2006-12-07 22:21:48 +00:00
|
|
|
DOUT << "gprellow relocation offset " << idx << "\n";
|
|
|
|
DOUT << " Pointer is " << (void*)MR->getResultPointer()
|
|
|
|
<< " GOT is " << (void*)&GOTBase[GOToffset * 8] << "\n";
|
2005-07-22 20:52:16 +00:00
|
|
|
break;
|
|
|
|
case Alpha::reloc_gprelhigh:
|
|
|
|
idx = (unsigned char*)MR->getResultPointer() - &GOTBase[GOToffset * 8];
|
|
|
|
idx = getUpper16(idx);
|
2006-12-07 22:21:48 +00:00
|
|
|
DOUT << "gprelhigh relocation offset " << idx << "\n";
|
|
|
|
DOUT << " Pointer is " << (void*)MR->getResultPointer()
|
|
|
|
<< " GOT is " << (void*)&GOTBase[GOToffset * 8] << "\n";
|
2005-07-22 20:52:16 +00:00
|
|
|
break;
|
|
|
|
case Alpha::reloc_gpdist:
|
|
|
|
switch (*RelocPos >> 26) {
|
|
|
|
case 0x09: //LDAH
|
|
|
|
idx = &GOTBase[GOToffset * 8] - (unsigned char*)RelocPos;
|
|
|
|
idx = getUpper16(idx);
|
2006-12-07 22:21:48 +00:00
|
|
|
DOUT << "LDAH: " << idx << "\n";
|
2005-07-22 20:52:16 +00:00
|
|
|
//add the relocation to the map
|
2006-01-01 22:20:31 +00:00
|
|
|
gpdistmap[std::make_pair(Function, MR->getConstantVal())] = RelocPos;
|
2005-07-22 20:52:16 +00:00
|
|
|
break;
|
|
|
|
case 0x08: //LDA
|
2006-01-01 22:20:31 +00:00
|
|
|
assert(gpdistmap[std::make_pair(Function, MR->getConstantVal())] &&
|
2005-07-22 20:52:16 +00:00
|
|
|
"LDAg without seeing LDAHg");
|
2005-07-27 06:12:32 +00:00
|
|
|
idx = &GOTBase[GOToffset * 8] -
|
2006-01-01 22:20:31 +00:00
|
|
|
(unsigned char*)gpdistmap[std::make_pair(Function, MR->getConstantVal())];
|
2005-07-22 20:52:16 +00:00
|
|
|
idx = getLower16(idx);
|
2006-12-07 22:21:48 +00:00
|
|
|
DOUT << "LDA: " << idx << "\n";
|
2005-07-22 20:52:16 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0 && "Cannot handle gpdist yet");
|
|
|
|
}
|
|
|
|
break;
|
2005-07-28 18:14:47 +00:00
|
|
|
case Alpha::reloc_bsr: {
|
2005-07-30 18:33:25 +00:00
|
|
|
idx = (((unsigned char*)MR->getResultPointer() -
|
2005-07-28 18:14:47 +00:00
|
|
|
(unsigned char*)RelocPos) >> 2) + 1; //skip first 2 inst of fun
|
|
|
|
*RelocPos |= (idx & ((1 << 21)-1));
|
|
|
|
doCommon = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (doCommon) {
|
|
|
|
short x = (short)idx;
|
|
|
|
assert(x == idx);
|
|
|
|
*(short*)RelocPos = x;
|
2005-07-22 20:52:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|