2004-11-21 00:54:33 +01:00
|
|
|
//===-- X86JITInfo.cpp - Implement the JIT interfaces for the X86 target --===//
|
2005-04-22 01:38:14 +02:00
|
|
|
//
|
2004-11-21 00:54:33 +01:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 01:38:14 +02:00
|
|
|
//
|
2004-11-21 00:54:33 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the JIT interfaces for the X86 target.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "jit"
|
|
|
|
#include "X86JITInfo.h"
|
|
|
|
#include "X86Relocations.h"
|
2006-10-16 23:01:55 +02:00
|
|
|
#include "X86Subtarget.h"
|
2009-09-03 06:37:05 +02:00
|
|
|
#include "X86TargetMachine.h"
|
2008-04-16 22:46:05 +02:00
|
|
|
#include "llvm/Function.h"
|
2008-07-16 19:54:34 +02:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-07-08 20:01:40 +02:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Valgrind.h"
|
2005-05-20 23:29:24 +02:00
|
|
|
#include <cstdlib>
|
2008-02-20 12:08:44 +01:00
|
|
|
#include <cstring>
|
2004-11-21 00:54:33 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2008-03-23 14:40:45 +01:00
|
|
|
// Determine the platform we're running on
|
2009-08-28 18:06:41 +02:00
|
|
|
#if defined (__x86_64__) || defined (_M_AMD64) || defined (_M_X64)
|
2008-03-23 14:40:45 +01:00
|
|
|
# define X86_64_JIT
|
|
|
|
#elif defined(__i386__) || defined(i386) || defined(_M_IX86)
|
|
|
|
# define X86_32_JIT
|
2006-01-26 20:55:20 +01:00
|
|
|
#endif
|
|
|
|
|
2004-11-21 00:54:33 +01:00
|
|
|
void X86JITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
|
|
|
|
unsigned char *OldByte = (unsigned char *)Old;
|
|
|
|
*OldByte++ = 0xE9; // Emit JMP opcode.
|
|
|
|
unsigned *OldWord = (unsigned *)OldByte;
|
|
|
|
unsigned NewAddr = (intptr_t)New;
|
|
|
|
unsigned OldAddr = (intptr_t)OldWord;
|
|
|
|
*OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
|
2010-03-15 05:57:55 +01:00
|
|
|
|
|
|
|
// X86 doesn't need to invalidate the processor cache, so just invalidate
|
|
|
|
// Valgrind's cache directly.
|
|
|
|
sys::ValgrindDiscardTranslations(Old, 5);
|
2004-11-21 00:54:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// JITCompilerFunction - This contains the address of the JIT function used to
|
|
|
|
/// compile a function lazily.
|
|
|
|
static TargetJITInfo::JITCompilerFn JITCompilerFunction;
|
|
|
|
|
2006-09-08 19:03:56 +02:00
|
|
|
// Get the ASMPREFIX for the current host. This is often '_'.
|
|
|
|
#ifndef __USER_LABEL_PREFIX__
|
|
|
|
#define __USER_LABEL_PREFIX__
|
|
|
|
#endif
|
|
|
|
#define GETASMPREFIX2(X) #X
|
|
|
|
#define GETASMPREFIX(X) GETASMPREFIX2(X)
|
|
|
|
#define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
|
|
|
|
|
2009-02-06 22:15:52 +01:00
|
|
|
// For ELF targets, use a .size and .type directive, to let tools
|
|
|
|
// know the extent of functions defined in assembler.
|
|
|
|
#if defined(__ELF__)
|
|
|
|
# define SIZE(sym) ".size " #sym ", . - " #sym "\n"
|
|
|
|
# define TYPE_FUNCTION(sym) ".type " #sym ", @function\n"
|
|
|
|
#else
|
|
|
|
# define SIZE(sym)
|
|
|
|
# define TYPE_FUNCTION(sym)
|
|
|
|
#endif
|
|
|
|
|
2007-12-11 00:10:20 +01:00
|
|
|
// Provide a convenient way for disabling usage of CFI directives.
|
2007-12-11 00:08:35 +01:00
|
|
|
// This is needed for old/broken assemblers (for example, gas on
|
|
|
|
// Darwin is pretty old and doesn't support these directives)
|
2007-12-11 00:04:38 +01:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
# define CFI(x)
|
|
|
|
#else
|
2007-12-22 21:41:12 +01:00
|
|
|
// FIXME: Disable this until we really want to use it. Also, we will
|
|
|
|
// need to add some workarounds for compilers, which support
|
|
|
|
// only subset of these directives.
|
2007-12-22 21:46:24 +01:00
|
|
|
# define CFI(x)
|
2007-12-11 00:04:38 +01:00
|
|
|
#endif
|
|
|
|
|
2005-05-19 08:49:17 +02:00
|
|
|
// Provide a wrapper for X86CompilationCallback2 that saves non-traditional
|
|
|
|
// callee saved registers, for the fastcc calling convention.
|
2005-05-20 03:35:39 +02:00
|
|
|
extern "C" {
|
2008-03-23 14:40:45 +01:00
|
|
|
#if defined(X86_64_JIT)
|
|
|
|
# ifndef _MSC_VER
|
2006-09-08 08:48:29 +02:00
|
|
|
// No need to save EAX/EDX for X86-64.
|
|
|
|
void X86CompilationCallback(void);
|
|
|
|
asm(
|
|
|
|
".text\n"
|
|
|
|
".align 8\n"
|
2006-09-08 19:03:56 +02:00
|
|
|
".globl " ASMPREFIX "X86CompilationCallback\n"
|
2009-02-06 22:15:52 +01:00
|
|
|
TYPE_FUNCTION(X86CompilationCallback)
|
2006-09-08 19:03:56 +02:00
|
|
|
ASMPREFIX "X86CompilationCallback:\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_startproc\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
// Save RBP
|
|
|
|
"pushq %rbp\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_def_cfa_offset 16\n")
|
|
|
|
CFI(".cfi_offset %rbp, -16\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
// Save RSP
|
|
|
|
"movq %rsp, %rbp\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_def_cfa_register %rbp\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
// Save all int arg registers
|
|
|
|
"pushq %rdi\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_rel_offset %rdi, 0\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
"pushq %rsi\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_rel_offset %rsi, 8\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
"pushq %rdx\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_rel_offset %rdx, 16\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
"pushq %rcx\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_rel_offset %rcx, 24\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
"pushq %r8\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_rel_offset %r8, 32\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
"pushq %r9\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_rel_offset %r9, 40\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
// Align stack on 16-byte boundary. ESP might not be properly aligned
|
|
|
|
// (8 byte) if this is called from an indirect stub.
|
|
|
|
"andq $-16, %rsp\n"
|
|
|
|
// Save all XMM arg registers
|
|
|
|
"subq $128, %rsp\n"
|
|
|
|
"movaps %xmm0, (%rsp)\n"
|
|
|
|
"movaps %xmm1, 16(%rsp)\n"
|
|
|
|
"movaps %xmm2, 32(%rsp)\n"
|
|
|
|
"movaps %xmm3, 48(%rsp)\n"
|
|
|
|
"movaps %xmm4, 64(%rsp)\n"
|
|
|
|
"movaps %xmm5, 80(%rsp)\n"
|
|
|
|
"movaps %xmm6, 96(%rsp)\n"
|
|
|
|
"movaps %xmm7, 112(%rsp)\n"
|
|
|
|
// JIT callee
|
2011-02-05 16:11:03 +01:00
|
|
|
#ifdef _WIN64
|
|
|
|
"subq $32, %rsp\n"
|
|
|
|
"movq %rbp, %rcx\n" // Pass prev frame and return address
|
|
|
|
"movq 8(%rbp), %rdx\n"
|
|
|
|
"call " ASMPREFIX "X86CompilationCallback2\n"
|
|
|
|
"addq $32, %rsp\n"
|
|
|
|
#else
|
2006-09-08 08:48:29 +02:00
|
|
|
"movq %rbp, %rdi\n" // Pass prev frame and return address
|
|
|
|
"movq 8(%rbp), %rsi\n"
|
2009-09-06 22:21:48 +02:00
|
|
|
"call " ASMPREFIX "X86CompilationCallback2\n"
|
2011-02-05 16:11:03 +01:00
|
|
|
#endif
|
2006-09-08 08:48:29 +02:00
|
|
|
// Restore all XMM arg registers
|
|
|
|
"movaps 112(%rsp), %xmm7\n"
|
|
|
|
"movaps 96(%rsp), %xmm6\n"
|
|
|
|
"movaps 80(%rsp), %xmm5\n"
|
|
|
|
"movaps 64(%rsp), %xmm4\n"
|
|
|
|
"movaps 48(%rsp), %xmm3\n"
|
|
|
|
"movaps 32(%rsp), %xmm2\n"
|
|
|
|
"movaps 16(%rsp), %xmm1\n"
|
|
|
|
"movaps (%rsp), %xmm0\n"
|
|
|
|
// Restore RSP
|
|
|
|
"movq %rbp, %rsp\n"
|
2007-12-12 03:38:28 +01:00
|
|
|
CFI(".cfi_def_cfa_register %rsp\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
// Restore all int arg registers
|
|
|
|
"subq $48, %rsp\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset 48\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
"popq %r9\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset -8\n")
|
|
|
|
CFI(".cfi_restore %r9\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
"popq %r8\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset -8\n")
|
|
|
|
CFI(".cfi_restore %r8\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
"popq %rcx\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset -8\n")
|
|
|
|
CFI(".cfi_restore %rcx\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
"popq %rdx\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset -8\n")
|
|
|
|
CFI(".cfi_restore %rdx\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
"popq %rsi\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset -8\n")
|
|
|
|
CFI(".cfi_restore %rsi\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
"popq %rdi\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset -8\n")
|
|
|
|
CFI(".cfi_restore %rdi\n")
|
2006-09-08 08:48:29 +02:00
|
|
|
// Restore RBP
|
|
|
|
"popq %rbp\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset -8\n")
|
|
|
|
CFI(".cfi_restore %rbp\n")
|
2007-12-10 16:27:07 +01:00
|
|
|
"ret\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_endproc\n")
|
2009-02-06 22:15:52 +01:00
|
|
|
SIZE(X86CompilationCallback)
|
2007-12-11 00:04:38 +01:00
|
|
|
);
|
2008-03-23 14:40:45 +01:00
|
|
|
# else
|
2008-03-23 15:44:32 +01:00
|
|
|
// No inline assembler support on this platform. The routine is in external
|
|
|
|
// file.
|
|
|
|
void X86CompilationCallback();
|
|
|
|
|
2008-03-23 14:40:45 +01:00
|
|
|
# endif
|
|
|
|
#elif defined (X86_32_JIT)
|
|
|
|
# ifndef _MSC_VER
|
2005-05-20 03:35:39 +02:00
|
|
|
void X86CompilationCallback(void);
|
|
|
|
asm(
|
|
|
|
".text\n"
|
|
|
|
".align 8\n"
|
2009-02-06 22:15:52 +01:00
|
|
|
".globl " ASMPREFIX "X86CompilationCallback\n"
|
|
|
|
TYPE_FUNCTION(X86CompilationCallback)
|
2006-09-08 19:03:56 +02:00
|
|
|
ASMPREFIX "X86CompilationCallback:\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_startproc\n")
|
2005-05-20 03:35:39 +02:00
|
|
|
"pushl %ebp\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_def_cfa_offset 8\n")
|
|
|
|
CFI(".cfi_offset %ebp, -8\n")
|
2005-05-20 03:35:39 +02:00
|
|
|
"movl %esp, %ebp\n" // Standard prologue
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_def_cfa_register %ebp\n")
|
2005-05-20 03:35:39 +02:00
|
|
|
"pushl %eax\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_rel_offset %eax, 0\n")
|
2007-01-29 22:28:01 +01:00
|
|
|
"pushl %edx\n" // Save EAX/EDX/ECX
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_rel_offset %edx, 4\n")
|
2007-01-29 22:28:01 +01:00
|
|
|
"pushl %ecx\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_rel_offset %ecx, 8\n")
|
2008-03-23 14:40:45 +01:00
|
|
|
# if defined(__APPLE__)
|
2006-06-24 10:36:10 +02:00
|
|
|
"andl $-16, %esp\n" // Align ESP on 16-byte boundary
|
2008-03-23 14:40:45 +01:00
|
|
|
# endif
|
2006-06-29 03:48:36 +02:00
|
|
|
"subl $16, %esp\n"
|
|
|
|
"movl 4(%ebp), %eax\n" // Pass prev frame and return address
|
|
|
|
"movl %eax, 4(%esp)\n"
|
|
|
|
"movl %ebp, (%esp)\n"
|
2009-09-06 22:21:48 +02:00
|
|
|
"call " ASMPREFIX "X86CompilationCallback2\n"
|
2006-06-24 10:36:10 +02:00
|
|
|
"movl %ebp, %esp\n" // Restore ESP
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_def_cfa_register %esp\n")
|
2007-01-29 22:28:01 +01:00
|
|
|
"subl $12, %esp\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset 12\n")
|
2007-01-29 22:28:01 +01:00
|
|
|
"popl %ecx\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %ecx\n")
|
2005-05-20 03:35:39 +02:00
|
|
|
"popl %edx\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %edx\n")
|
2005-05-20 03:35:39 +02:00
|
|
|
"popl %eax\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %eax\n")
|
2006-10-16 23:01:55 +02:00
|
|
|
"popl %ebp\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %ebp\n")
|
2007-12-10 15:54:42 +01:00
|
|
|
"ret\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_endproc\n")
|
2009-02-06 22:15:52 +01:00
|
|
|
SIZE(X86CompilationCallback)
|
2007-12-11 00:04:38 +01:00
|
|
|
);
|
2006-10-16 23:01:55 +02:00
|
|
|
|
|
|
|
// Same as X86CompilationCallback but also saves XMM argument registers.
|
|
|
|
void X86CompilationCallback_SSE(void);
|
|
|
|
asm(
|
|
|
|
".text\n"
|
|
|
|
".align 8\n"
|
2009-02-06 22:15:52 +01:00
|
|
|
".globl " ASMPREFIX "X86CompilationCallback_SSE\n"
|
|
|
|
TYPE_FUNCTION(X86CompilationCallback_SSE)
|
2006-10-16 23:01:55 +02:00
|
|
|
ASMPREFIX "X86CompilationCallback_SSE:\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_startproc\n")
|
2006-10-16 23:01:55 +02:00
|
|
|
"pushl %ebp\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_def_cfa_offset 8\n")
|
|
|
|
CFI(".cfi_offset %ebp, -8\n")
|
2006-10-16 23:01:55 +02:00
|
|
|
"movl %esp, %ebp\n" // Standard prologue
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_def_cfa_register %ebp\n")
|
2006-10-16 23:01:55 +02:00
|
|
|
"pushl %eax\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_rel_offset %eax, 0\n")
|
2007-01-29 22:28:01 +01:00
|
|
|
"pushl %edx\n" // Save EAX/EDX/ECX
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_rel_offset %edx, 4\n")
|
2007-01-29 22:28:01 +01:00
|
|
|
"pushl %ecx\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_rel_offset %ecx, 8\n")
|
2006-10-16 23:01:55 +02:00
|
|
|
"andl $-16, %esp\n" // Align ESP on 16-byte boundary
|
|
|
|
// Save all XMM arg registers
|
|
|
|
"subl $64, %esp\n"
|
2007-12-10 16:13:55 +01:00
|
|
|
// FIXME: provide frame move information for xmm registers.
|
|
|
|
// This can be tricky, because CFA register is ebp (unaligned)
|
|
|
|
// and we need to produce offsets relative to it.
|
2006-10-16 23:01:55 +02:00
|
|
|
"movaps %xmm0, (%esp)\n"
|
|
|
|
"movaps %xmm1, 16(%esp)\n"
|
|
|
|
"movaps %xmm2, 32(%esp)\n"
|
|
|
|
"movaps %xmm3, 48(%esp)\n"
|
|
|
|
"subl $16, %esp\n"
|
|
|
|
"movl 4(%ebp), %eax\n" // Pass prev frame and return address
|
|
|
|
"movl %eax, 4(%esp)\n"
|
|
|
|
"movl %ebp, (%esp)\n"
|
2009-09-06 22:21:48 +02:00
|
|
|
"call " ASMPREFIX "X86CompilationCallback2\n"
|
2006-10-16 23:01:55 +02:00
|
|
|
"addl $16, %esp\n"
|
|
|
|
"movaps 48(%esp), %xmm3\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_restore %xmm3\n")
|
2006-10-16 23:01:55 +02:00
|
|
|
"movaps 32(%esp), %xmm2\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_restore %xmm2\n")
|
2006-10-16 23:01:55 +02:00
|
|
|
"movaps 16(%esp), %xmm1\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_restore %xmm1\n")
|
2006-10-16 23:01:55 +02:00
|
|
|
"movaps (%esp), %xmm0\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_restore %xmm0\n")
|
2006-10-16 23:01:55 +02:00
|
|
|
"movl %ebp, %esp\n" // Restore ESP
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_def_cfa_register esp\n")
|
2007-01-29 22:28:01 +01:00
|
|
|
"subl $12, %esp\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset 12\n")
|
2007-01-29 22:28:01 +01:00
|
|
|
"popl %ecx\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %ecx\n")
|
2006-10-16 23:01:55 +02:00
|
|
|
"popl %edx\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %edx\n")
|
2006-10-16 23:01:55 +02:00
|
|
|
"popl %eax\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %eax\n")
|
2005-05-20 03:35:39 +02:00
|
|
|
"popl %ebp\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %ebp\n")
|
2007-12-10 16:13:55 +01:00
|
|
|
"ret\n"
|
2007-12-11 00:04:38 +01:00
|
|
|
CFI(".cfi_endproc\n")
|
2009-02-06 22:15:52 +01:00
|
|
|
SIZE(X86CompilationCallback_SSE)
|
2007-12-11 00:04:38 +01:00
|
|
|
);
|
2008-03-23 14:40:45 +01:00
|
|
|
# else
|
2008-03-23 13:32:54 +01:00
|
|
|
void X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr);
|
2005-05-20 03:35:39 +02:00
|
|
|
|
|
|
|
_declspec(naked) void X86CompilationCallback(void) {
|
|
|
|
__asm {
|
2008-03-23 13:32:54 +01:00
|
|
|
push ebp
|
|
|
|
mov ebp, esp
|
2005-05-20 03:35:39 +02:00
|
|
|
push eax
|
|
|
|
push edx
|
2007-01-29 22:28:01 +01:00
|
|
|
push ecx
|
2008-03-23 13:32:54 +01:00
|
|
|
and esp, -16
|
2010-02-08 09:37:27 +01:00
|
|
|
sub esp, 16
|
2008-03-23 13:32:54 +01:00
|
|
|
mov eax, dword ptr [ebp+4]
|
|
|
|
mov dword ptr [esp+4], eax
|
|
|
|
mov dword ptr [esp], ebp
|
2005-05-20 03:35:39 +02:00
|
|
|
call X86CompilationCallback2
|
2008-03-23 13:32:54 +01:00
|
|
|
mov esp, ebp
|
|
|
|
sub esp, 12
|
2007-01-29 22:28:01 +01:00
|
|
|
pop ecx
|
2005-05-20 03:35:39 +02:00
|
|
|
pop edx
|
|
|
|
pop eax
|
2008-03-23 13:32:54 +01:00
|
|
|
pop ebp
|
2005-05-20 03:35:39 +02:00
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
2008-03-23 13:32:54 +01:00
|
|
|
|
2008-03-23 14:40:45 +01:00
|
|
|
# endif // _MSC_VER
|
2005-05-19 08:49:17 +02:00
|
|
|
|
2005-05-20 21:46:50 +02:00
|
|
|
#else // Not an i386 host
|
2005-05-20 03:35:39 +02:00
|
|
|
void X86CompilationCallback() {
|
2009-07-14 18:55:14 +02:00
|
|
|
llvm_unreachable("Cannot call X86CompilationCallback() on a non-x86 arch!");
|
2005-05-20 03:35:39 +02:00
|
|
|
}
|
2005-05-19 08:49:17 +02:00
|
|
|
#endif
|
2005-05-20 03:35:39 +02:00
|
|
|
}
|
2005-05-19 08:49:17 +02:00
|
|
|
|
2009-02-06 22:15:52 +01:00
|
|
|
/// X86CompilationCallback2 - This is the target-specific function invoked by the
|
2004-11-21 00:54:33 +01:00
|
|
|
/// function stub when we did not know the real target of a call. This function
|
|
|
|
/// must locate the start of the stub or call site and pass it into the JIT
|
|
|
|
/// compiler function.
|
2009-09-06 22:21:48 +02:00
|
|
|
extern "C" {
|
|
|
|
#if !(defined (X86_64_JIT) && defined(_MSC_VER))
|
|
|
|
// the following function is called only from this translation unit,
|
2011-02-05 16:10:54 +01:00
|
|
|
// unless we are under 64bit Windows with MSC, where there is
|
2009-09-06 22:21:48 +02:00
|
|
|
// no support for inline assembly
|
|
|
|
static
|
|
|
|
#endif
|
2010-10-23 10:10:43 +02:00
|
|
|
void LLVM_ATTRIBUTE_USED
|
2008-07-16 19:54:34 +02:00
|
|
|
X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr) {
|
2006-06-29 03:48:36 +02:00
|
|
|
intptr_t *RetAddrLoc = &StackPtr[1];
|
2005-05-20 03:35:39 +02:00
|
|
|
assert(*RetAddrLoc == RetAddr &&
|
2004-11-21 00:54:33 +01:00
|
|
|
"Could not find return address on the stack!");
|
|
|
|
|
|
|
|
// It's a stub if there is an interrupt marker after the call.
|
2009-09-15 20:32:14 +02:00
|
|
|
bool isStub = ((unsigned char*)RetAddr)[0] == 0xCE;
|
2004-11-21 00:54:33 +01:00
|
|
|
|
|
|
|
// The call instruction should have pushed the return value onto the stack...
|
2008-03-23 14:40:45 +01:00
|
|
|
#if defined (X86_64_JIT)
|
2007-03-14 11:44:30 +01:00
|
|
|
RetAddr--; // Backtrack to the reference itself...
|
|
|
|
#else
|
2004-11-21 00:54:33 +01:00
|
|
|
RetAddr -= 4; // Backtrack to the reference itself...
|
2007-03-14 11:44:30 +01:00
|
|
|
#endif
|
2004-11-21 00:54:33 +01:00
|
|
|
|
|
|
|
#if 0
|
2010-01-05 02:29:23 +01:00
|
|
|
DEBUG(dbgs() << "In callback! Addr=" << (void*)RetAddr
|
2009-08-03 02:11:34 +02:00
|
|
|
<< " ESP=" << (void*)StackPtr
|
|
|
|
<< ": Resolving call to function: "
|
|
|
|
<< TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
|
2004-11-21 00:54:33 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Sanity check to make sure this really is a call instruction.
|
2008-03-23 14:40:45 +01:00
|
|
|
#if defined (X86_64_JIT)
|
2007-03-14 11:44:30 +01:00
|
|
|
assert(((unsigned char*)RetAddr)[-2] == 0x41 &&"Not a call instr!");
|
|
|
|
assert(((unsigned char*)RetAddr)[-1] == 0xFF &&"Not a call instr!");
|
|
|
|
#else
|
2006-09-08 08:48:29 +02:00
|
|
|
assert(((unsigned char*)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
|
2007-03-14 11:44:30 +01:00
|
|
|
#endif
|
2005-04-22 01:38:14 +02:00
|
|
|
|
2006-09-08 08:48:29 +02:00
|
|
|
intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
|
2004-11-21 00:54:33 +01:00
|
|
|
|
|
|
|
// Rewrite the call target... so that we don't end up here every time we
|
|
|
|
// execute the call.
|
2008-03-23 14:40:45 +01:00
|
|
|
#if defined (X86_64_JIT)
|
2009-11-16 23:41:33 +01:00
|
|
|
assert(isStub &&
|
|
|
|
"X86-64 doesn't support rewriting non-stub lazy compilation calls:"
|
|
|
|
" the call instruction varies too much.");
|
2007-03-14 11:44:30 +01:00
|
|
|
#else
|
|
|
|
*(intptr_t *)RetAddr = (intptr_t)(NewVal-RetAddr-4);
|
|
|
|
#endif
|
2004-11-21 00:54:33 +01:00
|
|
|
|
|
|
|
if (isStub) {
|
|
|
|
// If this is a stub, rewrite the call into an unconditional branch
|
|
|
|
// instruction so that two return addresses are not pushed onto the stack
|
|
|
|
// when the requested function finally gets called. This also makes the
|
2009-09-15 20:32:14 +02:00
|
|
|
// 0xCE byte (interrupt) dead, so the marker doesn't effect anything.
|
2008-03-23 14:40:45 +01:00
|
|
|
#if defined (X86_64_JIT)
|
2008-08-13 01:20:24 +02:00
|
|
|
// If the target address is within 32-bit range of the stub, use a
|
|
|
|
// PC-relative branch instead of loading the actual address. (This is
|
|
|
|
// considerably shorter than the 64-bit immediate load already there.)
|
|
|
|
// We assume here intptr_t is 64 bits.
|
|
|
|
intptr_t diff = NewVal-RetAddr+7;
|
|
|
|
if (diff >= -2147483648LL && diff <= 2147483647LL) {
|
|
|
|
*(unsigned char*)(RetAddr-0xc) = 0xE9;
|
|
|
|
*(intptr_t *)(RetAddr-0xb) = diff & 0xffffffff;
|
|
|
|
} else {
|
|
|
|
*(intptr_t *)(RetAddr - 0xa) = NewVal;
|
|
|
|
((unsigned char*)RetAddr)[0] = (2 | (4 << 3) | (3 << 6));
|
|
|
|
}
|
2010-03-15 05:57:55 +01:00
|
|
|
sys::ValgrindDiscardTranslations((void*)(RetAddr-0xc), 0xd);
|
2007-03-14 11:44:30 +01:00
|
|
|
#else
|
2006-09-08 08:48:29 +02:00
|
|
|
((unsigned char*)RetAddr)[-1] = 0xE9;
|
2010-03-15 05:57:55 +01:00
|
|
|
sys::ValgrindDiscardTranslations((void*)(RetAddr-1), 5);
|
2007-03-14 11:44:30 +01:00
|
|
|
#endif
|
2004-11-21 00:54:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Change the return address to reexecute the call instruction...
|
2008-03-23 14:40:45 +01:00
|
|
|
#if defined (X86_64_JIT)
|
2007-03-14 11:44:30 +01:00
|
|
|
*RetAddrLoc -= 0xd;
|
|
|
|
#else
|
2005-05-20 03:35:39 +02:00
|
|
|
*RetAddrLoc -= 5;
|
2007-03-14 11:44:30 +01:00
|
|
|
#endif
|
2004-11-21 00:54:33 +01:00
|
|
|
}
|
2009-09-06 22:21:48 +02:00
|
|
|
}
|
2004-11-21 00:54:33 +01:00
|
|
|
|
|
|
|
TargetJITInfo::LazyResolverFn
|
|
|
|
X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
|
|
|
|
JITCompilerFunction = F;
|
2006-10-16 23:01:55 +02:00
|
|
|
|
2009-09-03 07:01:00 +02:00
|
|
|
#if defined (X86_32_JIT) && !defined (_MSC_VER)
|
|
|
|
if (Subtarget->hasSSE1())
|
|
|
|
return X86CompilationCallback_SSE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return X86CompilationCallback;
|
2009-09-03 06:37:05 +02:00
|
|
|
}
|
2006-10-16 23:01:55 +02:00
|
|
|
|
2009-09-03 06:37:05 +02:00
|
|
|
X86JITInfo::X86JITInfo(X86TargetMachine &tm) : TM(tm) {
|
|
|
|
Subtarget = &TM.getSubtarget<X86Subtarget>();
|
|
|
|
useGOT = 0;
|
|
|
|
TLSOffset = 0;
|
2004-11-21 00:54:33 +01:00
|
|
|
}
|
|
|
|
|
2008-11-10 02:08:07 +01:00
|
|
|
void *X86JITInfo::emitGlobalValueIndirectSym(const GlobalValue* GV, void *ptr,
|
2009-05-30 22:51:52 +02:00
|
|
|
JITCodeEmitter &JCE) {
|
2008-03-23 14:40:45 +01:00
|
|
|
#if defined (X86_64_JIT)
|
2009-12-15 23:42:46 +01:00
|
|
|
const unsigned Alignment = 8;
|
|
|
|
uint8_t Buffer[8];
|
|
|
|
uint8_t *Cur = Buffer;
|
|
|
|
MachineCodeEmitter::emitWordLEInto(Cur, (unsigned)(intptr_t)ptr);
|
|
|
|
MachineCodeEmitter::emitWordLEInto(Cur, (unsigned)(((intptr_t)ptr) >> 32));
|
2008-01-04 11:46:51 +01:00
|
|
|
#else
|
2009-12-15 23:42:46 +01:00
|
|
|
const unsigned Alignment = 4;
|
|
|
|
uint8_t Buffer[4];
|
|
|
|
uint8_t *Cur = Buffer;
|
|
|
|
MachineCodeEmitter::emitWordLEInto(Cur, (intptr_t)ptr);
|
2008-01-04 11:46:51 +01:00
|
|
|
#endif
|
2009-12-15 23:42:46 +01:00
|
|
|
return JCE.allocIndirectGV(GV, Buffer, sizeof(Buffer), Alignment);
|
2008-01-04 11:46:51 +01:00
|
|
|
}
|
|
|
|
|
2009-11-24 00:35:19 +01:00
|
|
|
TargetJITInfo::StubLayout X86JITInfo::getStubLayout() {
|
|
|
|
// The 64-bit stub contains:
|
|
|
|
// movabs r10 <- 8-byte-target-address # 10 bytes
|
|
|
|
// call|jmp *r10 # 3 bytes
|
|
|
|
// The 32-bit stub contains a 5-byte call|jmp.
|
|
|
|
// If the stub is a call to the compilation callback, an extra byte is added
|
|
|
|
// to mark it as a stub.
|
|
|
|
StubLayout Result = {14, 4};
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *X86JITInfo::emitFunctionStub(const Function* F, void *Target,
|
2009-05-30 22:51:52 +02:00
|
|
|
JITCodeEmitter &JCE) {
|
2011-02-05 16:10:54 +01:00
|
|
|
// Note, we cast to intptr_t here to silence a -pedantic warning that
|
2006-06-01 19:13:10 +02:00
|
|
|
// complains about casting a function pointer to a normal pointer.
|
2008-03-23 14:40:45 +01:00
|
|
|
#if defined (X86_32_JIT) && !defined (_MSC_VER)
|
2009-11-24 00:35:19 +01:00
|
|
|
bool NotCC = (Target != (void*)(intptr_t)X86CompilationCallback &&
|
|
|
|
Target != (void*)(intptr_t)X86CompilationCallback_SSE);
|
2006-10-17 01:44:08 +02:00
|
|
|
#else
|
2009-11-24 00:35:19 +01:00
|
|
|
bool NotCC = Target != (void*)(intptr_t)X86CompilationCallback;
|
2006-10-17 00:53:28 +02:00
|
|
|
#endif
|
2009-11-24 00:35:19 +01:00
|
|
|
JCE.emitAlignment(4);
|
|
|
|
void *Result = (void*)JCE.getCurrentPCValue();
|
2006-10-17 00:53:28 +02:00
|
|
|
if (NotCC) {
|
2008-03-23 14:40:45 +01:00
|
|
|
#if defined (X86_64_JIT)
|
2009-05-30 22:51:52 +02:00
|
|
|
JCE.emitByte(0x49); // REX prefix
|
|
|
|
JCE.emitByte(0xB8+2); // movabsq r10
|
2009-11-24 00:35:19 +01:00
|
|
|
JCE.emitWordLE((unsigned)(intptr_t)Target);
|
|
|
|
JCE.emitWordLE((unsigned)(((intptr_t)Target) >> 32));
|
2009-05-30 22:51:52 +02:00
|
|
|
JCE.emitByte(0x41); // REX prefix
|
|
|
|
JCE.emitByte(0xFF); // jmpq *r10
|
|
|
|
JCE.emitByte(2 | (4 << 3) | (3 << 6));
|
2007-03-14 11:48:08 +01:00
|
|
|
#else
|
2009-05-30 22:51:52 +02:00
|
|
|
JCE.emitByte(0xE9);
|
2009-11-24 00:35:19 +01:00
|
|
|
JCE.emitWordLE((intptr_t)Target-JCE.getCurrentPCValue()-4);
|
2007-03-14 11:48:08 +01:00
|
|
|
#endif
|
2009-11-24 00:35:19 +01:00
|
|
|
return Result;
|
2004-11-22 23:25:30 +01:00
|
|
|
}
|
2005-04-22 01:38:14 +02:00
|
|
|
|
2008-03-23 14:40:45 +01:00
|
|
|
#if defined (X86_64_JIT)
|
2009-05-30 22:51:52 +02:00
|
|
|
JCE.emitByte(0x49); // REX prefix
|
|
|
|
JCE.emitByte(0xB8+2); // movabsq r10
|
2009-11-24 00:35:19 +01:00
|
|
|
JCE.emitWordLE((unsigned)(intptr_t)Target);
|
|
|
|
JCE.emitWordLE((unsigned)(((intptr_t)Target) >> 32));
|
2009-05-30 22:51:52 +02:00
|
|
|
JCE.emitByte(0x41); // REX prefix
|
|
|
|
JCE.emitByte(0xFF); // callq *r10
|
|
|
|
JCE.emitByte(2 | (2 << 3) | (3 << 6));
|
2007-03-14 11:44:30 +01:00
|
|
|
#else
|
2009-05-30 22:51:52 +02:00
|
|
|
JCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
|
2004-11-22 23:25:30 +01:00
|
|
|
|
2009-11-24 00:35:19 +01:00
|
|
|
JCE.emitWordLE((intptr_t)Target-JCE.getCurrentPCValue()-4);
|
2007-03-14 11:44:30 +01:00
|
|
|
#endif
|
2004-11-22 23:25:30 +01:00
|
|
|
|
2009-09-15 20:32:14 +02:00
|
|
|
// This used to use 0xCD, but that value is used by JITMemoryManager to
|
|
|
|
// initialize the buffer with garbage, which means it may follow a
|
|
|
|
// noreturn function call, confusing X86CompilationCallback2. PR 4929.
|
|
|
|
JCE.emitByte(0xCE); // Interrupt - Just a marker identifying the stub!
|
2009-11-24 00:35:19 +01:00
|
|
|
return Result;
|
2004-11-22 23:25:30 +01:00
|
|
|
}
|
2004-11-21 00:54:33 +01:00
|
|
|
|
2008-01-05 03:26:58 +01:00
|
|
|
/// getPICJumpTableEntry - Returns the value of the jumptable entry for the
|
|
|
|
/// specific basic block.
|
2008-12-10 03:32:19 +01:00
|
|
|
uintptr_t X86JITInfo::getPICJumpTableEntry(uintptr_t BB, uintptr_t Entry) {
|
2008-07-16 03:33:08 +02:00
|
|
|
#if defined(X86_64_JIT)
|
|
|
|
return BB - Entry;
|
|
|
|
#else
|
2008-01-05 03:26:58 +01:00
|
|
|
return BB - PICBase;
|
2008-07-16 03:33:08 +02:00
|
|
|
#endif
|
2008-01-05 03:26:58 +01:00
|
|
|
}
|
|
|
|
|
2004-11-21 00:54:33 +01:00
|
|
|
/// relocate - Before the JIT can run a block of code that has been emitted,
|
|
|
|
/// it must rewrite the code to contain the actual addresses of any
|
|
|
|
/// referenced global symbols.
|
|
|
|
void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
|
2005-07-22 22:49:37 +02:00
|
|
|
unsigned NumRelocs, unsigned char* GOTBase) {
|
2004-11-21 00:54:33 +01:00
|
|
|
for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
|
|
|
|
void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
|
|
|
|
intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
|
|
|
|
switch ((X86::RelocationType)MR->getRelocationType()) {
|
2006-09-08 08:48:29 +02:00
|
|
|
case X86::reloc_pcrel_word: {
|
2004-11-21 00:54:33 +01:00
|
|
|
// PC relative relocation, add the relocated value to the value already in
|
|
|
|
// memory, after we adjust it for where the PC is.
|
2007-12-22 10:40:20 +01:00
|
|
|
ResultPtr = ResultPtr -(intptr_t)RelocPos - 4 - MR->getConstantVal();
|
|
|
|
*((unsigned*)RelocPos) += (unsigned)ResultPtr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case X86::reloc_picrel_word: {
|
|
|
|
// PIC base relative relocation, add the relocated value to the value
|
|
|
|
// already in memory, after we adjust it for where the PIC base is.
|
|
|
|
ResultPtr = ResultPtr - ((intptr_t)Function + MR->getConstantVal());
|
2006-09-08 08:48:29 +02:00
|
|
|
*((unsigned*)RelocPos) += (unsigned)ResultPtr;
|
2004-11-21 00:54:33 +01:00
|
|
|
break;
|
2006-09-08 08:48:29 +02:00
|
|
|
}
|
2004-11-21 00:54:33 +01:00
|
|
|
case X86::reloc_absolute_word:
|
2009-08-05 02:11:21 +02:00
|
|
|
case X86::reloc_absolute_word_sext:
|
2004-11-21 00:54:33 +01:00
|
|
|
// Absolute relocation, just add the relocated value to the value already
|
|
|
|
// in memory.
|
2006-09-08 08:48:29 +02:00
|
|
|
*((unsigned*)RelocPos) += (unsigned)ResultPtr;
|
2004-11-21 00:54:33 +01:00
|
|
|
break;
|
2006-12-05 05:01:03 +01:00
|
|
|
case X86::reloc_absolute_dword:
|
|
|
|
*((intptr_t*)RelocPos) += ResultPtr;
|
|
|
|
break;
|
2004-11-21 00:54:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-25 17:41:43 +02:00
|
|
|
|
|
|
|
char* X86JITInfo::allocateThreadLocalMemory(size_t size) {
|
|
|
|
#if defined(X86_32_JIT) && !defined(__APPLE__) && !defined(_MSC_VER)
|
|
|
|
TLSOffset -= size;
|
|
|
|
return TLSOffset;
|
|
|
|
#else
|
2009-07-14 18:55:14 +02:00
|
|
|
llvm_unreachable("Cannot allocate thread local storage on this arch!");
|
2008-10-25 17:41:43 +02:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|