1 //===-- X86JITInfo.cpp - Implement the JIT interfaces for the X86 target --===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the JIT interfaces for the X86 target.
12 //===----------------------------------------------------------------------===//
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"
24 extern "C" void *_AddressOfReturnAddress(void);
25 #pragma intrinsic(_AddressOfReturnAddress)
28 void 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.
38 /// JITCompilerFunction - This contains the address of the JIT function used to
39 /// compile a function lazily.
40 static TargetJITInfo::JITCompilerFn JITCompilerFunction;
42 // Get the ASMPREFIX for the current host. This is often '_'.
43 #ifndef __USER_LABEL_PREFIX__
44 #define __USER_LABEL_PREFIX__
46 #define GETASMPREFIX2(X) #X
47 #define GETASMPREFIX(X) GETASMPREFIX2(X)
48 #define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
50 // Provide a wrapper for X86CompilationCallback2 that saves non-traditional
51 // callee saved registers, for the fastcc calling convention.
53 #if defined(__x86_64__)
54 // No need to save EAX/EDX for X86-64.
55 void X86CompilationCallback(void);
59 ".globl " ASMPREFIX "X86CompilationCallback\n"
60 ASMPREFIX "X86CompilationCallback:\n"
65 // Save all int arg registers
72 // Align stack on 16-byte boundary. ESP might not be properly aligned
73 // (8 byte) if this is called from an indirect stub.
75 // Save all XMM arg registers
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"
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"
100 // Restore all int arg registers
111 #elif defined(__i386__) || defined(i386) || defined(_M_IX86)
113 void X86CompilationCallback(void);
117 ".globl " ASMPREFIX "X86CompilationCallback\n"
118 ASMPREFIX "X86CompilationCallback:\n"
121 ".cfi_def_cfa_offset 8\n"
122 ".cfi_offset ebp, -8\n"
123 "movl %esp, %ebp\n" // Standard prologue
124 ".cfi_def_cfa_register ebp\n"
126 ".cfi_rel_offset eax, 0\n"
127 "pushl %edx\n" // Save EAX/EDX/ECX
128 ".cfi_rel_offset edx, 4\n"
130 ".cfi_rel_offset ecx, 8\n"
131 #if defined(__APPLE__)
132 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
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
140 ".cfi_def_cfa_register esp\n"
142 ".cfi_adjust_cfa_offset 12\n"
144 ".cfi_adjust_cfa_offset -4\n"
147 ".cfi_adjust_cfa_offset -4\n"
150 ".cfi_adjust_cfa_offset -4\n"
153 ".cfi_adjust_cfa_offset -4\n"
158 // Same as X86CompilationCallback but also saves XMM argument registers.
159 void X86CompilationCallback_SSE(void);
163 ".globl " ASMPREFIX "X86CompilationCallback_SSE\n"
164 ASMPREFIX "X86CompilationCallback_SSE:\n"
166 "movl %esp, %ebp\n" // Standard prologue
168 "pushl %edx\n" // Save EAX/EDX/ECX
170 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
171 // Save all XMM arg registers
173 "movaps %xmm0, (%esp)\n"
174 "movaps %xmm1, 16(%esp)\n"
175 "movaps %xmm2, 32(%esp)\n"
176 "movaps %xmm3, 48(%esp)\n"
178 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
179 "movl %eax, 4(%esp)\n"
180 "movl %ebp, (%esp)\n"
181 "call " ASMPREFIX "X86CompilationCallback2\n"
183 "movaps 48(%esp), %xmm3\n"
184 "movaps 32(%esp), %xmm2\n"
185 "movaps 16(%esp), %xmm1\n"
186 "movaps (%esp), %xmm0\n"
187 "movl %ebp, %esp\n" // Restore ESP
195 void X86CompilationCallback2(void);
197 _declspec(naked) void X86CompilationCallback(void) {
202 call X86CompilationCallback2
211 #else // Not an i386 host
212 void X86CompilationCallback() {
213 assert(0 && "Cannot call X86CompilationCallback() on a non-x86 arch!\n");
219 /// X86CompilationCallback - This is the target-specific function invoked by the
220 /// function stub when we did not know the real target of a call. This function
221 /// must locate the start of the stub or call site and pass it into the JIT
222 /// compiler function.
224 extern "C" void X86CompilationCallback2() {
225 assert(sizeof(size_t) == 4); // FIXME: handle Win64
226 intptr_t *RetAddrLoc = (intptr_t *)_AddressOfReturnAddress();
227 RetAddrLoc += 4; // skip over ret addr, edx, eax, ecx
228 intptr_t RetAddr = *RetAddrLoc;
230 extern "C" void X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr) {
231 intptr_t *RetAddrLoc = &StackPtr[1];
233 assert(*RetAddrLoc == RetAddr &&
234 "Could not find return address on the stack!");
236 // It's a stub if there is an interrupt marker after the call.
237 bool isStub = ((unsigned char*)RetAddr)[0] == 0xCD;
239 // The call instruction should have pushed the return value onto the stack...
241 RetAddr--; // Backtrack to the reference itself...
243 RetAddr -= 4; // Backtrack to the reference itself...
247 DOUT << "In callback! Addr=" << (void*)RetAddr
248 << " ESP=" << (void*)StackPtr
249 << ": Resolving call to function: "
250 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n";
253 // Sanity check to make sure this really is a call instruction.
255 assert(((unsigned char*)RetAddr)[-2] == 0x41 &&"Not a call instr!");
256 assert(((unsigned char*)RetAddr)[-1] == 0xFF &&"Not a call instr!");
258 assert(((unsigned char*)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
261 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
263 // Rewrite the call target... so that we don't end up here every time we
266 *(intptr_t *)(RetAddr - 0xa) = NewVal;
268 *(intptr_t *)RetAddr = (intptr_t)(NewVal-RetAddr-4);
272 // If this is a stub, rewrite the call into an unconditional branch
273 // instruction so that two return addresses are not pushed onto the stack
274 // when the requested function finally gets called. This also makes the
275 // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
277 ((unsigned char*)RetAddr)[0] = (2 | (4 << 3) | (3 << 6));
279 ((unsigned char*)RetAddr)[-1] = 0xE9;
283 // Change the return address to reexecute the call instruction...
291 TargetJITInfo::LazyResolverFn
292 X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
293 JITCompilerFunction = F;
295 #if (defined(__i386__) || defined(i386) || defined(_M_IX86)) && \
296 !defined(_MSC_VER) && !defined(__x86_64__)
297 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
303 if (!X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1)) {
304 // FIXME: support for AMD family of processors.
305 if (memcmp(text.c, "GenuineIntel", 12) == 0) {
306 X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
307 if ((EDX >> 25) & 0x1)
308 return X86CompilationCallback_SSE;
313 return X86CompilationCallback;
316 void *X86JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
317 // Note, we cast to intptr_t here to silence a -pedantic warning that
318 // complains about casting a function pointer to a normal pointer.
319 #if (defined(__i386__) || defined(i386) || defined(_M_IX86)) && \
320 !defined(_MSC_VER) && !defined(__x86_64__)
321 bool NotCC = (Fn != (void*)(intptr_t)X86CompilationCallback &&
322 Fn != (void*)(intptr_t)X86CompilationCallback_SSE);
324 bool NotCC = Fn != (void*)(intptr_t)X86CompilationCallback;
328 MCE.startFunctionStub(13, 4);
329 MCE.emitByte(0x49); // REX prefix
330 MCE.emitByte(0xB8+2); // movabsq r10
331 MCE.emitWordLE(((unsigned *)&Fn)[0]);
332 MCE.emitWordLE(((unsigned *)&Fn)[1]);
333 MCE.emitByte(0x41); // REX prefix
334 MCE.emitByte(0xFF); // jmpq *r10
335 MCE.emitByte(2 | (4 << 3) | (3 << 6));
337 MCE.startFunctionStub(5, 4);
339 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
341 return MCE.finishFunctionStub(0);
345 MCE.startFunctionStub(14, 4);
346 MCE.emitByte(0x49); // REX prefix
347 MCE.emitByte(0xB8+2); // movabsq r10
348 MCE.emitWordLE(((unsigned *)&Fn)[0]);
349 MCE.emitWordLE(((unsigned *)&Fn)[1]);
350 MCE.emitByte(0x41); // REX prefix
351 MCE.emitByte(0xFF); // callq *r10
352 MCE.emitByte(2 | (2 << 3) | (3 << 6));
354 MCE.startFunctionStub(6, 4);
355 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
357 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
360 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
361 return MCE.finishFunctionStub(0);
364 /// relocate - Before the JIT can run a block of code that has been emitted,
365 /// it must rewrite the code to contain the actual addresses of any
366 /// referenced global symbols.
367 void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
368 unsigned NumRelocs, unsigned char* GOTBase) {
369 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
370 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
371 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
372 switch ((X86::RelocationType)MR->getRelocationType()) {
373 case X86::reloc_pcrel_word: {
374 // PC relative relocation, add the relocated value to the value already in
375 // memory, after we adjust it for where the PC is.
376 ResultPtr = ResultPtr-(intptr_t)RelocPos-4-MR->getConstantVal();
377 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
380 case X86::reloc_absolute_word:
381 // Absolute relocation, just add the relocated value to the value already
383 *((unsigned*)RelocPos) += (unsigned)ResultPtr;
385 case X86::reloc_absolute_dword:
386 *((intptr_t*)RelocPos) += ResultPtr;