LLVM  14.0.0git
DwarfFile.h
Go to the documentation of this file.
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 
9 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
10 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
11 
12 #include "DwarfStringPool.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/CodeGen/DIE.h"
17 #include "llvm/Support/Allocator.h"
18 #include <map>
19 #include <memory>
20 #include <utility>
21 
22 namespace llvm {
23 
24 class AsmPrinter;
25 class DbgEntity;
26 class DbgVariable;
27 class DbgLabel;
28 class DINode;
29 class DwarfCompileUnit;
30 class DwarfUnit;
31 class LexicalScope;
32 class MCSection;
33 class MDNode;
34 
35 // Data structure to hold a range for range lists.
36 struct RangeSpan {
37  const MCSymbol *Begin;
38  const MCSymbol *End;
39 };
40 
41 struct RangeSpanList {
42  // Index for locating within the debug_range section this particular span.
45  // List of ranges.
47 };
48 
49 class DwarfFile {
50  // Target of Dwarf emission, used for sizing of abbreviations.
51  AsmPrinter *Asm;
52 
53  BumpPtrAllocator AbbrevAllocator;
54 
55  // Used to uniquely define abbreviations.
56  DIEAbbrevSet Abbrevs;
57 
58  // A pointer to all units in the section.
60 
61  DwarfStringPool StrPool;
62 
63  // List of range lists for a given compile unit, separate from the ranges for
64  // the CU itself.
65  SmallVector<RangeSpanList, 1> CURangeLists;
66 
67  /// DWARF v5: The symbol that designates the start of the contribution to
68  /// the string offsets table. The contribution is shared by all units.
69  MCSymbol *StringOffsetsStartSym = nullptr;
70 
71  /// DWARF v5: The symbol that designates the base of the range list table.
72  /// The table is shared by all units.
73  MCSymbol *RnglistsTableBaseSym = nullptr;
74 
75  /// The variables of a lexical scope.
76  struct ScopeVars {
77  /// We need to sort Args by ArgNo and check for duplicates. This could also
78  /// be implemented as a list or vector + std::lower_bound().
79  std::map<unsigned, DbgVariable *> Args;
81  };
82  /// Collection of DbgVariables of each lexical scope.
84 
85  /// Collection of DbgLabels of each lexical scope.
88 
89  // Collection of abstract subprogram DIEs.
90  DenseMap<const MDNode *, DIE *> AbstractSPDies;
92 
93  /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can
94  /// be shared across CUs, that is why we keep the map here instead
95  /// of in DwarfCompileUnit.
96  DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap;
97 
98 public:
100 
102  return CUs;
103  }
104 
105  std::pair<uint32_t, RangeSpanList *> addRange(const DwarfCompileUnit &CU,
107 
108  /// getRangeLists - Get the vector of range lists.
110  return CURangeLists;
111  }
112 
113  /// Compute the size and offset of a DIE given an incoming Offset.
114  unsigned computeSizeAndOffset(DIE &Die, unsigned Offset);
115 
116  /// Compute the size and offset of all the DIEs.
117  void computeSizeAndOffsets();
118 
119  /// Compute the size and offset of all the DIEs in the given unit.
120  /// \returns The size of the root DIE.
121  unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU);
122 
123  /// Add a unit to the list of CUs.
124  void addUnit(std::unique_ptr<DwarfCompileUnit> U);
125 
126  /// Emit all of the units to the section listed with the given
127  /// abbreviation section.
128  void emitUnits(bool UseOffsets);
129 
130  /// Emit the given unit to its section.
131  void emitUnit(DwarfUnit *TheU, bool UseOffsets);
132 
133  /// Emit a set of abbreviations to the specific section.
134  void emitAbbrevs(MCSection *);
135 
136  /// Emit all of the strings to the section given. If OffsetSection is
137  /// non-null, emit a table of string offsets to it. If UseRelativeOffsets
138  /// is false, emit absolute offsets to the strings. Otherwise, emit
139  /// relocatable references to the strings if they are supported by the target.
140  void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr,
141  bool UseRelativeOffsets = false);
142 
143  /// Returns the string pool.
144  DwarfStringPool &getStringPool() { return StrPool; }
145 
146  MCSymbol *getStringOffsetsStartSym() const { return StringOffsetsStartSym; }
147  void setStringOffsetsStartSym(MCSymbol *Sym) { StringOffsetsStartSym = Sym; }
148 
149  MCSymbol *getRnglistsTableBaseSym() const { return RnglistsTableBaseSym; }
150  void setRnglistsTableBaseSym(MCSymbol *Sym) { RnglistsTableBaseSym = Sym; }
151 
152  /// \returns false if the variable was merged with a previous one.
154 
155  void addScopeLabel(LexicalScope *LS, DbgLabel *Label);
156 
158  return ScopeVariables;
159  }
160 
162  return ScopeLabels;
163  }
164 
166  return AbstractSPDies;
167  }
168 
170  return AbstractEntities;
171  }
172 
173  void insertDIE(const MDNode *TypeMD, DIE *Die) {
174  DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die));
175  }
176 
177  DIE *getDIE(const MDNode *TypeMD) {
178  return DITypeNodeToDieMap.lookup(TypeMD);
179  }
180 };
181 
182 } // end namespace llvm
183 
184 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::RangeSpanList::Ranges
SmallVector< RangeSpan, 2 > Ranges
Definition: DwarfFile.h:46
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::lookup
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:197
StringRef.h
llvm::DwarfFile::DwarfFile
DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
Definition: DwarfFile.cpp:22
llvm::DwarfCompileUnit
Definition: DwarfCompileUnit.h:47
llvm::DwarfFile::getAbstractSPDies
DenseMap< const MDNode *, DIE * > & getAbstractSPDies()
Definition: DwarfFile.h:165
llvm::DwarfFile::getDIE
DIE * getDIE(const MDNode *TypeMD)
Definition: DwarfFile.h:177
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::LexicalScope
LexicalScope - This class is used to track scope information.
Definition: LexicalScopes.h:44
Allocator.h
llvm::RangeSpan
Definition: DwarfFile.h:36
llvm::DwarfFile::addScopeVariable
bool addScopeVariable(LexicalScope *LS, DbgVariable *Var)
Definition: DwarfFile.cpp:106
DenseMap.h
llvm::DwarfFile::getUnits
const SmallVectorImpl< std::unique_ptr< DwarfCompileUnit > > & getUnits()
Definition: DwarfFile.h:101
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::DIE
A structured debug information entry.
Definition: DIE.h:739
llvm::DwarfFile::insertDIE
void insertDIE(const MDNode *TypeMD, DIE *Die)
Definition: DwarfFile.h:173
llvm::DbgLabel
This class is used to track label information.
Definition: DwarfDebug.h:233
llvm::DwarfStringPool
Definition: DwarfStringPool.h:26
llvm::DwarfFile::setRnglistsTableBaseSym
void setRnglistsTableBaseSym(MCSymbol *Sym)
Definition: DwarfFile.h:150
llvm::DwarfFile::computeSizeAndOffset
unsigned computeSizeAndOffset(DIE &Die, unsigned Offset)
Compute the size and offset of a DIE given an incoming Offset.
Definition: DwarfFile.cpp:94
llvm::RangeSpanList::Label
MCSymbol * Label
Definition: DwarfFile.h:43
llvm::DwarfFile::emitUnit
void emitUnit(DwarfUnit *TheU, bool UseOffsets)
Emit the given unit to its section.
Definition: DwarfFile.cpp:36
llvm::M68kBeads::DA
@ DA
Definition: M68kBaseInfo.h:59
llvm::DwarfFile::addRange
std::pair< uint32_t, RangeSpanList * > addRange(const DwarfCompileUnit &CU, SmallVector< RangeSpan, 2 > R)
Definition: DwarfFile.cpp:129
llvm::DwarfFile::emitStrings
void emitStrings(MCSection *StrSection, MCSection *OffsetSection=nullptr, bool UseRelativeOffsets=false)
Emit all of the strings to the section given.
Definition: DwarfFile.cpp:101
llvm::DwarfFile::getScopeLabels
DenseMap< LexicalScope *, LabelList > & getScopeLabels()
Definition: DwarfFile.h:161
llvm::DbgVariable
This class is used to track local variable information.
Definition: DwarfDebug.h:116
llvm::DwarfFile::setStringOffsetsStartSym
void setStringOffsetsStartSym(MCSymbol *Sym)
Definition: DwarfFile.h:147
DIE.h
llvm::BumpPtrAllocatorImpl
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
llvm::DenseMap
Definition: DenseMap.h:714
llvm::MDNode
Metadata node.
Definition: Metadata.h:901
llvm::DwarfFile::computeSizeAndOffsetsForUnit
unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU)
Compute the size and offset of all the DIEs in the given unit.
Definition: DwarfFile.cpp:82
llvm::DwarfFile::addScopeLabel
void addScopeLabel(LexicalScope *LS, DbgLabel *Label)
Definition: DwarfFile.cpp:123
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::DwarfFile::getAbstractEntities
DenseMap< const DINode *, std::unique_ptr< DbgEntity > > & getAbstractEntities()
Definition: DwarfFile.h:169
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:207
llvm::DwarfFile::addUnit
void addUnit(std::unique_ptr< DwarfCompileUnit > U)
Add a unit to the list of CUs.
Definition: DwarfFile.cpp:25
llvm::DwarfFile::getStringPool
DwarfStringPool & getStringPool()
Returns the string pool.
Definition: DwarfFile.h:144
llvm::DwarfFile::getStringOffsetsStartSym
MCSymbol * getStringOffsetsStartSym() const
Definition: DwarfFile.h:146
llvm::DwarfFile::computeSizeAndOffsets
void computeSizeAndOffsets()
Compute the size and offset of all the DIEs.
Definition: DwarfFile.cpp:59
llvm::AArch64CC::LS
@ LS
Definition: AArch64BaseInfo.h:264
llvm::DIEAbbrevSet
Helps unique DIEAbbrev objects and assigns abbreviation numbers.
Definition: DIE.h:135
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:82
llvm::RangeSpan::Begin
const MCSymbol * Begin
Definition: DwarfFile.h:37
llvm::DwarfFile
Definition: DwarfFile.h:49
llvm::DwarfFile::emitUnits
void emitUnits(bool UseOffsets)
Emit all of the units to the section listed with the given abbreviation section.
Definition: DwarfFile.cpp:31
llvm::DwarfFile::getScopeVariables
DenseMap< LexicalScope *, ScopeVars > & getScopeVariables()
Definition: DwarfFile.h:157
llvm::DwarfFile::getRnglistsTableBaseSym
MCSymbol * getRnglistsTableBaseSym() const
Definition: DwarfFile.h:149
llvm::RangeSpanList::CU
const DwarfCompileUnit * CU
Definition: DwarfFile.h:44
SmallVector.h
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
llvm::DwarfUnit
This dwarf writer support class manages information associated with a source file.
Definition: DwarfUnit.h:37
DwarfStringPool.h
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
llvm::DwarfFile::emitAbbrevs
void emitAbbrevs(MCSection *)
Emit a set of abbreviations to the specific section.
Definition: DwarfFile.cpp:98
llvm::DwarfFile::getRangeLists
const SmallVectorImpl< RangeSpanList > & getRangeLists() const
getRangeLists - Get the vector of range lists.
Definition: DwarfFile.h:109
CU
Definition: AArch64AsmBackend.cpp:501
llvm::RangeSpanList
Definition: DwarfFile.h:41
llvm::RangeSpan::End
const MCSymbol * End
Definition: DwarfFile.h:38