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 //===----------------------------------------------------------------------===//
35 /// Unit - This dwarf writer support class manages information associated
36 /// with a source file.
39 /// UniqueID - a numeric ID unique among all CUs in the module
42 /// Node - MDNode for the compile unit.
45 /// Unit debug information entry.
46 const OwningPtr<DIE> UnitDie;
48 /// Offset of the UnitDie from beginning of debug info section.
49 unsigned DebugInfoOffset;
51 /// Asm - Target of Dwarf emission.
54 // Holders for some common dwarf information.
58 /// IndexTyDie - An anonymous type for index type. Owned by UnitDie.
61 /// MDNodeToDieMap - Tracks the mapping of unit level debug information
62 /// variables to debug information entries.
63 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
65 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information
66 /// descriptors to debug information entries using a DIEEntry proxy.
67 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
69 /// GlobalNames - A map of globally visible named entities for this unit.
70 StringMap<const DIE *> GlobalNames;
72 /// GlobalTypes - A map of globally visible types for this unit.
73 StringMap<const DIE *> GlobalTypes;
75 /// AccelNames - A map of names for the name accelerator table.
76 StringMap<std::vector<const DIE *> > AccelNames;
78 /// AccelObjC - A map of objc spec for the objc accelerator table.
79 StringMap<std::vector<const DIE *> > AccelObjC;
81 /// AccelNamespace - A map of names for the namespace accelerator table.
82 StringMap<std::vector<const DIE *> > AccelNamespace;
84 /// AccelTypes - A map of names for the type accelerator table.
85 StringMap<std::vector<std::pair<const DIE *, unsigned> > > AccelTypes;
87 /// DIEBlocks - A list of all the DIEBlocks in use.
88 std::vector<DIEBlock *> DIEBlocks;
90 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
91 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
92 /// corresponds to the MDNode mapped with the subprogram DIE.
93 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
95 // DIEValueAllocator - All DIEValues are allocated through this allocator.
96 BumpPtrAllocator DIEValueAllocator;
98 // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
99 DIEInteger *DIEIntegerOne;
101 Unit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A, DwarfDebug *DW,
108 unsigned getUniqueID() const { return UniqueID; }
109 virtual uint16_t getLanguage() const = 0;
110 DICompileUnit getNode() const { return Node; }
111 DIE *getUnitDie() const { return UnitDie.get(); }
112 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
113 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
115 const StringMap<std::vector<const DIE *> > &getAccelNames() const {
118 const StringMap<std::vector<const DIE *> > &getAccelObjC() const {
121 const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
122 return AccelNamespace;
124 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
125 getAccelTypes() const {
129 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
130 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
132 /// hasContent - Return true if this compile unit has something to write out.
133 bool hasContent() const { return !UnitDie->getChildren().empty(); }
135 /// getParentContextString - Get a string containing the language specific
136 /// context for a global name.
137 std::string getParentContextString(DIScope Context) const;
139 /// addGlobalName - Add a new global entity to the compile unit.
141 void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
143 /// addAccelName - Add a new name to the name accelerator table.
144 void addAccelName(StringRef Name, const DIE *Die);
146 /// addAccelObjC - Add a new name to the ObjC accelerator table.
147 void addAccelObjC(StringRef Name, const DIE *Die);
149 /// addAccelNamespace - Add a new name to the namespace accelerator table.
150 void addAccelNamespace(StringRef Name, const DIE *Die);
152 /// addAccelType - Add a new type to the type accelerator table.
153 void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
155 /// getDIE - Returns the debug information entry map slot for the
156 /// specified debug variable. We delegate the request to DwarfDebug
157 /// when the MDNode can be part of the type system, since DIEs for
158 /// the type system can be shared across CUs and the mappings are
159 /// kept in DwarfDebug.
160 DIE *getDIE(DIDescriptor D) const;
162 /// getDIEBlock - Returns a fresh newly allocated DIEBlock.
163 DIEBlock *getDIEBlock() { return new (DIEValueAllocator) DIEBlock(); }
165 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
166 /// when the MDNode can be part of the type system, since DIEs for
167 /// the type system can be shared across CUs and the mappings are
168 /// kept in DwarfDebug.
169 void insertDIE(DIDescriptor Desc, DIE *D);
171 /// addDie - Adds or interns the DIE to the compile unit.
173 void addDie(DIE *Buffer) { UnitDie->addChild(Buffer); }
175 /// addFlag - Add a flag that is true to the DIE.
176 void addFlag(DIE *Die, dwarf::Attribute Attribute);
178 /// addUInt - Add an unsigned integer attribute data and value.
179 void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
182 void addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer);
184 /// addSInt - Add an signed integer attribute data and value.
185 void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
188 void addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, int64_t Integer);
190 /// addString - Add a string attribute data and value.
191 void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
193 /// addLocalString - Add a string attribute data and value.
194 void addLocalString(DIE *Die, dwarf::Attribute Attribute,
195 const StringRef Str);
197 /// addExpr - Add a Dwarf expression attribute data and value.
198 void addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr);
200 /// addLabel - Add a Dwarf label attribute data and value.
201 void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
202 const MCSymbol *Label);
204 void addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label);
206 /// addSectionLabel - Add a Dwarf section label attribute data and value.
208 void addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
209 const MCSymbol *Label);
211 /// addSectionOffset - Add an offset into a section attribute data and value.
213 void addSectionOffset(DIE *Die, dwarf::Attribute Attribute, uint64_t Integer);
215 /// addOpAddress - Add a dwarf op address data and value using the
216 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
217 void addOpAddress(DIEBlock *Die, const MCSymbol *Label);
219 /// addSectionDelta - Add a label delta attribute data and value.
220 void addSectionDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
223 /// addDIEEntry - Add a DIE attribute data and value.
224 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
226 /// addDIEEntry - Add a DIE attribute data and value.
227 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
229 /// addBlock - Add block data.
230 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
232 /// addSourceLine - Add location information to specified debug information
234 void addSourceLine(DIE *Die, DIVariable V);
235 void addSourceLine(DIE *Die, DIGlobalVariable G);
236 void addSourceLine(DIE *Die, DISubprogram SP);
237 void addSourceLine(DIE *Die, DIType Ty);
238 void addSourceLine(DIE *Die, DINameSpace NS);
239 void addSourceLine(DIE *Die, DIObjCProperty Ty);
241 /// addAddress - Add an address attribute to a die based on the location
243 void addAddress(DIE *Die, dwarf::Attribute Attribute,
244 const MachineLocation &Location, bool Indirect = false);
246 /// addConstantValue - Add constant value entry in variable DIE.
247 void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
248 void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
249 void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
251 /// addConstantFPValue - Add constant value entry in variable DIE.
252 void addConstantFPValue(DIE *Die, const MachineOperand &MO);
253 void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
255 /// addTemplateParams - Add template parameters in buffer.
256 void addTemplateParams(DIE &Buffer, DIArray TParams);
258 /// addRegisterOp - Add register operand.
259 void addRegisterOp(DIEBlock *TheDie, unsigned Reg);
261 /// addRegisterOffset - Add register offset.
262 void addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset);
264 /// addComplexAddress - Start with the address based on the location provided,
265 /// and generate the DWARF information necessary to find the actual variable
266 /// (navigating the extra location information encoded in the type) based on
267 /// the starting location. Add the DWARF information to the die.
268 void addComplexAddress(const DbgVariable &DV, DIE *Die,
269 dwarf::Attribute Attribute,
270 const MachineLocation &Location);
272 // FIXME: Should be reformulated in terms of addComplexAddress.
273 /// addBlockByrefAddress - Start with the address based on the location
274 /// provided, and generate the DWARF information necessary to find the
275 /// actual Block variable (navigating the Block struct) based on the
276 /// starting location. Add the DWARF information to the die. Obsolete,
277 /// please use addComplexAddress instead.
278 void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
279 dwarf::Attribute Attribute,
280 const MachineLocation &Location);
282 /// addVariableAddress - Add DW_AT_location attribute for a
283 /// DbgVariable based on provided MachineLocation.
284 void addVariableAddress(const DbgVariable &DV, DIE *Die,
285 MachineLocation Location);
287 /// addType - Add a new type attribute to the specified entity. This takes
288 /// and attribute parameter because DW_AT_friend attributes are also
290 void addType(DIE *Entity, DIType Ty,
291 dwarf::Attribute Attribute = dwarf::DW_AT_type);
293 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
294 DIE *getOrCreateNameSpace(DINameSpace NS);
296 /// getOrCreateSubprogramDIE - Create new DIE using SP.
297 DIE *getOrCreateSubprogramDIE(DISubprogram SP);
299 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
301 DIE *getOrCreateTypeDIE(const MDNode *N);
303 /// getOrCreateContextDIE - Get context owner's DIE.
304 DIE *createTypeDIE(DICompositeType Ty);
306 /// getOrCreateContextDIE - Get context owner's DIE.
307 DIE *getOrCreateContextDIE(DIScope Context);
309 /// constructContainingTypeDIEs - Construct DIEs for types that contain
311 void constructContainingTypeDIEs();
313 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
314 DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
316 /// Create a DIE with the given Tag, add the DIE to its parent, and
317 /// call insertDIE if MD is not null.
318 DIE *createAndAddDIE(unsigned Tag, DIE &Parent,
319 DIDescriptor N = DIDescriptor());
321 /// Compute the size of a header for this unit, not including the initial
323 unsigned getHeaderSize() const {
324 return sizeof(int16_t) + // DWARF version number
325 sizeof(int32_t) + // Offset Into Abbrev. Section
326 sizeof(int8_t); // Pointer Size (in bytes)
329 /// Emit the header for this unit, not including the initial length field.
330 void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym);
333 /// getOrCreateStaticMemberDIE - Create new static data member DIE.
334 DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
337 /// constructTypeDIE - Construct basic type die from DIBasicType.
338 void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
340 /// constructTypeDIE - Construct derived type die from DIDerivedType.
341 void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
343 /// constructTypeDIE - Construct type DIE from DICompositeType.
344 void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
346 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
347 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
349 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
350 void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
352 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
353 void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
355 /// constructMemberDIE - Construct member DIE from DIDerivedType.
356 void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
358 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
359 /// DITemplateTypeParameter.
360 void constructTemplateTypeParameterDIE(DIE &Buffer,
361 DITemplateTypeParameter TP);
363 /// constructTemplateValueParameterDIE - Construct new DIE for the given
364 /// DITemplateValueParameter.
365 void constructTemplateValueParameterDIE(DIE &Buffer,
366 DITemplateValueParameter TVP);
368 /// getLowerBoundDefault - Return the default lower bound for an array. If the
369 /// DWARF version doesn't handle the language, return -1.
370 int64_t getDefaultLowerBound() const;
372 /// getDIEEntry - Returns the debug information entry for the specified
374 DIEEntry *getDIEEntry(const MDNode *N) const {
375 return MDNodeToDIEEntryMap.lookup(N);
378 /// insertDIEEntry - Insert debug information entry into the map.
379 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
380 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
383 // getIndexTyDie - Get an anonymous type for index type.
384 DIE *getIndexTyDie() { return IndexTyDie; }
386 // setIndexTyDie - Set D as anonymous type for index which can be reused
388 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
390 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
391 /// information entry.
392 DIEEntry *createDIEEntry(DIE *Entry);
394 /// resolve - Look in the DwarfDebug map for the MDNode that
395 /// corresponds to the reference.
396 template <typename T> T resolve(DIRef<T> Ref) const {
397 return DD->resolve(Ref);
400 /// If this is a named finished type then include it in the list of types for
401 /// the accelerator tables.
402 void updateAcceleratorTables(DIScope Context, DIType Ty, const DIE *TyDIE);
405 class CompileUnit : public Unit {
407 CompileUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
408 DwarfDebug *DW, DwarfUnits *DWU);
410 /// createGlobalVariableDIE - create global variable DIE.
411 void createGlobalVariableDIE(DIGlobalVariable GV);
413 /// addLabelAddress - Add a dwarf label attribute data and value using
414 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
415 void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label);
417 uint16_t getLanguage() const { return getNode().getLanguage(); }
420 class TypeUnit : public Unit {
425 TypeUnit(unsigned UID, DIE *D, uint16_t Language, AsmPrinter *A,
426 DwarfDebug *DW, DwarfUnits *DWU);
428 uint16_t getLanguage() const { return Language; }
430 } // end llvm namespace