Home | History | Annotate | Download | only in Orc
      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 
     14 #ifndef LLVM_EXECUTIONENGINE_ORC_JITSYMBOL_H
     15 #define LLVM_EXECUTIONENGINE_ORC_JITSYMBOL_H
     16 
     17 #include "llvm/ExecutionEngine/JITSymbolFlags.h"
     18 #include "llvm/ExecutionEngine/RuntimeDyld.h"
     19 #include "llvm/Support/DataTypes.h"
     20 #include <cassert>
     21 #include <functional>
     22 
     23 namespace llvm {
     24 namespace orc {
     25 
     26 /// @brief Represents an address in the target process's address space.
     27 typedef uint64_t TargetAddress;
     28 
     29 /// @brief Represents a symbol in the JIT.
     30 class JITSymbol : public JITSymbolBase {
     31 public:
     32 
     33   typedef std::function<TargetAddress()> GetAddressFtor;
     34 
     35   /// @brief Create a 'null' symbol that represents failure to find a symbol
     36   ///        definition.
     37   JITSymbol(std::nullptr_t)
     38       : JITSymbolBase(JITSymbolFlags::None), CachedAddr(0) {}
     39 
     40   /// @brief Create a symbol for a definition with a known address.
     41   JITSymbol(TargetAddress Addr, JITSymbolFlags Flags)
     42     : JITSymbolBase(Flags), CachedAddr(Addr) {}
     43 
     44   /// @brief Create a symbol for a definition that doesn't have a known address
     45   ///        yet.
     46   /// @param GetAddress A functor to materialize a definition (fixing the
     47   ///        address) on demand.
     48   ///
     49   ///   This constructor allows a JIT layer to provide a reference to a symbol
     50   /// definition without actually materializing the definition up front. The
     51   /// user can materialize the definition at any time by calling the getAddress
     52   /// method.
     53   JITSymbol(GetAddressFtor GetAddress, JITSymbolFlags Flags)
     54       : JITSymbolBase(Flags), GetAddress(std::move(GetAddress)), CachedAddr(0) {}
     55 
     56   /// @brief Create a JITSymbol from a RuntimeDyld::SymbolInfo.
     57   JITSymbol(const RuntimeDyld::SymbolInfo &Sym)
     58     : JITSymbolBase(Sym.getFlags()), CachedAddr(Sym.getAddress()) {}
     59 
     60   /// @brief Returns true if the symbol exists, false otherwise.
     61   explicit operator bool() const { return CachedAddr || GetAddress; }
     62 
     63   /// @brief Get the address of the symbol in the target address space. Returns
     64   ///        '0' if the symbol does not exist.
     65   TargetAddress getAddress() {
     66     if (GetAddress) {
     67       CachedAddr = GetAddress();
     68       assert(CachedAddr && "Symbol could not be materialized.");
     69       GetAddress = nullptr;
     70     }
     71     return CachedAddr;
     72   }
     73 
     74   /// @brief Convert this JITSymbol to a RuntimeDyld::SymbolInfo.
     75   RuntimeDyld::SymbolInfo toRuntimeDyldSymbol() {
     76     return RuntimeDyld::SymbolInfo(getAddress(), getFlags());
     77   }
     78 
     79 private:
     80   GetAddressFtor GetAddress;
     81   TargetAddress CachedAddr;
     82 };
     83 
     84 } // End namespace orc.
     85 } // End namespace llvm.
     86 
     87 #endif // LLVM_EXECUTIONENGINE_ORC_JITSYMBOL_H
     88