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" 27*ccb29cd2STorok 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 382d52c1b8SChris Lattner ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0; 392d52c1b8SChris Lattner ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0; 4021ad494fSNicolas Geoffray ExecutionEngine::EERegisterFn ExecutionEngine::ExceptionTableRegister = 0; 4121ad494fSNicolas Geoffray 422d52c1b8SChris Lattner 43fd6f3257SChris Lattner ExecutionEngine::ExecutionEngine(ModuleProvider *P) : LazyFunctionCreator(0) { 4487aee74cSChris Lattner LazyCompilationDisabled = false; 45cdc0060eSEvan Cheng GVCompilationDisabled = false; 4684a9055eSEvan Cheng SymbolSearchingDisabled = false; 4718d85e74SNate Begeman DlsymStubsEnabled = false; 480621caefSChris Lattner Modules.push_back(P); 49260b0c88SMisha Brukman assert(P && "ModuleProvider is null?"); 50260b0c88SMisha Brukman } 51260b0c88SMisha Brukman 5292f8b30dSBrian Gaeke ExecutionEngine::~ExecutionEngine() { 53603682adSReid Spencer clearAllGlobalMappings(); 540621caefSChris Lattner for (unsigned i = 0, e = Modules.size(); i != e; ++i) 550621caefSChris Lattner delete Modules[i]; 5692f8b30dSBrian Gaeke } 5792f8b30dSBrian Gaeke 585457ce9aSNicolas Geoffray char* ExecutionEngine::getMemoryForGV(const GlobalVariable* GV) { 595457ce9aSNicolas Geoffray const Type *ElTy = GV->getType()->getElementType(); 60af9eaa83SDuncan Sands size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy); 615457ce9aSNicolas Geoffray return new char[GVSize]; 625457ce9aSNicolas Geoffray } 635457ce9aSNicolas Geoffray 64324fe890SDevang Patel /// removeModuleProvider - Remove a ModuleProvider from the list of modules. 65617001d8SNate Begeman /// Relases the Module from the ModuleProvider, materializing it in the 66617001d8SNate Begeman /// process, and returns the materialized Module. 67324fe890SDevang Patel Module* ExecutionEngine::removeModuleProvider(ModuleProvider *P, 68324fe890SDevang Patel std::string *ErrInfo) { 69324fe890SDevang Patel for(SmallVector<ModuleProvider *, 1>::iterator I = Modules.begin(), 70324fe890SDevang Patel E = Modules.end(); I != E; ++I) { 71324fe890SDevang Patel ModuleProvider *MP = *I; 72324fe890SDevang Patel if (MP == P) { 73324fe890SDevang Patel Modules.erase(I); 748f83fc4dSNate Begeman clearGlobalMappingsFromModule(MP->getModule()); 75324fe890SDevang Patel return MP->releaseModule(ErrInfo); 76324fe890SDevang Patel } 77324fe890SDevang Patel } 78324fe890SDevang Patel return NULL; 79324fe890SDevang Patel } 80324fe890SDevang Patel 81617001d8SNate Begeman /// deleteModuleProvider - Remove a ModuleProvider from the list of modules, 82617001d8SNate Begeman /// and deletes the ModuleProvider and owned Module. Avoids materializing 83617001d8SNate Begeman /// the underlying module. 84617001d8SNate Begeman void ExecutionEngine::deleteModuleProvider(ModuleProvider *P, 85617001d8SNate Begeman std::string *ErrInfo) { 86617001d8SNate Begeman for(SmallVector<ModuleProvider *, 1>::iterator I = Modules.begin(), 87617001d8SNate Begeman E = Modules.end(); I != E; ++I) { 88617001d8SNate Begeman ModuleProvider *MP = *I; 89617001d8SNate Begeman if (MP == P) { 90617001d8SNate Begeman Modules.erase(I); 91617001d8SNate Begeman clearGlobalMappingsFromModule(MP->getModule()); 92617001d8SNate Begeman delete MP; 93617001d8SNate Begeman return; 94617001d8SNate Begeman } 95617001d8SNate Begeman } 96617001d8SNate Begeman } 97617001d8SNate Begeman 980621caefSChris Lattner /// FindFunctionNamed - Search all of the active modules to find the one that 990621caefSChris Lattner /// defines FnName. This is very slow operation and shouldn't be used for 1000621caefSChris Lattner /// general code. 1010621caefSChris Lattner Function *ExecutionEngine::FindFunctionNamed(const char *FnName) { 1020621caefSChris Lattner for (unsigned i = 0, e = Modules.size(); i != e; ++i) { 1031241d6d5SReid Spencer if (Function *F = Modules[i]->getModule()->getFunction(FnName)) 1040621caefSChris Lattner return F; 1050621caefSChris Lattner } 1060621caefSChris Lattner return 0; 1070621caefSChris Lattner } 1080621caefSChris Lattner 1090621caefSChris Lattner 1106d8dd189SChris Lattner /// addGlobalMapping - Tell the execution engine that the specified global is 1116d8dd189SChris Lattner /// at the specified location. This is used internally as functions are JIT'd 1126d8dd189SChris Lattner /// and as global variables are laid out in memory. It can and should also be 1136d8dd189SChris Lattner /// used by clients of the EE that want to have an LLVM global overlay 1146d8dd189SChris Lattner /// existing data in memory. 1156d8dd189SChris Lattner void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) { 1166d8dd189SChris Lattner MutexGuard locked(lock); 1176d8dd189SChris Lattner 118077f686dSEvan Cheng DOUT << "JIT: Map \'" << GV->getNameStart() << "\' to [" << Addr << "]\n"; 1196d8dd189SChris Lattner void *&CurVal = state.getGlobalAddressMap(locked)[GV]; 1206d8dd189SChris Lattner assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!"); 1216d8dd189SChris Lattner CurVal = Addr; 1226d8dd189SChris Lattner 1236d8dd189SChris Lattner // If we are using the reverse mapping, add it too 1246d8dd189SChris Lattner if (!state.getGlobalAddressReverseMap(locked).empty()) { 1256d8dd189SChris Lattner const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr]; 1266d8dd189SChris Lattner assert((V == 0 || GV == 0) && "GlobalMapping already established!"); 1276d8dd189SChris Lattner V = GV; 1286d8dd189SChris Lattner } 1296d8dd189SChris Lattner } 1306d8dd189SChris Lattner 1316d8dd189SChris Lattner /// clearAllGlobalMappings - Clear all global mappings and start over again 1326d8dd189SChris Lattner /// use in dynamic compilation scenarios when you want to move globals 1336d8dd189SChris Lattner void ExecutionEngine::clearAllGlobalMappings() { 1346d8dd189SChris Lattner MutexGuard locked(lock); 1356d8dd189SChris Lattner 1366d8dd189SChris Lattner state.getGlobalAddressMap(locked).clear(); 1376d8dd189SChris Lattner state.getGlobalAddressReverseMap(locked).clear(); 1386d8dd189SChris Lattner } 1396d8dd189SChris Lattner 1408f83fc4dSNate Begeman /// clearGlobalMappingsFromModule - Clear all global mappings that came from a 1418f83fc4dSNate Begeman /// particular module, because it has been removed from the JIT. 1428f83fc4dSNate Begeman void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) { 1438f83fc4dSNate Begeman MutexGuard locked(lock); 1448f83fc4dSNate Begeman 1458f83fc4dSNate Begeman for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) { 1468f83fc4dSNate Begeman state.getGlobalAddressMap(locked).erase(FI); 1478f83fc4dSNate Begeman state.getGlobalAddressReverseMap(locked).erase(FI); 1488f83fc4dSNate Begeman } 1498f83fc4dSNate Begeman for (Module::global_iterator GI = M->global_begin(), GE = M->global_end(); 1508f83fc4dSNate Begeman GI != GE; ++GI) { 1518f83fc4dSNate Begeman state.getGlobalAddressMap(locked).erase(GI); 1528f83fc4dSNate Begeman state.getGlobalAddressReverseMap(locked).erase(GI); 1538f83fc4dSNate Begeman } 1548f83fc4dSNate Begeman } 1558f83fc4dSNate Begeman 1566d8dd189SChris Lattner /// updateGlobalMapping - Replace an existing mapping for GV with a new 1576d8dd189SChris Lattner /// address. This updates both maps as required. If "Addr" is null, the 1586d8dd189SChris Lattner /// entry for the global is removed from the mappings. 159ee181730SChris Lattner void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) { 1606d8dd189SChris Lattner MutexGuard locked(lock); 1616d8dd189SChris Lattner 162ee181730SChris Lattner std::map<const GlobalValue*, void *> &Map = state.getGlobalAddressMap(locked); 163ee181730SChris Lattner 1646d8dd189SChris Lattner // Deleting from the mapping? 1656d8dd189SChris Lattner if (Addr == 0) { 166ee181730SChris Lattner std::map<const GlobalValue*, void *>::iterator I = Map.find(GV); 167ee181730SChris Lattner void *OldVal; 168ee181730SChris Lattner if (I == Map.end()) 169ee181730SChris Lattner OldVal = 0; 170ee181730SChris Lattner else { 171ee181730SChris Lattner OldVal = I->second; 172ee181730SChris Lattner Map.erase(I); 1736d8dd189SChris Lattner } 1746d8dd189SChris Lattner 175ee181730SChris Lattner if (!state.getGlobalAddressReverseMap(locked).empty()) 176ee181730SChris Lattner state.getGlobalAddressReverseMap(locked).erase(Addr); 177ee181730SChris Lattner return OldVal; 178ee181730SChris Lattner } 179ee181730SChris Lattner 180ee181730SChris Lattner void *&CurVal = Map[GV]; 181ee181730SChris Lattner void *OldVal = CurVal; 182ee181730SChris Lattner 1836d8dd189SChris Lattner if (CurVal && !state.getGlobalAddressReverseMap(locked).empty()) 1846d8dd189SChris Lattner state.getGlobalAddressReverseMap(locked).erase(CurVal); 1856d8dd189SChris Lattner CurVal = Addr; 1866d8dd189SChris Lattner 1876d8dd189SChris Lattner // If we are using the reverse mapping, add it too 1886d8dd189SChris Lattner if (!state.getGlobalAddressReverseMap(locked).empty()) { 1896d8dd189SChris Lattner const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr]; 1906d8dd189SChris Lattner assert((V == 0 || GV == 0) && "GlobalMapping already established!"); 1916d8dd189SChris Lattner V = GV; 1926d8dd189SChris Lattner } 193ee181730SChris Lattner return OldVal; 1946d8dd189SChris Lattner } 1956d8dd189SChris Lattner 1966d8dd189SChris Lattner /// getPointerToGlobalIfAvailable - This returns the address of the specified 1976d8dd189SChris Lattner /// global value if it is has already been codegen'd, otherwise it returns null. 1986d8dd189SChris Lattner /// 1996d8dd189SChris Lattner void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) { 2006d8dd189SChris Lattner MutexGuard locked(lock); 2016d8dd189SChris Lattner 2026d8dd189SChris Lattner std::map<const GlobalValue*, void*>::iterator I = 2036d8dd189SChris Lattner state.getGlobalAddressMap(locked).find(GV); 2046d8dd189SChris Lattner return I != state.getGlobalAddressMap(locked).end() ? I->second : 0; 2056d8dd189SChris Lattner } 2066d8dd189SChris Lattner 207748e8579SChris Lattner /// getGlobalValueAtAddress - Return the LLVM global value object that starts 208748e8579SChris Lattner /// at the specified address. 209748e8579SChris Lattner /// 210748e8579SChris Lattner const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { 21179876f52SReid Spencer MutexGuard locked(lock); 21279876f52SReid Spencer 213748e8579SChris Lattner // If we haven't computed the reverse mapping yet, do so first. 21479876f52SReid Spencer if (state.getGlobalAddressReverseMap(locked).empty()) { 2156d8dd189SChris Lattner for (std::map<const GlobalValue*, void *>::iterator 2166d8dd189SChris Lattner I = state.getGlobalAddressMap(locked).begin(), 2176d8dd189SChris Lattner E = state.getGlobalAddressMap(locked).end(); I != E; ++I) 2186d8dd189SChris Lattner state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, 2196d8dd189SChris Lattner I->first)); 220748e8579SChris Lattner } 221748e8579SChris Lattner 222748e8579SChris Lattner std::map<void *, const GlobalValue*>::iterator I = 22379876f52SReid Spencer state.getGlobalAddressReverseMap(locked).find(Addr); 22479876f52SReid Spencer return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0; 225748e8579SChris Lattner } 2265a0d4829SChris Lattner 2275a0d4829SChris Lattner // CreateArgv - Turn a vector of strings into a nice argv style array of 2285a0d4829SChris Lattner // pointers to null terminated strings. 2295a0d4829SChris Lattner // 2305a0d4829SChris Lattner static void *CreateArgv(ExecutionEngine *EE, 2315a0d4829SChris Lattner const std::vector<std::string> &InputArgv) { 23220a631fdSOwen Anderson unsigned PtrSize = EE->getTargetData()->getPointerSize(); 2335a0d4829SChris Lattner char *Result = new char[(InputArgv.size()+1)*PtrSize]; 2345a0d4829SChris Lattner 235972fd1a1SEvan Cheng DOUT << "JIT: ARGV = " << (void*)Result << "\n"; 236edf07887SChristopher Lamb const Type *SBytePtr = PointerType::getUnqual(Type::Int8Ty); 2375a0d4829SChris Lattner 2385a0d4829SChris Lattner for (unsigned i = 0; i != InputArgv.size(); ++i) { 2395a0d4829SChris Lattner unsigned Size = InputArgv[i].size()+1; 2405a0d4829SChris Lattner char *Dest = new char[Size]; 241972fd1a1SEvan Cheng DOUT << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n"; 2425a0d4829SChris Lattner 2435a0d4829SChris Lattner std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); 2445a0d4829SChris Lattner Dest[Size-1] = 0; 2455a0d4829SChris Lattner 2465a0d4829SChris Lattner // Endian safe: Result[i] = (PointerTy)Dest; 2475a0d4829SChris Lattner EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize), 2485a0d4829SChris Lattner SBytePtr); 2495a0d4829SChris Lattner } 2505a0d4829SChris Lattner 2515a0d4829SChris Lattner // Null terminate it 2525a0d4829SChris Lattner EE->StoreValueToMemory(PTOGV(0), 2535a0d4829SChris Lattner (GenericValue*)(Result+InputArgv.size()*PtrSize), 2545a0d4829SChris Lattner SBytePtr); 2555a0d4829SChris Lattner return Result; 2565a0d4829SChris Lattner } 2575a0d4829SChris Lattner 258faae50b6SChris Lattner 259faae50b6SChris Lattner /// runStaticConstructorsDestructors - This method is used to execute all of 2601a9a0b7bSEvan Cheng /// the static constructors or destructors for a module, depending on the 261faae50b6SChris Lattner /// value of isDtors. 2621a9a0b7bSEvan Cheng void ExecutionEngine::runStaticConstructorsDestructors(Module *module, bool isDtors) { 263faae50b6SChris Lattner const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors"; 2640621caefSChris Lattner 2650621caefSChris Lattner // Execute global ctors/dtors for each module in the program. 2661a9a0b7bSEvan Cheng 2671a9a0b7bSEvan Cheng GlobalVariable *GV = module->getNamedGlobal(Name); 268fe36eaebSChris Lattner 269fe36eaebSChris Lattner // If this global has internal linkage, or if it has a use, then it must be 270fe36eaebSChris Lattner // an old-style (llvmgcc3) static ctor with __main linked in and in use. If 2710621caefSChris Lattner // this is the case, don't execute any of the global ctors, __main will do 2720621caefSChris Lattner // it. 2736de96a1bSRafael Espindola if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return; 274faae50b6SChris Lattner 2750621caefSChris Lattner // Should be an array of '{ int, void ()* }' structs. The first value is 2760621caefSChris Lattner // the init priority, which we ignore. 277faae50b6SChris Lattner ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 2781a9a0b7bSEvan Cheng if (!InitList) return; 279faae50b6SChris Lattner for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) 2800621caefSChris Lattner if (ConstantStruct *CS = 2810621caefSChris Lattner dyn_cast<ConstantStruct>(InitList->getOperand(i))) { 2821a9a0b7bSEvan Cheng if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. 283faae50b6SChris Lattner 284faae50b6SChris Lattner Constant *FP = CS->getOperand(1); 285faae50b6SChris Lattner if (FP->isNullValue()) 2860621caefSChris Lattner break; // Found a null terminator, exit. 287faae50b6SChris Lattner 288faae50b6SChris Lattner if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) 2896c38f0bbSReid Spencer if (CE->isCast()) 290faae50b6SChris Lattner FP = CE->getOperand(0); 291faae50b6SChris Lattner if (Function *F = dyn_cast<Function>(FP)) { 292faae50b6SChris Lattner // Execute the ctor/dtor function! 293faae50b6SChris Lattner runFunction(F, std::vector<GenericValue>()); 294faae50b6SChris Lattner } 295faae50b6SChris Lattner } 296faae50b6SChris Lattner } 2971a9a0b7bSEvan Cheng 2981a9a0b7bSEvan Cheng /// runStaticConstructorsDestructors - This method is used to execute all of 2991a9a0b7bSEvan Cheng /// the static constructors or destructors for a program, depending on the 3001a9a0b7bSEvan Cheng /// value of isDtors. 3011a9a0b7bSEvan Cheng void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) { 3021a9a0b7bSEvan Cheng // Execute global ctors/dtors for each module in the program. 3031a9a0b7bSEvan Cheng for (unsigned m = 0, e = Modules.size(); m != e; ++m) 3041a9a0b7bSEvan Cheng runStaticConstructorsDestructors(Modules[m]->getModule(), isDtors); 3050621caefSChris Lattner } 306faae50b6SChris Lattner 307cf3e3017SDan Gohman #ifndef NDEBUG 3081202d1b1SDuncan Sands /// isTargetNullPtr - Return whether the target pointer stored at Loc is null. 3091202d1b1SDuncan Sands static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) { 3101202d1b1SDuncan Sands unsigned PtrSize = EE->getTargetData()->getPointerSize(); 3111202d1b1SDuncan Sands for (unsigned i = 0; i < PtrSize; ++i) 3121202d1b1SDuncan Sands if (*(i + (uint8_t*)Loc)) 3131202d1b1SDuncan Sands return false; 3141202d1b1SDuncan Sands return true; 3151202d1b1SDuncan Sands } 316cf3e3017SDan Gohman #endif 3171202d1b1SDuncan Sands 3185a0d4829SChris Lattner /// runFunctionAsMain - This is a helper function which wraps runFunction to 3195a0d4829SChris Lattner /// handle the common task of starting up main with the specified argc, argv, 3205a0d4829SChris Lattner /// and envp parameters. 3215a0d4829SChris Lattner int ExecutionEngine::runFunctionAsMain(Function *Fn, 3225a0d4829SChris Lattner const std::vector<std::string> &argv, 3235a0d4829SChris Lattner const char * const * envp) { 3245a0d4829SChris Lattner std::vector<GenericValue> GVArgs; 3255a0d4829SChris Lattner GenericValue GVArgc; 32687aa65f4SReid Spencer GVArgc.IntVal = APInt(32, argv.size()); 3278c32c111SAnton Korobeynikov 3288c32c111SAnton Korobeynikov // Check main() type 329b1cad0b3SChris Lattner unsigned NumArgs = Fn->getFunctionType()->getNumParams(); 3308c32c111SAnton Korobeynikov const FunctionType *FTy = Fn->getFunctionType(); 331edf07887SChristopher Lamb const Type* PPInt8Ty = 332edf07887SChristopher Lamb PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty)); 3338c32c111SAnton Korobeynikov switch (NumArgs) { 3348c32c111SAnton Korobeynikov case 3: 3358c32c111SAnton Korobeynikov if (FTy->getParamType(2) != PPInt8Ty) { 336*ccb29cd2STorok Edwin llvm_report_error("Invalid type for third argument of main() supplied"); 3378c32c111SAnton Korobeynikov } 338b781886dSAnton Korobeynikov // FALLS THROUGH 3398c32c111SAnton Korobeynikov case 2: 3408c32c111SAnton Korobeynikov if (FTy->getParamType(1) != PPInt8Ty) { 341*ccb29cd2STorok Edwin llvm_report_error("Invalid type for second argument of main() supplied"); 3428c32c111SAnton Korobeynikov } 343b781886dSAnton Korobeynikov // FALLS THROUGH 3448c32c111SAnton Korobeynikov case 1: 3458c32c111SAnton Korobeynikov if (FTy->getParamType(0) != Type::Int32Ty) { 346*ccb29cd2STorok Edwin llvm_report_error("Invalid type for first argument of main() supplied"); 3478c32c111SAnton Korobeynikov } 348b781886dSAnton Korobeynikov // FALLS THROUGH 3498c32c111SAnton Korobeynikov case 0: 350370ec10dSChris Lattner if (!isa<IntegerType>(FTy->getReturnType()) && 3518c32c111SAnton Korobeynikov FTy->getReturnType() != Type::VoidTy) { 352*ccb29cd2STorok Edwin llvm_report_error("Invalid return type of main() supplied"); 3538c32c111SAnton Korobeynikov } 3548c32c111SAnton Korobeynikov break; 3558c32c111SAnton Korobeynikov default: 356*ccb29cd2STorok Edwin llvm_report_error("Invalid number of arguments of main() supplied"); 3578c32c111SAnton Korobeynikov } 3588c32c111SAnton Korobeynikov 359b1cad0b3SChris Lattner if (NumArgs) { 3605a0d4829SChris Lattner GVArgs.push_back(GVArgc); // Arg #0 = argc. 361b1cad0b3SChris Lattner if (NumArgs > 1) { 3625a0d4829SChris Lattner GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv. 3631202d1b1SDuncan Sands assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) && 364b1cad0b3SChris Lattner "argv[0] was null after CreateArgv"); 365b1cad0b3SChris Lattner if (NumArgs > 2) { 3665a0d4829SChris Lattner std::vector<std::string> EnvVars; 3675a0d4829SChris Lattner for (unsigned i = 0; envp[i]; ++i) 3685a0d4829SChris Lattner EnvVars.push_back(envp[i]); 3695a0d4829SChris Lattner GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp. 370b1cad0b3SChris Lattner } 371b1cad0b3SChris Lattner } 372b1cad0b3SChris Lattner } 37387aa65f4SReid Spencer return runFunction(Fn, GVArgs).IntVal.getZExtValue(); 3745a0d4829SChris Lattner } 3755a0d4829SChris Lattner 376260b0c88SMisha Brukman /// If possible, create a JIT, unless the caller specifically requests an 377260b0c88SMisha Brukman /// Interpreter or there's an error. If even an Interpreter cannot be created, 378260b0c88SMisha Brukman /// NULL is returned. 379857c21b4SMisha Brukman /// 3802f1e2002SMisha Brukman ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP, 381603682adSReid Spencer bool ForceInterpreter, 3827ff05bf5SEvan Cheng std::string *ErrorStr, 38370415d97SJeffrey Yasskin CodeGenOpt::Level OptLevel, 38470415d97SJeffrey Yasskin bool GVsWithCode) { 3854bd3bd5bSBrian Gaeke ExecutionEngine *EE = 0; 3864bd3bd5bSBrian Gaeke 387a53414fdSNick Lewycky // Make sure we can resolve symbols in the program as well. The zero arg 388a53414fdSNick Lewycky // to the function tells DynamicLibrary to load the program, not a library. 389a53414fdSNick Lewycky if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr)) 390a53414fdSNick Lewycky return 0; 391a53414fdSNick Lewycky 392c8c6c03dSChris Lattner // Unless the interpreter was explicitly selected, try making a JIT. 3932d52c1b8SChris Lattner if (!ForceInterpreter && JITCtor) 39470415d97SJeffrey Yasskin EE = JITCtor(MP, ErrorStr, OptLevel, GVsWithCode); 3954bd3bd5bSBrian Gaeke 3964bd3bd5bSBrian Gaeke // If we can't make a JIT, make an interpreter instead. 3972d52c1b8SChris Lattner if (EE == 0 && InterpCtor) 39870415d97SJeffrey Yasskin EE = InterpCtor(MP, ErrorStr, OptLevel, GVsWithCode); 399c8c6c03dSChris Lattner 4004bd3bd5bSBrian Gaeke return EE; 4014bd3bd5bSBrian Gaeke } 4024bd3bd5bSBrian Gaeke 403b5163bb9SChris Lattner ExecutionEngine *ExecutionEngine::create(Module *M) { 404b5163bb9SChris Lattner return create(new ExistingModuleProvider(M)); 405b5163bb9SChris Lattner } 406b5163bb9SChris Lattner 407857c21b4SMisha Brukman /// getPointerToGlobal - This returns the address of the specified global 408857c21b4SMisha Brukman /// value. This may involve code generation if it's a function. 409857c21b4SMisha Brukman /// 410996fe010SChris Lattner void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { 4111678e859SBrian Gaeke if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) 412996fe010SChris Lattner return getPointerToFunction(F); 413996fe010SChris Lattner 41479876f52SReid Spencer MutexGuard locked(lock); 41569e84901SJeff Cohen void *p = state.getGlobalAddressMap(locked)[GV]; 41669e84901SJeff Cohen if (p) 41769e84901SJeff Cohen return p; 41869e84901SJeff Cohen 41969e84901SJeff Cohen // Global variable might have been added since interpreter started. 42069e84901SJeff Cohen if (GlobalVariable *GVar = 42169e84901SJeff Cohen const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV))) 42269e84901SJeff Cohen EmitGlobalVariable(GVar); 42369e84901SJeff Cohen else 4244da5e17cSChris Lattner assert(0 && "Global hasn't had an address allocated yet!"); 42579876f52SReid Spencer return state.getGlobalAddressMap(locked)[GV]; 426996fe010SChris Lattner } 427996fe010SChris Lattner 4286c38f0bbSReid Spencer /// This function converts a Constant* into a GenericValue. The interesting 4296c38f0bbSReid Spencer /// part is if C is a ConstantExpr. 4302dc9f132SReid Spencer /// @brief Get a GenericValue for a Constant* 431996fe010SChris Lattner GenericValue ExecutionEngine::getConstantValue(const Constant *C) { 4326c38f0bbSReid Spencer // If its undefined, return the garbage. 4334fd528f2SReid Spencer if (isa<UndefValue>(C)) 4344fd528f2SReid Spencer return GenericValue(); 4359de0d14dSChris Lattner 4366c38f0bbSReid Spencer // If the value is a ConstantExpr 4376c38f0bbSReid Spencer if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 4384fd528f2SReid Spencer Constant *Op0 = CE->getOperand(0); 4399de0d14dSChris Lattner switch (CE->getOpcode()) { 4409de0d14dSChris Lattner case Instruction::GetElementPtr: { 4416c38f0bbSReid Spencer // Compute the index 4424fd528f2SReid Spencer GenericValue Result = getConstantValue(Op0); 443c44bd78aSChris Lattner SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end()); 4449de0d14dSChris Lattner uint64_t Offset = 4454fd528f2SReid Spencer TD->getIndexedOffset(Op0->getType(), &Indices[0], Indices.size()); 4469de0d14dSChris Lattner 44787aa65f4SReid Spencer char* tmp = (char*) Result.PointerVal; 44887aa65f4SReid Spencer Result = PTOGV(tmp + Offset); 4499de0d14dSChris Lattner return Result; 4509de0d14dSChris Lattner } 4514fd528f2SReid Spencer case Instruction::Trunc: { 4524fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 4534fd528f2SReid Spencer uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 4544fd528f2SReid Spencer GV.IntVal = GV.IntVal.trunc(BitWidth); 4554fd528f2SReid Spencer return GV; 4564fd528f2SReid Spencer } 4574fd528f2SReid Spencer case Instruction::ZExt: { 4584fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 4594fd528f2SReid Spencer uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 4604fd528f2SReid Spencer GV.IntVal = GV.IntVal.zext(BitWidth); 4614fd528f2SReid Spencer return GV; 4624fd528f2SReid Spencer } 4634fd528f2SReid Spencer case Instruction::SExt: { 4644fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 4654fd528f2SReid Spencer uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 4664fd528f2SReid Spencer GV.IntVal = GV.IntVal.sext(BitWidth); 4674fd528f2SReid Spencer return GV; 4684fd528f2SReid Spencer } 4694fd528f2SReid Spencer case Instruction::FPTrunc: { 470a1336cf5SDale Johannesen // FIXME long double 4714fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 4724fd528f2SReid Spencer GV.FloatVal = float(GV.DoubleVal); 4734fd528f2SReid Spencer return GV; 4744fd528f2SReid Spencer } 4754fd528f2SReid Spencer case Instruction::FPExt:{ 476a1336cf5SDale Johannesen // FIXME long double 4774fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 4784fd528f2SReid Spencer GV.DoubleVal = double(GV.FloatVal); 4794fd528f2SReid Spencer return GV; 4804fd528f2SReid Spencer } 4814fd528f2SReid Spencer case Instruction::UIToFP: { 4824fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 4834fd528f2SReid Spencer if (CE->getType() == Type::FloatTy) 4844fd528f2SReid Spencer GV.FloatVal = float(GV.IntVal.roundToDouble()); 485a1336cf5SDale Johannesen else if (CE->getType() == Type::DoubleTy) 4864fd528f2SReid Spencer GV.DoubleVal = GV.IntVal.roundToDouble(); 487a1336cf5SDale Johannesen else if (CE->getType() == Type::X86_FP80Ty) { 488a1336cf5SDale Johannesen const uint64_t zero[] = {0, 0}; 489a1336cf5SDale Johannesen APFloat apf = APFloat(APInt(80, 2, zero)); 490ca24fd90SDan Gohman (void)apf.convertFromAPInt(GV.IntVal, 491ca24fd90SDan Gohman false, 4929150652bSDale Johannesen APFloat::rmNearestTiesToEven); 49354306fe4SDale Johannesen GV.IntVal = apf.bitcastToAPInt(); 494a1336cf5SDale Johannesen } 4954fd528f2SReid Spencer return GV; 4964fd528f2SReid Spencer } 4974fd528f2SReid Spencer case Instruction::SIToFP: { 4984fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 4994fd528f2SReid Spencer if (CE->getType() == Type::FloatTy) 5004fd528f2SReid Spencer GV.FloatVal = float(GV.IntVal.signedRoundToDouble()); 501a1336cf5SDale Johannesen else if (CE->getType() == Type::DoubleTy) 5024fd528f2SReid Spencer GV.DoubleVal = GV.IntVal.signedRoundToDouble(); 503a1336cf5SDale Johannesen else if (CE->getType() == Type::X86_FP80Ty) { 504a1336cf5SDale Johannesen const uint64_t zero[] = { 0, 0}; 505a1336cf5SDale Johannesen APFloat apf = APFloat(APInt(80, 2, zero)); 506ca24fd90SDan Gohman (void)apf.convertFromAPInt(GV.IntVal, 507ca24fd90SDan Gohman true, 5089150652bSDale Johannesen APFloat::rmNearestTiesToEven); 50954306fe4SDale Johannesen GV.IntVal = apf.bitcastToAPInt(); 510a1336cf5SDale Johannesen } 5114fd528f2SReid Spencer return GV; 5124fd528f2SReid Spencer } 5134fd528f2SReid Spencer case Instruction::FPToUI: // double->APInt conversion handles sign 5144fd528f2SReid Spencer case Instruction::FPToSI: { 5154fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5164fd528f2SReid Spencer uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 5174fd528f2SReid Spencer if (Op0->getType() == Type::FloatTy) 5184fd528f2SReid Spencer GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth); 519a1336cf5SDale Johannesen else if (Op0->getType() == Type::DoubleTy) 5204fd528f2SReid Spencer GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth); 521a1336cf5SDale Johannesen else if (Op0->getType() == Type::X86_FP80Ty) { 522a1336cf5SDale Johannesen APFloat apf = APFloat(GV.IntVal); 523a1336cf5SDale Johannesen uint64_t v; 5244f0bd68cSDale Johannesen bool ignored; 525a1336cf5SDale Johannesen (void)apf.convertToInteger(&v, BitWidth, 526a1336cf5SDale Johannesen CE->getOpcode()==Instruction::FPToSI, 5274f0bd68cSDale Johannesen APFloat::rmTowardZero, &ignored); 528a1336cf5SDale Johannesen GV.IntVal = v; // endian? 529a1336cf5SDale Johannesen } 5304fd528f2SReid Spencer return GV; 5314fd528f2SReid Spencer } 5326c38f0bbSReid Spencer case Instruction::PtrToInt: { 5334fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5344fd528f2SReid Spencer uint32_t PtrWidth = TD->getPointerSizeInBits(); 5354fd528f2SReid Spencer GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal)); 5364fd528f2SReid Spencer return GV; 5374fd528f2SReid Spencer } 5384fd528f2SReid Spencer case Instruction::IntToPtr: { 5394fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5404fd528f2SReid Spencer uint32_t PtrWidth = TD->getPointerSizeInBits(); 5414fd528f2SReid Spencer if (PtrWidth != GV.IntVal.getBitWidth()) 5424fd528f2SReid Spencer GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth); 5434fd528f2SReid Spencer assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width"); 5444fd528f2SReid Spencer GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue())); 5456c38f0bbSReid Spencer return GV; 5466c38f0bbSReid Spencer } 5476c38f0bbSReid Spencer case Instruction::BitCast: { 5484fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5494fd528f2SReid Spencer const Type* DestTy = CE->getType(); 5504fd528f2SReid Spencer switch (Op0->getType()->getTypeID()) { 5514fd528f2SReid Spencer default: assert(0 && "Invalid bitcast operand"); 5524fd528f2SReid Spencer case Type::IntegerTyID: 5534fd528f2SReid Spencer assert(DestTy->isFloatingPoint() && "invalid bitcast"); 5544fd528f2SReid Spencer if (DestTy == Type::FloatTy) 5554fd528f2SReid Spencer GV.FloatVal = GV.IntVal.bitsToFloat(); 5564fd528f2SReid Spencer else if (DestTy == Type::DoubleTy) 5574fd528f2SReid Spencer GV.DoubleVal = GV.IntVal.bitsToDouble(); 5586c38f0bbSReid Spencer break; 5594fd528f2SReid Spencer case Type::FloatTyID: 5604fd528f2SReid Spencer assert(DestTy == Type::Int32Ty && "Invalid bitcast"); 5614fd528f2SReid Spencer GV.IntVal.floatToBits(GV.FloatVal); 5624fd528f2SReid Spencer break; 5634fd528f2SReid Spencer case Type::DoubleTyID: 5644fd528f2SReid Spencer assert(DestTy == Type::Int64Ty && "Invalid bitcast"); 5654fd528f2SReid Spencer GV.IntVal.doubleToBits(GV.DoubleVal); 5664fd528f2SReid Spencer break; 5674fd528f2SReid Spencer case Type::PointerTyID: 5684fd528f2SReid Spencer assert(isa<PointerType>(DestTy) && "Invalid bitcast"); 5694fd528f2SReid Spencer break; // getConstantValue(Op0) above already converted it 5706c38f0bbSReid Spencer } 5714fd528f2SReid Spencer return GV; 57268cbcc3eSChris Lattner } 57368cbcc3eSChris Lattner case Instruction::Add: 574a5b9645cSDan Gohman case Instruction::FAdd: 5754fd528f2SReid Spencer case Instruction::Sub: 576a5b9645cSDan Gohman case Instruction::FSub: 5774fd528f2SReid Spencer case Instruction::Mul: 578a5b9645cSDan Gohman case Instruction::FMul: 5794fd528f2SReid Spencer case Instruction::UDiv: 5804fd528f2SReid Spencer case Instruction::SDiv: 5814fd528f2SReid Spencer case Instruction::URem: 5824fd528f2SReid Spencer case Instruction::SRem: 5834fd528f2SReid Spencer case Instruction::And: 5844fd528f2SReid Spencer case Instruction::Or: 5854fd528f2SReid Spencer case Instruction::Xor: { 5864fd528f2SReid Spencer GenericValue LHS = getConstantValue(Op0); 5874fd528f2SReid Spencer GenericValue RHS = getConstantValue(CE->getOperand(1)); 5884fd528f2SReid Spencer GenericValue GV; 589c4e6bb5fSChris Lattner switch (CE->getOperand(0)->getType()->getTypeID()) { 590*ccb29cd2STorok Edwin default: LLVM_UNREACHABLE("Bad add type!"); 5917a9c62baSReid Spencer case Type::IntegerTyID: 5924fd528f2SReid Spencer switch (CE->getOpcode()) { 5934fd528f2SReid Spencer default: assert(0 && "Invalid integer opcode"); 5944fd528f2SReid Spencer case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break; 5954fd528f2SReid Spencer case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break; 5964fd528f2SReid Spencer case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break; 5974fd528f2SReid Spencer case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break; 5984fd528f2SReid Spencer case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break; 5994fd528f2SReid Spencer case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break; 6004fd528f2SReid Spencer case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break; 6014fd528f2SReid Spencer case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break; 6024fd528f2SReid Spencer case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break; 6034fd528f2SReid Spencer case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break; 6044fd528f2SReid Spencer } 605c4e6bb5fSChris Lattner break; 606c4e6bb5fSChris Lattner case Type::FloatTyID: 6074fd528f2SReid Spencer switch (CE->getOpcode()) { 608*ccb29cd2STorok Edwin default: LLVM_UNREACHABLE("Invalid float opcode"); 609a5b9645cSDan Gohman case Instruction::FAdd: 6104fd528f2SReid Spencer GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break; 611a5b9645cSDan Gohman case Instruction::FSub: 6124fd528f2SReid Spencer GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break; 613a5b9645cSDan Gohman case Instruction::FMul: 6144fd528f2SReid Spencer GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break; 6154fd528f2SReid Spencer case Instruction::FDiv: 6164fd528f2SReid Spencer GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break; 6174fd528f2SReid Spencer case Instruction::FRem: 6184fd528f2SReid Spencer GV.FloatVal = ::fmodf(LHS.FloatVal,RHS.FloatVal); break; 6194fd528f2SReid Spencer } 620c4e6bb5fSChris Lattner break; 621c4e6bb5fSChris Lattner case Type::DoubleTyID: 6224fd528f2SReid Spencer switch (CE->getOpcode()) { 623*ccb29cd2STorok Edwin default: LLVM_UNREACHABLE("Invalid double opcode"); 624a5b9645cSDan Gohman case Instruction::FAdd: 6254fd528f2SReid Spencer GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break; 626a5b9645cSDan Gohman case Instruction::FSub: 6274fd528f2SReid Spencer GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break; 628a5b9645cSDan Gohman case Instruction::FMul: 6294fd528f2SReid Spencer GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break; 6304fd528f2SReid Spencer case Instruction::FDiv: 6314fd528f2SReid Spencer GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break; 6324fd528f2SReid Spencer case Instruction::FRem: 6334fd528f2SReid Spencer GV.DoubleVal = ::fmod(LHS.DoubleVal,RHS.DoubleVal); break; 6344fd528f2SReid Spencer } 635c4e6bb5fSChris Lattner break; 636a1336cf5SDale Johannesen case Type::X86_FP80TyID: 637a1336cf5SDale Johannesen case Type::PPC_FP128TyID: 638a1336cf5SDale Johannesen case Type::FP128TyID: { 639a1336cf5SDale Johannesen APFloat apfLHS = APFloat(LHS.IntVal); 640a1336cf5SDale Johannesen switch (CE->getOpcode()) { 6416c2d233eSTorok Edwin default: assert(0 && "Invalid long double opcode");llvm_unreachable(); 642a5b9645cSDan Gohman case Instruction::FAdd: 643a1336cf5SDale Johannesen apfLHS.add(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 64454306fe4SDale Johannesen GV.IntVal = apfLHS.bitcastToAPInt(); 645a1336cf5SDale Johannesen break; 646a5b9645cSDan Gohman case Instruction::FSub: 647a1336cf5SDale Johannesen apfLHS.subtract(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 64854306fe4SDale Johannesen GV.IntVal = apfLHS.bitcastToAPInt(); 649a1336cf5SDale Johannesen break; 650a5b9645cSDan Gohman case Instruction::FMul: 651a1336cf5SDale Johannesen apfLHS.multiply(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 65254306fe4SDale Johannesen GV.IntVal = apfLHS.bitcastToAPInt(); 653a1336cf5SDale Johannesen break; 654a1336cf5SDale Johannesen case Instruction::FDiv: 655a1336cf5SDale Johannesen apfLHS.divide(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 65654306fe4SDale Johannesen GV.IntVal = apfLHS.bitcastToAPInt(); 657a1336cf5SDale Johannesen break; 658a1336cf5SDale Johannesen case Instruction::FRem: 659a1336cf5SDale Johannesen apfLHS.mod(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 66054306fe4SDale Johannesen GV.IntVal = apfLHS.bitcastToAPInt(); 661a1336cf5SDale Johannesen break; 662a1336cf5SDale Johannesen } 663a1336cf5SDale Johannesen } 664a1336cf5SDale Johannesen break; 665c4e6bb5fSChris Lattner } 6664fd528f2SReid Spencer return GV; 6674fd528f2SReid Spencer } 6689de0d14dSChris Lattner default: 66968cbcc3eSChris Lattner break; 67068cbcc3eSChris Lattner } 671*ccb29cd2STorok Edwin std::string msg; 672*ccb29cd2STorok Edwin raw_string_ostream Msg(msg); 673*ccb29cd2STorok Edwin Msg << "ConstantExpr not handled: " << *CE; 674*ccb29cd2STorok Edwin llvm_report_error(Msg.str()); 6759de0d14dSChris Lattner } 676996fe010SChris Lattner 6774fd528f2SReid Spencer GenericValue Result; 6786b727599SChris Lattner switch (C->getType()->getTypeID()) { 67987aa65f4SReid Spencer case Type::FloatTyID: 680bed9dc42SDale Johannesen Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat(); 6817a9c62baSReid Spencer break; 68287aa65f4SReid Spencer case Type::DoubleTyID: 683bed9dc42SDale Johannesen Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble(); 68487aa65f4SReid Spencer break; 685a1336cf5SDale Johannesen case Type::X86_FP80TyID: 686a1336cf5SDale Johannesen case Type::FP128TyID: 687a1336cf5SDale Johannesen case Type::PPC_FP128TyID: 68854306fe4SDale Johannesen Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt(); 689a1336cf5SDale Johannesen break; 69087aa65f4SReid Spencer case Type::IntegerTyID: 69187aa65f4SReid Spencer Result.IntVal = cast<ConstantInt>(C)->getValue(); 69287aa65f4SReid Spencer break; 693996fe010SChris Lattner case Type::PointerTyID: 6946a0fd73bSReid Spencer if (isa<ConstantPointerNull>(C)) 695996fe010SChris Lattner Result.PointerVal = 0; 6966a0fd73bSReid Spencer else if (const Function *F = dyn_cast<Function>(C)) 6976a0fd73bSReid Spencer Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F))); 6986a0fd73bSReid Spencer else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C)) 6996a0fd73bSReid Spencer Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV))); 700e6492f10SChris Lattner else 701996fe010SChris Lattner assert(0 && "Unknown constant pointer type!"); 702996fe010SChris Lattner break; 703996fe010SChris Lattner default: 704*ccb29cd2STorok Edwin std::string msg; 705*ccb29cd2STorok Edwin raw_string_ostream Msg(msg); 706*ccb29cd2STorok Edwin Msg << "ERROR: Constant unimplemented for type: " << *C->getType(); 707*ccb29cd2STorok Edwin llvm_report_error(Msg.str()); 708996fe010SChris Lattner } 709996fe010SChris Lattner return Result; 710996fe010SChris Lattner } 711996fe010SChris Lattner 7121202d1b1SDuncan Sands /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst 7131202d1b1SDuncan Sands /// with the integer held in IntVal. 7141202d1b1SDuncan Sands static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, 7151202d1b1SDuncan Sands unsigned StoreBytes) { 7161202d1b1SDuncan Sands assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!"); 7171202d1b1SDuncan Sands uint8_t *Src = (uint8_t *)IntVal.getRawData(); 7185c65cb46SDuncan Sands 719aa121227SChris Lattner if (sys::isLittleEndianHost()) 7201202d1b1SDuncan Sands // Little-endian host - the source is ordered from LSB to MSB. Order the 7211202d1b1SDuncan Sands // destination from LSB to MSB: Do a straight copy. 7225c65cb46SDuncan Sands memcpy(Dst, Src, StoreBytes); 7235c65cb46SDuncan Sands else { 7245c65cb46SDuncan Sands // Big-endian host - the source is an array of 64 bit words ordered from 7251202d1b1SDuncan Sands // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination 7261202d1b1SDuncan Sands // from MSB to LSB: Reverse the word order, but not the bytes in a word. 7275c65cb46SDuncan Sands while (StoreBytes > sizeof(uint64_t)) { 7285c65cb46SDuncan Sands StoreBytes -= sizeof(uint64_t); 7295c65cb46SDuncan Sands // May not be aligned so use memcpy. 7305c65cb46SDuncan Sands memcpy(Dst + StoreBytes, Src, sizeof(uint64_t)); 7315c65cb46SDuncan Sands Src += sizeof(uint64_t); 7325c65cb46SDuncan Sands } 7335c65cb46SDuncan Sands 7345c65cb46SDuncan Sands memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes); 735815f8dd2SReid Spencer } 7367a9c62baSReid Spencer } 7371202d1b1SDuncan Sands 7381202d1b1SDuncan Sands /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr 7391202d1b1SDuncan Sands /// is the address of the memory at which to store Val, cast to GenericValue *. 7401202d1b1SDuncan Sands /// It is not a pointer to a GenericValue containing the address at which to 7411202d1b1SDuncan Sands /// store Val. 74209053e62SEvan Cheng void ExecutionEngine::StoreValueToMemory(const GenericValue &Val, 74309053e62SEvan Cheng GenericValue *Ptr, const Type *Ty) { 7441202d1b1SDuncan Sands const unsigned StoreBytes = getTargetData()->getTypeStoreSize(Ty); 7451202d1b1SDuncan Sands 7461202d1b1SDuncan Sands switch (Ty->getTypeID()) { 7471202d1b1SDuncan Sands case Type::IntegerTyID: 7481202d1b1SDuncan Sands StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes); 7491202d1b1SDuncan Sands break; 750996fe010SChris Lattner case Type::FloatTyID: 75187aa65f4SReid Spencer *((float*)Ptr) = Val.FloatVal; 75287aa65f4SReid Spencer break; 75387aa65f4SReid Spencer case Type::DoubleTyID: 75487aa65f4SReid Spencer *((double*)Ptr) = Val.DoubleVal; 755996fe010SChris Lattner break; 7564d7e4ee7SDale Johannesen case Type::X86_FP80TyID: 7574d7e4ee7SDale Johannesen memcpy(Ptr, Val.IntVal.getRawData(), 10); 758a1336cf5SDale Johannesen break; 7597a9c62baSReid Spencer case Type::PointerTyID: 7601202d1b1SDuncan Sands // Ensure 64 bit target pointers are fully initialized on 32 bit hosts. 7611202d1b1SDuncan Sands if (StoreBytes != sizeof(PointerTy)) 7621202d1b1SDuncan Sands memset(Ptr, 0, StoreBytes); 7631202d1b1SDuncan Sands 76487aa65f4SReid Spencer *((PointerTy*)Ptr) = Val.PointerVal; 765996fe010SChris Lattner break; 766996fe010SChris Lattner default: 767f3baad3eSBill Wendling cerr << "Cannot store value of type " << *Ty << "!\n"; 768996fe010SChris Lattner } 7691202d1b1SDuncan Sands 770aa121227SChris Lattner if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian()) 7711202d1b1SDuncan Sands // Host and target are different endian - reverse the stored bytes. 7721202d1b1SDuncan Sands std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr); 773996fe010SChris Lattner } 774996fe010SChris Lattner 7751202d1b1SDuncan Sands /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting 7761202d1b1SDuncan Sands /// from Src into IntVal, which is assumed to be wide enough and to hold zero. 7771202d1b1SDuncan Sands static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) { 7781202d1b1SDuncan Sands assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!"); 7791202d1b1SDuncan Sands uint8_t *Dst = (uint8_t *)IntVal.getRawData(); 7805c65cb46SDuncan Sands 781aa121227SChris Lattner if (sys::isLittleEndianHost()) 7825c65cb46SDuncan Sands // Little-endian host - the destination must be ordered from LSB to MSB. 7835c65cb46SDuncan Sands // The source is ordered from LSB to MSB: Do a straight copy. 7845c65cb46SDuncan Sands memcpy(Dst, Src, LoadBytes); 7855c65cb46SDuncan Sands else { 7865c65cb46SDuncan Sands // Big-endian - the destination is an array of 64 bit words ordered from 7875c65cb46SDuncan Sands // LSW to MSW. Each word must be ordered from MSB to LSB. The source is 7885c65cb46SDuncan Sands // ordered from MSB to LSB: Reverse the word order, but not the bytes in 7895c65cb46SDuncan Sands // a word. 7905c65cb46SDuncan Sands while (LoadBytes > sizeof(uint64_t)) { 7915c65cb46SDuncan Sands LoadBytes -= sizeof(uint64_t); 7925c65cb46SDuncan Sands // May not be aligned so use memcpy. 7935c65cb46SDuncan Sands memcpy(Dst, Src + LoadBytes, sizeof(uint64_t)); 7945c65cb46SDuncan Sands Dst += sizeof(uint64_t); 7955c65cb46SDuncan Sands } 7965c65cb46SDuncan Sands 7975c65cb46SDuncan Sands memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes); 7985c65cb46SDuncan Sands } 7997a9c62baSReid Spencer } 8001202d1b1SDuncan Sands 8011202d1b1SDuncan Sands /// FIXME: document 8021202d1b1SDuncan Sands /// 8031202d1b1SDuncan Sands void ExecutionEngine::LoadValueFromMemory(GenericValue &Result, 8041202d1b1SDuncan Sands GenericValue *Ptr, 8051202d1b1SDuncan Sands const Type *Ty) { 8061202d1b1SDuncan Sands const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty); 8071202d1b1SDuncan Sands 808aa121227SChris Lattner if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian()) { 8091202d1b1SDuncan Sands // Host and target are different endian - reverse copy the stored 8101202d1b1SDuncan Sands // bytes into a buffer, and load from that. 8111202d1b1SDuncan Sands uint8_t *Src = (uint8_t*)Ptr; 8121202d1b1SDuncan Sands uint8_t *Buf = (uint8_t*)alloca(LoadBytes); 8131202d1b1SDuncan Sands std::reverse_copy(Src, Src + LoadBytes, Buf); 8141202d1b1SDuncan Sands Ptr = (GenericValue*)Buf; 8151202d1b1SDuncan Sands } 8161202d1b1SDuncan Sands 8171202d1b1SDuncan Sands switch (Ty->getTypeID()) { 8181202d1b1SDuncan Sands case Type::IntegerTyID: 8191202d1b1SDuncan Sands // An APInt with all words initially zero. 8201202d1b1SDuncan Sands Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0); 8211202d1b1SDuncan Sands LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes); 8221202d1b1SDuncan Sands break; 8237f389e8cSChris Lattner case Type::FloatTyID: 82487aa65f4SReid Spencer Result.FloatVal = *((float*)Ptr); 82587aa65f4SReid Spencer break; 82687aa65f4SReid Spencer case Type::DoubleTyID: 82787aa65f4SReid Spencer Result.DoubleVal = *((double*)Ptr); 8287f389e8cSChris Lattner break; 8297a9c62baSReid Spencer case Type::PointerTyID: 83087aa65f4SReid Spencer Result.PointerVal = *((PointerTy*)Ptr); 8317f389e8cSChris Lattner break; 832a1336cf5SDale Johannesen case Type::X86_FP80TyID: { 833a1336cf5SDale Johannesen // This is endian dependent, but it will only work on x86 anyway. 83426d6539eSDuncan Sands // FIXME: Will not trap if loading a signaling NaN. 835ff306287SDuncan Sands uint64_t y[2]; 8364d7e4ee7SDale Johannesen memcpy(y, Ptr, 10); 837ff306287SDuncan Sands Result.IntVal = APInt(80, 2, y); 838a1336cf5SDale Johannesen break; 839a1336cf5SDale Johannesen } 8407f389e8cSChris Lattner default: 841*ccb29cd2STorok Edwin std::string msg; 842*ccb29cd2STorok Edwin raw_string_ostream Msg(msg); 843*ccb29cd2STorok Edwin Msg << "Cannot load value of type " << *Ty << "!"; 844*ccb29cd2STorok Edwin llvm_report_error(Msg.str()); 8457f389e8cSChris Lattner } 8467f389e8cSChris Lattner } 8477f389e8cSChris Lattner 848996fe010SChris Lattner // InitializeMemory - Recursive function to apply a Constant value into the 849996fe010SChris Lattner // specified memory location... 850996fe010SChris Lattner // 851996fe010SChris Lattner void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { 852972fd1a1SEvan Cheng DOUT << "JIT: Initializing " << Addr << " "; 853b086d382SDale Johannesen DEBUG(Init->dump()); 85461753bf8SChris Lattner if (isa<UndefValue>(Init)) { 85561753bf8SChris Lattner return; 856d84d35baSReid Spencer } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) { 85769d62138SRobert Bocchino unsigned ElementSize = 858af9eaa83SDuncan Sands getTargetData()->getTypeAllocSize(CP->getType()->getElementType()); 85969d62138SRobert Bocchino for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) 86069d62138SRobert Bocchino InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize); 86169d62138SRobert Bocchino return; 8621dd86b11SChris Lattner } else if (isa<ConstantAggregateZero>(Init)) { 863af9eaa83SDuncan Sands memset(Addr, 0, (size_t)getTargetData()->getTypeAllocSize(Init->getType())); 8641dd86b11SChris Lattner return; 86569ddfbfeSDan Gohman } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) { 86669ddfbfeSDan Gohman unsigned ElementSize = 867af9eaa83SDuncan Sands getTargetData()->getTypeAllocSize(CPA->getType()->getElementType()); 86869ddfbfeSDan Gohman for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) 86969ddfbfeSDan Gohman InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); 87069ddfbfeSDan Gohman return; 87169ddfbfeSDan Gohman } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) { 87269ddfbfeSDan Gohman const StructLayout *SL = 87369ddfbfeSDan Gohman getTargetData()->getStructLayout(cast<StructType>(CPS->getType())); 87469ddfbfeSDan Gohman for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) 87569ddfbfeSDan Gohman InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i)); 87669ddfbfeSDan Gohman return; 87761753bf8SChris Lattner } else if (Init->getType()->isFirstClassType()) { 878996fe010SChris Lattner GenericValue Val = getConstantValue(Init); 879996fe010SChris Lattner StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); 880996fe010SChris Lattner return; 881996fe010SChris Lattner } 882996fe010SChris Lattner 883f3baad3eSBill Wendling cerr << "Bad Type: " << *Init->getType() << "\n"; 884996fe010SChris Lattner assert(0 && "Unknown constant type to initialize memory with!"); 885996fe010SChris Lattner } 886996fe010SChris Lattner 887996fe010SChris Lattner /// EmitGlobals - Emit all of the global variables to memory, storing their 888996fe010SChris Lattner /// addresses into GlobalAddress. This must make sure to copy the contents of 889996fe010SChris Lattner /// their initializers into the memory. 890996fe010SChris Lattner /// 891996fe010SChris Lattner void ExecutionEngine::emitGlobals() { 892996fe010SChris Lattner 893996fe010SChris Lattner // Loop over all of the global variables in the program, allocating the memory 8940621caefSChris Lattner // to hold them. If there is more than one module, do a prepass over globals 8950621caefSChris Lattner // to figure out how the different modules should link together. 8960621caefSChris Lattner // 8970621caefSChris Lattner std::map<std::pair<std::string, const Type*>, 8980621caefSChris Lattner const GlobalValue*> LinkedGlobalsMap; 8990621caefSChris Lattner 9000621caefSChris Lattner if (Modules.size() != 1) { 9010621caefSChris Lattner for (unsigned m = 0, e = Modules.size(); m != e; ++m) { 9020621caefSChris Lattner Module &M = *Modules[m]->getModule(); 9030621caefSChris Lattner for (Module::const_global_iterator I = M.global_begin(), 9040621caefSChris Lattner E = M.global_end(); I != E; ++I) { 9050621caefSChris Lattner const GlobalValue *GV = I; 9066de96a1bSRafael Espindola if (GV->hasLocalLinkage() || GV->isDeclaration() || 9070621caefSChris Lattner GV->hasAppendingLinkage() || !GV->hasName()) 9080621caefSChris Lattner continue;// Ignore external globals and globals with internal linkage. 9090621caefSChris Lattner 9100621caefSChris Lattner const GlobalValue *&GVEntry = 9110621caefSChris Lattner LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; 9120621caefSChris Lattner 9130621caefSChris Lattner // If this is the first time we've seen this global, it is the canonical 9140621caefSChris Lattner // version. 9150621caefSChris Lattner if (!GVEntry) { 9160621caefSChris Lattner GVEntry = GV; 9170621caefSChris Lattner continue; 9180621caefSChris Lattner } 9190621caefSChris Lattner 9200621caefSChris Lattner // If the existing global is strong, never replace it. 921d61d39ecSAnton Korobeynikov if (GVEntry->hasExternalLinkage() || 922d61d39ecSAnton Korobeynikov GVEntry->hasDLLImportLinkage() || 923d61d39ecSAnton Korobeynikov GVEntry->hasDLLExportLinkage()) 9240621caefSChris Lattner continue; 9250621caefSChris Lattner 9260621caefSChris Lattner // Otherwise, we know it's linkonce/weak, replace it if this is a strong 927ce4396bcSDale Johannesen // symbol. FIXME is this right for common? 92812c94949SAnton Korobeynikov if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage()) 9290621caefSChris Lattner GVEntry = GV; 9300621caefSChris Lattner } 9310621caefSChris Lattner } 9320621caefSChris Lattner } 9330621caefSChris Lattner 9340621caefSChris Lattner std::vector<const GlobalValue*> NonCanonicalGlobals; 9350621caefSChris Lattner for (unsigned m = 0, e = Modules.size(); m != e; ++m) { 9360621caefSChris Lattner Module &M = *Modules[m]->getModule(); 9378ffb6611SChris Lattner for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 9380621caefSChris Lattner I != E; ++I) { 9390621caefSChris Lattner // In the multi-module case, see what this global maps to. 9400621caefSChris Lattner if (!LinkedGlobalsMap.empty()) { 9410621caefSChris Lattner if (const GlobalValue *GVEntry = 9420621caefSChris Lattner LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) { 9430621caefSChris Lattner // If something else is the canonical global, ignore this one. 9440621caefSChris Lattner if (GVEntry != &*I) { 9450621caefSChris Lattner NonCanonicalGlobals.push_back(I); 9460621caefSChris Lattner continue; 9470621caefSChris Lattner } 9480621caefSChris Lattner } 9490621caefSChris Lattner } 9500621caefSChris Lattner 9515301e7c6SReid Spencer if (!I->isDeclaration()) { 9525457ce9aSNicolas Geoffray addGlobalMapping(I, getMemoryForGV(I)); 953996fe010SChris Lattner } else { 954e8bbcfc2SBrian Gaeke // External variable reference. Try to use the dynamic loader to 955e8bbcfc2SBrian Gaeke // get a pointer to it. 9560621caefSChris Lattner if (void *SymAddr = 9570621caefSChris Lattner sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName().c_str())) 958748e8579SChris Lattner addGlobalMapping(I, SymAddr); 9599de0d14dSChris Lattner else { 9606c2d233eSTorok Edwin llvm_report_error("Could not resolve external global address: " 9616c2d233eSTorok Edwin +I->getName()); 9629de0d14dSChris Lattner } 963996fe010SChris Lattner } 9640621caefSChris Lattner } 9650621caefSChris Lattner 9660621caefSChris Lattner // If there are multiple modules, map the non-canonical globals to their 9670621caefSChris Lattner // canonical location. 9680621caefSChris Lattner if (!NonCanonicalGlobals.empty()) { 9690621caefSChris Lattner for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) { 9700621caefSChris Lattner const GlobalValue *GV = NonCanonicalGlobals[i]; 9710621caefSChris Lattner const GlobalValue *CGV = 9720621caefSChris Lattner LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; 9730621caefSChris Lattner void *Ptr = getPointerToGlobalIfAvailable(CGV); 9740621caefSChris Lattner assert(Ptr && "Canonical global wasn't codegen'd!"); 975a67f06b9SNuno Lopes addGlobalMapping(GV, Ptr); 9760621caefSChris Lattner } 9770621caefSChris Lattner } 978996fe010SChris Lattner 9797a9c62baSReid Spencer // Now that all of the globals are set up in memory, loop through them all 9807a9c62baSReid Spencer // and initialize their contents. 9818ffb6611SChris Lattner for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 9820621caefSChris Lattner I != E; ++I) { 9835301e7c6SReid Spencer if (!I->isDeclaration()) { 9840621caefSChris Lattner if (!LinkedGlobalsMap.empty()) { 9850621caefSChris Lattner if (const GlobalValue *GVEntry = 9860621caefSChris Lattner LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) 9870621caefSChris Lattner if (GVEntry != &*I) // Not the canonical variable. 9880621caefSChris Lattner continue; 9890621caefSChris Lattner } 9906bbe3eceSChris Lattner EmitGlobalVariable(I); 9916bbe3eceSChris Lattner } 9920621caefSChris Lattner } 9930621caefSChris Lattner } 9940621caefSChris Lattner } 9956bbe3eceSChris Lattner 9966bbe3eceSChris Lattner // EmitGlobalVariable - This method emits the specified global variable to the 9976bbe3eceSChris Lattner // address specified in GlobalAddresses, or allocates new memory if it's not 9986bbe3eceSChris Lattner // already in the map. 999fbcc0aa1SChris Lattner void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { 1000748e8579SChris Lattner void *GA = getPointerToGlobalIfAvailable(GV); 1001dc631735SChris Lattner 10026bbe3eceSChris Lattner if (GA == 0) { 10036bbe3eceSChris Lattner // If it's not already specified, allocate memory for the global. 10045457ce9aSNicolas Geoffray GA = getMemoryForGV(GV); 1005748e8579SChris Lattner addGlobalMapping(GV, GA); 10066bbe3eceSChris Lattner } 1007fbcc0aa1SChris Lattner 10085457ce9aSNicolas Geoffray // Don't initialize if it's thread local, let the client do it. 10095457ce9aSNicolas Geoffray if (!GV->isThreadLocal()) 10106bbe3eceSChris Lattner InitializeMemory(GV->getInitializer(), GA); 10115457ce9aSNicolas Geoffray 10125457ce9aSNicolas Geoffray const Type *ElTy = GV->getType()->getElementType(); 1013af9eaa83SDuncan Sands size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy); 1014df1f1524SChris Lattner NumInitBytes += (unsigned)GVSize; 10156bbe3eceSChris Lattner ++NumGlobals; 1016996fe010SChris Lattner } 1017