DWARFAddressRange.h 3.2 KB

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