LLVM  3.7.0
JITSymbol.h
Go to the documentation of this file.
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 
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.
36  JITSymbol(std::nullptr_t)
37  : JITSymbolBase(JITSymbolFlags::None), CachedAddr(0) {}
38 
39  /// @brief Create a symbol for a definition with a known address.
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.
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.
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
const NoneType None
Definition: None.h:23
TargetAddress getAddress()
Get the address of the symbol in the target address space.
Definition: JITSymbol.h:60
Base class for symbols in the JIT.
JITSymbol(GetAddressFtor GetAddress, JITSymbolFlags Flags)
Create a symbol for a definition that doesn't have a known address yet.
Definition: JITSymbol.h:52
JITSymbol(std::nullptr_t)
Create a 'null' symbol that represents failure to find a symbol definition.
Definition: JITSymbol.h:36
JITSymbolFlags
Flags for symbols in the JIT.
JITSymbol(TargetAddress Addr, JITSymbolFlags Flags)
Create a symbol for a definition with a known address.
Definition: JITSymbol.h:40
Represents a symbol in the JIT.
Definition: JITSymbol.h:29
uint64_t TargetAddress
Represents an address in the target process's address space.
Definition: JITSymbol.h:26
std::function< TargetAddress()> GetAddressFtor
Definition: JITSymbol.h:32