blob: 713579e6bb4a7e14891c91265c25e90b5d1fbbef [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>
21#include <iostream>
Chris Lattner7ddde322004-11-20 23:54:33 +000022using namespace llvm;
23
Chris Lattner38f73732006-01-26 19:55:20 +000024#ifdef _MSC_VER
25 extern "C" void *_AddressOfReturnAddress(void);
26 #pragma intrinsic(_AddressOfReturnAddress)
27#endif
28
Chris Lattner7ddde322004-11-20 23:54:33 +000029void X86JITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
30 unsigned char *OldByte = (unsigned char *)Old;
31 *OldByte++ = 0xE9; // Emit JMP opcode.
32 unsigned *OldWord = (unsigned *)OldByte;
33 unsigned NewAddr = (intptr_t)New;
34 unsigned OldAddr = (intptr_t)OldWord;
35 *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
36}
37
38
Chris Lattner7ddde322004-11-20 23:54:33 +000039/// JITCompilerFunction - This contains the address of the JIT function used to
40/// compile a function lazily.
41static TargetJITInfo::JITCompilerFn JITCompilerFunction;
42
Chris Lattner40f4ba52006-09-08 17:03:56 +000043// Get the ASMPREFIX for the current host. This is often '_'.
44#ifndef __USER_LABEL_PREFIX__
45#define __USER_LABEL_PREFIX__
46#endif
47#define GETASMPREFIX2(X) #X
48#define GETASMPREFIX(X) GETASMPREFIX2(X)
49#define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
50
Chris Lattner1030f722005-05-19 06:49:17 +000051// Provide a wrapper for X86CompilationCallback2 that saves non-traditional
52// callee saved registers, for the fastcc calling convention.
Jeff Cohen8bc6f932005-05-20 01:35:39 +000053extern "C" {
Evan Cheng25ab6902006-09-08 06:48:29 +000054#if defined(__x86_64__)
55 // No need to save EAX/EDX for X86-64.
56 void X86CompilationCallback(void);
57 asm(
58 ".text\n"
59 ".align 8\n"
Chris Lattner40f4ba52006-09-08 17:03:56 +000060 ".globl " ASMPREFIX "X86CompilationCallback\n"
61 ASMPREFIX "X86CompilationCallback:\n"
Evan Cheng25ab6902006-09-08 06:48:29 +000062 // Save RBP
63 "pushq %rbp\n"
64 // Save RSP
65 "movq %rsp, %rbp\n"
66 // Save all int arg registers
67 "pushq %rdi\n"
68 "pushq %rsi\n"
69 "pushq %rdx\n"
70 "pushq %rcx\n"
71 "pushq %r8\n"
72 "pushq %r9\n"
73 // Align stack on 16-byte boundary. ESP might not be properly aligned
74 // (8 byte) if this is called from an indirect stub.
75 "andq $-16, %rsp\n"
76 // Save all XMM arg registers
77 "subq $128, %rsp\n"
78 "movaps %xmm0, (%rsp)\n"
79 "movaps %xmm1, 16(%rsp)\n"
80 "movaps %xmm2, 32(%rsp)\n"
81 "movaps %xmm3, 48(%rsp)\n"
82 "movaps %xmm4, 64(%rsp)\n"
83 "movaps %xmm5, 80(%rsp)\n"
84 "movaps %xmm6, 96(%rsp)\n"
85 "movaps %xmm7, 112(%rsp)\n"
86 // JIT callee
87 "movq %rbp, %rdi\n" // Pass prev frame and return address
88 "movq 8(%rbp), %rsi\n"
Chris Lattner40f4ba52006-09-08 17:03:56 +000089 "call " ASMPREFIX "X86CompilationCallback2\n"
Evan Cheng25ab6902006-09-08 06:48:29 +000090 // Restore all XMM arg registers
91 "movaps 112(%rsp), %xmm7\n"
92 "movaps 96(%rsp), %xmm6\n"
93 "movaps 80(%rsp), %xmm5\n"
94 "movaps 64(%rsp), %xmm4\n"
95 "movaps 48(%rsp), %xmm3\n"
96 "movaps 32(%rsp), %xmm2\n"
97 "movaps 16(%rsp), %xmm1\n"
98 "movaps (%rsp), %xmm0\n"
99 // Restore RSP
100 "movq %rbp, %rsp\n"
101 // Restore all int arg registers
102 "subq $48, %rsp\n"
103 "popq %r9\n"
104 "popq %r8\n"
105 "popq %rcx\n"
106 "popq %rdx\n"
107 "popq %rsi\n"
108 "popq %rdi\n"
109 // Restore RBP
110 "popq %rbp\n"
111 "ret\n");
112#elif defined(__i386__) || defined(i386) || defined(_M_IX86)
Chris Lattner1030f722005-05-19 06:49:17 +0000113#ifndef _MSC_VER
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000114 void X86CompilationCallback(void);
115 asm(
116 ".text\n"
117 ".align 8\n"
Chris Lattner40f4ba52006-09-08 17:03:56 +0000118 ".globl " ASMPREFIX "X86CompilationCallback\n"
119 ASMPREFIX "X86CompilationCallback:\n"
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000120 "pushl %ebp\n"
121 "movl %esp, %ebp\n" // Standard prologue
Evan Chengda08d2c2006-06-24 08:36:10 +0000122#if FASTCC_NUM_INT_ARGS_INREGS > 0
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000123 "pushl %eax\n"
Evan Chengda08d2c2006-06-24 08:36:10 +0000124 "pushl %edx\n" // Save EAX/EDX
125#endif
126#if defined(__APPLE__)
127 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
128#endif
Evan Chengbe33dd92006-06-29 01:48:36 +0000129 "subl $16, %esp\n"
130 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
131 "movl %eax, 4(%esp)\n"
132 "movl %ebp, (%esp)\n"
Chris Lattner40f4ba52006-09-08 17:03:56 +0000133 "call " ASMPREFIX "X86CompilationCallback2\n"
Evan Chengda08d2c2006-06-24 08:36:10 +0000134 "movl %ebp, %esp\n" // Restore ESP
Evan Chengda08d2c2006-06-24 08:36:10 +0000135#if FASTCC_NUM_INT_ARGS_INREGS > 0
Evan Chengda08d2c2006-06-24 08:36:10 +0000136 "subl $8, %esp\n"
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000137 "popl %edx\n"
138 "popl %eax\n"
Evan Chengda08d2c2006-06-24 08:36:10 +0000139#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000140 "popl %ebp\n"
141 "ret\n");
Evan Cheng93b11f82006-10-16 21:01:55 +0000142
143 // Same as X86CompilationCallback but also saves XMM argument registers.
144 void X86CompilationCallback_SSE(void);
145 asm(
146 ".text\n"
147 ".align 8\n"
148 ".globl " ASMPREFIX "X86CompilationCallback_SSE\n"
149 ASMPREFIX "X86CompilationCallback_SSE:\n"
150 "pushl %ebp\n"
151 "movl %esp, %ebp\n" // Standard prologue
152#if FASTCC_NUM_INT_ARGS_INREGS > 0
153 "pushl %eax\n"
154 "pushl %edx\n" // Save EAX/EDX
155#endif
156 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
157 // Save all XMM arg registers
158 "subl $64, %esp\n"
159 "movaps %xmm0, (%esp)\n"
160 "movaps %xmm1, 16(%esp)\n"
161 "movaps %xmm2, 32(%esp)\n"
162 "movaps %xmm3, 48(%esp)\n"
163 "subl $16, %esp\n"
164 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
165 "movl %eax, 4(%esp)\n"
166 "movl %ebp, (%esp)\n"
167 "call " ASMPREFIX "X86CompilationCallback2\n"
168 "addl $16, %esp\n"
169 "movaps 48(%esp), %xmm3\n"
170 "movaps 32(%esp), %xmm2\n"
171 "movaps 16(%esp), %xmm1\n"
172 "movaps (%esp), %xmm0\n"
173 "movl %ebp, %esp\n" // Restore ESP
174#if FASTCC_NUM_INT_ARGS_INREGS > 0
175 "subl $8, %esp\n"
176 "popl %edx\n"
177 "popl %eax\n"
178#endif
179 "popl %ebp\n"
180 "ret\n");
Chris Lattner1030f722005-05-19 06:49:17 +0000181#else
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000182 void X86CompilationCallback2(void);
183
184 _declspec(naked) void X86CompilationCallback(void) {
185 __asm {
186 push eax
187 push edx
188 call X86CompilationCallback2
189 pop edx
190 pop eax
191 ret
192 }
193 }
Misha Brukman57ebf3d2005-05-20 19:46:50 +0000194#endif // _MSC_VER
Chris Lattner1030f722005-05-19 06:49:17 +0000195
Misha Brukman57ebf3d2005-05-20 19:46:50 +0000196#else // Not an i386 host
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000197 void X86CompilationCallback() {
Misha Brukman57ebf3d2005-05-20 19:46:50 +0000198 std::cerr << "Cannot call X86CompilationCallback() on a non-x86 arch!\n";
Chris Lattner170fbcb2005-05-20 17:00:21 +0000199 abort();
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000200 }
Chris Lattner1030f722005-05-19 06:49:17 +0000201#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000202}
Chris Lattner1030f722005-05-19 06:49:17 +0000203
204/// X86CompilationCallback - This is the target-specific function invoked by the
Chris Lattner7ddde322004-11-20 23:54:33 +0000205/// function stub when we did not know the real target of a call. This function
206/// must locate the start of the stub or call site and pass it into the JIT
207/// compiler function.
Chris Lattner7ddde322004-11-20 23:54:33 +0000208#ifdef _MSC_VER
Evan Chengbe33dd92006-06-29 01:48:36 +0000209extern "C" void X86CompilationCallback2() {
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000210 assert(sizeof(size_t) == 4); // FIXME: handle Win64
211 unsigned *RetAddrLoc = (unsigned *)_AddressOfReturnAddress();
212 RetAddrLoc += 3; // skip over ret addr, edx, eax
213 unsigned RetAddr = *RetAddrLoc;
Chris Lattner7ddde322004-11-20 23:54:33 +0000214#else
Evan Chengbe33dd92006-06-29 01:48:36 +0000215extern "C" void X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr) {
216 intptr_t *RetAddrLoc = &StackPtr[1];
Chris Lattner7ddde322004-11-20 23:54:33 +0000217#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000218 assert(*RetAddrLoc == RetAddr &&
Chris Lattner7ddde322004-11-20 23:54:33 +0000219 "Could not find return address on the stack!");
220
221 // It's a stub if there is an interrupt marker after the call.
Evan Cheng25ab6902006-09-08 06:48:29 +0000222 bool isStub = ((unsigned char*)RetAddr)[0] == 0xCD;
Chris Lattner7ddde322004-11-20 23:54:33 +0000223
224 // The call instruction should have pushed the return value onto the stack...
225 RetAddr -= 4; // Backtrack to the reference itself...
226
227#if 0
228 DEBUG(std::cerr << "In callback! Addr=" << (void*)RetAddr
229 << " ESP=" << (void*)StackPtr
230 << ": Resolving call to function: "
231 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
232#endif
233
234 // Sanity check to make sure this really is a call instruction.
Evan Cheng25ab6902006-09-08 06:48:29 +0000235 assert(((unsigned char*)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000236
Evan Cheng25ab6902006-09-08 06:48:29 +0000237 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
Chris Lattner7ddde322004-11-20 23:54:33 +0000238
239 // Rewrite the call target... so that we don't end up here every time we
240 // execute the call.
Evan Cheng25ab6902006-09-08 06:48:29 +0000241 *(unsigned *)RetAddr = (unsigned)(NewVal-RetAddr-4);
Chris Lattner7ddde322004-11-20 23:54:33 +0000242
243 if (isStub) {
244 // If this is a stub, rewrite the call into an unconditional branch
245 // instruction so that two return addresses are not pushed onto the stack
246 // when the requested function finally gets called. This also makes the
247 // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
Evan Cheng25ab6902006-09-08 06:48:29 +0000248 ((unsigned char*)RetAddr)[-1] = 0xE9;
Chris Lattner7ddde322004-11-20 23:54:33 +0000249 }
250
251 // Change the return address to reexecute the call instruction...
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000252 *RetAddrLoc -= 5;
Chris Lattner7ddde322004-11-20 23:54:33 +0000253}
254
Chris Lattner7ddde322004-11-20 23:54:33 +0000255TargetJITInfo::LazyResolverFn
256X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
257 JITCompilerFunction = F;
Evan Cheng93b11f82006-10-16 21:01:55 +0000258
Evan Cheng348b00d2006-10-16 22:53:28 +0000259#if !defined(__x86_64__)
Evan Cheng93b11f82006-10-16 21:01:55 +0000260 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
261 union {
262 unsigned u[3];
263 char c[12];
264 } text;
265
266 if (!X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1)) {
267 // FIXME: support for AMD family of processors.
268 if (memcmp(text.c, "GenuineIntel", 12) == 0) {
269 X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
270 if ((EDX >> 25) & 0x1)
271 return X86CompilationCallback_SSE;
272 }
273 }
Evan Cheng348b00d2006-10-16 22:53:28 +0000274#endif
Evan Cheng93b11f82006-10-16 21:01:55 +0000275
Chris Lattner1030f722005-05-19 06:49:17 +0000276 return X86CompilationCallback;
Chris Lattner7ddde322004-11-20 23:54:33 +0000277}
278
Chris Lattner90b1b452004-11-22 22:25:30 +0000279void *X86JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
Chris Lattner078d1822006-06-01 17:13:10 +0000280 // Note, we cast to intptr_t here to silence a -pedantic warning that
281 // complains about casting a function pointer to a normal pointer.
Evan Cheng348b00d2006-10-16 22:53:28 +0000282#if defined(__x86_64__)
283 bool NotCC = Fn != (void*)(intptr_t)X86CompilationCallback;
284#else
285 bool NotCC = (Fn != (void*)(intptr_t)X86CompilationCallback &&
286 Fn != (void*)(intptr_t)X86CompilationCallback_SSE);
287#endif
288 if (NotCC) {
Chris Lattner90b1b452004-11-22 22:25:30 +0000289 MCE.startFunctionStub(5);
290 MCE.emitByte(0xE9);
Chris Lattnerd3f0aef2006-05-02 19:14:47 +0000291 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
Chris Lattner90b1b452004-11-22 22:25:30 +0000292 return MCE.finishFunctionStub(0);
293 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000294
Chris Lattner90b1b452004-11-22 22:25:30 +0000295 MCE.startFunctionStub(6);
296 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
297
Chris Lattnerd3f0aef2006-05-02 19:14:47 +0000298 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
Chris Lattner90b1b452004-11-22 22:25:30 +0000299
300 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
301 return MCE.finishFunctionStub(0);
302}
Chris Lattner7ddde322004-11-20 23:54:33 +0000303
304/// relocate - Before the JIT can run a block of code that has been emitted,
305/// it must rewrite the code to contain the actual addresses of any
306/// referenced global symbols.
307void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
Andrew Lenharth908bc862005-07-22 20:49:37 +0000308 unsigned NumRelocs, unsigned char* GOTBase) {
Chris Lattner7ddde322004-11-20 23:54:33 +0000309 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
310 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
311 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
312 switch ((X86::RelocationType)MR->getRelocationType()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000313 case X86::reloc_pcrel_word: {
Chris Lattner7ddde322004-11-20 23:54:33 +0000314 // PC relative relocation, add the relocated value to the value already in
315 // memory, after we adjust it for where the PC is.
Evan Cheng25ab6902006-09-08 06:48:29 +0000316 ResultPtr = ResultPtr-(intptr_t)RelocPos-4-MR->getConstantVal();
317 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
Chris Lattner7ddde322004-11-20 23:54:33 +0000318 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000319 }
Chris Lattner7ddde322004-11-20 23:54:33 +0000320 case X86::reloc_absolute_word:
321 // Absolute relocation, just add the relocated value to the value already
322 // in memory.
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
Chris Lattner7ddde322004-11-20 23:54:33 +0000324 break;
325 }
326 }
327}