DwarfCompileUnit.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //===-- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit ---*- C++ -*--===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains support for writing dwarf compile unit.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
  14. #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
  15. #include "DwarfUnit.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/IR/DebugInfo.h"
  18. #include "llvm/Support/Dwarf.h"
  19. namespace llvm {
  20. class AsmPrinter;
  21. class DIE;
  22. class DwarfDebug;
  23. class DwarfFile;
  24. class MCSymbol;
  25. class LexicalScope;
  26. class DwarfCompileUnit : public DwarfUnit {
  27. /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
  28. /// the need to search for it in applyStmtList.
  29. DIE::value_iterator StmtListValue;
  30. /// Skeleton unit associated with this unit.
  31. DwarfCompileUnit *Skeleton;
  32. /// The start of the unit within its section.
  33. MCSymbol *LabelBegin;
  34. /// GlobalNames - A map of globally visible named entities for this unit.
  35. StringMap<const DIE *> GlobalNames;
  36. /// GlobalTypes - A map of globally visible types for this unit.
  37. StringMap<const DIE *> GlobalTypes;
  38. // List of range lists for a given compile unit, separate from the ranges for
  39. // the CU itself.
  40. SmallVector<RangeSpanList, 1> CURangeLists;
  41. // List of ranges for a given compile unit.
  42. SmallVector<RangeSpan, 2> CURanges;
  43. // The base address of this unit, if any. Used for relative references in
  44. // ranges/locs.
  45. const MCSymbol *BaseAddress;
  46. /// \brief Construct a DIE for the given DbgVariable without initializing the
  47. /// DbgVariable's DIE reference.
  48. DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
  49. bool isDwoUnit() const override;
  50. bool includeMinimalInlineScopes() const;
  51. public:
  52. DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
  53. DwarfDebug *DW, DwarfFile *DWU);
  54. DwarfCompileUnit *getSkeleton() const {
  55. return Skeleton;
  56. }
  57. void initStmtList();
  58. /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
  59. void applyStmtList(DIE &D);
  60. /// getOrCreateGlobalVariableDIE - get or create global variable DIE.
  61. DIE *getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV);
  62. /// addLabelAddress - Add a dwarf label attribute data and value using
  63. /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
  64. void addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
  65. const MCSymbol *Label);
  66. /// addLocalLabelAddress - Add a dwarf label attribute data and value using
  67. /// DW_FORM_addr only.
  68. void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute,
  69. const MCSymbol *Label);
  70. /// addSectionDelta - Add a label delta attribute data and value.
  71. DIE::value_iterator addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
  72. const MCSymbol *Hi, const MCSymbol *Lo);
  73. DwarfCompileUnit &getCU() override { return *this; }
  74. unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override;
  75. /// addRange - Add an address range to the list of ranges for this unit.
  76. void addRange(RangeSpan Range);
  77. void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
  78. /// addSectionLabel - Add a Dwarf section label attribute data and value.
  79. ///
  80. DIE::value_iterator addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
  81. const MCSymbol *Label,
  82. const MCSymbol *Sec);
  83. /// \brief Find DIE for the given subprogram and attach appropriate
  84. /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
  85. /// variables in this scope then create and insert DIEs for these
  86. /// variables.
  87. DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
  88. void constructScopeDIE(LexicalScope *Scope,
  89. SmallVectorImpl<DIE *> &FinalChildren);
  90. /// \brief A helper function to construct a RangeSpanList for a given
  91. /// lexical scope.
  92. void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
  93. void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
  94. void attachRangesOrLowHighPC(DIE &D,
  95. const SmallVectorImpl<InsnRange> &Ranges);
  96. /// \brief This scope represents inlined body of a function. Construct
  97. /// DIE to represent this concrete inlined copy of the function.
  98. DIE *constructInlinedScopeDIE(LexicalScope *Scope);
  99. /// \brief Construct new DW_TAG_lexical_block for this scope and
  100. /// attach DW_AT_low_pc/DW_AT_high_pc labels.
  101. DIE *constructLexicalScopeDIE(LexicalScope *Scope);
  102. /// constructVariableDIE - Construct a DIE for the given DbgVariable.
  103. DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
  104. DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
  105. DIE *&ObjectPointer);
  106. /// A helper function to create children of a Scope DIE.
  107. DIE *createScopeChildrenDIE(LexicalScope *Scope,
  108. SmallVectorImpl<DIE *> &Children,
  109. unsigned *ChildScopeCount = nullptr);
  110. /// \brief Construct a DIE for this subprogram scope.
  111. void constructSubprogramScopeDIE(LexicalScope *Scope);
  112. DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
  113. void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
  114. /// \brief Construct import_module DIE.
  115. DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
  116. void finishSubprogramDefinition(const DISubprogram *SP);
  117. void collectDeadVariables(const DISubprogram *SP);
  118. /// Set the skeleton unit associated with this unit.
  119. void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
  120. const MCSymbol *getSectionSym() const {
  121. assert(Section);
  122. return Section->getBeginSymbol();
  123. }
  124. unsigned getLength() {
  125. return sizeof(uint32_t) + // Length field
  126. getHeaderSize() + UnitDie.getSize();
  127. }
  128. void emitHeader(bool UseOffsets) override;
  129. MCSymbol *getLabelBegin() const {
  130. assert(Section);
  131. return LabelBegin;
  132. }
  133. /// Add a new global name to the compile unit.
  134. void addGlobalName(StringRef Name, DIE &Die, const DIScope *Context) override;
  135. /// Add a new global type to the compile unit.
  136. void addGlobalType(const DIType *Ty, const DIE &Die,
  137. const DIScope *Context) override;
  138. const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
  139. const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
  140. /// Add DW_AT_location attribute for a DbgVariable based on provided
  141. /// MachineLocation.
  142. void addVariableAddress(const DbgVariable &DV, DIE &Die,
  143. MachineLocation Location);
  144. /// Add an address attribute to a die based on the location provided.
  145. void addAddress(DIE &Die, dwarf::Attribute Attribute,
  146. const MachineLocation &Location);
  147. /// Start with the address based on the location provided, and generate the
  148. /// DWARF information necessary to find the actual variable (navigating the
  149. /// extra location information encoded in the type) based on the starting
  150. /// location. Add the DWARF information to the die.
  151. void addComplexAddress(const DbgVariable &DV, DIE &Die,
  152. dwarf::Attribute Attribute,
  153. const MachineLocation &Location);
  154. /// Add a Dwarf loclistptr attribute data and value.
  155. void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
  156. void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie);
  157. /// Add a Dwarf expression attribute data and value.
  158. void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
  159. void applySubprogramAttributesToDefinition(const DISubprogram *SP,
  160. DIE &SPDie);
  161. /// getRangeLists - Get the vector of range lists.
  162. const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
  163. return (Skeleton ? Skeleton : this)->CURangeLists;
  164. }
  165. /// getRanges - Get the list of ranges for this unit.
  166. const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
  167. SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
  168. void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
  169. const MCSymbol *getBaseAddress() const { return BaseAddress; }
  170. };
  171. } // end llvm namespace
  172. #endif