1*9880d681SAndroid Build Coastguard Worker //===-- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit ---*- C++ -*--===// 2*9880d681SAndroid Build Coastguard Worker // 3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure 4*9880d681SAndroid Build Coastguard Worker // 5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source 6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details. 7*9880d681SAndroid Build Coastguard Worker // 8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 9*9880d681SAndroid Build Coastguard Worker // 10*9880d681SAndroid Build Coastguard Worker // This file contains support for writing dwarf compile unit. 11*9880d681SAndroid Build Coastguard Worker // 12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 13*9880d681SAndroid Build Coastguard Worker 14*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H 15*9880d681SAndroid Build Coastguard Worker #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H 16*9880d681SAndroid Build Coastguard Worker 17*9880d681SAndroid Build Coastguard Worker #include "DwarfUnit.h" 18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DebugInfo.h" 19*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Dwarf.h" 20*9880d681SAndroid Build Coastguard Worker 21*9880d681SAndroid Build Coastguard Worker namespace llvm { 22*9880d681SAndroid Build Coastguard Worker 23*9880d681SAndroid Build Coastguard Worker class StringRef; 24*9880d681SAndroid Build Coastguard Worker class AsmPrinter; 25*9880d681SAndroid Build Coastguard Worker class DIE; 26*9880d681SAndroid Build Coastguard Worker class DwarfDebug; 27*9880d681SAndroid Build Coastguard Worker class DwarfFile; 28*9880d681SAndroid Build Coastguard Worker class MCSymbol; 29*9880d681SAndroid Build Coastguard Worker class LexicalScope; 30*9880d681SAndroid Build Coastguard Worker 31*9880d681SAndroid Build Coastguard Worker class DwarfCompileUnit : public DwarfUnit { 32*9880d681SAndroid Build Coastguard Worker /// A numeric ID unique among all CUs in the module 33*9880d681SAndroid Build Coastguard Worker unsigned UniqueID; 34*9880d681SAndroid Build Coastguard Worker 35*9880d681SAndroid Build Coastguard Worker /// Offset of the UnitDie from beginning of debug info section. 36*9880d681SAndroid Build Coastguard Worker unsigned DebugInfoOffset = 0; 37*9880d681SAndroid Build Coastguard Worker 38*9880d681SAndroid Build Coastguard Worker /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding 39*9880d681SAndroid Build Coastguard Worker /// the need to search for it in applyStmtList. 40*9880d681SAndroid Build Coastguard Worker DIE::value_iterator StmtListValue; 41*9880d681SAndroid Build Coastguard Worker 42*9880d681SAndroid Build Coastguard Worker /// Skeleton unit associated with this unit. 43*9880d681SAndroid Build Coastguard Worker DwarfCompileUnit *Skeleton; 44*9880d681SAndroid Build Coastguard Worker 45*9880d681SAndroid Build Coastguard Worker /// The start of the unit within its section. 46*9880d681SAndroid Build Coastguard Worker MCSymbol *LabelBegin; 47*9880d681SAndroid Build Coastguard Worker 48*9880d681SAndroid Build Coastguard Worker /// The start of the unit macro info within macro section. 49*9880d681SAndroid Build Coastguard Worker MCSymbol *MacroLabelBegin; 50*9880d681SAndroid Build Coastguard Worker 51*9880d681SAndroid Build Coastguard Worker typedef llvm::SmallVector<const MDNode *, 8> ImportedEntityList; 52*9880d681SAndroid Build Coastguard Worker typedef llvm::DenseMap<const MDNode *, ImportedEntityList> 53*9880d681SAndroid Build Coastguard Worker ImportedEntityMap; 54*9880d681SAndroid Build Coastguard Worker 55*9880d681SAndroid Build Coastguard Worker ImportedEntityMap ImportedEntities; 56*9880d681SAndroid Build Coastguard Worker 57*9880d681SAndroid Build Coastguard Worker /// GlobalNames - A map of globally visible named entities for this unit. 58*9880d681SAndroid Build Coastguard Worker StringMap<const DIE *> GlobalNames; 59*9880d681SAndroid Build Coastguard Worker 60*9880d681SAndroid Build Coastguard Worker /// GlobalTypes - A map of globally visible types for this unit. 61*9880d681SAndroid Build Coastguard Worker StringMap<const DIE *> GlobalTypes; 62*9880d681SAndroid Build Coastguard Worker 63*9880d681SAndroid Build Coastguard Worker // List of range lists for a given compile unit, separate from the ranges for 64*9880d681SAndroid Build Coastguard Worker // the CU itself. 65*9880d681SAndroid Build Coastguard Worker SmallVector<RangeSpanList, 1> CURangeLists; 66*9880d681SAndroid Build Coastguard Worker 67*9880d681SAndroid Build Coastguard Worker // List of ranges for a given compile unit. 68*9880d681SAndroid Build Coastguard Worker SmallVector<RangeSpan, 2> CURanges; 69*9880d681SAndroid Build Coastguard Worker 70*9880d681SAndroid Build Coastguard Worker // The base address of this unit, if any. Used for relative references in 71*9880d681SAndroid Build Coastguard Worker // ranges/locs. 72*9880d681SAndroid Build Coastguard Worker const MCSymbol *BaseAddress; 73*9880d681SAndroid Build Coastguard Worker 74*9880d681SAndroid Build Coastguard Worker /// \brief Construct a DIE for the given DbgVariable without initializing the 75*9880d681SAndroid Build Coastguard Worker /// DbgVariable's DIE reference. 76*9880d681SAndroid Build Coastguard Worker DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract); 77*9880d681SAndroid Build Coastguard Worker 78*9880d681SAndroid Build Coastguard Worker bool isDwoUnit() const override; 79*9880d681SAndroid Build Coastguard Worker 80*9880d681SAndroid Build Coastguard Worker bool includeMinimalInlineScopes() const; 81*9880d681SAndroid Build Coastguard Worker 82*9880d681SAndroid Build Coastguard Worker public: 83*9880d681SAndroid Build Coastguard Worker DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A, 84*9880d681SAndroid Build Coastguard Worker DwarfDebug *DW, DwarfFile *DWU); 85*9880d681SAndroid Build Coastguard Worker getUniqueID()86*9880d681SAndroid Build Coastguard Worker unsigned getUniqueID() const { return UniqueID; } getDebugInfoOffset()87*9880d681SAndroid Build Coastguard Worker unsigned getDebugInfoOffset() const { return DebugInfoOffset; } setDebugInfoOffset(unsigned DbgInfoOff)88*9880d681SAndroid Build Coastguard Worker void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; } 89*9880d681SAndroid Build Coastguard Worker getSkeleton()90*9880d681SAndroid Build Coastguard Worker DwarfCompileUnit *getSkeleton() const { 91*9880d681SAndroid Build Coastguard Worker return Skeleton; 92*9880d681SAndroid Build Coastguard Worker } 93*9880d681SAndroid Build Coastguard Worker 94*9880d681SAndroid Build Coastguard Worker void initStmtList(); 95*9880d681SAndroid Build Coastguard Worker 96*9880d681SAndroid Build Coastguard Worker /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE. 97*9880d681SAndroid Build Coastguard Worker void applyStmtList(DIE &D); 98*9880d681SAndroid Build Coastguard Worker 99*9880d681SAndroid Build Coastguard Worker /// getOrCreateGlobalVariableDIE - get or create global variable DIE. 100*9880d681SAndroid Build Coastguard Worker DIE *getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV); 101*9880d681SAndroid Build Coastguard Worker 102*9880d681SAndroid Build Coastguard Worker /// addLabelAddress - Add a dwarf label attribute data and value using 103*9880d681SAndroid Build Coastguard Worker /// either DW_FORM_addr or DW_FORM_GNU_addr_index. 104*9880d681SAndroid Build Coastguard Worker void addLabelAddress(DIE &Die, dwarf::Attribute Attribute, 105*9880d681SAndroid Build Coastguard Worker const MCSymbol *Label); 106*9880d681SAndroid Build Coastguard Worker 107*9880d681SAndroid Build Coastguard Worker /// addLocalLabelAddress - Add a dwarf label attribute data and value using 108*9880d681SAndroid Build Coastguard Worker /// DW_FORM_addr only. 109*9880d681SAndroid Build Coastguard Worker void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute, 110*9880d681SAndroid Build Coastguard Worker const MCSymbol *Label); 111*9880d681SAndroid Build Coastguard Worker 112*9880d681SAndroid Build Coastguard Worker /// addSectionDelta - Add a label delta attribute data and value. 113*9880d681SAndroid Build Coastguard Worker DIE::value_iterator addSectionDelta(DIE &Die, dwarf::Attribute Attribute, 114*9880d681SAndroid Build Coastguard Worker const MCSymbol *Hi, const MCSymbol *Lo); 115*9880d681SAndroid Build Coastguard Worker getCU()116*9880d681SAndroid Build Coastguard Worker DwarfCompileUnit &getCU() override { return *this; } 117*9880d681SAndroid Build Coastguard Worker 118*9880d681SAndroid Build Coastguard Worker unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override; 119*9880d681SAndroid Build Coastguard Worker addImportedEntity(const DIImportedEntity * IE)120*9880d681SAndroid Build Coastguard Worker void addImportedEntity(const DIImportedEntity* IE) { 121*9880d681SAndroid Build Coastguard Worker DIScope *Scope = IE->getScope(); 122*9880d681SAndroid Build Coastguard Worker assert(Scope && "Invalid Scope encoding!"); 123*9880d681SAndroid Build Coastguard Worker if (!isa<DILocalScope>(Scope)) 124*9880d681SAndroid Build Coastguard Worker // No need to add imported enities that are not local declaration. 125*9880d681SAndroid Build Coastguard Worker return; 126*9880d681SAndroid Build Coastguard Worker 127*9880d681SAndroid Build Coastguard Worker auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope(); 128*9880d681SAndroid Build Coastguard Worker ImportedEntities[LocalScope].push_back(IE); 129*9880d681SAndroid Build Coastguard Worker } 130*9880d681SAndroid Build Coastguard Worker 131*9880d681SAndroid Build Coastguard Worker /// addRange - Add an address range to the list of ranges for this unit. 132*9880d681SAndroid Build Coastguard Worker void addRange(RangeSpan Range); 133*9880d681SAndroid Build Coastguard Worker 134*9880d681SAndroid Build Coastguard Worker void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End); 135*9880d681SAndroid Build Coastguard Worker 136*9880d681SAndroid Build Coastguard Worker /// addSectionLabel - Add a Dwarf section label attribute data and value. 137*9880d681SAndroid Build Coastguard Worker /// 138*9880d681SAndroid Build Coastguard Worker DIE::value_iterator addSectionLabel(DIE &Die, dwarf::Attribute Attribute, 139*9880d681SAndroid Build Coastguard Worker const MCSymbol *Label, 140*9880d681SAndroid Build Coastguard Worker const MCSymbol *Sec); 141*9880d681SAndroid Build Coastguard Worker 142*9880d681SAndroid Build Coastguard Worker /// \brief Find DIE for the given subprogram and attach appropriate 143*9880d681SAndroid Build Coastguard Worker /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global 144*9880d681SAndroid Build Coastguard Worker /// variables in this scope then create and insert DIEs for these 145*9880d681SAndroid Build Coastguard Worker /// variables. 146*9880d681SAndroid Build Coastguard Worker DIE &updateSubprogramScopeDIE(const DISubprogram *SP); 147*9880d681SAndroid Build Coastguard Worker 148*9880d681SAndroid Build Coastguard Worker void constructScopeDIE(LexicalScope *Scope, 149*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<DIE *> &FinalChildren); 150*9880d681SAndroid Build Coastguard Worker 151*9880d681SAndroid Build Coastguard Worker /// \brief A helper function to construct a RangeSpanList for a given 152*9880d681SAndroid Build Coastguard Worker /// lexical scope. 153*9880d681SAndroid Build Coastguard Worker void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range); 154*9880d681SAndroid Build Coastguard Worker 155*9880d681SAndroid Build Coastguard Worker void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges); 156*9880d681SAndroid Build Coastguard Worker 157*9880d681SAndroid Build Coastguard Worker void attachRangesOrLowHighPC(DIE &D, 158*9880d681SAndroid Build Coastguard Worker const SmallVectorImpl<InsnRange> &Ranges); 159*9880d681SAndroid Build Coastguard Worker /// \brief This scope represents inlined body of a function. Construct 160*9880d681SAndroid Build Coastguard Worker /// DIE to represent this concrete inlined copy of the function. 161*9880d681SAndroid Build Coastguard Worker DIE *constructInlinedScopeDIE(LexicalScope *Scope); 162*9880d681SAndroid Build Coastguard Worker 163*9880d681SAndroid Build Coastguard Worker /// \brief Construct new DW_TAG_lexical_block for this scope and 164*9880d681SAndroid Build Coastguard Worker /// attach DW_AT_low_pc/DW_AT_high_pc labels. 165*9880d681SAndroid Build Coastguard Worker DIE *constructLexicalScopeDIE(LexicalScope *Scope); 166*9880d681SAndroid Build Coastguard Worker 167*9880d681SAndroid Build Coastguard Worker /// constructVariableDIE - Construct a DIE for the given DbgVariable. 168*9880d681SAndroid Build Coastguard Worker DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false); 169*9880d681SAndroid Build Coastguard Worker 170*9880d681SAndroid Build Coastguard Worker DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope, 171*9880d681SAndroid Build Coastguard Worker DIE *&ObjectPointer); 172*9880d681SAndroid Build Coastguard Worker 173*9880d681SAndroid Build Coastguard Worker /// A helper function to create children of a Scope DIE. 174*9880d681SAndroid Build Coastguard Worker DIE *createScopeChildrenDIE(LexicalScope *Scope, 175*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<DIE *> &Children, 176*9880d681SAndroid Build Coastguard Worker unsigned *ChildScopeCount = nullptr); 177*9880d681SAndroid Build Coastguard Worker 178*9880d681SAndroid Build Coastguard Worker /// \brief Construct a DIE for this subprogram scope. 179*9880d681SAndroid Build Coastguard Worker void constructSubprogramScopeDIE(LexicalScope *Scope); 180*9880d681SAndroid Build Coastguard Worker 181*9880d681SAndroid Build Coastguard Worker DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE); 182*9880d681SAndroid Build Coastguard Worker 183*9880d681SAndroid Build Coastguard Worker void constructAbstractSubprogramScopeDIE(LexicalScope *Scope); 184*9880d681SAndroid Build Coastguard Worker 185*9880d681SAndroid Build Coastguard Worker /// \brief Construct import_module DIE. 186*9880d681SAndroid Build Coastguard Worker DIE *constructImportedEntityDIE(const DIImportedEntity *Module); 187*9880d681SAndroid Build Coastguard Worker 188*9880d681SAndroid Build Coastguard Worker void finishSubprogramDefinition(const DISubprogram *SP); 189*9880d681SAndroid Build Coastguard Worker 190*9880d681SAndroid Build Coastguard Worker /// Set the skeleton unit associated with this unit. setSkeleton(DwarfCompileUnit & Skel)191*9880d681SAndroid Build Coastguard Worker void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; } 192*9880d681SAndroid Build Coastguard Worker getSectionSym()193*9880d681SAndroid Build Coastguard Worker const MCSymbol *getSectionSym() const { 194*9880d681SAndroid Build Coastguard Worker assert(Section); 195*9880d681SAndroid Build Coastguard Worker return Section->getBeginSymbol(); 196*9880d681SAndroid Build Coastguard Worker } 197*9880d681SAndroid Build Coastguard Worker getLength()198*9880d681SAndroid Build Coastguard Worker unsigned getLength() { 199*9880d681SAndroid Build Coastguard Worker return sizeof(uint32_t) + // Length field 200*9880d681SAndroid Build Coastguard Worker getHeaderSize() + UnitDie.getSize(); 201*9880d681SAndroid Build Coastguard Worker } 202*9880d681SAndroid Build Coastguard Worker 203*9880d681SAndroid Build Coastguard Worker void emitHeader(bool UseOffsets) override; 204*9880d681SAndroid Build Coastguard Worker getLabelBegin()205*9880d681SAndroid Build Coastguard Worker MCSymbol *getLabelBegin() const { 206*9880d681SAndroid Build Coastguard Worker assert(Section); 207*9880d681SAndroid Build Coastguard Worker return LabelBegin; 208*9880d681SAndroid Build Coastguard Worker } 209*9880d681SAndroid Build Coastguard Worker getMacroLabelBegin()210*9880d681SAndroid Build Coastguard Worker MCSymbol *getMacroLabelBegin() const { 211*9880d681SAndroid Build Coastguard Worker return MacroLabelBegin; 212*9880d681SAndroid Build Coastguard Worker } 213*9880d681SAndroid Build Coastguard Worker 214*9880d681SAndroid Build Coastguard Worker /// Add a new global name to the compile unit. 215*9880d681SAndroid Build Coastguard Worker void addGlobalName(StringRef Name, DIE &Die, const DIScope *Context) override; 216*9880d681SAndroid Build Coastguard Worker 217*9880d681SAndroid Build Coastguard Worker /// Add a new global type to the compile unit. 218*9880d681SAndroid Build Coastguard Worker void addGlobalType(const DIType *Ty, const DIE &Die, 219*9880d681SAndroid Build Coastguard Worker const DIScope *Context) override; 220*9880d681SAndroid Build Coastguard Worker getGlobalNames()221*9880d681SAndroid Build Coastguard Worker const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; } getGlobalTypes()222*9880d681SAndroid Build Coastguard Worker const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; } 223*9880d681SAndroid Build Coastguard Worker 224*9880d681SAndroid Build Coastguard Worker /// Add DW_AT_location attribute for a DbgVariable based on provided 225*9880d681SAndroid Build Coastguard Worker /// MachineLocation. 226*9880d681SAndroid Build Coastguard Worker void addVariableAddress(const DbgVariable &DV, DIE &Die, 227*9880d681SAndroid Build Coastguard Worker MachineLocation Location); 228*9880d681SAndroid Build Coastguard Worker /// Add an address attribute to a die based on the location provided. 229*9880d681SAndroid Build Coastguard Worker void addAddress(DIE &Die, dwarf::Attribute Attribute, 230*9880d681SAndroid Build Coastguard Worker const MachineLocation &Location); 231*9880d681SAndroid Build Coastguard Worker 232*9880d681SAndroid Build Coastguard Worker /// Start with the address based on the location provided, and generate the 233*9880d681SAndroid Build Coastguard Worker /// DWARF information necessary to find the actual variable (navigating the 234*9880d681SAndroid Build Coastguard Worker /// extra location information encoded in the type) based on the starting 235*9880d681SAndroid Build Coastguard Worker /// location. Add the DWARF information to the die. 236*9880d681SAndroid Build Coastguard Worker void addComplexAddress(const DbgVariable &DV, DIE &Die, 237*9880d681SAndroid Build Coastguard Worker dwarf::Attribute Attribute, 238*9880d681SAndroid Build Coastguard Worker const MachineLocation &Location); 239*9880d681SAndroid Build Coastguard Worker 240*9880d681SAndroid Build Coastguard Worker /// Add a Dwarf loclistptr attribute data and value. 241*9880d681SAndroid Build Coastguard Worker void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index); 242*9880d681SAndroid Build Coastguard Worker void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie); 243*9880d681SAndroid Build Coastguard Worker 244*9880d681SAndroid Build Coastguard Worker /// Add a Dwarf expression attribute data and value. 245*9880d681SAndroid Build Coastguard Worker void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr); 246*9880d681SAndroid Build Coastguard Worker 247*9880d681SAndroid Build Coastguard Worker void applySubprogramAttributesToDefinition(const DISubprogram *SP, 248*9880d681SAndroid Build Coastguard Worker DIE &SPDie); 249*9880d681SAndroid Build Coastguard Worker 250*9880d681SAndroid Build Coastguard Worker /// getRangeLists - Get the vector of range lists. getRangeLists()251*9880d681SAndroid Build Coastguard Worker const SmallVectorImpl<RangeSpanList> &getRangeLists() const { 252*9880d681SAndroid Build Coastguard Worker return (Skeleton ? Skeleton : this)->CURangeLists; 253*9880d681SAndroid Build Coastguard Worker } 254*9880d681SAndroid Build Coastguard Worker 255*9880d681SAndroid Build Coastguard Worker /// getRanges - Get the list of ranges for this unit. getRanges()256*9880d681SAndroid Build Coastguard Worker const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; } takeRanges()257*9880d681SAndroid Build Coastguard Worker SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); } 258*9880d681SAndroid Build Coastguard Worker setBaseAddress(const MCSymbol * Base)259*9880d681SAndroid Build Coastguard Worker void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; } getBaseAddress()260*9880d681SAndroid Build Coastguard Worker const MCSymbol *getBaseAddress() const { return BaseAddress; } 261*9880d681SAndroid Build Coastguard Worker }; 262*9880d681SAndroid Build Coastguard Worker 263*9880d681SAndroid Build Coastguard Worker } // end llvm namespace 264*9880d681SAndroid Build Coastguard Worker 265*9880d681SAndroid Build Coastguard Worker #endif 266