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.
36 const MCSymbol
*Begin
;
42 // Index for locating within the debug_range section this particular span.
44 const DwarfCompileUnit
*CU
;
46 SmallVector
<RangeSpan
, 2> Ranges
;
49 RangeSpanList(MCSymbol
*Sym
, const DwarfCompileUnit
&CU
,
50 SmallVector
<RangeSpan
, 2> Ranges
)
51 : RangeSym(Sym
), CU(&CU
), Ranges(std::move(Ranges
)) {}
52 MCSymbol
*getSym() const { return RangeSym
; }
53 const DwarfCompileUnit
&getCU() const { return *CU
; }
54 const SmallVectorImpl
<RangeSpan
> &getRanges() const { return Ranges
; }
58 // Target of Dwarf emission, used for sizing of abbreviations.
61 BumpPtrAllocator AbbrevAllocator
;
63 // Used to uniquely define abbreviations.
66 // A pointer to all units in the section.
67 SmallVector
<std::unique_ptr
<DwarfCompileUnit
>, 1> CUs
;
69 DwarfStringPool StrPool
;
71 // List of range lists for a given compile unit, separate from the ranges for
73 SmallVector
<RangeSpanList
, 1> CURangeLists
;
75 /// DWARF v5: The symbol that designates the start of the contribution to
76 /// the string offsets table. The contribution is shared by all units.
77 MCSymbol
*StringOffsetsStartSym
= nullptr;
79 /// DWARF v5: The symbol that designates the base of the range list table.
80 /// The table is shared by all units.
81 MCSymbol
*RnglistsTableBaseSym
= nullptr;
83 /// The variables of a lexical scope.
85 /// We need to sort Args by ArgNo and check for duplicates. This could also
86 /// be implemented as a list or vector + std::lower_bound().
87 std::map
<unsigned, DbgVariable
*> Args
;
88 SmallVector
<DbgVariable
*, 8> Locals
;
90 /// Collection of DbgVariables of each lexical scope.
91 DenseMap
<LexicalScope
*, ScopeVars
> ScopeVariables
;
93 /// Collection of DbgLabels of each lexical scope.
94 using LabelList
= SmallVector
<DbgLabel
*, 4>;
95 DenseMap
<LexicalScope
*, LabelList
> ScopeLabels
;
97 // Collection of abstract subprogram DIEs.
98 DenseMap
<const MDNode
*, DIE
*> AbstractSPDies
;
99 DenseMap
<const DINode
*, std::unique_ptr
<DbgEntity
>> AbstractEntities
;
101 /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can
102 /// be shared across CUs, that is why we keep the map here instead
103 /// of in DwarfCompileUnit.
104 DenseMap
<const MDNode
*, DIE
*> DITypeNodeToDieMap
;
107 DwarfFile(AsmPrinter
*AP
, StringRef Pref
, BumpPtrAllocator
&DA
);
109 const SmallVectorImpl
<std::unique_ptr
<DwarfCompileUnit
>> &getUnits() {
113 std::pair
<uint32_t, RangeSpanList
*> addRange(const DwarfCompileUnit
&CU
,
114 SmallVector
<RangeSpan
, 2> R
);
116 /// getRangeLists - Get the vector of range lists.
117 const SmallVectorImpl
<RangeSpanList
> &getRangeLists() const {
121 /// Compute the size and offset of a DIE given an incoming Offset.
122 unsigned computeSizeAndOffset(DIE
&Die
, unsigned Offset
);
124 /// Compute the size and offset of all the DIEs.
125 void computeSizeAndOffsets();
127 /// Compute the size and offset of all the DIEs in the given unit.
128 /// \returns The size of the root DIE.
129 unsigned computeSizeAndOffsetsForUnit(DwarfUnit
*TheU
);
131 /// Add a unit to the list of CUs.
132 void addUnit(std::unique_ptr
<DwarfCompileUnit
> U
);
134 /// Emit all of the units to the section listed with the given
135 /// abbreviation section.
136 void emitUnits(bool UseOffsets
);
138 /// Emit the given unit to its section.
139 void emitUnit(DwarfUnit
*TheU
, bool UseOffsets
);
141 /// Emit a set of abbreviations to the specific section.
142 void emitAbbrevs(MCSection
*);
144 /// Emit all of the strings to the section given. If OffsetSection is
145 /// non-null, emit a table of string offsets to it. If UseRelativeOffsets
146 /// is false, emit absolute offsets to the strings. Otherwise, emit
147 /// relocatable references to the strings if they are supported by the target.
148 void emitStrings(MCSection
*StrSection
, MCSection
*OffsetSection
= nullptr,
149 bool UseRelativeOffsets
= false);
151 /// Returns the string pool.
152 DwarfStringPool
&getStringPool() { return StrPool
; }
154 MCSymbol
*getStringOffsetsStartSym() const { return StringOffsetsStartSym
; }
155 void setStringOffsetsStartSym(MCSymbol
*Sym
) { StringOffsetsStartSym
= Sym
; }
157 MCSymbol
*getRnglistsTableBaseSym() const { return RnglistsTableBaseSym
; }
158 void setRnglistsTableBaseSym(MCSymbol
*Sym
) { RnglistsTableBaseSym
= Sym
; }
160 /// \returns false if the variable was merged with a previous one.
161 bool addScopeVariable(LexicalScope
*LS
, DbgVariable
*Var
);
163 void addScopeLabel(LexicalScope
*LS
, DbgLabel
*Label
);
165 DenseMap
<LexicalScope
*, ScopeVars
> &getScopeVariables() {
166 return ScopeVariables
;
169 DenseMap
<LexicalScope
*, LabelList
> &getScopeLabels() {
173 DenseMap
<const MDNode
*, DIE
*> &getAbstractSPDies() {
174 return AbstractSPDies
;
177 DenseMap
<const DINode
*, std::unique_ptr
<DbgEntity
>> &getAbstractEntities() {
178 return AbstractEntities
;
181 void insertDIE(const MDNode
*TypeMD
, DIE
*Die
) {
182 DITypeNodeToDieMap
.insert(std::make_pair(TypeMD
, Die
));
185 DIE
*getDIE(const MDNode
*TypeMD
) {
186 return DITypeNodeToDieMap
.lookup(TypeMD
);
190 } // end namespace llvm
192 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H