blob: 44d8cce054131fd694520e5bc0b99535303d2dfb [file] [log] [blame]
Chris Lattnerb7e72cb2004-11-20 23:54:33 +00001//===-- X86JITInfo.cpp - Implement the JIT interfaces for the X86 target --===//
Misha Brukmanc88330a2005-04-21 23:38:14 +00002//
Chris Lattnerb7e72cb2004-11-20 23:54:33 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanc88330a2005-04-21 23:38:14 +00007//
Chris Lattnerb7e72cb2004-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 Chengafb61042006-10-16 21:01:55 +000017#include "X86Subtarget.h"
Evan Cheng47455a72009-09-03 04:37:05 +000018#include "X86TargetMachine.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Function.h"
Devang Patel78e8a092008-07-16 17:54:34 +000020#include "llvm/Support/Compiler.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000021#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000022#include "llvm/Support/Valgrind.h"
Nate Begeman38724d32005-05-20 21:29:24 +000023#include <cstdlib>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000024#include <cstring>
Chris Lattnerb7e72cb2004-11-20 23:54:33 +000025using namespace llvm;
26
Anton Korobeynikov4733e722008-03-23 13:40:45 +000027// Determine the platform we're running on
Anton Korobeynikovf43ab912009-08-28 16:06:41 +000028#if defined (__x86_64__) || defined (_M_AMD64) || defined (_M_X64)
Anton Korobeynikov4733e722008-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 Lattnerebbfb382006-01-26 19:55:20 +000032#endif
33
Chris Lattnerb7e72cb2004-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 Yasskin3ddd88f2010-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 Lattnerb7e72cb2004-11-20 23:54:33 +000045}
46
47
Chris Lattnerb7e72cb2004-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 Lattner6c003a72006-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 Gohmanaf8f9942009-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 Korobeynikov21ade582007-12-10 23:10:20 +000070// Provide a convenient way for disabling usage of CFI directives.
Anton Korobeynikov77eb5e62007-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 Korobeynikova6b0f7e2007-12-10 23:04:38 +000073#if defined(__APPLE__)
74# define CFI(x)
75#else
Anton Korobeynikovd34112a2007-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 Korobeynikov1b125072007-12-22 20:46:24 +000079# define CFI(x)
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +000080#endif
81
Rafael Espindola9b7d4002013-02-15 14:08:43 +000082// Provide a wrapper for LLVMX86CompilationCallback2 that saves non-traditional
Chris Lattner83a6f102005-05-19 06:49:17 +000083// callee saved registers, for the fastcc calling convention.
Jeff Cohene3948c42005-05-20 01:35:39 +000084extern "C" {
Anton Korobeynikov4733e722008-03-23 13:40:45 +000085#if defined(X86_64_JIT)
86# ifndef _MSC_VER
Evan Cheng11b0a5d2006-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 Lattner6c003a72006-09-08 17:03:56 +000092 ".globl " ASMPREFIX "X86CompilationCallback\n"
Dan Gohmanaf8f9942009-02-06 21:15:52 +000093 TYPE_FUNCTION(X86CompilationCallback)
Chris Lattner6c003a72006-09-08 17:03:56 +000094 ASMPREFIX "X86CompilationCallback:\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +000095 CFI(".cfi_startproc\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +000096 // Save RBP
97 "pushq %rbp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +000098 CFI(".cfi_def_cfa_offset 16\n")
99 CFI(".cfi_offset %rbp, -16\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000100 // Save RSP
101 "movq %rsp, %rbp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000102 CFI(".cfi_def_cfa_register %rbp\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000103 // Save all int arg registers
104 "pushq %rdi\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000105 CFI(".cfi_rel_offset %rdi, 0\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000106 "pushq %rsi\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000107 CFI(".cfi_rel_offset %rsi, 8\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000108 "pushq %rdx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000109 CFI(".cfi_rel_offset %rdx, 16\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000110 "pushq %rcx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000111 CFI(".cfi_rel_offset %rcx, 24\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000112 "pushq %r8\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000113 CFI(".cfi_rel_offset %r8, 32\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000114 "pushq %r9\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000115 CFI(".cfi_rel_offset %r9, 40\n")
Evan Cheng11b0a5d2006-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 Takumib1bbdd82011-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"
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000134 "call " ASMPREFIX "LLVMX86CompilationCallback2\n"
NAKAMURA Takumib1bbdd82011-02-05 15:11:03 +0000135 "addq $32, %rsp\n"
136#else
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000137 "movq %rbp, %rdi\n" // Pass prev frame and return address
138 "movq 8(%rbp), %rsi\n"
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000139 "call " ASMPREFIX "LLVMX86CompilationCallback2\n"
NAKAMURA Takumib1bbdd82011-02-05 15:11:03 +0000140#endif
Evan Cheng11b0a5d2006-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 Michel4a8bc7e2007-12-12 02:38:28 +0000152 CFI(".cfi_def_cfa_register %rsp\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000153 // Restore all int arg registers
154 "subq $48, %rsp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000155 CFI(".cfi_adjust_cfa_offset 48\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000156 "popq %r9\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000157 CFI(".cfi_adjust_cfa_offset -8\n")
158 CFI(".cfi_restore %r9\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000159 "popq %r8\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000160 CFI(".cfi_adjust_cfa_offset -8\n")
161 CFI(".cfi_restore %r8\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000162 "popq %rcx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000163 CFI(".cfi_adjust_cfa_offset -8\n")
164 CFI(".cfi_restore %rcx\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000165 "popq %rdx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000166 CFI(".cfi_adjust_cfa_offset -8\n")
167 CFI(".cfi_restore %rdx\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000168 "popq %rsi\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000169 CFI(".cfi_adjust_cfa_offset -8\n")
170 CFI(".cfi_restore %rsi\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000171 "popq %rdi\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000172 CFI(".cfi_adjust_cfa_offset -8\n")
173 CFI(".cfi_restore %rdi\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000174 // Restore RBP
175 "popq %rbp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000176 CFI(".cfi_adjust_cfa_offset -8\n")
177 CFI(".cfi_restore %rbp\n")
Anton Korobeynikov657be862007-12-10 15:27:07 +0000178 "ret\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000179 CFI(".cfi_endproc\n")
Dan Gohmanaf8f9942009-02-06 21:15:52 +0000180 SIZE(X86CompilationCallback)
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000181 );
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000182# else
Anton Korobeynikov9f0e8202008-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 Korobeynikov4733e722008-03-23 13:40:45 +0000187# endif
188#elif defined (X86_32_JIT)
189# ifndef _MSC_VER
Jeff Cohene3948c42005-05-20 01:35:39 +0000190 void X86CompilationCallback(void);
191 asm(
192 ".text\n"
193 ".align 8\n"
Dan Gohmanaf8f9942009-02-06 21:15:52 +0000194 ".globl " ASMPREFIX "X86CompilationCallback\n"
195 TYPE_FUNCTION(X86CompilationCallback)
Chris Lattner6c003a72006-09-08 17:03:56 +0000196 ASMPREFIX "X86CompilationCallback:\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000197 CFI(".cfi_startproc\n")
Jeff Cohene3948c42005-05-20 01:35:39 +0000198 "pushl %ebp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000199 CFI(".cfi_def_cfa_offset 8\n")
200 CFI(".cfi_offset %ebp, -8\n")
Jeff Cohene3948c42005-05-20 01:35:39 +0000201 "movl %esp, %ebp\n" // Standard prologue
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000202 CFI(".cfi_def_cfa_register %ebp\n")
Jeff Cohene3948c42005-05-20 01:35:39 +0000203 "pushl %eax\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000204 CFI(".cfi_rel_offset %eax, 0\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000205 "pushl %edx\n" // Save EAX/EDX/ECX
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000206 CFI(".cfi_rel_offset %edx, 4\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000207 "pushl %ecx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000208 CFI(".cfi_rel_offset %ecx, 8\n")
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000209# if defined(__APPLE__)
Evan Cheng38c5aee2006-06-24 08:36:10 +0000210 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000211# endif
Evan Cheng28a95492006-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"
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000216 "call " ASMPREFIX "LLVMX86CompilationCallback2\n"
Evan Cheng38c5aee2006-06-24 08:36:10 +0000217 "movl %ebp, %esp\n" // Restore ESP
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000218 CFI(".cfi_def_cfa_register %esp\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000219 "subl $12, %esp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000220 CFI(".cfi_adjust_cfa_offset 12\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000221 "popl %ecx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000222 CFI(".cfi_adjust_cfa_offset -4\n")
223 CFI(".cfi_restore %ecx\n")
Jeff Cohene3948c42005-05-20 01:35:39 +0000224 "popl %edx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000225 CFI(".cfi_adjust_cfa_offset -4\n")
226 CFI(".cfi_restore %edx\n")
Jeff Cohene3948c42005-05-20 01:35:39 +0000227 "popl %eax\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000228 CFI(".cfi_adjust_cfa_offset -4\n")
229 CFI(".cfi_restore %eax\n")
Jeff Cohene3948c42005-05-20 01:35:39 +0000230 "popl %ebp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000231 CFI(".cfi_adjust_cfa_offset -4\n")
232 CFI(".cfi_restore %ebp\n")
Anton Korobeynikov81e9dc42007-12-10 14:54:42 +0000233 "ret\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000234 CFI(".cfi_endproc\n")
Dan Gohmanaf8f9942009-02-06 21:15:52 +0000235 SIZE(X86CompilationCallback)
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000236 );
Evan Chengafb61042006-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 Gohmanaf8f9942009-02-06 21:15:52 +0000243 ".globl " ASMPREFIX "X86CompilationCallback_SSE\n"
244 TYPE_FUNCTION(X86CompilationCallback_SSE)
Evan Chengafb61042006-10-16 21:01:55 +0000245 ASMPREFIX "X86CompilationCallback_SSE:\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000246 CFI(".cfi_startproc\n")
Evan Chengafb61042006-10-16 21:01:55 +0000247 "pushl %ebp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000248 CFI(".cfi_def_cfa_offset 8\n")
249 CFI(".cfi_offset %ebp, -8\n")
Evan Chengafb61042006-10-16 21:01:55 +0000250 "movl %esp, %ebp\n" // Standard prologue
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000251 CFI(".cfi_def_cfa_register %ebp\n")
Evan Chengafb61042006-10-16 21:01:55 +0000252 "pushl %eax\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000253 CFI(".cfi_rel_offset %eax, 0\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000254 "pushl %edx\n" // Save EAX/EDX/ECX
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000255 CFI(".cfi_rel_offset %edx, 4\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000256 "pushl %ecx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000257 CFI(".cfi_rel_offset %ecx, 8\n")
Evan Chengafb61042006-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 Korobeynikov88e9d082007-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 Chengafb61042006-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"
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000272 "call " ASMPREFIX "LLVMX86CompilationCallback2\n"
Evan Chengafb61042006-10-16 21:01:55 +0000273 "addl $16, %esp\n"
274 "movaps 48(%esp), %xmm3\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000275 CFI(".cfi_restore %xmm3\n")
Evan Chengafb61042006-10-16 21:01:55 +0000276 "movaps 32(%esp), %xmm2\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000277 CFI(".cfi_restore %xmm2\n")
Evan Chengafb61042006-10-16 21:01:55 +0000278 "movaps 16(%esp), %xmm1\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000279 CFI(".cfi_restore %xmm1\n")
Evan Chengafb61042006-10-16 21:01:55 +0000280 "movaps (%esp), %xmm0\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000281 CFI(".cfi_restore %xmm0\n")
Evan Chengafb61042006-10-16 21:01:55 +0000282 "movl %ebp, %esp\n" // Restore ESP
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000283 CFI(".cfi_def_cfa_register esp\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000284 "subl $12, %esp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000285 CFI(".cfi_adjust_cfa_offset 12\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000286 "popl %ecx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000287 CFI(".cfi_adjust_cfa_offset -4\n")
288 CFI(".cfi_restore %ecx\n")
Evan Chengafb61042006-10-16 21:01:55 +0000289 "popl %edx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000290 CFI(".cfi_adjust_cfa_offset -4\n")
291 CFI(".cfi_restore %edx\n")
Evan Chengafb61042006-10-16 21:01:55 +0000292 "popl %eax\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000293 CFI(".cfi_adjust_cfa_offset -4\n")
294 CFI(".cfi_restore %eax\n")
Evan Chengafb61042006-10-16 21:01:55 +0000295 "popl %ebp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000296 CFI(".cfi_adjust_cfa_offset -4\n")
297 CFI(".cfi_restore %ebp\n")
Anton Korobeynikov88e9d082007-12-10 15:13:55 +0000298 "ret\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000299 CFI(".cfi_endproc\n")
Dan Gohmanaf8f9942009-02-06 21:15:52 +0000300 SIZE(X86CompilationCallback_SSE)
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000301 );
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000302# else
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000303 void LLVMX86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr);
Jeff Cohene3948c42005-05-20 01:35:39 +0000304
305 _declspec(naked) void X86CompilationCallback(void) {
306 __asm {
Anton Korobeynikovbd472692008-03-23 12:32:54 +0000307 push ebp
308 mov ebp, esp
Jeff Cohene3948c42005-05-20 01:35:39 +0000309 push eax
310 push edx
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000311 push ecx
Anton Korobeynikovbd472692008-03-23 12:32:54 +0000312 and esp, -16
Torok Edwin6e29e9d2010-02-08 08:37:27 +0000313 sub esp, 16
Anton Korobeynikovbd472692008-03-23 12:32:54 +0000314 mov eax, dword ptr [ebp+4]
315 mov dword ptr [esp+4], eax
316 mov dword ptr [esp], ebp
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000317 call LLVMX86CompilationCallback2
Anton Korobeynikovbd472692008-03-23 12:32:54 +0000318 mov esp, ebp
319 sub esp, 12
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000320 pop ecx
Jeff Cohene3948c42005-05-20 01:35:39 +0000321 pop edx
322 pop eax
Anton Korobeynikovbd472692008-03-23 12:32:54 +0000323 pop ebp
Jeff Cohene3948c42005-05-20 01:35:39 +0000324 ret
325 }
326 }
Anton Korobeynikovbd472692008-03-23 12:32:54 +0000327
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000328# endif // _MSC_VER
Chris Lattner83a6f102005-05-19 06:49:17 +0000329
Misha Brukmaneba24712005-05-20 19:46:50 +0000330#else // Not an i386 host
Jeff Cohene3948c42005-05-20 01:35:39 +0000331 void X86CompilationCallback() {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000332 llvm_unreachable("Cannot call X86CompilationCallback() on a non-x86 arch!");
Jeff Cohene3948c42005-05-20 01:35:39 +0000333 }
Chris Lattner83a6f102005-05-19 06:49:17 +0000334#endif
Jeff Cohene3948c42005-05-20 01:35:39 +0000335}
Chris Lattner83a6f102005-05-19 06:49:17 +0000336
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000337/// This is the target-specific function invoked by the
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000338/// function stub when we did not know the real target of a call. This function
339/// must locate the start of the stub or call site and pass it into the JIT
340/// compiler function.
Anton Korobeynikovd2c19c72009-09-06 20:21:48 +0000341extern "C" {
Rafael Espindola1c040b52013-02-19 17:14:33 +0000342LLVM_LIBRARY_VISIBILITY void LLVMX86CompilationCallback2(intptr_t *StackPtr,
Rafael Espindola91cbcbb2013-02-15 14:15:59 +0000343 intptr_t RetAddr) {
Evan Cheng28a95492006-06-29 01:48:36 +0000344 intptr_t *RetAddrLoc = &StackPtr[1];
Evgeniy Stepanov1f5a7142013-02-04 07:03:24 +0000345 // We are reading raw stack data here. Tell MemorySanitizer that it is
346 // sufficiently initialized.
347 __msan_unpoison(RetAddrLoc, sizeof(*RetAddrLoc));
Jeff Cohene3948c42005-05-20 01:35:39 +0000348 assert(*RetAddrLoc == RetAddr &&
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000349 "Could not find return address on the stack!");
350
351 // It's a stub if there is an interrupt marker after the call.
Dale Johannesenc092b762009-09-15 18:32:14 +0000352 bool isStub = ((unsigned char*)RetAddr)[0] == 0xCE;
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000353
354 // The call instruction should have pushed the return value onto the stack...
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000355#if defined (X86_64_JIT)
Evan Chenge1a95202007-03-14 10:44:30 +0000356 RetAddr--; // Backtrack to the reference itself...
357#else
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000358 RetAddr -= 4; // Backtrack to the reference itself...
Evan Chenge1a95202007-03-14 10:44:30 +0000359#endif
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000360
361#if 0
David Greene0688a2422010-01-05 01:29:23 +0000362 DEBUG(dbgs() << "In callback! Addr=" << (void*)RetAddr
Bill Wendling6eecd562009-08-03 00:11:34 +0000363 << " ESP=" << (void*)StackPtr
364 << ": Resolving call to function: "
365 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000366#endif
367
368 // Sanity check to make sure this really is a call instruction.
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000369#if defined (X86_64_JIT)
Evan Chenge1a95202007-03-14 10:44:30 +0000370 assert(((unsigned char*)RetAddr)[-2] == 0x41 &&"Not a call instr!");
371 assert(((unsigned char*)RetAddr)[-1] == 0xFF &&"Not a call instr!");
372#else
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000373 assert(((unsigned char*)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
Evan Chenge1a95202007-03-14 10:44:30 +0000374#endif
Misha Brukmanc88330a2005-04-21 23:38:14 +0000375
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000376 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000377
378 // Rewrite the call target... so that we don't end up here every time we
379 // execute the call.
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000380#if defined (X86_64_JIT)
Jeffrey Yasskin10d36042009-11-16 22:41:33 +0000381 assert(isStub &&
382 "X86-64 doesn't support rewriting non-stub lazy compilation calls:"
383 " the call instruction varies too much.");
Evan Chenge1a95202007-03-14 10:44:30 +0000384#else
385 *(intptr_t *)RetAddr = (intptr_t)(NewVal-RetAddr-4);
386#endif
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000387
388 if (isStub) {
389 // If this is a stub, rewrite the call into an unconditional branch
390 // instruction so that two return addresses are not pushed onto the stack
391 // when the requested function finally gets called. This also makes the
Dale Johannesenc092b762009-09-15 18:32:14 +0000392 // 0xCE byte (interrupt) dead, so the marker doesn't effect anything.
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000393#if defined (X86_64_JIT)
Dale Johannesen40f83ac62008-08-12 23:20:24 +0000394 // If the target address is within 32-bit range of the stub, use a
395 // PC-relative branch instead of loading the actual address. (This is
396 // considerably shorter than the 64-bit immediate load already there.)
397 // We assume here intptr_t is 64 bits.
398 intptr_t diff = NewVal-RetAddr+7;
399 if (diff >= -2147483648LL && diff <= 2147483647LL) {
400 *(unsigned char*)(RetAddr-0xc) = 0xE9;
401 *(intptr_t *)(RetAddr-0xb) = diff & 0xffffffff;
402 } else {
403 *(intptr_t *)(RetAddr - 0xa) = NewVal;
404 ((unsigned char*)RetAddr)[0] = (2 | (4 << 3) | (3 << 6));
405 }
Jeffrey Yasskin3ddd88f2010-03-15 04:57:55 +0000406 sys::ValgrindDiscardTranslations((void*)(RetAddr-0xc), 0xd);
Evan Chenge1a95202007-03-14 10:44:30 +0000407#else
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000408 ((unsigned char*)RetAddr)[-1] = 0xE9;
Jeffrey Yasskin3ddd88f2010-03-15 04:57:55 +0000409 sys::ValgrindDiscardTranslations((void*)(RetAddr-1), 5);
Evan Chenge1a95202007-03-14 10:44:30 +0000410#endif
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000411 }
412
413 // Change the return address to reexecute the call instruction...
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000414#if defined (X86_64_JIT)
Evan Chenge1a95202007-03-14 10:44:30 +0000415 *RetAddrLoc -= 0xd;
416#else
Jeff Cohene3948c42005-05-20 01:35:39 +0000417 *RetAddrLoc -= 5;
Evan Chenge1a95202007-03-14 10:44:30 +0000418#endif
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000419}
Anton Korobeynikovd2c19c72009-09-06 20:21:48 +0000420}
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000421
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000422TargetJITInfo::LazyResolverFn
423X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
Nick Lewycky8fd12542011-12-03 02:45:50 +0000424 TsanIgnoreWritesBegin();
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000425 JITCompilerFunction = F;
Nick Lewycky8fd12542011-12-03 02:45:50 +0000426 TsanIgnoreWritesEnd();
Evan Chengafb61042006-10-16 21:01:55 +0000427
Evan Cheng155deab2009-09-03 05:01:00 +0000428#if defined (X86_32_JIT) && !defined (_MSC_VER)
429 if (Subtarget->hasSSE1())
430 return X86CompilationCallback_SSE;
431#endif
432
433 return X86CompilationCallback;
Evan Cheng47455a72009-09-03 04:37:05 +0000434}
Evan Chengafb61042006-10-16 21:01:55 +0000435
Evan Cheng47455a72009-09-03 04:37:05 +0000436X86JITInfo::X86JITInfo(X86TargetMachine &tm) : TM(tm) {
437 Subtarget = &TM.getSubtarget<X86Subtarget>();
438 useGOT = 0;
439 TLSOffset = 0;
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000440}
441
Evan Cheng9f3058f2008-11-10 01:08:07 +0000442void *X86JITInfo::emitGlobalValueIndirectSym(const GlobalValue* GV, void *ptr,
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000443 JITCodeEmitter &JCE) {
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000444#if defined (X86_64_JIT)
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +0000445 const unsigned Alignment = 8;
446 uint8_t Buffer[8];
447 uint8_t *Cur = Buffer;
448 MachineCodeEmitter::emitWordLEInto(Cur, (unsigned)(intptr_t)ptr);
449 MachineCodeEmitter::emitWordLEInto(Cur, (unsigned)(((intptr_t)ptr) >> 32));
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000450#else
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +0000451 const unsigned Alignment = 4;
452 uint8_t Buffer[4];
453 uint8_t *Cur = Buffer;
454 MachineCodeEmitter::emitWordLEInto(Cur, (intptr_t)ptr);
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000455#endif
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +0000456 return JCE.allocIndirectGV(GV, Buffer, sizeof(Buffer), Alignment);
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000457}
458
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000459TargetJITInfo::StubLayout X86JITInfo::getStubLayout() {
460 // The 64-bit stub contains:
461 // movabs r10 <- 8-byte-target-address # 10 bytes
462 // call|jmp *r10 # 3 bytes
463 // The 32-bit stub contains a 5-byte call|jmp.
464 // If the stub is a call to the compilation callback, an extra byte is added
465 // to mark it as a stub.
466 StubLayout Result = {14, 4};
467 return Result;
468}
469
470void *X86JITInfo::emitFunctionStub(const Function* F, void *Target,
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000471 JITCodeEmitter &JCE) {
NAKAMURA Takumif7f319d2011-02-05 15:10:54 +0000472 // Note, we cast to intptr_t here to silence a -pedantic warning that
Chris Lattnerb47b8a92006-06-01 17:13:10 +0000473 // complains about casting a function pointer to a normal pointer.
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000474#if defined (X86_32_JIT) && !defined (_MSC_VER)
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000475 bool NotCC = (Target != (void*)(intptr_t)X86CompilationCallback &&
476 Target != (void*)(intptr_t)X86CompilationCallback_SSE);
Evan Chenged60d162006-10-16 23:44:08 +0000477#else
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000478 bool NotCC = Target != (void*)(intptr_t)X86CompilationCallback;
Evan Cheng1367ff02006-10-16 22:53:28 +0000479#endif
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000480 JCE.emitAlignment(4);
481 void *Result = (void*)JCE.getCurrentPCValue();
Evan Cheng1367ff02006-10-16 22:53:28 +0000482 if (NotCC) {
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000483#if defined (X86_64_JIT)
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000484 JCE.emitByte(0x49); // REX prefix
485 JCE.emitByte(0xB8+2); // movabsq r10
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000486 JCE.emitWordLE((unsigned)(intptr_t)Target);
487 JCE.emitWordLE((unsigned)(((intptr_t)Target) >> 32));
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000488 JCE.emitByte(0x41); // REX prefix
489 JCE.emitByte(0xFF); // jmpq *r10
490 JCE.emitByte(2 | (4 << 3) | (3 << 6));
Evan Cheng386dfc92007-03-14 10:48:08 +0000491#else
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000492 JCE.emitByte(0xE9);
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000493 JCE.emitWordLE((intptr_t)Target-JCE.getCurrentPCValue()-4);
Evan Cheng386dfc92007-03-14 10:48:08 +0000494#endif
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000495 return Result;
Chris Lattnera76f09d2004-11-22 22:25:30 +0000496 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000497
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000498#if defined (X86_64_JIT)
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000499 JCE.emitByte(0x49); // REX prefix
500 JCE.emitByte(0xB8+2); // movabsq r10
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000501 JCE.emitWordLE((unsigned)(intptr_t)Target);
502 JCE.emitWordLE((unsigned)(((intptr_t)Target) >> 32));
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000503 JCE.emitByte(0x41); // REX prefix
504 JCE.emitByte(0xFF); // callq *r10
505 JCE.emitByte(2 | (2 << 3) | (3 << 6));
Evan Chenge1a95202007-03-14 10:44:30 +0000506#else
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000507 JCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
Chris Lattnera76f09d2004-11-22 22:25:30 +0000508
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000509 JCE.emitWordLE((intptr_t)Target-JCE.getCurrentPCValue()-4);
Evan Chenge1a95202007-03-14 10:44:30 +0000510#endif
Chris Lattnera76f09d2004-11-22 22:25:30 +0000511
Dale Johannesenc092b762009-09-15 18:32:14 +0000512 // This used to use 0xCD, but that value is used by JITMemoryManager to
513 // initialize the buffer with garbage, which means it may follow a
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000514 // noreturn function call, confusing LLVMX86CompilationCallback2. PR 4929.
Dale Johannesenc092b762009-09-15 18:32:14 +0000515 JCE.emitByte(0xCE); // Interrupt - Just a marker identifying the stub!
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000516 return Result;
Nate Begeman18d85e72009-02-18 08:31:02 +0000517}
518
Evan Cheng880b0802008-01-05 02:26:58 +0000519/// getPICJumpTableEntry - Returns the value of the jumptable entry for the
520/// specific basic block.
Evan Cheng0b773192008-12-10 02:32:19 +0000521uintptr_t X86JITInfo::getPICJumpTableEntry(uintptr_t BB, uintptr_t Entry) {
Evan Cheng68bd1542008-07-16 01:33:08 +0000522#if defined(X86_64_JIT)
523 return BB - Entry;
524#else
Evan Cheng880b0802008-01-05 02:26:58 +0000525 return BB - PICBase;
Evan Cheng68bd1542008-07-16 01:33:08 +0000526#endif
Evan Cheng880b0802008-01-05 02:26:58 +0000527}
528
Benjamin Kramer8f5c5de2012-08-29 16:17:01 +0000529template<typename T> static void addUnaligned(void *Pos, T Delta) {
Richard Smith13473852012-08-21 20:48:36 +0000530 T Value;
531 std::memcpy(reinterpret_cast<char*>(&Value), reinterpret_cast<char*>(Pos),
532 sizeof(T));
533 Value += Delta;
534 std::memcpy(reinterpret_cast<char*>(Pos), reinterpret_cast<char*>(&Value),
535 sizeof(T));
536}
537
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000538/// relocate - Before the JIT can run a block of code that has been emitted,
539/// it must rewrite the code to contain the actual addresses of any
540/// referenced global symbols.
541void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
Andrew Lenharth111e5e62005-07-22 20:49:37 +0000542 unsigned NumRelocs, unsigned char* GOTBase) {
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000543 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
544 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
545 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
546 switch ((X86::RelocationType)MR->getRelocationType()) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000547 case X86::reloc_pcrel_word: {
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000548 // PC relative relocation, add the relocated value to the value already in
549 // memory, after we adjust it for where the PC is.
Evan Cheng345a00b2007-12-22 09:40:20 +0000550 ResultPtr = ResultPtr -(intptr_t)RelocPos - 4 - MR->getConstantVal();
Richard Smith13473852012-08-21 20:48:36 +0000551 addUnaligned<unsigned>(RelocPos, ResultPtr);
Evan Cheng345a00b2007-12-22 09:40:20 +0000552 break;
553 }
554 case X86::reloc_picrel_word: {
555 // PIC base relative relocation, add the relocated value to the value
556 // already in memory, after we adjust it for where the PIC base is.
557 ResultPtr = ResultPtr - ((intptr_t)Function + MR->getConstantVal());
Richard Smith13473852012-08-21 20:48:36 +0000558 addUnaligned<unsigned>(RelocPos, ResultPtr);
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000559 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000560 }
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000561 case X86::reloc_absolute_word:
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000562 case X86::reloc_absolute_word_sext:
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000563 // Absolute relocation, just add the relocated value to the value already
564 // in memory.
Richard Smith13473852012-08-21 20:48:36 +0000565 addUnaligned<unsigned>(RelocPos, ResultPtr);
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000566 break;
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000567 case X86::reloc_absolute_dword:
Richard Smith13473852012-08-21 20:48:36 +0000568 addUnaligned<intptr_t>(RelocPos, ResultPtr);
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000569 break;
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000570 }
571 }
572}
Nicolas Geoffray5457ce92008-10-25 15:41:43 +0000573
574char* X86JITInfo::allocateThreadLocalMemory(size_t size) {
575#if defined(X86_32_JIT) && !defined(__APPLE__) && !defined(_MSC_VER)
576 TLSOffset -= size;
577 return TLSOffset;
578#else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000579 llvm_unreachable("Cannot allocate thread local storage on this arch!");
Nicolas Geoffray5457ce92008-10-25 15:41:43 +0000580#endif
581}