MCJIT.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "MCJIT.h"
  9. #include "llvm/ADT/STLExtras.h"
  10. #include "llvm/ExecutionEngine/GenericValue.h"
  11. #include "llvm/ExecutionEngine/JITEventListener.h"
  12. #include "llvm/ExecutionEngine/MCJIT.h"
  13. #include "llvm/ExecutionEngine/ObjectCache.h"
  14. #include "llvm/ExecutionEngine/SectionMemoryManager.h"
  15. #include "llvm/IR/DataLayout.h"
  16. #include "llvm/IR/DerivedTypes.h"
  17. #include "llvm/IR/Function.h"
  18. #include "llvm/IR/LegacyPassManager.h"
  19. #include "llvm/IR/Mangler.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/MC/MCContext.h"
  22. #include "llvm/Object/Archive.h"
  23. #include "llvm/Object/ObjectFile.h"
  24. #include "llvm/Support/DynamicLibrary.h"
  25. #include "llvm/Support/ErrorHandling.h"
  26. #include "llvm/Support/MemoryBuffer.h"
  27. #include "llvm/Support/SmallVectorMemoryBuffer.h"
  28. #include <mutex>
  29. using namespace llvm;
  30. namespace {
  31. static struct RegisterJIT {
  32. RegisterJIT() { MCJIT::Register(); }
  33. } JITRegistrator;
  34. }
  35. extern "C" void LLVMLinkInMCJIT() {
  36. }
  37. ExecutionEngine *
  38. MCJIT::createJIT(std::unique_ptr<Module> M, std::string *ErrorStr,
  39. std::shared_ptr<MCJITMemoryManager> MemMgr,
  40. std::shared_ptr<LegacyJITSymbolResolver> Resolver,
  41. std::unique_ptr<TargetMachine> TM) {
  42. // Try to register the program as a source of symbols to resolve against.
  43. //
  44. // FIXME: Don't do this here.
  45. sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
  46. if (!MemMgr || !Resolver) {
  47. auto RTDyldMM = std::make_shared<SectionMemoryManager>();
  48. if (!MemMgr)
  49. MemMgr = RTDyldMM;
  50. if (!Resolver)
  51. Resolver = RTDyldMM;
  52. }
  53. return new MCJIT(std::move(M), std::move(TM), std::move(MemMgr),
  54. std::move(Resolver));
  55. }
  56. MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> TM,
  57. std::shared_ptr<MCJITMemoryManager> MemMgr,
  58. std::shared_ptr<LegacyJITSymbolResolver> Resolver)
  59. : ExecutionEngine(TM->createDataLayout(), std::move(M)), TM(std::move(TM)),
  60. Ctx(nullptr), MemMgr(std::move(MemMgr)),
  61. Resolver(*this, std::move(Resolver)), Dyld(*this->MemMgr, this->Resolver),
  62. ObjCache(nullptr) {
  63. // FIXME: We are managing our modules, so we do not want the base class
  64. // ExecutionEngine to manage them as well. To avoid double destruction
  65. // of the first (and only) module added in ExecutionEngine constructor
  66. // we remove it from EE and will destruct it ourselves.
  67. //
  68. // It may make sense to move our module manager (based on SmallStPtr) back
  69. // into EE if the JIT and Interpreter can live with it.
  70. // If so, additional functions: addModule, removeModule, FindFunctionNamed,
  71. // runStaticConstructorsDestructors could be moved back to EE as well.
  72. //
  73. std::unique_ptr<Module> First = std::move(Modules[0]);
  74. Modules.clear();
  75. if (First->getDataLayout().isDefault())
  76. First->setDataLayout(getDataLayout());
  77. OwnedModules.addModule(std::move(First));
  78. RegisterJITEventListener(JITEventListener::createGDBRegistrationListener());
  79. }
  80. MCJIT::~MCJIT() {
  81. std::lock_guard<sys::Mutex> locked(lock);
  82. Dyld.deregisterEHFrames();
  83. for (auto &Obj : LoadedObjects)
  84. if (Obj)
  85. notifyFreeingObject(*Obj);
  86. Archives.clear();
  87. }
  88. void MCJIT::addModule(std::unique_ptr<Module> M) {
  89. std::lock_guard<sys::Mutex> locked(lock);
  90. if (M->getDataLayout().isDefault())
  91. M->setDataLayout(getDataLayout());
  92. OwnedModules.addModule(std::move(M));
  93. }
  94. bool MCJIT::removeModule(Module *M) {
  95. std::lock_guard<sys::Mutex> locked(lock);
  96. return OwnedModules.removeModule(M);
  97. }
  98. void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) {
  99. std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = Dyld.loadObject(*Obj);
  100. if (Dyld.hasError())
  101. report_fatal_error(Dyld.getErrorString());
  102. notifyObjectLoaded(*Obj, *L);
  103. LoadedObjects.push_back(std::move(Obj));
  104. }
  105. void MCJIT::addObjectFile(object::OwningBinary<object::ObjectFile> Obj) {
  106. std::unique_ptr<object::ObjectFile> ObjFile;
  107. std::unique_ptr<MemoryBuffer> MemBuf;
  108. std::tie(ObjFile, MemBuf) = Obj.takeBinary();
  109. addObjectFile(std::move(ObjFile));
  110. Buffers.push_back(std::move(MemBuf));
  111. }
  112. void MCJIT::addArchive(object::OwningBinary<object::Archive> A) {
  113. Archives.push_back(std::move(A));
  114. }
  115. void MCJIT::setObjectCache(ObjectCache* NewCache) {
  116. std::lock_guard<sys::Mutex> locked(lock);
  117. ObjCache = NewCache;
  118. }
  119. std::unique_ptr<MemoryBuffer> MCJIT::emitObject(Module *M) {
  120. assert(M && "Can not emit a null module");
  121. std::lock_guard<sys::Mutex> locked(lock);
  122. // Materialize all globals in the module if they have not been
  123. // materialized already.
  124. cantFail(M->materializeAll());
  125. // This must be a module which has already been added but not loaded to this
  126. // MCJIT instance, since these conditions are tested by our caller,
  127. // generateCodeForModule.
  128. legacy::PassManager PM;
  129. // The RuntimeDyld will take ownership of this shortly
  130. SmallVector<char, 4096> ObjBufferSV;
  131. raw_svector_ostream ObjStream(ObjBufferSV);
  132. // Turn the machine code intermediate representation into bytes in memory
  133. // that may be executed.
  134. if (TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules()))
  135. report_fatal_error("Target does not support MC emission!");
  136. // Initialize passes.
  137. PM.run(*M);
  138. // Flush the output buffer to get the generated code into memory
  139. auto CompiledObjBuffer = std::make_unique<SmallVectorMemoryBuffer>(
  140. std::move(ObjBufferSV), /*RequiresNullTerminator=*/false);
  141. // If we have an object cache, tell it about the new object.
  142. // Note that we're using the compiled image, not the loaded image (as below).
  143. if (ObjCache) {
  144. // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
  145. // to create a temporary object here and delete it after the call.
  146. MemoryBufferRef MB = CompiledObjBuffer->getMemBufferRef();
  147. ObjCache->notifyObjectCompiled(M, MB);
  148. }
  149. return CompiledObjBuffer;
  150. }
  151. void MCJIT::generateCodeForModule(Module *M) {
  152. // Get a thread lock to make sure we aren't trying to load multiple times
  153. std::lock_guard<sys::Mutex> locked(lock);
  154. // This must be a module which has already been added to this MCJIT instance.
  155. assert(OwnedModules.ownsModule(M) &&
  156. "MCJIT::generateCodeForModule: Unknown module.");
  157. // Re-compilation is not supported
  158. if (OwnedModules.hasModuleBeenLoaded(M))
  159. return;
  160. std::unique_ptr<MemoryBuffer> ObjectToLoad;
  161. // Try to load the pre-compiled object from cache if possible
  162. if (ObjCache)
  163. ObjectToLoad = ObjCache->getObject(M);
  164. assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch");
  165. // If the cache did not contain a suitable object, compile the object
  166. if (!ObjectToLoad) {
  167. ObjectToLoad = emitObject(M);
  168. assert(ObjectToLoad && "Compilation did not produce an object.");
  169. }
  170. // Load the object into the dynamic linker.
  171. // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
  172. Expected<std::unique_ptr<object::ObjectFile>> LoadedObject =
  173. object::ObjectFile::createObjectFile(ObjectToLoad->getMemBufferRef());
  174. if (!LoadedObject) {
  175. std::string Buf;
  176. raw_string_ostream OS(Buf);
  177. logAllUnhandledErrors(LoadedObject.takeError(), OS);
  178. report_fatal_error(Twine(OS.str()));
  179. }
  180. std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =
  181. Dyld.loadObject(*LoadedObject.get());
  182. if (Dyld.hasError())
  183. report_fatal_error(Dyld.getErrorString());
  184. notifyObjectLoaded(*LoadedObject.get(), *L);
  185. Buffers.push_back(std::move(ObjectToLoad));
  186. LoadedObjects.push_back(std::move(*LoadedObject));
  187. OwnedModules.markModuleAsLoaded(M);
  188. }
  189. void MCJIT::finalizeLoadedModules() {
  190. std::lock_guard<sys::Mutex> locked(lock);
  191. // Resolve any outstanding relocations.
  192. Dyld.resolveRelocations();
  193. // Check for Dyld error.
  194. if (Dyld.hasError())
  195. ErrMsg = Dyld.getErrorString().str();
  196. OwnedModules.markAllLoadedModulesAsFinalized();
  197. // Register EH frame data for any module we own which has been loaded
  198. Dyld.registerEHFrames();
  199. // Set page permissions.
  200. MemMgr->finalizeMemory();
  201. }
  202. // FIXME: Rename this.
  203. void MCJIT::finalizeObject() {
  204. std::lock_guard<sys::Mutex> locked(lock);
  205. // Generate code for module is going to move objects out of the 'added' list,
  206. // so we need to copy that out before using it:
  207. SmallVector<Module*, 16> ModsToAdd;
  208. for (auto *M : OwnedModules.added())
  209. ModsToAdd.push_back(M);
  210. for (auto *M : ModsToAdd)
  211. generateCodeForModule(M);
  212. finalizeLoadedModules();
  213. }
  214. void MCJIT::finalizeModule(Module *M) {
  215. std::lock_guard<sys::Mutex> locked(lock);
  216. // This must be a module which has already been added to this MCJIT instance.
  217. assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module.");
  218. // If the module hasn't been compiled, just do that.
  219. if (!OwnedModules.hasModuleBeenLoaded(M))
  220. generateCodeForModule(M);
  221. finalizeLoadedModules();
  222. }
  223. JITSymbol MCJIT::findExistingSymbol(const std::string &Name) {
  224. if (void *Addr = getPointerToGlobalIfAvailable(Name))
  225. return JITSymbol(static_cast<uint64_t>(
  226. reinterpret_cast<uintptr_t>(Addr)),
  227. JITSymbolFlags::Exported);
  228. return Dyld.getSymbol(Name);
  229. }
  230. Module *MCJIT::findModuleForSymbol(const std::string &Name,
  231. bool CheckFunctionsOnly) {
  232. StringRef DemangledName = Name;
  233. if (DemangledName[0] == getDataLayout().getGlobalPrefix())
  234. DemangledName = DemangledName.substr(1);
  235. std::lock_guard<sys::Mutex> locked(lock);
  236. // If it hasn't already been generated, see if it's in one of our modules.
  237. for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
  238. E = OwnedModules.end_added();
  239. I != E; ++I) {
  240. Module *M = *I;
  241. Function *F = M->getFunction(DemangledName);
  242. if (F && !F->isDeclaration())
  243. return M;
  244. if (!CheckFunctionsOnly) {
  245. GlobalVariable *G = M->getGlobalVariable(DemangledName);
  246. if (G && !G->isDeclaration())
  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 nullptr;
  253. }
  254. uint64_t MCJIT::getSymbolAddress(const std::string &Name,
  255. bool CheckFunctionsOnly) {
  256. std::string MangledName;
  257. {
  258. raw_string_ostream MangledNameStream(MangledName);
  259. Mangler::getNameWithPrefix(MangledNameStream, Name, getDataLayout());
  260. }
  261. if (auto Sym = findSymbol(MangledName, CheckFunctionsOnly)) {
  262. if (auto AddrOrErr = Sym.getAddress())
  263. return *AddrOrErr;
  264. else
  265. report_fatal_error(AddrOrErr.takeError());
  266. } else if (auto Err = Sym.takeError())
  267. report_fatal_error(Sym.takeError());
  268. return 0;
  269. }
  270. JITSymbol MCJIT::findSymbol(const std::string &Name,
  271. bool CheckFunctionsOnly) {
  272. std::lock_guard<sys::Mutex> locked(lock);
  273. // First, check to see if we already have this symbol.
  274. if (auto Sym = findExistingSymbol(Name))
  275. return Sym;
  276. for (object::OwningBinary<object::Archive> &OB : Archives) {
  277. object::Archive *A = OB.getBinary();
  278. // Look for our symbols in each Archive
  279. auto OptionalChildOrErr = A->findSym(Name);
  280. if (!OptionalChildOrErr)
  281. report_fatal_error(OptionalChildOrErr.takeError());
  282. auto &OptionalChild = *OptionalChildOrErr;
  283. if (OptionalChild) {
  284. // FIXME: Support nested archives?
  285. Expected<std::unique_ptr<object::Binary>> ChildBinOrErr =
  286. OptionalChild->getAsBinary();
  287. if (!ChildBinOrErr) {
  288. // TODO: Actually report errors helpfully.
  289. consumeError(ChildBinOrErr.takeError());
  290. continue;
  291. }
  292. std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
  293. if (ChildBin->isObject()) {
  294. std::unique_ptr<object::ObjectFile> OF(
  295. static_cast<object::ObjectFile *>(ChildBin.release()));
  296. // This causes the object file to be loaded.
  297. addObjectFile(std::move(OF));
  298. // The address should be here now.
  299. if (auto Sym = findExistingSymbol(Name))
  300. return Sym;
  301. }
  302. }
  303. }
  304. // If it hasn't already been generated, see if it's in one of our modules.
  305. Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
  306. if (M) {
  307. generateCodeForModule(M);
  308. // Check the RuntimeDyld table again, it should be there now.
  309. return findExistingSymbol(Name);
  310. }
  311. // If a LazyFunctionCreator is installed, use it to get/create the function.
  312. // FIXME: Should we instead have a LazySymbolCreator callback?
  313. if (LazyFunctionCreator) {
  314. auto Addr = static_cast<uint64_t>(
  315. reinterpret_cast<uintptr_t>(LazyFunctionCreator(Name)));
  316. return JITSymbol(Addr, JITSymbolFlags::Exported);
  317. }
  318. return nullptr;
  319. }
  320. uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) {
  321. std::lock_guard<sys::Mutex> locked(lock);
  322. uint64_t Result = getSymbolAddress(Name, false);
  323. if (Result != 0)
  324. finalizeLoadedModules();
  325. return Result;
  326. }
  327. uint64_t MCJIT::getFunctionAddress(const std::string &Name) {
  328. std::lock_guard<sys::Mutex> locked(lock);
  329. uint64_t Result = getSymbolAddress(Name, true);
  330. if (Result != 0)
  331. finalizeLoadedModules();
  332. return Result;
  333. }
  334. // Deprecated. Use getFunctionAddress instead.
  335. void *MCJIT::getPointerToFunction(Function *F) {
  336. std::lock_guard<sys::Mutex> locked(lock);
  337. Mangler Mang;
  338. SmallString<128> Name;
  339. TM->getNameWithPrefix(Name, F, Mang);
  340. if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
  341. bool AbortOnFailure = !F->hasExternalWeakLinkage();
  342. void *Addr = getPointerToNamedFunction(Name, AbortOnFailure);
  343. updateGlobalMapping(F, Addr);
  344. return Addr;
  345. }
  346. Module *M = F->getParent();
  347. bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
  348. // Make sure the relevant module has been compiled and loaded.
  349. if (HasBeenAddedButNotLoaded)
  350. generateCodeForModule(M);
  351. else if (!OwnedModules.hasModuleBeenLoaded(M)) {
  352. // If this function doesn't belong to one of our modules, we're done.
  353. // FIXME: Asking for the pointer to a function that hasn't been registered,
  354. // and isn't a declaration (which is handled above) should probably
  355. // be an assertion.
  356. return nullptr;
  357. }
  358. // FIXME: Should the Dyld be retaining module information? Probably not.
  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.
  362. return (void*)Dyld.getSymbol(Name).getAddress();
  363. }
  364. void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
  365. bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
  366. for (; I != E; ++I) {
  367. ExecutionEngine::runStaticConstructorsDestructors(**I, isDtors);
  368. }
  369. }
  370. void MCJIT::runStaticConstructorsDestructors(bool isDtors) {
  371. // Execute global ctors/dtors for each module in the program.
  372. runStaticConstructorsDestructorsInModulePtrSet(
  373. isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
  374. runStaticConstructorsDestructorsInModulePtrSet(
  375. isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
  376. runStaticConstructorsDestructorsInModulePtrSet(
  377. isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
  378. }
  379. Function *MCJIT::FindFunctionNamedInModulePtrSet(StringRef FnName,
  380. ModulePtrSet::iterator I,
  381. ModulePtrSet::iterator E) {
  382. for (; I != E; ++I) {
  383. Function *F = (*I)->getFunction(FnName);
  384. if (F && !F->isDeclaration())
  385. return F;
  386. }
  387. return nullptr;
  388. }
  389. GlobalVariable *MCJIT::FindGlobalVariableNamedInModulePtrSet(StringRef Name,
  390. bool AllowInternal,
  391. ModulePtrSet::iterator I,
  392. ModulePtrSet::iterator E) {
  393. for (; I != E; ++I) {
  394. GlobalVariable *GV = (*I)->getGlobalVariable(Name, AllowInternal);
  395. if (GV && !GV->isDeclaration())
  396. return GV;
  397. }
  398. return nullptr;
  399. }
  400. Function *MCJIT::FindFunctionNamed(StringRef FnName) {
  401. Function *F = FindFunctionNamedInModulePtrSet(
  402. FnName, OwnedModules.begin_added(), OwnedModules.end_added());
  403. if (!F)
  404. F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
  405. OwnedModules.end_loaded());
  406. if (!F)
  407. F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
  408. OwnedModules.end_finalized());
  409. return F;
  410. }
  411. GlobalVariable *MCJIT::FindGlobalVariableNamed(StringRef Name, bool AllowInternal) {
  412. GlobalVariable *GV = FindGlobalVariableNamedInModulePtrSet(
  413. Name, AllowInternal, OwnedModules.begin_added(), OwnedModules.end_added());
  414. if (!GV)
  415. GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_loaded(),
  416. OwnedModules.end_loaded());
  417. if (!GV)
  418. GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_finalized(),
  419. OwnedModules.end_finalized());
  420. return GV;
  421. }
  422. GenericValue MCJIT::runFunction(Function *F, ArrayRef<GenericValue> ArgValues) {
  423. assert(F && "Function *F was null at entry to run()");
  424. void *FPtr = getPointerToFunction(F);
  425. finalizeModule(F->getParent());
  426. assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
  427. FunctionType *FTy = F->getFunctionType();
  428. Type *RetTy = FTy->getReturnType();
  429. assert((FTy->getNumParams() == ArgValues.size() ||
  430. (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
  431. "Wrong number of arguments passed into function!");
  432. assert(FTy->getNumParams() == ArgValues.size() &&
  433. "This doesn't support passing arguments through varargs (yet)!");
  434. // Handle some common cases first. These cases correspond to common `main'
  435. // prototypes.
  436. if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
  437. switch (ArgValues.size()) {
  438. case 3:
  439. if (FTy->getParamType(0)->isIntegerTy(32) &&
  440. FTy->getParamType(1)->isPointerTy() &&
  441. FTy->getParamType(2)->isPointerTy()) {
  442. int (*PF)(int, char **, const char **) =
  443. (int(*)(int, char **, const char **))(intptr_t)FPtr;
  444. // Call the function.
  445. GenericValue rv;
  446. rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
  447. (char **)GVTOP(ArgValues[1]),
  448. (const char **)GVTOP(ArgValues[2])));
  449. return rv;
  450. }
  451. break;
  452. case 2:
  453. if (FTy->getParamType(0)->isIntegerTy(32) &&
  454. FTy->getParamType(1)->isPointerTy()) {
  455. int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
  456. // Call the function.
  457. GenericValue rv;
  458. rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
  459. (char **)GVTOP(ArgValues[1])));
  460. return rv;
  461. }
  462. break;
  463. case 1:
  464. if (FTy->getNumParams() == 1 &&
  465. FTy->getParamType(0)->isIntegerTy(32)) {
  466. GenericValue rv;
  467. int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
  468. rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
  469. return rv;
  470. }
  471. break;
  472. }
  473. }
  474. // Handle cases where no arguments are passed first.
  475. if (ArgValues.empty()) {
  476. GenericValue rv;
  477. switch (RetTy->getTypeID()) {
  478. default: llvm_unreachable("Unknown return type for function call!");
  479. case Type::IntegerTyID: {
  480. unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
  481. if (BitWidth == 1)
  482. rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
  483. else if (BitWidth <= 8)
  484. rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
  485. else if (BitWidth <= 16)
  486. rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
  487. else if (BitWidth <= 32)
  488. rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
  489. else if (BitWidth <= 64)
  490. rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
  491. else
  492. llvm_unreachable("Integer types > 64 bits not supported");
  493. return rv;
  494. }
  495. case Type::VoidTyID:
  496. rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
  497. return rv;
  498. case Type::FloatTyID:
  499. rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
  500. return rv;
  501. case Type::DoubleTyID:
  502. rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
  503. return rv;
  504. case Type::X86_FP80TyID:
  505. case Type::FP128TyID:
  506. case Type::PPC_FP128TyID:
  507. llvm_unreachable("long double not supported yet");
  508. case Type::PointerTyID:
  509. return PTOGV(((void*(*)())(intptr_t)FPtr)());
  510. }
  511. }
  512. report_fatal_error("MCJIT::runFunction does not support full-featured "
  513. "argument passing. Please use "
  514. "ExecutionEngine::getFunctionAddress and cast the result "
  515. "to the desired function pointer type.");
  516. }
  517. void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) {
  518. if (!isSymbolSearchingDisabled()) {
  519. if (auto Sym = Resolver.findSymbol(std::string(Name))) {
  520. if (auto AddrOrErr = Sym.getAddress())
  521. return reinterpret_cast<void*>(
  522. static_cast<uintptr_t>(*AddrOrErr));
  523. } else if (auto Err = Sym.takeError())
  524. report_fatal_error(std::move(Err));
  525. }
  526. /// If a LazyFunctionCreator is installed, use it to get/create the function.
  527. if (LazyFunctionCreator)
  528. if (void *RP = LazyFunctionCreator(std::string(Name)))
  529. return RP;
  530. if (AbortOnFailure) {
  531. report_fatal_error("Program used external function '"+Name+
  532. "' which could not be resolved!");
  533. }
  534. return nullptr;
  535. }
  536. void MCJIT::RegisterJITEventListener(JITEventListener *L) {
  537. if (!L)
  538. return;
  539. std::lock_guard<sys::Mutex> locked(lock);
  540. EventListeners.push_back(L);
  541. }
  542. void MCJIT::UnregisterJITEventListener(JITEventListener *L) {
  543. if (!L)
  544. return;
  545. std::lock_guard<sys::Mutex> locked(lock);
  546. auto I = find(reverse(EventListeners), L);
  547. if (I != EventListeners.rend()) {
  548. std::swap(*I, EventListeners.back());
  549. EventListeners.pop_back();
  550. }
  551. }
  552. void MCJIT::notifyObjectLoaded(const object::ObjectFile &Obj,
  553. const RuntimeDyld::LoadedObjectInfo &L) {
  554. uint64_t Key =
  555. static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Obj.getData().data()));
  556. std::lock_guard<sys::Mutex> locked(lock);
  557. MemMgr->notifyObjectLoaded(this, Obj);
  558. for (JITEventListener *EL : EventListeners)
  559. EL->notifyObjectLoaded(Key, Obj, L);
  560. }
  561. void MCJIT::notifyFreeingObject(const object::ObjectFile &Obj) {
  562. uint64_t Key =
  563. static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Obj.getData().data()));
  564. std::lock_guard<sys::Mutex> locked(lock);
  565. for (JITEventListener *L : EventListeners)
  566. L->notifyFreeingObject(Key);
  567. }
  568. JITSymbol
  569. LinkingSymbolResolver::findSymbol(const std::string &Name) {
  570. auto Result = ParentEngine.findSymbol(Name, false);
  571. if (Result)
  572. return Result;
  573. if (ParentEngine.isSymbolSearchingDisabled())
  574. return nullptr;
  575. return ClientResolver->findSymbol(Name);
  576. }
  577. void LinkingSymbolResolver::anchor() {}