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