1 //===-- llvm/CodeGen/DwarfUnit.h - Dwarf Compile Unit ---*- C++ -*--===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for writing dwarf compile unit.
11 //
12 //===----------------------------------------------------------------------===//
14 #ifndef CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
15 #define CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
17 #include "DIE.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/DebugInfo.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCSection.h"
27 namespace llvm {
29 class MachineLocation;
30 class MachineOperand;
31 class ConstantInt;
32 class ConstantFP;
33 class DbgVariable;
35 // Data structure to hold a range for range lists.
36 class RangeSpan {
37 public:
38 RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {}
39 const MCSymbol *getStart() const { return Start; }
40 const MCSymbol *getEnd() const { return End; }
42 private:
43 const MCSymbol *Start, *End;
44 };
46 class RangeSpanList {
47 private:
48 // Index for locating within the debug_range section this particular span.
49 MCSymbol *RangeSym;
50 // List of ranges.
51 SmallVector<RangeSpan, 2> Ranges;
53 public:
54 RangeSpanList(MCSymbol *Sym) : RangeSym(Sym) {}
55 MCSymbol *getSym() const { return RangeSym; }
56 const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
57 void addRange(RangeSpan Range) { Ranges.push_back(Range); }
58 };
60 //===----------------------------------------------------------------------===//
61 /// Unit - This dwarf writer support class manages information associated
62 /// with a source file.
63 class DwarfUnit {
64 protected:
65 /// UniqueID - a numeric ID unique among all CUs in the module
66 unsigned UniqueID;
68 /// Unit debug information entry.
69 const OwningPtr<DIE> UnitDie;
71 /// Offset of the UnitDie from beginning of debug info section.
72 unsigned DebugInfoOffset;
74 /// Asm - Target of Dwarf emission.
75 AsmPrinter *Asm;
77 // Holders for some common dwarf information.
78 DwarfDebug *DD;
79 DwarfFile *DU;
81 /// IndexTyDie - An anonymous type for index type. Owned by UnitDie.
82 DIE *IndexTyDie;
84 /// MDNodeToDieMap - Tracks the mapping of unit level debug information
85 /// variables to debug information entries.
86 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
88 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information
89 /// descriptors to debug information entries using a DIEEntry proxy.
90 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
92 /// GlobalNames - A map of globally visible named entities for this unit.
93 StringMap<const DIE *> GlobalNames;
95 /// GlobalTypes - A map of globally visible types for this unit.
96 StringMap<const DIE *> GlobalTypes;
98 /// AccelNames - A map of names for the name accelerator table.
99 StringMap<std::vector<const DIE *> > AccelNames;
101 /// AccelObjC - A map of objc spec for the objc accelerator table.
102 StringMap<std::vector<const DIE *> > AccelObjC;
104 /// AccelNamespace - A map of names for the namespace accelerator table.
105 StringMap<std::vector<const DIE *> > AccelNamespace;
107 /// AccelTypes - A map of names for the type accelerator table.
108 StringMap<std::vector<std::pair<const DIE *, unsigned> > > AccelTypes;
110 /// DIEBlocks - A list of all the DIEBlocks in use.
111 std::vector<DIEBlock *> DIEBlocks;
113 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
114 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
115 /// corresponds to the MDNode mapped with the subprogram DIE.
116 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
118 // List of ranges for a given compile unit.
119 SmallVector<RangeSpan, 1> CURanges;
121 // List of range lists for a given compile unit, separate from the ranges for
122 // the CU itself.
123 SmallVector<RangeSpanList, 1> CURangeLists;
125 // DIEValueAllocator - All DIEValues are allocated through this allocator.
126 BumpPtrAllocator DIEValueAllocator;
128 // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
129 DIEInteger *DIEIntegerOne;
131 /// The section this unit will be emitted in.
132 const MCSection *Section;
134 /// A label at the start of the non-dwo section related to this unit.
135 MCSymbol *SectionSym;
137 /// The start of the unit within its section.
138 MCSymbol *LabelBegin;
140 /// The end of the unit within its section.
141 MCSymbol *LabelEnd;
143 /// The label for the start of the range sets for the elements of this unit.
144 MCSymbol *LabelRange;
146 /// Skeleton unit associated with this unit.
147 DwarfUnit *Skeleton;
149 DwarfUnit(unsigned UID, DIE *D, AsmPrinter *A, DwarfDebug *DW,
150 DwarfFile *DWU);
152 public:
153 virtual ~DwarfUnit();
155 /// Set the skeleton unit associated with this unit.
156 void setSkeleton(DwarfUnit *Skel) { Skeleton = Skel; }
158 /// Get the skeleton unit associated with this unit.
159 DwarfUnit *getSkeleton() const { return Skeleton; }
161 /// Pass in the SectionSym even though we could recreate it in every compile
162 /// unit (type units will have actually distinct symbols once they're in
163 /// comdat sections).
164 void initSection(const MCSection *Section, MCSymbol *SectionSym) {
165 assert(!this->Section);
166 this->Section = Section;
167 this->SectionSym = SectionSym;
168 this->LabelBegin =
169 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
170 this->LabelEnd =
171 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
172 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
173 }
175 const MCSection *getSection() const {
176 assert(Section);
177 return Section;
178 }
180 /// If there's a skeleton then return the section symbol for the skeleton
181 /// unit, otherwise return the section symbol for this unit.
182 MCSymbol *getLocalSectionSym() const {
183 if (Skeleton)
184 return Skeleton->getSectionSym();
185 return getSectionSym();
186 }
188 MCSymbol *getSectionSym() const {
189 assert(Section);
190 return SectionSym;
191 }
193 /// If there's a skeleton then return the begin label for the skeleton unit,
194 /// otherwise return the local label for this unit.
195 MCSymbol *getLocalLabelBegin() const {
196 if (Skeleton)
197 return Skeleton->getLabelBegin();
198 return getLabelBegin();
199 }
201 MCSymbol *getLabelBegin() const {
202 assert(Section);
203 return LabelBegin;
204 }
206 MCSymbol *getLabelEnd() const {
207 assert(Section);
208 return LabelEnd;
209 }
211 MCSymbol *getLabelRange() const {
212 assert(Section);
213 return LabelRange;
214 }
216 // Accessors.
217 unsigned getUniqueID() const { return UniqueID; }
218 virtual uint16_t getLanguage() const = 0;
219 DIE *getUnitDie() const { return UnitDie.get(); }
220 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
221 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
223 const StringMap<std::vector<const DIE *> > &getAccelNames() const {
224 return AccelNames;
225 }
226 const StringMap<std::vector<const DIE *> > &getAccelObjC() const {
227 return AccelObjC;
228 }
229 const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
230 return AccelNamespace;
231 }
232 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
233 getAccelTypes() const {
234 return AccelTypes;
235 }
237 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
238 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
240 /// hasContent - Return true if this compile unit has something to write out.
241 bool hasContent() const { return !UnitDie->getChildren().empty(); }
243 /// addRange - Add an address range to the list of ranges for this unit.
244 void addRange(RangeSpan Range) { CURanges.push_back(Range); }
246 /// getRanges - Get the list of ranges for this unit.
247 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
248 SmallVectorImpl<RangeSpan> &getRanges() { return CURanges; }
250 /// addRangeList - Add an address range list to the list of range lists.
251 void addRangeList(RangeSpanList Ranges) { CURangeLists.push_back(Ranges); }
253 /// getRangeLists - Get the vector of range lists.
254 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
255 return CURangeLists;
256 }
257 SmallVectorImpl<RangeSpanList> &getRangeLists() { return CURangeLists; }
259 /// getParentContextString - Get a string containing the language specific
260 /// context for a global name.
261 std::string getParentContextString(DIScope Context) const;
263 /// addGlobalName - Add a new global entity to the compile unit.
264 ///
265 void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
267 /// addAccelName - Add a new name to the name accelerator table.
268 void addAccelName(StringRef Name, const DIE *Die);
270 /// addAccelObjC - Add a new name to the ObjC accelerator table.
271 void addAccelObjC(StringRef Name, const DIE *Die);
273 /// addAccelNamespace - Add a new name to the namespace accelerator table.
274 void addAccelNamespace(StringRef Name, const DIE *Die);
276 /// addAccelType - Add a new type to the type accelerator table.
277 void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
279 /// getDIE - Returns the debug information entry map slot for the
280 /// specified debug variable. We delegate the request to DwarfDebug
281 /// when the MDNode can be part of the type system, since DIEs for
282 /// the type system can be shared across CUs and the mappings are
283 /// kept in DwarfDebug.
284 DIE *getDIE(DIDescriptor D) const;
286 /// getDIEBlock - Returns a fresh newly allocated DIEBlock.
287 DIEBlock *getDIEBlock() { return new (DIEValueAllocator) DIEBlock(); }
289 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
290 /// when the MDNode can be part of the type system, since DIEs for
291 /// the type system can be shared across CUs and the mappings are
292 /// kept in DwarfDebug.
293 void insertDIE(DIDescriptor Desc, DIE *D);
295 /// addDie - Adds or interns the DIE to the compile unit.
296 ///
297 void addDie(DIE *Buffer) { UnitDie->addChild(Buffer); }
299 /// addFlag - Add a flag that is true to the DIE.
300 void addFlag(DIE *Die, dwarf::Attribute Attribute);
302 /// addUInt - Add an unsigned integer attribute data and value.
303 void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
304 uint64_t Integer);
306 void addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer);
308 /// addSInt - Add an signed integer attribute data and value.
309 void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
310 int64_t Integer);
312 void addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, int64_t Integer);
314 /// addString - Add a string attribute data and value.
315 void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
317 /// addLocalString - Add a string attribute data and value.
318 void addLocalString(DIE *Die, dwarf::Attribute Attribute,
319 const StringRef Str);
321 /// addExpr - Add a Dwarf expression attribute data and value.
322 void addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr);
324 /// addLabel - Add a Dwarf label attribute data and value.
325 void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
326 const MCSymbol *Label);
328 void addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label);
330 /// addSectionLabel - Add a Dwarf section label attribute data and value.
331 ///
332 void addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
333 const MCSymbol *Label);
335 /// addSectionOffset - Add an offset into a section attribute data and value.
336 ///
337 void addSectionOffset(DIE *Die, dwarf::Attribute Attribute, uint64_t Integer);
339 /// addOpAddress - Add a dwarf op address data and value using the
340 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
341 void addOpAddress(DIEBlock *Die, const MCSymbol *Label);
343 /// addSectionDelta - Add a label delta attribute data and value.
344 void addSectionDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
345 const MCSymbol *Lo);
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, DIEBlock *Block);
358 /// addSourceLine - Add location information to specified debug information
359 /// entry.
360 void addSourceLine(DIE *Die, DIVariable V);
361 void addSourceLine(DIE *Die, DIGlobalVariable G);
362 void addSourceLine(DIE *Die, DISubprogram SP);
363 void addSourceLine(DIE *Die, DIType Ty);
364 void addSourceLine(DIE *Die, DINameSpace NS);
365 void addSourceLine(DIE *Die, DIObjCProperty Ty);
367 /// addAddress - Add an address attribute to a die based on the location
368 /// provided.
369 void addAddress(DIE *Die, dwarf::Attribute Attribute,
370 const MachineLocation &Location, bool Indirect = false);
372 /// addConstantValue - Add constant value entry in variable DIE.
373 void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
374 void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
375 void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
377 /// addConstantFPValue - Add constant value entry in variable DIE.
378 void addConstantFPValue(DIE *Die, const MachineOperand &MO);
379 void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
381 /// addTemplateParams - Add template parameters in buffer.
382 void addTemplateParams(DIE &Buffer, DIArray TParams);
384 /// addRegisterOp - Add register operand.
385 void addRegisterOp(DIEBlock *TheDie, unsigned Reg);
387 /// addRegisterOffset - Add register offset.
388 void addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset);
390 /// addComplexAddress - Start with the address based on the location provided,
391 /// and generate the DWARF information necessary to find the actual variable
392 /// (navigating the extra location information encoded in the type) based on
393 /// the starting location. Add the DWARF information to the die.
394 void addComplexAddress(const DbgVariable &DV, DIE *Die,
395 dwarf::Attribute Attribute,
396 const MachineLocation &Location);
398 // FIXME: Should be reformulated in terms of addComplexAddress.
399 /// addBlockByrefAddress - Start with the address based on the location
400 /// provided, and generate the DWARF information necessary to find the
401 /// actual Block variable (navigating the Block struct) based on the
402 /// starting location. Add the DWARF information to the die. Obsolete,
403 /// please use addComplexAddress instead.
404 void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
405 dwarf::Attribute Attribute,
406 const MachineLocation &Location);
408 /// addVariableAddress - Add DW_AT_location attribute for a
409 /// DbgVariable based on provided MachineLocation.
410 void addVariableAddress(const DbgVariable &DV, DIE *Die,
411 MachineLocation Location);
413 /// addType - Add a new type attribute to the specified entity. This takes
414 /// and attribute parameter because DW_AT_friend attributes are also
415 /// type references.
416 void addType(DIE *Entity, DIType Ty,
417 dwarf::Attribute Attribute = dwarf::DW_AT_type);
419 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
420 DIE *getOrCreateNameSpace(DINameSpace NS);
422 /// getOrCreateSubprogramDIE - Create new DIE using SP.
423 DIE *getOrCreateSubprogramDIE(DISubprogram SP);
425 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
426 /// given DIType.
427 DIE *getOrCreateTypeDIE(const MDNode *N);
429 /// getOrCreateContextDIE - Get context owner's DIE.
430 DIE *createTypeDIE(DICompositeType Ty);
432 /// getOrCreateContextDIE - Get context owner's DIE.
433 DIE *getOrCreateContextDIE(DIScope Context);
435 /// constructContainingTypeDIEs - Construct DIEs for types that contain
436 /// vtables.
437 void constructContainingTypeDIEs();
439 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
440 DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
442 /// Create a DIE with the given Tag, add the DIE to its parent, and
443 /// call insertDIE if MD is not null.
444 DIE *createAndAddDIE(unsigned Tag, DIE &Parent,
445 DIDescriptor N = DIDescriptor());
447 /// Compute the size of a header for this unit, not including the initial
448 /// length field.
449 virtual unsigned getHeaderSize() const {
450 return sizeof(int16_t) + // DWARF version number
451 sizeof(int32_t) + // Offset Into Abbrev. Section
452 sizeof(int8_t); // Pointer Size (in bytes)
453 }
455 /// Emit the header for this unit, not including the initial length field.
456 virtual void emitHeader(const MCSection *ASection,
457 const MCSymbol *ASectionSym) const;
459 protected:
460 /// getOrCreateStaticMemberDIE - Create new static data member DIE.
461 DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
463 private:
464 /// constructTypeDIE - Construct basic type die from DIBasicType.
465 void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
467 /// constructTypeDIE - Construct derived type die from DIDerivedType.
468 void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
470 /// constructTypeDIE - Construct type DIE from DICompositeType.
471 void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
473 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
474 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
476 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
477 void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
479 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
480 void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
482 /// constructMemberDIE - Construct member DIE from DIDerivedType.
483 void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
485 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
486 /// DITemplateTypeParameter.
487 void constructTemplateTypeParameterDIE(DIE &Buffer,
488 DITemplateTypeParameter TP);
490 /// constructTemplateValueParameterDIE - Construct new DIE for the given
491 /// DITemplateValueParameter.
492 void constructTemplateValueParameterDIE(DIE &Buffer,
493 DITemplateValueParameter TVP);
495 /// getLowerBoundDefault - Return the default lower bound for an array. If the
496 /// DWARF version doesn't handle the language, return -1.
497 int64_t getDefaultLowerBound() const;
499 /// getDIEEntry - Returns the debug information entry for the specified
500 /// debug variable.
501 DIEEntry *getDIEEntry(const MDNode *N) const {
502 return MDNodeToDIEEntryMap.lookup(N);
503 }
505 /// insertDIEEntry - Insert debug information entry into the map.
506 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
507 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
508 }
510 // getIndexTyDie - Get an anonymous type for index type.
511 DIE *getIndexTyDie() { return IndexTyDie; }
513 // setIndexTyDie - Set D as anonymous type for index which can be reused
514 // later.
515 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
517 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
518 /// information entry.
519 DIEEntry *createDIEEntry(DIE *Entry);
521 /// resolve - Look in the DwarfDebug map for the MDNode that
522 /// corresponds to the reference.
523 template <typename T> T resolve(DIRef<T> Ref) const {
524 return DD->resolve(Ref);
525 }
527 /// If this is a named finished type then include it in the list of types for
528 /// the accelerator tables.
529 void updateAcceleratorTables(DIScope Context, DIType Ty, const DIE *TyDIE);
530 };
532 class DwarfCompileUnit : public DwarfUnit {
533 /// Node - MDNode for the compile unit.
534 DICompileUnit Node;
536 public:
537 DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
538 DwarfDebug *DW, DwarfFile *DWU);
539 DICompileUnit getNode() const { return Node; }
540 virtual ~DwarfCompileUnit() LLVM_OVERRIDE;
542 /// createGlobalVariableDIE - create global variable DIE.
543 void createGlobalVariableDIE(DIGlobalVariable GV);
545 /// addLabelAddress - Add a dwarf label attribute data and value using
546 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
547 void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label);
549 uint16_t getLanguage() const LLVM_OVERRIDE { return getNode().getLanguage(); }
550 };
552 class DwarfTypeUnit : public DwarfUnit {
553 private:
554 uint16_t Language;
555 uint64_t TypeSignature;
556 const DIE *Ty;
558 public:
559 DwarfTypeUnit(unsigned UID, DIE *D, uint16_t Language, AsmPrinter *A,
560 DwarfDebug *DW, DwarfFile *DWU);
561 virtual ~DwarfTypeUnit() LLVM_OVERRIDE;
563 void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
564 uint64_t getTypeSignature() const { return TypeSignature; }
565 void setType(const DIE *Ty) { this->Ty = Ty; }
567 uint16_t getLanguage() const LLVM_OVERRIDE { return Language; }
568 /// Emit the header for this unit, not including the initial length field.
569 void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym) const
570 LLVM_OVERRIDE;
571 unsigned getHeaderSize() const LLVM_OVERRIDE {
572 return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
573 sizeof(uint32_t); // Type DIE Offset
574 }
575 void initSection(const MCSection *Section);
576 };
577 } // end llvm namespace
578 #endif