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