1 //===- llvm/CodeGen/DwarfFile.h - Dwarf Debug Framework ---------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
10 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
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/IR/Metadata.h"
18 #include "llvm/Support/Allocator.h"
29 class DwarfCompileUnit;
34 // Data structure to hold a range for range lists.
37 RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {}
38 const MCSymbol *getStart() const { return Start; }
39 const MCSymbol *getEnd() const { return End; }
40 void setEnd(const MCSymbol *E) { End = E; }
43 const MCSymbol *Start, *End;
48 // Index for locating within the debug_range section this particular span.
50 const DwarfCompileUnit *CU;
52 SmallVector<RangeSpan, 2> Ranges;
55 RangeSpanList(MCSymbol *Sym, const DwarfCompileUnit &CU,
56 SmallVector<RangeSpan, 2> Ranges)
57 : RangeSym(Sym), CU(&CU), Ranges(std::move(Ranges)) {}
58 MCSymbol *getSym() const { return RangeSym; }
59 const DwarfCompileUnit &getCU() const { return *CU; }
60 const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
64 // Target of Dwarf emission, used for sizing of abbreviations.
67 BumpPtrAllocator AbbrevAllocator;
69 // Used to uniquely define abbreviations.
72 // A pointer to all units in the section.
73 SmallVector<std::unique_ptr<DwarfCompileUnit>, 1> CUs;
75 DwarfStringPool StrPool;
77 // List of range lists for a given compile unit, separate from the ranges for
79 SmallVector<RangeSpanList, 1> CURangeLists;
81 /// DWARF v5: The symbol that designates the start of the contribution to
82 /// the string offsets table. The contribution is shared by all units.
83 MCSymbol *StringOffsetsStartSym = nullptr;
85 /// DWARF v5: The symbol that designates the base of the range list table.
86 /// The table is shared by all units.
87 MCSymbol *RnglistsTableBaseSym = nullptr;
89 /// DWARF v5: The symbol that designates the base of the locations list table.
90 /// The table is shared by all units.
91 MCSymbol *LoclistsTableBaseSym = nullptr;
93 /// The variables of a lexical scope.
95 /// We need to sort Args by ArgNo and check for duplicates. This could also
96 /// be implemented as a list or vector + std::lower_bound().
97 std::map<unsigned, DbgVariable *> Args;
98 SmallVector<DbgVariable *, 8> Locals;
100 /// Collection of DbgVariables of each lexical scope.
101 DenseMap<LexicalScope *, ScopeVars> ScopeVariables;
103 /// Collection of DbgLabels of each lexical scope.
104 using LabelList = SmallVector<DbgLabel *, 4>;
105 DenseMap<LexicalScope *, LabelList> ScopeLabels;
107 // Collection of abstract subprogram DIEs.
108 DenseMap<const MDNode *, DIE *> AbstractSPDies;
109 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities;
111 /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can
112 /// be shared across CUs, that is why we keep the map here instead
113 /// of in DwarfCompileUnit.
114 DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap;
117 DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA);
119 const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
123 std::pair<uint32_t, RangeSpanList *> addRange(const DwarfCompileUnit &CU,
124 SmallVector<RangeSpan, 2> R);
126 /// getRangeLists - Get the vector of range lists.
127 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
131 /// Compute the size and offset of a DIE given an incoming Offset.
132 unsigned computeSizeAndOffset(DIE &Die, unsigned Offset);
134 /// Compute the size and offset of all the DIEs.
135 void computeSizeAndOffsets();
137 /// Compute the size and offset of all the DIEs in the given unit.
138 /// \returns The size of the root DIE.
139 unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU);
141 /// Add a unit to the list of CUs.
142 void addUnit(std::unique_ptr<DwarfCompileUnit> U);
144 /// Emit all of the units to the section listed with the given
145 /// abbreviation section.
146 void emitUnits(bool UseOffsets);
148 /// Emit the given unit to its section.
149 void emitUnit(DwarfUnit *TheU, bool UseOffsets);
151 /// Emit a set of abbreviations to the specific section.
152 void emitAbbrevs(MCSection *);
154 /// Emit all of the strings to the section given. If OffsetSection is
155 /// non-null, emit a table of string offsets to it. If UseRelativeOffsets
156 /// is false, emit absolute offsets to the strings. Otherwise, emit
157 /// relocatable references to the strings if they are supported by the target.
158 void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr,
159 bool UseRelativeOffsets = false);
161 /// Returns the string pool.
162 DwarfStringPool &getStringPool() { return StrPool; }
164 MCSymbol *getStringOffsetsStartSym() const { return StringOffsetsStartSym; }
165 void setStringOffsetsStartSym(MCSymbol *Sym) { StringOffsetsStartSym = Sym; }
167 MCSymbol *getRnglistsTableBaseSym() const { return RnglistsTableBaseSym; }
168 void setRnglistsTableBaseSym(MCSymbol *Sym) { RnglistsTableBaseSym = Sym; }
170 MCSymbol *getLoclistsTableBaseSym() const { return LoclistsTableBaseSym; }
171 void setLoclistsTableBaseSym(MCSymbol *Sym) { LoclistsTableBaseSym = Sym; }
173 /// \returns false if the variable was merged with a previous one.
174 bool addScopeVariable(LexicalScope *LS, DbgVariable *Var);
176 void addScopeLabel(LexicalScope *LS, DbgLabel *Label);
178 DenseMap<LexicalScope *, ScopeVars> &getScopeVariables() {
179 return ScopeVariables;
182 DenseMap<LexicalScope *, LabelList> &getScopeLabels() {
186 DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
187 return AbstractSPDies;
190 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() {
191 return AbstractEntities;
194 void insertDIE(const MDNode *TypeMD, DIE *Die) {
195 DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die));
198 DIE *getDIE(const MDNode *TypeMD) {
199 return DITypeNodeToDieMap.lookup(TypeMD);
203 } // end namespace llvm
205 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H