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