blob: 772ff244c5b71e42daf626778ea64dc741d777af [file] [log] [blame]
Andrew Walbran3d2c1972020-04-07 12:24:26 +01001//===- 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
23namespace llvm {
24class raw_ostream;
25
26namespace gsym {
27
28/// A class that represents an address range. The range is specified using
29/// a start and an end address.
30struct 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
52raw_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.
61class AddressRanges {
62protected:
63 using Collection = std::vector<AddressRange>;
64 Collection Ranges;
65public:
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
82raw_ostream &operator<<(raw_ostream &OS, const AddressRanges &AR);
83
84} // namespace gsym
85} // namespace llvm
86
87#endif // #ifndef LLVM_DEBUGINFO_GSYM_RANGE_H