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