1 //===---- GlobalMappingLayer.h - Run all IR through a functor ---*- 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 // Convenience layer for injecting symbols that will appear in calls to
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_EXECUTIONENGINE_ORC_GLOBALMAPPINGLAYER_H
16 #define LLVM_EXECUTIONENGINE_ORC_GLOBALMAPPINGLAYER_H
18 #include "JITSymbol.h"
24 /// @brief Global mapping layer.
26 /// This layer overrides the findSymbol method to first search a local symbol
27 /// table that the client can define. It can be used to inject new symbol
28 /// mappings into the JIT. Beware, however: symbols within a single IR module or
29 /// object file will still resolve locally (via RuntimeDyld's symbol table) -
30 /// such internal references cannot be overriden via this layer.
31 template <typename BaseLayerT>
32 class GlobalMappingLayer {
34 /// @brief Handle to a set of added modules.
35 typedef typename BaseLayerT::ModuleSetHandleT ModuleSetHandleT;
37 /// @brief Construct an GlobalMappingLayer with the given BaseLayer
38 GlobalMappingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
40 /// @brief Add the given module set to the JIT.
41 /// @return A handle for the added modules.
42 template <typename ModuleSetT, typename MemoryManagerPtrT,
43 typename SymbolResolverPtrT>
44 ModuleSetHandleT addModuleSet(ModuleSetT Ms,
45 MemoryManagerPtrT MemMgr,
46 SymbolResolverPtrT Resolver) {
47 return BaseLayer.addModuleSet(std::move(Ms), std::move(MemMgr),
51 /// @brief Remove the module set associated with the handle H.
52 void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeModuleSet(H); }
54 /// @brief Manually set the address to return for the given symbol.
55 void setGlobalMapping(const std::string &Name, TargetAddress Addr) {
56 SymbolTable[Name] = Addr;
59 /// @brief Remove the given symbol from the global mapping.
60 void eraseGlobalMapping(const std::string &Name) {
61 SymbolTable.erase(Name);
64 /// @brief Search for the given named symbol.
66 /// This method will first search the local symbol table, returning
67 /// any symbol found there. If the symbol is not found in the local
68 /// table then this call will be passed through to the base layer.
70 /// @param Name The name of the symbol to search for.
71 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
72 /// @return A handle for the given named symbol, if it exists.
73 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
74 auto I = SymbolTable.find(Name);
75 if (I != SymbolTable.end())
76 return JITSymbol(I->second, JITSymbolFlags::Exported);
77 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
80 /// @brief Get the address of the given symbol in the context of the set of
81 /// modules represented by the handle H. This call is forwarded to the
82 /// base layer's implementation.
83 /// @param H The handle for the module set to search in.
84 /// @param Name The name of the symbol to search for.
85 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
86 /// @return A handle for the given named symbol, if it is found in the
88 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
89 bool ExportedSymbolsOnly) {
90 return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
93 /// @brief Immediately emit and finalize the module set represented by the
95 /// @param H Handle for module set to emit/finalize.
96 void emitAndFinalize(ModuleSetHandleT H) {
97 BaseLayer.emitAndFinalize(H);
101 BaseLayerT &BaseLayer;
102 std::map<std::string, TargetAddress> SymbolTable;
105 } // End namespace orc.
106 } // End namespace llvm.
108 #endif // LLVM_EXECUTIONENGINE_ORC_GLOBALMAPPINGLAYER_H