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