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 LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H
17 #include "DwarfDebug.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/DIE.h"
23 #include "llvm/IR/DIBuilder.h"
24 #include "llvm/IR/DebugInfo.h"
25 #include "llvm/MC/MCDwarf.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCSection.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, SmallVector<RangeSpan, 2> Ranges)
59 : RangeSym(Sym), Ranges(std::move(Ranges)) {}
60 MCSymbol *getSym() const { return RangeSym; }
61 const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
62 void addRange(RangeSpan Range) { Ranges.push_back(Range); }
65 //===----------------------------------------------------------------------===//
66 /// This dwarf writer support class manages information associated with a
70 /// A numeric ID unique among all CUs in the module
73 /// MDNode for the compile unit.
74 const DICompileUnit *CUNode;
76 // All DIEValues are allocated through this allocator.
77 BumpPtrAllocator DIEValueAllocator;
79 /// Unit debug information entry.
82 /// Offset of the UnitDie from beginning of debug info section.
83 unsigned DebugInfoOffset;
85 /// Target of Dwarf emission.
88 // Holders for some common dwarf information.
92 /// An anonymous type for index type. Owned by UnitDie.
95 /// Tracks the mapping of unit level debug information variables to debug
96 /// information entries.
97 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
99 /// A list of all the DIEBlocks in use.
100 std::vector<DIEBlock *> DIEBlocks;
102 /// A list of all the DIELocs in use.
103 std::vector<DIELoc *> DIELocs;
105 /// This map is used to keep track of subprogram DIEs that need
106 /// DW_AT_containing_type attribute. This attribute points to a DIE that
107 /// corresponds to the MDNode mapped with the subprogram DIE.
108 DenseMap<DIE *, const DINode *> ContainingTypeMap;
110 /// The section this unit will be emitted in.
113 DwarfUnit(unsigned UID, dwarf::Tag, const DICompileUnit *CU, AsmPrinter *A,
114 DwarfDebug *DW, DwarfFile *DWU);
116 /// Add a string attribute data and value.
118 /// This is guaranteed to be in the local string pool instead of indirected.
119 void addLocalString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
121 void addIndexedString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
123 bool applySubprogramDefinitionAttributes(const DISubprogram *SP, DIE &SPDie);
126 virtual ~DwarfUnit();
128 void initSection(MCSection *Section);
130 MCSection *getSection() const {
136 AsmPrinter* getAsmPrinter() const { return Asm; }
137 unsigned getUniqueID() const { return UniqueID; }
138 uint16_t getLanguage() const { return CUNode->getSourceLanguage(); }
139 const DICompileUnit *getCUNode() const { return CUNode; }
140 DIE &getUnitDie() { return UnitDie; }
142 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
143 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
145 /// Return true if this compile unit has something to write out.
146 bool hasContent() const { return UnitDie.hasChildren(); }
148 /// Get string containing language specific context for a global name.
150 /// Walks the metadata parent chain in a language specific manner (using the
151 /// compile unit language) and returns it as a string. This is done at the
152 /// metadata level because DIEs may not currently have been added to the
153 /// parent context and walking the DIEs looking for names is more expensive
154 /// than walking the metadata.
155 std::string getParentContextString(const DIScope *Context) const;
157 /// Add a new global name to the compile unit.
158 virtual void addGlobalName(StringRef Name, DIE &Die, const DIScope *Context) {
161 /// Add a new global type to the compile unit.
162 virtual void addGlobalType(const DIType *Ty, const DIE &Die,
163 const DIScope *Context) {}
165 /// Add a new name to the namespace accelerator table.
166 void addAccelNamespace(StringRef Name, const DIE &Die);
168 /// Returns the DIE map slot for the specified debug variable.
170 /// We delegate the request to DwarfDebug when the MDNode can be part of the
171 /// type system, since DIEs for the type system can be shared across CUs and
172 /// the mappings are kept in DwarfDebug.
173 DIE *getDIE(const DINode *D) const;
175 /// Returns a fresh newly allocated DIELoc.
176 DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc; }
178 /// Insert DIE into the map.
180 /// We delegate the request to DwarfDebug when the MDNode can be part of the
181 /// type system, since DIEs for the type system can be shared across CUs and
182 /// the mappings are kept in DwarfDebug.
183 void insertDIE(const DINode *Desc, DIE *D);
185 /// Add a flag that is true to the DIE.
186 void addFlag(DIE &Die, dwarf::Attribute Attribute);
188 /// Add an unsigned integer attribute data and value.
189 void addUInt(DIE &Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
192 void addUInt(DIE &Block, dwarf::Form Form, uint64_t Integer);
194 /// Add an signed integer attribute data and value.
195 void addSInt(DIE &Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
198 void addSInt(DIELoc &Die, Optional<dwarf::Form> Form, int64_t Integer);
200 /// Add a string attribute data and value.
202 /// We always emit a reference to the string pool instead of immediate
203 /// strings so that DIEs have more predictable sizes. In the case of split
204 /// dwarf we emit an index into another table which gets us the static offset
205 /// into the string table.
206 void addString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
208 /// Add a Dwarf label attribute data and value.
209 DIE::value_iterator addLabel(DIE &Die, dwarf::Attribute Attribute,
210 dwarf::Form Form, const MCSymbol *Label);
212 void addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label);
214 /// Add an offset into a section attribute data and value.
215 void addSectionOffset(DIE &Die, dwarf::Attribute Attribute, uint64_t Integer);
217 /// Add a dwarf op address data and value using the form given and an
218 /// op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
219 void addOpAddress(DIELoc &Die, const MCSymbol *Label);
221 /// Add a label delta attribute data and value.
222 void addLabelDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
225 /// Add a DIE attribute data and value.
226 void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry);
228 /// Add a DIE attribute data and value.
229 void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIEEntry Entry);
231 void addDIETypeSignature(DIE &Die, const DwarfTypeUnit &Type);
234 void addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Block);
237 void addBlock(DIE &Die, dwarf::Attribute Attribute, DIEBlock *Block);
239 /// Add location information to specified debug information entry.
240 void addSourceLine(DIE &Die, unsigned Line, StringRef File,
241 StringRef Directory);
242 void addSourceLine(DIE &Die, const DILocalVariable *V);
243 void addSourceLine(DIE &Die, const DIGlobalVariable *G);
244 void addSourceLine(DIE &Die, const DISubprogram *SP);
245 void addSourceLine(DIE &Die, const DIType *Ty);
246 void addSourceLine(DIE &Die, const DINamespace *NS);
247 void addSourceLine(DIE &Die, const DIObjCProperty *Ty);
249 /// Add constant value entry in variable DIE.
250 void addConstantValue(DIE &Die, const MachineOperand &MO, const DIType *Ty);
251 void addConstantValue(DIE &Die, const ConstantInt *CI, const DIType *Ty);
252 void addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty);
253 void addConstantValue(DIE &Die, const APInt &Val, bool Unsigned);
254 void addConstantValue(DIE &Die, bool Unsigned, uint64_t Val);
256 /// Add constant value entry in variable DIE.
257 void addConstantFPValue(DIE &Die, const MachineOperand &MO);
258 void addConstantFPValue(DIE &Die, const ConstantFP *CFP);
260 /// Add a linkage name, if it isn't empty.
261 void addLinkageName(DIE &Die, StringRef LinkageName);
263 /// Add template parameters in buffer.
264 void addTemplateParams(DIE &Buffer, DINodeArray TParams);
266 /// Add register operand.
267 /// \returns false if the register does not exist, e.g., because it was never
269 bool addRegisterOpPiece(DIELoc &TheDie, unsigned Reg,
270 unsigned SizeInBits = 0, unsigned OffsetInBits = 0);
272 /// Add register offset.
273 /// \returns false if the register does not exist, e.g., because it was never
275 bool addRegisterOffset(DIELoc &TheDie, unsigned Reg, int64_t Offset);
277 // FIXME: Should be reformulated in terms of addComplexAddress.
278 /// Start with the address based on the location provided, and generate the
279 /// DWARF information necessary to find the actual Block variable (navigating
280 /// the Block struct) based on the starting location. Add the DWARF
281 /// information to the die. Obsolete, please use addComplexAddress instead.
282 void addBlockByrefAddress(const DbgVariable &DV, DIE &Die,
283 dwarf::Attribute Attribute,
284 const MachineLocation &Location);
286 /// Add a new type attribute to the specified entity.
288 /// This takes and attribute parameter because DW_AT_friend attributes are
289 /// also type references.
290 void addType(DIE &Entity, const DIType *Ty,
291 dwarf::Attribute Attribute = dwarf::DW_AT_type);
293 DIE *getOrCreateNameSpace(const DINamespace *NS);
294 DIE *getOrCreateModule(const DIModule *M);
295 DIE *getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal = false);
297 void applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
298 bool Minimal = false);
300 /// Find existing DIE or create new DIE for the given type.
301 DIE *getOrCreateTypeDIE(const MDNode *N);
303 /// Get context owner's DIE.
304 DIE *createTypeDIE(const DICompositeType *Ty);
306 /// Get context owner's DIE.
307 DIE *getOrCreateContextDIE(const DIScope *Context);
309 /// Construct DIEs for types that contain vtables.
310 void constructContainingTypeDIEs();
312 /// Construct function argument DIEs.
313 void constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args);
315 /// Create a DIE with the given Tag, add the DIE to its parent, and
316 /// call insertDIE if MD is not null.
317 DIE &createAndAddDIE(unsigned Tag, DIE &Parent, const DINode *N = nullptr);
319 /// Compute the size of a header for this unit, not including the initial
321 virtual unsigned getHeaderSize() const {
322 return sizeof(int16_t) + // DWARF version number
323 sizeof(int32_t) + // Offset Into Abbrev. Section
324 sizeof(int8_t); // Pointer Size (in bytes)
327 /// Emit the header for this unit, not including the initial length field.
328 virtual void emitHeader(bool UseOffsets);
330 virtual DwarfCompileUnit &getCU() = 0;
332 void constructTypeDIE(DIE &Buffer, const DICompositeType *CTy);
335 /// Create new static data member DIE.
336 DIE *getOrCreateStaticMemberDIE(const DIDerivedType *DT);
338 /// Look up the source ID with the given directory and source file names. If
339 /// none currently exists, create a new ID and insert it in the line table.
340 virtual unsigned getOrCreateSourceID(StringRef File, StringRef Directory) = 0;
342 /// Look in the DwarfDebug map for the MDNode that corresponds to the
344 template <typename T> T *resolve(TypedDINodeRef<T> Ref) const {
345 return DD->resolve(Ref);
349 void constructTypeDIE(DIE &Buffer, const DIBasicType *BTy);
350 void constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy);
351 void constructTypeDIE(DIE &Buffer, const DISubroutineType *DTy);
352 void constructSubrangeDIE(DIE &Buffer, const DISubrange *SR, DIE *IndexTy);
353 void constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy);
354 void constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy);
355 void constructMemberDIE(DIE &Buffer, const DIDerivedType *DT);
356 void constructTemplateTypeParameterDIE(DIE &Buffer,
357 const DITemplateTypeParameter *TP);
358 void constructTemplateValueParameterDIE(DIE &Buffer,
359 const DITemplateValueParameter *TVP);
361 /// Return the default lower bound for an array.
363 /// If the DWARF version doesn't handle the language, return -1.
364 int64_t getDefaultLowerBound() const;
366 /// Get an anonymous type for index type.
367 DIE *getIndexTyDie();
369 /// Set D as anonymous type for index which can be reused later.
370 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
372 /// If this is a named finished type then include it in the list of types for
373 /// the accelerator tables.
374 void updateAcceleratorTables(const DIScope *Context, const DIType *Ty,
377 virtual bool isDwoUnit() const = 0;
380 class DwarfTypeUnit : public DwarfUnit {
381 uint64_t TypeSignature;
383 DwarfCompileUnit &CU;
384 MCDwarfDwoLineTable *SplitLineTable;
386 unsigned getOrCreateSourceID(StringRef File, StringRef Directory) override;
387 bool isDwoUnit() const override;
390 DwarfTypeUnit(unsigned UID, DwarfCompileUnit &CU, AsmPrinter *A,
391 DwarfDebug *DW, DwarfFile *DWU,
392 MCDwarfDwoLineTable *SplitLineTable = nullptr);
394 void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
395 uint64_t getTypeSignature() const { return TypeSignature; }
396 void setType(const DIE *Ty) { this->Ty = Ty; }
398 /// Emit the header for this unit, not including the initial length field.
399 void emitHeader(bool UseOffsets) override;
400 unsigned getHeaderSize() const override {
401 return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
402 sizeof(uint32_t); // Type DIE Offset
404 DwarfCompileUnit &getCU() override { return CU; }
406 } // end llvm namespace