Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 1 | //===- AddressRange.h -------------------------------------------*- 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 | #ifndef LLVM_DEBUGINFO_GSYM_RANGE_H |
| 11 | #define LLVM_DEBUGINFO_GSYM_RANGE_H |
| 12 | |
| 13 | #include "llvm/Support/Format.h" |
| 14 | #include "llvm/Support/raw_ostream.h" |
| 15 | #include <stdint.h> |
| 16 | #include <vector> |
| 17 | |
| 18 | #define HEX8(v) llvm::format_hex(v, 4) |
| 19 | #define HEX16(v) llvm::format_hex(v, 6) |
| 20 | #define HEX32(v) llvm::format_hex(v, 10) |
| 21 | #define HEX64(v) llvm::format_hex(v, 18) |
| 22 | |
| 23 | namespace llvm { |
| 24 | class raw_ostream; |
| 25 | |
| 26 | namespace gsym { |
| 27 | |
| 28 | /// A class that represents an address range. The range is specified using |
| 29 | /// a start and an end address. |
| 30 | struct AddressRange { |
| 31 | uint64_t Start; |
| 32 | uint64_t End; |
| 33 | AddressRange() : Start(0), End(0) {} |
| 34 | AddressRange(uint64_t S, uint64_t E) : Start(S), End(E) {} |
| 35 | uint64_t size() const { return End - Start; } |
| 36 | bool contains(uint64_t Addr) const { return Start <= Addr && Addr < End; } |
| 37 | bool intersects(const AddressRange &R) const { |
| 38 | return Start < R.End && R.Start < End; |
| 39 | } |
| 40 | |
| 41 | bool operator==(const AddressRange &R) const { |
| 42 | return Start == R.Start && End == R.End; |
| 43 | } |
| 44 | bool operator!=(const AddressRange &R) const { |
| 45 | return !(*this == R); |
| 46 | } |
| 47 | bool operator<(const AddressRange &R) const { |
| 48 | return std::make_pair(Start, End) < std::make_pair(R.Start, R.End); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | raw_ostream &operator<<(raw_ostream &OS, const AddressRange &R); |
| 53 | |
| 54 | /// The AddressRanges class helps normalize address range collections. |
| 55 | /// This class keeps a sorted vector of AddressRange objects and can perform |
| 56 | /// insertions and searches efficiently. The address ranges are always sorted |
| 57 | /// and never contain any invalid or empty address ranges. This allows us to |
| 58 | /// emit address ranges into the GSYM file efficiently. Intersecting address |
| 59 | /// ranges are combined during insertion so that we can emit the most compact |
| 60 | /// representation for address ranges when writing to disk. |
| 61 | class AddressRanges { |
| 62 | protected: |
| 63 | using Collection = std::vector<AddressRange>; |
| 64 | Collection Ranges; |
| 65 | public: |
| 66 | void clear() { Ranges.clear(); } |
| 67 | bool empty() const { return Ranges.empty(); } |
| 68 | bool contains(uint64_t Addr) const; |
| 69 | void insert(AddressRange Range); |
| 70 | size_t size() const { return Ranges.size(); } |
| 71 | bool operator==(const AddressRanges &RHS) const { |
| 72 | return Ranges == RHS.Ranges; |
| 73 | } |
| 74 | const AddressRange &operator[](size_t i) const { |
| 75 | assert(i < Ranges.size()); |
| 76 | return Ranges[i]; |
| 77 | } |
| 78 | Collection::const_iterator begin() const { return Ranges.begin(); } |
| 79 | Collection::const_iterator end() const { return Ranges.end(); } |
| 80 | }; |
| 81 | |
| 82 | raw_ostream &operator<<(raw_ostream &OS, const AddressRanges &AR); |
| 83 | |
| 84 | } // namespace gsym |
| 85 | } // namespace llvm |
| 86 | |
| 87 | #endif // #ifndef LLVM_DEBUGINFO_GSYM_RANGE_H |