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