Eric Christopher | 5c896f7 | 2011-04-22 03:07:06 +0000 | [diff] [blame] | 1 | //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===// |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "MCJIT.h" |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 12 | #include "llvm/ExecutionEngine/GenericValue.h" |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 13 | #include "llvm/ExecutionEngine/JITEventListener.h" |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 14 | #include "llvm/ExecutionEngine/MCJIT.h" |
Andrew Kaylor | 31be5ef | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 15 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/DataLayout.h" |
| 17 | #include "llvm/IR/DerivedTypes.h" |
| 18 | #include "llvm/IR/Function.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LegacyPassManager.h" |
Rafael Espindola | 894843c | 2014-01-07 21:19:40 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Mangler.h" |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Module.h" |
Jim Grosbach | 348a548 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCAsmInfo.h" |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 23 | #include "llvm/Object/Archive.h" |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 24 | #include "llvm/Object/ObjectFile.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 25 | #include "llvm/Support/DynamicLibrary.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Jim Grosbach | 348a548 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MemoryBuffer.h" |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MutexGuard.h" |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 32 | void ObjectCache::anchor() {} |
| 33 | |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | |
| 36 | static struct RegisterJIT { |
| 37 | RegisterJIT() { MCJIT::Register(); } |
| 38 | } JITRegistrator; |
| 39 | |
| 40 | } |
| 41 | |
| 42 | extern "C" void LLVMLinkInMCJIT() { |
| 43 | } |
| 44 | |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 45 | ExecutionEngine* |
| 46 | MCJIT::createJIT(std::unique_ptr<Module> M, |
| 47 | std::string *ErrorStr, |
| 48 | std::shared_ptr<MCJITMemoryManager> MemMgr, |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame^] | 49 | std::shared_ptr<JITSymbolResolver> Resolver, |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 50 | std::unique_ptr<TargetMachine> TM) { |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 51 | // Try to register the program as a source of symbols to resolve against. |
| 52 | // |
| 53 | // FIXME: Don't do this here. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 54 | sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 55 | |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 56 | if (!MemMgr || !Resolver) { |
| 57 | auto RTDyldMM = std::make_shared<SectionMemoryManager>(); |
| 58 | if (!MemMgr) |
| 59 | MemMgr = RTDyldMM; |
| 60 | if (!Resolver) |
| 61 | Resolver = RTDyldMM; |
| 62 | } |
Lang Hames | 4a5697e | 2014-12-03 00:51:19 +0000 | [diff] [blame] | 63 | |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 64 | return new MCJIT(std::move(M), std::move(TM), std::move(MemMgr), |
| 65 | std::move(Resolver)); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Mehdi Amini | a3fcefb | 2015-07-16 16:34:23 +0000 | [diff] [blame] | 68 | MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> TM, |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 69 | std::shared_ptr<MCJITMemoryManager> MemMgr, |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame^] | 70 | std::shared_ptr<JITSymbolResolver> Resolver) |
Mehdi Amini | 26d4813 | 2015-07-24 16:04:22 +0000 | [diff] [blame] | 71 | : ExecutionEngine(TM->createDataLayout(), std::move(M)), TM(std::move(TM)), |
Mehdi Amini | a3fcefb | 2015-07-16 16:34:23 +0000 | [diff] [blame] | 72 | Ctx(nullptr), MemMgr(std::move(MemMgr)), |
| 73 | Resolver(*this, std::move(Resolver)), Dyld(*this->MemMgr, this->Resolver), |
| 74 | ObjCache(nullptr) { |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 75 | // FIXME: We are managing our modules, so we do not want the base class |
| 76 | // ExecutionEngine to manage them as well. To avoid double destruction |
| 77 | // of the first (and only) module added in ExecutionEngine constructor |
| 78 | // we remove it from EE and will destruct it ourselves. |
| 79 | // |
| 80 | // It may make sense to move our module manager (based on SmallStPtr) back |
| 81 | // into EE if the JIT and Interpreter can live with it. |
| 82 | // If so, additional functions: addModule, removeModule, FindFunctionNamed, |
| 83 | // runStaticConstructorsDestructors could be moved back to EE as well. |
| 84 | // |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 85 | std::unique_ptr<Module> First = std::move(Modules[0]); |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 86 | Modules.clear(); |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 87 | |
Lang Hames | 717eacf | 2016-06-11 05:47:04 +0000 | [diff] [blame] | 88 | if (First->getDataLayout().isDefault()) |
| 89 | First->setDataLayout(getDataLayout()); |
| 90 | |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 91 | OwnedModules.addModule(std::move(First)); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 92 | RegisterJITEventListener(JITEventListener::createGDBRegistrationListener()); |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | MCJIT::~MCJIT() { |
| 96 | MutexGuard locked(lock); |
| 97 | |
Andrew Kaylor | c442a76 | 2013-10-16 00:14:21 +0000 | [diff] [blame] | 98 | Dyld.deregisterEHFrames(); |
Chandler Carruth | d55d159 | 2013-10-24 09:52:56 +0000 | [diff] [blame] | 99 | |
David Blaikie | ed9709d | 2014-09-03 19:48:09 +0000 | [diff] [blame] | 100 | for (auto &Obj : LoadedObjects) |
| 101 | if (Obj) |
Chandler Carruth | d55d159 | 2013-10-24 09:52:56 +0000 | [diff] [blame] | 102 | NotifyFreeingObject(*Obj); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 103 | |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 104 | Archives.clear(); |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 107 | void MCJIT::addModule(std::unique_ptr<Module> M) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 108 | MutexGuard locked(lock); |
Lang Hames | 717eacf | 2016-06-11 05:47:04 +0000 | [diff] [blame] | 109 | |
| 110 | if (M->getDataLayout().isDefault()) |
| 111 | M->setDataLayout(getDataLayout()); |
| 112 | |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 113 | OwnedModules.addModule(std::move(M)); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 116 | bool MCJIT::removeModule(Module *M) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 117 | MutexGuard locked(lock); |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 118 | return OwnedModules.removeModule(M); |
| 119 | } |
| 120 | |
David Blaikie | 7a1e775 | 2014-04-29 21:52:46 +0000 | [diff] [blame] | 121 | void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) { |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 122 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = Dyld.loadObject(*Obj); |
| 123 | if (Dyld.hasError()) |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 124 | report_fatal_error(Dyld.getErrorString()); |
| 125 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 126 | NotifyObjectEmitted(*Obj, *L); |
David Blaikie | 168861a | 2014-09-04 18:37:31 +0000 | [diff] [blame] | 127 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 128 | LoadedObjects.push_back(std::move(Obj)); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Rafael Espindola | 7271c19 | 2014-08-26 21:04:04 +0000 | [diff] [blame] | 131 | void MCJIT::addObjectFile(object::OwningBinary<object::ObjectFile> Obj) { |
Lang Hames | f04de6e | 2014-10-31 21:37:49 +0000 | [diff] [blame] | 132 | std::unique_ptr<object::ObjectFile> ObjFile; |
| 133 | std::unique_ptr<MemoryBuffer> MemBuf; |
| 134 | std::tie(ObjFile, MemBuf) = Obj.takeBinary(); |
| 135 | addObjectFile(std::move(ObjFile)); |
| 136 | Buffers.push_back(std::move(MemBuf)); |
Rafael Espindola | 7271c19 | 2014-08-26 21:04:04 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 139 | void MCJIT::addArchive(object::OwningBinary<object::Archive> A) { |
Rafael Espindola | ce47a05 | 2014-08-01 18:09:32 +0000 | [diff] [blame] | 140 | Archives.push_back(std::move(A)); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 143 | void MCJIT::setObjectCache(ObjectCache* NewCache) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 144 | MutexGuard locked(lock); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 145 | ObjCache = NewCache; |
| 146 | } |
| 147 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 148 | std::unique_ptr<MemoryBuffer> MCJIT::emitObject(Module *M) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 149 | MutexGuard locked(lock); |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 150 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 151 | // This must be a module which has already been added but not loaded to this |
| 152 | // MCJIT instance, since these conditions are tested by our caller, |
| 153 | // generateCodeForModule. |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 154 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 155 | legacy::PassManager PM; |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 156 | |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 157 | // The RuntimeDyld will take ownership of this shortly |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 158 | SmallVector<char, 4096> ObjBufferSV; |
| 159 | raw_svector_ostream ObjStream(ObjBufferSV); |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 160 | |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 161 | // Turn the machine code intermediate representation into bytes in memory |
| 162 | // that may be executed. |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 163 | if (TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules())) |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 164 | report_fatal_error("Target does not support MC emission!"); |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 165 | |
| 166 | // Initialize passes. |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 167 | PM.run(*M); |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 168 | // Flush the output buffer to get the generated code into memory |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 169 | |
| 170 | std::unique_ptr<MemoryBuffer> CompiledObjBuffer( |
| 171 | new ObjectMemoryBuffer(std::move(ObjBufferSV))); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 172 | |
| 173 | // If we have an object cache, tell it about the new object. |
| 174 | // Note that we're using the compiled image, not the loaded image (as below). |
| 175 | if (ObjCache) { |
| 176 | // MemoryBuffer is a thin wrapper around the actual memory, so it's OK |
| 177 | // to create a temporary object here and delete it after the call. |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 178 | MemoryBufferRef MB = CompiledObjBuffer->getMemBufferRef(); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 179 | ObjCache->notifyObjectCompiled(M, MB); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 182 | return CompiledObjBuffer; |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 185 | void MCJIT::generateCodeForModule(Module *M) { |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 186 | // Get a thread lock to make sure we aren't trying to load multiple times |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 187 | MutexGuard locked(lock); |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 188 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 189 | // This must be a module which has already been added to this MCJIT instance. |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 190 | assert(OwnedModules.ownsModule(M) && |
| 191 | "MCJIT::generateCodeForModule: Unknown module."); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 192 | |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 193 | // Re-compilation is not supported |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 194 | if (OwnedModules.hasModuleBeenLoaded(M)) |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 195 | return; |
| 196 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 197 | std::unique_ptr<MemoryBuffer> ObjectToLoad; |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 198 | // Try to load the pre-compiled object from cache if possible |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 199 | if (ObjCache) |
| 200 | ObjectToLoad = ObjCache->getObject(M); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 201 | |
Lang Hames | 717eacf | 2016-06-11 05:47:04 +0000 | [diff] [blame] | 202 | assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch"); |
Rafael Espindola | 6763f5a | 2015-06-23 14:42:34 +0000 | [diff] [blame] | 203 | |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 204 | // If the cache did not contain a suitable object, compile the object |
| 205 | if (!ObjectToLoad) { |
David Blaikie | d110157 | 2014-09-03 19:57:35 +0000 | [diff] [blame] | 206 | ObjectToLoad = emitObject(M); |
| 207 | assert(ObjectToLoad && "Compilation did not produce an object."); |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 208 | } |
Jim Grosbach | 348a548 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 209 | |
| 210 | // Load the object into the dynamic linker. |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 211 | // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list). |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 212 | Expected<std::unique_ptr<object::ObjectFile>> LoadedObject = |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 213 | object::ObjectFile::createObjectFile(ObjectToLoad->getMemBufferRef()); |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 214 | if (!LoadedObject) { |
| 215 | std::string Buf; |
| 216 | raw_string_ostream OS(Buf); |
| 217 | logAllUnhandledErrors(LoadedObject.takeError(), OS, ""); |
| 218 | OS.flush(); |
| 219 | report_fatal_error(Buf); |
| 220 | } |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 221 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = |
| 222 | Dyld.loadObject(*LoadedObject.get()); |
| 223 | |
| 224 | if (Dyld.hasError()) |
Jim Grosbach | c114d89 | 2011-03-23 19:51:34 +0000 | [diff] [blame] | 225 | report_fatal_error(Dyld.getErrorString()); |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 226 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 227 | NotifyObjectEmitted(*LoadedObject.get(), *L); |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 228 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 229 | Buffers.push_back(std::move(ObjectToLoad)); |
| 230 | LoadedObjects.push_back(std::move(*LoadedObject)); |
David Blaikie | ed9709d | 2014-09-03 19:48:09 +0000 | [diff] [blame] | 231 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 232 | OwnedModules.markModuleAsLoaded(M); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 235 | void MCJIT::finalizeLoadedModules() { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 236 | MutexGuard locked(lock); |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 237 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 238 | // Resolve any outstanding relocations. |
| 239 | Dyld.resolveRelocations(); |
| 240 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 241 | OwnedModules.markAllLoadedModulesAsFinalized(); |
| 242 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 243 | // Register EH frame data for any module we own which has been loaded |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 244 | Dyld.registerEHFrames(); |
| 245 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 246 | // Set page permissions. |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 247 | MemMgr->finalizeMemory(); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // FIXME: Rename this. |
| 251 | void MCJIT::finalizeObject() { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 252 | MutexGuard locked(lock); |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 253 | |
Lang Hames | 018452e | 2014-09-05 23:38:35 +0000 | [diff] [blame] | 254 | // Generate code for module is going to move objects out of the 'added' list, |
| 255 | // so we need to copy that out before using it: |
| 256 | SmallVector<Module*, 16> ModsToAdd; |
| 257 | for (auto M : OwnedModules.added()) |
| 258 | ModsToAdd.push_back(M); |
| 259 | |
| 260 | for (auto M : ModsToAdd) |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 261 | generateCodeForModule(M); |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 262 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 263 | finalizeLoadedModules(); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void MCJIT::finalizeModule(Module *M) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 267 | MutexGuard locked(lock); |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 268 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 269 | // This must be a module which has already been added to this MCJIT instance. |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 270 | assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module."); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 271 | |
| 272 | // If the module hasn't been compiled, just do that. |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 273 | if (!OwnedModules.hasModuleBeenLoaded(M)) |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 274 | generateCodeForModule(M); |
| 275 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 276 | finalizeLoadedModules(); |
Andrew Kaylor | a714efc | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame^] | 279 | JITSymbol MCJIT::findExistingSymbol(const std::string &Name) { |
Rafael Espindola | 3e3a3f1 | 2013-11-28 08:59:52 +0000 | [diff] [blame] | 280 | SmallString<128> FullName; |
Mehdi Amini | a3fcefb | 2015-07-16 16:34:23 +0000 | [diff] [blame] | 281 | Mangler::getNameWithPrefix(FullName, Name, getDataLayout()); |
Lang Hames | 3393cfd | 2015-07-29 23:12:33 +0000 | [diff] [blame] | 282 | |
| 283 | if (void *Addr = getPointerToGlobalIfAvailable(FullName)) |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame^] | 284 | return JITSymbol(static_cast<uint64_t>( |
| 285 | reinterpret_cast<uintptr_t>(Addr)), |
| 286 | JITSymbolFlags::Exported); |
Lang Hames | 3393cfd | 2015-07-29 23:12:33 +0000 | [diff] [blame] | 287 | |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 288 | return Dyld.getSymbol(FullName); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 289 | } |
Jim Grosbach | dc1123f | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 290 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 291 | Module *MCJIT::findModuleForSymbol(const std::string &Name, |
| 292 | bool CheckFunctionsOnly) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 293 | MutexGuard locked(lock); |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 294 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 295 | // If it hasn't already been generated, see if it's in one of our modules. |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 296 | for (ModulePtrSet::iterator I = OwnedModules.begin_added(), |
| 297 | E = OwnedModules.end_added(); |
| 298 | I != E; ++I) { |
| 299 | Module *M = *I; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 300 | Function *F = M->getFunction(Name); |
Andrew Kaylor | 515b1da | 2013-11-15 22:10:21 +0000 | [diff] [blame] | 301 | if (F && !F->isDeclaration()) |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 302 | return M; |
| 303 | if (!CheckFunctionsOnly) { |
| 304 | GlobalVariable *G = M->getGlobalVariable(Name); |
Andrew Kaylor | 515b1da | 2013-11-15 22:10:21 +0000 | [diff] [blame] | 305 | if (G && !G->isDeclaration()) |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 306 | return M; |
| 307 | // FIXME: Do we need to worry about global aliases? |
| 308 | } |
| 309 | } |
| 310 | // We didn't find the symbol in any of our modules. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 311 | return nullptr; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | uint64_t MCJIT::getSymbolAddress(const std::string &Name, |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 315 | bool CheckFunctionsOnly) { |
| 316 | return findSymbol(Name, CheckFunctionsOnly).getAddress(); |
| 317 | } |
| 318 | |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame^] | 319 | JITSymbol MCJIT::findSymbol(const std::string &Name, |
| 320 | bool CheckFunctionsOnly) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 321 | MutexGuard locked(lock); |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 322 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 323 | // First, check to see if we already have this symbol. |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 324 | if (auto Sym = findExistingSymbol(Name)) |
| 325 | return Sym; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 326 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 327 | for (object::OwningBinary<object::Archive> &OB : Archives) { |
Lang Hames | f04de6e | 2014-10-31 21:37:49 +0000 | [diff] [blame] | 328 | object::Archive *A = OB.getBinary(); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 329 | // Look for our symbols in each Archive |
Lang Hames | 69f4902 | 2016-07-14 20:44:27 +0000 | [diff] [blame] | 330 | auto OptionalChildOrErr = A->findSym(Name); |
| 331 | if (!OptionalChildOrErr) |
| 332 | report_fatal_error(OptionalChildOrErr.takeError()); |
| 333 | auto &OptionalChild = *OptionalChildOrErr; |
| 334 | if (OptionalChild) { |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 335 | // FIXME: Support nested archives? |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 336 | Expected<std::unique_ptr<object::Binary>> ChildBinOrErr = |
Lang Hames | 69f4902 | 2016-07-14 20:44:27 +0000 | [diff] [blame] | 337 | OptionalChild->getAsBinary(); |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 338 | if (!ChildBinOrErr) { |
| 339 | // TODO: Actually report errors helpfully. |
| 340 | consumeError(ChildBinOrErr.takeError()); |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 341 | continue; |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 342 | } |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 343 | std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get(); |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 344 | if (ChildBin->isObject()) { |
David Blaikie | 7a1e775 | 2014-04-29 21:52:46 +0000 | [diff] [blame] | 345 | std::unique_ptr<object::ObjectFile> OF( |
| 346 | static_cast<object::ObjectFile *>(ChildBin.release())); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 347 | // This causes the object file to be loaded. |
David Blaikie | 7a1e775 | 2014-04-29 21:52:46 +0000 | [diff] [blame] | 348 | addObjectFile(std::move(OF)); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 349 | // The address should be here now. |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 350 | if (auto Sym = findExistingSymbol(Name)) |
| 351 | return Sym; |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 356 | // If it hasn't already been generated, see if it's in one of our modules. |
| 357 | Module *M = findModuleForSymbol(Name, CheckFunctionsOnly); |
Lang Hames | ea800ca | 2014-08-14 02:38:20 +0000 | [diff] [blame] | 358 | if (M) { |
| 359 | generateCodeForModule(M); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 360 | |
Lang Hames | ea800ca | 2014-08-14 02:38:20 +0000 | [diff] [blame] | 361 | // Check the RuntimeDyld table again, it should be there now. |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 362 | return findExistingSymbol(Name); |
Lang Hames | ea800ca | 2014-08-14 02:38:20 +0000 | [diff] [blame] | 363 | } |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 364 | |
Lang Hames | ea800ca | 2014-08-14 02:38:20 +0000 | [diff] [blame] | 365 | // If a LazyFunctionCreator is installed, use it to get/create the function. |
| 366 | // FIXME: Should we instead have a LazySymbolCreator callback? |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 367 | if (LazyFunctionCreator) { |
| 368 | auto Addr = static_cast<uint64_t>( |
| 369 | reinterpret_cast<uintptr_t>(LazyFunctionCreator(Name))); |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame^] | 370 | return JITSymbol(Addr, JITSymbolFlags::Exported); |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 371 | } |
Lang Hames | ea800ca | 2014-08-14 02:38:20 +0000 | [diff] [blame] | 372 | |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 373 | return nullptr; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 377 | MutexGuard locked(lock); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 378 | uint64_t Result = getSymbolAddress(Name, false); |
| 379 | if (Result != 0) |
| 380 | finalizeLoadedModules(); |
| 381 | return Result; |
| 382 | } |
| 383 | |
| 384 | uint64_t MCJIT::getFunctionAddress(const std::string &Name) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 385 | MutexGuard locked(lock); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 386 | uint64_t Result = getSymbolAddress(Name, true); |
| 387 | if (Result != 0) |
| 388 | finalizeLoadedModules(); |
| 389 | return Result; |
| 390 | } |
| 391 | |
| 392 | // Deprecated. Use getFunctionAddress instead. |
| 393 | void *MCJIT::getPointerToFunction(Function *F) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 394 | MutexGuard locked(lock); |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 395 | |
Rafael Espindola | c233f74 | 2015-06-23 13:59:29 +0000 | [diff] [blame] | 396 | Mangler Mang; |
Lang Hames | b7fbf59 | 2014-09-20 17:44:56 +0000 | [diff] [blame] | 397 | SmallString<128> Name; |
| 398 | TM->getNameWithPrefix(Name, F, Mang); |
| 399 | |
Jim Grosbach | d527440 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 400 | if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) { |
| 401 | bool AbortOnFailure = !F->hasExternalWeakLinkage(); |
Lang Hames | b7fbf59 | 2014-09-20 17:44:56 +0000 | [diff] [blame] | 402 | void *Addr = getPointerToNamedFunction(Name, AbortOnFailure); |
Lang Hames | efe7e22 | 2014-10-22 23:18:42 +0000 | [diff] [blame] | 403 | updateGlobalMapping(F, Addr); |
Jim Grosbach | d527440 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 404 | return Addr; |
| 405 | } |
| 406 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 407 | Module *M = F->getParent(); |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 408 | bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 409 | |
| 410 | // Make sure the relevant module has been compiled and loaded. |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 411 | if (HasBeenAddedButNotLoaded) |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 412 | generateCodeForModule(M); |
Lang Hames | b7fbf59 | 2014-09-20 17:44:56 +0000 | [diff] [blame] | 413 | else if (!OwnedModules.hasModuleBeenLoaded(M)) { |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 414 | // If this function doesn't belong to one of our modules, we're done. |
Lang Hames | b7fbf59 | 2014-09-20 17:44:56 +0000 | [diff] [blame] | 415 | // FIXME: Asking for the pointer to a function that hasn't been registered, |
| 416 | // and isn't a declaration (which is handled above) should probably |
| 417 | // be an assertion. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 418 | return nullptr; |
Lang Hames | b7fbf59 | 2014-09-20 17:44:56 +0000 | [diff] [blame] | 419 | } |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 420 | |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 421 | // FIXME: Should the Dyld be retaining module information? Probably not. |
Jim Grosbach | dc1123f | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 422 | // |
| 423 | // This is the accessor for the target address, so make sure to check the |
| 424 | // load address of the symbol, not the local address. |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 425 | return (void*)Dyld.getSymbol(Name).getAddress(); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 428 | void MCJIT::runStaticConstructorsDestructorsInModulePtrSet( |
| 429 | bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) { |
| 430 | for (; I != E; ++I) { |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 431 | ExecutionEngine::runStaticConstructorsDestructors(**I, isDtors); |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
| 435 | void MCJIT::runStaticConstructorsDestructors(bool isDtors) { |
| 436 | // Execute global ctors/dtors for each module in the program. |
| 437 | runStaticConstructorsDestructorsInModulePtrSet( |
| 438 | isDtors, OwnedModules.begin_added(), OwnedModules.end_added()); |
| 439 | runStaticConstructorsDestructorsInModulePtrSet( |
| 440 | isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded()); |
| 441 | runStaticConstructorsDestructorsInModulePtrSet( |
| 442 | isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized()); |
| 443 | } |
| 444 | |
| 445 | Function *MCJIT::FindFunctionNamedInModulePtrSet(const char *FnName, |
| 446 | ModulePtrSet::iterator I, |
| 447 | ModulePtrSet::iterator E) { |
| 448 | for (; I != E; ++I) { |
Keno Fischer | 5f92a08 | 2015-01-27 19:29:00 +0000 | [diff] [blame] | 449 | Function *F = (*I)->getFunction(FnName); |
| 450 | if (F && !F->isDeclaration()) |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 451 | return F; |
| 452 | } |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 453 | return nullptr; |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Keno Fischer | 73378eb | 2015-06-20 00:55:58 +0000 | [diff] [blame] | 456 | GlobalVariable *MCJIT::FindGlobalVariableNamedInModulePtrSet(const char *Name, |
| 457 | bool AllowInternal, |
| 458 | ModulePtrSet::iterator I, |
| 459 | ModulePtrSet::iterator E) { |
| 460 | for (; I != E; ++I) { |
| 461 | GlobalVariable *GV = (*I)->getGlobalVariable(Name, AllowInternal); |
| 462 | if (GV && !GV->isDeclaration()) |
| 463 | return GV; |
| 464 | } |
| 465 | return nullptr; |
| 466 | } |
| 467 | |
| 468 | |
Andrew Kaylor | c89fc82 | 2013-10-24 00:19:14 +0000 | [diff] [blame] | 469 | Function *MCJIT::FindFunctionNamed(const char *FnName) { |
| 470 | Function *F = FindFunctionNamedInModulePtrSet( |
| 471 | FnName, OwnedModules.begin_added(), OwnedModules.end_added()); |
| 472 | if (!F) |
| 473 | F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(), |
| 474 | OwnedModules.end_loaded()); |
| 475 | if (!F) |
| 476 | F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(), |
| 477 | OwnedModules.end_finalized()); |
| 478 | return F; |
| 479 | } |
| 480 | |
Keno Fischer | 73378eb | 2015-06-20 00:55:58 +0000 | [diff] [blame] | 481 | GlobalVariable *MCJIT::FindGlobalVariableNamed(const char *Name, bool AllowInternal) { |
| 482 | GlobalVariable *GV = FindGlobalVariableNamedInModulePtrSet( |
| 483 | Name, AllowInternal, OwnedModules.begin_added(), OwnedModules.end_added()); |
| 484 | if (!GV) |
| 485 | GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_loaded(), |
| 486 | OwnedModules.end_loaded()); |
| 487 | if (!GV) |
| 488 | GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_finalized(), |
| 489 | OwnedModules.end_finalized()); |
| 490 | return GV; |
| 491 | } |
| 492 | |
Benjamin Kramer | bd7b1c8 | 2015-06-13 19:50:29 +0000 | [diff] [blame] | 493 | GenericValue MCJIT::runFunction(Function *F, ArrayRef<GenericValue> ArgValues) { |
Jim Grosbach | d527440 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 494 | assert(F && "Function *F was null at entry to run()"); |
| 495 | |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 496 | void *FPtr = getPointerToFunction(F); |
Lang Hames | 717eacf | 2016-06-11 05:47:04 +0000 | [diff] [blame] | 497 | finalizeModule(F->getParent()); |
Jim Grosbach | d527440 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 498 | assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 499 | FunctionType *FTy = F->getFunctionType(); |
| 500 | Type *RetTy = FTy->getReturnType(); |
Jim Grosbach | d527440 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 501 | |
| 502 | assert((FTy->getNumParams() == ArgValues.size() || |
| 503 | (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) && |
| 504 | "Wrong number of arguments passed into function!"); |
| 505 | assert(FTy->getNumParams() == ArgValues.size() && |
| 506 | "This doesn't support passing arguments through varargs (yet)!"); |
| 507 | |
| 508 | // Handle some common cases first. These cases correspond to common `main' |
| 509 | // prototypes. |
| 510 | if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) { |
| 511 | switch (ArgValues.size()) { |
| 512 | case 3: |
| 513 | if (FTy->getParamType(0)->isIntegerTy(32) && |
| 514 | FTy->getParamType(1)->isPointerTy() && |
| 515 | FTy->getParamType(2)->isPointerTy()) { |
| 516 | int (*PF)(int, char **, const char **) = |
| 517 | (int(*)(int, char **, const char **))(intptr_t)FPtr; |
| 518 | |
| 519 | // Call the function. |
| 520 | GenericValue rv; |
| 521 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 522 | (char **)GVTOP(ArgValues[1]), |
| 523 | (const char **)GVTOP(ArgValues[2]))); |
| 524 | return rv; |
| 525 | } |
| 526 | break; |
| 527 | case 2: |
| 528 | if (FTy->getParamType(0)->isIntegerTy(32) && |
| 529 | FTy->getParamType(1)->isPointerTy()) { |
| 530 | int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr; |
| 531 | |
| 532 | // Call the function. |
| 533 | GenericValue rv; |
| 534 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 535 | (char **)GVTOP(ArgValues[1]))); |
| 536 | return rv; |
| 537 | } |
| 538 | break; |
| 539 | case 1: |
| 540 | if (FTy->getNumParams() == 1 && |
| 541 | FTy->getParamType(0)->isIntegerTy(32)) { |
| 542 | GenericValue rv; |
| 543 | int (*PF)(int) = (int(*)(int))(intptr_t)FPtr; |
| 544 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue())); |
| 545 | return rv; |
| 546 | } |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | // Handle cases where no arguments are passed first. |
| 552 | if (ArgValues.empty()) { |
| 553 | GenericValue rv; |
| 554 | switch (RetTy->getTypeID()) { |
| 555 | default: llvm_unreachable("Unknown return type for function call!"); |
| 556 | case Type::IntegerTyID: { |
| 557 | unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth(); |
| 558 | if (BitWidth == 1) |
| 559 | rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)()); |
| 560 | else if (BitWidth <= 8) |
| 561 | rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)()); |
| 562 | else if (BitWidth <= 16) |
| 563 | rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)()); |
| 564 | else if (BitWidth <= 32) |
| 565 | rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)()); |
| 566 | else if (BitWidth <= 64) |
| 567 | rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)()); |
| 568 | else |
| 569 | llvm_unreachable("Integer types > 64 bits not supported"); |
| 570 | return rv; |
| 571 | } |
| 572 | case Type::VoidTyID: |
| 573 | rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)()); |
| 574 | return rv; |
| 575 | case Type::FloatTyID: |
| 576 | rv.FloatVal = ((float(*)())(intptr_t)FPtr)(); |
| 577 | return rv; |
| 578 | case Type::DoubleTyID: |
| 579 | rv.DoubleVal = ((double(*)())(intptr_t)FPtr)(); |
| 580 | return rv; |
| 581 | case Type::X86_FP80TyID: |
| 582 | case Type::FP128TyID: |
| 583 | case Type::PPC_FP128TyID: |
| 584 | llvm_unreachable("long double not supported yet"); |
Jim Grosbach | d527440 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 585 | case Type::PointerTyID: |
| 586 | return PTOGV(((void*(*)())(intptr_t)FPtr)()); |
| 587 | } |
| 588 | } |
| 589 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 590 | llvm_unreachable("Full-featured argument passing not supported yet!"); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 591 | } |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 592 | |
Lang Hames | 9a78334 | 2014-09-15 17:50:22 +0000 | [diff] [blame] | 593 | void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 594 | if (!isSymbolSearchingDisabled()) { |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 595 | void *ptr = |
| 596 | reinterpret_cast<void*>( |
| 597 | static_cast<uintptr_t>(Resolver.findSymbol(Name).getAddress())); |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 598 | if (ptr) |
| 599 | return ptr; |
| 600 | } |
| 601 | |
| 602 | /// If a LazyFunctionCreator is installed, use it to get/create the function. |
| 603 | if (LazyFunctionCreator) |
| 604 | if (void *RP = LazyFunctionCreator(Name)) |
| 605 | return RP; |
| 606 | |
| 607 | if (AbortOnFailure) { |
| 608 | report_fatal_error("Program used external function '"+Name+ |
Eli Bendersky | 0e2ac5b | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 609 | "' which could not be resolved!"); |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 610 | } |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 611 | return nullptr; |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 612 | } |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 613 | |
| 614 | void MCJIT::RegisterJITEventListener(JITEventListener *L) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 615 | if (!L) |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 616 | return; |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 617 | MutexGuard locked(lock); |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 618 | EventListeners.push_back(L); |
| 619 | } |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 620 | |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 621 | void MCJIT::UnregisterJITEventListener(JITEventListener *L) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 622 | if (!L) |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 623 | return; |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 624 | MutexGuard locked(lock); |
Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 625 | auto I = std::find(EventListeners.rbegin(), EventListeners.rend(), L); |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 626 | if (I != EventListeners.rend()) { |
| 627 | std::swap(*I, EventListeners.back()); |
| 628 | EventListeners.pop_back(); |
| 629 | } |
| 630 | } |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 631 | |
| 632 | void MCJIT::NotifyObjectEmitted(const object::ObjectFile& Obj, |
| 633 | const RuntimeDyld::LoadedObjectInfo &L) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 634 | MutexGuard locked(lock); |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 635 | MemMgr->notifyObjectLoaded(this, Obj); |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 636 | for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) { |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 637 | EventListeners[I]->NotifyObjectEmitted(Obj, L); |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 640 | |
| 641 | void MCJIT::NotifyFreeingObject(const object::ObjectFile& Obj) { |
Zachary Turner | c04b892 | 2014-06-20 21:07:14 +0000 | [diff] [blame] | 642 | MutexGuard locked(lock); |
David Blaikie | ed9709d | 2014-09-03 19:48:09 +0000 | [diff] [blame] | 643 | for (JITEventListener *L : EventListeners) |
Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 644 | L->NotifyFreeingObject(Obj); |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 645 | } |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 646 | |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame^] | 647 | JITSymbol |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 648 | LinkingSymbolResolver::findSymbol(const std::string &Name) { |
| 649 | auto Result = ParentEngine.findSymbol(Name, false); |
Andrew Kaylor | 89bdd10 | 2013-10-01 16:42:50 +0000 | [diff] [blame] | 650 | // If the symbols wasn't found and it begins with an underscore, try again |
| 651 | // without the underscore. |
| 652 | if (!Result && Name[0] == '_') |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 653 | Result = ParentEngine.findSymbol(Name.substr(1), false); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 654 | if (Result) |
| 655 | return Result; |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 656 | if (ParentEngine.isSymbolSearchingDisabled()) |
| 657 | return nullptr; |
| 658 | return ClientResolver->findSymbol(Name); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 659 | } |