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