Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame^] | 1 | //===-- AlphaJITInfo.cpp - Implement the JIT interfaces for the Alpha ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 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" |
| 17 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 18 | #include "llvm/Config/alloca.h" |
| 19 | #include "llvm/Support/Debug.h" |
| 20 | #include <cstdlib> |
| 21 | #include <iostream> |
| 22 | #include <map> |
| 23 | using namespace std; |
| 24 | using namespace llvm; |
| 25 | |
| 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)) |
| 30 | |
| 31 | #define MERGE_PARTS(HH, HL, LH, LL) \ |
| 32 | (((HH * 65536 + HL) << 32) + (LH * 65536 + LL)) |
| 33 | |
| 34 | #define BUILD_LDQ(RD, RS, IMM16) \ |
| 35 | ((0x29 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 0xFFFF)) |
| 36 | |
| 37 | #define BUILD_JMP(RD, RS, IMM16) \ |
| 38 | ((0x1A << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 0xFFFF)) |
| 39 | |
| 40 | static void EmitBranchToAt(void *At, void *To, bool isCall) { |
| 41 | //FIXME |
| 42 | assert(0); |
| 43 | } |
| 44 | |
| 45 | void AlphaJITInfo::replaceMachineCodeForFunction(void *Old, void *New) { |
| 46 | //FIXME |
| 47 | assert(0); |
| 48 | } |
| 49 | |
| 50 | static TargetJITInfo::JITCompilerFn JITCompilerFunction; |
| 51 | //static AlphaJITInfo* AlphaJTI; |
| 52 | |
| 53 | extern "C" { |
| 54 | #if 0 |
| 55 | |
| 56 | void AlphaCompilationCallbackC(long* oldsp) |
| 57 | { |
| 58 | void* CameFromStub = (void*)*(oldsp - 1); |
| 59 | void* CameFromOrig = (void*)*(oldsp - 2); |
| 60 | |
| 61 | void* Target = JITCompilerFunction(CameFromStub); |
| 62 | |
| 63 | //rewrite the stub to an unconditional branch |
| 64 | EmitBranchToAt(CameFromStub, Target, false); |
| 65 | |
| 66 | //Change pv to new Target |
| 67 | *(oldsp - 1) = (long)Target; |
| 68 | |
| 69 | //special epilog |
| 70 | register long* RSP asm ("$0") = oldsp; |
| 71 | __asm__ __volatile__ ( |
| 72 | "ldq $16, 0($0)\n" |
| 73 | "ldq $17, 8($0)\n" |
| 74 | "ldq $18, 16($0)\n" |
| 75 | "ldq $19, 24($0)\n" |
| 76 | "ldq $20, 32($0)\n" |
| 77 | "ldq $21, 40($0)\n" |
| 78 | "ldt $f16, 48($0)\n" |
| 79 | "ldt $f17, 56($0)\n" |
| 80 | "ldt $f18, 64($0)\n" |
| 81 | "ldt $f19, 72($0)\n" |
| 82 | "ldt $f20, 80($0)\n" |
| 83 | "ldt $f21, 88($0)\n" |
| 84 | "ldq $9, 96($0)\n" |
| 85 | "ldq $10, 104($0)\n" |
| 86 | "ldq $11, 112($0)\n" |
| 87 | "ldq $12, 120($0)\n" |
| 88 | "ldq $13, 128($0)\n" |
| 89 | "ldq $14, 136($0)\n" |
| 90 | "ldt $f2, 144($0)\n" |
| 91 | "ldt $f3, 152($0)\n" |
| 92 | "ldt $f4, 160($0)\n" |
| 93 | "ldt $f5, 168($0)\n" |
| 94 | "ldt $f6, 176($0)\n" |
| 95 | "ldt $f7, 184($0)\n" |
| 96 | "ldt $f8, 192($0)\n" |
| 97 | "ldt $f9, 200($0)\n" |
| 98 | "ldq $15, 208($0)\n" |
| 99 | "ldq $26, 216($0)\n" |
| 100 | "ldq $27, 224($0)\n" |
| 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 |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | void AlphaCompilationCallback(void); |
| 108 | |
| 109 | asm( |
| 110 | ".text\n" |
| 111 | ".globl AlphaComilationCallbackC\n" |
| 112 | ".align 4\n" |
| 113 | ".globl AlphaCompilationCallback\n" |
| 114 | ".ent AlphaCompilationCallback\n" |
| 115 | "AlphaCompilationCallback:\n" |
| 116 | // //get JIT's GOT |
| 117 | // "ldgp\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" |
| 124 | "stq $16, 0($30)\n" |
| 125 | "stq $17, 8($30)\n" |
| 126 | "stq $18, 16($30)\n" |
| 127 | "stq $19, 24($30)\n" |
| 128 | "stq $20, 32($30)\n" |
| 129 | "stq $21, 40($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" |
| 136 | "stq $9, 96($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" |
| 156 | ); |
| 157 | #else |
| 158 | void AlphaCompilationCallback() { |
| 159 | std::cerr << "Cannot call AlphaCompilationCallback() on a non-Alpha arch!\n"; |
| 160 | abort(); |
| 161 | } |
| 162 | #endif |
| 163 | } |
| 164 | |
| 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); |
| 178 | // } |
| 179 | |
| 180 | assert(0 && "Need to be able to jump to this guy too"); |
| 181 | } |
| 182 | |
| 183 | TargetJITInfo::LazyResolverFn |
| 184 | AlphaJITInfo::getLazyResolverFunction(JITCompilerFn F) { |
| 185 | JITCompilerFunction = F; |
| 186 | // setZerothGOTEntry((void*)AlphaCompilationCallback); |
| 187 | return AlphaCompilationCallback; |
| 188 | } |
| 189 | |
| 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; |
| 194 | |
| 195 | static long getUpper16(long l) |
| 196 | { |
| 197 | long y = l / IMM_MULT; |
| 198 | if (l % IMM_MULT > IMM_HIGH) |
| 199 | ++y; |
| 200 | if (l % IMM_MULT < IMM_LOW) |
| 201 | --y; |
| 202 | assert((short)y == y && "displacement out of range"); |
| 203 | return y; |
| 204 | } |
| 205 | |
| 206 | static long getLower16(long l) |
| 207 | { |
| 208 | long h = getUpper16(l); |
| 209 | long y = l - h * IMM_MULT; |
| 210 | assert(y == (short)y && "Displacement out of range"); |
| 211 | return y; |
| 212 | } |
| 213 | |
| 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 |
| 218 | |
| 219 | static map<pair<void*, int>, void*> gpdistmap; |
| 220 | |
| 221 | for (unsigned i = 0; i != NumRelocs; ++i, ++MR) { |
| 222 | unsigned *RelocPos = (unsigned*)Function + MR->getMachineCodeOffset()/4; |
| 223 | long idx = 0; |
| 224 | switch ((Alpha::RelocationType)MR->getRelocationType()) { |
| 225 | default: assert(0 && "Unknown relocation type!"); |
| 226 | case Alpha::reloc_literal: |
| 227 | //This is a LDQl |
| 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"); |
| 232 | break; |
| 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"); |
| 239 | break; |
| 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"); |
| 246 | break; |
| 247 | case Alpha::reloc_gpdist: |
| 248 | switch (*RelocPos >> 26) { |
| 249 | case 0x09: //LDAH |
| 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; |
| 255 | break; |
| 256 | case 0x08: //LDA |
| 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"); |
| 263 | break; |
| 264 | default: |
| 265 | assert(0 && "Cannot handle gpdist yet"); |
| 266 | } |
| 267 | break; |
| 268 | } |
| 269 | short x = (short)idx; |
| 270 | assert(x == idx); |
| 271 | *(short*)RelocPos = x; |
| 272 | } |
| 273 | } |