Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the common interface used by the various execution engine |
| 11 | // subclasses. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "jit" |
Jeffrey Yasskin | 291d66a | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 17 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Module.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 21 | #include "llvm/ExecutionEngine/GenericValue.h" |
Chris Lattner | 5ea5b5c | 2009-08-23 22:49:13 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Edwin Török | f7cbfea | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MutexGuard.h" |
Jeffrey Yasskin | 291d66a | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ValueHandle.h" |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 28 | #include "llvm/System/DynamicLibrary.h" |
Duncan Sands | 2e6d342 | 2007-12-12 23:03:45 +0000 | [diff] [blame] | 29 | #include "llvm/System/Host.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetData.h" |
Anton Korobeynikov | 357a27d | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 31 | #include <cmath> |
| 32 | #include <cstring> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
| 35 | STATISTIC(NumInitBytes, "Number of bytes of global vars initialized"); |
| 36 | STATISTIC(NumGlobals , "Number of global vars initialized"); |
| 37 | |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 38 | ExecutionEngine *(*ExecutionEngine::JITCtor)(Module *M, |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 39 | std::string *ErrorStr, |
| 40 | JITMemoryManager *JMM, |
| 41 | CodeGenOpt::Level OptLevel, |
Eric Christopher | b064d5e | 2009-11-17 21:58:16 +0000 | [diff] [blame] | 42 | bool GVsWithCode, |
| 43 | CodeModel::Model CMM) = 0; |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 44 | ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M, |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 45 | std::string *ErrorStr) = 0; |
Nicolas Geoffray | 0e757e1 | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 46 | ExecutionEngine::EERegisterFn ExecutionEngine::ExceptionTableRegister = 0; |
| 47 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 48 | |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 49 | ExecutionEngine::ExecutionEngine(Module *M) |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 50 | : EEState(*this), |
| 51 | LazyFunctionCreator(0) { |
Jeffrey Yasskin | 4f6ac2f | 2009-10-27 20:30:28 +0000 | [diff] [blame] | 52 | CompilingLazily = false; |
Evan Cheng | 9b8bc83 | 2008-09-24 16:25:55 +0000 | [diff] [blame] | 53 | GVCompilationDisabled = false; |
Evan Cheng | 5c62a69 | 2008-06-17 16:49:02 +0000 | [diff] [blame] | 54 | SymbolSearchingDisabled = false; |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 55 | Modules.push_back(M); |
| 56 | assert(M && "Module is null?"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 59 | ExecutionEngine::~ExecutionEngine() { |
| 60 | clearAllGlobalMappings(); |
| 61 | for (unsigned i = 0, e = Modules.size(); i != e; ++i) |
| 62 | delete Modules[i]; |
| 63 | } |
| 64 | |
Nicolas Geoffray | 46fa153 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 65 | char* ExecutionEngine::getMemoryForGV(const GlobalVariable* GV) { |
| 66 | const Type *ElTy = GV->getType()->getElementType(); |
Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 67 | size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy); |
Nicolas Geoffray | 46fa153 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 68 | return new char[GVSize]; |
| 69 | } |
| 70 | |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 71 | /// removeModule - Remove a Module from the list of modules. |
| 72 | bool ExecutionEngine::removeModule(Module *M) { |
| 73 | for(SmallVector<Module *, 1>::iterator I = Modules.begin(), |
Devang Patel | 5d0d0d0 | 2007-10-15 19:56:32 +0000 | [diff] [blame] | 74 | E = Modules.end(); I != E; ++I) { |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 75 | Module *Found = *I; |
| 76 | if (Found == M) { |
Devang Patel | 5d0d0d0 | 2007-10-15 19:56:32 +0000 | [diff] [blame] | 77 | Modules.erase(I); |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 78 | clearGlobalMappingsFromModule(M); |
| 79 | return true; |
Devang Patel | 5d0d0d0 | 2007-10-15 19:56:32 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 82 | return false; |
Nate Begeman | b34045e | 2009-01-23 19:27:28 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 85 | /// FindFunctionNamed - Search all of the active modules to find the one that |
| 86 | /// defines FnName. This is very slow operation and shouldn't be used for |
| 87 | /// general code. |
| 88 | Function *ExecutionEngine::FindFunctionNamed(const char *FnName) { |
| 89 | for (unsigned i = 0, e = Modules.size(); i != e; ++i) { |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 90 | if (Function *F = Modules[i]->getFunction(FnName)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 91 | return F; |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | |
Jeffrey Yasskin | 43304d3 | 2009-10-09 22:10:27 +0000 | [diff] [blame] | 97 | void *ExecutionEngineState::RemoveMapping( |
| 98 | const MutexGuard &, const GlobalValue *ToUnmap) { |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 99 | GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap); |
Jeffrey Yasskin | 43304d3 | 2009-10-09 22:10:27 +0000 | [diff] [blame] | 100 | void *OldVal; |
| 101 | if (I == GlobalAddressMap.end()) |
| 102 | OldVal = 0; |
| 103 | else { |
| 104 | OldVal = I->second; |
| 105 | GlobalAddressMap.erase(I); |
| 106 | } |
| 107 | |
| 108 | GlobalAddressReverseMap.erase(OldVal); |
| 109 | return OldVal; |
| 110 | } |
| 111 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 112 | /// addGlobalMapping - Tell the execution engine that the specified global is |
| 113 | /// at the specified location. This is used internally as functions are JIT'd |
| 114 | /// and as global variables are laid out in memory. It can and should also be |
| 115 | /// used by clients of the EE that want to have an LLVM global overlay |
| 116 | /// existing data in memory. |
| 117 | void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) { |
| 118 | MutexGuard locked(lock); |
Evan Cheng | b83f697 | 2008-09-18 07:54:21 +0000 | [diff] [blame] | 119 | |
David Greene | 7050bcc | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 120 | DEBUG(dbgs() << "JIT: Map \'" << GV->getName() |
Daniel Dunbar | 23e2b80 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 121 | << "\' to [" << Addr << "]\n";); |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 122 | void *&CurVal = EEState.getGlobalAddressMap(locked)[GV]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 123 | assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!"); |
| 124 | CurVal = Addr; |
| 125 | |
| 126 | // If we are using the reverse mapping, add it too |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 127 | if (!EEState.getGlobalAddressReverseMap(locked).empty()) { |
Jeffrey Yasskin | 291d66a | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 128 | AssertingVH<const GlobalValue> &V = |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 129 | EEState.getGlobalAddressReverseMap(locked)[Addr]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 130 | assert((V == 0 || GV == 0) && "GlobalMapping already established!"); |
| 131 | V = GV; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// clearAllGlobalMappings - Clear all global mappings and start over again |
| 136 | /// use in dynamic compilation scenarios when you want to move globals |
| 137 | void ExecutionEngine::clearAllGlobalMappings() { |
| 138 | MutexGuard locked(lock); |
| 139 | |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 140 | EEState.getGlobalAddressMap(locked).clear(); |
| 141 | EEState.getGlobalAddressReverseMap(locked).clear(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Nate Begeman | f7113d9 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 144 | /// clearGlobalMappingsFromModule - Clear all global mappings that came from a |
| 145 | /// particular module, because it has been removed from the JIT. |
| 146 | void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) { |
| 147 | MutexGuard locked(lock); |
| 148 | |
| 149 | for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) { |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 150 | EEState.RemoveMapping(locked, FI); |
Nate Begeman | f7113d9 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 151 | } |
| 152 | for (Module::global_iterator GI = M->global_begin(), GE = M->global_end(); |
| 153 | GI != GE; ++GI) { |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 154 | EEState.RemoveMapping(locked, GI); |
Nate Begeman | f7113d9 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 158 | /// updateGlobalMapping - Replace an existing mapping for GV with a new |
| 159 | /// address. This updates both maps as required. If "Addr" is null, the |
| 160 | /// entry for the global is removed from the mappings. |
Chris Lattner | fb3f0f8 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 161 | void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 162 | MutexGuard locked(lock); |
Chris Lattner | fb3f0f8 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 163 | |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 164 | ExecutionEngineState::GlobalAddressMapTy &Map = |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 165 | EEState.getGlobalAddressMap(locked); |
Chris Lattner | fb3f0f8 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 166 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 167 | // Deleting from the mapping? |
| 168 | if (Addr == 0) { |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 169 | return EEState.RemoveMapping(locked, GV); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 172 | void *&CurVal = Map[GV]; |
Chris Lattner | fb3f0f8 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 173 | void *OldVal = CurVal; |
| 174 | |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 175 | if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty()) |
| 176 | EEState.getGlobalAddressReverseMap(locked).erase(CurVal); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 177 | CurVal = Addr; |
| 178 | |
| 179 | // If we are using the reverse mapping, add it too |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 180 | if (!EEState.getGlobalAddressReverseMap(locked).empty()) { |
Jeffrey Yasskin | 291d66a | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 181 | AssertingVH<const GlobalValue> &V = |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 182 | EEState.getGlobalAddressReverseMap(locked)[Addr]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 183 | assert((V == 0 || GV == 0) && "GlobalMapping already established!"); |
| 184 | V = GV; |
| 185 | } |
Chris Lattner | fb3f0f8 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 186 | return OldVal; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /// getPointerToGlobalIfAvailable - This returns the address of the specified |
| 190 | /// global value if it is has already been codegen'd, otherwise it returns null. |
| 191 | /// |
| 192 | void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) { |
| 193 | MutexGuard locked(lock); |
| 194 | |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 195 | ExecutionEngineState::GlobalAddressMapTy::iterator I = |
| 196 | EEState.getGlobalAddressMap(locked).find(GV); |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 197 | return I != EEState.getGlobalAddressMap(locked).end() ? I->second : 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /// getGlobalValueAtAddress - Return the LLVM global value object that starts |
| 201 | /// at the specified address. |
| 202 | /// |
| 203 | const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { |
| 204 | MutexGuard locked(lock); |
| 205 | |
| 206 | // If we haven't computed the reverse mapping yet, do so first. |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 207 | if (EEState.getGlobalAddressReverseMap(locked).empty()) { |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 208 | for (ExecutionEngineState::GlobalAddressMapTy::iterator |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 209 | I = EEState.getGlobalAddressMap(locked).begin(), |
| 210 | E = EEState.getGlobalAddressMap(locked).end(); I != E; ++I) |
| 211 | EEState.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 212 | I->first)); |
| 213 | } |
| 214 | |
Jeffrey Yasskin | 291d66a | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 215 | std::map<void *, AssertingVH<const GlobalValue> >::iterator I = |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 216 | EEState.getGlobalAddressReverseMap(locked).find(Addr); |
| 217 | return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // CreateArgv - Turn a vector of strings into a nice argv style array of |
| 221 | // pointers to null terminated strings. |
| 222 | // |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 223 | static void *CreateArgv(LLVMContext &C, ExecutionEngine *EE, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 224 | const std::vector<std::string> &InputArgv) { |
| 225 | unsigned PtrSize = EE->getTargetData()->getPointerSize(); |
| 226 | char *Result = new char[(InputArgv.size()+1)*PtrSize]; |
| 227 | |
David Greene | 7050bcc | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 228 | DEBUG(dbgs() << "JIT: ARGV = " << (void*)Result << "\n"); |
Duncan Sands | f2519d6 | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 229 | const Type *SBytePtr = Type::getInt8PtrTy(C); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 230 | |
| 231 | for (unsigned i = 0; i != InputArgv.size(); ++i) { |
| 232 | unsigned Size = InputArgv[i].size()+1; |
| 233 | char *Dest = new char[Size]; |
David Greene | 7050bcc | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 234 | DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 235 | |
| 236 | std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); |
| 237 | Dest[Size-1] = 0; |
| 238 | |
| 239 | // Endian safe: Result[i] = (PointerTy)Dest; |
| 240 | EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize), |
| 241 | SBytePtr); |
| 242 | } |
| 243 | |
| 244 | // Null terminate it |
| 245 | EE->StoreValueToMemory(PTOGV(0), |
| 246 | (GenericValue*)(Result+InputArgv.size()*PtrSize), |
| 247 | SBytePtr); |
| 248 | return Result; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | /// runStaticConstructorsDestructors - This method is used to execute all of |
Evan Cheng | 50a5882 | 2008-09-30 15:51:21 +0000 | [diff] [blame] | 253 | /// the static constructors or destructors for a module, depending on the |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 254 | /// value of isDtors. |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 255 | void ExecutionEngine::runStaticConstructorsDestructors(Module *module, |
| 256 | bool isDtors) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 257 | const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors"; |
| 258 | |
| 259 | // Execute global ctors/dtors for each module in the program. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 260 | |
Evan Cheng | 50a5882 | 2008-09-30 15:51:21 +0000 | [diff] [blame] | 261 | GlobalVariable *GV = module->getNamedGlobal(Name); |
| 262 | |
| 263 | // If this global has internal linkage, or if it has a use, then it must be |
| 264 | // an old-style (llvmgcc3) static ctor with __main linked in and in use. If |
| 265 | // this is the case, don't execute any of the global ctors, __main will do |
| 266 | // it. |
Rafael Espindola | a168fc9 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 267 | if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return; |
Evan Cheng | 50a5882 | 2008-09-30 15:51:21 +0000 | [diff] [blame] | 268 | |
| 269 | // Should be an array of '{ int, void ()* }' structs. The first value is |
| 270 | // the init priority, which we ignore. |
| 271 | ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 272 | if (!InitList) return; |
| 273 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 274 | if (ConstantStruct *CS = |
| 275 | dyn_cast<ConstantStruct>(InitList->getOperand(i))) { |
| 276 | if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. |
| 277 | |
| 278 | Constant *FP = CS->getOperand(1); |
| 279 | if (FP->isNullValue()) |
| 280 | break; // Found a null terminator, exit. |
| 281 | |
| 282 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) |
| 283 | if (CE->isCast()) |
| 284 | FP = CE->getOperand(0); |
| 285 | if (Function *F = dyn_cast<Function>(FP)) { |
| 286 | // Execute the ctor/dtor function! |
| 287 | runFunction(F, std::vector<GenericValue>()); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /// runStaticConstructorsDestructors - This method is used to execute all of |
| 293 | /// the static constructors or destructors for a program, depending on the |
| 294 | /// value of isDtors. |
| 295 | void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) { |
| 296 | // Execute global ctors/dtors for each module in the program. |
| 297 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 298 | runStaticConstructorsDestructors(Modules[m], isDtors); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Dan Gohman | c7e1ad0 | 2008-08-26 01:38:29 +0000 | [diff] [blame] | 301 | #ifndef NDEBUG |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 302 | /// isTargetNullPtr - Return whether the target pointer stored at Loc is null. |
| 303 | static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) { |
| 304 | unsigned PtrSize = EE->getTargetData()->getPointerSize(); |
| 305 | for (unsigned i = 0; i < PtrSize; ++i) |
| 306 | if (*(i + (uint8_t*)Loc)) |
| 307 | return false; |
| 308 | return true; |
| 309 | } |
Dan Gohman | c7e1ad0 | 2008-08-26 01:38:29 +0000 | [diff] [blame] | 310 | #endif |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 311 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 312 | /// runFunctionAsMain - This is a helper function which wraps runFunction to |
| 313 | /// handle the common task of starting up main with the specified argc, argv, |
| 314 | /// and envp parameters. |
| 315 | int ExecutionEngine::runFunctionAsMain(Function *Fn, |
| 316 | const std::vector<std::string> &argv, |
| 317 | const char * const * envp) { |
| 318 | std::vector<GenericValue> GVArgs; |
| 319 | GenericValue GVArgc; |
| 320 | GVArgc.IntVal = APInt(32, argv.size()); |
| 321 | |
| 322 | // Check main() type |
| 323 | unsigned NumArgs = Fn->getFunctionType()->getNumParams(); |
| 324 | const FunctionType *FTy = Fn->getFunctionType(); |
Benjamin Kramer | f2052d5 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 325 | const Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 326 | switch (NumArgs) { |
| 327 | case 3: |
| 328 | if (FTy->getParamType(2) != PPInt8Ty) { |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 329 | llvm_report_error("Invalid type for third argument of main() supplied"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 330 | } |
| 331 | // FALLS THROUGH |
| 332 | case 2: |
| 333 | if (FTy->getParamType(1) != PPInt8Ty) { |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 334 | llvm_report_error("Invalid type for second argument of main() supplied"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 335 | } |
| 336 | // FALLS THROUGH |
| 337 | case 1: |
Benjamin Kramer | f0e83d2 | 2010-01-05 21:05:54 +0000 | [diff] [blame] | 338 | if (!FTy->getParamType(0)->isInteger(32)) { |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 339 | llvm_report_error("Invalid type for first argument of main() supplied"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 340 | } |
| 341 | // FALLS THROUGH |
| 342 | case 0: |
Chris Lattner | 39bc61c | 2009-02-04 17:48:18 +0000 | [diff] [blame] | 343 | if (!isa<IntegerType>(FTy->getReturnType()) && |
Benjamin Kramer | f2052d5 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 344 | !FTy->getReturnType()->isVoidTy()) { |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 345 | llvm_report_error("Invalid return type of main() supplied"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 346 | } |
| 347 | break; |
| 348 | default: |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 349 | llvm_report_error("Invalid number of arguments of main() supplied"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | if (NumArgs) { |
| 353 | GVArgs.push_back(GVArgc); // Arg #0 = argc. |
| 354 | if (NumArgs > 1) { |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 355 | // Arg #1 = argv. |
| 356 | GVArgs.push_back(PTOGV(CreateArgv(Fn->getContext(), this, argv))); |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 357 | assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 358 | "argv[0] was null after CreateArgv"); |
| 359 | if (NumArgs > 2) { |
| 360 | std::vector<std::string> EnvVars; |
| 361 | for (unsigned i = 0; envp[i]; ++i) |
| 362 | EnvVars.push_back(envp[i]); |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 363 | // Arg #2 = envp. |
| 364 | GVArgs.push_back(PTOGV(CreateArgv(Fn->getContext(), this, EnvVars))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | } |
| 368 | return runFunction(Fn, GVArgs).IntVal.getZExtValue(); |
| 369 | } |
| 370 | |
| 371 | /// If possible, create a JIT, unless the caller specifically requests an |
| 372 | /// Interpreter or there's an error. If even an Interpreter cannot be created, |
| 373 | /// NULL is returned. |
| 374 | /// |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 375 | ExecutionEngine *ExecutionEngine::create(Module *M, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 376 | bool ForceInterpreter, |
Evan Cheng | a6394fc | 2008-08-08 08:11:34 +0000 | [diff] [blame] | 377 | std::string *ErrorStr, |
Jeffrey Yasskin | 892956a | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 378 | CodeGenOpt::Level OptLevel, |
| 379 | bool GVsWithCode) { |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 380 | return EngineBuilder(M) |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 381 | .setEngineKind(ForceInterpreter |
| 382 | ? EngineKind::Interpreter |
| 383 | : EngineKind::JIT) |
| 384 | .setErrorStr(ErrorStr) |
| 385 | .setOptLevel(OptLevel) |
| 386 | .setAllocateGVsWithCode(GVsWithCode) |
| 387 | .create(); |
| 388 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 389 | |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 390 | ExecutionEngine *ExecutionEngine::create(Module *M) { |
| 391 | return EngineBuilder(M).create(); |
| 392 | } |
| 393 | |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 394 | ExecutionEngine *EngineBuilder::create() { |
Nick Lewycky | baa3a03 | 2008-03-08 02:49:45 +0000 | [diff] [blame] | 395 | // Make sure we can resolve symbols in the program as well. The zero arg |
| 396 | // to the function tells DynamicLibrary to load the program, not a library. |
| 397 | if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr)) |
| 398 | return 0; |
| 399 | |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 400 | // If the user specified a memory manager but didn't specify which engine to |
| 401 | // create, we assume they only want the JIT, and we fail if they only want |
| 402 | // the interpreter. |
| 403 | if (JMM) { |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 404 | if (WhichEngine & EngineKind::JIT) |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 405 | WhichEngine = EngineKind::JIT; |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 406 | else { |
Chris Lattner | 03344a2 | 2009-09-23 02:03:49 +0000 | [diff] [blame] | 407 | if (ErrorStr) |
| 408 | *ErrorStr = "Cannot create an interpreter with a memory manager."; |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 409 | return 0; |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 412 | |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 413 | // Unless the interpreter was explicitly selected or the JIT is not linked, |
| 414 | // try making a JIT. |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 415 | if (WhichEngine & EngineKind::JIT) { |
| 416 | if (ExecutionEngine::JITCtor) { |
| 417 | ExecutionEngine *EE = |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 418 | ExecutionEngine::JITCtor(M, ErrorStr, JMM, OptLevel, |
Eric Christopher | b064d5e | 2009-11-17 21:58:16 +0000 | [diff] [blame] | 419 | AllocateGVsWithCode, CMModel); |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 420 | if (EE) return EE; |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 421 | } |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // If we can't make a JIT and we didn't request one specifically, try making |
| 425 | // an interpreter instead. |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 426 | if (WhichEngine & EngineKind::Interpreter) { |
| 427 | if (ExecutionEngine::InterpCtor) |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 428 | return ExecutionEngine::InterpCtor(M, ErrorStr); |
Chris Lattner | 03344a2 | 2009-09-23 02:03:49 +0000 | [diff] [blame] | 429 | if (ErrorStr) |
| 430 | *ErrorStr = "Interpreter has not been linked in."; |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 431 | return 0; |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 432 | } |
Chris Lattner | 03344a2 | 2009-09-23 02:03:49 +0000 | [diff] [blame] | 433 | |
| 434 | if ((WhichEngine & EngineKind::JIT) && ExecutionEngine::JITCtor == 0) { |
| 435 | if (ErrorStr) |
| 436 | *ErrorStr = "JIT has not been linked in."; |
| 437 | } |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 438 | return 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /// getPointerToGlobal - This returns the address of the specified global |
| 442 | /// value. This may involve code generation if it's a function. |
| 443 | /// |
| 444 | void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { |
| 445 | if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) |
| 446 | return getPointerToFunction(F); |
| 447 | |
| 448 | MutexGuard locked(lock); |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 449 | void *p = EEState.getGlobalAddressMap(locked)[GV]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 450 | if (p) |
| 451 | return p; |
| 452 | |
| 453 | // Global variable might have been added since interpreter started. |
| 454 | if (GlobalVariable *GVar = |
| 455 | const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV))) |
| 456 | EmitGlobalVariable(GVar); |
| 457 | else |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 458 | llvm_unreachable("Global hasn't had an address allocated yet!"); |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 459 | return EEState.getGlobalAddressMap(locked)[GV]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /// This function converts a Constant* into a GenericValue. The interesting |
| 463 | /// part is if C is a ConstantExpr. |
Reid Spencer | 10ffdf1 | 2007-08-11 15:57:56 +0000 | [diff] [blame] | 464 | /// @brief Get a GenericValue for a Constant* |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 465 | GenericValue ExecutionEngine::getConstantValue(const Constant *C) { |
| 466 | // If its undefined, return the garbage. |
Jay Foad | 1c91e67 | 2010-01-15 08:32:58 +0000 | [diff] [blame] | 467 | if (isa<UndefValue>(C)) { |
| 468 | GenericValue Result; |
| 469 | switch (C->getType()->getTypeID()) { |
| 470 | case Type::IntegerTyID: |
| 471 | case Type::X86_FP80TyID: |
| 472 | case Type::FP128TyID: |
| 473 | case Type::PPC_FP128TyID: |
| 474 | // Although the value is undefined, we still have to construct an APInt |
| 475 | // with the correct bit width. |
| 476 | Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0); |
| 477 | break; |
| 478 | default: |
| 479 | break; |
| 480 | } |
| 481 | return Result; |
| 482 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 483 | |
| 484 | // If the value is a ConstantExpr |
| 485 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 486 | Constant *Op0 = CE->getOperand(0); |
| 487 | switch (CE->getOpcode()) { |
| 488 | case Instruction::GetElementPtr: { |
| 489 | // Compute the index |
| 490 | GenericValue Result = getConstantValue(Op0); |
| 491 | SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end()); |
| 492 | uint64_t Offset = |
| 493 | TD->getIndexedOffset(Op0->getType(), &Indices[0], Indices.size()); |
| 494 | |
| 495 | char* tmp = (char*) Result.PointerVal; |
| 496 | Result = PTOGV(tmp + Offset); |
| 497 | return Result; |
| 498 | } |
| 499 | case Instruction::Trunc: { |
| 500 | GenericValue GV = getConstantValue(Op0); |
| 501 | uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); |
| 502 | GV.IntVal = GV.IntVal.trunc(BitWidth); |
| 503 | return GV; |
| 504 | } |
| 505 | case Instruction::ZExt: { |
| 506 | GenericValue GV = getConstantValue(Op0); |
| 507 | uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); |
| 508 | GV.IntVal = GV.IntVal.zext(BitWidth); |
| 509 | return GV; |
| 510 | } |
| 511 | case Instruction::SExt: { |
| 512 | GenericValue GV = getConstantValue(Op0); |
| 513 | uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); |
| 514 | GV.IntVal = GV.IntVal.sext(BitWidth); |
| 515 | return GV; |
| 516 | } |
| 517 | case Instruction::FPTrunc: { |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 518 | // FIXME long double |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 519 | GenericValue GV = getConstantValue(Op0); |
| 520 | GV.FloatVal = float(GV.DoubleVal); |
| 521 | return GV; |
| 522 | } |
| 523 | case Instruction::FPExt:{ |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 524 | // FIXME long double |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 525 | GenericValue GV = getConstantValue(Op0); |
| 526 | GV.DoubleVal = double(GV.FloatVal); |
| 527 | return GV; |
| 528 | } |
| 529 | case Instruction::UIToFP: { |
| 530 | GenericValue GV = getConstantValue(Op0); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 531 | if (CE->getType()->isFloatTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 532 | GV.FloatVal = float(GV.IntVal.roundToDouble()); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 533 | else if (CE->getType()->isDoubleTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 534 | GV.DoubleVal = GV.IntVal.roundToDouble(); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 535 | else if (CE->getType()->isX86_FP80Ty()) { |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 536 | const uint64_t zero[] = {0, 0}; |
| 537 | APFloat apf = APFloat(APInt(80, 2, zero)); |
Dan Gohman | 8faf868 | 2008-02-29 01:27:13 +0000 | [diff] [blame] | 538 | (void)apf.convertFromAPInt(GV.IntVal, |
| 539 | false, |
| 540 | APFloat::rmNearestTiesToEven); |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 541 | GV.IntVal = apf.bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 542 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 543 | return GV; |
| 544 | } |
| 545 | case Instruction::SIToFP: { |
| 546 | GenericValue GV = getConstantValue(Op0); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 547 | if (CE->getType()->isFloatTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 548 | GV.FloatVal = float(GV.IntVal.signedRoundToDouble()); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 549 | else if (CE->getType()->isDoubleTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 550 | GV.DoubleVal = GV.IntVal.signedRoundToDouble(); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 551 | else if (CE->getType()->isX86_FP80Ty()) { |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 552 | const uint64_t zero[] = { 0, 0}; |
| 553 | APFloat apf = APFloat(APInt(80, 2, zero)); |
Dan Gohman | 8faf868 | 2008-02-29 01:27:13 +0000 | [diff] [blame] | 554 | (void)apf.convertFromAPInt(GV.IntVal, |
| 555 | true, |
| 556 | APFloat::rmNearestTiesToEven); |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 557 | GV.IntVal = apf.bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 558 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 559 | return GV; |
| 560 | } |
| 561 | case Instruction::FPToUI: // double->APInt conversion handles sign |
| 562 | case Instruction::FPToSI: { |
| 563 | GenericValue GV = getConstantValue(Op0); |
| 564 | uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 565 | if (Op0->getType()->isFloatTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 566 | GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 567 | else if (Op0->getType()->isDoubleTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 568 | GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 569 | else if (Op0->getType()->isX86_FP80Ty()) { |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 570 | APFloat apf = APFloat(GV.IntVal); |
| 571 | uint64_t v; |
Dale Johannesen | 6e547b4 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 572 | bool ignored; |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 573 | (void)apf.convertToInteger(&v, BitWidth, |
| 574 | CE->getOpcode()==Instruction::FPToSI, |
Dale Johannesen | 6e547b4 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 575 | APFloat::rmTowardZero, &ignored); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 576 | GV.IntVal = v; // endian? |
| 577 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 578 | return GV; |
| 579 | } |
| 580 | case Instruction::PtrToInt: { |
| 581 | GenericValue GV = getConstantValue(Op0); |
| 582 | uint32_t PtrWidth = TD->getPointerSizeInBits(); |
| 583 | GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal)); |
| 584 | return GV; |
| 585 | } |
| 586 | case Instruction::IntToPtr: { |
| 587 | GenericValue GV = getConstantValue(Op0); |
| 588 | uint32_t PtrWidth = TD->getPointerSizeInBits(); |
| 589 | if (PtrWidth != GV.IntVal.getBitWidth()) |
| 590 | GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth); |
| 591 | assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width"); |
| 592 | GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue())); |
| 593 | return GV; |
| 594 | } |
| 595 | case Instruction::BitCast: { |
| 596 | GenericValue GV = getConstantValue(Op0); |
| 597 | const Type* DestTy = CE->getType(); |
| 598 | switch (Op0->getType()->getTypeID()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 599 | default: llvm_unreachable("Invalid bitcast operand"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 600 | case Type::IntegerTyID: |
| 601 | assert(DestTy->isFloatingPoint() && "invalid bitcast"); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 602 | if (DestTy->isFloatTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 603 | GV.FloatVal = GV.IntVal.bitsToFloat(); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 604 | else if (DestTy->isDoubleTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 605 | GV.DoubleVal = GV.IntVal.bitsToDouble(); |
| 606 | break; |
| 607 | case Type::FloatTyID: |
Benjamin Kramer | f0e83d2 | 2010-01-05 21:05:54 +0000 | [diff] [blame] | 608 | assert(DestTy->isInteger(32) && "Invalid bitcast"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 609 | GV.IntVal.floatToBits(GV.FloatVal); |
| 610 | break; |
| 611 | case Type::DoubleTyID: |
Benjamin Kramer | f0e83d2 | 2010-01-05 21:05:54 +0000 | [diff] [blame] | 612 | assert(DestTy->isInteger(64) && "Invalid bitcast"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 613 | GV.IntVal.doubleToBits(GV.DoubleVal); |
| 614 | break; |
| 615 | case Type::PointerTyID: |
| 616 | assert(isa<PointerType>(DestTy) && "Invalid bitcast"); |
| 617 | break; // getConstantValue(Op0) above already converted it |
| 618 | } |
| 619 | return GV; |
| 620 | } |
| 621 | case Instruction::Add: |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 622 | case Instruction::FAdd: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 623 | case Instruction::Sub: |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 624 | case Instruction::FSub: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 625 | case Instruction::Mul: |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 626 | case Instruction::FMul: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 627 | case Instruction::UDiv: |
| 628 | case Instruction::SDiv: |
| 629 | case Instruction::URem: |
| 630 | case Instruction::SRem: |
| 631 | case Instruction::And: |
| 632 | case Instruction::Or: |
| 633 | case Instruction::Xor: { |
| 634 | GenericValue LHS = getConstantValue(Op0); |
| 635 | GenericValue RHS = getConstantValue(CE->getOperand(1)); |
| 636 | GenericValue GV; |
| 637 | switch (CE->getOperand(0)->getType()->getTypeID()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 638 | default: llvm_unreachable("Bad add type!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 639 | case Type::IntegerTyID: |
| 640 | switch (CE->getOpcode()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 641 | default: llvm_unreachable("Invalid integer opcode"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 642 | case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break; |
| 643 | case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break; |
| 644 | case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break; |
| 645 | case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break; |
| 646 | case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break; |
| 647 | case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break; |
| 648 | case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break; |
| 649 | case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break; |
| 650 | case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break; |
| 651 | case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break; |
| 652 | } |
| 653 | break; |
| 654 | case Type::FloatTyID: |
| 655 | switch (CE->getOpcode()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 656 | default: llvm_unreachable("Invalid float opcode"); |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 657 | case Instruction::FAdd: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 658 | GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break; |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 659 | case Instruction::FSub: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 660 | GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break; |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 661 | case Instruction::FMul: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 662 | GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break; |
| 663 | case Instruction::FDiv: |
| 664 | GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break; |
| 665 | case Instruction::FRem: |
| 666 | GV.FloatVal = ::fmodf(LHS.FloatVal,RHS.FloatVal); break; |
| 667 | } |
| 668 | break; |
| 669 | case Type::DoubleTyID: |
| 670 | switch (CE->getOpcode()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 671 | default: llvm_unreachable("Invalid double opcode"); |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 672 | case Instruction::FAdd: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 673 | GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break; |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 674 | case Instruction::FSub: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 675 | GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break; |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 676 | case Instruction::FMul: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 677 | GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break; |
| 678 | case Instruction::FDiv: |
| 679 | GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break; |
| 680 | case Instruction::FRem: |
| 681 | GV.DoubleVal = ::fmod(LHS.DoubleVal,RHS.DoubleVal); break; |
| 682 | } |
| 683 | break; |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 684 | case Type::X86_FP80TyID: |
| 685 | case Type::PPC_FP128TyID: |
| 686 | case Type::FP128TyID: { |
| 687 | APFloat apfLHS = APFloat(LHS.IntVal); |
| 688 | switch (CE->getOpcode()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 689 | default: llvm_unreachable("Invalid long double opcode");llvm_unreachable(0); |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 690 | case Instruction::FAdd: |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 691 | apfLHS.add(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 692 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 693 | break; |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 694 | case Instruction::FSub: |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 695 | apfLHS.subtract(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 696 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 697 | break; |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 698 | case Instruction::FMul: |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 699 | apfLHS.multiply(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 700 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 701 | break; |
| 702 | case Instruction::FDiv: |
| 703 | apfLHS.divide(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 704 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 705 | break; |
| 706 | case Instruction::FRem: |
| 707 | apfLHS.mod(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 708 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 709 | break; |
| 710 | } |
| 711 | } |
| 712 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 713 | } |
| 714 | return GV; |
| 715 | } |
| 716 | default: |
| 717 | break; |
| 718 | } |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 719 | std::string msg; |
| 720 | raw_string_ostream Msg(msg); |
| 721 | Msg << "ConstantExpr not handled: " << *CE; |
| 722 | llvm_report_error(Msg.str()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | GenericValue Result; |
| 726 | switch (C->getType()->getTypeID()) { |
| 727 | case Type::FloatTyID: |
Dale Johannesen | b9de9f0 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 728 | Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 729 | break; |
| 730 | case Type::DoubleTyID: |
Dale Johannesen | b9de9f0 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 731 | Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 732 | break; |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 733 | case Type::X86_FP80TyID: |
| 734 | case Type::FP128TyID: |
| 735 | case Type::PPC_FP128TyID: |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 736 | Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 737 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 738 | case Type::IntegerTyID: |
| 739 | Result.IntVal = cast<ConstantInt>(C)->getValue(); |
| 740 | break; |
| 741 | case Type::PointerTyID: |
| 742 | if (isa<ConstantPointerNull>(C)) |
| 743 | Result.PointerVal = 0; |
| 744 | else if (const Function *F = dyn_cast<Function>(C)) |
| 745 | Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F))); |
Chris Lattner | bfa6b85 | 2009-10-29 05:26:09 +0000 | [diff] [blame] | 746 | else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 747 | Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV))); |
Chris Lattner | bfa6b85 | 2009-10-29 05:26:09 +0000 | [diff] [blame] | 748 | else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
| 749 | Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>( |
| 750 | BA->getBasicBlock()))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 751 | else |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 752 | llvm_unreachable("Unknown constant pointer type!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 753 | break; |
| 754 | default: |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 755 | std::string msg; |
| 756 | raw_string_ostream Msg(msg); |
| 757 | Msg << "ERROR: Constant unimplemented for type: " << *C->getType(); |
| 758 | llvm_report_error(Msg.str()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 759 | } |
| 760 | return Result; |
| 761 | } |
| 762 | |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 763 | /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst |
| 764 | /// with the integer held in IntVal. |
| 765 | static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, |
| 766 | unsigned StoreBytes) { |
| 767 | assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!"); |
| 768 | uint8_t *Src = (uint8_t *)IntVal.getRawData(); |
| 769 | |
Chris Lattner | fcaa7f8 | 2009-01-22 19:53:00 +0000 | [diff] [blame] | 770 | if (sys::isLittleEndianHost()) |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 771 | // Little-endian host - the source is ordered from LSB to MSB. Order the |
| 772 | // destination from LSB to MSB: Do a straight copy. |
| 773 | memcpy(Dst, Src, StoreBytes); |
| 774 | else { |
| 775 | // Big-endian host - the source is an array of 64 bit words ordered from |
| 776 | // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination |
| 777 | // from MSB to LSB: Reverse the word order, but not the bytes in a word. |
| 778 | while (StoreBytes > sizeof(uint64_t)) { |
| 779 | StoreBytes -= sizeof(uint64_t); |
| 780 | // May not be aligned so use memcpy. |
| 781 | memcpy(Dst + StoreBytes, Src, sizeof(uint64_t)); |
| 782 | Src += sizeof(uint64_t); |
| 783 | } |
| 784 | |
| 785 | memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes); |
| 786 | } |
| 787 | } |
| 788 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 789 | /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr |
| 790 | /// is the address of the memory at which to store Val, cast to GenericValue *. |
| 791 | /// It is not a pointer to a GenericValue containing the address at which to |
| 792 | /// store Val. |
Evan Cheng | d0e5e98 | 2008-11-04 06:10:31 +0000 | [diff] [blame] | 793 | void ExecutionEngine::StoreValueToMemory(const GenericValue &Val, |
| 794 | GenericValue *Ptr, const Type *Ty) { |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 795 | const unsigned StoreBytes = getTargetData()->getTypeStoreSize(Ty); |
| 796 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 797 | switch (Ty->getTypeID()) { |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 798 | case Type::IntegerTyID: |
| 799 | StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 800 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 801 | case Type::FloatTyID: |
| 802 | *((float*)Ptr) = Val.FloatVal; |
| 803 | break; |
| 804 | case Type::DoubleTyID: |
| 805 | *((double*)Ptr) = Val.DoubleVal; |
| 806 | break; |
Dale Johannesen | 2f29456 | 2009-03-24 18:16:17 +0000 | [diff] [blame] | 807 | case Type::X86_FP80TyID: |
| 808 | memcpy(Ptr, Val.IntVal.getRawData(), 10); |
| 809 | break; |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 810 | case Type::PointerTyID: |
| 811 | // Ensure 64 bit target pointers are fully initialized on 32 bit hosts. |
| 812 | if (StoreBytes != sizeof(PointerTy)) |
| 813 | memset(Ptr, 0, StoreBytes); |
| 814 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 815 | *((PointerTy*)Ptr) = Val.PointerVal; |
| 816 | break; |
| 817 | default: |
David Greene | 7050bcc | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 818 | dbgs() << "Cannot store value of type " << *Ty << "!\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 819 | } |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 820 | |
Chris Lattner | fcaa7f8 | 2009-01-22 19:53:00 +0000 | [diff] [blame] | 821 | if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian()) |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 822 | // Host and target are different endian - reverse the stored bytes. |
| 823 | std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr); |
| 824 | } |
| 825 | |
| 826 | /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting |
| 827 | /// from Src into IntVal, which is assumed to be wide enough and to hold zero. |
| 828 | static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) { |
| 829 | assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!"); |
| 830 | uint8_t *Dst = (uint8_t *)IntVal.getRawData(); |
| 831 | |
Chris Lattner | fcaa7f8 | 2009-01-22 19:53:00 +0000 | [diff] [blame] | 832 | if (sys::isLittleEndianHost()) |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 833 | // Little-endian host - the destination must be ordered from LSB to MSB. |
| 834 | // The source is ordered from LSB to MSB: Do a straight copy. |
| 835 | memcpy(Dst, Src, LoadBytes); |
| 836 | else { |
| 837 | // Big-endian - the destination is an array of 64 bit words ordered from |
| 838 | // LSW to MSW. Each word must be ordered from MSB to LSB. The source is |
| 839 | // ordered from MSB to LSB: Reverse the word order, but not the bytes in |
| 840 | // a word. |
| 841 | while (LoadBytes > sizeof(uint64_t)) { |
| 842 | LoadBytes -= sizeof(uint64_t); |
| 843 | // May not be aligned so use memcpy. |
| 844 | memcpy(Dst, Src + LoadBytes, sizeof(uint64_t)); |
| 845 | Dst += sizeof(uint64_t); |
| 846 | } |
| 847 | |
| 848 | memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes); |
| 849 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | /// FIXME: document |
| 853 | /// |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 854 | void ExecutionEngine::LoadValueFromMemory(GenericValue &Result, |
Duncan Sands | f06c7a6 | 2008-03-10 16:38:37 +0000 | [diff] [blame] | 855 | GenericValue *Ptr, |
| 856 | const Type *Ty) { |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 857 | const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty); |
Duncan Sands | 7feee8f | 2007-12-10 17:43:13 +0000 | [diff] [blame] | 858 | |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 859 | switch (Ty->getTypeID()) { |
| 860 | case Type::IntegerTyID: |
| 861 | // An APInt with all words initially zero. |
| 862 | Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0); |
| 863 | LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes); |
| 864 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 865 | case Type::FloatTyID: |
| 866 | Result.FloatVal = *((float*)Ptr); |
| 867 | break; |
| 868 | case Type::DoubleTyID: |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 869 | Result.DoubleVal = *((double*)Ptr); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 870 | break; |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 871 | case Type::PointerTyID: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 872 | Result.PointerVal = *((PointerTy*)Ptr); |
| 873 | break; |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 874 | case Type::X86_FP80TyID: { |
| 875 | // This is endian dependent, but it will only work on x86 anyway. |
Duncan Sands | 1d641aa | 2007-12-15 17:37:40 +0000 | [diff] [blame] | 876 | // FIXME: Will not trap if loading a signaling NaN. |
Dale Johannesen | 2f29456 | 2009-03-24 18:16:17 +0000 | [diff] [blame] | 877 | uint64_t y[2]; |
| 878 | memcpy(y, Ptr, 10); |
Duncan Sands | 8d00dd0 | 2007-11-28 10:36:19 +0000 | [diff] [blame] | 879 | Result.IntVal = APInt(80, 2, y); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 880 | break; |
| 881 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 882 | default: |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 883 | std::string msg; |
| 884 | raw_string_ostream Msg(msg); |
| 885 | Msg << "Cannot load value of type " << *Ty << "!"; |
| 886 | llvm_report_error(Msg.str()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
| 890 | // InitializeMemory - Recursive function to apply a Constant value into the |
| 891 | // specified memory location... |
| 892 | // |
| 893 | void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { |
David Greene | 7050bcc | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 894 | DEBUG(dbgs() << "JIT: Initializing " << Addr << " "); |
Dale Johannesen | 0ba4a0e | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 895 | DEBUG(Init->dump()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 896 | if (isa<UndefValue>(Init)) { |
| 897 | return; |
| 898 | } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) { |
| 899 | unsigned ElementSize = |
Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 900 | getTargetData()->getTypeAllocSize(CP->getType()->getElementType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 901 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 902 | InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize); |
| 903 | return; |
Chris Lattner | bfd482d | 2008-02-15 00:57:28 +0000 | [diff] [blame] | 904 | } else if (isa<ConstantAggregateZero>(Init)) { |
Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 905 | memset(Addr, 0, (size_t)getTargetData()->getTypeAllocSize(Init->getType())); |
Chris Lattner | bfd482d | 2008-02-15 00:57:28 +0000 | [diff] [blame] | 906 | return; |
Dan Gohman | 61dbdbd | 2008-05-20 03:20:09 +0000 | [diff] [blame] | 907 | } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) { |
| 908 | unsigned ElementSize = |
Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 909 | getTargetData()->getTypeAllocSize(CPA->getType()->getElementType()); |
Dan Gohman | 61dbdbd | 2008-05-20 03:20:09 +0000 | [diff] [blame] | 910 | for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) |
| 911 | InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); |
| 912 | return; |
| 913 | } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) { |
| 914 | const StructLayout *SL = |
| 915 | getTargetData()->getStructLayout(cast<StructType>(CPS->getType())); |
| 916 | for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) |
| 917 | InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i)); |
| 918 | return; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 919 | } else if (Init->getType()->isFirstClassType()) { |
| 920 | GenericValue Val = getConstantValue(Init); |
| 921 | StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); |
| 922 | return; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 923 | } |
| 924 | |
David Greene | 7050bcc | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 925 | dbgs() << "Bad Type: " << *Init->getType() << "\n"; |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 926 | llvm_unreachable("Unknown constant type to initialize memory with!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | /// EmitGlobals - Emit all of the global variables to memory, storing their |
| 930 | /// addresses into GlobalAddress. This must make sure to copy the contents of |
| 931 | /// their initializers into the memory. |
| 932 | /// |
| 933 | void ExecutionEngine::emitGlobals() { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 934 | |
| 935 | // Loop over all of the global variables in the program, allocating the memory |
| 936 | // to hold them. If there is more than one module, do a prepass over globals |
| 937 | // to figure out how the different modules should link together. |
| 938 | // |
| 939 | std::map<std::pair<std::string, const Type*>, |
| 940 | const GlobalValue*> LinkedGlobalsMap; |
| 941 | |
| 942 | if (Modules.size() != 1) { |
| 943 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) { |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 944 | Module &M = *Modules[m]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 945 | for (Module::const_global_iterator I = M.global_begin(), |
| 946 | E = M.global_end(); I != E; ++I) { |
| 947 | const GlobalValue *GV = I; |
Rafael Espindola | a168fc9 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 948 | if (GV->hasLocalLinkage() || GV->isDeclaration() || |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 949 | GV->hasAppendingLinkage() || !GV->hasName()) |
| 950 | continue;// Ignore external globals and globals with internal linkage. |
| 951 | |
| 952 | const GlobalValue *&GVEntry = |
| 953 | LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; |
| 954 | |
| 955 | // If this is the first time we've seen this global, it is the canonical |
| 956 | // version. |
| 957 | if (!GVEntry) { |
| 958 | GVEntry = GV; |
| 959 | continue; |
| 960 | } |
| 961 | |
| 962 | // If the existing global is strong, never replace it. |
| 963 | if (GVEntry->hasExternalLinkage() || |
| 964 | GVEntry->hasDLLImportLinkage() || |
| 965 | GVEntry->hasDLLExportLinkage()) |
| 966 | continue; |
| 967 | |
| 968 | // Otherwise, we know it's linkonce/weak, replace it if this is a strong |
Dale Johannesen | 49c4412 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 969 | // symbol. FIXME is this right for common? |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 970 | if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage()) |
| 971 | GVEntry = GV; |
| 972 | } |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | std::vector<const GlobalValue*> NonCanonicalGlobals; |
| 977 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) { |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 978 | Module &M = *Modules[m]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 979 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 980 | I != E; ++I) { |
| 981 | // In the multi-module case, see what this global maps to. |
| 982 | if (!LinkedGlobalsMap.empty()) { |
| 983 | if (const GlobalValue *GVEntry = |
| 984 | LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) { |
| 985 | // If something else is the canonical global, ignore this one. |
| 986 | if (GVEntry != &*I) { |
| 987 | NonCanonicalGlobals.push_back(I); |
| 988 | continue; |
| 989 | } |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | if (!I->isDeclaration()) { |
Nicolas Geoffray | 46fa153 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 994 | addGlobalMapping(I, getMemoryForGV(I)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 995 | } else { |
| 996 | // External variable reference. Try to use the dynamic loader to |
| 997 | // get a pointer to it. |
| 998 | if (void *SymAddr = |
Daniel Dunbar | 9198e93 | 2009-07-21 08:54:24 +0000 | [diff] [blame] | 999 | sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1000 | addGlobalMapping(I, SymAddr); |
| 1001 | else { |
Edwin Török | f7cbfea | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 1002 | llvm_report_error("Could not resolve external global address: " |
| 1003 | +I->getName()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | // If there are multiple modules, map the non-canonical globals to their |
| 1009 | // canonical location. |
| 1010 | if (!NonCanonicalGlobals.empty()) { |
| 1011 | for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) { |
| 1012 | const GlobalValue *GV = NonCanonicalGlobals[i]; |
| 1013 | const GlobalValue *CGV = |
| 1014 | LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; |
| 1015 | void *Ptr = getPointerToGlobalIfAvailable(CGV); |
| 1016 | assert(Ptr && "Canonical global wasn't codegen'd!"); |
Nuno Lopes | ec8dccb | 2008-10-14 10:04:52 +0000 | [diff] [blame] | 1017 | addGlobalMapping(GV, Ptr); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | // Now that all of the globals are set up in memory, loop through them all |
| 1022 | // and initialize their contents. |
| 1023 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 1024 | I != E; ++I) { |
| 1025 | if (!I->isDeclaration()) { |
| 1026 | if (!LinkedGlobalsMap.empty()) { |
| 1027 | if (const GlobalValue *GVEntry = |
| 1028 | LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) |
| 1029 | if (GVEntry != &*I) // Not the canonical variable. |
| 1030 | continue; |
| 1031 | } |
| 1032 | EmitGlobalVariable(I); |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | // EmitGlobalVariable - This method emits the specified global variable to the |
| 1039 | // address specified in GlobalAddresses, or allocates new memory if it's not |
| 1040 | // already in the map. |
| 1041 | void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { |
| 1042 | void *GA = getPointerToGlobalIfAvailable(GV); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1043 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1044 | if (GA == 0) { |
| 1045 | // If it's not already specified, allocate memory for the global. |
Nicolas Geoffray | 46fa153 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 1046 | GA = getMemoryForGV(GV); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1047 | addGlobalMapping(GV, GA); |
| 1048 | } |
Nicolas Geoffray | 46fa153 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 1049 | |
| 1050 | // Don't initialize if it's thread local, let the client do it. |
| 1051 | if (!GV->isThreadLocal()) |
| 1052 | InitializeMemory(GV->getInitializer(), GA); |
| 1053 | |
| 1054 | const Type *ElTy = GV->getType()->getElementType(); |
Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1055 | size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1056 | NumInitBytes += (unsigned)GVSize; |
| 1057 | ++NumGlobals; |
| 1058 | } |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 1059 | |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 1060 | ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE) |
| 1061 | : EE(EE), GlobalAddressMap(this) { |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 1064 | sys::Mutex *ExecutionEngineState::AddressMapConfig::getMutex( |
| 1065 | ExecutionEngineState *EES) { |
| 1066 | return &EES->EE.lock; |
| 1067 | } |
| 1068 | void ExecutionEngineState::AddressMapConfig::onDelete( |
| 1069 | ExecutionEngineState *EES, const GlobalValue *Old) { |
| 1070 | void *OldVal = EES->GlobalAddressMap.lookup(Old); |
| 1071 | EES->GlobalAddressReverseMap.erase(OldVal); |
| 1072 | } |
| 1073 | |
| 1074 | void ExecutionEngineState::AddressMapConfig::onRAUW( |
| 1075 | ExecutionEngineState *, const GlobalValue *, const GlobalValue *) { |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 1076 | assert(false && "The ExecutionEngine doesn't know how to handle a" |
| 1077 | " RAUW on a value it has a global mapping for."); |
| 1078 | } |