1 //===-- llvm/CodeGen/DwarfUnit.h - Dwarf Compile Unit ---*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf compile unit.
12 //===----------------------------------------------------------------------===//
14 #ifndef CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
15 #define CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
18 #include "DwarfDebug.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/IR/DIBuilder.h"
24 #include "llvm/IR/DebugInfo.h"
25 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCSection.h"
27 #include "llvm/MC/MCDwarf.h"
31 class MachineLocation;
36 class DwarfCompileUnit;
38 // Data structure to hold a range for range lists.
41 RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {}
42 const MCSymbol *getStart() const { return Start; }
43 const MCSymbol *getEnd() const { return End; }
44 void setEnd(const MCSymbol *E) { End = E; }
47 const MCSymbol *Start, *End;
52 // Index for locating within the debug_range section this particular span.
55 SmallVector<RangeSpan, 2> Ranges;
58 RangeSpanList(MCSymbol *Sym) : RangeSym(Sym) {}
59 MCSymbol *getSym() const { return RangeSym; }
60 const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
61 void addRange(RangeSpan Range) { Ranges.push_back(Range); }
64 //===----------------------------------------------------------------------===//
65 /// Unit - This dwarf writer support class manages information associated
66 /// with a source file.
69 /// UniqueID - a numeric ID unique among all CUs in the module
72 /// Node - MDNode for the compile unit.
75 /// Unit debug information entry.
76 const std::unique_ptr<DIE> UnitDie;
78 /// Offset of the UnitDie from beginning of debug info section.
79 unsigned DebugInfoOffset;
81 /// Asm - Target of Dwarf emission.
84 // Holders for some common dwarf information.
88 /// IndexTyDie - An anonymous type for index type. Owned by UnitDie.
91 /// MDNodeToDieMap - Tracks the mapping of unit level debug information
92 /// variables to debug information entries.
93 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
95 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information
96 /// descriptors to debug information entries using a DIEEntry proxy.
97 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
99 /// GlobalNames - A map of globally visible named entities for this unit.
100 StringMap<const DIE *> GlobalNames;
102 /// GlobalTypes - A map of globally visible types for this unit.
103 StringMap<const DIE *> GlobalTypes;
105 /// AccelNamespace - A map of names for the namespace accelerator table.
106 StringMap<std::vector<const DIE *> > AccelNamespace;
108 /// AccelTypes - A map of names for the type accelerator table.
109 StringMap<std::vector<std::pair<const DIE *, unsigned> > > AccelTypes;
111 /// DIEBlocks - A list of all the DIEBlocks in use.
112 std::vector<DIEBlock *> DIEBlocks;
114 /// DIELocs - A list of all the DIELocs in use.
115 std::vector<DIELoc *> DIELocs;
117 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
118 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
119 /// corresponds to the MDNode mapped with the subprogram DIE.
120 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
122 // List of ranges for a given compile unit.
123 SmallVector<RangeSpan, 1> CURanges;
125 // List of range lists for a given compile unit, separate from the ranges for
127 SmallVector<RangeSpanList, 1> CURangeLists;
129 // DIEValueAllocator - All DIEValues are allocated through this allocator.
130 BumpPtrAllocator DIEValueAllocator;
132 // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
133 DIEInteger *DIEIntegerOne;
135 /// The section this unit will be emitted in.
136 const MCSection *Section;
138 /// A label at the start of the non-dwo section related to this unit.
139 MCSymbol *SectionSym;
141 /// The start of the unit within its section.
142 MCSymbol *LabelBegin;
144 /// The end of the unit within its section.
147 /// The label for the start of the range sets for the elements of this unit.
148 MCSymbol *LabelRange;
150 /// Skeleton unit associated with this unit.
153 DwarfUnit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A,
154 DwarfDebug *DW, DwarfFile *DWU);
157 virtual ~DwarfUnit();
159 /// Set the skeleton unit associated with this unit.
160 void setSkeleton(DwarfUnit &Skel) { Skeleton = &Skel; }
162 /// Get the skeleton unit associated with this unit.
163 DwarfUnit *getSkeleton() const { return Skeleton; }
165 /// Pass in the SectionSym even though we could recreate it in every compile
166 /// unit (type units will have actually distinct symbols once they're in
167 /// comdat sections).
168 void initSection(const MCSection *Section, MCSymbol *SectionSym) {
169 assert(!this->Section);
170 this->Section = Section;
171 this->SectionSym = SectionSym;
173 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
175 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
176 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
179 const MCSection *getSection() const {
184 /// If there's a skeleton then return the section symbol for the skeleton
185 /// unit, otherwise return the section symbol for this unit.
186 MCSymbol *getLocalSectionSym() const {
188 return Skeleton->getSectionSym();
189 return getSectionSym();
192 MCSymbol *getSectionSym() const {
197 /// If there's a skeleton then return the begin label for the skeleton unit,
198 /// otherwise return the local label for this unit.
199 MCSymbol *getLocalLabelBegin() const {
201 return Skeleton->getLabelBegin();
202 return getLabelBegin();
205 MCSymbol *getLabelBegin() const {
210 MCSymbol *getLabelEnd() const {
215 MCSymbol *getLabelRange() const {
221 unsigned getUniqueID() const { return UniqueID; }
222 uint16_t getLanguage() const { return CUNode.getLanguage(); }
223 DICompileUnit getCUNode() const { return CUNode; }
224 DIE *getUnitDie() const { return UnitDie.get(); }
225 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
226 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
228 const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
229 return AccelNamespace;
231 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
232 getAccelTypes() const {
236 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
237 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
239 /// hasContent - Return true if this compile unit has something to write out.
240 bool hasContent() const { return !UnitDie->getChildren().empty(); }
242 /// addRange - Add an address range to the list of ranges for this unit.
243 void addRange(RangeSpan Range);
245 /// getRanges - Get the list of ranges for this unit.
246 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
247 SmallVectorImpl<RangeSpan> &getRanges() { return CURanges; }
249 /// addRangeList - Add an address range list to the list of range lists.
250 void addRangeList(RangeSpanList Ranges) { CURangeLists.push_back(Ranges); }
252 /// getRangeLists - Get the vector of range lists.
253 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
256 SmallVectorImpl<RangeSpanList> &getRangeLists() { return CURangeLists; }
258 /// getParentContextString - Get a string containing the language specific
259 /// context for a global name.
260 std::string getParentContextString(DIScope Context) const;
262 /// addGlobalName - Add a new global entity to the compile unit.
264 void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
266 /// addAccelNamespace - Add a new name to the namespace accelerator table.
267 void addAccelNamespace(StringRef Name, const DIE *Die);
269 /// addAccelType - Add a new type to the type accelerator table.
270 void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
272 /// getDIE - Returns the debug information entry map slot for the
273 /// specified debug variable. We delegate the request to DwarfDebug
274 /// when the MDNode can be part of the type system, since DIEs for
275 /// the type system can be shared across CUs and the mappings are
276 /// kept in DwarfDebug.
277 DIE *getDIE(DIDescriptor D) const;
279 /// getDIELoc - Returns a fresh newly allocated DIELoc.
280 DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc(); }
282 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
283 /// when the MDNode can be part of the type system, since DIEs for
284 /// the type system can be shared across CUs and the mappings are
285 /// kept in DwarfDebug.
286 void insertDIE(DIDescriptor Desc, DIE *D);
288 /// addDie - Adds or interns the DIE to the compile unit.
290 void addDie(DIE *Buffer) { UnitDie->addChild(Buffer); }
292 /// addFlag - Add a flag that is true to the DIE.
293 void addFlag(DIE *Die, dwarf::Attribute Attribute);
295 /// addUInt - Add an unsigned integer attribute data and value.
296 void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
299 void addUInt(DIE *Block, dwarf::Form Form, uint64_t Integer);
301 /// addSInt - Add an signed integer attribute data and value.
302 void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
305 void addSInt(DIELoc *Die, Optional<dwarf::Form> Form, int64_t Integer);
307 /// addString - Add a string attribute data and value.
308 void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
310 /// addLocalString - Add a string attribute data and value.
311 void addLocalString(DIE *Die, dwarf::Attribute Attribute,
312 const StringRef Str);
314 /// addExpr - Add a Dwarf expression attribute data and value.
315 void addExpr(DIELoc *Die, dwarf::Form Form, const MCExpr *Expr);
317 /// addLabel - Add a Dwarf label attribute data and value.
318 void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
319 const MCSymbol *Label);
321 void addLabel(DIELoc *Die, dwarf::Form Form, const MCSymbol *Label);
323 /// addLocationList - Add a Dwarf loclistptr attribute data and value.
324 void addLocationList(DIE *Die, dwarf::Attribute Attribute, unsigned Index);
326 /// addSectionLabel - Add a Dwarf section label attribute data and value.
328 void addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
329 const MCSymbol *Label);
331 /// addSectionOffset - Add an offset into a section attribute data and value.
333 void addSectionOffset(DIE *Die, dwarf::Attribute Attribute, uint64_t Integer);
335 /// addOpAddress - Add a dwarf op address data and value using the
336 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
337 void addOpAddress(DIELoc *Die, const MCSymbol *Label);
339 /// addSectionDelta - Add a label delta attribute data and value.
340 void addSectionDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
343 /// addLabelDelta - Add a label delta attribute data and value.
344 void addLabelDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
347 /// addDIEEntry - Add a DIE attribute data and value.
348 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
350 /// addDIEEntry - Add a DIE attribute data and value.
351 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
353 void addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type);
355 /// addBlock - Add block data.
356 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIELoc *Block);
358 /// addBlock - Add block data.
359 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
361 /// addSourceLine - Add location information to specified debug information
363 void addSourceLine(DIE *Die, unsigned Line, StringRef File,
364 StringRef Directory);
365 void addSourceLine(DIE *Die, DIVariable V);
366 void addSourceLine(DIE *Die, DIGlobalVariable G);
367 void addSourceLine(DIE *Die, DISubprogram SP);
368 void addSourceLine(DIE *Die, DIType Ty);
369 void addSourceLine(DIE *Die, DINameSpace NS);
370 void addSourceLine(DIE *Die, DIObjCProperty Ty);
372 /// addAddress - Add an address attribute to a die based on the location
374 void addAddress(DIE *Die, dwarf::Attribute Attribute,
375 const MachineLocation &Location, bool Indirect = false);
377 /// addConstantValue - Add constant value entry in variable DIE.
378 void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
379 void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
380 void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
382 /// addConstantFPValue - Add constant value entry in variable DIE.
383 void addConstantFPValue(DIE *Die, const MachineOperand &MO);
384 void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
386 /// addTemplateParams - Add template parameters in buffer.
387 void addTemplateParams(DIE &Buffer, DIArray TParams);
389 /// addRegisterOp - Add register operand.
390 void addRegisterOp(DIELoc *TheDie, unsigned Reg);
392 /// addRegisterOffset - Add register offset.
393 void addRegisterOffset(DIELoc *TheDie, unsigned Reg, int64_t Offset);
395 /// addComplexAddress - Start with the address based on the location provided,
396 /// and generate the DWARF information necessary to find the actual variable
397 /// (navigating the extra location information encoded in the type) based on
398 /// the starting location. Add the DWARF information to the die.
399 void addComplexAddress(const DbgVariable &DV, DIE *Die,
400 dwarf::Attribute Attribute,
401 const MachineLocation &Location);
403 // FIXME: Should be reformulated in terms of addComplexAddress.
404 /// addBlockByrefAddress - Start with the address based on the location
405 /// provided, and generate the DWARF information necessary to find the
406 /// actual Block variable (navigating the Block struct) based on the
407 /// starting location. Add the DWARF information to the die. Obsolete,
408 /// please use addComplexAddress instead.
409 void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
410 dwarf::Attribute Attribute,
411 const MachineLocation &Location);
413 /// addVariableAddress - Add DW_AT_location attribute for a
414 /// DbgVariable based on provided MachineLocation.
415 void addVariableAddress(const DbgVariable &DV, DIE *Die,
416 MachineLocation Location);
418 /// addType - Add a new type attribute to the specified entity. This takes
419 /// and attribute parameter because DW_AT_friend attributes are also
421 void addType(DIE *Entity, DIType Ty,
422 dwarf::Attribute Attribute = dwarf::DW_AT_type);
424 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
425 DIE *getOrCreateNameSpace(DINameSpace NS);
427 /// getOrCreateSubprogramDIE - Create new DIE using SP.
428 DIE *getOrCreateSubprogramDIE(DISubprogram SP);
430 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
432 DIE *getOrCreateTypeDIE(const MDNode *N);
434 /// getOrCreateContextDIE - Get context owner's DIE.
435 DIE *createTypeDIE(DICompositeType Ty);
437 /// getOrCreateContextDIE - Get context owner's DIE.
438 DIE *getOrCreateContextDIE(DIScope Context);
440 /// constructContainingTypeDIEs - Construct DIEs for types that contain
442 void constructContainingTypeDIEs();
444 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
445 DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
447 /// constructSubprogramArguments - Construct function argument DIEs.
448 void constructSubprogramArguments(DIE &Buffer, DIArray Args);
450 /// Create a DIE with the given Tag, add the DIE to its parent, and
451 /// call insertDIE if MD is not null.
452 DIE *createAndAddDIE(unsigned Tag, DIE &Parent,
453 DIDescriptor N = DIDescriptor());
455 /// Compute the size of a header for this unit, not including the initial
457 virtual unsigned getHeaderSize() const {
458 return sizeof(int16_t) + // DWARF version number
459 sizeof(int32_t) + // Offset Into Abbrev. Section
460 sizeof(int8_t); // Pointer Size (in bytes)
463 /// Emit the header for this unit, not including the initial length field.
464 virtual void emitHeader(const MCSymbol *ASectionSym) const;
466 virtual DwarfCompileUnit &getCU() = 0;
469 /// getOrCreateStaticMemberDIE - Create new static data member DIE.
470 DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
472 /// Look up the source ID with the given directory and source file names. If
473 /// none currently exists, create a new ID and insert it in the line table.
474 virtual unsigned getOrCreateSourceID(StringRef File, StringRef Directory) = 0;
477 /// constructTypeDIE - Construct basic type die from DIBasicType.
478 void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
480 /// constructTypeDIE - Construct derived type die from DIDerivedType.
481 void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
483 /// constructTypeDIE - Construct type DIE from DICompositeType.
484 void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
486 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
487 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
489 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
490 void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
492 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
493 void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
495 /// constructMemberDIE - Construct member DIE from DIDerivedType.
496 void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
498 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
499 /// DITemplateTypeParameter.
500 void constructTemplateTypeParameterDIE(DIE &Buffer,
501 DITemplateTypeParameter TP);
503 /// constructTemplateValueParameterDIE - Construct new DIE for the given
504 /// DITemplateValueParameter.
505 void constructTemplateValueParameterDIE(DIE &Buffer,
506 DITemplateValueParameter TVP);
508 /// getLowerBoundDefault - Return the default lower bound for an array. If the
509 /// DWARF version doesn't handle the language, return -1.
510 int64_t getDefaultLowerBound() const;
512 /// getDIEEntry - Returns the debug information entry for the specified
514 DIEEntry *getDIEEntry(const MDNode *N) const {
515 return MDNodeToDIEEntryMap.lookup(N);
518 /// insertDIEEntry - Insert debug information entry into the map.
519 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
520 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
523 // getIndexTyDie - Get an anonymous type for index type.
524 DIE *getIndexTyDie() { return IndexTyDie; }
526 // setIndexTyDie - Set D as anonymous type for index which can be reused
528 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
530 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
531 /// information entry.
532 DIEEntry *createDIEEntry(DIE *Entry);
534 /// resolve - Look in the DwarfDebug map for the MDNode that
535 /// corresponds to the reference.
536 template <typename T> T resolve(DIRef<T> Ref) const {
537 return DD->resolve(Ref);
540 /// If this is a named finished type then include it in the list of types for
541 /// the accelerator tables.
542 void updateAcceleratorTables(DIScope Context, DIType Ty, const DIE *TyDIE);
545 class DwarfCompileUnit : public DwarfUnit {
546 /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
547 /// the need to search for it in applyStmtList.
548 unsigned stmtListIndex;
551 DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
552 DwarfDebug *DW, DwarfFile *DWU);
554 void initStmtList(MCSymbol *DwarfLineSectionSym);
556 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
557 void applyStmtList(DIE &D);
559 /// createGlobalVariableDIE - create global variable DIE.
560 void createGlobalVariableDIE(DIGlobalVariable GV);
562 /// addLabelAddress - Add a dwarf label attribute data and value using
563 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
564 void addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
565 const MCSymbol *Label);
567 /// addLocalLabelAddress - Add a dwarf label attribute data and value using
568 /// DW_FORM_addr only.
569 void addLocalLabelAddress(DIE *Die, dwarf::Attribute Attribute,
570 const MCSymbol *Label);
572 DwarfCompileUnit &getCU() override { return *this; }
574 unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override;
577 class DwarfTypeUnit : public DwarfUnit {
579 uint64_t TypeSignature;
581 DwarfCompileUnit &CU;
582 MCDwarfDwoLineTable *SplitLineTable;
585 DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU, AsmPrinter *A,
586 DwarfDebug *DW, DwarfFile *DWU,
587 MCDwarfDwoLineTable *SplitLineTable = nullptr);
589 void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
590 uint64_t getTypeSignature() const { return TypeSignature; }
591 void setType(const DIE *Ty) { this->Ty = Ty; }
593 /// Emit the header for this unit, not including the initial length field.
594 void emitHeader(const MCSymbol *ASectionSym) const override;
595 unsigned getHeaderSize() const override {
596 return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
597 sizeof(uint32_t); // Type DIE Offset
599 void initSection(const MCSection *Section);
600 DwarfCompileUnit &getCU() override { return CU; }
603 unsigned getOrCreateSourceID(StringRef File, StringRef Directory) override;
605 } // end llvm namespace