LLVM  3.7.0
MCJIT.cpp
Go to the documentation of this file.
1 //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===//
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/ADT/STLExtras.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #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/Object/ObjectFile.h"
29 
30 using namespace llvm;
31 
32 void ObjectCache::anchor() {}
33 
34 namespace {
35 
36 static struct RegisterJIT {
37  RegisterJIT() { MCJIT::Register(); }
38 } JITRegistrator;
39 
40 }
41 
42 extern "C" void LLVMLinkInMCJIT() {
43 }
44 
46 MCJIT::createJIT(std::unique_ptr<Module> M,
47  std::string *ErrorStr,
48  std::shared_ptr<MCJITMemoryManager> MemMgr,
49  std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
50  std::unique_ptr<TargetMachine> TM) {
51  // Try to register the program as a source of symbols to resolve against.
52  //
53  // FIXME: Don't do this here.
55 
56  if (!MemMgr || !Resolver) {
57  auto RTDyldMM = std::make_shared<SectionMemoryManager>();
58  if (!MemMgr)
59  MemMgr = RTDyldMM;
60  if (!Resolver)
61  Resolver = RTDyldMM;
62  }
63 
64  return new MCJIT(std::move(M), std::move(TM), std::move(MemMgr),
65  std::move(Resolver));
66 }
67 
68 MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm,
69  std::shared_ptr<MCJITMemoryManager> MemMgr,
70  std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver)
71  : ExecutionEngine(std::move(M)), TM(std::move(tm)), Ctx(nullptr),
72  MemMgr(std::move(MemMgr)), Resolver(*this, std::move(Resolver)),
73  Dyld(*this->MemMgr, this->Resolver), ObjCache(nullptr) {
74  // FIXME: We are managing our modules, so we do not want the base class
75  // ExecutionEngine to manage them as well. To avoid double destruction
76  // of the first (and only) module added in ExecutionEngine constructor
77  // we remove it from EE and will destruct it ourselves.
78  //
79  // It may make sense to move our module manager (based on SmallStPtr) back
80  // into EE if the JIT and Interpreter can live with it.
81  // If so, additional functions: addModule, removeModule, FindFunctionNamed,
82  // runStaticConstructorsDestructors could be moved back to EE as well.
83  //
84  std::unique_ptr<Module> First = std::move(Modules[0]);
85  Modules.clear();
86 
87  OwnedModules.addModule(std::move(First));
88  setDataLayout(TM->getDataLayout());
90 }
91 
93  MutexGuard locked(lock);
94 
95  Dyld.deregisterEHFrames();
96 
97  for (auto &Obj : LoadedObjects)
98  if (Obj)
99  NotifyFreeingObject(*Obj);
100 
101  Archives.clear();
102 }
103 
104 void MCJIT::addModule(std::unique_ptr<Module> M) {
105  MutexGuard locked(lock);
106  OwnedModules.addModule(std::move(M));
107 }
108 
110  MutexGuard locked(lock);
111  return OwnedModules.removeModule(M);
112 }
113 
114 void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) {
115  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = Dyld.loadObject(*Obj);
116  if (Dyld.hasError())
118 
119  NotifyObjectEmitted(*Obj, *L);
120 
121  LoadedObjects.push_back(std::move(Obj));
122 }
123 
125  std::unique_ptr<object::ObjectFile> ObjFile;
126  std::unique_ptr<MemoryBuffer> MemBuf;
127  std::tie(ObjFile, MemBuf) = Obj.takeBinary();
128  addObjectFile(std::move(ObjFile));
129  Buffers.push_back(std::move(MemBuf));
130 }
131 
133  Archives.push_back(std::move(A));
134 }
135 
137  MutexGuard locked(lock);
138  ObjCache = NewCache;
139 }
140 
141 std::unique_ptr<MemoryBuffer> MCJIT::emitObject(Module *M) {
142  MutexGuard locked(lock);
143 
144  // This must be a module which has already been added but not loaded to this
145  // MCJIT instance, since these conditions are tested by our caller,
146  // generateCodeForModule.
147 
149 
150  // The RuntimeDyld will take ownership of this shortly
151  SmallVector<char, 4096> ObjBufferSV;
152  raw_svector_ostream ObjStream(ObjBufferSV);
153 
154  // Turn the machine code intermediate representation into bytes in memory
155  // that may be executed.
156  if (TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules()))
157  report_fatal_error("Target does not support MC emission!");
158 
159  // Initialize passes.
160  PM.run(*M);
161  // Flush the output buffer to get the generated code into memory
162  ObjStream.flush();
163 
164  std::unique_ptr<MemoryBuffer> CompiledObjBuffer(
165  new ObjectMemoryBuffer(std::move(ObjBufferSV)));
166 
167  // If we have an object cache, tell it about the new object.
168  // Note that we're using the compiled image, not the loaded image (as below).
169  if (ObjCache) {
170  // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
171  // to create a temporary object here and delete it after the call.
172  MemoryBufferRef MB = CompiledObjBuffer->getMemBufferRef();
173  ObjCache->notifyObjectCompiled(M, MB);
174  }
175 
176  return CompiledObjBuffer;
177 }
178 
180  // Get a thread lock to make sure we aren't trying to load multiple times
181  MutexGuard locked(lock);
182 
183  // This must be a module which has already been added to this MCJIT instance.
184  assert(OwnedModules.ownsModule(M) &&
185  "MCJIT::generateCodeForModule: Unknown module.");
186 
187  // Re-compilation is not supported
188  if (OwnedModules.hasModuleBeenLoaded(M))
189  return;
190 
191  std::unique_ptr<MemoryBuffer> ObjectToLoad;
192  // Try to load the pre-compiled object from cache if possible
193  if (ObjCache)
194  ObjectToLoad = ObjCache->getObject(M);
195 
196  M->setDataLayout(*TM->getDataLayout());
197 
198  // If the cache did not contain a suitable object, compile the object
199  if (!ObjectToLoad) {
200  ObjectToLoad = emitObject(M);
201  assert(ObjectToLoad && "Compilation did not produce an object.");
202  }
203 
204  // Load the object into the dynamic linker.
205  // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
207  object::ObjectFile::createObjectFile(ObjectToLoad->getMemBufferRef());
208  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =
209  Dyld.loadObject(*LoadedObject.get());
210 
211  if (Dyld.hasError())
213 
214  NotifyObjectEmitted(*LoadedObject.get(), *L);
215 
216  Buffers.push_back(std::move(ObjectToLoad));
217  LoadedObjects.push_back(std::move(*LoadedObject));
218 
219  OwnedModules.markModuleAsLoaded(M);
220 }
221 
223  MutexGuard locked(lock);
224 
225  // Resolve any outstanding relocations.
226  Dyld.resolveRelocations();
227 
228  OwnedModules.markAllLoadedModulesAsFinalized();
229 
230  // Register EH frame data for any module we own which has been loaded
231  Dyld.registerEHFrames();
232 
233  // Set page permissions.
234  MemMgr->finalizeMemory();
235 }
236 
237 // FIXME: Rename this.
239  MutexGuard locked(lock);
240 
241  // Generate code for module is going to move objects out of the 'added' list,
242  // so we need to copy that out before using it:
243  SmallVector<Module*, 16> ModsToAdd;
244  for (auto M : OwnedModules.added())
245  ModsToAdd.push_back(M);
246 
247  for (auto M : ModsToAdd)
249 
251 }
252 
254  MutexGuard locked(lock);
255 
256  // This must be a module which has already been added to this MCJIT instance.
257  assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module.");
258 
259  // If the module hasn't been compiled, just do that.
260  if (!OwnedModules.hasModuleBeenLoaded(M))
262 
264 }
265 
267  SmallString<128> FullName;
268  Mangler::getNameWithPrefix(FullName, Name, *TM->getDataLayout());
269 
270  if (void *Addr = getPointerToGlobalIfAvailable(FullName))
271  return RuntimeDyld::SymbolInfo(static_cast<uint64_t>(
272  reinterpret_cast<uintptr_t>(Addr)),
274 
275  return Dyld.getSymbol(FullName);
276 }
277 
279  bool CheckFunctionsOnly) {
280  MutexGuard locked(lock);
281 
282  // If it hasn't already been generated, see if it's in one of our modules.
283  for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
284  E = OwnedModules.end_added();
285  I != E; ++I) {
286  Module *M = *I;
287  Function *F = M->getFunction(Name);
288  if (F && !F->isDeclaration())
289  return M;
290  if (!CheckFunctionsOnly) {
291  GlobalVariable *G = M->getGlobalVariable(Name);
292  if (G && !G->isDeclaration())
293  return M;
294  // FIXME: Do we need to worry about global aliases?
295  }
296  }
297  // We didn't find the symbol in any of our modules.
298  return nullptr;
299 }
300 
301 uint64_t MCJIT::getSymbolAddress(const std::string &Name,
302  bool CheckFunctionsOnly) {
303  return findSymbol(Name, CheckFunctionsOnly).getAddress();
304 }
305 
307  bool CheckFunctionsOnly) {
308  MutexGuard locked(lock);
309 
310  // First, check to see if we already have this symbol.
311  if (auto Sym = findExistingSymbol(Name))
312  return Sym;
313 
314  for (object::OwningBinary<object::Archive> &OB : Archives) {
315  object::Archive *A = OB.getBinary();
316  // Look for our symbols in each Archive
317  object::Archive::child_iterator ChildIt = A->findSym(Name);
318  if (ChildIt != A->child_end()) {
319  // FIXME: Support nested archives?
321  ChildIt->getAsBinary();
322  if (ChildBinOrErr.getError())
323  continue;
324  std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
325  if (ChildBin->isObject()) {
326  std::unique_ptr<object::ObjectFile> OF(
327  static_cast<object::ObjectFile *>(ChildBin.release()));
328  // This causes the object file to be loaded.
329  addObjectFile(std::move(OF));
330  // The address should be here now.
331  if (auto Sym = findExistingSymbol(Name))
332  return Sym;
333  }
334  }
335  }
336 
337  // If it hasn't already been generated, see if it's in one of our modules.
338  Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
339  if (M) {
341 
342  // Check the RuntimeDyld table again, it should be there now.
343  return findExistingSymbol(Name);
344  }
345 
346  // If a LazyFunctionCreator is installed, use it to get/create the function.
347  // FIXME: Should we instead have a LazySymbolCreator callback?
348  if (LazyFunctionCreator) {
349  auto Addr = static_cast<uint64_t>(
350  reinterpret_cast<uintptr_t>(LazyFunctionCreator(Name)));
352  }
353 
354  return nullptr;
355 }
356 
357 uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) {
358  MutexGuard locked(lock);
359  uint64_t Result = getSymbolAddress(Name, false);
360  if (Result != 0)
362  return Result;
363 }
364 
365 uint64_t MCJIT::getFunctionAddress(const std::string &Name) {
366  MutexGuard locked(lock);
367  uint64_t Result = getSymbolAddress(Name, true);
368  if (Result != 0)
370  return Result;
371 }
372 
373 // Deprecated. Use getFunctionAddress instead.
375  MutexGuard locked(lock);
376 
377  Mangler Mang;
379  TM->getNameWithPrefix(Name, F, Mang);
380 
381  if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
382  bool AbortOnFailure = !F->hasExternalWeakLinkage();
383  void *Addr = getPointerToNamedFunction(Name, AbortOnFailure);
384  updateGlobalMapping(F, Addr);
385  return Addr;
386  }
387 
388  Module *M = F->getParent();
389  bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
390 
391  // Make sure the relevant module has been compiled and loaded.
392  if (HasBeenAddedButNotLoaded)
394  else if (!OwnedModules.hasModuleBeenLoaded(M)) {
395  // If this function doesn't belong to one of our modules, we're done.
396  // FIXME: Asking for the pointer to a function that hasn't been registered,
397  // and isn't a declaration (which is handled above) should probably
398  // be an assertion.
399  return nullptr;
400  }
401 
402  // FIXME: Should the Dyld be retaining module information? Probably not.
403  //
404  // This is the accessor for the target address, so make sure to check the
405  // load address of the symbol, not the local address.
406  return (void*)Dyld.getSymbol(Name).getAddress();
407 }
408 
409 void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
410  bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
411  for (; I != E; ++I) {
413  }
414 }
415 
417  // Execute global ctors/dtors for each module in the program.
418  runStaticConstructorsDestructorsInModulePtrSet(
419  isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
420  runStaticConstructorsDestructorsInModulePtrSet(
421  isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
422  runStaticConstructorsDestructorsInModulePtrSet(
423  isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
424 }
425 
426 Function *MCJIT::FindFunctionNamedInModulePtrSet(const char *FnName,
427  ModulePtrSet::iterator I,
428  ModulePtrSet::iterator E) {
429  for (; I != E; ++I) {
430  Function *F = (*I)->getFunction(FnName);
431  if (F && !F->isDeclaration())
432  return F;
433  }
434  return nullptr;
435 }
436 
437 GlobalVariable *MCJIT::FindGlobalVariableNamedInModulePtrSet(const char *Name,
438  bool AllowInternal,
439  ModulePtrSet::iterator I,
440  ModulePtrSet::iterator E) {
441  for (; I != E; ++I) {
442  GlobalVariable *GV = (*I)->getGlobalVariable(Name, AllowInternal);
443  if (GV && !GV->isDeclaration())
444  return GV;
445  }
446  return nullptr;
447 }
448 
449 
450 Function *MCJIT::FindFunctionNamed(const char *FnName) {
451  Function *F = FindFunctionNamedInModulePtrSet(
452  FnName, OwnedModules.begin_added(), OwnedModules.end_added());
453  if (!F)
454  F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
455  OwnedModules.end_loaded());
456  if (!F)
457  F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
458  OwnedModules.end_finalized());
459  return F;
460 }
461 
462 GlobalVariable *MCJIT::FindGlobalVariableNamed(const char *Name, bool AllowInternal) {
463  GlobalVariable *GV = FindGlobalVariableNamedInModulePtrSet(
464  Name, AllowInternal, OwnedModules.begin_added(), OwnedModules.end_added());
465  if (!GV)
466  GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_loaded(),
467  OwnedModules.end_loaded());
468  if (!GV)
469  GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_finalized(),
470  OwnedModules.end_finalized());
471  return GV;
472 }
473 
475  assert(F && "Function *F was null at entry to run()");
476 
477  void *FPtr = getPointerToFunction(F);
478  assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
479  FunctionType *FTy = F->getFunctionType();
480  Type *RetTy = FTy->getReturnType();
481 
482  assert((FTy->getNumParams() == ArgValues.size() ||
483  (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
484  "Wrong number of arguments passed into function!");
485  assert(FTy->getNumParams() == ArgValues.size() &&
486  "This doesn't support passing arguments through varargs (yet)!");
487 
488  // Handle some common cases first. These cases correspond to common `main'
489  // prototypes.
490  if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
491  switch (ArgValues.size()) {
492  case 3:
493  if (FTy->getParamType(0)->isIntegerTy(32) &&
494  FTy->getParamType(1)->isPointerTy() &&
495  FTy->getParamType(2)->isPointerTy()) {
496  int (*PF)(int, char **, const char **) =
497  (int(*)(int, char **, const char **))(intptr_t)FPtr;
498 
499  // Call the function.
500  GenericValue rv;
501  rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
502  (char **)GVTOP(ArgValues[1]),
503  (const char **)GVTOP(ArgValues[2])));
504  return rv;
505  }
506  break;
507  case 2:
508  if (FTy->getParamType(0)->isIntegerTy(32) &&
509  FTy->getParamType(1)->isPointerTy()) {
510  int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
511 
512  // Call the function.
513  GenericValue rv;
514  rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
515  (char **)GVTOP(ArgValues[1])));
516  return rv;
517  }
518  break;
519  case 1:
520  if (FTy->getNumParams() == 1 &&
521  FTy->getParamType(0)->isIntegerTy(32)) {
522  GenericValue rv;
523  int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
524  rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
525  return rv;
526  }
527  break;
528  }
529  }
530 
531  // Handle cases where no arguments are passed first.
532  if (ArgValues.empty()) {
533  GenericValue rv;
534  switch (RetTy->getTypeID()) {
535  default: llvm_unreachable("Unknown return type for function call!");
536  case Type::IntegerTyID: {
537  unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
538  if (BitWidth == 1)
539  rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
540  else if (BitWidth <= 8)
541  rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
542  else if (BitWidth <= 16)
543  rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
544  else if (BitWidth <= 32)
545  rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
546  else if (BitWidth <= 64)
547  rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
548  else
549  llvm_unreachable("Integer types > 64 bits not supported");
550  return rv;
551  }
552  case Type::VoidTyID:
553  rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
554  return rv;
555  case Type::FloatTyID:
556  rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
557  return rv;
558  case Type::DoubleTyID:
559  rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
560  return rv;
561  case Type::X86_FP80TyID:
562  case Type::FP128TyID:
563  case Type::PPC_FP128TyID:
564  llvm_unreachable("long double not supported yet");
565  case Type::PointerTyID:
566  return PTOGV(((void*(*)())(intptr_t)FPtr)());
567  }
568  }
569 
570  llvm_unreachable("Full-featured argument passing not supported yet!");
571 }
572 
573 void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) {
574  if (!isSymbolSearchingDisabled()) {
575  void *ptr =
576  reinterpret_cast<void*>(
577  static_cast<uintptr_t>(Resolver.findSymbol(Name).getAddress()));
578  if (ptr)
579  return ptr;
580  }
581 
582  /// If a LazyFunctionCreator is installed, use it to get/create the function.
584  if (void *RP = LazyFunctionCreator(Name))
585  return RP;
586 
587  if (AbortOnFailure) {
588  report_fatal_error("Program used external function '"+Name+
589  "' which could not be resolved!");
590  }
591  return nullptr;
592 }
593 
595  if (!L)
596  return;
597  MutexGuard locked(lock);
598  EventListeners.push_back(L);
599 }
600 
602  if (!L)
603  return;
604  MutexGuard locked(lock);
605  auto I = std::find(EventListeners.rbegin(), EventListeners.rend(), L);
606  if (I != EventListeners.rend()) {
607  std::swap(*I, EventListeners.back());
608  EventListeners.pop_back();
609  }
610 }
611 
614  MutexGuard locked(lock);
615  MemMgr->notifyObjectLoaded(this, Obj);
616  for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
617  EventListeners[I]->NotifyObjectEmitted(Obj, L);
618  }
619 }
620 
622  MutexGuard locked(lock);
623  for (JITEventListener *L : EventListeners)
624  L->NotifyFreeingObject(Obj);
625 }
626 
628 LinkingSymbolResolver::findSymbol(const std::string &Name) {
629  auto Result = ParentEngine.findSymbol(Name, false);
630  // If the symbols wasn't found and it begins with an underscore, try again
631  // without the underscore.
632  if (!Result && Name[0] == '_')
633  Result = ParentEngine.findSymbol(Name.substr(1), false);
634  if (Result)
635  return Result;
636  if (ParentEngine.isSymbolSearchingDisabled())
637  return nullptr;
638  return ClientResolver->findSymbol(Name);
639 }
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type (if unknown returns 0).
Information about the loaded object.
Definition: RuntimeDyld.h:59
void setObjectCache(ObjectCache *manager) override
Sets the object manager that MCJIT should use to avoid compilation.
Definition: MCJIT.cpp:136
std::error_code getError() const
Definition: ErrorOr.h:178
Represents either an error or a value T.
Definition: ErrorOr.h:82
virtual Function * FindFunctionNamed(const char *FnName) override
FindFunctionNamed - Search all of the active modules to find the function that defines FnName...
Definition: MCJIT.cpp:450
std::unique_ptr< LoadedObjectInfo > loadObject(const object::ObjectFile &O)
Add the referenced object file to the list of objects to be loaded and relocated. ...
void registerEHFrames()
Register any EH frame sections that have been loaded but not previously registered with the memory ma...
ErrorOr< std::unique_ptr< Binary > > getAsBinary(LLVMContext *Context=nullptr) const
Definition: Archive.cpp:214
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
unsigned getNumParams() const
getNumParams - Return the number of fixed parameters this function type requires. ...
Definition: DerivedTypes.h:136
2: 32-bit floating point type
Definition: Type.h:58
virtual std::unique_ptr< MemoryBuffer > getObject(const Module *M)=0
Returns a pointer to a newly allocated MemoryBuffer that contains the object which corresponds with M...
void * getPointerToGlobalIfAvailable(StringRef S)
getPointerToGlobalIfAvailable - This returns the address of the specified global value if it is has a...
sys::Mutex lock
lock - This lock protects the ExecutionEngine and MCJIT classes.
uint64_t updateGlobalMapping(const GlobalValue *GV, void *Addr)
updateGlobalMapping - Replace an existing mapping for GV with a new address.
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
StringRef getErrorString()
uint64_t getAddress() const
Definition: RuntimeDyld.h:53
static JITEventListener * createGDBRegistrationListener()
This class is the base class for all object file types.
Definition: ObjectFile.h:176
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:261
void setDataLayout(StringRef Desc)
Set the data layout.
Definition: Module.cpp:366
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:488
void generateCodeForModule(Module *M) override
generateCodeForModule - Run code generation for the specified module and load it into memory...
Definition: MCJIT.cpp:179
F(f)
4: 80-bit floating point type (X87)
Definition: Type.h:60
14: Pointers
Definition: Type.h:73
void UnregisterJITEventListener(JITEventListener *L) override
Definition: MCJIT.cpp:601
SymbolInfo getSymbol(StringRef Name) const
Get the target address and flags for the named symbol.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
virtual void runStaticConstructorsDestructors(bool isDtors)
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=nullptr)
This function permanently loads the dynamic library at the given path.
void runStaticConstructorsDestructors(bool isDtors) override
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
Definition: MCJIT.cpp:416
#define G(x, y, z)
Definition: MD5.cpp:52
uint64_t getGlobalValueAddress(const std::string &Name) override
getGlobalValueAddress - Return the address of the specified global value.
Definition: MCJIT.cpp:357
FunctionType - Class to represent function types.
Definition: DerivedTypes.h:96
virtual void finalizeModule(Module *)
Definition: MCJIT.cpp:253
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name, bool CheckFunctionsOnly)
Definition: MCJIT.cpp:306
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
TypeID getTypeID() const
getTypeID - Return the type id for the type.
Definition: Type.h:134
void * getPointerToFunction(Function *F) override
getPointerToFunction - The different EE's represent function bodies in different ways.
Definition: MCJIT.cpp:374
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:134
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:188
void finalizeObject() override
finalizeObject - ensure the module is fully processed and is usable.
Definition: MCJIT.cpp:238
10: Arbitrary bit width integers
Definition: Type.h:69
PassManager manages ModulePassManagers.
static ExecutionEngine * createJIT(std::unique_ptr< Module > M, std::string *ErrorStr, std::shared_ptr< MCJITMemoryManager > MemMgr, std::shared_ptr< RuntimeDyld::SymbolResolver > Resolver, std::unique_ptr< TargetMachine > TM)
Definition: MCJIT.cpp:46
Instances of this class acquire a given Mutex Lock when constructed and hold that lock until destruct...
Definition: MutexGuard.h:27
0: type with no size
Definition: Type.h:56
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:131
void addModule(std::unique_ptr< Module > M) override
Add a Module to the list of modules that we can JIT from.
Definition: MCJIT.cpp:104
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
child_iterator child_end() const
Definition: Archive.cpp:372
bool removeModule(Module *M) override
removeModule - Remove a Module from the list of modules.
Definition: MCJIT.cpp:109
void * getPointerToNamedFunction(StringRef Name, bool AbortOnFailure=true) override
getPointerToNamedFunction - This method returns the address of the specified function by using the dl...
Definition: MCJIT.cpp:573
void LLVMLinkInMCJIT()
Definition: MCJIT.cpp:42
uint64_t getFunctionAddress(const std::string &Name) override
getFunctionAddress - Return the address of the specified function.
Definition: MCJIT.cpp:365
6: 128-bit floating point type (two 64-bits, PowerPC)
Definition: Type.h:62
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:129
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
Definition: Type.h:217
void setDataLayout(const DataLayout *Val)
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
virtual GlobalVariable * FindGlobalVariableNamed(const char *Name, bool AllowInternal=false) override
FindGlobalVariableNamed - Search all of the active modules to find the global variable that defines N...
Definition: MCJIT.cpp:462
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
virtual void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj)=0
notifyObjectCompiled - Provides a pointer to compiled code for Module M.
child_iterator findSym(StringRef name) const
Definition: Archive.cpp:528
void * GVTOP(const GenericValue &GV)
Definition: GenericValue.h:50
std::pair< std::unique_ptr< T >, std::unique_ptr< MemoryBuffer > > takeBinary()
Definition: Binary.h:168
void addArchive(object::OwningBinary< object::Archive > O) override
addArchive - Add an Archive to the execution engine.
Definition: MCJIT.cpp:132
SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
Definition: SmallPtrSet.h:179
bool hasExternalWeakLinkage() const
Definition: GlobalValue.h:281
RuntimeDyld::SymbolInfo findExistingSymbol(const std::string &Name)
Definition: MCJIT.cpp:266
Module.h This file contains the declarations for the Module class.
uint64_t getSymbolAddress(const std::string &Name, bool CheckFunctionsOnly)
Definition: MCJIT.cpp:301
void RegisterJITEventListener(JITEventListener *L) override
Registers a listener to be called back on various events within the JIT.
Definition: MCJIT.cpp:594
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:576
GenericValue PTOGV(void *P)
Definition: GenericValue.h:49
Class for arbitrary precision integers.
Definition: APInt.h:73
bool isIntegerTy() const
isIntegerTy - True if this is an instance of IntegerType.
Definition: Type.h:193
Module * findModuleForSymbol(const std::string &Name, bool CheckFunctionsOnly)
Definition: MCJIT.cpp:278
GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues) override
runFunction - Execute the specified function with the specified arguments, and return the result...
Definition: MCJIT.cpp:474
std::unique_ptr< MemoryBuffer > emitObject(Module *M)
emitObject – Generate a JITed object in memory from the specified module Currently, MCJIT only supports a single module and the module passed to this function call is expected to be the contained module.
Definition: MCJIT.cpp:141
SmallVector< std::unique_ptr< Module >, 1 > Modules
The list of Modules that we are JIT'ing from.
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override
This method returns the address of the specified function or variable.
Definition: MCJIT.cpp:628
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:128
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:108
#define I(x, y, z)
Definition: MD5.cpp:54
FunctionType * getFunctionType() const
Definition: Function.cpp:227
SmallVector-backed MemoryBuffer instance.
Information about a named symbol.
Definition: RuntimeDyld.h:47
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
Definition: ObjectCache.h:22
void resolveRelocations()
Resolve the relocations for all symbols we currently know about.
bool isVarArg() const
Definition: DerivedTypes.h:120
3: 64-bit floating point type
Definition: Type.h:59
~MCJIT() override
Definition: MCJIT.cpp:92
Type * getReturnType() const
Definition: DerivedTypes.h:121
FunctionCreator LazyFunctionCreator
LazyFunctionCreator - If an unknown function is needed, this function pointer is invoked to create it...
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
void NotifyFreeingObject(const object::ObjectFile &Obj)
Definition: MCJIT.cpp:621
static ErrorOr< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Create ObjectFile from path.
Definition: ObjectFile.cpp:102
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
void NotifyObjectEmitted(const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &L)
Definition: MCJIT.cpp:612
void finalizeLoadedModules()
Definition: MCJIT.cpp:222
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
Definition: Module.h:381
reference get()
Definition: ErrorOr.h:175
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.
Definition: Type.h:137
5: 128-bit floating point type (112-bit mantissa)
Definition: Type.h:61
void addObjectFile(std::unique_ptr< object::ObjectFile > O) override
addObjectFile - Add an ObjectFile to the execution engine.
Definition: MCJIT.cpp:114