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