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