1 //===-- Callback.cpp - Trap handler for function resolution ---------------===//
3 // This file defines the SIGSEGV handler which is invoked when a reference to a
4 // non-codegen'd function is found.
6 //===----------------------------------------------------------------------===//
9 #include "Support/Statistic.h"
14 // CompilationCallback - Invoked the first time that a call site is found,
15 // which causes lazy compilation of the target function.
17 void VM::CompilationCallback() {
18 #if defined(i386) || defined(__i386__) || defined(__x86__)
19 unsigned *StackPtr = (unsigned*)__builtin_frame_address(0);
20 unsigned RetAddr = (unsigned)__builtin_return_address(0);
22 assert(StackPtr[1] == RetAddr &&
23 "Could not find return address on the stack!");
25 // The call instruction should have pushed the return value onto the stack...
26 RetAddr -= 4; // Backtrack to the reference itself...
28 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << RetAddr
29 << " ESP=0x" << (unsigned)StackPtr << std::dec
30 << ": Resolving call to function: "
31 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
33 // Sanity check to make sure this really is a call instruction...
34 assert(((unsigned char*)RetAddr)[-1] == 0xE8 && "Not a call instr!");
36 unsigned NewVal = (unsigned)TheVM->resolveFunctionReference((void*)RetAddr);
38 // Rewrite the call target... so that we don't fault every time we execute
40 *(unsigned*)RetAddr = NewVal-RetAddr-4;
42 // Change the return address to reexecute the call instruction...
50 void VM::registerCallback() {