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 /// AccelNames - A map of names for the name accelerator table.
106 StringMap<std::vector<const DIE *> > AccelNames;
108 /// AccelObjC - A map of objc spec for the objc accelerator table.
109 StringMap<std::vector<const DIE *> > AccelObjC;
111 /// AccelNamespace - A map of names for the namespace accelerator table.
112 StringMap<std::vector<const DIE *> > AccelNamespace;
114 /// AccelTypes - A map of names for the type accelerator table.
115 StringMap<std::vector<std::pair<const DIE *, unsigned> > > AccelTypes;
117 /// DIEBlocks - A list of all the DIEBlocks in use.
118 std::vector<DIEBlock *> DIEBlocks;
120 /// DIELocs - A list of all the DIELocs in use.
121 std::vector<DIELoc *> DIELocs;
123 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
124 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
125 /// corresponds to the MDNode mapped with the subprogram DIE.
126 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
128 // List of ranges for a given compile unit.
129 SmallVector<RangeSpan, 1> CURanges;
131 // List of range lists for a given compile unit, separate from the ranges for
133 SmallVector<RangeSpanList, 1> CURangeLists;
135 // DIEValueAllocator - All DIEValues are allocated through this allocator.
136 BumpPtrAllocator DIEValueAllocator;
138 // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
139 DIEInteger *DIEIntegerOne;
141 /// The section this unit will be emitted in.
142 const MCSection *Section;
144 /// A label at the start of the non-dwo section related to this unit.
145 MCSymbol *SectionSym;
147 /// The start of the unit within its section.
148 MCSymbol *LabelBegin;
150 /// The end of the unit within its section.
153 /// The label for the start of the range sets for the elements of this unit.
154 MCSymbol *LabelRange;
156 /// Skeleton unit associated with this unit.
159 DwarfUnit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A,
160 DwarfDebug *DW, DwarfFile *DWU);
163 virtual ~DwarfUnit();
165 /// Set the skeleton unit associated with this unit.
166 void setSkeleton(DwarfUnit *Skel) { Skeleton = Skel; }
168 /// Get the skeleton unit associated with this unit.
169 DwarfUnit *getSkeleton() const { return Skeleton; }
171 /// Pass in the SectionSym even though we could recreate it in every compile
172 /// unit (type units will have actually distinct symbols once they're in
173 /// comdat sections).
174 void initSection(const MCSection *Section, MCSymbol *SectionSym) {
175 assert(!this->Section);
176 this->Section = Section;
177 this->SectionSym = SectionSym;
179 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
181 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
182 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
185 const MCSection *getSection() const {
190 /// If there's a skeleton then return the section symbol for the skeleton
191 /// unit, otherwise return the section symbol for this unit.
192 MCSymbol *getLocalSectionSym() const {
194 return Skeleton->getSectionSym();
195 return getSectionSym();
198 MCSymbol *getSectionSym() const {
203 /// If there's a skeleton then return the begin label for the skeleton unit,
204 /// otherwise return the local label for this unit.
205 MCSymbol *getLocalLabelBegin() const {
207 return Skeleton->getLabelBegin();
208 return getLabelBegin();
211 MCSymbol *getLabelBegin() const {
216 MCSymbol *getLabelEnd() const {
221 MCSymbol *getLabelRange() const {
227 unsigned getUniqueID() const { return UniqueID; }
228 uint16_t getLanguage() const { return CUNode.getLanguage(); }
229 DICompileUnit getCUNode() const { return CUNode; }
230 DIE *getUnitDie() const { return UnitDie.get(); }
231 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
232 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
234 const StringMap<std::vector<const DIE *> > &getAccelNames() const {
237 const StringMap<std::vector<const DIE *> > &getAccelObjC() const {
240 const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
241 return AccelNamespace;
243 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
244 getAccelTypes() const {
248 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
249 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
251 /// hasContent - Return true if this compile unit has something to write out.
252 bool hasContent() const { return !UnitDie->getChildren().empty(); }
254 /// addRange - Add an address range to the list of ranges for this unit.
255 void addRange(RangeSpan Range);
257 /// getRanges - Get the list of ranges for this unit.
258 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
259 SmallVectorImpl<RangeSpan> &getRanges() { return CURanges; }
261 /// addRangeList - Add an address range list to the list of range lists.
262 void addRangeList(RangeSpanList Ranges) { CURangeLists.push_back(Ranges); }
264 /// getRangeLists - Get the vector of range lists.
265 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
268 SmallVectorImpl<RangeSpanList> &getRangeLists() { return CURangeLists; }
270 /// getParentContextString - Get a string containing the language specific
271 /// context for a global name.
272 std::string getParentContextString(DIScope Context) const;
274 /// addGlobalName - Add a new global entity to the compile unit.
276 void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
278 /// addAccelName - Add a new name to the name accelerator table.
279 void addAccelName(StringRef Name, const DIE *Die);
281 /// addAccelObjC - Add a new name to the ObjC accelerator table.
282 void addAccelObjC(StringRef Name, const DIE *Die);
284 /// addAccelNamespace - Add a new name to the namespace accelerator table.
285 void addAccelNamespace(StringRef Name, const DIE *Die);
287 /// addAccelType - Add a new type to the type accelerator table.
288 void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
290 /// getDIE - Returns the debug information entry map slot for the
291 /// specified debug variable. We delegate the request to DwarfDebug
292 /// when the MDNode can be part of the type system, since DIEs for
293 /// the type system can be shared across CUs and the mappings are
294 /// kept in DwarfDebug.
295 DIE *getDIE(DIDescriptor D) const;
297 /// getDIELoc - Returns a fresh newly allocated DIELoc.
298 DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc(); }
300 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
301 /// when the MDNode can be part of the type system, since DIEs for
302 /// the type system can be shared across CUs and the mappings are
303 /// kept in DwarfDebug.
304 void insertDIE(DIDescriptor Desc, DIE *D);
306 /// addDie - Adds or interns the DIE to the compile unit.
308 void addDie(DIE *Buffer) { UnitDie->addChild(Buffer); }
310 /// addFlag - Add a flag that is true to the DIE.
311 void addFlag(DIE *Die, dwarf::Attribute Attribute);
313 /// addUInt - Add an unsigned integer attribute data and value.
314 void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
317 void addUInt(DIE *Block, dwarf::Form Form, uint64_t Integer);
319 /// addSInt - Add an signed integer attribute data and value.
320 void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
323 void addSInt(DIELoc *Die, Optional<dwarf::Form> Form, int64_t Integer);
325 /// addString - Add a string attribute data and value.
326 void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
328 /// addLocalString - Add a string attribute data and value.
329 void addLocalString(DIE *Die, dwarf::Attribute Attribute,
330 const StringRef Str);
332 /// addExpr - Add a Dwarf expression attribute data and value.
333 void addExpr(DIELoc *Die, dwarf::Form Form, const MCExpr *Expr);
335 /// addLabel - Add a Dwarf label attribute data and value.
336 void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
337 const MCSymbol *Label);
339 void addLabel(DIELoc *Die, dwarf::Form Form, const MCSymbol *Label);
341 /// addLocationList - Add a Dwarf loclistptr attribute data and value.
342 void addLocationList(DIE *Die, dwarf::Attribute Attribute, unsigned Index);
344 /// addSectionLabel - Add a Dwarf section label attribute data and value.
346 void addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
347 const MCSymbol *Label);
349 /// addSectionOffset - Add an offset into a section attribute data and value.
351 void addSectionOffset(DIE *Die, dwarf::Attribute Attribute, uint64_t Integer);
353 /// addOpAddress - Add a dwarf op address data and value using the
354 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
355 void addOpAddress(DIELoc *Die, const MCSymbol *Label);
357 /// addSectionDelta - Add a label delta attribute data and value.
358 void addSectionDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
361 /// addLabelDelta - Add a label delta attribute data and value.
362 void addLabelDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
365 /// addDIEEntry - Add a DIE attribute data and value.
366 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
368 /// addDIEEntry - Add a DIE attribute data and value.
369 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
371 void addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type);
373 /// addBlock - Add block data.
374 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIELoc *Block);
376 /// addBlock - Add block data.
377 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
379 /// addSourceLine - Add location information to specified debug information
381 void addSourceLine(DIE *Die, unsigned Line, StringRef File,
382 StringRef Directory);
383 void addSourceLine(DIE *Die, DIVariable V);
384 void addSourceLine(DIE *Die, DIGlobalVariable G);
385 void addSourceLine(DIE *Die, DISubprogram SP);
386 void addSourceLine(DIE *Die, DIType Ty);
387 void addSourceLine(DIE *Die, DINameSpace NS);
388 void addSourceLine(DIE *Die, DIObjCProperty Ty);
390 /// addAddress - Add an address attribute to a die based on the location
392 void addAddress(DIE *Die, dwarf::Attribute Attribute,
393 const MachineLocation &Location, bool Indirect = false);
395 /// addConstantValue - Add constant value entry in variable DIE.
396 void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
397 void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
398 void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
400 /// addConstantFPValue - Add constant value entry in variable DIE.
401 void addConstantFPValue(DIE *Die, const MachineOperand &MO);
402 void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
404 /// addTemplateParams - Add template parameters in buffer.
405 void addTemplateParams(DIE &Buffer, DIArray TParams);
407 /// addRegisterOp - Add register operand.
408 void addRegisterOp(DIELoc *TheDie, unsigned Reg);
410 /// addRegisterOffset - Add register offset.
411 void addRegisterOffset(DIELoc *TheDie, unsigned Reg, int64_t Offset);
413 /// addComplexAddress - Start with the address based on the location provided,
414 /// and generate the DWARF information necessary to find the actual variable
415 /// (navigating the extra location information encoded in the type) based on
416 /// the starting location. Add the DWARF information to the die.
417 void addComplexAddress(const DbgVariable &DV, DIE *Die,
418 dwarf::Attribute Attribute,
419 const MachineLocation &Location);
421 // FIXME: Should be reformulated in terms of addComplexAddress.
422 /// addBlockByrefAddress - Start with the address based on the location
423 /// provided, and generate the DWARF information necessary to find the
424 /// actual Block variable (navigating the Block struct) based on the
425 /// starting location. Add the DWARF information to the die. Obsolete,
426 /// please use addComplexAddress instead.
427 void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
428 dwarf::Attribute Attribute,
429 const MachineLocation &Location);
431 /// addVariableAddress - Add DW_AT_location attribute for a
432 /// DbgVariable based on provided MachineLocation.
433 void addVariableAddress(const DbgVariable &DV, DIE *Die,
434 MachineLocation Location);
436 /// addType - Add a new type attribute to the specified entity. This takes
437 /// and attribute parameter because DW_AT_friend attributes are also
439 void addType(DIE *Entity, DIType Ty,
440 dwarf::Attribute Attribute = dwarf::DW_AT_type);
442 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
443 DIE *getOrCreateNameSpace(DINameSpace NS);
445 /// getOrCreateSubprogramDIE - Create new DIE using SP.
446 DIE *getOrCreateSubprogramDIE(DISubprogram SP);
448 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
450 DIE *getOrCreateTypeDIE(const MDNode *N);
452 /// getOrCreateContextDIE - Get context owner's DIE.
453 DIE *createTypeDIE(DICompositeType Ty);
455 /// getOrCreateContextDIE - Get context owner's DIE.
456 DIE *getOrCreateContextDIE(DIScope Context);
458 /// constructContainingTypeDIEs - Construct DIEs for types that contain
460 void constructContainingTypeDIEs();
462 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
463 DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
465 /// constructSubprogramArguments - Construct function argument DIEs.
466 void constructSubprogramArguments(DIE &Buffer, DIArray Args);
468 /// Create a DIE with the given Tag, add the DIE to its parent, and
469 /// call insertDIE if MD is not null.
470 DIE *createAndAddDIE(unsigned Tag, DIE &Parent,
471 DIDescriptor N = DIDescriptor());
473 /// Compute the size of a header for this unit, not including the initial
475 virtual unsigned getHeaderSize() const {
476 return sizeof(int16_t) + // DWARF version number
477 sizeof(int32_t) + // Offset Into Abbrev. Section
478 sizeof(int8_t); // Pointer Size (in bytes)
481 /// Emit the header for this unit, not including the initial length field.
482 virtual void emitHeader(const MCSymbol *ASectionSym) const;
484 virtual DwarfCompileUnit &getCU() = 0;
487 /// getOrCreateStaticMemberDIE - Create new static data member DIE.
488 DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
490 /// Look up the source ID with the given directory and source file names. If
491 /// none currently exists, create a new ID and insert it in the line table.
492 virtual unsigned getOrCreateSourceID(StringRef File, StringRef Directory) = 0;
495 /// constructTypeDIE - Construct basic type die from DIBasicType.
496 void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
498 /// constructTypeDIE - Construct derived type die from DIDerivedType.
499 void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
501 /// constructTypeDIE - Construct type DIE from DICompositeType.
502 void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
504 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
505 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
507 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
508 void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
510 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
511 void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
513 /// constructMemberDIE - Construct member DIE from DIDerivedType.
514 void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
516 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
517 /// DITemplateTypeParameter.
518 void constructTemplateTypeParameterDIE(DIE &Buffer,
519 DITemplateTypeParameter TP);
521 /// constructTemplateValueParameterDIE - Construct new DIE for the given
522 /// DITemplateValueParameter.
523 void constructTemplateValueParameterDIE(DIE &Buffer,
524 DITemplateValueParameter TVP);
526 /// getLowerBoundDefault - Return the default lower bound for an array. If the
527 /// DWARF version doesn't handle the language, return -1.
528 int64_t getDefaultLowerBound() const;
530 /// getDIEEntry - Returns the debug information entry for the specified
532 DIEEntry *getDIEEntry(const MDNode *N) const {
533 return MDNodeToDIEEntryMap.lookup(N);
536 /// insertDIEEntry - Insert debug information entry into the map.
537 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
538 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
541 // getIndexTyDie - Get an anonymous type for index type.
542 DIE *getIndexTyDie() { return IndexTyDie; }
544 // setIndexTyDie - Set D as anonymous type for index which can be reused
546 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
548 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
549 /// information entry.
550 DIEEntry *createDIEEntry(DIE *Entry);
552 /// resolve - Look in the DwarfDebug map for the MDNode that
553 /// corresponds to the reference.
554 template <typename T> T resolve(DIRef<T> Ref) const {
555 return DD->resolve(Ref);
558 /// If this is a named finished type then include it in the list of types for
559 /// the accelerator tables.
560 void updateAcceleratorTables(DIScope Context, DIType Ty, const DIE *TyDIE);
563 class DwarfCompileUnit : public DwarfUnit {
564 /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
565 /// the need to search for it in applyStmtList.
566 unsigned stmtListIndex;
569 DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
570 DwarfDebug *DW, DwarfFile *DWU);
572 void initStmtList(MCSymbol *DwarfLineSectionSym);
574 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
575 void applyStmtList(DIE &D);
577 /// createGlobalVariableDIE - create global variable DIE.
578 void createGlobalVariableDIE(DIGlobalVariable GV);
580 /// addLabelAddress - Add a dwarf label attribute data and value using
581 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
582 void addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
583 const MCSymbol *Label);
585 /// addLocalLabelAddress - Add a dwarf label attribute data and value using
586 /// DW_FORM_addr only.
587 void addLocalLabelAddress(DIE *Die, dwarf::Attribute Attribute,
588 const MCSymbol *Label);
590 DwarfCompileUnit &getCU() override { return *this; }
592 unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override;
595 class DwarfTypeUnit : public DwarfUnit {
597 uint64_t TypeSignature;
599 DwarfCompileUnit &CU;
600 MCDwarfDwoLineTable *SplitLineTable;
603 DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU, AsmPrinter *A,
604 DwarfDebug *DW, DwarfFile *DWU,
605 MCDwarfDwoLineTable *SplitLineTable = nullptr);
607 void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
608 uint64_t getTypeSignature() const { return TypeSignature; }
609 void setType(const DIE *Ty) { this->Ty = Ty; }
611 /// Emit the header for this unit, not including the initial length field.
612 void emitHeader(const MCSymbol *ASectionSym) const override;
613 unsigned getHeaderSize() const override {
614 return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
615 sizeof(uint32_t); // Type DIE Offset
617 void initSection(const MCSection *Section);
618 DwarfCompileUnit &getCU() override { return CU; }
621 unsigned getOrCreateSourceID(StringRef File, StringRef Directory) override;
623 } // end llvm namespace