Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 1 | //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 10 | // This file defines the common interface used by the various execution engine |
| 11 | // subclasses. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 3785fad | 2003-08-05 17:00:32 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "jit" |
Jeffrey Yasskin | 0d5bd59 | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 17 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 19 | #include "llvm/DerivedTypes.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Chris Lattner | fd13129 | 2003-09-05 20:08:15 +0000 | [diff] [blame] | 21 | #include "llvm/ExecutionEngine/GenericValue.h" |
Chris Lattner | 9f3ff92 | 2009-08-23 22:49:13 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Torok Edwin | 31e2466 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MutexGuard.h" |
Jeffrey Yasskin | 0d5bd59 | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ValueHandle.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | df5a37e | 2004-11-29 14:11:29 +0000 | [diff] [blame] | 28 | #include "llvm/System/DynamicLibrary.h" |
Duncan Sands | 67f1c49 | 2007-12-12 23:03:45 +0000 | [diff] [blame] | 29 | #include "llvm/System/Host.h" |
Reid Spencer | df5a37e | 2004-11-29 14:11:29 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetData.h" |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 31 | #include <cmath> |
| 32 | #include <cstring> |
Chris Lattner | c2ee9b9 | 2003-11-19 21:08:57 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 3634373 | 2006-12-19 22:43:32 +0000 | [diff] [blame] | 35 | STATISTIC(NumInitBytes, "Number of bytes of global vars initialized"); |
| 36 | STATISTIC(NumGlobals , "Number of global vars initialized"); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 37 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 38 | ExecutionEngine *(*ExecutionEngine::JITCtor)(Module *M, |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 39 | std::string *ErrorStr, |
| 40 | JITMemoryManager *JMM, |
| 41 | CodeGenOpt::Level OptLevel, |
Eric Christopher | 88b5aca | 2009-11-17 21:58:16 +0000 | [diff] [blame] | 42 | bool GVsWithCode, |
| 43 | CodeModel::Model CMM) = 0; |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 44 | ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M, |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 45 | std::string *ErrorStr) = 0; |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 46 | ExecutionEngine::EERegisterFn ExecutionEngine::ExceptionTableRegister = 0; |
| 47 | |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 48 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 49 | ExecutionEngine::ExecutionEngine(Module *M) |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 50 | : EEState(*this), |
| 51 | LazyFunctionCreator(0) { |
Jeffrey Yasskin | dc85724 | 2009-10-27 20:30:28 +0000 | [diff] [blame] | 52 | CompilingLazily = false; |
Evan Cheng | 446531e | 2008-09-24 16:25:55 +0000 | [diff] [blame] | 53 | GVCompilationDisabled = false; |
Evan Cheng | 1b088f3 | 2008-06-17 16:49:02 +0000 | [diff] [blame] | 54 | SymbolSearchingDisabled = false; |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 55 | Modules.push_back(M); |
| 56 | assert(M && "Module is null?"); |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Brian Gaeke | 8e53948 | 2003-09-04 22:57:27 +0000 | [diff] [blame] | 59 | ExecutionEngine::~ExecutionEngine() { |
Reid Spencer | d4c0e62 | 2007-03-03 18:19:18 +0000 | [diff] [blame] | 60 | clearAllGlobalMappings(); |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 61 | for (unsigned i = 0, e = Modules.size(); i != e; ++i) |
| 62 | delete Modules[i]; |
Brian Gaeke | 8e53948 | 2003-09-04 22:57:27 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Nicolas Geoffray | 46fa139 | 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 | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 67 | size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy); |
Nicolas Geoffray | 46fa139 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 68 | return new char[GVSize]; |
| 69 | } |
| 70 | |
Jeffrey Yasskin | f0356fe | 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 | 73d0e21 | 2007-10-15 19:56:32 +0000 | [diff] [blame] | 74 | E = Modules.end(); I != E; ++I) { |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 75 | Module *Found = *I; |
| 76 | if (Found == M) { |
Devang Patel | 73d0e21 | 2007-10-15 19:56:32 +0000 | [diff] [blame] | 77 | Modules.erase(I); |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 78 | clearGlobalMappingsFromModule(M); |
| 79 | return true; |
Devang Patel | 73d0e21 | 2007-10-15 19:56:32 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 82 | return false; |
Nate Begeman | 60789e4 | 2009-01-23 19:27:28 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +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 | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 90 | if (Function *F = Modules[i]->getFunction(FnName)) |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 91 | return F; |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | |
Jeffrey Yasskin | c89d27a | 2009-10-09 22:10:27 +0000 | [diff] [blame] | 97 | void *ExecutionEngineState::RemoveMapping( |
| 98 | const MutexGuard &, const GlobalValue *ToUnmap) { |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 99 | GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap); |
Jeffrey Yasskin | c89d27a | 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 | |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +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 | bc4707a | 2008-09-18 07:54:21 +0000 | [diff] [blame] | 119 | |
David Greene | ae7c59a | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 120 | DEBUG(dbgs() << "JIT: Map \'" << GV->getName() |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 121 | << "\' to [" << Addr << "]\n";); |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 122 | void *&CurVal = EEState.getGlobalAddressMap(locked)[GV]; |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +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 | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 127 | if (!EEState.getGlobalAddressReverseMap(locked).empty()) { |
Jeffrey Yasskin | 0d5bd59 | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 128 | AssertingVH<const GlobalValue> &V = |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 129 | EEState.getGlobalAddressReverseMap(locked)[Addr]; |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +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 | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 140 | EEState.getGlobalAddressMap(locked).clear(); |
| 141 | EEState.getGlobalAddressReverseMap(locked).clear(); |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Nate Begeman | f049e07 | 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 | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 150 | EEState.RemoveMapping(locked, FI); |
Nate Begeman | f049e07 | 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 | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 154 | EEState.RemoveMapping(locked, GI); |
Nate Begeman | f049e07 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +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 | f4cc309 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 161 | void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) { |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 162 | MutexGuard locked(lock); |
Chris Lattner | f4cc309 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 163 | |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 164 | ExecutionEngineState::GlobalAddressMapTy &Map = |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 165 | EEState.getGlobalAddressMap(locked); |
Chris Lattner | f4cc309 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 166 | |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 167 | // Deleting from the mapping? |
| 168 | if (Addr == 0) { |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 169 | return EEState.RemoveMapping(locked, GV); |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 172 | void *&CurVal = Map[GV]; |
Chris Lattner | f4cc309 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 173 | void *OldVal = CurVal; |
| 174 | |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 175 | if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty()) |
| 176 | EEState.getGlobalAddressReverseMap(locked).erase(CurVal); |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 177 | CurVal = Addr; |
| 178 | |
| 179 | // If we are using the reverse mapping, add it too |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 180 | if (!EEState.getGlobalAddressReverseMap(locked).empty()) { |
Jeffrey Yasskin | 0d5bd59 | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 181 | AssertingVH<const GlobalValue> &V = |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 182 | EEState.getGlobalAddressReverseMap(locked)[Addr]; |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 183 | assert((V == 0 || GV == 0) && "GlobalMapping already established!"); |
| 184 | V = GV; |
| 185 | } |
Chris Lattner | f4cc309 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 186 | return OldVal; |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +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 | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 195 | ExecutionEngineState::GlobalAddressMapTy::iterator I = |
| 196 | EEState.getGlobalAddressMap(locked).find(GV); |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 197 | return I != EEState.getGlobalAddressMap(locked).end() ? I->second : 0; |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 200 | /// getGlobalValueAtAddress - Return the LLVM global value object that starts |
| 201 | /// at the specified address. |
| 202 | /// |
| 203 | const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 204 | MutexGuard locked(lock); |
| 205 | |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 206 | // If we haven't computed the reverse mapping yet, do so first. |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 207 | if (EEState.getGlobalAddressReverseMap(locked).empty()) { |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 208 | for (ExecutionEngineState::GlobalAddressMapTy::iterator |
Jeffrey Yasskin | 4c5b23b | 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, |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 212 | I->first)); |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Jeffrey Yasskin | 0d5bd59 | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 215 | std::map<void *, AssertingVH<const GlobalValue> >::iterator I = |
Jeffrey Yasskin | 4c5b23b | 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; |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 218 | } |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 219 | |
| 220 | // CreateArgv - Turn a vector of strings into a nice argv style array of |
| 221 | // pointers to null terminated strings. |
| 222 | // |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 223 | static void *CreateArgv(LLVMContext &C, ExecutionEngine *EE, |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 224 | const std::vector<std::string> &InputArgv) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 225 | unsigned PtrSize = EE->getTargetData()->getPointerSize(); |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 226 | char *Result = new char[(InputArgv.size()+1)*PtrSize]; |
| 227 | |
David Greene | ae7c59a | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 228 | DEBUG(dbgs() << "JIT: ARGV = " << (void*)Result << "\n"); |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 229 | const Type *SBytePtr = Type::getInt8PtrTy(C); |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +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 | ae7c59a | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 234 | DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n"); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 236 | std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); |
| 237 | Dest[Size-1] = 0; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 251 | |
| 252 | /// runStaticConstructorsDestructors - This method is used to execute all of |
Evan Cheng | 18314dc | 2008-09-30 15:51:21 +0000 | [diff] [blame] | 253 | /// the static constructors or destructors for a module, depending on the |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 254 | /// value of isDtors. |
Chris Lattner | fbd3976 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 255 | void ExecutionEngine::runStaticConstructorsDestructors(Module *module, |
| 256 | bool isDtors) { |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 257 | const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors"; |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 258 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 259 | // Execute global ctors/dtors for each module in the program. |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 260 | |
Evan Cheng | 18314dc | 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 | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 267 | if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return; |
Evan Cheng | 18314dc | 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 | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 298 | runStaticConstructorsDestructors(Modules[m], isDtors); |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Dan Gohman | b6e3d6c | 2008-08-26 01:38:29 +0000 | [diff] [blame] | 301 | #ifndef NDEBUG |
Duncan Sands | 8a43e9e | 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 | b6e3d6c | 2008-08-26 01:38:29 +0000 | [diff] [blame] | 310 | #endif |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +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; |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 320 | GVArgc.IntVal = APInt(32, argv.size()); |
Anton Korobeynikov | 499d8f0 | 2007-06-03 19:17:35 +0000 | [diff] [blame] | 321 | |
| 322 | // Check main() type |
Chris Lattner | f24d099 | 2004-08-16 01:05:35 +0000 | [diff] [blame] | 323 | unsigned NumArgs = Fn->getFunctionType()->getNumParams(); |
Anton Korobeynikov | 499d8f0 | 2007-06-03 19:17:35 +0000 | [diff] [blame] | 324 | const FunctionType *FTy = Fn->getFunctionType(); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 325 | const Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo(); |
Anton Korobeynikov | 499d8f0 | 2007-06-03 19:17:35 +0000 | [diff] [blame] | 326 | switch (NumArgs) { |
| 327 | case 3: |
| 328 | if (FTy->getParamType(2) != PPInt8Ty) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 329 | llvm_report_error("Invalid type for third argument of main() supplied"); |
Anton Korobeynikov | 499d8f0 | 2007-06-03 19:17:35 +0000 | [diff] [blame] | 330 | } |
Anton Korobeynikov | fb45086 | 2007-06-03 19:20:49 +0000 | [diff] [blame] | 331 | // FALLS THROUGH |
Anton Korobeynikov | 499d8f0 | 2007-06-03 19:17:35 +0000 | [diff] [blame] | 332 | case 2: |
| 333 | if (FTy->getParamType(1) != PPInt8Ty) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 334 | llvm_report_error("Invalid type for second argument of main() supplied"); |
Anton Korobeynikov | 499d8f0 | 2007-06-03 19:17:35 +0000 | [diff] [blame] | 335 | } |
Anton Korobeynikov | fb45086 | 2007-06-03 19:20:49 +0000 | [diff] [blame] | 336 | // FALLS THROUGH |
Anton Korobeynikov | 499d8f0 | 2007-06-03 19:17:35 +0000 | [diff] [blame] | 337 | case 1: |
Benjamin Kramer | 8c65f6e | 2010-01-05 21:05:54 +0000 | [diff] [blame] | 338 | if (!FTy->getParamType(0)->isInteger(32)) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 339 | llvm_report_error("Invalid type for first argument of main() supplied"); |
Anton Korobeynikov | 499d8f0 | 2007-06-03 19:17:35 +0000 | [diff] [blame] | 340 | } |
Anton Korobeynikov | fb45086 | 2007-06-03 19:20:49 +0000 | [diff] [blame] | 341 | // FALLS THROUGH |
Anton Korobeynikov | 499d8f0 | 2007-06-03 19:17:35 +0000 | [diff] [blame] | 342 | case 0: |
Chris Lattner | 45f3683 | 2009-02-04 17:48:18 +0000 | [diff] [blame] | 343 | if (!isa<IntegerType>(FTy->getReturnType()) && |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 344 | !FTy->getReturnType()->isVoidTy()) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 345 | llvm_report_error("Invalid return type of main() supplied"); |
Anton Korobeynikov | 499d8f0 | 2007-06-03 19:17:35 +0000 | [diff] [blame] | 346 | } |
| 347 | break; |
| 348 | default: |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 349 | llvm_report_error("Invalid number of arguments of main() supplied"); |
Anton Korobeynikov | 499d8f0 | 2007-06-03 19:17:35 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Chris Lattner | f24d099 | 2004-08-16 01:05:35 +0000 | [diff] [blame] | 352 | if (NumArgs) { |
| 353 | GVArgs.push_back(GVArgc); // Arg #0 = argc. |
| 354 | if (NumArgs > 1) { |
Owen Anderson | 1d0be15 | 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 | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 357 | assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) && |
Chris Lattner | f24d099 | 2004-08-16 01:05:35 +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 | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 363 | // Arg #2 = envp. |
| 364 | GVArgs.push_back(PTOGV(CreateArgv(Fn->getContext(), this, EnvVars))); |
Chris Lattner | f24d099 | 2004-08-16 01:05:35 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | } |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 368 | return runFunction(Fn, GVArgs).IntVal.getZExtValue(); |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 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, |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 373 | /// NULL is returned. |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 374 | /// |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 375 | ExecutionEngine *ExecutionEngine::create(Module *M, |
Reid Spencer | d4c0e62 | 2007-03-03 18:19:18 +0000 | [diff] [blame] | 376 | bool ForceInterpreter, |
Evan Cheng | 502f20b | 2008-08-08 08:11:34 +0000 | [diff] [blame] | 377 | std::string *ErrorStr, |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 378 | CodeGenOpt::Level OptLevel, |
| 379 | bool GVsWithCode) { |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 380 | return EngineBuilder(M) |
Reid Kleckner | 4b1511b | 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 | } |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 389 | |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 390 | ExecutionEngine *ExecutionEngine::create(Module *M) { |
| 391 | return EngineBuilder(M).create(); |
| 392 | } |
| 393 | |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 394 | ExecutionEngine *EngineBuilder::create() { |
Nick Lewycky | 6456d86 | 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 | 4b1511b | 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 | fbd3976 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 404 | if (WhichEngine & EngineKind::JIT) |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 405 | WhichEngine = EngineKind::JIT; |
Chris Lattner | fbd3976 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 406 | else { |
Chris Lattner | c72efbe | 2009-09-23 02:03:49 +0000 | [diff] [blame] | 407 | if (ErrorStr) |
| 408 | *ErrorStr = "Cannot create an interpreter with a memory manager."; |
Chris Lattner | fbd3976 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 409 | return 0; |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 412 | |
Reid Kleckner | 4b1511b | 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 | fbd3976 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 415 | if (WhichEngine & EngineKind::JIT) { |
| 416 | if (ExecutionEngine::JITCtor) { |
| 417 | ExecutionEngine *EE = |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 418 | ExecutionEngine::JITCtor(M, ErrorStr, JMM, OptLevel, |
Eric Christopher | 88b5aca | 2009-11-17 21:58:16 +0000 | [diff] [blame] | 419 | AllocateGVsWithCode, CMModel); |
Chris Lattner | fbd3976 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 420 | if (EE) return EE; |
Chris Lattner | fbd3976 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 421 | } |
Reid Kleckner | 4b1511b | 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 | fbd3976 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 426 | if (WhichEngine & EngineKind::Interpreter) { |
| 427 | if (ExecutionEngine::InterpCtor) |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 428 | return ExecutionEngine::InterpCtor(M, ErrorStr); |
Chris Lattner | c72efbe | 2009-09-23 02:03:49 +0000 | [diff] [blame] | 429 | if (ErrorStr) |
| 430 | *ErrorStr = "Interpreter has not been linked in."; |
Chris Lattner | fbd3976 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 431 | return 0; |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 432 | } |
Chris Lattner | c72efbe | 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 | fbd3976 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 438 | return 0; |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 441 | /// getPointerToGlobal - This returns the address of the specified global |
| 442 | /// value. This may involve code generation if it's a function. |
| 443 | /// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 444 | void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { |
Brian Gaeke | 37df460 | 2003-08-13 18:16:14 +0000 | [diff] [blame] | 445 | if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 446 | return getPointerToFunction(F); |
| 447 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 448 | MutexGuard locked(lock); |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 449 | void *p = EEState.getGlobalAddressMap(locked)[GV]; |
Jeff Cohen | 68835dd | 2006-02-07 05:11:57 +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 |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 458 | llvm_unreachable("Global hasn't had an address allocated yet!"); |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 459 | return EEState.getGlobalAddressMap(locked)[GV]; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 462 | /// This function converts a Constant* into a GenericValue. The interesting |
| 463 | /// part is if C is a ConstantExpr. |
Reid Spencer | ba28cb9 | 2007-08-11 15:57:56 +0000 | [diff] [blame] | 464 | /// @brief Get a GenericValue for a Constant* |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 465 | GenericValue ExecutionEngine::getConstantValue(const Constant *C) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 466 | // If its undefined, return the garbage. |
Jay Foad | 5b37012 | 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 | } |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 483 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 484 | // If the value is a ConstantExpr |
| 485 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 486 | Constant *Op0 = CE->getOperand(0); |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 487 | switch (CE->getOpcode()) { |
| 488 | case Instruction::GetElementPtr: { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 489 | // Compute the index |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 490 | GenericValue Result = getConstantValue(Op0); |
Chris Lattner | 829621c | 2007-02-10 20:35:22 +0000 | [diff] [blame] | 491 | SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end()); |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 492 | uint64_t Offset = |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 493 | TD->getIndexedOffset(Op0->getType(), &Indices[0], Indices.size()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 494 | |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 495 | char* tmp = (char*) Result.PointerVal; |
| 496 | Result = PTOGV(tmp + Offset); |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 497 | return Result; |
| 498 | } |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 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 | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 518 | // FIXME long double |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 519 | GenericValue GV = getConstantValue(Op0); |
| 520 | GV.FloatVal = float(GV.DoubleVal); |
| 521 | return GV; |
| 522 | } |
| 523 | case Instruction::FPExt:{ |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 524 | // FIXME long double |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +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 | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 531 | if (CE->getType()->isFloatTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 532 | GV.FloatVal = float(GV.IntVal.roundToDouble()); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 533 | else if (CE->getType()->isDoubleTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 534 | GV.DoubleVal = GV.IntVal.roundToDouble(); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 535 | else if (CE->getType()->isX86_FP80Ty()) { |
Dale Johannesen | 1abac0d | 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 | 6282406 | 2008-02-29 01:27:13 +0000 | [diff] [blame] | 538 | (void)apf.convertFromAPInt(GV.IntVal, |
| 539 | false, |
| 540 | APFloat::rmNearestTiesToEven); |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 541 | GV.IntVal = apf.bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 542 | } |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 543 | return GV; |
| 544 | } |
| 545 | case Instruction::SIToFP: { |
| 546 | GenericValue GV = getConstantValue(Op0); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 547 | if (CE->getType()->isFloatTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 548 | GV.FloatVal = float(GV.IntVal.signedRoundToDouble()); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 549 | else if (CE->getType()->isDoubleTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 550 | GV.DoubleVal = GV.IntVal.signedRoundToDouble(); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 551 | else if (CE->getType()->isX86_FP80Ty()) { |
Dale Johannesen | 1abac0d | 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 | 6282406 | 2008-02-29 01:27:13 +0000 | [diff] [blame] | 554 | (void)apf.convertFromAPInt(GV.IntVal, |
| 555 | true, |
| 556 | APFloat::rmNearestTiesToEven); |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 557 | GV.IntVal = apf.bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 558 | } |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +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 | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 565 | if (Op0->getType()->isFloatTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 566 | GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 567 | else if (Op0->getType()->isDoubleTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 568 | GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 569 | else if (Op0->getType()->isX86_FP80Ty()) { |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 570 | APFloat apf = APFloat(GV.IntVal); |
| 571 | uint64_t v; |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 572 | bool ignored; |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 573 | (void)apf.convertToInteger(&v, BitWidth, |
| 574 | CE->getOpcode()==Instruction::FPToSI, |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 575 | APFloat::rmTowardZero, &ignored); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 576 | GV.IntVal = v; // endian? |
| 577 | } |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 578 | return GV; |
| 579 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 580 | case Instruction::PtrToInt: { |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 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())); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 593 | return GV; |
| 594 | } |
| 595 | case Instruction::BitCast: { |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 596 | GenericValue GV = getConstantValue(Op0); |
| 597 | const Type* DestTy = CE->getType(); |
| 598 | switch (Op0->getType()->getTypeID()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 599 | default: llvm_unreachable("Invalid bitcast operand"); |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 600 | case Type::IntegerTyID: |
| 601 | assert(DestTy->isFloatingPoint() && "invalid bitcast"); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 602 | if (DestTy->isFloatTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 603 | GV.FloatVal = GV.IntVal.bitsToFloat(); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 604 | else if (DestTy->isDoubleTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 605 | GV.DoubleVal = GV.IntVal.bitsToDouble(); |
| 606 | break; |
| 607 | case Type::FloatTyID: |
Benjamin Kramer | 8c65f6e | 2010-01-05 21:05:54 +0000 | [diff] [blame] | 608 | assert(DestTy->isInteger(32) && "Invalid bitcast"); |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 609 | GV.IntVal.floatToBits(GV.FloatVal); |
| 610 | break; |
| 611 | case Type::DoubleTyID: |
Benjamin Kramer | 8c65f6e | 2010-01-05 21:05:54 +0000 | [diff] [blame] | 612 | assert(DestTy->isInteger(64) && "Invalid bitcast"); |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +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; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 620 | } |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 621 | case Instruction::Add: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 622 | case Instruction::FAdd: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 623 | case Instruction::Sub: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 624 | case Instruction::FSub: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 625 | case Instruction::Mul: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 626 | case Instruction::FMul: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +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; |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 637 | switch (CE->getOperand(0)->getType()->getTypeID()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 638 | default: llvm_unreachable("Bad add type!"); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 639 | case Type::IntegerTyID: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 640 | switch (CE->getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 641 | default: llvm_unreachable("Invalid integer opcode"); |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +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 | } |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 653 | break; |
| 654 | case Type::FloatTyID: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 655 | switch (CE->getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 656 | default: llvm_unreachable("Invalid float opcode"); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 657 | case Instruction::FAdd: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 658 | GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 659 | case Instruction::FSub: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 660 | GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 661 | case Instruction::FMul: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +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 | } |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 668 | break; |
| 669 | case Type::DoubleTyID: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 670 | switch (CE->getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 671 | default: llvm_unreachable("Invalid double opcode"); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 672 | case Instruction::FAdd: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 673 | GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 674 | case Instruction::FSub: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 675 | GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 676 | case Instruction::FMul: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +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 | } |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 683 | break; |
Dale Johannesen | 1abac0d | 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()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 689 | default: llvm_unreachable("Invalid long double opcode");llvm_unreachable(0); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 690 | case Instruction::FAdd: |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 691 | apfLHS.add(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 692 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 693 | break; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 694 | case Instruction::FSub: |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 695 | apfLHS.subtract(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 696 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 697 | break; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 698 | case Instruction::FMul: |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 699 | apfLHS.multiply(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 700 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 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 | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 704 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 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 | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 708 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 709 | break; |
| 710 | } |
| 711 | } |
| 712 | break; |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 713 | } |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 714 | return GV; |
| 715 | } |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 716 | default: |
| 717 | break; |
| 718 | } |
Torok Edwin | 7d696d8 | 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()); |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 723 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 724 | |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 725 | GenericValue Result; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 726 | switch (C->getType()->getTypeID()) { |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 727 | case Type::FloatTyID: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 728 | Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 729 | break; |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 730 | case Type::DoubleTyID: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 731 | Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble(); |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 732 | break; |
Dale Johannesen | 1abac0d | 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 | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 736 | Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 737 | break; |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 738 | case Type::IntegerTyID: |
| 739 | Result.IntVal = cast<ConstantInt>(C)->getValue(); |
| 740 | break; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 741 | case Type::PointerTyID: |
Reid Spencer | 40cf2f9 | 2004-07-18 00:41:27 +0000 | [diff] [blame] | 742 | if (isa<ConstantPointerNull>(C)) |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 743 | Result.PointerVal = 0; |
Reid Spencer | 40cf2f9 | 2004-07-18 00:41:27 +0000 | [diff] [blame] | 744 | else if (const Function *F = dyn_cast<Function>(C)) |
| 745 | Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F))); |
Chris Lattner | f32a6a3 | 2009-10-29 05:26:09 +0000 | [diff] [blame] | 746 | else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) |
Reid Spencer | 40cf2f9 | 2004-07-18 00:41:27 +0000 | [diff] [blame] | 747 | Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV))); |
Chris Lattner | f32a6a3 | 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()))); |
Reid Spencer | 40cf2f9 | 2004-07-18 00:41:27 +0000 | [diff] [blame] | 751 | else |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 752 | llvm_unreachable("Unknown constant pointer type!"); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 753 | break; |
| 754 | default: |
Torok Edwin | 7d696d8 | 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()); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 759 | } |
| 760 | return Result; |
| 761 | } |
| 762 | |
Duncan Sands | 8a43e9e | 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 | b67c958 | 2009-01-22 19:53:00 +0000 | [diff] [blame] | 770 | if (sys::isLittleEndianHost()) |
Duncan Sands | 8a43e9e | 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 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +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 | 89687e3 | 2008-11-04 06:10:31 +0000 | [diff] [blame] | 793 | void ExecutionEngine::StoreValueToMemory(const GenericValue &Val, |
| 794 | GenericValue *Ptr, const Type *Ty) { |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 795 | const unsigned StoreBytes = getTargetData()->getTypeStoreSize(Ty); |
| 796 | |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 797 | switch (Ty->getTypeID()) { |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 798 | case Type::IntegerTyID: |
| 799 | StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes); |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 800 | break; |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +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 | 1f8c564 | 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 | 8a43e9e | 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 | |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 815 | *((PointerTy*)Ptr) = Val.PointerVal; |
| 816 | break; |
| 817 | default: |
David Greene | ae7c59a | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 818 | dbgs() << "Cannot store value of type " << *Ty << "!\n"; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 819 | } |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 820 | |
Chris Lattner | b67c958 | 2009-01-22 19:53:00 +0000 | [diff] [blame] | 821 | if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian()) |
Duncan Sands | 8a43e9e | 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 | b67c958 | 2009-01-22 19:53:00 +0000 | [diff] [blame] | 832 | if (sys::isLittleEndianHost()) |
Duncan Sands | 8a43e9e | 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 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 852 | /// FIXME: document |
| 853 | /// |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 854 | void ExecutionEngine::LoadValueFromMemory(GenericValue &Result, |
Duncan Sands | 08bfe26 | 2008-03-10 16:38:37 +0000 | [diff] [blame] | 855 | GenericValue *Ptr, |
| 856 | const Type *Ty) { |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 857 | const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty); |
Duncan Sands | 1eff704 | 2007-12-10 17:43:13 +0000 | [diff] [blame] | 858 | |
Duncan Sands | 8a43e9e | 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; |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 865 | case Type::FloatTyID: |
| 866 | Result.FloatVal = *((float*)Ptr); |
| 867 | break; |
| 868 | case Type::DoubleTyID: |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 869 | Result.DoubleVal = *((double*)Ptr); |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 870 | break; |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 871 | case Type::PointerTyID: |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 872 | Result.PointerVal = *((PointerTy*)Ptr); |
| 873 | break; |
Dale Johannesen | 1abac0d | 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 | 9e4635a | 2007-12-15 17:37:40 +0000 | [diff] [blame] | 876 | // FIXME: Will not trap if loading a signaling NaN. |
Dale Johannesen | 1f8c564 | 2009-03-24 18:16:17 +0000 | [diff] [blame] | 877 | uint64_t y[2]; |
| 878 | memcpy(y, Ptr, 10); |
Duncan Sands | dd65a73 | 2007-11-28 10:36:19 +0000 | [diff] [blame] | 879 | Result.IntVal = APInt(80, 2, y); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 880 | break; |
| 881 | } |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 882 | default: |
Torok Edwin | 7d696d8 | 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()); |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 887 | } |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 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 | ae7c59a | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 894 | DEBUG(dbgs() << "JIT: Initializing " << Addr << " "); |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 895 | DEBUG(Init->dump()); |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 896 | if (isa<UndefValue>(Init)) { |
| 897 | return; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 898 | } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) { |
Robert Bocchino | 7c2b7c7 | 2006-01-20 18:18:40 +0000 | [diff] [blame] | 899 | unsigned ElementSize = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 900 | getTargetData()->getTypeAllocSize(CP->getType()->getElementType()); |
Robert Bocchino | 7c2b7c7 | 2006-01-20 18:18:40 +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 | b6e1dd7 | 2008-02-15 00:57:28 +0000 | [diff] [blame] | 904 | } else if (isa<ConstantAggregateZero>(Init)) { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 905 | memset(Addr, 0, (size_t)getTargetData()->getTypeAllocSize(Init->getType())); |
Chris Lattner | b6e1dd7 | 2008-02-15 00:57:28 +0000 | [diff] [blame] | 906 | return; |
Dan Gohman | 638e378 | 2008-05-20 03:20:09 +0000 | [diff] [blame] | 907 | } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) { |
| 908 | unsigned ElementSize = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 909 | getTargetData()->getTypeAllocSize(CPA->getType()->getElementType()); |
Dan Gohman | 638e378 | 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; |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 919 | } else if (Init->getType()->isFirstClassType()) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 920 | GenericValue Val = getConstantValue(Init); |
| 921 | StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); |
| 922 | return; |
| 923 | } |
| 924 | |
David Greene | ae7c59a | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 925 | dbgs() << "Bad Type: " << *Init->getType() << "\n"; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 926 | llvm_unreachable("Unknown constant type to initialize memory with!"); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 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() { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 934 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 935 | // Loop over all of the global variables in the program, allocating the memory |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 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; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 941 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 942 | if (Modules.size() != 1) { |
| 943 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) { |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 944 | Module &M = *Modules[m]; |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +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 | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 948 | if (GV->hasLocalLinkage() || GV->isDeclaration() || |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +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. |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 963 | if (GVEntry->hasExternalLinkage() || |
| 964 | GVEntry->hasDLLImportLinkage() || |
| 965 | GVEntry->hasDLLExportLinkage()) |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 966 | continue; |
| 967 | |
| 968 | // Otherwise, we know it's linkonce/weak, replace it if this is a strong |
Dale Johannesen | aafce77 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 969 | // symbol. FIXME is this right for common? |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 970 | if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage()) |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 971 | GVEntry = GV; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 972 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 973 | } |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | std::vector<const GlobalValue*> NonCanonicalGlobals; |
| 977 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) { |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame^] | 978 | Module &M = *Modules[m]; |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +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 | |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 993 | if (!I->isDeclaration()) { |
Nicolas Geoffray | 46fa139 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 994 | addGlobalMapping(I, getMemoryForGV(I)); |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +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 | fbee579 | 2009-07-21 08:54:24 +0000 | [diff] [blame] | 999 | sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName())) |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1000 | addGlobalMapping(I, SymAddr); |
| 1001 | else { |
Torok Edwin | 31e2466 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 1002 | llvm_report_error("Could not resolve external global address: " |
| 1003 | +I->getName()); |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +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 | c8ed902 | 2008-10-14 10:04:52 +0000 | [diff] [blame] | 1017 | addGlobalMapping(GV, Ptr); |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1021 | // Now that all of the globals are set up in memory, loop through them all |
| 1022 | // and initialize their contents. |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1023 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 1024 | I != E; ++I) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1025 | if (!I->isDeclaration()) { |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 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. |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 1041 | void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 1042 | void *GA = getPointerToGlobalIfAvailable(GV); |
Chris Lattner | 23c4724 | 2004-02-08 19:33:23 +0000 | [diff] [blame] | 1043 | |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 1044 | if (GA == 0) { |
| 1045 | // If it's not already specified, allocate memory for the global. |
Nicolas Geoffray | 46fa139 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 1046 | GA = getMemoryForGV(GV); |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 1047 | addGlobalMapping(GV, GA); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 1048 | } |
Nicolas Geoffray | 46fa139 | 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 | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1055 | size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy); |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 1056 | NumInitBytes += (unsigned)GVSize; |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 1057 | ++NumGlobals; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1058 | } |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 1059 | |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 1060 | ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE) |
| 1061 | : EE(EE), GlobalAddressMap(this) { |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Jeffrey Yasskin | 23e5fcf | 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 | 4c5b23b | 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 | } |