ExecutionEngine::ExecutionEngine(ModuleProvider *P) : LazyFunctionCreator(0) {
LazyCompilationDisabled = false;
+ GVCompilationDisabled = false;
SymbolSearchingDisabled = false;
Modules.push_back(P);
assert(P && "ModuleProvider is null?");
delete Modules[i];
}
+char* ExecutionEngine::getMemoryForGV(const GlobalVariable* GV) {
+ const Type *ElTy = GV->getType()->getElementType();
+ size_t GVSize = (size_t)getTargetData()->getABITypeSize(ElTy);
+ return new char[GVSize];
+}
+
/// removeModuleProvider - Remove a ModuleProvider from the list of modules.
/// Release module from ModuleProvider.
Module* ExecutionEngine::removeModuleProvider(ModuleProvider *P,
/// existing data in memory.
void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
MutexGuard locked(lock);
-
+
+ DOUT << "JIT: Map \'" << GV->getNameStart() << "\' to [" << Addr << "]\n";
void *&CurVal = state.getGlobalAddressMap(locked)[GV];
assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
CurVal = Addr;
unsigned PtrSize = EE->getTargetData()->getPointerSize();
char *Result = new char[(InputArgv.size()+1)*PtrSize];
- DOUT << "ARGV = " << (void*)Result << "\n";
+ DOUT << "JIT: ARGV = " << (void*)Result << "\n";
const Type *SBytePtr = PointerType::getUnqual(Type::Int8Ty);
for (unsigned i = 0; i != InputArgv.size(); ++i) {
unsigned Size = InputArgv[i].size()+1;
char *Dest = new char[Size];
- DOUT << "ARGV[" << i << "] = " << (void*)Dest << "\n";
+ DOUT << "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 program, depending on the
+/// the static constructors or destructors for a module, depending on the
/// value of isDtors.
-void ExecutionEngine::runStaticConstructorsDestructors(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.
- for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
- GlobalVariable *GV = Modules[m]->getModule()->getNamedGlobal(Name);
-
- // If this global has internal linkage, or if it has a use, then it must be
- // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
- // this is the case, don't execute any of the global ctors, __main will do
- // it.
- if (!GV || GV->isDeclaration() || GV->hasInternalLinkage()) continue;
- // Should be an array of '{ int, void ()* }' structs. The first value is
- // the init priority, which we ignore.
- ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
- if (!InitList) continue;
- for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
- if (ConstantStruct *CS =
- dyn_cast<ConstantStruct>(InitList->getOperand(i))) {
- if (CS->getNumOperands() != 2) break; // Not array of 2-element structs.
-
- Constant *FP = CS->getOperand(1);
- if (FP->isNullValue())
- break; // Found a null terminator, exit.
-
- if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
- if (CE->isCast())
- FP = CE->getOperand(0);
- if (Function *F = dyn_cast<Function>(FP)) {
- // Execute the ctor/dtor function!
- runFunction(F, std::vector<GenericValue>());
- }
- }
- }
+ GlobalVariable *GV = module->getNamedGlobal(Name);
+
+ // If this global has internal linkage, or if it has a use, then it must be
+ // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
+ // this is the case, don't execute any of the global ctors, __main will do
+ // it.
+ if (!GV || GV->isDeclaration() || GV->hasInternalLinkage()) return;
+
+ // Should be an array of '{ int, void ()* }' structs. The first value is
+ // the init priority, which we ignore.
+ ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
+ if (!InitList) return;
+ for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
+ if (ConstantStruct *CS =
+ dyn_cast<ConstantStruct>(InitList->getOperand(i))) {
+ if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
+
+ Constant *FP = CS->getOperand(1);
+ if (FP->isNullValue())
+ break; // Found a null terminator, exit.
+
+ if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
+ if (CE->isCast())
+ FP = CE->getOperand(0);
+ if (Function *F = dyn_cast<Function>(FP)) {
+ // Execute the ctor/dtor function!
+ runFunction(F, std::vector<GenericValue>());
+ }
+ }
}
+/// runStaticConstructorsDestructors - This method is used to execute all of
+/// the static constructors or destructors for a program, depending on the
+/// value of isDtors.
+void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
+ // Execute global ctors/dtors for each module in the program.
+ for (unsigned m = 0, e = Modules.size(); m != e; ++m)
+ runStaticConstructorsDestructors(Modules[m]->getModule(), isDtors);
+}
+
+#ifndef NDEBUG
/// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
unsigned PtrSize = EE->getTargetData()->getPointerSize();
return false;
return true;
}
+#endif
/// runFunctionAsMain - This is a helper function which wraps runFunction to
/// handle the common task of starting up main with the specified argc, argv,
///
ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
bool ForceInterpreter,
- std::string *ErrorStr) {
+ std::string *ErrorStr,
+ bool Fast) {
ExecutionEngine *EE = 0;
// Make sure we can resolve symbols in the program as well. The zero arg
// Unless the interpreter was explicitly selected, try making a JIT.
if (!ForceInterpreter && JITCtor)
- EE = JITCtor(MP, ErrorStr);
+ EE = JITCtor(MP, ErrorStr, Fast);
// If we can't make a JIT, make an interpreter instead.
if (EE == 0 && InterpCtor)
- EE = InterpCtor(MP, ErrorStr);
+ EE = InterpCtor(MP, ErrorStr, Fast);
return EE;
}
(void)apf.convertFromAPInt(GV.IntVal,
false,
APFloat::rmNearestTiesToEven);
- GV.IntVal = apf.convertToAPInt();
+ GV.IntVal = apf.bitcastToAPInt();
}
return GV;
}
(void)apf.convertFromAPInt(GV.IntVal,
true,
APFloat::rmNearestTiesToEven);
- GV.IntVal = apf.convertToAPInt();
+ GV.IntVal = apf.bitcastToAPInt();
}
return GV;
}
else if (Op0->getType() == Type::X86_FP80Ty) {
APFloat apf = APFloat(GV.IntVal);
uint64_t v;
+ bool ignored;
(void)apf.convertToInteger(&v, BitWidth,
CE->getOpcode()==Instruction::FPToSI,
- APFloat::rmTowardZero);
+ APFloat::rmTowardZero, &ignored);
GV.IntVal = v; // endian?
}
return GV;
default: assert(0 && "Invalid long double opcode"); abort();
case Instruction::Add:
apfLHS.add(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
- GV.IntVal = apfLHS.convertToAPInt();
+ GV.IntVal = apfLHS.bitcastToAPInt();
break;
case Instruction::Sub:
apfLHS.subtract(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
- GV.IntVal = apfLHS.convertToAPInt();
+ GV.IntVal = apfLHS.bitcastToAPInt();
break;
case Instruction::Mul:
apfLHS.multiply(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
- GV.IntVal = apfLHS.convertToAPInt();
+ GV.IntVal = apfLHS.bitcastToAPInt();
break;
case Instruction::FDiv:
apfLHS.divide(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
- GV.IntVal = apfLHS.convertToAPInt();
+ GV.IntVal = apfLHS.bitcastToAPInt();
break;
case Instruction::FRem:
apfLHS.mod(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
- GV.IntVal = apfLHS.convertToAPInt();
+ GV.IntVal = apfLHS.bitcastToAPInt();
break;
}
}
case Type::X86_FP80TyID:
case Type::FP128TyID:
case Type::PPC_FP128TyID:
- Result.IntVal = cast <ConstantFP>(C)->getValueAPF().convertToAPInt();
+ Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
break;
case Type::IntegerTyID:
Result.IntVal = cast<ConstantInt>(C)->getValue();
/// is the address of the memory at which to store Val, cast to GenericValue *.
/// It is not a pointer to a GenericValue containing the address at which to
/// store Val.
-void ExecutionEngine::StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
- const Type *Ty) {
+void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
+ GenericValue *Ptr, const Type *Ty) {
const unsigned StoreBytes = getTargetData()->getTypeStoreSize(Ty);
switch (Ty->getTypeID()) {
// specified memory location...
//
void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
+ DOUT << "JIT: Initializing " << Addr << " ";
+ DEBUG(Init->dump());
if (isa<UndefValue>(Init)) {
return;
} else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
/// their initializers into the memory.
///
void ExecutionEngine::emitGlobals() {
- const TargetData *TD = getTargetData();
// Loop over all of the global variables in the program, allocating the memory
// to hold them. If there is more than one module, do a prepass over globals
}
if (!I->isDeclaration()) {
- // Get the type of the global.
- const Type *Ty = I->getType()->getElementType();
-
- // Allocate some memory for it!
- unsigned Size = TD->getABITypeSize(Ty);
- addGlobalMapping(I, new char[Size]);
+ addGlobalMapping(I, getMemoryForGV(I));
} else {
// External variable reference. Try to use the dynamic loader to
// get a pointer to it.
LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
void *Ptr = getPointerToGlobalIfAvailable(CGV);
assert(Ptr && "Canonical global wasn't codegen'd!");
- addGlobalMapping(GV, getPointerToGlobalIfAvailable(CGV));
+ addGlobalMapping(GV, Ptr);
}
}
// already in the map.
void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
void *GA = getPointerToGlobalIfAvailable(GV);
- DOUT << "Global '" << GV->getName() << "' -> " << GA << "\n";
- const Type *ElTy = GV->getType()->getElementType();
- size_t GVSize = (size_t)getTargetData()->getABITypeSize(ElTy);
if (GA == 0) {
// If it's not already specified, allocate memory for the global.
- GA = new char[GVSize];
+ GA = getMemoryForGV(GV);
addGlobalMapping(GV, GA);
}
-
- InitializeMemory(GV->getInitializer(), GA);
+
+ // Don't initialize if it's thread local, let the client do it.
+ if (!GV->isThreadLocal())
+ InitializeMemory(GV->getInitializer(), GA);
+
+ const Type *ElTy = GV->getType()->getElementType();
+ size_t GVSize = (size_t)getTargetData()->getABITypeSize(ElTy);
NumInitBytes += (unsigned)GVSize;
++NumGlobals;
}