blob: a082c4f8b0effc66c0344b321af7230d9034c3df [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
Chris Lattnerb7e72cb2004-11-20 23:54:33 +000014#include "X86JITInfo.h"
15#include "X86Relocations.h"
Evan Chengafb61042006-10-16 21:01:55 +000016#include "X86Subtarget.h"
Evan Cheng47455a72009-09-03 04:37:05 +000017#include "X86TargetMachine.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Function.h"
Devang Patel78e8a092008-07-16 17:54:34 +000019#include "llvm/Support/Compiler.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000020#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000021#include "llvm/Support/Valgrind.h"
Nate Begeman38724d32005-05-20 21:29:24 +000022#include <cstdlib>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000023#include <cstring>
Chris Lattnerb7e72cb2004-11-20 23:54:33 +000024using namespace llvm;
25
Chandler Carruth84e68b22014-04-22 02:41:26 +000026#define DEBUG_TYPE "jit"
27
Anton Korobeynikov4733e722008-03-23 13:40:45 +000028// Determine the platform we're running on
Anton Korobeynikovf43ab912009-08-28 16:06:41 +000029#if defined (__x86_64__) || defined (_M_AMD64) || defined (_M_X64)
Anton Korobeynikov4733e722008-03-23 13:40:45 +000030# define X86_64_JIT
31#elif defined(__i386__) || defined(i386) || defined(_M_IX86)
32# define X86_32_JIT
Chris Lattnerebbfb382006-01-26 19:55:20 +000033#endif
34
Chris Lattnerb7e72cb2004-11-20 23:54:33 +000035void X86JITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
36 unsigned char *OldByte = (unsigned char *)Old;
37 *OldByte++ = 0xE9; // Emit JMP opcode.
38 unsigned *OldWord = (unsigned *)OldByte;
39 unsigned NewAddr = (intptr_t)New;
40 unsigned OldAddr = (intptr_t)OldWord;
41 *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
Jeffrey Yasskin3ddd88f2010-03-15 04:57:55 +000042
43 // X86 doesn't need to invalidate the processor cache, so just invalidate
44 // Valgrind's cache directly.
45 sys::ValgrindDiscardTranslations(Old, 5);
Chris Lattnerb7e72cb2004-11-20 23:54:33 +000046}
47
48
Chris Lattnerb7e72cb2004-11-20 23:54:33 +000049/// JITCompilerFunction - This contains the address of the JIT function used to
50/// compile a function lazily.
51static TargetJITInfo::JITCompilerFn JITCompilerFunction;
52
Chris Lattner6c003a72006-09-08 17:03:56 +000053// Get the ASMPREFIX for the current host. This is often '_'.
54#ifndef __USER_LABEL_PREFIX__
55#define __USER_LABEL_PREFIX__
56#endif
57#define GETASMPREFIX2(X) #X
58#define GETASMPREFIX(X) GETASMPREFIX2(X)
59#define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
60
Dan Gohmanaf8f9942009-02-06 21:15:52 +000061// For ELF targets, use a .size and .type directive, to let tools
62// know the extent of functions defined in assembler.
63#if defined(__ELF__)
64# define SIZE(sym) ".size " #sym ", . - " #sym "\n"
65# define TYPE_FUNCTION(sym) ".type " #sym ", @function\n"
66#else
67# define SIZE(sym)
68# define TYPE_FUNCTION(sym)
69#endif
70
Anton Korobeynikov21ade582007-12-10 23:10:20 +000071// Provide a convenient way for disabling usage of CFI directives.
Anton Korobeynikov77eb5e62007-12-10 23:08:35 +000072// This is needed for old/broken assemblers (for example, gas on
73// Darwin is pretty old and doesn't support these directives)
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +000074#if defined(__APPLE__)
75# define CFI(x)
76#else
Anton Korobeynikovd34112a2007-12-22 20:41:12 +000077// FIXME: Disable this until we really want to use it. Also, we will
78// need to add some workarounds for compilers, which support
79// only subset of these directives.
Anton Korobeynikov1b125072007-12-22 20:46:24 +000080# define CFI(x)
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +000081#endif
82
Rafael Espindola9b7d4002013-02-15 14:08:43 +000083// Provide a wrapper for LLVMX86CompilationCallback2 that saves non-traditional
Chris Lattner83a6f102005-05-19 06:49:17 +000084// callee saved registers, for the fastcc calling convention.
Jeff Cohene3948c42005-05-20 01:35:39 +000085extern "C" {
Anton Korobeynikov4733e722008-03-23 13:40:45 +000086#if defined(X86_64_JIT)
87# ifndef _MSC_VER
Evan Cheng11b0a5d2006-09-08 06:48:29 +000088 // No need to save EAX/EDX for X86-64.
89 void X86CompilationCallback(void);
90 asm(
91 ".text\n"
92 ".align 8\n"
Chris Lattner6c003a72006-09-08 17:03:56 +000093 ".globl " ASMPREFIX "X86CompilationCallback\n"
Dan Gohmanaf8f9942009-02-06 21:15:52 +000094 TYPE_FUNCTION(X86CompilationCallback)
Chris Lattner6c003a72006-09-08 17:03:56 +000095 ASMPREFIX "X86CompilationCallback:\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +000096 CFI(".cfi_startproc\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +000097 // Save RBP
98 "pushq %rbp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +000099 CFI(".cfi_def_cfa_offset 16\n")
100 CFI(".cfi_offset %rbp, -16\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000101 // Save RSP
102 "movq %rsp, %rbp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000103 CFI(".cfi_def_cfa_register %rbp\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000104 // Save all int arg registers
105 "pushq %rdi\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000106 CFI(".cfi_rel_offset %rdi, 0\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000107 "pushq %rsi\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000108 CFI(".cfi_rel_offset %rsi, 8\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000109 "pushq %rdx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000110 CFI(".cfi_rel_offset %rdx, 16\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000111 "pushq %rcx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000112 CFI(".cfi_rel_offset %rcx, 24\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000113 "pushq %r8\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000114 CFI(".cfi_rel_offset %r8, 32\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000115 "pushq %r9\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000116 CFI(".cfi_rel_offset %r9, 40\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000117 // Align stack on 16-byte boundary. ESP might not be properly aligned
118 // (8 byte) if this is called from an indirect stub.
119 "andq $-16, %rsp\n"
120 // Save all XMM arg registers
121 "subq $128, %rsp\n"
122 "movaps %xmm0, (%rsp)\n"
123 "movaps %xmm1, 16(%rsp)\n"
124 "movaps %xmm2, 32(%rsp)\n"
125 "movaps %xmm3, 48(%rsp)\n"
126 "movaps %xmm4, 64(%rsp)\n"
127 "movaps %xmm5, 80(%rsp)\n"
128 "movaps %xmm6, 96(%rsp)\n"
129 "movaps %xmm7, 112(%rsp)\n"
130 // JIT callee
NAKAMURA Takumi19675892013-08-28 03:04:09 +0000131#if defined(_WIN64) || defined(__CYGWIN__)
NAKAMURA Takumib1bbdd82011-02-05 15:11:03 +0000132 "subq $32, %rsp\n"
133 "movq %rbp, %rcx\n" // Pass prev frame and return address
134 "movq 8(%rbp), %rdx\n"
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000135 "call " ASMPREFIX "LLVMX86CompilationCallback2\n"
NAKAMURA Takumib1bbdd82011-02-05 15:11:03 +0000136 "addq $32, %rsp\n"
137#else
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000138 "movq %rbp, %rdi\n" // Pass prev frame and return address
139 "movq 8(%rbp), %rsi\n"
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000140 "call " ASMPREFIX "LLVMX86CompilationCallback2\n"
NAKAMURA Takumib1bbdd82011-02-05 15:11:03 +0000141#endif
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000142 // Restore all XMM arg registers
143 "movaps 112(%rsp), %xmm7\n"
144 "movaps 96(%rsp), %xmm6\n"
145 "movaps 80(%rsp), %xmm5\n"
146 "movaps 64(%rsp), %xmm4\n"
147 "movaps 48(%rsp), %xmm3\n"
148 "movaps 32(%rsp), %xmm2\n"
149 "movaps 16(%rsp), %xmm1\n"
150 "movaps (%rsp), %xmm0\n"
151 // Restore RSP
152 "movq %rbp, %rsp\n"
Scott Michel4a8bc7e2007-12-12 02:38:28 +0000153 CFI(".cfi_def_cfa_register %rsp\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000154 // Restore all int arg registers
155 "subq $48, %rsp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000156 CFI(".cfi_adjust_cfa_offset 48\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000157 "popq %r9\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000158 CFI(".cfi_adjust_cfa_offset -8\n")
159 CFI(".cfi_restore %r9\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000160 "popq %r8\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000161 CFI(".cfi_adjust_cfa_offset -8\n")
162 CFI(".cfi_restore %r8\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000163 "popq %rcx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000164 CFI(".cfi_adjust_cfa_offset -8\n")
165 CFI(".cfi_restore %rcx\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000166 "popq %rdx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000167 CFI(".cfi_adjust_cfa_offset -8\n")
168 CFI(".cfi_restore %rdx\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000169 "popq %rsi\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000170 CFI(".cfi_adjust_cfa_offset -8\n")
171 CFI(".cfi_restore %rsi\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000172 "popq %rdi\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000173 CFI(".cfi_adjust_cfa_offset -8\n")
174 CFI(".cfi_restore %rdi\n")
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000175 // Restore RBP
176 "popq %rbp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000177 CFI(".cfi_adjust_cfa_offset -8\n")
178 CFI(".cfi_restore %rbp\n")
Anton Korobeynikov657be862007-12-10 15:27:07 +0000179 "ret\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000180 CFI(".cfi_endproc\n")
Dan Gohmanaf8f9942009-02-06 21:15:52 +0000181 SIZE(X86CompilationCallback)
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000182 );
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000183# else
Anton Korobeynikov9f0e8202008-03-23 14:44:32 +0000184 // No inline assembler support on this platform. The routine is in external
185 // file.
186 void X86CompilationCallback();
187
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000188# endif
189#elif defined (X86_32_JIT)
190# ifndef _MSC_VER
Jeff Cohene3948c42005-05-20 01:35:39 +0000191 void X86CompilationCallback(void);
192 asm(
193 ".text\n"
194 ".align 8\n"
Dan Gohmanaf8f9942009-02-06 21:15:52 +0000195 ".globl " ASMPREFIX "X86CompilationCallback\n"
196 TYPE_FUNCTION(X86CompilationCallback)
Chris Lattner6c003a72006-09-08 17:03:56 +0000197 ASMPREFIX "X86CompilationCallback:\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000198 CFI(".cfi_startproc\n")
Jeff Cohene3948c42005-05-20 01:35:39 +0000199 "pushl %ebp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000200 CFI(".cfi_def_cfa_offset 8\n")
201 CFI(".cfi_offset %ebp, -8\n")
Jeff Cohene3948c42005-05-20 01:35:39 +0000202 "movl %esp, %ebp\n" // Standard prologue
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000203 CFI(".cfi_def_cfa_register %ebp\n")
Jeff Cohene3948c42005-05-20 01:35:39 +0000204 "pushl %eax\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000205 CFI(".cfi_rel_offset %eax, 0\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000206 "pushl %edx\n" // Save EAX/EDX/ECX
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000207 CFI(".cfi_rel_offset %edx, 4\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000208 "pushl %ecx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000209 CFI(".cfi_rel_offset %ecx, 8\n")
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000210# if defined(__APPLE__)
Evan Cheng38c5aee2006-06-24 08:36:10 +0000211 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000212# endif
Evan Cheng28a95492006-06-29 01:48:36 +0000213 "subl $16, %esp\n"
214 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
215 "movl %eax, 4(%esp)\n"
216 "movl %ebp, (%esp)\n"
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000217 "call " ASMPREFIX "LLVMX86CompilationCallback2\n"
Evan Cheng38c5aee2006-06-24 08:36:10 +0000218 "movl %ebp, %esp\n" // Restore ESP
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000219 CFI(".cfi_def_cfa_register %esp\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000220 "subl $12, %esp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000221 CFI(".cfi_adjust_cfa_offset 12\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000222 "popl %ecx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000223 CFI(".cfi_adjust_cfa_offset -4\n")
224 CFI(".cfi_restore %ecx\n")
Jeff Cohene3948c42005-05-20 01:35:39 +0000225 "popl %edx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000226 CFI(".cfi_adjust_cfa_offset -4\n")
227 CFI(".cfi_restore %edx\n")
Jeff Cohene3948c42005-05-20 01:35:39 +0000228 "popl %eax\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000229 CFI(".cfi_adjust_cfa_offset -4\n")
230 CFI(".cfi_restore %eax\n")
Jeff Cohene3948c42005-05-20 01:35:39 +0000231 "popl %ebp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000232 CFI(".cfi_adjust_cfa_offset -4\n")
233 CFI(".cfi_restore %ebp\n")
Anton Korobeynikov81e9dc42007-12-10 14:54:42 +0000234 "ret\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000235 CFI(".cfi_endproc\n")
Dan Gohmanaf8f9942009-02-06 21:15:52 +0000236 SIZE(X86CompilationCallback)
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000237 );
Evan Chengafb61042006-10-16 21:01:55 +0000238
239 // Same as X86CompilationCallback but also saves XMM argument registers.
240 void X86CompilationCallback_SSE(void);
241 asm(
242 ".text\n"
243 ".align 8\n"
Dan Gohmanaf8f9942009-02-06 21:15:52 +0000244 ".globl " ASMPREFIX "X86CompilationCallback_SSE\n"
245 TYPE_FUNCTION(X86CompilationCallback_SSE)
Evan Chengafb61042006-10-16 21:01:55 +0000246 ASMPREFIX "X86CompilationCallback_SSE:\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000247 CFI(".cfi_startproc\n")
Evan Chengafb61042006-10-16 21:01:55 +0000248 "pushl %ebp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000249 CFI(".cfi_def_cfa_offset 8\n")
250 CFI(".cfi_offset %ebp, -8\n")
Evan Chengafb61042006-10-16 21:01:55 +0000251 "movl %esp, %ebp\n" // Standard prologue
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000252 CFI(".cfi_def_cfa_register %ebp\n")
Evan Chengafb61042006-10-16 21:01:55 +0000253 "pushl %eax\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000254 CFI(".cfi_rel_offset %eax, 0\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000255 "pushl %edx\n" // Save EAX/EDX/ECX
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000256 CFI(".cfi_rel_offset %edx, 4\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000257 "pushl %ecx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000258 CFI(".cfi_rel_offset %ecx, 8\n")
Evan Chengafb61042006-10-16 21:01:55 +0000259 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
260 // Save all XMM arg registers
261 "subl $64, %esp\n"
Anton Korobeynikov88e9d082007-12-10 15:13:55 +0000262 // FIXME: provide frame move information for xmm registers.
263 // This can be tricky, because CFA register is ebp (unaligned)
264 // and we need to produce offsets relative to it.
Evan Chengafb61042006-10-16 21:01:55 +0000265 "movaps %xmm0, (%esp)\n"
266 "movaps %xmm1, 16(%esp)\n"
267 "movaps %xmm2, 32(%esp)\n"
268 "movaps %xmm3, 48(%esp)\n"
269 "subl $16, %esp\n"
270 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
271 "movl %eax, 4(%esp)\n"
272 "movl %ebp, (%esp)\n"
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000273 "call " ASMPREFIX "LLVMX86CompilationCallback2\n"
Evan Chengafb61042006-10-16 21:01:55 +0000274 "addl $16, %esp\n"
275 "movaps 48(%esp), %xmm3\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000276 CFI(".cfi_restore %xmm3\n")
Evan Chengafb61042006-10-16 21:01:55 +0000277 "movaps 32(%esp), %xmm2\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000278 CFI(".cfi_restore %xmm2\n")
Evan Chengafb61042006-10-16 21:01:55 +0000279 "movaps 16(%esp), %xmm1\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000280 CFI(".cfi_restore %xmm1\n")
Evan Chengafb61042006-10-16 21:01:55 +0000281 "movaps (%esp), %xmm0\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000282 CFI(".cfi_restore %xmm0\n")
Evan Chengafb61042006-10-16 21:01:55 +0000283 "movl %ebp, %esp\n" // Restore ESP
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000284 CFI(".cfi_def_cfa_register esp\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000285 "subl $12, %esp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000286 CFI(".cfi_adjust_cfa_offset 12\n")
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000287 "popl %ecx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000288 CFI(".cfi_adjust_cfa_offset -4\n")
289 CFI(".cfi_restore %ecx\n")
Evan Chengafb61042006-10-16 21:01:55 +0000290 "popl %edx\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000291 CFI(".cfi_adjust_cfa_offset -4\n")
292 CFI(".cfi_restore %edx\n")
Evan Chengafb61042006-10-16 21:01:55 +0000293 "popl %eax\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000294 CFI(".cfi_adjust_cfa_offset -4\n")
295 CFI(".cfi_restore %eax\n")
Evan Chengafb61042006-10-16 21:01:55 +0000296 "popl %ebp\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000297 CFI(".cfi_adjust_cfa_offset -4\n")
298 CFI(".cfi_restore %ebp\n")
Anton Korobeynikov88e9d082007-12-10 15:13:55 +0000299 "ret\n"
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000300 CFI(".cfi_endproc\n")
Dan Gohmanaf8f9942009-02-06 21:15:52 +0000301 SIZE(X86CompilationCallback_SSE)
Anton Korobeynikova6b0f7e2007-12-10 23:04:38 +0000302 );
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000303# else
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000304 void LLVMX86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr);
Jeff Cohene3948c42005-05-20 01:35:39 +0000305
306 _declspec(naked) void X86CompilationCallback(void) {
307 __asm {
Anton Korobeynikovbd472692008-03-23 12:32:54 +0000308 push ebp
309 mov ebp, esp
Jeff Cohene3948c42005-05-20 01:35:39 +0000310 push eax
311 push edx
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000312 push ecx
Anton Korobeynikovbd472692008-03-23 12:32:54 +0000313 and esp, -16
Torok Edwin6e29e9d2010-02-08 08:37:27 +0000314 sub esp, 16
Anton Korobeynikovbd472692008-03-23 12:32:54 +0000315 mov eax, dword ptr [ebp+4]
316 mov dword ptr [esp+4], eax
317 mov dword ptr [esp], ebp
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000318 call LLVMX86CompilationCallback2
Anton Korobeynikovbd472692008-03-23 12:32:54 +0000319 mov esp, ebp
320 sub esp, 12
Anton Korobeynikovb8d8b822007-01-29 21:28:01 +0000321 pop ecx
Jeff Cohene3948c42005-05-20 01:35:39 +0000322 pop edx
323 pop eax
Anton Korobeynikovbd472692008-03-23 12:32:54 +0000324 pop ebp
Jeff Cohene3948c42005-05-20 01:35:39 +0000325 ret
326 }
327 }
Anton Korobeynikovbd472692008-03-23 12:32:54 +0000328
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000329# endif // _MSC_VER
Chris Lattner83a6f102005-05-19 06:49:17 +0000330
Misha Brukmaneba24712005-05-20 19:46:50 +0000331#else // Not an i386 host
Jeff Cohene3948c42005-05-20 01:35:39 +0000332 void X86CompilationCallback() {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000333 llvm_unreachable("Cannot call X86CompilationCallback() on a non-x86 arch!");
Jeff Cohene3948c42005-05-20 01:35:39 +0000334 }
Chris Lattner83a6f102005-05-19 06:49:17 +0000335#endif
Jeff Cohene3948c42005-05-20 01:35:39 +0000336}
Chris Lattner83a6f102005-05-19 06:49:17 +0000337
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000338/// This is the target-specific function invoked by the
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000339/// function stub when we did not know the real target of a call. This function
340/// must locate the start of the stub or call site and pass it into the JIT
341/// compiler function.
Anton Korobeynikovd2c19c72009-09-06 20:21:48 +0000342extern "C" {
Patrik Hagglund0cc888b2013-06-12 08:45:39 +0000343LLVM_ATTRIBUTE_USED // Referenced from inline asm.
Rafael Espindola1c040b52013-02-19 17:14:33 +0000344LLVM_LIBRARY_VISIBILITY void LLVMX86CompilationCallback2(intptr_t *StackPtr,
Rafael Espindola91cbcbb2013-02-15 14:15:59 +0000345 intptr_t RetAddr) {
Evan Cheng28a95492006-06-29 01:48:36 +0000346 intptr_t *RetAddrLoc = &StackPtr[1];
Evgeniy Stepanov1f5a7142013-02-04 07:03:24 +0000347 // We are reading raw stack data here. Tell MemorySanitizer that it is
348 // sufficiently initialized.
349 __msan_unpoison(RetAddrLoc, sizeof(*RetAddrLoc));
Jeff Cohene3948c42005-05-20 01:35:39 +0000350 assert(*RetAddrLoc == RetAddr &&
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000351 "Could not find return address on the stack!");
352
353 // It's a stub if there is an interrupt marker after the call.
Dale Johannesenc092b762009-09-15 18:32:14 +0000354 bool isStub = ((unsigned char*)RetAddr)[0] == 0xCE;
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000355
356 // The call instruction should have pushed the return value onto the stack...
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000357#if defined (X86_64_JIT)
Evan Chenge1a95202007-03-14 10:44:30 +0000358 RetAddr--; // Backtrack to the reference itself...
359#else
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000360 RetAddr -= 4; // Backtrack to the reference itself...
Evan Chenge1a95202007-03-14 10:44:30 +0000361#endif
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000362
363#if 0
David Greene0688a2422010-01-05 01:29:23 +0000364 DEBUG(dbgs() << "In callback! Addr=" << (void*)RetAddr
Bill Wendling6eecd562009-08-03 00:11:34 +0000365 << " ESP=" << (void*)StackPtr
366 << ": Resolving call to function: "
367 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000368#endif
369
370 // Sanity check to make sure this really is a call instruction.
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000371#if defined (X86_64_JIT)
Evan Chenge1a95202007-03-14 10:44:30 +0000372 assert(((unsigned char*)RetAddr)[-2] == 0x41 &&"Not a call instr!");
373 assert(((unsigned char*)RetAddr)[-1] == 0xFF &&"Not a call instr!");
374#else
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000375 assert(((unsigned char*)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
Evan Chenge1a95202007-03-14 10:44:30 +0000376#endif
Misha Brukmanc88330a2005-04-21 23:38:14 +0000377
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000378 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000379
380 // Rewrite the call target... so that we don't end up here every time we
381 // execute the call.
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000382#if defined (X86_64_JIT)
Jeffrey Yasskin10d36042009-11-16 22:41:33 +0000383 assert(isStub &&
384 "X86-64 doesn't support rewriting non-stub lazy compilation calls:"
385 " the call instruction varies too much.");
Evan Chenge1a95202007-03-14 10:44:30 +0000386#else
387 *(intptr_t *)RetAddr = (intptr_t)(NewVal-RetAddr-4);
388#endif
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000389
390 if (isStub) {
391 // If this is a stub, rewrite the call into an unconditional branch
392 // instruction so that two return addresses are not pushed onto the stack
393 // when the requested function finally gets called. This also makes the
Dale Johannesenc092b762009-09-15 18:32:14 +0000394 // 0xCE byte (interrupt) dead, so the marker doesn't effect anything.
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000395#if defined (X86_64_JIT)
Dale Johannesen40f83ac62008-08-12 23:20:24 +0000396 // If the target address is within 32-bit range of the stub, use a
397 // PC-relative branch instead of loading the actual address. (This is
398 // considerably shorter than the 64-bit immediate load already there.)
399 // We assume here intptr_t is 64 bits.
400 intptr_t diff = NewVal-RetAddr+7;
401 if (diff >= -2147483648LL && diff <= 2147483647LL) {
402 *(unsigned char*)(RetAddr-0xc) = 0xE9;
403 *(intptr_t *)(RetAddr-0xb) = diff & 0xffffffff;
404 } else {
405 *(intptr_t *)(RetAddr - 0xa) = NewVal;
406 ((unsigned char*)RetAddr)[0] = (2 | (4 << 3) | (3 << 6));
407 }
Jeffrey Yasskin3ddd88f2010-03-15 04:57:55 +0000408 sys::ValgrindDiscardTranslations((void*)(RetAddr-0xc), 0xd);
Evan Chenge1a95202007-03-14 10:44:30 +0000409#else
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000410 ((unsigned char*)RetAddr)[-1] = 0xE9;
Jeffrey Yasskin3ddd88f2010-03-15 04:57:55 +0000411 sys::ValgrindDiscardTranslations((void*)(RetAddr-1), 5);
Evan Chenge1a95202007-03-14 10:44:30 +0000412#endif
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000413 }
414
415 // Change the return address to reexecute the call instruction...
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000416#if defined (X86_64_JIT)
Evan Chenge1a95202007-03-14 10:44:30 +0000417 *RetAddrLoc -= 0xd;
418#else
Jeff Cohene3948c42005-05-20 01:35:39 +0000419 *RetAddrLoc -= 5;
Evan Chenge1a95202007-03-14 10:44:30 +0000420#endif
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000421}
Anton Korobeynikovd2c19c72009-09-06 20:21:48 +0000422}
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000423
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000424TargetJITInfo::LazyResolverFn
425X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
Nick Lewycky8fd12542011-12-03 02:45:50 +0000426 TsanIgnoreWritesBegin();
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000427 JITCompilerFunction = F;
Nick Lewycky8fd12542011-12-03 02:45:50 +0000428 TsanIgnoreWritesEnd();
Evan Chengafb61042006-10-16 21:01:55 +0000429
NAKAMURA Takumidf727642014-04-15 08:28:23 +0000430#if defined (X86_32_JIT) && !defined (_MSC_VER)
431#if defined(__SSE__)
432 // SSE Callback should be called for SSE-enabled LLVM.
NAKAMURA Takumi33ec29a2014-04-15 04:12:21 +0000433 return X86CompilationCallback_SSE;
NAKAMURA Takumidf727642014-04-15 08:28:23 +0000434#else
Eric Christopher28783da2014-06-06 23:26:48 +0000435 if (useSSE)
NAKAMURA Takumidf727642014-04-15 08:28:23 +0000436 return X86CompilationCallback_SSE;
437#endif
Evan Cheng155deab2009-09-03 05:01:00 +0000438#endif
439
440 return X86CompilationCallback;
Evan Cheng47455a72009-09-03 04:37:05 +0000441}
Evan Chengafb61042006-10-16 21:01:55 +0000442
Eric Christopher28783da2014-06-06 23:26:48 +0000443X86JITInfo::X86JITInfo(bool UseSSE) {
444 useSSE = UseSSE;
Evan Cheng47455a72009-09-03 04:37:05 +0000445 useGOT = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000446 TLSOffset = nullptr;
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000447}
448
Evan Cheng9f3058f2008-11-10 01:08:07 +0000449void *X86JITInfo::emitGlobalValueIndirectSym(const GlobalValue* GV, void *ptr,
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000450 JITCodeEmitter &JCE) {
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000451#if defined (X86_64_JIT)
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +0000452 const unsigned Alignment = 8;
453 uint8_t Buffer[8];
454 uint8_t *Cur = Buffer;
455 MachineCodeEmitter::emitWordLEInto(Cur, (unsigned)(intptr_t)ptr);
456 MachineCodeEmitter::emitWordLEInto(Cur, (unsigned)(((intptr_t)ptr) >> 32));
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000457#else
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +0000458 const unsigned Alignment = 4;
459 uint8_t Buffer[4];
460 uint8_t *Cur = Buffer;
461 MachineCodeEmitter::emitWordLEInto(Cur, (intptr_t)ptr);
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000462#endif
Jeffrey Yasskine0d8e142009-12-15 22:42:46 +0000463 return JCE.allocIndirectGV(GV, Buffer, sizeof(Buffer), Alignment);
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000464}
465
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000466TargetJITInfo::StubLayout X86JITInfo::getStubLayout() {
467 // The 64-bit stub contains:
468 // movabs r10 <- 8-byte-target-address # 10 bytes
469 // call|jmp *r10 # 3 bytes
470 // The 32-bit stub contains a 5-byte call|jmp.
471 // If the stub is a call to the compilation callback, an extra byte is added
472 // to mark it as a stub.
473 StubLayout Result = {14, 4};
474 return Result;
475}
476
477void *X86JITInfo::emitFunctionStub(const Function* F, void *Target,
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000478 JITCodeEmitter &JCE) {
NAKAMURA Takumif7f319d2011-02-05 15:10:54 +0000479 // Note, we cast to intptr_t here to silence a -pedantic warning that
Chris Lattnerb47b8a92006-06-01 17:13:10 +0000480 // complains about casting a function pointer to a normal pointer.
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000481#if defined (X86_32_JIT) && !defined (_MSC_VER)
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000482 bool NotCC = (Target != (void*)(intptr_t)X86CompilationCallback &&
483 Target != (void*)(intptr_t)X86CompilationCallback_SSE);
Evan Chenged60d162006-10-16 23:44:08 +0000484#else
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000485 bool NotCC = Target != (void*)(intptr_t)X86CompilationCallback;
Evan Cheng1367ff02006-10-16 22:53:28 +0000486#endif
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000487 JCE.emitAlignment(4);
488 void *Result = (void*)JCE.getCurrentPCValue();
Evan Cheng1367ff02006-10-16 22:53:28 +0000489 if (NotCC) {
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000490#if defined (X86_64_JIT)
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000491 JCE.emitByte(0x49); // REX prefix
492 JCE.emitByte(0xB8+2); // movabsq r10
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000493 JCE.emitWordLE((unsigned)(intptr_t)Target);
494 JCE.emitWordLE((unsigned)(((intptr_t)Target) >> 32));
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000495 JCE.emitByte(0x41); // REX prefix
496 JCE.emitByte(0xFF); // jmpq *r10
497 JCE.emitByte(2 | (4 << 3) | (3 << 6));
Evan Cheng386dfc92007-03-14 10:48:08 +0000498#else
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000499 JCE.emitByte(0xE9);
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000500 JCE.emitWordLE((intptr_t)Target-JCE.getCurrentPCValue()-4);
Evan Cheng386dfc92007-03-14 10:48:08 +0000501#endif
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000502 return Result;
Chris Lattnera76f09d2004-11-22 22:25:30 +0000503 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000504
Anton Korobeynikov4733e722008-03-23 13:40:45 +0000505#if defined (X86_64_JIT)
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000506 JCE.emitByte(0x49); // REX prefix
507 JCE.emitByte(0xB8+2); // movabsq r10
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000508 JCE.emitWordLE((unsigned)(intptr_t)Target);
509 JCE.emitWordLE((unsigned)(((intptr_t)Target) >> 32));
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000510 JCE.emitByte(0x41); // REX prefix
511 JCE.emitByte(0xFF); // callq *r10
512 JCE.emitByte(2 | (2 << 3) | (3 << 6));
Evan Chenge1a95202007-03-14 10:44:30 +0000513#else
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000514 JCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
Chris Lattnera76f09d2004-11-22 22:25:30 +0000515
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000516 JCE.emitWordLE((intptr_t)Target-JCE.getCurrentPCValue()-4);
Evan Chenge1a95202007-03-14 10:44:30 +0000517#endif
Chris Lattnera76f09d2004-11-22 22:25:30 +0000518
Dale Johannesenc092b762009-09-15 18:32:14 +0000519 // This used to use 0xCD, but that value is used by JITMemoryManager to
520 // initialize the buffer with garbage, which means it may follow a
Rafael Espindola9b7d4002013-02-15 14:08:43 +0000521 // noreturn function call, confusing LLVMX86CompilationCallback2. PR 4929.
Dale Johannesenc092b762009-09-15 18:32:14 +0000522 JCE.emitByte(0xCE); // Interrupt - Just a marker identifying the stub!
Jeffrey Yasskinf2ad5712009-11-23 23:35:19 +0000523 return Result;
Nate Begeman18d85e72009-02-18 08:31:02 +0000524}
525
Evan Cheng880b0802008-01-05 02:26:58 +0000526/// getPICJumpTableEntry - Returns the value of the jumptable entry for the
527/// specific basic block.
Evan Cheng0b773192008-12-10 02:32:19 +0000528uintptr_t X86JITInfo::getPICJumpTableEntry(uintptr_t BB, uintptr_t Entry) {
Evan Cheng68bd1542008-07-16 01:33:08 +0000529#if defined(X86_64_JIT)
530 return BB - Entry;
531#else
Evan Cheng880b0802008-01-05 02:26:58 +0000532 return BB - PICBase;
Evan Cheng68bd1542008-07-16 01:33:08 +0000533#endif
Evan Cheng880b0802008-01-05 02:26:58 +0000534}
535
Benjamin Kramer8f5c5de2012-08-29 16:17:01 +0000536template<typename T> static void addUnaligned(void *Pos, T Delta) {
Richard Smith13473852012-08-21 20:48:36 +0000537 T Value;
538 std::memcpy(reinterpret_cast<char*>(&Value), reinterpret_cast<char*>(Pos),
539 sizeof(T));
540 Value += Delta;
541 std::memcpy(reinterpret_cast<char*>(Pos), reinterpret_cast<char*>(&Value),
542 sizeof(T));
543}
544
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000545/// relocate - Before the JIT can run a block of code that has been emitted,
546/// it must rewrite the code to contain the actual addresses of any
547/// referenced global symbols.
548void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
Andrew Lenharth111e5e62005-07-22 20:49:37 +0000549 unsigned NumRelocs, unsigned char* GOTBase) {
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000550 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
551 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
552 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
553 switch ((X86::RelocationType)MR->getRelocationType()) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000554 case X86::reloc_pcrel_word: {
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000555 // PC relative relocation, add the relocated value to the value already in
556 // memory, after we adjust it for where the PC is.
Evan Cheng345a00b2007-12-22 09:40:20 +0000557 ResultPtr = ResultPtr -(intptr_t)RelocPos - 4 - MR->getConstantVal();
Richard Smith13473852012-08-21 20:48:36 +0000558 addUnaligned<unsigned>(RelocPos, ResultPtr);
Evan Cheng345a00b2007-12-22 09:40:20 +0000559 break;
560 }
561 case X86::reloc_picrel_word: {
562 // PIC base relative relocation, add the relocated value to the value
563 // already in memory, after we adjust it for where the PIC base is.
564 ResultPtr = ResultPtr - ((intptr_t)Function + MR->getConstantVal());
Richard Smith13473852012-08-21 20:48:36 +0000565 addUnaligned<unsigned>(RelocPos, ResultPtr);
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000566 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000567 }
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000568 case X86::reloc_absolute_word:
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000569 case X86::reloc_absolute_word_sext:
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000570 // Absolute relocation, just add the relocated value to the value already
571 // in memory.
Richard Smith13473852012-08-21 20:48:36 +0000572 addUnaligned<unsigned>(RelocPos, ResultPtr);
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000573 break;
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000574 case X86::reloc_absolute_dword:
Richard Smith13473852012-08-21 20:48:36 +0000575 addUnaligned<intptr_t>(RelocPos, ResultPtr);
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000576 break;
Chris Lattnerb7e72cb2004-11-20 23:54:33 +0000577 }
578 }
579}
Nicolas Geoffray5457ce92008-10-25 15:41:43 +0000580
581char* X86JITInfo::allocateThreadLocalMemory(size_t size) {
582#if defined(X86_32_JIT) && !defined(__APPLE__) && !defined(_MSC_VER)
583 TLSOffset -= size;
584 return TLSOffset;
585#else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000586 llvm_unreachable("Cannot allocate thread local storage on this arch!");
Nicolas Geoffray5457ce92008-10-25 15:41:43 +0000587#endif
588}