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/OwningPtr.h"
22 #include "llvm/ADT/StringMap.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"
30 class MachineLocation;
35 class DwarfCompileUnit;
37 // Data structure to hold a range for range lists.
40 RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {}
41 const MCSymbol *getStart() const { return Start; }
42 const MCSymbol *getEnd() const { return End; }
45 const MCSymbol *Start, *End;
50 // Index for locating within the debug_range section this particular span.
53 SmallVector<RangeSpan, 2> Ranges;
56 RangeSpanList(MCSymbol *Sym) : RangeSym(Sym) {}
57 MCSymbol *getSym() const { return RangeSym; }
58 const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
59 void addRange(RangeSpan Range) { Ranges.push_back(Range); }
62 //===----------------------------------------------------------------------===//
63 /// Unit - This dwarf writer support class manages information associated
64 /// with a source file.
67 /// UniqueID - a numeric ID unique among all CUs in the module
70 /// Node - MDNode for the compile unit.
73 /// Unit debug information entry.
74 const OwningPtr<DIE> UnitDie;
76 /// Offset of the UnitDie from beginning of debug info section.
77 unsigned DebugInfoOffset;
79 /// Asm - Target of Dwarf emission.
82 // Holders for some common dwarf information.
86 /// IndexTyDie - An anonymous type for index type. Owned by UnitDie.
89 /// MDNodeToDieMap - Tracks the mapping of unit level debug information
90 /// variables to debug information entries.
91 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
93 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information
94 /// descriptors to debug information entries using a DIEEntry proxy.
95 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
97 /// GlobalNames - A map of globally visible named entities for this unit.
98 StringMap<const DIE *> GlobalNames;
100 /// GlobalTypes - A map of globally visible types for this unit.
101 StringMap<const DIE *> GlobalTypes;
103 /// AccelNames - A map of names for the name accelerator table.
104 StringMap<std::vector<const DIE *> > AccelNames;
106 /// AccelObjC - A map of objc spec for the objc accelerator table.
107 StringMap<std::vector<const DIE *> > AccelObjC;
109 /// AccelNamespace - A map of names for the namespace accelerator table.
110 StringMap<std::vector<const DIE *> > AccelNamespace;
112 /// AccelTypes - A map of names for the type accelerator table.
113 StringMap<std::vector<std::pair<const DIE *, unsigned> > > AccelTypes;
115 /// DIEBlocks - A list of all the DIEBlocks in use.
116 std::vector<DIEBlock *> DIEBlocks;
118 /// DIELocs - A list of all the DIELocs in use.
119 std::vector<DIELoc *> DIELocs;
121 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
122 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
123 /// corresponds to the MDNode mapped with the subprogram DIE.
124 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
126 // List of ranges for a given compile unit.
127 SmallVector<RangeSpan, 1> CURanges;
129 // List of range lists for a given compile unit, separate from the ranges for
131 SmallVector<RangeSpanList, 1> CURangeLists;
133 // DIEValueAllocator - All DIEValues are allocated through this allocator.
134 BumpPtrAllocator DIEValueAllocator;
136 // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
137 DIEInteger *DIEIntegerOne;
139 /// The section this unit will be emitted in.
140 const MCSection *Section;
142 /// A label at the start of the non-dwo section related to this unit.
143 MCSymbol *SectionSym;
145 /// The start of the unit within its section.
146 MCSymbol *LabelBegin;
148 /// The end of the unit within its section.
151 /// The label for the start of the range sets for the elements of this unit.
152 MCSymbol *LabelRange;
154 /// Skeleton unit associated with this unit.
157 DwarfUnit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A,
158 DwarfDebug *DW, DwarfFile *DWU);
161 virtual ~DwarfUnit();
163 /// Set the skeleton unit associated with this unit.
164 void setSkeleton(DwarfUnit *Skel) { Skeleton = Skel; }
166 /// Get the skeleton unit associated with this unit.
167 DwarfUnit *getSkeleton() const { return Skeleton; }
169 /// Pass in the SectionSym even though we could recreate it in every compile
170 /// unit (type units will have actually distinct symbols once they're in
171 /// comdat sections).
172 void initSection(const MCSection *Section, MCSymbol *SectionSym) {
173 assert(!this->Section);
174 this->Section = Section;
175 this->SectionSym = SectionSym;
177 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
179 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
180 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
183 const MCSection *getSection() const {
188 /// If there's a skeleton then return the section symbol for the skeleton
189 /// unit, otherwise return the section symbol for this unit.
190 MCSymbol *getLocalSectionSym() const {
192 return Skeleton->getSectionSym();
193 return getSectionSym();
196 MCSymbol *getSectionSym() const {
201 /// If there's a skeleton then return the begin label for the skeleton unit,
202 /// otherwise return the local label for this unit.
203 MCSymbol *getLocalLabelBegin() const {
205 return Skeleton->getLabelBegin();
206 return getLabelBegin();
209 MCSymbol *getLabelBegin() const {
214 MCSymbol *getLabelEnd() const {
219 MCSymbol *getLabelRange() const {
225 unsigned getUniqueID() const { return UniqueID; }
226 uint16_t getLanguage() const { return CUNode.getLanguage(); }
227 DICompileUnit getCUNode() const { return CUNode; }
228 DIE *getUnitDie() const { return UnitDie.get(); }
229 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
230 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
232 const StringMap<std::vector<const DIE *> > &getAccelNames() const {
235 const StringMap<std::vector<const DIE *> > &getAccelObjC() const {
238 const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
239 return AccelNamespace;
241 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
242 getAccelTypes() const {
246 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
247 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
249 /// hasContent - Return true if this compile unit has something to write out.
250 bool hasContent() const { return !UnitDie->getChildren().empty(); }
252 /// addRange - Add an address range to the list of ranges for this unit.
253 void addRange(RangeSpan Range) {
254 // Only add a range for this unit if we're emitting full debug.
255 if (getCUNode().getEmissionKind() == DIBuilder::FullDebug)
256 CURanges.push_back(Range);
259 /// getRanges - Get the list of ranges for this unit.
260 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
261 SmallVectorImpl<RangeSpan> &getRanges() { return CURanges; }
263 /// addRangeList - Add an address range list to the list of range lists.
264 void addRangeList(RangeSpanList Ranges) { CURangeLists.push_back(Ranges); }
266 /// getRangeLists - Get the vector of range lists.
267 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
270 SmallVectorImpl<RangeSpanList> &getRangeLists() { return CURangeLists; }
272 /// getParentContextString - Get a string containing the language specific
273 /// context for a global name.
274 std::string getParentContextString(DIScope Context) const;
276 /// addGlobalName - Add a new global entity to the compile unit.
278 void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
280 /// addAccelName - Add a new name to the name accelerator table.
281 void addAccelName(StringRef Name, const DIE *Die);
283 /// addAccelObjC - Add a new name to the ObjC accelerator table.
284 void addAccelObjC(StringRef Name, const DIE *Die);
286 /// addAccelNamespace - Add a new name to the namespace accelerator table.
287 void addAccelNamespace(StringRef Name, const DIE *Die);
289 /// addAccelType - Add a new type to the type accelerator table.
290 void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
292 /// getDIE - Returns the debug information entry map slot for the
293 /// specified debug variable. We delegate the request to DwarfDebug
294 /// when the MDNode can be part of the type system, since DIEs for
295 /// the type system can be shared across CUs and the mappings are
296 /// kept in DwarfDebug.
297 DIE *getDIE(DIDescriptor D) const;
299 /// getDIELoc - Returns a fresh newly allocated DIELoc.
300 DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc(); }
302 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
303 /// when the MDNode can be part of the type system, since DIEs for
304 /// the type system can be shared across CUs and the mappings are
305 /// kept in DwarfDebug.
306 void insertDIE(DIDescriptor Desc, DIE *D);
308 /// addDie - Adds or interns the DIE to the compile unit.
310 void addDie(DIE *Buffer) { UnitDie->addChild(Buffer); }
312 /// addFlag - Add a flag that is true to the DIE.
313 void addFlag(DIE *Die, dwarf::Attribute Attribute);
315 /// addUInt - Add an unsigned integer attribute data and value.
316 void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
319 void addUInt(DIE *Block, dwarf::Form Form, uint64_t Integer);
321 /// addSInt - Add an signed integer attribute data and value.
322 void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
325 void addSInt(DIELoc *Die, Optional<dwarf::Form> Form, int64_t Integer);
327 /// addString - Add a string attribute data and value.
328 void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
330 /// addLocalString - Add a string attribute data and value.
331 void addLocalString(DIE *Die, dwarf::Attribute Attribute,
332 const StringRef Str);
334 /// addExpr - Add a Dwarf expression attribute data and value.
335 void addExpr(DIELoc *Die, dwarf::Form Form, const MCExpr *Expr);
337 /// addLabel - Add a Dwarf label attribute data and value.
338 void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
339 const MCSymbol *Label);
341 void addLabel(DIELoc *Die, dwarf::Form Form, const MCSymbol *Label);
343 /// addLocationList - Add a Dwarf loclistptr attribute data and value.
344 void addLocationList(DIE *Die, dwarf::Attribute Attribute, unsigned Index);
346 /// addSectionLabel - Add a Dwarf section label attribute data and value.
348 void addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
349 const MCSymbol *Label);
351 /// addSectionOffset - Add an offset into a section attribute data and value.
353 void addSectionOffset(DIE *Die, dwarf::Attribute Attribute, uint64_t Integer);
355 /// addOpAddress - Add a dwarf op address data and value using the
356 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
357 void addOpAddress(DIELoc *Die, const MCSymbol *Label);
359 /// addSectionDelta - Add a label delta attribute data and value.
360 void addSectionDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
363 /// addDIEEntry - Add a DIE attribute data and value.
364 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
366 /// addDIEEntry - Add a DIE attribute data and value.
367 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
369 void addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type);
371 /// addBlock - Add block data.
372 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIELoc *Block);
374 /// addBlock - Add block data.
375 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
377 /// addSourceLine - Add location information to specified debug information
379 void addSourceLine(DIE *Die, unsigned Line, StringRef File,
380 StringRef Directory);
381 void addSourceLine(DIE *Die, DIVariable V);
382 void addSourceLine(DIE *Die, DIGlobalVariable G);
383 void addSourceLine(DIE *Die, DISubprogram SP);
384 void addSourceLine(DIE *Die, DIType Ty);
385 void addSourceLine(DIE *Die, DINameSpace NS);
386 void addSourceLine(DIE *Die, DIObjCProperty Ty);
388 /// addAddress - Add an address attribute to a die based on the location
390 void addAddress(DIE *Die, dwarf::Attribute Attribute,
391 const MachineLocation &Location, bool Indirect = false);
393 /// addConstantValue - Add constant value entry in variable DIE.
394 void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
395 void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
396 void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
398 /// addConstantFPValue - Add constant value entry in variable DIE.
399 void addConstantFPValue(DIE *Die, const MachineOperand &MO);
400 void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
402 /// addTemplateParams - Add template parameters in buffer.
403 void addTemplateParams(DIE &Buffer, DIArray TParams);
405 /// addRegisterOp - Add register operand.
406 void addRegisterOp(DIELoc *TheDie, unsigned Reg);
408 /// addRegisterOffset - Add register offset.
409 void addRegisterOffset(DIELoc *TheDie, unsigned Reg, int64_t Offset);
411 /// addComplexAddress - Start with the address based on the location provided,
412 /// and generate the DWARF information necessary to find the actual variable
413 /// (navigating the extra location information encoded in the type) based on
414 /// the starting location. Add the DWARF information to the die.
415 void addComplexAddress(const DbgVariable &DV, DIE *Die,
416 dwarf::Attribute Attribute,
417 const MachineLocation &Location);
419 // FIXME: Should be reformulated in terms of addComplexAddress.
420 /// addBlockByrefAddress - Start with the address based on the location
421 /// provided, and generate the DWARF information necessary to find the
422 /// actual Block variable (navigating the Block struct) based on the
423 /// starting location. Add the DWARF information to the die. Obsolete,
424 /// please use addComplexAddress instead.
425 void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
426 dwarf::Attribute Attribute,
427 const MachineLocation &Location);
429 /// addVariableAddress - Add DW_AT_location attribute for a
430 /// DbgVariable based on provided MachineLocation.
431 void addVariableAddress(const DbgVariable &DV, DIE *Die,
432 MachineLocation Location);
434 /// addType - Add a new type attribute to the specified entity. This takes
435 /// and attribute parameter because DW_AT_friend attributes are also
437 void addType(DIE *Entity, DIType Ty,
438 dwarf::Attribute Attribute = dwarf::DW_AT_type);
440 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
441 DIE *getOrCreateNameSpace(DINameSpace NS);
443 /// getOrCreateSubprogramDIE - Create new DIE using SP.
444 DIE *getOrCreateSubprogramDIE(DISubprogram SP);
446 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
448 DIE *getOrCreateTypeDIE(const MDNode *N);
450 /// getOrCreateContextDIE - Get context owner's DIE.
451 DIE *createTypeDIE(DICompositeType Ty);
453 /// getOrCreateContextDIE - Get context owner's DIE.
454 DIE *getOrCreateContextDIE(DIScope Context);
456 /// constructContainingTypeDIEs - Construct DIEs for types that contain
458 void constructContainingTypeDIEs();
460 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
461 DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
463 /// constructSubprogramArguments - Construct function argument DIEs.
464 void constructSubprogramArguments(DIE &Buffer, DIArray Args);
466 /// Create a DIE with the given Tag, add the DIE to its parent, and
467 /// call insertDIE if MD is not null.
468 DIE *createAndAddDIE(unsigned Tag, DIE &Parent,
469 DIDescriptor N = DIDescriptor());
471 /// Compute the size of a header for this unit, not including the initial
473 virtual unsigned getHeaderSize() const {
474 return sizeof(int16_t) + // DWARF version number
475 sizeof(int32_t) + // Offset Into Abbrev. Section
476 sizeof(int8_t); // Pointer Size (in bytes)
479 /// Emit the header for this unit, not including the initial length field.
480 virtual void emitHeader(const MCSection *ASection,
481 const MCSymbol *ASectionSym) const;
483 virtual DwarfCompileUnit &getCU() = 0;
486 /// getOrCreateStaticMemberDIE - Create new static data member DIE.
487 DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
490 /// constructTypeDIE - Construct basic type die from DIBasicType.
491 void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
493 /// constructTypeDIE - Construct derived type die from DIDerivedType.
494 void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
496 /// constructTypeDIE - Construct type DIE from DICompositeType.
497 void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
499 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
500 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
502 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
503 void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
505 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
506 void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
508 /// constructMemberDIE - Construct member DIE from DIDerivedType.
509 void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
511 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
512 /// DITemplateTypeParameter.
513 void constructTemplateTypeParameterDIE(DIE &Buffer,
514 DITemplateTypeParameter TP);
516 /// constructTemplateValueParameterDIE - Construct new DIE for the given
517 /// DITemplateValueParameter.
518 void constructTemplateValueParameterDIE(DIE &Buffer,
519 DITemplateValueParameter TVP);
521 /// getLowerBoundDefault - Return the default lower bound for an array. If the
522 /// DWARF version doesn't handle the language, return -1.
523 int64_t getDefaultLowerBound() const;
525 /// getDIEEntry - Returns the debug information entry for the specified
527 DIEEntry *getDIEEntry(const MDNode *N) const {
528 return MDNodeToDIEEntryMap.lookup(N);
531 /// insertDIEEntry - Insert debug information entry into the map.
532 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
533 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
536 // getIndexTyDie - Get an anonymous type for index type.
537 DIE *getIndexTyDie() { return IndexTyDie; }
539 // setIndexTyDie - Set D as anonymous type for index which can be reused
541 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
543 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
544 /// information entry.
545 DIEEntry *createDIEEntry(DIE *Entry);
547 /// resolve - Look in the DwarfDebug map for the MDNode that
548 /// corresponds to the reference.
549 template <typename T> T resolve(DIRef<T> Ref) const {
550 return DD->resolve(Ref);
553 /// If this is a named finished type then include it in the list of types for
554 /// the accelerator tables.
555 void updateAcceleratorTables(DIScope Context, DIType Ty, const DIE *TyDIE);
558 class DwarfCompileUnit : public DwarfUnit {
559 /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
560 /// the need to search for it in applyStmtList.
561 unsigned stmtListIndex;
564 DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
565 DwarfDebug *DW, DwarfFile *DWU);
567 void initStmtList(MCSymbol *DwarfLineSectionSym);
569 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
570 void applyStmtList(DIE &D);
572 /// createGlobalVariableDIE - create global variable DIE.
573 void createGlobalVariableDIE(DIGlobalVariable GV);
575 /// addLabelAddress - Add a dwarf label attribute data and value using
576 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
577 void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label);
579 DwarfCompileUnit &getCU() override { return *this; }
582 class DwarfTypeUnit : public DwarfUnit {
584 uint64_t TypeSignature;
586 DwarfCompileUnit &CU;
589 DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU, AsmPrinter *A,
590 DwarfDebug *DW, DwarfFile *DWU);
592 void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
593 uint64_t getTypeSignature() const { return TypeSignature; }
594 void setType(const DIE *Ty) { this->Ty = Ty; }
596 /// Emit the header for this unit, not including the initial length field.
597 void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym) const
599 unsigned getHeaderSize() const override {
600 return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
601 sizeof(uint32_t); // Type DIE Offset
603 void initSection(const MCSection *Section);
604 DwarfCompileUnit &getCU() override { return CU; }
606 } // end llvm namespace