2004-11-20 23:54:33 +00:00
|
|
|
//===-- X86JITInfo.cpp - Implement the JIT interfaces for the X86 target --===//
|
2005-04-21 23:38:14 +00:00
|
|
|
//
|
2004-11-20 23:54:33 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:38:14 +00:00
|
|
|
//
|
2004-11-20 23:54:33 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the JIT interfaces for the X86 target.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "jit"
|
|
|
|
#include "X86JITInfo.h"
|
|
|
|
#include "X86Relocations.h"
|
2006-10-16 21:01:55 +00:00
|
|
|
#include "X86Subtarget.h"
|
2008-04-16 20:46:05 +00:00
|
|
|
#include "llvm/Function.h"
|
2004-11-20 23:54:33 +00:00
|
|
|
#include "llvm/Config/alloca.h"
|
2008-07-16 17:54:34 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-07-08 18:01:40 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2005-05-20 21:29:24 +00:00
|
|
|
#include <cstdlib>
|
2008-02-20 11:08:44 +00:00
|
|
|
#include <cstring>
|
2004-11-20 23:54:33 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2008-03-23 13:40:45 +00:00
|
|
|
// Determine the platform we're running on
|
|
|
|
#if defined (__x86_64__) || defined (_M_AMD64)
|
|
|
|
# define X86_64_JIT
|
|
|
|
#elif defined(__i386__) || defined(i386) || defined(_M_IX86)
|
|
|
|
# define X86_32_JIT
|
2006-01-26 19:55:20 +00:00
|
|
|
#endif
|
|
|
|
|
2004-11-20 23:54:33 +00: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.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// JITCompilerFunction - This contains the address of the JIT function used to
|
|
|
|
/// compile a function lazily.
|
|
|
|
static TargetJITInfo::JITCompilerFn JITCompilerFunction;
|
|
|
|
|
2006-09-08 17:03:56 +00: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__)
|
|
|
|
|
2008-06-24 00:50:01 +00:00
|
|
|
// Check if building with -fPIC
|
|
|
|
#if defined(__PIC__) && __PIC__ && defined(__linux__)
|
|
|
|
#define ASMCALLSUFFIX "@PLT"
|
|
|
|
#else
|
|
|
|
#define ASMCALLSUFFIX
|
|
|
|
#endif
|
|
|
|
|
2009-02-06 21:15:52 +00: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-10 23:10:20 +00:00
|
|
|
// Provide a convenient way for disabling usage of CFI directives.
|
2007-12-10 23:08:35 +00:00
|
|
|
// This is needed for old/broken assemblers (for example, gas on
|
|
|
|
// Darwin is pretty old and doesn't support these directives)
|
2007-12-10 23:04:38 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
# define CFI(x)
|
|
|
|
#else
|
2007-12-22 20:41:12 +00: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 20:46:24 +00:00
|
|
|
# define CFI(x)
|
2007-12-10 23:04:38 +00:00
|
|
|
#endif
|
|
|
|
|
2005-05-19 06:49:17 +00:00
|
|
|
// Provide a wrapper for X86CompilationCallback2 that saves non-traditional
|
|
|
|
// callee saved registers, for the fastcc calling convention.
|
2005-05-20 01:35:39 +00:00
|
|
|
extern "C" {
|
2008-03-23 13:40:45 +00:00
|
|
|
#if defined(X86_64_JIT)
|
|
|
|
# ifndef _MSC_VER
|
2006-09-08 06:48:29 +00:00
|
|
|
// No need to save EAX/EDX for X86-64.
|
|
|
|
void X86CompilationCallback(void);
|
|
|
|
asm(
|
|
|
|
".text\n"
|
|
|
|
".align 8\n"
|
2006-09-08 17:03:56 +00:00
|
|
|
".globl " ASMPREFIX "X86CompilationCallback\n"
|
2009-02-06 21:15:52 +00:00
|
|
|
TYPE_FUNCTION(X86CompilationCallback)
|
2006-09-08 17:03:56 +00:00
|
|
|
ASMPREFIX "X86CompilationCallback:\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_startproc\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
// Save RBP
|
|
|
|
"pushq %rbp\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_def_cfa_offset 16\n")
|
|
|
|
CFI(".cfi_offset %rbp, -16\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
// Save RSP
|
|
|
|
"movq %rsp, %rbp\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_def_cfa_register %rbp\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
// Save all int arg registers
|
|
|
|
"pushq %rdi\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_rel_offset %rdi, 0\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
"pushq %rsi\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_rel_offset %rsi, 8\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
"pushq %rdx\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_rel_offset %rdx, 16\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
"pushq %rcx\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_rel_offset %rcx, 24\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
"pushq %r8\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_rel_offset %r8, 32\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
"pushq %r9\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_rel_offset %r9, 40\n")
|
2006-09-08 06:48:29 +00: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
|
|
|
|
"movq %rbp, %rdi\n" // Pass prev frame and return address
|
|
|
|
"movq 8(%rbp), %rsi\n"
|
2008-06-24 00:50:01 +00:00
|
|
|
"call " ASMPREFIX "X86CompilationCallback2" ASMCALLSUFFIX "\n"
|
2006-09-08 06:48:29 +00: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 02:38:28 +00:00
|
|
|
CFI(".cfi_def_cfa_register %rsp\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
// Restore all int arg registers
|
|
|
|
"subq $48, %rsp\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset 48\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
"popq %r9\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset -8\n")
|
|
|
|
CFI(".cfi_restore %r9\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
"popq %r8\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset -8\n")
|
|
|
|
CFI(".cfi_restore %r8\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
"popq %rcx\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset -8\n")
|
|
|
|
CFI(".cfi_restore %rcx\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
"popq %rdx\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset -8\n")
|
|
|
|
CFI(".cfi_restore %rdx\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
"popq %rsi\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset -8\n")
|
|
|
|
CFI(".cfi_restore %rsi\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
"popq %rdi\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset -8\n")
|
|
|
|
CFI(".cfi_restore %rdi\n")
|
2006-09-08 06:48:29 +00:00
|
|
|
// Restore RBP
|
|
|
|
"popq %rbp\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset -8\n")
|
|
|
|
CFI(".cfi_restore %rbp\n")
|
2007-12-10 15:27:07 +00:00
|
|
|
"ret\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_endproc\n")
|
2009-02-06 21:15:52 +00:00
|
|
|
SIZE(X86CompilationCallback)
|
2007-12-10 23:04:38 +00:00
|
|
|
);
|
2008-03-23 13:40:45 +00:00
|
|
|
# else
|
2008-03-23 14:44:32 +00:00
|
|
|
// No inline assembler support on this platform. The routine is in external
|
|
|
|
// file.
|
|
|
|
void X86CompilationCallback();
|
|
|
|
|
2008-03-23 13:40:45 +00:00
|
|
|
# endif
|
|
|
|
#elif defined (X86_32_JIT)
|
|
|
|
# ifndef _MSC_VER
|
2005-05-20 01:35:39 +00:00
|
|
|
void X86CompilationCallback(void);
|
|
|
|
asm(
|
|
|
|
".text\n"
|
|
|
|
".align 8\n"
|
2009-02-06 21:15:52 +00:00
|
|
|
".globl " ASMPREFIX "X86CompilationCallback\n"
|
|
|
|
TYPE_FUNCTION(X86CompilationCallback)
|
2006-09-08 17:03:56 +00:00
|
|
|
ASMPREFIX "X86CompilationCallback:\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_startproc\n")
|
2005-05-20 01:35:39 +00:00
|
|
|
"pushl %ebp\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_def_cfa_offset 8\n")
|
|
|
|
CFI(".cfi_offset %ebp, -8\n")
|
2005-05-20 01:35:39 +00:00
|
|
|
"movl %esp, %ebp\n" // Standard prologue
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_def_cfa_register %ebp\n")
|
2005-05-20 01:35:39 +00:00
|
|
|
"pushl %eax\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_rel_offset %eax, 0\n")
|
2007-01-29 21:28:01 +00:00
|
|
|
"pushl %edx\n" // Save EAX/EDX/ECX
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_rel_offset %edx, 4\n")
|
2007-01-29 21:28:01 +00:00
|
|
|
"pushl %ecx\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_rel_offset %ecx, 8\n")
|
2008-03-23 13:40:45 +00:00
|
|
|
# if defined(__APPLE__)
|
2006-06-24 08:36:10 +00:00
|
|
|
"andl $-16, %esp\n" // Align ESP on 16-byte boundary
|
2008-03-23 13:40:45 +00:00
|
|
|
# endif
|
2006-06-29 01:48:36 +00: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"
|
2008-06-24 00:50:01 +00:00
|
|
|
"call " ASMPREFIX "X86CompilationCallback2" ASMCALLSUFFIX "\n"
|
2006-06-24 08:36:10 +00:00
|
|
|
"movl %ebp, %esp\n" // Restore ESP
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_def_cfa_register %esp\n")
|
2007-01-29 21:28:01 +00:00
|
|
|
"subl $12, %esp\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset 12\n")
|
2007-01-29 21:28:01 +00:00
|
|
|
"popl %ecx\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %ecx\n")
|
2005-05-20 01:35:39 +00:00
|
|
|
"popl %edx\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %edx\n")
|
2005-05-20 01:35:39 +00:00
|
|
|
"popl %eax\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %eax\n")
|
2006-10-16 21:01:55 +00:00
|
|
|
"popl %ebp\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %ebp\n")
|
2007-12-10 14:54:42 +00:00
|
|
|
"ret\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_endproc\n")
|
2009-02-06 21:15:52 +00:00
|
|
|
SIZE(X86CompilationCallback)
|
2007-12-10 23:04:38 +00:00
|
|
|
);
|
2006-10-16 21:01:55 +00:00
|
|
|
|
|
|
|
// Same as X86CompilationCallback but also saves XMM argument registers.
|
|
|
|
void X86CompilationCallback_SSE(void);
|
|
|
|
asm(
|
|
|
|
".text\n"
|
|
|
|
".align 8\n"
|
2009-02-06 21:15:52 +00:00
|
|
|
".globl " ASMPREFIX "X86CompilationCallback_SSE\n"
|
|
|
|
TYPE_FUNCTION(X86CompilationCallback_SSE)
|
2006-10-16 21:01:55 +00:00
|
|
|
ASMPREFIX "X86CompilationCallback_SSE:\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_startproc\n")
|
2006-10-16 21:01:55 +00:00
|
|
|
"pushl %ebp\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_def_cfa_offset 8\n")
|
|
|
|
CFI(".cfi_offset %ebp, -8\n")
|
2006-10-16 21:01:55 +00:00
|
|
|
"movl %esp, %ebp\n" // Standard prologue
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_def_cfa_register %ebp\n")
|
2006-10-16 21:01:55 +00:00
|
|
|
"pushl %eax\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_rel_offset %eax, 0\n")
|
2007-01-29 21:28:01 +00:00
|
|
|
"pushl %edx\n" // Save EAX/EDX/ECX
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_rel_offset %edx, 4\n")
|
2007-01-29 21:28:01 +00:00
|
|
|
"pushl %ecx\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_rel_offset %ecx, 8\n")
|
2006-10-16 21:01:55 +00:00
|
|
|
"andl $-16, %esp\n" // Align ESP on 16-byte boundary
|
|
|
|
// Save all XMM arg registers
|
|
|
|
"subl $64, %esp\n"
|
2007-12-10 15:13:55 +00: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 21:01:55 +00: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"
|
2008-06-24 00:50:01 +00:00
|
|
|
"call " ASMPREFIX "X86CompilationCallback2" ASMCALLSUFFIX "\n"
|
2006-10-16 21:01:55 +00:00
|
|
|
"addl $16, %esp\n"
|
|
|
|
"movaps 48(%esp), %xmm3\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_restore %xmm3\n")
|
2006-10-16 21:01:55 +00:00
|
|
|
"movaps 32(%esp), %xmm2\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_restore %xmm2\n")
|
2006-10-16 21:01:55 +00:00
|
|
|
"movaps 16(%esp), %xmm1\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_restore %xmm1\n")
|
2006-10-16 21:01:55 +00:00
|
|
|
"movaps (%esp), %xmm0\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_restore %xmm0\n")
|
2006-10-16 21:01:55 +00:00
|
|
|
"movl %ebp, %esp\n" // Restore ESP
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_def_cfa_register esp\n")
|
2007-01-29 21:28:01 +00:00
|
|
|
"subl $12, %esp\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset 12\n")
|
2007-01-29 21:28:01 +00:00
|
|
|
"popl %ecx\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %ecx\n")
|
2006-10-16 21:01:55 +00:00
|
|
|
"popl %edx\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %edx\n")
|
2006-10-16 21:01:55 +00:00
|
|
|
"popl %eax\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %eax\n")
|
2005-05-20 01:35:39 +00:00
|
|
|
"popl %ebp\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_adjust_cfa_offset -4\n")
|
|
|
|
CFI(".cfi_restore %ebp\n")
|
2007-12-10 15:13:55 +00:00
|
|
|
"ret\n"
|
2007-12-10 23:04:38 +00:00
|
|
|
CFI(".cfi_endproc\n")
|
2009-02-06 21:15:52 +00:00
|
|
|
SIZE(X86CompilationCallback_SSE)
|
2007-12-10 23:04:38 +00:00
|
|
|
);
|
2008-03-23 13:40:45 +00:00
|
|
|
# else
|
2008-03-23 12:32:54 +00:00
|
|
|
void X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr);
|
2005-05-20 01:35:39 +00:00
|
|
|
|
|
|
|
_declspec(naked) void X86CompilationCallback(void) {
|
|
|
|
__asm {
|
2008-03-23 12:32:54 +00:00
|
|
|
push ebp
|
|
|
|
mov ebp, esp
|
2005-05-20 01:35:39 +00:00
|
|
|
push eax
|
|
|
|
push edx
|
2007-01-29 21:28:01 +00:00
|
|
|
push ecx
|
2008-03-23 12:32:54 +00:00
|
|
|
and esp, -16
|
|
|
|
mov eax, dword ptr [ebp+4]
|
|
|
|
mov dword ptr [esp+4], eax
|
|
|
|
mov dword ptr [esp], ebp
|
2005-05-20 01:35:39 +00:00
|
|
|
call X86CompilationCallback2
|
2008-03-23 12:32:54 +00:00
|
|
|
mov esp, ebp
|
|
|
|
sub esp, 12
|
2007-01-29 21:28:01 +00:00
|
|
|
pop ecx
|
2005-05-20 01:35:39 +00:00
|
|
|
pop edx
|
|
|
|
pop eax
|
2008-03-23 12:32:54 +00:00
|
|
|
pop ebp
|
2005-05-20 01:35:39 +00:00
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
2008-03-23 12:32:54 +00:00
|
|
|
|
2008-03-23 13:40:45 +00:00
|
|
|
# endif // _MSC_VER
|
2005-05-19 06:49:17 +00:00
|
|
|
|
2005-05-20 19:46:50 +00:00
|
|
|
#else // Not an i386 host
|
2005-05-20 01:35:39 +00:00
|
|
|
void X86CompilationCallback() {
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Cannot call X86CompilationCallback() on a non-x86 arch!");
|
2005-05-20 01:35:39 +00:00
|
|
|
}
|
2005-05-19 06:49:17 +00:00
|
|
|
#endif
|
2005-05-20 01:35:39 +00:00
|
|
|
}
|
2005-05-19 06:49:17 +00:00
|
|
|
|
2009-02-06 21:15:52 +00:00
|
|
|
/// X86CompilationCallback2 - This is the target-specific function invoked by the
|
2004-11-20 23:54:33 +00: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.
|
2008-07-16 17:54:34 +00:00
|
|
|
extern "C" void ATTRIBUTE_USED
|
|
|
|
X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr) {
|
2006-06-29 01:48:36 +00:00
|
|
|
intptr_t *RetAddrLoc = &StackPtr[1];
|
2005-05-20 01:35:39 +00:00
|
|
|
assert(*RetAddrLoc == RetAddr &&
|
2004-11-20 23:54:33 +00:00
|
|
|
"Could not find return address on the stack!");
|
|
|
|
|
|
|
|
// It's a stub if there is an interrupt marker after the call.
|
2006-09-08 06:48:29 +00:00
|
|
|
bool isStub = ((unsigned char*)RetAddr)[0] == 0xCD;
|
2004-11-20 23:54:33 +00:00
|
|
|
|
|
|
|
// The call instruction should have pushed the return value onto the stack...
|
2008-03-23 13:40:45 +00:00
|
|
|
#if defined (X86_64_JIT)
|
2007-03-14 10:44:30 +00:00
|
|
|
RetAddr--; // Backtrack to the reference itself...
|
|
|
|
#else
|
2004-11-20 23:54:33 +00:00
|
|
|
RetAddr -= 4; // Backtrack to the reference itself...
|
2007-03-14 10:44:30 +00:00
|
|
|
#endif
|
2004-11-20 23:54:33 +00:00
|
|
|
|
|
|
|
#if 0
|
2009-08-03 00:11:34 +00:00
|
|
|
DEBUG(errs() << "In callback! Addr=" << (void*)RetAddr
|
|
|
|
<< " ESP=" << (void*)StackPtr
|
|
|
|
<< ": Resolving call to function: "
|
|
|
|
<< TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
|
2004-11-20 23:54:33 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Sanity check to make sure this really is a call instruction.
|
2008-03-23 13:40:45 +00:00
|
|
|
#if defined (X86_64_JIT)
|
2007-03-14 10:44:30 +00: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 06:48:29 +00:00
|
|
|
assert(((unsigned char*)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
|
2007-03-14 10:44:30 +00:00
|
|
|
#endif
|
2005-04-21 23:38:14 +00:00
|
|
|
|
2006-09-08 06:48:29 +00:00
|
|
|
intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
|
2004-11-20 23:54:33 +00:00
|
|
|
|
|
|
|
// Rewrite the call target... so that we don't end up here every time we
|
|
|
|
// execute the call.
|
2008-03-23 13:40:45 +00:00
|
|
|
#if defined (X86_64_JIT)
|
2008-08-12 23:20:24 +00:00
|
|
|
if (!isStub)
|
|
|
|
*(intptr_t *)(RetAddr - 0xa) = NewVal;
|
2007-03-14 10:44:30 +00:00
|
|
|
#else
|
|
|
|
*(intptr_t *)RetAddr = (intptr_t)(NewVal-RetAddr-4);
|
|
|
|
#endif
|
2004-11-20 23:54:33 +00: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
|
|
|
|
// 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
|
2008-03-23 13:40:45 +00:00
|
|
|
#if defined (X86_64_JIT)
|
2008-08-12 23:20:24 +00: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));
|
|
|
|
}
|
2007-03-14 10:44:30 +00:00
|
|
|
#else
|
2006-09-08 06:48:29 +00:00
|
|
|
((unsigned char*)RetAddr)[-1] = 0xE9;
|
2007-03-14 10:44:30 +00:00
|
|
|
#endif
|
2004-11-20 23:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Change the return address to reexecute the call instruction...
|
2008-03-23 13:40:45 +00:00
|
|
|
#if defined (X86_64_JIT)
|
2007-03-14 10:44:30 +00:00
|
|
|
*RetAddrLoc -= 0xd;
|
|
|
|
#else
|
2005-05-20 01:35:39 +00:00
|
|
|
*RetAddrLoc -= 5;
|
2007-03-14 10:44:30 +00:00
|
|
|
#endif
|
2004-11-20 23:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TargetJITInfo::LazyResolverFn
|
|
|
|
X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
|
|
|
|
JITCompilerFunction = F;
|
2006-10-16 21:01:55 +00:00
|
|
|
|
2008-03-23 13:40:45 +00:00
|
|
|
#if defined (X86_32_JIT) && !defined (_MSC_VER)
|
2006-10-16 21:01:55 +00:00
|
|
|
unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
|
|
|
|
union {
|
|
|
|
unsigned u[3];
|
|
|
|
char c[12];
|
|
|
|
} text;
|
|
|
|
|
|
|
|
if (!X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1)) {
|
|
|
|
// FIXME: support for AMD family of processors.
|
|
|
|
if (memcmp(text.c, "GenuineIntel", 12) == 0) {
|
|
|
|
X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
|
|
|
|
if ((EDX >> 25) & 0x1)
|
|
|
|
return X86CompilationCallback_SSE;
|
|
|
|
}
|
|
|
|
}
|
2006-10-16 22:53:28 +00:00
|
|
|
#endif
|
2006-10-16 21:01:55 +00:00
|
|
|
|
2005-05-19 06:49:17 +00:00
|
|
|
return X86CompilationCallback;
|
2004-11-20 23:54:33 +00:00
|
|
|
}
|
|
|
|
|
2008-11-10 01:08:07 +00:00
|
|
|
void *X86JITInfo::emitGlobalValueIndirectSym(const GlobalValue* GV, void *ptr,
|
2009-05-30 20:51:52 +00:00
|
|
|
JITCodeEmitter &JCE) {
|
2008-03-23 13:40:45 +00:00
|
|
|
#if defined (X86_64_JIT)
|
2009-05-30 20:51:52 +00:00
|
|
|
JCE.startGVStub(GV, 8, 8);
|
|
|
|
JCE.emitWordLE((unsigned)(intptr_t)ptr);
|
|
|
|
JCE.emitWordLE((unsigned)(((intptr_t)ptr) >> 32));
|
2008-01-04 10:46:51 +00:00
|
|
|
#else
|
2009-05-30 20:51:52 +00:00
|
|
|
JCE.startGVStub(GV, 4, 4);
|
|
|
|
JCE.emitWordLE((intptr_t)ptr);
|
2008-01-04 10:46:51 +00:00
|
|
|
#endif
|
2009-05-30 20:51:52 +00:00
|
|
|
return JCE.finishGVStub(GV);
|
2008-01-04 10:46:51 +00:00
|
|
|
}
|
|
|
|
|
2008-04-16 20:46:05 +00:00
|
|
|
void *X86JITInfo::emitFunctionStub(const Function* F, void *Fn,
|
2009-05-30 20:51:52 +00:00
|
|
|
JITCodeEmitter &JCE) {
|
2006-06-01 17:13:10 +00:00
|
|
|
// Note, we cast to intptr_t here to silence a -pedantic warning that
|
|
|
|
// complains about casting a function pointer to a normal pointer.
|
2008-03-23 13:40:45 +00:00
|
|
|
#if defined (X86_32_JIT) && !defined (_MSC_VER)
|
2006-10-16 22:53:28 +00:00
|
|
|
bool NotCC = (Fn != (void*)(intptr_t)X86CompilationCallback &&
|
|
|
|
Fn != (void*)(intptr_t)X86CompilationCallback_SSE);
|
2006-10-16 23:44:08 +00:00
|
|
|
#else
|
|
|
|
bool NotCC = Fn != (void*)(intptr_t)X86CompilationCallback;
|
2006-10-16 22:53:28 +00:00
|
|
|
#endif
|
|
|
|
if (NotCC) {
|
2008-03-23 13:40:45 +00:00
|
|
|
#if defined (X86_64_JIT)
|
2009-05-30 20:51:52 +00:00
|
|
|
JCE.startGVStub(F, 13, 4);
|
|
|
|
JCE.emitByte(0x49); // REX prefix
|
|
|
|
JCE.emitByte(0xB8+2); // movabsq r10
|
|
|
|
JCE.emitWordLE((unsigned)(intptr_t)Fn);
|
|
|
|
JCE.emitWordLE((unsigned)(((intptr_t)Fn) >> 32));
|
|
|
|
JCE.emitByte(0x41); // REX prefix
|
|
|
|
JCE.emitByte(0xFF); // jmpq *r10
|
|
|
|
JCE.emitByte(2 | (4 << 3) | (3 << 6));
|
2007-03-14 10:48:08 +00:00
|
|
|
#else
|
2009-05-30 20:51:52 +00:00
|
|
|
JCE.startGVStub(F, 5, 4);
|
|
|
|
JCE.emitByte(0xE9);
|
|
|
|
JCE.emitWordLE((intptr_t)Fn-JCE.getCurrentPCValue()-4);
|
2007-03-14 10:48:08 +00:00
|
|
|
#endif
|
2009-05-30 20:51:52 +00:00
|
|
|
return JCE.finishGVStub(F);
|
2004-11-22 22:25:30 +00:00
|
|
|
}
|
2005-04-21 23:38:14 +00:00
|
|
|
|
2008-03-23 13:40:45 +00:00
|
|
|
#if defined (X86_64_JIT)
|
2009-05-30 20:51:52 +00:00
|
|
|
JCE.startGVStub(F, 14, 4);
|
|
|
|
JCE.emitByte(0x49); // REX prefix
|
|
|
|
JCE.emitByte(0xB8+2); // movabsq r10
|
|
|
|
JCE.emitWordLE((unsigned)(intptr_t)Fn);
|
|
|
|
JCE.emitWordLE((unsigned)(((intptr_t)Fn) >> 32));
|
|
|
|
JCE.emitByte(0x41); // REX prefix
|
|
|
|
JCE.emitByte(0xFF); // callq *r10
|
|
|
|
JCE.emitByte(2 | (2 << 3) | (3 << 6));
|
2007-03-14 10:44:30 +00:00
|
|
|
#else
|
2009-05-30 20:51:52 +00:00
|
|
|
JCE.startGVStub(F, 6, 4);
|
|
|
|
JCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
|
2004-11-22 22:25:30 +00:00
|
|
|
|
2009-05-30 20:51:52 +00:00
|
|
|
JCE.emitWordLE((intptr_t)Fn-JCE.getCurrentPCValue()-4);
|
2007-03-14 10:44:30 +00:00
|
|
|
#endif
|
2004-11-22 22:25:30 +00:00
|
|
|
|
2009-05-30 20:51:52 +00:00
|
|
|
JCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
|
|
|
|
return JCE.finishGVStub(F);
|
2009-02-18 08:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void X86JITInfo::emitFunctionStubAtAddr(const Function* F, void *Fn, void *Stub,
|
2009-05-30 20:51:52 +00:00
|
|
|
JITCodeEmitter &JCE) {
|
2009-02-18 08:31:02 +00:00
|
|
|
// Note, we cast to intptr_t here to silence a -pedantic warning that
|
|
|
|
// complains about casting a function pointer to a normal pointer.
|
2009-05-30 20:51:52 +00:00
|
|
|
JCE.startGVStub(F, Stub, 5);
|
|
|
|
JCE.emitByte(0xE9);
|
2009-02-18 08:31:02 +00:00
|
|
|
#if defined (X86_64_JIT)
|
2009-05-30 20:51:52 +00:00
|
|
|
assert(((((intptr_t)Fn-JCE.getCurrentPCValue()-5) << 32) >> 32) ==
|
|
|
|
((intptr_t)Fn-JCE.getCurrentPCValue()-5)
|
2009-02-18 08:31:02 +00:00
|
|
|
&& "PIC displacement does not fit in displacement field!");
|
|
|
|
#endif
|
2009-05-30 20:51:52 +00:00
|
|
|
JCE.emitWordLE((intptr_t)Fn-JCE.getCurrentPCValue()-4);
|
|
|
|
JCE.finishGVStub(F);
|
2004-11-22 22:25:30 +00:00
|
|
|
}
|
2004-11-20 23:54:33 +00:00
|
|
|
|
2008-01-05 02:26:58 +00:00
|
|
|
/// getPICJumpTableEntry - Returns the value of the jumptable entry for the
|
|
|
|
/// specific basic block.
|
2008-12-10 02:32:19 +00:00
|
|
|
uintptr_t X86JITInfo::getPICJumpTableEntry(uintptr_t BB, uintptr_t Entry) {
|
2008-07-16 01:33:08 +00:00
|
|
|
#if defined(X86_64_JIT)
|
|
|
|
return BB - Entry;
|
|
|
|
#else
|
2008-01-05 02:26:58 +00:00
|
|
|
return BB - PICBase;
|
2008-07-16 01:33:08 +00:00
|
|
|
#endif
|
2008-01-05 02:26:58 +00:00
|
|
|
}
|
|
|
|
|
2004-11-20 23:54:33 +00: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 20:49:37 +00:00
|
|
|
unsigned NumRelocs, unsigned char* GOTBase) {
|
2004-11-20 23:54:33 +00: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 06:48:29 +00:00
|
|
|
case X86::reloc_pcrel_word: {
|
2004-11-20 23:54:33 +00: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 09:40:20 +00: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 06:48:29 +00:00
|
|
|
*((unsigned*)RelocPos) += (unsigned)ResultPtr;
|
2004-11-20 23:54:33 +00:00
|
|
|
break;
|
2006-09-08 06:48:29 +00:00
|
|
|
}
|
2004-11-20 23:54:33 +00:00
|
|
|
case X86::reloc_absolute_word:
|
2009-08-05 00:11:21 +00:00
|
|
|
case X86::reloc_absolute_word_sext:
|
2004-11-20 23:54:33 +00:00
|
|
|
// Absolute relocation, just add the relocated value to the value already
|
|
|
|
// in memory.
|
2006-09-08 06:48:29 +00:00
|
|
|
*((unsigned*)RelocPos) += (unsigned)ResultPtr;
|
2004-11-20 23:54:33 +00:00
|
|
|
break;
|
2006-12-05 04:01:03 +00:00
|
|
|
case X86::reloc_absolute_dword:
|
|
|
|
*((intptr_t*)RelocPos) += ResultPtr;
|
|
|
|
break;
|
2004-11-20 23:54:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-25 15:41:43 +00: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 16:55:14 +00:00
|
|
|
llvm_unreachable("Cannot allocate thread local storage on this arch!");
|
2008-10-25 15:41:43 +00:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|