[Orc] Add a JITSymbol class to the Orc APIs, refactor APIs, update clients.
[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 <functional>
18
19 namespace llvm {
20
21 /// @brief Represents an address in the target process's address space.
22 typedef uint64_t TargetAddress;
23
24 /// @brief Represents a symbol in the JIT.
25 class JITSymbol {
26 public:
27   typedef std::function<TargetAddress()> GetAddressFtor;
28
29   JITSymbol(std::nullptr_t) : CachedAddr(0) {}
30
31   JITSymbol(GetAddressFtor GetAddress)
32       : CachedAddr(0), GetAddress(std::move(GetAddress)) {}
33
34   /// @brief Returns true if the symbol exists, false otherwise.
35   explicit operator bool() const { return CachedAddr || GetAddress; }
36
37   /// @brief Get the address of the symbol in the target address space. Returns
38   ///        '0' if the symbol does not exist.
39   TargetAddress getAddress() {
40     if (GetAddress) {
41       CachedAddr = GetAddress();
42       assert(CachedAddr && "Symbol could not be materialized.");
43       GetAddress = nullptr;
44     }
45     return CachedAddr;
46   }
47
48 private:
49   TargetAddress CachedAddr;
50   GetAddressFtor GetAddress;
51 };
52
53 }
54
55 #endif // LLVM_EXECUTIONENGINE_ORC_JITSYMBOL_H