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::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
22 MCE.startFunctionStub(6);
23 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
25 MCE.emitWord((intptr_t)Fn-MCE.getCurrentPCValue()-4);
27 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
28 return MCE.finishFunctionStub(0);
31 void 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.
42 #pragma optimize("y", off)
45 /// JITCompilerFunction - This contains the address of the JIT function used to
46 /// compile a function lazily.
47 static TargetJITInfo::JITCompilerFn JITCompilerFunction;
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.
53 static void CompilationCallback() {
55 unsigned *StackPtr, RetAddr;
56 __asm mov StackPtr, ebp;
57 __asm mov eax, DWORD PTR [ebp + 4];
58 __asm mov RetAddr, eax;
60 unsigned *StackPtr = (unsigned*)__builtin_frame_address(0);
61 unsigned RetAddr = (unsigned)(intptr_t)__builtin_return_address(0);
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));
69 assert(StackPtr[1] == RetAddr &&
70 "Could not find return address on the stack!");
72 // It's a stub if there is an interrupt marker after the call.
73 bool isStub = ((unsigned char*)(intptr_t)RetAddr)[0] == 0xCD;
75 // The call instruction should have pushed the return value onto the stack...
76 RetAddr -= 4; // Backtrack to the reference itself...
79 DEBUG(std::cerr << "In callback! Addr=" << (void*)RetAddr
80 << " ESP=" << (void*)StackPtr
81 << ": Resolving call to function: "
82 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
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!");
88 unsigned NewVal = (intptr_t)JITCompilerFunction((void*)(intptr_t)RetAddr);
90 // Rewrite the call target... so that we don't end up here every time we
92 *(unsigned*)(intptr_t)RetAddr = NewVal-RetAddr-4;
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;
102 // Change the return address to reexecute the call instruction...
107 #pragma optimize( "", on )
110 TargetJITInfo::LazyResolverFn
111 X86JITInfo::getLazyResolverFunction(JITCompilerFn F) {
112 JITCompilerFunction = F;
113 return CompilationCallback;
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.
120 void 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;
132 case X86::reloc_absolute_word:
133 // Absolute relocation, just add the relocated value to the value already
135 *((intptr_t*)RelocPos) += ResultPtr;