1857c21b4SMisha Brukman //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===// 2996fe010SChris Lattner // 3482202a6SJohn Criswell // The LLVM Compiler Infrastructure 4482202a6SJohn Criswell // 5482202a6SJohn Criswell // This file was developed by the LLVM research group and is distributed under 6482202a6SJohn Criswell // the University of Illinois Open Source 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" 21260b0c88SMisha Brukman #include "llvm/ExecutionEngine/ExecutionEngine.h" 22ad481312SChris Lattner #include "llvm/ExecutionEngine/GenericValue.h" 237c16caa3SReid Spencer #include "llvm/Support/Debug.h" 246d8dd189SChris Lattner #include "llvm/Support/MutexGuard.h" 2570e37278SReid Spencer #include "llvm/System/DynamicLibrary.h" 2670e37278SReid Spencer #include "llvm/Target/TargetData.h" 2729681deeSChris Lattner using namespace llvm; 28996fe010SChris Lattner 2929681deeSChris Lattner namespace { 30996fe010SChris Lattner Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized"); 316bbe3eceSChris Lattner Statistic<> NumGlobals ("lli", "Number of global vars initialized"); 3229681deeSChris Lattner } 33996fe010SChris Lattner 342d52c1b8SChris Lattner ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0; 352d52c1b8SChris Lattner ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0; 362d52c1b8SChris Lattner 370621caefSChris Lattner ExecutionEngine::ExecutionEngine(ModuleProvider *P) { 3887aee74cSChris Lattner LazyCompilationDisabled = false; 390621caefSChris Lattner Modules.push_back(P); 40260b0c88SMisha Brukman assert(P && "ModuleProvider is null?"); 41260b0c88SMisha Brukman } 42260b0c88SMisha Brukman 430621caefSChris Lattner ExecutionEngine::ExecutionEngine(Module *M) { 4487aee74cSChris Lattner LazyCompilationDisabled = false; 45260b0c88SMisha Brukman assert(M && "Module is null?"); 460621caefSChris Lattner Modules.push_back(new ExistingModuleProvider(M)); 47260b0c88SMisha Brukman } 48260b0c88SMisha Brukman 4992f8b30dSBrian Gaeke ExecutionEngine::~ExecutionEngine() { 500621caefSChris Lattner for (unsigned i = 0, e = Modules.size(); i != e; ++i) 510621caefSChris Lattner delete Modules[i]; 5292f8b30dSBrian Gaeke } 5392f8b30dSBrian Gaeke 540621caefSChris Lattner /// FindFunctionNamed - Search all of the active modules to find the one that 550621caefSChris Lattner /// defines FnName. This is very slow operation and shouldn't be used for 560621caefSChris Lattner /// general code. 570621caefSChris Lattner Function *ExecutionEngine::FindFunctionNamed(const char *FnName) { 580621caefSChris Lattner for (unsigned i = 0, e = Modules.size(); i != e; ++i) { 590621caefSChris Lattner if (Function *F = Modules[i]->getModule()->getNamedFunction(FnName)) 600621caefSChris Lattner return F; 610621caefSChris Lattner } 620621caefSChris Lattner return 0; 630621caefSChris Lattner } 640621caefSChris Lattner 650621caefSChris Lattner 666d8dd189SChris Lattner /// addGlobalMapping - Tell the execution engine that the specified global is 676d8dd189SChris Lattner /// at the specified location. This is used internally as functions are JIT'd 686d8dd189SChris Lattner /// and as global variables are laid out in memory. It can and should also be 696d8dd189SChris Lattner /// used by clients of the EE that want to have an LLVM global overlay 706d8dd189SChris Lattner /// existing data in memory. 716d8dd189SChris Lattner void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) { 726d8dd189SChris Lattner MutexGuard locked(lock); 736d8dd189SChris Lattner 746d8dd189SChris Lattner void *&CurVal = state.getGlobalAddressMap(locked)[GV]; 756d8dd189SChris Lattner assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!"); 766d8dd189SChris Lattner CurVal = Addr; 776d8dd189SChris Lattner 786d8dd189SChris Lattner // If we are using the reverse mapping, add it too 796d8dd189SChris Lattner if (!state.getGlobalAddressReverseMap(locked).empty()) { 806d8dd189SChris Lattner const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr]; 816d8dd189SChris Lattner assert((V == 0 || GV == 0) && "GlobalMapping already established!"); 826d8dd189SChris Lattner V = GV; 836d8dd189SChris Lattner } 846d8dd189SChris Lattner } 856d8dd189SChris Lattner 866d8dd189SChris Lattner /// clearAllGlobalMappings - Clear all global mappings and start over again 876d8dd189SChris Lattner /// use in dynamic compilation scenarios when you want to move globals 886d8dd189SChris Lattner void ExecutionEngine::clearAllGlobalMappings() { 896d8dd189SChris Lattner MutexGuard locked(lock); 906d8dd189SChris Lattner 916d8dd189SChris Lattner state.getGlobalAddressMap(locked).clear(); 926d8dd189SChris Lattner state.getGlobalAddressReverseMap(locked).clear(); 936d8dd189SChris Lattner } 946d8dd189SChris Lattner 956d8dd189SChris Lattner /// updateGlobalMapping - Replace an existing mapping for GV with a new 966d8dd189SChris Lattner /// address. This updates both maps as required. If "Addr" is null, the 976d8dd189SChris Lattner /// entry for the global is removed from the mappings. 986d8dd189SChris Lattner void ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) { 996d8dd189SChris Lattner MutexGuard locked(lock); 1006d8dd189SChris Lattner 1016d8dd189SChris Lattner // Deleting from the mapping? 1026d8dd189SChris Lattner if (Addr == 0) { 1036d8dd189SChris Lattner state.getGlobalAddressMap(locked).erase(GV); 1046d8dd189SChris Lattner if (!state.getGlobalAddressReverseMap(locked).empty()) 1056d8dd189SChris Lattner state.getGlobalAddressReverseMap(locked).erase(Addr); 1066d8dd189SChris Lattner return; 1076d8dd189SChris Lattner } 1086d8dd189SChris Lattner 1096d8dd189SChris Lattner void *&CurVal = state.getGlobalAddressMap(locked)[GV]; 1106d8dd189SChris Lattner if (CurVal && !state.getGlobalAddressReverseMap(locked).empty()) 1116d8dd189SChris Lattner state.getGlobalAddressReverseMap(locked).erase(CurVal); 1126d8dd189SChris Lattner CurVal = Addr; 1136d8dd189SChris Lattner 1146d8dd189SChris Lattner // If we are using the reverse mapping, add it too 1156d8dd189SChris Lattner if (!state.getGlobalAddressReverseMap(locked).empty()) { 1166d8dd189SChris Lattner const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr]; 1176d8dd189SChris Lattner assert((V == 0 || GV == 0) && "GlobalMapping already established!"); 1186d8dd189SChris Lattner V = GV; 1196d8dd189SChris Lattner } 1206d8dd189SChris Lattner } 1216d8dd189SChris Lattner 1226d8dd189SChris Lattner /// getPointerToGlobalIfAvailable - This returns the address of the specified 1236d8dd189SChris Lattner /// global value if it is has already been codegen'd, otherwise it returns null. 1246d8dd189SChris Lattner /// 1256d8dd189SChris Lattner void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) { 1266d8dd189SChris Lattner MutexGuard locked(lock); 1276d8dd189SChris Lattner 1286d8dd189SChris Lattner std::map<const GlobalValue*, void*>::iterator I = 1296d8dd189SChris Lattner state.getGlobalAddressMap(locked).find(GV); 1306d8dd189SChris Lattner return I != state.getGlobalAddressMap(locked).end() ? I->second : 0; 1316d8dd189SChris Lattner } 1326d8dd189SChris Lattner 133748e8579SChris Lattner /// getGlobalValueAtAddress - Return the LLVM global value object that starts 134748e8579SChris Lattner /// at the specified address. 135748e8579SChris Lattner /// 136748e8579SChris Lattner const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { 13779876f52SReid Spencer MutexGuard locked(lock); 13879876f52SReid Spencer 139748e8579SChris Lattner // If we haven't computed the reverse mapping yet, do so first. 14079876f52SReid Spencer if (state.getGlobalAddressReverseMap(locked).empty()) { 1416d8dd189SChris Lattner for (std::map<const GlobalValue*, void *>::iterator 1426d8dd189SChris Lattner I = state.getGlobalAddressMap(locked).begin(), 1436d8dd189SChris Lattner E = state.getGlobalAddressMap(locked).end(); I != E; ++I) 1446d8dd189SChris Lattner state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, 1456d8dd189SChris Lattner I->first)); 146748e8579SChris Lattner } 147748e8579SChris Lattner 148748e8579SChris Lattner std::map<void *, const GlobalValue*>::iterator I = 14979876f52SReid Spencer state.getGlobalAddressReverseMap(locked).find(Addr); 15079876f52SReid Spencer return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0; 151748e8579SChris Lattner } 1525a0d4829SChris Lattner 1535a0d4829SChris Lattner // CreateArgv - Turn a vector of strings into a nice argv style array of 1545a0d4829SChris Lattner // pointers to null terminated strings. 1555a0d4829SChris Lattner // 1565a0d4829SChris Lattner static void *CreateArgv(ExecutionEngine *EE, 1575a0d4829SChris Lattner const std::vector<std::string> &InputArgv) { 15820a631fdSOwen Anderson unsigned PtrSize = EE->getTargetData()->getPointerSize(); 1595a0d4829SChris Lattner char *Result = new char[(InputArgv.size()+1)*PtrSize]; 1605a0d4829SChris Lattner 161*5834fdb3SBill Wendling DOUT << "ARGV = " << (void*)Result << "\n"; 1625a0d4829SChris Lattner const Type *SBytePtr = PointerType::get(Type::SByteTy); 1635a0d4829SChris Lattner 1645a0d4829SChris Lattner for (unsigned i = 0; i != InputArgv.size(); ++i) { 1655a0d4829SChris Lattner unsigned Size = InputArgv[i].size()+1; 1665a0d4829SChris Lattner char *Dest = new char[Size]; 167*5834fdb3SBill Wendling DOUT << "ARGV[" << i << "] = " << (void*)Dest << "\n"; 1685a0d4829SChris Lattner 1695a0d4829SChris Lattner std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); 1705a0d4829SChris Lattner Dest[Size-1] = 0; 1715a0d4829SChris Lattner 1725a0d4829SChris Lattner // Endian safe: Result[i] = (PointerTy)Dest; 1735a0d4829SChris Lattner EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize), 1745a0d4829SChris Lattner SBytePtr); 1755a0d4829SChris Lattner } 1765a0d4829SChris Lattner 1775a0d4829SChris Lattner // Null terminate it 1785a0d4829SChris Lattner EE->StoreValueToMemory(PTOGV(0), 1795a0d4829SChris Lattner (GenericValue*)(Result+InputArgv.size()*PtrSize), 1805a0d4829SChris Lattner SBytePtr); 1815a0d4829SChris Lattner return Result; 1825a0d4829SChris Lattner } 1835a0d4829SChris Lattner 184faae50b6SChris Lattner 185faae50b6SChris Lattner /// runStaticConstructorsDestructors - This method is used to execute all of 1860621caefSChris Lattner /// the static constructors or destructors for a program, depending on the 187faae50b6SChris Lattner /// value of isDtors. 188faae50b6SChris Lattner void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) { 189faae50b6SChris Lattner const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors"; 1900621caefSChris Lattner 1910621caefSChris Lattner // Execute global ctors/dtors for each module in the program. 1920621caefSChris Lattner for (unsigned m = 0, e = Modules.size(); m != e; ++m) { 1930621caefSChris Lattner GlobalVariable *GV = Modules[m]->getModule()->getNamedGlobal(Name); 194fe36eaebSChris Lattner 195fe36eaebSChris Lattner // If this global has internal linkage, or if it has a use, then it must be 196fe36eaebSChris Lattner // an old-style (llvmgcc3) static ctor with __main linked in and in use. If 1970621caefSChris Lattner // this is the case, don't execute any of the global ctors, __main will do 1980621caefSChris Lattner // it. 1990621caefSChris Lattner if (!GV || GV->isExternal() || GV->hasInternalLinkage()) continue; 200faae50b6SChris Lattner 2010621caefSChris Lattner // Should be an array of '{ int, void ()* }' structs. The first value is 2020621caefSChris Lattner // the init priority, which we ignore. 203faae50b6SChris Lattner ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 2040621caefSChris Lattner if (!InitList) continue; 205faae50b6SChris Lattner for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) 2060621caefSChris Lattner if (ConstantStruct *CS = 2070621caefSChris Lattner dyn_cast<ConstantStruct>(InitList->getOperand(i))) { 2080621caefSChris Lattner if (CS->getNumOperands() != 2) break; // Not array of 2-element structs. 209faae50b6SChris Lattner 210faae50b6SChris Lattner Constant *FP = CS->getOperand(1); 211faae50b6SChris Lattner if (FP->isNullValue()) 2120621caefSChris Lattner break; // Found a null terminator, exit. 213faae50b6SChris Lattner 214faae50b6SChris Lattner if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) 2156c38f0bbSReid Spencer if (CE->isCast()) 216faae50b6SChris Lattner FP = CE->getOperand(0); 217faae50b6SChris Lattner if (Function *F = dyn_cast<Function>(FP)) { 218faae50b6SChris Lattner // Execute the ctor/dtor function! 219faae50b6SChris Lattner runFunction(F, std::vector<GenericValue>()); 220faae50b6SChris Lattner } 221faae50b6SChris Lattner } 222faae50b6SChris Lattner } 2230621caefSChris Lattner } 224faae50b6SChris Lattner 2255a0d4829SChris Lattner /// runFunctionAsMain - This is a helper function which wraps runFunction to 2265a0d4829SChris Lattner /// handle the common task of starting up main with the specified argc, argv, 2275a0d4829SChris Lattner /// and envp parameters. 2285a0d4829SChris Lattner int ExecutionEngine::runFunctionAsMain(Function *Fn, 2295a0d4829SChris Lattner const std::vector<std::string> &argv, 2305a0d4829SChris Lattner const char * const * envp) { 2315a0d4829SChris Lattner std::vector<GenericValue> GVArgs; 2325a0d4829SChris Lattner GenericValue GVArgc; 2335a0d4829SChris Lattner GVArgc.IntVal = argv.size(); 234b1cad0b3SChris Lattner unsigned NumArgs = Fn->getFunctionType()->getNumParams(); 235b1cad0b3SChris Lattner if (NumArgs) { 2365a0d4829SChris Lattner GVArgs.push_back(GVArgc); // Arg #0 = argc. 237b1cad0b3SChris Lattner if (NumArgs > 1) { 2385a0d4829SChris Lattner GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv. 239b1cad0b3SChris Lattner assert(((char **)GVTOP(GVArgs[1]))[0] && 240b1cad0b3SChris Lattner "argv[0] was null after CreateArgv"); 241b1cad0b3SChris Lattner if (NumArgs > 2) { 2425a0d4829SChris Lattner std::vector<std::string> EnvVars; 2435a0d4829SChris Lattner for (unsigned i = 0; envp[i]; ++i) 2445a0d4829SChris Lattner EnvVars.push_back(envp[i]); 2455a0d4829SChris Lattner GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp. 246b1cad0b3SChris Lattner } 247b1cad0b3SChris Lattner } 248b1cad0b3SChris Lattner } 2495a0d4829SChris Lattner return runFunction(Fn, GVArgs).IntVal; 2505a0d4829SChris Lattner } 2515a0d4829SChris Lattner 252260b0c88SMisha Brukman /// If possible, create a JIT, unless the caller specifically requests an 253260b0c88SMisha Brukman /// Interpreter or there's an error. If even an Interpreter cannot be created, 254260b0c88SMisha Brukman /// NULL is returned. 255857c21b4SMisha Brukman /// 2562f1e2002SMisha Brukman ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP, 2570b2de9f2SChris Lattner bool ForceInterpreter) { 2584bd3bd5bSBrian Gaeke ExecutionEngine *EE = 0; 2594bd3bd5bSBrian Gaeke 260c8c6c03dSChris Lattner // Unless the interpreter was explicitly selected, try making a JIT. 2612d52c1b8SChris Lattner if (!ForceInterpreter && JITCtor) 2620b2de9f2SChris Lattner EE = JITCtor(MP); 2634bd3bd5bSBrian Gaeke 2644bd3bd5bSBrian Gaeke // If we can't make a JIT, make an interpreter instead. 2652d52c1b8SChris Lattner if (EE == 0 && InterpCtor) 2660b2de9f2SChris Lattner EE = InterpCtor(MP); 267c8c6c03dSChris Lattner 2680b2de9f2SChris Lattner if (EE) { 26970e37278SReid Spencer // Make sure we can resolve symbols in the program as well. The zero arg 27070e37278SReid Spencer // to the function tells DynamicLibrary to load the program, not a library. 27163539389SChris Lattner try { 27270e37278SReid Spencer sys::DynamicLibrary::LoadLibraryPermanently(0); 27363539389SChris Lattner } catch (...) { 27463539389SChris Lattner } 2750b2de9f2SChris Lattner } 27670e37278SReid Spencer 2774bd3bd5bSBrian Gaeke return EE; 2784bd3bd5bSBrian Gaeke } 2794bd3bd5bSBrian Gaeke 280857c21b4SMisha Brukman /// getPointerToGlobal - This returns the address of the specified global 281857c21b4SMisha Brukman /// value. This may involve code generation if it's a function. 282857c21b4SMisha Brukman /// 283996fe010SChris Lattner void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { 2841678e859SBrian Gaeke if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) 285996fe010SChris Lattner return getPointerToFunction(F); 286996fe010SChris Lattner 28779876f52SReid Spencer MutexGuard locked(lock); 28869e84901SJeff Cohen void *p = state.getGlobalAddressMap(locked)[GV]; 28969e84901SJeff Cohen if (p) 29069e84901SJeff Cohen return p; 29169e84901SJeff Cohen 29269e84901SJeff Cohen // Global variable might have been added since interpreter started. 29369e84901SJeff Cohen if (GlobalVariable *GVar = 29469e84901SJeff Cohen const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV))) 29569e84901SJeff Cohen EmitGlobalVariable(GVar); 29669e84901SJeff Cohen else 29769e84901SJeff Cohen assert("Global hasn't had an address allocated yet!"); 29879876f52SReid Spencer return state.getGlobalAddressMap(locked)[GV]; 299996fe010SChris Lattner } 300996fe010SChris Lattner 3016c38f0bbSReid Spencer /// This function converts a Constant* into a GenericValue. The interesting 3026c38f0bbSReid Spencer /// part is if C is a ConstantExpr. 3036c38f0bbSReid Spencer /// @brief Get a GenericValue for a Constnat* 304996fe010SChris Lattner GenericValue ExecutionEngine::getConstantValue(const Constant *C) { 3056c38f0bbSReid Spencer // Declare the result as garbage. 306996fe010SChris Lattner GenericValue Result; 3076c38f0bbSReid Spencer 3086c38f0bbSReid Spencer // If its undefined, return the garbage. 309d7a7a3f4SChris Lattner if (isa<UndefValue>(C)) return Result; 3109de0d14dSChris Lattner 3116c38f0bbSReid Spencer // If the value is a ConstantExpr 3126c38f0bbSReid Spencer if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 3139de0d14dSChris Lattner switch (CE->getOpcode()) { 3149de0d14dSChris Lattner case Instruction::GetElementPtr: { 3156c38f0bbSReid Spencer // Compute the index 31668cbcc3eSChris Lattner Result = getConstantValue(CE->getOperand(0)); 3179de0d14dSChris Lattner std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end()); 3189de0d14dSChris Lattner uint64_t Offset = 3199de0d14dSChris Lattner TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes); 3209de0d14dSChris Lattner 32120a631fdSOwen Anderson if (getTargetData()->getPointerSize() == 4) 322a551033eSChris Lattner Result.IntVal += Offset; 323a551033eSChris Lattner else 3249de0d14dSChris Lattner Result.LongVal += Offset; 3259de0d14dSChris Lattner return Result; 3269de0d14dSChris Lattner } 3276c38f0bbSReid Spencer case Instruction::Trunc: 3286c38f0bbSReid Spencer case Instruction::ZExt: 3296c38f0bbSReid Spencer case Instruction::SExt: 3306c38f0bbSReid Spencer case Instruction::FPTrunc: 3316c38f0bbSReid Spencer case Instruction::FPExt: 3326c38f0bbSReid Spencer case Instruction::UIToFP: 3336c38f0bbSReid Spencer case Instruction::SIToFP: 3346c38f0bbSReid Spencer case Instruction::FPToUI: 3356c38f0bbSReid Spencer case Instruction::FPToSI: 3366c38f0bbSReid Spencer break; 3376c38f0bbSReid Spencer case Instruction::PtrToInt: { 33868cbcc3eSChris Lattner Constant *Op = CE->getOperand(0); 3395def7a57SChris Lattner GenericValue GV = getConstantValue(Op); 3406c38f0bbSReid Spencer return GV; 3416c38f0bbSReid Spencer } 3426c38f0bbSReid Spencer case Instruction::BitCast: { 3436c38f0bbSReid Spencer // Bit casts are no-ops but we can only return the GV of the operand if 3446c38f0bbSReid Spencer // they are the same basic type (pointer->pointer, packed->packed, etc.) 3456c38f0bbSReid Spencer Constant *Op = CE->getOperand(0); 3466c38f0bbSReid Spencer GenericValue GV = getConstantValue(Op); 3476b727599SChris Lattner if (Op->getType()->getTypeID() == C->getType()->getTypeID()) 3485def7a57SChris Lattner return GV; 3496c38f0bbSReid Spencer break; 3506c38f0bbSReid Spencer } 3516c38f0bbSReid Spencer case Instruction::IntToPtr: { 3526c38f0bbSReid Spencer // IntToPtr casts are just so special. Cast to intptr_t first. 3536c38f0bbSReid Spencer Constant *Op = CE->getOperand(0); 3546c38f0bbSReid Spencer GenericValue GV = getConstantValue(Op); 3556b727599SChris Lattner switch (Op->getType()->getTypeID()) { 3565def7a57SChris Lattner case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal); 3575def7a57SChris Lattner case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal); 3585def7a57SChris Lattner case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal); 3595def7a57SChris Lattner case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal); 3605def7a57SChris Lattner case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal); 3615def7a57SChris Lattner case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal); 3625def7a57SChris Lattner case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal); 3635def7a57SChris Lattner case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal); 3645def7a57SChris Lattner case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal); 3655def7a57SChris Lattner default: assert(0 && "Unknown integral type!"); 3665def7a57SChris Lattner } 36768cbcc3eSChris Lattner break; 36868cbcc3eSChris Lattner } 36968cbcc3eSChris Lattner case Instruction::Add: 370c4e6bb5fSChris Lattner switch (CE->getOperand(0)->getType()->getTypeID()) { 371c4e6bb5fSChris Lattner default: assert(0 && "Bad add type!"); abort(); 372c4e6bb5fSChris Lattner case Type::LongTyID: 373c4e6bb5fSChris Lattner case Type::ULongTyID: 3743b3276beSChris Lattner Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal + 3753b3276beSChris Lattner getConstantValue(CE->getOperand(1)).LongVal; 37668cbcc3eSChris Lattner break; 377c4e6bb5fSChris Lattner case Type::IntTyID: 378c4e6bb5fSChris Lattner case Type::UIntTyID: 379c4e6bb5fSChris Lattner Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal + 380c4e6bb5fSChris Lattner getConstantValue(CE->getOperand(1)).IntVal; 381c4e6bb5fSChris Lattner break; 382c4e6bb5fSChris Lattner case Type::ShortTyID: 383c4e6bb5fSChris Lattner case Type::UShortTyID: 384c4e6bb5fSChris Lattner Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal + 385c4e6bb5fSChris Lattner getConstantValue(CE->getOperand(1)).ShortVal; 386c4e6bb5fSChris Lattner break; 387c4e6bb5fSChris Lattner case Type::SByteTyID: 388c4e6bb5fSChris Lattner case Type::UByteTyID: 389c4e6bb5fSChris Lattner Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal + 390c4e6bb5fSChris Lattner getConstantValue(CE->getOperand(1)).SByteVal; 391c4e6bb5fSChris Lattner break; 392c4e6bb5fSChris Lattner case Type::FloatTyID: 393c4e6bb5fSChris Lattner Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal + 394c4e6bb5fSChris Lattner getConstantValue(CE->getOperand(1)).FloatVal; 395c4e6bb5fSChris Lattner break; 396c4e6bb5fSChris Lattner case Type::DoubleTyID: 397c4e6bb5fSChris Lattner Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal + 398c4e6bb5fSChris Lattner getConstantValue(CE->getOperand(1)).DoubleVal; 399c4e6bb5fSChris Lattner break; 400c4e6bb5fSChris Lattner } 40168cbcc3eSChris Lattner return Result; 4029de0d14dSChris Lattner default: 40368cbcc3eSChris Lattner break; 40468cbcc3eSChris Lattner } 405*5834fdb3SBill Wendling llvm_cerr << "ConstantExpr not handled as global var init: " << *CE << "\n"; 4069de0d14dSChris Lattner abort(); 4079de0d14dSChris Lattner } 408996fe010SChris Lattner 4096b727599SChris Lattner switch (C->getType()->getTypeID()) { 410e0fc4dfcSReid Spencer #define GET_CONST_VAL(TY, CTY, CLASS, GETMETH) \ 411e0fc4dfcSReid Spencer case Type::TY##TyID: Result.TY##Val = (CTY)cast<CLASS>(C)->GETMETH(); break 412e0fc4dfcSReid Spencer GET_CONST_VAL(Bool , bool , ConstantBool, getValue); 413e0fc4dfcSReid Spencer GET_CONST_VAL(UByte , unsigned char , ConstantInt, getZExtValue); 414e0fc4dfcSReid Spencer GET_CONST_VAL(SByte , signed char , ConstantInt, getSExtValue); 415e0fc4dfcSReid Spencer GET_CONST_VAL(UShort , unsigned short, ConstantInt, getZExtValue); 416e0fc4dfcSReid Spencer GET_CONST_VAL(Short , signed short , ConstantInt, getSExtValue); 417e0fc4dfcSReid Spencer GET_CONST_VAL(UInt , unsigned int , ConstantInt, getZExtValue); 418e0fc4dfcSReid Spencer GET_CONST_VAL(Int , signed int , ConstantInt, getSExtValue); 419e0fc4dfcSReid Spencer GET_CONST_VAL(ULong , uint64_t , ConstantInt, getZExtValue); 420e0fc4dfcSReid Spencer GET_CONST_VAL(Long , int64_t , ConstantInt, getSExtValue); 421e0fc4dfcSReid Spencer GET_CONST_VAL(Float , float , ConstantFP, getValue); 422e0fc4dfcSReid Spencer GET_CONST_VAL(Double , double , ConstantFP, getValue); 423996fe010SChris Lattner #undef GET_CONST_VAL 424996fe010SChris Lattner case Type::PointerTyID: 4256a0fd73bSReid Spencer if (isa<ConstantPointerNull>(C)) 426996fe010SChris Lattner Result.PointerVal = 0; 4276a0fd73bSReid Spencer else if (const Function *F = dyn_cast<Function>(C)) 4286a0fd73bSReid Spencer Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F))); 4296a0fd73bSReid Spencer else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C)) 4306a0fd73bSReid Spencer Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV))); 431e6492f10SChris Lattner else 432996fe010SChris Lattner assert(0 && "Unknown constant pointer type!"); 433996fe010SChris Lattner break; 434996fe010SChris Lattner default: 435*5834fdb3SBill Wendling llvm_cerr << "ERROR: Constant unimp for type: " << *C->getType() << "\n"; 4369de0d14dSChris Lattner abort(); 437996fe010SChris Lattner } 438996fe010SChris Lattner return Result; 439996fe010SChris Lattner } 440996fe010SChris Lattner 4414ca2ea5bSNate Begeman /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr 4424ca2ea5bSNate Begeman /// is the address of the memory at which to store Val, cast to GenericValue *. 4434ca2ea5bSNate Begeman /// It is not a pointer to a GenericValue containing the address at which to 4444ca2ea5bSNate Begeman /// store Val. 445857c21b4SMisha Brukman /// 446996fe010SChris Lattner void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr, 447996fe010SChris Lattner const Type *Ty) { 44820a631fdSOwen Anderson if (getTargetData()->isLittleEndian()) { 4496b727599SChris Lattner switch (Ty->getTypeID()) { 450996fe010SChris Lattner case Type::BoolTyID: 451996fe010SChris Lattner case Type::UByteTyID: 452996fe010SChris Lattner case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break; 453996fe010SChris Lattner case Type::UShortTyID: 454996fe010SChris Lattner case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255; 455996fe010SChris Lattner Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255; 456996fe010SChris Lattner break; 457b348952dSChris Lattner Store4BytesLittleEndian: 458996fe010SChris Lattner case Type::FloatTyID: 459996fe010SChris Lattner case Type::UIntTyID: 460996fe010SChris Lattner case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255; 461996fe010SChris Lattner Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255; 462996fe010SChris Lattner Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255; 463996fe010SChris Lattner Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255; 464996fe010SChris Lattner break; 46520a631fdSOwen Anderson case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4) 466b348952dSChris Lattner goto Store4BytesLittleEndian; 467996fe010SChris Lattner case Type::DoubleTyID: 468996fe010SChris Lattner case Type::ULongTyID: 469df1f1524SChris Lattner case Type::LongTyID: 470df1f1524SChris Lattner Ptr->Untyped[0] = (unsigned char)(Val.ULongVal ); 471df1f1524SChris Lattner Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 8); 472df1f1524SChris Lattner Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 16); 473df1f1524SChris Lattner Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 24); 474df1f1524SChris Lattner Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 32); 475df1f1524SChris Lattner Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 40); 476df1f1524SChris Lattner Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 48); 477df1f1524SChris Lattner Ptr->Untyped[7] = (unsigned char)(Val.ULongVal >> 56); 478996fe010SChris Lattner break; 479996fe010SChris Lattner default: 480*5834fdb3SBill Wendling llvm_cerr << "Cannot store value of type " << *Ty << "!\n"; 481996fe010SChris Lattner } 482996fe010SChris Lattner } else { 4836b727599SChris Lattner switch (Ty->getTypeID()) { 484996fe010SChris Lattner case Type::BoolTyID: 485996fe010SChris Lattner case Type::UByteTyID: 486996fe010SChris Lattner case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break; 487996fe010SChris Lattner case Type::UShortTyID: 488996fe010SChris Lattner case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255; 489996fe010SChris Lattner Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255; 490996fe010SChris Lattner break; 491b348952dSChris Lattner Store4BytesBigEndian: 492996fe010SChris Lattner case Type::FloatTyID: 493996fe010SChris Lattner case Type::UIntTyID: 494996fe010SChris Lattner case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255; 495996fe010SChris Lattner Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255; 496996fe010SChris Lattner Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255; 497996fe010SChris Lattner Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255; 498996fe010SChris Lattner break; 49920a631fdSOwen Anderson case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4) 500b348952dSChris Lattner goto Store4BytesBigEndian; 501996fe010SChris Lattner case Type::DoubleTyID: 502996fe010SChris Lattner case Type::ULongTyID: 503df1f1524SChris Lattner case Type::LongTyID: 504df1f1524SChris Lattner Ptr->Untyped[7] = (unsigned char)(Val.ULongVal ); 505df1f1524SChris Lattner Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 8); 506df1f1524SChris Lattner Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 16); 507df1f1524SChris Lattner Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 24); 508df1f1524SChris Lattner Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 32); 509df1f1524SChris Lattner Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 40); 510df1f1524SChris Lattner Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 48); 511df1f1524SChris Lattner Ptr->Untyped[0] = (unsigned char)(Val.ULongVal >> 56); 512996fe010SChris Lattner break; 513996fe010SChris Lattner default: 514*5834fdb3SBill Wendling llvm_cerr << "Cannot store value of type " << *Ty << "!\n"; 515996fe010SChris Lattner } 516996fe010SChris Lattner } 517996fe010SChris Lattner } 518996fe010SChris Lattner 519857c21b4SMisha Brukman /// FIXME: document 520857c21b4SMisha Brukman /// 5217f389e8cSChris Lattner GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr, 5227f389e8cSChris Lattner const Type *Ty) { 5237f389e8cSChris Lattner GenericValue Result; 52420a631fdSOwen Anderson if (getTargetData()->isLittleEndian()) { 5256b727599SChris Lattner switch (Ty->getTypeID()) { 5267f389e8cSChris Lattner case Type::BoolTyID: 5277f389e8cSChris Lattner case Type::UByteTyID: 5287f389e8cSChris Lattner case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; 5297f389e8cSChris Lattner case Type::UShortTyID: 5307f389e8cSChris Lattner case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] | 5317f389e8cSChris Lattner ((unsigned)Ptr->Untyped[1] << 8); 5327f389e8cSChris Lattner break; 5337f389e8cSChris Lattner Load4BytesLittleEndian: 5347f389e8cSChris Lattner case Type::FloatTyID: 5357f389e8cSChris Lattner case Type::UIntTyID: 5367f389e8cSChris Lattner case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] | 5377f389e8cSChris Lattner ((unsigned)Ptr->Untyped[1] << 8) | 5387f389e8cSChris Lattner ((unsigned)Ptr->Untyped[2] << 16) | 5397f389e8cSChris Lattner ((unsigned)Ptr->Untyped[3] << 24); 5407f389e8cSChris Lattner break; 54120a631fdSOwen Anderson case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4) 5427f389e8cSChris Lattner goto Load4BytesLittleEndian; 5437f389e8cSChris Lattner case Type::DoubleTyID: 5447f389e8cSChris Lattner case Type::ULongTyID: 5457f389e8cSChris Lattner case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] | 5467f389e8cSChris Lattner ((uint64_t)Ptr->Untyped[1] << 8) | 5477f389e8cSChris Lattner ((uint64_t)Ptr->Untyped[2] << 16) | 5487f389e8cSChris Lattner ((uint64_t)Ptr->Untyped[3] << 24) | 5497f389e8cSChris Lattner ((uint64_t)Ptr->Untyped[4] << 32) | 5507f389e8cSChris Lattner ((uint64_t)Ptr->Untyped[5] << 40) | 5517f389e8cSChris Lattner ((uint64_t)Ptr->Untyped[6] << 48) | 5527f389e8cSChris Lattner ((uint64_t)Ptr->Untyped[7] << 56); 5537f389e8cSChris Lattner break; 5547f389e8cSChris Lattner default: 555*5834fdb3SBill Wendling llvm_cerr << "Cannot load value of type " << *Ty << "!\n"; 5567f389e8cSChris Lattner abort(); 5577f389e8cSChris Lattner } 5587f389e8cSChris Lattner } else { 5596b727599SChris Lattner switch (Ty->getTypeID()) { 5607f389e8cSChris Lattner case Type::BoolTyID: 5617f389e8cSChris Lattner case Type::UByteTyID: 5627f389e8cSChris Lattner case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; 5637f389e8cSChris Lattner case Type::UShortTyID: 5647f389e8cSChris Lattner case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] | 5657f389e8cSChris Lattner ((unsigned)Ptr->Untyped[0] << 8); 5667f389e8cSChris Lattner break; 5677f389e8cSChris Lattner Load4BytesBigEndian: 5687f389e8cSChris Lattner case Type::FloatTyID: 5697f389e8cSChris Lattner case Type::UIntTyID: 5707f389e8cSChris Lattner case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] | 5717f389e8cSChris Lattner ((unsigned)Ptr->Untyped[2] << 8) | 5727f389e8cSChris Lattner ((unsigned)Ptr->Untyped[1] << 16) | 5737f389e8cSChris Lattner ((unsigned)Ptr->Untyped[0] << 24); 5747f389e8cSChris Lattner break; 57520a631fdSOwen Anderson case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4) 5767f389e8cSChris Lattner goto Load4BytesBigEndian; 5777f389e8cSChris Lattner case Type::DoubleTyID: 5787f389e8cSChris Lattner case Type::ULongTyID: 5797f389e8cSChris Lattner case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] | 5807f389e8cSChris Lattner ((uint64_t)Ptr->Untyped[6] << 8) | 5817f389e8cSChris Lattner ((uint64_t)Ptr->Untyped[5] << 16) | 5827f389e8cSChris Lattner ((uint64_t)Ptr->Untyped[4] << 24) | 5837f389e8cSChris Lattner ((uint64_t)Ptr->Untyped[3] << 32) | 5847f389e8cSChris Lattner ((uint64_t)Ptr->Untyped[2] << 40) | 5857f389e8cSChris Lattner ((uint64_t)Ptr->Untyped[1] << 48) | 5867f389e8cSChris Lattner ((uint64_t)Ptr->Untyped[0] << 56); 5877f389e8cSChris Lattner break; 5887f389e8cSChris Lattner default: 589*5834fdb3SBill Wendling llvm_cerr << "Cannot load value of type " << *Ty << "!\n"; 5907f389e8cSChris Lattner abort(); 5917f389e8cSChris Lattner } 5927f389e8cSChris Lattner } 5937f389e8cSChris Lattner return Result; 5947f389e8cSChris Lattner } 5957f389e8cSChris Lattner 596996fe010SChris Lattner // InitializeMemory - Recursive function to apply a Constant value into the 597996fe010SChris Lattner // specified memory location... 598996fe010SChris Lattner // 599996fe010SChris Lattner void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { 60061753bf8SChris Lattner if (isa<UndefValue>(Init)) { 60161753bf8SChris Lattner return; 60269d62138SRobert Bocchino } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(Init)) { 60369d62138SRobert Bocchino unsigned ElementSize = 60420a631fdSOwen Anderson getTargetData()->getTypeSize(CP->getType()->getElementType()); 60569d62138SRobert Bocchino for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) 60669d62138SRobert Bocchino InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize); 60769d62138SRobert Bocchino return; 60861753bf8SChris Lattner } else if (Init->getType()->isFirstClassType()) { 609996fe010SChris Lattner GenericValue Val = getConstantValue(Init); 610996fe010SChris Lattner StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); 611996fe010SChris Lattner return; 612834b1272SChris Lattner } else if (isa<ConstantAggregateZero>(Init)) { 61320a631fdSOwen Anderson memset(Addr, 0, (size_t)getTargetData()->getTypeSize(Init->getType())); 614834b1272SChris Lattner return; 615996fe010SChris Lattner } 616996fe010SChris Lattner 6176b727599SChris Lattner switch (Init->getType()->getTypeID()) { 618996fe010SChris Lattner case Type::ArrayTyID: { 619996fe010SChris Lattner const ConstantArray *CPA = cast<ConstantArray>(Init); 620996fe010SChris Lattner unsigned ElementSize = 62120a631fdSOwen Anderson getTargetData()->getTypeSize(CPA->getType()->getElementType()); 62283243725SAlkis Evlogimenos for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) 62383243725SAlkis Evlogimenos InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); 624996fe010SChris Lattner return; 625996fe010SChris Lattner } 626996fe010SChris Lattner 627996fe010SChris Lattner case Type::StructTyID: { 628996fe010SChris Lattner const ConstantStruct *CPS = cast<ConstantStruct>(Init); 629996fe010SChris Lattner const StructLayout *SL = 63020a631fdSOwen Anderson getTargetData()->getStructLayout(cast<StructType>(CPS->getType())); 63183243725SAlkis Evlogimenos for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) 63283243725SAlkis Evlogimenos InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]); 633996fe010SChris Lattner return; 634996fe010SChris Lattner } 635996fe010SChris Lattner 636996fe010SChris Lattner default: 637*5834fdb3SBill Wendling llvm_cerr << "Bad Type: " << *Init->getType() << "\n"; 638996fe010SChris Lattner assert(0 && "Unknown constant type to initialize memory with!"); 639996fe010SChris Lattner } 640996fe010SChris Lattner } 641996fe010SChris Lattner 642996fe010SChris Lattner /// EmitGlobals - Emit all of the global variables to memory, storing their 643996fe010SChris Lattner /// addresses into GlobalAddress. This must make sure to copy the contents of 644996fe010SChris Lattner /// their initializers into the memory. 645996fe010SChris Lattner /// 646996fe010SChris Lattner void ExecutionEngine::emitGlobals() { 64720a631fdSOwen Anderson const TargetData *TD = getTargetData(); 648996fe010SChris Lattner 649996fe010SChris Lattner // Loop over all of the global variables in the program, allocating the memory 6500621caefSChris Lattner // to hold them. If there is more than one module, do a prepass over globals 6510621caefSChris Lattner // to figure out how the different modules should link together. 6520621caefSChris Lattner // 6530621caefSChris Lattner std::map<std::pair<std::string, const Type*>, 6540621caefSChris Lattner const GlobalValue*> LinkedGlobalsMap; 6550621caefSChris Lattner 6560621caefSChris Lattner if (Modules.size() != 1) { 6570621caefSChris Lattner for (unsigned m = 0, e = Modules.size(); m != e; ++m) { 6580621caefSChris Lattner Module &M = *Modules[m]->getModule(); 6590621caefSChris Lattner for (Module::const_global_iterator I = M.global_begin(), 6600621caefSChris Lattner E = M.global_end(); I != E; ++I) { 6610621caefSChris Lattner const GlobalValue *GV = I; 6620621caefSChris Lattner if (GV->hasInternalLinkage() || GV->isExternal() || 6630621caefSChris Lattner GV->hasAppendingLinkage() || !GV->hasName()) 6640621caefSChris Lattner continue;// Ignore external globals and globals with internal linkage. 6650621caefSChris Lattner 6660621caefSChris Lattner const GlobalValue *&GVEntry = 6670621caefSChris Lattner LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; 6680621caefSChris Lattner 6690621caefSChris Lattner // If this is the first time we've seen this global, it is the canonical 6700621caefSChris Lattner // version. 6710621caefSChris Lattner if (!GVEntry) { 6720621caefSChris Lattner GVEntry = GV; 6730621caefSChris Lattner continue; 6740621caefSChris Lattner } 6750621caefSChris Lattner 6760621caefSChris Lattner // If the existing global is strong, never replace it. 677d61d39ecSAnton Korobeynikov if (GVEntry->hasExternalLinkage() || 678d61d39ecSAnton Korobeynikov GVEntry->hasDLLImportLinkage() || 679d61d39ecSAnton Korobeynikov GVEntry->hasDLLExportLinkage()) 6800621caefSChris Lattner continue; 6810621caefSChris Lattner 6820621caefSChris Lattner // Otherwise, we know it's linkonce/weak, replace it if this is a strong 6830621caefSChris Lattner // symbol. 6840621caefSChris Lattner if (GV->hasExternalLinkage()) 6850621caefSChris Lattner GVEntry = GV; 6860621caefSChris Lattner } 6870621caefSChris Lattner } 6880621caefSChris Lattner } 6890621caefSChris Lattner 6900621caefSChris Lattner std::vector<const GlobalValue*> NonCanonicalGlobals; 6910621caefSChris Lattner for (unsigned m = 0, e = Modules.size(); m != e; ++m) { 6920621caefSChris Lattner Module &M = *Modules[m]->getModule(); 6938ffb6611SChris Lattner for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 6940621caefSChris Lattner I != E; ++I) { 6950621caefSChris Lattner // In the multi-module case, see what this global maps to. 6960621caefSChris Lattner if (!LinkedGlobalsMap.empty()) { 6970621caefSChris Lattner if (const GlobalValue *GVEntry = 6980621caefSChris Lattner LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) { 6990621caefSChris Lattner // If something else is the canonical global, ignore this one. 7000621caefSChris Lattner if (GVEntry != &*I) { 7010621caefSChris Lattner NonCanonicalGlobals.push_back(I); 7020621caefSChris Lattner continue; 7030621caefSChris Lattner } 7040621caefSChris Lattner } 7050621caefSChris Lattner } 7060621caefSChris Lattner 707996fe010SChris Lattner if (!I->isExternal()) { 7080621caefSChris Lattner // Get the type of the global. 709996fe010SChris Lattner const Type *Ty = I->getType()->getElementType(); 710996fe010SChris Lattner 711996fe010SChris Lattner // Allocate some memory for it! 71220a631fdSOwen Anderson unsigned Size = TD->getTypeSize(Ty); 7136bbe3eceSChris Lattner addGlobalMapping(I, new char[Size]); 714996fe010SChris Lattner } else { 715e8bbcfc2SBrian Gaeke // External variable reference. Try to use the dynamic loader to 716e8bbcfc2SBrian Gaeke // get a pointer to it. 7170621caefSChris Lattner if (void *SymAddr = 7180621caefSChris Lattner sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName().c_str())) 719748e8579SChris Lattner addGlobalMapping(I, SymAddr); 7209de0d14dSChris Lattner else { 721*5834fdb3SBill Wendling llvm_cerr << "Could not resolve external global address: " 7229de0d14dSChris Lattner << I->getName() << "\n"; 7239de0d14dSChris Lattner abort(); 7249de0d14dSChris Lattner } 725996fe010SChris Lattner } 7260621caefSChris Lattner } 7270621caefSChris Lattner 7280621caefSChris Lattner // If there are multiple modules, map the non-canonical globals to their 7290621caefSChris Lattner // canonical location. 7300621caefSChris Lattner if (!NonCanonicalGlobals.empty()) { 7310621caefSChris Lattner for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) { 7320621caefSChris Lattner const GlobalValue *GV = NonCanonicalGlobals[i]; 7330621caefSChris Lattner const GlobalValue *CGV = 7340621caefSChris Lattner LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; 7350621caefSChris Lattner void *Ptr = getPointerToGlobalIfAvailable(CGV); 7360621caefSChris Lattner assert(Ptr && "Canonical global wasn't codegen'd!"); 7370621caefSChris Lattner addGlobalMapping(GV, getPointerToGlobalIfAvailable(CGV)); 7380621caefSChris Lattner } 7390621caefSChris Lattner } 740996fe010SChris Lattner 741996fe010SChris Lattner // Now that all of the globals are set up in memory, loop through them all and 742996fe010SChris Lattner // initialize their contents. 7438ffb6611SChris Lattner for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 7440621caefSChris Lattner I != E; ++I) { 7450621caefSChris Lattner if (!I->isExternal()) { 7460621caefSChris Lattner if (!LinkedGlobalsMap.empty()) { 7470621caefSChris Lattner if (const GlobalValue *GVEntry = 7480621caefSChris Lattner LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) 7490621caefSChris Lattner if (GVEntry != &*I) // Not the canonical variable. 7500621caefSChris Lattner continue; 7510621caefSChris Lattner } 7526bbe3eceSChris Lattner EmitGlobalVariable(I); 7536bbe3eceSChris Lattner } 7540621caefSChris Lattner } 7550621caefSChris Lattner } 7560621caefSChris Lattner } 7576bbe3eceSChris Lattner 7586bbe3eceSChris Lattner // EmitGlobalVariable - This method emits the specified global variable to the 7596bbe3eceSChris Lattner // address specified in GlobalAddresses, or allocates new memory if it's not 7606bbe3eceSChris Lattner // already in the map. 761fbcc0aa1SChris Lattner void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { 762748e8579SChris Lattner void *GA = getPointerToGlobalIfAvailable(GV); 763*5834fdb3SBill Wendling DOUT << "Global '" << GV->getName() << "' -> " << GA << "\n"; 764dc631735SChris Lattner 765fbcc0aa1SChris Lattner const Type *ElTy = GV->getType()->getElementType(); 76620a631fdSOwen Anderson size_t GVSize = (size_t)getTargetData()->getTypeSize(ElTy); 7676bbe3eceSChris Lattner if (GA == 0) { 7686bbe3eceSChris Lattner // If it's not already specified, allocate memory for the global. 769d215992bSChris Lattner GA = new char[GVSize]; 770748e8579SChris Lattner addGlobalMapping(GV, GA); 7716bbe3eceSChris Lattner } 772fbcc0aa1SChris Lattner 7736bbe3eceSChris Lattner InitializeMemory(GV->getInitializer(), GA); 774df1f1524SChris Lattner NumInitBytes += (unsigned)GVSize; 7756bbe3eceSChris Lattner ++NumGlobals; 776996fe010SChris Lattner } 777