blob: 01d2c5c693a4055987345f107a56c310414c094b [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86JITInfo.cpp - Implement the JIT interfaces for the X86 target --===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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"
17#include "X86Subtarget.h"
18#include "llvm/CodeGen/MachineCodeEmitter.h"
19#include "llvm/Config/alloca.h"
20#include <cstdlib>
21using namespace llvm;
22
23#ifdef _MSC_VER
24 extern "C" void *_AddressOfReturnAddress(void);
25 #pragma intrinsic(_AddressOfReturnAddress)
26#endif
27
28void 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
38/// JITCompilerFunction - This contains the address of the JIT function used to
39/// compile a function lazily.
40static TargetJITInfo::JITCompilerFn JITCompilerFunction;
41
42// 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
50// Provide a wrapper for X86CompilationCallback2 that saves non-traditional
51// callee saved registers, for the fastcc calling convention.
52extern "C" {
53#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"
59 ".globl " ASMPREFIX "X86CompilationCallback\n"
60 ASMPREFIX "X86CompilationCallback:\n"
61 // 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"
88 "call " ASMPREFIX "X86CompilationCallback2\n"
89 // 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)
112#ifndef _MSC_VER
113 void X86CompilationCallback(void);
114 asm(
115 ".text\n"
116 ".align 8\n"
117 ".globl " ASMPREFIX "X86CompilationCallback\n"
118 ASMPREFIX "X86CompilationCallback:\n"
Anton Korobeynikov67fc5112007-12-10 14:54:42 +0000119 ".cfi_startproc\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 "pushl %ebp\n"
Anton Korobeynikov67fc5112007-12-10 14:54:42 +0000121 ".cfi_def_cfa_offset 8\n"
122 ".cfi_offset ebp, -8\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 "movl %esp, %ebp\n" // Standard prologue
Anton Korobeynikov67fc5112007-12-10 14:54:42 +0000124 ".cfi_def_cfa_register ebp\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 "pushl %eax\n"
Anton Korobeynikov67fc5112007-12-10 14:54:42 +0000126 ".cfi_rel_offset eax, 0\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 "pushl %edx\n" // Save EAX/EDX/ECX
Anton Korobeynikov67fc5112007-12-10 14:54:42 +0000128 ".cfi_rel_offset edx, 4\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 "pushl %ecx\n"
Anton Korobeynikov67fc5112007-12-10 14:54:42 +0000130 ".cfi_rel_offset ecx, 8\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131#if defined(__APPLE__)
132 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
133#endif
134 "subl $16, %esp\n"
135 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
136 "movl %eax, 4(%esp)\n"
137 "movl %ebp, (%esp)\n"
138 "call " ASMPREFIX "X86CompilationCallback2\n"
139 "movl %ebp, %esp\n" // Restore ESP
Anton Korobeynikov67fc5112007-12-10 14:54:42 +0000140 ".cfi_def_cfa_register esp\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 "subl $12, %esp\n"
Anton Korobeynikov67fc5112007-12-10 14:54:42 +0000142 ".cfi_adjust_cfa_offset 12\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 "popl %ecx\n"
Anton Korobeynikov67fc5112007-12-10 14:54:42 +0000144 ".cfi_adjust_cfa_offset -4\n"
145 ".cfi_restore ecx\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 "popl %edx\n"
Anton Korobeynikov67fc5112007-12-10 14:54:42 +0000147 ".cfi_adjust_cfa_offset -4\n"
148 ".cfi_restore edx\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 "popl %eax\n"
Anton Korobeynikov67fc5112007-12-10 14:54:42 +0000150 ".cfi_adjust_cfa_offset -4\n"
151 ".cfi_restore eax\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 "popl %ebp\n"
Anton Korobeynikov67fc5112007-12-10 14:54:42 +0000153 ".cfi_adjust_cfa_offset -4\n"
154 ".cfi_restore ebp\n"
155 "ret\n"
156 ".cfi_endproc\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157
158 // Same as X86CompilationCallback but also saves XMM argument registers.
159 void X86CompilationCallback_SSE(void);
160 asm(
161 ".text\n"
162 ".align 8\n"
163 ".globl " ASMPREFIX "X86CompilationCallback_SSE\n"
164 ASMPREFIX "X86CompilationCallback_SSE:\n"
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000165 ".cfi_startproc\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 "pushl %ebp\n"
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000167 ".cfi_def_cfa_offset 8\n"
168 ".cfi_offset ebp, -8\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 "movl %esp, %ebp\n" // Standard prologue
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000170 ".cfi_def_cfa_register ebp\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 "pushl %eax\n"
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000172 ".cfi_rel_offset eax, 0\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 "pushl %edx\n" // Save EAX/EDX/ECX
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000174 ".cfi_rel_offset edx, 4\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 "pushl %ecx\n"
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000176 ".cfi_rel_offset ecx, 8\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
178 // Save all XMM arg registers
179 "subl $64, %esp\n"
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000180 // FIXME: provide frame move information for xmm registers.
181 // This can be tricky, because CFA register is ebp (unaligned)
182 // and we need to produce offsets relative to it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 "movaps %xmm0, (%esp)\n"
184 "movaps %xmm1, 16(%esp)\n"
185 "movaps %xmm2, 32(%esp)\n"
186 "movaps %xmm3, 48(%esp)\n"
187 "subl $16, %esp\n"
188 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
189 "movl %eax, 4(%esp)\n"
190 "movl %ebp, (%esp)\n"
191 "call " ASMPREFIX "X86CompilationCallback2\n"
192 "addl $16, %esp\n"
193 "movaps 48(%esp), %xmm3\n"
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000194 ".cfi_restore xmm3\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 "movaps 32(%esp), %xmm2\n"
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000196 ".cfi_restore xmm2\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 "movaps 16(%esp), %xmm1\n"
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000198 ".cfi_restore xmm1\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 "movaps (%esp), %xmm0\n"
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000200 ".cfi_restore xmm0\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 "movl %ebp, %esp\n" // Restore ESP
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000202 ".cfi_def_cfa_register esp\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 "subl $12, %esp\n"
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000204 ".cfi_adjust_cfa_offset 12\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 "popl %ecx\n"
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000206 ".cfi_adjust_cfa_offset -4\n"
207 ".cfi_restore ecx\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 "popl %edx\n"
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000209 ".cfi_adjust_cfa_offset -4\n"
210 ".cfi_restore edx\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 "popl %eax\n"
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000212 ".cfi_adjust_cfa_offset -4\n"
213 ".cfi_restore eax\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 "popl %ebp\n"
Anton Korobeynikov4d1e0732007-12-10 15:13:55 +0000215 ".cfi_adjust_cfa_offset -4\n"
216 ".cfi_restore ebp\n"
217 "ret\n"
218 ".cfi_endproc\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219#else
220 void X86CompilationCallback2(void);
221
222 _declspec(naked) void X86CompilationCallback(void) {
223 __asm {
224 push eax
225 push edx
226 push ecx
227 call X86CompilationCallback2
228 pop ecx
229 pop edx
230 pop eax
231 ret
232 }
233 }
234#endif // _MSC_VER
235
236#else // Not an i386 host
237 void X86CompilationCallback() {
238 assert(0 && "Cannot call X86CompilationCallback() on a non-x86 arch!\n");
239 abort();
240 }
241#endif
242}
243
244/// X86CompilationCallback - This is the target-specific function invoked by the
245/// function stub when we did not know the real target of a call. This function
246/// must locate the start of the stub or call site and pass it into the JIT
247/// compiler function.
248#ifdef _MSC_VER
249extern "C" void X86CompilationCallback2() {
250 assert(sizeof(size_t) == 4); // FIXME: handle Win64
251 intptr_t *RetAddrLoc = (intptr_t *)_AddressOfReturnAddress();
252 RetAddrLoc += 4; // skip over ret addr, edx, eax, ecx
253 intptr_t RetAddr = *RetAddrLoc;
254#else
255extern "C" void X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr) {
256 intptr_t *RetAddrLoc = &StackPtr[1];
257#endif
258 assert(*RetAddrLoc == RetAddr &&
259 "Could not find return address on the stack!");
260
261 // It's a stub if there is an interrupt marker after the call.
262 bool isStub = ((unsigned char*)RetAddr)[0] == 0xCD;
263
264 // The call instruction should have pushed the return value onto the stack...
265#ifdef __x86_64__
266 RetAddr--; // Backtrack to the reference itself...
267#else
268 RetAddr -= 4; // Backtrack to the reference itself...
269#endif
270
271#if 0
272 DOUT << "In callback! Addr=" << (void*)RetAddr
273 << " ESP=" << (void*)StackPtr
274 << ": Resolving call to function: "
275 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n";
276#endif
277
278 // Sanity check to make sure this really is a call instruction.
279#ifdef __x86_64__
280 assert(((unsigned char*)RetAddr)[-2] == 0x41 &&"Not a call instr!");
281 assert(((unsigned char*)RetAddr)[-1] == 0xFF &&"Not a call instr!");
282#else
283 assert(((unsigned char*)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
284#endif
285
286 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
287
288 // Rewrite the call target... so that we don't end up here every time we
289 // execute the call.
290#ifdef __x86_64__
291 *(intptr_t *)(RetAddr - 0xa) = NewVal;
292#else
293 *(intptr_t *)RetAddr = (intptr_t)(NewVal-RetAddr-4);
294#endif
295
296 if (isStub) {
297 // If this is a stub, rewrite the call into an unconditional branch
298 // instruction so that two return addresses are not pushed onto the stack
299 // when the requested function finally gets called. This also makes the
300 // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
301#ifdef __x86_64__
302 ((unsigned char*)RetAddr)[0] = (2 | (4 << 3) | (3 << 6));
303#else
304 ((unsigned char*)RetAddr)[-1] = 0xE9;
305#endif
306 }
307
308 // Change the return address to reexecute the call instruction...
309#ifdef __x86_64__
310 *RetAddrLoc -= 0xd;
311#else
312 *RetAddrLoc -= 5;
313#endif
314}
315
316TargetJITInfo::LazyResolverFn
317X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
318 JITCompilerFunction = F;
319
320#if (defined(__i386__) || defined(i386) || defined(_M_IX86)) && \
321 !defined(_MSC_VER) && !defined(__x86_64__)
322 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
323 union {
324 unsigned u[3];
325 char c[12];
326 } text;
327
328 if (!X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1)) {
329 // FIXME: support for AMD family of processors.
330 if (memcmp(text.c, "GenuineIntel", 12) == 0) {
331 X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
332 if ((EDX >> 25) & 0x1)
333 return X86CompilationCallback_SSE;
334 }
335 }
336#endif
337
338 return X86CompilationCallback;
339}
340
341void *X86JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
342 // Note, we cast to intptr_t here to silence a -pedantic warning that
343 // complains about casting a function pointer to a normal pointer.
344#if (defined(__i386__) || defined(i386) || defined(_M_IX86)) && \
345 !defined(_MSC_VER) && !defined(__x86_64__)
346 bool NotCC = (Fn != (void*)(intptr_t)X86CompilationCallback &&
347 Fn != (void*)(intptr_t)X86CompilationCallback_SSE);
348#else
349 bool NotCC = Fn != (void*)(intptr_t)X86CompilationCallback;
350#endif
351 if (NotCC) {
352#ifdef __x86_64__
353 MCE.startFunctionStub(13, 4);
354 MCE.emitByte(0x49); // REX prefix
355 MCE.emitByte(0xB8+2); // movabsq r10
356 MCE.emitWordLE(((unsigned *)&Fn)[0]);
357 MCE.emitWordLE(((unsigned *)&Fn)[1]);
358 MCE.emitByte(0x41); // REX prefix
359 MCE.emitByte(0xFF); // jmpq *r10
360 MCE.emitByte(2 | (4 << 3) | (3 << 6));
361#else
362 MCE.startFunctionStub(5, 4);
363 MCE.emitByte(0xE9);
364 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
365#endif
366 return MCE.finishFunctionStub(0);
367 }
368
369#ifdef __x86_64__
370 MCE.startFunctionStub(14, 4);
371 MCE.emitByte(0x49); // REX prefix
372 MCE.emitByte(0xB8+2); // movabsq r10
373 MCE.emitWordLE(((unsigned *)&Fn)[0]);
374 MCE.emitWordLE(((unsigned *)&Fn)[1]);
375 MCE.emitByte(0x41); // REX prefix
376 MCE.emitByte(0xFF); // callq *r10
377 MCE.emitByte(2 | (2 << 3) | (3 << 6));
378#else
379 MCE.startFunctionStub(6, 4);
380 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
381
382 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
383#endif
384
385 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
386 return MCE.finishFunctionStub(0);
387}
388
389/// relocate - Before the JIT can run a block of code that has been emitted,
390/// it must rewrite the code to contain the actual addresses of any
391/// referenced global symbols.
392void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
393 unsigned NumRelocs, unsigned char* GOTBase) {
394 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
395 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
396 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
397 switch ((X86::RelocationType)MR->getRelocationType()) {
398 case X86::reloc_pcrel_word: {
399 // PC relative relocation, add the relocated value to the value already in
400 // memory, after we adjust it for where the PC is.
401 ResultPtr = ResultPtr-(intptr_t)RelocPos-4-MR->getConstantVal();
402 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
403 break;
404 }
405 case X86::reloc_absolute_word:
406 // Absolute relocation, just add the relocated value to the value already
407 // in memory.
408 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
409 break;
410 case X86::reloc_absolute_dword:
411 *((intptr_t*)RelocPos) += ResultPtr;
412 break;
413 }
414 }
415}