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
41 /// Node - MDNode for the compile unit.
44 /// Language - Language for the translation unit associated with this CU.
47 /// CUDie - Compile unit debug information entry.
48 const OwningPtr<DIE> CUDie;
50 /// Asm - Target of Dwarf emission.
53 // Holders for some common dwarf information.
57 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
60 /// MDNodeToDieMap - Tracks the mapping of unit level debug information
61 /// variables to debug information entries.
62 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
64 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information
65 /// descriptors to debug information entries using a DIEEntry proxy.
66 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
68 /// GlobalNames - A map of globally visible named entities for this unit.
69 StringMap<const DIE *> GlobalNames;
71 /// GlobalTypes - A map of globally visible types for this unit.
72 StringMap<const DIE *> GlobalTypes;
74 /// AccelNames - A map of names for the name accelerator table.
75 StringMap<std::vector<const DIE *> > AccelNames;
77 /// AccelObjC - A map of objc spec for the objc accelerator table.
78 StringMap<std::vector<const DIE *> > AccelObjC;
80 /// AccelNamespace - A map of names for the namespace accelerator table.
81 StringMap<std::vector<const DIE *> > AccelNamespace;
83 /// AccelTypes - A map of names for the type accelerator table.
84 StringMap<std::vector<std::pair<const DIE *, unsigned> > > AccelTypes;
86 /// DIEBlocks - A list of all the DIEBlocks in use.
87 std::vector<DIEBlock *> DIEBlocks;
89 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
90 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
91 /// corresponds to the MDNode mapped with the subprogram DIE.
92 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
94 // DIEValueAllocator - All DIEValues are allocated through this allocator.
95 BumpPtrAllocator DIEValueAllocator;
97 // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
98 DIEInteger *DIEIntegerOne;
100 /// Offset of the CUDie from beginning of debug info section.
101 unsigned DebugInfoOffset;
104 CompileUnit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A,
105 DwarfDebug *DW, DwarfUnits *DWU);
106 CompileUnit(unsigned UID, DIE *D, uint16_t Language, AsmPrinter *A,
107 DwarfDebug *DW, DwarfUnits *DWU);
111 unsigned getUniqueID() const { return UniqueID; }
112 uint16_t getLanguage() const { return Language; }
113 DICompileUnit getNode() const { return Node; }
114 DIE *getCUDie() const { return CUDie.get(); }
115 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
116 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
118 const StringMap<std::vector<const DIE *> > &getAccelNames() const {
121 const StringMap<std::vector<const DIE *> > &getAccelObjC() const {
124 const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
125 return AccelNamespace;
127 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
128 getAccelTypes() const {
132 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
133 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
135 /// 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.
147 void addGlobalType(DIType Ty);
149 /// addPubTypes - Add a set of types from the subprogram to the global types.
150 void addPubTypes(DISubprogram SP);
152 /// addAccelName - Add a new name to the name accelerator table.
153 void addAccelName(StringRef Name, const DIE *Die);
155 /// addAccelObjC - Add a new name to the ObjC accelerator table.
156 void addAccelObjC(StringRef Name, const DIE *Die);
158 /// addAccelNamespace - Add a new name to the namespace accelerator table.
159 void addAccelNamespace(StringRef Name, const DIE *Die);
161 /// addAccelType - Add a new type to the type accelerator table.
162 void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
164 /// getDIE - Returns the debug information entry map slot for the
165 /// specified debug variable. 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 DIE *getDIE(DIDescriptor D) const;
171 /// getDIEBlock - Returns a fresh newly allocated DIEBlock.
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.
188 void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
191 void addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer);
193 /// addSInt - Add an signed integer attribute data and value.
194 void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
197 void addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, int64_t Integer);
199 /// addString - Add a string attribute data and value.
200 void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
202 /// addLocalString - Add a string attribute data and value.
203 void addLocalString(DIE *Die, dwarf::Attribute Attribute,
204 const StringRef Str);
206 /// addExpr - Add a Dwarf expression attribute data and value.
207 void addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr);
209 /// addLabel - Add a Dwarf label attribute data and value.
210 void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
211 const MCSymbol *Label);
213 void addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label);
215 /// addLabelAddress - Add a dwarf label attribute data and value using
216 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
217 void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label);
219 /// addOpAddress - Add a dwarf op address data and value using the
220 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
221 void addOpAddress(DIEBlock *Die, const MCSymbol *Label);
223 /// addDelta - Add a label delta attribute data and value.
224 void addDelta(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
225 const MCSymbol *Hi, const MCSymbol *Lo);
227 /// addDIEEntry - Add a DIE attribute data and value.
228 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
230 /// addDIEEntry - Add a DIE attribute data and value.
231 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
233 /// addBlock - Add block data.
234 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
236 /// addSourceLine - Add location information to specified debug information
238 void addSourceLine(DIE *Die, DIVariable V);
239 void addSourceLine(DIE *Die, DIGlobalVariable G);
240 void addSourceLine(DIE *Die, DISubprogram SP);
241 void addSourceLine(DIE *Die, DIType Ty);
242 void addSourceLine(DIE *Die, DINameSpace NS);
243 void addSourceLine(DIE *Die, DIObjCProperty Ty);
245 /// addAddress - Add an address attribute to a die based on the location
247 void addAddress(DIE *Die, dwarf::Attribute Attribute,
248 const MachineLocation &Location, bool Indirect = false);
250 /// addConstantValue - Add constant value entry in variable DIE.
251 void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
252 void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
253 void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
255 /// addConstantFPValue - Add constant value entry in variable DIE.
256 void addConstantFPValue(DIE *Die, const MachineOperand &MO);
257 void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
259 /// addTemplateParams - Add template parameters in buffer.
260 void addTemplateParams(DIE &Buffer, DIArray TParams);
262 /// addRegisterOp - Add register operand.
263 void addRegisterOp(DIEBlock *TheDie, unsigned Reg);
265 /// addRegisterOffset - Add register offset.
266 void addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset);
268 /// addComplexAddress - Start with the address based on the location provided,
269 /// and generate the DWARF information necessary to find the actual variable
270 /// (navigating the extra location information encoded in the type) based on
271 /// the starting location. Add the DWARF information to the die.
272 void addComplexAddress(const DbgVariable &DV, DIE *Die,
273 dwarf::Attribute Attribute,
274 const MachineLocation &Location);
276 // FIXME: Should be reformulated in terms of addComplexAddress.
277 /// addBlockByrefAddress - Start with the address based on the location
278 /// provided, and generate the DWARF information necessary to find the
279 /// actual Block variable (navigating the Block struct) based on the
280 /// starting location. Add the DWARF information to the die. Obsolete,
281 /// please use addComplexAddress instead.
282 void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
283 dwarf::Attribute Attribute,
284 const MachineLocation &Location);
286 /// addVariableAddress - Add DW_AT_location attribute for a
287 /// DbgVariable based on provided MachineLocation.
288 void addVariableAddress(const DbgVariable &DV, DIE *Die,
289 MachineLocation Location);
291 /// addType - Add a new type attribute to the specified entity. This takes
292 /// and attribute parameter because DW_AT_friend attributes are also
294 void addType(DIE *Entity, DIType Ty,
295 dwarf::Attribute Attribute = dwarf::DW_AT_type);
297 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
298 DIE *getOrCreateNameSpace(DINameSpace NS);
300 /// getOrCreateSubprogramDIE - Create new DIE using SP.
301 DIE *getOrCreateSubprogramDIE(DISubprogram SP);
303 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
305 DIE *getOrCreateTypeDIE(const MDNode *N);
307 /// getOrCreateContextDIE - Get context owner's DIE.
308 DIE *createTypeDIE(DICompositeType Ty);
310 /// getOrCreateContextDIE - Get context owner's DIE.
311 DIE *getOrCreateContextDIE(DIScope Context);
313 /// createGlobalVariableDIE - create global variable DIE.
314 void createGlobalVariableDIE(DIGlobalVariable GV);
316 /// constructContainingTypeDIEs - Construct DIEs for types that contain
318 void constructContainingTypeDIEs();
320 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
321 DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
323 /// Create a DIE with the given Tag, add the DIE to its parent, and
324 /// call insertDIE if MD is not null.
325 DIE *createAndAddDIE(unsigned Tag, DIE &Parent,
326 DIDescriptor N = DIDescriptor());
328 /// constructTypeDIEImpl - Construct type DIE that is not a type unit
329 /// reference from a DICompositeType.
330 void constructTypeDIEImpl(DIE &Buffer, DICompositeType CTy);
332 /// Compute the size of a header for this unit, not including the initial
334 unsigned getHeaderSize() const {
335 return sizeof(int16_t) + // DWARF version number
336 sizeof(int32_t) + // Offset Into Abbrev. Section
337 sizeof(int8_t); // Pointer Size (in bytes)
340 /// Emit the header for this unit, not including the initial length field.
341 void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym);
344 /// constructTypeDIE - Construct basic type die from DIBasicType.
345 void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
347 /// constructTypeDIE - Construct derived type die from DIDerivedType.
348 void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
350 /// constructTypeDIE - Construct type DIE from DICompositeType.
351 void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
353 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
354 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
356 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
357 void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
359 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
360 void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
362 /// constructMemberDIE - Construct member DIE from DIDerivedType.
363 void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
365 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
366 /// DITemplateTypeParameter.
367 void constructTemplateTypeParameterDIE(DIE &Buffer,
368 DITemplateTypeParameter TP);
370 /// constructTemplateValueParameterDIE - Construct new DIE for the given
371 /// DITemplateValueParameter.
372 void constructTemplateValueParameterDIE(DIE &Buffer,
373 DITemplateValueParameter TVP);
375 /// getOrCreateStaticMemberDIE - Create new static data member DIE.
376 DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
378 /// getLowerBoundDefault - Return the default lower bound for an array. If the
379 /// DWARF version doesn't handle the language, return -1.
380 int64_t getDefaultLowerBound() const;
382 /// getDIEEntry - Returns the debug information entry for the specified
384 DIEEntry *getDIEEntry(const MDNode *N) const {
385 return MDNodeToDIEEntryMap.lookup(N);
388 /// insertDIEEntry - Insert debug information entry into the map.
389 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
390 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
393 // getIndexTyDie - Get an anonymous type for index type.
394 DIE *getIndexTyDie() { return IndexTyDie; }
396 // setIndexTyDie - Set D as anonymous type for index which can be reused
398 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
400 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
401 /// information entry.
402 DIEEntry *createDIEEntry(DIE *Entry);
404 /// resolve - Look in the DwarfDebug map for the MDNode that
405 /// corresponds to the reference.
406 template <typename T> T resolve(DIRef<T> Ref) const {
407 return DD->resolve(Ref);
410 /// If this is a named finished type then include it in the list of types for
411 /// the accelerator tables.
412 void updateAcceleratorTables(DIType Ty, const DIE *TyDIE);
415 } // end llvm namespace