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