LLVM API Documentation

X86JITInfo.cpp

Go to the documentation of this file.
00001 //===-- X86JITInfo.cpp - Implement the JIT interfaces for the X86 target --===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the JIT interfaces for the X86 target.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DEBUG_TYPE "jit"
00015 #include "X86JITInfo.h"
00016 #include "X86Relocations.h"
00017 #include "X86Subtarget.h"
00018 #include "llvm/Function.h"
00019 #include "llvm/Config/alloca.h"
00020 #include "llvm/Support/Compiler.h"
00021 #include "llvm/Support/ErrorHandling.h"
00022 #include <cstdlib>
00023 #include <cstring>
00024 using namespace llvm;
00025 
00026 // Determine the platform we're running on
00027 #if defined (__x86_64__) || defined (_M_AMD64) || defined (_M_X64)
00028 # define X86_64_JIT
00029 #elif defined(__i386__) || defined(i386) || defined(_M_IX86)
00030 # define X86_32_JIT
00031 #endif
00032 
00033 void X86JITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
00034   unsigned char *OldByte = (unsigned char *)Old;
00035   *OldByte++ = 0xE9;                // Emit JMP opcode.
00036   unsigned *OldWord = (unsigned *)OldByte;
00037   unsigned NewAddr = (intptr_t)New;
00038   unsigned OldAddr = (intptr_t)OldWord;
00039   *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
00040 }
00041 
00042 
00043 /// JITCompilerFunction - This contains the address of the JIT function used to
00044 /// compile a function lazily.
00045 static TargetJITInfo::JITCompilerFn JITCompilerFunction;
00046 
00047 // Get the ASMPREFIX for the current host.  This is often '_'.
00048 #ifndef __USER_LABEL_PREFIX__
00049 #define __USER_LABEL_PREFIX__
00050 #endif
00051 #define GETASMPREFIX2(X) #X
00052 #define GETASMPREFIX(X) GETASMPREFIX2(X)
00053 #define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
00054 
00055 // For ELF targets, use a .size and .type directive, to let tools
00056 // know the extent of functions defined in assembler.
00057 #if defined(__ELF__)
00058 # define SIZE(sym) ".size " #sym ", . - " #sym "\n"
00059 # define TYPE_FUNCTION(sym) ".type " #sym ", @function\n"
00060 #else
00061 # define SIZE(sym)
00062 # define TYPE_FUNCTION(sym)
00063 #endif
00064 
00065 // Provide a convenient way for disabling usage of CFI directives.
00066 // This is needed for old/broken assemblers (for example, gas on
00067 // Darwin is pretty old and doesn't support these directives)
00068 #if defined(__APPLE__)
00069 # define CFI(x)
00070 #else
00071 // FIXME: Disable this until we really want to use it. Also, we will
00072 //        need to add some workarounds for compilers, which support
00073 //        only subset of these directives.
00074 # define CFI(x)
00075 #endif
00076 
00077 // Provide a wrapper for X86CompilationCallback2 that saves non-traditional
00078 // callee saved registers, for the fastcc calling convention.
00079 extern "C" {
00080 #if defined(X86_64_JIT)
00081 # ifndef _MSC_VER
00082   // No need to save EAX/EDX for X86-64.
00083   void X86CompilationCallback(void);
00084   asm(
00085     ".text\n"
00086     ".align 8\n"
00087     ".globl " ASMPREFIX "X86CompilationCallback\n"
00088     TYPE_FUNCTION(X86CompilationCallback)
00089   ASMPREFIX "X86CompilationCallback:\n"
00090     CFI(".cfi_startproc\n")
00091     // Save RBP
00092     "pushq   %rbp\n"
00093     CFI(".cfi_def_cfa_offset 16\n")
00094     CFI(".cfi_offset %rbp, -16\n")
00095     // Save RSP
00096     "movq    %rsp, %rbp\n"
00097     CFI(".cfi_def_cfa_register %rbp\n")
00098     // Save all int arg registers
00099     "pushq   %rdi\n"
00100     CFI(".cfi_rel_offset %rdi, 0\n")
00101     "pushq   %rsi\n"
00102     CFI(".cfi_rel_offset %rsi, 8\n")
00103     "pushq   %rdx\n"
00104     CFI(".cfi_rel_offset %rdx, 16\n")
00105     "pushq   %rcx\n"
00106     CFI(".cfi_rel_offset %rcx, 24\n")
00107     "pushq   %r8\n"
00108     CFI(".cfi_rel_offset %r8, 32\n")
00109     "pushq   %r9\n"
00110     CFI(".cfi_rel_offset %r9, 40\n")
00111     // Align stack on 16-byte boundary. ESP might not be properly aligned
00112     // (8 byte) if this is called from an indirect stub.
00113     "andq    $-16, %rsp\n"
00114     // Save all XMM arg registers
00115     "subq    $128, %rsp\n"
00116     "movaps  %xmm0, (%rsp)\n"
00117     "movaps  %xmm1, 16(%rsp)\n"
00118     "movaps  %xmm2, 32(%rsp)\n"
00119     "movaps  %xmm3, 48(%rsp)\n"
00120     "movaps  %xmm4, 64(%rsp)\n"
00121     "movaps  %xmm5, 80(%rsp)\n"
00122     "movaps  %xmm6, 96(%rsp)\n"
00123     "movaps  %xmm7, 112(%rsp)\n"
00124     // JIT callee
00125     "movq    %rbp, %rdi\n"    // Pass prev frame and return address
00126     "movq    8(%rbp), %rsi\n"
00127     "call    " ASMPREFIX "X86CompilationCallback2\n"
00128     // Restore all XMM arg registers
00129     "movaps  112(%rsp), %xmm7\n"
00130     "movaps  96(%rsp), %xmm6\n"
00131     "movaps  80(%rsp), %xmm5\n"
00132     "movaps  64(%rsp), %xmm4\n"
00133     "movaps  48(%rsp), %xmm3\n"
00134     "movaps  32(%rsp), %xmm2\n"
00135     "movaps  16(%rsp), %xmm1\n"
00136     "movaps  (%rsp), %xmm0\n"
00137     // Restore RSP
00138     "movq    %rbp, %rsp\n"
00139     CFI(".cfi_def_cfa_register %rsp\n")
00140     // Restore all int arg registers
00141     "subq    $48, %rsp\n"
00142     CFI(".cfi_adjust_cfa_offset 48\n")
00143     "popq    %r9\n"
00144     CFI(".cfi_adjust_cfa_offset -8\n")
00145     CFI(".cfi_restore %r9\n")
00146     "popq    %r8\n"
00147     CFI(".cfi_adjust_cfa_offset -8\n")
00148     CFI(".cfi_restore %r8\n")
00149     "popq    %rcx\n"
00150     CFI(".cfi_adjust_cfa_offset -8\n")
00151     CFI(".cfi_restore %rcx\n")
00152     "popq    %rdx\n"
00153     CFI(".cfi_adjust_cfa_offset -8\n")
00154     CFI(".cfi_restore %rdx\n")
00155     "popq    %rsi\n"
00156     CFI(".cfi_adjust_cfa_offset -8\n")
00157     CFI(".cfi_restore %rsi\n")
00158     "popq    %rdi\n"
00159     CFI(".cfi_adjust_cfa_offset -8\n")
00160     CFI(".cfi_restore %rdi\n")
00161     // Restore RBP
00162     "popq    %rbp\n"
00163     CFI(".cfi_adjust_cfa_offset -8\n")
00164     CFI(".cfi_restore %rbp\n")
00165     "ret\n"
00166     CFI(".cfi_endproc\n")
00167     SIZE(X86CompilationCallback)
00168   );
00169 # else
00170   // No inline assembler support on this platform. The routine is in external
00171   // file.
00172   void X86CompilationCallback();
00173 
00174 # endif
00175 #elif defined (X86_32_JIT)
00176 # ifndef _MSC_VER
00177   void X86CompilationCallback(void);
00178   asm(
00179     ".text\n"
00180     ".align 8\n"
00181     ".globl " ASMPREFIX "X86CompilationCallback\n"
00182     TYPE_FUNCTION(X86CompilationCallback)
00183   ASMPREFIX "X86CompilationCallback:\n"
00184     CFI(".cfi_startproc\n")
00185     "pushl   %ebp\n"
00186     CFI(".cfi_def_cfa_offset 8\n")
00187     CFI(".cfi_offset %ebp, -8\n")
00188     "movl    %esp, %ebp\n"    // Standard prologue
00189     CFI(".cfi_def_cfa_register %ebp\n")
00190     "pushl   %eax\n"
00191     CFI(".cfi_rel_offset %eax, 0\n")
00192     "pushl   %edx\n"          // Save EAX/EDX/ECX
00193     CFI(".cfi_rel_offset %edx, 4\n")
00194     "pushl   %ecx\n"
00195     CFI(".cfi_rel_offset %ecx, 8\n")
00196 #  if defined(__APPLE__)
00197     "andl    $-16, %esp\n"    // Align ESP on 16-byte boundary
00198 #  endif
00199     "subl    $16, %esp\n"
00200     "movl    4(%ebp), %eax\n" // Pass prev frame and return address
00201     "movl    %eax, 4(%esp)\n"
00202     "movl    %ebp, (%esp)\n"
00203     "call    " ASMPREFIX "X86CompilationCallback2\n"
00204     "movl    %ebp, %esp\n"    // Restore ESP
00205     CFI(".cfi_def_cfa_register %esp\n")
00206     "subl    $12, %esp\n"
00207     CFI(".cfi_adjust_cfa_offset 12\n")
00208     "popl    %ecx\n"
00209     CFI(".cfi_adjust_cfa_offset -4\n")
00210     CFI(".cfi_restore %ecx\n")
00211     "popl    %edx\n"
00212     CFI(".cfi_adjust_cfa_offset -4\n")
00213     CFI(".cfi_restore %edx\n")
00214     "popl    %eax\n"
00215     CFI(".cfi_adjust_cfa_offset -4\n")
00216     CFI(".cfi_restore %eax\n")
00217     "popl    %ebp\n"
00218     CFI(".cfi_adjust_cfa_offset -4\n")
00219     CFI(".cfi_restore %ebp\n")
00220     "ret\n"
00221     CFI(".cfi_endproc\n")
00222     SIZE(X86CompilationCallback)
00223   );
00224 
00225   // Same as X86CompilationCallback but also saves XMM argument registers.
00226   void X86CompilationCallback_SSE(void);
00227   asm(
00228     ".text\n"
00229     ".align 8\n"
00230     ".globl " ASMPREFIX "X86CompilationCallback_SSE\n"
00231     TYPE_FUNCTION(X86CompilationCallback_SSE)
00232   ASMPREFIX "X86CompilationCallback_SSE:\n"
00233     CFI(".cfi_startproc\n")
00234     "pushl   %ebp\n"
00235     CFI(".cfi_def_cfa_offset 8\n")
00236     CFI(".cfi_offset %ebp, -8\n")
00237     "movl    %esp, %ebp\n"    // Standard prologue
00238     CFI(".cfi_def_cfa_register %ebp\n")
00239     "pushl   %eax\n"
00240     CFI(".cfi_rel_offset %eax, 0\n")
00241     "pushl   %edx\n"          // Save EAX/EDX/ECX
00242     CFI(".cfi_rel_offset %edx, 4\n")
00243     "pushl   %ecx\n"
00244     CFI(".cfi_rel_offset %ecx, 8\n")
00245     "andl    $-16, %esp\n"    // Align ESP on 16-byte boundary
00246     // Save all XMM arg registers
00247     "subl    $64, %esp\n"
00248     // FIXME: provide frame move information for xmm registers.
00249     // This can be tricky, because CFA register is ebp (unaligned)
00250     // and we need to produce offsets relative to it.
00251     "movaps  %xmm0, (%esp)\n"
00252     "movaps  %xmm1, 16(%esp)\n"
00253     "movaps  %xmm2, 32(%esp)\n"
00254     "movaps  %xmm3, 48(%esp)\n"
00255     "subl    $16, %esp\n"
00256     "movl    4(%ebp), %eax\n" // Pass prev frame and return address
00257     "movl    %eax, 4(%esp)\n"
00258     "movl    %ebp, (%esp)\n"
00259     "call    " ASMPREFIX "X86CompilationCallback2\n"
00260     "addl    $16, %esp\n"
00261     "movaps  48(%esp), %xmm3\n"
00262     CFI(".cfi_restore %xmm3\n")
00263     "movaps  32(%esp), %xmm2\n"
00264     CFI(".cfi_restore %xmm2\n")
00265     "movaps  16(%esp), %xmm1\n"
00266     CFI(".cfi_restore %xmm1\n")
00267     "movaps  (%esp), %xmm0\n"
00268     CFI(".cfi_restore %xmm0\n")
00269     "movl    %ebp, %esp\n"    // Restore ESP
00270     CFI(".cfi_def_cfa_register esp\n")
00271     "subl    $12, %esp\n"
00272     CFI(".cfi_adjust_cfa_offset 12\n")
00273     "popl    %ecx\n"
00274     CFI(".cfi_adjust_cfa_offset -4\n")
00275     CFI(".cfi_restore %ecx\n")
00276     "popl    %edx\n"
00277     CFI(".cfi_adjust_cfa_offset -4\n")
00278     CFI(".cfi_restore %edx\n")
00279     "popl    %eax\n"
00280     CFI(".cfi_adjust_cfa_offset -4\n")
00281     CFI(".cfi_restore %eax\n")
00282     "popl    %ebp\n"
00283     CFI(".cfi_adjust_cfa_offset -4\n")
00284     CFI(".cfi_restore %ebp\n")
00285     "ret\n"
00286     CFI(".cfi_endproc\n")
00287     SIZE(X86CompilationCallback_SSE)
00288   );
00289 # else
00290   void X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr);
00291 
00292   _declspec(naked) void X86CompilationCallback(void) {
00293     __asm {
00294       push  ebp
00295       mov   ebp, esp
00296       push  eax
00297       push  edx
00298       push  ecx
00299       and   esp, -16
00300       mov   eax, dword ptr [ebp+4]
00301       mov   dword ptr [esp+4], eax
00302       mov   dword ptr [esp], ebp
00303       call  X86CompilationCallback2
00304       mov   esp, ebp
00305       sub   esp, 12
00306       pop   ecx
00307       pop   edx
00308       pop   eax
00309       pop   ebp
00310       ret
00311     }
00312   }
00313 
00314 # endif // _MSC_VER
00315 
00316 #else // Not an i386 host
00317   void X86CompilationCallback() {
00318     llvm_unreachable("Cannot call X86CompilationCallback() on a non-x86 arch!");
00319   }
00320 #endif
00321 }
00322 
00323 /// X86CompilationCallback2 - This is the target-specific function invoked by the
00324 /// function stub when we did not know the real target of a call.  This function
00325 /// must locate the start of the stub or call site and pass it into the JIT
00326 /// compiler function.
00327 extern "C" {
00328 #if !(defined (X86_64_JIT) && defined(_MSC_VER))
00329  // the following function is called only from this translation unit,
00330  // unless we are under 64bit Windows with MSC, where there is 
00331  // no support for inline assembly
00332 static
00333 #endif
00334 void ATTRIBUTE_USED
00335 X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr) {
00336   intptr_t *RetAddrLoc = &StackPtr[1];
00337   assert(*RetAddrLoc == RetAddr &&
00338          "Could not find return address on the stack!");
00339 
00340   // It's a stub if there is an interrupt marker after the call.
00341   bool isStub = ((unsigned char*)RetAddr)[0] == 0xCE;
00342 
00343   // The call instruction should have pushed the return value onto the stack...
00344 #if defined (X86_64_JIT)
00345   RetAddr--;     // Backtrack to the reference itself...
00346 #else
00347   RetAddr -= 4;  // Backtrack to the reference itself...
00348 #endif
00349 
00350 #if 0
00351   DEBUG(errs() << "In callback! Addr=" << (void*)RetAddr
00352                << " ESP=" << (void*)StackPtr
00353                << ": Resolving call to function: "
00354                << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
00355 #endif
00356 
00357   // Sanity check to make sure this really is a call instruction.
00358 #if defined (X86_64_JIT)
00359   assert(((unsigned char*)RetAddr)[-2] == 0x41 &&"Not a call instr!");
00360   assert(((unsigned char*)RetAddr)[-1] == 0xFF &&"Not a call instr!");
00361 #else
00362   assert(((unsigned char*)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
00363 #endif
00364 
00365   intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
00366 
00367   // Rewrite the call target... so that we don't end up here every time we
00368   // execute the call.
00369 #if defined (X86_64_JIT)
00370   if (!isStub)
00371     *(intptr_t *)(RetAddr - 0xa) = NewVal;
00372 #else
00373   *(intptr_t *)RetAddr = (intptr_t)(NewVal-RetAddr-4);
00374 #endif
00375 
00376   if (isStub) {
00377     // If this is a stub, rewrite the call into an unconditional branch
00378     // instruction so that two return addresses are not pushed onto the stack
00379     // when the requested function finally gets called.  This also makes the
00380     // 0xCE byte (interrupt) dead, so the marker doesn't effect anything.
00381 #if defined (X86_64_JIT)
00382     // If the target address is within 32-bit range of the stub, use a
00383     // PC-relative branch instead of loading the actual address.  (This is
00384     // considerably shorter than the 64-bit immediate load already there.)
00385     // We assume here intptr_t is 64 bits.
00386     intptr_t diff = NewVal-RetAddr+7;
00387     if (diff >= -2147483648LL && diff <= 2147483647LL) {
00388       *(unsigned char*)(RetAddr-0xc) = 0xE9;
00389       *(intptr_t *)(RetAddr-0xb) = diff & 0xffffffff;
00390     } else {
00391       *(intptr_t *)(RetAddr - 0xa) = NewVal;
00392       ((unsigned char*)RetAddr)[0] = (2 | (4 << 3) | (3 << 6));
00393     }
00394 #else
00395     ((unsigned char*)RetAddr)[-1] = 0xE9;
00396 #endif
00397   }
00398 
00399   // Change the return address to reexecute the call instruction...
00400 #if defined (X86_64_JIT)
00401   *RetAddrLoc -= 0xd;
00402 #else
00403   *RetAddrLoc -= 5;
00404 #endif
00405 }
00406 }
00407 
00408 TargetJITInfo::LazyResolverFn
00409 X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
00410   JITCompilerFunction = F;
00411 
00412 #if defined (X86_32_JIT) && !defined (_MSC_VER)
00413   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
00414   union {
00415     unsigned u[3];
00416     char     c[12];
00417   } text;
00418 
00419   if (!X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1)) {
00420     // FIXME: support for AMD family of processors.
00421     if (memcmp(text.c, "GenuineIntel", 12) == 0) {
00422       X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
00423       if ((EDX >> 25) & 0x1)
00424         return X86CompilationCallback_SSE;
00425     }
00426   }
00427 #endif
00428 
00429   return X86CompilationCallback;
00430 }
00431 
00432 void *X86JITInfo::emitGlobalValueIndirectSym(const GlobalValue* GV, void *ptr,
00433                                              JITCodeEmitter &JCE) {
00434 #if defined (X86_64_JIT)
00435   JCE.startGVStub(GV, 8, 8);
00436   JCE.emitWordLE((unsigned)(intptr_t)ptr);
00437   JCE.emitWordLE((unsigned)(((intptr_t)ptr) >> 32));
00438 #else
00439   JCE.startGVStub(GV, 4, 4);
00440   JCE.emitWordLE((intptr_t)ptr);
00441 #endif
00442   return JCE.finishGVStub(GV);
00443 }
00444 
00445 void *X86JITInfo::emitFunctionStub(const Function* F, void *Fn,
00446                                    JITCodeEmitter &JCE) {
00447   // Note, we cast to intptr_t here to silence a -pedantic warning that 
00448   // complains about casting a function pointer to a normal pointer.
00449 #if defined (X86_32_JIT) && !defined (_MSC_VER)
00450   bool NotCC = (Fn != (void*)(intptr_t)X86CompilationCallback &&
00451                 Fn != (void*)(intptr_t)X86CompilationCallback_SSE);
00452 #else
00453   bool NotCC = Fn != (void*)(intptr_t)X86CompilationCallback;
00454 #endif
00455   if (NotCC) {
00456 #if defined (X86_64_JIT)
00457     JCE.startGVStub(F, 13, 4);
00458     JCE.emitByte(0x49);          // REX prefix
00459     JCE.emitByte(0xB8+2);        // movabsq r10
00460     JCE.emitWordLE((unsigned)(intptr_t)Fn);
00461     JCE.emitWordLE((unsigned)(((intptr_t)Fn) >> 32));
00462     JCE.emitByte(0x41);          // REX prefix
00463     JCE.emitByte(0xFF);          // jmpq *r10
00464     JCE.emitByte(2 | (4 << 3) | (3 << 6));
00465 #else
00466     JCE.startGVStub(F, 5, 4);
00467     JCE.emitByte(0xE9);
00468     JCE.emitWordLE((intptr_t)Fn-JCE.getCurrentPCValue()-4);
00469 #endif
00470     return JCE.finishGVStub(F);
00471   }
00472 
00473 #if defined (X86_64_JIT)
00474   JCE.startGVStub(F, 14, 4);
00475   JCE.emitByte(0x49);          // REX prefix
00476   JCE.emitByte(0xB8+2);        // movabsq r10
00477   JCE.emitWordLE((unsigned)(intptr_t)Fn);
00478   JCE.emitWordLE((unsigned)(((intptr_t)Fn) >> 32));
00479   JCE.emitByte(0x41);          // REX prefix
00480   JCE.emitByte(0xFF);          // callq *r10
00481   JCE.emitByte(2 | (2 << 3) | (3 << 6));
00482 #else
00483   JCE.startGVStub(F, 6, 4);
00484   JCE.emitByte(0xE8);   // Call with 32 bit pc-rel destination...
00485 
00486   JCE.emitWordLE((intptr_t)Fn-JCE.getCurrentPCValue()-4);
00487 #endif
00488 
00489   // This used to use 0xCD, but that value is used by JITMemoryManager to
00490   // initialize the buffer with garbage, which means it may follow a
00491   // noreturn function call, confusing X86CompilationCallback2.  PR 4929.
00492   JCE.emitByte(0xCE);   // Interrupt - Just a marker identifying the stub!
00493   return JCE.finishGVStub(F);
00494 }
00495 
00496 void X86JITInfo::emitFunctionStubAtAddr(const Function* F, void *Fn, void *Stub,
00497                                         JITCodeEmitter &JCE) {
00498   // Note, we cast to intptr_t here to silence a -pedantic warning that 
00499   // complains about casting a function pointer to a normal pointer.
00500   JCE.startGVStub(F, Stub, 5);
00501   JCE.emitByte(0xE9);
00502 #if defined (X86_64_JIT) && !defined (NDEBUG)
00503   // Yes, we need both of these casts, or some broken versions of GCC (4.2.4)
00504   // get the signed-ness of the expression wrong.  Go figure.
00505   intptr_t Displacement = (intptr_t)Fn - (intptr_t)JCE.getCurrentPCValue() - 5;
00506   assert(((Displacement << 32) >> 32) == Displacement
00507          && "PIC displacement does not fit in displacement field!");
00508 #endif
00509   JCE.emitWordLE((intptr_t)Fn-JCE.getCurrentPCValue()-4);
00510   JCE.finishGVStub(F);
00511 }
00512 
00513 /// getPICJumpTableEntry - Returns the value of the jumptable entry for the
00514 /// specific basic block.
00515 uintptr_t X86JITInfo::getPICJumpTableEntry(uintptr_t BB, uintptr_t Entry) {
00516 #if defined(X86_64_JIT)
00517   return BB - Entry;
00518 #else
00519   return BB - PICBase;
00520 #endif
00521 }
00522 
00523 /// relocate - Before the JIT can run a block of code that has been emitted,
00524 /// it must rewrite the code to contain the actual addresses of any
00525 /// referenced global symbols.
00526 void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
00527                           unsigned NumRelocs, unsigned char* GOTBase) {
00528   for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
00529     void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
00530     intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
00531     switch ((X86::RelocationType)MR->getRelocationType()) {
00532     case X86::reloc_pcrel_word: {
00533       // PC relative relocation, add the relocated value to the value already in
00534       // memory, after we adjust it for where the PC is.
00535       ResultPtr = ResultPtr -(intptr_t)RelocPos - 4 - MR->getConstantVal();
00536       *((unsigned*)RelocPos) += (unsigned)ResultPtr;
00537       break;
00538     }
00539     case X86::reloc_picrel_word: {
00540       // PIC base relative relocation, add the relocated value to the value
00541       // already in memory, after we adjust it for where the PIC base is.
00542       ResultPtr = ResultPtr - ((intptr_t)Function + MR->getConstantVal());
00543       *((unsigned*)RelocPos) += (unsigned)ResultPtr;
00544       break;
00545     }
00546     case X86::reloc_absolute_word:
00547     case X86::reloc_absolute_word_sext:
00548       // Absolute relocation, just add the relocated value to the value already
00549       // in memory.
00550       *((unsigned*)RelocPos) += (unsigned)ResultPtr;
00551       break;
00552     case X86::reloc_absolute_dword:
00553       *((intptr_t*)RelocPos) += ResultPtr;
00554       break;
00555     }
00556   }
00557 }
00558 
00559 char* X86JITInfo::allocateThreadLocalMemory(size_t size) {
00560 #if defined(X86_32_JIT) && !defined(__APPLE__) && !defined(_MSC_VER)
00561   TLSOffset -= size;
00562   return TLSOffset;
00563 #else
00564   llvm_unreachable("Cannot allocate thread local storage on this arch!");
00565   return 0;
00566 #endif
00567 }