1 //===- llvm/CodeGen/DwarfFile.h - Dwarf Debug Framework ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
11 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
13 #include "DwarfStringPool.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/CodeGen/DIE.h"
18 #include "llvm/IR/Metadata.h"
19 #include "llvm/Support/Allocator.h"
30 class DwarfCompileUnit;
35 // Data structure to hold a range for range lists.
38 RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {}
39 const MCSymbol *getStart() const { return Start; }
40 const MCSymbol *getEnd() const { return End; }
41 void setEnd(const MCSymbol *E) { End = E; }
44 const MCSymbol *Start, *End;
49 // Index for locating within the debug_range section this particular span.
51 const DwarfCompileUnit *CU;
53 SmallVector<RangeSpan, 2> Ranges;
56 RangeSpanList(MCSymbol *Sym, const DwarfCompileUnit &CU,
57 SmallVector<RangeSpan, 2> Ranges)
58 : RangeSym(Sym), CU(&CU), Ranges(std::move(Ranges)) {}
59 MCSymbol *getSym() const { return RangeSym; }
60 const DwarfCompileUnit &getCU() const { return *CU; }
61 const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
62 void addRange(RangeSpan Range) { Ranges.push_back(Range); }
66 // Target of Dwarf emission, used for sizing of abbreviations.
69 BumpPtrAllocator AbbrevAllocator;
71 // Used to uniquely define abbreviations.
74 // A pointer to all units in the section.
75 SmallVector<std::unique_ptr<DwarfCompileUnit>, 1> CUs;
77 DwarfStringPool StrPool;
79 // List of range lists for a given compile unit, separate from the ranges for
81 SmallVector<RangeSpanList, 1> CURangeLists;
83 /// DWARF v5: The symbol that designates the start of the contribution to
84 /// the string offsets table. The contribution is shared by all units.
85 MCSymbol *StringOffsetsStartSym = nullptr;
87 /// DWARF v5: The symbol that designates the base of the range list table.
88 /// The table is shared by all units.
89 MCSymbol *RnglistsTableBaseSym = nullptr;
91 /// DWARF v5: The symbol that designates the base of the locations list table.
92 /// The table is shared by all units.
93 MCSymbol *LoclistsTableBaseSym = nullptr;
95 /// The variables of a lexical scope.
97 /// We need to sort Args by ArgNo and check for duplicates. This could also
98 /// be implemented as a list or vector + std::lower_bound().
99 std::map<unsigned, DbgVariable *> Args;
100 SmallVector<DbgVariable *, 8> Locals;
102 /// Collection of DbgVariables of each lexical scope.
103 DenseMap<LexicalScope *, ScopeVars> ScopeVariables;
105 /// Collection of DbgLabels of each lexical scope.
106 using LabelList = SmallVector<DbgLabel *, 4>;
107 DenseMap<LexicalScope *, LabelList> ScopeLabels;
109 // Collection of abstract subprogram DIEs.
110 DenseMap<const MDNode *, DIE *> AbstractSPDies;
111 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities;
113 /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can
114 /// be shared across CUs, that is why we keep the map here instead
115 /// of in DwarfCompileUnit.
116 DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap;
119 DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA);
121 const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
125 std::pair<uint32_t, RangeSpanList *> addRange(const DwarfCompileUnit &CU,
126 SmallVector<RangeSpan, 2> R);
128 /// getRangeLists - Get the vector of range lists.
129 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
133 /// Compute the size and offset of a DIE given an incoming Offset.
134 unsigned computeSizeAndOffset(DIE &Die, unsigned Offset);
136 /// Compute the size and offset of all the DIEs.
137 void computeSizeAndOffsets();
139 /// Compute the size and offset of all the DIEs in the given unit.
140 /// \returns The size of the root DIE.
141 unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU);
143 /// Add a unit to the list of CUs.
144 void addUnit(std::unique_ptr<DwarfCompileUnit> U);
146 /// Emit all of the units to the section listed with the given
147 /// abbreviation section.
148 void emitUnits(bool UseOffsets);
150 /// Emit the given unit to its section.
151 void emitUnit(DwarfUnit *U, bool UseOffsets);
153 /// Emit a set of abbreviations to the specific section.
154 void emitAbbrevs(MCSection *);
156 /// Emit all of the strings to the section given. If OffsetSection is
157 /// non-null, emit a table of string offsets to it. If UseRelativeOffsets
158 /// is false, emit absolute offsets to the strings. Otherwise, emit
159 /// relocatable references to the strings if they are supported by the target.
160 void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr,
161 bool UseRelativeOffsets = false);
163 /// Returns the string pool.
164 DwarfStringPool &getStringPool() { return StrPool; }
166 MCSymbol *getStringOffsetsStartSym() const { return StringOffsetsStartSym; }
167 void setStringOffsetsStartSym(MCSymbol *Sym) { StringOffsetsStartSym = Sym; }
169 MCSymbol *getRnglistsTableBaseSym() const { return RnglistsTableBaseSym; }
170 void setRnglistsTableBaseSym(MCSymbol *Sym) { RnglistsTableBaseSym = Sym; }
172 MCSymbol *getLoclistsTableBaseSym() const { return LoclistsTableBaseSym; }
173 void setLoclistsTableBaseSym(MCSymbol *Sym) { LoclistsTableBaseSym = Sym; }
175 /// \returns false if the variable was merged with a previous one.
176 bool addScopeVariable(LexicalScope *LS, DbgVariable *Var);
178 void addScopeLabel(LexicalScope *LS, DbgLabel *Label);
180 DenseMap<LexicalScope *, ScopeVars> &getScopeVariables() {
181 return ScopeVariables;
184 DenseMap<LexicalScope *, LabelList> &getScopeLabels() {
188 DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
189 return AbstractSPDies;
192 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() {
193 return AbstractEntities;
196 void insertDIE(const MDNode *TypeMD, DIE *Die) {
197 DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die));
200 DIE *getDIE(const MDNode *TypeMD) {
201 return DITypeNodeToDieMap.lookup(TypeMD);
205 } // end namespace llvm
207 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H