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/DebugInfo.h"
24 #include "llvm/MC/MCExpr.h"
28 class MachineLocation;
34 // Data structure to hold a range for range lists.
37 RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {}
38 const MCSymbol *getStart() const { return Start; }
39 const MCSymbol *getEnd() const { return End; }
42 const MCSymbol *Start, *End;
47 // Index for locating within the debug_range section this particular span.
50 SmallVector<RangeSpan, 2> Ranges;
53 RangeSpanList(MCSymbol *Sym) : RangeSym(Sym) {}
54 MCSymbol *getSym() const { return RangeSym; }
55 const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
56 void addRange(RangeSpan Range) { Ranges.push_back(Range); }
59 //===----------------------------------------------------------------------===//
60 /// Unit - This dwarf writer support class manages information associated
61 /// with a source file.
64 /// UniqueID - a numeric ID unique among all CUs in the module
67 /// Node - MDNode for the compile unit.
70 /// Unit debug information entry.
71 const OwningPtr<DIE> UnitDie;
73 /// Offset of the UnitDie from beginning of debug info section.
74 unsigned DebugInfoOffset;
76 /// Asm - Target of Dwarf emission.
79 // Holders for some common dwarf information.
83 /// IndexTyDie - An anonymous type for index type. Owned by UnitDie.
86 /// MDNodeToDieMap - Tracks the mapping of unit level debug information
87 /// variables to debug information entries.
88 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
90 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information
91 /// descriptors to debug information entries using a DIEEntry proxy.
92 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
94 /// GlobalNames - A map of globally visible named entities for this unit.
95 StringMap<const DIE *> GlobalNames;
97 /// GlobalTypes - A map of globally visible types for this unit.
98 StringMap<const DIE *> GlobalTypes;
100 /// AccelNames - A map of names for the name accelerator table.
101 StringMap<std::vector<const DIE *> > AccelNames;
103 /// AccelObjC - A map of objc spec for the objc accelerator table.
104 StringMap<std::vector<const DIE *> > AccelObjC;
106 /// AccelNamespace - A map of names for the namespace accelerator table.
107 StringMap<std::vector<const DIE *> > AccelNamespace;
109 /// AccelTypes - A map of names for the type accelerator table.
110 StringMap<std::vector<std::pair<const DIE *, unsigned> > > AccelTypes;
112 /// DIEBlocks - A list of all the DIEBlocks in use.
113 std::vector<DIEBlock *> DIEBlocks;
115 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
116 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
117 /// corresponds to the MDNode mapped with the subprogram DIE.
118 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
120 // List of range lists for a given compile unit, separate from the ranges for
122 SmallVector<RangeSpanList, 1> CURangeLists;
124 // DIEValueAllocator - All DIEValues are allocated through this allocator.
125 BumpPtrAllocator DIEValueAllocator;
127 // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
128 DIEInteger *DIEIntegerOne;
130 /// The section this unit will be emitted in.
131 const MCSection *Section;
133 /// A label at the start of the non-dwo section related to this unit.
134 MCSymbol *SectionSym;
136 Unit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A, DwarfDebug *DW,
142 /// Pass in the SectionSym even though we could recreate it in every compile
143 /// unit (type units will have actually distinct symbols once they're in
144 /// comdat sections).
145 void initSection(const MCSection *Section, MCSymbol *SectionSym) {
146 assert(!this->Section);
147 this->Section = Section;
148 this->SectionSym = SectionSym;
150 const MCSection *getSection() const {
155 MCSymbol *getSectionSym() {
161 unsigned getUniqueID() const { return UniqueID; }
162 virtual uint16_t getLanguage() const = 0;
163 DICompileUnit getNode() const { return Node; }
164 DIE *getUnitDie() const { return UnitDie.get(); }
165 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
166 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
168 const StringMap<std::vector<const DIE *> > &getAccelNames() const {
171 const StringMap<std::vector<const DIE *> > &getAccelObjC() const {
174 const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
175 return AccelNamespace;
177 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
178 getAccelTypes() const {
182 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
183 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
185 /// hasContent - Return true if this compile unit has something to write out.
186 bool hasContent() const { return !UnitDie->getChildren().empty(); }
188 /// addRangeList - Add an address range list to the list of range lists.
189 void addRangeList(RangeSpanList Ranges) { CURangeLists.push_back(Ranges); }
191 /// getRangeLists - Get the vector of range lists.
192 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
195 SmallVectorImpl<RangeSpanList> &getRangeLists() { return CURangeLists; }
197 /// getParentContextString - Get a string containing the language specific
198 /// context for a global name.
199 std::string getParentContextString(DIScope Context) const;
201 /// addGlobalName - Add a new global entity to the compile unit.
203 void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
205 /// addAccelName - Add a new name to the name accelerator table.
206 void addAccelName(StringRef Name, const DIE *Die);
208 /// addAccelObjC - Add a new name to the ObjC accelerator table.
209 void addAccelObjC(StringRef Name, const DIE *Die);
211 /// addAccelNamespace - Add a new name to the namespace accelerator table.
212 void addAccelNamespace(StringRef Name, const DIE *Die);
214 /// addAccelType - Add a new type to the type accelerator table.
215 void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
217 /// getDIE - Returns the debug information entry map slot for the
218 /// specified debug variable. We delegate the request to DwarfDebug
219 /// when the MDNode can be part of the type system, since DIEs for
220 /// the type system can be shared across CUs and the mappings are
221 /// kept in DwarfDebug.
222 DIE *getDIE(DIDescriptor D) const;
224 /// getDIEBlock - Returns a fresh newly allocated DIEBlock.
225 DIEBlock *getDIEBlock() { return new (DIEValueAllocator) DIEBlock(); }
227 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
228 /// when the MDNode can be part of the type system, since DIEs for
229 /// the type system can be shared across CUs and the mappings are
230 /// kept in DwarfDebug.
231 void insertDIE(DIDescriptor Desc, DIE *D);
233 /// addDie - Adds or interns the DIE to the compile unit.
235 void addDie(DIE *Buffer) { UnitDie->addChild(Buffer); }
237 /// addFlag - Add a flag that is true to the DIE.
238 void addFlag(DIE *Die, dwarf::Attribute Attribute);
240 /// addUInt - Add an unsigned integer attribute data and value.
241 void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
244 void addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer);
246 /// addSInt - Add an signed integer attribute data and value.
247 void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
250 void addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, int64_t Integer);
252 /// addString - Add a string attribute data and value.
253 void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
255 /// addLocalString - Add a string attribute data and value.
256 void addLocalString(DIE *Die, dwarf::Attribute Attribute,
257 const StringRef Str);
259 /// addExpr - Add a Dwarf expression attribute data and value.
260 void addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr);
262 /// addLabel - Add a Dwarf label attribute data and value.
263 void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
264 const MCSymbol *Label);
266 void addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label);
268 /// addSectionLabel - Add a Dwarf section label attribute data and value.
270 void addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
271 const MCSymbol *Label);
273 /// addSectionOffset - Add an offset into a section attribute data and value.
275 void addSectionOffset(DIE *Die, dwarf::Attribute Attribute, uint64_t Integer);
277 /// addOpAddress - Add a dwarf op address data and value using the
278 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
279 void addOpAddress(DIEBlock *Die, const MCSymbol *Label);
281 /// addSectionDelta - Add a label delta attribute data and value.
282 void addSectionDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
285 /// addDIEEntry - Add a DIE attribute data and value.
286 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
288 /// addDIEEntry - Add a DIE attribute data and value.
289 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
291 /// addBlock - Add block data.
292 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
294 /// addSourceLine - Add location information to specified debug information
296 void addSourceLine(DIE *Die, DIVariable V);
297 void addSourceLine(DIE *Die, DIGlobalVariable G);
298 void addSourceLine(DIE *Die, DISubprogram SP);
299 void addSourceLine(DIE *Die, DIType Ty);
300 void addSourceLine(DIE *Die, DINameSpace NS);
301 void addSourceLine(DIE *Die, DIObjCProperty Ty);
303 /// addAddress - Add an address attribute to a die based on the location
305 void addAddress(DIE *Die, dwarf::Attribute Attribute,
306 const MachineLocation &Location, bool Indirect = false);
308 /// addConstantValue - Add constant value entry in variable DIE.
309 void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
310 void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
311 void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
313 /// addConstantFPValue - Add constant value entry in variable DIE.
314 void addConstantFPValue(DIE *Die, const MachineOperand &MO);
315 void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
317 /// addTemplateParams - Add template parameters in buffer.
318 void addTemplateParams(DIE &Buffer, DIArray TParams);
320 /// addRegisterOp - Add register operand.
321 void addRegisterOp(DIEBlock *TheDie, unsigned Reg);
323 /// addRegisterOffset - Add register offset.
324 void addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset);
326 /// addComplexAddress - Start with the address based on the location provided,
327 /// and generate the DWARF information necessary to find the actual variable
328 /// (navigating the extra location information encoded in the type) based on
329 /// the starting location. Add the DWARF information to the die.
330 void addComplexAddress(const DbgVariable &DV, DIE *Die,
331 dwarf::Attribute Attribute,
332 const MachineLocation &Location);
334 // FIXME: Should be reformulated in terms of addComplexAddress.
335 /// addBlockByrefAddress - Start with the address based on the location
336 /// provided, and generate the DWARF information necessary to find the
337 /// actual Block variable (navigating the Block struct) based on the
338 /// starting location. Add the DWARF information to the die. Obsolete,
339 /// please use addComplexAddress instead.
340 void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
341 dwarf::Attribute Attribute,
342 const MachineLocation &Location);
344 /// addVariableAddress - Add DW_AT_location attribute for a
345 /// DbgVariable based on provided MachineLocation.
346 void addVariableAddress(const DbgVariable &DV, DIE *Die,
347 MachineLocation Location);
349 /// addType - Add a new type attribute to the specified entity. This takes
350 /// and attribute parameter because DW_AT_friend attributes are also
352 void addType(DIE *Entity, DIType Ty,
353 dwarf::Attribute Attribute = dwarf::DW_AT_type);
355 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
356 DIE *getOrCreateNameSpace(DINameSpace NS);
358 /// getOrCreateSubprogramDIE - Create new DIE using SP.
359 DIE *getOrCreateSubprogramDIE(DISubprogram SP);
361 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
363 DIE *getOrCreateTypeDIE(const MDNode *N);
365 /// getOrCreateContextDIE - Get context owner's DIE.
366 DIE *createTypeDIE(DICompositeType Ty);
368 /// getOrCreateContextDIE - Get context owner's DIE.
369 DIE *getOrCreateContextDIE(DIScope Context);
371 /// constructContainingTypeDIEs - Construct DIEs for types that contain
373 void constructContainingTypeDIEs();
375 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
376 DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
378 /// Create a DIE with the given Tag, add the DIE to its parent, and
379 /// call insertDIE if MD is not null.
380 DIE *createAndAddDIE(unsigned Tag, DIE &Parent,
381 DIDescriptor N = DIDescriptor());
383 /// Compute the size of a header for this unit, not including the initial
385 unsigned getHeaderSize() const {
386 return sizeof(int16_t) + // DWARF version number
387 sizeof(int32_t) + // Offset Into Abbrev. Section
388 sizeof(int8_t); // Pointer Size (in bytes)
391 /// Emit the header for this unit, not including the initial length field.
392 void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym);
395 /// getOrCreateStaticMemberDIE - Create new static data member DIE.
396 DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
399 /// constructTypeDIE - Construct basic type die from DIBasicType.
400 void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
402 /// constructTypeDIE - Construct derived type die from DIDerivedType.
403 void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
405 /// constructTypeDIE - Construct type DIE from DICompositeType.
406 void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
408 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
409 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
411 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
412 void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
414 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
415 void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
417 /// constructMemberDIE - Construct member DIE from DIDerivedType.
418 void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
420 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
421 /// DITemplateTypeParameter.
422 void constructTemplateTypeParameterDIE(DIE &Buffer,
423 DITemplateTypeParameter TP);
425 /// constructTemplateValueParameterDIE - Construct new DIE for the given
426 /// DITemplateValueParameter.
427 void constructTemplateValueParameterDIE(DIE &Buffer,
428 DITemplateValueParameter TVP);
430 /// getLowerBoundDefault - Return the default lower bound for an array. If the
431 /// DWARF version doesn't handle the language, return -1.
432 int64_t getDefaultLowerBound() const;
434 /// getDIEEntry - Returns the debug information entry for the specified
436 DIEEntry *getDIEEntry(const MDNode *N) const {
437 return MDNodeToDIEEntryMap.lookup(N);
440 /// insertDIEEntry - Insert debug information entry into the map.
441 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
442 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
445 // getIndexTyDie - Get an anonymous type for index type.
446 DIE *getIndexTyDie() { return IndexTyDie; }
448 // setIndexTyDie - Set D as anonymous type for index which can be reused
450 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
452 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
453 /// information entry.
454 DIEEntry *createDIEEntry(DIE *Entry);
456 /// resolve - Look in the DwarfDebug map for the MDNode that
457 /// corresponds to the reference.
458 template <typename T> T resolve(DIRef<T> Ref) const {
459 return DD->resolve(Ref);
462 /// If this is a named finished type then include it in the list of types for
463 /// the accelerator tables.
464 void updateAcceleratorTables(DIScope Context, DIType Ty, const DIE *TyDIE);
467 class CompileUnit : public Unit {
469 CompileUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
470 DwarfDebug *DW, DwarfFile *DWU);
472 /// createGlobalVariableDIE - create global variable DIE.
473 void createGlobalVariableDIE(DIGlobalVariable GV);
475 /// addLabelAddress - Add a dwarf label attribute data and value using
476 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
477 void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label);
479 uint16_t getLanguage() const { return getNode().getLanguage(); }
482 class TypeUnit : public Unit {
487 TypeUnit(unsigned UID, DIE *D, uint16_t Language, AsmPrinter *A,
488 DwarfDebug *DW, DwarfFile *DWU);
490 uint16_t getLanguage() const { return Language; }
492 } // end llvm namespace