1 //===-- llvm/CodeGen/DwarfCompileUnit.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 /// CompileUnit - This dwarf writer support class manages information associated
36 /// with a source file.
38 /// UniqueID - a numeric ID unique among all CUs in the module
42 /// Node - MDNode for the compile unit.
45 /// Language - Language for the translation unit associated with this CU.
48 /// CUDie - Compile unit debug information entry.
50 const OwningPtr<DIE> CUDie;
52 /// Asm - Target of Dwarf emission.
55 // Holders for some common dwarf information.
59 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
62 /// MDNodeToDieMap - Tracks the mapping of unit level debug information
63 /// variables to debug information entries.
64 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
66 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information
67 /// descriptors to debug information entries using a DIEEntry proxy.
68 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
70 /// GlobalNames - A map of globally visible named entities for this unit.
72 StringMap<const DIE *> GlobalNames;
74 /// GlobalTypes - A map of globally visible types for this unit.
76 StringMap<const DIE *> GlobalTypes;
78 /// AccelNames - A map of names for the name accelerator table.
79 /// AccelObjC - A map of objc spec for the objc accelerator table.
80 /// AccelNamespace - A map of names for the namespace accelerator table.
81 /// AccelTypes - A map of names for the type accelerator table.
83 StringMap<std::vector<const DIE *> > AccelNames;
84 StringMap<std::vector<const DIE *> > AccelObjC;
85 StringMap<std::vector<const DIE *> > AccelNamespace;
86 StringMap<std::vector<std::pair<const DIE *, unsigned> > > AccelTypes;
88 /// DIEBlocks - A list of all the DIEBlocks in use.
89 std::vector<DIEBlock *> DIEBlocks;
91 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
92 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
93 /// corresponds to the MDNode mapped with the subprogram DIE.
94 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
96 // DIEValueAllocator - All DIEValues are allocated through this allocator.
97 BumpPtrAllocator DIEValueAllocator;
99 // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
100 DIEInteger *DIEIntegerOne;
103 CompileUnit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A,
104 DwarfDebug *DW, DwarfUnits *DWU);
105 CompileUnit(unsigned UID, DIE *D, uint16_t Language, AsmPrinter *A,
106 DwarfDebug *DW, DwarfUnits *DWU);
110 unsigned getUniqueID() const { return UniqueID; }
111 uint16_t getLanguage() const { return Language; }
112 DICompileUnit getNode() const { return Node; }
113 DIE *getCUDie() const { return CUDie.get(); }
114 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
115 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
117 const StringMap<std::vector<const DIE *> > &getAccelNames() const {
120 const StringMap<std::vector<const DIE *> > &getAccelObjC() const {
123 const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
124 return AccelNamespace;
126 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
127 getAccelTypes() const {
131 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
132 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
134 /// hasContent - Return true if this compile unit has something to write out.
136 bool hasContent() const { return !CUDie->getChildren().empty(); }
138 /// getParentContextString - Get a string containing the language specific
139 /// context for a global name.
140 std::string getParentContextString(DIScope Context) const;
142 /// addGlobalName - Add a new global entity to the compile unit.
144 void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
146 /// addGlobalType - Add a new global type to the compile unit.
148 void addGlobalType(DIType Ty);
150 /// addPubTypes - Add a set of types from the subprogram to the global types.
151 void addPubTypes(DISubprogram SP);
153 /// addAccelName - Add a new name to the name accelerator table.
154 void addAccelName(StringRef Name, const DIE *Die);
156 /// addAccelObjC - Add a new name to the ObjC accelerator table.
157 void addAccelObjC(StringRef Name, const DIE *Die);
159 /// addAccelNamespace - Add a new name to the namespace accelerator table.
160 void addAccelNamespace(StringRef Name, const DIE *Die);
162 /// addAccelType - Add a new type to the type accelerator table.
163 void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
165 /// getDIE - Returns the debug information entry map slot for the
166 /// specified debug variable. We delegate the request to DwarfDebug
167 /// when the MDNode can be part of the type system, since DIEs for
168 /// the type system can be shared across CUs and the mappings are
169 /// kept in DwarfDebug.
170 DIE *getDIE(DIDescriptor D) const;
172 DIEBlock *getDIEBlock() { return new (DIEValueAllocator) DIEBlock(); }
174 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
175 /// when the MDNode can be part of the type system, since DIEs for
176 /// the type system can be shared across CUs and the mappings are
177 /// kept in DwarfDebug.
178 void insertDIE(DIDescriptor Desc, DIE *D);
180 /// addDie - Adds or interns the DIE to the compile unit.
182 void addDie(DIE *Buffer) { CUDie->addChild(Buffer); }
184 /// addFlag - Add a flag that is true to the DIE.
185 void addFlag(DIE *Die, dwarf::Attribute Attribute);
187 /// addUInt - Add an unsigned integer attribute data and value.
189 void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
192 void addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer);
194 /// addSInt - Add an signed integer attribute data and value.
196 void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
199 void addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, int64_t Integer);
201 /// addString - Add a string attribute data and value.
203 void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
205 /// addLocalString - Add a string attribute data and value.
207 void addLocalString(DIE *Die, dwarf::Attribute Attribute,
208 const StringRef Str);
210 /// addExpr - Add a Dwarf expression attribute data and value.
212 void addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr);
214 /// addLabel - Add a Dwarf label attribute data and value.
216 void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
217 const MCSymbol *Label);
219 void addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label);
221 /// addLabelAddress - Add a dwarf label attribute data and value using
222 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
224 void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label);
226 /// addOpAddress - Add a dwarf op address data and value using the
227 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
229 void addOpAddress(DIEBlock *Die, const MCSymbol *Label);
231 /// addDelta - Add a label delta attribute data and value.
233 void addDelta(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
234 const MCSymbol *Hi, const MCSymbol *Lo);
236 /// addDIEEntry - Add a DIE attribute data and value.
238 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
240 /// addDIEEntry - Add a DIE attribute data and value.
242 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
244 /// addBlock - Add block data.
246 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
248 /// addSourceLine - Add location information to specified debug information
250 void addSourceLine(DIE *Die, DIVariable V);
251 void addSourceLine(DIE *Die, DIGlobalVariable G);
252 void addSourceLine(DIE *Die, DISubprogram SP);
253 void addSourceLine(DIE *Die, DIType Ty);
254 void addSourceLine(DIE *Die, DINameSpace NS);
255 void addSourceLine(DIE *Die, DIObjCProperty Ty);
257 /// addAddress - Add an address attribute to a die based on the location
259 void addAddress(DIE *Die, dwarf::Attribute Attribute,
260 const MachineLocation &Location, bool Indirect = false);
262 /// addConstantValue - Add constant value entry in variable DIE.
263 void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
264 void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
265 void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
267 /// addConstantFPValue - Add constant value entry in variable DIE.
268 void addConstantFPValue(DIE *Die, const MachineOperand &MO);
269 void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
271 /// addTemplateParams - Add template parameters in buffer.
272 void addTemplateParams(DIE &Buffer, DIArray TParams);
274 /// addRegisterOp - Add register operand.
275 void addRegisterOp(DIEBlock *TheDie, unsigned Reg);
277 /// addRegisterOffset - Add register offset.
278 void addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset);
280 /// addComplexAddress - Start with the address based on the location provided,
281 /// and generate the DWARF information necessary to find the actual variable
282 /// (navigating the extra location information encoded in the type) based on
283 /// the starting location. Add the DWARF information to the die.
285 void addComplexAddress(const DbgVariable &DV, DIE *Die,
286 dwarf::Attribute Attribute,
287 const MachineLocation &Location);
289 // FIXME: Should be reformulated in terms of addComplexAddress.
290 /// addBlockByrefAddress - Start with the address based on the location
291 /// provided, and generate the DWARF information necessary to find the
292 /// actual Block variable (navigating the Block struct) based on the
293 /// starting location. Add the DWARF information to the die. Obsolete,
294 /// please use addComplexAddress instead.
296 void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
297 dwarf::Attribute Attribute,
298 const MachineLocation &Location);
300 /// addVariableAddress - Add DW_AT_location attribute for a
301 /// DbgVariable based on provided MachineLocation.
302 void addVariableAddress(const DbgVariable &DV, DIE *Die,
303 MachineLocation Location);
305 /// addType - Add a new type attribute to the specified entity. This takes
306 /// and attribute parameter because DW_AT_friend attributes are also
308 void addType(DIE *Entity, DIType Ty,
309 dwarf::Attribute Attribute = dwarf::DW_AT_type);
311 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
312 DIE *getOrCreateNameSpace(DINameSpace NS);
314 /// getOrCreateSubprogramDIE - Create new DIE using SP.
315 DIE *getOrCreateSubprogramDIE(DISubprogram SP);
317 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
319 DIE *getOrCreateTypeDIE(const MDNode *N);
321 /// getOrCreateContextDIE - Get context owner's DIE.
322 DIE *createTypeDIE(DICompositeType Ty);
324 /// getOrCreateContextDIE - Get context owner's DIE.
325 DIE *getOrCreateContextDIE(DIScope Context);
327 /// createGlobalVariableDIE - create global variable DIE.
328 void createGlobalVariableDIE(DIGlobalVariable GV);
330 /// constructContainingTypeDIEs - Construct DIEs for types that contain
332 void constructContainingTypeDIEs();
334 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
335 DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
337 /// Create a DIE with the given Tag, add the DIE to its parent, and
338 /// call insertDIE if MD is not null.
339 DIE *createAndAddDIE(unsigned Tag, DIE &Parent,
340 DIDescriptor N = DIDescriptor());
342 /// constructTypeDIEImpl - Construct type DIE that is not a type unit
343 /// reference from a DICompositeType.
344 void constructTypeDIEImpl(DIE &Buffer, DICompositeType CTy);
346 /// Compute the size of a header for this unit, not including the initial
348 unsigned getHeaderSize() const {
349 return sizeof(int16_t) + // DWARF version number
350 sizeof(int32_t) + // Offset Into Abbrev. Section
351 sizeof(int8_t); // Pointer Size (in bytes)
354 /// Emit the header for this unit, not including the initial length field.
355 void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym);
358 /// constructTypeDIE - Construct basic type die from DIBasicType.
359 void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
361 /// constructTypeDIE - Construct derived type die from DIDerivedType.
362 void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
364 /// constructTypeDIE - Construct type DIE from DICompositeType.
365 void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
367 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
368 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
370 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
371 void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
373 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
374 void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
376 /// constructMemberDIE - Construct member DIE from DIDerivedType.
377 void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
379 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
380 /// DITemplateTypeParameter.
381 void constructTemplateTypeParameterDIE(DIE &Buffer,
382 DITemplateTypeParameter TP);
384 /// constructTemplateValueParameterDIE - Construct new DIE for the given
385 /// DITemplateValueParameter.
386 void constructTemplateValueParameterDIE(DIE &Buffer,
387 DITemplateValueParameter TVP);
389 /// getOrCreateStaticMemberDIE - Create new static data member DIE.
390 DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
392 /// Offset of the CUDie from beginning of debug info section.
393 unsigned DebugInfoOffset;
395 /// getLowerBoundDefault - Return the default lower bound for an array. If the
396 /// DWARF version doesn't handle the language, return -1.
397 int64_t getDefaultLowerBound() const;
399 /// getDIEEntry - Returns the debug information entry for the specified
401 DIEEntry *getDIEEntry(const MDNode *N) const {
402 return MDNodeToDIEEntryMap.lookup(N);
405 /// insertDIEEntry - Insert debug information entry into the map.
406 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
407 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
410 // getIndexTyDie - Get an anonymous type for index type.
411 DIE *getIndexTyDie() { return IndexTyDie; }
413 // setIndexTyDie - Set D as anonymous type for index which can be reused
415 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
417 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
418 /// information entry.
419 DIEEntry *createDIEEntry(DIE *Entry);
421 /// resolve - Look in the DwarfDebug map for the MDNode that
422 /// corresponds to the reference.
423 template <typename T> T resolve(DIRef<T> Ref) const {
424 return DD->resolve(Ref);
427 /// If this is a named finished type then include it in the list of types for
428 /// the accelerator tables.
429 void updateAcceleratorTables(DIType Ty, const DIE *TyDIE);
432 } // end llvm namespace