1857c21b4SMisha Brukman //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===// 2996fe010SChris Lattner // 3482202a6SJohn Criswell // The LLVM Compiler Infrastructure 4482202a6SJohn Criswell // 5f3ebc3f3SChris Lattner // This file is distributed under the University of Illinois Open Source 6f3ebc3f3SChris Lattner // License. See LICENSE.TXT for details. 7482202a6SJohn Criswell // 8482202a6SJohn Criswell //===----------------------------------------------------------------------===// 9482202a6SJohn Criswell // 10996fe010SChris Lattner // This file defines the common interface used by the various execution engine 11996fe010SChris Lattner // subclasses. 12996fe010SChris Lattner // 13996fe010SChris Lattner //===----------------------------------------------------------------------===// 14996fe010SChris Lattner 15ee937c80SChris Lattner #define DEBUG_TYPE "jit" 16996fe010SChris Lattner #include "llvm/Constants.h" 17260b0c88SMisha Brukman #include "llvm/DerivedTypes.h" 18996fe010SChris Lattner #include "llvm/Module.h" 19260b0c88SMisha Brukman #include "llvm/ModuleProvider.h" 2070e37278SReid Spencer #include "llvm/ADT/Statistic.h" 211202d1b1SDuncan Sands #include "llvm/Config/alloca.h" 22260b0c88SMisha Brukman #include "llvm/ExecutionEngine/ExecutionEngine.h" 23ad481312SChris Lattner #include "llvm/ExecutionEngine/GenericValue.h" 247c16caa3SReid Spencer #include "llvm/Support/Debug.h" 256c2d233eSTorok Edwin #include "llvm/Support/ErrorHandling.h" 266d8dd189SChris Lattner #include "llvm/Support/MutexGuard.h" 27ccb29cd2STorok Edwin #include "llvm/Support/raw_ostream.h" 2870e37278SReid Spencer #include "llvm/System/DynamicLibrary.h" 29fde55674SDuncan Sands #include "llvm/System/Host.h" 3070e37278SReid Spencer #include "llvm/Target/TargetData.h" 31579f0713SAnton Korobeynikov #include <cmath> 32579f0713SAnton Korobeynikov #include <cstring> 3329681deeSChris Lattner using namespace llvm; 34996fe010SChris Lattner 35c346ecd7SChris Lattner STATISTIC(NumInitBytes, "Number of bytes of global vars initialized"); 36c346ecd7SChris Lattner STATISTIC(NumGlobals , "Number of global vars initialized"); 37996fe010SChris Lattner 38fc8a2d5aSReid Kleckner ExecutionEngine *(*ExecutionEngine::JITCtor)(ModuleProvider *MP, 39fc8a2d5aSReid Kleckner std::string *ErrorStr, 40fc8a2d5aSReid Kleckner JITMemoryManager *JMM, 41fc8a2d5aSReid Kleckner CodeGenOpt::Level OptLevel, 42fc8a2d5aSReid Kleckner bool GVsWithCode) = 0; 43fc8a2d5aSReid Kleckner ExecutionEngine *(*ExecutionEngine::InterpCtor)(ModuleProvider *MP, 44fc8a2d5aSReid Kleckner std::string *ErrorStr) = 0; 4521ad494fSNicolas Geoffray ExecutionEngine::EERegisterFn ExecutionEngine::ExceptionTableRegister = 0; 4621ad494fSNicolas Geoffray 472d52c1b8SChris Lattner 48fd6f3257SChris Lattner ExecutionEngine::ExecutionEngine(ModuleProvider *P) : LazyFunctionCreator(0) { 4987aee74cSChris Lattner LazyCompilationDisabled = false; 50cdc0060eSEvan Cheng GVCompilationDisabled = false; 5184a9055eSEvan Cheng SymbolSearchingDisabled = false; 5218d85e74SNate Begeman DlsymStubsEnabled = false; 530621caefSChris Lattner Modules.push_back(P); 54260b0c88SMisha Brukman assert(P && "ModuleProvider is null?"); 55260b0c88SMisha Brukman } 56260b0c88SMisha Brukman 5792f8b30dSBrian Gaeke ExecutionEngine::~ExecutionEngine() { 58603682adSReid Spencer clearAllGlobalMappings(); 590621caefSChris Lattner for (unsigned i = 0, e = Modules.size(); i != e; ++i) 600621caefSChris Lattner delete Modules[i]; 6192f8b30dSBrian Gaeke } 6292f8b30dSBrian Gaeke 635457ce9aSNicolas Geoffray char* ExecutionEngine::getMemoryForGV(const GlobalVariable* GV) { 645457ce9aSNicolas Geoffray const Type *ElTy = GV->getType()->getElementType(); 65af9eaa83SDuncan Sands size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy); 665457ce9aSNicolas Geoffray return new char[GVSize]; 675457ce9aSNicolas Geoffray } 685457ce9aSNicolas Geoffray 69324fe890SDevang Patel /// removeModuleProvider - Remove a ModuleProvider from the list of modules. 70617001d8SNate Begeman /// Relases the Module from the ModuleProvider, materializing it in the 71617001d8SNate Begeman /// process, and returns the materialized Module. 72324fe890SDevang Patel Module* ExecutionEngine::removeModuleProvider(ModuleProvider *P, 73324fe890SDevang Patel std::string *ErrInfo) { 74324fe890SDevang Patel for(SmallVector<ModuleProvider *, 1>::iterator I = Modules.begin(), 75324fe890SDevang Patel E = Modules.end(); I != E; ++I) { 76324fe890SDevang Patel ModuleProvider *MP = *I; 77324fe890SDevang Patel if (MP == P) { 78324fe890SDevang Patel Modules.erase(I); 798f83fc4dSNate Begeman clearGlobalMappingsFromModule(MP->getModule()); 80324fe890SDevang Patel return MP->releaseModule(ErrInfo); 81324fe890SDevang Patel } 82324fe890SDevang Patel } 83324fe890SDevang Patel return NULL; 84324fe890SDevang Patel } 85324fe890SDevang Patel 86617001d8SNate Begeman /// deleteModuleProvider - Remove a ModuleProvider from the list of modules, 87617001d8SNate Begeman /// and deletes the ModuleProvider and owned Module. Avoids materializing 88617001d8SNate Begeman /// the underlying module. 89617001d8SNate Begeman void ExecutionEngine::deleteModuleProvider(ModuleProvider *P, 90617001d8SNate Begeman std::string *ErrInfo) { 91617001d8SNate Begeman for(SmallVector<ModuleProvider *, 1>::iterator I = Modules.begin(), 92617001d8SNate Begeman E = Modules.end(); I != E; ++I) { 93617001d8SNate Begeman ModuleProvider *MP = *I; 94617001d8SNate Begeman if (MP == P) { 95617001d8SNate Begeman Modules.erase(I); 96617001d8SNate Begeman clearGlobalMappingsFromModule(MP->getModule()); 97617001d8SNate Begeman delete MP; 98617001d8SNate Begeman return; 99617001d8SNate Begeman } 100617001d8SNate Begeman } 101617001d8SNate Begeman } 102617001d8SNate Begeman 1030621caefSChris Lattner /// FindFunctionNamed - Search all of the active modules to find the one that 1040621caefSChris Lattner /// defines FnName. This is very slow operation and shouldn't be used for 1050621caefSChris Lattner /// general code. 1060621caefSChris Lattner Function *ExecutionEngine::FindFunctionNamed(const char *FnName) { 1070621caefSChris Lattner for (unsigned i = 0, e = Modules.size(); i != e; ++i) { 1081241d6d5SReid Spencer if (Function *F = Modules[i]->getModule()->getFunction(FnName)) 1090621caefSChris Lattner return F; 1100621caefSChris Lattner } 1110621caefSChris Lattner return 0; 1120621caefSChris Lattner } 1130621caefSChris Lattner 1140621caefSChris Lattner 1156d8dd189SChris Lattner /// addGlobalMapping - Tell the execution engine that the specified global is 1166d8dd189SChris Lattner /// at the specified location. This is used internally as functions are JIT'd 1176d8dd189SChris Lattner /// and as global variables are laid out in memory. It can and should also be 1186d8dd189SChris Lattner /// used by clients of the EE that want to have an LLVM global overlay 1196d8dd189SChris Lattner /// existing data in memory. 1206d8dd189SChris Lattner void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) { 1216d8dd189SChris Lattner MutexGuard locked(lock); 1226d8dd189SChris Lattner 1239813b0b0SDaniel Dunbar DEBUG(errs() << "JIT: Map \'" << GV->getName() 1249813b0b0SDaniel Dunbar << "\' to [" << Addr << "]\n";); 1256d8dd189SChris Lattner void *&CurVal = state.getGlobalAddressMap(locked)[GV]; 1266d8dd189SChris Lattner assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!"); 1276d8dd189SChris Lattner CurVal = Addr; 1286d8dd189SChris Lattner 1296d8dd189SChris Lattner // If we are using the reverse mapping, add it too 1306d8dd189SChris Lattner if (!state.getGlobalAddressReverseMap(locked).empty()) { 1316d8dd189SChris Lattner const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr]; 1326d8dd189SChris Lattner assert((V == 0 || GV == 0) && "GlobalMapping already established!"); 1336d8dd189SChris Lattner V = GV; 1346d8dd189SChris Lattner } 1356d8dd189SChris Lattner } 1366d8dd189SChris Lattner 1376d8dd189SChris Lattner /// clearAllGlobalMappings - Clear all global mappings and start over again 1386d8dd189SChris Lattner /// use in dynamic compilation scenarios when you want to move globals 1396d8dd189SChris Lattner void ExecutionEngine::clearAllGlobalMappings() { 1406d8dd189SChris Lattner MutexGuard locked(lock); 1416d8dd189SChris Lattner 1426d8dd189SChris Lattner state.getGlobalAddressMap(locked).clear(); 1436d8dd189SChris Lattner state.getGlobalAddressReverseMap(locked).clear(); 1446d8dd189SChris Lattner } 1456d8dd189SChris Lattner 1468f83fc4dSNate Begeman /// clearGlobalMappingsFromModule - Clear all global mappings that came from a 1478f83fc4dSNate Begeman /// particular module, because it has been removed from the JIT. 1488f83fc4dSNate Begeman void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) { 1498f83fc4dSNate Begeman MutexGuard locked(lock); 1508f83fc4dSNate Begeman 1518f83fc4dSNate Begeman for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) { 1528f83fc4dSNate Begeman state.getGlobalAddressMap(locked).erase(FI); 1538f83fc4dSNate Begeman state.getGlobalAddressReverseMap(locked).erase(FI); 1548f83fc4dSNate Begeman } 1558f83fc4dSNate Begeman for (Module::global_iterator GI = M->global_begin(), GE = M->global_end(); 1568f83fc4dSNate Begeman GI != GE; ++GI) { 1578f83fc4dSNate Begeman state.getGlobalAddressMap(locked).erase(GI); 1588f83fc4dSNate Begeman state.getGlobalAddressReverseMap(locked).erase(GI); 1598f83fc4dSNate Begeman } 1608f83fc4dSNate Begeman } 1618f83fc4dSNate Begeman 1626d8dd189SChris Lattner /// updateGlobalMapping - Replace an existing mapping for GV with a new 1636d8dd189SChris Lattner /// address. This updates both maps as required. If "Addr" is null, the 1646d8dd189SChris Lattner /// entry for the global is removed from the mappings. 165ee181730SChris Lattner void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) { 1666d8dd189SChris Lattner MutexGuard locked(lock); 1676d8dd189SChris Lattner 168ee181730SChris Lattner std::map<const GlobalValue*, void *> &Map = state.getGlobalAddressMap(locked); 169ee181730SChris Lattner 1706d8dd189SChris Lattner // Deleting from the mapping? 1716d8dd189SChris Lattner if (Addr == 0) { 172ee181730SChris Lattner std::map<const GlobalValue*, void *>::iterator I = Map.find(GV); 173ee181730SChris Lattner void *OldVal; 174ee181730SChris Lattner if (I == Map.end()) 175ee181730SChris Lattner OldVal = 0; 176ee181730SChris Lattner else { 177ee181730SChris Lattner OldVal = I->second; 178ee181730SChris Lattner Map.erase(I); 1796d8dd189SChris Lattner } 1806d8dd189SChris Lattner 181ee181730SChris Lattner if (!state.getGlobalAddressReverseMap(locked).empty()) 182*337b124aSJeffrey Yasskin state.getGlobalAddressReverseMap(locked).erase(OldVal); 183ee181730SChris Lattner return OldVal; 184ee181730SChris Lattner } 185ee181730SChris Lattner 186ee181730SChris Lattner void *&CurVal = Map[GV]; 187ee181730SChris Lattner void *OldVal = CurVal; 188ee181730SChris Lattner 1896d8dd189SChris Lattner if (CurVal && !state.getGlobalAddressReverseMap(locked).empty()) 1906d8dd189SChris Lattner state.getGlobalAddressReverseMap(locked).erase(CurVal); 1916d8dd189SChris Lattner CurVal = Addr; 1926d8dd189SChris Lattner 1936d8dd189SChris Lattner // If we are using the reverse mapping, add it too 1946d8dd189SChris Lattner if (!state.getGlobalAddressReverseMap(locked).empty()) { 1956d8dd189SChris Lattner const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr]; 1966d8dd189SChris Lattner assert((V == 0 || GV == 0) && "GlobalMapping already established!"); 1976d8dd189SChris Lattner V = GV; 1986d8dd189SChris Lattner } 199ee181730SChris Lattner return OldVal; 2006d8dd189SChris Lattner } 2016d8dd189SChris Lattner 2026d8dd189SChris Lattner /// getPointerToGlobalIfAvailable - This returns the address of the specified 2036d8dd189SChris Lattner /// global value if it is has already been codegen'd, otherwise it returns null. 2046d8dd189SChris Lattner /// 2056d8dd189SChris Lattner void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) { 2066d8dd189SChris Lattner MutexGuard locked(lock); 2076d8dd189SChris Lattner 2086d8dd189SChris Lattner std::map<const GlobalValue*, void*>::iterator I = 2096d8dd189SChris Lattner state.getGlobalAddressMap(locked).find(GV); 2106d8dd189SChris Lattner return I != state.getGlobalAddressMap(locked).end() ? I->second : 0; 2116d8dd189SChris Lattner } 2126d8dd189SChris Lattner 213748e8579SChris Lattner /// getGlobalValueAtAddress - Return the LLVM global value object that starts 214748e8579SChris Lattner /// at the specified address. 215748e8579SChris Lattner /// 216748e8579SChris Lattner const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { 21779876f52SReid Spencer MutexGuard locked(lock); 21879876f52SReid Spencer 219748e8579SChris Lattner // If we haven't computed the reverse mapping yet, do so first. 22079876f52SReid Spencer if (state.getGlobalAddressReverseMap(locked).empty()) { 2216d8dd189SChris Lattner for (std::map<const GlobalValue*, void *>::iterator 2226d8dd189SChris Lattner I = state.getGlobalAddressMap(locked).begin(), 2236d8dd189SChris Lattner E = state.getGlobalAddressMap(locked).end(); I != E; ++I) 2246d8dd189SChris Lattner state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, 2256d8dd189SChris Lattner I->first)); 226748e8579SChris Lattner } 227748e8579SChris Lattner 228748e8579SChris Lattner std::map<void *, const GlobalValue*>::iterator I = 22979876f52SReid Spencer state.getGlobalAddressReverseMap(locked).find(Addr); 23079876f52SReid Spencer return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0; 231748e8579SChris Lattner } 2325a0d4829SChris Lattner 2335a0d4829SChris Lattner // CreateArgv - Turn a vector of strings into a nice argv style array of 2345a0d4829SChris Lattner // pointers to null terminated strings. 2355a0d4829SChris Lattner // 2365a0d4829SChris Lattner static void *CreateArgv(ExecutionEngine *EE, 2375a0d4829SChris Lattner const std::vector<std::string> &InputArgv) { 23820a631fdSOwen Anderson unsigned PtrSize = EE->getTargetData()->getPointerSize(); 2395a0d4829SChris Lattner char *Result = new char[(InputArgv.size()+1)*PtrSize]; 2405a0d4829SChris Lattner 241972fd1a1SEvan Cheng DOUT << "JIT: ARGV = " << (void*)Result << "\n"; 242edf07887SChristopher Lamb const Type *SBytePtr = PointerType::getUnqual(Type::Int8Ty); 2435a0d4829SChris Lattner 2445a0d4829SChris Lattner for (unsigned i = 0; i != InputArgv.size(); ++i) { 2455a0d4829SChris Lattner unsigned Size = InputArgv[i].size()+1; 2465a0d4829SChris Lattner char *Dest = new char[Size]; 247972fd1a1SEvan Cheng DOUT << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n"; 2485a0d4829SChris Lattner 2495a0d4829SChris Lattner std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); 2505a0d4829SChris Lattner Dest[Size-1] = 0; 2515a0d4829SChris Lattner 2525a0d4829SChris Lattner // Endian safe: Result[i] = (PointerTy)Dest; 2535a0d4829SChris Lattner EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize), 2545a0d4829SChris Lattner SBytePtr); 2555a0d4829SChris Lattner } 2565a0d4829SChris Lattner 2575a0d4829SChris Lattner // Null terminate it 2585a0d4829SChris Lattner EE->StoreValueToMemory(PTOGV(0), 2595a0d4829SChris Lattner (GenericValue*)(Result+InputArgv.size()*PtrSize), 2605a0d4829SChris Lattner SBytePtr); 2615a0d4829SChris Lattner return Result; 2625a0d4829SChris Lattner } 2635a0d4829SChris Lattner 264faae50b6SChris Lattner 265faae50b6SChris Lattner /// runStaticConstructorsDestructors - This method is used to execute all of 2661a9a0b7bSEvan Cheng /// the static constructors or destructors for a module, depending on the 267faae50b6SChris Lattner /// value of isDtors. 2681a9a0b7bSEvan Cheng void ExecutionEngine::runStaticConstructorsDestructors(Module *module, bool isDtors) { 269faae50b6SChris Lattner const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors"; 2700621caefSChris Lattner 2710621caefSChris Lattner // Execute global ctors/dtors for each module in the program. 2721a9a0b7bSEvan Cheng 2731a9a0b7bSEvan Cheng GlobalVariable *GV = module->getNamedGlobal(Name); 274fe36eaebSChris Lattner 275fe36eaebSChris Lattner // If this global has internal linkage, or if it has a use, then it must be 276fe36eaebSChris Lattner // an old-style (llvmgcc3) static ctor with __main linked in and in use. If 2770621caefSChris Lattner // this is the case, don't execute any of the global ctors, __main will do 2780621caefSChris Lattner // it. 2796de96a1bSRafael Espindola if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return; 280faae50b6SChris Lattner 2810621caefSChris Lattner // Should be an array of '{ int, void ()* }' structs. The first value is 2820621caefSChris Lattner // the init priority, which we ignore. 283faae50b6SChris Lattner ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 2841a9a0b7bSEvan Cheng if (!InitList) return; 285faae50b6SChris Lattner for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) 2860621caefSChris Lattner if (ConstantStruct *CS = 2870621caefSChris Lattner dyn_cast<ConstantStruct>(InitList->getOperand(i))) { 2881a9a0b7bSEvan Cheng if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. 289faae50b6SChris Lattner 290faae50b6SChris Lattner Constant *FP = CS->getOperand(1); 291faae50b6SChris Lattner if (FP->isNullValue()) 2920621caefSChris Lattner break; // Found a null terminator, exit. 293faae50b6SChris Lattner 294faae50b6SChris Lattner if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) 2956c38f0bbSReid Spencer if (CE->isCast()) 296faae50b6SChris Lattner FP = CE->getOperand(0); 297faae50b6SChris Lattner if (Function *F = dyn_cast<Function>(FP)) { 298faae50b6SChris Lattner // Execute the ctor/dtor function! 299faae50b6SChris Lattner runFunction(F, std::vector<GenericValue>()); 300faae50b6SChris Lattner } 301faae50b6SChris Lattner } 302faae50b6SChris Lattner } 3031a9a0b7bSEvan Cheng 3041a9a0b7bSEvan Cheng /// runStaticConstructorsDestructors - This method is used to execute all of 3051a9a0b7bSEvan Cheng /// the static constructors or destructors for a program, depending on the 3061a9a0b7bSEvan Cheng /// value of isDtors. 3071a9a0b7bSEvan Cheng void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) { 3081a9a0b7bSEvan Cheng // Execute global ctors/dtors for each module in the program. 3091a9a0b7bSEvan Cheng for (unsigned m = 0, e = Modules.size(); m != e; ++m) 3101a9a0b7bSEvan Cheng runStaticConstructorsDestructors(Modules[m]->getModule(), isDtors); 3110621caefSChris Lattner } 312faae50b6SChris Lattner 313cf3e3017SDan Gohman #ifndef NDEBUG 3141202d1b1SDuncan Sands /// isTargetNullPtr - Return whether the target pointer stored at Loc is null. 3151202d1b1SDuncan Sands static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) { 3161202d1b1SDuncan Sands unsigned PtrSize = EE->getTargetData()->getPointerSize(); 3171202d1b1SDuncan Sands for (unsigned i = 0; i < PtrSize; ++i) 3181202d1b1SDuncan Sands if (*(i + (uint8_t*)Loc)) 3191202d1b1SDuncan Sands return false; 3201202d1b1SDuncan Sands return true; 3211202d1b1SDuncan Sands } 322cf3e3017SDan Gohman #endif 3231202d1b1SDuncan Sands 3245a0d4829SChris Lattner /// runFunctionAsMain - This is a helper function which wraps runFunction to 3255a0d4829SChris Lattner /// handle the common task of starting up main with the specified argc, argv, 3265a0d4829SChris Lattner /// and envp parameters. 3275a0d4829SChris Lattner int ExecutionEngine::runFunctionAsMain(Function *Fn, 3285a0d4829SChris Lattner const std::vector<std::string> &argv, 3295a0d4829SChris Lattner const char * const * envp) { 3305a0d4829SChris Lattner std::vector<GenericValue> GVArgs; 3315a0d4829SChris Lattner GenericValue GVArgc; 33287aa65f4SReid Spencer GVArgc.IntVal = APInt(32, argv.size()); 3338c32c111SAnton Korobeynikov 3348c32c111SAnton Korobeynikov // Check main() type 335b1cad0b3SChris Lattner unsigned NumArgs = Fn->getFunctionType()->getNumParams(); 3368c32c111SAnton Korobeynikov const FunctionType *FTy = Fn->getFunctionType(); 337edf07887SChristopher Lamb const Type* PPInt8Ty = 338edf07887SChristopher Lamb PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty)); 3398c32c111SAnton Korobeynikov switch (NumArgs) { 3408c32c111SAnton Korobeynikov case 3: 3418c32c111SAnton Korobeynikov if (FTy->getParamType(2) != PPInt8Ty) { 342ccb29cd2STorok Edwin llvm_report_error("Invalid type for third argument of main() supplied"); 3438c32c111SAnton Korobeynikov } 344b781886dSAnton Korobeynikov // FALLS THROUGH 3458c32c111SAnton Korobeynikov case 2: 3468c32c111SAnton Korobeynikov if (FTy->getParamType(1) != PPInt8Ty) { 347ccb29cd2STorok Edwin llvm_report_error("Invalid type for second argument of main() supplied"); 3488c32c111SAnton Korobeynikov } 349b781886dSAnton Korobeynikov // FALLS THROUGH 3508c32c111SAnton Korobeynikov case 1: 3518c32c111SAnton Korobeynikov if (FTy->getParamType(0) != Type::Int32Ty) { 352ccb29cd2STorok Edwin llvm_report_error("Invalid type for first argument of main() supplied"); 3538c32c111SAnton Korobeynikov } 354b781886dSAnton Korobeynikov // FALLS THROUGH 3558c32c111SAnton Korobeynikov case 0: 356370ec10dSChris Lattner if (!isa<IntegerType>(FTy->getReturnType()) && 3578c32c111SAnton Korobeynikov FTy->getReturnType() != Type::VoidTy) { 358ccb29cd2STorok Edwin llvm_report_error("Invalid return type of main() supplied"); 3598c32c111SAnton Korobeynikov } 3608c32c111SAnton Korobeynikov break; 3618c32c111SAnton Korobeynikov default: 362ccb29cd2STorok Edwin llvm_report_error("Invalid number of arguments of main() supplied"); 3638c32c111SAnton Korobeynikov } 3648c32c111SAnton Korobeynikov 365b1cad0b3SChris Lattner if (NumArgs) { 3665a0d4829SChris Lattner GVArgs.push_back(GVArgc); // Arg #0 = argc. 367b1cad0b3SChris Lattner if (NumArgs > 1) { 3685a0d4829SChris Lattner GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv. 3691202d1b1SDuncan Sands assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) && 370b1cad0b3SChris Lattner "argv[0] was null after CreateArgv"); 371b1cad0b3SChris Lattner if (NumArgs > 2) { 3725a0d4829SChris Lattner std::vector<std::string> EnvVars; 3735a0d4829SChris Lattner for (unsigned i = 0; envp[i]; ++i) 3745a0d4829SChris Lattner EnvVars.push_back(envp[i]); 3755a0d4829SChris Lattner GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp. 376b1cad0b3SChris Lattner } 377b1cad0b3SChris Lattner } 378b1cad0b3SChris Lattner } 37987aa65f4SReid Spencer return runFunction(Fn, GVArgs).IntVal.getZExtValue(); 3805a0d4829SChris Lattner } 3815a0d4829SChris Lattner 382260b0c88SMisha Brukman /// If possible, create a JIT, unless the caller specifically requests an 383260b0c88SMisha Brukman /// Interpreter or there's an error. If even an Interpreter cannot be created, 384260b0c88SMisha Brukman /// NULL is returned. 385857c21b4SMisha Brukman /// 3862f1e2002SMisha Brukman ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP, 387603682adSReid Spencer bool ForceInterpreter, 3887ff05bf5SEvan Cheng std::string *ErrorStr, 38970415d97SJeffrey Yasskin CodeGenOpt::Level OptLevel, 39070415d97SJeffrey Yasskin bool GVsWithCode) { 391fc8a2d5aSReid Kleckner return EngineBuilder(MP) 392fc8a2d5aSReid Kleckner .setEngineKind(ForceInterpreter 393fc8a2d5aSReid Kleckner ? EngineKind::Interpreter 394fc8a2d5aSReid Kleckner : EngineKind::JIT) 395fc8a2d5aSReid Kleckner .setErrorStr(ErrorStr) 396fc8a2d5aSReid Kleckner .setOptLevel(OptLevel) 397fc8a2d5aSReid Kleckner .setAllocateGVsWithCode(GVsWithCode) 398fc8a2d5aSReid Kleckner .create(); 399fc8a2d5aSReid Kleckner } 4004bd3bd5bSBrian Gaeke 401fc8a2d5aSReid Kleckner ExecutionEngine *ExecutionEngine::create(Module *M) { 402fc8a2d5aSReid Kleckner return EngineBuilder(M).create(); 403fc8a2d5aSReid Kleckner } 404fc8a2d5aSReid Kleckner 405fc8a2d5aSReid Kleckner /// EngineBuilder - Overloaded constructor that automatically creates an 406fc8a2d5aSReid Kleckner /// ExistingModuleProvider for an existing module. 407fc8a2d5aSReid Kleckner EngineBuilder::EngineBuilder(Module *m) : MP(new ExistingModuleProvider(m)) { 408fc8a2d5aSReid Kleckner InitEngine(); 409fc8a2d5aSReid Kleckner } 410fc8a2d5aSReid Kleckner 411fc8a2d5aSReid Kleckner ExecutionEngine *EngineBuilder::create() { 412a53414fdSNick Lewycky // Make sure we can resolve symbols in the program as well. The zero arg 413a53414fdSNick Lewycky // to the function tells DynamicLibrary to load the program, not a library. 414a53414fdSNick Lewycky if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr)) 415a53414fdSNick Lewycky return 0; 416a53414fdSNick Lewycky 417fc8a2d5aSReid Kleckner // If the user specified a memory manager but didn't specify which engine to 418fc8a2d5aSReid Kleckner // create, we assume they only want the JIT, and we fail if they only want 419fc8a2d5aSReid Kleckner // the interpreter. 420fc8a2d5aSReid Kleckner if (JMM) { 421fc8a2d5aSReid Kleckner if (WhichEngine & EngineKind::JIT) { 422fc8a2d5aSReid Kleckner WhichEngine = EngineKind::JIT; 423fc8a2d5aSReid Kleckner } else { 424fc8a2d5aSReid Kleckner *ErrorStr = "Cannot create an interpreter with a memory manager."; 425fc8a2d5aSReid Kleckner } 4264bd3bd5bSBrian Gaeke } 4274bd3bd5bSBrian Gaeke 428fc8a2d5aSReid Kleckner ExecutionEngine *EE = 0; 429fc8a2d5aSReid Kleckner 430fc8a2d5aSReid Kleckner // Unless the interpreter was explicitly selected or the JIT is not linked, 431fc8a2d5aSReid Kleckner // try making a JIT. 432fc8a2d5aSReid Kleckner if (WhichEngine & EngineKind::JIT && ExecutionEngine::JITCtor) { 433fc8a2d5aSReid Kleckner EE = ExecutionEngine::JITCtor(MP, ErrorStr, JMM, OptLevel, 434fc8a2d5aSReid Kleckner AllocateGVsWithCode); 435fc8a2d5aSReid Kleckner } 436fc8a2d5aSReid Kleckner 437fc8a2d5aSReid Kleckner // If we can't make a JIT and we didn't request one specifically, try making 438fc8a2d5aSReid Kleckner // an interpreter instead. 439fc8a2d5aSReid Kleckner if (WhichEngine & EngineKind::Interpreter && EE == 0 && 440fc8a2d5aSReid Kleckner ExecutionEngine::InterpCtor) { 441fc8a2d5aSReid Kleckner EE = ExecutionEngine::InterpCtor(MP, ErrorStr); 442fc8a2d5aSReid Kleckner } 443fc8a2d5aSReid Kleckner 444fc8a2d5aSReid Kleckner return EE; 445b5163bb9SChris Lattner } 446b5163bb9SChris Lattner 447857c21b4SMisha Brukman /// getPointerToGlobal - This returns the address of the specified global 448857c21b4SMisha Brukman /// value. This may involve code generation if it's a function. 449857c21b4SMisha Brukman /// 450996fe010SChris Lattner void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { 4511678e859SBrian Gaeke if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) 452996fe010SChris Lattner return getPointerToFunction(F); 453996fe010SChris Lattner 45479876f52SReid Spencer MutexGuard locked(lock); 45569e84901SJeff Cohen void *p = state.getGlobalAddressMap(locked)[GV]; 45669e84901SJeff Cohen if (p) 45769e84901SJeff Cohen return p; 45869e84901SJeff Cohen 45969e84901SJeff Cohen // Global variable might have been added since interpreter started. 46069e84901SJeff Cohen if (GlobalVariable *GVar = 46169e84901SJeff Cohen const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV))) 46269e84901SJeff Cohen EmitGlobalVariable(GVar); 46369e84901SJeff Cohen else 464fbcc663cSTorok Edwin llvm_unreachable("Global hasn't had an address allocated yet!"); 46579876f52SReid Spencer return state.getGlobalAddressMap(locked)[GV]; 466996fe010SChris Lattner } 467996fe010SChris Lattner 4686c38f0bbSReid Spencer /// This function converts a Constant* into a GenericValue. The interesting 4696c38f0bbSReid Spencer /// part is if C is a ConstantExpr. 4702dc9f132SReid Spencer /// @brief Get a GenericValue for a Constant* 471996fe010SChris Lattner GenericValue ExecutionEngine::getConstantValue(const Constant *C) { 4726c38f0bbSReid Spencer // If its undefined, return the garbage. 4734fd528f2SReid Spencer if (isa<UndefValue>(C)) 4744fd528f2SReid Spencer return GenericValue(); 4759de0d14dSChris Lattner 4766c38f0bbSReid Spencer // If the value is a ConstantExpr 4776c38f0bbSReid Spencer if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 4784fd528f2SReid Spencer Constant *Op0 = CE->getOperand(0); 4799de0d14dSChris Lattner switch (CE->getOpcode()) { 4809de0d14dSChris Lattner case Instruction::GetElementPtr: { 4816c38f0bbSReid Spencer // Compute the index 4824fd528f2SReid Spencer GenericValue Result = getConstantValue(Op0); 483c44bd78aSChris Lattner SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end()); 4849de0d14dSChris Lattner uint64_t Offset = 4854fd528f2SReid Spencer TD->getIndexedOffset(Op0->getType(), &Indices[0], Indices.size()); 4869de0d14dSChris Lattner 48787aa65f4SReid Spencer char* tmp = (char*) Result.PointerVal; 48887aa65f4SReid Spencer Result = PTOGV(tmp + Offset); 4899de0d14dSChris Lattner return Result; 4909de0d14dSChris Lattner } 4914fd528f2SReid Spencer case Instruction::Trunc: { 4924fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 4934fd528f2SReid Spencer uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 4944fd528f2SReid Spencer GV.IntVal = GV.IntVal.trunc(BitWidth); 4954fd528f2SReid Spencer return GV; 4964fd528f2SReid Spencer } 4974fd528f2SReid Spencer case Instruction::ZExt: { 4984fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 4994fd528f2SReid Spencer uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 5004fd528f2SReid Spencer GV.IntVal = GV.IntVal.zext(BitWidth); 5014fd528f2SReid Spencer return GV; 5024fd528f2SReid Spencer } 5034fd528f2SReid Spencer case Instruction::SExt: { 5044fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5054fd528f2SReid Spencer uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 5064fd528f2SReid Spencer GV.IntVal = GV.IntVal.sext(BitWidth); 5074fd528f2SReid Spencer return GV; 5084fd528f2SReid Spencer } 5094fd528f2SReid Spencer case Instruction::FPTrunc: { 510a1336cf5SDale Johannesen // FIXME long double 5114fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5124fd528f2SReid Spencer GV.FloatVal = float(GV.DoubleVal); 5134fd528f2SReid Spencer return GV; 5144fd528f2SReid Spencer } 5154fd528f2SReid Spencer case Instruction::FPExt:{ 516a1336cf5SDale Johannesen // FIXME long double 5174fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5184fd528f2SReid Spencer GV.DoubleVal = double(GV.FloatVal); 5194fd528f2SReid Spencer return GV; 5204fd528f2SReid Spencer } 5214fd528f2SReid Spencer case Instruction::UIToFP: { 5224fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5234fd528f2SReid Spencer if (CE->getType() == Type::FloatTy) 5244fd528f2SReid Spencer GV.FloatVal = float(GV.IntVal.roundToDouble()); 525a1336cf5SDale Johannesen else if (CE->getType() == Type::DoubleTy) 5264fd528f2SReid Spencer GV.DoubleVal = GV.IntVal.roundToDouble(); 527a1336cf5SDale Johannesen else if (CE->getType() == Type::X86_FP80Ty) { 528a1336cf5SDale Johannesen const uint64_t zero[] = {0, 0}; 529a1336cf5SDale Johannesen APFloat apf = APFloat(APInt(80, 2, zero)); 530ca24fd90SDan Gohman (void)apf.convertFromAPInt(GV.IntVal, 531ca24fd90SDan Gohman false, 5329150652bSDale Johannesen APFloat::rmNearestTiesToEven); 53354306fe4SDale Johannesen GV.IntVal = apf.bitcastToAPInt(); 534a1336cf5SDale Johannesen } 5354fd528f2SReid Spencer return GV; 5364fd528f2SReid Spencer } 5374fd528f2SReid Spencer case Instruction::SIToFP: { 5384fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5394fd528f2SReid Spencer if (CE->getType() == Type::FloatTy) 5404fd528f2SReid Spencer GV.FloatVal = float(GV.IntVal.signedRoundToDouble()); 541a1336cf5SDale Johannesen else if (CE->getType() == Type::DoubleTy) 5424fd528f2SReid Spencer GV.DoubleVal = GV.IntVal.signedRoundToDouble(); 543a1336cf5SDale Johannesen else if (CE->getType() == Type::X86_FP80Ty) { 544a1336cf5SDale Johannesen const uint64_t zero[] = { 0, 0}; 545a1336cf5SDale Johannesen APFloat apf = APFloat(APInt(80, 2, zero)); 546ca24fd90SDan Gohman (void)apf.convertFromAPInt(GV.IntVal, 547ca24fd90SDan Gohman true, 5489150652bSDale Johannesen APFloat::rmNearestTiesToEven); 54954306fe4SDale Johannesen GV.IntVal = apf.bitcastToAPInt(); 550a1336cf5SDale Johannesen } 5514fd528f2SReid Spencer return GV; 5524fd528f2SReid Spencer } 5534fd528f2SReid Spencer case Instruction::FPToUI: // double->APInt conversion handles sign 5544fd528f2SReid Spencer case Instruction::FPToSI: { 5554fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5564fd528f2SReid Spencer uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 5574fd528f2SReid Spencer if (Op0->getType() == Type::FloatTy) 5584fd528f2SReid Spencer GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth); 559a1336cf5SDale Johannesen else if (Op0->getType() == Type::DoubleTy) 5604fd528f2SReid Spencer GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth); 561a1336cf5SDale Johannesen else if (Op0->getType() == Type::X86_FP80Ty) { 562a1336cf5SDale Johannesen APFloat apf = APFloat(GV.IntVal); 563a1336cf5SDale Johannesen uint64_t v; 5644f0bd68cSDale Johannesen bool ignored; 565a1336cf5SDale Johannesen (void)apf.convertToInteger(&v, BitWidth, 566a1336cf5SDale Johannesen CE->getOpcode()==Instruction::FPToSI, 5674f0bd68cSDale Johannesen APFloat::rmTowardZero, &ignored); 568a1336cf5SDale Johannesen GV.IntVal = v; // endian? 569a1336cf5SDale Johannesen } 5704fd528f2SReid Spencer return GV; 5714fd528f2SReid Spencer } 5726c38f0bbSReid Spencer case Instruction::PtrToInt: { 5734fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5744fd528f2SReid Spencer uint32_t PtrWidth = TD->getPointerSizeInBits(); 5754fd528f2SReid Spencer GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal)); 5764fd528f2SReid Spencer return GV; 5774fd528f2SReid Spencer } 5784fd528f2SReid Spencer case Instruction::IntToPtr: { 5794fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5804fd528f2SReid Spencer uint32_t PtrWidth = TD->getPointerSizeInBits(); 5814fd528f2SReid Spencer if (PtrWidth != GV.IntVal.getBitWidth()) 5824fd528f2SReid Spencer GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth); 5834fd528f2SReid Spencer assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width"); 5844fd528f2SReid Spencer GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue())); 5856c38f0bbSReid Spencer return GV; 5866c38f0bbSReid Spencer } 5876c38f0bbSReid Spencer case Instruction::BitCast: { 5884fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5894fd528f2SReid Spencer const Type* DestTy = CE->getType(); 5904fd528f2SReid Spencer switch (Op0->getType()->getTypeID()) { 591fbcc663cSTorok Edwin default: llvm_unreachable("Invalid bitcast operand"); 5924fd528f2SReid Spencer case Type::IntegerTyID: 5934fd528f2SReid Spencer assert(DestTy->isFloatingPoint() && "invalid bitcast"); 5944fd528f2SReid Spencer if (DestTy == Type::FloatTy) 5954fd528f2SReid Spencer GV.FloatVal = GV.IntVal.bitsToFloat(); 5964fd528f2SReid Spencer else if (DestTy == Type::DoubleTy) 5974fd528f2SReid Spencer GV.DoubleVal = GV.IntVal.bitsToDouble(); 5986c38f0bbSReid Spencer break; 5994fd528f2SReid Spencer case Type::FloatTyID: 6004fd528f2SReid Spencer assert(DestTy == Type::Int32Ty && "Invalid bitcast"); 6014fd528f2SReid Spencer GV.IntVal.floatToBits(GV.FloatVal); 6024fd528f2SReid Spencer break; 6034fd528f2SReid Spencer case Type::DoubleTyID: 6044fd528f2SReid Spencer assert(DestTy == Type::Int64Ty && "Invalid bitcast"); 6054fd528f2SReid Spencer GV.IntVal.doubleToBits(GV.DoubleVal); 6064fd528f2SReid Spencer break; 6074fd528f2SReid Spencer case Type::PointerTyID: 6084fd528f2SReid Spencer assert(isa<PointerType>(DestTy) && "Invalid bitcast"); 6094fd528f2SReid Spencer break; // getConstantValue(Op0) above already converted it 6106c38f0bbSReid Spencer } 6114fd528f2SReid Spencer return GV; 61268cbcc3eSChris Lattner } 61368cbcc3eSChris Lattner case Instruction::Add: 614a5b9645cSDan Gohman case Instruction::FAdd: 6154fd528f2SReid Spencer case Instruction::Sub: 616a5b9645cSDan Gohman case Instruction::FSub: 6174fd528f2SReid Spencer case Instruction::Mul: 618a5b9645cSDan Gohman case Instruction::FMul: 6194fd528f2SReid Spencer case Instruction::UDiv: 6204fd528f2SReid Spencer case Instruction::SDiv: 6214fd528f2SReid Spencer case Instruction::URem: 6224fd528f2SReid Spencer case Instruction::SRem: 6234fd528f2SReid Spencer case Instruction::And: 6244fd528f2SReid Spencer case Instruction::Or: 6254fd528f2SReid Spencer case Instruction::Xor: { 6264fd528f2SReid Spencer GenericValue LHS = getConstantValue(Op0); 6274fd528f2SReid Spencer GenericValue RHS = getConstantValue(CE->getOperand(1)); 6284fd528f2SReid Spencer GenericValue GV; 629c4e6bb5fSChris Lattner switch (CE->getOperand(0)->getType()->getTypeID()) { 630fbcc663cSTorok Edwin default: llvm_unreachable("Bad add type!"); 6317a9c62baSReid Spencer case Type::IntegerTyID: 6324fd528f2SReid Spencer switch (CE->getOpcode()) { 633fbcc663cSTorok Edwin default: llvm_unreachable("Invalid integer opcode"); 6344fd528f2SReid Spencer case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break; 6354fd528f2SReid Spencer case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break; 6364fd528f2SReid Spencer case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break; 6374fd528f2SReid Spencer case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break; 6384fd528f2SReid Spencer case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break; 6394fd528f2SReid Spencer case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break; 6404fd528f2SReid Spencer case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break; 6414fd528f2SReid Spencer case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break; 6424fd528f2SReid Spencer case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break; 6434fd528f2SReid Spencer case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break; 6444fd528f2SReid Spencer } 645c4e6bb5fSChris Lattner break; 646c4e6bb5fSChris Lattner case Type::FloatTyID: 6474fd528f2SReid Spencer switch (CE->getOpcode()) { 648fbcc663cSTorok Edwin default: llvm_unreachable("Invalid float opcode"); 649a5b9645cSDan Gohman case Instruction::FAdd: 6504fd528f2SReid Spencer GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break; 651a5b9645cSDan Gohman case Instruction::FSub: 6524fd528f2SReid Spencer GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break; 653a5b9645cSDan Gohman case Instruction::FMul: 6544fd528f2SReid Spencer GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break; 6554fd528f2SReid Spencer case Instruction::FDiv: 6564fd528f2SReid Spencer GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break; 6574fd528f2SReid Spencer case Instruction::FRem: 6584fd528f2SReid Spencer GV.FloatVal = ::fmodf(LHS.FloatVal,RHS.FloatVal); break; 6594fd528f2SReid Spencer } 660c4e6bb5fSChris Lattner break; 661c4e6bb5fSChris Lattner case Type::DoubleTyID: 6624fd528f2SReid Spencer switch (CE->getOpcode()) { 663fbcc663cSTorok Edwin default: llvm_unreachable("Invalid double opcode"); 664a5b9645cSDan Gohman case Instruction::FAdd: 6654fd528f2SReid Spencer GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break; 666a5b9645cSDan Gohman case Instruction::FSub: 6674fd528f2SReid Spencer GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break; 668a5b9645cSDan Gohman case Instruction::FMul: 6694fd528f2SReid Spencer GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break; 6704fd528f2SReid Spencer case Instruction::FDiv: 6714fd528f2SReid Spencer GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break; 6724fd528f2SReid Spencer case Instruction::FRem: 6734fd528f2SReid Spencer GV.DoubleVal = ::fmod(LHS.DoubleVal,RHS.DoubleVal); break; 6744fd528f2SReid Spencer } 675c4e6bb5fSChris Lattner break; 676a1336cf5SDale Johannesen case Type::X86_FP80TyID: 677a1336cf5SDale Johannesen case Type::PPC_FP128TyID: 678a1336cf5SDale Johannesen case Type::FP128TyID: { 679a1336cf5SDale Johannesen APFloat apfLHS = APFloat(LHS.IntVal); 680a1336cf5SDale Johannesen switch (CE->getOpcode()) { 681fbcc663cSTorok Edwin default: llvm_unreachable("Invalid long double opcode");llvm_unreachable(0); 682a5b9645cSDan Gohman case Instruction::FAdd: 683a1336cf5SDale Johannesen apfLHS.add(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 68454306fe4SDale Johannesen GV.IntVal = apfLHS.bitcastToAPInt(); 685a1336cf5SDale Johannesen break; 686a5b9645cSDan Gohman case Instruction::FSub: 687a1336cf5SDale Johannesen apfLHS.subtract(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 68854306fe4SDale Johannesen GV.IntVal = apfLHS.bitcastToAPInt(); 689a1336cf5SDale Johannesen break; 690a5b9645cSDan Gohman case Instruction::FMul: 691a1336cf5SDale Johannesen apfLHS.multiply(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 69254306fe4SDale Johannesen GV.IntVal = apfLHS.bitcastToAPInt(); 693a1336cf5SDale Johannesen break; 694a1336cf5SDale Johannesen case Instruction::FDiv: 695a1336cf5SDale Johannesen apfLHS.divide(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 69654306fe4SDale Johannesen GV.IntVal = apfLHS.bitcastToAPInt(); 697a1336cf5SDale Johannesen break; 698a1336cf5SDale Johannesen case Instruction::FRem: 699a1336cf5SDale Johannesen apfLHS.mod(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 70054306fe4SDale Johannesen GV.IntVal = apfLHS.bitcastToAPInt(); 701a1336cf5SDale Johannesen break; 702a1336cf5SDale Johannesen } 703a1336cf5SDale Johannesen } 704a1336cf5SDale Johannesen break; 705c4e6bb5fSChris Lattner } 7064fd528f2SReid Spencer return GV; 7074fd528f2SReid Spencer } 7089de0d14dSChris Lattner default: 70968cbcc3eSChris Lattner break; 71068cbcc3eSChris Lattner } 711ccb29cd2STorok Edwin std::string msg; 712ccb29cd2STorok Edwin raw_string_ostream Msg(msg); 713ccb29cd2STorok Edwin Msg << "ConstantExpr not handled: " << *CE; 714ccb29cd2STorok Edwin llvm_report_error(Msg.str()); 7159de0d14dSChris Lattner } 716996fe010SChris Lattner 7174fd528f2SReid Spencer GenericValue Result; 7186b727599SChris Lattner switch (C->getType()->getTypeID()) { 71987aa65f4SReid Spencer case Type::FloatTyID: 720bed9dc42SDale Johannesen Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat(); 7217a9c62baSReid Spencer break; 72287aa65f4SReid Spencer case Type::DoubleTyID: 723bed9dc42SDale Johannesen Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble(); 72487aa65f4SReid Spencer break; 725a1336cf5SDale Johannesen case Type::X86_FP80TyID: 726a1336cf5SDale Johannesen case Type::FP128TyID: 727a1336cf5SDale Johannesen case Type::PPC_FP128TyID: 72854306fe4SDale Johannesen Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt(); 729a1336cf5SDale Johannesen break; 73087aa65f4SReid Spencer case Type::IntegerTyID: 73187aa65f4SReid Spencer Result.IntVal = cast<ConstantInt>(C)->getValue(); 73287aa65f4SReid Spencer break; 733996fe010SChris Lattner case Type::PointerTyID: 7346a0fd73bSReid Spencer if (isa<ConstantPointerNull>(C)) 735996fe010SChris Lattner Result.PointerVal = 0; 7366a0fd73bSReid Spencer else if (const Function *F = dyn_cast<Function>(C)) 7376a0fd73bSReid Spencer Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F))); 7386a0fd73bSReid Spencer else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C)) 7396a0fd73bSReid Spencer Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV))); 740e6492f10SChris Lattner else 741fbcc663cSTorok Edwin llvm_unreachable("Unknown constant pointer type!"); 742996fe010SChris Lattner break; 743996fe010SChris Lattner default: 744ccb29cd2STorok Edwin std::string msg; 745ccb29cd2STorok Edwin raw_string_ostream Msg(msg); 746ccb29cd2STorok Edwin Msg << "ERROR: Constant unimplemented for type: " << *C->getType(); 747ccb29cd2STorok Edwin llvm_report_error(Msg.str()); 748996fe010SChris Lattner } 749996fe010SChris Lattner return Result; 750996fe010SChris Lattner } 751996fe010SChris Lattner 7521202d1b1SDuncan Sands /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst 7531202d1b1SDuncan Sands /// with the integer held in IntVal. 7541202d1b1SDuncan Sands static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, 7551202d1b1SDuncan Sands unsigned StoreBytes) { 7561202d1b1SDuncan Sands assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!"); 7571202d1b1SDuncan Sands uint8_t *Src = (uint8_t *)IntVal.getRawData(); 7585c65cb46SDuncan Sands 759aa121227SChris Lattner if (sys::isLittleEndianHost()) 7601202d1b1SDuncan Sands // Little-endian host - the source is ordered from LSB to MSB. Order the 7611202d1b1SDuncan Sands // destination from LSB to MSB: Do a straight copy. 7625c65cb46SDuncan Sands memcpy(Dst, Src, StoreBytes); 7635c65cb46SDuncan Sands else { 7645c65cb46SDuncan Sands // Big-endian host - the source is an array of 64 bit words ordered from 7651202d1b1SDuncan Sands // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination 7661202d1b1SDuncan Sands // from MSB to LSB: Reverse the word order, but not the bytes in a word. 7675c65cb46SDuncan Sands while (StoreBytes > sizeof(uint64_t)) { 7685c65cb46SDuncan Sands StoreBytes -= sizeof(uint64_t); 7695c65cb46SDuncan Sands // May not be aligned so use memcpy. 7705c65cb46SDuncan Sands memcpy(Dst + StoreBytes, Src, sizeof(uint64_t)); 7715c65cb46SDuncan Sands Src += sizeof(uint64_t); 7725c65cb46SDuncan Sands } 7735c65cb46SDuncan Sands 7745c65cb46SDuncan Sands memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes); 775815f8dd2SReid Spencer } 7767a9c62baSReid Spencer } 7771202d1b1SDuncan Sands 7781202d1b1SDuncan Sands /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr 7791202d1b1SDuncan Sands /// is the address of the memory at which to store Val, cast to GenericValue *. 7801202d1b1SDuncan Sands /// It is not a pointer to a GenericValue containing the address at which to 7811202d1b1SDuncan Sands /// store Val. 78209053e62SEvan Cheng void ExecutionEngine::StoreValueToMemory(const GenericValue &Val, 78309053e62SEvan Cheng GenericValue *Ptr, const Type *Ty) { 7841202d1b1SDuncan Sands const unsigned StoreBytes = getTargetData()->getTypeStoreSize(Ty); 7851202d1b1SDuncan Sands 7861202d1b1SDuncan Sands switch (Ty->getTypeID()) { 7871202d1b1SDuncan Sands case Type::IntegerTyID: 7881202d1b1SDuncan Sands StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes); 7891202d1b1SDuncan Sands break; 790996fe010SChris Lattner case Type::FloatTyID: 79187aa65f4SReid Spencer *((float*)Ptr) = Val.FloatVal; 79287aa65f4SReid Spencer break; 79387aa65f4SReid Spencer case Type::DoubleTyID: 79487aa65f4SReid Spencer *((double*)Ptr) = Val.DoubleVal; 795996fe010SChris Lattner break; 7964d7e4ee7SDale Johannesen case Type::X86_FP80TyID: 7974d7e4ee7SDale Johannesen memcpy(Ptr, Val.IntVal.getRawData(), 10); 798a1336cf5SDale Johannesen break; 7997a9c62baSReid Spencer case Type::PointerTyID: 8001202d1b1SDuncan Sands // Ensure 64 bit target pointers are fully initialized on 32 bit hosts. 8011202d1b1SDuncan Sands if (StoreBytes != sizeof(PointerTy)) 8021202d1b1SDuncan Sands memset(Ptr, 0, StoreBytes); 8031202d1b1SDuncan Sands 80487aa65f4SReid Spencer *((PointerTy*)Ptr) = Val.PointerVal; 805996fe010SChris Lattner break; 806996fe010SChris Lattner default: 807f3baad3eSBill Wendling cerr << "Cannot store value of type " << *Ty << "!\n"; 808996fe010SChris Lattner } 8091202d1b1SDuncan Sands 810aa121227SChris Lattner if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian()) 8111202d1b1SDuncan Sands // Host and target are different endian - reverse the stored bytes. 8121202d1b1SDuncan Sands std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr); 813996fe010SChris Lattner } 814996fe010SChris Lattner 8151202d1b1SDuncan Sands /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting 8161202d1b1SDuncan Sands /// from Src into IntVal, which is assumed to be wide enough and to hold zero. 8171202d1b1SDuncan Sands static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) { 8181202d1b1SDuncan Sands assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!"); 8191202d1b1SDuncan Sands uint8_t *Dst = (uint8_t *)IntVal.getRawData(); 8205c65cb46SDuncan Sands 821aa121227SChris Lattner if (sys::isLittleEndianHost()) 8225c65cb46SDuncan Sands // Little-endian host - the destination must be ordered from LSB to MSB. 8235c65cb46SDuncan Sands // The source is ordered from LSB to MSB: Do a straight copy. 8245c65cb46SDuncan Sands memcpy(Dst, Src, LoadBytes); 8255c65cb46SDuncan Sands else { 8265c65cb46SDuncan Sands // Big-endian - the destination is an array of 64 bit words ordered from 8275c65cb46SDuncan Sands // LSW to MSW. Each word must be ordered from MSB to LSB. The source is 8285c65cb46SDuncan Sands // ordered from MSB to LSB: Reverse the word order, but not the bytes in 8295c65cb46SDuncan Sands // a word. 8305c65cb46SDuncan Sands while (LoadBytes > sizeof(uint64_t)) { 8315c65cb46SDuncan Sands LoadBytes -= sizeof(uint64_t); 8325c65cb46SDuncan Sands // May not be aligned so use memcpy. 8335c65cb46SDuncan Sands memcpy(Dst, Src + LoadBytes, sizeof(uint64_t)); 8345c65cb46SDuncan Sands Dst += sizeof(uint64_t); 8355c65cb46SDuncan Sands } 8365c65cb46SDuncan Sands 8375c65cb46SDuncan Sands memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes); 8385c65cb46SDuncan Sands } 8397a9c62baSReid Spencer } 8401202d1b1SDuncan Sands 8411202d1b1SDuncan Sands /// FIXME: document 8421202d1b1SDuncan Sands /// 8431202d1b1SDuncan Sands void ExecutionEngine::LoadValueFromMemory(GenericValue &Result, 8441202d1b1SDuncan Sands GenericValue *Ptr, 8451202d1b1SDuncan Sands const Type *Ty) { 8461202d1b1SDuncan Sands const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty); 8471202d1b1SDuncan Sands 848aa121227SChris Lattner if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian()) { 8491202d1b1SDuncan Sands // Host and target are different endian - reverse copy the stored 8501202d1b1SDuncan Sands // bytes into a buffer, and load from that. 8511202d1b1SDuncan Sands uint8_t *Src = (uint8_t*)Ptr; 8521202d1b1SDuncan Sands uint8_t *Buf = (uint8_t*)alloca(LoadBytes); 8531202d1b1SDuncan Sands std::reverse_copy(Src, Src + LoadBytes, Buf); 8541202d1b1SDuncan Sands Ptr = (GenericValue*)Buf; 8551202d1b1SDuncan Sands } 8561202d1b1SDuncan Sands 8571202d1b1SDuncan Sands switch (Ty->getTypeID()) { 8581202d1b1SDuncan Sands case Type::IntegerTyID: 8591202d1b1SDuncan Sands // An APInt with all words initially zero. 8601202d1b1SDuncan Sands Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0); 8611202d1b1SDuncan Sands LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes); 8621202d1b1SDuncan Sands break; 8637f389e8cSChris Lattner case Type::FloatTyID: 86487aa65f4SReid Spencer Result.FloatVal = *((float*)Ptr); 86587aa65f4SReid Spencer break; 86687aa65f4SReid Spencer case Type::DoubleTyID: 86787aa65f4SReid Spencer Result.DoubleVal = *((double*)Ptr); 8687f389e8cSChris Lattner break; 8697a9c62baSReid Spencer case Type::PointerTyID: 87087aa65f4SReid Spencer Result.PointerVal = *((PointerTy*)Ptr); 8717f389e8cSChris Lattner break; 872a1336cf5SDale Johannesen case Type::X86_FP80TyID: { 873a1336cf5SDale Johannesen // This is endian dependent, but it will only work on x86 anyway. 87426d6539eSDuncan Sands // FIXME: Will not trap if loading a signaling NaN. 875ff306287SDuncan Sands uint64_t y[2]; 8764d7e4ee7SDale Johannesen memcpy(y, Ptr, 10); 877ff306287SDuncan Sands Result.IntVal = APInt(80, 2, y); 878a1336cf5SDale Johannesen break; 879a1336cf5SDale Johannesen } 8807f389e8cSChris Lattner default: 881ccb29cd2STorok Edwin std::string msg; 882ccb29cd2STorok Edwin raw_string_ostream Msg(msg); 883ccb29cd2STorok Edwin Msg << "Cannot load value of type " << *Ty << "!"; 884ccb29cd2STorok Edwin llvm_report_error(Msg.str()); 8857f389e8cSChris Lattner } 8867f389e8cSChris Lattner } 8877f389e8cSChris Lattner 888996fe010SChris Lattner // InitializeMemory - Recursive function to apply a Constant value into the 889996fe010SChris Lattner // specified memory location... 890996fe010SChris Lattner // 891996fe010SChris Lattner void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { 892972fd1a1SEvan Cheng DOUT << "JIT: Initializing " << Addr << " "; 893b086d382SDale Johannesen DEBUG(Init->dump()); 89461753bf8SChris Lattner if (isa<UndefValue>(Init)) { 89561753bf8SChris Lattner return; 896d84d35baSReid Spencer } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) { 89769d62138SRobert Bocchino unsigned ElementSize = 898af9eaa83SDuncan Sands getTargetData()->getTypeAllocSize(CP->getType()->getElementType()); 89969d62138SRobert Bocchino for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) 90069d62138SRobert Bocchino InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize); 90169d62138SRobert Bocchino return; 9021dd86b11SChris Lattner } else if (isa<ConstantAggregateZero>(Init)) { 903af9eaa83SDuncan Sands memset(Addr, 0, (size_t)getTargetData()->getTypeAllocSize(Init->getType())); 9041dd86b11SChris Lattner return; 90569ddfbfeSDan Gohman } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) { 90669ddfbfeSDan Gohman unsigned ElementSize = 907af9eaa83SDuncan Sands getTargetData()->getTypeAllocSize(CPA->getType()->getElementType()); 90869ddfbfeSDan Gohman for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) 90969ddfbfeSDan Gohman InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); 91069ddfbfeSDan Gohman return; 91169ddfbfeSDan Gohman } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) { 91269ddfbfeSDan Gohman const StructLayout *SL = 91369ddfbfeSDan Gohman getTargetData()->getStructLayout(cast<StructType>(CPS->getType())); 91469ddfbfeSDan Gohman for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) 91569ddfbfeSDan Gohman InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i)); 91669ddfbfeSDan Gohman return; 91761753bf8SChris Lattner } else if (Init->getType()->isFirstClassType()) { 918996fe010SChris Lattner GenericValue Val = getConstantValue(Init); 919996fe010SChris Lattner StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); 920996fe010SChris Lattner return; 921996fe010SChris Lattner } 922996fe010SChris Lattner 923f3baad3eSBill Wendling cerr << "Bad Type: " << *Init->getType() << "\n"; 924fbcc663cSTorok Edwin llvm_unreachable("Unknown constant type to initialize memory with!"); 925996fe010SChris Lattner } 926996fe010SChris Lattner 927996fe010SChris Lattner /// EmitGlobals - Emit all of the global variables to memory, storing their 928996fe010SChris Lattner /// addresses into GlobalAddress. This must make sure to copy the contents of 929996fe010SChris Lattner /// their initializers into the memory. 930996fe010SChris Lattner /// 931996fe010SChris Lattner void ExecutionEngine::emitGlobals() { 932996fe010SChris Lattner 933996fe010SChris Lattner // Loop over all of the global variables in the program, allocating the memory 9340621caefSChris Lattner // to hold them. If there is more than one module, do a prepass over globals 9350621caefSChris Lattner // to figure out how the different modules should link together. 9360621caefSChris Lattner // 9370621caefSChris Lattner std::map<std::pair<std::string, const Type*>, 9380621caefSChris Lattner const GlobalValue*> LinkedGlobalsMap; 9390621caefSChris Lattner 9400621caefSChris Lattner if (Modules.size() != 1) { 9410621caefSChris Lattner for (unsigned m = 0, e = Modules.size(); m != e; ++m) { 9420621caefSChris Lattner Module &M = *Modules[m]->getModule(); 9430621caefSChris Lattner for (Module::const_global_iterator I = M.global_begin(), 9440621caefSChris Lattner E = M.global_end(); I != E; ++I) { 9450621caefSChris Lattner const GlobalValue *GV = I; 9466de96a1bSRafael Espindola if (GV->hasLocalLinkage() || GV->isDeclaration() || 9470621caefSChris Lattner GV->hasAppendingLinkage() || !GV->hasName()) 9480621caefSChris Lattner continue;// Ignore external globals and globals with internal linkage. 9490621caefSChris Lattner 9500621caefSChris Lattner const GlobalValue *&GVEntry = 9510621caefSChris Lattner LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; 9520621caefSChris Lattner 9530621caefSChris Lattner // If this is the first time we've seen this global, it is the canonical 9540621caefSChris Lattner // version. 9550621caefSChris Lattner if (!GVEntry) { 9560621caefSChris Lattner GVEntry = GV; 9570621caefSChris Lattner continue; 9580621caefSChris Lattner } 9590621caefSChris Lattner 9600621caefSChris Lattner // If the existing global is strong, never replace it. 961d61d39ecSAnton Korobeynikov if (GVEntry->hasExternalLinkage() || 962d61d39ecSAnton Korobeynikov GVEntry->hasDLLImportLinkage() || 963d61d39ecSAnton Korobeynikov GVEntry->hasDLLExportLinkage()) 9640621caefSChris Lattner continue; 9650621caefSChris Lattner 9660621caefSChris Lattner // Otherwise, we know it's linkonce/weak, replace it if this is a strong 967ce4396bcSDale Johannesen // symbol. FIXME is this right for common? 96812c94949SAnton Korobeynikov if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage()) 9690621caefSChris Lattner GVEntry = GV; 9700621caefSChris Lattner } 9710621caefSChris Lattner } 9720621caefSChris Lattner } 9730621caefSChris Lattner 9740621caefSChris Lattner std::vector<const GlobalValue*> NonCanonicalGlobals; 9750621caefSChris Lattner for (unsigned m = 0, e = Modules.size(); m != e; ++m) { 9760621caefSChris Lattner Module &M = *Modules[m]->getModule(); 9778ffb6611SChris Lattner for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 9780621caefSChris Lattner I != E; ++I) { 9790621caefSChris Lattner // In the multi-module case, see what this global maps to. 9800621caefSChris Lattner if (!LinkedGlobalsMap.empty()) { 9810621caefSChris Lattner if (const GlobalValue *GVEntry = 9820621caefSChris Lattner LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) { 9830621caefSChris Lattner // If something else is the canonical global, ignore this one. 9840621caefSChris Lattner if (GVEntry != &*I) { 9850621caefSChris Lattner NonCanonicalGlobals.push_back(I); 9860621caefSChris Lattner continue; 9870621caefSChris Lattner } 9880621caefSChris Lattner } 9890621caefSChris Lattner } 9900621caefSChris Lattner 9915301e7c6SReid Spencer if (!I->isDeclaration()) { 9925457ce9aSNicolas Geoffray addGlobalMapping(I, getMemoryForGV(I)); 993996fe010SChris Lattner } else { 994e8bbcfc2SBrian Gaeke // External variable reference. Try to use the dynamic loader to 995e8bbcfc2SBrian Gaeke // get a pointer to it. 9960621caefSChris Lattner if (void *SymAddr = 9975899e340SDaniel Dunbar sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName())) 998748e8579SChris Lattner addGlobalMapping(I, SymAddr); 9999de0d14dSChris Lattner else { 10006c2d233eSTorok Edwin llvm_report_error("Could not resolve external global address: " 10016c2d233eSTorok Edwin +I->getName()); 10029de0d14dSChris Lattner } 1003996fe010SChris Lattner } 10040621caefSChris Lattner } 10050621caefSChris Lattner 10060621caefSChris Lattner // If there are multiple modules, map the non-canonical globals to their 10070621caefSChris Lattner // canonical location. 10080621caefSChris Lattner if (!NonCanonicalGlobals.empty()) { 10090621caefSChris Lattner for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) { 10100621caefSChris Lattner const GlobalValue *GV = NonCanonicalGlobals[i]; 10110621caefSChris Lattner const GlobalValue *CGV = 10120621caefSChris Lattner LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; 10130621caefSChris Lattner void *Ptr = getPointerToGlobalIfAvailable(CGV); 10140621caefSChris Lattner assert(Ptr && "Canonical global wasn't codegen'd!"); 1015a67f06b9SNuno Lopes addGlobalMapping(GV, Ptr); 10160621caefSChris Lattner } 10170621caefSChris Lattner } 1018996fe010SChris Lattner 10197a9c62baSReid Spencer // Now that all of the globals are set up in memory, loop through them all 10207a9c62baSReid Spencer // and initialize their contents. 10218ffb6611SChris Lattner for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 10220621caefSChris Lattner I != E; ++I) { 10235301e7c6SReid Spencer if (!I->isDeclaration()) { 10240621caefSChris Lattner if (!LinkedGlobalsMap.empty()) { 10250621caefSChris Lattner if (const GlobalValue *GVEntry = 10260621caefSChris Lattner LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) 10270621caefSChris Lattner if (GVEntry != &*I) // Not the canonical variable. 10280621caefSChris Lattner continue; 10290621caefSChris Lattner } 10306bbe3eceSChris Lattner EmitGlobalVariable(I); 10316bbe3eceSChris Lattner } 10320621caefSChris Lattner } 10330621caefSChris Lattner } 10340621caefSChris Lattner } 10356bbe3eceSChris Lattner 10366bbe3eceSChris Lattner // EmitGlobalVariable - This method emits the specified global variable to the 10376bbe3eceSChris Lattner // address specified in GlobalAddresses, or allocates new memory if it's not 10386bbe3eceSChris Lattner // already in the map. 1039fbcc0aa1SChris Lattner void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { 1040748e8579SChris Lattner void *GA = getPointerToGlobalIfAvailable(GV); 1041dc631735SChris Lattner 10426bbe3eceSChris Lattner if (GA == 0) { 10436bbe3eceSChris Lattner // If it's not already specified, allocate memory for the global. 10445457ce9aSNicolas Geoffray GA = getMemoryForGV(GV); 1045748e8579SChris Lattner addGlobalMapping(GV, GA); 10466bbe3eceSChris Lattner } 1047fbcc0aa1SChris Lattner 10485457ce9aSNicolas Geoffray // Don't initialize if it's thread local, let the client do it. 10495457ce9aSNicolas Geoffray if (!GV->isThreadLocal()) 10506bbe3eceSChris Lattner InitializeMemory(GV->getInitializer(), GA); 10515457ce9aSNicolas Geoffray 10525457ce9aSNicolas Geoffray const Type *ElTy = GV->getType()->getElementType(); 1053af9eaa83SDuncan Sands size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy); 1054df1f1524SChris Lattner NumInitBytes += (unsigned)GVSize; 10556bbe3eceSChris Lattner ++NumGlobals; 1056996fe010SChris Lattner } 1057