1 //===------ ExecutorAddress.h - Executing process address -------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Represents an address in the executing program.
11 // This file was derived from
12 // llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h.
14 //===----------------------------------------------------------------------===//
16 #ifndef ORC_RT_EXECUTOR_ADDRESS_H
17 #define ORC_RT_EXECUTOR_ADDRESS_H
20 #include "simple_packed_serialization.h"
23 #include <type_traits>
27 using ExecutorAddrDiff
= uint64_t;
29 /// Represents an address in the executor process.
32 /// A wrap/unwrap function that leaves pointers unmodified.
33 template <typename T
> using rawPtr
= orc_rt::identity
<T
*>;
35 /// Default wrap function to use on this host.
36 template <typename T
> using defaultWrap
= rawPtr
<T
>;
38 /// Default unwrap function to use on this host.
39 template <typename T
> using defaultUnwrap
= rawPtr
<T
>;
41 /// Merges a tag into the raw address value:
42 /// P' = P | (TagValue << TagOffset).
45 constexpr Tag(uintptr_t TagValue
, uintptr_t TagOffset
)
46 : TagMask(TagValue
<< TagOffset
) {}
48 template <typename T
> constexpr T
*operator()(T
*P
) {
49 return reinterpret_cast<T
*>(reinterpret_cast<uintptr_t>(P
) | TagMask
);
56 /// Strips a tag of the given length from the given offset within the pointer:
57 /// P' = P & ~(((1 << TagLen) -1) << TagOffset)
60 constexpr Untag(uintptr_t TagLen
, uintptr_t TagOffset
)
61 : UntagMask(~(((uintptr_t(1) << TagLen
) - 1) << TagOffset
)) {}
63 template <typename T
> constexpr T
*operator()(T
*P
) {
64 return reinterpret_cast<T
*>(reinterpret_cast<uintptr_t>(P
) & UntagMask
);
71 ExecutorAddr() = default;
72 explicit ExecutorAddr(uint64_t Addr
) : Addr(Addr
) {}
74 /// Create an ExecutorAddr from the given pointer.
75 template <typename T
, typename UnwrapFn
= defaultUnwrap
<T
>>
76 static ExecutorAddr
fromPtr(T
*Ptr
, UnwrapFn
&&Unwrap
= UnwrapFn()) {
78 static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Unwrap(Ptr
))));
81 /// Cast this ExecutorAddr to a pointer of the given type.
82 template <typename T
, typename WrapFn
= defaultWrap
<std::remove_pointer_t
<T
>>>
83 std::enable_if_t
<std::is_pointer
<T
>::value
, T
>
84 toPtr(WrapFn
&&Wrap
= WrapFn()) const {
85 uintptr_t IntPtr
= static_cast<uintptr_t>(Addr
);
86 assert(IntPtr
== Addr
&& "ExecutorAddr value out of range for uintptr_t");
87 return Wrap(reinterpret_cast<T
>(IntPtr
));
90 /// Cast this ExecutorAddr to a pointer of the given function type.
91 template <typename T
, typename WrapFn
= defaultWrap
<T
>>
92 std::enable_if_t
<std::is_function
<T
>::value
, T
*>
93 toPtr(WrapFn
&&Wrap
= WrapFn()) const {
94 uintptr_t IntPtr
= static_cast<uintptr_t>(Addr
);
95 assert(IntPtr
== Addr
&& "ExecutorAddr value out of range for uintptr_t");
96 return Wrap(reinterpret_cast<T
*>(IntPtr
));
99 uint64_t getValue() const { return Addr
; }
100 void setValue(uint64_t Addr
) { this->Addr
= Addr
; }
101 bool isNull() const { return Addr
== 0; }
103 explicit operator bool() const { return Addr
!= 0; }
105 friend bool operator==(const ExecutorAddr
&LHS
, const ExecutorAddr
&RHS
) {
106 return LHS
.Addr
== RHS
.Addr
;
109 friend bool operator!=(const ExecutorAddr
&LHS
, const ExecutorAddr
&RHS
) {
110 return LHS
.Addr
!= RHS
.Addr
;
113 friend bool operator<(const ExecutorAddr
&LHS
, const ExecutorAddr
&RHS
) {
114 return LHS
.Addr
< RHS
.Addr
;
117 friend bool operator<=(const ExecutorAddr
&LHS
, const ExecutorAddr
&RHS
) {
118 return LHS
.Addr
<= RHS
.Addr
;
121 friend bool operator>(const ExecutorAddr
&LHS
, const ExecutorAddr
&RHS
) {
122 return LHS
.Addr
> RHS
.Addr
;
125 friend bool operator>=(const ExecutorAddr
&LHS
, const ExecutorAddr
&RHS
) {
126 return LHS
.Addr
>= RHS
.Addr
;
129 ExecutorAddr
&operator++() {
133 ExecutorAddr
&operator--() {
137 ExecutorAddr
operator++(int) { return ExecutorAddr(Addr
++); }
138 ExecutorAddr
operator--(int) { return ExecutorAddr(Addr
++); }
140 ExecutorAddr
&operator+=(const ExecutorAddrDiff Delta
) {
145 ExecutorAddr
&operator-=(const ExecutorAddrDiff Delta
) {
154 /// Subtracting two addresses yields an offset.
155 inline ExecutorAddrDiff
operator-(const ExecutorAddr
&LHS
,
156 const ExecutorAddr
&RHS
) {
157 return ExecutorAddrDiff(LHS
.getValue() - RHS
.getValue());
160 /// Adding an offset and an address yields an address.
161 inline ExecutorAddr
operator+(const ExecutorAddr
&LHS
,
162 const ExecutorAddrDiff
&RHS
) {
163 return ExecutorAddr(LHS
.getValue() + RHS
);
166 /// Adding an address and an offset yields an address.
167 inline ExecutorAddr
operator+(const ExecutorAddrDiff
&LHS
,
168 const ExecutorAddr
&RHS
) {
169 return ExecutorAddr(LHS
+ RHS
.getValue());
172 /// Represents an address range in the exceutor process.
173 struct ExecutorAddrRange
{
174 ExecutorAddrRange() = default;
175 ExecutorAddrRange(ExecutorAddr Start
, ExecutorAddr End
)
176 : Start(Start
), End(End
) {}
177 ExecutorAddrRange(ExecutorAddr Start
, ExecutorAddrDiff Size
)
178 : Start(Start
), End(Start
+ Size
) {}
180 bool empty() const { return Start
== End
; }
181 ExecutorAddrDiff
size() const { return End
- Start
; }
183 friend bool operator==(const ExecutorAddrRange
&LHS
,
184 const ExecutorAddrRange
&RHS
) {
185 return LHS
.Start
== RHS
.Start
&& LHS
.End
== RHS
.End
;
187 friend bool operator!=(const ExecutorAddrRange
&LHS
,
188 const ExecutorAddrRange
&RHS
) {
189 return !(LHS
== RHS
);
191 bool contains(ExecutorAddr Addr
) const { return Start
<= Addr
&& Addr
< End
; }
192 bool overlaps(const ExecutorAddrRange
&Other
) {
193 return !(Other
.End
<= Start
|| End
<= Other
.Start
);
196 template <typename T
> span
<T
> toSpan() const {
197 assert(size() % sizeof(T
) == 0 &&
198 "AddressRange is not a multiple of sizeof(T)");
199 return span
<T
>(Start
.toPtr
<T
*>(), size() / sizeof(T
));
206 /// SPS serializatior for ExecutorAddr.
207 template <> class SPSSerializationTraits
<SPSExecutorAddr
, ExecutorAddr
> {
209 static size_t size(const ExecutorAddr
&EA
) {
210 return SPSArgList
<uint64_t>::size(EA
.getValue());
213 static bool serialize(SPSOutputBuffer
&BOB
, const ExecutorAddr
&EA
) {
214 return SPSArgList
<uint64_t>::serialize(BOB
, EA
.getValue());
217 static bool deserialize(SPSInputBuffer
&BIB
, ExecutorAddr
&EA
) {
219 if (!SPSArgList
<uint64_t>::deserialize(BIB
, Tmp
))
221 EA
= ExecutorAddr(Tmp
);
226 using SPSExecutorAddrRange
= SPSTuple
<SPSExecutorAddr
, SPSExecutorAddr
>;
228 /// Serialization traits for address ranges.
230 class SPSSerializationTraits
<SPSExecutorAddrRange
, ExecutorAddrRange
> {
232 static size_t size(const ExecutorAddrRange
&Value
) {
233 return SPSArgList
<SPSExecutorAddr
, SPSExecutorAddr
>::size(Value
.Start
,
237 static bool serialize(SPSOutputBuffer
&BOB
, const ExecutorAddrRange
&Value
) {
238 return SPSArgList
<SPSExecutorAddr
, SPSExecutorAddr
>::serialize(
239 BOB
, Value
.Start
, Value
.End
);
242 static bool deserialize(SPSInputBuffer
&BIB
, ExecutorAddrRange
&Value
) {
243 return SPSArgList
<SPSExecutorAddr
, SPSExecutorAddr
>::deserialize(
244 BIB
, Value
.Start
, Value
.End
);
248 using SPSExecutorAddrRangeSequence
= SPSSequence
<SPSExecutorAddrRange
>;
250 } // End namespace orc_rt
254 // Make ExecutorAddr hashable.
255 template <> struct hash
<orc_rt::ExecutorAddr
> {
256 size_t operator()(const orc_rt::ExecutorAddr
&A
) const {
257 return hash
<uint64_t>()(A
.getValue());
263 #endif // ORC_RT_EXECUTOR_ADDRESS_H