109467b48Spatrick //===- llvm/CodeGen/DwarfFile.h - Dwarf Debug Framework ---------*- C++ -*-===// 209467b48Spatrick // 309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information. 509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 609467b48Spatrick // 709467b48Spatrick //===----------------------------------------------------------------------===// 809467b48Spatrick 909467b48Spatrick #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H 1009467b48Spatrick #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H 1109467b48Spatrick 1209467b48Spatrick #include "DwarfStringPool.h" 1309467b48Spatrick #include "llvm/ADT/DenseMap.h" 1409467b48Spatrick #include "llvm/ADT/SmallVector.h" 1509467b48Spatrick #include "llvm/ADT/StringRef.h" 1609467b48Spatrick #include "llvm/CodeGen/DIE.h" 1709467b48Spatrick #include "llvm/Support/Allocator.h" 1809467b48Spatrick #include <map> 1909467b48Spatrick #include <memory> 2009467b48Spatrick #include <utility> 2109467b48Spatrick 2209467b48Spatrick namespace llvm { 2309467b48Spatrick 2409467b48Spatrick class AsmPrinter; 2509467b48Spatrick class DbgEntity; 2609467b48Spatrick class DbgVariable; 2709467b48Spatrick class DbgLabel; 28*73471bf0Spatrick class DINode; 2909467b48Spatrick class DwarfCompileUnit; 3009467b48Spatrick class DwarfUnit; 3109467b48Spatrick class LexicalScope; 3209467b48Spatrick class MCSection; 33*73471bf0Spatrick class MDNode; 3409467b48Spatrick 3509467b48Spatrick // Data structure to hold a range for range lists. 3609467b48Spatrick struct RangeSpan { 3709467b48Spatrick const MCSymbol *Begin; 3809467b48Spatrick const MCSymbol *End; 3909467b48Spatrick }; 4009467b48Spatrick 4109467b48Spatrick struct RangeSpanList { 4209467b48Spatrick // Index for locating within the debug_range section this particular span. 4309467b48Spatrick MCSymbol *Label; 4409467b48Spatrick const DwarfCompileUnit *CU; 4509467b48Spatrick // List of ranges. 4609467b48Spatrick SmallVector<RangeSpan, 2> Ranges; 4709467b48Spatrick }; 4809467b48Spatrick 4909467b48Spatrick class DwarfFile { 5009467b48Spatrick // Target of Dwarf emission, used for sizing of abbreviations. 5109467b48Spatrick AsmPrinter *Asm; 5209467b48Spatrick 5309467b48Spatrick BumpPtrAllocator AbbrevAllocator; 5409467b48Spatrick 5509467b48Spatrick // Used to uniquely define abbreviations. 5609467b48Spatrick DIEAbbrevSet Abbrevs; 5709467b48Spatrick 5809467b48Spatrick // A pointer to all units in the section. 5909467b48Spatrick SmallVector<std::unique_ptr<DwarfCompileUnit>, 1> CUs; 6009467b48Spatrick 6109467b48Spatrick DwarfStringPool StrPool; 6209467b48Spatrick 6309467b48Spatrick // List of range lists for a given compile unit, separate from the ranges for 6409467b48Spatrick // the CU itself. 6509467b48Spatrick SmallVector<RangeSpanList, 1> CURangeLists; 6609467b48Spatrick 6709467b48Spatrick /// DWARF v5: The symbol that designates the start of the contribution to 6809467b48Spatrick /// the string offsets table. The contribution is shared by all units. 6909467b48Spatrick MCSymbol *StringOffsetsStartSym = nullptr; 7009467b48Spatrick 7109467b48Spatrick /// DWARF v5: The symbol that designates the base of the range list table. 7209467b48Spatrick /// The table is shared by all units. 7309467b48Spatrick MCSymbol *RnglistsTableBaseSym = nullptr; 7409467b48Spatrick 7509467b48Spatrick /// The variables of a lexical scope. 7609467b48Spatrick struct ScopeVars { 7709467b48Spatrick /// We need to sort Args by ArgNo and check for duplicates. This could also 7809467b48Spatrick /// be implemented as a list or vector + std::lower_bound(). 7909467b48Spatrick std::map<unsigned, DbgVariable *> Args; 8009467b48Spatrick SmallVector<DbgVariable *, 8> Locals; 8109467b48Spatrick }; 8209467b48Spatrick /// Collection of DbgVariables of each lexical scope. 8309467b48Spatrick DenseMap<LexicalScope *, ScopeVars> ScopeVariables; 8409467b48Spatrick 8509467b48Spatrick /// Collection of DbgLabels of each lexical scope. 8609467b48Spatrick using LabelList = SmallVector<DbgLabel *, 4>; 8709467b48Spatrick DenseMap<LexicalScope *, LabelList> ScopeLabels; 8809467b48Spatrick 8909467b48Spatrick // Collection of abstract subprogram DIEs. 9009467b48Spatrick DenseMap<const MDNode *, DIE *> AbstractSPDies; 9109467b48Spatrick DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities; 9209467b48Spatrick 9309467b48Spatrick /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can 9409467b48Spatrick /// be shared across CUs, that is why we keep the map here instead 9509467b48Spatrick /// of in DwarfCompileUnit. 9609467b48Spatrick DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap; 9709467b48Spatrick 9809467b48Spatrick public: 9909467b48Spatrick DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA); 10009467b48Spatrick getUnits()10109467b48Spatrick const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() { 10209467b48Spatrick return CUs; 10309467b48Spatrick } 10409467b48Spatrick 10509467b48Spatrick std::pair<uint32_t, RangeSpanList *> addRange(const DwarfCompileUnit &CU, 10609467b48Spatrick SmallVector<RangeSpan, 2> R); 10709467b48Spatrick 10809467b48Spatrick /// getRangeLists - Get the vector of range lists. getRangeLists()10909467b48Spatrick const SmallVectorImpl<RangeSpanList> &getRangeLists() const { 11009467b48Spatrick return CURangeLists; 11109467b48Spatrick } 11209467b48Spatrick 11309467b48Spatrick /// Compute the size and offset of a DIE given an incoming Offset. 11409467b48Spatrick unsigned computeSizeAndOffset(DIE &Die, unsigned Offset); 11509467b48Spatrick 11609467b48Spatrick /// Compute the size and offset of all the DIEs. 11709467b48Spatrick void computeSizeAndOffsets(); 11809467b48Spatrick 11909467b48Spatrick /// Compute the size and offset of all the DIEs in the given unit. 12009467b48Spatrick /// \returns The size of the root DIE. 12109467b48Spatrick unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU); 12209467b48Spatrick 12309467b48Spatrick /// Add a unit to the list of CUs. 12409467b48Spatrick void addUnit(std::unique_ptr<DwarfCompileUnit> U); 12509467b48Spatrick 12609467b48Spatrick /// Emit all of the units to the section listed with the given 12709467b48Spatrick /// abbreviation section. 12809467b48Spatrick void emitUnits(bool UseOffsets); 12909467b48Spatrick 13009467b48Spatrick /// Emit the given unit to its section. 13109467b48Spatrick void emitUnit(DwarfUnit *TheU, bool UseOffsets); 13209467b48Spatrick 13309467b48Spatrick /// Emit a set of abbreviations to the specific section. 13409467b48Spatrick void emitAbbrevs(MCSection *); 13509467b48Spatrick 13609467b48Spatrick /// Emit all of the strings to the section given. If OffsetSection is 13709467b48Spatrick /// non-null, emit a table of string offsets to it. If UseRelativeOffsets 13809467b48Spatrick /// is false, emit absolute offsets to the strings. Otherwise, emit 13909467b48Spatrick /// relocatable references to the strings if they are supported by the target. 14009467b48Spatrick void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr, 14109467b48Spatrick bool UseRelativeOffsets = false); 14209467b48Spatrick 14309467b48Spatrick /// Returns the string pool. getStringPool()14409467b48Spatrick DwarfStringPool &getStringPool() { return StrPool; } 14509467b48Spatrick getStringOffsetsStartSym()14609467b48Spatrick MCSymbol *getStringOffsetsStartSym() const { return StringOffsetsStartSym; } setStringOffsetsStartSym(MCSymbol * Sym)14709467b48Spatrick void setStringOffsetsStartSym(MCSymbol *Sym) { StringOffsetsStartSym = Sym; } 14809467b48Spatrick getRnglistsTableBaseSym()14909467b48Spatrick MCSymbol *getRnglistsTableBaseSym() const { return RnglistsTableBaseSym; } setRnglistsTableBaseSym(MCSymbol * Sym)15009467b48Spatrick void setRnglistsTableBaseSym(MCSymbol *Sym) { RnglistsTableBaseSym = Sym; } 15109467b48Spatrick 15209467b48Spatrick /// \returns false if the variable was merged with a previous one. 15309467b48Spatrick bool addScopeVariable(LexicalScope *LS, DbgVariable *Var); 15409467b48Spatrick 15509467b48Spatrick void addScopeLabel(LexicalScope *LS, DbgLabel *Label); 15609467b48Spatrick getScopeVariables()15709467b48Spatrick DenseMap<LexicalScope *, ScopeVars> &getScopeVariables() { 15809467b48Spatrick return ScopeVariables; 15909467b48Spatrick } 16009467b48Spatrick getScopeLabels()16109467b48Spatrick DenseMap<LexicalScope *, LabelList> &getScopeLabels() { 16209467b48Spatrick return ScopeLabels; 16309467b48Spatrick } 16409467b48Spatrick getAbstractSPDies()16509467b48Spatrick DenseMap<const MDNode *, DIE *> &getAbstractSPDies() { 16609467b48Spatrick return AbstractSPDies; 16709467b48Spatrick } 16809467b48Spatrick getAbstractEntities()16909467b48Spatrick DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() { 17009467b48Spatrick return AbstractEntities; 17109467b48Spatrick } 17209467b48Spatrick insertDIE(const MDNode * TypeMD,DIE * Die)17309467b48Spatrick void insertDIE(const MDNode *TypeMD, DIE *Die) { 17409467b48Spatrick DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die)); 17509467b48Spatrick } 17609467b48Spatrick getDIE(const MDNode * TypeMD)17709467b48Spatrick DIE *getDIE(const MDNode *TypeMD) { 17809467b48Spatrick return DITypeNodeToDieMap.lookup(TypeMD); 17909467b48Spatrick } 18009467b48Spatrick }; 18109467b48Spatrick 18209467b48Spatrick } // end namespace llvm 18309467b48Spatrick 18409467b48Spatrick #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H 185