1221337Sdim//===-- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit ---*- C++ -*--===// 2221337Sdim// 3221337Sdim// The LLVM Compiler Infrastructure 4221337Sdim// 5221337Sdim// This file is distributed under the University of Illinois Open Source 6221337Sdim// License. See LICENSE.TXT for details. 7221337Sdim// 8221337Sdim//===----------------------------------------------------------------------===// 9221337Sdim// 10221337Sdim// This file contains support for writing dwarf compile unit. 11221337Sdim// 12221337Sdim//===----------------------------------------------------------------------===// 13221337Sdim 14221337Sdim#ifndef CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H 15221337Sdim#define CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H 16221337Sdim 17221337Sdim#include "DIE.h" 18263509Sdim#include "DwarfDebug.h" 19221337Sdim#include "llvm/ADT/DenseMap.h" 20263509Sdim#include "llvm/ADT/Optional.h" 21252723Sdim#include "llvm/ADT/OwningPtr.h" 22221337Sdim#include "llvm/ADT/StringMap.h" 23252723Sdim#include "llvm/DebugInfo.h" 24263509Sdim#include "llvm/MC/MCExpr.h" 25221337Sdim 26221337Sdimnamespace llvm { 27221337Sdim 28221337Sdimclass MachineLocation; 29221337Sdimclass MachineOperand; 30221337Sdimclass ConstantInt; 31252723Sdimclass ConstantFP; 32221337Sdimclass DbgVariable; 33221337Sdim 34221337Sdim//===----------------------------------------------------------------------===// 35235633Sdim/// CompileUnit - This dwarf writer support class manages information associated 36221337Sdim/// with a source file. 37221337Sdimclass CompileUnit { 38252723Sdim /// UniqueID - a numeric ID unique among all CUs in the module 39221337Sdim /// 40252723Sdim unsigned UniqueID; 41221337Sdim 42263509Sdim /// Node - MDNode for the compile unit. 43263509Sdim DICompileUnit Node; 44235633Sdim 45263509Sdim /// CUDie - Compile unit debug information entry. 46221337Sdim /// 47221337Sdim const OwningPtr<DIE> CUDie; 48221337Sdim 49221337Sdim /// Asm - Target of Dwarf emission. 50221337Sdim AsmPrinter *Asm; 51221337Sdim 52252723Sdim // Holders for some common dwarf information. 53221337Sdim DwarfDebug *DD; 54252723Sdim DwarfUnits *DU; 55221337Sdim 56221337Sdim /// IndexTyDie - An anonymous type for index type. Owned by CUDie. 57221337Sdim DIE *IndexTyDie; 58221337Sdim 59263509Sdim /// MDNodeToDieMap - Tracks the mapping of unit level debug information 60221337Sdim /// variables to debug information entries. 61221337Sdim DenseMap<const MDNode *, DIE *> MDNodeToDieMap; 62221337Sdim 63263509Sdim /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information 64221337Sdim /// descriptors to debug information entries using a DIEEntry proxy. 65221337Sdim DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap; 66221337Sdim 67252723Sdim /// GlobalNames - A map of globally visible named entities for this unit. 68252723Sdim /// 69263509Sdim StringMap<DIE *> GlobalNames; 70252723Sdim 71221337Sdim /// GlobalTypes - A map of globally visible types for this unit. 72221337Sdim /// 73263509Sdim StringMap<DIE *> GlobalTypes; 74221337Sdim 75235633Sdim /// AccelNames - A map of names for the name accelerator table. 76235633Sdim /// 77263509Sdim StringMap<std::vector<DIE *> > AccelNames; 78263509Sdim StringMap<std::vector<DIE *> > AccelObjC; 79263509Sdim StringMap<std::vector<DIE *> > AccelNamespace; 80263509Sdim StringMap<std::vector<std::pair<DIE *, unsigned> > > AccelTypes; 81235633Sdim 82221337Sdim /// DIEBlocks - A list of all the DIEBlocks in use. 83221337Sdim std::vector<DIEBlock *> DIEBlocks; 84221337Sdim 85226890Sdim /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that 86226890Sdim /// need DW_AT_containing_type attribute. This attribute points to a DIE that 87226890Sdim /// corresponds to the MDNode mapped with the subprogram DIE. 88226890Sdim DenseMap<DIE *, const MDNode *> ContainingTypeMap; 89226890Sdim 90263509Sdim // DIEValueAllocator - All DIEValues are allocated through this allocator. 91263509Sdim BumpPtrAllocator DIEValueAllocator; 92252723Sdim 93263509Sdim // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently. 94263509Sdim DIEInteger *DIEIntegerOne; 95252723Sdim 96221337Sdimpublic: 97263509Sdim CompileUnit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A, 98263509Sdim DwarfDebug *DW, DwarfUnits *DWU); 99221337Sdim ~CompileUnit(); 100221337Sdim 101221337Sdim // Accessors. 102263509Sdim unsigned getUniqueID() const { return UniqueID; } 103263509Sdim uint16_t getLanguage() const { return Node.getLanguage(); } 104263509Sdim DICompileUnit getNode() const { return Node; } 105263509Sdim DIE *getCUDie() const { return CUDie.get(); } 106263509Sdim const StringMap<DIE *> &getGlobalNames() const { return GlobalNames; } 107263509Sdim const StringMap<DIE *> &getGlobalTypes() const { return GlobalTypes; } 108221337Sdim 109263509Sdim const StringMap<std::vector<DIE *> > &getAccelNames() const { 110235633Sdim return AccelNames; 111235633Sdim } 112263509Sdim const StringMap<std::vector<DIE *> > &getAccelObjC() const { 113235633Sdim return AccelObjC; 114235633Sdim } 115263509Sdim const StringMap<std::vector<DIE *> > &getAccelNamespace() const { 116235633Sdim return AccelNamespace; 117235633Sdim } 118263509Sdim const StringMap<std::vector<std::pair<DIE *, unsigned> > > & 119263509Sdim getAccelTypes() const { 120235633Sdim return AccelTypes; 121235633Sdim } 122252723Sdim 123263509Sdim unsigned getDebugInfoOffset() const { return DebugInfoOffset; } 124252723Sdim void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; } 125263509Sdim 126221337Sdim /// hasContent - Return true if this compile unit has something to write out. 127221337Sdim /// 128221337Sdim bool hasContent() const { return !CUDie->getChildren().empty(); } 129221337Sdim 130263509Sdim /// getParentContextString - Get a string containing the language specific 131263509Sdim /// context for a global name. 132263509Sdim std::string getParentContextString(DIScope Context) const; 133263509Sdim 134252723Sdim /// addGlobalName - Add a new global entity to the compile unit. 135252723Sdim /// 136263509Sdim void addGlobalName(StringRef Name, DIE *Die, DIScope Context); 137252723Sdim 138221337Sdim /// addGlobalType - Add a new global type to the compile unit. 139221337Sdim /// 140223017Sdim void addGlobalType(DIType Ty); 141221337Sdim 142263509Sdim /// addPubTypes - Add a set of types from the subprogram to the global types. 143263509Sdim void addPubTypes(DISubprogram SP); 144235633Sdim 145235633Sdim /// addAccelName - Add a new name to the name accelerator table. 146263509Sdim void addAccelName(StringRef Name, DIE *Die); 147252723Sdim 148263509Sdim /// addAccelObjC - Add a new name to the ObjC accelerator table. 149263509Sdim void addAccelObjC(StringRef Name, DIE *Die); 150221337Sdim 151263509Sdim /// addAccelNamespace - Add a new name to the namespace accelerator table. 152263509Sdim void addAccelNamespace(StringRef Name, DIE *Die); 153221337Sdim 154263509Sdim /// addAccelType - Add a new type to the type accelerator table. 155263509Sdim void addAccelType(StringRef Name, std::pair<DIE *, unsigned> Die); 156221337Sdim 157263509Sdim /// getDIE - Returns the debug information entry map slot for the 158263509Sdim /// specified debug variable. We delegate the request to DwarfDebug 159263509Sdim /// when the MDNode can be part of the type system, since DIEs for 160263509Sdim /// the type system can be shared across CUs and the mappings are 161263509Sdim /// kept in DwarfDebug. 162263509Sdim DIE *getDIE(DIDescriptor D) const; 163221337Sdim 164263509Sdim DIEBlock *getDIEBlock() { return new (DIEValueAllocator) DIEBlock(); } 165221337Sdim 166263509Sdim /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug 167263509Sdim /// when the MDNode can be part of the type system, since DIEs for 168263509Sdim /// the type system can be shared across CUs and the mappings are 169263509Sdim /// kept in DwarfDebug. 170263509Sdim void insertDIE(DIDescriptor Desc, DIE *D); 171263509Sdim 172221337Sdim /// addDie - Adds or interns the DIE to the compile unit. 173221337Sdim /// 174263509Sdim void addDie(DIE *Buffer) { CUDie->addChild(Buffer); } 175221337Sdim 176245431Sdim /// addFlag - Add a flag that is true to the DIE. 177263509Sdim void addFlag(DIE *Die, dwarf::Attribute Attribute); 178252723Sdim 179221337Sdim /// addUInt - Add an unsigned integer attribute data and value. 180221337Sdim /// 181263509Sdim void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form, 182263509Sdim uint64_t Integer); 183221337Sdim 184263509Sdim void addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer); 185263509Sdim 186221337Sdim /// addSInt - Add an signed integer attribute data and value. 187221337Sdim /// 188263509Sdim void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form, 189263509Sdim int64_t Integer); 190221337Sdim 191263509Sdim void addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, int64_t Integer); 192263509Sdim 193221337Sdim /// addString - Add a string attribute data and value. 194221337Sdim /// 195263509Sdim void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str); 196221337Sdim 197252723Sdim /// addLocalString - Add a string attribute data and value. 198252723Sdim /// 199263509Sdim void addLocalString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str); 200252723Sdim 201263509Sdim /// addExpr - Add a Dwarf expression attribute data and value. 202263509Sdim /// 203263509Sdim void addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr); 204263509Sdim 205221337Sdim /// addLabel - Add a Dwarf label attribute data and value. 206221337Sdim /// 207263509Sdim void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form, 208221337Sdim const MCSymbol *Label); 209221337Sdim 210263509Sdim void addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label); 211263509Sdim 212263509Sdim /// addSectionLabel - Add a Dwarf section label attribute data and value. 213263509Sdim /// 214263509Sdim void addSectionLabel(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Label); 215263509Sdim 216263509Sdim /// addSectionOffset - Add an offset into a section attribute data and value. 217263509Sdim /// 218263509Sdim void addSectionOffset(DIE *Die, dwarf::Attribute Attribute, uint64_t Integer); 219263509Sdim 220252723Sdim /// addLabelAddress - Add a dwarf label attribute data and value using 221252723Sdim /// either DW_FORM_addr or DW_FORM_GNU_addr_index. 222252723Sdim /// 223263509Sdim void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label); 224252723Sdim 225252723Sdim /// addOpAddress - Add a dwarf op address data and value using the 226252723Sdim /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index. 227252723Sdim /// 228263509Sdim void addOpAddress(DIEBlock *Die, const MCSymbol *Label); 229252723Sdim 230263509Sdim /// addSectionDelta - Add a label delta attribute data and value. 231263509Sdim void addSectionDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi, 232263509Sdim const MCSymbol *Lo); 233263509Sdim 234263509Sdim /// addDIEEntry - Add a DIE attribute data and value. 235221337Sdim /// 236263509Sdim void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry); 237221337Sdim 238221337Sdim /// addDIEEntry - Add a DIE attribute data and value. 239221337Sdim /// 240263509Sdim void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry); 241252723Sdim 242221337Sdim /// addBlock - Add block data. 243221337Sdim /// 244263509Sdim void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block); 245221337Sdim 246221337Sdim /// addSourceLine - Add location information to specified debug information 247221337Sdim /// entry. 248221337Sdim void addSourceLine(DIE *Die, DIVariable V); 249221337Sdim void addSourceLine(DIE *Die, DIGlobalVariable G); 250221337Sdim void addSourceLine(DIE *Die, DISubprogram SP); 251221337Sdim void addSourceLine(DIE *Die, DIType Ty); 252221337Sdim void addSourceLine(DIE *Die, DINameSpace NS); 253235633Sdim void addSourceLine(DIE *Die, DIObjCProperty Ty); 254221337Sdim 255221337Sdim /// addAddress - Add an address attribute to a die based on the location 256221337Sdim /// provided. 257263509Sdim void addAddress(DIE *Die, dwarf::Attribute Attribute, const MachineLocation &Location, 258263509Sdim bool Indirect = false); 259221337Sdim 260221337Sdim /// addConstantValue - Add constant value entry in variable DIE. 261263509Sdim void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty); 262263509Sdim void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned); 263263509Sdim void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned); 264221337Sdim 265221337Sdim /// addConstantFPValue - Add constant value entry in variable DIE. 266263509Sdim void addConstantFPValue(DIE *Die, const MachineOperand &MO); 267263509Sdim void addConstantFPValue(DIE *Die, const ConstantFP *CFP); 268221337Sdim 269221337Sdim /// addTemplateParams - Add template parameters in buffer. 270221337Sdim void addTemplateParams(DIE &Buffer, DIArray TParams); 271221337Sdim 272221337Sdim /// addRegisterOp - Add register operand. 273263509Sdim void addRegisterOp(DIEBlock *TheDie, unsigned Reg); 274221337Sdim 275221337Sdim /// addRegisterOffset - Add register offset. 276263509Sdim void addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset); 277221337Sdim 278221337Sdim /// addComplexAddress - Start with the address based on the location provided, 279221337Sdim /// and generate the DWARF information necessary to find the actual variable 280221337Sdim /// (navigating the extra location information encoded in the type) based on 281221337Sdim /// the starting location. Add the DWARF information to the die. 282221337Sdim /// 283263509Sdim void addComplexAddress(const DbgVariable &DV, DIE *Die, dwarf::Attribute Attribute, 284221337Sdim const MachineLocation &Location); 285221337Sdim 286221337Sdim // FIXME: Should be reformulated in terms of addComplexAddress. 287221337Sdim /// addBlockByrefAddress - Start with the address based on the location 288221337Sdim /// provided, and generate the DWARF information necessary to find the 289221337Sdim /// actual Block variable (navigating the Block struct) based on the 290221337Sdim /// starting location. Add the DWARF information to the die. Obsolete, 291221337Sdim /// please use addComplexAddress instead. 292221337Sdim /// 293263509Sdim void addBlockByrefAddress(const DbgVariable &DV, DIE *Die, dwarf::Attribute Attribute, 294221337Sdim const MachineLocation &Location); 295221337Sdim 296252723Sdim /// addVariableAddress - Add DW_AT_location attribute for a 297221337Sdim /// DbgVariable based on provided MachineLocation. 298263509Sdim void addVariableAddress(const DbgVariable &DV, DIE *Die, 299263509Sdim MachineLocation Location); 300221337Sdim 301235633Sdim /// addType - Add a new type attribute to the specified entity. This takes 302235633Sdim /// and attribute parameter because DW_AT_friend attributes are also 303235633Sdim /// type references. 304263509Sdim void addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute = dwarf::DW_AT_type); 305221337Sdim 306221337Sdim /// getOrCreateNameSpace - Create a DIE for DINameSpace. 307221337Sdim DIE *getOrCreateNameSpace(DINameSpace NS); 308221337Sdim 309226890Sdim /// getOrCreateSubprogramDIE - Create new DIE using SP. 310226890Sdim DIE *getOrCreateSubprogramDIE(DISubprogram SP); 311226890Sdim 312221337Sdim /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the 313221337Sdim /// given DIType. 314226890Sdim DIE *getOrCreateTypeDIE(const MDNode *N); 315221337Sdim 316263509Sdim /// getOrCreateContextDIE - Get context owner's DIE. 317263509Sdim DIE *getOrCreateContextDIE(DIScope Context); 318221337Sdim 319263509Sdim /// createGlobalVariableDIE - create global variable DIE. 320263509Sdim void createGlobalVariableDIE(DIGlobalVariable GV); 321221337Sdim 322263509Sdim /// constructContainingTypeDIEs - Construct DIEs for types that contain 323263509Sdim /// vtables. 324263509Sdim void constructContainingTypeDIEs(); 325221337Sdim 326263509Sdim /// constructVariableDIE - Construct a DIE for the given DbgVariable. 327263509Sdim DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract); 328226890Sdim 329263509Sdim /// Create a DIE with the given Tag, add the DIE to its parent, and 330263509Sdim /// call insertDIE if MD is not null. 331263509Sdim DIE *createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N = DIDescriptor()); 332221337Sdim 333263509Sdim /// Compute the size of a header for this unit, not including the initial 334263509Sdim /// length field. 335263509Sdim unsigned getHeaderSize() const { 336263509Sdim return sizeof(int16_t) + // DWARF version number 337263509Sdim sizeof(int32_t) + // Offset Into Abbrev. Section 338263509Sdim sizeof(int8_t); // Pointer Size (in bytes) 339263509Sdim } 340263509Sdim 341263509Sdim /// Emit the header for this unit, not including the initial length field. 342263509Sdim void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym); 343263509Sdim 344263509Sdimprivate: 345221337Sdim /// constructTypeDIE - Construct basic type die from DIBasicType. 346263509Sdim void constructTypeDIE(DIE &Buffer, DIBasicType BTy); 347221337Sdim 348221337Sdim /// constructTypeDIE - Construct derived type die from DIDerivedType. 349263509Sdim void constructTypeDIE(DIE &Buffer, DIDerivedType DTy); 350221337Sdim 351221337Sdim /// constructTypeDIE - Construct type DIE from DICompositeType. 352263509Sdim void constructTypeDIE(DIE &Buffer, DICompositeType CTy); 353221337Sdim 354221337Sdim /// constructSubrangeDIE - Construct subrange DIE from DISubrange. 355221337Sdim void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy); 356221337Sdim 357221337Sdim /// constructArrayTypeDIE - Construct array type DIE from DICompositeType. 358263509Sdim void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy); 359221337Sdim 360221337Sdim /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator. 361263509Sdim void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy); 362221337Sdim 363263509Sdim /// constructMemberDIE - Construct member DIE from DIDerivedType. 364263509Sdim void constructMemberDIE(DIE &Buffer, DIDerivedType DT); 365226890Sdim 366263509Sdim /// constructTemplateTypeParameterDIE - Construct new DIE for the given 367263509Sdim /// DITemplateTypeParameter. 368263509Sdim void constructTemplateTypeParameterDIE(DIE &Buffer, 369263509Sdim DITemplateTypeParameter TP); 370226890Sdim 371263509Sdim /// constructTemplateValueParameterDIE - Construct new DIE for the given 372263509Sdim /// DITemplateValueParameter. 373263509Sdim void constructTemplateValueParameterDIE(DIE &Buffer, 374263509Sdim DITemplateValueParameter TVP); 375221337Sdim 376263509Sdim /// getOrCreateStaticMemberDIE - Create new static data member DIE. 377263509Sdim DIE *getOrCreateStaticMemberDIE(DIDerivedType DT); 378252723Sdim 379263509Sdim /// Offset of the CUDie from beginning of debug info section. 380263509Sdim unsigned DebugInfoOffset; 381252723Sdim 382263509Sdim /// getLowerBoundDefault - Return the default lower bound for an array. If the 383263509Sdim /// DWARF version doesn't handle the language, return -1. 384263509Sdim int64_t getDefaultLowerBound() const; 385221337Sdim 386263509Sdim /// getDIEEntry - Returns the debug information entry for the specified 387263509Sdim /// debug variable. 388263509Sdim DIEEntry *getDIEEntry(const MDNode *N) const { 389263509Sdim return MDNodeToDIEEntryMap.lookup(N); 390263509Sdim } 391263509Sdim 392263509Sdim /// insertDIEEntry - Insert debug information entry into the map. 393263509Sdim void insertDIEEntry(const MDNode *N, DIEEntry *E) { 394263509Sdim MDNodeToDIEEntryMap.insert(std::make_pair(N, E)); 395263509Sdim } 396263509Sdim 397263509Sdim // getIndexTyDie - Get an anonymous type for index type. 398263509Sdim DIE *getIndexTyDie() { return IndexTyDie; } 399263509Sdim 400263509Sdim // setIndexTyDie - Set D as anonymous type for index which can be reused 401263509Sdim // later. 402263509Sdim void setIndexTyDie(DIE *D) { IndexTyDie = D; } 403263509Sdim 404263509Sdim /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug 405263509Sdim /// information entry. 406263509Sdim DIEEntry *createDIEEntry(DIE *Entry); 407263509Sdim 408263509Sdim /// resolve - Look in the DwarfDebug map for the MDNode that 409263509Sdim /// corresponds to the reference. 410263509Sdim template <typename T> T resolve(DIRef<T> Ref) const { 411263509Sdim return DD->resolve(Ref); 412263509Sdim } 413221337Sdim}; 414221337Sdim 415221337Sdim} // end llvm namespace 416221337Sdim#endif 417