//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "jit"
+#include "llvm/ExecutionEngine/ExecutionEngine.h"
+
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Module.h"
#include "llvm/ModuleProvider.h"
-#include "llvm/ADT/Statistic.h"
-#include "llvm/Config/alloca.h"
-#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/GenericValue.h"
+#include "llvm/ADT/Statistic.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MutexGuard.h"
+#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/System/DynamicLibrary.h"
#include "llvm/System/Host.h"
void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
MutexGuard locked(lock);
- DOUT << "JIT: Map \'" << GV->getNameStart() << "\' to [" << Addr << "]\n";
+ DEBUG(errs() << "JIT: Map \'" << GV->getName()
+ << "\' to [" << Addr << "]\n";);
void *&CurVal = state.getGlobalAddressMap(locked)[GV];
assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
CurVal = Addr;
// If we are using the reverse mapping, add it too
if (!state.getGlobalAddressReverseMap(locked).empty()) {
- const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
+ AssertingVH<const GlobalValue> &V =
+ state.getGlobalAddressReverseMap(locked)[Addr];
assert((V == 0 || GV == 0) && "GlobalMapping already established!");
V = GV;
}
MutexGuard locked(lock);
for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) {
- state.getGlobalAddressMap(locked).erase(FI);
- state.getGlobalAddressReverseMap(locked).erase(FI);
+ state.getGlobalAddressMap(locked).erase(&*FI);
+ state.getGlobalAddressReverseMap(locked).erase(&*FI);
}
for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
GI != GE; ++GI) {
- state.getGlobalAddressMap(locked).erase(GI);
- state.getGlobalAddressReverseMap(locked).erase(GI);
+ state.getGlobalAddressMap(locked).erase(&*GI);
+ state.getGlobalAddressReverseMap(locked).erase(&*GI);
}
}
void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
MutexGuard locked(lock);
- std::map<const GlobalValue*, void *> &Map = state.getGlobalAddressMap(locked);
+ std::map<AssertingVH<const GlobalValue>, void *> &Map =
+ state.getGlobalAddressMap(locked);
// Deleting from the mapping?
if (Addr == 0) {
- std::map<const GlobalValue*, void *>::iterator I = Map.find(GV);
+ std::map<AssertingVH<const GlobalValue>, void *>::iterator I = Map.find(GV);
void *OldVal;
if (I == Map.end())
OldVal = 0;
}
if (!state.getGlobalAddressReverseMap(locked).empty())
- state.getGlobalAddressReverseMap(locked).erase(Addr);
+ state.getGlobalAddressReverseMap(locked).erase(OldVal);
return OldVal;
}
// If we are using the reverse mapping, add it too
if (!state.getGlobalAddressReverseMap(locked).empty()) {
- const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
+ AssertingVH<const GlobalValue> &V =
+ state.getGlobalAddressReverseMap(locked)[Addr];
assert((V == 0 || GV == 0) && "GlobalMapping already established!");
V = GV;
}
void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
MutexGuard locked(lock);
- std::map<const GlobalValue*, void*>::iterator I =
- state.getGlobalAddressMap(locked).find(GV);
+ std::map<AssertingVH<const GlobalValue>, void*>::iterator I =
+ state.getGlobalAddressMap(locked).find(GV);
return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
}
// If we haven't computed the reverse mapping yet, do so first.
if (state.getGlobalAddressReverseMap(locked).empty()) {
- for (std::map<const GlobalValue*, void *>::iterator
+ for (std::map<AssertingVH<const GlobalValue>, void *>::iterator
I = state.getGlobalAddressMap(locked).begin(),
E = state.getGlobalAddressMap(locked).end(); I != E; ++I)
state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second,
I->first));
}
- std::map<void *, const GlobalValue*>::iterator I =
+ std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
state.getGlobalAddressReverseMap(locked).find(Addr);
return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
}
// CreateArgv - Turn a vector of strings into a nice argv style array of
// pointers to null terminated strings.
//
-static void *CreateArgv(ExecutionEngine *EE,
+static void *CreateArgv(LLVMContext &C, ExecutionEngine *EE,
const std::vector<std::string> &InputArgv) {
unsigned PtrSize = EE->getTargetData()->getPointerSize();
char *Result = new char[(InputArgv.size()+1)*PtrSize];
- DOUT << "JIT: ARGV = " << (void*)Result << "\n";
- const Type *SBytePtr = PointerType::getUnqual(Type::Int8Ty);
+ DEBUG(errs() << "JIT: ARGV = " << (void*)Result << "\n");
+ const Type *SBytePtr = PointerType::getUnqual(Type::getInt8Ty(C));
for (unsigned i = 0; i != InputArgv.size(); ++i) {
unsigned Size = InputArgv[i].size()+1;
char *Dest = new char[Size];
- DOUT << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n";
+ DEBUG(errs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n");
std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
Dest[Size-1] = 0;
/// runStaticConstructorsDestructors - This method is used to execute all of
/// the static constructors or destructors for a module, depending on the
/// value of isDtors.
-void ExecutionEngine::runStaticConstructorsDestructors(Module *module, bool isDtors) {
+void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
+ bool isDtors) {
const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
// Execute global ctors/dtors for each module in the program.
unsigned NumArgs = Fn->getFunctionType()->getNumParams();
const FunctionType *FTy = Fn->getFunctionType();
const Type* PPInt8Ty =
- PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty));
+ PointerType::getUnqual(PointerType::getUnqual(
+ Type::getInt8Ty(Fn->getContext())));
switch (NumArgs) {
case 3:
if (FTy->getParamType(2) != PPInt8Ty) {
}
// FALLS THROUGH
case 1:
- if (FTy->getParamType(0) != Type::Int32Ty) {
+ if (FTy->getParamType(0) != Type::getInt32Ty(Fn->getContext())) {
llvm_report_error("Invalid type for first argument of main() supplied");
}
// FALLS THROUGH
case 0:
if (!isa<IntegerType>(FTy->getReturnType()) &&
- FTy->getReturnType() != Type::VoidTy) {
+ FTy->getReturnType() != Type::getVoidTy(FTy->getContext())) {
llvm_report_error("Invalid return type of main() supplied");
}
break;
if (NumArgs) {
GVArgs.push_back(GVArgc); // Arg #0 = argc.
if (NumArgs > 1) {
- GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
+ // Arg #1 = argv.
+ GVArgs.push_back(PTOGV(CreateArgv(Fn->getContext(), this, argv)));
assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
"argv[0] was null after CreateArgv");
if (NumArgs > 2) {
std::vector<std::string> EnvVars;
for (unsigned i = 0; envp[i]; ++i)
EnvVars.push_back(envp[i]);
- GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
+ // Arg #2 = envp.
+ GVArgs.push_back(PTOGV(CreateArgv(Fn->getContext(), this, EnvVars)));
}
}
}
// create, we assume they only want the JIT, and we fail if they only want
// the interpreter.
if (JMM) {
- if (WhichEngine & EngineKind::JIT) {
+ if (WhichEngine & EngineKind::JIT)
WhichEngine = EngineKind::JIT;
- } else {
+ else {
*ErrorStr = "Cannot create an interpreter with a memory manager.";
+ return 0;
}
}
- ExecutionEngine *EE = 0;
-
// Unless the interpreter was explicitly selected or the JIT is not linked,
// try making a JIT.
- if (WhichEngine & EngineKind::JIT && ExecutionEngine::JITCtor) {
- EE = ExecutionEngine::JITCtor(MP, ErrorStr, JMM, OptLevel,
- AllocateGVsWithCode);
+ if (WhichEngine & EngineKind::JIT) {
+ if (ExecutionEngine::JITCtor) {
+ ExecutionEngine *EE =
+ ExecutionEngine::JITCtor(MP, ErrorStr, JMM, OptLevel,
+ AllocateGVsWithCode);
+ if (EE) return EE;
+ } else {
+ *ErrorStr = "JIT has not been linked in.";
+ return 0;
+ }
}
// If we can't make a JIT and we didn't request one specifically, try making
// an interpreter instead.
- if (WhichEngine & EngineKind::Interpreter && EE == 0 &&
- ExecutionEngine::InterpCtor) {
- EE = ExecutionEngine::InterpCtor(MP, ErrorStr);
+ if (WhichEngine & EngineKind::Interpreter) {
+ if (ExecutionEngine::InterpCtor)
+ return ExecutionEngine::InterpCtor(MP, ErrorStr);
+ *ErrorStr = "Interpreter has not been linked in.";
+ return 0;
}
-
- return EE;
+
+ return 0;
}
/// getPointerToGlobal - This returns the address of the specified global
}
case Instruction::UIToFP: {
GenericValue GV = getConstantValue(Op0);
- if (CE->getType() == Type::FloatTy)
+ if (CE->getType() == Type::getFloatTy(CE->getContext()))
GV.FloatVal = float(GV.IntVal.roundToDouble());
- else if (CE->getType() == Type::DoubleTy)
+ else if (CE->getType() == Type::getDoubleTy(CE->getContext()))
GV.DoubleVal = GV.IntVal.roundToDouble();
- else if (CE->getType() == Type::X86_FP80Ty) {
+ else if (CE->getType() == Type::getX86_FP80Ty(Op0->getContext())) {
const uint64_t zero[] = {0, 0};
APFloat apf = APFloat(APInt(80, 2, zero));
(void)apf.convertFromAPInt(GV.IntVal,
}
case Instruction::SIToFP: {
GenericValue GV = getConstantValue(Op0);
- if (CE->getType() == Type::FloatTy)
+ if (CE->getType() == Type::getFloatTy(CE->getContext()))
GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
- else if (CE->getType() == Type::DoubleTy)
+ else if (CE->getType() == Type::getDoubleTy(CE->getContext()))
GV.DoubleVal = GV.IntVal.signedRoundToDouble();
- else if (CE->getType() == Type::X86_FP80Ty) {
+ else if (CE->getType() == Type::getX86_FP80Ty(CE->getContext())) {
const uint64_t zero[] = { 0, 0};
APFloat apf = APFloat(APInt(80, 2, zero));
(void)apf.convertFromAPInt(GV.IntVal,
case Instruction::FPToSI: {
GenericValue GV = getConstantValue(Op0);
uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
- if (Op0->getType() == Type::FloatTy)
+ if (Op0->getType() == Type::getFloatTy(Op0->getContext()))
GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
- else if (Op0->getType() == Type::DoubleTy)
+ else if (Op0->getType() == Type::getDoubleTy(Op0->getContext()))
GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
- else if (Op0->getType() == Type::X86_FP80Ty) {
+ else if (Op0->getType() == Type::getX86_FP80Ty(Op0->getContext())) {
APFloat apf = APFloat(GV.IntVal);
uint64_t v;
bool ignored;
default: llvm_unreachable("Invalid bitcast operand");
case Type::IntegerTyID:
assert(DestTy->isFloatingPoint() && "invalid bitcast");
- if (DestTy == Type::FloatTy)
+ if (DestTy == Type::getFloatTy(Op0->getContext()))
GV.FloatVal = GV.IntVal.bitsToFloat();
- else if (DestTy == Type::DoubleTy)
+ else if (DestTy == Type::getDoubleTy(DestTy->getContext()))
GV.DoubleVal = GV.IntVal.bitsToDouble();
break;
case Type::FloatTyID:
- assert(DestTy == Type::Int32Ty && "Invalid bitcast");
+ assert(DestTy == Type::getInt32Ty(DestTy->getContext()) &&
+ "Invalid bitcast");
GV.IntVal.floatToBits(GV.FloatVal);
break;
case Type::DoubleTyID:
- assert(DestTy == Type::Int64Ty && "Invalid bitcast");
+ assert(DestTy == Type::getInt64Ty(DestTy->getContext()) &&
+ "Invalid bitcast");
GV.IntVal.doubleToBits(GV.DoubleVal);
break;
case Type::PointerTyID:
*((PointerTy*)Ptr) = Val.PointerVal;
break;
default:
- cerr << "Cannot store value of type " << *Ty << "!\n";
+ errs() << "Cannot store value of type " << *Ty << "!\n";
}
if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian())
const Type *Ty) {
const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty);
- if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian()) {
- // Host and target are different endian - reverse copy the stored
- // bytes into a buffer, and load from that.
- uint8_t *Src = (uint8_t*)Ptr;
- uint8_t *Buf = (uint8_t*)alloca(LoadBytes);
- std::reverse_copy(Src, Src + LoadBytes, Buf);
- Ptr = (GenericValue*)Buf;
- }
-
switch (Ty->getTypeID()) {
case Type::IntegerTyID:
// An APInt with all words initially zero.
// specified memory location...
//
void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
- DOUT << "JIT: Initializing " << Addr << " ";
+ DEBUG(errs() << "JIT: Initializing " << Addr << " ");
DEBUG(Init->dump());
if (isa<UndefValue>(Init)) {
return;
return;
}
- cerr << "Bad Type: " << *Init->getType() << "\n";
+ errs() << "Bad Type: " << *Init->getType() << "\n";
llvm_unreachable("Unknown constant type to initialize memory with!");
}