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 | |
Jeffrey Yasskin | 0d5bd59 | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 15 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 9f3ff92 | 2009-08-23 22:49:13 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ExecutionEngine/GenericValue.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 19 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
| 20 | #include "llvm/ExecutionEngine/ObjectCache.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Constants.h" |
| 22 | #include "llvm/IR/DataLayout.h" |
| 23 | #include "llvm/IR/DerivedTypes.h" |
| 24 | #include "llvm/IR/Module.h" |
| 25 | #include "llvm/IR/Operator.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 26 | #include "llvm/IR/ValueHandle.h" |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 27 | #include "llvm/Object/ObjectFile.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 29 | #include "llvm/Support/DynamicLibrary.h" |
Torok Edwin | 31e2466 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Host.h" |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MutexGuard.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 33 | #include "llvm/Support/TargetRegistry.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Dylan Noblesmith | c5b2858 | 2011-05-13 21:51:29 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetMachine.h" |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 36 | #include <cmath> |
| 37 | #include <cstring> |
Chris Lattner | c2ee9b9 | 2003-11-19 21:08:57 +0000 | [diff] [blame] | 38 | using namespace llvm; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 39 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 40 | #define DEBUG_TYPE "jit" |
| 41 | |
Chris Lattner | 3634373 | 2006-12-19 22:43:32 +0000 | [diff] [blame] | 42 | STATISTIC(NumInitBytes, "Number of bytes of global vars initialized"); |
| 43 | STATISTIC(NumGlobals , "Number of global vars initialized"); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 44 | |
Juergen Ributzka | 3543625 | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 45 | // Pin the vtable to this file. |
| 46 | void ObjectCache::anchor() {} |
| 47 | void ObjectBuffer::anchor() {} |
| 48 | void ObjectBufferStream::anchor() {} |
| 49 | |
Jeffrey Yasskin | 4688261 | 2010-02-05 16:19:36 +0000 | [diff] [blame] | 50 | ExecutionEngine *(*ExecutionEngine::JITCtor)( |
| 51 | Module *M, |
| 52 | std::string *ErrorStr, |
| 53 | JITMemoryManager *JMM, |
Jeffrey Yasskin | 4688261 | 2010-02-05 16:19:36 +0000 | [diff] [blame] | 54 | bool GVsWithCode, |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 55 | TargetMachine *TM) = nullptr; |
Daniel Dunbar | 6d13597 | 2010-11-17 16:06:37 +0000 | [diff] [blame] | 56 | ExecutionEngine *(*ExecutionEngine::MCJITCtor)( |
| 57 | Module *M, |
| 58 | std::string *ErrorStr, |
Filip Pizlo | 13a3cf1 | 2013-05-14 19:29:00 +0000 | [diff] [blame] | 59 | RTDyldMemoryManager *MCJMM, |
Daniel Dunbar | 6d13597 | 2010-11-17 16:06:37 +0000 | [diff] [blame] | 60 | bool GVsWithCode, |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 61 | TargetMachine *TM) = nullptr; |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 62 | ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M, |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 63 | std::string *ErrorStr) =nullptr; |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 64 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 65 | ExecutionEngine::ExecutionEngine(Module *M) |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 66 | : EEState(*this), |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 67 | LazyFunctionCreator(nullptr) { |
Jeffrey Yasskin | dc85724 | 2009-10-27 20:30:28 +0000 | [diff] [blame] | 68 | CompilingLazily = false; |
Evan Cheng | 446531e | 2008-09-24 16:25:55 +0000 | [diff] [blame] | 69 | GVCompilationDisabled = false; |
Evan Cheng | 1b088f3 | 2008-06-17 16:49:02 +0000 | [diff] [blame] | 70 | SymbolSearchingDisabled = false; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 71 | |
| 72 | // IR module verification is enabled by default in debug builds, and disabled |
| 73 | // by default in release builds. |
| 74 | #ifndef NDEBUG |
| 75 | VerifyModules = true; |
| 76 | #else |
| 77 | VerifyModules = false; |
| 78 | #endif |
| 79 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 80 | Modules.push_back(M); |
| 81 | assert(M && "Module is null?"); |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Brian Gaeke | 8e53948 | 2003-09-04 22:57:27 +0000 | [diff] [blame] | 84 | ExecutionEngine::~ExecutionEngine() { |
Reid Spencer | d4c0e62 | 2007-03-03 18:19:18 +0000 | [diff] [blame] | 85 | clearAllGlobalMappings(); |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 86 | for (unsigned i = 0, e = Modules.size(); i != e; ++i) |
| 87 | delete Modules[i]; |
Brian Gaeke | 8e53948 | 2003-09-04 22:57:27 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Jeffrey Yasskin | 47b7112 | 2010-03-27 04:53:56 +0000 | [diff] [blame] | 90 | namespace { |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 91 | /// \brief Helper class which uses a value handler to automatically deletes the |
| 92 | /// memory block when the GlobalVariable is destroyed. |
Jeffrey Yasskin | 47b7112 | 2010-03-27 04:53:56 +0000 | [diff] [blame] | 93 | class GVMemoryBlock : public CallbackVH { |
| 94 | GVMemoryBlock(const GlobalVariable *GV) |
| 95 | : CallbackVH(const_cast<GlobalVariable*>(GV)) {} |
| 96 | |
| 97 | public: |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 98 | /// \brief Returns the address the GlobalVariable should be written into. The |
| 99 | /// GVMemoryBlock object prefixes that. |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 100 | static char *Create(const GlobalVariable *GV, const DataLayout& TD) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 101 | Type *ElTy = GV->getType()->getElementType(); |
Jeffrey Yasskin | 47b7112 | 2010-03-27 04:53:56 +0000 | [diff] [blame] | 102 | size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy); |
| 103 | void *RawMemory = ::operator new( |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 104 | DataLayout::RoundUpAlignment(sizeof(GVMemoryBlock), |
Jeffrey Yasskin | 47b7112 | 2010-03-27 04:53:56 +0000 | [diff] [blame] | 105 | TD.getPreferredAlignment(GV)) |
| 106 | + GVSize); |
| 107 | new(RawMemory) GVMemoryBlock(GV); |
| 108 | return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock); |
| 109 | } |
| 110 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 111 | void deleted() override { |
Jeffrey Yasskin | 47b7112 | 2010-03-27 04:53:56 +0000 | [diff] [blame] | 112 | // We allocated with operator new and with some extra memory hanging off the |
| 113 | // end, so don't just delete this. I'm not sure if this is actually |
| 114 | // required. |
| 115 | this->~GVMemoryBlock(); |
| 116 | ::operator delete(this); |
| 117 | } |
| 118 | }; |
| 119 | } // anonymous namespace |
| 120 | |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 121 | char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 122 | return GVMemoryBlock::Create(GV, *getDataLayout()); |
Nicolas Geoffray | 46fa139 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 125 | void ExecutionEngine::addObjectFile(std::unique_ptr<object::ObjectFile> O) { |
| 126 | llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile."); |
| 127 | } |
| 128 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 129 | bool ExecutionEngine::removeModule(Module *M) { |
Craig Topper | 6227d5c | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 130 | for(SmallVectorImpl<Module *>::iterator I = Modules.begin(), |
Devang Patel | 73d0e21 | 2007-10-15 19:56:32 +0000 | [diff] [blame] | 131 | E = Modules.end(); I != E; ++I) { |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 132 | Module *Found = *I; |
| 133 | if (Found == M) { |
Devang Patel | 73d0e21 | 2007-10-15 19:56:32 +0000 | [diff] [blame] | 134 | Modules.erase(I); |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 135 | clearGlobalMappingsFromModule(M); |
| 136 | return true; |
Devang Patel | 73d0e21 | 2007-10-15 19:56:32 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 139 | return false; |
Nate Begeman | 60789e4 | 2009-01-23 19:27:28 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 142 | Function *ExecutionEngine::FindFunctionNamed(const char *FnName) { |
| 143 | for (unsigned i = 0, e = Modules.size(); i != e; ++i) { |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 144 | if (Function *F = Modules[i]->getFunction(FnName)) |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 145 | return F; |
| 146 | } |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 147 | return nullptr; |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 151 | void *ExecutionEngineState::RemoveMapping(const MutexGuard &, |
| 152 | const GlobalValue *ToUnmap) { |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 153 | GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap); |
Jeffrey Yasskin | c89d27a | 2009-10-09 22:10:27 +0000 | [diff] [blame] | 154 | void *OldVal; |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 155 | |
| 156 | // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the |
| 157 | // GlobalAddressMap. |
Jeffrey Yasskin | c89d27a | 2009-10-09 22:10:27 +0000 | [diff] [blame] | 158 | if (I == GlobalAddressMap.end()) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 159 | OldVal = nullptr; |
Jeffrey Yasskin | c89d27a | 2009-10-09 22:10:27 +0000 | [diff] [blame] | 160 | else { |
| 161 | OldVal = I->second; |
| 162 | GlobalAddressMap.erase(I); |
| 163 | } |
| 164 | |
| 165 | GlobalAddressReverseMap.erase(OldVal); |
| 166 | return OldVal; |
| 167 | } |
| 168 | |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 169 | void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) { |
| 170 | MutexGuard locked(lock); |
Evan Cheng | bc4707a | 2008-09-18 07:54:21 +0000 | [diff] [blame] | 171 | |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 172 | DEBUG(dbgs() << "JIT: Map \'" << GV->getName() |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 173 | << "\' to [" << Addr << "]\n";); |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 174 | void *&CurVal = EEState.getGlobalAddressMap(locked)[GV]; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 175 | assert((!CurVal || !Addr) && "GlobalMapping already established!"); |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 176 | CurVal = Addr; |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 177 | |
| 178 | // If we are using the reverse mapping, add it too. |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 179 | if (!EEState.getGlobalAddressReverseMap(locked).empty()) { |
Jeffrey Yasskin | 0d5bd59 | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 180 | AssertingVH<const GlobalValue> &V = |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 181 | EEState.getGlobalAddressReverseMap(locked)[Addr]; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 182 | assert((!V || !GV) && "GlobalMapping already established!"); |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 183 | V = GV; |
| 184 | } |
| 185 | } |
| 186 | |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 187 | void ExecutionEngine::clearAllGlobalMappings() { |
| 188 | MutexGuard locked(lock); |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 189 | |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 190 | EEState.getGlobalAddressMap(locked).clear(); |
| 191 | EEState.getGlobalAddressReverseMap(locked).clear(); |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Nate Begeman | f049e07 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 194 | void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) { |
| 195 | MutexGuard locked(lock); |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 196 | |
| 197 | for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 198 | EEState.RemoveMapping(locked, FI); |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 199 | for (Module::global_iterator GI = M->global_begin(), GE = M->global_end(); |
| 200 | GI != GE; ++GI) |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 201 | EEState.RemoveMapping(locked, GI); |
Nate Begeman | f049e07 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 202 | } |
| 203 | |
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? |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 211 | if (!Addr) |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 212 | return EEState.RemoveMapping(locked, GV); |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 213 | |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 214 | void *&CurVal = Map[GV]; |
Chris Lattner | f4cc309 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 215 | void *OldVal = CurVal; |
| 216 | |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 217 | if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty()) |
| 218 | EEState.getGlobalAddressReverseMap(locked).erase(CurVal); |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 219 | CurVal = Addr; |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 220 | |
| 221 | // If we are using the reverse mapping, add it too. |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 222 | if (!EEState.getGlobalAddressReverseMap(locked).empty()) { |
Jeffrey Yasskin | 0d5bd59 | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 223 | AssertingVH<const GlobalValue> &V = |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 224 | EEState.getGlobalAddressReverseMap(locked)[Addr]; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 225 | assert((!V || !GV) && "GlobalMapping already established!"); |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 226 | V = GV; |
| 227 | } |
Chris Lattner | f4cc309 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 228 | return OldVal; |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 231 | void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) { |
| 232 | MutexGuard locked(lock); |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 233 | |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 234 | ExecutionEngineState::GlobalAddressMapTy::iterator I = |
| 235 | EEState.getGlobalAddressMap(locked).find(GV); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 236 | return I != EEState.getGlobalAddressMap(locked).end() ? I->second : nullptr; |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 239 | const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 240 | MutexGuard locked(lock); |
| 241 | |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 242 | // If we haven't computed the reverse mapping yet, do so first. |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 243 | if (EEState.getGlobalAddressReverseMap(locked).empty()) { |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 244 | for (ExecutionEngineState::GlobalAddressMapTy::iterator |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 245 | I = EEState.getGlobalAddressMap(locked).begin(), |
| 246 | E = EEState.getGlobalAddressMap(locked).end(); I != E; ++I) |
Daniel Dunbar | ab19da4 | 2010-11-13 00:55:42 +0000 | [diff] [blame] | 247 | EEState.getGlobalAddressReverseMap(locked).insert(std::make_pair( |
| 248 | I->second, I->first)); |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Jeffrey Yasskin | 0d5bd59 | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 251 | std::map<void *, AssertingVH<const GlobalValue> >::iterator I = |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 252 | EEState.getGlobalAddressReverseMap(locked).find(Addr); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 253 | return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : nullptr; |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 254 | } |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 255 | |
Jeffrey Yasskin | b193838 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 256 | namespace { |
| 257 | class ArgvArray { |
| 258 | char *Array; |
| 259 | std::vector<char*> Values; |
| 260 | public: |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 261 | ArgvArray() : Array(nullptr) {} |
Jeffrey Yasskin | b193838 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 262 | ~ArgvArray() { clear(); } |
| 263 | void clear() { |
| 264 | delete[] Array; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 265 | Array = nullptr; |
Jeffrey Yasskin | b193838 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 266 | for (size_t I = 0, E = Values.size(); I != E; ++I) { |
| 267 | delete[] Values[I]; |
| 268 | } |
| 269 | Values.clear(); |
| 270 | } |
| 271 | /// Turn a vector of strings into a nice argv style array of pointers to null |
| 272 | /// terminated strings. |
| 273 | void *reset(LLVMContext &C, ExecutionEngine *EE, |
| 274 | const std::vector<std::string> &InputArgv); |
| 275 | }; |
| 276 | } // anonymous namespace |
| 277 | void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE, |
| 278 | const std::vector<std::string> &InputArgv) { |
| 279 | clear(); // Free the old contents. |
Chandler Carruth | 426c2bf | 2012-11-01 09:14:31 +0000 | [diff] [blame] | 280 | unsigned PtrSize = EE->getDataLayout()->getPointerSize(); |
Jeffrey Yasskin | b193838 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 281 | Array = new char[(InputArgv.size()+1)*PtrSize]; |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 282 | |
Jeffrey Yasskin | b193838 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 283 | DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n"); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 284 | Type *SBytePtr = Type::getInt8PtrTy(C); |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 285 | |
| 286 | for (unsigned i = 0; i != InputArgv.size(); ++i) { |
| 287 | unsigned Size = InputArgv[i].size()+1; |
| 288 | char *Dest = new char[Size]; |
Jeffrey Yasskin | b193838 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 289 | Values.push_back(Dest); |
David Greene | ae7c59a | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 290 | DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n"); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 291 | |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 292 | std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); |
| 293 | Dest[Size-1] = 0; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 294 | |
Jeffrey Yasskin | b193838 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 295 | // Endian safe: Array[i] = (PointerTy)Dest; |
| 296 | EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize), |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 297 | SBytePtr); |
| 298 | } |
| 299 | |
| 300 | // Null terminate it |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 301 | EE->StoreValueToMemory(PTOGV(nullptr), |
Jeffrey Yasskin | b193838 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 302 | (GenericValue*)(Array+InputArgv.size()*PtrSize), |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 303 | SBytePtr); |
Jeffrey Yasskin | b193838 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 304 | return Array; |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Chris Lattner | fbd3976 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 307 | void ExecutionEngine::runStaticConstructorsDestructors(Module *module, |
| 308 | bool isDtors) { |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 309 | const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors"; |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 310 | GlobalVariable *GV = module->getNamedGlobal(Name); |
Evan Cheng | 18314dc | 2008-09-30 15:51:21 +0000 | [diff] [blame] | 311 | |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 312 | // If this global has internal linkage, or if it has a use, then it must be |
| 313 | // an old-style (llvmgcc3) static ctor with __main linked in and in use. If |
| 314 | // this is the case, don't execute any of the global ctors, __main will do |
| 315 | // it. |
| 316 | if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return; |
| 317 | |
Nick Lewycky | a040d47 | 2011-04-06 20:38:44 +0000 | [diff] [blame] | 318 | // Should be an array of '{ i32, void ()* }' structs. The first value is |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 319 | // the init priority, which we ignore. |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 320 | ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 321 | if (!InitList) |
Nick Lewycky | 5ea5c61 | 2011-04-11 22:11:20 +0000 | [diff] [blame] | 322 | return; |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 323 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 324 | ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i)); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 325 | if (!CS) continue; |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 326 | |
| 327 | Constant *FP = CS->getOperand(1); |
| 328 | if (FP->isNullValue()) |
Nick Lewycky | 5ea5c61 | 2011-04-11 22:11:20 +0000 | [diff] [blame] | 329 | continue; // Found a sentinal value, ignore. |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 330 | |
| 331 | // Strip off constant expression casts. |
| 332 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) |
| 333 | if (CE->isCast()) |
| 334 | FP = CE->getOperand(0); |
| 335 | |
| 336 | // Execute the ctor/dtor function! |
| 337 | if (Function *F = dyn_cast<Function>(FP)) |
| 338 | runFunction(F, std::vector<GenericValue>()); |
| 339 | |
| 340 | // FIXME: It is marginally lame that we just do nothing here if we see an |
| 341 | // entry we don't recognize. It might not be unreasonable for the verifier |
| 342 | // to not even allow this and just assert here. |
| 343 | } |
Evan Cheng | 18314dc | 2008-09-30 15:51:21 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Evan Cheng | 18314dc | 2008-09-30 15:51:21 +0000 | [diff] [blame] | 346 | void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) { |
| 347 | // Execute global ctors/dtors for each module in the program. |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 348 | for (unsigned i = 0, e = Modules.size(); i != e; ++i) |
| 349 | runStaticConstructorsDestructors(Modules[i], isDtors); |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Dan Gohman | b6e3d6c | 2008-08-26 01:38:29 +0000 | [diff] [blame] | 352 | #ifndef NDEBUG |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 353 | /// isTargetNullPtr - Return whether the target pointer stored at Loc is null. |
| 354 | static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) { |
Chandler Carruth | 426c2bf | 2012-11-01 09:14:31 +0000 | [diff] [blame] | 355 | unsigned PtrSize = EE->getDataLayout()->getPointerSize(); |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 356 | for (unsigned i = 0; i < PtrSize; ++i) |
| 357 | if (*(i + (uint8_t*)Loc)) |
| 358 | return false; |
| 359 | return true; |
| 360 | } |
Dan Gohman | b6e3d6c | 2008-08-26 01:38:29 +0000 | [diff] [blame] | 361 | #endif |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 363 | int ExecutionEngine::runFunctionAsMain(Function *Fn, |
| 364 | const std::vector<std::string> &argv, |
| 365 | const char * const * envp) { |
| 366 | std::vector<GenericValue> GVArgs; |
| 367 | GenericValue GVArgc; |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 368 | GVArgc.IntVal = APInt(32, argv.size()); |
Anton Korobeynikov | 499d8f0 | 2007-06-03 19:17:35 +0000 | [diff] [blame] | 369 | |
| 370 | // Check main() type |
Chris Lattner | f24d099 | 2004-08-16 01:05:35 +0000 | [diff] [blame] | 371 | unsigned NumArgs = Fn->getFunctionType()->getNumParams(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 372 | FunctionType *FTy = Fn->getFunctionType(); |
| 373 | Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo(); |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 374 | |
| 375 | // Check the argument types. |
| 376 | if (NumArgs > 3) |
| 377 | report_fatal_error("Invalid number of arguments of main() supplied"); |
| 378 | if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty) |
| 379 | report_fatal_error("Invalid type for third argument of main() supplied"); |
| 380 | if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty) |
| 381 | report_fatal_error("Invalid type for second argument of main() supplied"); |
| 382 | if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32)) |
| 383 | report_fatal_error("Invalid type for first argument of main() supplied"); |
| 384 | if (!FTy->getReturnType()->isIntegerTy() && |
| 385 | !FTy->getReturnType()->isVoidTy()) |
| 386 | report_fatal_error("Invalid return type of main() supplied"); |
| 387 | |
Jeffrey Yasskin | b193838 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 388 | ArgvArray CArgv; |
| 389 | ArgvArray CEnv; |
Chris Lattner | f24d099 | 2004-08-16 01:05:35 +0000 | [diff] [blame] | 390 | if (NumArgs) { |
| 391 | GVArgs.push_back(GVArgc); // Arg #0 = argc. |
| 392 | if (NumArgs > 1) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 393 | // Arg #1 = argv. |
Jeffrey Yasskin | b193838 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 394 | GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv))); |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 395 | assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) && |
Chris Lattner | f24d099 | 2004-08-16 01:05:35 +0000 | [diff] [blame] | 396 | "argv[0] was null after CreateArgv"); |
| 397 | if (NumArgs > 2) { |
| 398 | std::vector<std::string> EnvVars; |
| 399 | for (unsigned i = 0; envp[i]; ++i) |
| 400 | EnvVars.push_back(envp[i]); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 401 | // Arg #2 = envp. |
Jeffrey Yasskin | b193838 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 402 | GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars))); |
Chris Lattner | f24d099 | 2004-08-16 01:05:35 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | } |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 406 | |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 407 | return runFunction(Fn, GVArgs).IntVal.getZExtValue(); |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 410 | ExecutionEngine *ExecutionEngine::create(Module *M, |
Reid Spencer | d4c0e62 | 2007-03-03 18:19:18 +0000 | [diff] [blame] | 411 | bool ForceInterpreter, |
Evan Cheng | 502f20b | 2008-08-08 08:11:34 +0000 | [diff] [blame] | 412 | std::string *ErrorStr, |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 413 | CodeGenOpt::Level OptLevel, |
| 414 | bool GVsWithCode) { |
Owen Anderson | 8e1fc56 | 2012-03-23 17:40:56 +0000 | [diff] [blame] | 415 | EngineBuilder EB = EngineBuilder(M) |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 416 | .setEngineKind(ForceInterpreter |
| 417 | ? EngineKind::Interpreter |
| 418 | : EngineKind::JIT) |
| 419 | .setErrorStr(ErrorStr) |
| 420 | .setOptLevel(OptLevel) |
Owen Anderson | 8e1fc56 | 2012-03-23 17:40:56 +0000 | [diff] [blame] | 421 | .setAllocateGVsWithCode(GVsWithCode); |
| 422 | |
| 423 | return EB.create(); |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 424 | } |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 425 | |
Dylan Noblesmith | f5895e9 | 2011-05-13 22:02:53 +0000 | [diff] [blame] | 426 | /// createJIT - This is the factory method for creating a JIT for the current |
| 427 | /// machine, it does not fall back to the interpreter. This takes ownership |
| 428 | /// of the module. |
| 429 | ExecutionEngine *ExecutionEngine::createJIT(Module *M, |
| 430 | std::string *ErrorStr, |
| 431 | JITMemoryManager *JMM, |
Dylan Noblesmith | d95e67d | 2011-12-01 21:49:21 +0000 | [diff] [blame] | 432 | CodeGenOpt::Level OL, |
Dylan Noblesmith | f5895e9 | 2011-05-13 22:02:53 +0000 | [diff] [blame] | 433 | bool GVsWithCode, |
Evan Cheng | 4396613 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 434 | Reloc::Model RM, |
Dylan Noblesmith | f5895e9 | 2011-05-13 22:02:53 +0000 | [diff] [blame] | 435 | CodeModel::Model CMM) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 436 | if (!ExecutionEngine::JITCtor) { |
Dylan Noblesmith | f5895e9 | 2011-05-13 22:02:53 +0000 | [diff] [blame] | 437 | if (ErrorStr) |
| 438 | *ErrorStr = "JIT has not been linked in."; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 439 | return nullptr; |
Dylan Noblesmith | f5895e9 | 2011-05-13 22:02:53 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | // Use the defaults for extra parameters. Users can use EngineBuilder to |
| 443 | // set them. |
Owen Anderson | 8e1fc56 | 2012-03-23 17:40:56 +0000 | [diff] [blame] | 444 | EngineBuilder EB(M); |
| 445 | EB.setEngineKind(EngineKind::JIT); |
| 446 | EB.setErrorStr(ErrorStr); |
| 447 | EB.setRelocationModel(RM); |
| 448 | EB.setCodeModel(CMM); |
| 449 | EB.setAllocateGVsWithCode(GVsWithCode); |
| 450 | EB.setOptLevel(OL); |
| 451 | EB.setJITMemoryManager(JMM); |
Dylan Noblesmith | f5895e9 | 2011-05-13 22:02:53 +0000 | [diff] [blame] | 452 | |
Peter Collingbourne | d40e103 | 2011-12-07 23:58:57 +0000 | [diff] [blame] | 453 | // TODO: permit custom TargetOptions here |
Owen Anderson | 8e1fc56 | 2012-03-23 17:40:56 +0000 | [diff] [blame] | 454 | TargetMachine *TM = EB.selectTarget(); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 455 | if (!TM || (ErrorStr && ErrorStr->length() > 0)) return nullptr; |
Dylan Noblesmith | f5895e9 | 2011-05-13 22:02:53 +0000 | [diff] [blame] | 456 | |
Dylan Noblesmith | 9ea4717 | 2011-12-12 04:20:36 +0000 | [diff] [blame] | 457 | return ExecutionEngine::JITCtor(M, ErrorStr, JMM, GVsWithCode, TM); |
Dylan Noblesmith | f5895e9 | 2011-05-13 22:02:53 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Owen Anderson | 8e1fc56 | 2012-03-23 17:40:56 +0000 | [diff] [blame] | 460 | ExecutionEngine *EngineBuilder::create(TargetMachine *TM) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 461 | std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership. |
Benjamin Kramer | 0f55449 | 2012-04-08 14:53:14 +0000 | [diff] [blame] | 462 | |
Nick Lewycky | 6456d86 | 2008-03-08 02:49:45 +0000 | [diff] [blame] | 463 | // Make sure we can resolve symbols in the program as well. The zero arg |
| 464 | // to the function tells DynamicLibrary to load the program, not a library. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 465 | if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr)) |
| 466 | return nullptr; |
Nick Lewycky | 6456d86 | 2008-03-08 02:49:45 +0000 | [diff] [blame] | 467 | |
Filip Pizlo | 13a3cf1 | 2013-05-14 19:29:00 +0000 | [diff] [blame] | 468 | assert(!(JMM && MCJMM)); |
| 469 | |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 470 | // If the user specified a memory manager but didn't specify which engine to |
| 471 | // create, we assume they only want the JIT, and we fail if they only want |
| 472 | // the interpreter. |
Filip Pizlo | 13a3cf1 | 2013-05-14 19:29:00 +0000 | [diff] [blame] | 473 | if (JMM || MCJMM) { |
Chris Lattner | fbd3976 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 474 | if (WhichEngine & EngineKind::JIT) |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 475 | WhichEngine = EngineKind::JIT; |
Chris Lattner | fbd3976 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 476 | else { |
Chris Lattner | c72efbe | 2009-09-23 02:03:49 +0000 | [diff] [blame] | 477 | if (ErrorStr) |
| 478 | *ErrorStr = "Cannot create an interpreter with a memory manager."; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 479 | return nullptr; |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
Filip Pizlo | 13a3cf1 | 2013-05-14 19:29:00 +0000 | [diff] [blame] | 482 | |
| 483 | if (MCJMM && ! UseMCJIT) { |
| 484 | if (ErrorStr) |
| 485 | *ErrorStr = |
| 486 | "Cannot create a legacy JIT with a runtime dyld memory " |
| 487 | "manager."; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 488 | return nullptr; |
Filip Pizlo | 13a3cf1 | 2013-05-14 19:29:00 +0000 | [diff] [blame] | 489 | } |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 490 | |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 491 | // Unless the interpreter was explicitly selected or the JIT is not linked, |
| 492 | // try making a JIT. |
Benjamin Kramer | 0f55449 | 2012-04-08 14:53:14 +0000 | [diff] [blame] | 493 | if ((WhichEngine & EngineKind::JIT) && TheTM) { |
Dylan Noblesmith | 9ea4717 | 2011-12-12 04:20:36 +0000 | [diff] [blame] | 494 | Triple TT(M->getTargetTriple()); |
Owen Anderson | 8e1fc56 | 2012-03-23 17:40:56 +0000 | [diff] [blame] | 495 | if (!TM->getTarget().hasJIT()) { |
| 496 | errs() << "WARNING: This target JIT is not designed for the host" |
| 497 | << " you are running. If bad things happen, please choose" |
| 498 | << " a different -march switch.\n"; |
| 499 | } |
Dylan Noblesmith | 9ea4717 | 2011-12-12 04:20:36 +0000 | [diff] [blame] | 500 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 501 | ExecutionEngine *EE = nullptr; |
| 502 | if (UseMCJIT && ExecutionEngine::MCJITCtor) |
| 503 | EE = ExecutionEngine::MCJITCtor(M, ErrorStr, MCJMM ? MCJMM : JMM, |
| 504 | AllocateGVsWithCode, TheTM.release()); |
| 505 | else if (ExecutionEngine::JITCtor) |
| 506 | EE = ExecutionEngine::JITCtor(M, ErrorStr, JMM, |
| 507 | AllocateGVsWithCode, TheTM.release()); |
| 508 | |
| 509 | if (EE) { |
| 510 | EE->setVerifyModules(VerifyModules); |
| 511 | return EE; |
Chris Lattner | fbd3976 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 512 | } |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | // If we can't make a JIT and we didn't request one specifically, try making |
| 516 | // an interpreter instead. |
Chris Lattner | fbd3976 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 517 | if (WhichEngine & EngineKind::Interpreter) { |
| 518 | if (ExecutionEngine::InterpCtor) |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 519 | return ExecutionEngine::InterpCtor(M, ErrorStr); |
Chris Lattner | c72efbe | 2009-09-23 02:03:49 +0000 | [diff] [blame] | 520 | if (ErrorStr) |
| 521 | *ErrorStr = "Interpreter has not been linked in."; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 522 | return nullptr; |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 523 | } |
Chris Lattner | c72efbe | 2009-09-23 02:03:49 +0000 | [diff] [blame] | 524 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 525 | if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::JITCtor && |
| 526 | !ExecutionEngine::MCJITCtor) { |
Chris Lattner | c72efbe | 2009-09-23 02:03:49 +0000 | [diff] [blame] | 527 | if (ErrorStr) |
| 528 | *ErrorStr = "JIT has not been linked in."; |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 531 | return nullptr; |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 534 | void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { |
Brian Gaeke | 37df460 | 2003-08-13 18:16:14 +0000 | [diff] [blame] | 535 | if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 536 | return getPointerToFunction(F); |
| 537 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 538 | MutexGuard locked(lock); |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 539 | if (void *P = EEState.getGlobalAddressMap(locked)[GV]) |
| 540 | return P; |
Jeff Cohen | 68835dd | 2006-02-07 05:11:57 +0000 | [diff] [blame] | 541 | |
| 542 | // Global variable might have been added since interpreter started. |
| 543 | if (GlobalVariable *GVar = |
| 544 | const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV))) |
| 545 | EmitGlobalVariable(GVar); |
| 546 | else |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 547 | llvm_unreachable("Global hasn't had an address allocated yet!"); |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 548 | |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 549 | return EEState.getGlobalAddressMap(locked)[GV]; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 552 | /// \brief Converts a Constant* into a GenericValue, including handling of |
| 553 | /// ConstantExpr values. |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 554 | GenericValue ExecutionEngine::getConstantValue(const Constant *C) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 555 | // If its undefined, return the garbage. |
Jay Foad | 5b37012 | 2010-01-15 08:32:58 +0000 | [diff] [blame] | 556 | if (isa<UndefValue>(C)) { |
| 557 | GenericValue Result; |
| 558 | switch (C->getType()->getTypeID()) { |
Nadav Rotem | 953783e | 2013-04-01 15:53:30 +0000 | [diff] [blame] | 559 | default: |
| 560 | break; |
Jay Foad | 5b37012 | 2010-01-15 08:32:58 +0000 | [diff] [blame] | 561 | case Type::IntegerTyID: |
| 562 | case Type::X86_FP80TyID: |
| 563 | case Type::FP128TyID: |
| 564 | case Type::PPC_FP128TyID: |
| 565 | // Although the value is undefined, we still have to construct an APInt |
| 566 | // with the correct bit width. |
| 567 | Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0); |
| 568 | break; |
Elena Demikhovsky | 3c5ce29 | 2013-09-12 10:48:23 +0000 | [diff] [blame] | 569 | case Type::StructTyID: { |
| 570 | // if the whole struct is 'undef' just reserve memory for the value. |
| 571 | if(StructType *STy = dyn_cast<StructType>(C->getType())) { |
| 572 | unsigned int elemNum = STy->getNumElements(); |
| 573 | Result.AggregateVal.resize(elemNum); |
| 574 | for (unsigned int i = 0; i < elemNum; ++i) { |
| 575 | Type *ElemTy = STy->getElementType(i); |
| 576 | if (ElemTy->isIntegerTy()) |
| 577 | Result.AggregateVal[i].IntVal = |
| 578 | APInt(ElemTy->getPrimitiveSizeInBits(), 0); |
| 579 | else if (ElemTy->isAggregateType()) { |
| 580 | const Constant *ElemUndef = UndefValue::get(ElemTy); |
| 581 | Result.AggregateVal[i] = getConstantValue(ElemUndef); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | break; |
Nadav Rotem | 953783e | 2013-04-01 15:53:30 +0000 | [diff] [blame] | 587 | case Type::VectorTyID: |
| 588 | // if the whole vector is 'undef' just reserve memory for the value. |
| 589 | const VectorType* VTy = dyn_cast<VectorType>(C->getType()); |
| 590 | const Type *ElemTy = VTy->getElementType(); |
| 591 | unsigned int elemNum = VTy->getNumElements(); |
| 592 | Result.AggregateVal.resize(elemNum); |
| 593 | if (ElemTy->isIntegerTy()) |
| 594 | for (unsigned int i = 0; i < elemNum; ++i) |
Elena Demikhovsky | 3c5ce29 | 2013-09-12 10:48:23 +0000 | [diff] [blame] | 595 | Result.AggregateVal[i].IntVal = |
Nadav Rotem | 953783e | 2013-04-01 15:53:30 +0000 | [diff] [blame] | 596 | APInt(ElemTy->getPrimitiveSizeInBits(), 0); |
Jay Foad | 5b37012 | 2010-01-15 08:32:58 +0000 | [diff] [blame] | 597 | break; |
| 598 | } |
| 599 | return Result; |
| 600 | } |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 601 | |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 602 | // Otherwise, if the value is a ConstantExpr... |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 603 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 604 | Constant *Op0 = CE->getOperand(0); |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 605 | switch (CE->getOpcode()) { |
| 606 | case Instruction::GetElementPtr: { |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 607 | // Compute the index |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 608 | GenericValue Result = getConstantValue(Op0); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 609 | APInt Offset(DL->getPointerSizeInBits(), 0); |
| 610 | cast<GEPOperator>(CE)->accumulateConstantOffset(*DL, Offset); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 611 | |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 612 | char* tmp = (char*) Result.PointerVal; |
Nuno Lopes | 98281a2 | 2012-12-30 16:25:48 +0000 | [diff] [blame] | 613 | Result = PTOGV(tmp + Offset.getSExtValue()); |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 614 | return Result; |
| 615 | } |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 616 | case Instruction::Trunc: { |
| 617 | GenericValue GV = getConstantValue(Op0); |
| 618 | uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); |
| 619 | GV.IntVal = GV.IntVal.trunc(BitWidth); |
| 620 | return GV; |
| 621 | } |
| 622 | case Instruction::ZExt: { |
| 623 | GenericValue GV = getConstantValue(Op0); |
| 624 | uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); |
| 625 | GV.IntVal = GV.IntVal.zext(BitWidth); |
| 626 | return GV; |
| 627 | } |
| 628 | case Instruction::SExt: { |
| 629 | GenericValue GV = getConstantValue(Op0); |
| 630 | uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); |
| 631 | GV.IntVal = GV.IntVal.sext(BitWidth); |
| 632 | return GV; |
| 633 | } |
| 634 | case Instruction::FPTrunc: { |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 635 | // FIXME long double |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 636 | GenericValue GV = getConstantValue(Op0); |
| 637 | GV.FloatVal = float(GV.DoubleVal); |
| 638 | return GV; |
| 639 | } |
| 640 | case Instruction::FPExt:{ |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 641 | // FIXME long double |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 642 | GenericValue GV = getConstantValue(Op0); |
| 643 | GV.DoubleVal = double(GV.FloatVal); |
| 644 | return GV; |
| 645 | } |
| 646 | case Instruction::UIToFP: { |
| 647 | GenericValue GV = getConstantValue(Op0); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 648 | if (CE->getType()->isFloatTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 649 | GV.FloatVal = float(GV.IntVal.roundToDouble()); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 650 | else if (CE->getType()->isDoubleTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 651 | GV.DoubleVal = GV.IntVal.roundToDouble(); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 652 | else if (CE->getType()->isX86_FP80Ty()) { |
Benjamin Kramer | 3069cbf | 2010-12-04 15:28:22 +0000 | [diff] [blame] | 653 | APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended); |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 654 | (void)apf.convertFromAPInt(GV.IntVal, |
Dan Gohman | 6282406 | 2008-02-29 01:27:13 +0000 | [diff] [blame] | 655 | false, |
| 656 | APFloat::rmNearestTiesToEven); |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 657 | GV.IntVal = apf.bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 658 | } |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 659 | return GV; |
| 660 | } |
| 661 | case Instruction::SIToFP: { |
| 662 | GenericValue GV = getConstantValue(Op0); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 663 | if (CE->getType()->isFloatTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 664 | GV.FloatVal = float(GV.IntVal.signedRoundToDouble()); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 665 | else if (CE->getType()->isDoubleTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 666 | GV.DoubleVal = GV.IntVal.signedRoundToDouble(); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 667 | else if (CE->getType()->isX86_FP80Ty()) { |
Benjamin Kramer | 3069cbf | 2010-12-04 15:28:22 +0000 | [diff] [blame] | 668 | APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended); |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 669 | (void)apf.convertFromAPInt(GV.IntVal, |
Dan Gohman | 6282406 | 2008-02-29 01:27:13 +0000 | [diff] [blame] | 670 | true, |
| 671 | APFloat::rmNearestTiesToEven); |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 672 | GV.IntVal = apf.bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 673 | } |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 674 | return GV; |
| 675 | } |
| 676 | case Instruction::FPToUI: // double->APInt conversion handles sign |
| 677 | case Instruction::FPToSI: { |
| 678 | GenericValue GV = getConstantValue(Op0); |
| 679 | uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 680 | if (Op0->getType()->isFloatTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 681 | GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 682 | else if (Op0->getType()->isDoubleTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 683 | GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 684 | else if (Op0->getType()->isX86_FP80Ty()) { |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 685 | APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 686 | uint64_t v; |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 687 | bool ignored; |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 688 | (void)apf.convertToInteger(&v, BitWidth, |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 689 | CE->getOpcode()==Instruction::FPToSI, |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 690 | APFloat::rmTowardZero, &ignored); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 691 | GV.IntVal = v; // endian? |
| 692 | } |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 693 | return GV; |
| 694 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 695 | case Instruction::PtrToInt: { |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 696 | GenericValue GV = getConstantValue(Op0); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 697 | uint32_t PtrWidth = DL->getTypeSizeInBits(Op0->getType()); |
Eli Friedman | bbc6e67 | 2012-10-30 22:21:55 +0000 | [diff] [blame] | 698 | assert(PtrWidth <= 64 && "Bad pointer width"); |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 699 | GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal)); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 700 | uint32_t IntWidth = DL->getTypeSizeInBits(CE->getType()); |
Eli Friedman | bbc6e67 | 2012-10-30 22:21:55 +0000 | [diff] [blame] | 701 | GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth); |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 702 | return GV; |
| 703 | } |
| 704 | case Instruction::IntToPtr: { |
| 705 | GenericValue GV = getConstantValue(Op0); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 706 | uint32_t PtrWidth = DL->getTypeSizeInBits(CE->getType()); |
Eli Friedman | bbc6e67 | 2012-10-30 22:21:55 +0000 | [diff] [blame] | 707 | GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth); |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 708 | assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width"); |
| 709 | GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue())); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 710 | return GV; |
| 711 | } |
| 712 | case Instruction::BitCast: { |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 713 | GenericValue GV = getConstantValue(Op0); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 714 | Type* DestTy = CE->getType(); |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 715 | switch (Op0->getType()->getTypeID()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 716 | default: llvm_unreachable("Invalid bitcast operand"); |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 717 | case Type::IntegerTyID: |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 718 | assert(DestTy->isFloatingPointTy() && "invalid bitcast"); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 719 | if (DestTy->isFloatTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 720 | GV.FloatVal = GV.IntVal.bitsToFloat(); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 721 | else if (DestTy->isDoubleTy()) |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 722 | GV.DoubleVal = GV.IntVal.bitsToDouble(); |
| 723 | break; |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 724 | case Type::FloatTyID: |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 725 | assert(DestTy->isIntegerTy(32) && "Invalid bitcast"); |
Jay Foad | e4d19c9 | 2010-11-28 21:04:48 +0000 | [diff] [blame] | 726 | GV.IntVal = APInt::floatToBits(GV.FloatVal); |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 727 | break; |
| 728 | case Type::DoubleTyID: |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 729 | assert(DestTy->isIntegerTy(64) && "Invalid bitcast"); |
Jay Foad | e4d19c9 | 2010-11-28 21:04:48 +0000 | [diff] [blame] | 730 | GV.IntVal = APInt::doubleToBits(GV.DoubleVal); |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 731 | break; |
| 732 | case Type::PointerTyID: |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 733 | assert(DestTy->isPointerTy() && "Invalid bitcast"); |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 734 | break; // getConstantValue(Op0) above already converted it |
| 735 | } |
| 736 | return GV; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 737 | } |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 738 | case Instruction::Add: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 739 | case Instruction::FAdd: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 740 | case Instruction::Sub: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 741 | case Instruction::FSub: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 742 | case Instruction::Mul: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 743 | case Instruction::FMul: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 744 | case Instruction::UDiv: |
| 745 | case Instruction::SDiv: |
| 746 | case Instruction::URem: |
| 747 | case Instruction::SRem: |
| 748 | case Instruction::And: |
| 749 | case Instruction::Or: |
| 750 | case Instruction::Xor: { |
| 751 | GenericValue LHS = getConstantValue(Op0); |
| 752 | GenericValue RHS = getConstantValue(CE->getOperand(1)); |
| 753 | GenericValue GV; |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 754 | switch (CE->getOperand(0)->getType()->getTypeID()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 755 | default: llvm_unreachable("Bad add type!"); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 756 | case Type::IntegerTyID: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 757 | switch (CE->getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 758 | default: llvm_unreachable("Invalid integer opcode"); |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 759 | case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break; |
| 760 | case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break; |
| 761 | case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break; |
| 762 | case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break; |
| 763 | case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break; |
| 764 | case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break; |
| 765 | case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break; |
| 766 | case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break; |
| 767 | case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break; |
| 768 | case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break; |
| 769 | } |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 770 | break; |
| 771 | case Type::FloatTyID: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 772 | switch (CE->getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 773 | default: llvm_unreachable("Invalid float opcode"); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 774 | case Instruction::FAdd: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 775 | GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 776 | case Instruction::FSub: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 777 | GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 778 | case Instruction::FMul: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 779 | GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break; |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 780 | case Instruction::FDiv: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 781 | GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break; |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 782 | case Instruction::FRem: |
Chris Lattner | 87565c1 | 2010-05-15 17:10:24 +0000 | [diff] [blame] | 783 | GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break; |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 784 | } |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 785 | break; |
| 786 | case Type::DoubleTyID: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 787 | switch (CE->getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 788 | default: llvm_unreachable("Invalid double opcode"); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 789 | case Instruction::FAdd: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 790 | GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 791 | case Instruction::FSub: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 792 | GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 793 | case Instruction::FMul: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 794 | GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break; |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 795 | case Instruction::FDiv: |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 796 | GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break; |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 797 | case Instruction::FRem: |
Chris Lattner | 87565c1 | 2010-05-15 17:10:24 +0000 | [diff] [blame] | 798 | GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break; |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 799 | } |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 800 | break; |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 801 | case Type::X86_FP80TyID: |
| 802 | case Type::PPC_FP128TyID: |
| 803 | case Type::FP128TyID: { |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 804 | const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics(); |
| 805 | APFloat apfLHS = APFloat(Sem, LHS.IntVal); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 806 | switch (CE->getOpcode()) { |
Daniel Dunbar | ab19da4 | 2010-11-13 00:55:42 +0000 | [diff] [blame] | 807 | default: llvm_unreachable("Invalid long double opcode"); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 808 | case Instruction::FAdd: |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 809 | apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven); |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 810 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 811 | break; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 812 | case Instruction::FSub: |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 813 | apfLHS.subtract(APFloat(Sem, RHS.IntVal), |
| 814 | APFloat::rmNearestTiesToEven); |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 815 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 816 | break; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 817 | case Instruction::FMul: |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 818 | apfLHS.multiply(APFloat(Sem, RHS.IntVal), |
| 819 | APFloat::rmNearestTiesToEven); |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 820 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 821 | break; |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 822 | case Instruction::FDiv: |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 823 | apfLHS.divide(APFloat(Sem, RHS.IntVal), |
| 824 | APFloat::rmNearestTiesToEven); |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 825 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 826 | break; |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 827 | case Instruction::FRem: |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 828 | apfLHS.mod(APFloat(Sem, RHS.IntVal), |
| 829 | APFloat::rmNearestTiesToEven); |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 830 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 831 | break; |
| 832 | } |
| 833 | } |
| 834 | break; |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 835 | } |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 836 | return GV; |
| 837 | } |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 838 | default: |
| 839 | break; |
| 840 | } |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 841 | |
| 842 | SmallString<256> Msg; |
| 843 | raw_svector_ostream OS(Msg); |
| 844 | OS << "ConstantExpr not handled: " << *CE; |
| 845 | report_fatal_error(OS.str()); |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 846 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 847 | |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 848 | // Otherwise, we have a simple constant. |
Reid Spencer | bce30f1 | 2007-03-06 22:23:15 +0000 | [diff] [blame] | 849 | GenericValue Result; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 850 | switch (C->getType()->getTypeID()) { |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 851 | case Type::FloatTyID: |
| 852 | Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 853 | break; |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 854 | case Type::DoubleTyID: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 855 | Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble(); |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 856 | break; |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 857 | case Type::X86_FP80TyID: |
| 858 | case Type::FP128TyID: |
| 859 | case Type::PPC_FP128TyID: |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 860 | Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt(); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 861 | break; |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 862 | case Type::IntegerTyID: |
| 863 | Result.IntVal = cast<ConstantInt>(C)->getValue(); |
| 864 | break; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 865 | case Type::PointerTyID: |
Reid Spencer | 40cf2f9 | 2004-07-18 00:41:27 +0000 | [diff] [blame] | 866 | if (isa<ConstantPointerNull>(C)) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 867 | Result.PointerVal = nullptr; |
Reid Spencer | 40cf2f9 | 2004-07-18 00:41:27 +0000 | [diff] [blame] | 868 | else if (const Function *F = dyn_cast<Function>(C)) |
| 869 | Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F))); |
Chris Lattner | f32a6a3 | 2009-10-29 05:26:09 +0000 | [diff] [blame] | 870 | else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) |
Reid Spencer | 40cf2f9 | 2004-07-18 00:41:27 +0000 | [diff] [blame] | 871 | Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV))); |
Chris Lattner | f32a6a3 | 2009-10-29 05:26:09 +0000 | [diff] [blame] | 872 | else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
| 873 | Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>( |
| 874 | BA->getBasicBlock()))); |
Reid Spencer | 40cf2f9 | 2004-07-18 00:41:27 +0000 | [diff] [blame] | 875 | else |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 876 | llvm_unreachable("Unknown constant pointer type!"); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 877 | break; |
Nadav Rotem | 953783e | 2013-04-01 15:53:30 +0000 | [diff] [blame] | 878 | case Type::VectorTyID: { |
| 879 | unsigned elemNum; |
| 880 | Type* ElemTy; |
| 881 | const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C); |
| 882 | const ConstantVector *CV = dyn_cast<ConstantVector>(C); |
| 883 | const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C); |
| 884 | |
| 885 | if (CDV) { |
| 886 | elemNum = CDV->getNumElements(); |
| 887 | ElemTy = CDV->getElementType(); |
| 888 | } else if (CV || CAZ) { |
| 889 | VectorType* VTy = dyn_cast<VectorType>(C->getType()); |
| 890 | elemNum = VTy->getNumElements(); |
| 891 | ElemTy = VTy->getElementType(); |
| 892 | } else { |
| 893 | llvm_unreachable("Unknown constant vector type!"); |
| 894 | } |
| 895 | |
| 896 | Result.AggregateVal.resize(elemNum); |
| 897 | // Check if vector holds floats. |
| 898 | if(ElemTy->isFloatTy()) { |
| 899 | if (CAZ) { |
| 900 | GenericValue floatZero; |
| 901 | floatZero.FloatVal = 0.f; |
| 902 | std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(), |
| 903 | floatZero); |
| 904 | break; |
| 905 | } |
| 906 | if(CV) { |
| 907 | for (unsigned i = 0; i < elemNum; ++i) |
| 908 | if (!isa<UndefValue>(CV->getOperand(i))) |
| 909 | Result.AggregateVal[i].FloatVal = cast<ConstantFP>( |
| 910 | CV->getOperand(i))->getValueAPF().convertToFloat(); |
| 911 | break; |
| 912 | } |
| 913 | if(CDV) |
| 914 | for (unsigned i = 0; i < elemNum; ++i) |
| 915 | Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i); |
| 916 | |
| 917 | break; |
| 918 | } |
| 919 | // Check if vector holds doubles. |
| 920 | if (ElemTy->isDoubleTy()) { |
| 921 | if (CAZ) { |
| 922 | GenericValue doubleZero; |
| 923 | doubleZero.DoubleVal = 0.0; |
| 924 | std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(), |
| 925 | doubleZero); |
| 926 | break; |
| 927 | } |
| 928 | if(CV) { |
| 929 | for (unsigned i = 0; i < elemNum; ++i) |
| 930 | if (!isa<UndefValue>(CV->getOperand(i))) |
| 931 | Result.AggregateVal[i].DoubleVal = cast<ConstantFP>( |
| 932 | CV->getOperand(i))->getValueAPF().convertToDouble(); |
| 933 | break; |
| 934 | } |
| 935 | if(CDV) |
| 936 | for (unsigned i = 0; i < elemNum; ++i) |
| 937 | Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i); |
| 938 | |
| 939 | break; |
| 940 | } |
| 941 | // Check if vector holds integers. |
| 942 | if (ElemTy->isIntegerTy()) { |
| 943 | if (CAZ) { |
| 944 | GenericValue intZero; |
| 945 | intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull); |
| 946 | std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(), |
| 947 | intZero); |
| 948 | break; |
| 949 | } |
| 950 | if(CV) { |
| 951 | for (unsigned i = 0; i < elemNum; ++i) |
| 952 | if (!isa<UndefValue>(CV->getOperand(i))) |
| 953 | Result.AggregateVal[i].IntVal = cast<ConstantInt>( |
| 954 | CV->getOperand(i))->getValue(); |
| 955 | else { |
| 956 | Result.AggregateVal[i].IntVal = |
| 957 | APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0); |
| 958 | } |
| 959 | break; |
| 960 | } |
| 961 | if(CDV) |
| 962 | for (unsigned i = 0; i < elemNum; ++i) |
| 963 | Result.AggregateVal[i].IntVal = APInt( |
| 964 | CDV->getElementType()->getPrimitiveSizeInBits(), |
| 965 | CDV->getElementAsInteger(i)); |
| 966 | |
| 967 | break; |
| 968 | } |
| 969 | llvm_unreachable("Unknown constant pointer type!"); |
| 970 | } |
| 971 | break; |
| 972 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 973 | default: |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 974 | SmallString<256> Msg; |
| 975 | raw_svector_ostream OS(Msg); |
| 976 | OS << "ERROR: Constant unimplemented for type: " << *C->getType(); |
| 977 | report_fatal_error(OS.str()); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 978 | } |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 979 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 980 | return Result; |
| 981 | } |
| 982 | |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 983 | /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst |
| 984 | /// with the integer held in IntVal. |
| 985 | static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, |
| 986 | unsigned StoreBytes) { |
| 987 | assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!"); |
Roman Divacky | 5932429 | 2012-09-05 22:26:57 +0000 | [diff] [blame] | 988 | const uint8_t *Src = (const uint8_t *)IntVal.getRawData(); |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 989 | |
Rafael Espindola | 21a01d1 | 2013-04-15 14:44:24 +0000 | [diff] [blame] | 990 | if (sys::IsLittleEndianHost) { |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 991 | // Little-endian host - the source is ordered from LSB to MSB. Order the |
| 992 | // destination from LSB to MSB: Do a straight copy. |
| 993 | memcpy(Dst, Src, StoreBytes); |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 994 | } else { |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 995 | // Big-endian host - the source is an array of 64 bit words ordered from |
| 996 | // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination |
| 997 | // from MSB to LSB: Reverse the word order, but not the bytes in a word. |
| 998 | while (StoreBytes > sizeof(uint64_t)) { |
| 999 | StoreBytes -= sizeof(uint64_t); |
| 1000 | // May not be aligned so use memcpy. |
| 1001 | memcpy(Dst + StoreBytes, Src, sizeof(uint64_t)); |
| 1002 | Src += sizeof(uint64_t); |
| 1003 | } |
| 1004 | |
| 1005 | memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes); |
| 1006 | } |
| 1007 | } |
| 1008 | |
Evan Cheng | 89687e3 | 2008-11-04 06:10:31 +0000 | [diff] [blame] | 1009 | void ExecutionEngine::StoreValueToMemory(const GenericValue &Val, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1010 | GenericValue *Ptr, Type *Ty) { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1011 | const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty); |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 1012 | |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 1013 | switch (Ty->getTypeID()) { |
Nadav Rotem | 953783e | 2013-04-01 15:53:30 +0000 | [diff] [blame] | 1014 | default: |
| 1015 | dbgs() << "Cannot store value of type " << *Ty << "!\n"; |
| 1016 | break; |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 1017 | case Type::IntegerTyID: |
| 1018 | StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes); |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 1019 | break; |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 1020 | case Type::FloatTyID: |
| 1021 | *((float*)Ptr) = Val.FloatVal; |
| 1022 | break; |
| 1023 | case Type::DoubleTyID: |
| 1024 | *((double*)Ptr) = Val.DoubleVal; |
| 1025 | break; |
Dale Johannesen | 1f8c564 | 2009-03-24 18:16:17 +0000 | [diff] [blame] | 1026 | case Type::X86_FP80TyID: |
| 1027 | memcpy(Ptr, Val.IntVal.getRawData(), 10); |
| 1028 | break; |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 1029 | case Type::PointerTyID: |
| 1030 | // Ensure 64 bit target pointers are fully initialized on 32 bit hosts. |
| 1031 | if (StoreBytes != sizeof(PointerTy)) |
Chandler Carruth | 80d9e07 | 2011-04-28 08:37:18 +0000 | [diff] [blame] | 1032 | memset(&(Ptr->PointerVal), 0, StoreBytes); |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 1033 | |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 1034 | *((PointerTy*)Ptr) = Val.PointerVal; |
| 1035 | break; |
Nadav Rotem | 953783e | 2013-04-01 15:53:30 +0000 | [diff] [blame] | 1036 | case Type::VectorTyID: |
| 1037 | for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) { |
| 1038 | if (cast<VectorType>(Ty)->getElementType()->isDoubleTy()) |
| 1039 | *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal; |
| 1040 | if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) |
| 1041 | *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal; |
| 1042 | if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) { |
| 1043 | unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8; |
| 1044 | StoreIntToMemory(Val.AggregateVal[i].IntVal, |
| 1045 | (uint8_t*)Ptr + numOfBytes*i, numOfBytes); |
| 1046 | } |
| 1047 | } |
| 1048 | break; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1049 | } |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 1050 | |
Rafael Espindola | 21a01d1 | 2013-04-15 14:44:24 +0000 | [diff] [blame] | 1051 | if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian()) |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 1052 | // Host and target are different endian - reverse the stored bytes. |
| 1053 | std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr); |
| 1054 | } |
| 1055 | |
| 1056 | /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting |
| 1057 | /// from Src into IntVal, which is assumed to be wide enough and to hold zero. |
| 1058 | static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) { |
| 1059 | assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!"); |
David Greene | c2680be | 2013-01-14 21:04:45 +0000 | [diff] [blame] | 1060 | uint8_t *Dst = reinterpret_cast<uint8_t *>( |
| 1061 | const_cast<uint64_t *>(IntVal.getRawData())); |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 1062 | |
Rafael Espindola | 21a01d1 | 2013-04-15 14:44:24 +0000 | [diff] [blame] | 1063 | if (sys::IsLittleEndianHost) |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 1064 | // Little-endian host - the destination must be ordered from LSB to MSB. |
| 1065 | // The source is ordered from LSB to MSB: Do a straight copy. |
| 1066 | memcpy(Dst, Src, LoadBytes); |
| 1067 | else { |
| 1068 | // Big-endian - the destination is an array of 64 bit words ordered from |
| 1069 | // LSW to MSW. Each word must be ordered from MSB to LSB. The source is |
| 1070 | // ordered from MSB to LSB: Reverse the word order, but not the bytes in |
| 1071 | // a word. |
| 1072 | while (LoadBytes > sizeof(uint64_t)) { |
| 1073 | LoadBytes -= sizeof(uint64_t); |
| 1074 | // May not be aligned so use memcpy. |
| 1075 | memcpy(Dst, Src + LoadBytes, sizeof(uint64_t)); |
| 1076 | Dst += sizeof(uint64_t); |
| 1077 | } |
| 1078 | |
| 1079 | memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes); |
| 1080 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 1083 | /// FIXME: document |
| 1084 | /// |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 1085 | void ExecutionEngine::LoadValueFromMemory(GenericValue &Result, |
Duncan Sands | 08bfe26 | 2008-03-10 16:38:37 +0000 | [diff] [blame] | 1086 | GenericValue *Ptr, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1087 | Type *Ty) { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1088 | const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty); |
Duncan Sands | 1eff704 | 2007-12-10 17:43:13 +0000 | [diff] [blame] | 1089 | |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 1090 | switch (Ty->getTypeID()) { |
| 1091 | case Type::IntegerTyID: |
| 1092 | // An APInt with all words initially zero. |
| 1093 | Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0); |
| 1094 | LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes); |
| 1095 | break; |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 1096 | case Type::FloatTyID: |
| 1097 | Result.FloatVal = *((float*)Ptr); |
| 1098 | break; |
| 1099 | case Type::DoubleTyID: |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 1100 | Result.DoubleVal = *((double*)Ptr); |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 1101 | break; |
Duncan Sands | 8a43e9e | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 1102 | case Type::PointerTyID: |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 1103 | Result.PointerVal = *((PointerTy*)Ptr); |
| 1104 | break; |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 1105 | case Type::X86_FP80TyID: { |
| 1106 | // This is endian dependent, but it will only work on x86 anyway. |
Duncan Sands | 9e4635a | 2007-12-15 17:37:40 +0000 | [diff] [blame] | 1107 | // FIXME: Will not trap if loading a signaling NaN. |
Dale Johannesen | 1f8c564 | 2009-03-24 18:16:17 +0000 | [diff] [blame] | 1108 | uint64_t y[2]; |
| 1109 | memcpy(y, Ptr, 10); |
Jeffrey Yasskin | 3ba292d | 2011-07-18 21:45:40 +0000 | [diff] [blame] | 1110 | Result.IntVal = APInt(80, y); |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 1111 | break; |
| 1112 | } |
Nadav Rotem | 953783e | 2013-04-01 15:53:30 +0000 | [diff] [blame] | 1113 | case Type::VectorTyID: { |
| 1114 | const VectorType *VT = cast<VectorType>(Ty); |
| 1115 | const Type *ElemT = VT->getElementType(); |
| 1116 | const unsigned numElems = VT->getNumElements(); |
| 1117 | if (ElemT->isFloatTy()) { |
| 1118 | Result.AggregateVal.resize(numElems); |
| 1119 | for (unsigned i = 0; i < numElems; ++i) |
| 1120 | Result.AggregateVal[i].FloatVal = *((float*)Ptr+i); |
| 1121 | } |
| 1122 | if (ElemT->isDoubleTy()) { |
| 1123 | Result.AggregateVal.resize(numElems); |
| 1124 | for (unsigned i = 0; i < numElems; ++i) |
| 1125 | Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i); |
| 1126 | } |
| 1127 | if (ElemT->isIntegerTy()) { |
| 1128 | GenericValue intZero; |
| 1129 | const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth(); |
| 1130 | intZero.IntVal = APInt(elemBitWidth, 0); |
| 1131 | Result.AggregateVal.resize(numElems, intZero); |
| 1132 | for (unsigned i = 0; i < numElems; ++i) |
| 1133 | LoadIntFromMemory(Result.AggregateVal[i].IntVal, |
| 1134 | (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8); |
| 1135 | } |
| 1136 | break; |
| 1137 | } |
Reid Spencer | 8fb0f19 | 2007-03-06 03:04:04 +0000 | [diff] [blame] | 1138 | default: |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1139 | SmallString<256> Msg; |
| 1140 | raw_svector_ostream OS(Msg); |
| 1141 | OS << "Cannot load value of type " << *Ty << "!"; |
| 1142 | report_fatal_error(OS.str()); |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 1143 | } |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1146 | void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { |
David Greene | ae7c59a | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 1147 | DEBUG(dbgs() << "JIT: Initializing " << Addr << " "); |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 1148 | DEBUG(Init->dump()); |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1149 | if (isa<UndefValue>(Init)) |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 1150 | return; |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1151 | |
| 1152 | if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) { |
Robert Bocchino | 7c2b7c7 | 2006-01-20 18:18:40 +0000 | [diff] [blame] | 1153 | unsigned ElementSize = |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1154 | getDataLayout()->getTypeAllocSize(CP->getType()->getElementType()); |
Robert Bocchino | 7c2b7c7 | 2006-01-20 18:18:40 +0000 | [diff] [blame] | 1155 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 1156 | InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize); |
| 1157 | return; |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | if (isa<ConstantAggregateZero>(Init)) { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1161 | memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType())); |
Chris Lattner | b6e1dd7 | 2008-02-15 00:57:28 +0000 | [diff] [blame] | 1162 | return; |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) { |
Dan Gohman | 638e378 | 2008-05-20 03:20:09 +0000 | [diff] [blame] | 1166 | unsigned ElementSize = |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1167 | getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType()); |
Dan Gohman | 638e378 | 2008-05-20 03:20:09 +0000 | [diff] [blame] | 1168 | for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) |
| 1169 | InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); |
| 1170 | return; |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) { |
Dan Gohman | 638e378 | 2008-05-20 03:20:09 +0000 | [diff] [blame] | 1174 | const StructLayout *SL = |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1175 | getDataLayout()->getStructLayout(cast<StructType>(CPS->getType())); |
Dan Gohman | 638e378 | 2008-05-20 03:20:09 +0000 | [diff] [blame] | 1176 | for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) |
| 1177 | InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i)); |
| 1178 | return; |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | if (const ConstantDataSequential *CDS = |
| 1182 | dyn_cast<ConstantDataSequential>(Init)) { |
| 1183 | // CDS is already laid out in host memory order. |
| 1184 | StringRef Data = CDS->getRawDataValues(); |
| 1185 | memcpy(Addr, Data.data(), Data.size()); |
| 1186 | return; |
| 1187 | } |
| 1188 | |
| 1189 | if (Init->getType()->isFirstClassType()) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1190 | GenericValue Val = getConstantValue(Init); |
| 1191 | StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); |
| 1192 | return; |
| 1193 | } |
| 1194 | |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1195 | DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n"); |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1196 | llvm_unreachable("Unknown constant type to initialize memory with!"); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1199 | /// EmitGlobals - Emit all of the global variables to memory, storing their |
| 1200 | /// addresses into GlobalAddress. This must make sure to copy the contents of |
| 1201 | /// their initializers into the memory. |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1202 | void ExecutionEngine::emitGlobals() { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1203 | // 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] | 1204 | // to hold them. If there is more than one module, do a prepass over globals |
| 1205 | // to figure out how the different modules should link together. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1206 | std::map<std::pair<std::string, Type*>, |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1207 | const GlobalValue*> LinkedGlobalsMap; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1208 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1209 | if (Modules.size() != 1) { |
| 1210 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) { |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1211 | Module &M = *Modules[m]; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1212 | for (const auto &GV : M.globals()) { |
| 1213 | if (GV.hasLocalLinkage() || GV.isDeclaration() || |
| 1214 | GV.hasAppendingLinkage() || !GV.hasName()) |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1215 | continue;// Ignore external globals and globals with internal linkage. |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1216 | |
| 1217 | const GlobalValue *&GVEntry = |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1218 | LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())]; |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1219 | |
| 1220 | // If this is the first time we've seen this global, it is the canonical |
| 1221 | // version. |
| 1222 | if (!GVEntry) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1223 | GVEntry = &GV; |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1224 | continue; |
| 1225 | } |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1226 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1227 | // If the existing global is strong, never replace it. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1228 | if (GVEntry->hasExternalLinkage()) |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1229 | continue; |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1230 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1231 | // 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] | 1232 | // symbol. FIXME is this right for common? |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1233 | if (GV.hasExternalLinkage() || GVEntry->hasExternalWeakLinkage()) |
| 1234 | GVEntry = &GV; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 1235 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1236 | } |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1237 | } |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1238 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1239 | std::vector<const GlobalValue*> NonCanonicalGlobals; |
| 1240 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) { |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1241 | Module &M = *Modules[m]; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1242 | for (const auto &GV : M.globals()) { |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1243 | // In the multi-module case, see what this global maps to. |
| 1244 | if (!LinkedGlobalsMap.empty()) { |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1245 | if (const GlobalValue *GVEntry = |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1246 | LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())]) { |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1247 | // If something else is the canonical global, ignore this one. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1248 | if (GVEntry != &GV) { |
| 1249 | NonCanonicalGlobals.push_back(&GV); |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1250 | continue; |
| 1251 | } |
| 1252 | } |
| 1253 | } |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1254 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1255 | if (!GV.isDeclaration()) { |
| 1256 | addGlobalMapping(&GV, getMemoryForGV(&GV)); |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1257 | } else { |
| 1258 | // External variable reference. Try to use the dynamic loader to |
| 1259 | // get a pointer to it. |
| 1260 | if (void *SymAddr = |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1261 | sys::DynamicLibrary::SearchForAddressOfSymbol(GV.getName())) |
| 1262 | addGlobalMapping(&GV, SymAddr); |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1263 | else { |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 1264 | report_fatal_error("Could not resolve external global address: " |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1265 | +GV.getName()); |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1266 | } |
| 1267 | } |
| 1268 | } |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1269 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1270 | // If there are multiple modules, map the non-canonical globals to their |
| 1271 | // canonical location. |
| 1272 | if (!NonCanonicalGlobals.empty()) { |
| 1273 | for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) { |
| 1274 | const GlobalValue *GV = NonCanonicalGlobals[i]; |
| 1275 | const GlobalValue *CGV = |
| 1276 | LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; |
| 1277 | void *Ptr = getPointerToGlobalIfAvailable(CGV); |
| 1278 | assert(Ptr && "Canonical global wasn't codegen'd!"); |
Nuno Lopes | c8ed902 | 2008-10-14 10:04:52 +0000 | [diff] [blame] | 1279 | addGlobalMapping(GV, Ptr); |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1280 | } |
| 1281 | } |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1282 | |
| 1283 | // Now that all of the globals are set up in memory, loop through them all |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1284 | // and initialize their contents. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1285 | for (const auto &GV : M.globals()) { |
| 1286 | if (!GV.isDeclaration()) { |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1287 | if (!LinkedGlobalsMap.empty()) { |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1288 | if (const GlobalValue *GVEntry = |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1289 | LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())]) |
| 1290 | if (GVEntry != &GV) // Not the canonical variable. |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1291 | continue; |
| 1292 | } |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1293 | EmitGlobalVariable(&GV); |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | } |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | // EmitGlobalVariable - This method emits the specified global variable to the |
| 1300 | // address specified in GlobalAddresses, or allocates new memory if it's not |
| 1301 | // already in the map. |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 1302 | void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 1303 | void *GA = getPointerToGlobalIfAvailable(GV); |
Chris Lattner | 23c4724 | 2004-02-08 19:33:23 +0000 | [diff] [blame] | 1304 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1305 | if (!GA) { |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 1306 | // If it's not already specified, allocate memory for the global. |
Nicolas Geoffray | 46fa139 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 1307 | GA = getMemoryForGV(GV); |
Andrew Kaylor | 48079e0 | 2013-11-15 17:52:54 +0000 | [diff] [blame] | 1308 | |
| 1309 | // If we failed to allocate memory for this global, return. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1310 | if (!GA) return; |
Andrew Kaylor | 48079e0 | 2013-11-15 17:52:54 +0000 | [diff] [blame] | 1311 | |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 1312 | addGlobalMapping(GV, GA); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 1313 | } |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1314 | |
Nicolas Geoffray | 46fa139 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 1315 | // Don't initialize if it's thread local, let the client do it. |
| 1316 | if (!GV->isThreadLocal()) |
| 1317 | InitializeMemory(GV->getInitializer(), GA); |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1318 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1319 | Type *ElTy = GV->getType()->getElementType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1320 | size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy); |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 1321 | NumInitBytes += (unsigned)GVSize; |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 1322 | ++NumGlobals; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1323 | } |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 1324 | |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 1325 | ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE) |
| 1326 | : EE(EE), GlobalAddressMap(this) { |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1329 | sys::Mutex * |
| 1330 | ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) { |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 1331 | return &EES->EE.lock; |
| 1332 | } |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1333 | |
| 1334 | void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES, |
| 1335 | const GlobalValue *Old) { |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 1336 | void *OldVal = EES->GlobalAddressMap.lookup(Old); |
| 1337 | EES->GlobalAddressReverseMap.erase(OldVal); |
| 1338 | } |
| 1339 | |
Daniel Dunbar | 48dd875 | 2010-11-13 02:48:57 +0000 | [diff] [blame] | 1340 | void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *, |
| 1341 | const GlobalValue *, |
| 1342 | const GlobalValue *) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1343 | llvm_unreachable("The ExecutionEngine doesn't know how to handle a" |
| 1344 | " RAUW on a value it has a global mapping for."); |
Jeffrey Yasskin | 4c5b23b | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 1345 | } |