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" 166bf87df5SJeffrey Yasskin #include "llvm/ExecutionEngine/ExecutionEngine.h" 176bf87df5SJeffrey Yasskin 18996fe010SChris Lattner #include "llvm/Constants.h" 19260b0c88SMisha Brukman #include "llvm/DerivedTypes.h" 20996fe010SChris Lattner #include "llvm/Module.h" 21ad481312SChris Lattner #include "llvm/ExecutionEngine/GenericValue.h" 22390d78b3SChris Lattner #include "llvm/ADT/Statistic.h" 237c16caa3SReid Spencer #include "llvm/Support/Debug.h" 246c2d233eSTorok Edwin #include "llvm/Support/ErrorHandling.h" 256d8dd189SChris Lattner #include "llvm/Support/MutexGuard.h" 266bf87df5SJeffrey Yasskin #include "llvm/Support/ValueHandle.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 38*31faefffSJeffrey Yasskin ExecutionEngine *(*ExecutionEngine::JITCtor)( 39*31faefffSJeffrey Yasskin Module *M, 40fc8a2d5aSReid Kleckner std::string *ErrorStr, 41fc8a2d5aSReid Kleckner JITMemoryManager *JMM, 42fc8a2d5aSReid Kleckner CodeGenOpt::Level OptLevel, 43700d08e1SEric Christopher bool GVsWithCode, 44*31faefffSJeffrey Yasskin CodeModel::Model CMM, 45*31faefffSJeffrey Yasskin StringRef MArch, 46*31faefffSJeffrey Yasskin StringRef MCPU, 47*31faefffSJeffrey Yasskin const SmallVectorImpl<std::string>& MAttrs) = 0; 48091217beSJeffrey Yasskin ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M, 49fc8a2d5aSReid Kleckner std::string *ErrorStr) = 0; 5021ad494fSNicolas Geoffray ExecutionEngine::EERegisterFn ExecutionEngine::ExceptionTableRegister = 0; 5121ad494fSNicolas Geoffray 522d52c1b8SChris Lattner 53091217beSJeffrey Yasskin ExecutionEngine::ExecutionEngine(Module *M) 54f98e981cSJeffrey Yasskin : EEState(*this), 55f98e981cSJeffrey Yasskin LazyFunctionCreator(0) { 564567db45SJeffrey Yasskin CompilingLazily = false; 57cdc0060eSEvan Cheng GVCompilationDisabled = false; 5884a9055eSEvan Cheng SymbolSearchingDisabled = false; 59091217beSJeffrey Yasskin Modules.push_back(M); 60091217beSJeffrey Yasskin assert(M && "Module is null?"); 61260b0c88SMisha Brukman } 62260b0c88SMisha Brukman 6392f8b30dSBrian Gaeke ExecutionEngine::~ExecutionEngine() { 64603682adSReid Spencer clearAllGlobalMappings(); 650621caefSChris Lattner for (unsigned i = 0, e = Modules.size(); i != e; ++i) 660621caefSChris Lattner delete Modules[i]; 6792f8b30dSBrian Gaeke } 6892f8b30dSBrian Gaeke 695457ce9aSNicolas Geoffray char* ExecutionEngine::getMemoryForGV(const GlobalVariable* GV) { 705457ce9aSNicolas Geoffray const Type *ElTy = GV->getType()->getElementType(); 71af9eaa83SDuncan Sands size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy); 725457ce9aSNicolas Geoffray return new char[GVSize]; 735457ce9aSNicolas Geoffray } 745457ce9aSNicolas Geoffray 75091217beSJeffrey Yasskin /// removeModule - Remove a Module from the list of modules. 76091217beSJeffrey Yasskin bool ExecutionEngine::removeModule(Module *M) { 77091217beSJeffrey Yasskin for(SmallVector<Module *, 1>::iterator I = Modules.begin(), 78324fe890SDevang Patel E = Modules.end(); I != E; ++I) { 79091217beSJeffrey Yasskin Module *Found = *I; 80091217beSJeffrey Yasskin if (Found == M) { 81324fe890SDevang Patel Modules.erase(I); 82091217beSJeffrey Yasskin clearGlobalMappingsFromModule(M); 83091217beSJeffrey Yasskin return true; 84324fe890SDevang Patel } 85324fe890SDevang Patel } 86091217beSJeffrey Yasskin return false; 87617001d8SNate Begeman } 88617001d8SNate Begeman 890621caefSChris Lattner /// FindFunctionNamed - Search all of the active modules to find the one that 900621caefSChris Lattner /// defines FnName. This is very slow operation and shouldn't be used for 910621caefSChris Lattner /// general code. 920621caefSChris Lattner Function *ExecutionEngine::FindFunctionNamed(const char *FnName) { 930621caefSChris Lattner for (unsigned i = 0, e = Modules.size(); i != e; ++i) { 94091217beSJeffrey Yasskin if (Function *F = Modules[i]->getFunction(FnName)) 950621caefSChris Lattner return F; 960621caefSChris Lattner } 970621caefSChris Lattner return 0; 980621caefSChris Lattner } 990621caefSChris Lattner 1000621caefSChris Lattner 101307c053fSJeffrey Yasskin void *ExecutionEngineState::RemoveMapping( 102307c053fSJeffrey Yasskin const MutexGuard &, const GlobalValue *ToUnmap) { 103d0fc8f80SJeffrey Yasskin GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap); 104307c053fSJeffrey Yasskin void *OldVal; 105307c053fSJeffrey Yasskin if (I == GlobalAddressMap.end()) 106307c053fSJeffrey Yasskin OldVal = 0; 107307c053fSJeffrey Yasskin else { 108307c053fSJeffrey Yasskin OldVal = I->second; 109307c053fSJeffrey Yasskin GlobalAddressMap.erase(I); 110307c053fSJeffrey Yasskin } 111307c053fSJeffrey Yasskin 112307c053fSJeffrey Yasskin GlobalAddressReverseMap.erase(OldVal); 113307c053fSJeffrey Yasskin return OldVal; 114307c053fSJeffrey Yasskin } 115307c053fSJeffrey Yasskin 1166d8dd189SChris Lattner /// addGlobalMapping - Tell the execution engine that the specified global is 1176d8dd189SChris Lattner /// at the specified location. This is used internally as functions are JIT'd 1186d8dd189SChris Lattner /// and as global variables are laid out in memory. It can and should also be 1196d8dd189SChris Lattner /// used by clients of the EE that want to have an LLVM global overlay 1206d8dd189SChris Lattner /// existing data in memory. 1216d8dd189SChris Lattner void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) { 1226d8dd189SChris Lattner MutexGuard locked(lock); 1236d8dd189SChris Lattner 1240967d2dfSDavid Greene DEBUG(dbgs() << "JIT: Map \'" << GV->getName() 1259813b0b0SDaniel Dunbar << "\' to [" << Addr << "]\n";); 126d0fc8f80SJeffrey Yasskin void *&CurVal = EEState.getGlobalAddressMap(locked)[GV]; 1276d8dd189SChris Lattner assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!"); 1286d8dd189SChris Lattner CurVal = Addr; 1296d8dd189SChris Lattner 1306d8dd189SChris Lattner // If we are using the reverse mapping, add it too 131f98e981cSJeffrey Yasskin if (!EEState.getGlobalAddressReverseMap(locked).empty()) { 1326bf87df5SJeffrey Yasskin AssertingVH<const GlobalValue> &V = 133f98e981cSJeffrey Yasskin EEState.getGlobalAddressReverseMap(locked)[Addr]; 1346d8dd189SChris Lattner assert((V == 0 || GV == 0) && "GlobalMapping already established!"); 1356d8dd189SChris Lattner V = GV; 1366d8dd189SChris Lattner } 1376d8dd189SChris Lattner } 1386d8dd189SChris Lattner 1396d8dd189SChris Lattner /// clearAllGlobalMappings - Clear all global mappings and start over again 1406d8dd189SChris Lattner /// use in dynamic compilation scenarios when you want to move globals 1416d8dd189SChris Lattner void ExecutionEngine::clearAllGlobalMappings() { 1426d8dd189SChris Lattner MutexGuard locked(lock); 1436d8dd189SChris Lattner 144f98e981cSJeffrey Yasskin EEState.getGlobalAddressMap(locked).clear(); 145f98e981cSJeffrey Yasskin EEState.getGlobalAddressReverseMap(locked).clear(); 1466d8dd189SChris Lattner } 1476d8dd189SChris Lattner 1488f83fc4dSNate Begeman /// clearGlobalMappingsFromModule - Clear all global mappings that came from a 1498f83fc4dSNate Begeman /// particular module, because it has been removed from the JIT. 1508f83fc4dSNate Begeman void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) { 1518f83fc4dSNate Begeman MutexGuard locked(lock); 1528f83fc4dSNate Begeman 1538f83fc4dSNate Begeman for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) { 154f98e981cSJeffrey Yasskin EEState.RemoveMapping(locked, FI); 1558f83fc4dSNate Begeman } 1568f83fc4dSNate Begeman for (Module::global_iterator GI = M->global_begin(), GE = M->global_end(); 1578f83fc4dSNate Begeman GI != GE; ++GI) { 158f98e981cSJeffrey Yasskin EEState.RemoveMapping(locked, 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 168d0fc8f80SJeffrey Yasskin ExecutionEngineState::GlobalAddressMapTy &Map = 169f98e981cSJeffrey Yasskin EEState.getGlobalAddressMap(locked); 170ee181730SChris Lattner 1716d8dd189SChris Lattner // Deleting from the mapping? 1726d8dd189SChris Lattner if (Addr == 0) { 173f98e981cSJeffrey Yasskin return EEState.RemoveMapping(locked, GV); 174ee181730SChris Lattner } 175ee181730SChris Lattner 176d0fc8f80SJeffrey Yasskin void *&CurVal = Map[GV]; 177ee181730SChris Lattner void *OldVal = CurVal; 178ee181730SChris Lattner 179f98e981cSJeffrey Yasskin if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty()) 180f98e981cSJeffrey Yasskin EEState.getGlobalAddressReverseMap(locked).erase(CurVal); 1816d8dd189SChris Lattner CurVal = Addr; 1826d8dd189SChris Lattner 1836d8dd189SChris Lattner // If we are using the reverse mapping, add it too 184f98e981cSJeffrey Yasskin if (!EEState.getGlobalAddressReverseMap(locked).empty()) { 1856bf87df5SJeffrey Yasskin AssertingVH<const GlobalValue> &V = 186f98e981cSJeffrey Yasskin EEState.getGlobalAddressReverseMap(locked)[Addr]; 1876d8dd189SChris Lattner assert((V == 0 || GV == 0) && "GlobalMapping already established!"); 1886d8dd189SChris Lattner V = GV; 1896d8dd189SChris Lattner } 190ee181730SChris Lattner return OldVal; 1916d8dd189SChris Lattner } 1926d8dd189SChris Lattner 1936d8dd189SChris Lattner /// getPointerToGlobalIfAvailable - This returns the address of the specified 1946d8dd189SChris Lattner /// global value if it is has already been codegen'd, otherwise it returns null. 1956d8dd189SChris Lattner /// 1966d8dd189SChris Lattner void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) { 1976d8dd189SChris Lattner MutexGuard locked(lock); 1986d8dd189SChris Lattner 199d0fc8f80SJeffrey Yasskin ExecutionEngineState::GlobalAddressMapTy::iterator I = 200d0fc8f80SJeffrey Yasskin EEState.getGlobalAddressMap(locked).find(GV); 201f98e981cSJeffrey Yasskin return I != EEState.getGlobalAddressMap(locked).end() ? I->second : 0; 2026d8dd189SChris Lattner } 2036d8dd189SChris Lattner 204748e8579SChris Lattner /// getGlobalValueAtAddress - Return the LLVM global value object that starts 205748e8579SChris Lattner /// at the specified address. 206748e8579SChris Lattner /// 207748e8579SChris Lattner const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { 20879876f52SReid Spencer MutexGuard locked(lock); 20979876f52SReid Spencer 210748e8579SChris Lattner // If we haven't computed the reverse mapping yet, do so first. 211f98e981cSJeffrey Yasskin if (EEState.getGlobalAddressReverseMap(locked).empty()) { 212d0fc8f80SJeffrey Yasskin for (ExecutionEngineState::GlobalAddressMapTy::iterator 213f98e981cSJeffrey Yasskin I = EEState.getGlobalAddressMap(locked).begin(), 214f98e981cSJeffrey Yasskin E = EEState.getGlobalAddressMap(locked).end(); I != E; ++I) 215f98e981cSJeffrey Yasskin EEState.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, 2166d8dd189SChris Lattner I->first)); 217748e8579SChris Lattner } 218748e8579SChris Lattner 2196bf87df5SJeffrey Yasskin std::map<void *, AssertingVH<const GlobalValue> >::iterator I = 220f98e981cSJeffrey Yasskin EEState.getGlobalAddressReverseMap(locked).find(Addr); 221f98e981cSJeffrey Yasskin return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : 0; 222748e8579SChris Lattner } 2235a0d4829SChris Lattner 2245a0d4829SChris Lattner // CreateArgv - Turn a vector of strings into a nice argv style array of 2255a0d4829SChris Lattner // pointers to null terminated strings. 2265a0d4829SChris Lattner // 22755f1c09eSOwen Anderson static void *CreateArgv(LLVMContext &C, ExecutionEngine *EE, 2285a0d4829SChris Lattner const std::vector<std::string> &InputArgv) { 22920a631fdSOwen Anderson unsigned PtrSize = EE->getTargetData()->getPointerSize(); 2305a0d4829SChris Lattner char *Result = new char[(InputArgv.size()+1)*PtrSize]; 2315a0d4829SChris Lattner 2320967d2dfSDavid Greene DEBUG(dbgs() << "JIT: ARGV = " << (void*)Result << "\n"); 2339ed7b16bSDuncan Sands const Type *SBytePtr = Type::getInt8PtrTy(C); 2345a0d4829SChris Lattner 2355a0d4829SChris Lattner for (unsigned i = 0; i != InputArgv.size(); ++i) { 2365a0d4829SChris Lattner unsigned Size = InputArgv[i].size()+1; 2375a0d4829SChris Lattner char *Dest = new char[Size]; 2380967d2dfSDavid Greene DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n"); 2395a0d4829SChris Lattner 2405a0d4829SChris Lattner std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); 2415a0d4829SChris Lattner Dest[Size-1] = 0; 2425a0d4829SChris Lattner 2435a0d4829SChris Lattner // Endian safe: Result[i] = (PointerTy)Dest; 2445a0d4829SChris Lattner EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize), 2455a0d4829SChris Lattner SBytePtr); 2465a0d4829SChris Lattner } 2475a0d4829SChris Lattner 2485a0d4829SChris Lattner // Null terminate it 2495a0d4829SChris Lattner EE->StoreValueToMemory(PTOGV(0), 2505a0d4829SChris Lattner (GenericValue*)(Result+InputArgv.size()*PtrSize), 2515a0d4829SChris Lattner SBytePtr); 2525a0d4829SChris Lattner return Result; 2535a0d4829SChris Lattner } 2545a0d4829SChris Lattner 255faae50b6SChris Lattner 256faae50b6SChris Lattner /// runStaticConstructorsDestructors - This method is used to execute all of 2571a9a0b7bSEvan Cheng /// the static constructors or destructors for a module, depending on the 258faae50b6SChris Lattner /// value of isDtors. 25941fa2bd1SChris Lattner void ExecutionEngine::runStaticConstructorsDestructors(Module *module, 26041fa2bd1SChris Lattner bool isDtors) { 261faae50b6SChris Lattner const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors"; 2620621caefSChris Lattner 2630621caefSChris Lattner // Execute global ctors/dtors for each module in the program. 2641a9a0b7bSEvan Cheng 2651a9a0b7bSEvan Cheng GlobalVariable *GV = module->getNamedGlobal(Name); 266fe36eaebSChris Lattner 267fe36eaebSChris Lattner // If this global has internal linkage, or if it has a use, then it must be 268fe36eaebSChris Lattner // an old-style (llvmgcc3) static ctor with __main linked in and in use. If 2690621caefSChris Lattner // this is the case, don't execute any of the global ctors, __main will do 2700621caefSChris Lattner // it. 2716de96a1bSRafael Espindola if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return; 272faae50b6SChris Lattner 2730621caefSChris Lattner // Should be an array of '{ int, void ()* }' structs. The first value is 2740621caefSChris Lattner // the init priority, which we ignore. 275faae50b6SChris Lattner ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 2761a9a0b7bSEvan Cheng if (!InitList) return; 277faae50b6SChris Lattner for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) 2780621caefSChris Lattner if (ConstantStruct *CS = 2790621caefSChris Lattner dyn_cast<ConstantStruct>(InitList->getOperand(i))) { 2801a9a0b7bSEvan Cheng if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. 281faae50b6SChris Lattner 282faae50b6SChris Lattner Constant *FP = CS->getOperand(1); 283faae50b6SChris Lattner if (FP->isNullValue()) 2840621caefSChris Lattner break; // Found a null terminator, exit. 285faae50b6SChris Lattner 286faae50b6SChris Lattner if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) 2876c38f0bbSReid Spencer if (CE->isCast()) 288faae50b6SChris Lattner FP = CE->getOperand(0); 289faae50b6SChris Lattner if (Function *F = dyn_cast<Function>(FP)) { 290faae50b6SChris Lattner // Execute the ctor/dtor function! 291faae50b6SChris Lattner runFunction(F, std::vector<GenericValue>()); 292faae50b6SChris Lattner } 293faae50b6SChris Lattner } 294faae50b6SChris Lattner } 2951a9a0b7bSEvan Cheng 2961a9a0b7bSEvan Cheng /// runStaticConstructorsDestructors - This method is used to execute all of 2971a9a0b7bSEvan Cheng /// the static constructors or destructors for a program, depending on the 2981a9a0b7bSEvan Cheng /// value of isDtors. 2991a9a0b7bSEvan Cheng void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) { 3001a9a0b7bSEvan Cheng // Execute global ctors/dtors for each module in the program. 3011a9a0b7bSEvan Cheng for (unsigned m = 0, e = Modules.size(); m != e; ++m) 302091217beSJeffrey Yasskin runStaticConstructorsDestructors(Modules[m], isDtors); 3030621caefSChris Lattner } 304faae50b6SChris Lattner 305cf3e3017SDan Gohman #ifndef NDEBUG 3061202d1b1SDuncan Sands /// isTargetNullPtr - Return whether the target pointer stored at Loc is null. 3071202d1b1SDuncan Sands static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) { 3081202d1b1SDuncan Sands unsigned PtrSize = EE->getTargetData()->getPointerSize(); 3091202d1b1SDuncan Sands for (unsigned i = 0; i < PtrSize; ++i) 3101202d1b1SDuncan Sands if (*(i + (uint8_t*)Loc)) 3111202d1b1SDuncan Sands return false; 3121202d1b1SDuncan Sands return true; 3131202d1b1SDuncan Sands } 314cf3e3017SDan Gohman #endif 3151202d1b1SDuncan Sands 3165a0d4829SChris Lattner /// runFunctionAsMain - This is a helper function which wraps runFunction to 3175a0d4829SChris Lattner /// handle the common task of starting up main with the specified argc, argv, 3185a0d4829SChris Lattner /// and envp parameters. 3195a0d4829SChris Lattner int ExecutionEngine::runFunctionAsMain(Function *Fn, 3205a0d4829SChris Lattner const std::vector<std::string> &argv, 3215a0d4829SChris Lattner const char * const * envp) { 3225a0d4829SChris Lattner std::vector<GenericValue> GVArgs; 3235a0d4829SChris Lattner GenericValue GVArgc; 32487aa65f4SReid Spencer GVArgc.IntVal = APInt(32, argv.size()); 3258c32c111SAnton Korobeynikov 3268c32c111SAnton Korobeynikov // Check main() type 327b1cad0b3SChris Lattner unsigned NumArgs = Fn->getFunctionType()->getNumParams(); 3288c32c111SAnton Korobeynikov const FunctionType *FTy = Fn->getFunctionType(); 329ccce8baeSBenjamin Kramer const Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo(); 3308c32c111SAnton Korobeynikov switch (NumArgs) { 3318c32c111SAnton Korobeynikov case 3: 3328c32c111SAnton Korobeynikov if (FTy->getParamType(2) != PPInt8Ty) { 333ccb29cd2STorok Edwin llvm_report_error("Invalid type for third argument of main() supplied"); 3348c32c111SAnton Korobeynikov } 335b781886dSAnton Korobeynikov // FALLS THROUGH 3368c32c111SAnton Korobeynikov case 2: 3378c32c111SAnton Korobeynikov if (FTy->getParamType(1) != PPInt8Ty) { 338ccb29cd2STorok Edwin llvm_report_error("Invalid type for second argument of main() supplied"); 3398c32c111SAnton Korobeynikov } 340b781886dSAnton Korobeynikov // FALLS THROUGH 3418c32c111SAnton Korobeynikov case 1: 342d2564e3aSBenjamin Kramer if (!FTy->getParamType(0)->isInteger(32)) { 343ccb29cd2STorok Edwin llvm_report_error("Invalid type for first argument of main() supplied"); 3448c32c111SAnton Korobeynikov } 345b781886dSAnton Korobeynikov // FALLS THROUGH 3468c32c111SAnton Korobeynikov case 0: 347370ec10dSChris Lattner if (!isa<IntegerType>(FTy->getReturnType()) && 348ccce8baeSBenjamin Kramer !FTy->getReturnType()->isVoidTy()) { 349ccb29cd2STorok Edwin llvm_report_error("Invalid return type of main() supplied"); 3508c32c111SAnton Korobeynikov } 3518c32c111SAnton Korobeynikov break; 3528c32c111SAnton Korobeynikov default: 353ccb29cd2STorok Edwin llvm_report_error("Invalid number of arguments of main() supplied"); 3548c32c111SAnton Korobeynikov } 3558c32c111SAnton Korobeynikov 356b1cad0b3SChris Lattner if (NumArgs) { 3575a0d4829SChris Lattner GVArgs.push_back(GVArgc); // Arg #0 = argc. 358b1cad0b3SChris Lattner if (NumArgs > 1) { 35955f1c09eSOwen Anderson // Arg #1 = argv. 36055f1c09eSOwen Anderson GVArgs.push_back(PTOGV(CreateArgv(Fn->getContext(), this, argv))); 3611202d1b1SDuncan Sands assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) && 362b1cad0b3SChris Lattner "argv[0] was null after CreateArgv"); 363b1cad0b3SChris Lattner if (NumArgs > 2) { 3645a0d4829SChris Lattner std::vector<std::string> EnvVars; 3655a0d4829SChris Lattner for (unsigned i = 0; envp[i]; ++i) 3665a0d4829SChris Lattner EnvVars.push_back(envp[i]); 36755f1c09eSOwen Anderson // Arg #2 = envp. 36855f1c09eSOwen Anderson GVArgs.push_back(PTOGV(CreateArgv(Fn->getContext(), this, EnvVars))); 369b1cad0b3SChris Lattner } 370b1cad0b3SChris Lattner } 371b1cad0b3SChris Lattner } 37287aa65f4SReid Spencer return runFunction(Fn, GVArgs).IntVal.getZExtValue(); 3735a0d4829SChris Lattner } 3745a0d4829SChris Lattner 375260b0c88SMisha Brukman /// If possible, create a JIT, unless the caller specifically requests an 376260b0c88SMisha Brukman /// Interpreter or there's an error. If even an Interpreter cannot be created, 377260b0c88SMisha Brukman /// NULL is returned. 378857c21b4SMisha Brukman /// 379091217beSJeffrey Yasskin ExecutionEngine *ExecutionEngine::create(Module *M, 380603682adSReid Spencer bool ForceInterpreter, 3817ff05bf5SEvan Cheng std::string *ErrorStr, 38270415d97SJeffrey Yasskin CodeGenOpt::Level OptLevel, 38370415d97SJeffrey Yasskin bool GVsWithCode) { 384091217beSJeffrey Yasskin return EngineBuilder(M) 385fc8a2d5aSReid Kleckner .setEngineKind(ForceInterpreter 386fc8a2d5aSReid Kleckner ? EngineKind::Interpreter 387fc8a2d5aSReid Kleckner : EngineKind::JIT) 388fc8a2d5aSReid Kleckner .setErrorStr(ErrorStr) 389fc8a2d5aSReid Kleckner .setOptLevel(OptLevel) 390fc8a2d5aSReid Kleckner .setAllocateGVsWithCode(GVsWithCode) 391fc8a2d5aSReid Kleckner .create(); 392fc8a2d5aSReid Kleckner } 3934bd3bd5bSBrian Gaeke 394fc8a2d5aSReid Kleckner ExecutionEngine *EngineBuilder::create() { 395a53414fdSNick Lewycky // Make sure we can resolve symbols in the program as well. The zero arg 396a53414fdSNick Lewycky // to the function tells DynamicLibrary to load the program, not a library. 397a53414fdSNick Lewycky if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr)) 398a53414fdSNick Lewycky return 0; 399a53414fdSNick Lewycky 400fc8a2d5aSReid Kleckner // If the user specified a memory manager but didn't specify which engine to 401fc8a2d5aSReid Kleckner // create, we assume they only want the JIT, and we fail if they only want 402fc8a2d5aSReid Kleckner // the interpreter. 403fc8a2d5aSReid Kleckner if (JMM) { 40441fa2bd1SChris Lattner if (WhichEngine & EngineKind::JIT) 405fc8a2d5aSReid Kleckner WhichEngine = EngineKind::JIT; 40641fa2bd1SChris Lattner else { 4078bcc6445SChris Lattner if (ErrorStr) 408fc8a2d5aSReid Kleckner *ErrorStr = "Cannot create an interpreter with a memory manager."; 40941fa2bd1SChris Lattner return 0; 410fc8a2d5aSReid Kleckner } 4114bd3bd5bSBrian Gaeke } 4124bd3bd5bSBrian Gaeke 413fc8a2d5aSReid Kleckner // Unless the interpreter was explicitly selected or the JIT is not linked, 414fc8a2d5aSReid Kleckner // try making a JIT. 41541fa2bd1SChris Lattner if (WhichEngine & EngineKind::JIT) { 41641fa2bd1SChris Lattner if (ExecutionEngine::JITCtor) { 41741fa2bd1SChris Lattner ExecutionEngine *EE = 418091217beSJeffrey Yasskin ExecutionEngine::JITCtor(M, ErrorStr, JMM, OptLevel, 419*31faefffSJeffrey Yasskin AllocateGVsWithCode, CMModel, 420*31faefffSJeffrey Yasskin MArch, MCPU, MAttrs); 42141fa2bd1SChris Lattner if (EE) return EE; 42241fa2bd1SChris Lattner } 423fc8a2d5aSReid Kleckner } 424fc8a2d5aSReid Kleckner 425fc8a2d5aSReid Kleckner // If we can't make a JIT and we didn't request one specifically, try making 426fc8a2d5aSReid Kleckner // an interpreter instead. 42741fa2bd1SChris Lattner if (WhichEngine & EngineKind::Interpreter) { 42841fa2bd1SChris Lattner if (ExecutionEngine::InterpCtor) 429091217beSJeffrey Yasskin return ExecutionEngine::InterpCtor(M, ErrorStr); 4308bcc6445SChris Lattner if (ErrorStr) 43141fa2bd1SChris Lattner *ErrorStr = "Interpreter has not been linked in."; 43241fa2bd1SChris Lattner return 0; 433fc8a2d5aSReid Kleckner } 434fc8a2d5aSReid Kleckner 4358bcc6445SChris Lattner if ((WhichEngine & EngineKind::JIT) && ExecutionEngine::JITCtor == 0) { 4368bcc6445SChris Lattner if (ErrorStr) 4378bcc6445SChris Lattner *ErrorStr = "JIT has not been linked in."; 4388bcc6445SChris Lattner } 43941fa2bd1SChris Lattner return 0; 440b5163bb9SChris Lattner } 441b5163bb9SChris Lattner 442857c21b4SMisha Brukman /// getPointerToGlobal - This returns the address of the specified global 443857c21b4SMisha Brukman /// value. This may involve code generation if it's a function. 444857c21b4SMisha Brukman /// 445996fe010SChris Lattner void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { 4461678e859SBrian Gaeke if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) 447996fe010SChris Lattner return getPointerToFunction(F); 448996fe010SChris Lattner 44979876f52SReid Spencer MutexGuard locked(lock); 450d0fc8f80SJeffrey Yasskin void *p = EEState.getGlobalAddressMap(locked)[GV]; 45169e84901SJeff Cohen if (p) 45269e84901SJeff Cohen return p; 45369e84901SJeff Cohen 45469e84901SJeff Cohen // Global variable might have been added since interpreter started. 45569e84901SJeff Cohen if (GlobalVariable *GVar = 45669e84901SJeff Cohen const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV))) 45769e84901SJeff Cohen EmitGlobalVariable(GVar); 45869e84901SJeff Cohen else 459fbcc663cSTorok Edwin llvm_unreachable("Global hasn't had an address allocated yet!"); 460d0fc8f80SJeffrey Yasskin return EEState.getGlobalAddressMap(locked)[GV]; 461996fe010SChris Lattner } 462996fe010SChris Lattner 4636c38f0bbSReid Spencer /// This function converts a Constant* into a GenericValue. The interesting 4646c38f0bbSReid Spencer /// part is if C is a ConstantExpr. 4652dc9f132SReid Spencer /// @brief Get a GenericValue for a Constant* 466996fe010SChris Lattner GenericValue ExecutionEngine::getConstantValue(const Constant *C) { 4676c38f0bbSReid Spencer // If its undefined, return the garbage. 468bcbdbfb3SJay Foad if (isa<UndefValue>(C)) { 469bcbdbfb3SJay Foad GenericValue Result; 470bcbdbfb3SJay Foad switch (C->getType()->getTypeID()) { 471bcbdbfb3SJay Foad case Type::IntegerTyID: 472bcbdbfb3SJay Foad case Type::X86_FP80TyID: 473bcbdbfb3SJay Foad case Type::FP128TyID: 474bcbdbfb3SJay Foad case Type::PPC_FP128TyID: 475bcbdbfb3SJay Foad // Although the value is undefined, we still have to construct an APInt 476bcbdbfb3SJay Foad // with the correct bit width. 477bcbdbfb3SJay Foad Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0); 478bcbdbfb3SJay Foad break; 479bcbdbfb3SJay Foad default: 480bcbdbfb3SJay Foad break; 481bcbdbfb3SJay Foad } 482bcbdbfb3SJay Foad return Result; 483bcbdbfb3SJay Foad } 4849de0d14dSChris Lattner 4856c38f0bbSReid Spencer // If the value is a ConstantExpr 4866c38f0bbSReid Spencer if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 4874fd528f2SReid Spencer Constant *Op0 = CE->getOperand(0); 4889de0d14dSChris Lattner switch (CE->getOpcode()) { 4899de0d14dSChris Lattner case Instruction::GetElementPtr: { 4906c38f0bbSReid Spencer // Compute the index 4914fd528f2SReid Spencer GenericValue Result = getConstantValue(Op0); 492c44bd78aSChris Lattner SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end()); 4939de0d14dSChris Lattner uint64_t Offset = 4944fd528f2SReid Spencer TD->getIndexedOffset(Op0->getType(), &Indices[0], Indices.size()); 4959de0d14dSChris Lattner 49687aa65f4SReid Spencer char* tmp = (char*) Result.PointerVal; 49787aa65f4SReid Spencer Result = PTOGV(tmp + Offset); 4989de0d14dSChris Lattner return Result; 4999de0d14dSChris Lattner } 5004fd528f2SReid Spencer case Instruction::Trunc: { 5014fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5024fd528f2SReid Spencer uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 5034fd528f2SReid Spencer GV.IntVal = GV.IntVal.trunc(BitWidth); 5044fd528f2SReid Spencer return GV; 5054fd528f2SReid Spencer } 5064fd528f2SReid Spencer case Instruction::ZExt: { 5074fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5084fd528f2SReid Spencer uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 5094fd528f2SReid Spencer GV.IntVal = GV.IntVal.zext(BitWidth); 5104fd528f2SReid Spencer return GV; 5114fd528f2SReid Spencer } 5124fd528f2SReid Spencer case Instruction::SExt: { 5134fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5144fd528f2SReid Spencer uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 5154fd528f2SReid Spencer GV.IntVal = GV.IntVal.sext(BitWidth); 5164fd528f2SReid Spencer return GV; 5174fd528f2SReid Spencer } 5184fd528f2SReid Spencer case Instruction::FPTrunc: { 519a1336cf5SDale Johannesen // FIXME long double 5204fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5214fd528f2SReid Spencer GV.FloatVal = float(GV.DoubleVal); 5224fd528f2SReid Spencer return GV; 5234fd528f2SReid Spencer } 5244fd528f2SReid Spencer case Instruction::FPExt:{ 525a1336cf5SDale Johannesen // FIXME long double 5264fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5274fd528f2SReid Spencer GV.DoubleVal = double(GV.FloatVal); 5284fd528f2SReid Spencer return GV; 5294fd528f2SReid Spencer } 5304fd528f2SReid Spencer case Instruction::UIToFP: { 5314fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 532fdd87907SChris Lattner if (CE->getType()->isFloatTy()) 5334fd528f2SReid Spencer GV.FloatVal = float(GV.IntVal.roundToDouble()); 534fdd87907SChris Lattner else if (CE->getType()->isDoubleTy()) 5354fd528f2SReid Spencer GV.DoubleVal = GV.IntVal.roundToDouble(); 536fdd87907SChris Lattner else if (CE->getType()->isX86_FP80Ty()) { 537a1336cf5SDale Johannesen const uint64_t zero[] = {0, 0}; 538a1336cf5SDale Johannesen APFloat apf = APFloat(APInt(80, 2, zero)); 539ca24fd90SDan Gohman (void)apf.convertFromAPInt(GV.IntVal, 540ca24fd90SDan Gohman false, 5419150652bSDale Johannesen APFloat::rmNearestTiesToEven); 54254306fe4SDale Johannesen GV.IntVal = apf.bitcastToAPInt(); 543a1336cf5SDale Johannesen } 5444fd528f2SReid Spencer return GV; 5454fd528f2SReid Spencer } 5464fd528f2SReid Spencer case Instruction::SIToFP: { 5474fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 548fdd87907SChris Lattner if (CE->getType()->isFloatTy()) 5494fd528f2SReid Spencer GV.FloatVal = float(GV.IntVal.signedRoundToDouble()); 550fdd87907SChris Lattner else if (CE->getType()->isDoubleTy()) 5514fd528f2SReid Spencer GV.DoubleVal = GV.IntVal.signedRoundToDouble(); 552fdd87907SChris Lattner else if (CE->getType()->isX86_FP80Ty()) { 553a1336cf5SDale Johannesen const uint64_t zero[] = { 0, 0}; 554a1336cf5SDale Johannesen APFloat apf = APFloat(APInt(80, 2, zero)); 555ca24fd90SDan Gohman (void)apf.convertFromAPInt(GV.IntVal, 556ca24fd90SDan Gohman true, 5579150652bSDale Johannesen APFloat::rmNearestTiesToEven); 55854306fe4SDale Johannesen GV.IntVal = apf.bitcastToAPInt(); 559a1336cf5SDale Johannesen } 5604fd528f2SReid Spencer return GV; 5614fd528f2SReid Spencer } 5624fd528f2SReid Spencer case Instruction::FPToUI: // double->APInt conversion handles sign 5634fd528f2SReid Spencer case Instruction::FPToSI: { 5644fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5654fd528f2SReid Spencer uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 566fdd87907SChris Lattner if (Op0->getType()->isFloatTy()) 5674fd528f2SReid Spencer GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth); 568fdd87907SChris Lattner else if (Op0->getType()->isDoubleTy()) 5694fd528f2SReid Spencer GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth); 570fdd87907SChris Lattner else if (Op0->getType()->isX86_FP80Ty()) { 571a1336cf5SDale Johannesen APFloat apf = APFloat(GV.IntVal); 572a1336cf5SDale Johannesen uint64_t v; 5734f0bd68cSDale Johannesen bool ignored; 574a1336cf5SDale Johannesen (void)apf.convertToInteger(&v, BitWidth, 575a1336cf5SDale Johannesen CE->getOpcode()==Instruction::FPToSI, 5764f0bd68cSDale Johannesen APFloat::rmTowardZero, &ignored); 577a1336cf5SDale Johannesen GV.IntVal = v; // endian? 578a1336cf5SDale Johannesen } 5794fd528f2SReid Spencer return GV; 5804fd528f2SReid Spencer } 5816c38f0bbSReid Spencer case Instruction::PtrToInt: { 5824fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5834fd528f2SReid Spencer uint32_t PtrWidth = TD->getPointerSizeInBits(); 5844fd528f2SReid Spencer GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal)); 5854fd528f2SReid Spencer return GV; 5864fd528f2SReid Spencer } 5874fd528f2SReid Spencer case Instruction::IntToPtr: { 5884fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5894fd528f2SReid Spencer uint32_t PtrWidth = TD->getPointerSizeInBits(); 5904fd528f2SReid Spencer if (PtrWidth != GV.IntVal.getBitWidth()) 5914fd528f2SReid Spencer GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth); 5924fd528f2SReid Spencer assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width"); 5934fd528f2SReid Spencer GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue())); 5946c38f0bbSReid Spencer return GV; 5956c38f0bbSReid Spencer } 5966c38f0bbSReid Spencer case Instruction::BitCast: { 5974fd528f2SReid Spencer GenericValue GV = getConstantValue(Op0); 5984fd528f2SReid Spencer const Type* DestTy = CE->getType(); 5994fd528f2SReid Spencer switch (Op0->getType()->getTypeID()) { 600fbcc663cSTorok Edwin default: llvm_unreachable("Invalid bitcast operand"); 6014fd528f2SReid Spencer case Type::IntegerTyID: 6024fd528f2SReid Spencer assert(DestTy->isFloatingPoint() && "invalid bitcast"); 603fdd87907SChris Lattner if (DestTy->isFloatTy()) 6044fd528f2SReid Spencer GV.FloatVal = GV.IntVal.bitsToFloat(); 605fdd87907SChris Lattner else if (DestTy->isDoubleTy()) 6064fd528f2SReid Spencer GV.DoubleVal = GV.IntVal.bitsToDouble(); 6076c38f0bbSReid Spencer break; 6084fd528f2SReid Spencer case Type::FloatTyID: 609d2564e3aSBenjamin Kramer assert(DestTy->isInteger(32) && "Invalid bitcast"); 6104fd528f2SReid Spencer GV.IntVal.floatToBits(GV.FloatVal); 6114fd528f2SReid Spencer break; 6124fd528f2SReid Spencer case Type::DoubleTyID: 613d2564e3aSBenjamin Kramer assert(DestTy->isInteger(64) && "Invalid bitcast"); 6144fd528f2SReid Spencer GV.IntVal.doubleToBits(GV.DoubleVal); 6154fd528f2SReid Spencer break; 6164fd528f2SReid Spencer case Type::PointerTyID: 6174fd528f2SReid Spencer assert(isa<PointerType>(DestTy) && "Invalid bitcast"); 6184fd528f2SReid Spencer break; // getConstantValue(Op0) above already converted it 6196c38f0bbSReid Spencer } 6204fd528f2SReid Spencer return GV; 62168cbcc3eSChris Lattner } 62268cbcc3eSChris Lattner case Instruction::Add: 623a5b9645cSDan Gohman case Instruction::FAdd: 6244fd528f2SReid Spencer case Instruction::Sub: 625a5b9645cSDan Gohman case Instruction::FSub: 6264fd528f2SReid Spencer case Instruction::Mul: 627a5b9645cSDan Gohman case Instruction::FMul: 6284fd528f2SReid Spencer case Instruction::UDiv: 6294fd528f2SReid Spencer case Instruction::SDiv: 6304fd528f2SReid Spencer case Instruction::URem: 6314fd528f2SReid Spencer case Instruction::SRem: 6324fd528f2SReid Spencer case Instruction::And: 6334fd528f2SReid Spencer case Instruction::Or: 6344fd528f2SReid Spencer case Instruction::Xor: { 6354fd528f2SReid Spencer GenericValue LHS = getConstantValue(Op0); 6364fd528f2SReid Spencer GenericValue RHS = getConstantValue(CE->getOperand(1)); 6374fd528f2SReid Spencer GenericValue GV; 638c4e6bb5fSChris Lattner switch (CE->getOperand(0)->getType()->getTypeID()) { 639fbcc663cSTorok Edwin default: llvm_unreachable("Bad add type!"); 6407a9c62baSReid Spencer case Type::IntegerTyID: 6414fd528f2SReid Spencer switch (CE->getOpcode()) { 642fbcc663cSTorok Edwin default: llvm_unreachable("Invalid integer opcode"); 6434fd528f2SReid Spencer case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break; 6444fd528f2SReid Spencer case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break; 6454fd528f2SReid Spencer case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break; 6464fd528f2SReid Spencer case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break; 6474fd528f2SReid Spencer case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break; 6484fd528f2SReid Spencer case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break; 6494fd528f2SReid Spencer case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break; 6504fd528f2SReid Spencer case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break; 6514fd528f2SReid Spencer case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break; 6524fd528f2SReid Spencer case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break; 6534fd528f2SReid Spencer } 654c4e6bb5fSChris Lattner break; 655c4e6bb5fSChris Lattner case Type::FloatTyID: 6564fd528f2SReid Spencer switch (CE->getOpcode()) { 657fbcc663cSTorok Edwin default: llvm_unreachable("Invalid float opcode"); 658a5b9645cSDan Gohman case Instruction::FAdd: 6594fd528f2SReid Spencer GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break; 660a5b9645cSDan Gohman case Instruction::FSub: 6614fd528f2SReid Spencer GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break; 662a5b9645cSDan Gohman case Instruction::FMul: 6634fd528f2SReid Spencer GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break; 6644fd528f2SReid Spencer case Instruction::FDiv: 6654fd528f2SReid Spencer GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break; 6664fd528f2SReid Spencer case Instruction::FRem: 6674fd528f2SReid Spencer GV.FloatVal = ::fmodf(LHS.FloatVal,RHS.FloatVal); break; 6684fd528f2SReid Spencer } 669c4e6bb5fSChris Lattner break; 670c4e6bb5fSChris Lattner case Type::DoubleTyID: 6714fd528f2SReid Spencer switch (CE->getOpcode()) { 672fbcc663cSTorok Edwin default: llvm_unreachable("Invalid double opcode"); 673a5b9645cSDan Gohman case Instruction::FAdd: 6744fd528f2SReid Spencer GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break; 675a5b9645cSDan Gohman case Instruction::FSub: 6764fd528f2SReid Spencer GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break; 677a5b9645cSDan Gohman case Instruction::FMul: 6784fd528f2SReid Spencer GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break; 6794fd528f2SReid Spencer case Instruction::FDiv: 6804fd528f2SReid Spencer GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break; 6814fd528f2SReid Spencer case Instruction::FRem: 6824fd528f2SReid Spencer GV.DoubleVal = ::fmod(LHS.DoubleVal,RHS.DoubleVal); break; 6834fd528f2SReid Spencer } 684c4e6bb5fSChris Lattner break; 685a1336cf5SDale Johannesen case Type::X86_FP80TyID: 686a1336cf5SDale Johannesen case Type::PPC_FP128TyID: 687a1336cf5SDale Johannesen case Type::FP128TyID: { 688a1336cf5SDale Johannesen APFloat apfLHS = APFloat(LHS.IntVal); 689a1336cf5SDale Johannesen switch (CE->getOpcode()) { 690fbcc663cSTorok Edwin default: llvm_unreachable("Invalid long double opcode");llvm_unreachable(0); 691a5b9645cSDan Gohman case Instruction::FAdd: 692a1336cf5SDale Johannesen apfLHS.add(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 69354306fe4SDale Johannesen GV.IntVal = apfLHS.bitcastToAPInt(); 694a1336cf5SDale Johannesen break; 695a5b9645cSDan Gohman case Instruction::FSub: 696a1336cf5SDale Johannesen apfLHS.subtract(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 69754306fe4SDale Johannesen GV.IntVal = apfLHS.bitcastToAPInt(); 698a1336cf5SDale Johannesen break; 699a5b9645cSDan Gohman case Instruction::FMul: 700a1336cf5SDale Johannesen apfLHS.multiply(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 70154306fe4SDale Johannesen GV.IntVal = apfLHS.bitcastToAPInt(); 702a1336cf5SDale Johannesen break; 703a1336cf5SDale Johannesen case Instruction::FDiv: 704a1336cf5SDale Johannesen apfLHS.divide(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 70554306fe4SDale Johannesen GV.IntVal = apfLHS.bitcastToAPInt(); 706a1336cf5SDale Johannesen break; 707a1336cf5SDale Johannesen case Instruction::FRem: 708a1336cf5SDale Johannesen apfLHS.mod(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 70954306fe4SDale Johannesen GV.IntVal = apfLHS.bitcastToAPInt(); 710a1336cf5SDale Johannesen break; 711a1336cf5SDale Johannesen } 712a1336cf5SDale Johannesen } 713a1336cf5SDale Johannesen break; 714c4e6bb5fSChris Lattner } 7154fd528f2SReid Spencer return GV; 7164fd528f2SReid Spencer } 7179de0d14dSChris Lattner default: 71868cbcc3eSChris Lattner break; 71968cbcc3eSChris Lattner } 720ccb29cd2STorok Edwin std::string msg; 721ccb29cd2STorok Edwin raw_string_ostream Msg(msg); 722ccb29cd2STorok Edwin Msg << "ConstantExpr not handled: " << *CE; 723ccb29cd2STorok Edwin llvm_report_error(Msg.str()); 7249de0d14dSChris Lattner } 725996fe010SChris Lattner 7264fd528f2SReid Spencer GenericValue Result; 7276b727599SChris Lattner switch (C->getType()->getTypeID()) { 72887aa65f4SReid Spencer case Type::FloatTyID: 729bed9dc42SDale Johannesen Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat(); 7307a9c62baSReid Spencer break; 73187aa65f4SReid Spencer case Type::DoubleTyID: 732bed9dc42SDale Johannesen Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble(); 73387aa65f4SReid Spencer break; 734a1336cf5SDale Johannesen case Type::X86_FP80TyID: 735a1336cf5SDale Johannesen case Type::FP128TyID: 736a1336cf5SDale Johannesen case Type::PPC_FP128TyID: 73754306fe4SDale Johannesen Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt(); 738a1336cf5SDale Johannesen break; 73987aa65f4SReid Spencer case Type::IntegerTyID: 74087aa65f4SReid Spencer Result.IntVal = cast<ConstantInt>(C)->getValue(); 74187aa65f4SReid Spencer break; 742996fe010SChris Lattner case Type::PointerTyID: 7436a0fd73bSReid Spencer if (isa<ConstantPointerNull>(C)) 744996fe010SChris Lattner Result.PointerVal = 0; 7456a0fd73bSReid Spencer else if (const Function *F = dyn_cast<Function>(C)) 7466a0fd73bSReid Spencer Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F))); 7476a0fd73bSReid Spencer else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) 7486a0fd73bSReid Spencer Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV))); 7490c778f70SChris Lattner else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) 7500c778f70SChris Lattner Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>( 7510c778f70SChris Lattner BA->getBasicBlock()))); 752e6492f10SChris Lattner else 753fbcc663cSTorok Edwin llvm_unreachable("Unknown constant pointer type!"); 754996fe010SChris Lattner break; 755996fe010SChris Lattner default: 756ccb29cd2STorok Edwin std::string msg; 757ccb29cd2STorok Edwin raw_string_ostream Msg(msg); 758ccb29cd2STorok Edwin Msg << "ERROR: Constant unimplemented for type: " << *C->getType(); 759ccb29cd2STorok Edwin llvm_report_error(Msg.str()); 760996fe010SChris Lattner } 761996fe010SChris Lattner return Result; 762996fe010SChris Lattner } 763996fe010SChris Lattner 7641202d1b1SDuncan Sands /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst 7651202d1b1SDuncan Sands /// with the integer held in IntVal. 7661202d1b1SDuncan Sands static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, 7671202d1b1SDuncan Sands unsigned StoreBytes) { 7681202d1b1SDuncan Sands assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!"); 7691202d1b1SDuncan Sands uint8_t *Src = (uint8_t *)IntVal.getRawData(); 7705c65cb46SDuncan Sands 771aa121227SChris Lattner if (sys::isLittleEndianHost()) 7721202d1b1SDuncan Sands // Little-endian host - the source is ordered from LSB to MSB. Order the 7731202d1b1SDuncan Sands // destination from LSB to MSB: Do a straight copy. 7745c65cb46SDuncan Sands memcpy(Dst, Src, StoreBytes); 7755c65cb46SDuncan Sands else { 7765c65cb46SDuncan Sands // Big-endian host - the source is an array of 64 bit words ordered from 7771202d1b1SDuncan Sands // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination 7781202d1b1SDuncan Sands // from MSB to LSB: Reverse the word order, but not the bytes in a word. 7795c65cb46SDuncan Sands while (StoreBytes > sizeof(uint64_t)) { 7805c65cb46SDuncan Sands StoreBytes -= sizeof(uint64_t); 7815c65cb46SDuncan Sands // May not be aligned so use memcpy. 7825c65cb46SDuncan Sands memcpy(Dst + StoreBytes, Src, sizeof(uint64_t)); 7835c65cb46SDuncan Sands Src += sizeof(uint64_t); 7845c65cb46SDuncan Sands } 7855c65cb46SDuncan Sands 7865c65cb46SDuncan Sands memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes); 787815f8dd2SReid Spencer } 7887a9c62baSReid Spencer } 7891202d1b1SDuncan Sands 7901202d1b1SDuncan Sands /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr 7911202d1b1SDuncan Sands /// is the address of the memory at which to store Val, cast to GenericValue *. 7921202d1b1SDuncan Sands /// It is not a pointer to a GenericValue containing the address at which to 7931202d1b1SDuncan Sands /// store Val. 79409053e62SEvan Cheng void ExecutionEngine::StoreValueToMemory(const GenericValue &Val, 79509053e62SEvan Cheng GenericValue *Ptr, const Type *Ty) { 7961202d1b1SDuncan Sands const unsigned StoreBytes = getTargetData()->getTypeStoreSize(Ty); 7971202d1b1SDuncan Sands 7981202d1b1SDuncan Sands switch (Ty->getTypeID()) { 7991202d1b1SDuncan Sands case Type::IntegerTyID: 8001202d1b1SDuncan Sands StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes); 8011202d1b1SDuncan Sands break; 802996fe010SChris Lattner case Type::FloatTyID: 80387aa65f4SReid Spencer *((float*)Ptr) = Val.FloatVal; 80487aa65f4SReid Spencer break; 80587aa65f4SReid Spencer case Type::DoubleTyID: 80687aa65f4SReid Spencer *((double*)Ptr) = Val.DoubleVal; 807996fe010SChris Lattner break; 8084d7e4ee7SDale Johannesen case Type::X86_FP80TyID: 8094d7e4ee7SDale Johannesen memcpy(Ptr, Val.IntVal.getRawData(), 10); 810a1336cf5SDale Johannesen break; 8117a9c62baSReid Spencer case Type::PointerTyID: 8121202d1b1SDuncan Sands // Ensure 64 bit target pointers are fully initialized on 32 bit hosts. 8131202d1b1SDuncan Sands if (StoreBytes != sizeof(PointerTy)) 8141202d1b1SDuncan Sands memset(Ptr, 0, StoreBytes); 8151202d1b1SDuncan Sands 81687aa65f4SReid Spencer *((PointerTy*)Ptr) = Val.PointerVal; 817996fe010SChris Lattner break; 818996fe010SChris Lattner default: 8190967d2dfSDavid Greene dbgs() << "Cannot store value of type " << *Ty << "!\n"; 820996fe010SChris Lattner } 8211202d1b1SDuncan Sands 822aa121227SChris Lattner if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian()) 8231202d1b1SDuncan Sands // Host and target are different endian - reverse the stored bytes. 8241202d1b1SDuncan Sands std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr); 825996fe010SChris Lattner } 826996fe010SChris Lattner 8271202d1b1SDuncan Sands /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting 8281202d1b1SDuncan Sands /// from Src into IntVal, which is assumed to be wide enough and to hold zero. 8291202d1b1SDuncan Sands static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) { 8301202d1b1SDuncan Sands assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!"); 8311202d1b1SDuncan Sands uint8_t *Dst = (uint8_t *)IntVal.getRawData(); 8325c65cb46SDuncan Sands 833aa121227SChris Lattner if (sys::isLittleEndianHost()) 8345c65cb46SDuncan Sands // Little-endian host - the destination must be ordered from LSB to MSB. 8355c65cb46SDuncan Sands // The source is ordered from LSB to MSB: Do a straight copy. 8365c65cb46SDuncan Sands memcpy(Dst, Src, LoadBytes); 8375c65cb46SDuncan Sands else { 8385c65cb46SDuncan Sands // Big-endian - the destination is an array of 64 bit words ordered from 8395c65cb46SDuncan Sands // LSW to MSW. Each word must be ordered from MSB to LSB. The source is 8405c65cb46SDuncan Sands // ordered from MSB to LSB: Reverse the word order, but not the bytes in 8415c65cb46SDuncan Sands // a word. 8425c65cb46SDuncan Sands while (LoadBytes > sizeof(uint64_t)) { 8435c65cb46SDuncan Sands LoadBytes -= sizeof(uint64_t); 8445c65cb46SDuncan Sands // May not be aligned so use memcpy. 8455c65cb46SDuncan Sands memcpy(Dst, Src + LoadBytes, sizeof(uint64_t)); 8465c65cb46SDuncan Sands Dst += sizeof(uint64_t); 8475c65cb46SDuncan Sands } 8485c65cb46SDuncan Sands 8495c65cb46SDuncan Sands memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes); 8505c65cb46SDuncan Sands } 8517a9c62baSReid Spencer } 8521202d1b1SDuncan Sands 8531202d1b1SDuncan Sands /// FIXME: document 8541202d1b1SDuncan Sands /// 8551202d1b1SDuncan Sands void ExecutionEngine::LoadValueFromMemory(GenericValue &Result, 8561202d1b1SDuncan Sands GenericValue *Ptr, 8571202d1b1SDuncan Sands const Type *Ty) { 8581202d1b1SDuncan Sands const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty); 8591202d1b1SDuncan Sands 8601202d1b1SDuncan Sands switch (Ty->getTypeID()) { 8611202d1b1SDuncan Sands case Type::IntegerTyID: 8621202d1b1SDuncan Sands // An APInt with all words initially zero. 8631202d1b1SDuncan Sands Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0); 8641202d1b1SDuncan Sands LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes); 8651202d1b1SDuncan Sands break; 8667f389e8cSChris Lattner case Type::FloatTyID: 86787aa65f4SReid Spencer Result.FloatVal = *((float*)Ptr); 86887aa65f4SReid Spencer break; 86987aa65f4SReid Spencer case Type::DoubleTyID: 87087aa65f4SReid Spencer Result.DoubleVal = *((double*)Ptr); 8717f389e8cSChris Lattner break; 8727a9c62baSReid Spencer case Type::PointerTyID: 87387aa65f4SReid Spencer Result.PointerVal = *((PointerTy*)Ptr); 8747f389e8cSChris Lattner break; 875a1336cf5SDale Johannesen case Type::X86_FP80TyID: { 876a1336cf5SDale Johannesen // This is endian dependent, but it will only work on x86 anyway. 87726d6539eSDuncan Sands // FIXME: Will not trap if loading a signaling NaN. 878ff306287SDuncan Sands uint64_t y[2]; 8794d7e4ee7SDale Johannesen memcpy(y, Ptr, 10); 880ff306287SDuncan Sands Result.IntVal = APInt(80, 2, y); 881a1336cf5SDale Johannesen break; 882a1336cf5SDale Johannesen } 8837f389e8cSChris Lattner default: 884ccb29cd2STorok Edwin std::string msg; 885ccb29cd2STorok Edwin raw_string_ostream Msg(msg); 886ccb29cd2STorok Edwin Msg << "Cannot load value of type " << *Ty << "!"; 887ccb29cd2STorok Edwin llvm_report_error(Msg.str()); 8887f389e8cSChris Lattner } 8897f389e8cSChris Lattner } 8907f389e8cSChris Lattner 891996fe010SChris Lattner // InitializeMemory - Recursive function to apply a Constant value into the 892996fe010SChris Lattner // specified memory location... 893996fe010SChris Lattner // 894996fe010SChris Lattner void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { 8950967d2dfSDavid Greene DEBUG(dbgs() << "JIT: Initializing " << Addr << " "); 896b086d382SDale Johannesen DEBUG(Init->dump()); 89761753bf8SChris Lattner if (isa<UndefValue>(Init)) { 89861753bf8SChris Lattner return; 899d84d35baSReid Spencer } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) { 90069d62138SRobert Bocchino unsigned ElementSize = 901af9eaa83SDuncan Sands getTargetData()->getTypeAllocSize(CP->getType()->getElementType()); 90269d62138SRobert Bocchino for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) 90369d62138SRobert Bocchino InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize); 90469d62138SRobert Bocchino return; 9051dd86b11SChris Lattner } else if (isa<ConstantAggregateZero>(Init)) { 906af9eaa83SDuncan Sands memset(Addr, 0, (size_t)getTargetData()->getTypeAllocSize(Init->getType())); 9071dd86b11SChris Lattner return; 90869ddfbfeSDan Gohman } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) { 90969ddfbfeSDan Gohman unsigned ElementSize = 910af9eaa83SDuncan Sands getTargetData()->getTypeAllocSize(CPA->getType()->getElementType()); 91169ddfbfeSDan Gohman for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) 91269ddfbfeSDan Gohman InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); 91369ddfbfeSDan Gohman return; 91469ddfbfeSDan Gohman } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) { 91569ddfbfeSDan Gohman const StructLayout *SL = 91669ddfbfeSDan Gohman getTargetData()->getStructLayout(cast<StructType>(CPS->getType())); 91769ddfbfeSDan Gohman for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) 91869ddfbfeSDan Gohman InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i)); 91969ddfbfeSDan Gohman return; 92061753bf8SChris Lattner } else if (Init->getType()->isFirstClassType()) { 921996fe010SChris Lattner GenericValue Val = getConstantValue(Init); 922996fe010SChris Lattner StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); 923996fe010SChris Lattner return; 924996fe010SChris Lattner } 925996fe010SChris Lattner 9260967d2dfSDavid Greene dbgs() << "Bad Type: " << *Init->getType() << "\n"; 927fbcc663cSTorok Edwin llvm_unreachable("Unknown constant type to initialize memory with!"); 928996fe010SChris Lattner } 929996fe010SChris Lattner 930996fe010SChris Lattner /// EmitGlobals - Emit all of the global variables to memory, storing their 931996fe010SChris Lattner /// addresses into GlobalAddress. This must make sure to copy the contents of 932996fe010SChris Lattner /// their initializers into the memory. 933996fe010SChris Lattner /// 934996fe010SChris Lattner void ExecutionEngine::emitGlobals() { 935996fe010SChris Lattner 936996fe010SChris Lattner // Loop over all of the global variables in the program, allocating the memory 9370621caefSChris Lattner // to hold them. If there is more than one module, do a prepass over globals 9380621caefSChris Lattner // to figure out how the different modules should link together. 9390621caefSChris Lattner // 9400621caefSChris Lattner std::map<std::pair<std::string, const Type*>, 9410621caefSChris Lattner const GlobalValue*> LinkedGlobalsMap; 9420621caefSChris Lattner 9430621caefSChris Lattner if (Modules.size() != 1) { 9440621caefSChris Lattner for (unsigned m = 0, e = Modules.size(); m != e; ++m) { 945091217beSJeffrey Yasskin Module &M = *Modules[m]; 9460621caefSChris Lattner for (Module::const_global_iterator I = M.global_begin(), 9470621caefSChris Lattner E = M.global_end(); I != E; ++I) { 9480621caefSChris Lattner const GlobalValue *GV = I; 9496de96a1bSRafael Espindola if (GV->hasLocalLinkage() || GV->isDeclaration() || 9500621caefSChris Lattner GV->hasAppendingLinkage() || !GV->hasName()) 9510621caefSChris Lattner continue;// Ignore external globals and globals with internal linkage. 9520621caefSChris Lattner 9530621caefSChris Lattner const GlobalValue *&GVEntry = 9540621caefSChris Lattner LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; 9550621caefSChris Lattner 9560621caefSChris Lattner // If this is the first time we've seen this global, it is the canonical 9570621caefSChris Lattner // version. 9580621caefSChris Lattner if (!GVEntry) { 9590621caefSChris Lattner GVEntry = GV; 9600621caefSChris Lattner continue; 9610621caefSChris Lattner } 9620621caefSChris Lattner 9630621caefSChris Lattner // If the existing global is strong, never replace it. 964d61d39ecSAnton Korobeynikov if (GVEntry->hasExternalLinkage() || 965d61d39ecSAnton Korobeynikov GVEntry->hasDLLImportLinkage() || 966d61d39ecSAnton Korobeynikov GVEntry->hasDLLExportLinkage()) 9670621caefSChris Lattner continue; 9680621caefSChris Lattner 9690621caefSChris Lattner // Otherwise, we know it's linkonce/weak, replace it if this is a strong 970ce4396bcSDale Johannesen // symbol. FIXME is this right for common? 97112c94949SAnton Korobeynikov if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage()) 9720621caefSChris Lattner GVEntry = GV; 9730621caefSChris Lattner } 9740621caefSChris Lattner } 9750621caefSChris Lattner } 9760621caefSChris Lattner 9770621caefSChris Lattner std::vector<const GlobalValue*> NonCanonicalGlobals; 9780621caefSChris Lattner for (unsigned m = 0, e = Modules.size(); m != e; ++m) { 979091217beSJeffrey Yasskin Module &M = *Modules[m]; 9808ffb6611SChris Lattner for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 9810621caefSChris Lattner I != E; ++I) { 9820621caefSChris Lattner // In the multi-module case, see what this global maps to. 9830621caefSChris Lattner if (!LinkedGlobalsMap.empty()) { 9840621caefSChris Lattner if (const GlobalValue *GVEntry = 9850621caefSChris Lattner LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) { 9860621caefSChris Lattner // If something else is the canonical global, ignore this one. 9870621caefSChris Lattner if (GVEntry != &*I) { 9880621caefSChris Lattner NonCanonicalGlobals.push_back(I); 9890621caefSChris Lattner continue; 9900621caefSChris Lattner } 9910621caefSChris Lattner } 9920621caefSChris Lattner } 9930621caefSChris Lattner 9945301e7c6SReid Spencer if (!I->isDeclaration()) { 9955457ce9aSNicolas Geoffray addGlobalMapping(I, getMemoryForGV(I)); 996996fe010SChris Lattner } else { 997e8bbcfc2SBrian Gaeke // External variable reference. Try to use the dynamic loader to 998e8bbcfc2SBrian Gaeke // get a pointer to it. 9990621caefSChris Lattner if (void *SymAddr = 10005899e340SDaniel Dunbar sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName())) 1001748e8579SChris Lattner addGlobalMapping(I, SymAddr); 10029de0d14dSChris Lattner else { 10036c2d233eSTorok Edwin llvm_report_error("Could not resolve external global address: " 10046c2d233eSTorok Edwin +I->getName()); 10059de0d14dSChris Lattner } 1006996fe010SChris Lattner } 10070621caefSChris Lattner } 10080621caefSChris Lattner 10090621caefSChris Lattner // If there are multiple modules, map the non-canonical globals to their 10100621caefSChris Lattner // canonical location. 10110621caefSChris Lattner if (!NonCanonicalGlobals.empty()) { 10120621caefSChris Lattner for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) { 10130621caefSChris Lattner const GlobalValue *GV = NonCanonicalGlobals[i]; 10140621caefSChris Lattner const GlobalValue *CGV = 10150621caefSChris Lattner LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; 10160621caefSChris Lattner void *Ptr = getPointerToGlobalIfAvailable(CGV); 10170621caefSChris Lattner assert(Ptr && "Canonical global wasn't codegen'd!"); 1018a67f06b9SNuno Lopes addGlobalMapping(GV, Ptr); 10190621caefSChris Lattner } 10200621caefSChris Lattner } 1021996fe010SChris Lattner 10227a9c62baSReid Spencer // Now that all of the globals are set up in memory, loop through them all 10237a9c62baSReid Spencer // and initialize their contents. 10248ffb6611SChris Lattner for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 10250621caefSChris Lattner I != E; ++I) { 10265301e7c6SReid Spencer if (!I->isDeclaration()) { 10270621caefSChris Lattner if (!LinkedGlobalsMap.empty()) { 10280621caefSChris Lattner if (const GlobalValue *GVEntry = 10290621caefSChris Lattner LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) 10300621caefSChris Lattner if (GVEntry != &*I) // Not the canonical variable. 10310621caefSChris Lattner continue; 10320621caefSChris Lattner } 10336bbe3eceSChris Lattner EmitGlobalVariable(I); 10346bbe3eceSChris Lattner } 10350621caefSChris Lattner } 10360621caefSChris Lattner } 10370621caefSChris Lattner } 10386bbe3eceSChris Lattner 10396bbe3eceSChris Lattner // EmitGlobalVariable - This method emits the specified global variable to the 10406bbe3eceSChris Lattner // address specified in GlobalAddresses, or allocates new memory if it's not 10416bbe3eceSChris Lattner // already in the map. 1042fbcc0aa1SChris Lattner void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { 1043748e8579SChris Lattner void *GA = getPointerToGlobalIfAvailable(GV); 1044dc631735SChris Lattner 10456bbe3eceSChris Lattner if (GA == 0) { 10466bbe3eceSChris Lattner // If it's not already specified, allocate memory for the global. 10475457ce9aSNicolas Geoffray GA = getMemoryForGV(GV); 1048748e8579SChris Lattner addGlobalMapping(GV, GA); 10496bbe3eceSChris Lattner } 1050fbcc0aa1SChris Lattner 10515457ce9aSNicolas Geoffray // Don't initialize if it's thread local, let the client do it. 10525457ce9aSNicolas Geoffray if (!GV->isThreadLocal()) 10536bbe3eceSChris Lattner InitializeMemory(GV->getInitializer(), GA); 10545457ce9aSNicolas Geoffray 10555457ce9aSNicolas Geoffray const Type *ElTy = GV->getType()->getElementType(); 1056af9eaa83SDuncan Sands size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy); 1057df1f1524SChris Lattner NumInitBytes += (unsigned)GVSize; 10586bbe3eceSChris Lattner ++NumGlobals; 1059996fe010SChris Lattner } 1060f98e981cSJeffrey Yasskin 1061d0fc8f80SJeffrey Yasskin ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE) 1062d0fc8f80SJeffrey Yasskin : EE(EE), GlobalAddressMap(this) { 1063f98e981cSJeffrey Yasskin } 1064f98e981cSJeffrey Yasskin 1065d0fc8f80SJeffrey Yasskin sys::Mutex *ExecutionEngineState::AddressMapConfig::getMutex( 1066d0fc8f80SJeffrey Yasskin ExecutionEngineState *EES) { 1067d0fc8f80SJeffrey Yasskin return &EES->EE.lock; 1068d0fc8f80SJeffrey Yasskin } 1069d0fc8f80SJeffrey Yasskin void ExecutionEngineState::AddressMapConfig::onDelete( 1070d0fc8f80SJeffrey Yasskin ExecutionEngineState *EES, const GlobalValue *Old) { 1071d0fc8f80SJeffrey Yasskin void *OldVal = EES->GlobalAddressMap.lookup(Old); 1072d0fc8f80SJeffrey Yasskin EES->GlobalAddressReverseMap.erase(OldVal); 1073d0fc8f80SJeffrey Yasskin } 1074d0fc8f80SJeffrey Yasskin 1075d0fc8f80SJeffrey Yasskin void ExecutionEngineState::AddressMapConfig::onRAUW( 1076d0fc8f80SJeffrey Yasskin ExecutionEngineState *, const GlobalValue *, const GlobalValue *) { 1077f98e981cSJeffrey Yasskin assert(false && "The ExecutionEngine doesn't know how to handle a" 1078f98e981cSJeffrey Yasskin " RAUW on a value it has a global mapping for."); 1079f98e981cSJeffrey Yasskin } 1080