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"
21 void X86JITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
22 unsigned char *OldByte = (unsigned char *)Old;
23 *OldByte++ = 0xE9; // Emit JMP opcode.
24 unsigned *OldWord = (unsigned *)OldByte;
25 unsigned NewAddr = (intptr_t)New;
26 unsigned OldAddr = (intptr_t)OldWord;
27 *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
32 #pragma optimize("y", off)
35 /// JITCompilerFunction - This contains the address of the JIT function used to
36 /// compile a function lazily.
37 static TargetJITInfo::JITCompilerFn JITCompilerFunction;
39 // Provide a wrapper for X86CompilationCallback2 that saves non-traditional
40 // callee saved registers, for the fastcc calling convention.
41 extern "C" void X86CompilationCallback(void);
43 #if defined(__i386__) || defined(i386)
48 ".globl X86CompilationCallback\n"
49 "X86CompilationCallback:\n"
51 "movl %esp, %ebp\n" // Standard prologue
53 "pushl %edx\n" // save EAX/EDX
54 "call X86CompilationCallback2\n"
60 // FIXME: implement this for VC++
65 void X86CompilationCallback() {
66 assert(0 && "This is not a X86, you can't execute this!");
71 /// X86CompilationCallback - This is the target-specific function invoked by the
72 /// function stub when we did not know the real target of a call. This function
73 /// must locate the start of the stub or call site and pass it into the JIT
74 /// compiler function.
75 extern "C" void X86CompilationCallback2() {
77 // FIXME: This needs to go up one more level!
78 unsigned *StackPtr, RetAddr;
79 __asm mov StackPtr, ebp;
80 __asm mov eax, DWORD PTR [ebp + 4];
81 __asm mov RetAddr, eax;
83 unsigned *StackPtr = (unsigned*)__builtin_frame_address(1);
84 unsigned RetAddr = (unsigned)(intptr_t)__builtin_return_address(1);
86 // NOTE: __builtin_frame_address doesn't work if frame pointer elimination has
87 // been performed. Having a variable sized alloca disables frame pointer
88 // elimination currently, even if it's dead. This is a gross hack.
89 alloca(10+(RetAddr >> 31));
92 assert(StackPtr[1] == RetAddr &&
93 "Could not find return address on the stack!");
95 // It's a stub if there is an interrupt marker after the call.
96 bool isStub = ((unsigned char*)(intptr_t)RetAddr)[0] == 0xCD;
98 // The call instruction should have pushed the return value onto the stack...
99 RetAddr -= 4; // Backtrack to the reference itself...
102 DEBUG(std::cerr << "In callback! Addr=" << (void*)RetAddr
103 << " ESP=" << (void*)StackPtr
104 << ": Resolving call to function: "
105 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
108 // Sanity check to make sure this really is a call instruction.
109 assert(((unsigned char*)(intptr_t)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
111 unsigned NewVal = (intptr_t)JITCompilerFunction((void*)(intptr_t)RetAddr);
113 // Rewrite the call target... so that we don't end up here every time we
115 *(unsigned*)(intptr_t)RetAddr = NewVal-RetAddr-4;
118 // If this is a stub, rewrite the call into an unconditional branch
119 // instruction so that two return addresses are not pushed onto the stack
120 // when the requested function finally gets called. This also makes the
121 // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
122 ((unsigned char*)(intptr_t)RetAddr)[-1] = 0xE9;
125 // Change the return address to reexecute the call instruction...
130 #pragma optimize( "", on )
133 TargetJITInfo::LazyResolverFn
134 X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
135 JITCompilerFunction = F;
136 return X86CompilationCallback;
139 void *X86JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
140 if (Fn != X86CompilationCallback) {
141 MCE.startFunctionStub(5);
143 MCE.emitWord((intptr_t)Fn-MCE.getCurrentPCValue()-4);
144 return MCE.finishFunctionStub(0);
147 MCE.startFunctionStub(6);
148 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
150 MCE.emitWord((intptr_t)Fn-MCE.getCurrentPCValue()-4);
152 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
153 return MCE.finishFunctionStub(0);
156 /// relocate - Before the JIT can run a block of code that has been emitted,
157 /// it must rewrite the code to contain the actual addresses of any
158 /// referenced global symbols.
159 void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
160 unsigned NumRelocs) {
161 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
162 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
163 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
164 switch ((X86::RelocationType)MR->getRelocationType()) {
165 case X86::reloc_pcrel_word:
166 // PC relative relocation, add the relocated value to the value already in
167 // memory, after we adjust it for where the PC is.
168 ResultPtr = ResultPtr-(intptr_t)RelocPos-4;
169 *((intptr_t*)RelocPos) += ResultPtr;
171 case X86::reloc_absolute_word:
172 // Absolute relocation, just add the relocated value to the value already
174 *((intptr_t*)RelocPos) += ResultPtr;