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