1 //===- ObjectTransformLayer.h - Run all objects through functor -*- 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 // Run all objects passed in through a user supplied functor.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
16 
17 #include "JITSymbol.h"
18 
19 namespace llvm {
20 namespace orc {
21 
22 /// @brief Object mutating layer.
23 ///
24 ///   This layer accepts sets of ObjectFiles (via addObjectSet). It
25 /// immediately applies the user supplied functor to each object, then adds
26 /// the set of transformed objects to the layer below.
27 template <typename BaseLayerT, typename TransformFtor>
28 class ObjectTransformLayer {
29 public:
30   /// @brief Handle to a set of added objects.
31   typedef typename BaseLayerT::ObjSetHandleT ObjSetHandleT;
32 
33   /// @brief Construct an ObjectTransformLayer with the given BaseLayer
34   ObjectTransformLayer(BaseLayerT &BaseLayer,
35                        TransformFtor Transform = TransformFtor())
BaseLayer(BaseLayer)36       : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
37 
38   /// @brief Apply the transform functor to each object in the object set, then
39   ///        add the resulting set of objects to the base layer, along with the
40   ///        memory manager and symbol resolver.
41   ///
42   /// @return A handle for the added objects.
43   template <typename ObjSetT, typename MemoryManagerPtrT,
44             typename SymbolResolverPtrT>
addObjectSet(ObjSetT & Objects,MemoryManagerPtrT MemMgr,SymbolResolverPtrT Resolver)45   ObjSetHandleT addObjectSet(ObjSetT &Objects, MemoryManagerPtrT MemMgr,
46                              SymbolResolverPtrT Resolver) {
47 
48     for (auto I = Objects.begin(), E = Objects.end(); I != E; ++I)
49       *I = Transform(std::move(*I));
50 
51     return BaseLayer.addObjectSet(Objects, std::move(MemMgr),
52                                   std::move(Resolver));
53   }
54 
55   /// @brief Remove the object set associated with the handle H.
removeObjectSet(ObjSetHandleT H)56   void removeObjectSet(ObjSetHandleT H) { BaseLayer.removeObjectSet(H); }
57 
58   /// @brief Search for the given named symbol.
59   /// @param Name The name of the symbol to search for.
60   /// @param ExportedSymbolsOnly If true, search only for exported symbols.
61   /// @return A handle for the given named symbol, if it exists.
findSymbol(const std::string & Name,bool ExportedSymbolsOnly)62   JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
63     return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
64   }
65 
66   /// @brief Get the address of the given symbol in the context of the set of
67   ///        objects represented by the handle H. This call is forwarded to the
68   ///        base layer's implementation.
69   /// @param H The handle for the object set to search in.
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 is found in the
73   ///         given object set.
findSymbolIn(ObjSetHandleT H,const std::string & Name,bool ExportedSymbolsOnly)74   JITSymbol findSymbolIn(ObjSetHandleT H, const std::string &Name,
75                          bool ExportedSymbolsOnly) {
76     return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
77   }
78 
79   /// @brief Immediately emit and finalize the object set represented by the
80   ///        given handle.
81   /// @param H Handle for object set to emit/finalize.
emitAndFinalize(ObjSetHandleT H)82   void emitAndFinalize(ObjSetHandleT H) { BaseLayer.emitAndFinalize(H); }
83 
84   /// @brief Map section addresses for the objects associated with the handle H.
mapSectionAddress(ObjSetHandleT H,const void * LocalAddress,TargetAddress TargetAddr)85   void mapSectionAddress(ObjSetHandleT H, const void *LocalAddress,
86                          TargetAddress TargetAddr) {
87     BaseLayer.mapSectionAddress(H, LocalAddress, TargetAddr);
88   }
89 
90   /// @brief Access the transform functor directly.
getTransform()91   TransformFtor &getTransform() { return Transform; }
92 
93   /// @brief Access the mumate functor directly.
getTransform()94   const TransformFtor &getTransform() const { return Transform; }
95 
96 private:
97   BaseLayerT &BaseLayer;
98   TransformFtor Transform;
99 };
100 
101 } // End namespace orc.
102 } // End namespace llvm.
103 
104 #endif // LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
105