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