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