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/IR/DIBuilder.h"
23 #include "llvm/IR/DebugInfo.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCSection.h"
29 class MachineLocation;
34 class DwarfCompileUnit;
36 // Data structure to hold a range for range lists.
39 RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {}
40 const MCSymbol *getStart() const { return Start; }
41 const MCSymbol *getEnd() const { return End; }
44 const MCSymbol *Start, *End;
49 // Index for locating within the debug_range section this particular span.
52 SmallVector<RangeSpan, 2> Ranges;
55 RangeSpanList(MCSymbol *Sym) : RangeSym(Sym) {}
56 MCSymbol *getSym() const { return RangeSym; }
57 const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
58 void addRange(RangeSpan Range) { Ranges.push_back(Range); }
61 //===----------------------------------------------------------------------===//
62 /// Unit - This dwarf writer support class manages information associated
63 /// with a source file.
66 /// UniqueID - a numeric ID unique among all CUs in the module
69 /// Node - MDNode for the compile unit.
72 /// Unit debug information entry.
73 const std::unique_ptr<DIE> UnitDie;
75 /// Offset of the UnitDie from beginning of debug info section.
76 unsigned DebugInfoOffset;
78 /// Asm - Target of Dwarf emission.
81 // Holders for some common dwarf information.
85 /// IndexTyDie - An anonymous type for index type. Owned by UnitDie.
88 /// MDNodeToDieMap - Tracks the mapping of unit level debug information
89 /// variables to debug information entries.
90 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
92 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information
93 /// descriptors to debug information entries using a DIEEntry proxy.
94 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
96 /// GlobalNames - A map of globally visible named entities for this unit.
97 StringMap<const DIE *> GlobalNames;
99 /// GlobalTypes - A map of globally visible types for this unit.
100 StringMap<const DIE *> GlobalTypes;
102 /// AccelNames - A map of names for the name accelerator table.
103 StringMap<std::vector<const DIE *> > AccelNames;
105 /// AccelObjC - A map of objc spec for the objc accelerator table.
106 StringMap<std::vector<const DIE *> > AccelObjC;
108 /// AccelNamespace - A map of names for the namespace accelerator table.
109 StringMap<std::vector<const DIE *> > AccelNamespace;
111 /// AccelTypes - A map of names for the type accelerator table.
112 StringMap<std::vector<std::pair<const DIE *, unsigned> > > AccelTypes;
114 /// DIEBlocks - A list of all the DIEBlocks in use.
115 std::vector<DIEBlock *> DIEBlocks;
117 /// DIELocs - A list of all the DIELocs in use.
118 std::vector<DIELoc *> DIELocs;
120 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
121 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
122 /// corresponds to the MDNode mapped with the subprogram DIE.
123 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
125 // List of ranges for a given compile unit.
126 SmallVector<RangeSpan, 1> CURanges;
128 // List of range lists for a given compile unit, separate from the ranges for
130 SmallVector<RangeSpanList, 1> CURangeLists;
132 // DIEValueAllocator - All DIEValues are allocated through this allocator.
133 BumpPtrAllocator DIEValueAllocator;
135 // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
136 DIEInteger *DIEIntegerOne;
138 /// The section this unit will be emitted in.
139 const MCSection *Section;
141 /// A label at the start of the non-dwo section related to this unit.
142 MCSymbol *SectionSym;
144 /// The start of the unit within its section.
145 MCSymbol *LabelBegin;
147 /// The end of the unit within its section.
150 /// The label for the start of the range sets for the elements of this unit.
151 MCSymbol *LabelRange;
153 /// Skeleton unit associated with this unit.
156 DwarfUnit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A,
157 DwarfDebug *DW, DwarfFile *DWU);
160 virtual ~DwarfUnit();
162 /// Set the skeleton unit associated with this unit.
163 void setSkeleton(DwarfUnit *Skel) { Skeleton = Skel; }
165 /// Get the skeleton unit associated with this unit.
166 DwarfUnit *getSkeleton() const { return Skeleton; }
168 /// Pass in the SectionSym even though we could recreate it in every compile
169 /// unit (type units will have actually distinct symbols once they're in
170 /// comdat sections).
171 void initSection(const MCSection *Section, MCSymbol *SectionSym) {
172 assert(!this->Section);
173 this->Section = Section;
174 this->SectionSym = SectionSym;
176 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
178 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
179 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
182 const MCSection *getSection() const {
187 /// If there's a skeleton then return the section symbol for the skeleton
188 /// unit, otherwise return the section symbol for this unit.
189 MCSymbol *getLocalSectionSym() const {
191 return Skeleton->getSectionSym();
192 return getSectionSym();
195 MCSymbol *getSectionSym() const {
200 /// If there's a skeleton then return the begin label for the skeleton unit,
201 /// otherwise return the local label for this unit.
202 MCSymbol *getLocalLabelBegin() const {
204 return Skeleton->getLabelBegin();
205 return getLabelBegin();
208 MCSymbol *getLabelBegin() const {
213 MCSymbol *getLabelEnd() const {
218 MCSymbol *getLabelRange() const {
224 unsigned getUniqueID() const { return UniqueID; }
225 uint16_t getLanguage() const { return CUNode.getLanguage(); }
226 DICompileUnit getCUNode() const { return CUNode; }
227 DIE *getUnitDie() const { return UnitDie.get(); }
228 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
229 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
231 const StringMap<std::vector<const DIE *> > &getAccelNames() const {
234 const StringMap<std::vector<const DIE *> > &getAccelObjC() const {
237 const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
238 return AccelNamespace;
240 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
241 getAccelTypes() const {
245 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
246 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
248 /// hasContent - Return true if this compile unit has something to write out.
249 bool hasContent() const { return !UnitDie->getChildren().empty(); }
251 /// addRange - Add an address range to the list of ranges for this unit.
252 void addRange(RangeSpan Range) {
253 // Only add a range for this unit if we're emitting full debug.
254 if (getCUNode().getEmissionKind() == DIBuilder::FullDebug)
255 CURanges.push_back(Range);
258 /// getRanges - Get the list of ranges for this unit.
259 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
260 SmallVectorImpl<RangeSpan> &getRanges() { return CURanges; }
262 /// addRangeList - Add an address range list to the list of range lists.
263 void addRangeList(RangeSpanList Ranges) { CURangeLists.push_back(Ranges); }
265 /// getRangeLists - Get the vector of range lists.
266 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
269 SmallVectorImpl<RangeSpanList> &getRangeLists() { return CURangeLists; }
271 /// getParentContextString - Get a string containing the language specific
272 /// context for a global name.
273 std::string getParentContextString(DIScope Context) const;
275 /// addGlobalName - Add a new global entity to the compile unit.
277 void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
279 /// addAccelName - Add a new name to the name accelerator table.
280 void addAccelName(StringRef Name, const DIE *Die);
282 /// addAccelObjC - Add a new name to the ObjC accelerator table.
283 void addAccelObjC(StringRef Name, const DIE *Die);
285 /// addAccelNamespace - Add a new name to the namespace accelerator table.
286 void addAccelNamespace(StringRef Name, const DIE *Die);
288 /// addAccelType - Add a new type to the type accelerator table.
289 void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
291 /// getDIE - Returns the debug information entry map slot for the
292 /// specified debug variable. We delegate the request to DwarfDebug
293 /// when the MDNode can be part of the type system, since DIEs for
294 /// the type system can be shared across CUs and the mappings are
295 /// kept in DwarfDebug.
296 DIE *getDIE(DIDescriptor D) const;
298 /// getDIELoc - Returns a fresh newly allocated DIELoc.
299 DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc(); }
301 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
302 /// when the MDNode can be part of the type system, since DIEs for
303 /// the type system can be shared across CUs and the mappings are
304 /// kept in DwarfDebug.
305 void insertDIE(DIDescriptor Desc, DIE *D);
307 /// addDie - Adds or interns the DIE to the compile unit.
309 void addDie(DIE *Buffer) { UnitDie->addChild(Buffer); }
311 /// addFlag - Add a flag that is true to the DIE.
312 void addFlag(DIE *Die, dwarf::Attribute Attribute);
314 /// addUInt - Add an unsigned integer attribute data and value.
315 void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
318 void addUInt(DIE *Block, dwarf::Form Form, uint64_t Integer);
320 /// addSInt - Add an signed integer attribute data and value.
321 void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
324 void addSInt(DIELoc *Die, Optional<dwarf::Form> Form, int64_t Integer);
326 /// addString - Add a string attribute data and value.
327 void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
329 /// addLocalString - Add a string attribute data and value.
330 void addLocalString(DIE *Die, dwarf::Attribute Attribute,
331 const StringRef Str);
333 /// addExpr - Add a Dwarf expression attribute data and value.
334 void addExpr(DIELoc *Die, dwarf::Form Form, const MCExpr *Expr);
336 /// addLabel - Add a Dwarf label attribute data and value.
337 void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
338 const MCSymbol *Label);
340 void addLabel(DIELoc *Die, dwarf::Form Form, const MCSymbol *Label);
342 /// addLocationList - Add a Dwarf loclistptr attribute data and value.
343 void addLocationList(DIE *Die, dwarf::Attribute Attribute, unsigned Index);
345 /// addSectionLabel - Add a Dwarf section label attribute data and value.
347 void addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
348 const MCSymbol *Label);
350 /// addSectionOffset - Add an offset into a section attribute data and value.
352 void addSectionOffset(DIE *Die, dwarf::Attribute Attribute, uint64_t Integer);
354 /// addOpAddress - Add a dwarf op address data and value using the
355 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
356 void addOpAddress(DIELoc *Die, const MCSymbol *Label);
358 /// addSectionDelta - Add a label delta attribute data and value.
359 void addSectionDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
362 /// addDIEEntry - Add a DIE attribute data and value.
363 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
365 /// addDIEEntry - Add a DIE attribute data and value.
366 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
368 void addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type);
370 /// addBlock - Add block data.
371 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIELoc *Block);
373 /// addBlock - Add block data.
374 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
376 /// addSourceLine - Add location information to specified debug information
378 void addSourceLine(DIE *Die, unsigned Line, StringRef File,
379 StringRef Directory);
380 void addSourceLine(DIE *Die, DIVariable V);
381 void addSourceLine(DIE *Die, DIGlobalVariable G);
382 void addSourceLine(DIE *Die, DISubprogram SP);
383 void addSourceLine(DIE *Die, DIType Ty);
384 void addSourceLine(DIE *Die, DINameSpace NS);
385 void addSourceLine(DIE *Die, DIObjCProperty Ty);
387 /// addAddress - Add an address attribute to a die based on the location
389 void addAddress(DIE *Die, dwarf::Attribute Attribute,
390 const MachineLocation &Location, bool Indirect = false);
392 /// addConstantValue - Add constant value entry in variable DIE.
393 void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
394 void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
395 void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
397 /// addConstantFPValue - Add constant value entry in variable DIE.
398 void addConstantFPValue(DIE *Die, const MachineOperand &MO);
399 void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
401 /// addTemplateParams - Add template parameters in buffer.
402 void addTemplateParams(DIE &Buffer, DIArray TParams);
404 /// addRegisterOp - Add register operand.
405 void addRegisterOp(DIELoc *TheDie, unsigned Reg);
407 /// addRegisterOffset - Add register offset.
408 void addRegisterOffset(DIELoc *TheDie, unsigned Reg, int64_t Offset);
410 /// addComplexAddress - Start with the address based on the location provided,
411 /// and generate the DWARF information necessary to find the actual variable
412 /// (navigating the extra location information encoded in the type) based on
413 /// the starting location. Add the DWARF information to the die.
414 void addComplexAddress(const DbgVariable &DV, DIE *Die,
415 dwarf::Attribute Attribute,
416 const MachineLocation &Location);
418 // FIXME: Should be reformulated in terms of addComplexAddress.
419 /// addBlockByrefAddress - Start with the address based on the location
420 /// provided, and generate the DWARF information necessary to find the
421 /// actual Block variable (navigating the Block struct) based on the
422 /// starting location. Add the DWARF information to the die. Obsolete,
423 /// please use addComplexAddress instead.
424 void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
425 dwarf::Attribute Attribute,
426 const MachineLocation &Location);
428 /// addVariableAddress - Add DW_AT_location attribute for a
429 /// DbgVariable based on provided MachineLocation.
430 void addVariableAddress(const DbgVariable &DV, DIE *Die,
431 MachineLocation Location);
433 /// addType - Add a new type attribute to the specified entity. This takes
434 /// and attribute parameter because DW_AT_friend attributes are also
436 void addType(DIE *Entity, DIType Ty,
437 dwarf::Attribute Attribute = dwarf::DW_AT_type);
439 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
440 DIE *getOrCreateNameSpace(DINameSpace NS);
442 /// getOrCreateSubprogramDIE - Create new DIE using SP.
443 DIE *getOrCreateSubprogramDIE(DISubprogram SP);
445 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
447 DIE *getOrCreateTypeDIE(const MDNode *N);
449 /// getOrCreateContextDIE - Get context owner's DIE.
450 DIE *createTypeDIE(DICompositeType Ty);
452 /// getOrCreateContextDIE - Get context owner's DIE.
453 DIE *getOrCreateContextDIE(DIScope Context);
455 /// constructContainingTypeDIEs - Construct DIEs for types that contain
457 void constructContainingTypeDIEs();
459 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
460 DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
462 /// constructSubprogramArguments - Construct function argument DIEs.
463 void constructSubprogramArguments(DIE &Buffer, DIArray Args);
465 /// Create a DIE with the given Tag, add the DIE to its parent, and
466 /// call insertDIE if MD is not null.
467 DIE *createAndAddDIE(unsigned Tag, DIE &Parent,
468 DIDescriptor N = DIDescriptor());
470 /// Compute the size of a header for this unit, not including the initial
472 virtual unsigned getHeaderSize() const {
473 return sizeof(int16_t) + // DWARF version number
474 sizeof(int32_t) + // Offset Into Abbrev. Section
475 sizeof(int8_t); // Pointer Size (in bytes)
478 /// Emit the header for this unit, not including the initial length field.
479 virtual void emitHeader(const MCSection *ASection,
480 const MCSymbol *ASectionSym) const;
482 virtual DwarfCompileUnit &getCU() = 0;
485 /// getOrCreateStaticMemberDIE - Create new static data member DIE.
486 DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
489 /// constructTypeDIE - Construct basic type die from DIBasicType.
490 void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
492 /// constructTypeDIE - Construct derived type die from DIDerivedType.
493 void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
495 /// constructTypeDIE - Construct type DIE from DICompositeType.
496 void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
498 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
499 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
501 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
502 void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
504 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
505 void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
507 /// constructMemberDIE - Construct member DIE from DIDerivedType.
508 void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
510 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
511 /// DITemplateTypeParameter.
512 void constructTemplateTypeParameterDIE(DIE &Buffer,
513 DITemplateTypeParameter TP);
515 /// constructTemplateValueParameterDIE - Construct new DIE for the given
516 /// DITemplateValueParameter.
517 void constructTemplateValueParameterDIE(DIE &Buffer,
518 DITemplateValueParameter TVP);
520 /// getLowerBoundDefault - Return the default lower bound for an array. If the
521 /// DWARF version doesn't handle the language, return -1.
522 int64_t getDefaultLowerBound() const;
524 /// getDIEEntry - Returns the debug information entry for the specified
526 DIEEntry *getDIEEntry(const MDNode *N) const {
527 return MDNodeToDIEEntryMap.lookup(N);
530 /// insertDIEEntry - Insert debug information entry into the map.
531 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
532 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
535 // getIndexTyDie - Get an anonymous type for index type.
536 DIE *getIndexTyDie() { return IndexTyDie; }
538 // setIndexTyDie - Set D as anonymous type for index which can be reused
540 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
542 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
543 /// information entry.
544 DIEEntry *createDIEEntry(DIE *Entry);
546 /// resolve - Look in the DwarfDebug map for the MDNode that
547 /// corresponds to the reference.
548 template <typename T> T resolve(DIRef<T> Ref) const {
549 return DD->resolve(Ref);
552 /// If this is a named finished type then include it in the list of types for
553 /// the accelerator tables.
554 void updateAcceleratorTables(DIScope Context, DIType Ty, const DIE *TyDIE);
557 class DwarfCompileUnit : public DwarfUnit {
558 /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
559 /// the need to search for it in applyStmtList.
560 unsigned stmtListIndex;
563 DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
564 DwarfDebug *DW, DwarfFile *DWU);
566 void initStmtList(MCSymbol *DwarfLineSectionSym);
568 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
569 void applyStmtList(DIE &D);
571 /// createGlobalVariableDIE - create global variable DIE.
572 void createGlobalVariableDIE(DIGlobalVariable GV);
574 /// addLabelAddress - Add a dwarf label attribute data and value using
575 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
576 void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label);
578 DwarfCompileUnit &getCU() override { return *this; }
581 class DwarfTypeUnit : public DwarfUnit {
583 uint64_t TypeSignature;
585 DwarfCompileUnit &CU;
588 DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU, AsmPrinter *A,
589 DwarfDebug *DW, DwarfFile *DWU);
591 void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
592 uint64_t getTypeSignature() const { return TypeSignature; }
593 void setType(const DIE *Ty) { this->Ty = Ty; }
595 /// Emit the header for this unit, not including the initial length field.
596 void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym) const
598 unsigned getHeaderSize() const override {
599 return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
600 sizeof(uint32_t); // Type DIE Offset
602 void initSection(const MCSection *Section);
603 DwarfCompileUnit &getCU() override { return CU; }
605 } // end llvm namespace