Improve compatibility with VC2005, patch by Morten Ofstad!
[oota-llvm.git] / lib / Target / X86 / X86JITInfo.cpp
1 //===-- 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 "llvm/CodeGen/MachineCodeEmitter.h"
18 #include "llvm/Config/alloca.h"
19 #include <cstdlib>
20 #include <iostream>
21 using namespace llvm;
22
23 #ifdef _MSC_VER
24   extern "C" void *_AddressOfReturnAddress(void);
25   #pragma intrinsic(_AddressOfReturnAddress)
26 #endif
27
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.
35 }
36
37
38 /// JITCompilerFunction - This contains the address of the JIT function used to
39 /// compile a function lazily.
40 static TargetJITInfo::JITCompilerFn JITCompilerFunction;
41
42 // Provide a wrapper for X86CompilationCallback2 that saves non-traditional
43 // callee saved registers, for the fastcc calling convention.
44 extern "C" {
45 #if defined(__i386__) || defined(i386) || defined(_M_IX86)
46 #ifndef _MSC_VER
47   void X86CompilationCallback(void);
48   asm(
49     ".text\n"
50     ".align 8\n"
51 #if defined(__CYGWIN__) || defined(__APPLE__)
52     ".globl _X86CompilationCallback\n"
53   "_X86CompilationCallback:\n"
54 #else
55     ".globl X86CompilationCallback\n"
56   "X86CompilationCallback:\n"
57 #endif
58     "pushl   %ebp\n"
59     "movl    %esp, %ebp\n"    // Standard prologue
60     "pushl   %eax\n"
61     "pushl   %edx\n"          // save EAX/EDX
62 #if defined(__CYGWIN__) || defined(__APPLE__)
63     "call _X86CompilationCallback2\n"
64 #else
65     "call X86CompilationCallback2\n"
66 #endif
67     "popl    %edx\n"
68     "popl    %eax\n"
69     "popl    %ebp\n"
70     "ret\n");
71 #else
72   void X86CompilationCallback2(void);
73
74   _declspec(naked) void X86CompilationCallback(void) {
75     __asm {
76       push  eax
77       push  edx
78       call  X86CompilationCallback2
79       pop   edx
80       pop   eax
81       ret
82     }
83   }
84 #endif // _MSC_VER
85
86 #else // Not an i386 host
87   void X86CompilationCallback() {
88     std::cerr << "Cannot call X86CompilationCallback() on a non-x86 arch!\n";
89     abort();
90   }
91 #endif
92 }
93
94 /// X86CompilationCallback - This is the target-specific function invoked by the
95 /// function stub when we did not know the real target of a call.  This function
96 /// must locate the start of the stub or call site and pass it into the JIT
97 /// compiler function.
98 extern "C" void X86CompilationCallback2() {
99 #ifdef _MSC_VER
100   assert(sizeof(size_t) == 4); // FIXME: handle Win64
101   unsigned *RetAddrLoc = (unsigned *)_AddressOfReturnAddress();
102   RetAddrLoc += 3;  // skip over ret addr, edx, eax
103   unsigned RetAddr = *RetAddrLoc;
104 #else
105   unsigned *StackPtr = (unsigned*)__builtin_frame_address(1);
106   unsigned RetAddr = (unsigned)(intptr_t)__builtin_return_address(1);
107   unsigned *RetAddrLoc = &StackPtr[1];
108
109   // NOTE: __builtin_frame_address doesn't work if frame pointer elimination has
110   // been performed.  Having a variable sized alloca disables frame pointer
111   // elimination currently, even if it's dead.  This is a gross hack.
112   alloca(10+(RetAddr >> 31));
113
114 #endif
115   assert(*RetAddrLoc == RetAddr &&
116          "Could not find return address on the stack!");
117
118   // It's a stub if there is an interrupt marker after the call.
119   bool isStub = ((unsigned char*)(intptr_t)RetAddr)[0] == 0xCD;
120
121   // The call instruction should have pushed the return value onto the stack...
122   RetAddr -= 4;  // Backtrack to the reference itself...
123
124 #if 0
125   DEBUG(std::cerr << "In callback! Addr=" << (void*)RetAddr
126                   << " ESP=" << (void*)StackPtr
127                   << ": Resolving call to function: "
128                   << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
129 #endif
130
131   // Sanity check to make sure this really is a call instruction.
132   assert(((unsigned char*)(intptr_t)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
133
134   unsigned NewVal = (intptr_t)JITCompilerFunction((void*)(intptr_t)RetAddr);
135
136   // Rewrite the call target... so that we don't end up here every time we
137   // execute the call.
138   *(unsigned*)(intptr_t)RetAddr = NewVal-RetAddr-4;
139
140   if (isStub) {
141     // If this is a stub, rewrite the call into an unconditional branch
142     // instruction so that two return addresses are not pushed onto the stack
143     // when the requested function finally gets called.  This also makes the
144     // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
145     ((unsigned char*)(intptr_t)RetAddr)[-1] = 0xE9;
146   }
147
148   // Change the return address to reexecute the call instruction...
149   *RetAddrLoc -= 5;
150 }
151
152 TargetJITInfo::LazyResolverFn
153 X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
154   JITCompilerFunction = F;
155   return X86CompilationCallback;
156 }
157
158 void *X86JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
159   if (Fn != X86CompilationCallback) {
160     MCE.startFunctionStub(5);
161     MCE.emitByte(0xE9);
162     MCE.emitWord((intptr_t)Fn-MCE.getCurrentPCValue()-4);
163     return MCE.finishFunctionStub(0);
164   }
165
166   MCE.startFunctionStub(6);
167   MCE.emitByte(0xE8);   // Call with 32 bit pc-rel destination...
168
169   MCE.emitWord((intptr_t)Fn-MCE.getCurrentPCValue()-4);
170
171   MCE.emitByte(0xCD);   // Interrupt - Just a marker identifying the stub!
172   return MCE.finishFunctionStub(0);
173 }
174
175 /// relocate - Before the JIT can run a block of code that has been emitted,
176 /// it must rewrite the code to contain the actual addresses of any
177 /// referenced global symbols.
178 void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
179                           unsigned NumRelocs, unsigned char* GOTBase) {
180   for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
181     void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
182     intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
183     switch ((X86::RelocationType)MR->getRelocationType()) {
184     case X86::reloc_pcrel_word:
185       // PC relative relocation, add the relocated value to the value already in
186       // memory, after we adjust it for where the PC is.
187       ResultPtr = ResultPtr-(intptr_t)RelocPos-4;
188       *((intptr_t*)RelocPos) += ResultPtr;
189       break;
190     case X86::reloc_absolute_word:
191       // Absolute relocation, just add the relocated value to the value already
192       // in memory.
193       *((intptr_t*)RelocPos) += ResultPtr;
194       break;
195     }
196   }
197 }