blob: 5c4e53257b734a45a8318ccc6014b33fe62a9275 [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!");
Jeff Cohen8bc6f932005-05-20 01:35:39 +000077 }
Chris Lattner1030f722005-05-19 06:49:17 +000078#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +000079}
Chris Lattner1030f722005-05-19 06:49:17 +000080
81/// X86CompilationCallback - This is the target-specific function invoked by the
Chris Lattner7ddde322004-11-20 23:54:33 +000082/// function stub when we did not know the real target of a call. This function
83/// must locate the start of the stub or call site and pass it into the JIT
84/// compiler function.
Chris Lattner1030f722005-05-19 06:49:17 +000085extern "C" void X86CompilationCallback2() {
Chris Lattner7ddde322004-11-20 23:54:33 +000086#ifdef _MSC_VER
Jeff Cohen8bc6f932005-05-20 01:35:39 +000087 assert(sizeof(size_t) == 4); // FIXME: handle Win64
88 unsigned *RetAddrLoc = (unsigned *)_AddressOfReturnAddress();
89 RetAddrLoc += 3; // skip over ret addr, edx, eax
90 unsigned RetAddr = *RetAddrLoc;
Chris Lattner7ddde322004-11-20 23:54:33 +000091#else
Chris Lattner1030f722005-05-19 06:49:17 +000092 unsigned *StackPtr = (unsigned*)__builtin_frame_address(1);
93 unsigned RetAddr = (unsigned)(intptr_t)__builtin_return_address(1);
Jeff Cohen8bc6f932005-05-20 01:35:39 +000094 unsigned *RetAddrLoc = &StackPtr[1];
Chris Lattner7ddde322004-11-20 23:54:33 +000095
96 // NOTE: __builtin_frame_address doesn't work if frame pointer elimination has
97 // been performed. Having a variable sized alloca disables frame pointer
98 // elimination currently, even if it's dead. This is a gross hack.
99 alloca(10+(RetAddr >> 31));
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000100
Chris Lattner7ddde322004-11-20 23:54:33 +0000101#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000102 assert(*RetAddrLoc == RetAddr &&
Chris Lattner7ddde322004-11-20 23:54:33 +0000103 "Could not find return address on the stack!");
104
105 // It's a stub if there is an interrupt marker after the call.
106 bool isStub = ((unsigned char*)(intptr_t)RetAddr)[0] == 0xCD;
107
108 // The call instruction should have pushed the return value onto the stack...
109 RetAddr -= 4; // Backtrack to the reference itself...
110
111#if 0
112 DEBUG(std::cerr << "In callback! Addr=" << (void*)RetAddr
113 << " ESP=" << (void*)StackPtr
114 << ": Resolving call to function: "
115 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
116#endif
117
118 // Sanity check to make sure this really is a call instruction.
119 assert(((unsigned char*)(intptr_t)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000120
Chris Lattner7ddde322004-11-20 23:54:33 +0000121 unsigned NewVal = (intptr_t)JITCompilerFunction((void*)(intptr_t)RetAddr);
122
123 // Rewrite the call target... so that we don't end up here every time we
124 // execute the call.
125 *(unsigned*)(intptr_t)RetAddr = NewVal-RetAddr-4;
126
127 if (isStub) {
128 // If this is a stub, rewrite the call into an unconditional branch
129 // instruction so that two return addresses are not pushed onto the stack
130 // when the requested function finally gets called. This also makes the
131 // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
132 ((unsigned char*)(intptr_t)RetAddr)[-1] = 0xE9;
133 }
134
135 // Change the return address to reexecute the call instruction...
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000136 *RetAddrLoc -= 5;
Chris Lattner7ddde322004-11-20 23:54:33 +0000137}
138
Chris Lattner7ddde322004-11-20 23:54:33 +0000139TargetJITInfo::LazyResolverFn
140X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
141 JITCompilerFunction = F;
Chris Lattner1030f722005-05-19 06:49:17 +0000142 return X86CompilationCallback;
Chris Lattner7ddde322004-11-20 23:54:33 +0000143}
144
Chris Lattner90b1b452004-11-22 22:25:30 +0000145void *X86JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
Chris Lattner1030f722005-05-19 06:49:17 +0000146 if (Fn != X86CompilationCallback) {
Chris Lattner90b1b452004-11-22 22:25:30 +0000147 MCE.startFunctionStub(5);
148 MCE.emitByte(0xE9);
149 MCE.emitWord((intptr_t)Fn-MCE.getCurrentPCValue()-4);
150 return MCE.finishFunctionStub(0);
151 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000152
Chris Lattner90b1b452004-11-22 22:25:30 +0000153 MCE.startFunctionStub(6);
154 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
155
156 MCE.emitWord((intptr_t)Fn-MCE.getCurrentPCValue()-4);
157
158 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
159 return MCE.finishFunctionStub(0);
160}
Chris Lattner7ddde322004-11-20 23:54:33 +0000161
162/// relocate - Before the JIT can run a block of code that has been emitted,
163/// it must rewrite the code to contain the actual addresses of any
164/// referenced global symbols.
165void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
166 unsigned NumRelocs) {
167 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
168 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
169 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
170 switch ((X86::RelocationType)MR->getRelocationType()) {
171 case X86::reloc_pcrel_word:
172 // PC relative relocation, add the relocated value to the value already in
173 // memory, after we adjust it for where the PC is.
174 ResultPtr = ResultPtr-(intptr_t)RelocPos-4;
175 *((intptr_t*)RelocPos) += ResultPtr;
176 break;
177 case X86::reloc_absolute_word:
178 // Absolute relocation, just add the relocated value to the value already
179 // in memory.
180 *((intptr_t*)RelocPos) += ResultPtr;
181 break;
182 }
183 }
184}