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 /// AccelTypes - A map of names for the type accelerator table.
106 StringMap<std::vector<std::pair<const DIE *, unsigned> > > AccelTypes;
108 /// DIEBlocks - A list of all the DIEBlocks in use.
109 std::vector<DIEBlock *> DIEBlocks;
111 /// DIELocs - A list of all the DIELocs in use.
112 std::vector<DIELoc *> DIELocs;
114 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
115 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
116 /// corresponds to the MDNode mapped with the subprogram DIE.
117 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
119 // List of ranges for a given compile unit.
120 SmallVector<RangeSpan, 1> CURanges;
122 // List of range lists for a given compile unit, separate from the ranges for
124 SmallVector<RangeSpanList, 1> CURangeLists;
126 // DIEValueAllocator - All DIEValues are allocated through this allocator.
127 BumpPtrAllocator DIEValueAllocator;
129 // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
130 DIEInteger *DIEIntegerOne;
132 /// The section this unit will be emitted in.
133 const MCSection *Section;
135 /// A label at the start of the non-dwo section related to this unit.
136 MCSymbol *SectionSym;
138 /// The start of the unit within its section.
139 MCSymbol *LabelBegin;
141 /// The end of the unit within its section.
144 /// The label for the start of the range sets for the elements of this unit.
145 MCSymbol *LabelRange;
147 /// Skeleton unit associated with this unit.
150 DwarfUnit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A,
151 DwarfDebug *DW, DwarfFile *DWU);
154 virtual ~DwarfUnit();
156 /// Set the skeleton unit associated with this unit.
157 void setSkeleton(DwarfUnit &Skel) { Skeleton = &Skel; }
159 /// Get the skeleton unit associated with this unit.
160 DwarfUnit *getSkeleton() const { return Skeleton; }
162 /// Pass in the SectionSym even though we could recreate it in every compile
163 /// unit (type units will have actually distinct symbols once they're in
164 /// comdat sections).
165 void initSection(const MCSection *Section, MCSymbol *SectionSym) {
166 assert(!this->Section);
167 this->Section = Section;
168 this->SectionSym = SectionSym;
170 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
172 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
173 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
176 const MCSection *getSection() const {
181 /// If there's a skeleton then return the section symbol for the skeleton
182 /// unit, otherwise return the section symbol for this unit.
183 MCSymbol *getLocalSectionSym() const {
185 return Skeleton->getSectionSym();
186 return getSectionSym();
189 MCSymbol *getSectionSym() const {
194 /// If there's a skeleton then return the begin label for the skeleton unit,
195 /// otherwise return the local label for this unit.
196 MCSymbol *getLocalLabelBegin() const {
198 return Skeleton->getLabelBegin();
199 return getLabelBegin();
202 MCSymbol *getLabelBegin() const {
207 MCSymbol *getLabelEnd() const {
212 MCSymbol *getLabelRange() const {
218 unsigned getUniqueID() const { return UniqueID; }
219 uint16_t getLanguage() const { return CUNode.getLanguage(); }
220 DICompileUnit getCUNode() const { return CUNode; }
221 DIE *getUnitDie() const { return UnitDie.get(); }
222 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
223 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
225 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
226 getAccelTypes() const {
230 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
231 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
233 /// hasContent - Return true if this compile unit has something to write out.
234 bool hasContent() const { return !UnitDie->getChildren().empty(); }
236 /// addRange - Add an address range to the list of ranges for this unit.
237 void addRange(RangeSpan Range);
239 /// getRanges - Get the list of ranges for this unit.
240 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
241 SmallVectorImpl<RangeSpan> &getRanges() { return CURanges; }
243 /// addRangeList - Add an address range list to the list of range lists.
244 void addRangeList(RangeSpanList Ranges) { CURangeLists.push_back(Ranges); }
246 /// getRangeLists - Get the vector of range lists.
247 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
250 SmallVectorImpl<RangeSpanList> &getRangeLists() { return CURangeLists; }
252 /// getParentContextString - Get a string containing the language specific
253 /// context for a global name.
254 std::string getParentContextString(DIScope Context) const;
256 /// addGlobalName - Add a new global entity to the compile unit.
258 void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
260 /// addAccelNamespace - Add a new name to the namespace accelerator table.
261 void addAccelNamespace(StringRef Name, const DIE *Die);
263 /// addAccelType - Add a new type to the type accelerator table.
264 void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
266 /// getDIE - Returns the debug information entry map slot for the
267 /// specified debug variable. We delegate the request to DwarfDebug
268 /// when the MDNode can be part of the type system, since DIEs for
269 /// the type system can be shared across CUs and the mappings are
270 /// kept in DwarfDebug.
271 DIE *getDIE(DIDescriptor D) const;
273 /// getDIELoc - Returns a fresh newly allocated DIELoc.
274 DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc(); }
276 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
277 /// when the MDNode can be part of the type system, since DIEs for
278 /// the type system can be shared across CUs and the mappings are
279 /// kept in DwarfDebug.
280 void insertDIE(DIDescriptor Desc, DIE *D);
282 /// addDie - Adds or interns the DIE to the compile unit.
284 void addDie(DIE *Buffer) { UnitDie->addChild(Buffer); }
286 /// addFlag - Add a flag that is true to the DIE.
287 void addFlag(DIE *Die, dwarf::Attribute Attribute);
289 /// addUInt - Add an unsigned integer attribute data and value.
290 void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
293 void addUInt(DIE *Block, dwarf::Form Form, uint64_t Integer);
295 /// addSInt - Add an signed integer attribute data and value.
296 void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
299 void addSInt(DIELoc *Die, Optional<dwarf::Form> Form, int64_t Integer);
301 /// addString - Add a string attribute data and value.
302 void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
304 /// addLocalString - Add a string attribute data and value.
305 void addLocalString(DIE *Die, dwarf::Attribute Attribute,
306 const StringRef Str);
308 /// addExpr - Add a Dwarf expression attribute data and value.
309 void addExpr(DIELoc *Die, dwarf::Form Form, const MCExpr *Expr);
311 /// addLabel - Add a Dwarf label attribute data and value.
312 void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
313 const MCSymbol *Label);
315 void addLabel(DIELoc *Die, dwarf::Form Form, const MCSymbol *Label);
317 /// addLocationList - Add a Dwarf loclistptr attribute data and value.
318 void addLocationList(DIE *Die, dwarf::Attribute Attribute, unsigned Index);
320 /// addSectionLabel - Add a Dwarf section label attribute data and value.
322 void addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
323 const MCSymbol *Label);
325 /// addSectionOffset - Add an offset into a section attribute data and value.
327 void addSectionOffset(DIE *Die, dwarf::Attribute Attribute, uint64_t Integer);
329 /// addOpAddress - Add a dwarf op address data and value using the
330 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
331 void addOpAddress(DIELoc *Die, const MCSymbol *Label);
333 /// addSectionDelta - Add a label delta attribute data and value.
334 void addSectionDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
337 /// addLabelDelta - Add a label delta attribute data and value.
338 void addLabelDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
341 /// addDIEEntry - Add a DIE attribute data and value.
342 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
344 /// addDIEEntry - Add a DIE attribute data and value.
345 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
347 void addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type);
349 /// addBlock - Add block data.
350 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIELoc *Block);
352 /// addBlock - Add block data.
353 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
355 /// addSourceLine - Add location information to specified debug information
357 void addSourceLine(DIE *Die, unsigned Line, StringRef File,
358 StringRef Directory);
359 void addSourceLine(DIE *Die, DIVariable V);
360 void addSourceLine(DIE *Die, DIGlobalVariable G);
361 void addSourceLine(DIE *Die, DISubprogram SP);
362 void addSourceLine(DIE *Die, DIType Ty);
363 void addSourceLine(DIE *Die, DINameSpace NS);
364 void addSourceLine(DIE *Die, DIObjCProperty Ty);
366 /// addAddress - Add an address attribute to a die based on the location
368 void addAddress(DIE *Die, dwarf::Attribute Attribute,
369 const MachineLocation &Location, bool Indirect = false);
371 /// addConstantValue - Add constant value entry in variable DIE.
372 void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
373 void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
374 void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
376 /// addConstantFPValue - Add constant value entry in variable DIE.
377 void addConstantFPValue(DIE *Die, const MachineOperand &MO);
378 void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
380 /// addTemplateParams - Add template parameters in buffer.
381 void addTemplateParams(DIE &Buffer, DIArray TParams);
383 /// addRegisterOp - Add register operand.
384 void addRegisterOp(DIELoc *TheDie, unsigned Reg);
386 /// addRegisterOffset - Add register offset.
387 void addRegisterOffset(DIELoc *TheDie, unsigned Reg, int64_t Offset);
389 /// addComplexAddress - Start with the address based on the location provided,
390 /// and generate the DWARF information necessary to find the actual variable
391 /// (navigating the extra location information encoded in the type) based on
392 /// the starting location. Add the DWARF information to the die.
393 void addComplexAddress(const DbgVariable &DV, DIE *Die,
394 dwarf::Attribute Attribute,
395 const MachineLocation &Location);
397 // FIXME: Should be reformulated in terms of addComplexAddress.
398 /// addBlockByrefAddress - Start with the address based on the location
399 /// provided, and generate the DWARF information necessary to find the
400 /// actual Block variable (navigating the Block struct) based on the
401 /// starting location. Add the DWARF information to the die. Obsolete,
402 /// please use addComplexAddress instead.
403 void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
404 dwarf::Attribute Attribute,
405 const MachineLocation &Location);
407 /// addVariableAddress - Add DW_AT_location attribute for a
408 /// DbgVariable based on provided MachineLocation.
409 void addVariableAddress(const DbgVariable &DV, DIE *Die,
410 MachineLocation Location);
412 /// addType - Add a new type attribute to the specified entity. This takes
413 /// and attribute parameter because DW_AT_friend attributes are also
415 void addType(DIE *Entity, DIType Ty,
416 dwarf::Attribute Attribute = dwarf::DW_AT_type);
418 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
419 DIE *getOrCreateNameSpace(DINameSpace NS);
421 /// getOrCreateSubprogramDIE - Create new DIE using SP.
422 DIE *getOrCreateSubprogramDIE(DISubprogram SP);
424 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
426 DIE *getOrCreateTypeDIE(const MDNode *N);
428 /// getOrCreateContextDIE - Get context owner's DIE.
429 DIE *createTypeDIE(DICompositeType Ty);
431 /// getOrCreateContextDIE - Get context owner's DIE.
432 DIE *getOrCreateContextDIE(DIScope Context);
434 /// constructContainingTypeDIEs - Construct DIEs for types that contain
436 void constructContainingTypeDIEs();
438 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
439 DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
441 /// constructSubprogramArguments - Construct function argument DIEs.
442 void constructSubprogramArguments(DIE &Buffer, DIArray Args);
444 /// Create a DIE with the given Tag, add the DIE to its parent, and
445 /// call insertDIE if MD is not null.
446 DIE *createAndAddDIE(unsigned Tag, DIE &Parent,
447 DIDescriptor N = DIDescriptor());
449 /// Compute the size of a header for this unit, not including the initial
451 virtual unsigned getHeaderSize() const {
452 return sizeof(int16_t) + // DWARF version number
453 sizeof(int32_t) + // Offset Into Abbrev. Section
454 sizeof(int8_t); // Pointer Size (in bytes)
457 /// Emit the header for this unit, not including the initial length field.
458 virtual void emitHeader(const MCSymbol *ASectionSym) const;
460 virtual DwarfCompileUnit &getCU() = 0;
463 /// getOrCreateStaticMemberDIE - Create new static data member DIE.
464 DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
466 /// Look up the source ID with the given directory and source file names. If
467 /// none currently exists, create a new ID and insert it in the line table.
468 virtual unsigned getOrCreateSourceID(StringRef File, StringRef Directory) = 0;
471 /// constructTypeDIE - Construct basic type die from DIBasicType.
472 void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
474 /// constructTypeDIE - Construct derived type die from DIDerivedType.
475 void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
477 /// constructTypeDIE - Construct type DIE from DICompositeType.
478 void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
480 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
481 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
483 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
484 void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
486 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
487 void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
489 /// constructMemberDIE - Construct member DIE from DIDerivedType.
490 void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
492 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
493 /// DITemplateTypeParameter.
494 void constructTemplateTypeParameterDIE(DIE &Buffer,
495 DITemplateTypeParameter TP);
497 /// constructTemplateValueParameterDIE - Construct new DIE for the given
498 /// DITemplateValueParameter.
499 void constructTemplateValueParameterDIE(DIE &Buffer,
500 DITemplateValueParameter TVP);
502 /// getLowerBoundDefault - Return the default lower bound for an array. If the
503 /// DWARF version doesn't handle the language, return -1.
504 int64_t getDefaultLowerBound() const;
506 /// getDIEEntry - Returns the debug information entry for the specified
508 DIEEntry *getDIEEntry(const MDNode *N) const {
509 return MDNodeToDIEEntryMap.lookup(N);
512 /// insertDIEEntry - Insert debug information entry into the map.
513 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
514 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
517 // getIndexTyDie - Get an anonymous type for index type.
518 DIE *getIndexTyDie() { return IndexTyDie; }
520 // setIndexTyDie - Set D as anonymous type for index which can be reused
522 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
524 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
525 /// information entry.
526 DIEEntry *createDIEEntry(DIE *Entry);
528 /// resolve - Look in the DwarfDebug map for the MDNode that
529 /// corresponds to the reference.
530 template <typename T> T resolve(DIRef<T> Ref) const {
531 return DD->resolve(Ref);
534 /// If this is a named finished type then include it in the list of types for
535 /// the accelerator tables.
536 void updateAcceleratorTables(DIScope Context, DIType Ty, const DIE *TyDIE);
539 class DwarfCompileUnit : public DwarfUnit {
540 /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
541 /// the need to search for it in applyStmtList.
542 unsigned stmtListIndex;
545 DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
546 DwarfDebug *DW, DwarfFile *DWU);
548 void initStmtList(MCSymbol *DwarfLineSectionSym);
550 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
551 void applyStmtList(DIE &D);
553 /// createGlobalVariableDIE - create global variable DIE.
554 void createGlobalVariableDIE(DIGlobalVariable GV);
556 /// addLabelAddress - Add a dwarf label attribute data and value using
557 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
558 void addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
559 const MCSymbol *Label);
561 /// addLocalLabelAddress - Add a dwarf label attribute data and value using
562 /// DW_FORM_addr only.
563 void addLocalLabelAddress(DIE *Die, dwarf::Attribute Attribute,
564 const MCSymbol *Label);
566 DwarfCompileUnit &getCU() override { return *this; }
568 unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override;
571 class DwarfTypeUnit : public DwarfUnit {
573 uint64_t TypeSignature;
575 DwarfCompileUnit &CU;
576 MCDwarfDwoLineTable *SplitLineTable;
579 DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU, AsmPrinter *A,
580 DwarfDebug *DW, DwarfFile *DWU,
581 MCDwarfDwoLineTable *SplitLineTable = nullptr);
583 void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
584 uint64_t getTypeSignature() const { return TypeSignature; }
585 void setType(const DIE *Ty) { this->Ty = Ty; }
587 /// Emit the header for this unit, not including the initial length field.
588 void emitHeader(const MCSymbol *ASectionSym) const override;
589 unsigned getHeaderSize() const override {
590 return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
591 sizeof(uint32_t); // Type DIE Offset
593 void initSection(const MCSection *Section);
594 DwarfCompileUnit &getCU() override { return CU; }
597 unsigned getOrCreateSourceID(StringRef File, StringRef Directory) override;
599 } // end llvm namespace