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