Range.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Range.h --------------------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_GSYM_RANGE_H
  14. #define LLVM_DEBUGINFO_GSYM_RANGE_H
  15. #include "llvm/ADT/Optional.h"
  16. #include "llvm/Support/Format.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. #include <stdint.h>
  19. #include <vector>
  20. #define HEX8(v) llvm::format_hex(v, 4)
  21. #define HEX16(v) llvm::format_hex(v, 6)
  22. #define HEX32(v) llvm::format_hex(v, 10)
  23. #define HEX64(v) llvm::format_hex(v, 18)
  24. namespace llvm {
  25. class DataExtractor;
  26. class raw_ostream;
  27. namespace gsym {
  28. class FileWriter;
  29. /// A class that represents an address range. The range is specified using
  30. /// a start and an end address.
  31. struct AddressRange {
  32. uint64_t Start;
  33. uint64_t End;
  34. AddressRange() : Start(0), End(0) {}
  35. AddressRange(uint64_t S, uint64_t E) : Start(S), End(E) {}
  36. uint64_t size() const { return End - Start; }
  37. bool contains(uint64_t Addr) const { return Start <= Addr && Addr < End; }
  38. bool intersects(const AddressRange &R) const {
  39. return Start < R.End && R.Start < End;
  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. /// AddressRange objects are encoded and decoded to be relative to a base
  51. /// address. This will be the FunctionInfo's start address if the AddressRange
  52. /// is directly contained in a FunctionInfo, or a base address of the
  53. /// containing parent AddressRange or AddressRanges. This allows address
  54. /// ranges to be efficiently encoded using ULEB128 encodings as we encode the
  55. /// offset and size of each range instead of full addresses. This also makes
  56. /// encoded addresses easy to relocate as we just need to relocate one base
  57. /// address.
  58. /// @{
  59. void decode(DataExtractor &Data, uint64_t BaseAddr, uint64_t &Offset);
  60. void encode(FileWriter &O, uint64_t BaseAddr) const;
  61. /// @}
  62. /// Skip an address range object in the specified data a the specified
  63. /// offset.
  64. ///
  65. /// \param Data The binary stream to read the data from.
  66. ///
  67. /// \param Offset The byte offset within \a Data.
  68. static void skip(DataExtractor &Data, uint64_t &Offset);
  69. };
  70. raw_ostream &operator<<(raw_ostream &OS, const AddressRange &R);
  71. /// The AddressRanges class helps normalize address range collections.
  72. /// This class keeps a sorted vector of AddressRange objects and can perform
  73. /// insertions and searches efficiently. The address ranges are always sorted
  74. /// and never contain any invalid or empty address ranges. This allows us to
  75. /// emit address ranges into the GSYM file efficiently. Intersecting address
  76. /// ranges are combined during insertion so that we can emit the most compact
  77. /// representation for address ranges when writing to disk.
  78. class AddressRanges {
  79. protected:
  80. using Collection = std::vector<AddressRange>;
  81. Collection Ranges;
  82. public:
  83. void clear() { Ranges.clear(); }
  84. bool empty() const { return Ranges.empty(); }
  85. bool contains(uint64_t Addr) const;
  86. bool contains(AddressRange Range) const;
  87. Optional<AddressRange> getRangeThatContains(uint64_t Addr) const;
  88. void insert(AddressRange Range);
  89. size_t size() const { return Ranges.size(); }
  90. bool operator==(const AddressRanges &RHS) const {
  91. return Ranges == RHS.Ranges;
  92. }
  93. const AddressRange &operator[](size_t i) const {
  94. assert(i < Ranges.size());
  95. return Ranges[i];
  96. }
  97. Collection::const_iterator begin() const { return Ranges.begin(); }
  98. Collection::const_iterator end() const { return Ranges.end(); }
  99. /// Address ranges are decoded and encoded to be relative to a base address.
  100. /// See the AddressRange comment for the encode and decode methods for full
  101. /// details.
  102. /// @{
  103. void decode(DataExtractor &Data, uint64_t BaseAddr, uint64_t &Offset);
  104. void encode(FileWriter &O, uint64_t BaseAddr) const;
  105. /// @}
  106. /// Skip an address range object in the specified data a the specified
  107. /// offset.
  108. ///
  109. /// \param Data The binary stream to read the data from.
  110. ///
  111. /// \param Offset The byte offset within \a Data.
  112. ///
  113. /// \returns The number of address ranges that were skipped.
  114. static uint64_t skip(DataExtractor &Data, uint64_t &Offset);
  115. };
  116. raw_ostream &operator<<(raw_ostream &OS, const AddressRanges &AR);
  117. } // namespace gsym
  118. } // namespace llvm
  119. #endif // #ifndef LLVM_DEBUGINFO_GSYM_RANGE_H
  120. #ifdef __GNUC__
  121. #pragma GCC diagnostic pop
  122. #endif