2003-12-20 09:46:27 +08:00
|
|
|
//===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
|
2005-04-22 06:55:34 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 06:55:34 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-24 08:01:05 +08:00
|
|
|
//
|
2003-12-20 09:46:27 +08:00
|
|
|
// This tool implements a just-in-time compiler for LLVM, allowing direct
|
2007-07-06 01:07:56 +08:00
|
|
|
// execution of LLVM bitcode in an efficient manner.
|
2002-12-24 08:01:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-12-20 09:46:27 +08:00
|
|
|
#include "JIT.h"
|
2010-02-11 09:07:39 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2009-05-31 04:51:52 +08:00
|
|
|
#include "llvm/CodeGen/JITCodeEmitter.h"
|
2009-05-19 05:06:40 +08:00
|
|
|
#include "llvm/CodeGen/MachineCodeInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2009-06-25 10:04:04 +08:00
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
|
|
|
#include "llvm/ExecutionEngine/JITEventListener.h"
|
2012-03-29 05:46:36 +08:00
|
|
|
#include "llvm/ExecutionEngine/JITMemoryManager.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2009-03-04 04:10:23 +08:00
|
|
|
#include "llvm/Support/Dwarf.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/DynamicLibrary.h"
|
2009-07-08 01:32:34 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-02-11 09:07:39 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2009-03-04 04:10:23 +08:00
|
|
|
#include "llvm/Support/MutexGuard.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetJITInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2007-07-31 04:02:02 +08:00
|
|
|
|
2003-12-08 16:06:28 +08:00
|
|
|
using namespace llvm;
|
2003-05-28 05:40:39 +08:00
|
|
|
|
2011-03-16 04:25:54 +08:00
|
|
|
#ifdef __APPLE__
|
2007-07-31 04:02:02 +08:00
|
|
|
// Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
|
|
|
|
// of atexit). It passes the address of linker generated symbol __dso_handle
|
|
|
|
// to the function.
|
|
|
|
// This configuration change happened at version 5330.
|
|
|
|
# include <AvailabilityMacros.h>
|
|
|
|
# if defined(MAC_OS_X_VERSION_10_4) && \
|
|
|
|
((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
|
|
|
|
(MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
|
|
|
|
__APPLE_CC__ >= 5330))
|
|
|
|
# ifndef HAVE___DSO_HANDLE
|
|
|
|
# define HAVE___DSO_HANDLE 1
|
|
|
|
# endif
|
|
|
|
# endif
|
2006-07-22 07:06:20 +08:00
|
|
|
#endif
|
2007-07-31 04:02:02 +08:00
|
|
|
|
|
|
|
#if HAVE___DSO_HANDLE
|
|
|
|
extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
|
2006-07-23 00:59:38 +08:00
|
|
|
#endif
|
2006-07-22 07:06:20 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
namespace {
|
|
|
|
|
2006-03-22 14:07:50 +08:00
|
|
|
static struct RegisterJIT {
|
|
|
|
RegisterJIT() { JIT::Register(); }
|
|
|
|
} JITRegistrator;
|
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
}
|
|
|
|
|
2009-06-25 05:09:18 +08:00
|
|
|
extern "C" void LLVMLinkInJIT() {
|
2006-03-24 10:53:49 +08:00
|
|
|
}
|
|
|
|
|
2010-11-13 08:55:45 +08:00
|
|
|
// Determine whether we can register EH tables.
|
|
|
|
#if (defined(__GNUC__) && !defined(__ARM_EABI__) && \
|
|
|
|
!defined(__USING_SJLJ_EXCEPTIONS__))
|
|
|
|
#define HAVE_EHTABLE_SUPPORT 1
|
|
|
|
#else
|
|
|
|
#define HAVE_EHTABLE_SUPPORT 0
|
|
|
|
#endif
|
2008-08-29 06:34:49 +08:00
|
|
|
|
2010-11-13 08:55:45 +08:00
|
|
|
#if HAVE_EHTABLE_SUPPORT
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2008-08-29 06:34:49 +08:00
|
|
|
// libgcc defines the __register_frame function to dynamically register new
|
|
|
|
// dwarf frames for exception handling. This functionality is not portable
|
|
|
|
// across compilers and is only provided by GCC. We use the __register_frame
|
|
|
|
// function here so that code generated by the JIT cooperates with the unwinding
|
|
|
|
// runtime of libgcc. When JITting with exception handling enable, LLVM
|
|
|
|
// generates dwarf frames and registers it to libgcc with __register_frame.
|
|
|
|
//
|
|
|
|
// The __register_frame function works with Linux.
|
|
|
|
//
|
|
|
|
// Unfortunately, this functionality seems to be in libgcc after the unwinding
|
|
|
|
// library of libgcc for darwin was written. The code for darwin overwrites the
|
|
|
|
// value updated by __register_frame with a value fetched with "keymgr".
|
|
|
|
// "keymgr" is an obsolete functionality, which should be rewritten some day.
|
|
|
|
// In the meantime, since "keymgr" is on all libgccs shipped with apple-gcc, we
|
|
|
|
// need a workaround in LLVM which uses the "keymgr" to dynamically modify the
|
|
|
|
// values of an opaque key, used by libgcc to find dwarf tables.
|
|
|
|
|
2008-03-22 16:53:09 +08:00
|
|
|
extern "C" void __register_frame(void*);
|
2010-10-21 16:57:29 +08:00
|
|
|
extern "C" void __deregister_frame(void*);
|
2008-08-29 06:34:49 +08:00
|
|
|
|
2009-04-15 06:31:59 +08:00
|
|
|
#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED <= 1050
|
|
|
|
# define USE_KEYMGR 1
|
|
|
|
#else
|
|
|
|
# define USE_KEYMGR 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if USE_KEYMGR
|
2008-08-29 06:34:49 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// LibgccObject - This is the structure defined in libgcc. There is no #include
|
|
|
|
// provided for this structure, so we also define it here. libgcc calls it
|
|
|
|
// "struct object". The structure is undocumented in libgcc.
|
|
|
|
struct LibgccObject {
|
|
|
|
void *unused1;
|
|
|
|
void *unused2;
|
|
|
|
void *unused3;
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2008-08-29 06:34:49 +08:00
|
|
|
/// frame - Pointer to the exception table.
|
|
|
|
void *frame;
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2008-08-29 06:34:49 +08:00
|
|
|
/// encoding - The encoding of the object?
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
unsigned long sorted : 1;
|
|
|
|
unsigned long from_array : 1;
|
|
|
|
unsigned long mixed_encoding : 1;
|
|
|
|
unsigned long encoding : 8;
|
2011-03-16 04:25:54 +08:00
|
|
|
unsigned long count : 21;
|
2008-08-29 06:34:49 +08:00
|
|
|
} b;
|
|
|
|
size_t i;
|
|
|
|
} encoding;
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2008-08-29 06:34:49 +08:00
|
|
|
/// fde_end - libgcc defines this field only if some macro is defined. We
|
|
|
|
/// include this field even if it may not there, to make libgcc happy.
|
|
|
|
char *fde_end;
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2008-08-29 06:34:49 +08:00
|
|
|
/// next - At least we know it's a chained list!
|
|
|
|
struct LibgccObject *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
// "kemgr" stuff. Apparently, all frame tables are stored there.
|
|
|
|
extern "C" void _keymgr_set_and_unlock_processwide_ptr(int, void *);
|
|
|
|
extern "C" void *_keymgr_get_and_lock_processwide_ptr(int);
|
|
|
|
#define KEYMGR_GCC3_DW2_OBJ_LIST 302 /* Dwarf2 object list */
|
|
|
|
|
|
|
|
/// LibgccObjectInfo - libgcc defines this struct as km_object_info. It
|
|
|
|
/// probably contains all dwarf tables that are loaded.
|
|
|
|
struct LibgccObjectInfo {
|
|
|
|
|
|
|
|
/// seenObjects - LibgccObjects already parsed by the unwinding runtime.
|
|
|
|
///
|
|
|
|
struct LibgccObject* seenObjects;
|
|
|
|
|
|
|
|
/// unseenObjects - LibgccObjects not parsed yet by the unwinding runtime.
|
|
|
|
///
|
|
|
|
struct LibgccObject* unseenObjects;
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2008-08-29 06:34:49 +08:00
|
|
|
unsigned unused[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
/// darwin_register_frame - Since __register_frame does not work with darwin's
|
|
|
|
/// libgcc,we provide our own function, which "tricks" libgcc by modifying the
|
|
|
|
/// "Dwarf2 object list" key.
|
|
|
|
void DarwinRegisterFrame(void* FrameBegin) {
|
|
|
|
// Get the key.
|
2009-03-04 04:10:23 +08:00
|
|
|
LibgccObjectInfo* LOI = (struct LibgccObjectInfo*)
|
2008-08-29 06:34:49 +08:00
|
|
|
_keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST);
|
2009-03-04 04:10:23 +08:00
|
|
|
assert(LOI && "This should be preallocated by the runtime");
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2008-08-29 06:34:49 +08:00
|
|
|
// Allocate a new LibgccObject to represent this frame. Deallocation of this
|
|
|
|
// object may be impossible: since darwin code in libgcc was written after
|
|
|
|
// the ability to dynamically register frames, things may crash if we
|
|
|
|
// deallocate it.
|
|
|
|
struct LibgccObject* ob = (struct LibgccObject*)
|
|
|
|
malloc(sizeof(struct LibgccObject));
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2008-08-29 06:34:49 +08:00
|
|
|
// Do like libgcc for the values of the field.
|
|
|
|
ob->unused1 = (void *)-1;
|
|
|
|
ob->unused2 = 0;
|
|
|
|
ob->unused3 = 0;
|
|
|
|
ob->frame = FrameBegin;
|
2011-03-16 04:25:54 +08:00
|
|
|
ob->encoding.i = 0;
|
2008-08-29 06:34:49 +08:00
|
|
|
ob->encoding.b.encoding = llvm::dwarf::DW_EH_PE_omit;
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2010-02-11 00:03:48 +08:00
|
|
|
// Put the info on both places, as libgcc uses the first or the second
|
2008-08-29 06:34:49 +08:00
|
|
|
// field. Note that we rely on having two pointers here. If fde_end was a
|
|
|
|
// char, things would get complicated.
|
|
|
|
ob->fde_end = (char*)LOI->unseenObjects;
|
|
|
|
ob->next = LOI->unseenObjects;
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2008-08-29 06:34:49 +08:00
|
|
|
// Update the key's unseenObjects list.
|
|
|
|
LOI->unseenObjects = ob;
|
2011-03-16 04:25:54 +08:00
|
|
|
|
|
|
|
// Finally update the "key". Apparently, libgcc requires it.
|
2008-08-29 06:34:49 +08:00
|
|
|
_keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST,
|
|
|
|
LOI);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif // __APPLE__
|
2010-11-13 08:55:45 +08:00
|
|
|
#endif // HAVE_EHTABLE_SUPPORT
|
2008-03-22 16:53:09 +08:00
|
|
|
|
2007-12-06 09:34:04 +08:00
|
|
|
/// createJIT - This is the factory method for creating a JIT for the current
|
|
|
|
/// machine, it does not fall back to the interpreter. This takes ownership
|
2010-01-28 04:34:15 +08:00
|
|
|
/// of the module.
|
|
|
|
ExecutionEngine *JIT::createJIT(Module *M,
|
2009-07-18 08:42:18 +08:00
|
|
|
std::string *ErrorStr,
|
|
|
|
JITMemoryManager *JMM,
|
2009-11-18 05:58:16 +08:00
|
|
|
bool GVsWithCode,
|
2011-05-14 05:51:29 +08:00
|
|
|
TargetMachine *TM) {
|
2010-08-18 00:19:18 +08:00
|
|
|
// Try to register the program as a source of symbols to resolve against.
|
2011-05-14 05:36:16 +08:00
|
|
|
//
|
|
|
|
// FIXME: Don't do this here.
|
2010-08-18 00:19:18 +08:00
|
|
|
sys::DynamicLibrary::LoadLibraryPermanently(0, NULL);
|
|
|
|
|
2011-05-14 05:36:16 +08:00
|
|
|
// If the target supports JIT code generation, create the JIT.
|
2009-07-18 08:42:18 +08:00
|
|
|
if (TargetJITInfo *TJ = TM->getJITInfo()) {
|
2011-12-12 12:20:36 +08:00
|
|
|
return new JIT(M, *TM, *TJ, JMM, GVsWithCode);
|
2009-07-18 08:42:18 +08:00
|
|
|
} else {
|
|
|
|
if (ErrorStr)
|
|
|
|
*ErrorStr = "target does not support JIT code generation";
|
|
|
|
return 0;
|
|
|
|
}
|
2007-12-06 09:34:04 +08:00
|
|
|
}
|
|
|
|
|
2010-02-11 09:07:39 +08:00
|
|
|
namespace {
|
|
|
|
/// This class supports the global getPointerToNamedFunction(), which allows
|
|
|
|
/// bugpoint or gdb users to search for a function by name without any context.
|
|
|
|
class JitPool {
|
|
|
|
SmallPtrSet<JIT*, 1> JITs; // Optimize for process containing just 1 JIT.
|
|
|
|
mutable sys::Mutex Lock;
|
|
|
|
public:
|
|
|
|
void Add(JIT *jit) {
|
|
|
|
MutexGuard guard(Lock);
|
|
|
|
JITs.insert(jit);
|
|
|
|
}
|
|
|
|
void Remove(JIT *jit) {
|
|
|
|
MutexGuard guard(Lock);
|
|
|
|
JITs.erase(jit);
|
|
|
|
}
|
|
|
|
void *getPointerToNamedFunction(const char *Name) const {
|
|
|
|
MutexGuard guard(Lock);
|
|
|
|
assert(JITs.size() != 0 && "No Jit registered");
|
|
|
|
//search function in every instance of JIT
|
|
|
|
for (SmallPtrSet<JIT*, 1>::const_iterator Jit = JITs.begin(),
|
|
|
|
end = JITs.end();
|
|
|
|
Jit != end; ++Jit) {
|
|
|
|
if (Function *F = (*Jit)->FindFunctionNamed(Name))
|
|
|
|
return (*Jit)->getPointerToFunction(F);
|
|
|
|
}
|
|
|
|
// The function is not available : fallback on the first created (will
|
|
|
|
// search in symbol of the current program/library)
|
|
|
|
return (*JITs.begin())->getPointerToNamedFunction(Name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ManagedStatic<JitPool> AllJits;
|
|
|
|
}
|
|
|
|
extern "C" {
|
|
|
|
// getPointerToNamedFunction - This function is used as a global wrapper to
|
|
|
|
// JIT::getPointerToNamedFunction for the purpose of resolving symbols when
|
|
|
|
// 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.
|
|
|
|
void *getPointerToNamedFunction(const char *Name) {
|
|
|
|
return AllJits->getPointerToNamedFunction(Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-28 04:34:15 +08:00
|
|
|
JIT::JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
|
2012-03-29 05:46:36 +08:00
|
|
|
JITMemoryManager *jmm, bool GVsWithCode)
|
|
|
|
: ExecutionEngine(M), TM(tm), TJI(tji),
|
|
|
|
JMM(jmm ? jmm : JITMemoryManager::CreateDefaultMemManager()),
|
|
|
|
AllocateGVsWithCode(GVsWithCode), isAlreadyCodeGenerating(false) {
|
2012-10-09 00:38:25 +08:00
|
|
|
setDataLayout(TM.getDataLayout());
|
2003-05-28 05:40:39 +08:00
|
|
|
|
2010-01-28 04:34:15 +08:00
|
|
|
jitstate = new JITState(M);
|
2008-05-22 00:34:48 +08:00
|
|
|
|
2009-05-31 04:51:52 +08:00
|
|
|
// Initialize JCE
|
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 07:52:43 +08:00
|
|
|
JCE = createEmitter(*this, JMM, TM);
|
2005-04-22 06:55:34 +08:00
|
|
|
|
2010-02-11 09:07:39 +08:00
|
|
|
// Register in global list of all JITs.
|
|
|
|
AllJits->Add(this);
|
|
|
|
|
2004-04-15 01:45:52 +08:00
|
|
|
// Add target data
|
2005-07-12 23:51:55 +08:00
|
|
|
MutexGuard locked(lock);
|
2008-05-22 00:34:48 +08:00
|
|
|
FunctionPassManager &PM = jitstate->getPM(locked);
|
2012-10-09 00:38:25 +08:00
|
|
|
PM.add(new DataLayout(*TM.getDataLayout()));
|
2004-04-15 01:45:52 +08:00
|
|
|
|
2003-12-20 09:46:27 +08:00
|
|
|
// Turn the machine code intermediate representation into bytes in memory that
|
|
|
|
// may be executed.
|
2011-12-02 05:49:21 +08:00
|
|
|
if (TM.addPassesToEmitMachineCode(PM, *JCE)) {
|
2010-04-08 06:58:41 +08:00
|
|
|
report_fatal_error("Target does not support machine code emission!");
|
2003-12-20 09:46:27 +08:00
|
|
|
}
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2008-08-18 22:53:56 +08:00
|
|
|
// Register routine for informing unwinding runtime about new EH frames
|
2010-11-13 08:55:45 +08:00
|
|
|
#if HAVE_EHTABLE_SUPPORT
|
2009-04-15 06:31:59 +08:00
|
|
|
#if USE_KEYMGR
|
2008-08-29 06:34:49 +08:00
|
|
|
struct LibgccObjectInfo* LOI = (struct LibgccObjectInfo*)
|
|
|
|
_keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST);
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2008-08-29 06:34:49 +08:00
|
|
|
// The key is created on demand, and libgcc creates it the first time an
|
|
|
|
// exception occurs. Since we need the key to register frames, we create
|
|
|
|
// it now.
|
2009-04-17 05:47:59 +08:00
|
|
|
if (!LOI)
|
2011-03-16 04:25:54 +08:00
|
|
|
LOI = (LibgccObjectInfo*)calloc(sizeof(struct LibgccObjectInfo), 1);
|
2009-04-17 05:47:59 +08:00
|
|
|
_keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST, LOI);
|
2008-08-29 06:34:49 +08:00
|
|
|
InstallExceptionTableRegister(DarwinRegisterFrame);
|
2010-10-21 16:57:29 +08:00
|
|
|
// Not sure about how to deregister on Darwin.
|
2008-08-29 06:34:49 +08:00
|
|
|
#else
|
2008-08-18 22:53:56 +08:00
|
|
|
InstallExceptionTableRegister(__register_frame);
|
2010-10-21 16:57:29 +08:00
|
|
|
InstallExceptionTableDeregister(__deregister_frame);
|
2008-08-29 06:34:49 +08:00
|
|
|
#endif // __APPLE__
|
2010-11-13 08:55:45 +08:00
|
|
|
#endif // HAVE_EHTABLE_SUPPORT
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2006-09-04 12:14:57 +08:00
|
|
|
// Initialize passes.
|
|
|
|
PM.doInitialization();
|
2002-12-24 08:01:05 +08:00
|
|
|
}
|
|
|
|
|
2003-12-20 09:46:27 +08:00
|
|
|
JIT::~JIT() {
|
2010-10-21 16:57:29 +08:00
|
|
|
// Unregister all exception tables registered by this JIT.
|
|
|
|
DeregisterAllTables();
|
|
|
|
// Cleanup.
|
2010-02-11 09:07:39 +08:00
|
|
|
AllJits->Remove(this);
|
2008-05-22 00:34:48 +08:00
|
|
|
delete jitstate;
|
2009-05-31 04:51:52 +08:00
|
|
|
delete JCE;
|
2012-03-29 05:46:36 +08:00
|
|
|
// JMM is a ownership of JCE, so we no need delete JMM here.
|
2003-12-20 09:46:27 +08:00
|
|
|
delete &TM;
|
|
|
|
}
|
|
|
|
|
2010-01-28 04:34:15 +08:00
|
|
|
/// addModule - Add a new Module to the JIT. If we previously removed the last
|
|
|
|
/// Module, we need re-initialize jitstate with a valid Module.
|
|
|
|
void JIT::addModule(Module *M) {
|
2008-05-22 00:34:48 +08:00
|
|
|
MutexGuard locked(lock);
|
|
|
|
|
|
|
|
if (Modules.empty()) {
|
|
|
|
assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
|
|
|
|
|
2010-01-28 04:34:15 +08:00
|
|
|
jitstate = new JITState(M);
|
2008-05-22 00:34:48 +08:00
|
|
|
|
|
|
|
FunctionPassManager &PM = jitstate->getPM(locked);
|
2012-10-09 00:38:25 +08:00
|
|
|
PM.add(new DataLayout(*TM.getDataLayout()));
|
2008-05-22 00:34:48 +08:00
|
|
|
|
|
|
|
// Turn the machine code intermediate representation into bytes in memory
|
|
|
|
// that may be executed.
|
2011-12-02 05:49:21 +08:00
|
|
|
if (TM.addPassesToEmitMachineCode(PM, *JCE)) {
|
2010-04-08 06:58:41 +08:00
|
|
|
report_fatal_error("Target does not support machine code emission!");
|
2008-05-22 00:34:48 +08:00
|
|
|
}
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2008-05-22 00:34:48 +08:00
|
|
|
// Initialize passes.
|
|
|
|
PM.doInitialization();
|
|
|
|
}
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2010-01-28 04:34:15 +08:00
|
|
|
ExecutionEngine::addModule(M);
|
2008-05-22 00:34:48 +08:00
|
|
|
}
|
|
|
|
|
2010-01-28 04:34:15 +08:00
|
|
|
/// removeModule - If we are removing the last Module, invalidate the jitstate
|
|
|
|
/// since the PassManager it contains references a released Module.
|
|
|
|
bool JIT::removeModule(Module *M) {
|
|
|
|
bool result = ExecutionEngine::removeModule(M);
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2008-05-22 00:34:48 +08:00
|
|
|
MutexGuard locked(lock);
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2012-07-26 03:06:29 +08:00
|
|
|
if (jitstate && jitstate->getModule() == M) {
|
2008-05-22 00:34:48 +08:00
|
|
|
delete jitstate;
|
|
|
|
jitstate = 0;
|
|
|
|
}
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2009-02-18 16:31:02 +08:00
|
|
|
if (!jitstate && !Modules.empty()) {
|
|
|
|
jitstate = new JITState(Modules[0]);
|
|
|
|
|
|
|
|
FunctionPassManager &PM = jitstate->getPM(locked);
|
2012-10-09 00:38:25 +08:00
|
|
|
PM.add(new DataLayout(*TM.getDataLayout()));
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2009-02-18 16:31:02 +08:00
|
|
|
// Turn the machine code intermediate representation into bytes in memory
|
|
|
|
// that may be executed.
|
2011-12-02 05:49:21 +08:00
|
|
|
if (TM.addPassesToEmitMachineCode(PM, *JCE)) {
|
2010-04-08 06:58:41 +08:00
|
|
|
report_fatal_error("Target does not support machine code emission!");
|
2009-02-18 16:31:02 +08:00
|
|
|
}
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2009-02-18 16:31:02 +08:00
|
|
|
// Initialize passes.
|
|
|
|
PM.doInitialization();
|
2011-03-16 04:25:54 +08:00
|
|
|
}
|
2008-05-22 00:34:48 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-09-06 02:42:01 +08:00
|
|
|
/// run - Start execution with the specified function and arguments.
|
2003-08-22 05:32:12 +08:00
|
|
|
///
|
2003-12-26 14:13:47 +08:00
|
|
|
GenericValue JIT::runFunction(Function *F,
|
|
|
|
const std::vector<GenericValue> &ArgValues) {
|
2004-08-16 07:29:50 +08:00
|
|
|
assert(F && "Function *F was null at entry to run()");
|
|
|
|
|
|
|
|
void *FPtr = getPointerToFunction(F);
|
2004-08-16 07:31:43 +08:00
|
|
|
assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
|
2011-07-18 12:54:35 +08:00
|
|
|
FunctionType *FTy = F->getFunctionType();
|
|
|
|
Type *RetTy = FTy->getReturnType();
|
2002-12-24 08:01:05 +08:00
|
|
|
|
2009-02-19 10:55:18 +08:00
|
|
|
assert((FTy->getNumParams() == ArgValues.size() ||
|
|
|
|
(FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
|
|
|
|
"Wrong number of arguments passed into function!");
|
2004-08-16 07:53:06 +08:00
|
|
|
assert(FTy->getNumParams() == ArgValues.size() &&
|
|
|
|
"This doesn't support passing arguments through varargs (yet)!");
|
|
|
|
|
2004-10-23 07:35:57 +08:00
|
|
|
// Handle some common cases first. These cases correspond to common `main'
|
2004-08-16 07:53:06 +08:00
|
|
|
// prototypes.
|
2010-02-16 00:12:20 +08:00
|
|
|
if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
|
2004-08-16 07:39:59 +08:00
|
|
|
switch (ArgValues.size()) {
|
|
|
|
case 3:
|
2010-02-16 00:12:20 +08:00
|
|
|
if (FTy->getParamType(0)->isIntegerTy(32) &&
|
2010-02-16 19:11:14 +08:00
|
|
|
FTy->getParamType(1)->isPointerTy() &&
|
|
|
|
FTy->getParamType(2)->isPointerTy()) {
|
2004-08-16 07:39:59 +08:00
|
|
|
int (*PF)(int, char **, const char **) =
|
2006-06-02 01:29:22 +08:00
|
|
|
(int(*)(int, char **, const char **))(intptr_t)FPtr;
|
2004-10-23 07:35:57 +08:00
|
|
|
|
2004-08-16 07:39:59 +08:00
|
|
|
// Call the function.
|
2004-08-16 07:53:06 +08:00
|
|
|
GenericValue rv;
|
2011-03-16 04:25:54 +08:00
|
|
|
rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
|
2007-03-06 11:11:31 +08:00
|
|
|
(char **)GVTOP(ArgValues[1]),
|
|
|
|
(const char **)GVTOP(ArgValues[2])));
|
2004-08-16 07:39:59 +08:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
break;
|
2004-08-16 09:07:04 +08:00
|
|
|
case 2:
|
2010-02-16 00:12:20 +08:00
|
|
|
if (FTy->getParamType(0)->isIntegerTy(32) &&
|
2010-02-16 19:11:14 +08:00
|
|
|
FTy->getParamType(1)->isPointerTy()) {
|
2006-06-02 01:29:22 +08:00
|
|
|
int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
|
2004-10-23 07:35:57 +08:00
|
|
|
|
2004-08-16 09:07:04 +08:00
|
|
|
// Call the function.
|
|
|
|
GenericValue rv;
|
2011-03-16 04:25:54 +08:00
|
|
|
rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
|
2007-03-06 11:11:31 +08:00
|
|
|
(char **)GVTOP(ArgValues[1])));
|
2004-08-16 09:07:04 +08:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
break;
|
2004-08-16 07:39:59 +08:00
|
|
|
case 1:
|
2012-08-02 20:09:32 +08:00
|
|
|
if (FTy->getParamType(0)->isIntegerTy(32)) {
|
2004-08-16 07:53:06 +08:00
|
|
|
GenericValue rv;
|
2006-06-02 01:29:22 +08:00
|
|
|
int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
|
2007-03-06 11:11:31 +08:00
|
|
|
rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
|
2004-08-16 07:39:59 +08:00
|
|
|
return rv;
|
|
|
|
}
|
2012-08-02 20:09:32 +08:00
|
|
|
if (FTy->getParamType(0)->isPointerTy()) {
|
|
|
|
GenericValue rv;
|
|
|
|
int (*PF)(char *) = (int(*)(char *))(intptr_t)FPtr;
|
|
|
|
rv.IntVal = APInt(32, PF((char*)GVTOP(ArgValues[0])));
|
|
|
|
return rv;
|
|
|
|
}
|
2004-08-16 07:39:59 +08:00
|
|
|
break;
|
2004-08-16 07:53:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle cases where no arguments are passed first.
|
|
|
|
if (ArgValues.empty()) {
|
|
|
|
GenericValue rv;
|
|
|
|
switch (RetTy->getTypeID()) {
|
2009-07-15 00:55:14 +08:00
|
|
|
default: llvm_unreachable("Unknown return type for function call!");
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
llvm-svn: 33113
2007-01-12 15:05:14 +08:00
|
|
|
case Type::IntegerTyID: {
|
|
|
|
unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
|
|
|
|
if (BitWidth == 1)
|
2007-03-06 11:11:31 +08:00
|
|
|
rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
llvm-svn: 33113
2007-01-12 15:05:14 +08:00
|
|
|
else if (BitWidth <= 8)
|
2007-03-06 11:11:31 +08:00
|
|
|
rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
llvm-svn: 33113
2007-01-12 15:05:14 +08:00
|
|
|
else if (BitWidth <= 16)
|
2007-03-06 11:11:31 +08:00
|
|
|
rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
llvm-svn: 33113
2007-01-12 15:05:14 +08:00
|
|
|
else if (BitWidth <= 32)
|
2007-03-06 11:11:31 +08:00
|
|
|
rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
llvm-svn: 33113
2007-01-12 15:05:14 +08:00
|
|
|
else if (BitWidth <= 64)
|
2007-03-06 11:11:31 +08:00
|
|
|
rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
|
2011-03-16 04:25:54 +08:00
|
|
|
else
|
2009-07-15 00:55:14 +08:00
|
|
|
llvm_unreachable("Integer types > 64 bits not supported");
|
2004-08-16 07:53:06 +08:00
|
|
|
return rv;
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
llvm-svn: 33113
2007-01-12 15:05:14 +08:00
|
|
|
}
|
2004-08-16 07:53:06 +08:00
|
|
|
case Type::VoidTyID:
|
2007-03-06 11:11:31 +08:00
|
|
|
rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
|
2004-08-16 07:53:06 +08:00
|
|
|
return rv;
|
|
|
|
case Type::FloatTyID:
|
2006-06-02 01:29:22 +08:00
|
|
|
rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
|
2004-08-16 07:53:06 +08:00
|
|
|
return rv;
|
|
|
|
case Type::DoubleTyID:
|
2006-06-02 01:29:22 +08:00
|
|
|
rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
|
2004-08-16 07:34:48 +08:00
|
|
|
return rv;
|
2007-09-18 02:44:13 +08:00
|
|
|
case Type::X86_FP80TyID:
|
|
|
|
case Type::FP128TyID:
|
|
|
|
case Type::PPC_FP128TyID:
|
2009-07-15 00:55:14 +08:00
|
|
|
llvm_unreachable("long double not supported yet");
|
2004-08-16 07:53:06 +08:00
|
|
|
case Type::PointerTyID:
|
2006-06-02 01:29:22 +08:00
|
|
|
return PTOGV(((void*(*)())(intptr_t)FPtr)());
|
2004-08-16 07:34:48 +08:00
|
|
|
}
|
2003-12-26 14:13:47 +08:00
|
|
|
}
|
2003-09-06 02:42:01 +08:00
|
|
|
|
2004-08-16 08:14:18 +08:00
|
|
|
// Okay, this is not one of our quick and easy cases. Because we don't have a
|
|
|
|
// full FFI, we have to codegen a nullary stub function that just calls the
|
|
|
|
// function we are interested in, passing in constants for all of the
|
|
|
|
// arguments. Make this function and return.
|
|
|
|
|
|
|
|
// First, create the function.
|
2009-07-30 06:17:13 +08:00
|
|
|
FunctionType *STy=FunctionType::get(RetTy, false);
|
2008-04-07 04:25:17 +08:00
|
|
|
Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
|
|
|
|
F->getParent());
|
2004-08-16 08:14:18 +08:00
|
|
|
|
|
|
|
// Insert a basic block.
|
2009-08-14 05:58:54 +08:00
|
|
|
BasicBlock *StubBB = BasicBlock::Create(F->getContext(), "", Stub);
|
2004-08-16 08:14:18 +08:00
|
|
|
|
|
|
|
// Convert all of the GenericValue arguments over to constants. Note that we
|
|
|
|
// currently don't support varargs.
|
2007-02-13 14:01:22 +08:00
|
|
|
SmallVector<Value*, 8> Args;
|
2004-08-16 08:14:18 +08:00
|
|
|
for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
|
|
|
|
Constant *C = 0;
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *ArgTy = FTy->getParamType(i);
|
2004-08-16 08:14:18 +08:00
|
|
|
const GenericValue &AV = ArgValues[i];
|
|
|
|
switch (ArgTy->getTypeID()) {
|
2009-07-15 00:55:14 +08:00
|
|
|
default: llvm_unreachable("Unknown argument type for function call!");
|
2008-04-20 08:41:09 +08:00
|
|
|
case Type::IntegerTyID:
|
2009-07-25 07:12:02 +08:00
|
|
|
C = ConstantInt::get(F->getContext(), AV.IntVal);
|
2008-04-20 08:41:09 +08:00
|
|
|
break;
|
|
|
|
case Type::FloatTyID:
|
2009-07-28 04:59:43 +08:00
|
|
|
C = ConstantFP::get(F->getContext(), APFloat(AV.FloatVal));
|
2008-04-20 08:41:09 +08:00
|
|
|
break;
|
|
|
|
case Type::DoubleTyID:
|
2009-07-28 04:59:43 +08:00
|
|
|
C = ConstantFP::get(F->getContext(), APFloat(AV.DoubleVal));
|
2008-04-20 08:41:09 +08:00
|
|
|
break;
|
2007-09-18 02:44:13 +08:00
|
|
|
case Type::PPC_FP128TyID:
|
|
|
|
case Type::X86_FP80TyID:
|
2008-04-20 08:41:09 +08:00
|
|
|
case Type::FP128TyID:
|
2013-01-22 17:46:31 +08:00
|
|
|
C = ConstantFP::get(F->getContext(), APFloat(ArgTy->getFltSemantics(),
|
|
|
|
AV.IntVal));
|
2008-04-20 08:41:09 +08:00
|
|
|
break;
|
2004-08-16 08:14:18 +08:00
|
|
|
case Type::PointerTyID:
|
|
|
|
void *ArgPtr = GVTOP(AV);
|
2008-04-20 08:41:09 +08:00
|
|
|
if (sizeof(void*) == 4)
|
2011-03-16 04:25:54 +08:00
|
|
|
C = ConstantInt::get(Type::getInt32Ty(F->getContext()),
|
2009-08-14 05:58:54 +08:00
|
|
|
(int)(intptr_t)ArgPtr);
|
2008-04-20 08:41:09 +08:00
|
|
|
else
|
2009-08-14 05:58:54 +08:00
|
|
|
C = ConstantInt::get(Type::getInt64Ty(F->getContext()),
|
|
|
|
(intptr_t)ArgPtr);
|
2009-07-15 07:09:55 +08:00
|
|
|
// Cast the integer to pointer
|
2009-07-30 02:55:55 +08:00
|
|
|
C = ConstantExpr::getIntToPtr(C, ArgTy);
|
2004-08-16 08:14:18 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
Args.push_back(C);
|
|
|
|
}
|
|
|
|
|
2011-07-15 16:37:34 +08:00
|
|
|
CallInst *TheCall = CallInst::Create(F, Args, "", StubBB);
|
2008-11-23 16:00:11 +08:00
|
|
|
TheCall->setCallingConv(F->getCallingConv());
|
2005-05-06 14:48:54 +08:00
|
|
|
TheCall->setTailCall();
|
2010-01-05 21:12:22 +08:00
|
|
|
if (!TheCall->getType()->isVoidTy())
|
2009-08-14 05:58:54 +08:00
|
|
|
// Return result of the call.
|
|
|
|
ReturnInst::Create(F->getContext(), TheCall, StubBB);
|
2004-08-16 08:14:18 +08:00
|
|
|
else
|
2009-08-14 05:58:54 +08:00
|
|
|
ReturnInst::Create(F->getContext(), StubBB); // Just return void.
|
2004-08-16 08:14:18 +08:00
|
|
|
|
2010-02-13 07:05:31 +08:00
|
|
|
// Finally, call our nullary stub function.
|
|
|
|
GenericValue Result = runFunction(Stub, std::vector<GenericValue>());
|
|
|
|
// Erase it, since no other function can have a reference to it.
|
|
|
|
Stub->eraseFromParent();
|
|
|
|
// And return the result.
|
|
|
|
return Result;
|
2002-12-24 08:01:05 +08:00
|
|
|
}
|
2003-12-20 09:46:27 +08:00
|
|
|
|
2009-06-25 10:04:04 +08:00
|
|
|
void JIT::RegisterJITEventListener(JITEventListener *L) {
|
|
|
|
if (L == NULL)
|
|
|
|
return;
|
|
|
|
MutexGuard locked(lock);
|
|
|
|
EventListeners.push_back(L);
|
|
|
|
}
|
|
|
|
void JIT::UnregisterJITEventListener(JITEventListener *L) {
|
|
|
|
if (L == NULL)
|
|
|
|
return;
|
|
|
|
MutexGuard locked(lock);
|
|
|
|
std::vector<JITEventListener*>::reverse_iterator I=
|
|
|
|
std::find(EventListeners.rbegin(), EventListeners.rend(), L);
|
|
|
|
if (I != EventListeners.rend()) {
|
|
|
|
std::swap(*I, EventListeners.back());
|
|
|
|
EventListeners.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void JIT::NotifyFunctionEmitted(
|
|
|
|
const Function &F,
|
|
|
|
void *Code, size_t Size,
|
|
|
|
const JITEvent_EmittedFunctionDetails &Details) {
|
|
|
|
MutexGuard locked(lock);
|
|
|
|
for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
|
|
|
|
EventListeners[I]->NotifyFunctionEmitted(F, Code, Size, Details);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-27 08:03:05 +08:00
|
|
|
void JIT::NotifyFreeingMachineCode(void *OldPtr) {
|
2009-06-25 10:04:04 +08:00
|
|
|
MutexGuard locked(lock);
|
|
|
|
for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
|
2009-10-27 08:03:05 +08:00
|
|
|
EventListeners[I]->NotifyFreeingMachineCode(OldPtr);
|
2009-06-25 10:04:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-20 09:46:27 +08:00
|
|
|
/// runJITOnFunction - Run the FunctionPassManager full of
|
|
|
|
/// just-in-time compilation passes on F, hopefully filling in
|
|
|
|
/// GlobalAddress[F] with the address of F's machine code.
|
|
|
|
///
|
2009-05-19 05:06:40 +08:00
|
|
|
void JIT::runJITOnFunction(Function *F, MachineCodeInfo *MCI) {
|
2005-07-12 23:51:55 +08:00
|
|
|
MutexGuard locked(lock);
|
2009-05-19 05:06:40 +08:00
|
|
|
|
2009-06-25 10:04:04 +08:00
|
|
|
class MCIListener : public JITEventListener {
|
|
|
|
MachineCodeInfo *const MCI;
|
|
|
|
public:
|
|
|
|
MCIListener(MachineCodeInfo *mci) : MCI(mci) {}
|
|
|
|
virtual void NotifyFunctionEmitted(const Function &,
|
|
|
|
void *Code, size_t Size,
|
|
|
|
const EmittedFunctionDetails &) {
|
|
|
|
MCI->setAddress(Code);
|
|
|
|
MCI->setSize(Size);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
MCIListener MCIL(MCI);
|
2009-12-23 07:18:18 +08:00
|
|
|
if (MCI)
|
|
|
|
RegisterJITEventListener(&MCIL);
|
2009-05-19 05:06:40 +08:00
|
|
|
|
2009-02-07 05:25:08 +08:00
|
|
|
runJITOnFunctionUnlocked(F, locked);
|
2009-05-19 05:06:40 +08:00
|
|
|
|
2009-12-23 07:18:18 +08:00
|
|
|
if (MCI)
|
|
|
|
UnregisterJITEventListener(&MCIL);
|
2009-02-07 05:25:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void JIT::runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked) {
|
2007-08-14 04:08:16 +08:00
|
|
|
assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
|
2003-12-20 09:46:27 +08:00
|
|
|
|
2010-07-12 07:07:28 +08:00
|
|
|
jitTheFunction(F, locked);
|
2003-12-20 11:36:47 +08:00
|
|
|
|
2009-02-18 16:31:02 +08:00
|
|
|
// If the function referred to another function that had not yet been
|
2009-10-28 04:30:28 +08:00
|
|
|
// read from bitcode, and we are jitting non-lazily, emit it now.
|
2009-02-18 16:31:02 +08:00
|
|
|
while (!jitstate->getPendingFunctions(locked).empty()) {
|
|
|
|
Function *PF = jitstate->getPendingFunctions(locked).back();
|
|
|
|
jitstate->getPendingFunctions(locked).pop_back();
|
|
|
|
|
2009-12-18 05:35:29 +08:00
|
|
|
assert(!PF->hasAvailableExternallyLinkage() &&
|
|
|
|
"Externally-defined function should not be in pending list.");
|
|
|
|
|
2010-07-12 07:07:28 +08:00
|
|
|
jitTheFunction(PF, locked);
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2009-02-18 16:31:02 +08:00
|
|
|
// Now that the function has been jitted, ask the JITEmitter to rewrite
|
|
|
|
// the stub with real address of the function.
|
|
|
|
updateFunctionStub(PF);
|
2003-12-20 11:36:47 +08:00
|
|
|
}
|
2003-12-20 09:46:27 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 07:07:28 +08:00
|
|
|
void JIT::jitTheFunction(Function *F, const MutexGuard &locked) {
|
|
|
|
isAlreadyCodeGenerating = true;
|
|
|
|
jitstate->getPM(locked).run(*F);
|
|
|
|
isAlreadyCodeGenerating = false;
|
|
|
|
|
|
|
|
// clear basic block addresses after this function is done
|
|
|
|
getBasicBlockAddressMap(locked).clear();
|
|
|
|
}
|
|
|
|
|
2003-12-20 09:46:27 +08:00
|
|
|
/// getPointerToFunction - This method is used to get the address of the
|
2011-04-15 13:18:47 +08:00
|
|
|
/// specified function, compiling it if necessary.
|
2003-12-20 09:46:27 +08:00
|
|
|
///
|
|
|
|
void *JIT::getPointerToFunction(Function *F) {
|
2005-07-12 23:51:55 +08:00
|
|
|
|
2003-12-20 11:36:47 +08:00
|
|
|
if (void *Addr = getPointerToGlobalIfAvailable(F))
|
|
|
|
return Addr; // Check if function already code gen'd
|
2003-12-20 09:46:27 +08:00
|
|
|
|
2009-02-19 10:40:15 +08:00
|
|
|
MutexGuard locked(lock);
|
2008-10-10 09:47:42 +08:00
|
|
|
|
2009-12-18 05:35:29 +08:00
|
|
|
// Now that this thread owns the lock, make sure we read in the function if it
|
|
|
|
// exists in this Module.
|
|
|
|
std::string ErrorMsg;
|
2010-01-28 04:34:15 +08:00
|
|
|
if (F->Materialize(&ErrorMsg)) {
|
2010-04-08 06:58:41 +08:00
|
|
|
report_fatal_error("Error reading function '" + F->getName()+
|
2009-12-18 05:35:29 +08:00
|
|
|
"' from bitcode file: " + ErrorMsg);
|
2008-04-20 16:33:02 +08:00
|
|
|
}
|
2003-12-20 09:46:27 +08:00
|
|
|
|
2009-12-18 05:35:29 +08:00
|
|
|
// ... and check if another thread has already code gen'd the function.
|
|
|
|
if (void *Addr = getPointerToGlobalIfAvailable(F))
|
|
|
|
return Addr;
|
|
|
|
|
2009-10-26 07:06:42 +08:00
|
|
|
if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
|
2009-11-10 06:34:19 +08:00
|
|
|
bool AbortOnFailure = !F->hasExternalWeakLinkage();
|
2009-01-05 13:32:42 +08:00
|
|
|
void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
|
2003-12-20 11:36:47 +08:00
|
|
|
addGlobalMapping(F, Addr);
|
|
|
|
return Addr;
|
|
|
|
}
|
2003-12-20 09:46:27 +08:00
|
|
|
|
2009-02-07 05:25:08 +08:00
|
|
|
runJITOnFunctionUnlocked(F, locked);
|
2003-12-20 11:36:47 +08:00
|
|
|
|
|
|
|
void *Addr = getPointerToGlobalIfAvailable(F);
|
2003-12-20 09:46:27 +08:00
|
|
|
assert(Addr && "Code generation didn't add function to GlobalAddress table!");
|
|
|
|
return Addr;
|
|
|
|
}
|
|
|
|
|
2010-07-12 07:07:28 +08:00
|
|
|
void JIT::addPointerToBasicBlock(const BasicBlock *BB, void *Addr) {
|
|
|
|
MutexGuard locked(lock);
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2010-07-12 07:07:28 +08:00
|
|
|
BasicBlockAddressMapTy::iterator I =
|
|
|
|
getBasicBlockAddressMap(locked).find(BB);
|
|
|
|
if (I == getBasicBlockAddressMap(locked).end()) {
|
|
|
|
getBasicBlockAddressMap(locked)[BB] = Addr;
|
|
|
|
} else {
|
|
|
|
// ignore repeats: some BBs can be split into few MBBs?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JIT::clearPointerToBasicBlock(const BasicBlock *BB) {
|
|
|
|
MutexGuard locked(lock);
|
|
|
|
getBasicBlockAddressMap(locked).erase(BB);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *JIT::getPointerToBasicBlock(BasicBlock *BB) {
|
|
|
|
// make sure it's function is compiled by JIT
|
|
|
|
(void)getPointerToFunction(BB->getParent());
|
|
|
|
|
|
|
|
// resolve basic block address
|
|
|
|
MutexGuard locked(lock);
|
2011-03-16 04:25:54 +08:00
|
|
|
|
2010-07-12 07:07:28 +08:00
|
|
|
BasicBlockAddressMapTy::iterator I =
|
|
|
|
getBasicBlockAddressMap(locked).find(BB);
|
|
|
|
if (I != getBasicBlockAddressMap(locked).end()) {
|
|
|
|
return I->second;
|
|
|
|
} else {
|
2012-02-07 13:05:23 +08:00
|
|
|
llvm_unreachable("JIT does not have BB address for address-of-label, was"
|
|
|
|
" it eliminated by optimizer?");
|
2010-07-12 07:07:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-29 05:46:36 +08:00
|
|
|
void *JIT::getPointerToNamedFunction(const std::string &Name,
|
|
|
|
bool AbortOnFailure){
|
|
|
|
if (!isSymbolSearchingDisabled()) {
|
|
|
|
void *ptr = JMM->getPointerToNamedFunction(Name, false);
|
|
|
|
if (ptr)
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If a LazyFunctionCreator is installed, use it to get/create the function.
|
|
|
|
if (LazyFunctionCreator)
|
|
|
|
if (void *RP = LazyFunctionCreator(Name))
|
|
|
|
return RP;
|
|
|
|
|
|
|
|
if (AbortOnFailure) {
|
|
|
|
report_fatal_error("Program used external function '"+Name+
|
|
|
|
"' which could not be resolved!");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-20 11:36:47 +08:00
|
|
|
/// getOrEmitGlobalVariable - Return the address of the specified global
|
|
|
|
/// variable, possibly emitting it to memory if needed. This is used by the
|
|
|
|
/// Emitter.
|
|
|
|
void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
|
2005-07-12 23:51:55 +08:00
|
|
|
MutexGuard locked(lock);
|
|
|
|
|
2003-12-20 11:36:47 +08:00
|
|
|
void *Ptr = getPointerToGlobalIfAvailable(GV);
|
|
|
|
if (Ptr) return Ptr;
|
|
|
|
|
|
|
|
// If the global is external, just remember the address.
|
2009-12-14 04:30:32 +08:00
|
|
|
if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage()) {
|
2007-07-31 04:02:02 +08:00
|
|
|
#if HAVE___DSO_HANDLE
|
2006-07-22 07:06:20 +08:00
|
|
|
if (GV->getName() == "__dso_handle")
|
|
|
|
return (void*)&__dso_handle;
|
2006-07-22 08:42:03 +08:00
|
|
|
#endif
|
2009-07-21 16:54:24 +08:00
|
|
|
Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName());
|
2009-11-10 06:34:19 +08:00
|
|
|
if (Ptr == 0) {
|
2010-04-08 06:58:41 +08:00
|
|
|
report_fatal_error("Could not resolve external global address: "
|
2009-07-08 01:32:34 +08:00
|
|
|
+GV->getName());
|
2003-12-20 11:36:47 +08:00
|
|
|
}
|
2009-03-05 03:10:38 +08:00
|
|
|
addGlobalMapping(GV, Ptr);
|
2003-12-20 11:36:47 +08:00
|
|
|
} else {
|
2008-08-07 09:30:15 +08:00
|
|
|
// If the global hasn't been emitted to memory yet, allocate space and
|
2009-07-09 05:59:57 +08:00
|
|
|
// emit it into memory.
|
|
|
|
Ptr = getMemoryForGV(GV);
|
2008-08-07 09:30:15 +08:00
|
|
|
addGlobalMapping(GV, Ptr);
|
2009-07-09 05:59:57 +08:00
|
|
|
EmitGlobalVariable(GV); // Initialize the variable.
|
2003-12-20 11:36:47 +08:00
|
|
|
}
|
|
|
|
return Ptr;
|
|
|
|
}
|
|
|
|
|
2003-12-20 09:46:27 +08:00
|
|
|
/// recompileAndRelinkFunction - This method is used to force a function
|
|
|
|
/// which has already been compiled, to be compiled again, possibly
|
|
|
|
/// after it has been modified. Then the entry to the old copy is overwritten
|
|
|
|
/// with a branch to the new copy. If there was no old copy, this acts
|
|
|
|
/// just like JIT::getPointerToFunction().
|
|
|
|
///
|
|
|
|
void *JIT::recompileAndRelinkFunction(Function *F) {
|
2003-12-20 11:36:47 +08:00
|
|
|
void *OldAddr = getPointerToGlobalIfAvailable(F);
|
2003-12-20 09:46:27 +08:00
|
|
|
|
2003-12-20 11:36:47 +08:00
|
|
|
// If it's not already compiled there is no reason to patch it up.
|
|
|
|
if (OldAddr == 0) { return getPointerToFunction(F); }
|
2003-12-20 09:46:27 +08:00
|
|
|
|
2003-12-20 11:36:47 +08:00
|
|
|
// Delete the old function mapping.
|
|
|
|
addGlobalMapping(F, 0);
|
|
|
|
|
|
|
|
// Recodegen the function
|
2003-12-20 09:46:27 +08:00
|
|
|
runJITOnFunction(F);
|
2003-12-20 11:36:47 +08:00
|
|
|
|
|
|
|
// Update state, forward the old function to the new function.
|
|
|
|
void *Addr = getPointerToGlobalIfAvailable(F);
|
2003-12-20 09:46:27 +08:00
|
|
|
assert(Addr && "Code generation didn't add function to GlobalAddress table!");
|
|
|
|
TJI.replaceMachineCodeForFunction(OldAddr, Addr);
|
|
|
|
return Addr;
|
|
|
|
}
|
2004-11-08 07:58:46 +08:00
|
|
|
|
2008-10-25 23:41:43 +08:00
|
|
|
/// getMemoryForGV - This method abstracts memory allocation of global
|
|
|
|
/// variable so that the JIT can allocate thread local variables depending
|
|
|
|
/// on the target.
|
|
|
|
///
|
|
|
|
char* JIT::getMemoryForGV(const GlobalVariable* GV) {
|
2009-07-09 05:59:57 +08:00
|
|
|
char *Ptr;
|
|
|
|
|
|
|
|
// GlobalVariable's which are not "constant" will cause trouble in a server
|
|
|
|
// situation. It's returned in the same block of memory as code which may
|
|
|
|
// not be writable.
|
|
|
|
if (isGVCompilationDisabled() && !GV->isConstant()) {
|
2010-04-08 06:58:41 +08:00
|
|
|
report_fatal_error("Compilation of non-internal GlobalValue is disabled!");
|
2009-07-09 05:59:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Some applications require globals and code to live together, so they may
|
|
|
|
// be allocated into the same buffer, but in general globals are allocated
|
|
|
|
// through the memory manager which puts them near the code but not in the
|
|
|
|
// same buffer.
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *GlobalType = GV->getType()->getElementType();
|
2012-10-09 00:38:25 +08:00
|
|
|
size_t S = getDataLayout()->getTypeAllocSize(GlobalType);
|
|
|
|
size_t A = getDataLayout()->getPreferredAlignment(GV);
|
2008-10-25 23:41:43 +08:00
|
|
|
if (GV->isThreadLocal()) {
|
|
|
|
MutexGuard locked(lock);
|
2009-07-09 05:59:57 +08:00
|
|
|
Ptr = TJI.allocateThreadLocalMemory(S);
|
|
|
|
} else if (TJI.allocateSeparateGVMemory()) {
|
|
|
|
if (A <= 8) {
|
|
|
|
Ptr = (char*)malloc(S);
|
|
|
|
} else {
|
|
|
|
// Allocate S+A bytes of memory, then use an aligned pointer within that
|
|
|
|
// space.
|
|
|
|
Ptr = (char*)malloc(S+A);
|
|
|
|
unsigned MisAligned = ((intptr_t)Ptr & (A-1));
|
|
|
|
Ptr = Ptr + (MisAligned ? (A-MisAligned) : 0);
|
|
|
|
}
|
|
|
|
} else if (AllocateGVsWithCode) {
|
|
|
|
Ptr = (char*)JCE->allocateSpace(S, A);
|
2008-10-25 23:41:43 +08:00
|
|
|
} else {
|
2009-07-09 05:59:57 +08:00
|
|
|
Ptr = (char*)JCE->allocateGlobal(S, A);
|
2008-10-25 23:41:43 +08:00
|
|
|
}
|
2009-07-09 05:59:57 +08:00
|
|
|
return Ptr;
|
2008-10-25 23:41:43 +08:00
|
|
|
}
|
2009-02-18 16:31:02 +08:00
|
|
|
|
|
|
|
void JIT::addPendingFunction(Function *F) {
|
|
|
|
MutexGuard locked(lock);
|
|
|
|
jitstate->getPendingFunctions(locked).push_back(F);
|
|
|
|
}
|
2009-06-25 10:04:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
JITEventListener::~JITEventListener() {}
|