GTIRB  v2.2.0
GrammaTech Intermediate Representation for Binaries: C++ API
Addr.hpp
Go to the documentation of this file.
1 //===- Addr.hpp -------------------------------------------------*- C++ -*-===//
2 //
3 // Copyright (C) 2020 GrammaTech, Inc.
4 //
5 // This code is licensed under the MIT license. See the LICENSE file in the
6 // project root for license terms.
7 //
8 // This project is sponsored by the Office of Naval Research, One Liberty
9 // Center, 875 N. Randolph Street, Arlington, VA 22203 under contract #
10 // N68335-17-C-0700. The content of the information does not necessarily
11 // reflect the position or policy of the Government and no official
12 // endorsement should be inferred.
13 //
14 //===----------------------------------------------------------------------===//
15 #ifndef GTIRB_ADDR_H
16 #define GTIRB_ADDR_H
17 
18 #include <gtirb/Export.hpp>
19 #include <cstddef>
20 #include <cstdint>
21 #include <iomanip>
22 #include <iosfwd>
23 #include <optional>
24 
27 
28 namespace gtirb {
38 public:
40  typedef uint64_t value_type;
42  typedef int64_t difference_type;
43 
45  constexpr Addr() noexcept : Address{0} {}
46 
50  constexpr explicit Addr(value_type X) noexcept : Address(X) {}
51 
55  constexpr explicit operator value_type() const noexcept { return Address; }
56 
61  constexpr Addr operator+() const noexcept { return Addr(+Address); }
62 
67  constexpr Addr operator~() const noexcept { return Addr(~Address); }
68 
72  constexpr Addr& operator++() noexcept {
73  ++Address;
74  return *this;
75  }
76 
81  constexpr Addr operator++(int) noexcept {
82  Addr R(*this);
83  ++Address;
84  return R;
85  }
86 
90  constexpr Addr& operator--() noexcept {
91  --Address;
92  return *this;
93  }
94 
99  constexpr Addr operator--(int) noexcept {
100  Addr R(*this);
101  --Address;
102  return R;
103  }
104 
111  friend constexpr Addr operator+(const Addr& A, value_type Offset) noexcept {
112  return Addr(A.Address + Offset);
113  }
114 
121  friend constexpr Addr operator+(value_type Offset, const Addr& A) noexcept {
122  return A + Offset;
123  }
124 
130  constexpr Addr& operator+=(value_type Offset) noexcept {
131  Address += Offset;
132  return *this;
133  }
134 
144  friend constexpr Addr operator-(const Addr& A, value_type Offset) noexcept {
145  return Addr(A.Address - Offset);
146  }
147 
153  constexpr Addr& operator-=(value_type Offset) noexcept {
154  Address -= Offset;
155  return *this;
156  }
157 
165  friend constexpr difference_type operator-(const Addr& A,
166  const Addr& B) noexcept {
167  return static_cast<difference_type>(A.Address - B.Address);
168  }
169 
171  friend constexpr bool operator==(const Addr& LHS, const Addr& RHS) noexcept {
172  return LHS.Address == RHS.Address;
173  }
174 
176  friend constexpr bool operator!=(const Addr& LHS, const Addr& RHS) noexcept {
177  return !operator==(LHS, RHS);
178  }
179 
181  friend constexpr bool operator<(const Addr& LHS, const Addr& RHS) noexcept {
182  return LHS.Address < RHS.Address;
183  }
184 
186  friend constexpr bool operator>(const Addr& LHS, const Addr& RHS) noexcept {
187  return operator<(RHS, LHS);
188  }
189 
191  friend constexpr bool operator<=(const Addr& LHS, const Addr& RHS) noexcept {
192  return !operator<(RHS, LHS);
193  }
194 
196  friend constexpr bool operator>=(const Addr& LHS, const Addr& RHS) noexcept {
197  return !operator<(LHS, RHS);
198  }
199 
200 private:
201  value_type Address{0};
202 };
203 
206 public:
215  constexpr explicit AddrRange(Addr Lower, Addr Upper) noexcept
216  : First(Lower), Size(Upper - (Lower < Upper ? Lower : Upper)) {}
217 
223  constexpr explicit AddrRange(Addr Lower, uint64_t Count) noexcept
224  : First(Lower), Size(Count) {}
225 
227  constexpr bool operator==(const AddrRange& RHS) const noexcept {
228  return First == RHS.First && Size == RHS.Size;
229  }
230 
232  constexpr bool operator!=(const AddrRange& RHS) const noexcept {
233  return First != RHS.First || Size != RHS.Size;
234  }
235 
237  constexpr Addr lower() const noexcept { return First; }
238 
240  constexpr Addr upper() const noexcept { return First + Size; }
241 
243  constexpr uint64_t size() const noexcept { return Size; }
244 
245 private:
247  Addr First;
248 
250  uint64_t Size;
251 };
252 
253 template <typename T> std::optional<uint64_t> asOptionalSize(T X);
254 
255 template <> std::optional<uint64_t> inline asOptionalSize(uint64_t X) {
256  return X;
257 }
258 
259 template <>
260 std::optional<uint64_t> inline asOptionalSize(std::optional<uint64_t> X) {
261  return X;
262 }
263 
275 template <typename T> std::optional<AddrRange> addressRange(const T& Object) {
276  if (std::optional<Addr> A = Object.getAddress()) {
277  if (std::optional<uint64_t> S = Object.getSize()) {
278  return AddrRange{*A, *S};
279  }
280  }
281  return std::nullopt;
282 }
283 
294 template <typename T> std::optional<Addr> addressLimit(const T& Object) {
295  auto A = Object.getAddress();
296  auto B = asOptionalSize(Object.getSize());
297  if (A && B) {
298  return *A + *B;
299  }
300  return std::nullopt;
301 }
302 
314 template <typename T> bool containsAddr(const T& Object, Addr Ea) {
315  return Object.getAddress() <= Ea && addressLimit(Object) > Ea;
316 }
317 
329 template <typename CharT, typename Traits>
330 std::basic_ostream<CharT, Traits>&
331 operator<<(std::basic_ostream<CharT, Traits>& Stream, Addr A) {
332  auto Flags = Stream.flags();
333  Stream << std::setbase(16) << std::showbase << static_cast<uint64_t>(A);
334  Stream.flags(Flags);
335  return Stream;
336 }
337 
349 template <typename CharT, typename Traits>
350 std::basic_ostream<CharT, Traits>&
351 operator<<(std::basic_ostream<CharT, Traits>& Stream, std::optional<Addr> A) {
352  if (A)
353  return Stream << *A;
354  return Stream << "<none>";
355 }
356 
357 } // namespace gtirb
358 
359 namespace std {
360 
362 template <> struct hash<gtirb::Addr> {
363  size_t operator()(const gtirb::Addr& A) const {
364  return std::hash<uint64_t>{}(static_cast<uint64_t>(A));
365  }
366 };
367 
368 } // namespace std
369 
370 #endif // GTIRB_ADDR_H
gtirb::AddrRange::operator==
constexpr bool operator==(const AddrRange &RHS) const noexcept
Equality operator for AddrRange.
Definition: Addr.hpp:227
gtirb::Addr::operator<=
constexpr friend bool operator<=(const Addr &LHS, const Addr &RHS) noexcept
Less-than-or-equal operator for Addr.
Definition: Addr.hpp:191
gtirb::AddrRange::size
constexpr uint64_t size() const noexcept
Number of addresses in the range.
Definition: Addr.hpp:243
gtirb::Addr::addressLimit
std::optional< Addr > addressLimit(const T &Object)
Exclusive upper limit of an object's address range.
Definition: Addr.hpp:294
gtirb::Addr::value_type
uint64_t value_type
The underlying type used to represent an Addr object.
Definition: Addr.hpp:40
gtirb::Addr::operator!=
constexpr friend bool operator!=(const Addr &LHS, const Addr &RHS) noexcept
Inquality operator for Addr.
Definition: Addr.hpp:176
gtirb::AddrRange
A range of addresses.
Definition: Addr.hpp:205
gtirb::Addr::operator+
constexpr friend Addr operator+(value_type Offset, const Addr &A) noexcept
Binary + operator for integral offset + Addr.
Definition: Addr.hpp:121
gtirb::Addr
A special class to store an Effective Address.
Definition: Addr.hpp:37
gtirb::Addr::operator--
constexpr Addr & operator--() noexcept
Predecrement for Addr.
Definition: Addr.hpp:90
gtirb::Addr::operator--
constexpr Addr operator--(int) noexcept
Postdecrement for Addr.
Definition: Addr.hpp:99
gtirb::Addr::operator-=
constexpr Addr & operator-=(value_type Offset) noexcept
Subtract-assign for Addr.
Definition: Addr.hpp:153
gtirb::AddrRange::upper
constexpr Addr upper() const noexcept
Exclusive upper bound of the address range.
Definition: Addr.hpp:240
gtirb::operator==
std::enable_if_t< std::is_error_code_enum< E >::value||std::is_error_condition_enum< E >::value, bool > operator==(const ErrorOr< T > &Err, E Code)
Definition: ErrorOr.hpp:277
gtirb::Addr::Addr
constexpr Addr() noexcept
Default constructor.
Definition: Addr.hpp:45
gtirb::Addr::difference_type
int64_t difference_type
The type used to represent a difference between two Addr objects.
Definition: Addr.hpp:42
gtirb::Offset
Describes a location inside a node (byte interval, block, etc).
Definition: Offset.hpp:32
gtirb::Addr::addressRange
std::optional< AddrRange > addressRange(const T &Object)
Address range of an object.
Definition: Addr.hpp:275
gtirb::Addr::operator>=
constexpr friend bool operator>=(const Addr &LHS, const Addr &RHS) noexcept
Greater-than-or-equal operator for Addr.
Definition: Addr.hpp:196
gtirb::SymAttribute::S
@ S
GTIRB_EXPORT_API
#define GTIRB_EXPORT_API
This macro controls the visibility of exported symbols (i.e. classes) in shared libraries....
Definition: Export.hpp:52
gtirb
Main namespace for the GTIRB API.
Definition: Addr.hpp:28
Export.hpp
gtirb::Addr::operator-
constexpr friend difference_type operator-(const Addr &A, const Addr &B) noexcept
Binary - operator for Addr - Addr.
Definition: Addr.hpp:165
gtirb::AddrRange::lower
constexpr Addr lower() const noexcept
Inclusive lower bound of the address range.
Definition: Addr.hpp:237
gtirb::Addr::operator++
constexpr Addr operator++(int) noexcept
Postincrement for Addr.
Definition: Addr.hpp:81
gtirb::asOptionalSize
std::optional< uint64_t > asOptionalSize(T X)
gtirb::Addr::operator+
constexpr Addr operator+() const noexcept
Unary plus for Addr. This is a noop because Addr objects represent an unsigned address value.
Definition: Addr.hpp:61
gtirb::Addr::Addr
constexpr Addr(value_type X) noexcept
Explicit conversion from value_type to Addr.
Definition: Addr.hpp:50
gtirb::Addr::operator+
constexpr friend Addr operator+(const Addr &A, value_type Offset) noexcept
Binary + operator for Addr + integral offset.
Definition: Addr.hpp:111
gtirb::Addr::operator-
constexpr friend Addr operator-(const Addr &A, value_type Offset) noexcept
Binary - operator for Addr - integral offset.
Definition: Addr.hpp:144
gtirb::Addr::operator>
constexpr friend bool operator>(const Addr &LHS, const Addr &RHS) noexcept
Greater-than operator for Addr.
Definition: Addr.hpp:186
gtirb::Addr::operator<
constexpr friend bool operator<(const Addr &LHS, const Addr &RHS) noexcept
Less-than operator for Addr.
Definition: Addr.hpp:181
gtirb::Addr::operator+=
constexpr Addr & operator+=(value_type Offset) noexcept
Add-assign for Addr.
Definition: Addr.hpp:130
std
Definition: Addr.hpp:359
gtirb::AddrRange::operator!=
constexpr bool operator!=(const AddrRange &RHS) const noexcept
Inequality operator for AddrRange.
Definition: Addr.hpp:232
gtirb::AddrRange::AddrRange
constexpr AddrRange(Addr Lower, uint64_t Count) noexcept
Construct a range starting at Lower and containing a total of Count addresses.
Definition: Addr.hpp:223
gtirb::AddrRange::AddrRange
constexpr AddrRange(Addr Lower, Addr Upper) noexcept
Construct a range starting at Lower, extending up to, but not including, Upper.
Definition: Addr.hpp:215
gtirb::operator<<
GTIRB_EXPORT_API std::ostream & operator<<(std::ostream &OS, const ConditionalEdge &CE)
gtirb::Addr::containsAddr
bool containsAddr(const T &Object, Addr Ea)
Check: Does the specified object contain the specified address?
Definition: Addr.hpp:314
gtirb::Addr::operator++
constexpr Addr & operator++() noexcept
Preincrement for Addr.
Definition: Addr.hpp:72
gtirb::Addr::operator==
constexpr friend bool operator==(const Addr &LHS, const Addr &RHS) noexcept
Equality operator for Addr.
Definition: Addr.hpp:171
std::hash< gtirb::Addr >::operator()
size_t operator()(const gtirb::Addr &A) const
Definition: Addr.hpp:363
gtirb::Addr::operator~
constexpr Addr operator~() const noexcept
Unary complement for Addr. Flips the value of all bits in the address.
Definition: Addr.hpp:67