blob: 0168d12231f70060deb42eecf78e73cf55244e56 [file] [log] [blame]
Chris Lattner7ddde322004-11-20 23:54:33 +00001//===-- X86JITInfo.cpp - Implement the JIT interfaces for the X86 target --===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
Chris Lattner7ddde322004-11-20 23:54:33 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
Chris Lattner7ddde322004-11-20 23:54:33 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the JIT interfaces for the X86 target.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "jit"
15#include "X86JITInfo.h"
16#include "X86Relocations.h"
Evan Cheng93b11f82006-10-16 21:01:55 +000017#include "X86Subtarget.h"
Evan Chengd0da6ff2009-09-03 04:37:05 +000018#include "X86TargetMachine.h"
Nicolas Geoffray51cc3c12008-04-16 20:46:05 +000019#include "llvm/Function.h"
Devang Patel0d885d12008-07-16 17:54:34 +000020#include "llvm/Support/Compiler.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000021#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000022#include "llvm/Support/Valgrind.h"
Nate Begemanbe136342005-05-20 21:29:24 +000023#include <cstdlib>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000024#include <cstring>
Chris Lattner7ddde322004-11-20 23:54:33 +000025using namespace llvm;
26
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +000027// Determine the platform we're running on
Anton Korobeynikov6f9bb6f2009-08-28 16:06:41 +000028#if defined (__x86_64__) || defined (_M_AMD64) || defined (_M_X64)
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +000029# define X86_64_JIT
30#elif defined(__i386__) || defined(i386) || defined(_M_IX86)
31# define X86_32_JIT
Chris Lattner38f73732006-01-26 19:55:20 +000032#endif
33
Chris Lattner7ddde322004-11-20 23:54:33 +000034void X86JITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
35 unsigned char *OldByte = (unsigned char *)Old;
36 *OldByte++ = 0xE9; // Emit JMP opcode.
37 unsigned *OldWord = (unsigned *)OldByte;
38 unsigned NewAddr = (intptr_t)New;
39 unsigned OldAddr = (intptr_t)OldWord;
40 *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
Jeffrey Yasskinf28411f2010-03-15 04:57:55 +000041
42 // X86 doesn't need to invalidate the processor cache, so just invalidate
43 // Valgrind's cache directly.
44 sys::ValgrindDiscardTranslations(Old, 5);
Chris Lattner7ddde322004-11-20 23:54:33 +000045}
46
47
Chris Lattner7ddde322004-11-20 23:54:33 +000048/// JITCompilerFunction - This contains the address of the JIT function used to
49/// compile a function lazily.
50static TargetJITInfo::JITCompilerFn JITCompilerFunction;
51
Chris Lattner40f4ba52006-09-08 17:03:56 +000052// Get the ASMPREFIX for the current host. This is often '_'.
53#ifndef __USER_LABEL_PREFIX__
54#define __USER_LABEL_PREFIX__
55#endif
56#define GETASMPREFIX2(X) #X
57#define GETASMPREFIX(X) GETASMPREFIX2(X)
58#define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
59
Dan Gohman9036d802009-02-06 21:15:52 +000060// For ELF targets, use a .size and .type directive, to let tools
61// know the extent of functions defined in assembler.
62#if defined(__ELF__)
63# define SIZE(sym) ".size " #sym ", . - " #sym "\n"
64# define TYPE_FUNCTION(sym) ".type " #sym ", @function\n"
65#else
66# define SIZE(sym)
67# define TYPE_FUNCTION(sym)
68#endif
69
Anton Korobeynikov7eb58772007-12-10 23:10:20 +000070// Provide a convenient way for disabling usage of CFI directives.
Anton Korobeynikov2fb9dee2007-12-10 23:08:35 +000071// This is needed for old/broken assemblers (for example, gas on
72// Darwin is pretty old and doesn't support these directives)
Anton Korobeynikov5f682872007-12-10 23:04:38 +000073#if defined(__APPLE__)
74# define CFI(x)
75#else
Anton Korobeynikov144a45e2007-12-22 20:41:12 +000076// FIXME: Disable this until we really want to use it. Also, we will
77// need to add some workarounds for compilers, which support
78// only subset of these directives.
Anton Korobeynikovd07310a2007-12-22 20:46:24 +000079# define CFI(x)
Anton Korobeynikov5f682872007-12-10 23:04:38 +000080#endif
81
Chris Lattner1030f722005-05-19 06:49:17 +000082// Provide a wrapper for X86CompilationCallback2 that saves non-traditional
83// callee saved registers, for the fastcc calling convention.
Jeff Cohen8bc6f932005-05-20 01:35:39 +000084extern "C" {
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +000085#if defined(X86_64_JIT)
86# ifndef _MSC_VER
Evan Cheng25ab6902006-09-08 06:48:29 +000087 // No need to save EAX/EDX for X86-64.
88 void X86CompilationCallback(void);
89 asm(
90 ".text\n"
91 ".align 8\n"
Chris Lattner40f4ba52006-09-08 17:03:56 +000092 ".globl " ASMPREFIX "X86CompilationCallback\n"
Dan Gohman9036d802009-02-06 21:15:52 +000093 TYPE_FUNCTION(X86CompilationCallback)
Chris Lattner40f4ba52006-09-08 17:03:56 +000094 ASMPREFIX "X86CompilationCallback:\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +000095 CFI(".cfi_startproc\n")
Evan Cheng25ab6902006-09-08 06:48:29 +000096 // Save RBP
97 "pushq %rbp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +000098 CFI(".cfi_def_cfa_offset 16\n")
99 CFI(".cfi_offset %rbp, -16\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000100 // Save RSP
101 "movq %rsp, %rbp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000102 CFI(".cfi_def_cfa_register %rbp\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000103 // Save all int arg registers
104 "pushq %rdi\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000105 CFI(".cfi_rel_offset %rdi, 0\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000106 "pushq %rsi\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000107 CFI(".cfi_rel_offset %rsi, 8\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000108 "pushq %rdx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000109 CFI(".cfi_rel_offset %rdx, 16\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000110 "pushq %rcx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000111 CFI(".cfi_rel_offset %rcx, 24\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000112 "pushq %r8\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000113 CFI(".cfi_rel_offset %r8, 32\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000114 "pushq %r9\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000115 CFI(".cfi_rel_offset %r9, 40\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000116 // Align stack on 16-byte boundary. ESP might not be properly aligned
117 // (8 byte) if this is called from an indirect stub.
118 "andq $-16, %rsp\n"
119 // Save all XMM arg registers
120 "subq $128, %rsp\n"
121 "movaps %xmm0, (%rsp)\n"
122 "movaps %xmm1, 16(%rsp)\n"
123 "movaps %xmm2, 32(%rsp)\n"
124 "movaps %xmm3, 48(%rsp)\n"
125 "movaps %xmm4, 64(%rsp)\n"
126 "movaps %xmm5, 80(%rsp)\n"
127 "movaps %xmm6, 96(%rsp)\n"
128 "movaps %xmm7, 112(%rsp)\n"
129 // JIT callee
NAKAMURA Takumi9c7719e2011-02-05 15:11:03 +0000130#ifdef _WIN64
131 "subq $32, %rsp\n"
132 "movq %rbp, %rcx\n" // Pass prev frame and return address
133 "movq 8(%rbp), %rdx\n"
134 "call " ASMPREFIX "X86CompilationCallback2\n"
135 "addq $32, %rsp\n"
136#else
Evan Cheng25ab6902006-09-08 06:48:29 +0000137 "movq %rbp, %rdi\n" // Pass prev frame and return address
138 "movq 8(%rbp), %rsi\n"
Anton Korobeynikov6c402572009-09-06 20:21:48 +0000139 "call " ASMPREFIX "X86CompilationCallback2\n"
NAKAMURA Takumi9c7719e2011-02-05 15:11:03 +0000140#endif
Evan Cheng25ab6902006-09-08 06:48:29 +0000141 // Restore all XMM arg registers
142 "movaps 112(%rsp), %xmm7\n"
143 "movaps 96(%rsp), %xmm6\n"
144 "movaps 80(%rsp), %xmm5\n"
145 "movaps 64(%rsp), %xmm4\n"
146 "movaps 48(%rsp), %xmm3\n"
147 "movaps 32(%rsp), %xmm2\n"
148 "movaps 16(%rsp), %xmm1\n"
149 "movaps (%rsp), %xmm0\n"
150 // Restore RSP
151 "movq %rbp, %rsp\n"
Scott Michela28c6bf2007-12-12 02:38:28 +0000152 CFI(".cfi_def_cfa_register %rsp\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000153 // Restore all int arg registers
154 "subq $48, %rsp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000155 CFI(".cfi_adjust_cfa_offset 48\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000156 "popq %r9\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000157 CFI(".cfi_adjust_cfa_offset -8\n")
158 CFI(".cfi_restore %r9\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000159 "popq %r8\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000160 CFI(".cfi_adjust_cfa_offset -8\n")
161 CFI(".cfi_restore %r8\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000162 "popq %rcx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000163 CFI(".cfi_adjust_cfa_offset -8\n")
164 CFI(".cfi_restore %rcx\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000165 "popq %rdx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000166 CFI(".cfi_adjust_cfa_offset -8\n")
167 CFI(".cfi_restore %rdx\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000168 "popq %rsi\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000169 CFI(".cfi_adjust_cfa_offset -8\n")
170 CFI(".cfi_restore %rsi\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000171 "popq %rdi\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000172 CFI(".cfi_adjust_cfa_offset -8\n")
173 CFI(".cfi_restore %rdi\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 // Restore RBP
175 "popq %rbp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000176 CFI(".cfi_adjust_cfa_offset -8\n")
177 CFI(".cfi_restore %rbp\n")
Anton Korobeynikov3a7bcc42007-12-10 15:27:07 +0000178 "ret\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000179 CFI(".cfi_endproc\n")
Dan Gohman9036d802009-02-06 21:15:52 +0000180 SIZE(X86CompilationCallback)
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000181 );
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000182# else
Anton Korobeynikov231e9642008-03-23 14:44:32 +0000183 // No inline assembler support on this platform. The routine is in external
184 // file.
185 void X86CompilationCallback();
186
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000187# endif
188#elif defined (X86_32_JIT)
189# ifndef _MSC_VER
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000190 void X86CompilationCallback(void);
191 asm(
192 ".text\n"
193 ".align 8\n"
Dan Gohman9036d802009-02-06 21:15:52 +0000194 ".globl " ASMPREFIX "X86CompilationCallback\n"
195 TYPE_FUNCTION(X86CompilationCallback)
Chris Lattner40f4ba52006-09-08 17:03:56 +0000196 ASMPREFIX "X86CompilationCallback:\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000197 CFI(".cfi_startproc\n")
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000198 "pushl %ebp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000199 CFI(".cfi_def_cfa_offset 8\n")
200 CFI(".cfi_offset %ebp, -8\n")
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000201 "movl %esp, %ebp\n" // Standard prologue
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000202 CFI(".cfi_def_cfa_register %ebp\n")
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000203 "pushl %eax\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000204 CFI(".cfi_rel_offset %eax, 0\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000205 "pushl %edx\n" // Save EAX/EDX/ECX
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000206 CFI(".cfi_rel_offset %edx, 4\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000207 "pushl %ecx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000208 CFI(".cfi_rel_offset %ecx, 8\n")
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000209# if defined(__APPLE__)
Evan Chengda08d2c2006-06-24 08:36:10 +0000210 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000211# endif
Evan Chengbe33dd92006-06-29 01:48:36 +0000212 "subl $16, %esp\n"
213 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
214 "movl %eax, 4(%esp)\n"
215 "movl %ebp, (%esp)\n"
Anton Korobeynikov6c402572009-09-06 20:21:48 +0000216 "call " ASMPREFIX "X86CompilationCallback2\n"
Evan Chengda08d2c2006-06-24 08:36:10 +0000217 "movl %ebp, %esp\n" // Restore ESP
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000218 CFI(".cfi_def_cfa_register %esp\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000219 "subl $12, %esp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000220 CFI(".cfi_adjust_cfa_offset 12\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000221 "popl %ecx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000222 CFI(".cfi_adjust_cfa_offset -4\n")
223 CFI(".cfi_restore %ecx\n")
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000224 "popl %edx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000225 CFI(".cfi_adjust_cfa_offset -4\n")
226 CFI(".cfi_restore %edx\n")
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000227 "popl %eax\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000228 CFI(".cfi_adjust_cfa_offset -4\n")
229 CFI(".cfi_restore %eax\n")
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000230 "popl %ebp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000231 CFI(".cfi_adjust_cfa_offset -4\n")
232 CFI(".cfi_restore %ebp\n")
Anton Korobeynikova14b6692007-12-10 14:54:42 +0000233 "ret\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000234 CFI(".cfi_endproc\n")
Dan Gohman9036d802009-02-06 21:15:52 +0000235 SIZE(X86CompilationCallback)
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000236 );
Evan Cheng93b11f82006-10-16 21:01:55 +0000237
238 // Same as X86CompilationCallback but also saves XMM argument registers.
239 void X86CompilationCallback_SSE(void);
240 asm(
241 ".text\n"
242 ".align 8\n"
Dan Gohman9036d802009-02-06 21:15:52 +0000243 ".globl " ASMPREFIX "X86CompilationCallback_SSE\n"
244 TYPE_FUNCTION(X86CompilationCallback_SSE)
Evan Cheng93b11f82006-10-16 21:01:55 +0000245 ASMPREFIX "X86CompilationCallback_SSE:\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000246 CFI(".cfi_startproc\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000247 "pushl %ebp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000248 CFI(".cfi_def_cfa_offset 8\n")
249 CFI(".cfi_offset %ebp, -8\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000250 "movl %esp, %ebp\n" // Standard prologue
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000251 CFI(".cfi_def_cfa_register %ebp\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000252 "pushl %eax\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000253 CFI(".cfi_rel_offset %eax, 0\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000254 "pushl %edx\n" // Save EAX/EDX/ECX
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000255 CFI(".cfi_rel_offset %edx, 4\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000256 "pushl %ecx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000257 CFI(".cfi_rel_offset %ecx, 8\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000258 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
259 // Save all XMM arg registers
260 "subl $64, %esp\n"
Anton Korobeynikovdf7814c2007-12-10 15:13:55 +0000261 // FIXME: provide frame move information for xmm registers.
262 // This can be tricky, because CFA register is ebp (unaligned)
263 // and we need to produce offsets relative to it.
Evan Cheng93b11f82006-10-16 21:01:55 +0000264 "movaps %xmm0, (%esp)\n"
265 "movaps %xmm1, 16(%esp)\n"
266 "movaps %xmm2, 32(%esp)\n"
267 "movaps %xmm3, 48(%esp)\n"
268 "subl $16, %esp\n"
269 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
270 "movl %eax, 4(%esp)\n"
271 "movl %ebp, (%esp)\n"
Anton Korobeynikov6c402572009-09-06 20:21:48 +0000272 "call " ASMPREFIX "X86CompilationCallback2\n"
Evan Cheng93b11f82006-10-16 21:01:55 +0000273 "addl $16, %esp\n"
274 "movaps 48(%esp), %xmm3\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000275 CFI(".cfi_restore %xmm3\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000276 "movaps 32(%esp), %xmm2\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000277 CFI(".cfi_restore %xmm2\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000278 "movaps 16(%esp), %xmm1\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000279 CFI(".cfi_restore %xmm1\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000280 "movaps (%esp), %xmm0\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000281 CFI(".cfi_restore %xmm0\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000282 "movl %ebp, %esp\n" // Restore ESP
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000283 CFI(".cfi_def_cfa_register esp\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000284 "subl $12, %esp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000285 CFI(".cfi_adjust_cfa_offset 12\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000286 "popl %ecx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000287 CFI(".cfi_adjust_cfa_offset -4\n")
288 CFI(".cfi_restore %ecx\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000289 "popl %edx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000290 CFI(".cfi_adjust_cfa_offset -4\n")
291 CFI(".cfi_restore %edx\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000292 "popl %eax\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000293 CFI(".cfi_adjust_cfa_offset -4\n")
294 CFI(".cfi_restore %eax\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000295 "popl %ebp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000296 CFI(".cfi_adjust_cfa_offset -4\n")
297 CFI(".cfi_restore %ebp\n")
Anton Korobeynikovdf7814c2007-12-10 15:13:55 +0000298 "ret\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000299 CFI(".cfi_endproc\n")
Dan Gohman9036d802009-02-06 21:15:52 +0000300 SIZE(X86CompilationCallback_SSE)
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000301 );
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000302# else
Ahmed Charlesb83a67e2012-02-13 06:30:56 +0000303 // the following function is called only from this translation unit,
304 // unless we are under 64bit Windows with MSC, where there is
305 // no support for inline assembly
306 static void X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr);
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000307
308 _declspec(naked) void X86CompilationCallback(void) {
309 __asm {
Anton Korobeynikov6aeedfd2008-03-23 12:32:54 +0000310 push ebp
311 mov ebp, esp
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000312 push eax
313 push edx
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000314 push ecx
Anton Korobeynikov6aeedfd2008-03-23 12:32:54 +0000315 and esp, -16
Torok Edwinacf2a652010-02-08 08:37:27 +0000316 sub esp, 16
Anton Korobeynikov6aeedfd2008-03-23 12:32:54 +0000317 mov eax, dword ptr [ebp+4]
318 mov dword ptr [esp+4], eax
319 mov dword ptr [esp], ebp
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000320 call X86CompilationCallback2
Anton Korobeynikov6aeedfd2008-03-23 12:32:54 +0000321 mov esp, ebp
322 sub esp, 12
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000323 pop ecx
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000324 pop edx
325 pop eax
Anton Korobeynikov6aeedfd2008-03-23 12:32:54 +0000326 pop ebp
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000327 ret
328 }
329 }
Anton Korobeynikov6aeedfd2008-03-23 12:32:54 +0000330
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000331# endif // _MSC_VER
Chris Lattner1030f722005-05-19 06:49:17 +0000332
Misha Brukman57ebf3d2005-05-20 19:46:50 +0000333#else // Not an i386 host
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000334 void X86CompilationCallback() {
Torok Edwinc23197a2009-07-14 16:55:14 +0000335 llvm_unreachable("Cannot call X86CompilationCallback() on a non-x86 arch!");
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000336 }
Chris Lattner1030f722005-05-19 06:49:17 +0000337#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000338}
Chris Lattner1030f722005-05-19 06:49:17 +0000339
Dan Gohman9036d802009-02-06 21:15:52 +0000340/// X86CompilationCallback2 - This is the target-specific function invoked by the
Chris Lattner7ddde322004-11-20 23:54:33 +0000341/// function stub when we did not know the real target of a call. This function
342/// must locate the start of the stub or call site and pass it into the JIT
343/// compiler function.
Anton Korobeynikov6c402572009-09-06 20:21:48 +0000344extern "C" {
345#if !(defined (X86_64_JIT) && defined(_MSC_VER))
346 // the following function is called only from this translation unit,
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000347 // unless we are under 64bit Windows with MSC, where there is
Anton Korobeynikov6c402572009-09-06 20:21:48 +0000348 // no support for inline assembly
349static
350#endif
Chandler Carruth100c2672010-10-23 08:10:43 +0000351void LLVM_ATTRIBUTE_USED
Devang Patel0d885d12008-07-16 17:54:34 +0000352X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr) {
Evan Chengbe33dd92006-06-29 01:48:36 +0000353 intptr_t *RetAddrLoc = &StackPtr[1];
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000354 assert(*RetAddrLoc == RetAddr &&
Chris Lattner7ddde322004-11-20 23:54:33 +0000355 "Could not find return address on the stack!");
356
357 // It's a stub if there is an interrupt marker after the call.
Dale Johannesen7d1a7c02009-09-15 18:32:14 +0000358 bool isStub = ((unsigned char*)RetAddr)[0] == 0xCE;
Chris Lattner7ddde322004-11-20 23:54:33 +0000359
360 // The call instruction should have pushed the return value onto the stack...
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000361#if defined (X86_64_JIT)
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000362 RetAddr--; // Backtrack to the reference itself...
363#else
Chris Lattner7ddde322004-11-20 23:54:33 +0000364 RetAddr -= 4; // Backtrack to the reference itself...
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000365#endif
Chris Lattner7ddde322004-11-20 23:54:33 +0000366
367#if 0
David Greene92f16442010-01-05 01:29:23 +0000368 DEBUG(dbgs() << "In callback! Addr=" << (void*)RetAddr
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000369 << " ESP=" << (void*)StackPtr
370 << ": Resolving call to function: "
371 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
Chris Lattner7ddde322004-11-20 23:54:33 +0000372#endif
373
374 // Sanity check to make sure this really is a call instruction.
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000375#if defined (X86_64_JIT)
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000376 assert(((unsigned char*)RetAddr)[-2] == 0x41 &&"Not a call instr!");
377 assert(((unsigned char*)RetAddr)[-1] == 0xFF &&"Not a call instr!");
378#else
Evan Cheng25ab6902006-09-08 06:48:29 +0000379 assert(((unsigned char*)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000380#endif
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000381
Evan Cheng25ab6902006-09-08 06:48:29 +0000382 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
Chris Lattner7ddde322004-11-20 23:54:33 +0000383
384 // Rewrite the call target... so that we don't end up here every time we
385 // execute the call.
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000386#if defined (X86_64_JIT)
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +0000387 assert(isStub &&
388 "X86-64 doesn't support rewriting non-stub lazy compilation calls:"
389 " the call instruction varies too much.");
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000390#else
391 *(intptr_t *)RetAddr = (intptr_t)(NewVal-RetAddr-4);
392#endif
Chris Lattner7ddde322004-11-20 23:54:33 +0000393
394 if (isStub) {
395 // If this is a stub, rewrite the call into an unconditional branch
396 // instruction so that two return addresses are not pushed onto the stack
397 // when the requested function finally gets called. This also makes the
Dale Johannesen7d1a7c02009-09-15 18:32:14 +0000398 // 0xCE byte (interrupt) dead, so the marker doesn't effect anything.
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000399#if defined (X86_64_JIT)
Dale Johannesen6f83be02008-08-12 23:20:24 +0000400 // If the target address is within 32-bit range of the stub, use a
401 // PC-relative branch instead of loading the actual address. (This is
402 // considerably shorter than the 64-bit immediate load already there.)
403 // We assume here intptr_t is 64 bits.
404 intptr_t diff = NewVal-RetAddr+7;
405 if (diff >= -2147483648LL && diff <= 2147483647LL) {
406 *(unsigned char*)(RetAddr-0xc) = 0xE9;
407 *(intptr_t *)(RetAddr-0xb) = diff & 0xffffffff;
408 } else {
409 *(intptr_t *)(RetAddr - 0xa) = NewVal;
410 ((unsigned char*)RetAddr)[0] = (2 | (4 << 3) | (3 << 6));
411 }
Jeffrey Yasskinf28411f2010-03-15 04:57:55 +0000412 sys::ValgrindDiscardTranslations((void*)(RetAddr-0xc), 0xd);
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000413#else
Evan Cheng25ab6902006-09-08 06:48:29 +0000414 ((unsigned char*)RetAddr)[-1] = 0xE9;
Jeffrey Yasskinf28411f2010-03-15 04:57:55 +0000415 sys::ValgrindDiscardTranslations((void*)(RetAddr-1), 5);
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000416#endif
Chris Lattner7ddde322004-11-20 23:54:33 +0000417 }
418
419 // Change the return address to reexecute the call instruction...
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000420#if defined (X86_64_JIT)
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000421 *RetAddrLoc -= 0xd;
422#else
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000423 *RetAddrLoc -= 5;
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000424#endif
Chris Lattner7ddde322004-11-20 23:54:33 +0000425}
Anton Korobeynikov6c402572009-09-06 20:21:48 +0000426}
Chris Lattner7ddde322004-11-20 23:54:33 +0000427
Chris Lattner7ddde322004-11-20 23:54:33 +0000428TargetJITInfo::LazyResolverFn
429X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
Nick Lewycky36049242011-12-03 02:45:50 +0000430 TsanIgnoreWritesBegin();
Chris Lattner7ddde322004-11-20 23:54:33 +0000431 JITCompilerFunction = F;
Nick Lewycky36049242011-12-03 02:45:50 +0000432 TsanIgnoreWritesEnd();
Evan Cheng93b11f82006-10-16 21:01:55 +0000433
Evan Cheng22f91442009-09-03 05:01:00 +0000434#if defined (X86_32_JIT) && !defined (_MSC_VER)
435 if (Subtarget->hasSSE1())
436 return X86CompilationCallback_SSE;
437#endif
438
439 return X86CompilationCallback;
Evan Chengd0da6ff2009-09-03 04:37:05 +0000440}
Evan Cheng93b11f82006-10-16 21:01:55 +0000441
Evan Chengd0da6ff2009-09-03 04:37:05 +0000442X86JITInfo::X86JITInfo(X86TargetMachine &tm) : TM(tm) {
443 Subtarget = &TM.getSubtarget<X86Subtarget>();
444 useGOT = 0;
445 TLSOffset = 0;
Chris Lattner7ddde322004-11-20 23:54:33 +0000446}
447
Evan Cheng9ed2f802008-11-10 01:08:07 +0000448void *X86JITInfo::emitGlobalValueIndirectSym(const GlobalValue* GV, void *ptr,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000449 JITCodeEmitter &JCE) {
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000450#if defined (X86_64_JIT)
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +0000451 const unsigned Alignment = 8;
452 uint8_t Buffer[8];
453 uint8_t *Cur = Buffer;
454 MachineCodeEmitter::emitWordLEInto(Cur, (unsigned)(intptr_t)ptr);
455 MachineCodeEmitter::emitWordLEInto(Cur, (unsigned)(((intptr_t)ptr) >> 32));
Evan Chengbe8c03f2008-01-04 10:46:51 +0000456#else
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +0000457 const unsigned Alignment = 4;
458 uint8_t Buffer[4];
459 uint8_t *Cur = Buffer;
460 MachineCodeEmitter::emitWordLEInto(Cur, (intptr_t)ptr);
Evan Chengbe8c03f2008-01-04 10:46:51 +0000461#endif
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +0000462 return JCE.allocIndirectGV(GV, Buffer, sizeof(Buffer), Alignment);
Evan Chengbe8c03f2008-01-04 10:46:51 +0000463}
464
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000465TargetJITInfo::StubLayout X86JITInfo::getStubLayout() {
466 // The 64-bit stub contains:
467 // movabs r10 <- 8-byte-target-address # 10 bytes
468 // call|jmp *r10 # 3 bytes
469 // The 32-bit stub contains a 5-byte call|jmp.
470 // If the stub is a call to the compilation callback, an extra byte is added
471 // to mark it as a stub.
472 StubLayout Result = {14, 4};
473 return Result;
474}
475
476void *X86JITInfo::emitFunctionStub(const Function* F, void *Target,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000477 JITCodeEmitter &JCE) {
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000478 // Note, we cast to intptr_t here to silence a -pedantic warning that
Chris Lattner078d1822006-06-01 17:13:10 +0000479 // complains about casting a function pointer to a normal pointer.
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000480#if defined (X86_32_JIT) && !defined (_MSC_VER)
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000481 bool NotCC = (Target != (void*)(intptr_t)X86CompilationCallback &&
482 Target != (void*)(intptr_t)X86CompilationCallback_SSE);
Evan Chengcf922302006-10-16 23:44:08 +0000483#else
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000484 bool NotCC = Target != (void*)(intptr_t)X86CompilationCallback;
Evan Cheng348b00d2006-10-16 22:53:28 +0000485#endif
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000486 JCE.emitAlignment(4);
487 void *Result = (void*)JCE.getCurrentPCValue();
Evan Cheng348b00d2006-10-16 22:53:28 +0000488 if (NotCC) {
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000489#if defined (X86_64_JIT)
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000490 JCE.emitByte(0x49); // REX prefix
491 JCE.emitByte(0xB8+2); // movabsq r10
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000492 JCE.emitWordLE((unsigned)(intptr_t)Target);
493 JCE.emitWordLE((unsigned)(((intptr_t)Target) >> 32));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000494 JCE.emitByte(0x41); // REX prefix
495 JCE.emitByte(0xFF); // jmpq *r10
496 JCE.emitByte(2 | (4 << 3) | (3 << 6));
Evan Cheng8510dc02007-03-14 10:48:08 +0000497#else
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000498 JCE.emitByte(0xE9);
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000499 JCE.emitWordLE((intptr_t)Target-JCE.getCurrentPCValue()-4);
Evan Cheng8510dc02007-03-14 10:48:08 +0000500#endif
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000501 return Result;
Chris Lattner90b1b452004-11-22 22:25:30 +0000502 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000503
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000504#if defined (X86_64_JIT)
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000505 JCE.emitByte(0x49); // REX prefix
506 JCE.emitByte(0xB8+2); // movabsq r10
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000507 JCE.emitWordLE((unsigned)(intptr_t)Target);
508 JCE.emitWordLE((unsigned)(((intptr_t)Target) >> 32));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000509 JCE.emitByte(0x41); // REX prefix
510 JCE.emitByte(0xFF); // callq *r10
511 JCE.emitByte(2 | (2 << 3) | (3 << 6));
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000512#else
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000513 JCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
Chris Lattner90b1b452004-11-22 22:25:30 +0000514
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000515 JCE.emitWordLE((intptr_t)Target-JCE.getCurrentPCValue()-4);
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000516#endif
Chris Lattner90b1b452004-11-22 22:25:30 +0000517
Dale Johannesen7d1a7c02009-09-15 18:32:14 +0000518 // This used to use 0xCD, but that value is used by JITMemoryManager to
519 // initialize the buffer with garbage, which means it may follow a
520 // noreturn function call, confusing X86CompilationCallback2. PR 4929.
521 JCE.emitByte(0xCE); // Interrupt - Just a marker identifying the stub!
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000522 return Result;
Nate Begemand6b7a242009-02-18 08:31:02 +0000523}
524
Evan Cheng2a3e08b2008-01-05 02:26:58 +0000525/// getPICJumpTableEntry - Returns the value of the jumptable entry for the
526/// specific basic block.
Evan Cheng5788d1a2008-12-10 02:32:19 +0000527uintptr_t X86JITInfo::getPICJumpTableEntry(uintptr_t BB, uintptr_t Entry) {
Evan Cheng81fb5fe2008-07-16 01:33:08 +0000528#if defined(X86_64_JIT)
529 return BB - Entry;
530#else
Evan Cheng2a3e08b2008-01-05 02:26:58 +0000531 return BB - PICBase;
Evan Cheng81fb5fe2008-07-16 01:33:08 +0000532#endif
Evan Cheng2a3e08b2008-01-05 02:26:58 +0000533}
534
Chris Lattner7ddde322004-11-20 23:54:33 +0000535/// relocate - Before the JIT can run a block of code that has been emitted,
536/// it must rewrite the code to contain the actual addresses of any
537/// referenced global symbols.
538void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
Andrew Lenharth908bc862005-07-22 20:49:37 +0000539 unsigned NumRelocs, unsigned char* GOTBase) {
Chris Lattner7ddde322004-11-20 23:54:33 +0000540 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
541 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
542 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
543 switch ((X86::RelocationType)MR->getRelocationType()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000544 case X86::reloc_pcrel_word: {
Chris Lattner7ddde322004-11-20 23:54:33 +0000545 // PC relative relocation, add the relocated value to the value already in
546 // memory, after we adjust it for where the PC is.
Evan Chengaabe38b2007-12-22 09:40:20 +0000547 ResultPtr = ResultPtr -(intptr_t)RelocPos - 4 - MR->getConstantVal();
548 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
549 break;
550 }
551 case X86::reloc_picrel_word: {
552 // PIC base relative relocation, add the relocated value to the value
553 // already in memory, after we adjust it for where the PIC base is.
554 ResultPtr = ResultPtr - ((intptr_t)Function + MR->getConstantVal());
Evan Cheng25ab6902006-09-08 06:48:29 +0000555 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
Chris Lattner7ddde322004-11-20 23:54:33 +0000556 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000557 }
Chris Lattner7ddde322004-11-20 23:54:33 +0000558 case X86::reloc_absolute_word:
Bruno Cardoso Lopese55fef32009-08-05 00:11:21 +0000559 case X86::reloc_absolute_word_sext:
Chris Lattner7ddde322004-11-20 23:54:33 +0000560 // Absolute relocation, just add the relocated value to the value already
561 // in memory.
Evan Cheng25ab6902006-09-08 06:48:29 +0000562 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
Chris Lattner7ddde322004-11-20 23:54:33 +0000563 break;
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000564 case X86::reloc_absolute_dword:
565 *((intptr_t*)RelocPos) += ResultPtr;
566 break;
Chris Lattner7ddde322004-11-20 23:54:33 +0000567 }
568 }
569}
Nicolas Geoffray46fa1392008-10-25 15:41:43 +0000570
571char* X86JITInfo::allocateThreadLocalMemory(size_t size) {
572#if defined(X86_32_JIT) && !defined(__APPLE__) && !defined(_MSC_VER)
573 TLSOffset -= size;
574 return TLSOffset;
575#else
Torok Edwinc23197a2009-07-14 16:55:14 +0000576 llvm_unreachable("Cannot allocate thread local storage on this arch!");
Nicolas Geoffray46fa1392008-10-25 15:41:43 +0000577#endif
578}