1 //===- ObjectTransformLayer.h - Run all objects through 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 // Run all objects passed in through a user supplied functor.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
17 #include "JITSymbol.h"
22 /// @brief Object mutating layer.
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 {
30 /// @brief Handle to a set of added objects.
31 typedef typename BaseLayerT::ObjSetHandleT ObjSetHandleT;
33 /// @brief Construct an ObjectTransformLayer with the given BaseLayer
34 ObjectTransformLayer(BaseLayerT &BaseLayer,
35 TransformFtor Transform = TransformFtor())
36 : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
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.
42 /// @return A handle for the added objects.
43 template <typename ObjSetT, typename MemoryManagerPtrT,
44 typename SymbolResolverPtrT>
45 ObjSetHandleT addObjectSet(ObjSetT &Objects, MemoryManagerPtrT MemMgr,
46 SymbolResolverPtrT Resolver) {
48 for (auto I = Objects.begin(), E = Objects.end(); I != E; ++I)
49 *I = Transform(std::move(*I));
51 return BaseLayer.addObjectSet(Objects, std::move(MemMgr),
55 /// @brief Remove the object set associated with the handle H.
56 void removeObjectSet(ObjSetHandleT H) { BaseLayer.removeObjectSet(H); }
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.
62 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
63 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
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
74 JITSymbol findSymbolIn(ObjSetHandleT H, const std::string &Name,
75 bool ExportedSymbolsOnly) {
76 return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
79 /// @brief Immediately emit and finalize the object set represented by the
81 /// @param H Handle for object set to emit/finalize.
82 void emitAndFinalize(ObjSetHandleT H) { BaseLayer.emitAndFinalize(H); }
84 /// @brief Map section addresses for the objects associated with the handle H.
85 void mapSectionAddress(ObjSetHandleT H, const void *LocalAddress,
86 TargetAddress TargetAddr) {
87 BaseLayer.mapSectionAddress(H, LocalAddress, TargetAddr);
90 /// @brief Access the transform functor directly.
91 TransformFtor &getTransform() { return Transform; }
93 /// @brief Access the mumate functor directly.
94 const TransformFtor &getTransform() const { return Transform; }
97 BaseLayerT &BaseLayer;
98 TransformFtor Transform;
101 } // End namespace orc.
102 } // End namespace llvm.
104 #endif // LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H