|
| 1 | +//===------ ExecutorAddress.h - Executing process address -------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// Utilites for representing addresses and address ranges in the executing |
| 10 | +// program that can be shared with an ORC controller. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#ifndef ORC_RT_EXECUTORADDRESS_H |
| 15 | +#define ORC_RT_EXECUTORADDRESS_H |
| 16 | + |
| 17 | +#include "span.h" |
| 18 | + |
| 19 | +#include <cassert> |
| 20 | +#include <cstdint> |
| 21 | +#include <functional> |
| 22 | +#include <type_traits> |
| 23 | + |
| 24 | +namespace orc_rt { |
| 25 | + |
| 26 | +using ExecutorAddrDiff = uint64_t; |
| 27 | + |
| 28 | +/// Represents an address in the executor process. |
| 29 | +class ExecutorAddr { |
| 30 | +public: |
| 31 | + /// Return pointer unmodified. |
| 32 | + template <typename T> struct rawPtr { |
| 33 | + T *operator()(T *p) const { return p; } |
| 34 | + }; |
| 35 | + |
| 36 | + /// Default wrap function to use on this host. |
| 37 | + template <typename T> using defaultWrap = rawPtr<T>; |
| 38 | + |
| 39 | + /// Default unwrap function to use on this host. |
| 40 | + template <typename T> using defaultUnwrap = rawPtr<T>; |
| 41 | + |
| 42 | + /// Merges a tag into the raw address value: |
| 43 | + /// P' = P | (TagValue << TagOffset). |
| 44 | + class Tag { |
| 45 | + public: |
| 46 | + constexpr Tag(uintptr_t TagValue, uintptr_t TagOffset) |
| 47 | + : TagMask(TagValue << TagOffset) {} |
| 48 | + |
| 49 | + template <typename T> constexpr T *operator()(T *P) { |
| 50 | + return reinterpret_cast<T *>(reinterpret_cast<uintptr_t>(P) | TagMask); |
| 51 | + } |
| 52 | + |
| 53 | + private: |
| 54 | + uintptr_t TagMask; |
| 55 | + }; |
| 56 | + |
| 57 | + /// Strips a tag of the given length from the given offset within the pointer: |
| 58 | + /// P' = P & ~(((1 << TagLen) -1) << TagOffset) |
| 59 | + class Untag { |
| 60 | + public: |
| 61 | + constexpr Untag(uintptr_t TagLen, uintptr_t TagOffset) |
| 62 | + : UntagMask(~(((uintptr_t(1) << TagLen) - 1) << TagOffset)) {} |
| 63 | + |
| 64 | + template <typename T> constexpr T *operator()(T *P) { |
| 65 | + return reinterpret_cast<T *>(reinterpret_cast<uintptr_t>(P) & UntagMask); |
| 66 | + } |
| 67 | + |
| 68 | + private: |
| 69 | + uintptr_t UntagMask; |
| 70 | + }; |
| 71 | + |
| 72 | + constexpr ExecutorAddr() noexcept = default; |
| 73 | + explicit constexpr ExecutorAddr(uint64_t Addr) noexcept : Addr(Addr) {} |
| 74 | + |
| 75 | + /// Create an ExecutorAddr from the given pointer. |
| 76 | + template <typename T, typename UnwrapFn = defaultUnwrap<T>> |
| 77 | + static constexpr ExecutorAddr fromPtr(T *Ptr, |
| 78 | + UnwrapFn &&Unwrap = UnwrapFn()) { |
| 79 | + return ExecutorAddr( |
| 80 | + static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Unwrap(Ptr)))); |
| 81 | + } |
| 82 | + |
| 83 | + /// Cast this ExecutorAddr to a pointer of the given type. |
| 84 | + template <typename T, typename WrapFn = defaultWrap<std::remove_pointer_t<T>>> |
| 85 | + constexpr std::enable_if_t<std::is_pointer<T>::value, T> |
| 86 | + toPtr(WrapFn &&Wrap = WrapFn()) const { |
| 87 | + uintptr_t IntPtr = static_cast<uintptr_t>(Addr); |
| 88 | + assert(IntPtr == Addr && "ExecutorAddr value out of range for uintptr_t"); |
| 89 | + return Wrap(reinterpret_cast<T>(IntPtr)); |
| 90 | + } |
| 91 | + |
| 92 | + /// Cast this ExecutorAddr to a pointer of the given function type. |
| 93 | + template <typename T, typename WrapFn = defaultWrap<T>> |
| 94 | + constexpr std::enable_if_t<std::is_function<T>::value, T *> |
| 95 | + toPtr(WrapFn &&Wrap = WrapFn()) const { |
| 96 | + uintptr_t IntPtr = static_cast<uintptr_t>(Addr); |
| 97 | + assert(IntPtr == Addr && "ExecutorAddr value out of range for uintptr_t"); |
| 98 | + return Wrap(reinterpret_cast<T *>(IntPtr)); |
| 99 | + } |
| 100 | + |
| 101 | + constexpr uint64_t getValue() const noexcept { return Addr; } |
| 102 | + constexpr void setValue(uint64_t Addr) noexcept { this->Addr = Addr; } |
| 103 | + constexpr bool isNull() const noexcept { return Addr == 0; } |
| 104 | + |
| 105 | + constexpr explicit operator bool() const noexcept { return Addr != 0; } |
| 106 | + |
| 107 | + friend constexpr bool operator==(const ExecutorAddr &LHS, |
| 108 | + const ExecutorAddr &RHS) noexcept { |
| 109 | + return LHS.Addr == RHS.Addr; |
| 110 | + } |
| 111 | + |
| 112 | + friend constexpr bool operator!=(const ExecutorAddr &LHS, |
| 113 | + const ExecutorAddr &RHS) noexcept { |
| 114 | + return LHS.Addr != RHS.Addr; |
| 115 | + } |
| 116 | + |
| 117 | + friend constexpr bool operator<(const ExecutorAddr &LHS, |
| 118 | + const ExecutorAddr &RHS) noexcept { |
| 119 | + return LHS.Addr < RHS.Addr; |
| 120 | + } |
| 121 | + |
| 122 | + friend constexpr bool operator<=(const ExecutorAddr &LHS, |
| 123 | + const ExecutorAddr &RHS) noexcept { |
| 124 | + return LHS.Addr <= RHS.Addr; |
| 125 | + } |
| 126 | + |
| 127 | + friend constexpr bool operator>(const ExecutorAddr &LHS, |
| 128 | + const ExecutorAddr &RHS) noexcept { |
| 129 | + return LHS.Addr > RHS.Addr; |
| 130 | + } |
| 131 | + |
| 132 | + friend constexpr bool operator>=(const ExecutorAddr &LHS, |
| 133 | + const ExecutorAddr &RHS) noexcept { |
| 134 | + return LHS.Addr >= RHS.Addr; |
| 135 | + } |
| 136 | + |
| 137 | + constexpr ExecutorAddr &operator++() noexcept { |
| 138 | + ++Addr; |
| 139 | + return *this; |
| 140 | + } |
| 141 | + constexpr ExecutorAddr &operator--() noexcept { |
| 142 | + --Addr; |
| 143 | + return *this; |
| 144 | + } |
| 145 | + constexpr ExecutorAddr operator++(int) noexcept { |
| 146 | + return ExecutorAddr(Addr++); |
| 147 | + } |
| 148 | + constexpr ExecutorAddr operator--(int) noexcept { |
| 149 | + return ExecutorAddr(Addr++); |
| 150 | + } |
| 151 | + |
| 152 | + constexpr ExecutorAddr &operator+=(const ExecutorAddrDiff Delta) noexcept { |
| 153 | + Addr += Delta; |
| 154 | + return *this; |
| 155 | + } |
| 156 | + |
| 157 | + constexpr ExecutorAddr &operator-=(const ExecutorAddrDiff Delta) noexcept { |
| 158 | + Addr -= Delta; |
| 159 | + return *this; |
| 160 | + } |
| 161 | + |
| 162 | +private: |
| 163 | + uint64_t Addr = 0; |
| 164 | +}; |
| 165 | + |
| 166 | +/// Subtracting two addresses yields an offset. |
| 167 | +inline constexpr ExecutorAddrDiff operator-(const ExecutorAddr &LHS, |
| 168 | + const ExecutorAddr &RHS) noexcept { |
| 169 | + return ExecutorAddrDiff(LHS.getValue() - RHS.getValue()); |
| 170 | +} |
| 171 | + |
| 172 | +/// Adding an offset and an address yields an address. |
| 173 | +inline constexpr ExecutorAddr operator+(const ExecutorAddr &LHS, |
| 174 | + const ExecutorAddrDiff &RHS) noexcept { |
| 175 | + return ExecutorAddr(LHS.getValue() + RHS); |
| 176 | +} |
| 177 | + |
| 178 | +/// Adding an address and an offset yields an address. |
| 179 | +inline constexpr ExecutorAddr operator+(const ExecutorAddrDiff &LHS, |
| 180 | + const ExecutorAddr &RHS) noexcept { |
| 181 | + return ExecutorAddr(LHS + RHS.getValue()); |
| 182 | +} |
| 183 | + |
| 184 | +/// Represents an address range in the exceutor process. |
| 185 | +struct ExecutorAddrRange { |
| 186 | + constexpr ExecutorAddrRange() noexcept = default; |
| 187 | + constexpr ExecutorAddrRange(ExecutorAddr Start, ExecutorAddr End) noexcept |
| 188 | + : Start(Start), End(End) {} |
| 189 | + constexpr ExecutorAddrRange(ExecutorAddr Start, |
| 190 | + ExecutorAddrDiff Size) noexcept |
| 191 | + : Start(Start), End(Start + Size) {} |
| 192 | + |
| 193 | + constexpr bool empty() const noexcept { return Start == End; } |
| 194 | + constexpr ExecutorAddrDiff size() const noexcept { return End - Start; } |
| 195 | + |
| 196 | + friend constexpr bool operator==(const ExecutorAddrRange &LHS, |
| 197 | + const ExecutorAddrRange &RHS) noexcept { |
| 198 | + return LHS.Start == RHS.Start && LHS.End == RHS.End; |
| 199 | + } |
| 200 | + friend constexpr bool operator!=(const ExecutorAddrRange &LHS, |
| 201 | + const ExecutorAddrRange &RHS) noexcept { |
| 202 | + return !(LHS == RHS); |
| 203 | + } |
| 204 | + constexpr bool contains(ExecutorAddr Addr) const noexcept { |
| 205 | + return Start <= Addr && Addr < End; |
| 206 | + } |
| 207 | + constexpr bool overlaps(const ExecutorAddrRange &Other) const noexcept { |
| 208 | + return !(Other.End <= Start || End <= Other.Start); |
| 209 | + } |
| 210 | + |
| 211 | + template <typename T> constexpr span<T> toSpan() const noexcept { |
| 212 | + assert(size() % sizeof(T) == 0 && |
| 213 | + "AddressRange is not a multiple of sizeof(T)"); |
| 214 | + return span<T>(Start.toPtr<T *>(), size() / sizeof(T)); |
| 215 | + } |
| 216 | + |
| 217 | + ExecutorAddr Start; |
| 218 | + ExecutorAddr End; |
| 219 | +}; |
| 220 | + |
| 221 | +} // namespace orc_rt |
| 222 | + |
| 223 | +// Make ExecutorAddr hashable. |
| 224 | +template <> struct std::hash<orc_rt::ExecutorAddr> { |
| 225 | + constexpr size_t operator()(const orc_rt::ExecutorAddr &A) const noexcept { |
| 226 | + return std::hash<uint64_t>()(A.getValue()); |
| 227 | + } |
| 228 | +}; |
| 229 | + |
| 230 | +#endif // ORC_RT_EXECUTORADDRESS_H |
0 commit comments