1193323Sed//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file defines the common interface used by the various execution engine 11193323Sed// subclasses. 12193323Sed// 13193323Sed//===----------------------------------------------------------------------===// 14193323Sed 15193323Sed#define DEBUG_TYPE "jit" 16198090Srdivacky#include "llvm/ExecutionEngine/ExecutionEngine.h" 17218893Sdim#include "llvm/ADT/SmallString.h" 18193323Sed#include "llvm/ADT/Statistic.h" 19249423Sdim#include "llvm/ExecutionEngine/GenericValue.h" 20249423Sdim#include "llvm/IR/Constants.h" 21249423Sdim#include "llvm/IR/DataLayout.h" 22249423Sdim#include "llvm/IR/DerivedTypes.h" 23249423Sdim#include "llvm/IR/Module.h" 24249423Sdim#include "llvm/IR/Operator.h" 25193323Sed#include "llvm/Support/Debug.h" 26249423Sdim#include "llvm/Support/DynamicLibrary.h" 27198090Srdivacky#include "llvm/Support/ErrorHandling.h" 28249423Sdim#include "llvm/Support/Host.h" 29193323Sed#include "llvm/Support/MutexGuard.h" 30249423Sdim#include "llvm/Support/TargetRegistry.h" 31198090Srdivacky#include "llvm/Support/ValueHandle.h" 32198090Srdivacky#include "llvm/Support/raw_ostream.h" 33223017Sdim#include "llvm/Target/TargetMachine.h" 34193323Sed#include <cmath> 35193323Sed#include <cstring> 36193323Sedusing namespace llvm; 37193323Sed 38193323SedSTATISTIC(NumInitBytes, "Number of bytes of global vars initialized"); 39193323SedSTATISTIC(NumGlobals , "Number of global vars initialized"); 40193323Sed 41203954SrdivackyExecutionEngine *(*ExecutionEngine::JITCtor)( 42203954Srdivacky Module *M, 43203954Srdivacky std::string *ErrorStr, 44203954Srdivacky JITMemoryManager *JMM, 45203954Srdivacky bool GVsWithCode, 46223017Sdim TargetMachine *TM) = 0; 47218893SdimExecutionEngine *(*ExecutionEngine::MCJITCtor)( 48218893Sdim Module *M, 49218893Sdim std::string *ErrorStr, 50218893Sdim JITMemoryManager *JMM, 51218893Sdim bool GVsWithCode, 52223017Sdim TargetMachine *TM) = 0; 53203954SrdivackyExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M, 54198090Srdivacky std::string *ErrorStr) = 0; 55193323Sed 56203954SrdivackyExecutionEngine::ExecutionEngine(Module *M) 57198090Srdivacky : EEState(*this), 58218893Sdim LazyFunctionCreator(0), 59218893Sdim ExceptionTableRegister(0), 60218893Sdim ExceptionTableDeregister(0) { 61198892Srdivacky CompilingLazily = false; 62193323Sed GVCompilationDisabled = false; 63193323Sed SymbolSearchingDisabled = false; 64203954Srdivacky Modules.push_back(M); 65203954Srdivacky assert(M && "Module is null?"); 66193323Sed} 67193323Sed 68193323SedExecutionEngine::~ExecutionEngine() { 69193323Sed clearAllGlobalMappings(); 70193323Sed for (unsigned i = 0, e = Modules.size(); i != e; ++i) 71193323Sed delete Modules[i]; 72193323Sed} 73193323Sed 74218893Sdimvoid ExecutionEngine::DeregisterAllTables() { 75218893Sdim if (ExceptionTableDeregister) { 76221345Sdim DenseMap<const Function*, void*>::iterator it = AllExceptionTables.begin(); 77221345Sdim DenseMap<const Function*, void*>::iterator ite = AllExceptionTables.end(); 78221345Sdim for (; it != ite; ++it) 79221345Sdim ExceptionTableDeregister(it->second); 80218893Sdim AllExceptionTables.clear(); 81218893Sdim } 82218893Sdim} 83218893Sdim 84206083Srdivackynamespace { 85218893Sdim/// \brief Helper class which uses a value handler to automatically deletes the 86218893Sdim/// memory block when the GlobalVariable is destroyed. 87206083Srdivackyclass GVMemoryBlock : public CallbackVH { 88206083Srdivacky GVMemoryBlock(const GlobalVariable *GV) 89206083Srdivacky : CallbackVH(const_cast<GlobalVariable*>(GV)) {} 90206083Srdivacky 91206083Srdivackypublic: 92218893Sdim /// \brief Returns the address the GlobalVariable should be written into. The 93218893Sdim /// GVMemoryBlock object prefixes that. 94243830Sdim static char *Create(const GlobalVariable *GV, const DataLayout& TD) { 95226633Sdim Type *ElTy = GV->getType()->getElementType(); 96206083Srdivacky size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy); 97206083Srdivacky void *RawMemory = ::operator new( 98243830Sdim DataLayout::RoundUpAlignment(sizeof(GVMemoryBlock), 99206083Srdivacky TD.getPreferredAlignment(GV)) 100206083Srdivacky + GVSize); 101206083Srdivacky new(RawMemory) GVMemoryBlock(GV); 102206083Srdivacky return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock); 103206083Srdivacky } 104206083Srdivacky 105206083Srdivacky virtual void deleted() { 106206083Srdivacky // We allocated with operator new and with some extra memory hanging off the 107206083Srdivacky // end, so don't just delete this. I'm not sure if this is actually 108206083Srdivacky // required. 109206083Srdivacky this->~GVMemoryBlock(); 110206083Srdivacky ::operator delete(this); 111206083Srdivacky } 112206083Srdivacky}; 113206083Srdivacky} // anonymous namespace 114206083Srdivacky 115218893Sdimchar *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) { 116243830Sdim return GVMemoryBlock::Create(GV, *getDataLayout()); 117193323Sed} 118193323Sed 119203954Srdivackybool ExecutionEngine::removeModule(Module *M) { 120218893Sdim for(SmallVector<Module *, 1>::iterator I = Modules.begin(), 121193323Sed E = Modules.end(); I != E; ++I) { 122203954Srdivacky Module *Found = *I; 123203954Srdivacky if (Found == M) { 124193323Sed Modules.erase(I); 125203954Srdivacky clearGlobalMappingsFromModule(M); 126203954Srdivacky return true; 127193323Sed } 128193323Sed } 129203954Srdivacky return false; 130193323Sed} 131193323Sed 132193323SedFunction *ExecutionEngine::FindFunctionNamed(const char *FnName) { 133193323Sed for (unsigned i = 0, e = Modules.size(); i != e; ++i) { 134203954Srdivacky if (Function *F = Modules[i]->getFunction(FnName)) 135193323Sed return F; 136193323Sed } 137193323Sed return 0; 138193323Sed} 139193323Sed 140193323Sed 141218893Sdimvoid *ExecutionEngineState::RemoveMapping(const MutexGuard &, 142218893Sdim const GlobalValue *ToUnmap) { 143198892Srdivacky GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap); 144198090Srdivacky void *OldVal; 145218893Sdim 146218893Sdim // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the 147218893Sdim // GlobalAddressMap. 148198090Srdivacky if (I == GlobalAddressMap.end()) 149198090Srdivacky OldVal = 0; 150198090Srdivacky else { 151198090Srdivacky OldVal = I->second; 152198090Srdivacky GlobalAddressMap.erase(I); 153198090Srdivacky } 154198090Srdivacky 155198090Srdivacky GlobalAddressReverseMap.erase(OldVal); 156198090Srdivacky return OldVal; 157198090Srdivacky} 158198090Srdivacky 159193323Sedvoid ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) { 160193323Sed MutexGuard locked(lock); 161193323Sed 162218893Sdim DEBUG(dbgs() << "JIT: Map \'" << GV->getName() 163198090Srdivacky << "\' to [" << Addr << "]\n";); 164198892Srdivacky void *&CurVal = EEState.getGlobalAddressMap(locked)[GV]; 165193323Sed assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!"); 166193323Sed CurVal = Addr; 167218893Sdim 168218893Sdim // If we are using the reverse mapping, add it too. 169198090Srdivacky if (!EEState.getGlobalAddressReverseMap(locked).empty()) { 170198090Srdivacky AssertingVH<const GlobalValue> &V = 171198090Srdivacky EEState.getGlobalAddressReverseMap(locked)[Addr]; 172193323Sed assert((V == 0 || GV == 0) && "GlobalMapping already established!"); 173193323Sed V = GV; 174193323Sed } 175193323Sed} 176193323Sed 177193323Sedvoid ExecutionEngine::clearAllGlobalMappings() { 178193323Sed MutexGuard locked(lock); 179218893Sdim 180198090Srdivacky EEState.getGlobalAddressMap(locked).clear(); 181198090Srdivacky EEState.getGlobalAddressReverseMap(locked).clear(); 182193323Sed} 183193323Sed 184193323Sedvoid ExecutionEngine::clearGlobalMappingsFromModule(Module *M) { 185193323Sed MutexGuard locked(lock); 186218893Sdim 187218893Sdim for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) 188198090Srdivacky EEState.RemoveMapping(locked, FI); 189218893Sdim for (Module::global_iterator GI = M->global_begin(), GE = M->global_end(); 190218893Sdim GI != GE; ++GI) 191198090Srdivacky EEState.RemoveMapping(locked, GI); 192193323Sed} 193193323Sed 194193323Sedvoid *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) { 195193323Sed MutexGuard locked(lock); 196193323Sed 197198892Srdivacky ExecutionEngineState::GlobalAddressMapTy &Map = 198198090Srdivacky EEState.getGlobalAddressMap(locked); 199193323Sed 200193323Sed // Deleting from the mapping? 201218893Sdim if (Addr == 0) 202198090Srdivacky return EEState.RemoveMapping(locked, GV); 203218893Sdim 204198892Srdivacky void *&CurVal = Map[GV]; 205193323Sed void *OldVal = CurVal; 206193323Sed 207198090Srdivacky if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty()) 208198090Srdivacky EEState.getGlobalAddressReverseMap(locked).erase(CurVal); 209193323Sed CurVal = Addr; 210218893Sdim 211218893Sdim // If we are using the reverse mapping, add it too. 212198090Srdivacky if (!EEState.getGlobalAddressReverseMap(locked).empty()) { 213198090Srdivacky AssertingVH<const GlobalValue> &V = 214198090Srdivacky EEState.getGlobalAddressReverseMap(locked)[Addr]; 215193323Sed assert((V == 0 || GV == 0) && "GlobalMapping already established!"); 216193323Sed V = GV; 217193323Sed } 218193323Sed return OldVal; 219193323Sed} 220193323Sed 221193323Sedvoid *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) { 222193323Sed MutexGuard locked(lock); 223218893Sdim 224198892Srdivacky ExecutionEngineState::GlobalAddressMapTy::iterator I = 225198892Srdivacky EEState.getGlobalAddressMap(locked).find(GV); 226198090Srdivacky return I != EEState.getGlobalAddressMap(locked).end() ? I->second : 0; 227193323Sed} 228193323Sed 229193323Sedconst GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { 230193323Sed MutexGuard locked(lock); 231193323Sed 232193323Sed // If we haven't computed the reverse mapping yet, do so first. 233198090Srdivacky if (EEState.getGlobalAddressReverseMap(locked).empty()) { 234198892Srdivacky for (ExecutionEngineState::GlobalAddressMapTy::iterator 235198090Srdivacky I = EEState.getGlobalAddressMap(locked).begin(), 236198090Srdivacky E = EEState.getGlobalAddressMap(locked).end(); I != E; ++I) 237218893Sdim EEState.getGlobalAddressReverseMap(locked).insert(std::make_pair( 238218893Sdim I->second, I->first)); 239193323Sed } 240193323Sed 241198090Srdivacky std::map<void *, AssertingVH<const GlobalValue> >::iterator I = 242198090Srdivacky EEState.getGlobalAddressReverseMap(locked).find(Addr); 243198090Srdivacky return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : 0; 244193323Sed} 245193323Sed 246206083Srdivackynamespace { 247206083Srdivackyclass ArgvArray { 248206083Srdivacky char *Array; 249206083Srdivacky std::vector<char*> Values; 250206083Srdivackypublic: 251206083Srdivacky ArgvArray() : Array(NULL) {} 252206083Srdivacky ~ArgvArray() { clear(); } 253206083Srdivacky void clear() { 254206083Srdivacky delete[] Array; 255206083Srdivacky Array = NULL; 256206083Srdivacky for (size_t I = 0, E = Values.size(); I != E; ++I) { 257206083Srdivacky delete[] Values[I]; 258206083Srdivacky } 259206083Srdivacky Values.clear(); 260206083Srdivacky } 261206083Srdivacky /// Turn a vector of strings into a nice argv style array of pointers to null 262206083Srdivacky /// terminated strings. 263206083Srdivacky void *reset(LLVMContext &C, ExecutionEngine *EE, 264206083Srdivacky const std::vector<std::string> &InputArgv); 265206083Srdivacky}; 266206083Srdivacky} // anonymous namespace 267206083Srdivackyvoid *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE, 268206083Srdivacky const std::vector<std::string> &InputArgv) { 269206083Srdivacky clear(); // Free the old contents. 270243830Sdim unsigned PtrSize = EE->getDataLayout()->getPointerSize(); 271206083Srdivacky Array = new char[(InputArgv.size()+1)*PtrSize]; 272193323Sed 273206083Srdivacky DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n"); 274226633Sdim Type *SBytePtr = Type::getInt8PtrTy(C); 275193323Sed 276193323Sed for (unsigned i = 0; i != InputArgv.size(); ++i) { 277193323Sed unsigned Size = InputArgv[i].size()+1; 278193323Sed char *Dest = new char[Size]; 279206083Srdivacky Values.push_back(Dest); 280202375Srdivacky DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n"); 281193323Sed 282193323Sed std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); 283193323Sed Dest[Size-1] = 0; 284193323Sed 285206083Srdivacky // Endian safe: Array[i] = (PointerTy)Dest; 286206083Srdivacky EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize), 287193323Sed SBytePtr); 288193323Sed } 289193323Sed 290193323Sed // Null terminate it 291193323Sed EE->StoreValueToMemory(PTOGV(0), 292206083Srdivacky (GenericValue*)(Array+InputArgv.size()*PtrSize), 293193323Sed SBytePtr); 294206083Srdivacky return Array; 295193323Sed} 296193323Sed 297198090Srdivackyvoid ExecutionEngine::runStaticConstructorsDestructors(Module *module, 298198090Srdivacky bool isDtors) { 299193323Sed const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors"; 300218893Sdim GlobalVariable *GV = module->getNamedGlobal(Name); 301193323Sed 302218893Sdim // If this global has internal linkage, or if it has a use, then it must be 303218893Sdim // an old-style (llvmgcc3) static ctor with __main linked in and in use. If 304218893Sdim // this is the case, don't execute any of the global ctors, __main will do 305218893Sdim // it. 306218893Sdim if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return; 307218893Sdim 308221345Sdim // Should be an array of '{ i32, void ()* }' structs. The first value is 309218893Sdim // the init priority, which we ignore. 310234353Sdim ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 311234353Sdim if (InitList == 0) 312221345Sdim return; 313218893Sdim for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { 314234353Sdim ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i)); 315234353Sdim if (CS == 0) continue; 316218893Sdim 317218893Sdim Constant *FP = CS->getOperand(1); 318218893Sdim if (FP->isNullValue()) 319221345Sdim continue; // Found a sentinal value, ignore. 320218893Sdim 321218893Sdim // Strip off constant expression casts. 322218893Sdim if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) 323218893Sdim if (CE->isCast()) 324218893Sdim FP = CE->getOperand(0); 325218893Sdim 326218893Sdim // Execute the ctor/dtor function! 327218893Sdim if (Function *F = dyn_cast<Function>(FP)) 328218893Sdim runFunction(F, std::vector<GenericValue>()); 329218893Sdim 330218893Sdim // FIXME: It is marginally lame that we just do nothing here if we see an 331218893Sdim // entry we don't recognize. It might not be unreasonable for the verifier 332218893Sdim // to not even allow this and just assert here. 333218893Sdim } 334193323Sed} 335193323Sed 336193323Sedvoid ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) { 337193323Sed // Execute global ctors/dtors for each module in the program. 338218893Sdim for (unsigned i = 0, e = Modules.size(); i != e; ++i) 339218893Sdim runStaticConstructorsDestructors(Modules[i], isDtors); 340193323Sed} 341193323Sed 342193323Sed#ifndef NDEBUG 343193323Sed/// isTargetNullPtr - Return whether the target pointer stored at Loc is null. 344193323Sedstatic bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) { 345243830Sdim unsigned PtrSize = EE->getDataLayout()->getPointerSize(); 346193323Sed for (unsigned i = 0; i < PtrSize; ++i) 347193323Sed if (*(i + (uint8_t*)Loc)) 348193323Sed return false; 349193323Sed return true; 350193323Sed} 351193323Sed#endif 352193323Sed 353193323Sedint ExecutionEngine::runFunctionAsMain(Function *Fn, 354193323Sed const std::vector<std::string> &argv, 355193323Sed const char * const * envp) { 356193323Sed std::vector<GenericValue> GVArgs; 357193323Sed GenericValue GVArgc; 358193323Sed GVArgc.IntVal = APInt(32, argv.size()); 359193323Sed 360193323Sed // Check main() type 361193323Sed unsigned NumArgs = Fn->getFunctionType()->getNumParams(); 362226633Sdim FunctionType *FTy = Fn->getFunctionType(); 363226633Sdim Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo(); 364218893Sdim 365218893Sdim // Check the argument types. 366218893Sdim if (NumArgs > 3) 367218893Sdim report_fatal_error("Invalid number of arguments of main() supplied"); 368218893Sdim if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty) 369218893Sdim report_fatal_error("Invalid type for third argument of main() supplied"); 370218893Sdim if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty) 371218893Sdim report_fatal_error("Invalid type for second argument of main() supplied"); 372218893Sdim if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32)) 373218893Sdim report_fatal_error("Invalid type for first argument of main() supplied"); 374218893Sdim if (!FTy->getReturnType()->isIntegerTy() && 375218893Sdim !FTy->getReturnType()->isVoidTy()) 376218893Sdim report_fatal_error("Invalid return type of main() supplied"); 377218893Sdim 378206083Srdivacky ArgvArray CArgv; 379206083Srdivacky ArgvArray CEnv; 380193323Sed if (NumArgs) { 381193323Sed GVArgs.push_back(GVArgc); // Arg #0 = argc. 382193323Sed if (NumArgs > 1) { 383198090Srdivacky // Arg #1 = argv. 384206083Srdivacky GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv))); 385193323Sed assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) && 386193323Sed "argv[0] was null after CreateArgv"); 387193323Sed if (NumArgs > 2) { 388193323Sed std::vector<std::string> EnvVars; 389193323Sed for (unsigned i = 0; envp[i]; ++i) 390193323Sed EnvVars.push_back(envp[i]); 391198090Srdivacky // Arg #2 = envp. 392206083Srdivacky GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars))); 393193323Sed } 394193323Sed } 395193323Sed } 396218893Sdim 397193323Sed return runFunction(Fn, GVArgs).IntVal.getZExtValue(); 398193323Sed} 399193323Sed 400203954SrdivackyExecutionEngine *ExecutionEngine::create(Module *M, 401193323Sed bool ForceInterpreter, 402193323Sed std::string *ErrorStr, 403198090Srdivacky CodeGenOpt::Level OptLevel, 404198090Srdivacky bool GVsWithCode) { 405234353Sdim EngineBuilder EB = EngineBuilder(M) 406198090Srdivacky .setEngineKind(ForceInterpreter 407198090Srdivacky ? EngineKind::Interpreter 408198090Srdivacky : EngineKind::JIT) 409198090Srdivacky .setErrorStr(ErrorStr) 410198090Srdivacky .setOptLevel(OptLevel) 411234353Sdim .setAllocateGVsWithCode(GVsWithCode); 412234353Sdim 413234353Sdim return EB.create(); 414198090Srdivacky} 415193323Sed 416223017Sdim/// createJIT - This is the factory method for creating a JIT for the current 417223017Sdim/// machine, it does not fall back to the interpreter. This takes ownership 418223017Sdim/// of the module. 419223017SdimExecutionEngine *ExecutionEngine::createJIT(Module *M, 420223017Sdim std::string *ErrorStr, 421223017Sdim JITMemoryManager *JMM, 422234353Sdim CodeGenOpt::Level OL, 423223017Sdim bool GVsWithCode, 424226633Sdim Reloc::Model RM, 425223017Sdim CodeModel::Model CMM) { 426223017Sdim if (ExecutionEngine::JITCtor == 0) { 427223017Sdim if (ErrorStr) 428223017Sdim *ErrorStr = "JIT has not been linked in."; 429223017Sdim return 0; 430223017Sdim } 431223017Sdim 432223017Sdim // Use the defaults for extra parameters. Users can use EngineBuilder to 433223017Sdim // set them. 434234353Sdim EngineBuilder EB(M); 435234353Sdim EB.setEngineKind(EngineKind::JIT); 436234353Sdim EB.setErrorStr(ErrorStr); 437234353Sdim EB.setRelocationModel(RM); 438234353Sdim EB.setCodeModel(CMM); 439234353Sdim EB.setAllocateGVsWithCode(GVsWithCode); 440234353Sdim EB.setOptLevel(OL); 441234353Sdim EB.setJITMemoryManager(JMM); 442223017Sdim 443234353Sdim // TODO: permit custom TargetOptions here 444234353Sdim TargetMachine *TM = EB.selectTarget(); 445223017Sdim if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0; 446223017Sdim 447234353Sdim return ExecutionEngine::JITCtor(M, ErrorStr, JMM, GVsWithCode, TM); 448223017Sdim} 449223017Sdim 450234353SdimExecutionEngine *EngineBuilder::create(TargetMachine *TM) { 451234353Sdim OwningPtr<TargetMachine> TheTM(TM); // Take ownership. 452234353Sdim 453193323Sed // Make sure we can resolve symbols in the program as well. The zero arg 454193323Sed // to the function tells DynamicLibrary to load the program, not a library. 455193323Sed if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr)) 456193323Sed return 0; 457193323Sed 458198090Srdivacky // If the user specified a memory manager but didn't specify which engine to 459198090Srdivacky // create, we assume they only want the JIT, and we fail if they only want 460198090Srdivacky // the interpreter. 461198090Srdivacky if (JMM) { 462198090Srdivacky if (WhichEngine & EngineKind::JIT) 463198090Srdivacky WhichEngine = EngineKind::JIT; 464198090Srdivacky else { 465198090Srdivacky if (ErrorStr) 466198090Srdivacky *ErrorStr = "Cannot create an interpreter with a memory manager."; 467198090Srdivacky return 0; 468198090Srdivacky } 469198090Srdivacky } 470193323Sed 471198090Srdivacky // Unless the interpreter was explicitly selected or the JIT is not linked, 472198090Srdivacky // try making a JIT. 473234353Sdim if ((WhichEngine & EngineKind::JIT) && TheTM) { 474234353Sdim Triple TT(M->getTargetTriple()); 475234353Sdim if (!TM->getTarget().hasJIT()) { 476234353Sdim errs() << "WARNING: This target JIT is not designed for the host" 477234353Sdim << " you are running. If bad things happen, please choose" 478234353Sdim << " a different -march switch.\n"; 479198090Srdivacky } 480234353Sdim 481234353Sdim if (UseMCJIT && ExecutionEngine::MCJITCtor) { 482234353Sdim ExecutionEngine *EE = 483234353Sdim ExecutionEngine::MCJITCtor(M, ErrorStr, JMM, 484234353Sdim AllocateGVsWithCode, TheTM.take()); 485234353Sdim if (EE) return EE; 486234353Sdim } else if (ExecutionEngine::JITCtor) { 487234353Sdim ExecutionEngine *EE = 488234353Sdim ExecutionEngine::JITCtor(M, ErrorStr, JMM, 489234353Sdim AllocateGVsWithCode, TheTM.take()); 490234353Sdim if (EE) return EE; 491234353Sdim } 492198090Srdivacky } 493193323Sed 494198090Srdivacky // If we can't make a JIT and we didn't request one specifically, try making 495198090Srdivacky // an interpreter instead. 496198090Srdivacky if (WhichEngine & EngineKind::Interpreter) { 497198090Srdivacky if (ExecutionEngine::InterpCtor) 498203954Srdivacky return ExecutionEngine::InterpCtor(M, ErrorStr); 499198090Srdivacky if (ErrorStr) 500198090Srdivacky *ErrorStr = "Interpreter has not been linked in."; 501198090Srdivacky return 0; 502198090Srdivacky } 503193323Sed 504243830Sdim if ((WhichEngine & EngineKind::JIT) && ExecutionEngine::JITCtor == 0 && 505243830Sdim ExecutionEngine::MCJITCtor == 0) { 506198090Srdivacky if (ErrorStr) 507198090Srdivacky *ErrorStr = "JIT has not been linked in."; 508218893Sdim } 509218893Sdim 510198090Srdivacky return 0; 511193323Sed} 512193323Sed 513193323Sedvoid *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { 514193323Sed if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) 515193323Sed return getPointerToFunction(F); 516193323Sed 517193323Sed MutexGuard locked(lock); 518218893Sdim if (void *P = EEState.getGlobalAddressMap(locked)[GV]) 519218893Sdim return P; 520193323Sed 521193323Sed // Global variable might have been added since interpreter started. 522193323Sed if (GlobalVariable *GVar = 523193323Sed const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV))) 524193323Sed EmitGlobalVariable(GVar); 525193323Sed else 526198090Srdivacky llvm_unreachable("Global hasn't had an address allocated yet!"); 527218893Sdim 528198892Srdivacky return EEState.getGlobalAddressMap(locked)[GV]; 529193323Sed} 530193323Sed 531218893Sdim/// \brief Converts a Constant* into a GenericValue, including handling of 532218893Sdim/// ConstantExpr values. 533193323SedGenericValue ExecutionEngine::getConstantValue(const Constant *C) { 534193323Sed // If its undefined, return the garbage. 535202375Srdivacky if (isa<UndefValue>(C)) { 536202375Srdivacky GenericValue Result; 537202375Srdivacky switch (C->getType()->getTypeID()) { 538249423Sdim default: 539249423Sdim break; 540202375Srdivacky case Type::IntegerTyID: 541202375Srdivacky case Type::X86_FP80TyID: 542202375Srdivacky case Type::FP128TyID: 543202375Srdivacky case Type::PPC_FP128TyID: 544202375Srdivacky // Although the value is undefined, we still have to construct an APInt 545202375Srdivacky // with the correct bit width. 546202375Srdivacky Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0); 547202375Srdivacky break; 548249423Sdim case Type::VectorTyID: 549249423Sdim // if the whole vector is 'undef' just reserve memory for the value. 550249423Sdim const VectorType* VTy = dyn_cast<VectorType>(C->getType()); 551249423Sdim const Type *ElemTy = VTy->getElementType(); 552249423Sdim unsigned int elemNum = VTy->getNumElements(); 553249423Sdim Result.AggregateVal.resize(elemNum); 554249423Sdim if (ElemTy->isIntegerTy()) 555249423Sdim for (unsigned int i = 0; i < elemNum; ++i) 556249423Sdim Result.AggregateVal[i].IntVal = 557249423Sdim APInt(ElemTy->getPrimitiveSizeInBits(), 0); 558202375Srdivacky break; 559202375Srdivacky } 560202375Srdivacky return Result; 561202375Srdivacky } 562193323Sed 563218893Sdim // Otherwise, if the value is a ConstantExpr... 564193323Sed if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 565193323Sed Constant *Op0 = CE->getOperand(0); 566193323Sed switch (CE->getOpcode()) { 567193323Sed case Instruction::GetElementPtr: { 568218893Sdim // Compute the index 569193323Sed GenericValue Result = getConstantValue(Op0); 570249423Sdim APInt Offset(TD->getPointerSizeInBits(), 0); 571249423Sdim cast<GEPOperator>(CE)->accumulateConstantOffset(*TD, Offset); 572193323Sed 573193323Sed char* tmp = (char*) Result.PointerVal; 574249423Sdim Result = PTOGV(tmp + Offset.getSExtValue()); 575193323Sed return Result; 576193323Sed } 577193323Sed case Instruction::Trunc: { 578193323Sed GenericValue GV = getConstantValue(Op0); 579193323Sed uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 580193323Sed GV.IntVal = GV.IntVal.trunc(BitWidth); 581193323Sed return GV; 582193323Sed } 583193323Sed case Instruction::ZExt: { 584193323Sed GenericValue GV = getConstantValue(Op0); 585193323Sed uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 586193323Sed GV.IntVal = GV.IntVal.zext(BitWidth); 587193323Sed return GV; 588193323Sed } 589193323Sed case Instruction::SExt: { 590193323Sed GenericValue GV = getConstantValue(Op0); 591193323Sed uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 592193323Sed GV.IntVal = GV.IntVal.sext(BitWidth); 593193323Sed return GV; 594193323Sed } 595193323Sed case Instruction::FPTrunc: { 596193323Sed // FIXME long double 597193323Sed GenericValue GV = getConstantValue(Op0); 598193323Sed GV.FloatVal = float(GV.DoubleVal); 599193323Sed return GV; 600193323Sed } 601193323Sed case Instruction::FPExt:{ 602193323Sed // FIXME long double 603193323Sed GenericValue GV = getConstantValue(Op0); 604193323Sed GV.DoubleVal = double(GV.FloatVal); 605193323Sed return GV; 606193323Sed } 607193323Sed case Instruction::UIToFP: { 608193323Sed GenericValue GV = getConstantValue(Op0); 609198090Srdivacky if (CE->getType()->isFloatTy()) 610193323Sed GV.FloatVal = float(GV.IntVal.roundToDouble()); 611198090Srdivacky else if (CE->getType()->isDoubleTy()) 612193323Sed GV.DoubleVal = GV.IntVal.roundToDouble(); 613198090Srdivacky else if (CE->getType()->isX86_FP80Ty()) { 614218893Sdim APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended); 615218893Sdim (void)apf.convertFromAPInt(GV.IntVal, 616193323Sed false, 617193323Sed APFloat::rmNearestTiesToEven); 618193323Sed GV.IntVal = apf.bitcastToAPInt(); 619193323Sed } 620193323Sed return GV; 621193323Sed } 622193323Sed case Instruction::SIToFP: { 623193323Sed GenericValue GV = getConstantValue(Op0); 624198090Srdivacky if (CE->getType()->isFloatTy()) 625193323Sed GV.FloatVal = float(GV.IntVal.signedRoundToDouble()); 626198090Srdivacky else if (CE->getType()->isDoubleTy()) 627193323Sed GV.DoubleVal = GV.IntVal.signedRoundToDouble(); 628198090Srdivacky else if (CE->getType()->isX86_FP80Ty()) { 629218893Sdim APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended); 630218893Sdim (void)apf.convertFromAPInt(GV.IntVal, 631193323Sed true, 632193323Sed APFloat::rmNearestTiesToEven); 633193323Sed GV.IntVal = apf.bitcastToAPInt(); 634193323Sed } 635193323Sed return GV; 636193323Sed } 637193323Sed case Instruction::FPToUI: // double->APInt conversion handles sign 638193323Sed case Instruction::FPToSI: { 639193323Sed GenericValue GV = getConstantValue(Op0); 640193323Sed uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 641198090Srdivacky if (Op0->getType()->isFloatTy()) 642193323Sed GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth); 643198090Srdivacky else if (Op0->getType()->isDoubleTy()) 644193323Sed GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth); 645198090Srdivacky else if (Op0->getType()->isX86_FP80Ty()) { 646249423Sdim APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal); 647193323Sed uint64_t v; 648193323Sed bool ignored; 649193323Sed (void)apf.convertToInteger(&v, BitWidth, 650218893Sdim CE->getOpcode()==Instruction::FPToSI, 651193323Sed APFloat::rmTowardZero, &ignored); 652193323Sed GV.IntVal = v; // endian? 653193323Sed } 654193323Sed return GV; 655193323Sed } 656193323Sed case Instruction::PtrToInt: { 657193323Sed GenericValue GV = getConstantValue(Op0); 658243830Sdim uint32_t PtrWidth = TD->getTypeSizeInBits(Op0->getType()); 659243830Sdim assert(PtrWidth <= 64 && "Bad pointer width"); 660193323Sed GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal)); 661243830Sdim uint32_t IntWidth = TD->getTypeSizeInBits(CE->getType()); 662243830Sdim GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth); 663193323Sed return GV; 664193323Sed } 665193323Sed case Instruction::IntToPtr: { 666193323Sed GenericValue GV = getConstantValue(Op0); 667243830Sdim uint32_t PtrWidth = TD->getTypeSizeInBits(CE->getType()); 668243830Sdim GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth); 669193323Sed assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width"); 670193323Sed GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue())); 671193323Sed return GV; 672193323Sed } 673193323Sed case Instruction::BitCast: { 674193323Sed GenericValue GV = getConstantValue(Op0); 675226633Sdim Type* DestTy = CE->getType(); 676193323Sed switch (Op0->getType()->getTypeID()) { 677198090Srdivacky default: llvm_unreachable("Invalid bitcast operand"); 678193323Sed case Type::IntegerTyID: 679203954Srdivacky assert(DestTy->isFloatingPointTy() && "invalid bitcast"); 680198090Srdivacky if (DestTy->isFloatTy()) 681193323Sed GV.FloatVal = GV.IntVal.bitsToFloat(); 682198090Srdivacky else if (DestTy->isDoubleTy()) 683193323Sed GV.DoubleVal = GV.IntVal.bitsToDouble(); 684193323Sed break; 685218893Sdim case Type::FloatTyID: 686203954Srdivacky assert(DestTy->isIntegerTy(32) && "Invalid bitcast"); 687218893Sdim GV.IntVal = APInt::floatToBits(GV.FloatVal); 688193323Sed break; 689193323Sed case Type::DoubleTyID: 690203954Srdivacky assert(DestTy->isIntegerTy(64) && "Invalid bitcast"); 691218893Sdim GV.IntVal = APInt::doubleToBits(GV.DoubleVal); 692193323Sed break; 693193323Sed case Type::PointerTyID: 694204642Srdivacky assert(DestTy->isPointerTy() && "Invalid bitcast"); 695193323Sed break; // getConstantValue(Op0) above already converted it 696193323Sed } 697193323Sed return GV; 698193323Sed } 699193323Sed case Instruction::Add: 700193574Sed case Instruction::FAdd: 701193323Sed case Instruction::Sub: 702193574Sed case Instruction::FSub: 703193323Sed case Instruction::Mul: 704193574Sed case Instruction::FMul: 705193323Sed case Instruction::UDiv: 706193323Sed case Instruction::SDiv: 707193323Sed case Instruction::URem: 708193323Sed case Instruction::SRem: 709193323Sed case Instruction::And: 710193323Sed case Instruction::Or: 711193323Sed case Instruction::Xor: { 712193323Sed GenericValue LHS = getConstantValue(Op0); 713193323Sed GenericValue RHS = getConstantValue(CE->getOperand(1)); 714193323Sed GenericValue GV; 715193323Sed switch (CE->getOperand(0)->getType()->getTypeID()) { 716198090Srdivacky default: llvm_unreachable("Bad add type!"); 717193323Sed case Type::IntegerTyID: 718193323Sed switch (CE->getOpcode()) { 719198090Srdivacky default: llvm_unreachable("Invalid integer opcode"); 720193323Sed case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break; 721193323Sed case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break; 722193323Sed case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break; 723193323Sed case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break; 724193323Sed case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break; 725193323Sed case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break; 726193323Sed case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break; 727193323Sed case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break; 728193323Sed case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break; 729193323Sed case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break; 730193323Sed } 731193323Sed break; 732193323Sed case Type::FloatTyID: 733193323Sed switch (CE->getOpcode()) { 734198090Srdivacky default: llvm_unreachable("Invalid float opcode"); 735193574Sed case Instruction::FAdd: 736193323Sed GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break; 737193574Sed case Instruction::FSub: 738193323Sed GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break; 739193574Sed case Instruction::FMul: 740193323Sed GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break; 741218893Sdim case Instruction::FDiv: 742193323Sed GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break; 743218893Sdim case Instruction::FRem: 744208599Srdivacky GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break; 745193323Sed } 746193323Sed break; 747193323Sed case Type::DoubleTyID: 748193323Sed switch (CE->getOpcode()) { 749198090Srdivacky default: llvm_unreachable("Invalid double opcode"); 750193574Sed case Instruction::FAdd: 751193323Sed GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break; 752193574Sed case Instruction::FSub: 753193323Sed GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break; 754193574Sed case Instruction::FMul: 755193323Sed GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break; 756218893Sdim case Instruction::FDiv: 757193323Sed GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break; 758218893Sdim case Instruction::FRem: 759208599Srdivacky GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break; 760193323Sed } 761193323Sed break; 762193323Sed case Type::X86_FP80TyID: 763193323Sed case Type::PPC_FP128TyID: 764193323Sed case Type::FP128TyID: { 765249423Sdim const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics(); 766249423Sdim APFloat apfLHS = APFloat(Sem, LHS.IntVal); 767193323Sed switch (CE->getOpcode()) { 768218893Sdim default: llvm_unreachable("Invalid long double opcode"); 769193574Sed case Instruction::FAdd: 770249423Sdim apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven); 771193323Sed GV.IntVal = apfLHS.bitcastToAPInt(); 772193323Sed break; 773193574Sed case Instruction::FSub: 774249423Sdim apfLHS.subtract(APFloat(Sem, RHS.IntVal), 775249423Sdim APFloat::rmNearestTiesToEven); 776193323Sed GV.IntVal = apfLHS.bitcastToAPInt(); 777193323Sed break; 778193574Sed case Instruction::FMul: 779249423Sdim apfLHS.multiply(APFloat(Sem, RHS.IntVal), 780249423Sdim APFloat::rmNearestTiesToEven); 781193323Sed GV.IntVal = apfLHS.bitcastToAPInt(); 782193323Sed break; 783218893Sdim case Instruction::FDiv: 784249423Sdim apfLHS.divide(APFloat(Sem, RHS.IntVal), 785249423Sdim APFloat::rmNearestTiesToEven); 786193323Sed GV.IntVal = apfLHS.bitcastToAPInt(); 787193323Sed break; 788218893Sdim case Instruction::FRem: 789249423Sdim apfLHS.mod(APFloat(Sem, RHS.IntVal), 790249423Sdim APFloat::rmNearestTiesToEven); 791193323Sed GV.IntVal = apfLHS.bitcastToAPInt(); 792193323Sed break; 793193323Sed } 794193323Sed } 795193323Sed break; 796193323Sed } 797193323Sed return GV; 798193323Sed } 799193323Sed default: 800193323Sed break; 801193323Sed } 802218893Sdim 803218893Sdim SmallString<256> Msg; 804218893Sdim raw_svector_ostream OS(Msg); 805218893Sdim OS << "ConstantExpr not handled: " << *CE; 806218893Sdim report_fatal_error(OS.str()); 807193323Sed } 808193323Sed 809218893Sdim // Otherwise, we have a simple constant. 810193323Sed GenericValue Result; 811193323Sed switch (C->getType()->getTypeID()) { 812218893Sdim case Type::FloatTyID: 813218893Sdim Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat(); 814193323Sed break; 815193323Sed case Type::DoubleTyID: 816193323Sed Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble(); 817193323Sed break; 818193323Sed case Type::X86_FP80TyID: 819193323Sed case Type::FP128TyID: 820193323Sed case Type::PPC_FP128TyID: 821193323Sed Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt(); 822193323Sed break; 823193323Sed case Type::IntegerTyID: 824193323Sed Result.IntVal = cast<ConstantInt>(C)->getValue(); 825193323Sed break; 826193323Sed case Type::PointerTyID: 827193323Sed if (isa<ConstantPointerNull>(C)) 828193323Sed Result.PointerVal = 0; 829193323Sed else if (const Function *F = dyn_cast<Function>(C)) 830193323Sed Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F))); 831198892Srdivacky else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) 832193323Sed Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV))); 833198892Srdivacky else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) 834198892Srdivacky Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>( 835198892Srdivacky BA->getBasicBlock()))); 836193323Sed else 837198090Srdivacky llvm_unreachable("Unknown constant pointer type!"); 838193323Sed break; 839249423Sdim case Type::VectorTyID: { 840249423Sdim unsigned elemNum; 841249423Sdim Type* ElemTy; 842249423Sdim const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C); 843249423Sdim const ConstantVector *CV = dyn_cast<ConstantVector>(C); 844249423Sdim const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C); 845249423Sdim 846249423Sdim if (CDV) { 847249423Sdim elemNum = CDV->getNumElements(); 848249423Sdim ElemTy = CDV->getElementType(); 849249423Sdim } else if (CV || CAZ) { 850249423Sdim VectorType* VTy = dyn_cast<VectorType>(C->getType()); 851249423Sdim elemNum = VTy->getNumElements(); 852249423Sdim ElemTy = VTy->getElementType(); 853249423Sdim } else { 854249423Sdim llvm_unreachable("Unknown constant vector type!"); 855249423Sdim } 856249423Sdim 857249423Sdim Result.AggregateVal.resize(elemNum); 858249423Sdim // Check if vector holds floats. 859249423Sdim if(ElemTy->isFloatTy()) { 860249423Sdim if (CAZ) { 861249423Sdim GenericValue floatZero; 862249423Sdim floatZero.FloatVal = 0.f; 863249423Sdim std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(), 864249423Sdim floatZero); 865249423Sdim break; 866249423Sdim } 867249423Sdim if(CV) { 868249423Sdim for (unsigned i = 0; i < elemNum; ++i) 869249423Sdim if (!isa<UndefValue>(CV->getOperand(i))) 870249423Sdim Result.AggregateVal[i].FloatVal = cast<ConstantFP>( 871249423Sdim CV->getOperand(i))->getValueAPF().convertToFloat(); 872249423Sdim break; 873249423Sdim } 874249423Sdim if(CDV) 875249423Sdim for (unsigned i = 0; i < elemNum; ++i) 876249423Sdim Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i); 877249423Sdim 878249423Sdim break; 879249423Sdim } 880249423Sdim // Check if vector holds doubles. 881249423Sdim if (ElemTy->isDoubleTy()) { 882249423Sdim if (CAZ) { 883249423Sdim GenericValue doubleZero; 884249423Sdim doubleZero.DoubleVal = 0.0; 885249423Sdim std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(), 886249423Sdim doubleZero); 887249423Sdim break; 888249423Sdim } 889249423Sdim if(CV) { 890249423Sdim for (unsigned i = 0; i < elemNum; ++i) 891249423Sdim if (!isa<UndefValue>(CV->getOperand(i))) 892249423Sdim Result.AggregateVal[i].DoubleVal = cast<ConstantFP>( 893249423Sdim CV->getOperand(i))->getValueAPF().convertToDouble(); 894249423Sdim break; 895249423Sdim } 896249423Sdim if(CDV) 897249423Sdim for (unsigned i = 0; i < elemNum; ++i) 898249423Sdim Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i); 899249423Sdim 900249423Sdim break; 901249423Sdim } 902249423Sdim // Check if vector holds integers. 903249423Sdim if (ElemTy->isIntegerTy()) { 904249423Sdim if (CAZ) { 905249423Sdim GenericValue intZero; 906249423Sdim intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull); 907249423Sdim std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(), 908249423Sdim intZero); 909249423Sdim break; 910249423Sdim } 911249423Sdim if(CV) { 912249423Sdim for (unsigned i = 0; i < elemNum; ++i) 913249423Sdim if (!isa<UndefValue>(CV->getOperand(i))) 914249423Sdim Result.AggregateVal[i].IntVal = cast<ConstantInt>( 915249423Sdim CV->getOperand(i))->getValue(); 916249423Sdim else { 917249423Sdim Result.AggregateVal[i].IntVal = 918249423Sdim APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0); 919249423Sdim } 920249423Sdim break; 921249423Sdim } 922249423Sdim if(CDV) 923249423Sdim for (unsigned i = 0; i < elemNum; ++i) 924249423Sdim Result.AggregateVal[i].IntVal = APInt( 925249423Sdim CDV->getElementType()->getPrimitiveSizeInBits(), 926249423Sdim CDV->getElementAsInteger(i)); 927249423Sdim 928249423Sdim break; 929249423Sdim } 930249423Sdim llvm_unreachable("Unknown constant pointer type!"); 931249423Sdim } 932249423Sdim break; 933249423Sdim 934193323Sed default: 935218893Sdim SmallString<256> Msg; 936218893Sdim raw_svector_ostream OS(Msg); 937218893Sdim OS << "ERROR: Constant unimplemented for type: " << *C->getType(); 938218893Sdim report_fatal_error(OS.str()); 939193323Sed } 940218893Sdim 941193323Sed return Result; 942193323Sed} 943193323Sed 944193323Sed/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst 945193323Sed/// with the integer held in IntVal. 946193323Sedstatic void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, 947193323Sed unsigned StoreBytes) { 948193323Sed assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!"); 949243830Sdim const uint8_t *Src = (const uint8_t *)IntVal.getRawData(); 950193323Sed 951251662Sdim if (sys::IsLittleEndianHost) { 952193323Sed // Little-endian host - the source is ordered from LSB to MSB. Order the 953193323Sed // destination from LSB to MSB: Do a straight copy. 954193323Sed memcpy(Dst, Src, StoreBytes); 955218893Sdim } else { 956193323Sed // Big-endian host - the source is an array of 64 bit words ordered from 957193323Sed // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination 958193323Sed // from MSB to LSB: Reverse the word order, but not the bytes in a word. 959193323Sed while (StoreBytes > sizeof(uint64_t)) { 960193323Sed StoreBytes -= sizeof(uint64_t); 961193323Sed // May not be aligned so use memcpy. 962193323Sed memcpy(Dst + StoreBytes, Src, sizeof(uint64_t)); 963193323Sed Src += sizeof(uint64_t); 964193323Sed } 965193323Sed 966193323Sed memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes); 967193323Sed } 968193323Sed} 969193323Sed 970193323Sedvoid ExecutionEngine::StoreValueToMemory(const GenericValue &Val, 971226633Sdim GenericValue *Ptr, Type *Ty) { 972243830Sdim const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty); 973193323Sed 974193323Sed switch (Ty->getTypeID()) { 975249423Sdim default: 976249423Sdim dbgs() << "Cannot store value of type " << *Ty << "!\n"; 977249423Sdim break; 978193323Sed case Type::IntegerTyID: 979193323Sed StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes); 980193323Sed break; 981193323Sed case Type::FloatTyID: 982193323Sed *((float*)Ptr) = Val.FloatVal; 983193323Sed break; 984193323Sed case Type::DoubleTyID: 985193323Sed *((double*)Ptr) = Val.DoubleVal; 986193323Sed break; 987193323Sed case Type::X86_FP80TyID: 988193323Sed memcpy(Ptr, Val.IntVal.getRawData(), 10); 989193323Sed break; 990193323Sed case Type::PointerTyID: 991193323Sed // Ensure 64 bit target pointers are fully initialized on 32 bit hosts. 992193323Sed if (StoreBytes != sizeof(PointerTy)) 993221345Sdim memset(&(Ptr->PointerVal), 0, StoreBytes); 994193323Sed 995193323Sed *((PointerTy*)Ptr) = Val.PointerVal; 996193323Sed break; 997249423Sdim case Type::VectorTyID: 998249423Sdim for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) { 999249423Sdim if (cast<VectorType>(Ty)->getElementType()->isDoubleTy()) 1000249423Sdim *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal; 1001249423Sdim if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) 1002249423Sdim *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal; 1003249423Sdim if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) { 1004249423Sdim unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8; 1005249423Sdim StoreIntToMemory(Val.AggregateVal[i].IntVal, 1006249423Sdim (uint8_t*)Ptr + numOfBytes*i, numOfBytes); 1007249423Sdim } 1008249423Sdim } 1009249423Sdim break; 1010193323Sed } 1011193323Sed 1012251662Sdim if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian()) 1013193323Sed // Host and target are different endian - reverse the stored bytes. 1014193323Sed std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr); 1015193323Sed} 1016193323Sed 1017193323Sed/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting 1018193323Sed/// from Src into IntVal, which is assumed to be wide enough and to hold zero. 1019193323Sedstatic void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) { 1020193323Sed assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!"); 1021249423Sdim uint8_t *Dst = reinterpret_cast<uint8_t *>( 1022249423Sdim const_cast<uint64_t *>(IntVal.getRawData())); 1023193323Sed 1024251662Sdim if (sys::IsLittleEndianHost) 1025193323Sed // Little-endian host - the destination must be ordered from LSB to MSB. 1026193323Sed // The source is ordered from LSB to MSB: Do a straight copy. 1027193323Sed memcpy(Dst, Src, LoadBytes); 1028193323Sed else { 1029193323Sed // Big-endian - the destination is an array of 64 bit words ordered from 1030193323Sed // LSW to MSW. Each word must be ordered from MSB to LSB. The source is 1031193323Sed // ordered from MSB to LSB: Reverse the word order, but not the bytes in 1032193323Sed // a word. 1033193323Sed while (LoadBytes > sizeof(uint64_t)) { 1034193323Sed LoadBytes -= sizeof(uint64_t); 1035193323Sed // May not be aligned so use memcpy. 1036193323Sed memcpy(Dst, Src + LoadBytes, sizeof(uint64_t)); 1037193323Sed Dst += sizeof(uint64_t); 1038193323Sed } 1039193323Sed 1040193323Sed memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes); 1041193323Sed } 1042193323Sed} 1043193323Sed 1044193323Sed/// FIXME: document 1045193323Sed/// 1046193323Sedvoid ExecutionEngine::LoadValueFromMemory(GenericValue &Result, 1047193323Sed GenericValue *Ptr, 1048226633Sdim Type *Ty) { 1049243830Sdim const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty); 1050193323Sed 1051193323Sed switch (Ty->getTypeID()) { 1052193323Sed case Type::IntegerTyID: 1053193323Sed // An APInt with all words initially zero. 1054193323Sed Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0); 1055193323Sed LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes); 1056193323Sed break; 1057193323Sed case Type::FloatTyID: 1058193323Sed Result.FloatVal = *((float*)Ptr); 1059193323Sed break; 1060193323Sed case Type::DoubleTyID: 1061193323Sed Result.DoubleVal = *((double*)Ptr); 1062193323Sed break; 1063193323Sed case Type::PointerTyID: 1064193323Sed Result.PointerVal = *((PointerTy*)Ptr); 1065193323Sed break; 1066193323Sed case Type::X86_FP80TyID: { 1067193323Sed // This is endian dependent, but it will only work on x86 anyway. 1068193323Sed // FIXME: Will not trap if loading a signaling NaN. 1069193323Sed uint64_t y[2]; 1070193323Sed memcpy(y, Ptr, 10); 1071226633Sdim Result.IntVal = APInt(80, y); 1072193323Sed break; 1073193323Sed } 1074249423Sdim case Type::VectorTyID: { 1075249423Sdim const VectorType *VT = cast<VectorType>(Ty); 1076249423Sdim const Type *ElemT = VT->getElementType(); 1077249423Sdim const unsigned numElems = VT->getNumElements(); 1078249423Sdim if (ElemT->isFloatTy()) { 1079249423Sdim Result.AggregateVal.resize(numElems); 1080249423Sdim for (unsigned i = 0; i < numElems; ++i) 1081249423Sdim Result.AggregateVal[i].FloatVal = *((float*)Ptr+i); 1082249423Sdim } 1083249423Sdim if (ElemT->isDoubleTy()) { 1084249423Sdim Result.AggregateVal.resize(numElems); 1085249423Sdim for (unsigned i = 0; i < numElems; ++i) 1086249423Sdim Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i); 1087249423Sdim } 1088249423Sdim if (ElemT->isIntegerTy()) { 1089249423Sdim GenericValue intZero; 1090249423Sdim const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth(); 1091249423Sdim intZero.IntVal = APInt(elemBitWidth, 0); 1092249423Sdim Result.AggregateVal.resize(numElems, intZero); 1093249423Sdim for (unsigned i = 0; i < numElems; ++i) 1094249423Sdim LoadIntFromMemory(Result.AggregateVal[i].IntVal, 1095249423Sdim (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8); 1096249423Sdim } 1097249423Sdim break; 1098249423Sdim } 1099193323Sed default: 1100218893Sdim SmallString<256> Msg; 1101218893Sdim raw_svector_ostream OS(Msg); 1102218893Sdim OS << "Cannot load value of type " << *Ty << "!"; 1103218893Sdim report_fatal_error(OS.str()); 1104193323Sed } 1105193323Sed} 1106193323Sed 1107193323Sedvoid ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { 1108202375Srdivacky DEBUG(dbgs() << "JIT: Initializing " << Addr << " "); 1109193323Sed DEBUG(Init->dump()); 1110234353Sdim if (isa<UndefValue>(Init)) 1111193323Sed return; 1112234353Sdim 1113234353Sdim if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) { 1114193323Sed unsigned ElementSize = 1115243830Sdim getDataLayout()->getTypeAllocSize(CP->getType()->getElementType()); 1116193323Sed for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) 1117193323Sed InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize); 1118193323Sed return; 1119234353Sdim } 1120234353Sdim 1121234353Sdim if (isa<ConstantAggregateZero>(Init)) { 1122243830Sdim memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType())); 1123193323Sed return; 1124234353Sdim } 1125234353Sdim 1126234353Sdim if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) { 1127193323Sed unsigned ElementSize = 1128243830Sdim getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType()); 1129193323Sed for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) 1130193323Sed InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); 1131193323Sed return; 1132234353Sdim } 1133234353Sdim 1134234353Sdim if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) { 1135193323Sed const StructLayout *SL = 1136243830Sdim getDataLayout()->getStructLayout(cast<StructType>(CPS->getType())); 1137193323Sed for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) 1138193323Sed InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i)); 1139193323Sed return; 1140234353Sdim } 1141234353Sdim 1142234353Sdim if (const ConstantDataSequential *CDS = 1143234353Sdim dyn_cast<ConstantDataSequential>(Init)) { 1144234353Sdim // CDS is already laid out in host memory order. 1145234353Sdim StringRef Data = CDS->getRawDataValues(); 1146234353Sdim memcpy(Addr, Data.data(), Data.size()); 1147234353Sdim return; 1148234353Sdim } 1149234353Sdim 1150234353Sdim if (Init->getType()->isFirstClassType()) { 1151193323Sed GenericValue Val = getConstantValue(Init); 1152193323Sed StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); 1153193323Sed return; 1154193323Sed } 1155193323Sed 1156218893Sdim DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n"); 1157198090Srdivacky llvm_unreachable("Unknown constant type to initialize memory with!"); 1158193323Sed} 1159193323Sed 1160193323Sed/// EmitGlobals - Emit all of the global variables to memory, storing their 1161193323Sed/// addresses into GlobalAddress. This must make sure to copy the contents of 1162193323Sed/// their initializers into the memory. 1163193323Sedvoid ExecutionEngine::emitGlobals() { 1164193323Sed // Loop over all of the global variables in the program, allocating the memory 1165193323Sed // to hold them. If there is more than one module, do a prepass over globals 1166193323Sed // to figure out how the different modules should link together. 1167226633Sdim std::map<std::pair<std::string, Type*>, 1168193323Sed const GlobalValue*> LinkedGlobalsMap; 1169193323Sed 1170193323Sed if (Modules.size() != 1) { 1171193323Sed for (unsigned m = 0, e = Modules.size(); m != e; ++m) { 1172203954Srdivacky Module &M = *Modules[m]; 1173193323Sed for (Module::const_global_iterator I = M.global_begin(), 1174193323Sed E = M.global_end(); I != E; ++I) { 1175193323Sed const GlobalValue *GV = I; 1176193323Sed if (GV->hasLocalLinkage() || GV->isDeclaration() || 1177193323Sed GV->hasAppendingLinkage() || !GV->hasName()) 1178193323Sed continue;// Ignore external globals and globals with internal linkage. 1179218893Sdim 1180218893Sdim const GlobalValue *&GVEntry = 1181193323Sed LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; 1182193323Sed 1183193323Sed // If this is the first time we've seen this global, it is the canonical 1184193323Sed // version. 1185193323Sed if (!GVEntry) { 1186193323Sed GVEntry = GV; 1187193323Sed continue; 1188193323Sed } 1189218893Sdim 1190193323Sed // If the existing global is strong, never replace it. 1191193323Sed if (GVEntry->hasExternalLinkage() || 1192193323Sed GVEntry->hasDLLImportLinkage() || 1193193323Sed GVEntry->hasDLLExportLinkage()) 1194193323Sed continue; 1195218893Sdim 1196193323Sed // Otherwise, we know it's linkonce/weak, replace it if this is a strong 1197193323Sed // symbol. FIXME is this right for common? 1198193323Sed if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage()) 1199193323Sed GVEntry = GV; 1200193323Sed } 1201193323Sed } 1202193323Sed } 1203218893Sdim 1204193323Sed std::vector<const GlobalValue*> NonCanonicalGlobals; 1205193323Sed for (unsigned m = 0, e = Modules.size(); m != e; ++m) { 1206203954Srdivacky Module &M = *Modules[m]; 1207193323Sed for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 1208193323Sed I != E; ++I) { 1209193323Sed // In the multi-module case, see what this global maps to. 1210193323Sed if (!LinkedGlobalsMap.empty()) { 1211218893Sdim if (const GlobalValue *GVEntry = 1212193323Sed LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) { 1213193323Sed // If something else is the canonical global, ignore this one. 1214193323Sed if (GVEntry != &*I) { 1215193323Sed NonCanonicalGlobals.push_back(I); 1216193323Sed continue; 1217193323Sed } 1218193323Sed } 1219193323Sed } 1220218893Sdim 1221193323Sed if (!I->isDeclaration()) { 1222193323Sed addGlobalMapping(I, getMemoryForGV(I)); 1223193323Sed } else { 1224193323Sed // External variable reference. Try to use the dynamic loader to 1225193323Sed // get a pointer to it. 1226193323Sed if (void *SymAddr = 1227198090Srdivacky sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName())) 1228193323Sed addGlobalMapping(I, SymAddr); 1229193323Sed else { 1230207618Srdivacky report_fatal_error("Could not resolve external global address: " 1231198090Srdivacky +I->getName()); 1232193323Sed } 1233193323Sed } 1234193323Sed } 1235218893Sdim 1236193323Sed // If there are multiple modules, map the non-canonical globals to their 1237193323Sed // canonical location. 1238193323Sed if (!NonCanonicalGlobals.empty()) { 1239193323Sed for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) { 1240193323Sed const GlobalValue *GV = NonCanonicalGlobals[i]; 1241193323Sed const GlobalValue *CGV = 1242193323Sed LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; 1243193323Sed void *Ptr = getPointerToGlobalIfAvailable(CGV); 1244193323Sed assert(Ptr && "Canonical global wasn't codegen'd!"); 1245193323Sed addGlobalMapping(GV, Ptr); 1246193323Sed } 1247193323Sed } 1248218893Sdim 1249218893Sdim // Now that all of the globals are set up in memory, loop through them all 1250193323Sed // and initialize their contents. 1251193323Sed for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 1252193323Sed I != E; ++I) { 1253193323Sed if (!I->isDeclaration()) { 1254193323Sed if (!LinkedGlobalsMap.empty()) { 1255218893Sdim if (const GlobalValue *GVEntry = 1256193323Sed LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) 1257193323Sed if (GVEntry != &*I) // Not the canonical variable. 1258193323Sed continue; 1259193323Sed } 1260193323Sed EmitGlobalVariable(I); 1261193323Sed } 1262193323Sed } 1263193323Sed } 1264193323Sed} 1265193323Sed 1266193323Sed// EmitGlobalVariable - This method emits the specified global variable to the 1267193323Sed// address specified in GlobalAddresses, or allocates new memory if it's not 1268193323Sed// already in the map. 1269193323Sedvoid ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { 1270193323Sed void *GA = getPointerToGlobalIfAvailable(GV); 1271193323Sed 1272193323Sed if (GA == 0) { 1273193323Sed // If it's not already specified, allocate memory for the global. 1274193323Sed GA = getMemoryForGV(GV); 1275193323Sed addGlobalMapping(GV, GA); 1276193323Sed } 1277218893Sdim 1278193323Sed // Don't initialize if it's thread local, let the client do it. 1279193323Sed if (!GV->isThreadLocal()) 1280193323Sed InitializeMemory(GV->getInitializer(), GA); 1281218893Sdim 1282226633Sdim Type *ElTy = GV->getType()->getElementType(); 1283243830Sdim size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy); 1284193323Sed NumInitBytes += (unsigned)GVSize; 1285193323Sed ++NumGlobals; 1286193323Sed} 1287198090Srdivacky 1288198892SrdivackyExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE) 1289198892Srdivacky : EE(EE), GlobalAddressMap(this) { 1290198892Srdivacky} 1291198090Srdivacky 1292218893Sdimsys::Mutex * 1293218893SdimExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) { 1294198892Srdivacky return &EES->EE.lock; 1295198090Srdivacky} 1296218893Sdim 1297218893Sdimvoid ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES, 1298218893Sdim const GlobalValue *Old) { 1299198892Srdivacky void *OldVal = EES->GlobalAddressMap.lookup(Old); 1300198892Srdivacky EES->GlobalAddressReverseMap.erase(OldVal); 1301198892Srdivacky} 1302198090Srdivacky 1303218893Sdimvoid ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *, 1304218893Sdim const GlobalValue *, 1305218893Sdim const GlobalValue *) { 1306234353Sdim llvm_unreachable("The ExecutionEngine doesn't know how to handle a" 1307234353Sdim " RAUW on a value it has a global mapping for."); 1308198090Srdivacky} 1309