blob: 9a07700b930b601b4cf191792d18f708e75025e7 [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
130 "movq %rbp, %rdi\n" // Pass prev frame and return address
131 "movq 8(%rbp), %rsi\n"
Anton Korobeynikov6c402572009-09-06 20:21:48 +0000132 "call " ASMPREFIX "X86CompilationCallback2\n"
Evan Cheng25ab6902006-09-08 06:48:29 +0000133 // Restore all XMM arg registers
134 "movaps 112(%rsp), %xmm7\n"
135 "movaps 96(%rsp), %xmm6\n"
136 "movaps 80(%rsp), %xmm5\n"
137 "movaps 64(%rsp), %xmm4\n"
138 "movaps 48(%rsp), %xmm3\n"
139 "movaps 32(%rsp), %xmm2\n"
140 "movaps 16(%rsp), %xmm1\n"
141 "movaps (%rsp), %xmm0\n"
142 // Restore RSP
143 "movq %rbp, %rsp\n"
Scott Michela28c6bf2007-12-12 02:38:28 +0000144 CFI(".cfi_def_cfa_register %rsp\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000145 // Restore all int arg registers
146 "subq $48, %rsp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000147 CFI(".cfi_adjust_cfa_offset 48\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000148 "popq %r9\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000149 CFI(".cfi_adjust_cfa_offset -8\n")
150 CFI(".cfi_restore %r9\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000151 "popq %r8\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000152 CFI(".cfi_adjust_cfa_offset -8\n")
153 CFI(".cfi_restore %r8\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000154 "popq %rcx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000155 CFI(".cfi_adjust_cfa_offset -8\n")
156 CFI(".cfi_restore %rcx\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000157 "popq %rdx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000158 CFI(".cfi_adjust_cfa_offset -8\n")
159 CFI(".cfi_restore %rdx\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000160 "popq %rsi\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000161 CFI(".cfi_adjust_cfa_offset -8\n")
162 CFI(".cfi_restore %rsi\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000163 "popq %rdi\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000164 CFI(".cfi_adjust_cfa_offset -8\n")
165 CFI(".cfi_restore %rdi\n")
Evan Cheng25ab6902006-09-08 06:48:29 +0000166 // Restore RBP
167 "popq %rbp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000168 CFI(".cfi_adjust_cfa_offset -8\n")
169 CFI(".cfi_restore %rbp\n")
Anton Korobeynikov3a7bcc42007-12-10 15:27:07 +0000170 "ret\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000171 CFI(".cfi_endproc\n")
Dan Gohman9036d802009-02-06 21:15:52 +0000172 SIZE(X86CompilationCallback)
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000173 );
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000174# else
Anton Korobeynikov231e9642008-03-23 14:44:32 +0000175 // No inline assembler support on this platform. The routine is in external
176 // file.
177 void X86CompilationCallback();
178
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000179# endif
180#elif defined (X86_32_JIT)
181# ifndef _MSC_VER
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000182 void X86CompilationCallback(void);
183 asm(
184 ".text\n"
185 ".align 8\n"
Dan Gohman9036d802009-02-06 21:15:52 +0000186 ".globl " ASMPREFIX "X86CompilationCallback\n"
187 TYPE_FUNCTION(X86CompilationCallback)
Chris Lattner40f4ba52006-09-08 17:03:56 +0000188 ASMPREFIX "X86CompilationCallback:\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000189 CFI(".cfi_startproc\n")
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000190 "pushl %ebp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000191 CFI(".cfi_def_cfa_offset 8\n")
192 CFI(".cfi_offset %ebp, -8\n")
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000193 "movl %esp, %ebp\n" // Standard prologue
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000194 CFI(".cfi_def_cfa_register %ebp\n")
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000195 "pushl %eax\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000196 CFI(".cfi_rel_offset %eax, 0\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000197 "pushl %edx\n" // Save EAX/EDX/ECX
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000198 CFI(".cfi_rel_offset %edx, 4\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000199 "pushl %ecx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000200 CFI(".cfi_rel_offset %ecx, 8\n")
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000201# if defined(__APPLE__)
Evan Chengda08d2c2006-06-24 08:36:10 +0000202 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000203# endif
Evan Chengbe33dd92006-06-29 01:48:36 +0000204 "subl $16, %esp\n"
205 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
206 "movl %eax, 4(%esp)\n"
207 "movl %ebp, (%esp)\n"
Anton Korobeynikov6c402572009-09-06 20:21:48 +0000208 "call " ASMPREFIX "X86CompilationCallback2\n"
Evan Chengda08d2c2006-06-24 08:36:10 +0000209 "movl %ebp, %esp\n" // Restore ESP
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000210 CFI(".cfi_def_cfa_register %esp\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000211 "subl $12, %esp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000212 CFI(".cfi_adjust_cfa_offset 12\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000213 "popl %ecx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000214 CFI(".cfi_adjust_cfa_offset -4\n")
215 CFI(".cfi_restore %ecx\n")
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000216 "popl %edx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000217 CFI(".cfi_adjust_cfa_offset -4\n")
218 CFI(".cfi_restore %edx\n")
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000219 "popl %eax\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000220 CFI(".cfi_adjust_cfa_offset -4\n")
221 CFI(".cfi_restore %eax\n")
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000222 "popl %ebp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000223 CFI(".cfi_adjust_cfa_offset -4\n")
224 CFI(".cfi_restore %ebp\n")
Anton Korobeynikova14b6692007-12-10 14:54:42 +0000225 "ret\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000226 CFI(".cfi_endproc\n")
Dan Gohman9036d802009-02-06 21:15:52 +0000227 SIZE(X86CompilationCallback)
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000228 );
Evan Cheng93b11f82006-10-16 21:01:55 +0000229
230 // Same as X86CompilationCallback but also saves XMM argument registers.
231 void X86CompilationCallback_SSE(void);
232 asm(
233 ".text\n"
234 ".align 8\n"
Dan Gohman9036d802009-02-06 21:15:52 +0000235 ".globl " ASMPREFIX "X86CompilationCallback_SSE\n"
236 TYPE_FUNCTION(X86CompilationCallback_SSE)
Evan Cheng93b11f82006-10-16 21:01:55 +0000237 ASMPREFIX "X86CompilationCallback_SSE:\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000238 CFI(".cfi_startproc\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000239 "pushl %ebp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000240 CFI(".cfi_def_cfa_offset 8\n")
241 CFI(".cfi_offset %ebp, -8\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000242 "movl %esp, %ebp\n" // Standard prologue
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000243 CFI(".cfi_def_cfa_register %ebp\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000244 "pushl %eax\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000245 CFI(".cfi_rel_offset %eax, 0\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000246 "pushl %edx\n" // Save EAX/EDX/ECX
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000247 CFI(".cfi_rel_offset %edx, 4\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000248 "pushl %ecx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000249 CFI(".cfi_rel_offset %ecx, 8\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000250 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
251 // Save all XMM arg registers
252 "subl $64, %esp\n"
Anton Korobeynikovdf7814c2007-12-10 15:13:55 +0000253 // FIXME: provide frame move information for xmm registers.
254 // This can be tricky, because CFA register is ebp (unaligned)
255 // and we need to produce offsets relative to it.
Evan Cheng93b11f82006-10-16 21:01:55 +0000256 "movaps %xmm0, (%esp)\n"
257 "movaps %xmm1, 16(%esp)\n"
258 "movaps %xmm2, 32(%esp)\n"
259 "movaps %xmm3, 48(%esp)\n"
260 "subl $16, %esp\n"
261 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
262 "movl %eax, 4(%esp)\n"
263 "movl %ebp, (%esp)\n"
Anton Korobeynikov6c402572009-09-06 20:21:48 +0000264 "call " ASMPREFIX "X86CompilationCallback2\n"
Evan Cheng93b11f82006-10-16 21:01:55 +0000265 "addl $16, %esp\n"
266 "movaps 48(%esp), %xmm3\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000267 CFI(".cfi_restore %xmm3\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000268 "movaps 32(%esp), %xmm2\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000269 CFI(".cfi_restore %xmm2\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000270 "movaps 16(%esp), %xmm1\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000271 CFI(".cfi_restore %xmm1\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000272 "movaps (%esp), %xmm0\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000273 CFI(".cfi_restore %xmm0\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000274 "movl %ebp, %esp\n" // Restore ESP
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000275 CFI(".cfi_def_cfa_register esp\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000276 "subl $12, %esp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000277 CFI(".cfi_adjust_cfa_offset 12\n")
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000278 "popl %ecx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000279 CFI(".cfi_adjust_cfa_offset -4\n")
280 CFI(".cfi_restore %ecx\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000281 "popl %edx\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000282 CFI(".cfi_adjust_cfa_offset -4\n")
283 CFI(".cfi_restore %edx\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000284 "popl %eax\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000285 CFI(".cfi_adjust_cfa_offset -4\n")
286 CFI(".cfi_restore %eax\n")
Evan Cheng93b11f82006-10-16 21:01:55 +0000287 "popl %ebp\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000288 CFI(".cfi_adjust_cfa_offset -4\n")
289 CFI(".cfi_restore %ebp\n")
Anton Korobeynikovdf7814c2007-12-10 15:13:55 +0000290 "ret\n"
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000291 CFI(".cfi_endproc\n")
Dan Gohman9036d802009-02-06 21:15:52 +0000292 SIZE(X86CompilationCallback_SSE)
Anton Korobeynikov5f682872007-12-10 23:04:38 +0000293 );
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000294# else
Anton Korobeynikov6aeedfd2008-03-23 12:32:54 +0000295 void X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr);
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000296
297 _declspec(naked) void X86CompilationCallback(void) {
298 __asm {
Anton Korobeynikov6aeedfd2008-03-23 12:32:54 +0000299 push ebp
300 mov ebp, esp
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000301 push eax
302 push edx
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000303 push ecx
Anton Korobeynikov6aeedfd2008-03-23 12:32:54 +0000304 and esp, -16
Torok Edwinacf2a652010-02-08 08:37:27 +0000305 sub esp, 16
Anton Korobeynikov6aeedfd2008-03-23 12:32:54 +0000306 mov eax, dword ptr [ebp+4]
307 mov dword ptr [esp+4], eax
308 mov dword ptr [esp], ebp
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000309 call X86CompilationCallback2
Anton Korobeynikov6aeedfd2008-03-23 12:32:54 +0000310 mov esp, ebp
311 sub esp, 12
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000312 pop ecx
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000313 pop edx
314 pop eax
Anton Korobeynikov6aeedfd2008-03-23 12:32:54 +0000315 pop ebp
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000316 ret
317 }
318 }
Anton Korobeynikov6aeedfd2008-03-23 12:32:54 +0000319
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000320# endif // _MSC_VER
Chris Lattner1030f722005-05-19 06:49:17 +0000321
Misha Brukman57ebf3d2005-05-20 19:46:50 +0000322#else // Not an i386 host
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000323 void X86CompilationCallback() {
Torok Edwinc23197a2009-07-14 16:55:14 +0000324 llvm_unreachable("Cannot call X86CompilationCallback() on a non-x86 arch!");
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000325 }
Chris Lattner1030f722005-05-19 06:49:17 +0000326#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000327}
Chris Lattner1030f722005-05-19 06:49:17 +0000328
Dan Gohman9036d802009-02-06 21:15:52 +0000329/// X86CompilationCallback2 - This is the target-specific function invoked by the
Chris Lattner7ddde322004-11-20 23:54:33 +0000330/// function stub when we did not know the real target of a call. This function
331/// must locate the start of the stub or call site and pass it into the JIT
332/// compiler function.
Anton Korobeynikov6c402572009-09-06 20:21:48 +0000333extern "C" {
334#if !(defined (X86_64_JIT) && defined(_MSC_VER))
335 // the following function is called only from this translation unit,
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000336 // unless we are under 64bit Windows with MSC, where there is
Anton Korobeynikov6c402572009-09-06 20:21:48 +0000337 // no support for inline assembly
338static
339#endif
Chandler Carruth100c2672010-10-23 08:10:43 +0000340void LLVM_ATTRIBUTE_USED
Devang Patel0d885d12008-07-16 17:54:34 +0000341X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr) {
Evan Chengbe33dd92006-06-29 01:48:36 +0000342 intptr_t *RetAddrLoc = &StackPtr[1];
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000343 assert(*RetAddrLoc == RetAddr &&
Chris Lattner7ddde322004-11-20 23:54:33 +0000344 "Could not find return address on the stack!");
345
346 // It's a stub if there is an interrupt marker after the call.
Dale Johannesen7d1a7c02009-09-15 18:32:14 +0000347 bool isStub = ((unsigned char*)RetAddr)[0] == 0xCE;
Chris Lattner7ddde322004-11-20 23:54:33 +0000348
349 // The call instruction should have pushed the return value onto the stack...
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000350#if defined (X86_64_JIT)
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000351 RetAddr--; // Backtrack to the reference itself...
352#else
Chris Lattner7ddde322004-11-20 23:54:33 +0000353 RetAddr -= 4; // Backtrack to the reference itself...
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000354#endif
Chris Lattner7ddde322004-11-20 23:54:33 +0000355
356#if 0
David Greene92f16442010-01-05 01:29:23 +0000357 DEBUG(dbgs() << "In callback! Addr=" << (void*)RetAddr
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000358 << " ESP=" << (void*)StackPtr
359 << ": Resolving call to function: "
360 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
Chris Lattner7ddde322004-11-20 23:54:33 +0000361#endif
362
363 // Sanity check to make sure this really is a call instruction.
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000364#if defined (X86_64_JIT)
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000365 assert(((unsigned char*)RetAddr)[-2] == 0x41 &&"Not a call instr!");
366 assert(((unsigned char*)RetAddr)[-1] == 0xFF &&"Not a call instr!");
367#else
Evan Cheng25ab6902006-09-08 06:48:29 +0000368 assert(((unsigned char*)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000369#endif
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000370
Evan Cheng25ab6902006-09-08 06:48:29 +0000371 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
Chris Lattner7ddde322004-11-20 23:54:33 +0000372
373 // Rewrite the call target... so that we don't end up here every time we
374 // execute the call.
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000375#if defined (X86_64_JIT)
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +0000376 assert(isStub &&
377 "X86-64 doesn't support rewriting non-stub lazy compilation calls:"
378 " the call instruction varies too much.");
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000379#else
380 *(intptr_t *)RetAddr = (intptr_t)(NewVal-RetAddr-4);
381#endif
Chris Lattner7ddde322004-11-20 23:54:33 +0000382
383 if (isStub) {
384 // If this is a stub, rewrite the call into an unconditional branch
385 // instruction so that two return addresses are not pushed onto the stack
386 // when the requested function finally gets called. This also makes the
Dale Johannesen7d1a7c02009-09-15 18:32:14 +0000387 // 0xCE byte (interrupt) dead, so the marker doesn't effect anything.
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000388#if defined (X86_64_JIT)
Dale Johannesen6f83be02008-08-12 23:20:24 +0000389 // If the target address is within 32-bit range of the stub, use a
390 // PC-relative branch instead of loading the actual address. (This is
391 // considerably shorter than the 64-bit immediate load already there.)
392 // We assume here intptr_t is 64 bits.
393 intptr_t diff = NewVal-RetAddr+7;
394 if (diff >= -2147483648LL && diff <= 2147483647LL) {
395 *(unsigned char*)(RetAddr-0xc) = 0xE9;
396 *(intptr_t *)(RetAddr-0xb) = diff & 0xffffffff;
397 } else {
398 *(intptr_t *)(RetAddr - 0xa) = NewVal;
399 ((unsigned char*)RetAddr)[0] = (2 | (4 << 3) | (3 << 6));
400 }
Jeffrey Yasskinf28411f2010-03-15 04:57:55 +0000401 sys::ValgrindDiscardTranslations((void*)(RetAddr-0xc), 0xd);
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000402#else
Evan Cheng25ab6902006-09-08 06:48:29 +0000403 ((unsigned char*)RetAddr)[-1] = 0xE9;
Jeffrey Yasskinf28411f2010-03-15 04:57:55 +0000404 sys::ValgrindDiscardTranslations((void*)(RetAddr-1), 5);
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000405#endif
Chris Lattner7ddde322004-11-20 23:54:33 +0000406 }
407
408 // Change the return address to reexecute the call instruction...
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000409#if defined (X86_64_JIT)
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000410 *RetAddrLoc -= 0xd;
411#else
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000412 *RetAddrLoc -= 5;
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000413#endif
Chris Lattner7ddde322004-11-20 23:54:33 +0000414}
Anton Korobeynikov6c402572009-09-06 20:21:48 +0000415}
Chris Lattner7ddde322004-11-20 23:54:33 +0000416
Chris Lattner7ddde322004-11-20 23:54:33 +0000417TargetJITInfo::LazyResolverFn
418X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
419 JITCompilerFunction = F;
Evan Cheng93b11f82006-10-16 21:01:55 +0000420
Evan Cheng22f91442009-09-03 05:01:00 +0000421#if defined (X86_32_JIT) && !defined (_MSC_VER)
422 if (Subtarget->hasSSE1())
423 return X86CompilationCallback_SSE;
424#endif
425
426 return X86CompilationCallback;
Evan Chengd0da6ff2009-09-03 04:37:05 +0000427}
Evan Cheng93b11f82006-10-16 21:01:55 +0000428
Evan Chengd0da6ff2009-09-03 04:37:05 +0000429X86JITInfo::X86JITInfo(X86TargetMachine &tm) : TM(tm) {
430 Subtarget = &TM.getSubtarget<X86Subtarget>();
431 useGOT = 0;
432 TLSOffset = 0;
Chris Lattner7ddde322004-11-20 23:54:33 +0000433}
434
Evan Cheng9ed2f802008-11-10 01:08:07 +0000435void *X86JITInfo::emitGlobalValueIndirectSym(const GlobalValue* GV, void *ptr,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000436 JITCodeEmitter &JCE) {
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000437#if defined (X86_64_JIT)
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +0000438 const unsigned Alignment = 8;
439 uint8_t Buffer[8];
440 uint8_t *Cur = Buffer;
441 MachineCodeEmitter::emitWordLEInto(Cur, (unsigned)(intptr_t)ptr);
442 MachineCodeEmitter::emitWordLEInto(Cur, (unsigned)(((intptr_t)ptr) >> 32));
Evan Chengbe8c03f2008-01-04 10:46:51 +0000443#else
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +0000444 const unsigned Alignment = 4;
445 uint8_t Buffer[4];
446 uint8_t *Cur = Buffer;
447 MachineCodeEmitter::emitWordLEInto(Cur, (intptr_t)ptr);
Evan Chengbe8c03f2008-01-04 10:46:51 +0000448#endif
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +0000449 return JCE.allocIndirectGV(GV, Buffer, sizeof(Buffer), Alignment);
Evan Chengbe8c03f2008-01-04 10:46:51 +0000450}
451
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000452TargetJITInfo::StubLayout X86JITInfo::getStubLayout() {
453 // The 64-bit stub contains:
454 // movabs r10 <- 8-byte-target-address # 10 bytes
455 // call|jmp *r10 # 3 bytes
456 // The 32-bit stub contains a 5-byte call|jmp.
457 // If the stub is a call to the compilation callback, an extra byte is added
458 // to mark it as a stub.
459 StubLayout Result = {14, 4};
460 return Result;
461}
462
463void *X86JITInfo::emitFunctionStub(const Function* F, void *Target,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000464 JITCodeEmitter &JCE) {
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000465 // Note, we cast to intptr_t here to silence a -pedantic warning that
Chris Lattner078d1822006-06-01 17:13:10 +0000466 // complains about casting a function pointer to a normal pointer.
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000467#if defined (X86_32_JIT) && !defined (_MSC_VER)
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000468 bool NotCC = (Target != (void*)(intptr_t)X86CompilationCallback &&
469 Target != (void*)(intptr_t)X86CompilationCallback_SSE);
Evan Chengcf922302006-10-16 23:44:08 +0000470#else
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000471 bool NotCC = Target != (void*)(intptr_t)X86CompilationCallback;
Evan Cheng348b00d2006-10-16 22:53:28 +0000472#endif
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000473 JCE.emitAlignment(4);
474 void *Result = (void*)JCE.getCurrentPCValue();
Evan Cheng348b00d2006-10-16 22:53:28 +0000475 if (NotCC) {
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000476#if defined (X86_64_JIT)
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000477 JCE.emitByte(0x49); // REX prefix
478 JCE.emitByte(0xB8+2); // movabsq r10
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000479 JCE.emitWordLE((unsigned)(intptr_t)Target);
480 JCE.emitWordLE((unsigned)(((intptr_t)Target) >> 32));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000481 JCE.emitByte(0x41); // REX prefix
482 JCE.emitByte(0xFF); // jmpq *r10
483 JCE.emitByte(2 | (4 << 3) | (3 << 6));
Evan Cheng8510dc02007-03-14 10:48:08 +0000484#else
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000485 JCE.emitByte(0xE9);
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000486 JCE.emitWordLE((intptr_t)Target-JCE.getCurrentPCValue()-4);
Evan Cheng8510dc02007-03-14 10:48:08 +0000487#endif
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000488 return Result;
Chris Lattner90b1b452004-11-22 22:25:30 +0000489 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000490
Anton Korobeynikovfd9d9762008-03-23 13:40:45 +0000491#if defined (X86_64_JIT)
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000492 JCE.emitByte(0x49); // REX prefix
493 JCE.emitByte(0xB8+2); // movabsq r10
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000494 JCE.emitWordLE((unsigned)(intptr_t)Target);
495 JCE.emitWordLE((unsigned)(((intptr_t)Target) >> 32));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000496 JCE.emitByte(0x41); // REX prefix
497 JCE.emitByte(0xFF); // callq *r10
498 JCE.emitByte(2 | (2 << 3) | (3 << 6));
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000499#else
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000500 JCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
Chris Lattner90b1b452004-11-22 22:25:30 +0000501
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000502 JCE.emitWordLE((intptr_t)Target-JCE.getCurrentPCValue()-4);
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000503#endif
Chris Lattner90b1b452004-11-22 22:25:30 +0000504
Dale Johannesen7d1a7c02009-09-15 18:32:14 +0000505 // This used to use 0xCD, but that value is used by JITMemoryManager to
506 // initialize the buffer with garbage, which means it may follow a
507 // noreturn function call, confusing X86CompilationCallback2. PR 4929.
508 JCE.emitByte(0xCE); // Interrupt - Just a marker identifying the stub!
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000509 return Result;
Nate Begemand6b7a242009-02-18 08:31:02 +0000510}
511
Evan Cheng2a3e08b2008-01-05 02:26:58 +0000512/// getPICJumpTableEntry - Returns the value of the jumptable entry for the
513/// specific basic block.
Evan Cheng5788d1a2008-12-10 02:32:19 +0000514uintptr_t X86JITInfo::getPICJumpTableEntry(uintptr_t BB, uintptr_t Entry) {
Evan Cheng81fb5fe2008-07-16 01:33:08 +0000515#if defined(X86_64_JIT)
516 return BB - Entry;
517#else
Evan Cheng2a3e08b2008-01-05 02:26:58 +0000518 return BB - PICBase;
Evan Cheng81fb5fe2008-07-16 01:33:08 +0000519#endif
Evan Cheng2a3e08b2008-01-05 02:26:58 +0000520}
521
Chris Lattner7ddde322004-11-20 23:54:33 +0000522/// relocate - Before the JIT can run a block of code that has been emitted,
523/// it must rewrite the code to contain the actual addresses of any
524/// referenced global symbols.
525void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
Andrew Lenharth908bc862005-07-22 20:49:37 +0000526 unsigned NumRelocs, unsigned char* GOTBase) {
Chris Lattner7ddde322004-11-20 23:54:33 +0000527 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
528 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
529 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
530 switch ((X86::RelocationType)MR->getRelocationType()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000531 case X86::reloc_pcrel_word: {
Chris Lattner7ddde322004-11-20 23:54:33 +0000532 // PC relative relocation, add the relocated value to the value already in
533 // memory, after we adjust it for where the PC is.
Evan Chengaabe38b2007-12-22 09:40:20 +0000534 ResultPtr = ResultPtr -(intptr_t)RelocPos - 4 - MR->getConstantVal();
535 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
536 break;
537 }
538 case X86::reloc_picrel_word: {
539 // PIC base relative relocation, add the relocated value to the value
540 // already in memory, after we adjust it for where the PIC base is.
541 ResultPtr = ResultPtr - ((intptr_t)Function + MR->getConstantVal());
Evan Cheng25ab6902006-09-08 06:48:29 +0000542 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
Chris Lattner7ddde322004-11-20 23:54:33 +0000543 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000544 }
Chris Lattner7ddde322004-11-20 23:54:33 +0000545 case X86::reloc_absolute_word:
Bruno Cardoso Lopese55fef32009-08-05 00:11:21 +0000546 case X86::reloc_absolute_word_sext:
Chris Lattner7ddde322004-11-20 23:54:33 +0000547 // Absolute relocation, just add the relocated value to the value already
548 // in memory.
Evan Cheng25ab6902006-09-08 06:48:29 +0000549 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
Chris Lattner7ddde322004-11-20 23:54:33 +0000550 break;
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000551 case X86::reloc_absolute_dword:
552 *((intptr_t*)RelocPos) += ResultPtr;
553 break;
Chris Lattner7ddde322004-11-20 23:54:33 +0000554 }
555 }
556}
Nicolas Geoffray46fa1392008-10-25 15:41:43 +0000557
558char* X86JITInfo::allocateThreadLocalMemory(size_t size) {
559#if defined(X86_32_JIT) && !defined(__APPLE__) && !defined(_MSC_VER)
560 TLSOffset -= size;
561 return TLSOffset;
562#else
Torok Edwinc23197a2009-07-14 16:55:14 +0000563 llvm_unreachable("Cannot allocate thread local storage on this arch!");
Nicolas Geoffray46fa1392008-10-25 15:41:43 +0000564 return 0;
565#endif
566}