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