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