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 Unit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A, DwarfDebug *DW,
137 unsigned getUniqueID() const { return UniqueID; }
138 virtual uint16_t getLanguage() const = 0;
139 DICompileUnit getNode() const { return Node; }
140 DIE *getUnitDie() const { return UnitDie.get(); }
141 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
142 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
144 const StringMap<std::vector<const DIE *> > &getAccelNames() const {
147 const StringMap<std::vector<const DIE *> > &getAccelObjC() const {
150 const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
151 return AccelNamespace;
153 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
154 getAccelTypes() const {
158 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
159 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
161 /// hasContent - Return true if this compile unit has something to write out.
162 bool hasContent() const { return !UnitDie->getChildren().empty(); }
164 /// addRangeList - Add an address range list to the list of range lists.
165 void addRangeList(RangeSpanList Ranges) { CURangeLists.push_back(Ranges); }
167 /// getRangeLists - Get the vector of range lists.
168 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
171 SmallVectorImpl<RangeSpanList> &getRangeLists() { return CURangeLists; }
173 /// getParentContextString - Get a string containing the language specific
174 /// context for a global name.
175 std::string getParentContextString(DIScope Context) const;
177 /// addGlobalName - Add a new global entity to the compile unit.
179 void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
181 /// addAccelName - Add a new name to the name accelerator table.
182 void addAccelName(StringRef Name, const DIE *Die);
184 /// addAccelObjC - Add a new name to the ObjC accelerator table.
185 void addAccelObjC(StringRef Name, const DIE *Die);
187 /// addAccelNamespace - Add a new name to the namespace accelerator table.
188 void addAccelNamespace(StringRef Name, const DIE *Die);
190 /// addAccelType - Add a new type to the type accelerator table.
191 void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
193 /// getDIE - Returns the debug information entry map slot for the
194 /// specified debug variable. We delegate the request to DwarfDebug
195 /// when the MDNode can be part of the type system, since DIEs for
196 /// the type system can be shared across CUs and the mappings are
197 /// kept in DwarfDebug.
198 DIE *getDIE(DIDescriptor D) const;
200 /// getDIEBlock - Returns a fresh newly allocated DIEBlock.
201 DIEBlock *getDIEBlock() { return new (DIEValueAllocator) DIEBlock(); }
203 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
204 /// when the MDNode can be part of the type system, since DIEs for
205 /// the type system can be shared across CUs and the mappings are
206 /// kept in DwarfDebug.
207 void insertDIE(DIDescriptor Desc, DIE *D);
209 /// addDie - Adds or interns the DIE to the compile unit.
211 void addDie(DIE *Buffer) { UnitDie->addChild(Buffer); }
213 /// addFlag - Add a flag that is true to the DIE.
214 void addFlag(DIE *Die, dwarf::Attribute Attribute);
216 /// addUInt - Add an unsigned integer attribute data and value.
217 void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
220 void addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer);
222 /// addSInt - Add an signed integer attribute data and value.
223 void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
226 void addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, int64_t Integer);
228 /// addString - Add a string attribute data and value.
229 void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
231 /// addLocalString - Add a string attribute data and value.
232 void addLocalString(DIE *Die, dwarf::Attribute Attribute,
233 const StringRef Str);
235 /// addExpr - Add a Dwarf expression attribute data and value.
236 void addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr);
238 /// addLabel - Add a Dwarf label attribute data and value.
239 void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
240 const MCSymbol *Label);
242 void addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label);
244 /// addSectionLabel - Add a Dwarf section label attribute data and value.
246 void addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
247 const MCSymbol *Label);
249 /// addSectionOffset - Add an offset into a section attribute data and value.
251 void addSectionOffset(DIE *Die, dwarf::Attribute Attribute, uint64_t Integer);
253 /// addOpAddress - Add a dwarf op address data and value using the
254 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
255 void addOpAddress(DIEBlock *Die, const MCSymbol *Label);
257 /// addSectionDelta - Add a label delta attribute data and value.
258 void addSectionDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
261 /// addDIEEntry - Add a DIE attribute data and value.
262 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
264 /// addDIEEntry - Add a DIE attribute data and value.
265 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
267 /// addBlock - Add block data.
268 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
270 /// addSourceLine - Add location information to specified debug information
272 void addSourceLine(DIE *Die, DIVariable V);
273 void addSourceLine(DIE *Die, DIGlobalVariable G);
274 void addSourceLine(DIE *Die, DISubprogram SP);
275 void addSourceLine(DIE *Die, DIType Ty);
276 void addSourceLine(DIE *Die, DINameSpace NS);
277 void addSourceLine(DIE *Die, DIObjCProperty Ty);
279 /// addAddress - Add an address attribute to a die based on the location
281 void addAddress(DIE *Die, dwarf::Attribute Attribute,
282 const MachineLocation &Location, bool Indirect = false);
284 /// addConstantValue - Add constant value entry in variable DIE.
285 void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
286 void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
287 void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
289 /// addConstantFPValue - Add constant value entry in variable DIE.
290 void addConstantFPValue(DIE *Die, const MachineOperand &MO);
291 void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
293 /// addTemplateParams - Add template parameters in buffer.
294 void addTemplateParams(DIE &Buffer, DIArray TParams);
296 /// addRegisterOp - Add register operand.
297 void addRegisterOp(DIEBlock *TheDie, unsigned Reg);
299 /// addRegisterOffset - Add register offset.
300 void addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset);
302 /// addComplexAddress - Start with the address based on the location provided,
303 /// and generate the DWARF information necessary to find the actual variable
304 /// (navigating the extra location information encoded in the type) based on
305 /// the starting location. Add the DWARF information to the die.
306 void addComplexAddress(const DbgVariable &DV, DIE *Die,
307 dwarf::Attribute Attribute,
308 const MachineLocation &Location);
310 // FIXME: Should be reformulated in terms of addComplexAddress.
311 /// addBlockByrefAddress - Start with the address based on the location
312 /// provided, and generate the DWARF information necessary to find the
313 /// actual Block variable (navigating the Block struct) based on the
314 /// starting location. Add the DWARF information to the die. Obsolete,
315 /// please use addComplexAddress instead.
316 void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
317 dwarf::Attribute Attribute,
318 const MachineLocation &Location);
320 /// addVariableAddress - Add DW_AT_location attribute for a
321 /// DbgVariable based on provided MachineLocation.
322 void addVariableAddress(const DbgVariable &DV, DIE *Die,
323 MachineLocation Location);
325 /// addType - Add a new type attribute to the specified entity. This takes
326 /// and attribute parameter because DW_AT_friend attributes are also
328 void addType(DIE *Entity, DIType Ty,
329 dwarf::Attribute Attribute = dwarf::DW_AT_type);
331 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
332 DIE *getOrCreateNameSpace(DINameSpace NS);
334 /// getOrCreateSubprogramDIE - Create new DIE using SP.
335 DIE *getOrCreateSubprogramDIE(DISubprogram SP);
337 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
339 DIE *getOrCreateTypeDIE(const MDNode *N);
341 /// getOrCreateContextDIE - Get context owner's DIE.
342 DIE *createTypeDIE(DICompositeType Ty);
344 /// getOrCreateContextDIE - Get context owner's DIE.
345 DIE *getOrCreateContextDIE(DIScope Context);
347 /// constructContainingTypeDIEs - Construct DIEs for types that contain
349 void constructContainingTypeDIEs();
351 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
352 DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
354 /// Create a DIE with the given Tag, add the DIE to its parent, and
355 /// call insertDIE if MD is not null.
356 DIE *createAndAddDIE(unsigned Tag, DIE &Parent,
357 DIDescriptor N = DIDescriptor());
359 /// Compute the size of a header for this unit, not including the initial
361 unsigned getHeaderSize() const {
362 return sizeof(int16_t) + // DWARF version number
363 sizeof(int32_t) + // Offset Into Abbrev. Section
364 sizeof(int8_t); // Pointer Size (in bytes)
367 /// Emit the header for this unit, not including the initial length field.
368 void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym);
371 /// getOrCreateStaticMemberDIE - Create new static data member DIE.
372 DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
375 /// constructTypeDIE - Construct basic type die from DIBasicType.
376 void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
378 /// constructTypeDIE - Construct derived type die from DIDerivedType.
379 void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
381 /// constructTypeDIE - Construct type DIE from DICompositeType.
382 void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
384 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
385 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
387 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
388 void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
390 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
391 void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
393 /// constructMemberDIE - Construct member DIE from DIDerivedType.
394 void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
396 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
397 /// DITemplateTypeParameter.
398 void constructTemplateTypeParameterDIE(DIE &Buffer,
399 DITemplateTypeParameter TP);
401 /// constructTemplateValueParameterDIE - Construct new DIE for the given
402 /// DITemplateValueParameter.
403 void constructTemplateValueParameterDIE(DIE &Buffer,
404 DITemplateValueParameter TVP);
406 /// getLowerBoundDefault - Return the default lower bound for an array. If the
407 /// DWARF version doesn't handle the language, return -1.
408 int64_t getDefaultLowerBound() const;
410 /// getDIEEntry - Returns the debug information entry for the specified
412 DIEEntry *getDIEEntry(const MDNode *N) const {
413 return MDNodeToDIEEntryMap.lookup(N);
416 /// insertDIEEntry - Insert debug information entry into the map.
417 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
418 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
421 // getIndexTyDie - Get an anonymous type for index type.
422 DIE *getIndexTyDie() { return IndexTyDie; }
424 // setIndexTyDie - Set D as anonymous type for index which can be reused
426 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
428 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
429 /// information entry.
430 DIEEntry *createDIEEntry(DIE *Entry);
432 /// resolve - Look in the DwarfDebug map for the MDNode that
433 /// corresponds to the reference.
434 template <typename T> T resolve(DIRef<T> Ref) const {
435 return DD->resolve(Ref);
438 /// If this is a named finished type then include it in the list of types for
439 /// the accelerator tables.
440 void updateAcceleratorTables(DIScope Context, DIType Ty, const DIE *TyDIE);
443 class CompileUnit : public Unit {
445 CompileUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
446 DwarfDebug *DW, DwarfUnits *DWU);
448 /// createGlobalVariableDIE - create global variable DIE.
449 void createGlobalVariableDIE(DIGlobalVariable GV);
451 /// addLabelAddress - Add a dwarf label attribute data and value using
452 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
453 void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label);
455 uint16_t getLanguage() const { return getNode().getLanguage(); }
458 class TypeUnit : public Unit {
463 TypeUnit(unsigned UID, DIE *D, uint16_t Language, AsmPrinter *A,
464 DwarfDebug *DW, DwarfUnits *DWU);
466 uint16_t getLanguage() const { return Language; }
468 } // end llvm namespace