JITSymbol.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //===----------- JITSymbol.h - JIT symbol abstraction -----------*- 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. // Abstraction for target process addresses.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_EXECUTIONENGINE_ORC_JITSYMBOL_H
  14. #define LLVM_EXECUTIONENGINE_ORC_JITSYMBOL_H
  15. #include "llvm/ExecutionEngine/JITSymbolFlags.h"
  16. #include "llvm/Support/DataTypes.h"
  17. #include <cassert>
  18. #include <functional>
  19. namespace llvm {
  20. namespace orc {
  21. /// @brief Represents an address in the target process's address space.
  22. typedef uint64_t TargetAddress;
  23. /// @brief Represents a symbol in the JIT.
  24. class JITSymbol : public JITSymbolBase {
  25. public:
  26. typedef std::function<TargetAddress()> GetAddressFtor;
  27. /// @brief Create a 'null' symbol that represents failure to find a symbol
  28. /// definition.
  29. JITSymbol(std::nullptr_t)
  30. : JITSymbolBase(JITSymbolFlags::None), CachedAddr(0) {}
  31. /// @brief Create a symbol for a definition with a known address.
  32. JITSymbol(TargetAddress Addr, JITSymbolFlags Flags)
  33. : JITSymbolBase(Flags), CachedAddr(Addr) {}
  34. /// @brief Create a symbol for a definition that doesn't have a known address
  35. /// yet.
  36. /// @param GetAddress A functor to materialize a definition (fixing the
  37. /// address) on demand.
  38. ///
  39. /// This constructor allows a JIT layer to provide a reference to a symbol
  40. /// definition without actually materializing the definition up front. The
  41. /// user can materialize the definition at any time by calling the getAddress
  42. /// method.
  43. JITSymbol(GetAddressFtor GetAddress, JITSymbolFlags Flags)
  44. : JITSymbolBase(Flags), GetAddress(std::move(GetAddress)), CachedAddr(0) {}
  45. /// @brief Returns true if the symbol exists, false otherwise.
  46. explicit operator bool() const { return CachedAddr || GetAddress; }
  47. /// @brief Get the address of the symbol in the target address space. Returns
  48. /// '0' if the symbol does not exist.
  49. TargetAddress getAddress() {
  50. if (GetAddress) {
  51. CachedAddr = GetAddress();
  52. assert(CachedAddr && "Symbol could not be materialized.");
  53. GetAddress = nullptr;
  54. }
  55. return CachedAddr;
  56. }
  57. private:
  58. GetAddressFtor GetAddress;
  59. TargetAddress CachedAddr;
  60. };
  61. } // End namespace orc.
  62. } // End namespace llvm.
  63. #endif // LLVM_EXECUTIONENGINE_ORC_JITSYMBOL_H