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> |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 21 | #include <map> |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Andrew Lenharth | a4433e1 | 2005-07-28 12:45:20 +0000 | [diff] [blame] | 24 | #define BUILD_OFormatI(Op, RA, LIT, FUN, RC) \ |
| 25 | ((Op << 26) | (RA << 21) | (LIT << 13) | (1 << 12) | (FUN << 5) | (RC)) |
| 26 | #define BUILD_OFormat(Op, RA, RB, FUN, RC) \ |
| 27 | ((Op << 26) | (RA << 21) | (RB << 16) | (FUN << 5) | (RC)) |
| 28 | |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 29 | #define BUILD_LDA(RD, RS, IMM16) \ |
| 30 | ((0x08 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 65535)) |
| 31 | #define BUILD_LDAH(RD, RS, IMM16) \ |
| 32 | ((0x09 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 65535)) |
| 33 | |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 34 | #define BUILD_LDQ(RD, RS, IMM16) \ |
| 35 | ((0x29 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 0xFFFF)) |
| 36 | |
| 37 | #define BUILD_JMP(RD, RS, IMM16) \ |
Andrew Lenharth | a4433e1 | 2005-07-28 12:45:20 +0000 | [diff] [blame] | 38 | ((0x1A << 26) | ((RD) << 21) | ((RS) << 16) | (0x00 << 14) | ((IMM16) & 0x3FFF)) |
| 39 | #define BUILD_JSR(RD, RS, IMM16) \ |
| 40 | ((0x1A << 26) | ((RD) << 21) | ((RS) << 16) | (0x01 << 14) | ((IMM16) & 0x3FFF)) |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 41 | |
Andrew Lenharth | a4433e1 | 2005-07-28 12:45:20 +0000 | [diff] [blame] | 42 | #define BUILD_SLLi(RD, RS, IMM8) \ |
| 43 | (BUILD_OFormatI(0x12, RS, IMM8, 0x39, RD)) |
| 44 | |
| 45 | #define BUILD_ORi(RD, RS, IMM8) \ |
| 46 | (BUILD_OFormatI(0x11, RS, IMM8, 0x20, RD)) |
| 47 | |
| 48 | #define BUILD_OR(RD, RS, RT) \ |
| 49 | (BUILD_OFormat(0x11, RS, RT, 0x20, RD)) |
| 50 | |
| 51 | |
| 52 | |
| 53 | static void EmitBranchToAt(void *At, void *To) { |
| 54 | unsigned long Fn = (unsigned long)To; |
| 55 | |
| 56 | unsigned *AtI = (unsigned*)At; |
| 57 | |
| 58 | AtI[0] = BUILD_OR(0, 27, 27); |
| 59 | |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame^] | 60 | DOUT << "Stub targeting " << To << "\n"; |
Andrew Lenharth | a4433e1 | 2005-07-28 12:45:20 +0000 | [diff] [blame] | 61 | |
| 62 | for (int x = 1; x <= 8; ++x) { |
| 63 | AtI[2*x - 1] = BUILD_SLLi(27,27,8); |
| 64 | unsigned d = (Fn >> (64 - 8 * x)) & 0x00FF; |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame^] | 65 | //DOUT << "outputing " << hex << d << dec << "\n"; |
Andrew Lenharth | a4433e1 | 2005-07-28 12:45:20 +0000 | [diff] [blame] | 66 | AtI[2*x] = BUILD_ORi(27, 27, d); |
| 67 | } |
| 68 | AtI[17] = BUILD_JMP(31,27,0); //jump, preserving ra, and setting pv |
| 69 | AtI[18] = 0x00FFFFFF; //mark this as a stub |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void AlphaJITInfo::replaceMachineCodeForFunction(void *Old, void *New) { |
| 73 | //FIXME |
| 74 | assert(0); |
| 75 | } |
| 76 | |
| 77 | static TargetJITInfo::JITCompilerFn JITCompilerFunction; |
| 78 | //static AlphaJITInfo* AlphaJTI; |
| 79 | |
| 80 | extern "C" { |
Andrew Lenharth | 38396f8 | 2005-07-22 21:00:30 +0000 | [diff] [blame] | 81 | #ifdef __alpha |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 82 | |
Andrew Lenharth | a4433e1 | 2005-07-28 12:45:20 +0000 | [diff] [blame] | 83 | void AlphaCompilationCallbackC(long* oldpv, void* CameFromStub) |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 84 | { |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 85 | void* Target = JITCompilerFunction(CameFromStub); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 86 | |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 87 | //rewrite the stub to an unconditional branch |
Andrew Lenharth | a4433e1 | 2005-07-28 12:45:20 +0000 | [diff] [blame] | 88 | if (((unsigned*)CameFromStub)[18] == 0x00FFFFFF) { |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame^] | 89 | DOUT << "Came from a stub, rewriting\n"; |
Andrew Lenharth | a4433e1 | 2005-07-28 12:45:20 +0000 | [diff] [blame] | 90 | EmitBranchToAt(CameFromStub, Target); |
| 91 | } else { |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame^] | 92 | DOUT << "confused, didn't come from stub at " << CameFromStub |
| 93 | << " old jump vector " << oldpv |
| 94 | << " new jump vector " << Target << "\n"; |
Andrew Lenharth | a4433e1 | 2005-07-28 12:45:20 +0000 | [diff] [blame] | 95 | } |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 96 | |
| 97 | //Change pv to new Target |
Andrew Lenharth | a4433e1 | 2005-07-28 12:45:20 +0000 | [diff] [blame] | 98 | *oldpv = (long)Target; |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void AlphaCompilationCallback(void); |
| 102 | |
| 103 | asm( |
| 104 | ".text\n" |
| 105 | ".globl AlphaComilationCallbackC\n" |
| 106 | ".align 4\n" |
| 107 | ".globl AlphaCompilationCallback\n" |
| 108 | ".ent AlphaCompilationCallback\n" |
| 109 | "AlphaCompilationCallback:\n" |
| 110 | // //get JIT's GOT |
Andrew Lenharth | a4433e1 | 2005-07-28 12:45:20 +0000 | [diff] [blame] | 111 | "ldgp $29, 0($27)\n" |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 112 | //Save args, callee saved, and perhaps others? |
| 113 | //args: $16-$21 $f16-$f21 (12) |
| 114 | //callee: $9-$14 $f2-$f9 (14) |
| 115 | //others: fp:$15 ra:$26 pv:$27 (3) |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 116 | "lda $30, -232($30)\n" |
| 117 | "stq $16, 0($30)\n" |
| 118 | "stq $17, 8($30)\n" |
| 119 | "stq $18, 16($30)\n" |
| 120 | "stq $19, 24($30)\n" |
| 121 | "stq $20, 32($30)\n" |
| 122 | "stq $21, 40($30)\n" |
| 123 | "stt $f16, 48($30)\n" |
| 124 | "stt $f17, 56($30)\n" |
| 125 | "stt $f18, 64($30)\n" |
| 126 | "stt $f19, 72($30)\n" |
| 127 | "stt $f20, 80($30)\n" |
| 128 | "stt $f21, 88($30)\n" |
| 129 | "stq $9, 96($30)\n" |
| 130 | "stq $10, 104($30)\n" |
| 131 | "stq $11, 112($30)\n" |
| 132 | "stq $12, 120($30)\n" |
| 133 | "stq $13, 128($30)\n" |
| 134 | "stq $14, 136($30)\n" |
| 135 | "stt $f2, 144($30)\n" |
| 136 | "stt $f3, 152($30)\n" |
| 137 | "stt $f4, 160($30)\n" |
| 138 | "stt $f5, 168($30)\n" |
| 139 | "stt $f6, 176($30)\n" |
| 140 | "stt $f7, 184($30)\n" |
| 141 | "stt $f8, 192($30)\n" |
| 142 | "stt $f9, 200($30)\n" |
| 143 | "stq $15, 208($30)\n" |
| 144 | "stq $26, 216($30)\n" |
| 145 | "stq $27, 224($30)\n" |
Andrew Lenharth | a4433e1 | 2005-07-28 12:45:20 +0000 | [diff] [blame] | 146 | |
| 147 | "addq $30, 224, $16\n" //pass the addr of saved pv as the first arg |
| 148 | "bis $0, $0, $17\n" //pass the roughly stub addr in second arg |
| 149 | "jsr $26, AlphaCompilationCallbackC\n" //call without saving ra |
| 150 | |
| 151 | "ldq $16, 0($30)\n" |
| 152 | "ldq $17, 8($30)\n" |
| 153 | "ldq $18, 16($30)\n" |
| 154 | "ldq $19, 24($30)\n" |
| 155 | "ldq $20, 32($30)\n" |
| 156 | "ldq $21, 40($30)\n" |
| 157 | "ldt $f16, 48($30)\n" |
| 158 | "ldt $f17, 56($30)\n" |
| 159 | "ldt $f18, 64($30)\n" |
| 160 | "ldt $f19, 72($30)\n" |
| 161 | "ldt $f20, 80($30)\n" |
| 162 | "ldt $f21, 88($30)\n" |
| 163 | "ldq $9, 96($30)\n" |
| 164 | "ldq $10, 104($30)\n" |
| 165 | "ldq $11, 112($30)\n" |
| 166 | "ldq $12, 120($30)\n" |
| 167 | "ldq $13, 128($30)\n" |
| 168 | "ldq $14, 136($30)\n" |
| 169 | "ldt $f2, 144($30)\n" |
| 170 | "ldt $f3, 152($30)\n" |
| 171 | "ldt $f4, 160($30)\n" |
| 172 | "ldt $f5, 168($30)\n" |
| 173 | "ldt $f6, 176($30)\n" |
| 174 | "ldt $f7, 184($30)\n" |
| 175 | "ldt $f8, 192($30)\n" |
| 176 | "ldt $f9, 200($30)\n" |
| 177 | "ldq $15, 208($30)\n" |
| 178 | "ldq $26, 216($30)\n" |
| 179 | "ldq $27, 224($30)\n" //this was updated in the callback with the target |
| 180 | |
| 181 | "lda $30, 232($30)\n" //restore sp |
| 182 | "jmp $31, ($27)\n" //jump to the new function |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 183 | ".end AlphaCompilationCallback\n" |
| 184 | ); |
| 185 | #else |
| 186 | void AlphaCompilationCallback() { |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame^] | 187 | cerr << "Cannot call AlphaCompilationCallback() on a non-Alpha arch!\n"; |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 188 | abort(); |
| 189 | } |
| 190 | #endif |
| 191 | } |
| 192 | |
| 193 | void *AlphaJITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) { |
Andrew Lenharth | a4433e1 | 2005-07-28 12:45:20 +0000 | [diff] [blame] | 194 | //assert(Fn == AlphaCompilationCallback && "Where are you going?\n"); |
| 195 | //Do things in a stupid slow way! |
| 196 | MCE.startFunctionStub(19*4); |
| 197 | void* Addr = (void*)(intptr_t)MCE.getCurrentPCValue(); |
| 198 | for (int x = 0; x < 19; ++ x) |
Chris Lattner | d3f0aef | 2006-05-02 19:14:47 +0000 | [diff] [blame] | 199 | MCE.emitWordLE(0); |
Andrew Lenharth | a4433e1 | 2005-07-28 12:45:20 +0000 | [diff] [blame] | 200 | EmitBranchToAt(Addr, Fn); |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame^] | 201 | DOUT << "Emitting Stub to " << Fn << " at [" << Addr << "]\n"; |
Andrew Lenharth | a4433e1 | 2005-07-28 12:45:20 +0000 | [diff] [blame] | 202 | return MCE.finishFunctionStub(0); |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | TargetJITInfo::LazyResolverFn |
| 206 | AlphaJITInfo::getLazyResolverFunction(JITCompilerFn F) { |
| 207 | JITCompilerFunction = F; |
| 208 | // setZerothGOTEntry((void*)AlphaCompilationCallback); |
| 209 | return AlphaCompilationCallback; |
| 210 | } |
| 211 | |
| 212 | //These describe LDAx |
| 213 | static const int IMM_LOW = -32768; |
| 214 | static const int IMM_HIGH = 32767; |
| 215 | static const int IMM_MULT = 65536; |
| 216 | |
| 217 | static long getUpper16(long l) |
| 218 | { |
| 219 | long y = l / IMM_MULT; |
| 220 | if (l % IMM_MULT > IMM_HIGH) |
| 221 | ++y; |
| 222 | if (l % IMM_MULT < IMM_LOW) |
| 223 | --y; |
| 224 | assert((short)y == y && "displacement out of range"); |
| 225 | return y; |
| 226 | } |
| 227 | |
| 228 | static long getLower16(long l) |
| 229 | { |
| 230 | long h = getUpper16(l); |
| 231 | long y = l - h * IMM_MULT; |
| 232 | assert(y == (short)y && "Displacement out of range"); |
| 233 | return y; |
| 234 | } |
| 235 | |
| 236 | void AlphaJITInfo::relocate(void *Function, MachineRelocation *MR, |
| 237 | unsigned NumRelocs, unsigned char* GOTBase) { |
| 238 | //because gpdist are paired and relative to the pc of the first inst, |
| 239 | //we need to have some state |
| 240 | |
Chris Lattner | 0e9aa45 | 2006-01-01 22:20:31 +0000 | [diff] [blame] | 241 | static std::map<std::pair<void*, int>, void*> gpdistmap; |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 242 | |
| 243 | for (unsigned i = 0; i != NumRelocs; ++i, ++MR) { |
| 244 | unsigned *RelocPos = (unsigned*)Function + MR->getMachineCodeOffset()/4; |
| 245 | long idx = 0; |
Andrew Lenharth | 98169be | 2005-07-28 18:14:47 +0000 | [diff] [blame] | 246 | bool doCommon = true; |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 247 | switch ((Alpha::RelocationType)MR->getRelocationType()) { |
| 248 | default: assert(0 && "Unknown relocation type!"); |
| 249 | case Alpha::reloc_literal: |
| 250 | //This is a LDQl |
| 251 | idx = MR->getGOTIndex(); |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame^] | 252 | DOUT << "Literal relocation to slot " << idx; |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 253 | idx = (idx - GOToffset) * 8; |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame^] | 254 | DOUT << " offset " << idx << "\n"; |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 255 | break; |
| 256 | case Alpha::reloc_gprellow: |
| 257 | idx = (unsigned char*)MR->getResultPointer() - &GOTBase[GOToffset * 8]; |
| 258 | idx = getLower16(idx); |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame^] | 259 | DOUT << "gprellow relocation offset " << idx << "\n"; |
| 260 | DOUT << " Pointer is " << (void*)MR->getResultPointer() |
| 261 | << " GOT is " << (void*)&GOTBase[GOToffset * 8] << "\n"; |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 262 | break; |
| 263 | case Alpha::reloc_gprelhigh: |
| 264 | idx = (unsigned char*)MR->getResultPointer() - &GOTBase[GOToffset * 8]; |
| 265 | idx = getUpper16(idx); |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame^] | 266 | DOUT << "gprelhigh relocation offset " << idx << "\n"; |
| 267 | DOUT << " Pointer is " << (void*)MR->getResultPointer() |
| 268 | << " GOT is " << (void*)&GOTBase[GOToffset * 8] << "\n"; |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 269 | break; |
| 270 | case Alpha::reloc_gpdist: |
| 271 | switch (*RelocPos >> 26) { |
| 272 | case 0x09: //LDAH |
| 273 | idx = &GOTBase[GOToffset * 8] - (unsigned char*)RelocPos; |
| 274 | idx = getUpper16(idx); |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame^] | 275 | DOUT << "LDAH: " << idx << "\n"; |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 276 | //add the relocation to the map |
Chris Lattner | 0e9aa45 | 2006-01-01 22:20:31 +0000 | [diff] [blame] | 277 | gpdistmap[std::make_pair(Function, MR->getConstantVal())] = RelocPos; |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 278 | break; |
| 279 | case 0x08: //LDA |
Chris Lattner | 0e9aa45 | 2006-01-01 22:20:31 +0000 | [diff] [blame] | 280 | assert(gpdistmap[std::make_pair(Function, MR->getConstantVal())] && |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 281 | "LDAg without seeing LDAHg"); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 282 | idx = &GOTBase[GOToffset * 8] - |
Chris Lattner | 0e9aa45 | 2006-01-01 22:20:31 +0000 | [diff] [blame] | 283 | (unsigned char*)gpdistmap[std::make_pair(Function, MR->getConstantVal())]; |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 284 | idx = getLower16(idx); |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame^] | 285 | DOUT << "LDA: " << idx << "\n"; |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 286 | break; |
| 287 | default: |
| 288 | assert(0 && "Cannot handle gpdist yet"); |
| 289 | } |
| 290 | break; |
Andrew Lenharth | 98169be | 2005-07-28 18:14:47 +0000 | [diff] [blame] | 291 | case Alpha::reloc_bsr: { |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 292 | idx = (((unsigned char*)MR->getResultPointer() - |
Andrew Lenharth | 98169be | 2005-07-28 18:14:47 +0000 | [diff] [blame] | 293 | (unsigned char*)RelocPos) >> 2) + 1; //skip first 2 inst of fun |
| 294 | *RelocPos |= (idx & ((1 << 21)-1)); |
| 295 | doCommon = false; |
| 296 | break; |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 297 | } |
Andrew Lenharth | 98169be | 2005-07-28 18:14:47 +0000 | [diff] [blame] | 298 | } |
| 299 | if (doCommon) { |
| 300 | short x = (short)idx; |
| 301 | assert(x == idx); |
| 302 | *(short*)RelocPos = x; |
| 303 | } |
Andrew Lenharth | 0934ae0 | 2005-07-22 20:52:16 +0000 | [diff] [blame] | 304 | } |
| 305 | } |