ObjectTransformLayer.h 4.4 KB

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