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/SmallPtrSet.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/CodeGen/DIE.h"
18#include "llvm/Support/Allocator.h"
19#include <map>
20#include <memory>
21#include <utility>
22
23namespace llvm {
24
25class AsmPrinter;
26class DbgEntity;
27class DbgVariable;
28class DbgLabel;
29class DINode;
30class DILocalScope;
31class DISubprogram;
32class DwarfCompileUnit;
33class DwarfUnit;
34class LexicalScope;
35class MCSection;
36class MDNode;
37
38// Data structure to hold a range for range lists.
39struct RangeSpan {
40 const MCSymbol *Begin;
41 const MCSymbol *End;
42
43 bool operator==(const RangeSpan &Other) const {
44 return Begin == Other.Begin && End == Other.End;
45 }
46};
47
48struct RangeSpanList {
49 // Index for locating within the debug_range section this particular span.
50 MCSymbol *Label;
51 const DwarfCompileUnit *CU;
52 // List of ranges.
53 SmallVector<RangeSpan, 2> Ranges;
54};
55
56class DwarfFile {
57 // Target of Dwarf emission, used for sizing of abbreviations.
58 AsmPrinter *Asm;
59
60 BumpPtrAllocator AbbrevAllocator;
61
62 // Used to uniquely define abbreviations.
63 DIEAbbrevSet Abbrevs;
64
65 // A pointer to all units in the section.
66 SmallVector<std::unique_ptr<DwarfCompileUnit>, 1> CUs;
67
68 DwarfStringPool StrPool;
69
70 // List of range lists for a given compile unit, separate from the ranges for
71 // the CU itself.
72 SmallVector<RangeSpanList, 1> CURangeLists;
73
74 /// DWARF v5: The symbol that designates the start of the contribution to
75 /// the string offsets table. The contribution is shared by all units.
76 MCSymbol *StringOffsetsStartSym = nullptr;
77
78 /// DWARF v5: The symbol that designates the base of the range list table.
79 /// The table is shared by all units.
80 MCSymbol *RnglistsTableBaseSym = nullptr;
81
82 /// The variables of a lexical scope.
83 struct ScopeVars {
84 /// We need to sort Args by ArgNo and check for duplicates. This could also
85 /// be implemented as a list or vector + std::lower_bound().
86 std::map<unsigned, DbgVariable *> Args;
87 SmallVector<DbgVariable *, 8> Locals;
88 };
89 /// Collection of DbgVariables of each lexical scope.
90 DenseMap<LexicalScope *, ScopeVars> ScopeVariables;
91
92 /// Collection of DbgLabels of each lexical scope.
93 using LabelList = SmallVector<DbgLabel *, 4>;
94 DenseMap<LexicalScope *, LabelList> ScopeLabels;
95
96 // Collection of abstract subprogram DIEs.
97 DenseMap<const DILocalScope *, DIE *> AbstractLocalScopeDIEs;
98 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities;
99 /// Keeps track of abstract subprograms to populate them only once.
100 // FIXME: merge creation and population of abstract scopes.
101 SmallPtrSet<const DISubprogram *, 8> FinalizedAbstractSubprograms;
102
103 /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can
104 /// be shared across CUs, that is why we keep the map here instead
105 /// of in DwarfCompileUnit.
106 DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap;
107
108public:
109 DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA);
110
111 const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
112 return CUs;
113 }
114
115 std::pair<uint32_t, RangeSpanList *> addRange(const DwarfCompileUnit &CU,
116 SmallVector<RangeSpan, 2> R);
117
118 /// getRangeLists - Get the vector of range lists.
119 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
120 return CURangeLists;
121 }
122
123 /// Compute the size and offset of a DIE given an incoming Offset.
124 unsigned computeSizeAndOffset(DIE &Die, unsigned Offset);
125
126 /// Compute the size and offset of all the DIEs.
127 void computeSizeAndOffsets();
128
129 /// Compute the size and offset of all the DIEs in the given unit.
130 /// \returns The size of the root DIE.
131 unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU);
132
133 /// Add a unit to the list of CUs.
134 void addUnit(std::unique_ptr<DwarfCompileUnit> U);
135
136 /// Emit all of the units to the section listed with the given
137 /// abbreviation section.
138 void emitUnits(bool UseOffsets);
139
140 /// Emit the given unit to its section.
141 void emitUnit(DwarfUnit *TheU, bool UseOffsets);
142
143 /// Emit a set of abbreviations to the specific section.
144 void emitAbbrevs(MCSection *);
145
146 /// Emit all of the strings to the section given. If OffsetSection is
147 /// non-null, emit a table of string offsets to it. If UseRelativeOffsets
148 /// is false, emit absolute offsets to the strings. Otherwise, emit
149 /// relocatable references to the strings if they are supported by the target.
150 void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr,
151 bool UseRelativeOffsets = false);
152
153 /// Returns the string pool.
154 DwarfStringPool &getStringPool() { return StrPool; }
155
156 MCSymbol *getStringOffsetsStartSym() const { return StringOffsetsStartSym; }
157 void setStringOffsetsStartSym(MCSymbol *Sym) { StringOffsetsStartSym = Sym; }
158
159 MCSymbol *getRnglistsTableBaseSym() const { return RnglistsTableBaseSym; }
160 void setRnglistsTableBaseSym(MCSymbol *Sym) { RnglistsTableBaseSym = Sym; }
161
162 void addScopeVariable(LexicalScope *LS, DbgVariable *Var);
163
164 void addScopeLabel(LexicalScope *LS, DbgLabel *Label);
165
166 DenseMap<LexicalScope *, ScopeVars> &getScopeVariables() {
167 return ScopeVariables;
168 }
169
170 DenseMap<LexicalScope *, LabelList> &getScopeLabels() {
171 return ScopeLabels;
172 }
173
174 DenseMap<const DILocalScope *, DIE *> &getAbstractScopeDIEs() {
175 return AbstractLocalScopeDIEs;
176 }
177
178 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() {
179 return AbstractEntities;
180 }
181
182 auto &getFinalizedAbstractSubprograms() {
183 return FinalizedAbstractSubprograms;
184 }
185
186 void insertDIE(const MDNode *TypeMD, DIE *Die) {
187 DITypeNodeToDieMap.insert(KV: std::make_pair(x&: TypeMD, y&: Die));
188 }
189
190 DIE *getDIE(const MDNode *TypeMD) {
191 return DITypeNodeToDieMap.lookup(Val: TypeMD);
192 }
193};
194
195} // end namespace llvm
196
197#endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
198