DwarfStringPoolEntry.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/CodeGen/DwarfStringPoolEntry.h - String pool entry --*- 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_CODEGEN_DWARFSTRINGPOOLENTRY_H
  14. #define LLVM_CODEGEN_DWARFSTRINGPOOLENTRY_H
  15. #include "llvm/ADT/PointerUnion.h"
  16. #include "llvm/ADT/StringMap.h"
  17. namespace llvm {
  18. class MCSymbol;
  19. /// Data for a string pool entry.
  20. struct DwarfStringPoolEntry {
  21. static constexpr unsigned NotIndexed = -1;
  22. MCSymbol *Symbol = nullptr;
  23. uint64_t Offset = 0;
  24. unsigned Index = 0;
  25. bool isIndexed() const { return Index != NotIndexed; }
  26. };
  27. /// DwarfStringPoolEntryRef: Dwarf string pool entry reference.
  28. ///
  29. /// Dwarf string pool entry keeps string value and its data.
  30. /// There are two variants how data are represented:
  31. ///
  32. /// 1. By value - StringMapEntry<DwarfStringPoolEntry>.
  33. /// 2. By pointer - StringMapEntry<DwarfStringPoolEntry *>.
  34. ///
  35. /// The "By pointer" variant allows for reducing memory usage for the case
  36. /// when string pool entry does not have data: it keeps the null pointer
  37. /// and so no need to waste space for the full DwarfStringPoolEntry.
  38. /// It is recommended to use "By pointer" variant if not all entries
  39. /// of dwarf string pool have corresponding DwarfStringPoolEntry.
  40. class DwarfStringPoolEntryRef {
  41. /// Pointer type for "By value" string entry.
  42. using ByValStringEntryPtr = const StringMapEntry<DwarfStringPoolEntry> *;
  43. /// Pointer type for "By pointer" string entry.
  44. using ByPtrStringEntryPtr = const StringMapEntry<DwarfStringPoolEntry *> *;
  45. /// Pointer to the dwarf string pool Entry.
  46. PointerUnion<ByValStringEntryPtr, ByPtrStringEntryPtr> MapEntry = nullptr;
  47. public:
  48. DwarfStringPoolEntryRef() = default;
  49. /// ASSUMPTION: DwarfStringPoolEntryRef keeps pointer to \p Entry,
  50. /// thus specified entry mustn`t be reallocated.
  51. DwarfStringPoolEntryRef(const StringMapEntry<DwarfStringPoolEntry> &Entry)
  52. : MapEntry(&Entry) {}
  53. /// ASSUMPTION: DwarfStringPoolEntryRef keeps pointer to \p Entry,
  54. /// thus specified entry mustn`t be reallocated.
  55. DwarfStringPoolEntryRef(const StringMapEntry<DwarfStringPoolEntry *> &Entry)
  56. : MapEntry(&Entry) {
  57. assert(MapEntry.get<ByPtrStringEntryPtr>()->second != nullptr);
  58. }
  59. explicit operator bool() const { return !MapEntry.isNull(); }
  60. /// \returns symbol for the dwarf string.
  61. MCSymbol *getSymbol() const {
  62. assert(getEntry().Symbol && "No symbol available!");
  63. return getEntry().Symbol;
  64. }
  65. /// \returns offset for the dwarf string.
  66. uint64_t getOffset() const { return getEntry().Offset; }
  67. /// \returns index for the dwarf string.
  68. unsigned getIndex() const {
  69. assert(getEntry().isIndexed() && "Index is not set!");
  70. return getEntry().Index;
  71. }
  72. /// \returns string.
  73. StringRef getString() const {
  74. if (MapEntry.is<ByValStringEntryPtr>())
  75. return MapEntry.get<ByValStringEntryPtr>()->first();
  76. return MapEntry.get<ByPtrStringEntryPtr>()->first();
  77. }
  78. /// \returns the entire string pool entry for convenience.
  79. const DwarfStringPoolEntry &getEntry() const {
  80. if (MapEntry.is<ByValStringEntryPtr>())
  81. return MapEntry.get<ByValStringEntryPtr>()->second;
  82. return *MapEntry.get<ByPtrStringEntryPtr>()->second;
  83. }
  84. bool operator==(const DwarfStringPoolEntryRef &X) const {
  85. return MapEntry.getOpaqueValue() == X.MapEntry.getOpaqueValue();
  86. }
  87. bool operator!=(const DwarfStringPoolEntryRef &X) const {
  88. return MapEntry.getOpaqueValue() != X.MapEntry.getOpaqueValue();
  89. }
  90. };
  91. } // end namespace llvm
  92. #endif
  93. #ifdef __GNUC__
  94. #pragma GCC diagnostic pop
  95. #endif