LogicalDylib.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //===--- LogicalDylib.h - Simulates dylib-style symbol lookup ---*- C++ -*-===//
  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. // Simulates symbol resolution inside a dylib.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H
  14. #define LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H
  15. namespace llvm {
  16. namespace orc {
  17. template <typename BaseLayerT,
  18. typename LogicalModuleResources,
  19. typename LogicalDylibResources>
  20. class LogicalDylib {
  21. public:
  22. typedef typename BaseLayerT::ModuleSetHandleT BaseLayerModuleSetHandleT;
  23. private:
  24. typedef std::vector<BaseLayerModuleSetHandleT> BaseLayerHandleList;
  25. struct LogicalModule {
  26. LogicalModuleResources Resources;
  27. BaseLayerHandleList BaseLayerHandles;
  28. };
  29. typedef std::vector<LogicalModule> LogicalModuleList;
  30. public:
  31. typedef typename BaseLayerHandleList::iterator BaseLayerHandleIterator;
  32. typedef typename LogicalModuleList::iterator LogicalModuleHandle;
  33. LogicalDylib(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
  34. ~LogicalDylib() {
  35. for (auto &LM : LogicalModules)
  36. for (auto BLH : LM.BaseLayerHandles)
  37. BaseLayer.removeModuleSet(BLH);
  38. }
  39. LogicalModuleHandle createLogicalModule() {
  40. LogicalModules.push_back(LogicalModule());
  41. return std::prev(LogicalModules.end());
  42. }
  43. void addToLogicalModule(LogicalModuleHandle LMH,
  44. BaseLayerModuleSetHandleT BaseLayerHandle) {
  45. LMH->BaseLayerHandles.push_back(BaseLayerHandle);
  46. }
  47. LogicalModuleResources& getLogicalModuleResources(LogicalModuleHandle LMH) {
  48. return LMH->Resources;
  49. }
  50. BaseLayerHandleIterator moduleHandlesBegin(LogicalModuleHandle LMH) {
  51. return LMH->BaseLayerHandles.begin();
  52. }
  53. BaseLayerHandleIterator moduleHandlesEnd(LogicalModuleHandle LMH) {
  54. return LMH->BaseLayerHandles.end();
  55. }
  56. JITSymbol findSymbolInLogicalModule(LogicalModuleHandle LMH,
  57. const std::string &Name) {
  58. for (auto BLH : LMH->BaseLayerHandles)
  59. if (auto Symbol = BaseLayer.findSymbolIn(BLH, Name, false))
  60. return Symbol;
  61. return nullptr;
  62. }
  63. JITSymbol findSymbolInternally(LogicalModuleHandle LMH,
  64. const std::string &Name) {
  65. if (auto Symbol = findSymbolInLogicalModule(LMH, Name))
  66. return Symbol;
  67. for (auto LMI = LogicalModules.begin(), LME = LogicalModules.end();
  68. LMI != LME; ++LMI) {
  69. if (LMI != LMH)
  70. if (auto Symbol = findSymbolInLogicalModule(LMI, Name))
  71. return Symbol;
  72. }
  73. return nullptr;
  74. }
  75. JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
  76. for (auto &LM : LogicalModules)
  77. for (auto BLH : LM.BaseLayerHandles)
  78. if (auto Symbol =
  79. BaseLayer.findSymbolIn(BLH, Name, ExportedSymbolsOnly))
  80. return Symbol;
  81. return nullptr;
  82. }
  83. LogicalDylibResources& getDylibResources() { return DylibResources; }
  84. protected:
  85. BaseLayerT BaseLayer;
  86. LogicalModuleList LogicalModules;
  87. LogicalDylibResources DylibResources;
  88. };
  89. } // End namespace orc.
  90. } // End namespace llvm.
  91. #endif // LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H