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