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