DwarfFile.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //===- llvm/CodeGen/DwarfFile.h - Dwarf Debug Framework ---------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
  9. #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
  10. #include "DwarfStringPool.h"
  11. #include "llvm/ADT/DenseMap.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/CodeGen/DIE.h"
  15. #include "llvm/Support/Allocator.h"
  16. #include <map>
  17. #include <memory>
  18. #include <utility>
  19. namespace llvm {
  20. class AsmPrinter;
  21. class DbgEntity;
  22. class DbgVariable;
  23. class DbgLabel;
  24. class DINode;
  25. class DwarfCompileUnit;
  26. class DwarfUnit;
  27. class LexicalScope;
  28. class MCSection;
  29. class MDNode;
  30. // Data structure to hold a range for range lists.
  31. struct RangeSpan {
  32. const MCSymbol *Begin;
  33. const MCSymbol *End;
  34. };
  35. struct RangeSpanList {
  36. // Index for locating within the debug_range section this particular span.
  37. MCSymbol *Label;
  38. const DwarfCompileUnit *CU;
  39. // List of ranges.
  40. SmallVector<RangeSpan, 2> Ranges;
  41. };
  42. class DwarfFile {
  43. // Target of Dwarf emission, used for sizing of abbreviations.
  44. AsmPrinter *Asm;
  45. BumpPtrAllocator AbbrevAllocator;
  46. // Used to uniquely define abbreviations.
  47. DIEAbbrevSet Abbrevs;
  48. // A pointer to all units in the section.
  49. SmallVector<std::unique_ptr<DwarfCompileUnit>, 1> CUs;
  50. DwarfStringPool StrPool;
  51. // List of range lists for a given compile unit, separate from the ranges for
  52. // the CU itself.
  53. SmallVector<RangeSpanList, 1> CURangeLists;
  54. /// DWARF v5: The symbol that designates the start of the contribution to
  55. /// the string offsets table. The contribution is shared by all units.
  56. MCSymbol *StringOffsetsStartSym = nullptr;
  57. /// DWARF v5: The symbol that designates the base of the range list table.
  58. /// The table is shared by all units.
  59. MCSymbol *RnglistsTableBaseSym = nullptr;
  60. /// The variables of a lexical scope.
  61. struct ScopeVars {
  62. /// We need to sort Args by ArgNo and check for duplicates. This could also
  63. /// be implemented as a list or vector + std::lower_bound().
  64. std::map<unsigned, DbgVariable *> Args;
  65. SmallVector<DbgVariable *, 8> Locals;
  66. };
  67. /// Collection of DbgVariables of each lexical scope.
  68. DenseMap<LexicalScope *, ScopeVars> ScopeVariables;
  69. /// Collection of DbgLabels of each lexical scope.
  70. using LabelList = SmallVector<DbgLabel *, 4>;
  71. DenseMap<LexicalScope *, LabelList> ScopeLabels;
  72. // Collection of abstract subprogram DIEs.
  73. DenseMap<const MDNode *, DIE *> AbstractSPDies;
  74. DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities;
  75. /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can
  76. /// be shared across CUs, that is why we keep the map here instead
  77. /// of in DwarfCompileUnit.
  78. DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap;
  79. public:
  80. DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA);
  81. const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
  82. return CUs;
  83. }
  84. std::pair<uint32_t, RangeSpanList *> addRange(const DwarfCompileUnit &CU,
  85. SmallVector<RangeSpan, 2> R);
  86. /// getRangeLists - Get the vector of range lists.
  87. const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
  88. return CURangeLists;
  89. }
  90. /// Compute the size and offset of a DIE given an incoming Offset.
  91. unsigned computeSizeAndOffset(DIE &Die, unsigned Offset);
  92. /// Compute the size and offset of all the DIEs.
  93. void computeSizeAndOffsets();
  94. /// Compute the size and offset of all the DIEs in the given unit.
  95. /// \returns The size of the root DIE.
  96. unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU);
  97. /// Add a unit to the list of CUs.
  98. void addUnit(std::unique_ptr<DwarfCompileUnit> U);
  99. /// Emit all of the units to the section listed with the given
  100. /// abbreviation section.
  101. void emitUnits(bool UseOffsets);
  102. /// Emit the given unit to its section.
  103. void emitUnit(DwarfUnit *TheU, bool UseOffsets);
  104. /// Emit a set of abbreviations to the specific section.
  105. void emitAbbrevs(MCSection *);
  106. /// Emit all of the strings to the section given. If OffsetSection is
  107. /// non-null, emit a table of string offsets to it. If UseRelativeOffsets
  108. /// is false, emit absolute offsets to the strings. Otherwise, emit
  109. /// relocatable references to the strings if they are supported by the target.
  110. void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr,
  111. bool UseRelativeOffsets = false);
  112. /// Returns the string pool.
  113. DwarfStringPool &getStringPool() { return StrPool; }
  114. MCSymbol *getStringOffsetsStartSym() const { return StringOffsetsStartSym; }
  115. void setStringOffsetsStartSym(MCSymbol *Sym) { StringOffsetsStartSym = Sym; }
  116. MCSymbol *getRnglistsTableBaseSym() const { return RnglistsTableBaseSym; }
  117. void setRnglistsTableBaseSym(MCSymbol *Sym) { RnglistsTableBaseSym = Sym; }
  118. /// \returns false if the variable was merged with a previous one.
  119. bool addScopeVariable(LexicalScope *LS, DbgVariable *Var);
  120. void addScopeLabel(LexicalScope *LS, DbgLabel *Label);
  121. DenseMap<LexicalScope *, ScopeVars> &getScopeVariables() {
  122. return ScopeVariables;
  123. }
  124. DenseMap<LexicalScope *, LabelList> &getScopeLabels() {
  125. return ScopeLabels;
  126. }
  127. DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
  128. return AbstractSPDies;
  129. }
  130. DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() {
  131. return AbstractEntities;
  132. }
  133. void insertDIE(const MDNode *TypeMD, DIE *Die) {
  134. DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die));
  135. }
  136. DIE *getDIE(const MDNode *TypeMD) {
  137. return DITypeNodeToDieMap.lookup(TypeMD);
  138. }
  139. };
  140. } // end namespace llvm
  141. #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H