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
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and 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
|
|
|
|
// execution of LLVM bytecode in an efficient manner.
|
2002-12-24 08:01:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-12-20 09:46:27 +08:00
|
|
|
#include "JIT.h"
|
2004-08-16 08:14:18 +08:00
|
|
|
#include "llvm/Constants.h"
|
2003-12-20 11:36:47 +08:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2003-12-20 09:46:27 +08:00
|
|
|
#include "llvm/Function.h"
|
2003-12-20 11:36:47 +08:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2004-08-16 08:14:18 +08:00
|
|
|
#include "llvm/Instructions.h"
|
2003-10-17 05:19:34 +08:00
|
|
|
#include "llvm/ModuleProvider.h"
|
2003-12-20 09:46:27 +08:00
|
|
|
#include "llvm/CodeGen/MachineCodeEmitter.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2003-09-06 03:39:22 +08:00
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
2006-05-09 06:00:52 +08:00
|
|
|
#include "llvm/Support/MutexGuard.h"
|
2004-11-29 22:11:29 +08:00
|
|
|
#include "llvm/System/DynamicLibrary.h"
|
2006-05-12 14:33:49 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2002-12-24 08:01:05 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2003-12-20 09:46:27 +08:00
|
|
|
#include "llvm/Target/TargetJITInfo.h"
|
2003-12-08 16:06:28 +08:00
|
|
|
using namespace llvm;
|
2003-05-28 05:40:39 +08:00
|
|
|
|
2006-07-23 00:59:38 +08:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <AvailabilityMacros.h>
|
2006-12-18 05:04:02 +08:00
|
|
|
#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))
|
2006-07-22 07:06:20 +08:00
|
|
|
// __dso_handle is resolved by Mac OS X dynamic linker.
|
|
|
|
extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
|
|
|
|
#endif
|
2006-07-23 00:59:38 +08:00
|
|
|
#endif
|
2006-07-22 07:06:20 +08:00
|
|
|
|
2006-03-22 14:07:50 +08:00
|
|
|
static struct RegisterJIT {
|
|
|
|
RegisterJIT() { JIT::Register(); }
|
|
|
|
} JITRegistrator;
|
|
|
|
|
2006-03-24 10:53:49 +08:00
|
|
|
namespace llvm {
|
|
|
|
void LinkInJIT() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-20 09:46:27 +08:00
|
|
|
JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
|
2007-04-21 06:40:05 +08:00
|
|
|
: ExecutionEngine(MP), TM(tm), TJI(tji), jitstate(MP) {
|
2002-12-24 08:01:05 +08:00
|
|
|
setTargetData(TM.getTargetData());
|
2003-05-28 05:40:39 +08:00
|
|
|
|
|
|
|
// Initialize MCE
|
2003-06-02 11:23:16 +08:00
|
|
|
MCE = createEmitter(*this);
|
2005-04-22 06:55:34 +08:00
|
|
|
|
2004-04-15 01:45:52 +08:00
|
|
|
// Add target data
|
2005-07-12 23:51:55 +08:00
|
|
|
MutexGuard locked(lock);
|
2007-04-21 06:40:05 +08:00
|
|
|
FunctionPassManager &PM = jitstate.getPM(locked);
|
2006-05-05 05:18:40 +08:00
|
|
|
PM.add(new TargetData(*TM.getTargetData()));
|
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.
|
2006-09-04 12:14:57 +08:00
|
|
|
if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
|
2006-12-08 04:04:42 +08:00
|
|
|
cerr << "Target does not support machine code emission!\n";
|
2003-12-20 09:46:27 +08:00
|
|
|
abort();
|
|
|
|
}
|
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() {
|
|
|
|
delete MCE;
|
|
|
|
delete &TM;
|
|
|
|
}
|
|
|
|
|
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");
|
2004-08-16 07:39:59 +08:00
|
|
|
const FunctionType *FTy = F->getFunctionType();
|
2004-08-16 07:53:06 +08:00
|
|
|
const Type *RetTy = FTy->getReturnType();
|
2002-12-24 08:01:05 +08:00
|
|
|
|
2004-08-16 07:53:06 +08:00
|
|
|
assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) &&
|
|
|
|
"Too many arguments passed into function!");
|
|
|
|
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.
|
2006-12-31 13:51:36 +08:00
|
|
|
if (RetTy == Type::Int32Ty || RetTy == Type::Int32Ty || RetTy == Type::VoidTy) {
|
2004-08-16 07:39:59 +08:00
|
|
|
switch (ArgValues.size()) {
|
|
|
|
case 3:
|
2006-12-31 13:51:36 +08:00
|
|
|
if ((FTy->getParamType(0) == Type::Int32Ty ||
|
|
|
|
FTy->getParamType(0) == Type::Int32Ty) &&
|
2004-08-16 07:39:59 +08:00
|
|
|
isa<PointerType>(FTy->getParamType(1)) &&
|
|
|
|
isa<PointerType>(FTy->getParamType(2))) {
|
|
|
|
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;
|
2007-03-06 11:11:31 +08:00
|
|
|
rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
|
|
|
|
(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:
|
2006-12-31 13:51:36 +08:00
|
|
|
if ((FTy->getParamType(0) == Type::Int32Ty ||
|
|
|
|
FTy->getParamType(0) == Type::Int32Ty) &&
|
2004-08-16 09:07:04 +08:00
|
|
|
isa<PointerType>(FTy->getParamType(1))) {
|
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;
|
2007-03-06 11:11:31 +08:00
|
|
|
rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
|
|
|
|
(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:
|
|
|
|
if (FTy->getNumParams() == 1 &&
|
2006-12-31 13:51:36 +08:00
|
|
|
(FTy->getParamType(0) == Type::Int32Ty ||
|
|
|
|
FTy->getParamType(0) == Type::Int32Ty)) {
|
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;
|
|
|
|
}
|
|
|
|
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()) {
|
|
|
|
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.
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)());
|
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
|
|
|
|
assert(0 && "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;
|
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.
|
|
|
|
FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
|
|
|
|
Function *Stub = new Function(STy, Function::InternalLinkage, "",
|
|
|
|
F->getParent());
|
|
|
|
|
|
|
|
// Insert a basic block.
|
|
|
|
BasicBlock *StubBB = new BasicBlock("", Stub);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
const Type *ArgTy = FTy->getParamType(i);
|
|
|
|
const GenericValue &AV = ArgValues[i];
|
|
|
|
switch (ArgTy->getTypeID()) {
|
|
|
|
default: assert(0 && "Unknown argument type for function call!");
|
2007-03-06 11:11:31 +08:00
|
|
|
case Type::IntegerTyID: C = ConstantInt::get(AV.IntVal); break;
|
|
|
|
case Type::FloatTyID: C = ConstantFP ::get(ArgTy, AV.FloatVal); break;
|
|
|
|
case Type::DoubleTyID: C = ConstantFP ::get(ArgTy, AV.DoubleVal); break;
|
2004-08-16 08:14:18 +08:00
|
|
|
case Type::PointerTyID:
|
|
|
|
void *ArgPtr = GVTOP(AV);
|
|
|
|
if (sizeof(void*) == 4) {
|
2006-12-31 13:51:36 +08:00
|
|
|
C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
|
2004-08-16 08:14:18 +08:00
|
|
|
} else {
|
2006-12-31 13:51:36 +08:00
|
|
|
C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
|
2004-08-16 08:14:18 +08:00
|
|
|
}
|
2006-12-12 09:17:41 +08:00
|
|
|
C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer
|
2004-08-16 08:14:18 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
Args.push_back(C);
|
|
|
|
}
|
|
|
|
|
2007-02-13 14:01:22 +08:00
|
|
|
CallInst *TheCall = new CallInst(F, &Args[0], Args.size(), "", StubBB);
|
2005-05-06 14:48:54 +08:00
|
|
|
TheCall->setTailCall();
|
2004-08-16 08:14:18 +08:00
|
|
|
if (TheCall->getType() != Type::VoidTy)
|
|
|
|
new ReturnInst(TheCall, StubBB); // Return result of the call.
|
|
|
|
else
|
|
|
|
new ReturnInst(StubBB); // Just return void.
|
|
|
|
|
|
|
|
// Finally, return the value returned by our nullary stub function.
|
|
|
|
return runFunction(Stub, std::vector<GenericValue>());
|
2002-12-24 08:01:05 +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.
|
|
|
|
///
|
|
|
|
void JIT::runJITOnFunction(Function *F) {
|
|
|
|
static bool isAlreadyCodeGenerating = false;
|
|
|
|
assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-07-12 23:51:55 +08:00
|
|
|
MutexGuard locked(lock);
|
2003-12-20 09:46:27 +08:00
|
|
|
|
|
|
|
// JIT the function
|
|
|
|
isAlreadyCodeGenerating = true;
|
2007-04-21 06:40:05 +08:00
|
|
|
jitstate.getPM(locked).run(*F);
|
2003-12-20 09:46:27 +08:00
|
|
|
isAlreadyCodeGenerating = false;
|
2003-12-20 11:36:47 +08:00
|
|
|
|
|
|
|
// If the function referred to a global variable that had not yet been
|
|
|
|
// emitted, it allocates memory for the global, but doesn't emit it yet. Emit
|
|
|
|
// all of these globals now.
|
2007-04-21 06:40:05 +08:00
|
|
|
while (!jitstate.getPendingGlobals(locked).empty()) {
|
|
|
|
const GlobalVariable *GV = jitstate.getPendingGlobals(locked).back();
|
|
|
|
jitstate.getPendingGlobals(locked).pop_back();
|
2003-12-20 11:36:47 +08:00
|
|
|
EmitGlobalVariable(GV);
|
|
|
|
}
|
2003-12-20 09:46:27 +08: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 23:51:55 +08:00
|
|
|
MutexGuard locked(lock);
|
|
|
|
|
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
|
|
|
|
2006-08-16 09:24:12 +08:00
|
|
|
// Make sure we read in the function if it exists in this Module.
|
2006-07-08 01:18:09 +08:00
|
|
|
if (F->hasNotBeenReadFromBytecode()) {
|
2006-08-16 09:24:12 +08: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-08 01:18:09 +08:00
|
|
|
std::string ErrorMsg;
|
|
|
|
if (MP->materializeFunction(F, &ErrorMsg)) {
|
2006-12-08 04:04:42 +08:00
|
|
|
cerr << "Error reading function '" << F->getName()
|
|
|
|
<< "' from bytecode file: " << ErrorMsg << "\n";
|
2004-11-16 07:18:09 +08:00
|
|
|
abort();
|
|
|
|
}
|
2006-07-08 01:18:09 +08:00
|
|
|
}
|
2003-12-20 09:46:27 +08:00
|
|
|
|
2007-01-31 04:08:39 +08:00
|
|
|
if (F->isDeclaration()) {
|
2003-12-20 11:36:47 +08:00
|
|
|
void *Addr = getPointerToNamedFunction(F->getName());
|
|
|
|
addGlobalMapping(F, Addr);
|
|
|
|
return Addr;
|
|
|
|
}
|
2003-12-20 09:46:27 +08:00
|
|
|
|
|
|
|
runJITOnFunction(F);
|
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;
|
|
|
|
}
|
|
|
|
|
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.
|
2007-01-31 04:08:39 +08:00
|
|
|
if (GV->isDeclaration()) {
|
2006-12-18 05:04:02 +08:00
|
|
|
#if defined(__APPLE__) && 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))
|
2006-09-01 15:09:56 +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.
|
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
|
2004-11-29 22:11:29 +08:00
|
|
|
Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
|
2003-12-20 11:36:47 +08:00
|
|
|
if (Ptr == 0) {
|
2006-12-08 04:04:42 +08:00
|
|
|
cerr << "Could not resolve external global address: "
|
|
|
|
<< GV->getName() << "\n";
|
2003-12-20 11:36:47 +08:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If the global hasn't been emitted to memory yet, allocate space. We will
|
|
|
|
// actually initialize the global after current function has finished
|
|
|
|
// compilation.
|
2006-05-03 05:57:51 +08:00
|
|
|
const Type *GlobalType = GV->getType()->getElementType();
|
2006-05-03 09:29:57 +08:00
|
|
|
size_t S = getTargetData()->getTypeSize(GlobalType);
|
2007-02-14 13:52:17 +08:00
|
|
|
size_t A = getTargetData()->getPrefTypeAlignment(GlobalType);
|
2006-05-03 05:57:51 +08:00
|
|
|
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));
|
2006-07-12 08:31:47 +08:00
|
|
|
Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
|
2006-05-03 05:57:51 +08:00
|
|
|
}
|
2007-04-21 06:40:05 +08:00
|
|
|
jitstate.getPendingGlobals(locked).push_back(GV);
|
2003-12-20 11:36:47 +08:00
|
|
|
}
|
|
|
|
addGlobalMapping(GV, Ptr);
|
|
|
|
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
|
|
|
|