blob: 4fef094a3facb3e90fd5b39953a80772fbe4b529 [file] [log] [blame]
Chris Lattner7ddde322004-11-20 23:54:33 +00001//===-- X86JITInfo.cpp - Implement the JIT interfaces for the X86 target --===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
Chris Lattner7ddde322004-11-20 23:54:33 +00003// 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.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
Chris Lattner7ddde322004-11-20 23:54:33 +00008//===----------------------------------------------------------------------===//
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"
19using namespace llvm;
20
Chris Lattner7ddde322004-11-20 23:54:33 +000021void 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.
28}
29
30
Chris Lattner7ddde322004-11-20 23:54:33 +000031/// JITCompilerFunction - This contains the address of the JIT function used to
32/// compile a function lazily.
33static TargetJITInfo::JITCompilerFn JITCompilerFunction;
34
Chris Lattner1030f722005-05-19 06:49:17 +000035// Provide a wrapper for X86CompilationCallback2 that saves non-traditional
36// callee saved registers, for the fastcc calling convention.
Jeff Cohen8bc6f932005-05-20 01:35:39 +000037extern "C" {
38#if defined(__i386__) || defined(i386) || defined(_M_IX86)
Chris Lattner1030f722005-05-19 06:49:17 +000039#ifndef _MSC_VER
Jeff Cohen8bc6f932005-05-20 01:35:39 +000040 void X86CompilationCallback(void);
41 asm(
42 ".text\n"
43 ".align 8\n"
44 ".globl X86CompilationCallback\n"
45 "X86CompilationCallback:\n"
46 "pushl %ebp\n"
47 "movl %esp, %ebp\n" // Standard prologue
48 "pushl %eax\n"
49 "pushl %edx\n" // save EAX/EDX
50 "call X86CompilationCallback2\n"
51 "popl %edx\n"
52 "popl %eax\n"
53 "popl %ebp\n"
54 "ret\n");
Chris Lattner1030f722005-05-19 06:49:17 +000055#else
Jeff Cohen8bc6f932005-05-20 01:35:39 +000056 extern "C" void *_AddressOfReturnAddress(void);
57 #pragma intrinsic(_AddressOfReturnAddress)
58
59 void X86CompilationCallback2(void);
60
61 _declspec(naked) void X86CompilationCallback(void) {
62 __asm {
63 push eax
64 push edx
65 call X86CompilationCallback2
66 pop edx
67 pop eax
68 ret
69 }
70 }
Chris Lattner1030f722005-05-19 06:49:17 +000071#endif
72
73#else
Jeff Cohen8bc6f932005-05-20 01:35:39 +000074 // Not an i386 host
75 void X86CompilationCallback() {
76 assert(0 && "This is not a X86, you can't execute this!");
77 abort();
78 }
Chris Lattner1030f722005-05-19 06:49:17 +000079#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +000080}
Chris Lattner1030f722005-05-19 06:49:17 +000081
82/// X86CompilationCallback - This is the target-specific function invoked by the
Chris Lattner7ddde322004-11-20 23:54:33 +000083/// function stub when we did not know the real target of a call. This function
84/// must locate the start of the stub or call site and pass it into the JIT
85/// compiler function.
Chris Lattner1030f722005-05-19 06:49:17 +000086extern "C" void X86CompilationCallback2() {
Chris Lattner7ddde322004-11-20 23:54:33 +000087#ifdef _MSC_VER
Jeff Cohen8bc6f932005-05-20 01:35:39 +000088 assert(sizeof(size_t) == 4); // FIXME: handle Win64
89 unsigned *RetAddrLoc = (unsigned *)_AddressOfReturnAddress();
90 RetAddrLoc += 3; // skip over ret addr, edx, eax
91 unsigned RetAddr = *RetAddrLoc;
Chris Lattner7ddde322004-11-20 23:54:33 +000092#else
Chris Lattner1030f722005-05-19 06:49:17 +000093 unsigned *StackPtr = (unsigned*)__builtin_frame_address(1);
94 unsigned RetAddr = (unsigned)(intptr_t)__builtin_return_address(1);
Jeff Cohen8bc6f932005-05-20 01:35:39 +000095 unsigned *RetAddrLoc = &StackPtr[1];
Chris Lattner7ddde322004-11-20 23:54:33 +000096
97 // NOTE: __builtin_frame_address doesn't work if frame pointer elimination has
98 // been performed. Having a variable sized alloca disables frame pointer
99 // elimination currently, even if it's dead. This is a gross hack.
100 alloca(10+(RetAddr >> 31));
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000101
Chris Lattner7ddde322004-11-20 23:54:33 +0000102#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000103 assert(*RetAddrLoc == RetAddr &&
Chris Lattner7ddde322004-11-20 23:54:33 +0000104 "Could not find return address on the stack!");
105
106 // It's a stub if there is an interrupt marker after the call.
107 bool isStub = ((unsigned char*)(intptr_t)RetAddr)[0] == 0xCD;
108
109 // The call instruction should have pushed the return value onto the stack...
110 RetAddr -= 4; // Backtrack to the reference itself...
111
112#if 0
113 DEBUG(std::cerr << "In callback! Addr=" << (void*)RetAddr
114 << " ESP=" << (void*)StackPtr
115 << ": Resolving call to function: "
116 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
117#endif
118
119 // Sanity check to make sure this really is a call instruction.
120 assert(((unsigned char*)(intptr_t)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000121
Chris Lattner7ddde322004-11-20 23:54:33 +0000122 unsigned NewVal = (intptr_t)JITCompilerFunction((void*)(intptr_t)RetAddr);
123
124 // Rewrite the call target... so that we don't end up here every time we
125 // execute the call.
126 *(unsigned*)(intptr_t)RetAddr = NewVal-RetAddr-4;
127
128 if (isStub) {
129 // If this is a stub, rewrite the call into an unconditional branch
130 // instruction so that two return addresses are not pushed onto the stack
131 // when the requested function finally gets called. This also makes the
132 // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
133 ((unsigned char*)(intptr_t)RetAddr)[-1] = 0xE9;
134 }
135
136 // Change the return address to reexecute the call instruction...
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000137 *RetAddrLoc -= 5;
Chris Lattner7ddde322004-11-20 23:54:33 +0000138}
139
Chris Lattner7ddde322004-11-20 23:54:33 +0000140TargetJITInfo::LazyResolverFn
141X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
142 JITCompilerFunction = F;
Chris Lattner1030f722005-05-19 06:49:17 +0000143 return X86CompilationCallback;
Chris Lattner7ddde322004-11-20 23:54:33 +0000144}
145
Chris Lattner90b1b452004-11-22 22:25:30 +0000146void *X86JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
Chris Lattner1030f722005-05-19 06:49:17 +0000147 if (Fn != X86CompilationCallback) {
Chris Lattner90b1b452004-11-22 22:25:30 +0000148 MCE.startFunctionStub(5);
149 MCE.emitByte(0xE9);
150 MCE.emitWord((intptr_t)Fn-MCE.getCurrentPCValue()-4);
151 return MCE.finishFunctionStub(0);
152 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000153
Chris Lattner90b1b452004-11-22 22:25:30 +0000154 MCE.startFunctionStub(6);
155 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
156
157 MCE.emitWord((intptr_t)Fn-MCE.getCurrentPCValue()-4);
158
159 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
160 return MCE.finishFunctionStub(0);
161}
Chris Lattner7ddde322004-11-20 23:54:33 +0000162
163/// relocate - Before the JIT can run a block of code that has been emitted,
164/// it must rewrite the code to contain the actual addresses of any
165/// referenced global symbols.
166void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
167 unsigned NumRelocs) {
168 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
169 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
170 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
171 switch ((X86::RelocationType)MR->getRelocationType()) {
172 case X86::reloc_pcrel_word:
173 // PC relative relocation, add the relocated value to the value already in
174 // memory, after we adjust it for where the PC is.
175 ResultPtr = ResultPtr-(intptr_t)RelocPos-4;
176 *((intptr_t*)RelocPos) += ResultPtr;
177 break;
178 case X86::reloc_absolute_word:
179 // Absolute relocation, just add the relocated value to the value already
180 // in memory.
181 *((intptr_t*)RelocPos) += ResultPtr;
182 break;
183 }
184 }
185}