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