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