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