blob: 9a20b951c13a95adcda635fa7c0a4d7c8f708f95 [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"
Nate Begemanbe136342005-05-20 21:29:24 +000019#include <cstdlib>
20#include <iostream>
Chris Lattner7ddde322004-11-20 23:54:33 +000021using namespace llvm;
22
Chris Lattner38f73732006-01-26 19:55:20 +000023#ifdef _MSC_VER
24 extern "C" void *_AddressOfReturnAddress(void);
25 #pragma intrinsic(_AddressOfReturnAddress)
26#endif
27
Chris Lattner7ddde322004-11-20 23:54:33 +000028void 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
Chris Lattner7ddde322004-11-20 23:54:33 +000038/// JITCompilerFunction - This contains the address of the JIT function used to
39/// compile a function lazily.
40static TargetJITInfo::JITCompilerFn JITCompilerFunction;
41
Chris Lattner1030f722005-05-19 06:49:17 +000042// Provide a wrapper for X86CompilationCallback2 that saves non-traditional
43// callee saved registers, for the fastcc calling convention.
Jeff Cohen8bc6f932005-05-20 01:35:39 +000044extern "C" {
45#if defined(__i386__) || defined(i386) || defined(_M_IX86)
Chris Lattner1030f722005-05-19 06:49:17 +000046#ifndef _MSC_VER
Jeff Cohen8bc6f932005-05-20 01:35:39 +000047 void X86CompilationCallback(void);
48 asm(
49 ".text\n"
50 ".align 8\n"
Jeff Cohen10a59ce2006-04-29 18:41:44 +000051#if defined(__CYGWIN__) || defined(__APPLE__) || defined(__MINGW32__)
Reid Spencer628214e2005-06-02 21:33:19 +000052 ".globl _X86CompilationCallback\n"
53 "_X86CompilationCallback:\n"
54#else
Jeff Cohen8bc6f932005-05-20 01:35:39 +000055 ".globl X86CompilationCallback\n"
56 "X86CompilationCallback:\n"
Reid Spencer628214e2005-06-02 21:33:19 +000057#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +000058 "pushl %ebp\n"
59 "movl %esp, %ebp\n" // Standard prologue
Evan Chengda08d2c2006-06-24 08:36:10 +000060#if FASTCC_NUM_INT_ARGS_INREGS > 0
Jeff Cohen8bc6f932005-05-20 01:35:39 +000061 "pushl %eax\n"
Evan Chengda08d2c2006-06-24 08:36:10 +000062 "pushl %edx\n" // Save EAX/EDX
63#endif
64#if defined(__APPLE__)
65 "andl $-16, %esp\n" // Align ESP on 16-byte boundary
66#endif
Evan Chengbe33dd92006-06-29 01:48:36 +000067 "subl $16, %esp\n"
68 "movl 4(%ebp), %eax\n" // Pass prev frame and return address
69 "movl %eax, 4(%esp)\n"
70 "movl %ebp, (%esp)\n"
Evan Chengda08d2c2006-06-24 08:36:10 +000071#if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__APPLE__)
Nate Begemanadd19dc2006-04-25 20:54:26 +000072 "call _X86CompilationCallback2\n"
Reid Spencer628214e2005-06-02 21:33:19 +000073#else
Evan Chengda08d2c2006-06-24 08:36:10 +000074 "call X86CompilationCallback2\n"
75#endif
76#if defined(__APPLE__)
77 "movl %ebp, %esp\n" // Restore ESP
78#endif
79#if FASTCC_NUM_INT_ARGS_INREGS > 0
80#if defined(__APPLE__)
81 "subl $8, %esp\n"
Reid Spencer628214e2005-06-02 21:33:19 +000082#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +000083 "popl %edx\n"
84 "popl %eax\n"
Evan Chengda08d2c2006-06-24 08:36:10 +000085#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +000086 "popl %ebp\n"
87 "ret\n");
Chris Lattner1030f722005-05-19 06:49:17 +000088#else
Jeff Cohen8bc6f932005-05-20 01:35:39 +000089 void X86CompilationCallback2(void);
90
91 _declspec(naked) void X86CompilationCallback(void) {
92 __asm {
93 push eax
94 push edx
95 call X86CompilationCallback2
96 pop edx
97 pop eax
98 ret
99 }
100 }
Misha Brukman57ebf3d2005-05-20 19:46:50 +0000101#endif // _MSC_VER
Chris Lattner1030f722005-05-19 06:49:17 +0000102
Misha Brukman57ebf3d2005-05-20 19:46:50 +0000103#else // Not an i386 host
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000104 void X86CompilationCallback() {
Misha Brukman57ebf3d2005-05-20 19:46:50 +0000105 std::cerr << "Cannot call X86CompilationCallback() on a non-x86 arch!\n";
Chris Lattner170fbcb2005-05-20 17:00:21 +0000106 abort();
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000107 }
Chris Lattner1030f722005-05-19 06:49:17 +0000108#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000109}
Chris Lattner1030f722005-05-19 06:49:17 +0000110
111/// X86CompilationCallback - This is the target-specific function invoked by the
Chris Lattner7ddde322004-11-20 23:54:33 +0000112/// function stub when we did not know the real target of a call. This function
113/// must locate the start of the stub or call site and pass it into the JIT
114/// compiler function.
Chris Lattner7ddde322004-11-20 23:54:33 +0000115#ifdef _MSC_VER
Evan Chengbe33dd92006-06-29 01:48:36 +0000116extern "C" void X86CompilationCallback2() {
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000117 assert(sizeof(size_t) == 4); // FIXME: handle Win64
118 unsigned *RetAddrLoc = (unsigned *)_AddressOfReturnAddress();
119 RetAddrLoc += 3; // skip over ret addr, edx, eax
120 unsigned RetAddr = *RetAddrLoc;
Chris Lattner7ddde322004-11-20 23:54:33 +0000121#else
Evan Chengbe33dd92006-06-29 01:48:36 +0000122extern "C" void X86CompilationCallback2(intptr_t *StackPtr, intptr_t RetAddr) {
123 intptr_t *RetAddrLoc = &StackPtr[1];
Chris Lattner7ddde322004-11-20 23:54:33 +0000124#endif
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000125 assert(*RetAddrLoc == RetAddr &&
Chris Lattner7ddde322004-11-20 23:54:33 +0000126 "Could not find return address on the stack!");
127
128 // It's a stub if there is an interrupt marker after the call.
129 bool isStub = ((unsigned char*)(intptr_t)RetAddr)[0] == 0xCD;
130
131 // The call instruction should have pushed the return value onto the stack...
132 RetAddr -= 4; // Backtrack to the reference itself...
133
134#if 0
135 DEBUG(std::cerr << "In callback! Addr=" << (void*)RetAddr
136 << " ESP=" << (void*)StackPtr
137 << ": Resolving call to function: "
138 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
139#endif
140
141 // Sanity check to make sure this really is a call instruction.
142 assert(((unsigned char*)(intptr_t)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000143
Chris Lattner7ddde322004-11-20 23:54:33 +0000144 unsigned NewVal = (intptr_t)JITCompilerFunction((void*)(intptr_t)RetAddr);
145
146 // Rewrite the call target... so that we don't end up here every time we
147 // execute the call.
148 *(unsigned*)(intptr_t)RetAddr = NewVal-RetAddr-4;
149
150 if (isStub) {
151 // If this is a stub, rewrite the call into an unconditional branch
152 // instruction so that two return addresses are not pushed onto the stack
153 // when the requested function finally gets called. This also makes the
154 // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
155 ((unsigned char*)(intptr_t)RetAddr)[-1] = 0xE9;
156 }
157
158 // Change the return address to reexecute the call instruction...
Jeff Cohen8bc6f932005-05-20 01:35:39 +0000159 *RetAddrLoc -= 5;
Chris Lattner7ddde322004-11-20 23:54:33 +0000160}
161
Chris Lattner7ddde322004-11-20 23:54:33 +0000162TargetJITInfo::LazyResolverFn
163X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
164 JITCompilerFunction = F;
Chris Lattner1030f722005-05-19 06:49:17 +0000165 return X86CompilationCallback;
Chris Lattner7ddde322004-11-20 23:54:33 +0000166}
167
Chris Lattner90b1b452004-11-22 22:25:30 +0000168void *X86JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
Chris Lattner078d1822006-06-01 17:13:10 +0000169 // Note, we cast to intptr_t here to silence a -pedantic warning that
170 // complains about casting a function pointer to a normal pointer.
171 if (Fn != (void*)(intptr_t)X86CompilationCallback) {
Chris Lattner90b1b452004-11-22 22:25:30 +0000172 MCE.startFunctionStub(5);
173 MCE.emitByte(0xE9);
Chris Lattnerd3f0aef2006-05-02 19:14:47 +0000174 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
Chris Lattner90b1b452004-11-22 22:25:30 +0000175 return MCE.finishFunctionStub(0);
176 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000177
Chris Lattner90b1b452004-11-22 22:25:30 +0000178 MCE.startFunctionStub(6);
179 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
180
Chris Lattnerd3f0aef2006-05-02 19:14:47 +0000181 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
Chris Lattner90b1b452004-11-22 22:25:30 +0000182
183 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
184 return MCE.finishFunctionStub(0);
185}
Chris Lattner7ddde322004-11-20 23:54:33 +0000186
187/// relocate - Before the JIT can run a block of code that has been emitted,
188/// it must rewrite the code to contain the actual addresses of any
189/// referenced global symbols.
190void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
Andrew Lenharth908bc862005-07-22 20:49:37 +0000191 unsigned NumRelocs, unsigned char* GOTBase) {
Chris Lattner7ddde322004-11-20 23:54:33 +0000192 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
193 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
194 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
195 switch ((X86::RelocationType)MR->getRelocationType()) {
196 case X86::reloc_pcrel_word:
197 // PC relative relocation, add the relocated value to the value already in
198 // memory, after we adjust it for where the PC is.
199 ResultPtr = ResultPtr-(intptr_t)RelocPos-4;
200 *((intptr_t*)RelocPos) += ResultPtr;
201 break;
202 case X86::reloc_absolute_word:
203 // Absolute relocation, just add the relocated value to the value already
204 // in memory.
205 *((intptr_t*)RelocPos) += ResultPtr;
206 break;
207 }
208 }
209}