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