diff options
| author | Chris Lattner <sabre@nondot.org> | 2002-12-24 00:01:05 +0000 | 
|---|---|---|
| committer | Chris Lattner <sabre@nondot.org> | 2002-12-24 00:01:05 +0000 | 
| commit | 996fe0102878d706d47a06bc78d21d1f38ed2aec (patch) | |
| tree | 8449f4d70e68c16bcb8a5b6ba2dfe8d01c6095b8 /llvm/lib/ExecutionEngine/JIT/Callback.cpp | |
| parent | a0d7b084ef7faab8739ef3e9186fdf9a94c3ed22 (diff) | |
| download | bcm5719-llvm-996fe0102878d706d47a06bc78d21d1f38ed2aec.tar.gz bcm5719-llvm-996fe0102878d706d47a06bc78d21d1f38ed2aec.zip | |
Initial checkin of new LLI with JIT compiler
llvm-svn: 5126
Diffstat (limited to 'llvm/lib/ExecutionEngine/JIT/Callback.cpp')
| -rw-r--r-- | llvm/lib/ExecutionEngine/JIT/Callback.cpp | 62 | 
1 files changed, 62 insertions, 0 deletions
| diff --git a/llvm/lib/ExecutionEngine/JIT/Callback.cpp b/llvm/lib/ExecutionEngine/JIT/Callback.cpp new file mode 100644 index 00000000000..b843e106895 --- /dev/null +++ b/llvm/lib/ExecutionEngine/JIT/Callback.cpp @@ -0,0 +1,62 @@ +//===-- Callback.cpp - Trap handler for function resolution ---------------===// +// +// This file defines the SIGSEGV handler which is invoked when a reference to a +// non-codegen'd function is found. +// +//===----------------------------------------------------------------------===// + +#include "VM.h" +#include "Support/Statistic.h" +#include <signal.h> +#include <ucontext.h> +#include <iostream> + +static VM *TheVM = 0; + +static void TrapHandler(int TN, siginfo_t *SI, ucontext_t *ucp) { +  assert(TN == SIGSEGV && "Should be SIGSEGV!"); + +#ifdef REG_EIP   /* this code does not compile on Sparc! */ +  if (SI->si_code != SEGV_MAPERR || SI->si_addr != 0 || +      ucp->uc_mcontext.gregs[REG_EIP] != 0) { +    std::cerr << "Bad SEGV encountered!\n"; +    abort(); +  } + +  // The call instruction should have pushed the return value onto the stack... +  unsigned RefAddr = *(unsigned*)ucp->uc_mcontext.gregs[REG_ESP]; +  RefAddr -= 4;  // Backtrack to the reference itself... + +  DEBUG(std::cerr << "In SEGV handler! Addr=0x" << std::hex << RefAddr +                  << " ESP=0x" << ucp->uc_mcontext.gregs[REG_ESP] << std::dec +                  << ": Resolving call to function: " +                  << TheVM->getFunctionReferencedName((void*)RefAddr) << "\n"); + +  // Sanity check to make sure this really is a call instruction... +  assert(((unsigned char*)RefAddr)[-1] == 0xE8 && "Not a call instr!"); +   +  unsigned NewVal = (unsigned)TheVM->resolveFunctionReference((void*)RefAddr); + +  // Rewrite the call target... so that we don't fault every time we execute +  // the call. +  *(unsigned*)RefAddr = NewVal-RefAddr-4;     + +  // Change the instruction pointer to be the real target of the call... +  ucp->uc_mcontext.gregs[REG_EIP] = NewVal; + +#endif +} + + +void VM::registerCallback() { +  TheVM = this; + +  // Register the signal handler... +  struct sigaction SA; +  SA.sa_sigaction = (void (*)(int, siginfo_t*, void*))TrapHandler; +  sigfillset(&SA.sa_mask);               // Block all signals while codegen'ing +  SA.sa_flags = SA_NOCLDSTOP|SA_SIGINFO; // Get siginfo +  sigaction(SIGSEGV, &SA, 0);            // Install the handler +} + + | 

