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