blob: b32fab21db0f99e6ba7d17767c5959a0d883e8b7 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Chris Lattner7ddde322004-11-20 23:54:33 +000018#include "llvm/CodeGen/MachineCodeEmitter.h"
19#include "llvm/Config/alloca.h"
Nate Begemanbe136342005-05-20 21:29:24 +000020#include <cstdlib>
Chris Lattner7ddde322004-11-20 23:54:33 +000021using namespace llvm;
22
Chris Lattner38f73732006-01-26 19:55:20 +000023#ifdef _MSC_VER
24 extern "C" void *_AddressOfReturnAddress(void);
25 #pragma intrinsic(_AddressOfReturnAddress)
26#endif
27
Chris Lattner7ddde322004-11-20 23:54:33 +000028void X86JITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
29 unsigned char *OldByte = (unsigned char *)Old;
30 *OldByte++ = 0xE9; // Emit JMP opcode.
31 unsigned *OldWord = (unsigned *)OldByte;
32 unsigned NewAddr = (intptr_t)New;
33 unsigned OldAddr = (intptr_t)OldWord;
34 *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
35}
36
37
Chris Lattner7ddde322004-11-20 23:54:33 +000038/// JITCompilerFunction - This contains the address of the JIT function used to
39/// compile a function lazily.
40static TargetJITInfo::JITCompilerFn JITCompilerFunction;
41
Chris Lattner40f4ba52006-09-08 17:03:56 +000042// Get the ASMPREFIX for the current host. This is often '_'.
43#ifndef __USER_LABEL_PREFIX__
44#define __USER_LABEL_PREFIX__
45#endif
46#define GETASMPREFIX2(X) #X
47#define GETASMPREFIX(X) GETASMPREFIX2(X)
48#define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
49
Chris Lattner1030f722005-05-19 06:49:17 +000050// Provide a wrapper for X86CompilationCallback2 that saves non-traditional
51// callee saved registers, for the fastcc calling convention.
Jeff Cohen8bc6f932005-05-20 01:35:39 +000052extern "C" {
Evan Cheng25ab6902006-09-08 06:48:29 +000053#if defined(__x86_64__)
54 // No need to save EAX/EDX for X86-64.
55 void X86CompilationCallback(void);
56 asm(
57 ".text\n"
58 ".align 8\n"
Chris Lattner40f4ba52006-09-08 17:03:56 +000059 ".globl " ASMPREFIX "X86CompilationCallback\n"
60 ASMPREFIX "X86CompilationCallback:\n"
Evan Cheng25ab6902006-09-08 06:48:29 +000061 // Save RBP
62 "pushq %rbp\n"
63 // Save RSP
64 "movq %rsp, %rbp\n"
65 // Save all int arg registers
66 "pushq %rdi\n"
67 "pushq %rsi\n"
68 "pushq %rdx\n"
69 "pushq %rcx\n"
70 "pushq %r8\n"
71 "pushq %r9\n"
72 // Align stack on 16-byte boundary. ESP might not be properly aligned
73 // (8 byte) if this is called from an indirect stub.
74 "andq $-16, %rsp\n"
75 // Save all XMM arg registers
76 "subq $128, %rsp\n"
77 "movaps %xmm0, (%rsp)\n"
78 "movaps %xmm1, 16(%rsp)\n"
79 "movaps %xmm2, 32(%rsp)\n"
80 "movaps %xmm3, 48(%rsp)\n"
81 "movaps %xmm4, 64(%rsp)\n"
82 "movaps %xmm5, 80(%rsp)\n"
83 "movaps %xmm6, 96(%rsp)\n"
84 "movaps %xmm7, 112(%rsp)\n"
85 // JIT callee
86 "movq %rbp, %rdi\n" // Pass prev frame and return address
87 "movq 8(%rbp), %rsi\n"
Chris Lattner40f4ba52006-09-08 17:03:56 +000088 "call " ASMPREFIX "X86CompilationCallback2\n"
Evan Cheng25ab6902006-09-08 06:48:29 +000089 // Restore all XMM arg registers
90 "movaps 112(%rsp), %xmm7\n"
91 "movaps 96(%rsp), %xmm6\n"
92 "movaps 80(%rsp), %xmm5\n"
93 "movaps 64(%rsp), %xmm4\n"
94 "movaps 48(%rsp), %xmm3\n"
95 "movaps 32(%rsp), %xmm2\n"
96 "movaps 16(%rsp), %xmm1\n"
97 "movaps (%rsp), %xmm0\n"
98 // Restore RSP
99 "movq %rbp, %rsp\n"
100 // Restore all int arg registers
101 "subq $48, %rsp\n"
102 "popq %r9\n"
103 "popq %r8\n"
104 "popq %rcx\n"
105 "popq %rdx\n"
106 "popq %rsi\n"
107 "popq %rdi\n"
108 // Restore RBP
109 "popq %rbp\n"
110 "ret\n");
111#elif defined(__i386__) || defined(i386) || defined(_M_IX86)
Chris Lattner1030f722005-05-19 06:49:17 +0000112#ifndef _MSC_VER
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000113 void X86CompilationCallback(void);
114 asm(
115 ".text\n"
116 ".align 8\n"
Chris Lattner40f4ba52006-09-08 17:03:56 +0000117 ".globl " ASMPREFIX "X86CompilationCallback\n"
118 ASMPREFIX "X86CompilationCallback:\n"
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000119 "pushl %ebp\n"
120 "movl %esp, %ebp\n" // Standard prologue
121 "pushl %eax\n"
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000122 "pushl %edx\n" // Save EAX/EDX/ECX
123 "pushl %ecx\n"
Evan Chengda08d2c2006-06-24 08:36:10 +0000124#if defined(__APPLE__)
125 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
126#endif
Evan Chengbe33dd92006-06-29 01:48:36 +0000127 "subl $16, %esp\n"
128 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
129 "movl %eax, 4(%esp)\n"
130 "movl %ebp, (%esp)\n"
Chris Lattner40f4ba52006-09-08 17:03:56 +0000131 "call " ASMPREFIX "X86CompilationCallback2\n"
Evan Chengda08d2c2006-06-24 08:36:10 +0000132 "movl %ebp, %esp\n" // Restore ESP
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000133 "subl $12, %esp\n"
134 "popl %ecx\n"
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000135 "popl %edx\n"
136 "popl %eax\n"
137 "popl %ebp\n"
138 "ret\n");
Evan Cheng93b11f82006-10-16 21:01:55 +0000139
140 // Same as X86CompilationCallback but also saves XMM argument registers.
141 void X86CompilationCallback_SSE(void);
142 asm(
143 ".text\n"
144 ".align 8\n"
145 ".globl " ASMPREFIX "X86CompilationCallback_SSE\n"
146 ASMPREFIX "X86CompilationCallback_SSE:\n"
147 "pushl %ebp\n"
148 "movl %esp, %ebp\n" // Standard prologue
Evan Cheng93b11f82006-10-16 21:01:55 +0000149 "pushl %eax\n"
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000150 "pushl %edx\n" // Save EAX/EDX/ECX
151 "pushl %ecx\n"
Evan Cheng93b11f82006-10-16 21:01:55 +0000152 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
153 // Save all XMM arg registers
154 "subl $64, %esp\n"
155 "movaps %xmm0, (%esp)\n"
156 "movaps %xmm1, 16(%esp)\n"
157 "movaps %xmm2, 32(%esp)\n"
158 "movaps %xmm3, 48(%esp)\n"
159 "subl $16, %esp\n"
160 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
161 "movl %eax, 4(%esp)\n"
162 "movl %ebp, (%esp)\n"
163 "call " ASMPREFIX "X86CompilationCallback2\n"
164 "addl $16, %esp\n"
165 "movaps 48(%esp), %xmm3\n"
166 "movaps 32(%esp), %xmm2\n"
167 "movaps 16(%esp), %xmm1\n"
168 "movaps (%esp), %xmm0\n"
169 "movl %ebp, %esp\n" // Restore ESP
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000170 "subl $12, %esp\n"
171 "popl %ecx\n"
Evan Cheng93b11f82006-10-16 21:01:55 +0000172 "popl %edx\n"
173 "popl %eax\n"
Evan Cheng93b11f82006-10-16 21:01:55 +0000174 "popl %ebp\n"
175 "ret\n");
Chris Lattner1030f722005-05-19 06:49:17 +0000176#else
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000177 void X86CompilationCallback2(void);
178
179 _declspec(naked) void X86CompilationCallback(void) {
180 __asm {
181 push eax
182 push edx
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000183 push ecx
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000184 call X86CompilationCallback2
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000185 pop ecx
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000186 pop edx
187 pop eax
188 ret
189 }
190 }
Misha Brukman57ebf3d2005-05-20 19:46:50 +0000191#endif // _MSC_VER
Chris Lattner1030f722005-05-19 06:49:17 +0000192
Misha Brukman57ebf3d2005-05-20 19:46:50 +0000193#else // Not an i386 host
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000194 void X86CompilationCallback() {
Bill Wendling6345d752006-11-17 07:52:03 +0000195 assert(0 && "Cannot call X86CompilationCallback() on a non-x86 arch!\n");
Chris Lattner170fbcb2005-05-20 17:00:21 +0000196 abort();
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000197 }
Chris Lattner1030f722005-05-19 06:49:17 +0000198#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000199}
Chris Lattner1030f722005-05-19 06:49:17 +0000200
201/// X86CompilationCallback - This is the target-specific function invoked by the
Chris Lattner7ddde322004-11-20 23:54:33 +0000202/// function stub when we did not know the real target of a call. This function
203/// must locate the start of the stub or call site and pass it into the JIT
204/// compiler function.
Chris Lattner7ddde322004-11-20 23:54:33 +0000205#ifdef _MSC_VER
Evan Chengbe33dd92006-06-29 01:48:36 +0000206extern "C" void X86CompilationCallback2() {
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000207 assert(sizeof(size_t) == 4); // FIXME: handle Win64
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000208 intptr_t *RetAddrLoc = (intptr_t *)_AddressOfReturnAddress();
Anton Korobeynikov1620f1a2007-01-29 21:28:01 +0000209 RetAddrLoc += 4; // skip over ret addr, edx, eax, ecx
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000210 intptr_t RetAddr = *RetAddrLoc;
Chris Lattner7ddde322004-11-20 23:54:33 +0000211#else
Evan Chengbe33dd92006-06-29 01:48:36 +0000212extern "C" void X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr) {
213 intptr_t *RetAddrLoc = &StackPtr[1];
Chris Lattner7ddde322004-11-20 23:54:33 +0000214#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000215 assert(*RetAddrLoc == RetAddr &&
Chris Lattner7ddde322004-11-20 23:54:33 +0000216 "Could not find return address on the stack!");
217
218 // It's a stub if there is an interrupt marker after the call.
Evan Cheng25ab6902006-09-08 06:48:29 +0000219 bool isStub = ((unsigned char*)RetAddr)[0] == 0xCD;
Chris Lattner7ddde322004-11-20 23:54:33 +0000220
221 // The call instruction should have pushed the return value onto the stack...
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000222#ifdef __x86_64__
223 RetAddr--; // Backtrack to the reference itself...
224#else
Chris Lattner7ddde322004-11-20 23:54:33 +0000225 RetAddr -= 4; // Backtrack to the reference itself...
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000226#endif
Chris Lattner7ddde322004-11-20 23:54:33 +0000227
228#if 0
Bill Wendling6345d752006-11-17 07:52:03 +0000229 DOUT << "In callback! Addr=" << (void*)RetAddr
230 << " ESP=" << (void*)StackPtr
231 << ": Resolving call to function: "
232 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n";
Chris Lattner7ddde322004-11-20 23:54:33 +0000233#endif
234
235 // Sanity check to make sure this really is a call instruction.
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000236#ifdef __x86_64__
237 assert(((unsigned char*)RetAddr)[-2] == 0x41 &&"Not a call instr!");
238 assert(((unsigned char*)RetAddr)[-1] == 0xFF &&"Not a call instr!");
239#else
Evan Cheng25ab6902006-09-08 06:48:29 +0000240 assert(((unsigned char*)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000241#endif
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000242
Evan Cheng25ab6902006-09-08 06:48:29 +0000243 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
Chris Lattner7ddde322004-11-20 23:54:33 +0000244
245 // Rewrite the call target... so that we don't end up here every time we
246 // execute the call.
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000247#ifdef __x86_64__
248 *(intptr_t *)(RetAddr - 0xa) = NewVal;
249#else
250 *(intptr_t *)RetAddr = (intptr_t)(NewVal-RetAddr-4);
251#endif
Chris Lattner7ddde322004-11-20 23:54:33 +0000252
253 if (isStub) {
254 // If this is a stub, rewrite the call into an unconditional branch
255 // instruction so that two return addresses are not pushed onto the stack
256 // when the requested function finally gets called. This also makes the
257 // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000258#ifdef __x86_64__
259 ((unsigned char*)RetAddr)[0] = (2 | (4 << 3) | (3 << 6));
260#else
Evan Cheng25ab6902006-09-08 06:48:29 +0000261 ((unsigned char*)RetAddr)[-1] = 0xE9;
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000262#endif
Chris Lattner7ddde322004-11-20 23:54:33 +0000263 }
264
265 // Change the return address to reexecute the call instruction...
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000266#ifdef __x86_64__
267 *RetAddrLoc -= 0xd;
268#else
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000269 *RetAddrLoc -= 5;
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000270#endif
Chris Lattner7ddde322004-11-20 23:54:33 +0000271}
272
Chris Lattner7ddde322004-11-20 23:54:33 +0000273TargetJITInfo::LazyResolverFn
274X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
275 JITCompilerFunction = F;
Evan Cheng93b11f82006-10-16 21:01:55 +0000276
Evan Chengcf922302006-10-16 23:44:08 +0000277#if (defined(__i386__) || defined(i386) || defined(_M_IX86)) && \
278 !defined(_MSC_VER) && !defined(__x86_64__)
Evan Cheng93b11f82006-10-16 21:01:55 +0000279 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
280 union {
281 unsigned u[3];
282 char c[12];
283 } text;
284
285 if (!X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1)) {
286 // FIXME: support for AMD family of processors.
287 if (memcmp(text.c, "GenuineIntel", 12) == 0) {
288 X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
289 if ((EDX >> 25) & 0x1)
290 return X86CompilationCallback_SSE;
291 }
292 }
Evan Cheng348b00d2006-10-16 22:53:28 +0000293#endif
Evan Cheng93b11f82006-10-16 21:01:55 +0000294
Chris Lattner1030f722005-05-19 06:49:17 +0000295 return X86CompilationCallback;
Chris Lattner7ddde322004-11-20 23:54:33 +0000296}
297
Chris Lattner90b1b452004-11-22 22:25:30 +0000298void *X86JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
Chris Lattner078d1822006-06-01 17:13:10 +0000299 // Note, we cast to intptr_t here to silence a -pedantic warning that
300 // complains about casting a function pointer to a normal pointer.
Evan Chengcf922302006-10-16 23:44:08 +0000301#if (defined(__i386__) || defined(i386) || defined(_M_IX86)) && \
302 !defined(_MSC_VER) && !defined(__x86_64__)
Evan Cheng348b00d2006-10-16 22:53:28 +0000303 bool NotCC = (Fn != (void*)(intptr_t)X86CompilationCallback &&
304 Fn != (void*)(intptr_t)X86CompilationCallback_SSE);
Evan Chengcf922302006-10-16 23:44:08 +0000305#else
306 bool NotCC = Fn != (void*)(intptr_t)X86CompilationCallback;
Evan Cheng348b00d2006-10-16 22:53:28 +0000307#endif
308 if (NotCC) {
Evan Cheng73b00942006-11-16 20:13:34 +0000309 MCE.startFunctionStub(5, 4);
Chris Lattner90b1b452004-11-22 22:25:30 +0000310 MCE.emitByte(0xE9);
Chris Lattnerd3f0aef2006-05-02 19:14:47 +0000311 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
Chris Lattner90b1b452004-11-22 22:25:30 +0000312 return MCE.finishFunctionStub(0);
313 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000314
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000315#ifdef __x86_64__
316 MCE.startFunctionStub(14, 4);
317 MCE.emitByte(0x49); // REX prefix
318 MCE.emitByte(0xB8+2); // movabsq r10
319 MCE.emitWordLE(((unsigned *)&Fn)[0]);
320 MCE.emitWordLE(((unsigned *)&Fn)[1]);
321 MCE.emitByte(0x41); // REX prefix
322 MCE.emitByte(0xFF); // callq *r10
323 MCE.emitByte(2 | (2 << 3) | (3 << 6));
324#else
Evan Cheng73b00942006-11-16 20:13:34 +0000325 MCE.startFunctionStub(6, 4);
Chris Lattner90b1b452004-11-22 22:25:30 +0000326 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
327
Chris Lattnerd3f0aef2006-05-02 19:14:47 +0000328 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
Evan Cheng5c0b61a2007-03-14 10:44:30 +0000329#endif
Chris Lattner90b1b452004-11-22 22:25:30 +0000330
331 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
332 return MCE.finishFunctionStub(0);
333}
Chris Lattner7ddde322004-11-20 23:54:33 +0000334
335/// relocate - Before the JIT can run a block of code that has been emitted,
336/// it must rewrite the code to contain the actual addresses of any
337/// referenced global symbols.
338void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
Andrew Lenharth908bc862005-07-22 20:49:37 +0000339 unsigned NumRelocs, unsigned char* GOTBase) {
Chris Lattner7ddde322004-11-20 23:54:33 +0000340 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
341 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
342 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
343 switch ((X86::RelocationType)MR->getRelocationType()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000344 case X86::reloc_pcrel_word: {
Chris Lattner7ddde322004-11-20 23:54:33 +0000345 // PC relative relocation, add the relocated value to the value already in
346 // memory, after we adjust it for where the PC is.
Evan Cheng25ab6902006-09-08 06:48:29 +0000347 ResultPtr = ResultPtr-(intptr_t)RelocPos-4-MR->getConstantVal();
348 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
Chris Lattner7ddde322004-11-20 23:54:33 +0000349 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000350 }
Chris Lattner7ddde322004-11-20 23:54:33 +0000351 case X86::reloc_absolute_word:
352 // Absolute relocation, just add the relocated value to the value already
353 // in memory.
Evan Cheng25ab6902006-09-08 06:48:29 +0000354 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
Chris Lattner7ddde322004-11-20 23:54:33 +0000355 break;
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000356 case X86::reloc_absolute_dword:
357 *((intptr_t*)RelocPos) += ResultPtr;
358 break;
Chris Lattner7ddde322004-11-20 23:54:33 +0000359 }
360 }
361}