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 "llvm/CodeGen/MachineCodeEmitter.h"
18 #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 // Provide a wrapper for X86CompilationCallback2 that saves non-traditional
43 // callee saved registers, for the fastcc calling convention.
45 #if defined(__i386__) || defined(i386) || defined(_M_IX86)
47 void X86CompilationCallback(void);
51 #if defined(__CYGWIN__) || defined(__APPLE__) || defined(__MINGW32__)
52 ".globl _X86CompilationCallback\n"
53 "_X86CompilationCallback:\n"
55 ".globl X86CompilationCallback\n"
56 "X86CompilationCallback:\n"
59 "movl %esp, %ebp\n" // Standard prologue
60 #if FASTCC_NUM_INT_ARGS_INREGS > 0
62 "pushl %edx\n" // Save EAX/EDX
64 #if defined(__APPLE__)
65 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
68 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
69 "movl %eax, 4(%esp)\n"
71 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__APPLE__)
72 "call _X86CompilationCallback2\n"
74 "call X86CompilationCallback2\n"
76 "movl %ebp, %esp\n" // Restore ESP
77 #if FASTCC_NUM_INT_ARGS_INREGS > 0
85 void X86CompilationCallback2(void);
87 _declspec(naked) void X86CompilationCallback(void) {
91 call X86CompilationCallback2
99 #else // Not an i386 host
100 void X86CompilationCallback() {
101 std::cerr << "Cannot call X86CompilationCallback() on a non-x86 arch!\n";
107 /// X86CompilationCallback - This is the target-specific function invoked by the
108 /// function stub when we did not know the real target of a call. This function
109 /// must locate the start of the stub or call site and pass it into the JIT
110 /// compiler function.
112 extern "C" void X86CompilationCallback2() {
113 assert(sizeof(size_t) == 4); // FIXME: handle Win64
114 unsigned *RetAddrLoc = (unsigned *)_AddressOfReturnAddress();
115 RetAddrLoc += 3; // skip over ret addr, edx, eax
116 unsigned RetAddr = *RetAddrLoc;
118 extern "C" void X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr) {
119 intptr_t *RetAddrLoc = &StackPtr[1];
121 assert(*RetAddrLoc == RetAddr &&
122 "Could not find return address on the stack!");
124 // It's a stub if there is an interrupt marker after the call.
125 bool isStub = ((unsigned char*)(intptr_t)RetAddr)[0] == 0xCD;
127 // The call instruction should have pushed the return value onto the stack...
128 RetAddr -= 4; // Backtrack to the reference itself...
131 DEBUG(std::cerr << "In callback! Addr=" << (void*)RetAddr
132 << " ESP=" << (void*)StackPtr
133 << ": Resolving call to function: "
134 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
137 // Sanity check to make sure this really is a call instruction.
138 assert(((unsigned char*)(intptr_t)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
140 unsigned NewVal = (intptr_t)JITCompilerFunction((void*)(intptr_t)RetAddr);
142 // Rewrite the call target... so that we don't end up here every time we
144 *(unsigned*)(intptr_t)RetAddr = NewVal-RetAddr-4;
147 // If this is a stub, rewrite the call into an unconditional branch
148 // instruction so that two return addresses are not pushed onto the stack
149 // when the requested function finally gets called. This also makes the
150 // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
151 ((unsigned char*)(intptr_t)RetAddr)[-1] = 0xE9;
154 // Change the return address to reexecute the call instruction...
158 TargetJITInfo::LazyResolverFn
159 X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
160 JITCompilerFunction = F;
161 return X86CompilationCallback;
164 void *X86JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
165 // Note, we cast to intptr_t here to silence a -pedantic warning that
166 // complains about casting a function pointer to a normal pointer.
167 if (Fn != (void*)(intptr_t)X86CompilationCallback) {
168 MCE.startFunctionStub(5);
170 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
171 return MCE.finishFunctionStub(0);
174 MCE.startFunctionStub(6);
175 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
177 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
179 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
180 return MCE.finishFunctionStub(0);
183 /// relocate - Before the JIT can run a block of code that has been emitted,
184 /// it must rewrite the code to contain the actual addresses of any
185 /// referenced global symbols.
186 void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
187 unsigned NumRelocs, unsigned char* GOTBase) {
188 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
189 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
190 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
191 switch ((X86::RelocationType)MR->getRelocationType()) {
192 case X86::reloc_pcrel_word:
193 // PC relative relocation, add the relocated value to the value already in
194 // memory, after we adjust it for where the PC is.
195 ResultPtr = ResultPtr-(intptr_t)RelocPos-4;
196 *((intptr_t*)RelocPos) += ResultPtr;
198 case X86::reloc_absolute_word:
199 // Absolute relocation, just add the relocated value to the value already
201 *((intptr_t*)RelocPos) += ResultPtr;
207 void X86JITInfo::resolveBBRefs(MachineCodeEmitter &MCE) {
208 // Resolve all forward branches now.
209 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
210 unsigned Location = MCE.getMachineBasicBlockAddress(BBRefs[i].first);
211 intptr_t Ref = BBRefs[i].second;
212 *((unsigned*)Ref) = Location-Ref-4;