DWARFAddressRange.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DWARFAddressRange.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_DWARF_DWARFADDRESSRANGE_H
  14. #define LLVM_DEBUGINFO_DWARF_DWARFADDRESSRANGE_H
  15. #include "llvm/DebugInfo/DIContext.h"
  16. #include <cstdint>
  17. #include <tuple>
  18. #include <vector>
  19. namespace llvm {
  20. class raw_ostream;
  21. class DWARFObject;
  22. struct DWARFAddressRange {
  23. uint64_t LowPC;
  24. uint64_t HighPC;
  25. uint64_t SectionIndex;
  26. DWARFAddressRange() = default;
  27. /// Used for unit testing.
  28. DWARFAddressRange(
  29. uint64_t LowPC, uint64_t HighPC,
  30. uint64_t SectionIndex = object::SectionedAddress::UndefSection)
  31. : LowPC(LowPC), HighPC(HighPC), SectionIndex(SectionIndex) {}
  32. /// Returns true if LowPC is smaller or equal to HighPC. This accounts for
  33. /// dead-stripped ranges.
  34. bool valid() const { return LowPC <= HighPC; }
  35. /// Returns true if [LowPC, HighPC) intersects with [RHS.LowPC, RHS.HighPC).
  36. bool intersects(const DWARFAddressRange &RHS) const {
  37. assert(valid() && RHS.valid());
  38. if (SectionIndex != RHS.SectionIndex)
  39. return false;
  40. // Empty ranges can't intersect.
  41. if (LowPC == HighPC || RHS.LowPC == RHS.HighPC)
  42. return false;
  43. return LowPC < RHS.HighPC && RHS.LowPC < HighPC;
  44. }
  45. /// Union two address ranges if they intersect.
  46. ///
  47. /// This function will union two address ranges if they intersect by
  48. /// modifying this range to be the union of both ranges. If the two ranges
  49. /// don't intersect this range will be left alone.
  50. ///
  51. /// \param RHS Another address range to combine with.
  52. ///
  53. /// \returns false if the ranges don't intersect, true if they do and the
  54. /// ranges were combined.
  55. bool merge(const DWARFAddressRange &RHS) {
  56. if (!intersects(RHS))
  57. return false;
  58. LowPC = std::min<uint64_t>(LowPC, RHS.LowPC);
  59. HighPC = std::max<uint64_t>(HighPC, RHS.HighPC);
  60. return true;
  61. }
  62. void dump(raw_ostream &OS, uint32_t AddressSize, DIDumpOptions DumpOpts = {},
  63. const DWARFObject *Obj = nullptr) const;
  64. };
  65. inline bool operator<(const DWARFAddressRange &LHS,
  66. const DWARFAddressRange &RHS) {
  67. return std::tie(LHS.SectionIndex, LHS.LowPC, LHS.HighPC) < std::tie(RHS.SectionIndex, RHS.LowPC, RHS.HighPC);
  68. }
  69. inline bool operator==(const DWARFAddressRange &LHS,
  70. const DWARFAddressRange &RHS) {
  71. return std::tie(LHS.SectionIndex, LHS.LowPC, LHS.HighPC) == std::tie(RHS.SectionIndex, RHS.LowPC, RHS.HighPC);
  72. }
  73. raw_ostream &operator<<(raw_ostream &OS, const DWARFAddressRange &R);
  74. /// DWARFAddressRangesVector - represents a set of absolute address ranges.
  75. using DWARFAddressRangesVector = std::vector<DWARFAddressRange>;
  76. } // end namespace llvm
  77. #endif // LLVM_DEBUGINFO_DWARF_DWARFADDRESSRANGE_H
  78. #ifdef __GNUC__
  79. #pragma GCC diagnostic pop
  80. #endif