2004-11-22 23:00:25 +01:00
|
|
|
//===-- JITEmitter.cpp - Write machine code to executable memory ----------===//
|
2005-04-22 00:55:34 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 00:55:34 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-24 01:01:05 +01:00
|
|
|
//
|
2004-11-20 04:46:14 +01:00
|
|
|
// This file defines a MachineCodeEmitter object that is used by the JIT to
|
|
|
|
// write machine code to memory and remember where relocatable values are.
|
2002-12-24 01:01:05 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-08-05 19:00:32 +02:00
|
|
|
#define DEBUG_TYPE "jit"
|
2003-12-20 02:46:27 +01:00
|
|
|
#include "JIT.h"
|
Implement the JIT side of the GDB JIT debugging interface. To enable this
feature, either build the JIT in debug mode to enable it by default or pass
-jit-emit-debug to lli.
Right now, the only debug information that this communicates to GDB is call
frame information, since it's already being generated to support exceptions in
the JIT. Eventually, when DWARF generation isn't tied so tightly to AsmPrinter,
it will be easy to push that information to GDB through this interface.
Here's a step-by-step breakdown of how the feature works:
- The JIT generates the machine code and DWARF call frame info
(.eh_frame/.debug_frame) for a function into memory.
- The JIT copies that info into an in-memory ELF file with a symbol for the
function.
- The JIT creates a code entry pointing to the ELF buffer and adds it to a
linked list hanging off of a global descriptor at a special symbol that GDB
knows about.
- The JIT calls a function marked noinline that GDB knows about and has put an
internal breakpoint in.
- GDB catches the breakpoint and reads the global descriptor to look for new
code.
- When sees there is new code, it reads the ELF from the inferior's memory and
adds it to itself as an object file.
- The JIT continues, and the next time we stop the program, we are able to
produce a proper backtrace.
Consider running the following program through the JIT:
#include <stdio.h>
void baz(short z) {
long w = z + 1;
printf("%d, %x\n", w, *((int*)NULL)); // SEGFAULT here
}
void bar(short y) {
int z = y + 1;
baz(z);
}
void foo(char x) {
short y = x + 1;
bar(y);
}
int main(int argc, char** argv) {
char x = 1;
foo(x);
}
Here is a backtrace before this patch:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x2aaaabdfbd10 (LWP 25476)]
0x00002aaaabe7d1a8 in ?? ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in ?? ()
#1 0x0000000000000003 in ?? ()
#2 0x0000000000000004 in ?? ()
#3 0x00032aaaabe7cfd0 in ?? ()
#4 0x00002aaaabe7d12c in ?? ()
#5 0x00022aaa00000003 in ?? ()
#6 0x00002aaaabe7d0aa in ?? ()
#7 0x01000002abe7cff0 in ?? ()
#8 0x00002aaaabe7d02c in ?? ()
#9 0x0100000000000001 in ?? ()
#10 0x00000000014388e0 in ?? ()
#11 0x00007fff00000001 in ?? ()
#12 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=@0x7fffffffe050)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#13 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=@0x13f06f8, envp=0x7fffffffe3b0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#14 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe398,
envp=0x7fffffffe3b0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
And a backtrace after this patch:
Program received signal SIGSEGV, Segmentation fault.
0x00002aaaabe7d1a8 in baz ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in baz ()
#1 0x00002aaaabe7d12c in bar ()
#2 0x00002aaaabe7d0aa in foo ()
#3 0x00002aaaabe7d02c in main ()
#4 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=...)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#5 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=..., envp=0x7fffffffe3c0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#6 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe3a8,
envp=0x7fffffffe3c0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
llvm-svn: 82418
2009-09-21 01:52:43 +02:00
|
|
|
#include "JITDebugRegisterer.h"
|
2008-02-13 19:39:37 +01:00
|
|
|
#include "JITDwarfEmitter.h"
|
Implement the JIT side of the GDB JIT debugging interface. To enable this
feature, either build the JIT in debug mode to enable it by default or pass
-jit-emit-debug to lli.
Right now, the only debug information that this communicates to GDB is call
frame information, since it's already being generated to support exceptions in
the JIT. Eventually, when DWARF generation isn't tied so tightly to AsmPrinter,
it will be easy to push that information to GDB through this interface.
Here's a step-by-step breakdown of how the feature works:
- The JIT generates the machine code and DWARF call frame info
(.eh_frame/.debug_frame) for a function into memory.
- The JIT copies that info into an in-memory ELF file with a symbol for the
function.
- The JIT creates a code entry pointing to the ELF buffer and adds it to a
linked list hanging off of a global descriptor at a special symbol that GDB
knows about.
- The JIT calls a function marked noinline that GDB knows about and has put an
internal breakpoint in.
- GDB catches the breakpoint and reads the global descriptor to look for new
code.
- When sees there is new code, it reads the ELF from the inferior's memory and
adds it to itself as an object file.
- The JIT continues, and the next time we stop the program, we are able to
produce a proper backtrace.
Consider running the following program through the JIT:
#include <stdio.h>
void baz(short z) {
long w = z + 1;
printf("%d, %x\n", w, *((int*)NULL)); // SEGFAULT here
}
void bar(short y) {
int z = y + 1;
baz(z);
}
void foo(char x) {
short y = x + 1;
bar(y);
}
int main(int argc, char** argv) {
char x = 1;
foo(x);
}
Here is a backtrace before this patch:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x2aaaabdfbd10 (LWP 25476)]
0x00002aaaabe7d1a8 in ?? ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in ?? ()
#1 0x0000000000000003 in ?? ()
#2 0x0000000000000004 in ?? ()
#3 0x00032aaaabe7cfd0 in ?? ()
#4 0x00002aaaabe7d12c in ?? ()
#5 0x00022aaa00000003 in ?? ()
#6 0x00002aaaabe7d0aa in ?? ()
#7 0x01000002abe7cff0 in ?? ()
#8 0x00002aaaabe7d02c in ?? ()
#9 0x0100000000000001 in ?? ()
#10 0x00000000014388e0 in ?? ()
#11 0x00007fff00000001 in ?? ()
#12 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=@0x7fffffffe050)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#13 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=@0x13f06f8, envp=0x7fffffffe3b0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#14 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe398,
envp=0x7fffffffe3b0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
And a backtrace after this patch:
Program received signal SIGSEGV, Segmentation fault.
0x00002aaaabe7d1a8 in baz ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in baz ()
#1 0x00002aaaabe7d12c in bar ()
#2 0x00002aaaabe7d0aa in foo ()
#3 0x00002aaaabe7d02c in main ()
#4 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=...)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#5 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=..., envp=0x7fffffffe3c0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#6 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe3a8,
envp=0x7fffffffe3c0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
llvm-svn: 82418
2009-09-21 01:52:43 +02:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2008-08-07 03:30:15 +02:00
|
|
|
#include "llvm/Constants.h"
|
2003-11-30 05:23:21 +01:00
|
|
|
#include "llvm/Module.h"
|
2008-08-07 03:30:15 +02:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2009-05-30 22:51:52 +02:00
|
|
|
#include "llvm/CodeGen/JITCodeEmitter.h"
|
2002-12-24 01:01:05 +01:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2003-01-13 02:00:12 +01:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2006-04-22 20:53:45 +02:00
|
|
|
#include "llvm/CodeGen/MachineJumpTableInfo.h"
|
2008-02-13 19:39:37 +01:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2004-11-20 04:46:14 +01:00
|
|
|
#include "llvm/CodeGen/MachineRelocation.h"
|
2008-08-07 03:30:15 +02:00
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
2009-06-25 04:04:04 +02:00
|
|
|
#include "llvm/ExecutionEngine/JITEventListener.h"
|
|
|
|
#include "llvm/ExecutionEngine/JITMemoryManager.h"
|
2009-05-18 23:06:40 +02:00
|
|
|
#include "llvm/CodeGen/MachineCodeInfo.h"
|
2003-01-13 02:00:12 +01:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2004-11-20 04:46:14 +01:00
|
|
|
#include "llvm/Target/TargetJITInfo.h"
|
2006-12-14 20:17:33 +01:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2008-02-13 19:39:37 +01:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 15:10:19 +02:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2006-05-09 00:00:52 +02:00
|
|
|
#include "llvm/Support/MutexGuard.h"
|
2009-04-19 20:32:03 +02:00
|
|
|
#include "llvm/Support/ValueHandle.h"
|
2009-07-11 15:10:19 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-01-23 11:26:08 +01:00
|
|
|
#include "llvm/System/Disassembler.h"
|
2008-06-25 19:18:44 +02:00
|
|
|
#include "llvm/System/Memory.h"
|
2008-04-18 22:59:31 +02:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2009-10-20 20:13:21 +02:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2008-11-07 10:02:17 +01:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2009-02-18 09:31:02 +01:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2009-10-24 00:37:43 +02:00
|
|
|
#include "llvm/ADT/ValueMap.h"
|
2005-07-30 01:40:16 +02:00
|
|
|
#include <algorithm>
|
2008-11-06 00:44:08 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include <iomanip>
|
|
|
|
#endif
|
2003-12-08 09:06:28 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2006-12-19 23:43:32 +01:00
|
|
|
STATISTIC(NumBytes, "Number of bytes of machine code compiled");
|
|
|
|
STATISTIC(NumRelos, "Number of relocations applied");
|
2009-07-23 23:46:56 +02:00
|
|
|
STATISTIC(NumRetries, "Number of retries with more memory");
|
2006-12-19 23:43:32 +01:00
|
|
|
static JIT *TheJIT = 0;
|
2002-12-24 01:01:05 +01:00
|
|
|
|
2005-07-30 01:40:16 +02:00
|
|
|
|
2004-11-21 00:57:07 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// JIT lazy compilation code.
|
|
|
|
//
|
|
|
|
namespace {
|
2009-11-07 01:00:10 +01:00
|
|
|
class JITEmitter;
|
2009-10-24 00:37:43 +02:00
|
|
|
class JITResolverState;
|
|
|
|
|
|
|
|
template<typename ValueTy>
|
|
|
|
struct NoRAUWValueMapConfig : public ValueMapConfig<ValueTy> {
|
|
|
|
typedef JITResolverState *ExtraData;
|
|
|
|
static void onRAUW(JITResolverState *, Value *Old, Value *New) {
|
|
|
|
assert(false && "The JIT doesn't know how to handle a"
|
|
|
|
" RAUW on a value it has emitted.");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CallSiteValueMapConfig : public NoRAUWValueMapConfig<Function*> {
|
|
|
|
typedef JITResolverState *ExtraData;
|
|
|
|
static void onDelete(JITResolverState *JRS, Function *F);
|
|
|
|
};
|
|
|
|
|
2005-07-12 17:51:55 +02:00
|
|
|
class JITResolverState {
|
2009-04-27 07:09:44 +02:00
|
|
|
public:
|
2009-10-24 00:37:43 +02:00
|
|
|
typedef ValueMap<Function*, void*, NoRAUWValueMapConfig<Function*> >
|
|
|
|
FunctionToStubMapTy;
|
2009-10-19 20:49:59 +02:00
|
|
|
typedef std::map<void*, AssertingVH<Function> > CallSiteToFunctionMapTy;
|
2009-10-24 00:37:43 +02:00
|
|
|
typedef ValueMap<Function *, SmallPtrSet<void*, 1>,
|
|
|
|
CallSiteValueMapConfig> FunctionToCallSitesMapTy;
|
2009-04-27 07:09:44 +02:00
|
|
|
typedef std::map<AssertingVH<GlobalValue>, void*> GlobalToIndirectSymMapTy;
|
2005-07-12 17:51:55 +02:00
|
|
|
private:
|
|
|
|
/// FunctionToStubMap - Keep track of the stub created for a particular
|
|
|
|
/// function so that we can reuse them if necessary.
|
2009-04-27 07:09:44 +02:00
|
|
|
FunctionToStubMapTy FunctionToStubMap;
|
2005-07-12 17:51:55 +02:00
|
|
|
|
2009-10-19 20:49:59 +02:00
|
|
|
/// CallSiteToFunctionMap - Keep track of the function that each lazy call
|
|
|
|
/// site corresponds to, and vice versa.
|
|
|
|
CallSiteToFunctionMapTy CallSiteToFunctionMap;
|
|
|
|
FunctionToCallSitesMapTy FunctionToCallSitesMap;
|
2005-07-27 08:12:32 +02:00
|
|
|
|
2008-11-10 02:52:24 +01:00
|
|
|
/// GlobalToIndirectSymMap - Keep track of the indirect symbol created for a
|
2008-01-04 11:46:51 +01:00
|
|
|
/// particular GlobalVariable so that we can reuse them if necessary.
|
2009-04-27 07:09:44 +02:00
|
|
|
GlobalToIndirectSymMapTy GlobalToIndirectSymMap;
|
2008-01-04 11:46:51 +01:00
|
|
|
|
2005-07-12 17:51:55 +02:00
|
|
|
public:
|
2009-10-24 00:37:43 +02:00
|
|
|
JITResolverState() : FunctionToStubMap(this),
|
|
|
|
FunctionToCallSitesMap(this) {}
|
|
|
|
|
2009-04-27 07:09:44 +02:00
|
|
|
FunctionToStubMapTy& getFunctionToStubMap(const MutexGuard& locked) {
|
2005-07-12 17:51:55 +02:00
|
|
|
assert(locked.holds(TheJIT->lock));
|
|
|
|
return FunctionToStubMap;
|
|
|
|
}
|
2005-07-27 08:12:32 +02:00
|
|
|
|
2009-10-19 20:49:59 +02:00
|
|
|
GlobalToIndirectSymMapTy& getGlobalToIndirectSymMap(const MutexGuard& locked) {
|
2005-07-12 17:51:55 +02:00
|
|
|
assert(locked.holds(TheJIT->lock));
|
2009-10-19 20:49:59 +02:00
|
|
|
return GlobalToIndirectSymMap;
|
2005-07-12 17:51:55 +02:00
|
|
|
}
|
2008-01-04 11:46:51 +01:00
|
|
|
|
2009-10-19 20:49:59 +02:00
|
|
|
pair<void *, Function *> LookupFunctionFromCallSite(
|
|
|
|
const MutexGuard &locked, void *CallSite) const {
|
2008-01-04 11:46:51 +01:00
|
|
|
assert(locked.holds(TheJIT->lock));
|
2009-10-19 20:49:59 +02:00
|
|
|
|
|
|
|
// The address given to us for the stub may not be exactly right, it might be
|
|
|
|
// a little bit after the stub. As such, use upper_bound to find it.
|
|
|
|
CallSiteToFunctionMapTy::const_iterator I =
|
|
|
|
CallSiteToFunctionMap.upper_bound(CallSite);
|
|
|
|
assert(I != CallSiteToFunctionMap.begin() &&
|
|
|
|
"This is not a known call site!");
|
|
|
|
--I;
|
|
|
|
return *I;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddCallSite(const MutexGuard &locked, void *CallSite, Function *F) {
|
|
|
|
assert(locked.holds(TheJIT->lock));
|
|
|
|
|
2009-10-24 00:37:43 +02:00
|
|
|
bool Inserted = CallSiteToFunctionMap.insert(
|
|
|
|
std::make_pair(CallSite, F)).second;
|
|
|
|
(void)Inserted;
|
|
|
|
assert(Inserted && "Pair was already in CallSiteToFunctionMap");
|
2009-10-19 20:49:59 +02:00
|
|
|
FunctionToCallSitesMap[F].insert(CallSite);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the Function of the stub if a stub was erased, or NULL if there
|
|
|
|
// was no stub. This function uses the call-site->function map to find a
|
|
|
|
// relevant function, but asserts that only stubs and not other call sites
|
|
|
|
// will be passed in.
|
|
|
|
Function *EraseStub(const MutexGuard &locked, void *Stub) {
|
|
|
|
CallSiteToFunctionMapTy::iterator C2F_I =
|
|
|
|
CallSiteToFunctionMap.find(Stub);
|
|
|
|
if (C2F_I == CallSiteToFunctionMap.end()) {
|
|
|
|
// Not a stub.
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Function *const F = C2F_I->second;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
void *RealStub = FunctionToStubMap.lookup(F);
|
|
|
|
assert(RealStub == Stub &&
|
|
|
|
"Call-site that wasn't a stub pass in to EraseStub");
|
|
|
|
#endif
|
|
|
|
FunctionToStubMap.erase(F);
|
|
|
|
CallSiteToFunctionMap.erase(C2F_I);
|
|
|
|
|
|
|
|
// Remove the stub from the function->call-sites map, and remove the whole
|
|
|
|
// entry from the map if that was the last call site.
|
|
|
|
FunctionToCallSitesMapTy::iterator F2C_I = FunctionToCallSitesMap.find(F);
|
|
|
|
assert(F2C_I != FunctionToCallSitesMap.end() &&
|
|
|
|
"FunctionToCallSitesMap broken");
|
2009-10-24 00:37:43 +02:00
|
|
|
bool Erased = F2C_I->second.erase(Stub);
|
|
|
|
(void)Erased;
|
|
|
|
assert(Erased && "FunctionToCallSitesMap broken");
|
2009-10-19 20:49:59 +02:00
|
|
|
if (F2C_I->second.empty())
|
|
|
|
FunctionToCallSitesMap.erase(F2C_I);
|
|
|
|
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EraseAllCallSites(const MutexGuard &locked, Function *F) {
|
|
|
|
assert(locked.holds(TheJIT->lock));
|
2009-10-24 00:37:43 +02:00
|
|
|
EraseAllCallSitesPrelocked(F);
|
|
|
|
}
|
|
|
|
void EraseAllCallSitesPrelocked(Function *F) {
|
2009-10-19 20:49:59 +02:00
|
|
|
FunctionToCallSitesMapTy::iterator F2C = FunctionToCallSitesMap.find(F);
|
|
|
|
if (F2C == FunctionToCallSitesMap.end())
|
|
|
|
return;
|
|
|
|
for (SmallPtrSet<void*, 1>::const_iterator I = F2C->second.begin(),
|
|
|
|
E = F2C->second.end(); I != E; ++I) {
|
2009-10-24 00:37:43 +02:00
|
|
|
bool Erased = CallSiteToFunctionMap.erase(*I);
|
|
|
|
(void)Erased;
|
|
|
|
assert(Erased && "Missing call site->function mapping");
|
2009-10-19 20:49:59 +02:00
|
|
|
}
|
|
|
|
FunctionToCallSitesMap.erase(F2C);
|
2008-01-04 11:46:51 +01:00
|
|
|
}
|
2005-07-12 17:51:55 +02:00
|
|
|
};
|
2005-07-27 08:12:32 +02:00
|
|
|
|
2004-11-21 00:57:07 +01:00
|
|
|
/// JITResolver - Keep track of, and resolve, call sites for functions that
|
|
|
|
/// have not yet been compiled.
|
|
|
|
class JITResolver {
|
2009-04-27 07:09:44 +02:00
|
|
|
typedef JITResolverState::FunctionToStubMapTy FunctionToStubMapTy;
|
2009-10-19 20:49:59 +02:00
|
|
|
typedef JITResolverState::CallSiteToFunctionMapTy CallSiteToFunctionMapTy;
|
2009-04-27 07:09:44 +02:00
|
|
|
typedef JITResolverState::GlobalToIndirectSymMapTy GlobalToIndirectSymMapTy;
|
|
|
|
|
2004-11-21 04:37:42 +01:00
|
|
|
/// LazyResolverFn - The target lazy resolver function that we actually
|
|
|
|
/// rewrite instructions to use.
|
|
|
|
TargetJITInfo::LazyResolverFn LazyResolverFn;
|
|
|
|
|
2005-07-12 17:51:55 +02:00
|
|
|
JITResolverState state;
|
2004-11-21 00:57:07 +01:00
|
|
|
|
2005-04-18 03:44:27 +02:00
|
|
|
/// ExternalFnToStubMap - This is the equivalent of FunctionToStubMap for
|
|
|
|
/// external functions.
|
|
|
|
std::map<void*, void*> ExternalFnToStubMap;
|
2005-07-28 14:44:13 +02:00
|
|
|
|
2009-03-13 08:51:59 +01:00
|
|
|
/// revGOTMap - map addresses to indexes in the GOT
|
2005-07-28 14:44:13 +02:00
|
|
|
std::map<void*, unsigned> revGOTMap;
|
|
|
|
unsigned nextGOTIndex;
|
|
|
|
|
2009-11-07 01:00:10 +01:00
|
|
|
JITEmitter &JE;
|
|
|
|
|
2007-02-24 03:57:03 +01:00
|
|
|
static JITResolver *TheJITResolver;
|
2004-11-21 00:57:07 +01:00
|
|
|
public:
|
2009-11-07 01:00:10 +01:00
|
|
|
explicit JITResolver(JIT &jit, JITEmitter &je) : nextGOTIndex(0), JE(je) {
|
2007-02-24 03:57:03 +01:00
|
|
|
TheJIT = &jit;
|
|
|
|
|
|
|
|
LazyResolverFn = jit.getJITInfo().getLazyResolverFunction(JITCompilerFn);
|
|
|
|
assert(TheJITResolver == 0 && "Multiple JIT resolvers?");
|
|
|
|
TheJITResolver = this;
|
|
|
|
}
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2007-02-24 03:57:03 +01:00
|
|
|
~JITResolver() {
|
|
|
|
TheJITResolver = 0;
|
2004-11-21 04:37:42 +01:00
|
|
|
}
|
2004-11-21 00:57:07 +01:00
|
|
|
|
2008-11-13 22:50:50 +01:00
|
|
|
/// getFunctionStubIfAvailable - This returns a pointer to a function stub
|
|
|
|
/// if it has already been created.
|
|
|
|
void *getFunctionStubIfAvailable(Function *F);
|
|
|
|
|
2004-11-21 00:57:07 +01:00
|
|
|
/// getFunctionStub - This returns a pointer to a function stub, creating
|
2009-02-18 09:31:02 +01:00
|
|
|
/// one on demand as needed. If empty is true, create a function stub
|
|
|
|
/// pointing at address 0, to be filled in later.
|
2009-03-07 07:41:19 +01:00
|
|
|
void *getFunctionStub(Function *F);
|
2004-11-21 00:57:07 +01:00
|
|
|
|
2005-04-18 03:44:27 +02:00
|
|
|
/// getExternalFunctionStub - Return a stub for the function at the
|
|
|
|
/// specified address, created lazily on demand.
|
|
|
|
void *getExternalFunctionStub(void *FnAddr);
|
|
|
|
|
2008-11-10 02:52:24 +01:00
|
|
|
/// getGlobalValueIndirectSym - Return an indirect symbol containing the
|
2008-11-05 02:50:32 +01:00
|
|
|
/// specified GV address.
|
2008-11-10 02:52:24 +01:00
|
|
|
void *getGlobalValueIndirectSym(GlobalValue *V, void *GVAddress);
|
2008-01-04 11:46:51 +01:00
|
|
|
|
2004-11-21 04:37:42 +01:00
|
|
|
/// AddCallbackAtLocation - If the target is capable of rewriting an
|
|
|
|
/// instruction without the use of a stub, record the location of the use so
|
|
|
|
/// we know which function is being used at the location.
|
|
|
|
void *AddCallbackAtLocation(Function *F, void *Location) {
|
2005-07-12 17:51:55 +02:00
|
|
|
MutexGuard locked(TheJIT->lock);
|
2004-11-21 04:37:42 +01:00
|
|
|
/// Get the target-specific JIT resolver function.
|
2009-10-19 20:49:59 +02:00
|
|
|
state.AddCallSite(locked, Location, F);
|
2006-06-01 19:29:22 +02:00
|
|
|
return (void*)(intptr_t)LazyResolverFn;
|
2004-11-21 04:37:42 +01:00
|
|
|
}
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-02-18 09:31:02 +01:00
|
|
|
void getRelocatableGVs(SmallVectorImpl<GlobalValue*> &GVs,
|
2009-03-05 07:34:37 +01:00
|
|
|
SmallVectorImpl<void*> &Ptrs);
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-03-05 07:34:37 +01:00
|
|
|
GlobalValue *invalidateStub(void *Stub);
|
2004-11-21 04:37:42 +01:00
|
|
|
|
2005-07-28 14:44:13 +02:00
|
|
|
/// getGOTIndexForAddress - Return a new or existing index in the GOT for
|
2007-12-06 00:39:57 +01:00
|
|
|
/// an address. This function only manages slots, it does not manage the
|
2005-07-28 14:44:13 +02:00
|
|
|
/// contents of the slots or the memory associated with the GOT.
|
2007-12-06 00:39:57 +01:00
|
|
|
unsigned getGOTIndexForAddr(void *addr);
|
2005-07-28 14:44:13 +02:00
|
|
|
|
2004-11-21 00:57:07 +01:00
|
|
|
/// JITCompilerFn - This function is called to resolve a stub to a compiled
|
|
|
|
/// address. If the LLVM Function corresponding to the stub has not yet
|
|
|
|
/// been compiled, this function compiles it first.
|
|
|
|
static void *JITCompilerFn(void *Stub);
|
|
|
|
};
|
2009-11-07 01:00:10 +01:00
|
|
|
|
|
|
|
/// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
|
|
|
|
/// used to output functions to memory for execution.
|
|
|
|
class JITEmitter : public JITCodeEmitter {
|
|
|
|
JITMemoryManager *MemMgr;
|
|
|
|
|
|
|
|
// When outputting a function stub in the context of some other function, we
|
|
|
|
// save BufferBegin/BufferEnd/CurBufferPtr here.
|
|
|
|
uint8_t *SavedBufferBegin, *SavedBufferEnd, *SavedCurBufferPtr;
|
|
|
|
|
|
|
|
// When reattempting to JIT a function after running out of space, we store
|
|
|
|
// the estimated size of the function we're trying to JIT here, so we can
|
|
|
|
// ask the memory manager for at least this much space. When we
|
|
|
|
// successfully emit the function, we reset this back to zero.
|
|
|
|
uintptr_t SizeEstimate;
|
|
|
|
|
|
|
|
/// Relocations - These are the relocations that the function needs, as
|
|
|
|
/// emitted.
|
|
|
|
std::vector<MachineRelocation> Relocations;
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-11-07 01:00:10 +01:00
|
|
|
/// MBBLocations - This vector is a mapping from MBB ID's to their address.
|
|
|
|
/// It is filled in by the StartMachineBasicBlock callback and queried by
|
|
|
|
/// the getMachineBasicBlockAddress callback.
|
|
|
|
std::vector<uintptr_t> MBBLocations;
|
|
|
|
|
|
|
|
/// ConstantPool - The constant pool for the current function.
|
|
|
|
///
|
|
|
|
MachineConstantPool *ConstantPool;
|
|
|
|
|
|
|
|
/// ConstantPoolBase - A pointer to the first entry in the constant pool.
|
|
|
|
///
|
|
|
|
void *ConstantPoolBase;
|
|
|
|
|
|
|
|
/// ConstPoolAddresses - Addresses of individual constant pool entries.
|
|
|
|
///
|
|
|
|
SmallVector<uintptr_t, 8> ConstPoolAddresses;
|
|
|
|
|
|
|
|
/// JumpTable - The jump tables for the current function.
|
|
|
|
///
|
|
|
|
MachineJumpTableInfo *JumpTable;
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-11-07 01:00:10 +01:00
|
|
|
/// JumpTableBase - A pointer to the first entry in the jump table.
|
|
|
|
///
|
|
|
|
void *JumpTableBase;
|
|
|
|
|
|
|
|
/// Resolver - This contains info about the currently resolved functions.
|
|
|
|
JITResolver Resolver;
|
|
|
|
|
|
|
|
/// DE - The dwarf emitter for the jit.
|
|
|
|
OwningPtr<JITDwarfEmitter> DE;
|
|
|
|
|
|
|
|
/// DR - The debug registerer for the jit.
|
|
|
|
OwningPtr<JITDebugRegisterer> DR;
|
|
|
|
|
2009-11-12 04:12:18 +01:00
|
|
|
/// LabelLocations - This vector is a mapping from Label ID's to their
|
2009-11-07 01:00:10 +01:00
|
|
|
/// address.
|
|
|
|
std::vector<uintptr_t> LabelLocations;
|
|
|
|
|
|
|
|
/// MMI - Machine module info for exception informations
|
|
|
|
MachineModuleInfo* MMI;
|
|
|
|
|
|
|
|
// GVSet - a set to keep track of which globals have been seen
|
|
|
|
SmallPtrSet<const GlobalVariable*, 8> GVSet;
|
|
|
|
|
2009-11-12 04:12:18 +01:00
|
|
|
// CurFn - The llvm function being emitted. Only valid during
|
2009-11-07 01:00:10 +01:00
|
|
|
// finishFunction().
|
|
|
|
const Function *CurFn;
|
|
|
|
|
|
|
|
/// Information about emitted code, which is passed to the
|
|
|
|
/// JITEventListeners. This is reset in startFunction and used in
|
|
|
|
/// finishFunction.
|
|
|
|
JITEvent_EmittedFunctionDetails EmissionDetails;
|
|
|
|
|
|
|
|
struct EmittedCode {
|
|
|
|
void *FunctionBody; // Beginning of the function's allocation.
|
|
|
|
void *Code; // The address the function's code actually starts at.
|
|
|
|
void *ExceptionTable;
|
|
|
|
EmittedCode() : FunctionBody(0), Code(0), ExceptionTable(0) {}
|
|
|
|
};
|
|
|
|
struct EmittedFunctionConfig : public ValueMapConfig<const Function*> {
|
|
|
|
typedef JITEmitter *ExtraData;
|
|
|
|
static void onDelete(JITEmitter *, const Function*);
|
|
|
|
static void onRAUW(JITEmitter *, const Function*, const Function*);
|
|
|
|
};
|
|
|
|
ValueMap<const Function *, EmittedCode,
|
|
|
|
EmittedFunctionConfig> EmittedFunctions;
|
|
|
|
|
|
|
|
// CurFnStubUses - For a given Function, a vector of stubs that it
|
|
|
|
// references. This facilitates the JIT detecting that a stub is no
|
|
|
|
// longer used, so that it may be deallocated.
|
|
|
|
DenseMap<AssertingVH<const Function>, SmallVector<void*, 1> > CurFnStubUses;
|
|
|
|
|
|
|
|
// StubFnRefs - For a given pointer to a stub, a set of Functions which
|
|
|
|
// reference the stub. When the count of a stub's references drops to zero,
|
|
|
|
// the stub is unused.
|
|
|
|
DenseMap<void *, SmallPtrSet<const Function*, 1> > StubFnRefs;
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-11-07 01:00:10 +01:00
|
|
|
DebugLocTuple PrevDLT;
|
|
|
|
|
|
|
|
public:
|
|
|
|
JITEmitter(JIT &jit, JITMemoryManager *JMM, TargetMachine &TM)
|
|
|
|
: SizeEstimate(0), Resolver(jit, *this), MMI(0), CurFn(0),
|
|
|
|
EmittedFunctions(this) {
|
|
|
|
MemMgr = JMM ? JMM : JITMemoryManager::CreateDefaultMemManager();
|
|
|
|
if (jit.getJITInfo().needsGOT()) {
|
|
|
|
MemMgr->AllocateGOT();
|
|
|
|
DEBUG(errs() << "JIT is managing a GOT\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DwarfExceptionHandling || JITEmitDebugInfo) {
|
|
|
|
DE.reset(new JITDwarfEmitter(jit));
|
|
|
|
}
|
|
|
|
if (JITEmitDebugInfo) {
|
|
|
|
DR.reset(new JITDebugRegisterer(TM));
|
|
|
|
}
|
|
|
|
}
|
2009-11-12 04:12:18 +01:00
|
|
|
~JITEmitter() {
|
2009-11-07 01:00:10 +01:00
|
|
|
delete MemMgr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// classof - Methods for support type inquiry through isa, cast, and
|
|
|
|
/// dyn_cast:
|
|
|
|
///
|
|
|
|
static inline bool classof(const JITEmitter*) { return true; }
|
|
|
|
static inline bool classof(const MachineCodeEmitter*) { return true; }
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-11-07 01:00:10 +01:00
|
|
|
JITResolver &getJITResolver() { return Resolver; }
|
|
|
|
|
|
|
|
virtual void startFunction(MachineFunction &F);
|
|
|
|
virtual bool finishFunction(MachineFunction &F);
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-11-07 01:00:10 +01:00
|
|
|
void emitConstantPool(MachineConstantPool *MCP);
|
|
|
|
void initJumpTableInfo(MachineJumpTableInfo *MJTI);
|
|
|
|
void emitJumpTableInfo(MachineJumpTableInfo *MJTI);
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-11-07 01:00:10 +01:00
|
|
|
virtual void startGVStub(const GlobalValue* GV, unsigned StubSize,
|
|
|
|
unsigned Alignment = 1);
|
|
|
|
virtual void startGVStub(const GlobalValue* GV, void *Buffer,
|
|
|
|
unsigned StubSize);
|
|
|
|
virtual void* finishGVStub(const GlobalValue *GV);
|
|
|
|
|
|
|
|
/// allocateSpace - Reserves space in the current block if any, or
|
|
|
|
/// allocate a new one of the given size.
|
|
|
|
virtual void *allocateSpace(uintptr_t Size, unsigned Alignment);
|
|
|
|
|
|
|
|
/// allocateGlobal - Allocate memory for a global. Unlike allocateSpace,
|
|
|
|
/// this method does not allocate memory in the current output buffer,
|
|
|
|
/// because a global may live longer than the current function.
|
|
|
|
virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment);
|
|
|
|
|
|
|
|
virtual void addRelocation(const MachineRelocation &MR) {
|
|
|
|
Relocations.push_back(MR);
|
|
|
|
}
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-11-07 01:00:10 +01:00
|
|
|
virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
|
|
|
|
if (MBBLocations.size() <= (unsigned)MBB->getNumber())
|
|
|
|
MBBLocations.resize((MBB->getNumber()+1)*2);
|
|
|
|
MBBLocations[MBB->getNumber()] = getCurrentPCValue();
|
|
|
|
DEBUG(errs() << "JIT: Emitting BB" << MBB->getNumber() << " at ["
|
|
|
|
<< (void*) getCurrentPCValue() << "]\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual uintptr_t getConstantPoolEntryAddress(unsigned Entry) const;
|
|
|
|
virtual uintptr_t getJumpTableEntryAddress(unsigned Entry) const;
|
|
|
|
|
|
|
|
virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
|
2009-11-12 04:12:18 +01:00
|
|
|
assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
|
2009-11-07 01:00:10 +01:00
|
|
|
MBBLocations[MBB->getNumber()] && "MBB not emitted!");
|
|
|
|
return MBBLocations[MBB->getNumber()];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// retryWithMoreMemory - Log a retry and deallocate all memory for the
|
|
|
|
/// given function. Increase the minimum allocation size so that we get
|
|
|
|
/// more memory next time.
|
|
|
|
void retryWithMoreMemory(MachineFunction &F);
|
|
|
|
|
|
|
|
/// deallocateMemForFunction - Deallocate all memory for the specified
|
|
|
|
/// function body.
|
|
|
|
void deallocateMemForFunction(const Function *F);
|
|
|
|
|
|
|
|
/// AddStubToCurrentFunction - Mark the current function being JIT'd as
|
|
|
|
/// using the stub at the specified address. Allows
|
|
|
|
/// deallocateMemForFunction to also remove stubs no longer referenced.
|
|
|
|
void AddStubToCurrentFunction(void *Stub);
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-11-07 01:00:10 +01:00
|
|
|
virtual void processDebugLoc(DebugLoc DL, bool BeforePrintingInsn);
|
|
|
|
|
|
|
|
virtual void emitLabel(uint64_t LabelID) {
|
|
|
|
if (LabelLocations.size() <= LabelID)
|
|
|
|
LabelLocations.resize((LabelID+1)*2);
|
|
|
|
LabelLocations[LabelID] = getCurrentPCValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual uintptr_t getLabelAddress(uint64_t LabelID) const {
|
2009-11-12 04:12:18 +01:00
|
|
|
assert(LabelLocations.size() > (unsigned)LabelID &&
|
2009-11-07 01:00:10 +01:00
|
|
|
LabelLocations[LabelID] && "Label not emitted!");
|
|
|
|
return LabelLocations[LabelID];
|
|
|
|
}
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-11-07 01:00:10 +01:00
|
|
|
virtual void setModuleInfo(MachineModuleInfo* Info) {
|
|
|
|
MMI = Info;
|
|
|
|
if (DE.get()) DE->setModuleInfo(Info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setMemoryExecutable() {
|
|
|
|
MemMgr->setMemoryExecutable();
|
|
|
|
}
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-11-07 01:00:10 +01:00
|
|
|
JITMemoryManager *getMemMgr() const { return MemMgr; }
|
|
|
|
|
|
|
|
private:
|
2009-11-07 09:51:52 +01:00
|
|
|
void *getPointerToGlobal(GlobalValue *GV, void *Reference,
|
|
|
|
bool MayNeedFarStub);
|
|
|
|
void *getPointerToGVIndirectSym(GlobalValue *V, void *Reference);
|
2009-11-07 01:00:10 +01:00
|
|
|
unsigned addSizeOfGlobal(const GlobalVariable *GV, unsigned Size);
|
|
|
|
unsigned addSizeOfGlobalsInConstantVal(const Constant *C, unsigned Size);
|
|
|
|
unsigned addSizeOfGlobalsInInitializer(const Constant *Init, unsigned Size);
|
|
|
|
unsigned GetSizeOfGlobalsInBytes(MachineFunction &MF);
|
|
|
|
};
|
2004-11-21 00:57:07 +01:00
|
|
|
}
|
|
|
|
|
2007-02-24 03:57:03 +01:00
|
|
|
JITResolver *JITResolver::TheJITResolver = 0;
|
2004-11-21 00:57:07 +01:00
|
|
|
|
2009-10-24 00:37:43 +02:00
|
|
|
void CallSiteValueMapConfig::onDelete(JITResolverState *JRS, Function *F) {
|
|
|
|
JRS->EraseAllCallSitesPrelocked(F);
|
|
|
|
}
|
|
|
|
|
2008-11-13 22:50:50 +01:00
|
|
|
/// getFunctionStubIfAvailable - This returns a pointer to a function stub
|
|
|
|
/// if it has already been created.
|
|
|
|
void *JITResolver::getFunctionStubIfAvailable(Function *F) {
|
|
|
|
MutexGuard locked(TheJIT->lock);
|
|
|
|
|
|
|
|
// If we already have a stub for this function, recycle it.
|
2009-10-19 20:49:59 +02:00
|
|
|
return state.getFunctionToStubMap(locked).lookup(F);
|
2008-11-13 22:50:50 +01:00
|
|
|
}
|
|
|
|
|
2004-11-21 00:57:07 +01:00
|
|
|
/// getFunctionStub - This returns a pointer to a function stub, creating
|
|
|
|
/// one on demand as needed.
|
2009-03-07 07:41:19 +01:00
|
|
|
void *JITResolver::getFunctionStub(Function *F) {
|
2005-07-12 17:51:55 +02:00
|
|
|
MutexGuard locked(TheJIT->lock);
|
|
|
|
|
2004-11-21 00:57:07 +01:00
|
|
|
// If we already have a stub for this function, recycle it.
|
2005-07-12 17:51:55 +02:00
|
|
|
void *&Stub = state.getFunctionToStubMap(locked)[F];
|
2004-11-21 00:57:07 +01:00
|
|
|
if (Stub) return Stub;
|
|
|
|
|
2009-10-27 21:30:28 +01:00
|
|
|
// Call the lazy resolver function if we are JIT'ing lazily. Otherwise we
|
|
|
|
// must resolve the symbol now.
|
|
|
|
void *Actual = TheJIT->isCompilingLazily()
|
|
|
|
? (void *)(intptr_t)LazyResolverFn : (void *)0;
|
|
|
|
|
2009-03-07 07:41:19 +01:00
|
|
|
// If this is an external declaration, attempt to resolve the address now
|
|
|
|
// to place in the stub.
|
2009-01-05 06:32:42 +01:00
|
|
|
if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode()) {
|
2004-11-22 08:24:43 +01:00
|
|
|
Actual = TheJIT->getPointerToFunction(F);
|
2005-04-22 00:55:34 +02:00
|
|
|
|
2009-01-05 06:32:42 +01:00
|
|
|
// If we resolved the symbol to a null address (eg. a weak external)
|
2009-11-09 23:34:19 +01:00
|
|
|
// don't emit a stub. Return a null pointer to the application.
|
|
|
|
if (!Actual) return 0;
|
2009-01-05 06:32:42 +01:00
|
|
|
}
|
|
|
|
|
2009-03-07 07:41:19 +01:00
|
|
|
// Codegen a new stub, calling the lazy resolver or the actual address of the
|
|
|
|
// external function, if it was resolved.
|
2009-11-07 01:00:10 +01:00
|
|
|
Stub = TheJIT->getJITInfo().emitFunctionStub(F, Actual, JE);
|
2004-11-22 08:24:43 +01:00
|
|
|
|
2006-06-01 19:29:22 +02:00
|
|
|
if (Actual != (void*)(intptr_t)LazyResolverFn) {
|
2004-11-22 08:24:43 +01:00
|
|
|
// If we are getting the stub for an external function, we really want the
|
|
|
|
// address of the stub in the GlobalAddressMap for the JIT, not the address
|
|
|
|
// of the external function.
|
|
|
|
TheJIT->updateGlobalMapping(F, Stub);
|
|
|
|
}
|
2004-11-21 00:57:07 +01:00
|
|
|
|
2009-07-25 02:23:56 +02:00
|
|
|
DEBUG(errs() << "JIT: Stub emitted at [" << Stub << "] for function '"
|
|
|
|
<< F->getName() << "'\n");
|
2004-11-21 04:44:32 +01:00
|
|
|
|
2004-11-21 00:57:07 +01:00
|
|
|
// Finally, keep track of the stub-to-Function mapping so that the
|
|
|
|
// JITCompilerFn knows which function to compile!
|
2009-10-19 20:49:59 +02:00
|
|
|
state.AddCallSite(locked, Stub, F);
|
2009-10-13 23:32:57 +02:00
|
|
|
|
2009-03-07 07:41:19 +01:00
|
|
|
// If we are JIT'ing non-lazily but need to call a function that does not
|
|
|
|
// exist yet, add it to the JIT's work list so that we can fill in the stub
|
|
|
|
// address later.
|
2009-10-27 21:30:28 +01:00
|
|
|
if (!Actual && !TheJIT->isCompilingLazily())
|
2009-03-07 07:41:19 +01:00
|
|
|
if (!F->isDeclaration() || F->hasNotBeenReadFromBitcode())
|
|
|
|
TheJIT->addPendingFunction(F);
|
2009-10-13 23:32:57 +02:00
|
|
|
|
2004-11-21 00:57:07 +01:00
|
|
|
return Stub;
|
|
|
|
}
|
|
|
|
|
2008-11-10 02:52:24 +01:00
|
|
|
/// getGlobalValueIndirectSym - Return a lazy pointer containing the specified
|
2008-01-04 11:46:51 +01:00
|
|
|
/// GV address.
|
2008-11-10 02:52:24 +01:00
|
|
|
void *JITResolver::getGlobalValueIndirectSym(GlobalValue *GV, void *GVAddress) {
|
2008-01-04 11:46:51 +01:00
|
|
|
MutexGuard locked(TheJIT->lock);
|
|
|
|
|
|
|
|
// If we already have a stub for this global variable, recycle it.
|
2008-11-10 02:52:24 +01:00
|
|
|
void *&IndirectSym = state.getGlobalToIndirectSymMap(locked)[GV];
|
|
|
|
if (IndirectSym) return IndirectSym;
|
2008-01-04 11:46:51 +01:00
|
|
|
|
2008-11-11 00:26:16 +01:00
|
|
|
// Otherwise, codegen a new indirect symbol.
|
2008-11-10 02:52:24 +01:00
|
|
|
IndirectSym = TheJIT->getJITInfo().emitGlobalValueIndirectSym(GV, GVAddress,
|
2009-11-07 01:00:10 +01:00
|
|
|
JE);
|
2008-01-04 11:46:51 +01:00
|
|
|
|
2009-11-12 04:12:18 +01:00
|
|
|
DEBUG(errs() << "JIT: Indirect symbol emitted at [" << IndirectSym
|
2009-07-25 02:23:56 +02:00
|
|
|
<< "] for GV '" << GV->getName() << "'\n");
|
2008-01-04 11:46:51 +01:00
|
|
|
|
2008-11-10 02:52:24 +01:00
|
|
|
return IndirectSym;
|
2008-01-04 11:46:51 +01:00
|
|
|
}
|
|
|
|
|
2005-04-18 03:44:27 +02:00
|
|
|
/// getExternalFunctionStub - Return a stub for the function at the
|
|
|
|
/// specified address, created lazily on demand.
|
|
|
|
void *JITResolver::getExternalFunctionStub(void *FnAddr) {
|
|
|
|
// If we already have a stub for this function, recycle it.
|
|
|
|
void *&Stub = ExternalFnToStubMap[FnAddr];
|
|
|
|
if (Stub) return Stub;
|
|
|
|
|
2009-11-07 01:00:10 +01:00
|
|
|
Stub = TheJIT->getJITInfo().emitFunctionStub(0, FnAddr, JE);
|
2006-07-25 22:40:54 +02:00
|
|
|
|
2009-08-23 08:35:02 +02:00
|
|
|
DEBUG(errs() << "JIT: Stub emitted at [" << Stub
|
|
|
|
<< "] for external function at '" << FnAddr << "'\n");
|
2005-04-18 03:44:27 +02:00
|
|
|
return Stub;
|
|
|
|
}
|
|
|
|
|
2005-07-28 14:44:13 +02:00
|
|
|
unsigned JITResolver::getGOTIndexForAddr(void* addr) {
|
|
|
|
unsigned idx = revGOTMap[addr];
|
|
|
|
if (!idx) {
|
|
|
|
idx = ++nextGOTIndex;
|
|
|
|
revGOTMap[addr] = idx;
|
2009-08-23 08:35:02 +02:00
|
|
|
DEBUG(errs() << "JIT: Adding GOT entry " << idx << " for addr ["
|
|
|
|
<< addr << "]\n");
|
2005-07-28 14:44:13 +02:00
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
2005-04-18 03:44:27 +02:00
|
|
|
|
2009-02-18 09:31:02 +01:00
|
|
|
void JITResolver::getRelocatableGVs(SmallVectorImpl<GlobalValue*> &GVs,
|
|
|
|
SmallVectorImpl<void*> &Ptrs) {
|
|
|
|
MutexGuard locked(TheJIT->lock);
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-10-19 20:49:59 +02:00
|
|
|
const FunctionToStubMapTy &FM = state.getFunctionToStubMap(locked);
|
2009-04-27 07:09:44 +02:00
|
|
|
GlobalToIndirectSymMapTy &GM = state.getGlobalToIndirectSymMap(locked);
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-10-19 20:49:59 +02:00
|
|
|
for (FunctionToStubMapTy::const_iterator i = FM.begin(), e = FM.end();
|
|
|
|
i != e; ++i){
|
2009-02-18 09:31:02 +01:00
|
|
|
Function *F = i->first;
|
|
|
|
if (F->isDeclaration() && F->hasExternalLinkage()) {
|
|
|
|
GVs.push_back(i->first);
|
|
|
|
Ptrs.push_back(i->second);
|
|
|
|
}
|
|
|
|
}
|
2009-04-27 07:09:44 +02:00
|
|
|
for (GlobalToIndirectSymMapTy::iterator i = GM.begin(), e = GM.end();
|
2009-02-18 09:31:02 +01:00
|
|
|
i != e; ++i) {
|
|
|
|
GVs.push_back(i->first);
|
|
|
|
Ptrs.push_back(i->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-05 07:34:37 +01:00
|
|
|
GlobalValue *JITResolver::invalidateStub(void *Stub) {
|
|
|
|
MutexGuard locked(TheJIT->lock);
|
2009-10-19 20:49:59 +02:00
|
|
|
|
2009-04-27 07:09:44 +02:00
|
|
|
GlobalToIndirectSymMapTy &GM = state.getGlobalToIndirectSymMap(locked);
|
2009-10-19 20:49:59 +02:00
|
|
|
|
2009-03-05 07:34:37 +01:00
|
|
|
// Look up the cheap way first, to see if it's a function stub we are
|
|
|
|
// invalidating. If so, remove it from both the forward and reverse maps.
|
2009-10-19 20:49:59 +02:00
|
|
|
if (Function *F = state.EraseStub(locked, Stub)) {
|
2009-03-05 07:34:37 +01:00
|
|
|
return F;
|
|
|
|
}
|
2009-10-19 20:49:59 +02:00
|
|
|
|
2009-03-11 08:03:43 +01:00
|
|
|
// Otherwise, it might be an indirect symbol stub. Find it and remove it.
|
2009-04-27 07:09:44 +02:00
|
|
|
for (GlobalToIndirectSymMapTy::iterator i = GM.begin(), e = GM.end();
|
2009-03-05 07:34:37 +01:00
|
|
|
i != e; ++i) {
|
|
|
|
if (i->second != Stub)
|
|
|
|
continue;
|
|
|
|
GlobalValue *GV = i->first;
|
|
|
|
GM.erase(i);
|
|
|
|
return GV;
|
|
|
|
}
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-03-11 08:03:43 +01:00
|
|
|
// Lastly, check to see if it's in the ExternalFnToStubMap.
|
|
|
|
for (std::map<void *, void *>::iterator i = ExternalFnToStubMap.begin(),
|
|
|
|
e = ExternalFnToStubMap.end(); i != e; ++i) {
|
|
|
|
if (i->second != Stub)
|
|
|
|
continue;
|
|
|
|
ExternalFnToStubMap.erase(i);
|
|
|
|
break;
|
|
|
|
}
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-03-05 07:34:37 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-21 00:57:07 +01:00
|
|
|
/// JITCompilerFn - This function is called when a lazy compilation stub has
|
|
|
|
/// been entered. It looks up which function this stub corresponds to, compiles
|
|
|
|
/// it if necessary, then returns the resultant function pointer.
|
|
|
|
void *JITResolver::JITCompilerFn(void *Stub) {
|
2007-02-24 03:57:03 +01:00
|
|
|
JITResolver &JR = *TheJITResolver;
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2008-10-03 09:27:08 +02:00
|
|
|
Function* F = 0;
|
|
|
|
void* ActualPtr = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
// Only lock for getting the Function. The call getPointerToFunction made
|
|
|
|
// in this function might trigger function materializing, which requires
|
|
|
|
// JIT lock to be unlocked.
|
|
|
|
MutexGuard locked(TheJIT->lock);
|
|
|
|
|
2009-10-19 20:49:59 +02:00
|
|
|
// The address given to us for the stub may not be exactly right, it might
|
|
|
|
// be a little bit after the stub. As such, use upper_bound to find it.
|
|
|
|
pair<void*, Function*> I =
|
|
|
|
JR.state.LookupFunctionFromCallSite(locked, Stub);
|
|
|
|
F = I.second;
|
|
|
|
ActualPtr = I.first;
|
2008-10-03 09:27:08 +02:00
|
|
|
}
|
2004-11-21 00:57:07 +01:00
|
|
|
|
2007-06-30 02:10:37 +02:00
|
|
|
// If we have already code generated the function, just return the address.
|
|
|
|
void *Result = TheJIT->getPointerToGlobalIfAvailable(F);
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2007-06-30 02:10:37 +02:00
|
|
|
if (!Result) {
|
|
|
|
// Otherwise we don't have it, do lazy compilation now.
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2007-06-30 02:10:37 +02:00
|
|
|
// If lazy compilation is disabled, emit a useful error message and abort.
|
2009-10-27 21:30:28 +01:00
|
|
|
if (!TheJIT->isCompilingLazily()) {
|
2009-07-11 15:10:19 +02:00
|
|
|
llvm_report_error("LLVM JIT requested to do lazy compilation of function '"
|
|
|
|
+ F->getName() + "' when lazy compiles are disabled!");
|
2007-06-30 02:10:37 +02:00
|
|
|
}
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-07-25 02:23:56 +02:00
|
|
|
DEBUG(errs() << "JIT: Lazily resolving function '" << F->getName()
|
|
|
|
<< "' In stub ptr = " << Stub << " actual ptr = "
|
|
|
|
<< ActualPtr << "\n");
|
2007-06-30 02:10:37 +02:00
|
|
|
|
|
|
|
Result = TheJIT->getPointerToFunction(F);
|
|
|
|
}
|
2009-10-19 20:49:59 +02:00
|
|
|
|
|
|
|
// Reacquire the lock to update the GOT map.
|
2008-10-03 09:27:08 +02:00
|
|
|
MutexGuard locked(TheJIT->lock);
|
2004-11-21 00:57:07 +01:00
|
|
|
|
2009-10-19 20:49:59 +02:00
|
|
|
// We might like to remove the call site from the CallSiteToFunction map, but
|
|
|
|
// we can't do that! Multiple threads could be stuck, waiting to acquire the
|
|
|
|
// lock above. As soon as the 1st function finishes compiling the function,
|
|
|
|
// the next one will be released, and needs to be able to find the function it
|
|
|
|
// needs to call.
|
2004-11-21 00:57:07 +01:00
|
|
|
|
|
|
|
// FIXME: We could rewrite all references to this stub if we knew them.
|
2005-07-28 14:44:13 +02:00
|
|
|
|
2005-07-30 20:33:25 +02:00
|
|
|
// What we will do is set the compiled function address to map to the
|
|
|
|
// same GOT entry as the stub so that later clients may update the GOT
|
2005-07-28 14:44:13 +02:00
|
|
|
// if they see it still using the stub address.
|
|
|
|
// Note: this is done so the Resolver doesn't have to manage GOT memory
|
|
|
|
// Do this without allocating map space if the target isn't using a GOT
|
|
|
|
if(JR.revGOTMap.find(Stub) != JR.revGOTMap.end())
|
|
|
|
JR.revGOTMap[Result] = JR.revGOTMap[Stub];
|
|
|
|
|
2004-11-21 00:57:07 +01:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2004-11-22 23:00:25 +01:00
|
|
|
// JITEmitter code.
|
2004-11-21 00:57:07 +01:00
|
|
|
//
|
2004-11-22 23:00:25 +01:00
|
|
|
void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
|
2009-11-07 09:51:52 +01:00
|
|
|
bool MayNeedFarStub) {
|
2009-02-18 09:31:02 +01:00
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
|
2004-11-21 00:57:07 +01:00
|
|
|
return TheJIT->getOrEmitGlobalVariable(GV);
|
2009-02-18 09:31:02 +01:00
|
|
|
|
2008-06-25 22:21:35 +02:00
|
|
|
if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
|
2008-09-09 22:05:04 +02:00
|
|
|
return TheJIT->getPointerToGlobal(GA->resolveAliasedGlobal(false));
|
2004-11-21 00:57:07 +01:00
|
|
|
|
|
|
|
// If we have already compiled the function, return a pointer to its body.
|
|
|
|
Function *F = cast<Function>(V);
|
2009-11-12 04:12:18 +01:00
|
|
|
|
|
|
|
void *FnStub = Resolver.getFunctionStubIfAvailable(F);
|
|
|
|
if (FnStub) {
|
|
|
|
// Return the function stub if it's already created. We do this first
|
|
|
|
// so that we're returning the same address for the function as any
|
|
|
|
// previous call.
|
|
|
|
AddStubToCurrentFunction(FnStub);
|
|
|
|
return FnStub;
|
2009-03-05 07:34:37 +01:00
|
|
|
}
|
2009-11-12 04:12:18 +01:00
|
|
|
|
|
|
|
// Otherwise if we have code, go ahead and return that.
|
|
|
|
void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
|
2004-11-21 00:57:07 +01:00
|
|
|
if (ResultPtr) return ResultPtr;
|
|
|
|
|
2009-02-18 09:31:02 +01:00
|
|
|
// If this is an external function pointer, we can force the JIT to
|
2009-11-09 23:34:19 +01:00
|
|
|
// 'compile' it, which really just adds it to the map.
|
2009-03-07 07:41:19 +01:00
|
|
|
if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode() &&
|
2009-11-09 23:34:19 +01:00
|
|
|
!MayNeedFarStub)
|
2009-02-18 09:31:02 +01:00
|
|
|
return TheJIT->getPointerToFunction(F);
|
|
|
|
|
2004-11-21 04:37:42 +01:00
|
|
|
// Okay, the function has not been compiled yet, if the target callback
|
|
|
|
// mechanism is capable of rewriting the instruction directly, prefer to do
|
2009-03-07 07:41:19 +01:00
|
|
|
// that instead of emitting a stub. This uses the lazy resolver, so is not
|
|
|
|
// legal if lazy compilation is disabled.
|
2009-11-07 09:51:52 +01:00
|
|
|
if (!MayNeedFarStub && TheJIT->isCompilingLazily())
|
2007-02-24 03:57:03 +01:00
|
|
|
return Resolver.AddCallbackAtLocation(F, Reference);
|
2004-11-21 04:37:42 +01:00
|
|
|
|
2009-03-07 07:41:19 +01:00
|
|
|
// Otherwise, we have to emit a stub.
|
2009-03-05 07:34:37 +01:00
|
|
|
void *StubAddr = Resolver.getFunctionStub(F);
|
|
|
|
|
|
|
|
// Add the stub to the current function's list of referenced stubs, so we can
|
2009-03-07 07:41:19 +01:00
|
|
|
// deallocate them if the current function is ever freed. It's possible to
|
|
|
|
// return null from getFunctionStub in the case of a weak extern that fails
|
|
|
|
// to resolve.
|
|
|
|
if (StubAddr)
|
|
|
|
AddStubToCurrentFunction(StubAddr);
|
|
|
|
|
2009-03-05 07:34:37 +01:00
|
|
|
return StubAddr;
|
2004-11-21 00:57:07 +01:00
|
|
|
}
|
|
|
|
|
2009-11-07 09:51:52 +01:00
|
|
|
void *JITEmitter::getPointerToGVIndirectSym(GlobalValue *V, void *Reference) {
|
2009-03-05 07:34:37 +01:00
|
|
|
// Make sure GV is emitted first, and create a stub containing the fully
|
|
|
|
// resolved address.
|
2009-11-07 09:51:52 +01:00
|
|
|
void *GVAddress = getPointerToGlobal(V, Reference, false);
|
2009-03-05 07:34:37 +01:00
|
|
|
void *StubAddr = Resolver.getGlobalValueIndirectSym(V, GVAddress);
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-03-05 07:34:37 +01:00
|
|
|
// Add the stub to the current function's list of referenced stubs, so we can
|
|
|
|
// deallocate them if the current function is ever freed.
|
|
|
|
AddStubToCurrentFunction(StubAddr);
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-03-05 07:34:37 +01:00
|
|
|
return StubAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JITEmitter::AddStubToCurrentFunction(void *StubAddr) {
|
|
|
|
assert(CurFn && "Stub added to current function, but current function is 0!");
|
2009-10-13 23:32:57 +02:00
|
|
|
|
2009-03-05 07:34:37 +01:00
|
|
|
SmallVectorImpl<void*> &StubsUsed = CurFnStubUses[CurFn];
|
|
|
|
StubsUsed.push_back(StubAddr);
|
|
|
|
|
|
|
|
SmallPtrSet<const Function *, 1> &FnRefs = StubFnRefs[StubAddr];
|
|
|
|
FnRefs.insert(CurFn);
|
2008-01-04 11:46:51 +01:00
|
|
|
}
|
|
|
|
|
2009-10-06 05:04:58 +02:00
|
|
|
void JITEmitter::processDebugLoc(DebugLoc DL, bool BeforePrintingInsn) {
|
2009-07-16 23:07:26 +02:00
|
|
|
if (!DL.isUnknown()) {
|
|
|
|
DebugLocTuple CurDLT = EmissionDetails.MF->getDebugLocTuple(DL);
|
|
|
|
|
2009-10-06 05:04:58 +02:00
|
|
|
if (BeforePrintingInsn) {
|
2009-10-14 01:28:53 +02:00
|
|
|
if (CurDLT.Scope != 0 && PrevDLT != CurDLT) {
|
2009-10-06 05:04:58 +02:00
|
|
|
JITEvent_EmittedFunctionDetails::LineStart NextLine;
|
|
|
|
NextLine.Address = getCurrentPCValue();
|
|
|
|
NextLine.Loc = DL;
|
|
|
|
EmissionDetails.LineStarts.push_back(NextLine);
|
|
|
|
}
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-10-06 05:04:58 +02:00
|
|
|
PrevDLT = CurDLT;
|
2009-07-16 23:07:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-13 08:51:59 +01:00
|
|
|
static unsigned GetConstantPoolSizeInBytes(MachineConstantPool *MCP,
|
|
|
|
const TargetData *TD) {
|
2008-04-18 22:59:31 +02:00
|
|
|
const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants();
|
|
|
|
if (Constants.empty()) return 0;
|
|
|
|
|
2009-03-13 08:51:59 +01:00
|
|
|
unsigned Size = 0;
|
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
|
|
|
|
MachineConstantPoolEntry CPE = Constants[i];
|
|
|
|
unsigned AlignMask = CPE.getAlignment() - 1;
|
|
|
|
Size = (Size + AlignMask) & ~AlignMask;
|
|
|
|
const Type *Ty = CPE.getType();
|
2009-05-09 09:06:46 +02:00
|
|
|
Size += TD->getTypeAllocSize(Ty);
|
2009-03-13 08:51:59 +01:00
|
|
|
}
|
2008-04-18 22:59:31 +02:00
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned GetJumpTableSizeInBytes(MachineJumpTableInfo *MJTI) {
|
|
|
|
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
|
|
|
|
if (JT.empty()) return 0;
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2008-04-18 22:59:31 +02:00
|
|
|
unsigned NumEntries = 0;
|
|
|
|
for (unsigned i = 0, e = JT.size(); i != e; ++i)
|
|
|
|
NumEntries += JT[i].MBBs.size();
|
|
|
|
|
|
|
|
unsigned EntrySize = MJTI->getEntrySize();
|
|
|
|
|
|
|
|
return NumEntries * EntrySize;
|
|
|
|
}
|
|
|
|
|
2008-04-21 01:39:44 +02:00
|
|
|
static uintptr_t RoundUpToAlign(uintptr_t Size, unsigned Alignment) {
|
2008-04-18 22:59:31 +02:00
|
|
|
if (Alignment == 0) Alignment = 1;
|
2009-11-12 04:12:18 +01:00
|
|
|
// Since we do not know where the buffer will be allocated, be pessimistic.
|
2008-04-21 01:39:44 +02:00
|
|
|
return Size + Alignment;
|
2008-04-18 22:59:31 +02:00
|
|
|
}
|
2008-01-04 11:46:51 +01:00
|
|
|
|
2008-08-07 03:30:15 +02:00
|
|
|
/// addSizeOfGlobal - add the size of the global (plus any alignment padding)
|
|
|
|
/// into the running total Size.
|
|
|
|
|
|
|
|
unsigned JITEmitter::addSizeOfGlobal(const GlobalVariable *GV, unsigned Size) {
|
|
|
|
const Type *ElTy = GV->getType()->getElementType();
|
2009-05-09 09:06:46 +02:00
|
|
|
size_t GVSize = (size_t)TheJIT->getTargetData()->getTypeAllocSize(ElTy);
|
2009-11-12 04:12:18 +01:00
|
|
|
size_t GVAlign =
|
2008-08-07 03:30:15 +02:00
|
|
|
(size_t)TheJIT->getTargetData()->getPreferredAlignment(GV);
|
2009-08-23 08:35:02 +02:00
|
|
|
DEBUG(errs() << "JIT: Adding in size " << GVSize << " alignment " << GVAlign);
|
2008-08-07 03:30:15 +02:00
|
|
|
DEBUG(GV->dump());
|
|
|
|
// Assume code section ends with worst possible alignment, so first
|
|
|
|
// variable needs maximal padding.
|
|
|
|
if (Size==0)
|
|
|
|
Size = 1;
|
|
|
|
Size = ((Size+GVAlign-1)/GVAlign)*GVAlign;
|
|
|
|
Size += GVSize;
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// addSizeOfGlobalsInConstantVal - find any globals that we haven't seen yet
|
|
|
|
/// but are referenced from the constant; put them in GVSet and add their
|
|
|
|
/// size into the running total Size.
|
|
|
|
|
2009-11-12 04:12:18 +01:00
|
|
|
unsigned JITEmitter::addSizeOfGlobalsInConstantVal(const Constant *C,
|
2008-08-07 03:30:15 +02:00
|
|
|
unsigned Size) {
|
|
|
|
// If its undefined, return the garbage.
|
|
|
|
if (isa<UndefValue>(C))
|
|
|
|
return Size;
|
|
|
|
|
|
|
|
// If the value is a ConstantExpr
|
|
|
|
if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
|
|
|
|
Constant *Op0 = CE->getOperand(0);
|
|
|
|
switch (CE->getOpcode()) {
|
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
case Instruction::Trunc:
|
|
|
|
case Instruction::ZExt:
|
|
|
|
case Instruction::SExt:
|
|
|
|
case Instruction::FPTrunc:
|
|
|
|
case Instruction::FPExt:
|
|
|
|
case Instruction::UIToFP:
|
|
|
|
case Instruction::SIToFP:
|
|
|
|
case Instruction::FPToUI:
|
|
|
|
case Instruction::FPToSI:
|
|
|
|
case Instruction::PtrToInt:
|
|
|
|
case Instruction::IntToPtr:
|
|
|
|
case Instruction::BitCast: {
|
|
|
|
Size = addSizeOfGlobalsInConstantVal(Op0, Size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::Add:
|
2009-06-05 00:49:04 +02:00
|
|
|
case Instruction::FAdd:
|
2008-08-07 03:30:15 +02:00
|
|
|
case Instruction::Sub:
|
2009-06-05 00:49:04 +02:00
|
|
|
case Instruction::FSub:
|
2008-08-07 03:30:15 +02:00
|
|
|
case Instruction::Mul:
|
2009-06-05 00:49:04 +02:00
|
|
|
case Instruction::FMul:
|
2008-08-07 03:30:15 +02:00
|
|
|
case Instruction::UDiv:
|
|
|
|
case Instruction::SDiv:
|
|
|
|
case Instruction::URem:
|
|
|
|
case Instruction::SRem:
|
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor: {
|
|
|
|
Size = addSizeOfGlobalsInConstantVal(Op0, Size);
|
|
|
|
Size = addSizeOfGlobalsInConstantVal(CE->getOperand(1), Size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2009-07-11 15:10:19 +02:00
|
|
|
std::string msg;
|
|
|
|
raw_string_ostream Msg(msg);
|
|
|
|
Msg << "ConstantExpr not handled: " << *CE;
|
|
|
|
llvm_report_error(Msg.str());
|
2008-08-07 03:30:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C->getType()->getTypeID() == Type::PointerTyID)
|
|
|
|
if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
|
2008-11-07 10:02:17 +01:00
|
|
|
if (GVSet.insert(GV))
|
2008-08-07 03:30:15 +02:00
|
|
|
Size = addSizeOfGlobal(GV, Size);
|
|
|
|
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// addSizeOfGLobalsInInitializer - handle any globals that we haven't seen yet
|
|
|
|
/// but are referenced from the given initializer.
|
|
|
|
|
2009-11-12 04:12:18 +01:00
|
|
|
unsigned JITEmitter::addSizeOfGlobalsInInitializer(const Constant *Init,
|
2008-08-07 03:30:15 +02:00
|
|
|
unsigned Size) {
|
|
|
|
if (!isa<UndefValue>(Init) &&
|
|
|
|
!isa<ConstantVector>(Init) &&
|
|
|
|
!isa<ConstantAggregateZero>(Init) &&
|
|
|
|
!isa<ConstantArray>(Init) &&
|
|
|
|
!isa<ConstantStruct>(Init) &&
|
|
|
|
Init->getType()->isFirstClassType())
|
|
|
|
Size = addSizeOfGlobalsInConstantVal(Init, Size);
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GetSizeOfGlobalsInBytes - walk the code for the function, looking for
|
|
|
|
/// globals; then walk the initializers of those globals looking for more.
|
|
|
|
/// If their size has not been considered yet, add it into the running total
|
|
|
|
/// Size.
|
|
|
|
|
|
|
|
unsigned JITEmitter::GetSizeOfGlobalsInBytes(MachineFunction &MF) {
|
|
|
|
unsigned Size = 0;
|
|
|
|
GVSet.clear();
|
|
|
|
|
2009-11-12 04:12:18 +01:00
|
|
|
for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
|
2008-08-07 03:30:15 +02:00
|
|
|
MBB != E; ++MBB) {
|
|
|
|
for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const TargetInstrDesc &Desc = I->getDesc();
|
|
|
|
const MachineInstr &MI = *I;
|
|
|
|
unsigned NumOps = Desc.getNumOperands();
|
|
|
|
for (unsigned CurOp = 0; CurOp < NumOps; CurOp++) {
|
|
|
|
const MachineOperand &MO = MI.getOperand(CurOp);
|
2008-10-03 17:45:36 +02:00
|
|
|
if (MO.isGlobal()) {
|
2008-08-07 03:30:15 +02:00
|
|
|
GlobalValue* V = MO.getGlobal();
|
|
|
|
const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V);
|
|
|
|
if (!GV)
|
|
|
|
continue;
|
|
|
|
// If seen in previous function, it will have an entry here.
|
|
|
|
if (TheJIT->getPointerToGlobalIfAvailable(GV))
|
|
|
|
continue;
|
|
|
|
// If seen earlier in this function, it will have an entry here.
|
|
|
|
// FIXME: it should be possible to combine these tables, by
|
|
|
|
// assuming the addresses of the new globals in this module
|
|
|
|
// start at 0 (or something) and adjusting them after codegen
|
|
|
|
// complete. Another possibility is to grab a marker bit in GV.
|
2008-11-07 10:02:17 +01:00
|
|
|
if (GVSet.insert(GV))
|
2008-08-07 03:30:15 +02:00
|
|
|
// A variable as yet unseen. Add in its size.
|
|
|
|
Size = addSizeOfGlobal(GV, Size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-23 08:35:02 +02:00
|
|
|
DEBUG(errs() << "JIT: About to look through initializers\n");
|
2008-08-07 03:30:15 +02:00
|
|
|
// Look for more globals that are referenced only from initializers.
|
|
|
|
// GVSet.end is computed each time because the set can grow as we go.
|
2009-11-12 04:12:18 +01:00
|
|
|
for (SmallPtrSet<const GlobalVariable *, 8>::iterator I = GVSet.begin();
|
2008-08-07 03:30:15 +02:00
|
|
|
I != GVSet.end(); I++) {
|
|
|
|
const GlobalVariable* GV = *I;
|
|
|
|
if (GV->hasInitializer())
|
|
|
|
Size = addSizeOfGlobalsInInitializer(GV->getInitializer(), Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2004-11-22 23:00:25 +01:00
|
|
|
void JITEmitter::startFunction(MachineFunction &F) {
|
2009-07-25 02:23:56 +02:00
|
|
|
DEBUG(errs() << "JIT: Starting CodeGen of Function "
|
|
|
|
<< F.getFunction()->getName() << "\n");
|
2008-11-06 18:46:04 +01:00
|
|
|
|
2008-04-18 22:59:31 +02:00
|
|
|
uintptr_t ActualSize = 0;
|
2008-10-03 18:17:20 +02:00
|
|
|
// Set the memory writable, if it's not already
|
|
|
|
MemMgr->setMemoryWritable();
|
2008-04-20 19:44:19 +02:00
|
|
|
if (MemMgr->NeedsExactSize()) {
|
2009-08-23 08:35:02 +02:00
|
|
|
DEBUG(errs() << "JIT: ExactSize\n");
|
2008-04-18 22:59:31 +02:00
|
|
|
const TargetInstrInfo* TII = F.getTarget().getInstrInfo();
|
|
|
|
MachineJumpTableInfo *MJTI = F.getJumpTableInfo();
|
|
|
|
MachineConstantPool *MCP = F.getConstantPool();
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2008-04-18 22:59:31 +02:00
|
|
|
// Ensure the constant pool/jump table info is at least 4-byte aligned.
|
2008-04-21 01:39:44 +02:00
|
|
|
ActualSize = RoundUpToAlign(ActualSize, 16);
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2008-04-18 22:59:31 +02:00
|
|
|
// Add the alignment of the constant pool
|
2009-03-13 08:51:59 +01:00
|
|
|
ActualSize = RoundUpToAlign(ActualSize, MCP->getConstantPoolAlignment());
|
2008-04-18 22:59:31 +02:00
|
|
|
|
|
|
|
// Add the constant pool size
|
2009-03-13 08:51:59 +01:00
|
|
|
ActualSize += GetConstantPoolSizeInBytes(MCP, TheJIT->getTargetData());
|
2008-04-18 22:59:31 +02:00
|
|
|
|
|
|
|
// Add the aligment of the jump table info
|
2008-04-21 01:39:44 +02:00
|
|
|
ActualSize = RoundUpToAlign(ActualSize, MJTI->getAlignment());
|
2008-04-18 22:59:31 +02:00
|
|
|
|
|
|
|
// Add the jump table size
|
|
|
|
ActualSize += GetJumpTableSizeInBytes(MJTI);
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2008-04-18 22:59:31 +02:00
|
|
|
// Add the alignment for the function
|
2008-04-21 01:39:44 +02:00
|
|
|
ActualSize = RoundUpToAlign(ActualSize,
|
|
|
|
std::max(F.getFunction()->getAlignment(), 8U));
|
2008-04-18 22:59:31 +02:00
|
|
|
|
|
|
|
// Add the function size
|
|
|
|
ActualSize += TII->GetFunctionSizeInBytes(F);
|
2008-08-07 03:30:15 +02:00
|
|
|
|
2009-08-23 08:35:02 +02:00
|
|
|
DEBUG(errs() << "JIT: ActualSize before globals " << ActualSize << "\n");
|
2008-08-07 03:30:15 +02:00
|
|
|
// Add the size of the globals that will be allocated after this function.
|
|
|
|
// These are all the ones referenced from this function that were not
|
|
|
|
// previously allocated.
|
|
|
|
ActualSize += GetSizeOfGlobalsInBytes(F);
|
2009-08-23 08:35:02 +02:00
|
|
|
DEBUG(errs() << "JIT: ActualSize after globals " << ActualSize << "\n");
|
2009-07-23 23:46:56 +02:00
|
|
|
} else if (SizeEstimate > 0) {
|
|
|
|
// SizeEstimate will be non-zero on reallocation attempts.
|
|
|
|
ActualSize = SizeEstimate;
|
2008-04-18 22:59:31 +02:00
|
|
|
}
|
|
|
|
|
2007-12-06 00:39:57 +01:00
|
|
|
BufferBegin = CurBufferPtr = MemMgr->startFunctionBody(F.getFunction(),
|
|
|
|
ActualSize);
|
2006-05-12 01:08:08 +02:00
|
|
|
BufferEnd = BufferBegin+ActualSize;
|
2009-10-20 20:13:21 +02:00
|
|
|
EmittedFunctions[F.getFunction()].FunctionBody = BufferBegin;
|
|
|
|
|
2006-11-16 21:04:54 +01:00
|
|
|
// Ensure the constant pool/jump table info is at least 4-byte aligned.
|
|
|
|
emitAlignment(16);
|
|
|
|
|
2006-05-03 01:22:24 +02:00
|
|
|
emitConstantPool(F.getConstantPool());
|
|
|
|
initJumpTableInfo(F.getJumpTableInfo());
|
|
|
|
|
|
|
|
// About to start emitting the machine code for the function.
|
2006-05-03 03:03:20 +02:00
|
|
|
emitAlignment(std::max(F.getFunction()->getAlignment(), 8U));
|
2006-05-03 01:22:24 +02:00
|
|
|
TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr);
|
2009-10-27 01:03:05 +01:00
|
|
|
EmittedFunctions[F.getFunction()].Code = CurBufferPtr;
|
2006-07-25 22:40:54 +02:00
|
|
|
|
2006-05-03 19:10:41 +02:00
|
|
|
MBBLocations.clear();
|
2009-07-16 23:07:26 +02:00
|
|
|
|
|
|
|
EmissionDetails.MF = &F;
|
|
|
|
EmissionDetails.LineStarts.clear();
|
2002-12-24 01:01:05 +01:00
|
|
|
}
|
|
|
|
|
2006-05-02 20:27:26 +02:00
|
|
|
bool JITEmitter::finishFunction(MachineFunction &F) {
|
2006-05-12 01:08:08 +02:00
|
|
|
if (CurBufferPtr == BufferEnd) {
|
2009-07-23 23:46:56 +02:00
|
|
|
// We must call endFunctionBody before retrying, because
|
|
|
|
// deallocateMemForFunction requires it.
|
|
|
|
MemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr);
|
|
|
|
retryWithMoreMemory(F);
|
|
|
|
return true;
|
2006-05-12 01:08:08 +02:00
|
|
|
}
|
2009-07-23 23:46:56 +02:00
|
|
|
|
2006-12-14 23:53:42 +01:00
|
|
|
emitJumpTableInfo(F.getJumpTableInfo());
|
2009-07-23 23:46:56 +02:00
|
|
|
|
2006-06-16 20:09:26 +02:00
|
|
|
// FnStart is the start of the text, not the start of the constant pool and
|
|
|
|
// other per-function data.
|
2009-06-04 02:15:51 +02:00
|
|
|
uint8_t *FnStart =
|
|
|
|
(uint8_t *)TheJIT->getPointerToGlobalIfAvailable(F.getFunction());
|
2002-12-24 01:01:05 +01:00
|
|
|
|
2009-05-01 01:01:58 +02:00
|
|
|
// FnEnd is the end of the function's machine code.
|
2009-06-04 02:15:51 +02:00
|
|
|
uint8_t *FnEnd = CurBufferPtr;
|
2009-05-01 01:01:58 +02:00
|
|
|
|
2004-11-20 04:46:14 +01:00
|
|
|
if (!Relocations.empty()) {
|
2009-03-05 07:34:37 +01:00
|
|
|
CurFn = F.getFunction();
|
2005-07-20 18:29:20 +02:00
|
|
|
NumRelos += Relocations.size();
|
|
|
|
|
2004-11-20 04:46:14 +01:00
|
|
|
// Resolve the relocations to concrete pointers.
|
|
|
|
for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
|
|
|
|
MachineRelocation &MR = Relocations[i];
|
2008-11-03 08:14:02 +01:00
|
|
|
void *ResultPtr = 0;
|
2008-10-30 00:54:46 +01:00
|
|
|
if (!MR.letTargetResolve()) {
|
2008-11-08 08:37:34 +01:00
|
|
|
if (MR.isExternalSymbol()) {
|
2009-01-05 06:32:42 +01:00
|
|
|
ResultPtr = TheJIT->getPointerToNamedFunction(MR.getExternalSymbol(),
|
|
|
|
false);
|
2009-08-23 08:35:02 +02:00
|
|
|
DEBUG(errs() << "JIT: Map \'" << MR.getExternalSymbol() << "\' to ["
|
2009-11-12 04:12:18 +01:00
|
|
|
<< ResultPtr << "]\n");
|
2008-10-30 00:54:46 +01:00
|
|
|
|
|
|
|
// If the target REALLY wants a stub for this function, emit it now.
|
2009-11-07 09:51:52 +01:00
|
|
|
if (MR.mayNeedFarStub()) {
|
2009-11-09 23:34:19 +01:00
|
|
|
ResultPtr = Resolver.getExternalFunctionStub(ResultPtr);
|
2009-03-11 08:03:43 +01:00
|
|
|
}
|
2008-10-30 00:54:46 +01:00
|
|
|
} else if (MR.isGlobalValue()) {
|
|
|
|
ResultPtr = getPointerToGlobal(MR.getGlobalValue(),
|
|
|
|
BufferBegin+MR.getMachineCodeOffset(),
|
2009-11-07 09:51:52 +01:00
|
|
|
MR.mayNeedFarStub());
|
2008-11-10 02:52:24 +01:00
|
|
|
} else if (MR.isIndirectSymbol()) {
|
2009-11-07 09:51:52 +01:00
|
|
|
ResultPtr = getPointerToGVIndirectSym(
|
|
|
|
MR.getGlobalValue(), BufferBegin+MR.getMachineCodeOffset());
|
2008-10-30 00:54:46 +01:00
|
|
|
} else if (MR.isBasicBlock()) {
|
|
|
|
ResultPtr = (void*)getMachineBasicBlockAddress(MR.getBasicBlock());
|
|
|
|
} else if (MR.isConstantPoolIndex()) {
|
|
|
|
ResultPtr = (void*)getConstantPoolEntryAddress(MR.getConstantPoolIndex());
|
|
|
|
} else {
|
|
|
|
assert(MR.isJumpTableIndex());
|
|
|
|
ResultPtr=(void*)getJumpTableEntryAddress(MR.getJumpTableIndex());
|
|
|
|
}
|
2005-07-27 08:12:32 +02:00
|
|
|
|
2008-10-30 00:54:46 +01:00
|
|
|
MR.setResultPointer(ResultPtr);
|
|
|
|
}
|
2005-07-22 22:48:12 +02:00
|
|
|
|
2005-07-28 14:44:13 +02:00
|
|
|
// if we are managing the GOT and the relocation wants an index,
|
|
|
|
// give it one
|
2007-12-06 00:39:57 +01:00
|
|
|
if (MR.isGOTRelative() && MemMgr->isManagingGOT()) {
|
2007-02-24 03:57:03 +01:00
|
|
|
unsigned idx = Resolver.getGOTIndexForAddr(ResultPtr);
|
2005-07-28 14:44:13 +02:00
|
|
|
MR.setGOTIndex(idx);
|
2007-12-06 00:39:57 +01:00
|
|
|
if (((void**)MemMgr->getGOTBase())[idx] != ResultPtr) {
|
2009-08-23 08:35:02 +02:00
|
|
|
DEBUG(errs() << "JIT: GOT was out of date for " << ResultPtr
|
|
|
|
<< " pointing at " << ((void**)MemMgr->getGOTBase())[idx]
|
|
|
|
<< "\n");
|
2007-12-06 00:39:57 +01:00
|
|
|
((void**)MemMgr->getGOTBase())[idx] = ResultPtr;
|
2005-07-28 14:44:13 +02:00
|
|
|
}
|
2005-07-22 22:48:12 +02:00
|
|
|
}
|
2004-11-20 04:46:14 +01:00
|
|
|
}
|
|
|
|
|
2009-03-05 07:34:37 +01:00
|
|
|
CurFn = 0;
|
2006-05-02 20:27:26 +02:00
|
|
|
TheJIT->getJITInfo().relocate(BufferBegin, &Relocations[0],
|
2007-12-06 00:39:57 +01:00
|
|
|
Relocations.size(), MemMgr->getGOTBase());
|
2004-11-20 04:46:14 +01:00
|
|
|
}
|
|
|
|
|
2006-05-03 20:55:56 +02:00
|
|
|
// Update the GOT entry for F to point to the new code.
|
2007-12-06 00:39:57 +01:00
|
|
|
if (MemMgr->isManagingGOT()) {
|
2007-02-24 03:57:03 +01:00
|
|
|
unsigned idx = Resolver.getGOTIndexForAddr((void*)BufferBegin);
|
2007-12-06 00:39:57 +01:00
|
|
|
if (((void**)MemMgr->getGOTBase())[idx] != (void*)BufferBegin) {
|
2009-08-23 08:35:02 +02:00
|
|
|
DEBUG(errs() << "JIT: GOT was out of date for " << (void*)BufferBegin
|
|
|
|
<< " pointing at " << ((void**)MemMgr->getGOTBase())[idx]
|
|
|
|
<< "\n");
|
2007-12-06 00:39:57 +01:00
|
|
|
((void**)MemMgr->getGOTBase())[idx] = (void*)BufferBegin;
|
2005-07-28 14:44:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-01 01:01:58 +02:00
|
|
|
// CurBufferPtr may have moved beyond FnEnd, due to memory allocation for
|
|
|
|
// global variables that were referenced in the relocations.
|
|
|
|
MemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr);
|
2008-12-10 03:32:19 +01:00
|
|
|
|
|
|
|
if (CurBufferPtr == BufferEnd) {
|
2009-07-23 23:46:56 +02:00
|
|
|
retryWithMoreMemory(F);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Now that we've succeeded in emitting the function, reset the
|
|
|
|
// SizeEstimate back down to zero.
|
|
|
|
SizeEstimate = 0;
|
2008-12-10 03:32:19 +01:00
|
|
|
}
|
|
|
|
|
2008-10-21 13:42:16 +02:00
|
|
|
BufferBegin = CurBufferPtr = 0;
|
|
|
|
NumBytes += FnEnd-FnStart;
|
|
|
|
|
2006-07-25 22:40:54 +02:00
|
|
|
// Invalidate the icache if necessary.
|
2008-06-25 19:18:44 +02:00
|
|
|
sys::Memory::InvalidateInstructionCache(FnStart, FnEnd-FnStart);
|
2009-06-25 04:04:04 +02:00
|
|
|
|
|
|
|
TheJIT->NotifyFunctionEmitted(*F.getFunction(), FnStart, FnEnd-FnStart,
|
2009-07-16 23:07:26 +02:00
|
|
|
EmissionDetails);
|
2006-07-25 22:40:54 +02:00
|
|
|
|
2009-07-25 02:23:56 +02:00
|
|
|
DEBUG(errs() << "JIT: Finished CodeGen of [" << (void*)FnStart
|
|
|
|
<< "] Function: " << F.getFunction()->getName()
|
|
|
|
<< ": " << (FnEnd-FnStart) << " bytes of text, "
|
|
|
|
<< Relocations.size() << " relocations\n");
|
2009-05-18 23:06:40 +02:00
|
|
|
|
2004-11-20 04:46:14 +01:00
|
|
|
Relocations.clear();
|
2009-03-13 08:51:59 +01:00
|
|
|
ConstPoolAddresses.clear();
|
2007-01-19 18:25:17 +01:00
|
|
|
|
2008-09-18 09:54:21 +02:00
|
|
|
// Mark code region readable and executable if it's not so already.
|
2008-10-03 18:17:20 +02:00
|
|
|
MemMgr->setMemoryExecutable();
|
2008-09-18 09:54:21 +02:00
|
|
|
|
2009-08-23 08:35:02 +02:00
|
|
|
DEBUG(
|
2008-11-12 09:22:43 +01:00
|
|
|
if (sys::hasDisassembler()) {
|
2009-08-23 08:35:02 +02:00
|
|
|
errs() << "JIT: Disassembled code:\n";
|
|
|
|
errs() << sys::disassembleBuffer(FnStart, FnEnd-FnStart,
|
|
|
|
(uintptr_t)FnStart);
|
2008-11-12 09:22:43 +01:00
|
|
|
} else {
|
2009-08-23 08:35:02 +02:00
|
|
|
errs() << "JIT: Binary code:\n";
|
2009-06-04 02:15:51 +02:00
|
|
|
uint8_t* q = FnStart;
|
2008-11-12 09:22:43 +01:00
|
|
|
for (int i = 0; q < FnEnd; q += 4, ++i) {
|
|
|
|
if (i == 4)
|
|
|
|
i = 0;
|
|
|
|
if (i == 0)
|
2009-08-23 08:35:02 +02:00
|
|
|
errs() << "JIT: " << (long)(q - FnStart) << ": ";
|
2008-11-12 09:22:43 +01:00
|
|
|
bool Done = false;
|
|
|
|
for (int j = 3; j >= 0; --j) {
|
|
|
|
if (q + j >= FnEnd)
|
|
|
|
Done = true;
|
|
|
|
else
|
2009-08-23 08:35:02 +02:00
|
|
|
errs() << (unsigned short)q[j];
|
2008-11-12 09:22:43 +01:00
|
|
|
}
|
|
|
|
if (Done)
|
|
|
|
break;
|
2009-08-23 08:35:02 +02:00
|
|
|
errs() << ' ';
|
2008-11-12 09:22:43 +01:00
|
|
|
if (i == 3)
|
2009-08-23 08:35:02 +02:00
|
|
|
errs() << '\n';
|
2008-11-06 00:44:08 +01:00
|
|
|
}
|
2009-08-23 08:35:02 +02:00
|
|
|
errs()<< '\n';
|
2008-11-06 00:44:08 +01:00
|
|
|
}
|
2009-08-23 08:35:02 +02:00
|
|
|
);
|
|
|
|
|
Implement the JIT side of the GDB JIT debugging interface. To enable this
feature, either build the JIT in debug mode to enable it by default or pass
-jit-emit-debug to lli.
Right now, the only debug information that this communicates to GDB is call
frame information, since it's already being generated to support exceptions in
the JIT. Eventually, when DWARF generation isn't tied so tightly to AsmPrinter,
it will be easy to push that information to GDB through this interface.
Here's a step-by-step breakdown of how the feature works:
- The JIT generates the machine code and DWARF call frame info
(.eh_frame/.debug_frame) for a function into memory.
- The JIT copies that info into an in-memory ELF file with a symbol for the
function.
- The JIT creates a code entry pointing to the ELF buffer and adds it to a
linked list hanging off of a global descriptor at a special symbol that GDB
knows about.
- The JIT calls a function marked noinline that GDB knows about and has put an
internal breakpoint in.
- GDB catches the breakpoint and reads the global descriptor to look for new
code.
- When sees there is new code, it reads the ELF from the inferior's memory and
adds it to itself as an object file.
- The JIT continues, and the next time we stop the program, we are able to
produce a proper backtrace.
Consider running the following program through the JIT:
#include <stdio.h>
void baz(short z) {
long w = z + 1;
printf("%d, %x\n", w, *((int*)NULL)); // SEGFAULT here
}
void bar(short y) {
int z = y + 1;
baz(z);
}
void foo(char x) {
short y = x + 1;
bar(y);
}
int main(int argc, char** argv) {
char x = 1;
foo(x);
}
Here is a backtrace before this patch:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x2aaaabdfbd10 (LWP 25476)]
0x00002aaaabe7d1a8 in ?? ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in ?? ()
#1 0x0000000000000003 in ?? ()
#2 0x0000000000000004 in ?? ()
#3 0x00032aaaabe7cfd0 in ?? ()
#4 0x00002aaaabe7d12c in ?? ()
#5 0x00022aaa00000003 in ?? ()
#6 0x00002aaaabe7d0aa in ?? ()
#7 0x01000002abe7cff0 in ?? ()
#8 0x00002aaaabe7d02c in ?? ()
#9 0x0100000000000001 in ?? ()
#10 0x00000000014388e0 in ?? ()
#11 0x00007fff00000001 in ?? ()
#12 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=@0x7fffffffe050)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#13 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=@0x13f06f8, envp=0x7fffffffe3b0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#14 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe398,
envp=0x7fffffffe3b0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
And a backtrace after this patch:
Program received signal SIGSEGV, Segmentation fault.
0x00002aaaabe7d1a8 in baz ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in baz ()
#1 0x00002aaaabe7d12c in bar ()
#2 0x00002aaaabe7d0aa in foo ()
#3 0x00002aaaabe7d02c in main ()
#4 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=...)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#5 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=..., envp=0x7fffffffe3c0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#6 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe3a8,
envp=0x7fffffffe3c0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
llvm-svn: 82418
2009-09-21 01:52:43 +02:00
|
|
|
if (DwarfExceptionHandling || JITEmitDebugInfo) {
|
2008-04-18 22:59:31 +02:00
|
|
|
uintptr_t ActualSize = 0;
|
2008-02-13 19:39:37 +01:00
|
|
|
SavedBufferBegin = BufferBegin;
|
|
|
|
SavedBufferEnd = BufferEnd;
|
|
|
|
SavedCurBufferPtr = CurBufferPtr;
|
Implement the JIT side of the GDB JIT debugging interface. To enable this
feature, either build the JIT in debug mode to enable it by default or pass
-jit-emit-debug to lli.
Right now, the only debug information that this communicates to GDB is call
frame information, since it's already being generated to support exceptions in
the JIT. Eventually, when DWARF generation isn't tied so tightly to AsmPrinter,
it will be easy to push that information to GDB through this interface.
Here's a step-by-step breakdown of how the feature works:
- The JIT generates the machine code and DWARF call frame info
(.eh_frame/.debug_frame) for a function into memory.
- The JIT copies that info into an in-memory ELF file with a symbol for the
function.
- The JIT creates a code entry pointing to the ELF buffer and adds it to a
linked list hanging off of a global descriptor at a special symbol that GDB
knows about.
- The JIT calls a function marked noinline that GDB knows about and has put an
internal breakpoint in.
- GDB catches the breakpoint and reads the global descriptor to look for new
code.
- When sees there is new code, it reads the ELF from the inferior's memory and
adds it to itself as an object file.
- The JIT continues, and the next time we stop the program, we are able to
produce a proper backtrace.
Consider running the following program through the JIT:
#include <stdio.h>
void baz(short z) {
long w = z + 1;
printf("%d, %x\n", w, *((int*)NULL)); // SEGFAULT here
}
void bar(short y) {
int z = y + 1;
baz(z);
}
void foo(char x) {
short y = x + 1;
bar(y);
}
int main(int argc, char** argv) {
char x = 1;
foo(x);
}
Here is a backtrace before this patch:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x2aaaabdfbd10 (LWP 25476)]
0x00002aaaabe7d1a8 in ?? ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in ?? ()
#1 0x0000000000000003 in ?? ()
#2 0x0000000000000004 in ?? ()
#3 0x00032aaaabe7cfd0 in ?? ()
#4 0x00002aaaabe7d12c in ?? ()
#5 0x00022aaa00000003 in ?? ()
#6 0x00002aaaabe7d0aa in ?? ()
#7 0x01000002abe7cff0 in ?? ()
#8 0x00002aaaabe7d02c in ?? ()
#9 0x0100000000000001 in ?? ()
#10 0x00000000014388e0 in ?? ()
#11 0x00007fff00000001 in ?? ()
#12 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=@0x7fffffffe050)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#13 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=@0x13f06f8, envp=0x7fffffffe3b0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#14 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe398,
envp=0x7fffffffe3b0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
And a backtrace after this patch:
Program received signal SIGSEGV, Segmentation fault.
0x00002aaaabe7d1a8 in baz ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in baz ()
#1 0x00002aaaabe7d12c in bar ()
#2 0x00002aaaabe7d0aa in foo ()
#3 0x00002aaaabe7d02c in main ()
#4 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=...)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#5 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=..., envp=0x7fffffffe3c0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#6 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe3a8,
envp=0x7fffffffe3c0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
llvm-svn: 82418
2009-09-21 01:52:43 +02:00
|
|
|
|
2008-04-20 19:44:19 +02:00
|
|
|
if (MemMgr->NeedsExactSize()) {
|
|
|
|
ActualSize = DE->GetDwarfTableSizeInBytes(F, *this, FnStart, FnEnd);
|
2008-04-18 22:59:31 +02:00
|
|
|
}
|
2008-02-13 19:39:37 +01:00
|
|
|
|
|
|
|
BufferBegin = CurBufferPtr = MemMgr->startExceptionTable(F.getFunction(),
|
|
|
|
ActualSize);
|
|
|
|
BufferEnd = BufferBegin+ActualSize;
|
2009-10-20 20:13:21 +02:00
|
|
|
EmittedFunctions[F.getFunction()].ExceptionTable = BufferBegin;
|
Implement the JIT side of the GDB JIT debugging interface. To enable this
feature, either build the JIT in debug mode to enable it by default or pass
-jit-emit-debug to lli.
Right now, the only debug information that this communicates to GDB is call
frame information, since it's already being generated to support exceptions in
the JIT. Eventually, when DWARF generation isn't tied so tightly to AsmPrinter,
it will be easy to push that information to GDB through this interface.
Here's a step-by-step breakdown of how the feature works:
- The JIT generates the machine code and DWARF call frame info
(.eh_frame/.debug_frame) for a function into memory.
- The JIT copies that info into an in-memory ELF file with a symbol for the
function.
- The JIT creates a code entry pointing to the ELF buffer and adds it to a
linked list hanging off of a global descriptor at a special symbol that GDB
knows about.
- The JIT calls a function marked noinline that GDB knows about and has put an
internal breakpoint in.
- GDB catches the breakpoint and reads the global descriptor to look for new
code.
- When sees there is new code, it reads the ELF from the inferior's memory and
adds it to itself as an object file.
- The JIT continues, and the next time we stop the program, we are able to
produce a proper backtrace.
Consider running the following program through the JIT:
#include <stdio.h>
void baz(short z) {
long w = z + 1;
printf("%d, %x\n", w, *((int*)NULL)); // SEGFAULT here
}
void bar(short y) {
int z = y + 1;
baz(z);
}
void foo(char x) {
short y = x + 1;
bar(y);
}
int main(int argc, char** argv) {
char x = 1;
foo(x);
}
Here is a backtrace before this patch:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x2aaaabdfbd10 (LWP 25476)]
0x00002aaaabe7d1a8 in ?? ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in ?? ()
#1 0x0000000000000003 in ?? ()
#2 0x0000000000000004 in ?? ()
#3 0x00032aaaabe7cfd0 in ?? ()
#4 0x00002aaaabe7d12c in ?? ()
#5 0x00022aaa00000003 in ?? ()
#6 0x00002aaaabe7d0aa in ?? ()
#7 0x01000002abe7cff0 in ?? ()
#8 0x00002aaaabe7d02c in ?? ()
#9 0x0100000000000001 in ?? ()
#10 0x00000000014388e0 in ?? ()
#11 0x00007fff00000001 in ?? ()
#12 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=@0x7fffffffe050)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#13 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=@0x13f06f8, envp=0x7fffffffe3b0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#14 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe398,
envp=0x7fffffffe3b0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
And a backtrace after this patch:
Program received signal SIGSEGV, Segmentation fault.
0x00002aaaabe7d1a8 in baz ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in baz ()
#1 0x00002aaaabe7d12c in bar ()
#2 0x00002aaaabe7d0aa in foo ()
#3 0x00002aaaabe7d02c in main ()
#4 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=...)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#5 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=..., envp=0x7fffffffe3c0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#6 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe3a8,
envp=0x7fffffffe3c0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
llvm-svn: 82418
2009-09-21 01:52:43 +02:00
|
|
|
uint8_t *EhStart;
|
|
|
|
uint8_t *FrameRegister = DE->EmitDwarfTable(F, *this, FnStart, FnEnd,
|
|
|
|
EhStart);
|
2008-03-07 21:05:43 +01:00
|
|
|
MemMgr->endExceptionTable(F.getFunction(), BufferBegin, CurBufferPtr,
|
|
|
|
FrameRegister);
|
Implement the JIT side of the GDB JIT debugging interface. To enable this
feature, either build the JIT in debug mode to enable it by default or pass
-jit-emit-debug to lli.
Right now, the only debug information that this communicates to GDB is call
frame information, since it's already being generated to support exceptions in
the JIT. Eventually, when DWARF generation isn't tied so tightly to AsmPrinter,
it will be easy to push that information to GDB through this interface.
Here's a step-by-step breakdown of how the feature works:
- The JIT generates the machine code and DWARF call frame info
(.eh_frame/.debug_frame) for a function into memory.
- The JIT copies that info into an in-memory ELF file with a symbol for the
function.
- The JIT creates a code entry pointing to the ELF buffer and adds it to a
linked list hanging off of a global descriptor at a special symbol that GDB
knows about.
- The JIT calls a function marked noinline that GDB knows about and has put an
internal breakpoint in.
- GDB catches the breakpoint and reads the global descriptor to look for new
code.
- When sees there is new code, it reads the ELF from the inferior's memory and
adds it to itself as an object file.
- The JIT continues, and the next time we stop the program, we are able to
produce a proper backtrace.
Consider running the following program through the JIT:
#include <stdio.h>
void baz(short z) {
long w = z + 1;
printf("%d, %x\n", w, *((int*)NULL)); // SEGFAULT here
}
void bar(short y) {
int z = y + 1;
baz(z);
}
void foo(char x) {
short y = x + 1;
bar(y);
}
int main(int argc, char** argv) {
char x = 1;
foo(x);
}
Here is a backtrace before this patch:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x2aaaabdfbd10 (LWP 25476)]
0x00002aaaabe7d1a8 in ?? ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in ?? ()
#1 0x0000000000000003 in ?? ()
#2 0x0000000000000004 in ?? ()
#3 0x00032aaaabe7cfd0 in ?? ()
#4 0x00002aaaabe7d12c in ?? ()
#5 0x00022aaa00000003 in ?? ()
#6 0x00002aaaabe7d0aa in ?? ()
#7 0x01000002abe7cff0 in ?? ()
#8 0x00002aaaabe7d02c in ?? ()
#9 0x0100000000000001 in ?? ()
#10 0x00000000014388e0 in ?? ()
#11 0x00007fff00000001 in ?? ()
#12 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=@0x7fffffffe050)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#13 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=@0x13f06f8, envp=0x7fffffffe3b0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#14 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe398,
envp=0x7fffffffe3b0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
And a backtrace after this patch:
Program received signal SIGSEGV, Segmentation fault.
0x00002aaaabe7d1a8 in baz ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in baz ()
#1 0x00002aaaabe7d12c in bar ()
#2 0x00002aaaabe7d0aa in foo ()
#3 0x00002aaaabe7d02c in main ()
#4 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=...)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#5 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=..., envp=0x7fffffffe3c0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#6 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe3a8,
envp=0x7fffffffe3c0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
llvm-svn: 82418
2009-09-21 01:52:43 +02:00
|
|
|
uint8_t *EhEnd = CurBufferPtr;
|
2008-02-13 19:39:37 +01:00
|
|
|
BufferBegin = SavedBufferBegin;
|
|
|
|
BufferEnd = SavedBufferEnd;
|
|
|
|
CurBufferPtr = SavedCurBufferPtr;
|
|
|
|
|
Implement the JIT side of the GDB JIT debugging interface. To enable this
feature, either build the JIT in debug mode to enable it by default or pass
-jit-emit-debug to lli.
Right now, the only debug information that this communicates to GDB is call
frame information, since it's already being generated to support exceptions in
the JIT. Eventually, when DWARF generation isn't tied so tightly to AsmPrinter,
it will be easy to push that information to GDB through this interface.
Here's a step-by-step breakdown of how the feature works:
- The JIT generates the machine code and DWARF call frame info
(.eh_frame/.debug_frame) for a function into memory.
- The JIT copies that info into an in-memory ELF file with a symbol for the
function.
- The JIT creates a code entry pointing to the ELF buffer and adds it to a
linked list hanging off of a global descriptor at a special symbol that GDB
knows about.
- The JIT calls a function marked noinline that GDB knows about and has put an
internal breakpoint in.
- GDB catches the breakpoint and reads the global descriptor to look for new
code.
- When sees there is new code, it reads the ELF from the inferior's memory and
adds it to itself as an object file.
- The JIT continues, and the next time we stop the program, we are able to
produce a proper backtrace.
Consider running the following program through the JIT:
#include <stdio.h>
void baz(short z) {
long w = z + 1;
printf("%d, %x\n", w, *((int*)NULL)); // SEGFAULT here
}
void bar(short y) {
int z = y + 1;
baz(z);
}
void foo(char x) {
short y = x + 1;
bar(y);
}
int main(int argc, char** argv) {
char x = 1;
foo(x);
}
Here is a backtrace before this patch:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x2aaaabdfbd10 (LWP 25476)]
0x00002aaaabe7d1a8 in ?? ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in ?? ()
#1 0x0000000000000003 in ?? ()
#2 0x0000000000000004 in ?? ()
#3 0x00032aaaabe7cfd0 in ?? ()
#4 0x00002aaaabe7d12c in ?? ()
#5 0x00022aaa00000003 in ?? ()
#6 0x00002aaaabe7d0aa in ?? ()
#7 0x01000002abe7cff0 in ?? ()
#8 0x00002aaaabe7d02c in ?? ()
#9 0x0100000000000001 in ?? ()
#10 0x00000000014388e0 in ?? ()
#11 0x00007fff00000001 in ?? ()
#12 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=@0x7fffffffe050)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#13 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=@0x13f06f8, envp=0x7fffffffe3b0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#14 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe398,
envp=0x7fffffffe3b0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
And a backtrace after this patch:
Program received signal SIGSEGV, Segmentation fault.
0x00002aaaabe7d1a8 in baz ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in baz ()
#1 0x00002aaaabe7d12c in bar ()
#2 0x00002aaaabe7d0aa in foo ()
#3 0x00002aaaabe7d02c in main ()
#4 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=...)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#5 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=..., envp=0x7fffffffe3c0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#6 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe3a8,
envp=0x7fffffffe3c0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
llvm-svn: 82418
2009-09-21 01:52:43 +02:00
|
|
|
if (DwarfExceptionHandling) {
|
|
|
|
TheJIT->RegisterTable(FrameRegister);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JITEmitDebugInfo) {
|
|
|
|
DebugInfo I;
|
|
|
|
I.FnStart = FnStart;
|
|
|
|
I.FnEnd = FnEnd;
|
|
|
|
I.EhStart = EhStart;
|
|
|
|
I.EhEnd = EhEnd;
|
|
|
|
DR->RegisterFunction(F.getFunction(), I);
|
|
|
|
}
|
2008-02-13 19:39:37 +01:00
|
|
|
}
|
2008-09-02 10:14:01 +02:00
|
|
|
|
|
|
|
if (MMI)
|
|
|
|
MMI->EndFunction();
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2006-05-02 20:27:26 +02:00
|
|
|
return false;
|
2002-12-24 01:01:05 +01:00
|
|
|
}
|
|
|
|
|
2009-07-23 23:46:56 +02:00
|
|
|
void JITEmitter::retryWithMoreMemory(MachineFunction &F) {
|
2009-08-23 08:35:02 +02:00
|
|
|
DEBUG(errs() << "JIT: Ran out of space for native code. Reattempting.\n");
|
2009-07-23 23:46:56 +02:00
|
|
|
Relocations.clear(); // Clear the old relocations or we'll reapply them.
|
|
|
|
ConstPoolAddresses.clear();
|
|
|
|
++NumRetries;
|
|
|
|
deallocateMemForFunction(F.getFunction());
|
|
|
|
// Try again with at least twice as much free space.
|
|
|
|
SizeEstimate = (uintptr_t)(2 * (BufferEnd - BufferBegin));
|
|
|
|
}
|
|
|
|
|
2009-03-05 07:34:37 +01:00
|
|
|
/// deallocateMemForFunction - Deallocate all memory for the specified
|
|
|
|
/// function body. Also drop any references the function has to stubs.
|
2009-10-27 01:03:05 +01:00
|
|
|
/// May be called while the Function is being destroyed inside ~Value().
|
2009-07-23 23:46:56 +02:00
|
|
|
void JITEmitter::deallocateMemForFunction(const Function *F) {
|
2009-10-27 01:03:05 +01:00
|
|
|
ValueMap<const Function *, EmittedCode, EmittedFunctionConfig>::iterator
|
|
|
|
Emitted = EmittedFunctions.find(F);
|
2009-10-20 20:13:21 +02:00
|
|
|
if (Emitted != EmittedFunctions.end()) {
|
|
|
|
MemMgr->deallocateFunctionBody(Emitted->second.FunctionBody);
|
|
|
|
MemMgr->deallocateExceptionTable(Emitted->second.ExceptionTable);
|
2009-10-27 01:03:05 +01:00
|
|
|
TheJIT->NotifyFreeingMachineCode(Emitted->second.Code);
|
|
|
|
|
2009-10-20 20:13:21 +02:00
|
|
|
EmittedFunctions.erase(Emitted);
|
|
|
|
}
|
2009-03-05 07:34:37 +01:00
|
|
|
|
Implement the JIT side of the GDB JIT debugging interface. To enable this
feature, either build the JIT in debug mode to enable it by default or pass
-jit-emit-debug to lli.
Right now, the only debug information that this communicates to GDB is call
frame information, since it's already being generated to support exceptions in
the JIT. Eventually, when DWARF generation isn't tied so tightly to AsmPrinter,
it will be easy to push that information to GDB through this interface.
Here's a step-by-step breakdown of how the feature works:
- The JIT generates the machine code and DWARF call frame info
(.eh_frame/.debug_frame) for a function into memory.
- The JIT copies that info into an in-memory ELF file with a symbol for the
function.
- The JIT creates a code entry pointing to the ELF buffer and adds it to a
linked list hanging off of a global descriptor at a special symbol that GDB
knows about.
- The JIT calls a function marked noinline that GDB knows about and has put an
internal breakpoint in.
- GDB catches the breakpoint and reads the global descriptor to look for new
code.
- When sees there is new code, it reads the ELF from the inferior's memory and
adds it to itself as an object file.
- The JIT continues, and the next time we stop the program, we are able to
produce a proper backtrace.
Consider running the following program through the JIT:
#include <stdio.h>
void baz(short z) {
long w = z + 1;
printf("%d, %x\n", w, *((int*)NULL)); // SEGFAULT here
}
void bar(short y) {
int z = y + 1;
baz(z);
}
void foo(char x) {
short y = x + 1;
bar(y);
}
int main(int argc, char** argv) {
char x = 1;
foo(x);
}
Here is a backtrace before this patch:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x2aaaabdfbd10 (LWP 25476)]
0x00002aaaabe7d1a8 in ?? ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in ?? ()
#1 0x0000000000000003 in ?? ()
#2 0x0000000000000004 in ?? ()
#3 0x00032aaaabe7cfd0 in ?? ()
#4 0x00002aaaabe7d12c in ?? ()
#5 0x00022aaa00000003 in ?? ()
#6 0x00002aaaabe7d0aa in ?? ()
#7 0x01000002abe7cff0 in ?? ()
#8 0x00002aaaabe7d02c in ?? ()
#9 0x0100000000000001 in ?? ()
#10 0x00000000014388e0 in ?? ()
#11 0x00007fff00000001 in ?? ()
#12 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=@0x7fffffffe050)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#13 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=@0x13f06f8, envp=0x7fffffffe3b0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#14 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe398,
envp=0x7fffffffe3b0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
And a backtrace after this patch:
Program received signal SIGSEGV, Segmentation fault.
0x00002aaaabe7d1a8 in baz ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in baz ()
#1 0x00002aaaabe7d12c in bar ()
#2 0x00002aaaabe7d0aa in foo ()
#3 0x00002aaaabe7d02c in main ()
#4 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=...)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#5 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=..., envp=0x7fffffffe3c0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#6 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe3a8,
envp=0x7fffffffe3c0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
llvm-svn: 82418
2009-09-21 01:52:43 +02:00
|
|
|
// TODO: Do we need to unregister exception handling information from libgcc
|
|
|
|
// here?
|
|
|
|
|
|
|
|
if (JITEmitDebugInfo) {
|
|
|
|
DR->UnregisterFunction(F);
|
|
|
|
}
|
|
|
|
|
2009-03-05 07:34:37 +01:00
|
|
|
// If the function did not reference any stubs, return.
|
|
|
|
if (CurFnStubUses.find(F) == CurFnStubUses.end())
|
|
|
|
return;
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-03-05 07:34:37 +01:00
|
|
|
// For each referenced stub, erase the reference to this function, and then
|
|
|
|
// erase the list of referenced stubs.
|
|
|
|
SmallVectorImpl<void *> &StubList = CurFnStubUses[F];
|
|
|
|
for (unsigned i = 0, e = StubList.size(); i != e; ++i) {
|
|
|
|
void *Stub = StubList[i];
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-03-11 08:03:43 +01:00
|
|
|
// If we already invalidated this stub for this function, continue.
|
|
|
|
if (StubFnRefs.count(Stub) == 0)
|
|
|
|
continue;
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-03-05 07:34:37 +01:00
|
|
|
SmallPtrSet<const Function *, 1> &FnRefs = StubFnRefs[Stub];
|
|
|
|
FnRefs.erase(F);
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-03-05 07:34:37 +01:00
|
|
|
// If this function was the last reference to the stub, invalidate the stub
|
|
|
|
// in the JITResolver. Were there a memory manager deallocateStub routine,
|
|
|
|
// we could call that at this point too.
|
|
|
|
if (FnRefs.empty()) {
|
2009-08-23 08:35:02 +02:00
|
|
|
DEBUG(errs() << "\nJIT: Invalidated Stub at [" << Stub << "]\n");
|
2009-03-11 08:03:43 +01:00
|
|
|
StubFnRefs.erase(Stub);
|
|
|
|
|
|
|
|
// Invalidate the stub. If it is a GV stub, update the JIT's global
|
2009-11-09 23:34:19 +01:00
|
|
|
// mapping for that GV to zero.
|
2009-03-07 07:41:19 +01:00
|
|
|
GlobalValue *GV = Resolver.invalidateStub(Stub);
|
2009-03-11 08:03:43 +01:00
|
|
|
if (GV) {
|
|
|
|
TheJIT->updateGlobalMapping(GV, 0);
|
|
|
|
}
|
2009-03-05 07:34:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CurFnStubUses.erase(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-10 03:32:19 +01:00
|
|
|
void* JITEmitter::allocateSpace(uintptr_t Size, unsigned Alignment) {
|
2008-10-21 13:42:16 +02:00
|
|
|
if (BufferBegin)
|
2009-05-30 22:51:52 +02:00
|
|
|
return JITCodeEmitter::allocateSpace(Size, Alignment);
|
2008-10-21 13:42:16 +02:00
|
|
|
|
|
|
|
// create a new memory block if there is no active one.
|
|
|
|
// care must be taken so that BufferBegin is invalidated when a
|
|
|
|
// block is trimmed
|
|
|
|
BufferBegin = CurBufferPtr = MemMgr->allocateSpace(Size, Alignment);
|
|
|
|
BufferEnd = BufferBegin+Size;
|
|
|
|
return CurBufferPtr;
|
|
|
|
}
|
|
|
|
|
2009-07-08 23:59:57 +02:00
|
|
|
void* JITEmitter::allocateGlobal(uintptr_t Size, unsigned Alignment) {
|
|
|
|
// Delegate this call through the memory manager.
|
|
|
|
return MemMgr->allocateGlobal(Size, Alignment);
|
|
|
|
}
|
|
|
|
|
2004-11-22 23:00:25 +01:00
|
|
|
void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
|
2008-11-07 10:02:17 +01:00
|
|
|
if (TheJIT->getJITInfo().hasCustomConstantPool())
|
2008-10-31 00:44:39 +01:00
|
|
|
return;
|
2008-11-07 10:02:17 +01:00
|
|
|
|
2006-02-09 05:22:52 +01:00
|
|
|
const std::vector<MachineConstantPoolEntry> &Constants = MCP->getConstants();
|
2003-11-30 05:23:21 +01:00
|
|
|
if (Constants.empty()) return;
|
|
|
|
|
2009-03-13 08:51:59 +01:00
|
|
|
unsigned Size = GetConstantPoolSizeInBytes(MCP, TheJIT->getTargetData());
|
|
|
|
unsigned Align = MCP->getConstantPoolAlignment();
|
2008-04-12 02:22:01 +02:00
|
|
|
ConstantPoolBase = allocateSpace(Size, Align);
|
2006-02-09 05:49:59 +01:00
|
|
|
ConstantPool = MCP;
|
2006-05-03 01:22:24 +02:00
|
|
|
|
|
|
|
if (ConstantPoolBase == 0) return; // Buffer overflow.
|
|
|
|
|
2009-08-23 08:35:02 +02:00
|
|
|
DEBUG(errs() << "JIT: Emitted constant pool at [" << ConstantPoolBase
|
|
|
|
<< "] (size: " << Size << ", alignment: " << Align << ")\n");
|
2008-04-12 02:22:01 +02:00
|
|
|
|
2006-02-09 05:49:59 +01:00
|
|
|
// Initialize the memory for all of the constant pool entries.
|
2009-03-13 08:51:59 +01:00
|
|
|
unsigned Offset = 0;
|
2006-02-09 05:46:04 +01:00
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
|
2009-03-13 08:51:59 +01:00
|
|
|
MachineConstantPoolEntry CPE = Constants[i];
|
|
|
|
unsigned AlignMask = CPE.getAlignment() - 1;
|
|
|
|
Offset = (Offset + AlignMask) & ~AlignMask;
|
|
|
|
|
|
|
|
uintptr_t CAddr = (uintptr_t)ConstantPoolBase + Offset;
|
|
|
|
ConstPoolAddresses.push_back(CAddr);
|
|
|
|
if (CPE.isMachineConstantPoolEntry()) {
|
2006-09-12 22:59:59 +02:00
|
|
|
// FIXME: add support to lower machine constant pool values into bytes!
|
2009-07-11 15:10:19 +02:00
|
|
|
llvm_report_error("Initialize memory with machine specific constant pool"
|
|
|
|
"entry has not been implemented!");
|
2006-09-12 22:59:59 +02:00
|
|
|
}
|
2009-03-13 08:51:59 +01:00
|
|
|
TheJIT->InitializeMemory(CPE.Val.ConstVal, (void*)CAddr);
|
2009-08-23 08:35:02 +02:00
|
|
|
DEBUG(errs() << "JIT: CP" << i << " at [0x";
|
|
|
|
errs().write_hex(CAddr) << "]\n");
|
2009-03-13 08:51:59 +01:00
|
|
|
|
|
|
|
const Type *Ty = CPE.Val.ConstVal->getType();
|
2009-05-09 09:06:46 +02:00
|
|
|
Offset += TheJIT->getTargetData()->getTypeAllocSize(Ty);
|
2003-01-13 02:00:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-22 20:53:45 +02:00
|
|
|
void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) {
|
2008-11-07 10:02:17 +01:00
|
|
|
if (TheJIT->getJITInfo().hasCustomJumpTables())
|
|
|
|
return;
|
|
|
|
|
2006-04-22 20:53:45 +02:00
|
|
|
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
|
|
|
|
if (JT.empty()) return;
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2006-05-03 01:22:24 +02:00
|
|
|
unsigned NumEntries = 0;
|
2006-04-22 20:53:45 +02:00
|
|
|
for (unsigned i = 0, e = JT.size(); i != e; ++i)
|
2006-05-03 01:22:24 +02:00
|
|
|
NumEntries += JT[i].MBBs.size();
|
|
|
|
|
|
|
|
unsigned EntrySize = MJTI->getEntrySize();
|
|
|
|
|
2006-04-22 20:53:45 +02:00
|
|
|
// Just allocate space for all the jump tables now. We will fix up the actual
|
|
|
|
// MBB entries in the tables after we emit the code for each block, since then
|
|
|
|
// we will know the final locations of the MBBs in memory.
|
|
|
|
JumpTable = MJTI;
|
2006-05-03 01:22:24 +02:00
|
|
|
JumpTableBase = allocateSpace(NumEntries * EntrySize, MJTI->getAlignment());
|
2006-04-22 20:53:45 +02:00
|
|
|
}
|
|
|
|
|
2006-12-14 23:53:42 +01:00
|
|
|
void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI) {
|
2008-11-07 10:02:17 +01:00
|
|
|
if (TheJIT->getJITInfo().hasCustomJumpTables())
|
|
|
|
return;
|
|
|
|
|
2006-04-22 20:53:45 +02:00
|
|
|
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
|
2006-05-03 01:22:24 +02:00
|
|
|
if (JT.empty() || JumpTableBase == 0) return;
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2006-12-14 23:53:42 +01:00
|
|
|
if (TargetMachine::getRelocationModel() == Reloc::PIC_) {
|
2006-12-14 20:17:33 +01:00
|
|
|
assert(MJTI->getEntrySize() == 4 && "Cross JIT'ing?");
|
|
|
|
// For each jump table, place the offset from the beginning of the table
|
|
|
|
// to the target address.
|
|
|
|
int *SlotPtr = (int*)JumpTableBase;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = JT.size(); i != e; ++i) {
|
|
|
|
const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
|
|
|
|
// Store the offset of the basic block for this jump table slot in the
|
|
|
|
// memory we allocated for the jump table in 'initJumpTableInfo'
|
2008-12-10 03:32:19 +01:00
|
|
|
uintptr_t Base = (uintptr_t)SlotPtr;
|
2008-01-05 03:26:58 +01:00
|
|
|
for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
|
2008-12-10 03:32:19 +01:00
|
|
|
uintptr_t MBBAddr = getMachineBasicBlockAddress(MBBs[mi]);
|
2008-01-05 03:26:58 +01:00
|
|
|
*SlotPtr++ = TheJIT->getJITInfo().getPICJumpTableEntry(MBBAddr, Base);
|
|
|
|
}
|
2006-12-14 20:17:33 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(MJTI->getEntrySize() == sizeof(void*) && "Cross JIT'ing?");
|
2009-11-12 04:12:18 +01:00
|
|
|
|
|
|
|
// For each jump table, map each target in the jump table to the address of
|
2006-12-14 20:17:33 +01:00
|
|
|
// an emitted MachineBasicBlock.
|
|
|
|
intptr_t *SlotPtr = (intptr_t*)JumpTableBase;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = JT.size(); i != e; ++i) {
|
|
|
|
const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
|
|
|
|
// Store the address of the basic block for this jump table slot in the
|
|
|
|
// memory we allocated for the jump table in 'initJumpTableInfo'
|
|
|
|
for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi)
|
|
|
|
*SlotPtr++ = getMachineBasicBlockAddress(MBBs[mi]);
|
|
|
|
}
|
2006-04-22 20:53:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-08 09:02:53 +01:00
|
|
|
void JITEmitter::startGVStub(const GlobalValue* GV, unsigned StubSize,
|
|
|
|
unsigned Alignment) {
|
2006-05-02 20:27:26 +02:00
|
|
|
SavedBufferBegin = BufferBegin;
|
|
|
|
SavedBufferEnd = BufferEnd;
|
|
|
|
SavedCurBufferPtr = CurBufferPtr;
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2008-11-08 09:02:53 +01:00
|
|
|
BufferBegin = CurBufferPtr = MemMgr->allocateStub(GV, StubSize, Alignment);
|
2006-05-02 20:27:26 +02:00
|
|
|
BufferEnd = BufferBegin+StubSize+1;
|
2003-05-09 05:30:07 +02:00
|
|
|
}
|
|
|
|
|
2009-02-18 09:31:02 +01:00
|
|
|
void JITEmitter::startGVStub(const GlobalValue* GV, void *Buffer,
|
|
|
|
unsigned StubSize) {
|
|
|
|
SavedBufferBegin = BufferBegin;
|
|
|
|
SavedBufferEnd = BufferEnd;
|
|
|
|
SavedCurBufferPtr = CurBufferPtr;
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2009-06-04 02:15:51 +02:00
|
|
|
BufferBegin = CurBufferPtr = (uint8_t *)Buffer;
|
2009-02-18 09:31:02 +01:00
|
|
|
BufferEnd = BufferBegin+StubSize+1;
|
|
|
|
}
|
|
|
|
|
2008-11-08 09:02:53 +01:00
|
|
|
void *JITEmitter::finishGVStub(const GlobalValue* GV) {
|
2006-05-02 20:27:26 +02:00
|
|
|
NumBytes += getCurrentPCOffset();
|
|
|
|
std::swap(SavedBufferBegin, BufferBegin);
|
|
|
|
BufferEnd = SavedBufferEnd;
|
|
|
|
CurBufferPtr = SavedCurBufferPtr;
|
|
|
|
return SavedBufferBegin;
|
2003-06-02 01:24:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
|
|
|
|
// in the constant pool that was last emitted with the 'emitConstantPool'
|
|
|
|
// method.
|
|
|
|
//
|
2008-12-10 03:32:19 +01:00
|
|
|
uintptr_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) const {
|
2006-02-09 05:49:59 +01:00
|
|
|
assert(ConstantNum < ConstantPool->getConstants().size() &&
|
2005-04-22 06:08:30 +02:00
|
|
|
"Invalid ConstantPoolIndex!");
|
2009-03-13 08:51:59 +01:00
|
|
|
return ConstPoolAddresses[ConstantNum];
|
2003-06-02 01:24:36 +02:00
|
|
|
}
|
|
|
|
|
2006-04-22 20:53:45 +02:00
|
|
|
// getJumpTableEntryAddress - Return the address of the JumpTable with index
|
|
|
|
// 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo'
|
|
|
|
//
|
2008-12-10 03:32:19 +01:00
|
|
|
uintptr_t JITEmitter::getJumpTableEntryAddress(unsigned Index) const {
|
2006-04-22 20:53:45 +02:00
|
|
|
const std::vector<MachineJumpTableEntry> &JT = JumpTable->getJumpTables();
|
|
|
|
assert(Index < JT.size() && "Invalid jump table index!");
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2006-04-22 20:53:45 +02:00
|
|
|
unsigned Offset = 0;
|
|
|
|
unsigned EntrySize = JumpTable->getEntrySize();
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2006-04-22 20:53:45 +02:00
|
|
|
for (unsigned i = 0; i < Index; ++i)
|
2006-12-14 20:17:33 +01:00
|
|
|
Offset += JT[i].MBBs.size();
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2006-12-14 20:17:33 +01:00
|
|
|
Offset *= EntrySize;
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2008-12-10 03:32:19 +01:00
|
|
|
return (uintptr_t)((char *)JumpTableBase + Offset);
|
2006-04-22 20:53:45 +02:00
|
|
|
}
|
|
|
|
|
2009-10-27 01:03:05 +01:00
|
|
|
void JITEmitter::EmittedFunctionConfig::onDelete(
|
|
|
|
JITEmitter *Emitter, const Function *F) {
|
|
|
|
Emitter->deallocateMemForFunction(F);
|
|
|
|
}
|
|
|
|
void JITEmitter::EmittedFunctionConfig::onRAUW(
|
|
|
|
JITEmitter *, const Function*, const Function*) {
|
|
|
|
llvm_unreachable("The JIT doesn't know how to handle a"
|
|
|
|
" RAUW on a value it has emitted.");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-12 01:08:08 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Public interface to this file
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
Implement the JIT side of the GDB JIT debugging interface. To enable this
feature, either build the JIT in debug mode to enable it by default or pass
-jit-emit-debug to lli.
Right now, the only debug information that this communicates to GDB is call
frame information, since it's already being generated to support exceptions in
the JIT. Eventually, when DWARF generation isn't tied so tightly to AsmPrinter,
it will be easy to push that information to GDB through this interface.
Here's a step-by-step breakdown of how the feature works:
- The JIT generates the machine code and DWARF call frame info
(.eh_frame/.debug_frame) for a function into memory.
- The JIT copies that info into an in-memory ELF file with a symbol for the
function.
- The JIT creates a code entry pointing to the ELF buffer and adds it to a
linked list hanging off of a global descriptor at a special symbol that GDB
knows about.
- The JIT calls a function marked noinline that GDB knows about and has put an
internal breakpoint in.
- GDB catches the breakpoint and reads the global descriptor to look for new
code.
- When sees there is new code, it reads the ELF from the inferior's memory and
adds it to itself as an object file.
- The JIT continues, and the next time we stop the program, we are able to
produce a proper backtrace.
Consider running the following program through the JIT:
#include <stdio.h>
void baz(short z) {
long w = z + 1;
printf("%d, %x\n", w, *((int*)NULL)); // SEGFAULT here
}
void bar(short y) {
int z = y + 1;
baz(z);
}
void foo(char x) {
short y = x + 1;
bar(y);
}
int main(int argc, char** argv) {
char x = 1;
foo(x);
}
Here is a backtrace before this patch:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x2aaaabdfbd10 (LWP 25476)]
0x00002aaaabe7d1a8 in ?? ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in ?? ()
#1 0x0000000000000003 in ?? ()
#2 0x0000000000000004 in ?? ()
#3 0x00032aaaabe7cfd0 in ?? ()
#4 0x00002aaaabe7d12c in ?? ()
#5 0x00022aaa00000003 in ?? ()
#6 0x00002aaaabe7d0aa in ?? ()
#7 0x01000002abe7cff0 in ?? ()
#8 0x00002aaaabe7d02c in ?? ()
#9 0x0100000000000001 in ?? ()
#10 0x00000000014388e0 in ?? ()
#11 0x00007fff00000001 in ?? ()
#12 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=@0x7fffffffe050)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#13 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=@0x13f06f8, envp=0x7fffffffe3b0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#14 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe398,
envp=0x7fffffffe3b0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
And a backtrace after this patch:
Program received signal SIGSEGV, Segmentation fault.
0x00002aaaabe7d1a8 in baz ()
(gdb) bt
#0 0x00002aaaabe7d1a8 in baz ()
#1 0x00002aaaabe7d12c in bar ()
#2 0x00002aaaabe7d0aa in foo ()
#3 0x00002aaaabe7d02c in main ()
#4 0x0000000000b870a2 in llvm::JIT::runFunction (this=0x1405b70,
F=0x14024e0, ArgValues=...)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/JIT/JIT.cpp:395
#5 0x0000000000baa4c5 in llvm::ExecutionEngine::runFunctionAsMain
(this=0x1405b70, Fn=0x14024e0, argv=..., envp=0x7fffffffe3c0)
at /home/rnk/llvm-gdb/lib/ExecutionEngine/ExecutionEngine.cpp:377
#6 0x00000000007ebd52 in main (argc=2, argv=0x7fffffffe3a8,
envp=0x7fffffffe3c0) at /home/rnk/llvm-gdb/tools/lli/lli.cpp:208
llvm-svn: 82418
2009-09-21 01:52:43 +02:00
|
|
|
JITCodeEmitter *JIT::createEmitter(JIT &jit, JITMemoryManager *JMM,
|
|
|
|
TargetMachine &tm) {
|
|
|
|
return new JITEmitter(jit, JMM, tm);
|
2006-05-12 01:08:08 +02:00
|
|
|
}
|
|
|
|
|
2003-07-28 21:09:06 +02:00
|
|
|
// getPointerToNamedFunction - This function is used as a global wrapper to
|
2003-12-20 02:46:27 +01:00
|
|
|
// JIT::getPointerToNamedFunction for the purpose of resolving symbols when
|
2003-07-28 21:09:06 +02:00
|
|
|
// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
|
|
|
|
// need to resolve function(s) that are being mis-codegenerated, so we need to
|
|
|
|
// resolve their addresses at runtime, and this is the way to do it.
|
|
|
|
extern "C" {
|
|
|
|
void *getPointerToNamedFunction(const char *Name) {
|
2006-08-16 03:24:12 +02:00
|
|
|
if (Function *F = TheJIT->FindFunctionNamed(Name))
|
2003-12-20 02:46:27 +01:00
|
|
|
return TheJIT->getPointerToFunction(F);
|
|
|
|
return TheJIT->getPointerToNamedFunction(Name);
|
2003-07-28 21:09:06 +02:00
|
|
|
}
|
|
|
|
}
|
2006-05-12 01:08:08 +02:00
|
|
|
|
|
|
|
// getPointerToFunctionOrStub - If the specified function has been
|
|
|
|
// code-gen'd, return a pointer to the function. If not, compile it, or use
|
|
|
|
// a stub to implement lazy compilation if available.
|
|
|
|
//
|
|
|
|
void *JIT::getPointerToFunctionOrStub(Function *F) {
|
|
|
|
// If we have already code generated the function, just return the address.
|
|
|
|
if (void *Addr = getPointerToGlobalIfAvailable(F))
|
|
|
|
return Addr;
|
2009-11-12 04:12:18 +01:00
|
|
|
|
2007-02-24 03:57:03 +01:00
|
|
|
// Get a stub if the target supports it.
|
2009-05-30 22:51:52 +02:00
|
|
|
assert(isa<JITEmitter>(JCE) && "Unexpected MCE?");
|
2008-08-20 02:28:12 +02:00
|
|
|
JITEmitter *JE = cast<JITEmitter>(getCodeEmitter());
|
2007-02-24 03:57:03 +01:00
|
|
|
return JE->getJITResolver().getFunctionStub(F);
|
2006-05-12 01:08:08 +02:00
|
|
|
}
|
|
|
|
|
2009-02-18 09:31:02 +01:00
|
|
|
void JIT::updateFunctionStub(Function *F) {
|
|
|
|
// Get the empty stub we generated earlier.
|
2009-05-30 22:51:52 +02:00
|
|
|
assert(isa<JITEmitter>(JCE) && "Unexpected MCE?");
|
2009-02-18 09:31:02 +01:00
|
|
|
JITEmitter *JE = cast<JITEmitter>(getCodeEmitter());
|
|
|
|
void *Stub = JE->getJITResolver().getFunctionStub(F);
|
|
|
|
|
|
|
|
// Tell the target jit info to rewrite the stub at the specified address,
|
|
|
|
// rather than creating a new one.
|
|
|
|
void *Addr = getPointerToGlobalIfAvailable(F);
|
|
|
|
getJITInfo().emitFunctionStubAtAddr(F, Addr, Stub, *getCodeEmitter());
|
|
|
|
}
|
|
|
|
|
2006-05-12 01:08:08 +02:00
|
|
|
/// freeMachineCodeForFunction - release machine code memory for given Function.
|
|
|
|
///
|
|
|
|
void JIT::freeMachineCodeForFunction(Function *F) {
|
|
|
|
// Delete translation for this from the ExecutionEngine, so it will get
|
|
|
|
// retranslated next time it is used.
|
2009-10-27 01:03:05 +01:00
|
|
|
updateGlobalMapping(F, 0);
|
2006-05-12 01:08:08 +02:00
|
|
|
|
|
|
|
// Free the actual memory for the function body and related stuff.
|
2009-05-30 22:51:52 +02:00
|
|
|
assert(isa<JITEmitter>(JCE) && "Unexpected MCE?");
|
|
|
|
cast<JITEmitter>(JCE)->deallocateMemForFunction(F);
|
2006-05-12 01:08:08 +02:00
|
|
|
}
|