2003-12-20 01:46:27 +00:00
|
|
|
//===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-24 00:01:05 +00:00
|
|
|
//
|
2003-12-20 01:46:27 +00:00
|
|
|
// This tool implements a just-in-time compiler for LLVM, allowing direct
|
2007-07-05 17:07:56 +00:00
|
|
|
// execution of LLVM bitcode in an efficient manner.
|
2002-12-24 00:01:05 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-12-20 01:46:27 +00:00
|
|
|
#include "JIT.h"
|
2004-08-16 00:14:18 +00:00
|
|
|
#include "llvm/Constants.h"
|
2003-12-20 03:36:47 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2003-12-20 01:46:27 +00:00
|
|
|
#include "llvm/Function.h"
|
2003-12-20 03:36:47 +00:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2004-08-16 00:14:18 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2003-10-16 21:19:34 +00:00
|
|
|
#include "llvm/ModuleProvider.h"
|
2009-05-30 20:51:52 +00:00
|
|
|
#include "llvm/CodeGen/JITCodeEmitter.h"
|
2009-05-18 21:06:40 +00:00
|
|
|
#include "llvm/CodeGen/MachineCodeInfo.h"
|
2009-06-25 02:04:04 +00:00
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
|
|
|
#include "llvm/ExecutionEngine/JITEventListener.h"
|
2006-05-12 06:33:49 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2002-12-24 00:01:05 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2003-12-20 01:46:27 +00:00
|
|
|
#include "llvm/Target/TargetJITInfo.h"
|
2009-03-03 20:10:23 +00:00
|
|
|
#include "llvm/Support/Dwarf.h"
|
|
|
|
#include "llvm/Support/MutexGuard.h"
|
|
|
|
#include "llvm/System/DynamicLibrary.h"
|
2007-07-30 20:02:02 +00:00
|
|
|
#include "llvm/Config/config.h"
|
|
|
|
|
2003-12-08 08:06:28 +00:00
|
|
|
using namespace llvm;
|
2003-05-27 21:40:39 +00:00
|
|
|
|
2006-07-22 16:59:38 +00:00
|
|
|
#ifdef __APPLE__
|
2007-07-30 20:02:02 +00: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-21 23:06:20 +00:00
|
|
|
#endif
|
2007-07-30 20:02:02 +00:00
|
|
|
|
|
|
|
#if HAVE___DSO_HANDLE
|
|
|
|
extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
|
2006-07-22 16:59:38 +00:00
|
|
|
#endif
|
2006-07-21 23:06:20 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
namespace {
|
|
|
|
|
2006-03-22 06:07:50 +00:00
|
|
|
static struct RegisterJIT {
|
|
|
|
RegisterJIT() { JIT::Register(); }
|
|
|
|
} JITRegistrator;
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
}
|
|
|
|
|
2009-06-24 21:09:18 +00:00
|
|
|
extern "C" void LLVMLinkInJIT() {
|
2006-03-24 02:53:49 +00:00
|
|
|
}
|
|
|
|
|
2008-08-28 22:34:49 +00:00
|
|
|
|
2009-02-01 06:42:27 +00:00
|
|
|
#if defined(__GNUC__) && !defined(__ARM__EABI__)
|
2008-08-28 22:34:49 +00: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 08:53:09 +00:00
|
|
|
extern "C" void __register_frame(void*);
|
2008-08-28 22:34:49 +00:00
|
|
|
|
2009-04-14 22:31:59 +00: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-28 22:34:49 +00: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;
|
|
|
|
|
|
|
|
/// frame - Pointer to the exception table.
|
|
|
|
void *frame;
|
|
|
|
|
|
|
|
/// 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;
|
|
|
|
unsigned long count : 21;
|
|
|
|
} b;
|
|
|
|
size_t i;
|
|
|
|
} encoding;
|
|
|
|
|
|
|
|
/// 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;
|
|
|
|
|
|
|
|
/// 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;
|
|
|
|
|
|
|
|
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-03 20:10:23 +00:00
|
|
|
LibgccObjectInfo* LOI = (struct LibgccObjectInfo*)
|
2008-08-28 22:34:49 +00:00
|
|
|
_keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST);
|
2009-03-03 20:10:23 +00:00
|
|
|
assert(LOI && "This should be preallocated by the runtime");
|
2008-08-28 22:34:49 +00: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));
|
|
|
|
|
|
|
|
// Do like libgcc for the values of the field.
|
|
|
|
ob->unused1 = (void *)-1;
|
|
|
|
ob->unused2 = 0;
|
|
|
|
ob->unused3 = 0;
|
|
|
|
ob->frame = FrameBegin;
|
|
|
|
ob->encoding.i = 0;
|
|
|
|
ob->encoding.b.encoding = llvm::dwarf::DW_EH_PE_omit;
|
|
|
|
|
|
|
|
// Put the info on both places, as libgcc uses the first or the the second
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// Update the key's unseenObjects list.
|
|
|
|
LOI->unseenObjects = ob;
|
|
|
|
|
|
|
|
// Finally update the "key". Apparently, libgcc requires it.
|
|
|
|
_keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST,
|
|
|
|
LOI);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif // __APPLE__
|
|
|
|
#endif // __GNUC__
|
2008-03-22 08:53:09 +00:00
|
|
|
|
2007-12-06 01:34:04 +00: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
|
|
|
|
/// of the module provider.
|
|
|
|
ExecutionEngine *ExecutionEngine::createJIT(ModuleProvider *MP,
|
|
|
|
std::string *ErrorStr,
|
2008-08-08 08:11:34 +00:00
|
|
|
JITMemoryManager *JMM,
|
2009-04-29 23:29:43 +00:00
|
|
|
CodeGenOpt::Level OptLevel) {
|
2009-04-29 00:32:19 +00:00
|
|
|
ExecutionEngine *EE = JIT::createJIT(MP, ErrorStr, JMM, OptLevel);
|
2007-12-06 01:34:04 +00:00
|
|
|
if (!EE) return 0;
|
|
|
|
|
|
|
|
// Make sure we can resolve symbols in the program as well. The zero arg
|
|
|
|
// to the function tells DynamicLibrary to load the program, not a library.
|
|
|
|
sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr);
|
|
|
|
return EE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
|
2009-04-29 23:29:43 +00:00
|
|
|
JITMemoryManager *JMM, CodeGenOpt::Level OptLevel)
|
2008-05-21 16:34:48 +00:00
|
|
|
: ExecutionEngine(MP), TM(tm), TJI(tji) {
|
2002-12-24 00:01:05 +00:00
|
|
|
setTargetData(TM.getTargetData());
|
2003-05-27 21:40:39 +00:00
|
|
|
|
2008-05-21 16:34:48 +00:00
|
|
|
jitstate = new JITState(MP);
|
|
|
|
|
2009-05-30 20:51:52 +00:00
|
|
|
// Initialize JCE
|
|
|
|
JCE = createEmitter(*this, JMM);
|
2005-04-21 22:55:34 +00:00
|
|
|
|
2004-04-14 17:45:52 +00:00
|
|
|
// Add target data
|
2005-07-12 15:51:55 +00:00
|
|
|
MutexGuard locked(lock);
|
2008-05-21 16:34:48 +00:00
|
|
|
FunctionPassManager &PM = jitstate->getPM(locked);
|
2006-05-04 21:18:40 +00:00
|
|
|
PM.add(new TargetData(*TM.getTargetData()));
|
2004-04-14 17:45:52 +00:00
|
|
|
|
2003-12-20 01:46:27 +00:00
|
|
|
// Turn the machine code intermediate representation into bytes in memory that
|
|
|
|
// may be executed.
|
2009-05-30 20:51:52 +00:00
|
|
|
if (TM.addPassesToEmitMachineCode(PM, *JCE, OptLevel)) {
|
2006-12-07 20:04:42 +00:00
|
|
|
cerr << "Target does not support machine code emission!\n";
|
2003-12-20 01:46:27 +00:00
|
|
|
abort();
|
|
|
|
}
|
2006-09-04 04:14:57 +00:00
|
|
|
|
2008-08-18 14:53:56 +00:00
|
|
|
// Register routine for informing unwinding runtime about new EH frames
|
2009-02-01 06:42:27 +00:00
|
|
|
#if defined(__GNUC__) && !defined(__ARM_EABI__)
|
2009-04-14 22:31:59 +00:00
|
|
|
#if USE_KEYMGR
|
2008-08-28 22:34:49 +00:00
|
|
|
struct LibgccObjectInfo* LOI = (struct LibgccObjectInfo*)
|
|
|
|
_keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST);
|
|
|
|
|
|
|
|
// 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-16 21:47:59 +00:00
|
|
|
if (!LOI)
|
|
|
|
LOI = (LibgccObjectInfo*)calloc(sizeof(struct LibgccObjectInfo), 1);
|
|
|
|
_keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST, LOI);
|
2008-08-28 22:34:49 +00:00
|
|
|
InstallExceptionTableRegister(DarwinRegisterFrame);
|
|
|
|
#else
|
2008-08-18 14:53:56 +00:00
|
|
|
InstallExceptionTableRegister(__register_frame);
|
2008-08-28 22:34:49 +00:00
|
|
|
#endif // __APPLE__
|
|
|
|
#endif // __GNUC__
|
2008-08-18 14:53:56 +00:00
|
|
|
|
2006-09-04 04:14:57 +00:00
|
|
|
// Initialize passes.
|
|
|
|
PM.doInitialization();
|
2002-12-24 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
2003-12-20 01:46:27 +00:00
|
|
|
JIT::~JIT() {
|
2008-05-21 16:34:48 +00:00
|
|
|
delete jitstate;
|
2009-05-30 20:51:52 +00:00
|
|
|
delete JCE;
|
2003-12-20 01:46:27 +00:00
|
|
|
delete &TM;
|
|
|
|
}
|
|
|
|
|
2008-05-21 16:34:48 +00:00
|
|
|
/// addModuleProvider - Add a new ModuleProvider to the JIT. If we previously
|
|
|
|
/// removed the last ModuleProvider, we need re-initialize jitstate with a valid
|
|
|
|
/// ModuleProvider.
|
|
|
|
void JIT::addModuleProvider(ModuleProvider *MP) {
|
|
|
|
MutexGuard locked(lock);
|
|
|
|
|
|
|
|
if (Modules.empty()) {
|
|
|
|
assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
|
|
|
|
|
|
|
|
jitstate = new JITState(MP);
|
|
|
|
|
|
|
|
FunctionPassManager &PM = jitstate->getPM(locked);
|
|
|
|
PM.add(new TargetData(*TM.getTargetData()));
|
|
|
|
|
|
|
|
// Turn the machine code intermediate representation into bytes in memory
|
|
|
|
// that may be executed.
|
2009-05-30 20:51:52 +00:00
|
|
|
if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) {
|
2008-05-21 16:34:48 +00:00
|
|
|
cerr << "Target does not support machine code emission!\n";
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize passes.
|
|
|
|
PM.doInitialization();
|
|
|
|
}
|
|
|
|
|
|
|
|
ExecutionEngine::addModuleProvider(MP);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// removeModuleProvider - If we are removing the last ModuleProvider,
|
|
|
|
/// invalidate the jitstate since the PassManager it contains references a
|
|
|
|
/// released ModuleProvider.
|
|
|
|
Module *JIT::removeModuleProvider(ModuleProvider *MP, std::string *E) {
|
|
|
|
Module *result = ExecutionEngine::removeModuleProvider(MP, E);
|
|
|
|
|
|
|
|
MutexGuard locked(lock);
|
2009-02-18 08:31:02 +00:00
|
|
|
|
|
|
|
if (jitstate->getMP() == MP) {
|
2008-05-21 16:34:48 +00:00
|
|
|
delete jitstate;
|
|
|
|
jitstate = 0;
|
|
|
|
}
|
|
|
|
|
2009-02-18 08:31:02 +00:00
|
|
|
if (!jitstate && !Modules.empty()) {
|
|
|
|
jitstate = new JITState(Modules[0]);
|
|
|
|
|
|
|
|
FunctionPassManager &PM = jitstate->getPM(locked);
|
|
|
|
PM.add(new TargetData(*TM.getTargetData()));
|
|
|
|
|
|
|
|
// Turn the machine code intermediate representation into bytes in memory
|
|
|
|
// that may be executed.
|
2009-05-30 20:51:52 +00:00
|
|
|
if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) {
|
2009-02-18 08:31:02 +00:00
|
|
|
cerr << "Target does not support machine code emission!\n";
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize passes.
|
|
|
|
PM.doInitialization();
|
|
|
|
}
|
2008-05-21 16:34:48 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-01-23 19:27:28 +00:00
|
|
|
/// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
|
|
|
|
/// and deletes the ModuleProvider and owned Module. Avoids materializing
|
|
|
|
/// the underlying module.
|
|
|
|
void JIT::deleteModuleProvider(ModuleProvider *MP, std::string *E) {
|
|
|
|
ExecutionEngine::deleteModuleProvider(MP, E);
|
|
|
|
|
|
|
|
MutexGuard locked(lock);
|
2009-02-18 08:31:02 +00:00
|
|
|
|
|
|
|
if (jitstate->getMP() == MP) {
|
2009-01-23 19:27:28 +00:00
|
|
|
delete jitstate;
|
|
|
|
jitstate = 0;
|
|
|
|
}
|
2009-02-18 08:31:02 +00:00
|
|
|
|
|
|
|
if (!jitstate && !Modules.empty()) {
|
|
|
|
jitstate = new JITState(Modules[0]);
|
|
|
|
|
|
|
|
FunctionPassManager &PM = jitstate->getPM(locked);
|
|
|
|
PM.add(new TargetData(*TM.getTargetData()));
|
|
|
|
|
|
|
|
// Turn the machine code intermediate representation into bytes in memory
|
|
|
|
// that may be executed.
|
2009-05-30 20:51:52 +00:00
|
|
|
if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) {
|
2009-02-18 08:31:02 +00:00
|
|
|
cerr << "Target does not support machine code emission!\n";
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize passes.
|
|
|
|
PM.doInitialization();
|
|
|
|
}
|
2009-01-23 19:27:28 +00:00
|
|
|
}
|
|
|
|
|
2003-09-05 18:42:01 +00:00
|
|
|
/// run - Start execution with the specified function and arguments.
|
2003-08-21 21:32:12 +00:00
|
|
|
///
|
2003-12-26 06:13:47 +00:00
|
|
|
GenericValue JIT::runFunction(Function *F,
|
|
|
|
const std::vector<GenericValue> &ArgValues) {
|
2004-08-15 23:29:50 +00:00
|
|
|
assert(F && "Function *F was null at entry to run()");
|
|
|
|
|
|
|
|
void *FPtr = getPointerToFunction(F);
|
2004-08-15 23:31:43 +00:00
|
|
|
assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
|
2004-08-15 23:39:59 +00:00
|
|
|
const FunctionType *FTy = F->getFunctionType();
|
2004-08-15 23:53:06 +00:00
|
|
|
const Type *RetTy = FTy->getReturnType();
|
2002-12-24 00:01:05 +00:00
|
|
|
|
2009-02-19 02:55:18 +00:00
|
|
|
assert((FTy->getNumParams() == ArgValues.size() ||
|
|
|
|
(FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
|
|
|
|
"Wrong number of arguments passed into function!");
|
2004-08-15 23:53:06 +00:00
|
|
|
assert(FTy->getNumParams() == ArgValues.size() &&
|
|
|
|
"This doesn't support passing arguments through varargs (yet)!");
|
|
|
|
|
2004-10-22 23:35:57 +00:00
|
|
|
// Handle some common cases first. These cases correspond to common `main'
|
2004-08-15 23:53:06 +00:00
|
|
|
// prototypes.
|
2007-08-08 16:19:57 +00:00
|
|
|
if (RetTy == Type::Int32Ty || RetTy == Type::VoidTy) {
|
2004-08-15 23:39:59 +00:00
|
|
|
switch (ArgValues.size()) {
|
|
|
|
case 3:
|
2007-08-08 16:19:57 +00:00
|
|
|
if (FTy->getParamType(0) == Type::Int32Ty &&
|
2004-08-15 23:39:59 +00:00
|
|
|
isa<PointerType>(FTy->getParamType(1)) &&
|
|
|
|
isa<PointerType>(FTy->getParamType(2))) {
|
|
|
|
int (*PF)(int, char **, const char **) =
|
2006-06-01 17:29:22 +00:00
|
|
|
(int(*)(int, char **, const char **))(intptr_t)FPtr;
|
2004-10-22 23:35:57 +00:00
|
|
|
|
2004-08-15 23:39:59 +00:00
|
|
|
// Call the function.
|
2004-08-15 23:53:06 +00:00
|
|
|
GenericValue rv;
|
2007-03-06 03:11:31 +00:00
|
|
|
rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
|
|
|
|
(char **)GVTOP(ArgValues[1]),
|
|
|
|
(const char **)GVTOP(ArgValues[2])));
|
2004-08-15 23:39:59 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
break;
|
2004-08-16 01:07:04 +00:00
|
|
|
case 2:
|
2007-08-08 16:19:57 +00:00
|
|
|
if (FTy->getParamType(0) == Type::Int32Ty &&
|
2004-08-16 01:07:04 +00:00
|
|
|
isa<PointerType>(FTy->getParamType(1))) {
|
2006-06-01 17:29:22 +00:00
|
|
|
int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
|
2004-10-22 23:35:57 +00:00
|
|
|
|
2004-08-16 01:07:04 +00:00
|
|
|
// Call the function.
|
|
|
|
GenericValue rv;
|
2007-03-06 03:11:31 +00:00
|
|
|
rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
|
|
|
|
(char **)GVTOP(ArgValues[1])));
|
2004-08-16 01:07:04 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
break;
|
2004-08-15 23:39:59 +00:00
|
|
|
case 1:
|
|
|
|
if (FTy->getNumParams() == 1 &&
|
2007-08-08 16:19:57 +00:00
|
|
|
FTy->getParamType(0) == Type::Int32Ty) {
|
2004-08-15 23:53:06 +00:00
|
|
|
GenericValue rv;
|
2006-06-01 17:29:22 +00:00
|
|
|
int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
|
2007-03-06 03:11:31 +00:00
|
|
|
rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
|
2004-08-15 23:39:59 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
break;
|
2004-08-15 23:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle cases where no arguments are passed first.
|
|
|
|
if (ArgValues.empty()) {
|
|
|
|
GenericValue rv;
|
|
|
|
switch (RetTy->getTypeID()) {
|
|
|
|
default: assert(0 && "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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
case Type::IntegerTyID: {
|
|
|
|
unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
|
|
|
|
if (BitWidth == 1)
|
2007-03-06 03:11:31 +00: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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
else if (BitWidth <= 8)
|
2007-03-06 03:11:31 +00: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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
else if (BitWidth <= 16)
|
2007-03-06 03:11:31 +00: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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
else if (BitWidth <= 32)
|
2007-03-06 03:11:31 +00: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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
else if (BitWidth <= 64)
|
2007-03-06 03:11:31 +00:00
|
|
|
rv.IntVal = APInt(BitWidth, ((int64_t(*)())(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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
else
|
|
|
|
assert(0 && "Integer types > 64 bits not supported");
|
2004-08-15 23:53:06 +00: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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
}
|
2004-08-15 23:53:06 +00:00
|
|
|
case Type::VoidTyID:
|
2007-03-06 03:11:31 +00:00
|
|
|
rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
|
2004-08-15 23:53:06 +00:00
|
|
|
return rv;
|
|
|
|
case Type::FloatTyID:
|
2006-06-01 17:29:22 +00:00
|
|
|
rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
|
2004-08-15 23:53:06 +00:00
|
|
|
return rv;
|
|
|
|
case Type::DoubleTyID:
|
2006-06-01 17:29:22 +00:00
|
|
|
rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
|
2004-08-15 23:34:48 +00:00
|
|
|
return rv;
|
2007-09-17 18:44:13 +00:00
|
|
|
case Type::X86_FP80TyID:
|
|
|
|
case Type::FP128TyID:
|
|
|
|
case Type::PPC_FP128TyID:
|
|
|
|
assert(0 && "long double not supported yet");
|
|
|
|
return rv;
|
2004-08-15 23:53:06 +00:00
|
|
|
case Type::PointerTyID:
|
2006-06-01 17:29:22 +00:00
|
|
|
return PTOGV(((void*(*)())(intptr_t)FPtr)());
|
2004-08-15 23:34:48 +00:00
|
|
|
}
|
2003-12-26 06:13:47 +00:00
|
|
|
}
|
2003-09-05 18:42:01 +00:00
|
|
|
|
2004-08-16 00:14:18 +00: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-01 04:13:31 +00:00
|
|
|
FunctionType *STy=FunctionType::get(RetTy, false);
|
2008-04-06 20:25:17 +00:00
|
|
|
Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
|
|
|
|
F->getParent());
|
2004-08-16 00:14:18 +00:00
|
|
|
|
|
|
|
// Insert a basic block.
|
2008-04-06 20:25:17 +00:00
|
|
|
BasicBlock *StubBB = BasicBlock::Create("", Stub);
|
2004-08-16 00:14:18 +00:00
|
|
|
|
|
|
|
// Convert all of the GenericValue arguments over to constants. Note that we
|
|
|
|
// currently don't support varargs.
|
2007-02-13 06:01:22 +00:00
|
|
|
SmallVector<Value*, 8> Args;
|
2004-08-16 00:14:18 +00:00
|
|
|
for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
|
|
|
|
Constant *C = 0;
|
|
|
|
const Type *ArgTy = FTy->getParamType(i);
|
|
|
|
const GenericValue &AV = ArgValues[i];
|
|
|
|
switch (ArgTy->getTypeID()) {
|
|
|
|
default: assert(0 && "Unknown argument type for function call!");
|
2008-04-20 00:41:09 +00:00
|
|
|
case Type::IntegerTyID:
|
|
|
|
C = ConstantInt::get(AV.IntVal);
|
|
|
|
break;
|
|
|
|
case Type::FloatTyID:
|
|
|
|
C = ConstantFP::get(APFloat(AV.FloatVal));
|
|
|
|
break;
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
C = ConstantFP::get(APFloat(AV.DoubleVal));
|
|
|
|
break;
|
2007-09-17 18:44:13 +00:00
|
|
|
case Type::PPC_FP128TyID:
|
|
|
|
case Type::X86_FP80TyID:
|
2008-04-20 00:41:09 +00:00
|
|
|
case Type::FP128TyID:
|
|
|
|
C = ConstantFP::get(APFloat(AV.IntVal));
|
|
|
|
break;
|
2004-08-16 00:14:18 +00:00
|
|
|
case Type::PointerTyID:
|
|
|
|
void *ArgPtr = GVTOP(AV);
|
2008-04-20 00:41:09 +00:00
|
|
|
if (sizeof(void*) == 4)
|
2006-12-31 05:51:36 +00:00
|
|
|
C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
|
2008-04-20 00:41:09 +00:00
|
|
|
else
|
2006-12-31 05:51:36 +00:00
|
|
|
C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
|
2006-12-12 01:17:41 +00:00
|
|
|
C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer
|
2004-08-16 00:14:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
Args.push_back(C);
|
|
|
|
}
|
|
|
|
|
2008-05-15 10:04:30 +00:00
|
|
|
CallInst *TheCall = CallInst::Create(F, Args.begin(), Args.end(),
|
|
|
|
"", StubBB);
|
2008-11-23 08:00:11 +00:00
|
|
|
TheCall->setCallingConv(F->getCallingConv());
|
2005-05-06 06:48:54 +00:00
|
|
|
TheCall->setTailCall();
|
2004-08-16 00:14:18 +00:00
|
|
|
if (TheCall->getType() != Type::VoidTy)
|
2008-05-15 10:04:30 +00:00
|
|
|
ReturnInst::Create(TheCall, StubBB); // Return result of the call.
|
2004-08-16 00:14:18 +00:00
|
|
|
else
|
2008-05-15 10:04:30 +00:00
|
|
|
ReturnInst::Create(StubBB); // Just return void.
|
2004-08-16 00:14:18 +00:00
|
|
|
|
|
|
|
// Finally, return the value returned by our nullary stub function.
|
|
|
|
return runFunction(Stub, std::vector<GenericValue>());
|
2002-12-24 00:01:05 +00:00
|
|
|
}
|
2003-12-20 01:46:27 +00:00
|
|
|
|
2009-06-25 02:04:04 +00: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JIT::NotifyFreeingMachineCode(const Function &F, void *OldPtr) {
|
|
|
|
MutexGuard locked(lock);
|
|
|
|
for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
|
|
|
|
EventListeners[I]->NotifyFreeingMachineCode(F, OldPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-20 01:46:27 +00: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-18 21:06:40 +00:00
|
|
|
void JIT::runJITOnFunction(Function *F, MachineCodeInfo *MCI) {
|
2005-07-12 15:51:55 +00:00
|
|
|
MutexGuard locked(lock);
|
2009-05-18 21:06:40 +00:00
|
|
|
|
2009-06-25 02:04:04 +00: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);
|
|
|
|
RegisterJITEventListener(&MCIL);
|
2009-05-18 21:06:40 +00:00
|
|
|
|
2009-02-06 21:25:08 +00:00
|
|
|
runJITOnFunctionUnlocked(F, locked);
|
2009-05-18 21:06:40 +00:00
|
|
|
|
2009-06-25 02:04:04 +00:00
|
|
|
UnregisterJITEventListener(&MCIL);
|
2009-02-06 21:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JIT::runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked) {
|
|
|
|
static bool isAlreadyCodeGenerating = false;
|
2007-08-13 20:08:16 +00:00
|
|
|
assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
|
2003-12-20 01:46:27 +00:00
|
|
|
|
|
|
|
// JIT the function
|
|
|
|
isAlreadyCodeGenerating = true;
|
2008-05-21 16:34:48 +00:00
|
|
|
jitstate->getPM(locked).run(*F);
|
2003-12-20 01:46:27 +00:00
|
|
|
isAlreadyCodeGenerating = false;
|
2003-12-20 03:36:47 +00:00
|
|
|
|
2009-02-18 08:31:02 +00:00
|
|
|
// If the function referred to another function that had not yet been
|
|
|
|
// read from bitcode, but we are jitting non-lazily, emit it now.
|
|
|
|
while (!jitstate->getPendingFunctions(locked).empty()) {
|
|
|
|
Function *PF = jitstate->getPendingFunctions(locked).back();
|
|
|
|
jitstate->getPendingFunctions(locked).pop_back();
|
|
|
|
|
|
|
|
// JIT the function
|
|
|
|
isAlreadyCodeGenerating = true;
|
|
|
|
jitstate->getPM(locked).run(*PF);
|
|
|
|
isAlreadyCodeGenerating = false;
|
|
|
|
|
|
|
|
// 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 03:36:47 +00:00
|
|
|
}
|
2009-02-18 08:31:02 +00:00
|
|
|
|
|
|
|
// If the JIT is configured to emit info so that dlsym can be used to
|
|
|
|
// rewrite stubs to external globals, do so now.
|
|
|
|
if (areDlsymStubsEnabled() && isLazyCompilationDisabled())
|
|
|
|
updateDlsymStubTable();
|
2003-12-20 01:46:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getPointerToFunction - This method is used to get the address of the
|
|
|
|
/// specified function, compiling it if neccesary.
|
|
|
|
///
|
|
|
|
void *JIT::getPointerToFunction(Function *F) {
|
2005-07-12 15:51:55 +00:00
|
|
|
|
2003-12-20 03:36:47 +00:00
|
|
|
if (void *Addr = getPointerToGlobalIfAvailable(F))
|
|
|
|
return Addr; // Check if function already code gen'd
|
2003-12-20 01:46:27 +00:00
|
|
|
|
2009-02-19 02:40:15 +00:00
|
|
|
MutexGuard locked(lock);
|
2009-06-12 14:11:08 +00:00
|
|
|
|
|
|
|
// Now that this thread owns the lock, check if another thread has already
|
|
|
|
// code gen'd the function.
|
|
|
|
if (void *Addr = getPointerToGlobalIfAvailable(F))
|
|
|
|
return Addr;
|
2009-02-19 02:40:15 +00:00
|
|
|
|
2006-08-16 01:24:12 +00:00
|
|
|
// Make sure we read in the function if it exists in this Module.
|
2007-07-05 17:07:56 +00:00
|
|
|
if (F->hasNotBeenReadFromBitcode()) {
|
2006-08-16 01:24:12 +00:00
|
|
|
// Determine the module provider this function is provided by.
|
|
|
|
Module *M = F->getParent();
|
|
|
|
ModuleProvider *MP = 0;
|
|
|
|
for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
|
|
|
|
if (Modules[i]->getModule() == M) {
|
|
|
|
MP = Modules[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(MP && "Function isn't in a module we know about!");
|
|
|
|
|
2006-07-07 17:18:09 +00:00
|
|
|
std::string ErrorMsg;
|
|
|
|
if (MP->materializeFunction(F, &ErrorMsg)) {
|
2006-12-07 20:04:42 +00:00
|
|
|
cerr << "Error reading function '" << F->getName()
|
2007-07-05 17:07:56 +00:00
|
|
|
<< "' from bitcode file: " << ErrorMsg << "\n";
|
2004-11-15 23:18:09 +00:00
|
|
|
abort();
|
|
|
|
}
|
2008-10-10 01:47:42 +00:00
|
|
|
|
2009-01-05 05:32:42 +00:00
|
|
|
// Now retry to get the address.
|
|
|
|
if (void *Addr = getPointerToGlobalIfAvailable(F))
|
|
|
|
return Addr;
|
2008-04-20 08:33:02 +00:00
|
|
|
}
|
2003-12-20 01:46:27 +00:00
|
|
|
|
2007-01-30 20:08:39 +00:00
|
|
|
if (F->isDeclaration()) {
|
Introduce new linkage types linkonce_odr, weak_odr, common_odr
and extern_weak_odr. These are the same as the non-odr versions,
except that they indicate that the global will only be overridden
by an *equivalent* global. In C, a function with weak linkage can
be overridden by a function which behaves completely differently.
This means that IP passes have to skip weak functions, since any
deductions made from the function definition might be wrong, since
the definition could be replaced by something completely different
at link time. This is not allowed in C++, thanks to the ODR
(One-Definition-Rule): if a function is replaced by another at
link-time, then the new function must be the same as the original
function. If a language knows that a function or other global can
only be overridden by an equivalent global, it can give it the
weak_odr linkage type, and the optimizers will understand that it
is alright to make deductions based on the function body. The
code generators on the other hand map weak and weak_odr linkage
to the same thing.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66339 91177308-0d34-0410-b5e6-96231b3b80d8
2009-03-07 15:45:40 +00:00
|
|
|
bool AbortOnFailure =
|
|
|
|
!areDlsymStubsEnabled() && !F->hasExternalWeakLinkage();
|
2009-01-05 05:32:42 +00:00
|
|
|
void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
|
2003-12-20 03:36:47 +00:00
|
|
|
addGlobalMapping(F, Addr);
|
|
|
|
return Addr;
|
|
|
|
}
|
2003-12-20 01:46:27 +00:00
|
|
|
|
2009-02-06 21:25:08 +00:00
|
|
|
runJITOnFunctionUnlocked(F, locked);
|
2003-12-20 03:36:47 +00:00
|
|
|
|
|
|
|
void *Addr = getPointerToGlobalIfAvailable(F);
|
2003-12-20 01:46:27 +00:00
|
|
|
assert(Addr && "Code generation didn't add function to GlobalAddress table!");
|
|
|
|
return Addr;
|
|
|
|
}
|
|
|
|
|
2003-12-20 03:36:47 +00: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 15:51:55 +00:00
|
|
|
MutexGuard locked(lock);
|
|
|
|
|
2003-12-20 03:36:47 +00:00
|
|
|
void *Ptr = getPointerToGlobalIfAvailable(GV);
|
|
|
|
if (Ptr) return Ptr;
|
|
|
|
|
|
|
|
// If the global is external, just remember the address.
|
2007-01-30 20:08:39 +00:00
|
|
|
if (GV->isDeclaration()) {
|
2007-07-30 20:02:02 +00:00
|
|
|
#if HAVE___DSO_HANDLE
|
2006-07-21 23:06:20 +00:00
|
|
|
if (GV->getName() == "__dso_handle")
|
|
|
|
return (void*)&__dso_handle;
|
2006-07-22 00:42:03 +00:00
|
|
|
#endif
|
2004-11-29 14:11:29 +00:00
|
|
|
Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
|
2009-03-04 19:10:38 +00:00
|
|
|
if (Ptr == 0 && !areDlsymStubsEnabled()) {
|
2006-12-07 20:04:42 +00:00
|
|
|
cerr << "Could not resolve external global address: "
|
|
|
|
<< GV->getName() << "\n";
|
2003-12-20 03:36:47 +00:00
|
|
|
abort();
|
|
|
|
}
|
2009-03-04 19:10:38 +00:00
|
|
|
addGlobalMapping(GV, Ptr);
|
2003-12-20 03:36:47 +00:00
|
|
|
} else {
|
2009-01-16 19:14:49 +00:00
|
|
|
// 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()) {
|
2008-12-09 07:31:49 +00:00
|
|
|
cerr << "Compilation of non-internal GlobalValue is disabled!\n";
|
2008-09-24 16:25:55 +00:00
|
|
|
abort();
|
|
|
|
}
|
2008-08-07 01:30:15 +00:00
|
|
|
// If the global hasn't been emitted to memory yet, allocate space and
|
|
|
|
// emit it into memory. It goes in the same array as the generated
|
|
|
|
// code, jump tables, etc.
|
2006-05-02 21:57:51 +00:00
|
|
|
const Type *GlobalType = GV->getType()->getElementType();
|
2009-05-09 07:06:46 +00:00
|
|
|
size_t S = getTargetData()->getTypeAllocSize(GlobalType);
|
2008-01-29 06:23:44 +00:00
|
|
|
size_t A = getTargetData()->getPreferredAlignment(GV);
|
2008-10-25 15:41:43 +00:00
|
|
|
if (GV->isThreadLocal()) {
|
|
|
|
MutexGuard locked(lock);
|
|
|
|
Ptr = TJI.allocateThreadLocalMemory(S);
|
2008-11-04 09:30:48 +00:00
|
|
|
} else if (TJI.allocateSeparateGVMemory()) {
|
|
|
|
if (A <= 8) {
|
|
|
|
Ptr = malloc(S);
|
|
|
|
} else {
|
|
|
|
// Allocate S+A bytes of memory, then use an aligned pointer within that
|
|
|
|
// space.
|
|
|
|
Ptr = malloc(S+A);
|
|
|
|
unsigned MisAligned = ((intptr_t)Ptr & (A-1));
|
|
|
|
Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
|
|
|
|
}
|
2008-10-25 15:41:43 +00:00
|
|
|
} else {
|
2009-05-30 20:51:52 +00:00
|
|
|
Ptr = JCE->allocateSpace(S, A);
|
2008-10-25 15:41:43 +00:00
|
|
|
}
|
2008-08-07 01:30:15 +00:00
|
|
|
addGlobalMapping(GV, Ptr);
|
|
|
|
EmitGlobalVariable(GV);
|
2003-12-20 03:36:47 +00:00
|
|
|
}
|
|
|
|
return Ptr;
|
|
|
|
}
|
|
|
|
|
2003-12-20 01:46:27 +00: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 03:36:47 +00:00
|
|
|
void *OldAddr = getPointerToGlobalIfAvailable(F);
|
2003-12-20 01:46:27 +00:00
|
|
|
|
2003-12-20 03:36:47 +00:00
|
|
|
// If it's not already compiled there is no reason to patch it up.
|
|
|
|
if (OldAddr == 0) { return getPointerToFunction(F); }
|
2003-12-20 01:46:27 +00:00
|
|
|
|
2003-12-20 03:36:47 +00:00
|
|
|
// Delete the old function mapping.
|
|
|
|
addGlobalMapping(F, 0);
|
|
|
|
|
|
|
|
// Recodegen the function
|
2003-12-20 01:46:27 +00:00
|
|
|
runJITOnFunction(F);
|
2003-12-20 03:36:47 +00:00
|
|
|
|
|
|
|
// Update state, forward the old function to the new function.
|
|
|
|
void *Addr = getPointerToGlobalIfAvailable(F);
|
2003-12-20 01:46:27 +00:00
|
|
|
assert(Addr && "Code generation didn't add function to GlobalAddress table!");
|
|
|
|
TJI.replaceMachineCodeForFunction(OldAddr, Addr);
|
|
|
|
return Addr;
|
|
|
|
}
|
2004-11-07 23:58:46 +00:00
|
|
|
|
2008-10-25 15:41:43 +00: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) {
|
|
|
|
const Type *ElTy = GV->getType()->getElementType();
|
2009-05-09 07:06:46 +00:00
|
|
|
size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy);
|
2008-10-25 15:41:43 +00:00
|
|
|
if (GV->isThreadLocal()) {
|
|
|
|
MutexGuard locked(lock);
|
|
|
|
return TJI.allocateThreadLocalMemory(GVSize);
|
|
|
|
} else {
|
|
|
|
return new char[GVSize];
|
|
|
|
}
|
|
|
|
}
|
2009-02-18 08:31:02 +00:00
|
|
|
|
|
|
|
void JIT::addPendingFunction(Function *F) {
|
|
|
|
MutexGuard locked(lock);
|
|
|
|
jitstate->getPendingFunctions(locked).push_back(F);
|
|
|
|
}
|
2009-06-25 02:04:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
JITEventListener::~JITEventListener() {}
|