xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===- llvm/CodeGen/DwarfDebug.h - Dwarf Debug Framework --------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains support for writing dwarf debug info into asm files.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFDEBUG_H
140b57cec5SDimitry Andric #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFDEBUG_H
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "AddressPool.h"
170b57cec5SDimitry Andric #include "DebugLocEntry.h"
1881ad6265SDimitry Andric #include "DebugLocStream.h"
190b57cec5SDimitry Andric #include "DwarfFile.h"
200b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
210b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h"
220b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h"
230b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
240b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
250b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
260b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h"
270b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
280b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/AccelTable.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/DebugHandlerBase.h"
320b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
330b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
340b57cec5SDimitry Andric #include "llvm/IR/Metadata.h"
350b57cec5SDimitry Andric #include "llvm/MC/MCDwarf.h"
360b57cec5SDimitry Andric #include "llvm/Support/Allocator.h"
370b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
380b57cec5SDimitry Andric #include <cassert>
390b57cec5SDimitry Andric #include <cstdint>
400b57cec5SDimitry Andric #include <limits>
410b57cec5SDimitry Andric #include <memory>
420b57cec5SDimitry Andric #include <utility>
43*5f757f3fSDimitry Andric #include <variant>
440b57cec5SDimitry Andric #include <vector>
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric namespace llvm {
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric class AsmPrinter;
490b57cec5SDimitry Andric class ByteStreamer;
500b57cec5SDimitry Andric class DIE;
510b57cec5SDimitry Andric class DwarfCompileUnit;
520b57cec5SDimitry Andric class DwarfExpression;
530b57cec5SDimitry Andric class DwarfTypeUnit;
540b57cec5SDimitry Andric class DwarfUnit;
550b57cec5SDimitry Andric class LexicalScope;
560b57cec5SDimitry Andric class MachineFunction;
570b57cec5SDimitry Andric class MCSection;
580b57cec5SDimitry Andric class MCSymbol;
590b57cec5SDimitry Andric class Module;
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
620b57cec5SDimitry Andric /// This class is defined as the common parent of DbgVariable and DbgLabel
630b57cec5SDimitry Andric /// such that it could levarage polymorphism to extract common code for
640b57cec5SDimitry Andric /// DbgVariable and DbgLabel.
650b57cec5SDimitry Andric class DbgEntity {
660b57cec5SDimitry Andric public:
670b57cec5SDimitry Andric   enum DbgEntityKind {
680b57cec5SDimitry Andric     DbgVariableKind,
690b57cec5SDimitry Andric     DbgLabelKind
700b57cec5SDimitry Andric   };
710b57cec5SDimitry Andric 
72349cc55cSDimitry Andric private:
73349cc55cSDimitry Andric   const DINode *Entity;
74349cc55cSDimitry Andric   const DILocation *InlinedAt;
75349cc55cSDimitry Andric   DIE *TheDIE = nullptr;
76349cc55cSDimitry Andric   const DbgEntityKind SubclassID;
77349cc55cSDimitry Andric 
78349cc55cSDimitry Andric public:
79349cc55cSDimitry Andric   DbgEntity(const DINode *N, const DILocation *IA, DbgEntityKind ID)
800b57cec5SDimitry Andric       : Entity(N), InlinedAt(IA), SubclassID(ID) {}
8181ad6265SDimitry Andric   virtual ~DbgEntity() = default;
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   /// Accessors.
840b57cec5SDimitry Andric   /// @{
850b57cec5SDimitry Andric   const DINode *getEntity() const { return Entity; }
860b57cec5SDimitry Andric   const DILocation *getInlinedAt() const { return InlinedAt; }
870b57cec5SDimitry Andric   DIE *getDIE() const { return TheDIE; }
88349cc55cSDimitry Andric   DbgEntityKind getDbgEntityID() const { return SubclassID; }
890b57cec5SDimitry Andric   /// @}
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric   void setDIE(DIE &D) { TheDIE = &D; }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   static bool classof(const DbgEntity *N) {
940b57cec5SDimitry Andric     switch (N->getDbgEntityID()) {
950b57cec5SDimitry Andric     case DbgVariableKind:
960b57cec5SDimitry Andric     case DbgLabelKind:
970b57cec5SDimitry Andric       return true;
980b57cec5SDimitry Andric     }
99349cc55cSDimitry Andric     llvm_unreachable("Invalid DbgEntityKind");
1000b57cec5SDimitry Andric   }
1010b57cec5SDimitry Andric };
1020b57cec5SDimitry Andric 
103*5f757f3fSDimitry Andric class DbgVariable;
104480093f4SDimitry Andric 
105*5f757f3fSDimitry Andric bool operator<(const struct FrameIndexExpr &LHS,
106*5f757f3fSDimitry Andric                const struct FrameIndexExpr &RHS);
107*5f757f3fSDimitry Andric bool operator<(const struct EntryValueInfo &LHS,
108*5f757f3fSDimitry Andric                const struct EntryValueInfo &RHS);
1090b57cec5SDimitry Andric 
110*5f757f3fSDimitry Andric /// Proxy for one MMI entry.
1110b57cec5SDimitry Andric struct FrameIndexExpr {
1120b57cec5SDimitry Andric   int FI;
1130b57cec5SDimitry Andric   const DIExpression *Expr;
114*5f757f3fSDimitry Andric 
115*5f757f3fSDimitry Andric   /// Operator enabling sorting based on fragment offset.
116*5f757f3fSDimitry Andric   friend bool operator<(const FrameIndexExpr &LHS, const FrameIndexExpr &RHS);
1170b57cec5SDimitry Andric };
118*5f757f3fSDimitry Andric 
119*5f757f3fSDimitry Andric /// Represents an entry-value location, or a fragment of one.
120*5f757f3fSDimitry Andric struct EntryValueInfo {
121*5f757f3fSDimitry Andric   MCRegister Reg;
122*5f757f3fSDimitry Andric   const DIExpression &Expr;
123*5f757f3fSDimitry Andric 
124*5f757f3fSDimitry Andric   /// Operator enabling sorting based on fragment offset.
125*5f757f3fSDimitry Andric   friend bool operator<(const EntryValueInfo &LHS, const EntryValueInfo &RHS);
126*5f757f3fSDimitry Andric };
127*5f757f3fSDimitry Andric 
128*5f757f3fSDimitry Andric // Namespace for alternatives of a DbgVariable.
129*5f757f3fSDimitry Andric namespace Loc {
130*5f757f3fSDimitry Andric /// Single value location description.
131*5f757f3fSDimitry Andric class Single {
132*5f757f3fSDimitry Andric   std::unique_ptr<DbgValueLoc> ValueLoc;
133*5f757f3fSDimitry Andric   const DIExpression *Expr;
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric public:
136*5f757f3fSDimitry Andric   explicit Single(DbgValueLoc ValueLoc);
137*5f757f3fSDimitry Andric   explicit Single(const MachineInstr *DbgValue);
138*5f757f3fSDimitry Andric   const DbgValueLoc &getValueLoc() const { return *ValueLoc; }
139*5f757f3fSDimitry Andric   const DIExpression *getExpr() const { return Expr; }
140*5f757f3fSDimitry Andric };
141*5f757f3fSDimitry Andric /// Multi-value location description.
142*5f757f3fSDimitry Andric class Multi {
143*5f757f3fSDimitry Andric   /// Index of the entry list in DebugLocs.
144*5f757f3fSDimitry Andric   unsigned DebugLocListIndex;
145*5f757f3fSDimitry Andric   /// DW_OP_LLVM_tag_offset value from DebugLocs.
146*5f757f3fSDimitry Andric   std::optional<uint8_t> DebugLocListTagOffset;
1470b57cec5SDimitry Andric 
148*5f757f3fSDimitry Andric public:
149*5f757f3fSDimitry Andric   explicit Multi(unsigned DebugLocListIndex,
150*5f757f3fSDimitry Andric                  std::optional<uint8_t> DebugLocListTagOffset)
151*5f757f3fSDimitry Andric       : DebugLocListIndex(DebugLocListIndex),
152*5f757f3fSDimitry Andric         DebugLocListTagOffset(DebugLocListTagOffset) {}
153*5f757f3fSDimitry Andric   unsigned getDebugLocListIndex() const { return DebugLocListIndex; }
154*5f757f3fSDimitry Andric   std::optional<uint8_t> getDebugLocListTagOffset() const {
155*5f757f3fSDimitry Andric     return DebugLocListTagOffset;
156*5f757f3fSDimitry Andric   }
157*5f757f3fSDimitry Andric };
158*5f757f3fSDimitry Andric /// Single location defined by (potentially multiple) MMI entries.
159*5f757f3fSDimitry Andric struct MMI {
160*5f757f3fSDimitry Andric   std::set<FrameIndexExpr> FrameIndexExprs;
1610b57cec5SDimitry Andric 
162*5f757f3fSDimitry Andric public:
163*5f757f3fSDimitry Andric   explicit MMI(const DIExpression *E, int FI) : FrameIndexExprs({{FI, E}}) {
1640b57cec5SDimitry Andric     assert((!E || E->isValid()) && "Expected valid expression");
1650b57cec5SDimitry Andric     assert(FI != std::numeric_limits<int>::max() && "Expected valid index");
166*5f757f3fSDimitry Andric   }
167*5f757f3fSDimitry Andric   void addFrameIndexExpr(const DIExpression *Expr, int FI);
168*5f757f3fSDimitry Andric   /// Get the FI entries, sorted by fragment offset.
169*5f757f3fSDimitry Andric   const std::set<FrameIndexExpr> &getFrameIndexExprs() const;
170*5f757f3fSDimitry Andric };
171*5f757f3fSDimitry Andric /// Single location defined by (potentially multiple) EntryValueInfo.
172*5f757f3fSDimitry Andric struct EntryValue {
173*5f757f3fSDimitry Andric   std::set<EntryValueInfo> EntryValues;
174*5f757f3fSDimitry Andric   explicit EntryValue(MCRegister Reg, const DIExpression &Expr) {
175*5f757f3fSDimitry Andric     addExpr(Reg, Expr);
176*5f757f3fSDimitry Andric   };
177*5f757f3fSDimitry Andric   // Add the pair Reg, Expr to the list of entry values describing the variable.
178*5f757f3fSDimitry Andric   // If multiple expressions are added, it is the callers responsibility to
179*5f757f3fSDimitry Andric   // ensure they are all non-overlapping fragments.
180*5f757f3fSDimitry Andric   void addExpr(MCRegister Reg, const DIExpression &Expr) {
181*5f757f3fSDimitry Andric     std::optional<const DIExpression *> NonVariadicExpr =
182*5f757f3fSDimitry Andric         DIExpression::convertToNonVariadicExpression(&Expr);
183*5f757f3fSDimitry Andric     assert(NonVariadicExpr && *NonVariadicExpr);
1840b57cec5SDimitry Andric 
185*5f757f3fSDimitry Andric     EntryValues.insert({Reg, **NonVariadicExpr});
186*5f757f3fSDimitry Andric   }
187*5f757f3fSDimitry Andric };
188*5f757f3fSDimitry Andric /// Alias for the std::variant specialization base class of DbgVariable.
189*5f757f3fSDimitry Andric using Variant = std::variant<std::monostate, Loc::Single, Loc::Multi, Loc::MMI,
190*5f757f3fSDimitry Andric                              Loc::EntryValue>;
191*5f757f3fSDimitry Andric } // namespace Loc
192*5f757f3fSDimitry Andric 
193*5f757f3fSDimitry Andric //===----------------------------------------------------------------------===//
194*5f757f3fSDimitry Andric /// This class is used to track local variable information.
195*5f757f3fSDimitry Andric ///
196*5f757f3fSDimitry Andric /// Variables that have been optimized out hold the \c monostate alternative.
197*5f757f3fSDimitry Andric /// This is not distinguished from the case of a constructed \c DbgVariable
198*5f757f3fSDimitry Andric /// which has not be initialized yet.
199*5f757f3fSDimitry Andric ///
200*5f757f3fSDimitry Andric /// Variables can be created from allocas, in which case they're generated from
201*5f757f3fSDimitry Andric /// the MMI table. Such variables hold the \c Loc::MMI alternative which can
202*5f757f3fSDimitry Andric /// have multiple expressions and frame indices.
203*5f757f3fSDimitry Andric ///
204*5f757f3fSDimitry Andric /// Variables can be created from the entry value of registers, in which case
205*5f757f3fSDimitry Andric /// they're generated from the MMI table. Such variables hold the \c
206*5f757f3fSDimitry Andric /// EntryValueLoc alternative which can either have a single expression or
207*5f757f3fSDimitry Andric /// multiple *fragment* expressions.
208*5f757f3fSDimitry Andric ///
209*5f757f3fSDimitry Andric /// Variables can be created from \c DBG_VALUE instructions. Those whose
210*5f757f3fSDimitry Andric /// location changes over time hold a \c Loc::Multi alternative which uses \c
211*5f757f3fSDimitry Andric /// DebugLocListIndex and (optionally) \c DebugLocListTagOffset, while those
212*5f757f3fSDimitry Andric /// with a single location hold a \c Loc::Single alternative which use \c
213*5f757f3fSDimitry Andric /// ValueLoc and (optionally) a single \c Expr.
214*5f757f3fSDimitry Andric class DbgVariable : public DbgEntity, public Loc::Variant {
215*5f757f3fSDimitry Andric 
216*5f757f3fSDimitry Andric public:
217*5f757f3fSDimitry Andric   /// To workaround P2162R0 https://github.com/cplusplus/papers/issues/873 the
218*5f757f3fSDimitry Andric   /// base class subobject needs to be passed directly to std::visit, so expose
219*5f757f3fSDimitry Andric   /// it directly here.
220*5f757f3fSDimitry Andric   Loc::Variant &asVariant() { return *static_cast<Loc::Variant *>(this); }
221*5f757f3fSDimitry Andric   const Loc::Variant &asVariant() const {
222*5f757f3fSDimitry Andric     return *static_cast<const Loc::Variant *>(this);
223*5f757f3fSDimitry Andric   }
224*5f757f3fSDimitry Andric   /// Member shorthand for std::holds_alternative
225*5f757f3fSDimitry Andric   template <typename T> bool holds() const {
226*5f757f3fSDimitry Andric     return std::holds_alternative<T>(*this);
227*5f757f3fSDimitry Andric   }
228*5f757f3fSDimitry Andric   /// Asserting, noexcept member alternative to std::get
229*5f757f3fSDimitry Andric   template <typename T> auto &get() noexcept {
230*5f757f3fSDimitry Andric     assert(holds<T>());
231*5f757f3fSDimitry Andric     return *std::get_if<T>(this);
232*5f757f3fSDimitry Andric   }
233*5f757f3fSDimitry Andric   /// Asserting, noexcept member alternative to std::get
234*5f757f3fSDimitry Andric   template <typename T> const auto &get() const noexcept {
235*5f757f3fSDimitry Andric     assert(holds<T>());
236*5f757f3fSDimitry Andric     return *std::get_if<T>(this);
2370b57cec5SDimitry Andric   }
2380b57cec5SDimitry Andric 
239*5f757f3fSDimitry Andric   /// Construct a DbgVariable.
240*5f757f3fSDimitry Andric   ///
241*5f757f3fSDimitry Andric   /// Creates a variable without any DW_AT_location.
242*5f757f3fSDimitry Andric   DbgVariable(const DILocalVariable *V, const DILocation *IA)
243*5f757f3fSDimitry Andric       : DbgEntity(V, IA, DbgVariableKind) {}
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric   // Accessors.
2460b57cec5SDimitry Andric   const DILocalVariable *getVariable() const {
2470b57cec5SDimitry Andric     return cast<DILocalVariable>(getEntity());
2480b57cec5SDimitry Andric   }
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric   StringRef getName() const { return getVariable()->getName(); }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric   // Translate tag to proper Dwarf tag.
2530b57cec5SDimitry Andric   dwarf::Tag getTag() const {
2540b57cec5SDimitry Andric     // FIXME: Why don't we just infer this tag and store it all along?
2550b57cec5SDimitry Andric     if (getVariable()->isParameter())
2560b57cec5SDimitry Andric       return dwarf::DW_TAG_formal_parameter;
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric     return dwarf::DW_TAG_variable;
2590b57cec5SDimitry Andric   }
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric   /// Return true if DbgVariable is artificial.
2620b57cec5SDimitry Andric   bool isArtificial() const {
2630b57cec5SDimitry Andric     if (getVariable()->isArtificial())
2640b57cec5SDimitry Andric       return true;
2650b57cec5SDimitry Andric     if (getType()->isArtificial())
2660b57cec5SDimitry Andric       return true;
2670b57cec5SDimitry Andric     return false;
2680b57cec5SDimitry Andric   }
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric   bool isObjectPointer() const {
2710b57cec5SDimitry Andric     if (getVariable()->isObjectPointer())
2720b57cec5SDimitry Andric       return true;
2730b57cec5SDimitry Andric     if (getType()->isObjectPointer())
2740b57cec5SDimitry Andric       return true;
2750b57cec5SDimitry Andric     return false;
2760b57cec5SDimitry Andric   }
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric   const DIType *getType() const;
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric   static bool classof(const DbgEntity *N) {
2810b57cec5SDimitry Andric     return N->getDbgEntityID() == DbgVariableKind;
2820b57cec5SDimitry Andric   }
2830b57cec5SDimitry Andric };
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2860b57cec5SDimitry Andric /// This class is used to track label information.
2870b57cec5SDimitry Andric ///
2880b57cec5SDimitry Andric /// Labels are collected from \c DBG_LABEL instructions.
2890b57cec5SDimitry Andric class DbgLabel : public DbgEntity {
2900b57cec5SDimitry Andric   const MCSymbol *Sym;                  /// Symbol before DBG_LABEL instruction.
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric public:
2930b57cec5SDimitry Andric   /// We need MCSymbol information to generate DW_AT_low_pc.
2940b57cec5SDimitry Andric   DbgLabel(const DILabel *L, const DILocation *IA, const MCSymbol *Sym = nullptr)
2950b57cec5SDimitry Andric       : DbgEntity(L, IA, DbgLabelKind), Sym(Sym) {}
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric   /// Accessors.
2980b57cec5SDimitry Andric   /// @{
2990b57cec5SDimitry Andric   const DILabel *getLabel() const { return cast<DILabel>(getEntity()); }
3000b57cec5SDimitry Andric   const MCSymbol *getSymbol() const { return Sym; }
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric   StringRef getName() const { return getLabel()->getName(); }
3030b57cec5SDimitry Andric   /// @}
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric   /// Translate tag to proper Dwarf tag.
3060b57cec5SDimitry Andric   dwarf::Tag getTag() const {
3070b57cec5SDimitry Andric     return dwarf::DW_TAG_label;
3080b57cec5SDimitry Andric   }
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric   static bool classof(const DbgEntity *N) {
3110b57cec5SDimitry Andric     return N->getDbgEntityID() == DbgLabelKind;
3120b57cec5SDimitry Andric   }
3130b57cec5SDimitry Andric };
3140b57cec5SDimitry Andric 
3158bcb0991SDimitry Andric /// Used for tracking debug info about call site parameters.
3168bcb0991SDimitry Andric class DbgCallSiteParam {
3178bcb0991SDimitry Andric private:
3188bcb0991SDimitry Andric   unsigned Register; ///< Parameter register at the callee entry point.
3198bcb0991SDimitry Andric   DbgValueLoc Value; ///< Corresponding location for the parameter value at
3208bcb0991SDimitry Andric                      ///< the call site.
3218bcb0991SDimitry Andric public:
3228bcb0991SDimitry Andric   DbgCallSiteParam(unsigned Reg, DbgValueLoc Val)
3238bcb0991SDimitry Andric       : Register(Reg), Value(Val) {
3248bcb0991SDimitry Andric     assert(Reg && "Parameter register cannot be undef");
3258bcb0991SDimitry Andric   }
3268bcb0991SDimitry Andric 
3278bcb0991SDimitry Andric   unsigned getRegister() const { return Register; }
3288bcb0991SDimitry Andric   DbgValueLoc getValue() const { return Value; }
3298bcb0991SDimitry Andric };
3308bcb0991SDimitry Andric 
3318bcb0991SDimitry Andric /// Collection used for storing debug call site parameters.
3328bcb0991SDimitry Andric using ParamSet = SmallVector<DbgCallSiteParam, 4>;
3338bcb0991SDimitry Andric 
3340b57cec5SDimitry Andric /// Helper used to pair up a symbol and its DWARF compile unit.
3350b57cec5SDimitry Andric struct SymbolCU {
3360b57cec5SDimitry Andric   SymbolCU(DwarfCompileUnit *CU, const MCSymbol *Sym) : Sym(Sym), CU(CU) {}
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric   const MCSymbol *Sym;
3390b57cec5SDimitry Andric   DwarfCompileUnit *CU;
3400b57cec5SDimitry Andric };
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric /// The kind of accelerator tables we should emit.
3430b57cec5SDimitry Andric enum class AccelTableKind {
3440b57cec5SDimitry Andric   Default, ///< Platform default.
3450b57cec5SDimitry Andric   None,    ///< None.
3460b57cec5SDimitry Andric   Apple,   ///< .apple_names, .apple_namespaces, .apple_types, .apple_objc.
3470b57cec5SDimitry Andric   Dwarf,   ///< DWARF v5 .debug_names.
3480b57cec5SDimitry Andric };
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric /// Collects and handles dwarf debug information.
3510b57cec5SDimitry Andric class DwarfDebug : public DebugHandlerBase {
3520b57cec5SDimitry Andric   /// All DIEValues are allocated through this allocator.
3530b57cec5SDimitry Andric   BumpPtrAllocator DIEValueAllocator;
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   /// Maps MDNode with its corresponding DwarfCompileUnit.
3560b57cec5SDimitry Andric   MapVector<const MDNode *, DwarfCompileUnit *> CUMap;
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric   /// Maps a CU DIE with its corresponding DwarfCompileUnit.
3590b57cec5SDimitry Andric   DenseMap<const DIE *, DwarfCompileUnit *> CUDieMap;
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric   /// List of all labels used in aranges generation.
3620b57cec5SDimitry Andric   std::vector<SymbolCU> ArangeLabels;
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric   /// Size of each symbol emitted (for those symbols that have a specific size).
3650b57cec5SDimitry Andric   DenseMap<const MCSymbol *, uint64_t> SymSize;
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric   /// Collection of abstract variables/labels.
3680b57cec5SDimitry Andric   SmallVector<std::unique_ptr<DbgEntity>, 64> ConcreteEntities;
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric   /// Collection of DebugLocEntry. Stored in a linked list so that DIELocLists
3710b57cec5SDimitry Andric   /// can refer to them in spite of insertions into this list.
3720b57cec5SDimitry Andric   DebugLocStream DebugLocs;
3730b57cec5SDimitry Andric 
3740b57cec5SDimitry Andric   /// This is a collection of subprogram MDNodes that are processed to
3750b57cec5SDimitry Andric   /// create DIEs.
37606c3fb27SDimitry Andric   SmallSetVector<const DISubprogram *, 16> ProcessedSPNodes;
37706c3fb27SDimitry Andric 
37806c3fb27SDimitry Andric   /// Map function-local imported entities to their parent local scope
37906c3fb27SDimitry Andric   /// (either DILexicalBlock or DISubprogram) for a processed function
38006c3fb27SDimitry Andric   /// (including inlined subprograms).
38106c3fb27SDimitry Andric   using MDNodeSet = SetVector<const MDNode *, SmallVector<const MDNode *, 2>,
38206c3fb27SDimitry Andric                               SmallPtrSet<const MDNode *, 2>>;
38306c3fb27SDimitry Andric   DenseMap<const DILocalScope *, MDNodeSet> LocalDeclsPerLS;
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric   /// If nonnull, stores the current machine function we're processing.
3860b57cec5SDimitry Andric   const MachineFunction *CurFn = nullptr;
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric   /// If nonnull, stores the CU in which the previous subprogram was contained.
3895ffd83dbSDimitry Andric   const DwarfCompileUnit *PrevCU = nullptr;
3900b57cec5SDimitry Andric 
3910b57cec5SDimitry Andric   /// As an optimization, there is no need to emit an entry in the directory
3920b57cec5SDimitry Andric   /// table for the same directory as DW_AT_comp_dir.
3930b57cec5SDimitry Andric   StringRef CompilationDir;
3940b57cec5SDimitry Andric 
3950b57cec5SDimitry Andric   /// Holder for the file specific debug information.
3960b57cec5SDimitry Andric   DwarfFile InfoHolder;
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric   /// Holders for the various debug information flags that we might need to
3990b57cec5SDimitry Andric   /// have exposed. See accessor functions below for description.
4000b57cec5SDimitry Andric 
4010b57cec5SDimitry Andric   /// Map from MDNodes for user-defined types to their type signatures. Also
4020b57cec5SDimitry Andric   /// used to keep track of which types we have emitted type units for.
4030b57cec5SDimitry Andric   DenseMap<const MDNode *, uint64_t> TypeSignatures;
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric   DenseMap<const MCSection *, const MCSymbol *> SectionLabels;
4060b57cec5SDimitry Andric 
4070b57cec5SDimitry Andric   SmallVector<
4080b57cec5SDimitry Andric       std::pair<std::unique_ptr<DwarfTypeUnit>, const DICompositeType *>, 1>
4090b57cec5SDimitry Andric       TypeUnitsUnderConstruction;
4100b57cec5SDimitry Andric 
411*5f757f3fSDimitry Andric   /// Used to set a uniqe ID for a Type Unit.
412*5f757f3fSDimitry Andric   /// This counter represents number of DwarfTypeUnits created, not necessarily
413*5f757f3fSDimitry Andric   /// number of type units that will be emitted.
414*5f757f3fSDimitry Andric   unsigned NumTypeUnitsCreated = 0;
415*5f757f3fSDimitry Andric 
4160b57cec5SDimitry Andric   /// Whether to use the GNU TLS opcode (instead of the standard opcode).
4170b57cec5SDimitry Andric   bool UseGNUTLSOpcode;
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric   /// Whether to use DWARF 2 bitfields (instead of the DWARF 4 format).
4200b57cec5SDimitry Andric   bool UseDWARF2Bitfields;
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric   /// Whether to emit all linkage names, or just abstract subprograms.
4230b57cec5SDimitry Andric   bool UseAllLinkageNames;
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric   /// Use inlined strings.
4260b57cec5SDimitry Andric   bool UseInlineStrings = false;
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric   /// Allow emission of .debug_ranges section.
4290b57cec5SDimitry Andric   bool UseRangesSection = true;
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric   /// True if the sections itself must be used as references and don't create
4320b57cec5SDimitry Andric   /// temp symbols inside DWARF sections.
4330b57cec5SDimitry Andric   bool UseSectionsAsReferences = false;
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric   ///Allow emission of the .debug_loc section.
4360b57cec5SDimitry Andric   bool UseLocSection = true;
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric   /// Generate DWARF v4 type units.
4390b57cec5SDimitry Andric   bool GenerateTypeUnits;
4400b57cec5SDimitry Andric 
441e8d8bef9SDimitry Andric   /// Emit a .debug_macro section instead of .debug_macinfo.
442e8d8bef9SDimitry Andric   bool UseDebugMacroSection;
443e8d8bef9SDimitry Andric 
444e8d8bef9SDimitry Andric   /// Avoid using DW_OP_convert due to consumer incompatibilities.
445e8d8bef9SDimitry Andric   bool EnableOpConvert;
446e8d8bef9SDimitry Andric 
447e8d8bef9SDimitry Andric public:
448e8d8bef9SDimitry Andric   enum class MinimizeAddrInV5 {
449e8d8bef9SDimitry Andric     Default,
450e8d8bef9SDimitry Andric     Disabled,
451e8d8bef9SDimitry Andric     Ranges,
452fe6060f1SDimitry Andric     Expressions,
453fe6060f1SDimitry Andric     Form,
454e8d8bef9SDimitry Andric   };
455e8d8bef9SDimitry Andric 
456*5f757f3fSDimitry Andric   enum class DWARF5AccelTableKind {
457*5f757f3fSDimitry Andric     CU = 0,
458*5f757f3fSDimitry Andric     TU = 1,
459*5f757f3fSDimitry Andric   };
460*5f757f3fSDimitry Andric 
461e8d8bef9SDimitry Andric private:
462e8d8bef9SDimitry Andric   /// Force the use of DW_AT_ranges even for single-entry range lists.
463e8d8bef9SDimitry Andric   MinimizeAddrInV5 MinimizeAddr = MinimizeAddrInV5::Disabled;
464e8d8bef9SDimitry Andric 
4650b57cec5SDimitry Andric   /// DWARF5 Experimental Options
4660b57cec5SDimitry Andric   /// @{
4670b57cec5SDimitry Andric   AccelTableKind TheAccelTableKind;
4680b57cec5SDimitry Andric   bool HasAppleExtensionAttributes;
4690b57cec5SDimitry Andric   bool HasSplitDwarf;
4700b57cec5SDimitry Andric 
4710b57cec5SDimitry Andric   /// Whether to generate the DWARF v5 string offsets table.
4720b57cec5SDimitry Andric   /// It consists of a series of contributions, each preceded by a header.
4730b57cec5SDimitry Andric   /// The pre-DWARF v5 string offsets table for split dwarf is, in contrast,
4740b57cec5SDimitry Andric   /// a monolithic sequence of string offsets.
4750b57cec5SDimitry Andric   bool UseSegmentedStringOffsetsTable;
4760b57cec5SDimitry Andric 
4775ffd83dbSDimitry Andric   /// Enable production of call site parameters needed to print the debug entry
4785ffd83dbSDimitry Andric   /// values. Useful for testing purposes when a debugger does not support the
4795ffd83dbSDimitry Andric   /// feature yet.
4805ffd83dbSDimitry Andric   bool EmitDebugEntryValues;
4815ffd83dbSDimitry Andric 
4820b57cec5SDimitry Andric   /// Separated Dwarf Variables
4830b57cec5SDimitry Andric   /// In general these will all be for bits that are left in the
4840b57cec5SDimitry Andric   /// original object file, rather than things that are meant
4850b57cec5SDimitry Andric   /// to be in the .dwo sections.
4860b57cec5SDimitry Andric 
4870b57cec5SDimitry Andric   /// Holder for the skeleton information.
4880b57cec5SDimitry Andric   DwarfFile SkeletonHolder;
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric   /// Store file names for type units under fission in a line table
4910b57cec5SDimitry Andric   /// header that will be emitted into debug_line.dwo.
4920b57cec5SDimitry Andric   // FIXME: replace this with a map from comp_dir to table so that we
4930b57cec5SDimitry Andric   // can emit multiple tables during LTO each of which uses directory
4940b57cec5SDimitry Andric   // 0, referencing the comp_dir of all the type units that use it.
4950b57cec5SDimitry Andric   MCDwarfDwoLineTable SplitTypeUnitFileTable;
4960b57cec5SDimitry Andric   /// @}
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric   /// True iff there are multiple CUs in this module.
4990b57cec5SDimitry Andric   bool SingleCU;
5000b57cec5SDimitry Andric   bool IsDarwin;
5010b57cec5SDimitry Andric 
502e8d8bef9SDimitry Andric   /// Map for tracking Fortran deferred CHARACTER lengths.
503e8d8bef9SDimitry Andric   DenseMap<const DIStringType *, unsigned> StringTypeLocMap;
504e8d8bef9SDimitry Andric 
5050b57cec5SDimitry Andric   AddressPool AddrPool;
5060b57cec5SDimitry Andric 
5070b57cec5SDimitry Andric   /// Accelerator tables.
508*5f757f3fSDimitry Andric   DWARF5AccelTable AccelDebugNames;
509*5f757f3fSDimitry Andric   DWARF5AccelTable AccelTypeUnitsDebugNames;
510*5f757f3fSDimitry Andric   /// Used to hide which DWARF5AccelTable we are using now.
511*5f757f3fSDimitry Andric   DWARF5AccelTable *CurrentDebugNames = &AccelDebugNames;
5120b57cec5SDimitry Andric   AccelTable<AppleAccelTableOffsetData> AccelNames;
5130b57cec5SDimitry Andric   AccelTable<AppleAccelTableOffsetData> AccelObjC;
5140b57cec5SDimitry Andric   AccelTable<AppleAccelTableOffsetData> AccelNamespace;
5150b57cec5SDimitry Andric   AccelTable<AppleAccelTableTypeData> AccelTypes;
5160b57cec5SDimitry Andric 
517fe6060f1SDimitry Andric   /// Identify a debugger for "tuning" the debug info.
518fe6060f1SDimitry Andric   ///
519fe6060f1SDimitry Andric   /// The "tuning" should be used to set defaults for individual feature flags
520fe6060f1SDimitry Andric   /// in DwarfDebug; if a given feature has a more specific command-line option,
521fe6060f1SDimitry Andric   /// that option should take precedence over the tuning.
5220b57cec5SDimitry Andric   DebuggerKind DebuggerTuning = DebuggerKind::Default;
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric   MCDwarfDwoLineTable *getDwoLineTable(const DwarfCompileUnit &);
5250b57cec5SDimitry Andric 
5260b57cec5SDimitry Andric   const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
5270b57cec5SDimitry Andric     return InfoHolder.getUnits();
5280b57cec5SDimitry Andric   }
5290b57cec5SDimitry Andric 
5300b57cec5SDimitry Andric   using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric   void ensureAbstractEntityIsCreatedIfScoped(DwarfCompileUnit &CU,
5330b57cec5SDimitry Andric                                              const DINode *Node,
5340b57cec5SDimitry Andric                                              const MDNode *Scope);
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric   DbgEntity *createConcreteEntity(DwarfCompileUnit &TheCU,
5370b57cec5SDimitry Andric                                   LexicalScope &Scope,
5380b57cec5SDimitry Andric                                   const DINode *Node,
5390b57cec5SDimitry Andric                                   const DILocation *Location,
5400b57cec5SDimitry Andric                                   const MCSymbol *Sym = nullptr);
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric   /// Construct a DIE for this abstract scope.
5430b57cec5SDimitry Andric   void constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU, LexicalScope *Scope);
5440b57cec5SDimitry Andric 
5450b57cec5SDimitry Andric   /// Construct DIEs for call site entries describing the calls in \p MF.
5460b57cec5SDimitry Andric   void constructCallSiteEntryDIEs(const DISubprogram &SP, DwarfCompileUnit &CU,
5470b57cec5SDimitry Andric                                   DIE &ScopeDIE, const MachineFunction &MF);
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric   template <typename DataT>
550*5f757f3fSDimitry Andric   void addAccelNameImpl(const DwarfUnit &Unit,
551*5f757f3fSDimitry Andric                         const DICompileUnit::DebugNameTableKind NameTableKind,
552*5f757f3fSDimitry Andric                         AccelTable<DataT> &AppleAccel, StringRef Name,
553*5f757f3fSDimitry Andric                         const DIE &Die);
5540b57cec5SDimitry Andric 
5550b57cec5SDimitry Andric   void finishEntityDefinitions();
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric   void finishSubprogramDefinitions();
5580b57cec5SDimitry Andric 
5590b57cec5SDimitry Andric   /// Finish off debug information after all functions have been
5600b57cec5SDimitry Andric   /// processed.
5610b57cec5SDimitry Andric   void finalizeModuleInfo();
5620b57cec5SDimitry Andric 
5630b57cec5SDimitry Andric   /// Emit the debug info section.
5640b57cec5SDimitry Andric   void emitDebugInfo();
5650b57cec5SDimitry Andric 
5660b57cec5SDimitry Andric   /// Emit the abbreviation section.
5670b57cec5SDimitry Andric   void emitAbbreviations();
5680b57cec5SDimitry Andric 
5690b57cec5SDimitry Andric   /// Emit the string offsets table header.
5700b57cec5SDimitry Andric   void emitStringOffsetsTableHeader();
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric   /// Emit a specified accelerator table.
5730b57cec5SDimitry Andric   template <typename AccelTableT>
5740b57cec5SDimitry Andric   void emitAccel(AccelTableT &Accel, MCSection *Section, StringRef TableName);
5750b57cec5SDimitry Andric 
5760b57cec5SDimitry Andric   /// Emit DWARF v5 accelerator table.
5770b57cec5SDimitry Andric   void emitAccelDebugNames();
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric   /// Emit visible names into a hashed accelerator table section.
5800b57cec5SDimitry Andric   void emitAccelNames();
5810b57cec5SDimitry Andric 
5820b57cec5SDimitry Andric   /// Emit objective C classes and categories into a hashed
5830b57cec5SDimitry Andric   /// accelerator table section.
5840b57cec5SDimitry Andric   void emitAccelObjC();
5850b57cec5SDimitry Andric 
5860b57cec5SDimitry Andric   /// Emit namespace dies into a hashed accelerator table.
5870b57cec5SDimitry Andric   void emitAccelNamespaces();
5880b57cec5SDimitry Andric 
5890b57cec5SDimitry Andric   /// Emit type dies into a hashed accelerator table.
5900b57cec5SDimitry Andric   void emitAccelTypes();
5910b57cec5SDimitry Andric 
5920b57cec5SDimitry Andric   /// Emit visible names and types into debug pubnames and pubtypes sections.
5930b57cec5SDimitry Andric   void emitDebugPubSections();
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric   void emitDebugPubSection(bool GnuStyle, StringRef Name,
5960b57cec5SDimitry Andric                            DwarfCompileUnit *TheU,
5970b57cec5SDimitry Andric                            const StringMap<const DIE *> &Globals);
5980b57cec5SDimitry Andric 
5990b57cec5SDimitry Andric   /// Emit null-terminated strings into a debug str section.
6000b57cec5SDimitry Andric   void emitDebugStr();
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric   /// Emit variable locations into a debug loc section.
6030b57cec5SDimitry Andric   void emitDebugLoc();
6040b57cec5SDimitry Andric 
6050b57cec5SDimitry Andric   /// Emit variable locations into a debug loc dwo section.
6060b57cec5SDimitry Andric   void emitDebugLocDWO();
6070b57cec5SDimitry Andric 
608480093f4SDimitry Andric   void emitDebugLocImpl(MCSection *Sec);
609480093f4SDimitry Andric 
6100b57cec5SDimitry Andric   /// Emit address ranges into a debug aranges section.
6110b57cec5SDimitry Andric   void emitDebugARanges();
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric   /// Emit address ranges into a debug ranges section.
6140b57cec5SDimitry Andric   void emitDebugRanges();
6150b57cec5SDimitry Andric   void emitDebugRangesDWO();
616480093f4SDimitry Andric   void emitDebugRangesImpl(const DwarfFile &Holder, MCSection *Section);
6170b57cec5SDimitry Andric 
6180b57cec5SDimitry Andric   /// Emit macros into a debug macinfo section.
6190b57cec5SDimitry Andric   void emitDebugMacinfo();
620480093f4SDimitry Andric   /// Emit macros into a debug macinfo.dwo section.
621480093f4SDimitry Andric   void emitDebugMacinfoDWO();
622480093f4SDimitry Andric   void emitDebugMacinfoImpl(MCSection *Section);
6230b57cec5SDimitry Andric   void emitMacro(DIMacro &M);
6240b57cec5SDimitry Andric   void emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U);
6255ffd83dbSDimitry Andric   void emitMacroFileImpl(DIMacroFile &F, DwarfCompileUnit &U,
6265ffd83dbSDimitry Andric                          unsigned StartFile, unsigned EndFile,
6275ffd83dbSDimitry Andric                          StringRef (*MacroFormToString)(unsigned Form));
6280b57cec5SDimitry Andric   void handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U);
6290b57cec5SDimitry Andric 
6300b57cec5SDimitry Andric   /// DWARF 5 Experimental Split Dwarf Emitters
6310b57cec5SDimitry Andric 
6320b57cec5SDimitry Andric   /// Initialize common features of skeleton units.
6330b57cec5SDimitry Andric   void initSkeletonUnit(const DwarfUnit &U, DIE &Die,
6340b57cec5SDimitry Andric                         std::unique_ptr<DwarfCompileUnit> NewU);
6350b57cec5SDimitry Andric 
6360b57cec5SDimitry Andric   /// Construct the split debug info compile unit for the debug info section.
6370b57cec5SDimitry Andric   /// In DWARF v5, the skeleton unit DIE may have the following attributes:
6380b57cec5SDimitry Andric   /// DW_AT_addr_base, DW_AT_comp_dir, DW_AT_dwo_name, DW_AT_high_pc,
6390b57cec5SDimitry Andric   /// DW_AT_low_pc, DW_AT_ranges, DW_AT_stmt_list, and DW_AT_str_offsets_base.
6400b57cec5SDimitry Andric   /// Prior to DWARF v5 it may also have DW_AT_GNU_dwo_id. DW_AT_GNU_dwo_name
6410b57cec5SDimitry Andric   /// is used instead of DW_AT_dwo_name, Dw_AT_GNU_addr_base instead of
6420b57cec5SDimitry Andric   /// DW_AT_addr_base, and DW_AT_GNU_ranges_base instead of DW_AT_rnglists_base.
6430b57cec5SDimitry Andric   DwarfCompileUnit &constructSkeletonCU(const DwarfCompileUnit &CU);
6440b57cec5SDimitry Andric 
6450b57cec5SDimitry Andric   /// Emit the debug info dwo section.
6460b57cec5SDimitry Andric   void emitDebugInfoDWO();
6470b57cec5SDimitry Andric 
6480b57cec5SDimitry Andric   /// Emit the debug abbrev dwo section.
6490b57cec5SDimitry Andric   void emitDebugAbbrevDWO();
6500b57cec5SDimitry Andric 
6510b57cec5SDimitry Andric   /// Emit the debug line dwo section.
6520b57cec5SDimitry Andric   void emitDebugLineDWO();
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric   /// Emit the dwo stringoffsets table header.
6550b57cec5SDimitry Andric   void emitStringOffsetsTableHeaderDWO();
6560b57cec5SDimitry Andric 
6570b57cec5SDimitry Andric   /// Emit the debug str dwo section.
6580b57cec5SDimitry Andric   void emitDebugStrDWO();
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric   /// Emit DWO addresses.
6610b57cec5SDimitry Andric   void emitDebugAddr();
6620b57cec5SDimitry Andric 
6630b57cec5SDimitry Andric   /// Flags to let the linker know we have emitted new style pubnames. Only
6640b57cec5SDimitry Andric   /// emit it here if we don't have a skeleton CU for split dwarf.
6650b57cec5SDimitry Andric   void addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const;
6660b57cec5SDimitry Andric 
6670b57cec5SDimitry Andric   /// Create new DwarfCompileUnit for the given metadata node with tag
6680b57cec5SDimitry Andric   /// DW_TAG_compile_unit.
6690b57cec5SDimitry Andric   DwarfCompileUnit &getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit);
6700b57cec5SDimitry Andric   void finishUnitAttributes(const DICompileUnit *DIUnit,
6710b57cec5SDimitry Andric                             DwarfCompileUnit &NewCU);
6720b57cec5SDimitry Andric 
6730b57cec5SDimitry Andric   /// Register a source line with debug info. Returns the unique
6740b57cec5SDimitry Andric   /// label that was emitted and which provides correspondence to the
6750b57cec5SDimitry Andric   /// source line list.
6760b57cec5SDimitry Andric   void recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope,
6770b57cec5SDimitry Andric                         unsigned Flags);
6780b57cec5SDimitry Andric 
6790b57cec5SDimitry Andric   /// Populate LexicalScope entries with variables' info.
6800b57cec5SDimitry Andric   void collectEntityInfo(DwarfCompileUnit &TheCU, const DISubprogram *SP,
6810b57cec5SDimitry Andric                          DenseSet<InlinedEntity> &ProcessedVars);
6820b57cec5SDimitry Andric 
6830b57cec5SDimitry Andric   /// Build the location list for all DBG_VALUEs in the
6840b57cec5SDimitry Andric   /// function that describe the same variable. If the resulting
6850b57cec5SDimitry Andric   /// list has only one entry that is valid for entire variable's
6860b57cec5SDimitry Andric   /// scope return true.
687e8d8bef9SDimitry Andric   bool buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
688e8d8bef9SDimitry Andric                          const DbgValueHistoryMap::Entries &Entries);
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric   /// Collect variable information from the side table maintained by MF.
6910b57cec5SDimitry Andric   void collectVariableInfoFromMFTable(DwarfCompileUnit &TheCU,
6920b57cec5SDimitry Andric                                       DenseSet<InlinedEntity> &P);
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric   /// Emit the reference to the section.
6950b57cec5SDimitry Andric   void emitSectionReference(const DwarfCompileUnit &CU);
6960b57cec5SDimitry Andric 
6970b57cec5SDimitry Andric protected:
6980b57cec5SDimitry Andric   /// Gather pre-function debug information.
6990b57cec5SDimitry Andric   void beginFunctionImpl(const MachineFunction *MF) override;
7000b57cec5SDimitry Andric 
7010b57cec5SDimitry Andric   /// Gather and emit post-function debug information.
7020b57cec5SDimitry Andric   void endFunctionImpl(const MachineFunction *MF) override;
7030b57cec5SDimitry Andric 
704349cc55cSDimitry Andric   /// Get Dwarf compile unit ID for line table.
705349cc55cSDimitry Andric   unsigned getDwarfCompileUnitIDForLineTable(const DwarfCompileUnit &CU);
706349cc55cSDimitry Andric 
7070b57cec5SDimitry Andric   void skippedNonDebugFunction() override;
7080b57cec5SDimitry Andric 
7090b57cec5SDimitry Andric public:
7100b57cec5SDimitry Andric   //===--------------------------------------------------------------------===//
7110b57cec5SDimitry Andric   // Main entry points.
7120b57cec5SDimitry Andric   //
713e8d8bef9SDimitry Andric   DwarfDebug(AsmPrinter *A);
7140b57cec5SDimitry Andric 
7150b57cec5SDimitry Andric   ~DwarfDebug() override;
7160b57cec5SDimitry Andric 
7170b57cec5SDimitry Andric   /// Emit all Dwarf sections that should come prior to the
7180b57cec5SDimitry Andric   /// content.
719e8d8bef9SDimitry Andric   void beginModule(Module *M) override;
7200b57cec5SDimitry Andric 
7210b57cec5SDimitry Andric   /// Emit all Dwarf sections that should come after the content.
7220b57cec5SDimitry Andric   void endModule() override;
7230b57cec5SDimitry Andric 
7240b57cec5SDimitry Andric   /// Emits inital debug location directive.
7250b57cec5SDimitry Andric   DebugLoc emitInitialLocDirective(const MachineFunction &MF, unsigned CUID);
7260b57cec5SDimitry Andric 
7270b57cec5SDimitry Andric   /// Process beginning of an instruction.
7280b57cec5SDimitry Andric   void beginInstruction(const MachineInstr *MI) override;
7290b57cec5SDimitry Andric 
7300b57cec5SDimitry Andric   /// Perform an MD5 checksum of \p Identifier and return the lower 64 bits.
7310b57cec5SDimitry Andric   static uint64_t makeTypeSignature(StringRef Identifier);
7320b57cec5SDimitry Andric 
7330b57cec5SDimitry Andric   /// Add a DIE to the set of types that we're going to pull into
7340b57cec5SDimitry Andric   /// type units.
7350b57cec5SDimitry Andric   void addDwarfTypeUnitType(DwarfCompileUnit &CU, StringRef Identifier,
7360b57cec5SDimitry Andric                             DIE &Die, const DICompositeType *CTy);
7370b57cec5SDimitry Andric 
7380b57cec5SDimitry Andric   /// Add a label so that arange data can be generated for it.
7390b57cec5SDimitry Andric   void addArangeLabel(SymbolCU SCU) { ArangeLabels.push_back(SCU); }
7400b57cec5SDimitry Andric 
7410b57cec5SDimitry Andric   /// For symbols that have a size designated (e.g. common symbols),
7420b57cec5SDimitry Andric   /// this tracks that size.
7430b57cec5SDimitry Andric   void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override {
7440b57cec5SDimitry Andric     SymSize[Sym] = Size;
7450b57cec5SDimitry Andric   }
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric   /// Returns whether we should emit all DW_AT_[MIPS_]linkage_name.
7480b57cec5SDimitry Andric   /// If not, we still might emit certain cases.
7490b57cec5SDimitry Andric   bool useAllLinkageNames() const { return UseAllLinkageNames; }
7500b57cec5SDimitry Andric 
7510b57cec5SDimitry Andric   /// Returns whether to use DW_OP_GNU_push_tls_address, instead of the
7520b57cec5SDimitry Andric   /// standard DW_OP_form_tls_address opcode
7530b57cec5SDimitry Andric   bool useGNUTLSOpcode() const { return UseGNUTLSOpcode; }
7540b57cec5SDimitry Andric 
7550b57cec5SDimitry Andric   /// Returns whether to use the DWARF2 format for bitfields instyead of the
7560b57cec5SDimitry Andric   /// DWARF4 format.
7570b57cec5SDimitry Andric   bool useDWARF2Bitfields() const { return UseDWARF2Bitfields; }
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric   /// Returns whether to use inline strings.
7600b57cec5SDimitry Andric   bool useInlineStrings() const { return UseInlineStrings; }
7610b57cec5SDimitry Andric 
7620b57cec5SDimitry Andric   /// Returns whether ranges section should be emitted.
7630b57cec5SDimitry Andric   bool useRangesSection() const { return UseRangesSection; }
7640b57cec5SDimitry Andric 
765e8d8bef9SDimitry Andric   /// Returns whether range encodings should be used for single entry range
766e8d8bef9SDimitry Andric   /// lists.
76706c3fb27SDimitry Andric   bool alwaysUseRanges(const DwarfCompileUnit &) const;
768e8d8bef9SDimitry Andric 
769fe6060f1SDimitry Andric   // Returns whether novel exprloc addrx+offset encodings should be used to
770fe6060f1SDimitry Andric   // reduce debug_addr size.
771fe6060f1SDimitry Andric   bool useAddrOffsetExpressions() const {
772fe6060f1SDimitry Andric     return MinimizeAddr == MinimizeAddrInV5::Expressions;
773fe6060f1SDimitry Andric   }
774fe6060f1SDimitry Andric 
775fe6060f1SDimitry Andric   // Returns whether addrx+offset LLVM extension form should be used to reduce
776fe6060f1SDimitry Andric   // debug_addr size.
777fe6060f1SDimitry Andric   bool useAddrOffsetForm() const {
778fe6060f1SDimitry Andric     return MinimizeAddr == MinimizeAddrInV5::Form;
779fe6060f1SDimitry Andric   }
780fe6060f1SDimitry Andric 
7810b57cec5SDimitry Andric   /// Returns whether to use sections as labels rather than temp symbols.
7820b57cec5SDimitry Andric   bool useSectionsAsReferences() const {
7830b57cec5SDimitry Andric     return UseSectionsAsReferences;
7840b57cec5SDimitry Andric   }
7850b57cec5SDimitry Andric 
7860b57cec5SDimitry Andric   /// Returns whether .debug_loc section should be emitted.
7870b57cec5SDimitry Andric   bool useLocSection() const { return UseLocSection; }
7880b57cec5SDimitry Andric 
7890b57cec5SDimitry Andric   /// Returns whether to generate DWARF v4 type units.
7900b57cec5SDimitry Andric   bool generateTypeUnits() const { return GenerateTypeUnits; }
7910b57cec5SDimitry Andric 
7920b57cec5SDimitry Andric   // Experimental DWARF5 features.
7930b57cec5SDimitry Andric 
7940b57cec5SDimitry Andric   /// Returns what kind (if any) of accelerator tables to emit.
7950b57cec5SDimitry Andric   AccelTableKind getAccelTableKind() const { return TheAccelTableKind; }
7960b57cec5SDimitry Andric 
797*5f757f3fSDimitry Andric   /// Seet TheAccelTableKind
798*5f757f3fSDimitry Andric   void setTheAccelTableKind(AccelTableKind K) { TheAccelTableKind = K; };
799*5f757f3fSDimitry Andric 
8000b57cec5SDimitry Andric   bool useAppleExtensionAttributes() const {
8010b57cec5SDimitry Andric     return HasAppleExtensionAttributes;
8020b57cec5SDimitry Andric   }
8030b57cec5SDimitry Andric 
8040b57cec5SDimitry Andric   /// Returns whether or not to change the current debug info for the
8050b57cec5SDimitry Andric   /// split dwarf proposal support.
8060b57cec5SDimitry Andric   bool useSplitDwarf() const { return HasSplitDwarf; }
8070b57cec5SDimitry Andric 
8080b57cec5SDimitry Andric   /// Returns whether to generate a string offsets table with (possibly shared)
8090b57cec5SDimitry Andric   /// contributions from each CU and type unit. This implies the use of
8100b57cec5SDimitry Andric   /// DW_FORM_strx* indirect references with DWARF v5 and beyond. Note that
8110b57cec5SDimitry Andric   /// DW_FORM_GNU_str_index is also an indirect reference, but it is used with
8120b57cec5SDimitry Andric   /// a pre-DWARF v5 implementation of split DWARF sections, which uses a
8130b57cec5SDimitry Andric   /// monolithic string offsets table.
8140b57cec5SDimitry Andric   bool useSegmentedStringOffsetsTable() const {
8150b57cec5SDimitry Andric     return UseSegmentedStringOffsetsTable;
8160b57cec5SDimitry Andric   }
8170b57cec5SDimitry Andric 
8185ffd83dbSDimitry Andric   bool emitDebugEntryValues() const {
8195ffd83dbSDimitry Andric     return EmitDebugEntryValues;
8205ffd83dbSDimitry Andric   }
8215ffd83dbSDimitry Andric 
822e8d8bef9SDimitry Andric   bool useOpConvert() const {
823e8d8bef9SDimitry Andric     return EnableOpConvert;
824e8d8bef9SDimitry Andric   }
825e8d8bef9SDimitry Andric 
8260b57cec5SDimitry Andric   bool shareAcrossDWOCUs() const;
8270b57cec5SDimitry Andric 
8280b57cec5SDimitry Andric   /// Returns the Dwarf Version.
8290b57cec5SDimitry Andric   uint16_t getDwarfVersion() const;
8300b57cec5SDimitry Andric 
831e8d8bef9SDimitry Andric   /// Returns a suitable DWARF form to represent a section offset, i.e.
832e8d8bef9SDimitry Andric   /// * DW_FORM_sec_offset for DWARF version >= 4;
833e8d8bef9SDimitry Andric   /// * DW_FORM_data8 for 64-bit DWARFv3;
834e8d8bef9SDimitry Andric   /// * DW_FORM_data4 for 32-bit DWARFv3 and DWARFv2.
835e8d8bef9SDimitry Andric   dwarf::Form getDwarfSectionOffsetForm() const;
836e8d8bef9SDimitry Andric 
8370b57cec5SDimitry Andric   /// Returns the previous CU that was being updated
8380b57cec5SDimitry Andric   const DwarfCompileUnit *getPrevCU() const { return PrevCU; }
8390b57cec5SDimitry Andric   void setPrevCU(const DwarfCompileUnit *PrevCU) { this->PrevCU = PrevCU; }
8400b57cec5SDimitry Andric 
841349cc55cSDimitry Andric   /// Terminate the line table by adding the last range label.
842349cc55cSDimitry Andric   void terminateLineTable(const DwarfCompileUnit *CU);
843349cc55cSDimitry Andric 
8440b57cec5SDimitry Andric   /// Returns the entries for the .debug_loc section.
8450b57cec5SDimitry Andric   const DebugLocStream &getDebugLocs() const { return DebugLocs; }
8460b57cec5SDimitry Andric 
8470b57cec5SDimitry Andric   /// Emit an entry for the debug loc section. This can be used to
8480b57cec5SDimitry Andric   /// handle an entry that's going to be emitted into the debug loc section.
8490b57cec5SDimitry Andric   void emitDebugLocEntry(ByteStreamer &Streamer,
8500b57cec5SDimitry Andric                          const DebugLocStream::Entry &Entry,
8510b57cec5SDimitry Andric                          const DwarfCompileUnit *CU);
8520b57cec5SDimitry Andric 
8530b57cec5SDimitry Andric   /// Emit the location for a debug loc entry, including the size header.
8540b57cec5SDimitry Andric   void emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry,
8550b57cec5SDimitry Andric                                  const DwarfCompileUnit *CU);
8560b57cec5SDimitry Andric 
857*5f757f3fSDimitry Andric   void addSubprogramNames(const DwarfUnit &Unit,
858*5f757f3fSDimitry Andric                           const DICompileUnit::DebugNameTableKind NameTableKind,
859*5f757f3fSDimitry Andric                           const DISubprogram *SP, DIE &Die);
8600b57cec5SDimitry Andric 
8610b57cec5SDimitry Andric   AddressPool &getAddressPool() { return AddrPool; }
8620b57cec5SDimitry Andric 
863*5f757f3fSDimitry Andric   void addAccelName(const DwarfUnit &Unit,
864*5f757f3fSDimitry Andric                     const DICompileUnit::DebugNameTableKind NameTableKind,
865*5f757f3fSDimitry Andric                     StringRef Name, const DIE &Die);
8660b57cec5SDimitry Andric 
867*5f757f3fSDimitry Andric   void addAccelObjC(const DwarfUnit &Unit,
868*5f757f3fSDimitry Andric                     const DICompileUnit::DebugNameTableKind NameTableKind,
869*5f757f3fSDimitry Andric                     StringRef Name, const DIE &Die);
8700b57cec5SDimitry Andric 
871*5f757f3fSDimitry Andric   void addAccelNamespace(const DwarfUnit &Unit,
872*5f757f3fSDimitry Andric                          const DICompileUnit::DebugNameTableKind NameTableKind,
873*5f757f3fSDimitry Andric                          StringRef Name, const DIE &Die);
8740b57cec5SDimitry Andric 
875*5f757f3fSDimitry Andric   void addAccelType(const DwarfUnit &Unit,
876*5f757f3fSDimitry Andric                     const DICompileUnit::DebugNameTableKind NameTableKind,
877*5f757f3fSDimitry Andric                     StringRef Name, const DIE &Die, char Flags);
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric   const MachineFunction *getCurrentFunction() const { return CurFn; }
8800b57cec5SDimitry Andric 
8810b57cec5SDimitry Andric   /// A helper function to check whether the DIE for a given Scope is
8820b57cec5SDimitry Andric   /// going to be null.
8830b57cec5SDimitry Andric   bool isLexicalScopeDIENull(LexicalScope *Scope);
8840b57cec5SDimitry Andric 
8850b57cec5SDimitry Andric   /// Find the matching DwarfCompileUnit for the given CU DIE.
8860b57cec5SDimitry Andric   DwarfCompileUnit *lookupCU(const DIE *Die) { return CUDieMap.lookup(Die); }
8870b57cec5SDimitry Andric   const DwarfCompileUnit *lookupCU(const DIE *Die) const {
8880b57cec5SDimitry Andric     return CUDieMap.lookup(Die);
8890b57cec5SDimitry Andric   }
8900b57cec5SDimitry Andric 
891e8d8bef9SDimitry Andric   unsigned getStringTypeLoc(const DIStringType *ST) const {
892e8d8bef9SDimitry Andric     return StringTypeLocMap.lookup(ST);
893e8d8bef9SDimitry Andric   }
894e8d8bef9SDimitry Andric 
895e8d8bef9SDimitry Andric   void addStringTypeLoc(const DIStringType *ST, unsigned Loc) {
896e8d8bef9SDimitry Andric     assert(ST);
897e8d8bef9SDimitry Andric     if (Loc)
898e8d8bef9SDimitry Andric       StringTypeLocMap[ST] = Loc;
899e8d8bef9SDimitry Andric   }
900e8d8bef9SDimitry Andric 
9010b57cec5SDimitry Andric   /// \defgroup DebuggerTuning Predicates to tune DWARF for a given debugger.
9020b57cec5SDimitry Andric   ///
9030b57cec5SDimitry Andric   /// Returns whether we are "tuning" for a given debugger.
9040b57cec5SDimitry Andric   /// @{
9050b57cec5SDimitry Andric   bool tuneForGDB() const { return DebuggerTuning == DebuggerKind::GDB; }
9060b57cec5SDimitry Andric   bool tuneForLLDB() const { return DebuggerTuning == DebuggerKind::LLDB; }
9070b57cec5SDimitry Andric   bool tuneForSCE() const { return DebuggerTuning == DebuggerKind::SCE; }
908fe6060f1SDimitry Andric   bool tuneForDBX() const { return DebuggerTuning == DebuggerKind::DBX; }
9090b57cec5SDimitry Andric   /// @}
9100b57cec5SDimitry Andric 
9110b57cec5SDimitry Andric   const MCSymbol *getSectionLabel(const MCSection *S);
9125ffd83dbSDimitry Andric   void insertSectionLabel(const MCSymbol *S);
9130b57cec5SDimitry Andric 
9140b57cec5SDimitry Andric   static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
9150b57cec5SDimitry Andric                                 const DbgValueLoc &Value,
9160b57cec5SDimitry Andric                                 DwarfExpression &DwarfExpr);
917e8d8bef9SDimitry Andric 
918e8d8bef9SDimitry Andric   /// If the \p File has an MD5 checksum, return it as an MD5Result
919e8d8bef9SDimitry Andric   /// allocated in the MCContext.
920bdd1243dSDimitry Andric   std::optional<MD5::MD5Result> getMD5AsBytes(const DIFile *File) const;
92106c3fb27SDimitry Andric 
92206c3fb27SDimitry Andric   MDNodeSet &getLocalDeclsForScope(const DILocalScope *S) {
92306c3fb27SDimitry Andric     return LocalDeclsPerLS[S];
92406c3fb27SDimitry Andric   }
925*5f757f3fSDimitry Andric 
926*5f757f3fSDimitry Andric   /// Sets the current DWARF5AccelTable to use.
927*5f757f3fSDimitry Andric   void setCurrentDWARF5AccelTable(const DWARF5AccelTableKind Kind) {
928*5f757f3fSDimitry Andric     switch (Kind) {
929*5f757f3fSDimitry Andric     case DWARF5AccelTableKind::CU:
930*5f757f3fSDimitry Andric       CurrentDebugNames = &AccelDebugNames;
931*5f757f3fSDimitry Andric       break;
932*5f757f3fSDimitry Andric     case DWARF5AccelTableKind::TU:
933*5f757f3fSDimitry Andric       CurrentDebugNames = &AccelTypeUnitsDebugNames;
934*5f757f3fSDimitry Andric     }
935*5f757f3fSDimitry Andric   }
936*5f757f3fSDimitry Andric   /// Returns either CU or TU DWARF5AccelTable.
937*5f757f3fSDimitry Andric   DWARF5AccelTable &getCurrentDWARF5AccelTable() { return *CurrentDebugNames; }
9380b57cec5SDimitry Andric };
9390b57cec5SDimitry Andric 
9400b57cec5SDimitry Andric } // end namespace llvm
9410b57cec5SDimitry Andric 
9420b57cec5SDimitry Andric #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFDEBUG_H
943