#ifndef LLVM_EXECUTIONENGINE_ORC_SHARED_EXECUTORADDRESS_H
#define LLVM_EXECUTIONENGINE_ORC_SHARED_EXECUTORADDRESS_H
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <type_traits>
namespace llvm {
namespace orc {
using ExecutorAddrDiff = uint64_t;
class ExecutorAddr {
public:
ExecutorAddr() = default;
explicit constexpr ExecutorAddr(uint64_t Addr) : Addr(Addr) {}
template <typename T> static ExecutorAddr fromPtr(T *Value) {
return ExecutorAddr(
static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Value)));
}
template <typename T>
std::enable_if_t<std::is_pointer<T>::value, T> toPtr() const {
uintptr_t IntPtr = static_cast<uintptr_t>(Addr);
assert(IntPtr == Addr && "ExecutorAddr value out of range for uintptr_t");
return reinterpret_cast<T>(IntPtr);
}
template <typename T>
std::enable_if_t<std::is_function<T>::value, T *> toPtr() const {
uintptr_t IntPtr = static_cast<uintptr_t>(Addr);
assert(IntPtr == Addr && "ExecutorAddr value out of range for uintptr_t");
return reinterpret_cast<T *>(IntPtr);
}
uint64_t getValue() const { return Addr; }
void setValue(uint64_t Addr) { this->Addr = Addr; }
bool isNull() const { return Addr == 0; }
explicit operator bool() const { return Addr != 0; }
friend bool operator==(const ExecutorAddr &LHS, const ExecutorAddr &RHS) {
return LHS.Addr == RHS.Addr;
}
friend bool operator!=(const ExecutorAddr &LHS, const ExecutorAddr &RHS) {
return LHS.Addr != RHS.Addr;
}
friend bool operator<(const ExecutorAddr &LHS, const ExecutorAddr &RHS) {
return LHS.Addr < RHS.Addr;
}
friend bool operator<=(const ExecutorAddr &LHS, const ExecutorAddr &RHS) {
return LHS.Addr <= RHS.Addr;
}
friend bool operator>(const ExecutorAddr &LHS, const ExecutorAddr &RHS) {
return LHS.Addr > RHS.Addr;
}
friend bool operator>=(const ExecutorAddr &LHS, const ExecutorAddr &RHS) {
return LHS.Addr >= RHS.Addr;
}
ExecutorAddr &operator++() {
++Addr;
return *this;
}
ExecutorAddr &operator--() {
--Addr;
return *this;
}
ExecutorAddr operator++(int) { return ExecutorAddr(Addr++); }
ExecutorAddr operator--(int) { return ExecutorAddr(Addr--); }
ExecutorAddr &operator+=(const ExecutorAddrDiff &Delta) {
Addr += Delta;
return *this;
}
ExecutorAddr &operator-=(const ExecutorAddrDiff &Delta) {
Addr -= Delta;
return *this;
}
private:
uint64_t Addr = 0;
};
inline ExecutorAddrDiff operator-(const ExecutorAddr &LHS,
const ExecutorAddr &RHS) {
return ExecutorAddrDiff(LHS.getValue() - RHS.getValue());
}
inline ExecutorAddr operator+(const ExecutorAddr &LHS,
const ExecutorAddrDiff &RHS) {
return ExecutorAddr(LHS.getValue() + RHS);
}
inline ExecutorAddr operator+(const ExecutorAddrDiff &LHS,
const ExecutorAddr &RHS) {
return ExecutorAddr(LHS + RHS.getValue());
}
inline ExecutorAddr operator-(const ExecutorAddr &LHS,
const ExecutorAddrDiff &RHS) {
return ExecutorAddr(LHS.getValue() - RHS);
}
inline ExecutorAddrDiff operator%(const ExecutorAddr &LHS,
const ExecutorAddrDiff &RHS) {
return ExecutorAddrDiff(LHS.getValue() % RHS);
}
struct ExecutorAddrRange {
ExecutorAddrRange() = default;
ExecutorAddrRange(ExecutorAddr Start, ExecutorAddr End)
: Start(Start), End(End) {}
ExecutorAddrRange(ExecutorAddr Start, ExecutorAddrDiff Size)
: Start(Start), End(Start + Size) {}
bool empty() const { return Start == End; }
ExecutorAddrDiff size() const { return End - Start; }
friend bool operator==(const ExecutorAddrRange &LHS,
const ExecutorAddrRange &RHS) {
return LHS.Start == RHS.Start && LHS.End == RHS.End;
}
friend bool operator!=(const ExecutorAddrRange &LHS,
const ExecutorAddrRange &RHS) {
return !(LHS == RHS);
}
bool contains(ExecutorAddr Addr) const { return Start <= Addr && Addr < End; }
bool overlaps(const ExecutorAddrRange &Other) {
return !(Other.End <= Start || End <= Other.Start);
}
ExecutorAddr Start;
ExecutorAddr End;
};
inline raw_ostream &operator<<(raw_ostream &OS, const ExecutorAddr &A) {
return OS << formatv("{0:x}", A.getValue());
}
inline raw_ostream &operator<<(raw_ostream &OS, const ExecutorAddrRange &R) {
return OS << formatv("{0:x} -- {1:x}", R.Start.getValue(), R.End.getValue());
}
namespace shared {
class SPSExecutorAddr {};
template <> class SPSSerializationTraits<SPSExecutorAddr, ExecutorAddr> {
public:
static size_t size(const ExecutorAddr &EA) {
return SPSArgList<uint64_t>::size(EA.getValue());
}
static bool serialize(SPSOutputBuffer &BOB, const ExecutorAddr &EA) {
return SPSArgList<uint64_t>::serialize(BOB, EA.getValue());
}
static bool deserialize(SPSInputBuffer &BIB, ExecutorAddr &EA) {
uint64_t Tmp;
if (!SPSArgList<uint64_t>::deserialize(BIB, Tmp))
return false;
EA = ExecutorAddr(Tmp);
return true;
}
};
using SPSExecutorAddrRange = SPSTuple<SPSExecutorAddr, SPSExecutorAddr>;
template <>
class SPSSerializationTraits<SPSExecutorAddrRange, ExecutorAddrRange> {
public:
static size_t size(const ExecutorAddrRange &Value) {
return SPSArgList<SPSExecutorAddr, SPSExecutorAddr>::size(Value.Start,
Value.End);
}
static bool serialize(SPSOutputBuffer &BOB, const ExecutorAddrRange &Value) {
return SPSArgList<SPSExecutorAddr, SPSExecutorAddr>::serialize(
BOB, Value.Start, Value.End);
}
static bool deserialize(SPSInputBuffer &BIB, ExecutorAddrRange &Value) {
return SPSArgList<SPSExecutorAddr, SPSExecutorAddr>::deserialize(
BIB, Value.Start, Value.End);
}
};
using SPSExecutorAddrRangeSequence = SPSSequence<SPSExecutorAddrRange>;
} }
template <> struct DenseMapInfo<orc::ExecutorAddr> {
static inline orc::ExecutorAddr getEmptyKey() {
return orc::ExecutorAddr(DenseMapInfo<uint64_t>::getEmptyKey());
}
static inline orc::ExecutorAddr getTombstoneKey() {
return orc::ExecutorAddr(DenseMapInfo<uint64_t>::getTombstoneKey());
}
static unsigned getHashValue(const orc::ExecutorAddr &Addr) {
return DenseMapInfo<uint64_t>::getHashValue(Addr.getValue());
}
static bool isEqual(const orc::ExecutorAddr &LHS,
const orc::ExecutorAddr &RHS) {
return DenseMapInfo<uint64_t>::isEqual(LHS.getValue(), RHS.getValue());
}
};
}
#endif