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