blob: a976d232f5b539f2614ac5d8c015bb55cf3d822b [file] [log] [blame]
Chris Lattner7ddde322004-11-20 23:54:33 +00001//===-- 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"
19using namespace llvm;
20
21void *X86JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
22 MCE.startFunctionStub(6);
23 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
24
25 MCE.emitWord((intptr_t)Fn-MCE.getCurrentPCValue()-4);
26
27 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
28 return MCE.finishFunctionStub(0);
29}
30
31void X86JITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
32 unsigned char *OldByte = (unsigned char *)Old;
33 *OldByte++ = 0xE9; // Emit JMP opcode.
34 unsigned *OldWord = (unsigned *)OldByte;
35 unsigned NewAddr = (intptr_t)New;
36 unsigned OldAddr = (intptr_t)OldWord;
37 *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
38}
39
40
41#ifdef _MSC_VER
42#pragma optimize("y", off)
43#endif
44
45/// JITCompilerFunction - This contains the address of the JIT function used to
46/// compile a function lazily.
47static TargetJITInfo::JITCompilerFn JITCompilerFunction;
48
49/// CompilationCallback - This is the target-specific function invoked by the
50/// function stub when we did not know the real target of a call. This function
51/// must locate the start of the stub or call site and pass it into the JIT
52/// compiler function.
53static void CompilationCallback() {
54#ifdef _MSC_VER
55 unsigned *StackPtr, RetAddr;
56 __asm mov StackPtr, ebp;
57 __asm mov eax, DWORD PTR [ebp + 4];
58 __asm mov RetAddr, eax;
59#else
60 unsigned *StackPtr = (unsigned*)__builtin_frame_address(0);
61 unsigned RetAddr = (unsigned)(intptr_t)__builtin_return_address(0);
62
63 // NOTE: __builtin_frame_address doesn't work if frame pointer elimination has
64 // been performed. Having a variable sized alloca disables frame pointer
65 // elimination currently, even if it's dead. This is a gross hack.
66 alloca(10+(RetAddr >> 31));
67
68#endif
69 assert(StackPtr[1] == RetAddr &&
70 "Could not find return address on the stack!");
71
72 // It's a stub if there is an interrupt marker after the call.
73 bool isStub = ((unsigned char*)(intptr_t)RetAddr)[0] == 0xCD;
74
75 // The call instruction should have pushed the return value onto the stack...
76 RetAddr -= 4; // Backtrack to the reference itself...
77
78#if 0
79 DEBUG(std::cerr << "In callback! Addr=" << (void*)RetAddr
80 << " ESP=" << (void*)StackPtr
81 << ": Resolving call to function: "
82 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
83#endif
84
85 // Sanity check to make sure this really is a call instruction.
86 assert(((unsigned char*)(intptr_t)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
87
88 unsigned NewVal = (intptr_t)JITCompilerFunction((void*)(intptr_t)RetAddr);
89
90 // Rewrite the call target... so that we don't end up here every time we
91 // execute the call.
92 *(unsigned*)(intptr_t)RetAddr = NewVal-RetAddr-4;
93
94 if (isStub) {
95 // If this is a stub, rewrite the call into an unconditional branch
96 // instruction so that two return addresses are not pushed onto the stack
97 // when the requested function finally gets called. This also makes the
98 // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
99 ((unsigned char*)(intptr_t)RetAddr)[-1] = 0xE9;
100 }
101
102 // Change the return address to reexecute the call instruction...
103 StackPtr[1] -= 5;
104}
105
106#ifdef _MSC_VER
107#pragma optimize( "", on )
108#endif
109
110TargetJITInfo::LazyResolverFn
111X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
112 JITCompilerFunction = F;
113 return CompilationCallback;
114}
115
116
117/// relocate - Before the JIT can run a block of code that has been emitted,
118/// it must rewrite the code to contain the actual addresses of any
119/// referenced global symbols.
120void X86JITInfo::relocate(void *Function, MachineRelocation *MR,
121 unsigned NumRelocs) {
122 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
123 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
124 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
125 switch ((X86::RelocationType)MR->getRelocationType()) {
126 case X86::reloc_pcrel_word:
127 // PC relative relocation, add the relocated value to the value already in
128 // memory, after we adjust it for where the PC is.
129 ResultPtr = ResultPtr-(intptr_t)RelocPos-4;
130 *((intptr_t*)RelocPos) += ResultPtr;
131 break;
132 case X86::reloc_absolute_word:
133 // Absolute relocation, just add the relocated value to the value already
134 // in memory.
135 *((intptr_t*)RelocPos) += ResultPtr;
136 break;
137 }
138 }
139}
140