1 //===--- LogicalDylib.h - Simulates dylib-style symbol lookup ---*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
10 // Simulates symbol resolution inside a dylib.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H
15 #define LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H
17 #include "llvm/ExecutionEngine/JITSymbol.h"
24 template <typename BaseLayerT,
25 typename LogicalModuleResources,
26 typename LogicalDylibResources>
29 typedef typename BaseLayerT::ModuleSetHandleT BaseLayerModuleSetHandleT;
32 typedef std::vector<BaseLayerModuleSetHandleT> BaseLayerHandleList;
34 struct LogicalModule {
35 // Make this move-only to ensure they don't get duplicated across moves of
36 // LogicalDylib or anything like that.
37 LogicalModule(LogicalModule &&RHS)
38 : Resources(std::move(RHS.Resources)),
39 BaseLayerHandles(std::move(RHS.BaseLayerHandles)) {}
40 LogicalModule() = default;
41 LogicalModuleResources Resources;
42 BaseLayerHandleList BaseLayerHandles;
44 typedef std::vector<LogicalModule> LogicalModuleList;
48 typedef typename BaseLayerHandleList::iterator BaseLayerHandleIterator;
49 typedef typename LogicalModuleList::size_type LogicalModuleHandle;
51 LogicalDylib(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
54 for (auto &LM : LogicalModules)
55 for (auto BLH : LM.BaseLayerHandles)
56 BaseLayer.removeModuleSet(BLH);
59 // If possible, remove this and ~LogicalDylib once the work in the dtor is
60 // moved to members (eg: self-unregistering base layer handles).
61 LogicalDylib(LogicalDylib &&RHS)
62 : BaseLayer(std::move(RHS.BaseLayer)),
63 LogicalModules(std::move(RHS.LogicalModules)),
64 DylibResources(std::move(RHS.DylibResources)) {}
66 LogicalModuleHandle createLogicalModule() {
67 LogicalModules.push_back(LogicalModule());
68 return LogicalModules.size() - 1;
71 void addToLogicalModule(LogicalModuleHandle LMH,
72 BaseLayerModuleSetHandleT BaseLayerHandle) {
73 LogicalModules[LMH].BaseLayerHandles.push_back(BaseLayerHandle);
76 LogicalModuleResources& getLogicalModuleResources(LogicalModuleHandle LMH) {
77 return LogicalModules[LMH].Resources;
80 BaseLayerHandleIterator moduleHandlesBegin(LogicalModuleHandle LMH) {
81 return LogicalModules[LMH].BaseLayerHandles.begin();
84 BaseLayerHandleIterator moduleHandlesEnd(LogicalModuleHandle LMH) {
85 return LogicalModules[LMH].BaseLayerHandles.end();
88 JITSymbol findSymbolInLogicalModule(LogicalModuleHandle LMH,
89 const std::string &Name,
90 bool ExportedSymbolsOnly) {
93 LogicalModules[LMH].Resources.findSymbol(Name, ExportedSymbolsOnly))
96 for (auto BLH : LogicalModules[LMH].BaseLayerHandles)
97 if (auto Symbol = BaseLayer.findSymbolIn(BLH, Name, ExportedSymbolsOnly))
102 JITSymbol findSymbolInternally(LogicalModuleHandle LMH,
103 const std::string &Name) {
104 if (auto Symbol = findSymbolInLogicalModule(LMH, Name, false))
107 for (typename LogicalModuleList::size_type I = 0, E = LogicalModules.size();
110 if (auto Symbol = findSymbolInLogicalModule(I, Name, false))
117 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
118 for (typename LogicalModuleList::size_type I = 0, E = LogicalModules.size();
120 if (auto Sym = findSymbolInLogicalModule(I, Name, ExportedSymbolsOnly))
125 LogicalDylibResources& getDylibResources() { return DylibResources; }
127 LogicalModuleResources*
128 getLogicalModuleResourcesForSymbol(const std::string &Name,
129 bool ExportedSymbolsOnly) {
130 for (typename LogicalModuleList::size_type I = 0, E = LogicalModules.size();
132 if (auto Sym = LogicalModules[I].Resources.findSymbol(Name, ExportedSymbolsOnly))
133 return &LogicalModules[I].Resources;
138 BaseLayerT BaseLayer;
139 LogicalModuleList LogicalModules;
140 LogicalDylibResources DylibResources;
143 } // End namespace orc.
144 } // End namespace llvm.
146 #endif // LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H