ExecutionEngine.cpp revision b19383818bcaf65509f4050c81f75c8ee0624f8c
18dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===// 28dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// 38dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// The LLVM Compiler Infrastructure 48dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// 58dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// This file is distributed under the University of Illinois Open Source 68dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// License. See LICENSE.TXT for details. 78dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// 88dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===// 98dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// 108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// This file defines the common interface used by the various execution engine 118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// subclasses. 122d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall// 13e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor//===----------------------------------------------------------------------===// 14f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall 157cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#define DEBUG_TYPE "jit" 16aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor#include "llvm/ExecutionEngine/ExecutionEngine.h" 178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "llvm/Constants.h" 198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "llvm/DerivedTypes.h" 200c01d18094100db92d38daa923c95661512db203John McCall#include "llvm/Module.h" 218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "llvm/ExecutionEngine/GenericValue.h" 22a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor#include "llvm/ADT/Statistic.h" 2321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall#include "llvm/Support/Debug.h" 2483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor#include "llvm/Support/ErrorHandling.h" 258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "llvm/Support/MutexGuard.h" 268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "llvm/Support/ValueHandle.h" 278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "llvm/Support/raw_ostream.h" 28b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall#include "llvm/System/DynamicLibrary.h" 29b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall#include "llvm/System/Host.h" 30c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor#include "llvm/Target/TargetData.h" 31c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor#include <cmath> 32a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi#include <cstring> 33c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorusing namespace llvm; 34a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi 35c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas GregorSTATISTIC(NumInitBytes, "Number of bytes of global vars initialized"); 36a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA TakumiSTATISTIC(NumGlobals , "Number of global vars initialized"); 37c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor 38b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallExecutionEngine *(*ExecutionEngine::JITCtor)( 39a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi Module *M, 40c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor std::string *ErrorStr, 41b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall JITMemoryManager *JMM, 42b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall CodeGenOpt::Level OptLevel, 43b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall bool GVsWithCode, 44b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall CodeModel::Model CMM, 45b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall StringRef MArch, 46c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor StringRef MCPU, 47c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor const SmallVectorImpl<std::string>& MAttrs) = 0; 48a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA TakumiExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M, 49c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor std::string *ErrorStr) = 0; 50a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA TakumiExecutionEngine::EERegisterFn ExecutionEngine::ExceptionTableRegister = 0; 51c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor 52a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi 53c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas GregorExecutionEngine::ExecutionEngine(Module *M) 54b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall : EEState(*this), 55a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi LazyFunctionCreator(0) { 56c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor CompilingLazily = false; 57b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall GVCompilationDisabled = false; 58b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall SymbolSearchingDisabled = false; 59b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall Modules.push_back(M); 607b9ff0c09025dcbe48ec7db71330e2066d1e1863DeLesley Hutchins assert(M && "Module is null?"); 617b9ff0c09025dcbe48ec7db71330e2066d1e1863DeLesley Hutchins} 627b9ff0c09025dcbe48ec7db71330e2066d1e1863DeLesley Hutchins 631d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCallExecutionEngine::~ExecutionEngine() { 6423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins clearAllGlobalMappings(); 6523323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins for (unsigned i = 0, e = Modules.size(); i != e; ++i) 6623323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins delete Modules[i]; 67cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt} 68cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt 69cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Huntchar* ExecutionEngine::getMemoryForGV(const GlobalVariable* GV) { 7023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins const Type *ElTy = GV->getType()->getElementType(); 714ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy); 724ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth return new char[GVSize]; 73cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt} 74cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt 75f6702a3927147655206ae729a84339c4fda4c651Richard Smith/// removeModule - Remove a Module from the list of modules. 76f6702a3927147655206ae729a84339c4fda4c651Richard Smithbool ExecutionEngine::removeModule(Module *M) { 77f6702a3927147655206ae729a84339c4fda4c651Richard Smith for(SmallVector<Module *, 1>::iterator I = Modules.begin(), 78f6702a3927147655206ae729a84339c4fda4c651Richard Smith E = Modules.end(); I != E; ++I) { 7960d7b3a319d84d688752be3870615ac0f111fb16John McCall Module *Found = *I; 807663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky if (Found == M) { 81cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt Modules.erase(I); 82fc685ace387734599c475426b1a8efdb491054b8Aaron Ballman clearGlobalMappingsFromModule(M); 83fc685ace387734599c475426b1a8efdb491054b8Aaron Ballman return true; 84f6702a3927147655206ae729a84339c4fda4c651Richard Smith } 85cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt } 867663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky return false; 87a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi} 887663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky 89cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt/// FindFunctionNamed - Search all of the active modules to find the one that 90fc685ace387734599c475426b1a8efdb491054b8Aaron Ballman/// defines FnName. This is very slow operation and shouldn't be used for 91fc685ace387734599c475426b1a8efdb491054b8Aaron Ballman/// general code. 92cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean HuntFunction *ExecutionEngine::FindFunctionNamed(const char *FnName) { 934ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth for (unsigned i = 0, e = Modules.size(); i != e; ++i) { 944ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth if (Function *F = Modules[i]->getFunction(FnName)) 954ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth return F; 964ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth } 9723323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins return 0; 9823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins} 9923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins 10023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins 10123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchinsvoid *ExecutionEngineState::RemoveMapping( 10223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins const MutexGuard &, const GlobalValue *ToUnmap) { 10323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap); 10423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins void *OldVal; 1055bbc385ad2d8e487edfbc2756eaf4fb0b920cfe4Benjamin Kramer if (I == GlobalAddressMap.end()) 1065bbc385ad2d8e487edfbc2756eaf4fb0b920cfe4Benjamin Kramer OldVal = 0; 10731c195ac0f3869e742d42f9d02b6cd33442fb630Rafael Espindola else { 10831c195ac0f3869e742d42f9d02b6cd33442fb630Rafael Espindola OldVal = I->second; 10923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins GlobalAddressMap.erase(I); 110d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson } 111d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson 112d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson GlobalAddressReverseMap.erase(OldVal); 1134f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor return OldVal; 1144f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor} 115b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie 1164f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor/// addGlobalMapping - Tell the execution engine that the specified global is 1174f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor/// at the specified location. This is used internally as functions are JIT'd 1184f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor/// and as global variables are laid out in memory. It can and should also be 11957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner/// used by clients of the EE that want to have an LLVM global overlay 12057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner/// existing data in memory. 12157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattnervoid ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) { 12257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner MutexGuard locked(lock); 12357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner 12457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner DEBUG(dbgs() << "JIT: Map \'" << GV->getName() 12557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner << "\' to [" << Addr << "]\n";); 12657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner void *&CurVal = EEState.getGlobalAddressMap(locked)[GV]; 1274f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!"); 128b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie CurVal = Addr; 1294f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor 1304f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor // If we are using the reverse mapping, add it too 1313dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall if (!EEState.getGlobalAddressReverseMap(locked).empty()) { 1323dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall AssertingVH<const GlobalValue> &V = 1333dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall EEState.getGlobalAddressReverseMap(locked)[Addr]; 1343dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall assert((V == 0 || GV == 0) && "GlobalMapping already established!"); 1353dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall V = GV; 1363dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall } 1370cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor} 1380cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor 1393dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall/// clearAllGlobalMappings - Clear all global mappings and start over again 1403dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall/// use in dynamic compilation scenarios when you want to move globals 1413dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallvoid ExecutionEngine::clearAllGlobalMappings() { 1423dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall MutexGuard locked(lock); 1433dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall 1443dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall EEState.getGlobalAddressMap(locked).clear(); 1453e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith EEState.getGlobalAddressReverseMap(locked).clear(); 1463e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith} 1478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 148a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// clearGlobalMappingsFromModule - Clear all global mappings that came from a 149561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor/// particular module, because it has been removed from the JIT. 150836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregorvoid ExecutionEngine::clearGlobalMappingsFromModule(Module *M) { 151ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall MutexGuard locked(lock); 152ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall 153ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) { 1548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EEState.RemoveMapping(locked, FI); 155a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall } 1568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor for (Module::global_iterator GI = M->global_begin(), GE = M->global_end(); 157b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor GI != GE; ++GI) { 158b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor EEState.RemoveMapping(locked, GI); 1598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 1601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump} 161b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith 162b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith/// updateGlobalMapping - Replace an existing mapping for GV with a new 163b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith/// address. This updates both maps as required. If "Addr" is null, the 164b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith/// entry for the global is removed from the mappings. 165b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smithvoid *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) { 166b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith MutexGuard locked(lock); 167b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith 168b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith ExecutionEngineState::GlobalAddressMapTy &Map = 169b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith EEState.getGlobalAddressMap(locked); 170b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith 171b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith // Deleting from the mapping? 172b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith if (Addr == 0) { 173b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith return EEState.RemoveMapping(locked, GV); 174b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith } 175b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith 176b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith void *&CurVal = Map[GV]; 1778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor void *OldVal = CurVal; 178162e1c1b487352434552147967c3dd296ebee2f7Richard Smith 179162e1c1b487352434552147967c3dd296ebee2f7Richard Smith if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty()) 180162e1c1b487352434552147967c3dd296ebee2f7Richard Smith EEState.getGlobalAddressReverseMap(locked).erase(CurVal); 181162e1c1b487352434552147967c3dd296ebee2f7Richard Smith CurVal = Addr; 182162e1c1b487352434552147967c3dd296ebee2f7Richard Smith 183162e1c1b487352434552147967c3dd296ebee2f7Richard Smith // If we are using the reverse mapping, add it too 184162e1c1b487352434552147967c3dd296ebee2f7Richard Smith if (!EEState.getGlobalAddressReverseMap(locked).empty()) { 1858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor AssertingVH<const GlobalValue> &V = 1868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EEState.getGlobalAddressReverseMap(locked)[Addr]; 1878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor assert((V == 0 || GV == 0) && "GlobalMapping already established!"); 188cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall V = GV; 189cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall } 190cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall return OldVal; 191cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall} 192162e1c1b487352434552147967c3dd296ebee2f7Richard Smith 193cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall/// getPointerToGlobalIfAvailable - This returns the address of the specified 194162e1c1b487352434552147967c3dd296ebee2f7Richard Smith/// global value if it is has already been codegen'd, otherwise it returns null. 195162e1c1b487352434552147967c3dd296ebee2f7Richard Smith/// 196cde5a400dbc9655eddf0f383585d3cf67c11c539John McCallvoid *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) { 197d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor MutexGuard locked(lock); 198a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi 199ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor ExecutionEngineState::GlobalAddressMapTy::iterator I = 2007c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor EEState.getGlobalAddressMap(locked).find(GV); 2017c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor return I != EEState.getGlobalAddressMap(locked).end() ? I->second : 0; 202b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor} 203b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor 204a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// getGlobalValueAtAddress - Return the LLVM global value object that starts 2055df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola/// at the specified address. 2065df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola/// 2075df37bd0242e838e465f0bd51a70af424d152053Rafael Espindolaconst GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { 2085df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola MutexGuard locked(lock); 2095df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola 2105df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola // If we haven't computed the reverse mapping yet, do so first. 2115126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall if (EEState.getGlobalAddressReverseMap(locked).empty()) { 2125126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall for (ExecutionEngineState::GlobalAddressMapTy::iterator 2131d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall I = EEState.getGlobalAddressMap(locked).begin(), 214d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor E = EEState.getGlobalAddressMap(locked).end(); I != E; ++I) 21546460a68f6508775e98c19b4bb8454bb471aac24John McCall EEState.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, 2161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump I->first)); 2178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 2188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 2198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor std::map<void *, AssertingVH<const GlobalValue> >::iterator I = 220162e1c1b487352434552147967c3dd296ebee2f7Richard Smith EEState.getGlobalAddressReverseMap(locked).find(Addr); 2213e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : 0; 2223e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith} 2233e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith 224162e1c1b487352434552147967c3dd296ebee2f7Richard Smithnamespace { 225162e1c1b487352434552147967c3dd296ebee2f7Richard Smithclass ArgvArray { 226162e1c1b487352434552147967c3dd296ebee2f7Richard Smith char *Array; 2273e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith std::vector<char*> Values; 2283e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smithpublic: 2293e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith ArgvArray() : Array(NULL) {} 2303e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith ~ArgvArray() { clear(); } 2313e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith void clear() { 2323e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith delete[] Array; 2333e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith Array = NULL; 2343e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith for (size_t I = 0, E = Values.size(); I != E; ++I) { 2353e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith delete[] Values[I]; 2363e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith } 2373e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith Values.clear(); 2383e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith } 2393e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith /// Turn a vector of strings into a nice argv style array of pointers to null 2403e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith /// terminated strings. 2413e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith void *reset(LLVMContext &C, ExecutionEngine *EE, 2423e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith const std::vector<std::string> &InputArgv); 2433e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith}; 2443e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith} // anonymous namespace 2453e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smithvoid *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE, 246ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor const std::vector<std::string> &InputArgv) { 2473e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith clear(); // Free the old contents. 2483e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith unsigned PtrSize = EE->getTargetData()->getPointerSize(); 2493e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith Array = new char[(InputArgv.size()+1)*PtrSize]; 2503e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith 2513e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n"); 2523e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith const Type *SBytePtr = Type::getInt8PtrTy(C); 2533e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith 2543e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith for (unsigned i = 0; i != InputArgv.size(); ++i) { 2553e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith unsigned Size = InputArgv[i].size()+1; 2563e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith char *Dest = new char[Size]; 2573e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith Values.push_back(Dest); 2583e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n"); 2593e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith 2603e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); 2613e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith Dest[Size-1] = 0; 2623e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith 2633e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith // Endian safe: Array[i] = (PointerTy)Dest; 2643e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize), 2653e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith SBytePtr); 2663e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith } 2673e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith 268a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi // Null terminate it 2693e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith EE->StoreValueToMemory(PTOGV(0), 2703e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith (GenericValue*)(Array+InputArgv.size()*PtrSize), 2713e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith SBytePtr); 272162e1c1b487352434552147967c3dd296ebee2f7Richard Smith return Array; 273162e1c1b487352434552147967c3dd296ebee2f7Richard Smith} 2743d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor 2759901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor 2769901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor/// runStaticConstructorsDestructors - This method is used to execute all of 2779901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor/// the static constructors or destructors for a module, depending on the 2789901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor/// value of isDtors. 2799901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregorvoid ExecutionEngine::runStaticConstructorsDestructors(Module *module, 2809901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor bool isDtors) { 2819901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors"; 282ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall 283a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall // Execute global ctors/dtors for each module in the program. 2840a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall 2850a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall GlobalVariable *GV = module->getNamedGlobal(Name); 2860a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall 2870a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall // If this global has internal linkage, or if it has a use, then it must be 2883d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor // an old-style (llvmgcc3) static ctor with __main linked in and in use. If 2893d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor // this is the case, don't execute any of the global ctors, __main will do 290c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor // it. 291c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return; 292c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor 293c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor // Should be an array of '{ int, void ()* }' structs. The first value is 294c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor // the init priority, which we ignore. 295a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 296b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor if (!InitList) return; 2973d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) 298ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara if (ConstantStruct *CS = 2993d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor dyn_cast<ConstantStruct>(InitList->getOperand(i))) { 3000a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. 30116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor 30216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor Constant *FP = CS->getOperand(1); 3033d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor if (FP->isNullValue()) 3045b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl break; // Found a null terminator, exit. 305ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 306796c1a1e3e63e459e371383ac878aa5f40b02a8cRichard Smith if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) 3071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (CE->isCast()) 308b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall FP = CE->getOperand(0); 309b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall if (Function *F = dyn_cast<Function>(FP)) { 310b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall // Execute the ctor/dtor function! 311b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall runFunction(F, std::vector<GenericValue>()); 3121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 3137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor } 3147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor} 3157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 3167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// runStaticConstructorsDestructors - This method is used to execute all of 3171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// the static constructors or destructors for a program, depending on the 31846460a68f6508775e98c19b4bb8454bb471aac24John McCall/// value of isDtors. 319a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumivoid ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) { 3206b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis // Execute global ctors/dtors for each module in the program. 321c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor for (unsigned m = 0, e = Modules.size(); m != e; ++m) 3226b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis runStaticConstructorsDestructors(Modules[m], isDtors); 3236b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis} 324a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi 325390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump#ifndef NDEBUG 326390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump/// isTargetNullPtr - Return whether the target pointer stored at Loc is null. 3276826314938f8510cd1a6b03b5d032592456ae27bJohn McCallstatic bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) { 3286826314938f8510cd1a6b03b5d032592456ae27bJohn McCall unsigned PtrSize = EE->getTargetData()->getPointerSize(); 329449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor for (unsigned i = 0; i < PtrSize; ++i) 33060c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor if (*(i + (uint8_t*)Loc)) 33160c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor return false; 3329aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor return true; 3339aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor} 3344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie#endif 3359aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor 3369aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor/// runFunctionAsMain - This is a helper function which wraps runFunction to 3379aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor/// handle the common task of starting up main with the specified argc, argv, 3382c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain/// and envp parameters. 3391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpint ExecutionEngine::runFunctionAsMain(Function *Fn, 3407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor const std::vector<std::string> &argv, 3413e9ea0b8cd7c4691d62e385245556be5fded58a7Richard Smith const char * const * envp) { 3427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor std::vector<GenericValue> GVArgs; 3437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor GenericValue GVArgc; 3447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor GVArgc.IntVal = APInt(32, argv.size()); 345f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor 346f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor // Check main() type 3477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor unsigned NumArgs = Fn->getFunctionType()->getNumParams(); 348dd5756c04c98e354b85c4f7eb660ae60c6d341ecDeLesley Hutchins const FunctionType *FTy = Fn->getFunctionType(); 349a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi const Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo(); 350251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor switch (NumArgs) { 351251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor case 3: 352251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor if (FTy->getParamType(2) != PPInt8Ty) { 353a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi llvm_report_error("Invalid type for third argument of main() supplied"); 354cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor } 355a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi // FALLS THROUGH 35660c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor case 2: 35760c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor if (FTy->getParamType(1) != PPInt8Ty) { 35860c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor llvm_report_error("Invalid type for second argument of main() supplied"); 3591f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor } 360adb1d4c18ee83249d4cffc99ef902f98e846092aRichard Smith // FALLS THROUGH 3611f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor case 1: 362adb1d4c18ee83249d4cffc99ef902f98e846092aRichard Smith if (!FTy->getParamType(0)->isIntegerTy(32)) { 3631f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor llvm_report_error("Invalid type for first argument of main() supplied"); 3646b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor } 3655b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl // FALLS THROUGH 3665b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl case 0: 3675b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl if (!FTy->getReturnType()->isIntegerTy() && 36834b41d939a1328f484511c6002ba2456db879a29Richard Smith !FTy->getReturnType()->isVoidTy()) { 3695b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl llvm_report_error("Invalid return type of main() supplied"); 3705b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl } 3715b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl break; 3725b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl default: 3735b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl llvm_report_error("Invalid number of arguments of main() supplied"); 3746aeaa60217e1ed11a621409acf1b53df0d14b591Eli Friedman } 3756eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor 3766b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor ArgvArray CArgv; 3776b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor ArgvArray CEnv; 3786b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor if (NumArgs) { 3796eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor GVArgs.push_back(GVArgc); // Arg #0 = argc. 380a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi if (NumArgs > 1) { 3811f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor // Arg #1 = argv. 382ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv))); 383ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) && 384d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall "argv[0] was null after CreateArgv"); 3853d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor if (NumArgs > 2) { 386e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith std::vector<std::string> EnvVars; 387e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith for (unsigned i = 0; envp[i]; ++i) 388e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith EnvVars.push_back(envp[i]); 389e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith // Arg #2 = envp. 3905764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars))); 391bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis } 3923d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor } 3933d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor } 3943d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor return runFunction(Fn, GVArgs).IntVal.getZExtValue(); 3956206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara} 3966206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara 3976206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara/// If possible, create a JIT, unless the caller specifically requests an 3986206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara/// Interpreter or there's an error. If even an Interpreter cannot be created, 3996206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara/// NULL is returned. 4006206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara/// 4016206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraExecutionEngine *ExecutionEngine::create(Module *M, 4026206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara bool ForceInterpreter, 4038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor std::string *ErrorStr, 4048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor CodeGenOpt::Level OptLevel, 405a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall bool GVsWithCode) { 406561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor return EngineBuilder(M) 407836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor .setEngineKind(ForceInterpreter 40807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall ? EngineKind::Interpreter 40907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall : EngineKind::JIT) 41007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall .setErrorStr(ErrorStr) 411a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall .setOptLevel(OptLevel) 41207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall .setAllocateGVsWithCode(GVsWithCode) 41307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall .create(); 4148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 4158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 4168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorExecutionEngine *EngineBuilder::create() { 4178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // Make sure we can resolve symbols in the program as well. The zero arg 4188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // to the function tells DynamicLibrary to load the program, not a library. 4198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr)) 4208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return 0; 42107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall 4228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // If the user specified a memory manager but didn't specify which engine to 4238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // create, we assume they only want the JIT, and we fail if they only want 424b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor // the interpreter. 425b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor if (JMM) { 4268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (WhichEngine & EngineKind::JIT) 4278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor WhichEngine = EngineKind::JIT; 4288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor else { 4298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (ErrorStr) 4308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor *ErrorStr = "Cannot create an interpreter with a memory manager."; 4318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return 0; 432f6702a3927147655206ae729a84339c4fda4c651Richard Smith } 433f6702a3927147655206ae729a84339c4fda4c651Richard Smith } 434f6702a3927147655206ae729a84339c4fda4c651Richard Smith 4351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Unless the interpreter was explicitly selected or the JIT is not linked, 43660d7b3a319d84d688752be3870615ac0f111fb16John McCall // try making a JIT. 437ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall if (WhichEngine & EngineKind::JIT) { 4388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (ExecutionEngine::JITCtor) { 4398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor ExecutionEngine *EE = 4408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor ExecutionEngine::JITCtor(M, ErrorStr, JMM, OptLevel, 4418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor AllocateGVsWithCode, CMModel, 442e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson MArch, MCPU, MAttrs); 4438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (EE) return EE; 4448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 44507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall } 44607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall 4471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // If we can't make a JIT and we didn't request one specifically, try making 4488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // an interpreter instead. 4498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (WhichEngine & EngineKind::Interpreter) { 4508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (ExecutionEngine::InterpCtor) 451ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith return ExecutionEngine::InterpCtor(M, ErrorStr); 452703b6015176550eefc91f3e2f19cd19beacbc592Richard Smith if (ErrorStr) 4538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor *ErrorStr = "Interpreter has not been linked in."; 4548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return 0; 455663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor } 456663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor 457f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson if ((WhichEngine & EngineKind::JIT) && ExecutionEngine::JITCtor == 0) { 458663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor if (ErrorStr) 4591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump *ErrorStr = "JIT has not been linked in."; 46023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins } 461a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi return 0; 462f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson} 463f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson 4641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getPointerToGlobal - This returns the address of the specified global 465f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson/// value. This may involve code generation if it's a function. 466f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson/// 467f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlssonvoid *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { 468a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) 4699901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor return getPointerToFunction(F); 4709901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor 4717a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl MutexGuard locked(lock); 4729901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor void *p = EEState.getGlobalAddressMap(locked)[GV]; 4738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (p) 4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return p; 475f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson 47646460a68f6508775e98c19b4bb8454bb471aac24John McCall // Global variable might have been added since interpreter started. 477f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson if (GlobalVariable *GVar = 4788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV))) 4798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EmitGlobalVariable(GVar); 4808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor else 4818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor llvm_unreachable("Global hasn't had an address allocated yet!"); 48287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet return EEState.getGlobalAddressMap(locked)[GV]; 48387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet} 48487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet 48587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet/// This function converts a Constant* into a GenericValue. The interesting 48687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet/// part is if C is a ConstantExpr. 48787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet/// @brief Get a GenericValue for a Constant* 48887c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetGenericValue ExecutionEngine::getConstantValue(const Constant *C) { 489b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor // If its undefined, return the garbage. 490a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi if (isa<UndefValue>(C)) { 491b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor GenericValue Result; 492b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor switch (C->getType()->getTypeID()) { 493b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor case Type::IntegerTyID: 494a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi case Type::X86_FP80TyID: 495b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor case Type::FP128TyID: 496b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor case Type::PPC_FP128TyID: 49787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet // Although the value is undefined, we still have to construct an APInt 49840e17752086c2c497951d64f5ac6ab5039466113Francois Pichet // with the correct bit width. 49987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0); 50087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet break; 50140e17752086c2c497951d64f5ac6ab5039466113Francois Pichet default: 50287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet break; 50387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet } 50487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet return Result; 50587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet } 50687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet 50787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet // If the value is a ConstantExpr 50887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 50987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet Constant *Op0 = CE->getOperand(0); 51087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet switch (CE->getOpcode()) { 51102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall case Instruction::GetElementPtr: { 51202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // Compute the index 51306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor GenericValue Result = getConstantValue(Op0); 51432f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end()); 5154fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth uint64_t Offset = 5164fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth TD->getIndexedOffset(Op0->getType(), &Indices[0], Indices.size()); 5174fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth 5184fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth char* tmp = (char*) Result.PointerVal; 5194fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth Result = PTOGV(tmp + Offset); 5204fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth return Result; 5214fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth } 5224fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth case Instruction::Trunc: { 5234fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth GenericValue GV = getConstantValue(Op0); 5244fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 5254fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth GV.IntVal = GV.IntVal.trunc(BitWidth); 5264fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth return GV; 52706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor } 528fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall case Instruction::ZExt: { 529d6f80daa84164ceeb8900da07f43b6a150edf713Richard Smith GenericValue GV = getConstantValue(Op0); 5300216df8fd3ce58f5a68ef2ab141ea34c96c11164Abramo Bagnara uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 53106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor GV.IntVal = GV.IntVal.zext(BitWidth); 53206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor return GV; 533a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi } 53406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor case Instruction::SExt: { 5359a34edb710917798aa30263374f624f13b594605John McCall GenericValue GV = getConstantValue(Op0); 53606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 53706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor GV.IntVal = GV.IntVal.sext(BitWidth); 538a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi return GV; 539a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi } 54006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor case Instruction::FPTrunc: { 54106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor // FIXME long double 54232f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall GenericValue GV = getConstantValue(Op0); 543af2094e7cecadf36667deb61a83587ffdd979bd3John McCall GV.FloatVal = float(GV.DoubleVal); 544af2094e7cecadf36667deb61a83587ffdd979bd3John McCall return GV; 545af2094e7cecadf36667deb61a83587ffdd979bd3John McCall } 546af2094e7cecadf36667deb61a83587ffdd979bd3John McCall case Instruction::FPExt:{ 547af2094e7cecadf36667deb61a83587ffdd979bd3John McCall // FIXME long double 54806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor GenericValue GV = getConstantValue(Op0); 5491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump GV.DoubleVal = double(GV.FloatVal); 55002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall return GV; 551a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi } 55206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor case Instruction::UIToFP: { 5535fee110ac106370f75592df024001de73edced2aJohn McCall GenericValue GV = getConstantValue(Op0); 5549a34edb710917798aa30263374f624f13b594605John McCall if (CE->getType()->isFloatTy()) 55502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall GV.FloatVal = float(GV.IntVal.roundToDouble()); 55602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall else if (CE->getType()->isDoubleTy()) 557fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall GV.DoubleVal = GV.IntVal.roundToDouble(); 558fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall else if (CE->getType()->isX86_FP80Ty()) { 5598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor const uint64_t zero[] = {0, 0}; 5608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor APFloat apf = APFloat(APInt(80, 2, zero)); 5611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump (void)apf.convertFromAPInt(GV.IntVal, 562f6702a3927147655206ae729a84339c4fda4c651Richard Smith false, 563f6702a3927147655206ae729a84339c4fda4c651Richard Smith APFloat::rmNearestTiesToEven); 564f6702a3927147655206ae729a84339c4fda4c651Richard Smith GV.IntVal = apf.bitcastToAPInt(); 5651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 56660d7b3a319d84d688752be3870615ac0f111fb16John McCall return GV; 567ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall } 5688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor case Instruction::SIToFP: { 5698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor GenericValue GV = getConstantValue(Op0); 5708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (CE->getType()->isFloatTy()) 571e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith GV.FloatVal = float(GV.IntVal.signedRoundToDouble()); 5729ae2f076ca5ab1feb3ba95629099ec2319833701John McCall else if (CE->getType()->isDoubleTy()) 573e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith GV.DoubleVal = GV.IntVal.signedRoundToDouble(); 574e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith else if (CE->getType()->isX86_FP80Ty()) { 575e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith const uint64_t zero[] = { 0, 0}; 5768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor APFloat apf = APFloat(APInt(80, 2, zero)); 5778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor (void)apf.convertFromAPInt(GV.IntVal, 5788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor true, 57938f0df352fadc546c5666079fb22de5ec1819d92Richard Smith APFloat::rmNearestTiesToEven); 58038f0df352fadc546c5666079fb22de5ec1819d92Richard Smith GV.IntVal = apf.bitcastToAPInt(); 58138f0df352fadc546c5666079fb22de5ec1819d92Richard Smith } 58238f0df352fadc546c5666079fb22de5ec1819d92Richard Smith return GV; 58338f0df352fadc546c5666079fb22de5ec1819d92Richard Smith } 58438f0df352fadc546c5666079fb22de5ec1819d92Richard Smith case Instruction::FPToUI: // double->APInt conversion handles sign 58538f0df352fadc546c5666079fb22de5ec1819d92Richard Smith case Instruction::FPToSI: { 58638f0df352fadc546c5666079fb22de5ec1819d92Richard Smith GenericValue GV = getConstantValue(Op0); 58738f0df352fadc546c5666079fb22de5ec1819d92Richard Smith uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); 588ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara if (Op0->getType()->isFloatTy()) 5898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth); 59038f0df352fadc546c5666079fb22de5ec1819d92Richard Smith else if (Op0->getType()->isDoubleTy()) 591a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth); 5921274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor else if (Op0->getType()->isX86_FP80Ty()) { 593f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith APFloat apf = APFloat(GV.IntVal); 5941274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor uint64_t v; 5951274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor bool ignored; 5961274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor (void)apf.convertToInteger(&v, BitWidth, 5971274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor CE->getOpcode()==Instruction::FPToSI, 598f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith APFloat::rmTowardZero, &ignored); 599f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith GV.IntVal = v; // endian? 600f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith } 6011274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor return GV; 602f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith } 603f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith case Instruction::PtrToInt: { 604f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith GenericValue GV = getConstantValue(Op0); 6051274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor uint32_t PtrWidth = TD->getPointerSizeInBits(); 6061274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal)); 6071274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor return GV; 6081274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor } 6091274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor case Instruction::IntToPtr: { 6101274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor GenericValue GV = getConstantValue(Op0); 6115b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall uint32_t PtrWidth = TD->getPointerSizeInBits(); 6125b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall if (PtrWidth != GV.IntVal.getBitWidth()) 613f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth); 61406c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width"); 615b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue())); 61617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis return GV; 617f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith } 6184ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith case Instruction::BitCast: { 6194ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith GenericValue GV = getConstantValue(Op0); 6204ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith const Type* DestTy = CE->getType(); 6214ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith switch (Op0->getType()->getTypeID()) { 6224ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith default: llvm_unreachable("Invalid bitcast operand"); 6234ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith case Type::IntegerTyID: 6244ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith assert(DestTy->isFloatingPointTy() && "invalid bitcast"); 6254ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith if (DestTy->isFloatTy()) 6264ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith GV.FloatVal = GV.IntVal.bitsToFloat(); 6274ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith else if (DestTy->isDoubleTy()) 6284ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith GV.DoubleVal = GV.IntVal.bitsToDouble(); 6294ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith break; 6304ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith case Type::FloatTyID: 6314ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith assert(DestTy->isIntegerTy(32) && "Invalid bitcast"); 6328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor GV.IntVal.floatToBits(GV.FloatVal); 63396084f171f4824397dc48453146f0a9719cb9247Douglas Gregor break; 63496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor case Type::DoubleTyID: 635a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi assert(DestTy->isIntegerTy(64) && "Invalid bitcast"); 636f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith GV.IntVal.doubleToBits(GV.DoubleVal); 637f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith break; 638f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith case Type::PointerTyID: 639f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith assert(DestTy->isPointerTy() && "Invalid bitcast"); 64038f0df352fadc546c5666079fb22de5ec1819d92Richard Smith break; // getConstantValue(Op0) above already converted it 64138f0df352fadc546c5666079fb22de5ec1819d92Richard Smith } 64238f0df352fadc546c5666079fb22de5ec1819d92Richard Smith return GV; 64338f0df352fadc546c5666079fb22de5ec1819d92Richard Smith } 64438f0df352fadc546c5666079fb22de5ec1819d92Richard Smith case Instruction::Add: 6454ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith case Instruction::FAdd: 646f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith case Instruction::Sub: 647f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith case Instruction::FSub: 648f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith case Instruction::Mul: 649f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith case Instruction::FMul: 650f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith case Instruction::UDiv: 651f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith case Instruction::SDiv: 652f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith case Instruction::URem: 653f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith case Instruction::SRem: 6541af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith case Instruction::And: 6551af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith case Instruction::Or: 6561af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith case Instruction::Xor: { 6575f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner GenericValue LHS = getConstantValue(Op0); 6588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor GenericValue RHS = getConstantValue(CE->getOperand(1)); 6598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor GenericValue GV; 660f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith switch (CE->getOperand(0)->getType()->getTypeID()) { 661f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith default: llvm_unreachable("Bad add type!"); 6628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor case Type::IntegerTyID: 6638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor switch (CE->getOpcode()) { 66460d7b3a319d84d688752be3870615ac0f111fb16John McCall default: llvm_unreachable("Invalid integer opcode"); 665ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break; 666f6702a3927147655206ae729a84339c4fda4c651Richard Smith case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break; 6671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break; 668f6702a3927147655206ae729a84339c4fda4c651Richard Smith case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break; 6691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break; 670ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break; 671ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break; 6728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break; 6738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break; 6748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break; 6758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 6768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor break; 6778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor case Type::FloatTyID: 6788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor switch (CE->getOpcode()) { 6798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor default: llvm_unreachable("Invalid float opcode"); 6801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Instruction::FAdd: 6818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break; 6828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor case Instruction::FSub: 6839ae2f076ca5ab1feb3ba95629099ec2319833701John McCall GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break; 6848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor case Instruction::FMul: 6858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break; 6868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor case Instruction::FDiv: 6878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break; 6888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor case Instruction::FRem: 6898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor GV.FloatVal = ::fmodf(LHS.FloatVal,RHS.FloatVal); break; 6908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 6918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor break; 692581deb3da481053c4993c7600f97acf7768caac5David Blaikie case Type::DoubleTyID: 6935b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall switch (CE->getOpcode()) { 6943b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall default: llvm_unreachable("Invalid double opcode"); 69517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis case Instruction::FAdd: 696d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break; 6978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor case Instruction::FSub: 698a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break; 699f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith case Instruction::FMul: 700f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break; 70196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor case Instruction::FDiv: 70296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break; 703581deb3da481053c4993c7600f97acf7768caac5David Blaikie case Instruction::FRem: 70496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor GV.DoubleVal = ::fmod(LHS.DoubleVal,RHS.DoubleVal); break; 7058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 7068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor break; 7071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Type::X86_FP80TyID: 708f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith case Type::PPC_FP128TyID: 709f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith case Type::FP128TyID: { 710f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith APFloat apfLHS = APFloat(LHS.IntVal); 711de7a0fcdf9f30cb5a97aab614f3975d93cd9926fEli Friedman switch (CE->getOpcode()) { 712fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan default: llvm_unreachable("Invalid long double opcode");llvm_unreachable(0); 7138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor case Instruction::FAdd: 7148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor apfLHS.add(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 7156477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor GV.IntVal = apfLHS.bitcastToAPInt(); 716b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie break; 7176477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor case Instruction::FSub: 7186477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor apfLHS.subtract(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 719e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall GV.IntVal = apfLHS.bitcastToAPInt(); 72093ba8579c341d5329175f1413cdc3b35a36592d2John McCall break; 72193ba8579c341d5329175f1413cdc3b35a36592d2John McCall case Instruction::FMul: 722550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor apfLHS.multiply(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 723550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor GV.IntVal = apfLHS.bitcastToAPInt(); 7242a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall break; 725e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall case Instruction::FDiv: 726ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall apfLHS.divide(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 7271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump GV.IntVal = apfLHS.bitcastToAPInt(); 728d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor break; 729e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall case Instruction::FRem: 730e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall apfLHS.mod(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); 73193ba8579c341d5329175f1413cdc3b35a36592d2John McCall GV.IntVal = apfLHS.bitcastToAPInt(); 73293ba8579c341d5329175f1413cdc3b35a36592d2John McCall break; 73393ba8579c341d5329175f1413cdc3b35a36592d2John McCall } 73493ba8579c341d5329175f1413cdc3b35a36592d2John McCall } 735c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor break; 736c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor } 737c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor return GV; 738c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor } 739c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor default: 740c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor break; 74193ba8579c341d5329175f1413cdc3b35a36592d2John McCall } 74293ba8579c341d5329175f1413cdc3b35a36592d2John McCall std::string msg; 74393ba8579c341d5329175f1413cdc3b35a36592d2John McCall raw_string_ostream Msg(msg); 74493ba8579c341d5329175f1413cdc3b35a36592d2John McCall Msg << "ConstantExpr not handled: " << *CE; 74593ba8579c341d5329175f1413cdc3b35a36592d2John McCall llvm_report_error(Msg.str()); 746ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor } 74737574f55cd637340f651330f5cfda69742880d36Nick Lewycky 74837574f55cd637340f651330f5cfda69742880d36Nick Lewycky GenericValue Result; 74937574f55cd637340f651330f5cfda69742880d36Nick Lewycky switch (C->getType()->getTypeID()) { 75037574f55cd637340f651330f5cfda69742880d36Nick Lewycky case Type::FloatTyID: 75137574f55cd637340f651330f5cfda69742880d36Nick Lewycky Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat(); 75237574f55cd637340f651330f5cfda69742880d36Nick Lewycky break; 75337574f55cd637340f651330f5cfda69742880d36Nick Lewycky case Type::DoubleTyID: 75437574f55cd637340f651330f5cfda69742880d36Nick Lewycky Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble(); 75593ba8579c341d5329175f1413cdc3b35a36592d2John McCall break; 75693ba8579c341d5329175f1413cdc3b35a36592d2John McCall case Type::X86_FP80TyID: 75793ba8579c341d5329175f1413cdc3b35a36592d2John McCall case Type::FP128TyID: 75893ba8579c341d5329175f1413cdc3b35a36592d2John McCall case Type::PPC_FP128TyID: 75993ba8579c341d5329175f1413cdc3b35a36592d2John McCall Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt(); 76093ba8579c341d5329175f1413cdc3b35a36592d2John McCall break; 761c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor case Type::IntegerTyID: 76293ba8579c341d5329175f1413cdc3b35a36592d2John McCall Result.IntVal = cast<ConstantInt>(C)->getValue(); 763c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor break; 76493ba8579c341d5329175f1413cdc3b35a36592d2John McCall case Type::PointerTyID: 76593ba8579c341d5329175f1413cdc3b35a36592d2John McCall if (isa<ConstantPointerNull>(C)) 76693ba8579c341d5329175f1413cdc3b35a36592d2John McCall Result.PointerVal = 0; 76793ba8579c341d5329175f1413cdc3b35a36592d2John McCall else if (const Function *F = dyn_cast<Function>(C)) 76893ba8579c341d5329175f1413cdc3b35a36592d2John McCall Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F))); 76993ba8579c341d5329175f1413cdc3b35a36592d2John McCall else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) 77093ba8579c341d5329175f1413cdc3b35a36592d2John McCall Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV))); 77193ba8579c341d5329175f1413cdc3b35a36592d2John McCall else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) 77293ba8579c341d5329175f1413cdc3b35a36592d2John McCall Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>( 77393ba8579c341d5329175f1413cdc3b35a36592d2John McCall BA->getBasicBlock()))); 77493ba8579c341d5329175f1413cdc3b35a36592d2John McCall else 77593ba8579c341d5329175f1413cdc3b35a36592d2John McCall llvm_unreachable("Unknown constant pointer type!"); 77693ba8579c341d5329175f1413cdc3b35a36592d2John McCall break; 77793ba8579c341d5329175f1413cdc3b35a36592d2John McCall default: 77893ba8579c341d5329175f1413cdc3b35a36592d2John McCall std::string msg; 77993ba8579c341d5329175f1413cdc3b35a36592d2John McCall raw_string_ostream Msg(msg); 78093ba8579c341d5329175f1413cdc3b35a36592d2John McCall Msg << "ERROR: Constant unimplemented for type: " << *C->getType(); 78193ba8579c341d5329175f1413cdc3b35a36592d2John McCall llvm_report_error(Msg.str()); 78293ba8579c341d5329175f1413cdc3b35a36592d2John McCall } 78393ba8579c341d5329175f1413cdc3b35a36592d2John McCall return Result; 784c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor} 78593ba8579c341d5329175f1413cdc3b35a36592d2John McCall 7861eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst 787c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor/// with the integer held in IntVal. 78893ba8579c341d5329175f1413cdc3b35a36592d2John McCallstatic void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, 78993ba8579c341d5329175f1413cdc3b35a36592d2John McCall unsigned StoreBytes) { 79093ba8579c341d5329175f1413cdc3b35a36592d2John McCall assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!"); 791c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor uint8_t *Src = (uint8_t *)IntVal.getRawData(); 79293ba8579c341d5329175f1413cdc3b35a36592d2John McCall 793c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor if (sys::isLittleEndianHost()) 794c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor // Little-endian host - the source is ordered from LSB to MSB. Order the 795c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor // destination from LSB to MSB: Do a straight copy. 796c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor memcpy(Dst, Src, StoreBytes); 797c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor else { 798c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor // Big-endian host - the source is an array of 64 bit words ordered from 799c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination 800c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor // from MSB to LSB: Reverse the word order, but not the bytes in a word. 801a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi while (StoreBytes > sizeof(uint64_t)) { 802c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor StoreBytes -= sizeof(uint64_t); 803c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor // May not be aligned so use memcpy. 804c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor memcpy(Dst + StoreBytes, Src, sizeof(uint64_t)); 805c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor Src += sizeof(uint64_t); 806c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor } 807c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor 808c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes); 809c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor } 810c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor} 811c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor 812c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor/// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr 813c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor/// is the address of the memory at which to store Val, cast to GenericValue *. 814c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor/// It is not a pointer to a GenericValue containing the address at which to 815c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor/// store Val. 816c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregorvoid ExecutionEngine::StoreValueToMemory(const GenericValue &Val, 817c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor GenericValue *Ptr, const Type *Ty) { 818c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor const unsigned StoreBytes = getTargetData()->getTypeStoreSize(Ty); 81993ba8579c341d5329175f1413cdc3b35a36592d2John McCall 82093ba8579c341d5329175f1413cdc3b35a36592d2John McCall switch (Ty->getTypeID()) { 82193ba8579c341d5329175f1413cdc3b35a36592d2John McCall case Type::IntegerTyID: 82293ba8579c341d5329175f1413cdc3b35a36592d2John McCall StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes); 82393ba8579c341d5329175f1413cdc3b35a36592d2John McCall break; 824a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi case Type::FloatTyID: 825c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor *((float*)Ptr) = Val.FloatVal; 826c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor break; 827c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor case Type::DoubleTyID: 828c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor *((double*)Ptr) = Val.DoubleVal; 829c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor break; 830c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor case Type::X86_FP80TyID: 831c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor memcpy(Ptr, Val.IntVal.getRawData(), 10); 83293ba8579c341d5329175f1413cdc3b35a36592d2John McCall break; 83393ba8579c341d5329175f1413cdc3b35a36592d2John McCall case Type::PointerTyID: 83493ba8579c341d5329175f1413cdc3b35a36592d2John McCall // Ensure 64 bit target pointers are fully initialized on 32 bit hosts. 835c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor if (StoreBytes != sizeof(PointerTy)) 836c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor memset(Ptr, 0, StoreBytes); 83793ba8579c341d5329175f1413cdc3b35a36592d2John McCall 83893ba8579c341d5329175f1413cdc3b35a36592d2John McCall *((PointerTy*)Ptr) = Val.PointerVal; 83993ba8579c341d5329175f1413cdc3b35a36592d2John McCall break; 84093ba8579c341d5329175f1413cdc3b35a36592d2John McCall default: 84193ba8579c341d5329175f1413cdc3b35a36592d2John McCall dbgs() << "Cannot store value of type " << *Ty << "!\n"; 842e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall } 84393ba8579c341d5329175f1413cdc3b35a36592d2John McCall 844ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian()) 845ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara // Host and target are different endian - reverse the stored bytes. 846f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr); 847e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall} 848c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor 849c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting 850b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall/// from Src into IntVal, which is assumed to be wide enough and to hold zero. 851e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallstatic void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) { 85293ba8579c341d5329175f1413cdc3b35a36592d2John McCall assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!"); 85393ba8579c341d5329175f1413cdc3b35a36592d2John McCall uint8_t *Dst = (uint8_t *)IntVal.getRawData(); 85493ba8579c341d5329175f1413cdc3b35a36592d2John McCall 855e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall if (sys::isLittleEndianHost()) 856ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall // Little-endian host - the destination must be ordered from LSB to MSB. 85793ba8579c341d5329175f1413cdc3b35a36592d2John McCall // The source is ordered from LSB to MSB: Do a straight copy. 858ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall memcpy(Dst, Src, LoadBytes); 859ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall else { 860ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall // Big-endian - the destination is an array of 64 bit words ordered from 861ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall // LSW to MSW. Each word must be ordered from MSB to LSB. The source is 862ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall // ordered from MSB to LSB: Reverse the word order, but not the bytes in 86393ba8579c341d5329175f1413cdc3b35a36592d2John McCall // a word. 86493ba8579c341d5329175f1413cdc3b35a36592d2John McCall while (LoadBytes > sizeof(uint64_t)) { 86593ba8579c341d5329175f1413cdc3b35a36592d2John McCall LoadBytes -= sizeof(uint64_t); 86693ba8579c341d5329175f1413cdc3b35a36592d2John McCall // May not be aligned so use memcpy. 867e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor memcpy(Dst, Src + LoadBytes, sizeof(uint64_t)); 86837574f55cd637340f651330f5cfda69742880d36Nick Lewycky Dst += sizeof(uint64_t); 86937574f55cd637340f651330f5cfda69742880d36Nick Lewycky } 87093ba8579c341d5329175f1413cdc3b35a36592d2John McCall 871a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes); 872f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor } 8733cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall} 87424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor 875ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall/// FIXME: document 876259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor/// 87793ba8579c341d5329175f1413cdc3b35a36592d2John McCallvoid ExecutionEngine::LoadValueFromMemory(GenericValue &Result, 8781b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith GenericValue *Ptr, 8794c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara const Type *Ty) { 8804c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty); 881e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor 882259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor switch (Ty->getTypeID()) { 883a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi case Type::IntegerTyID: 8844c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara // An APInt with all words initially zero. 8854c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0); 8864c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes); 8874c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara break; 8884c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara case Type::FloatTyID: 889e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Result.FloatVal = *((float*)Ptr); 890d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor break; 891d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor case Type::DoubleTyID: 892d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor Result.DoubleVal = *((double*)Ptr); 893d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor break; 894d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor case Type::PointerTyID: 8955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner Result.PointerVal = *((PointerTy*)Ptr); 896d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor break; 897d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor case Type::X86_FP80TyID: { 898d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor // This is endian dependent, but it will only work on x86 anyway. 899d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor // FIXME: Will not trap if loading a signaling NaN. 900d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor uint64_t y[2]; 901d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor memcpy(y, Ptr, 10); 902e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Result.IntVal = APInt(80, 2, y); 903e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall break; 904e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall } 905d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor default: 9067974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor std::string msg; 9077974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor raw_string_ostream Msg(msg); 908ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Msg << "Cannot load value of type " << *Ty << "!"; 909a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi llvm_report_error(Msg.str()); 910ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor } 911ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor} 912ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor 913ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// InitializeMemory - Recursive function to apply a Constant value into the 914ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specified memory location... 915ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// 916a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumivoid ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { 917ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor DEBUG(dbgs() << "JIT: Initializing " << Addr << " "); 918ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor DEBUG(Init->dump()); 919ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor if (isa<UndefValue>(Init)) { 920ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor return; 921a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) { 922d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor unsigned ElementSize = 923d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor getTargetData()->getTypeAllocSize(CP->getType()->getElementType()); 924d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) 925d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize); 926d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor return; 9277974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor } else if (isa<ConstantAggregateZero>(Init)) { 9287974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor memset(Addr, 0, (size_t)getTargetData()->getTypeAllocSize(Init->getType())); 9297974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor return; 930d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) { 931550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor unsigned ElementSize = 932550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor getTargetData()->getTypeAllocSize(CPA->getType()->getElementType()); 933a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) 934550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); 9352a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall return; 936895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) { 937d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor const StructLayout *SL = 938d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor getTargetData()->getStructLayout(cast<StructType>(CPS->getType())); 9391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) 940d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i)); 941a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi return; 942a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor } else if (Init->getType()->isFirstClassType()) { 943a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor GenericValue Val = getConstantValue(Init); 944a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); 945a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor return; 946a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor } 947a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor 948a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi dbgs() << "Bad Type: " << *Init->getType() << "\n"; 949a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor llvm_unreachable("Unknown constant type to initialize memory with!"); 950a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi} 951a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor 952d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor/// EmitGlobals - Emit all of the global variables to memory, storing their 953d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor/// addresses into GlobalAddress. This must make sure to copy the contents of 9541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// their initializers into the memory. 955d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor/// 956a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumivoid ExecutionEngine::emitGlobals() { 957a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor 95837d68185088947322a97eabdc1c0714b0debd929Douglas Gregor // Loop over all of the global variables in the program, allocating the memory 959a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi // to hold them. If there is more than one module, do a prepass over globals 960a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor // to figure out how the different modules should link together. 961e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall // 962b1a56e767cfb645fcb25027ab728dd5824d92615John McCall std::map<std::pair<std::string, const Type*>, 963b1a56e767cfb645fcb25027ab728dd5824d92615John McCall const GlobalValue*> LinkedGlobalsMap; 964e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall 965e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall if (Modules.size() != 1) { 966e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall for (unsigned m = 0, e = Modules.size(); m != e; ++m) { 967b1a56e767cfb645fcb25027ab728dd5824d92615John McCall Module &M = *Modules[m]; 968a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor for (Module::const_global_iterator I = M.global_begin(), 969a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi E = M.global_end(); I != E; ++I) { 970b1a56e767cfb645fcb25027ab728dd5824d92615John McCall const GlobalValue *GV = I; 9711f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall if (GV->hasLocalLinkage() || GV->isDeclaration() || 972a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor GV->hasAppendingLinkage() || !GV->hasName()) 9731f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall continue;// Ignore external globals and globals with internal linkage. 9741f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall 9751f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall const GlobalValue *&GVEntry = 9761f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; 977b1a56e767cfb645fcb25027ab728dd5824d92615John McCall 978d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // If this is the first time we've seen this global, it is the canonical 979d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // version. 980d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (!GVEntry) { 981d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor GVEntry = GV; 982d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor continue; 983d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor } 984d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor 985ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor // If the existing global is strong, never replace it. 9867c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor if (GVEntry->hasExternalLinkage() || 987ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor GVEntry->hasDLLImportLinkage() || 9886c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall GVEntry->hasDLLExportLinkage()) 9896c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall continue; 9906c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall 9916c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall // Otherwise, we know it's linkonce/weak, replace it if this is a strong 992d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor // symbol. FIXME is this right for common? 993d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage()) 9941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump GVEntry = GV; 995ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara } 996ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara } 997b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall } 998b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall 999b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall std::vector<const GlobalValue*> NonCanonicalGlobals; 1000b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall for (unsigned m = 0, e = Modules.size(); m != e; ++m) { 1001b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall Module &M = *Modules[m]; 1002d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 1003eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman I != E; ++I) { 1004eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman // In the multi-module case, see what this global maps to. 1005eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman if (!LinkedGlobalsMap.empty()) { 1006eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman if (const GlobalValue *GVEntry = 1007eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) { 1008d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor // If something else is the canonical global, ignore this one. 1009f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor if (GVEntry != &*I) { 1010d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor NonCanonicalGlobals.push_back(I); 101102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall continue; 101202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall } 101302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall } 101402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall } 101502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall 10169901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor if (!I->isDeclaration()) { 10179901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor addGlobalMapping(I, getMemoryForGV(I)); 10189901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor } else { 10197a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl // External variable reference. Try to use the dynamic loader to 10209901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor // get a pointer to it. 10219901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor if (void *SymAddr = 1022d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName())) 102317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis addGlobalMapping(I, SymAddr); 1024d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor else { 1025d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor llvm_report_error("Could not resolve external global address: " 1026d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor +I->getName()); 102771074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor } 102871074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor } 102971074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor } 103071074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor 103171074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor // If there are multiple modules, map the non-canonical globals to their 103271074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor // canonical location. 103371074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor if (!NonCanonicalGlobals.empty()) { 103471074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) { 103571074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor const GlobalValue *GV = NonCanonicalGlobals[i]; 1036bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor const GlobalValue *CGV = 1037bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; 1038bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor void *Ptr = getPointerToGlobalIfAvailable(CGV); 1039bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor assert(Ptr && "Canonical global wasn't codegen'd!"); 1040bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor addGlobalMapping(GV, Ptr); 1041bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor } 1042bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor } 1043bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor 1044bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor // Now that all of the globals are set up in memory, loop through them all 1045bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor // and initialize their contents. 1046bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 1047bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor I != E; ++I) { 1048bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor if (!I->isDeclaration()) { 104971074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor if (!LinkedGlobalsMap.empty()) { 105071074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor if (const GlobalValue *GVEntry = 105102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) 105202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (GVEntry != &*I) // Not the canonical variable. 105302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall continue; 105402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall } 105502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall EmitGlobalVariable(I); 10567557a1348d2821dce126a778aa7acd7a00b814fdDouglas Gregor } 1057a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor } 1058127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor } 1059127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor} 1060127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor 1061b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall// EmitGlobalVariable - This method emits the specified global variable to the 1062a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi// address specified in GlobalAddresses, or allocates new memory if it's not 106324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor// already in the map. 10641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { 10651e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor void *GA = getPointerToGlobalIfAvailable(GV); 10662c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis 10672c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis if (GA == 0) { 10682c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis // If it's not already specified, allocate memory for the global. 10691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump GA = getMemoryForGV(GV); 1070127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor addGlobalMapping(GV, GA); 10712c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis } 10722c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis 1073127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor // Don't initialize if it's thread local, let the client do it. 10741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (!GV->isThreadLocal()) 1075b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall InitializeMemory(GV->getInitializer(), GA); 1076b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall 1077b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall const Type *ElTy = GV->getType()->getElementType(); 1078b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy); 1079b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall NumInitBytes += (unsigned)GVSize; 1080b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall ++NumGlobals; 108179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor} 1082b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor 1083a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA TakumiExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE) 108479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor : EE(EE), GlobalAddressMap(this) { 10852a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall} 10861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnersys::Mutex *ExecutionEngineState::AddressMapConfig::getMutex( 108864b4b43a23aa8b8009470e3cc451333f623d7d58David Blaikie ExecutionEngineState *EES) { 108921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall return &EES->EE.lock; 10902dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor} 109171074fdf40a8f5b53810712102b58c27efc30759Douglas Gregorvoid ExecutionEngineState::AddressMapConfig::onDelete( 1092fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall ExecutionEngineState *EES, const GlobalValue *Old) { 1093c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor void *OldVal = EES->GlobalAddressMap.lookup(Old); 1094c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor EES->GlobalAddressReverseMap.erase(OldVal); 1095c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor} 1096c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor 1097c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid ExecutionEngineState::AddressMapConfig::onRAUW( 1098c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor ExecutionEngineState *, const GlobalValue *, const GlobalValue *) { 1099d325daa506338ab86f9dd468b48fd010673f49a6John McCall assert(false && "The ExecutionEngine doesn't know how to handle a" 1100d325daa506338ab86f9dd468b48fd010673f49a6John McCall " RAUW on a value it has a global mapping for."); 110168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall} 110268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall