1 //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "llvm/ExecutionEngine/GenericValue.h"
12 #include "llvm/ExecutionEngine/JITEventListener.h"
13 #include "llvm/ExecutionEngine/MCJIT.h"
14 #include "llvm/ExecutionEngine/ObjectBuffer.h"
15 #include "llvm/ExecutionEngine/ObjectImage.h"
16 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Mangler.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/Object/Archive.h"
24 #include "llvm/PassManager.h"
25 #include "llvm/Support/DynamicLibrary.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/MutexGuard.h"
29 #include "llvm/Target/TargetLowering.h"
30 #include "llvm/Target/TargetSubtargetInfo.h"
36 static struct RegisterJIT {
37 RegisterJIT() { MCJIT::Register(); }
42 extern "C" void LLVMLinkInMCJIT() {
45 ExecutionEngine *MCJIT::createJIT(std::unique_ptr<Module> M,
46 std::string *ErrorStr,
47 RTDyldMemoryManager *MemMgr,
48 std::unique_ptr<TargetMachine> TM) {
49 // Try to register the program as a source of symbols to resolve against.
51 // FIXME: Don't do this here.
52 sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
54 return new MCJIT(std::move(M), std::move(TM),
55 MemMgr ? MemMgr : new SectionMemoryManager());
58 MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm,
59 RTDyldMemoryManager *MM)
60 : ExecutionEngine(std::move(M)), TM(std::move(tm)), Ctx(nullptr),
61 MemMgr(this, MM), Dyld(&MemMgr), ObjCache(nullptr) {
62 // FIXME: We are managing our modules, so we do not want the base class
63 // ExecutionEngine to manage them as well. To avoid double destruction
64 // of the first (and only) module added in ExecutionEngine constructor
65 // we remove it from EE and will destruct it ourselves.
67 // It may make sense to move our module manager (based on SmallStPtr) back
68 // into EE if the JIT and Interpreter can live with it.
69 // If so, additional functions: addModule, removeModule, FindFunctionNamed,
70 // runStaticConstructorsDestructors could be moved back to EE as well.
72 std::unique_ptr<Module> First = std::move(Modules[0]);
75 OwnedModules.addModule(std::move(First));
76 setDataLayout(TM->getSubtargetImpl()->getDataLayout());
80 MutexGuard locked(lock);
82 Dyld.deregisterEHFrames();
84 for (auto &Obj : LoadedObjects)
86 NotifyFreeingObject(*Obj);
91 void MCJIT::addModule(std::unique_ptr<Module> M) {
92 MutexGuard locked(lock);
93 OwnedModules.addModule(std::move(M));
96 bool MCJIT::removeModule(Module *M) {
97 MutexGuard locked(lock);
98 return OwnedModules.removeModule(M);
101 void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) {
102 std::unique_ptr<ObjectImage> LoadedObject = Dyld.loadObject(std::move(Obj));
103 if (!LoadedObject || Dyld.hasError())
104 report_fatal_error(Dyld.getErrorString());
106 NotifyObjectEmitted(*LoadedObject);
108 LoadedObjects.push_back(std::move(LoadedObject));
111 void MCJIT::addObjectFile(object::OwningBinary<object::ObjectFile> Obj) {
112 std::unique_ptr<object::ObjectFile> ObjFile;
113 std::unique_ptr<MemoryBuffer> MemBuf;
114 std::tie(ObjFile, MemBuf) = Obj.takeBinary();
115 addObjectFile(std::move(ObjFile));
116 Buffers.push_back(std::move(MemBuf));
119 void MCJIT::addArchive(object::OwningBinary<object::Archive> A) {
120 Archives.push_back(std::move(A));
123 void MCJIT::setObjectCache(ObjectCache* NewCache) {
124 MutexGuard locked(lock);
128 std::unique_ptr<ObjectBufferStream> MCJIT::emitObject(Module *M) {
129 MutexGuard locked(lock);
131 // This must be a module which has already been added but not loaded to this
132 // MCJIT instance, since these conditions are tested by our caller,
133 // generateCodeForModule.
137 M->setDataLayout(TM->getSubtargetImpl()->getDataLayout());
138 PM.add(new DataLayoutPass());
140 // The RuntimeDyld will take ownership of this shortly
141 std::unique_ptr<ObjectBufferStream> CompiledObject(new ObjectBufferStream());
143 // Turn the machine code intermediate representation into bytes in memory
144 // that may be executed.
145 if (TM->addPassesToEmitMC(PM, Ctx, CompiledObject->getOStream(),
146 !getVerifyModules())) {
147 report_fatal_error("Target does not support MC emission!");
150 // Initialize passes.
152 // Flush the output buffer to get the generated code into memory
153 CompiledObject->flush();
155 // If we have an object cache, tell it about the new object.
156 // Note that we're using the compiled image, not the loaded image (as below).
158 // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
159 // to create a temporary object here and delete it after the call.
160 MemoryBufferRef MB = CompiledObject->getMemBuffer();
161 ObjCache->notifyObjectCompiled(M, MB);
164 return CompiledObject;
167 void MCJIT::generateCodeForModule(Module *M) {
168 // Get a thread lock to make sure we aren't trying to load multiple times
169 MutexGuard locked(lock);
171 // This must be a module which has already been added to this MCJIT instance.
172 assert(OwnedModules.ownsModule(M) &&
173 "MCJIT::generateCodeForModule: Unknown module.");
175 // Re-compilation is not supported
176 if (OwnedModules.hasModuleBeenLoaded(M))
179 std::unique_ptr<ObjectBuffer> ObjectToLoad;
180 // Try to load the pre-compiled object from cache if possible
182 if (std::unique_ptr<MemoryBuffer> PreCompiledObject =
183 ObjCache->getObject(M))
185 llvm::make_unique<ObjectBuffer>(std::move(PreCompiledObject));
188 // If the cache did not contain a suitable object, compile the object
190 ObjectToLoad = emitObject(M);
191 assert(ObjectToLoad && "Compilation did not produce an object.");
194 // Load the object into the dynamic linker.
195 // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
196 std::unique_ptr<ObjectImage> LoadedObject =
197 Dyld.loadObject(std::move(ObjectToLoad));
199 report_fatal_error(Dyld.getErrorString());
201 // FIXME: Make this optional, maybe even move it to a JIT event listener
202 LoadedObject->registerWithDebugger();
204 NotifyObjectEmitted(*LoadedObject);
206 LoadedObjects.push_back(std::move(LoadedObject));
208 OwnedModules.markModuleAsLoaded(M);
211 void MCJIT::finalizeLoadedModules() {
212 MutexGuard locked(lock);
214 // Resolve any outstanding relocations.
215 Dyld.resolveRelocations();
217 OwnedModules.markAllLoadedModulesAsFinalized();
219 // Register EH frame data for any module we own which has been loaded
220 Dyld.registerEHFrames();
222 // Set page permissions.
223 MemMgr.finalizeMemory();
226 // FIXME: Rename this.
227 void MCJIT::finalizeObject() {
228 MutexGuard locked(lock);
230 // Generate code for module is going to move objects out of the 'added' list,
231 // so we need to copy that out before using it:
232 SmallVector<Module*, 16> ModsToAdd;
233 for (auto M : OwnedModules.added())
234 ModsToAdd.push_back(M);
236 for (auto M : ModsToAdd)
237 generateCodeForModule(M);
239 finalizeLoadedModules();
242 void MCJIT::finalizeModule(Module *M) {
243 MutexGuard locked(lock);
245 // This must be a module which has already been added to this MCJIT instance.
246 assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module.");
248 // If the module hasn't been compiled, just do that.
249 if (!OwnedModules.hasModuleBeenLoaded(M))
250 generateCodeForModule(M);
252 finalizeLoadedModules();
255 uint64_t MCJIT::getExistingSymbolAddress(const std::string &Name) {
256 Mangler Mang(TM->getSubtargetImpl()->getDataLayout());
257 SmallString<128> FullName;
258 Mang.getNameWithPrefix(FullName, Name);
259 return Dyld.getSymbolLoadAddress(FullName);
262 Module *MCJIT::findModuleForSymbol(const std::string &Name,
263 bool CheckFunctionsOnly) {
264 MutexGuard locked(lock);
266 // If it hasn't already been generated, see if it's in one of our modules.
267 for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
268 E = OwnedModules.end_added();
271 Function *F = M->getFunction(Name);
272 if (F && !F->isDeclaration())
274 if (!CheckFunctionsOnly) {
275 GlobalVariable *G = M->getGlobalVariable(Name);
276 if (G && !G->isDeclaration())
278 // FIXME: Do we need to worry about global aliases?
281 // We didn't find the symbol in any of our modules.
285 uint64_t MCJIT::getSymbolAddress(const std::string &Name,
286 bool CheckFunctionsOnly)
288 MutexGuard locked(lock);
290 // First, check to see if we already have this symbol.
291 uint64_t Addr = getExistingSymbolAddress(Name);
295 for (object::OwningBinary<object::Archive> &OB : Archives) {
296 object::Archive *A = OB.getBinary();
297 // Look for our symbols in each Archive
298 object::Archive::child_iterator ChildIt = A->findSym(Name);
299 if (ChildIt != A->child_end()) {
300 // FIXME: Support nested archives?
301 ErrorOr<std::unique_ptr<object::Binary>> ChildBinOrErr =
302 ChildIt->getAsBinary();
303 if (ChildBinOrErr.getError())
305 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
306 if (ChildBin->isObject()) {
307 std::unique_ptr<object::ObjectFile> OF(
308 static_cast<object::ObjectFile *>(ChildBin.release()));
309 // This causes the object file to be loaded.
310 addObjectFile(std::move(OF));
311 // The address should be here now.
312 Addr = getExistingSymbolAddress(Name);
319 // If it hasn't already been generated, see if it's in one of our modules.
320 Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
322 generateCodeForModule(M);
324 // Check the RuntimeDyld table again, it should be there now.
325 return getExistingSymbolAddress(Name);
328 // If a LazyFunctionCreator is installed, use it to get/create the function.
329 // FIXME: Should we instead have a LazySymbolCreator callback?
330 if (LazyFunctionCreator)
331 Addr = (uint64_t)LazyFunctionCreator(Name);
336 uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) {
337 MutexGuard locked(lock);
338 uint64_t Result = getSymbolAddress(Name, false);
340 finalizeLoadedModules();
344 uint64_t MCJIT::getFunctionAddress(const std::string &Name) {
345 MutexGuard locked(lock);
346 uint64_t Result = getSymbolAddress(Name, true);
348 finalizeLoadedModules();
352 // Deprecated. Use getFunctionAddress instead.
353 void *MCJIT::getPointerToFunction(Function *F) {
354 MutexGuard locked(lock);
356 Mangler Mang(TM->getSubtargetImpl()->getDataLayout());
357 SmallString<128> Name;
358 TM->getNameWithPrefix(Name, F, Mang);
360 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
361 bool AbortOnFailure = !F->hasExternalWeakLinkage();
362 void *Addr = getPointerToNamedFunction(Name, AbortOnFailure);
363 updateGlobalMapping(F, Addr);
367 Module *M = F->getParent();
368 bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
370 // Make sure the relevant module has been compiled and loaded.
371 if (HasBeenAddedButNotLoaded)
372 generateCodeForModule(M);
373 else if (!OwnedModules.hasModuleBeenLoaded(M)) {
374 // If this function doesn't belong to one of our modules, we're done.
375 // FIXME: Asking for the pointer to a function that hasn't been registered,
376 // and isn't a declaration (which is handled above) should probably
381 // FIXME: Should the Dyld be retaining module information? Probably not.
383 // This is the accessor for the target address, so make sure to check the
384 // load address of the symbol, not the local address.
385 return (void*)Dyld.getSymbolLoadAddress(Name);
388 void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
389 bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
390 for (; I != E; ++I) {
391 ExecutionEngine::runStaticConstructorsDestructors(**I, isDtors);
395 void MCJIT::runStaticConstructorsDestructors(bool isDtors) {
396 // Execute global ctors/dtors for each module in the program.
397 runStaticConstructorsDestructorsInModulePtrSet(
398 isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
399 runStaticConstructorsDestructorsInModulePtrSet(
400 isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
401 runStaticConstructorsDestructorsInModulePtrSet(
402 isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
405 Function *MCJIT::FindFunctionNamedInModulePtrSet(const char *FnName,
406 ModulePtrSet::iterator I,
407 ModulePtrSet::iterator E) {
408 for (; I != E; ++I) {
409 if (Function *F = (*I)->getFunction(FnName))
415 Function *MCJIT::FindFunctionNamed(const char *FnName) {
416 Function *F = FindFunctionNamedInModulePtrSet(
417 FnName, OwnedModules.begin_added(), OwnedModules.end_added());
419 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
420 OwnedModules.end_loaded());
422 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
423 OwnedModules.end_finalized());
427 GenericValue MCJIT::runFunction(Function *F,
428 const std::vector<GenericValue> &ArgValues) {
429 assert(F && "Function *F was null at entry to run()");
431 void *FPtr = getPointerToFunction(F);
432 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
433 FunctionType *FTy = F->getFunctionType();
434 Type *RetTy = FTy->getReturnType();
436 assert((FTy->getNumParams() == ArgValues.size() ||
437 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
438 "Wrong number of arguments passed into function!");
439 assert(FTy->getNumParams() == ArgValues.size() &&
440 "This doesn't support passing arguments through varargs (yet)!");
442 // Handle some common cases first. These cases correspond to common `main'
444 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
445 switch (ArgValues.size()) {
447 if (FTy->getParamType(0)->isIntegerTy(32) &&
448 FTy->getParamType(1)->isPointerTy() &&
449 FTy->getParamType(2)->isPointerTy()) {
450 int (*PF)(int, char **, const char **) =
451 (int(*)(int, char **, const char **))(intptr_t)FPtr;
453 // Call the function.
455 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
456 (char **)GVTOP(ArgValues[1]),
457 (const char **)GVTOP(ArgValues[2])));
462 if (FTy->getParamType(0)->isIntegerTy(32) &&
463 FTy->getParamType(1)->isPointerTy()) {
464 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
466 // Call the function.
468 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
469 (char **)GVTOP(ArgValues[1])));
474 if (FTy->getNumParams() == 1 &&
475 FTy->getParamType(0)->isIntegerTy(32)) {
477 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
478 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
485 // Handle cases where no arguments are passed first.
486 if (ArgValues.empty()) {
488 switch (RetTy->getTypeID()) {
489 default: llvm_unreachable("Unknown return type for function call!");
490 case Type::IntegerTyID: {
491 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
493 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
494 else if (BitWidth <= 8)
495 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
496 else if (BitWidth <= 16)
497 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
498 else if (BitWidth <= 32)
499 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
500 else if (BitWidth <= 64)
501 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
503 llvm_unreachable("Integer types > 64 bits not supported");
507 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
509 case Type::FloatTyID:
510 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
512 case Type::DoubleTyID:
513 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
515 case Type::X86_FP80TyID:
516 case Type::FP128TyID:
517 case Type::PPC_FP128TyID:
518 llvm_unreachable("long double not supported yet");
519 case Type::PointerTyID:
520 return PTOGV(((void*(*)())(intptr_t)FPtr)());
524 llvm_unreachable("Full-featured argument passing not supported yet!");
527 void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) {
528 if (!isSymbolSearchingDisabled()) {
529 void *ptr = MemMgr.getPointerToNamedFunction(Name, false);
534 /// If a LazyFunctionCreator is installed, use it to get/create the function.
535 if (LazyFunctionCreator)
536 if (void *RP = LazyFunctionCreator(Name))
539 if (AbortOnFailure) {
540 report_fatal_error("Program used external function '"+Name+
541 "' which could not be resolved!");
546 void MCJIT::RegisterJITEventListener(JITEventListener *L) {
549 MutexGuard locked(lock);
550 EventListeners.push_back(L);
552 void MCJIT::UnregisterJITEventListener(JITEventListener *L) {
555 MutexGuard locked(lock);
556 auto I = std::find(EventListeners.rbegin(), EventListeners.rend(), L);
557 if (I != EventListeners.rend()) {
558 std::swap(*I, EventListeners.back());
559 EventListeners.pop_back();
562 void MCJIT::NotifyObjectEmitted(const ObjectImage& Obj) {
563 MutexGuard locked(lock);
564 MemMgr.notifyObjectLoaded(this, &Obj);
565 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
566 EventListeners[I]->NotifyObjectEmitted(Obj);
569 void MCJIT::NotifyFreeingObject(const ObjectImage& Obj) {
570 MutexGuard locked(lock);
571 for (JITEventListener *L : EventListeners)
572 L->NotifyFreeingObject(Obj);
575 uint64_t LinkingMemoryManager::getSymbolAddress(const std::string &Name) {
576 uint64_t Result = ParentEngine->getSymbolAddress(Name, false);
577 // If the symbols wasn't found and it begins with an underscore, try again
578 // without the underscore.
579 if (!Result && Name[0] == '_')
580 Result = ParentEngine->getSymbolAddress(Name.substr(1), false);
583 if (ParentEngine->isSymbolSearchingDisabled())
585 return ClientMM->getSymbolAddress(Name);