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/OwningPtr.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/DebugInfo.h"
23 #include "llvm/MC/MCExpr.h"
27 class MachineLocation;
33 //===----------------------------------------------------------------------===//
34 /// CompileUnit - This dwarf writer support class manages information associated
35 /// with a source file.
37 /// UniqueID - a numeric ID unique among all CUs in the module
41 /// Node - MDNode for the compile unit.
44 /// Die - Compile unit debug information entry.
46 const OwningPtr<DIE> CUDie;
48 /// Asm - Target of Dwarf emission.
51 // Holders for some common dwarf information.
55 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
58 /// MDNodeToDieMap - Tracks the mapping of unit level debug information
59 /// variables to debug information entries.
60 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
62 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information
63 /// descriptors to debug information entries using a DIEEntry proxy.
64 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
66 /// GlobalNames - A map of globally visible named entities for this unit.
68 StringMap<DIE *> GlobalNames;
70 /// GlobalTypes - A map of globally visible types for this unit.
72 StringMap<DIE *> GlobalTypes;
74 /// AccelNames - A map of names for the name accelerator table.
76 StringMap<std::vector<DIE *> > AccelNames;
77 StringMap<std::vector<DIE *> > AccelObjC;
78 StringMap<std::vector<DIE *> > AccelNamespace;
79 StringMap<std::vector<std::pair<DIE *, unsigned> > > AccelTypes;
81 /// DIEBlocks - A list of all the DIEBlocks in use.
82 std::vector<DIEBlock *> DIEBlocks;
84 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
85 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
86 /// corresponds to the MDNode mapped with the subprogram DIE.
87 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
90 CompileUnit(unsigned UID, DIE *D, const MDNode *N, AsmPrinter *A,
91 DwarfDebug *DW, DwarfUnits *DWU);
95 unsigned getUniqueID() const { return UniqueID; }
96 uint16_t getLanguage() const { return DICompileUnit(Node).getLanguage(); }
97 const MDNode *getNode() const { return Node; }
98 DIE *getCUDie() const { return CUDie.get(); }
99 const StringMap<DIE *> &getGlobalNames() const { return GlobalNames; }
100 const StringMap<DIE *> &getGlobalTypes() const { return GlobalTypes; }
102 const StringMap<std::vector<DIE *> > &getAccelNames() const {
105 const StringMap<std::vector<DIE *> > &getAccelObjC() const {
108 const StringMap<std::vector<DIE *> > &getAccelNamespace() const {
109 return AccelNamespace;
111 const StringMap<std::vector<std::pair<DIE *, unsigned> > > &
112 getAccelTypes() const {
116 /// hasContent - Return true if this compile unit has something to write out.
118 bool hasContent() const { return !CUDie->getChildren().empty(); }
120 /// addGlobalName - Add a new global entity to the compile unit.
122 void addGlobalName(StringRef Name, DIE *Die);
124 /// addGlobalType - Add a new global type to the compile unit.
126 void addGlobalType(DIType Ty);
128 /// addPubTypes - Add a set of types from the subprogram to the global types.
129 void addPubTypes(DISubprogram SP);
131 /// addAccelName - Add a new name to the name accelerator table.
132 void addAccelName(StringRef Name, DIE *Die);
134 /// addAccelObjC - Add a new name to the ObjC accelerator table.
135 void addAccelObjC(StringRef Name, DIE *Die);
137 /// addAccelNamespace - Add a new name to the namespace accelerator table.
138 void addAccelNamespace(StringRef Name, DIE *Die);
140 /// addAccelType - Add a new type to the type accelerator table.
141 void addAccelType(StringRef Name, std::pair<DIE *, unsigned> Die);
143 /// getDIE - Returns the debug information entry map slot for the
144 /// specified debug variable.
145 DIE *getDIE(const MDNode *N) const { return MDNodeToDieMap.lookup(N); }
147 DIEBlock *getDIEBlock() { return new (DIEValueAllocator) DIEBlock(); }
149 /// insertDIE - Insert DIE into the map.
150 void insertDIE(const MDNode *N, DIE *D) {
151 MDNodeToDieMap.insert(std::make_pair(N, D));
154 /// addDie - Adds or interns the DIE to the compile unit.
156 void addDie(DIE *Buffer) { CUDie->addChild(Buffer); }
158 /// addFlag - Add a flag that is true to the DIE.
159 void addFlag(DIE *Die, uint16_t Attribute);
161 /// addUInt - Add an unsigned integer attribute data and value.
163 void addUInt(DIE *Die, uint16_t Attribute, uint16_t Form, uint64_t Integer);
165 /// addSInt - Add an signed integer attribute data and value.
167 void addSInt(DIE *Die, uint16_t Attribute, uint16_t Form, int64_t Integer);
169 /// addString - Add a string attribute data and value.
171 void addString(DIE *Die, uint16_t Attribute, const StringRef Str);
173 /// addLocalString - Add a string attribute data and value.
175 void addLocalString(DIE *Die, uint16_t Attribute, const StringRef Str);
177 /// addExpr - Add a Dwarf expression attribute data and value.
179 void addExpr(DIE *Die, uint16_t Attribute, uint16_t Form, const MCExpr *Expr);
181 /// addLabel - Add a Dwarf label attribute data and value.
183 void addLabel(DIE *Die, uint16_t Attribute, uint16_t Form,
184 const MCSymbol *Label);
186 /// addLabelAddress - Add a dwarf label attribute data and value using
187 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
189 void addLabelAddress(DIE *Die, uint16_t Attribute, MCSymbol *Label);
191 /// addOpAddress - Add a dwarf op address data and value using the
192 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
194 void addOpAddress(DIE *Die, const MCSymbol *Label);
195 void addOpAddress(DIE *Die, const MCSymbolRefExpr *Label);
197 /// addDelta - Add a label delta attribute data and value.
199 void addDelta(DIE *Die, uint16_t Attribute, uint16_t Form, const MCSymbol *Hi,
202 /// addDIEEntry - Add a DIE attribute data and value.
204 void addDIEEntry(DIE *Die, uint16_t Attribute, uint16_t Form, DIE *Entry);
206 /// addBlock - Add block data.
208 void addBlock(DIE *Die, uint16_t Attribute, uint16_t Form, DIEBlock *Block);
210 /// addSourceLine - Add location information to specified debug information
212 void addSourceLine(DIE *Die, DIVariable V);
213 void addSourceLine(DIE *Die, DIGlobalVariable G);
214 void addSourceLine(DIE *Die, DISubprogram SP);
215 void addSourceLine(DIE *Die, DIType Ty);
216 void addSourceLine(DIE *Die, DINameSpace NS);
217 void addSourceLine(DIE *Die, DIObjCProperty Ty);
219 /// addAddress - Add an address attribute to a die based on the location
221 void addAddress(DIE *Die, uint16_t Attribute, const MachineLocation &Location,
222 bool Indirect = false);
224 /// addConstantValue - Add constant value entry in variable DIE.
225 void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
226 void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
227 void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
229 /// addConstantFPValue - Add constant value entry in variable DIE.
230 void addConstantFPValue(DIE *Die, const MachineOperand &MO);
231 void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
233 /// addTemplateParams - Add template parameters in buffer.
234 void addTemplateParams(DIE &Buffer, DIArray TParams);
236 /// addRegisterOp - Add register operand.
237 void addRegisterOp(DIE *TheDie, unsigned Reg);
239 /// addRegisterOffset - Add register offset.
240 void addRegisterOffset(DIE *TheDie, unsigned Reg, int64_t Offset);
242 /// addComplexAddress - Start with the address based on the location provided,
243 /// and generate the DWARF information necessary to find the actual variable
244 /// (navigating the extra location information encoded in the type) based on
245 /// the starting location. Add the DWARF information to the die.
247 void addComplexAddress(const DbgVariable &DV, DIE *Die, uint16_t Attribute,
248 const MachineLocation &Location);
250 // FIXME: Should be reformulated in terms of addComplexAddress.
251 /// addBlockByrefAddress - Start with the address based on the location
252 /// provided, and generate the DWARF information necessary to find the
253 /// actual Block variable (navigating the Block struct) based on the
254 /// starting location. Add the DWARF information to the die. Obsolete,
255 /// please use addComplexAddress instead.
257 void addBlockByrefAddress(const DbgVariable &DV, DIE *Die, uint16_t Attribute,
258 const MachineLocation &Location);
260 /// addVariableAddress - Add DW_AT_location attribute for a
261 /// DbgVariable based on provided MachineLocation.
262 void addVariableAddress(const DbgVariable &DV, DIE *Die,
263 MachineLocation Location);
265 /// addToContextOwner - Add Die into the list of its context owner's children.
266 void addToContextOwner(DIE *Die, DIScope Context);
268 /// addType - Add a new type attribute to the specified entity. This takes
269 /// and attribute parameter because DW_AT_friend attributes are also
271 void addType(DIE *Entity, DIType Ty, uint16_t Attribute = dwarf::DW_AT_type);
273 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
274 DIE *getOrCreateNameSpace(DINameSpace NS);
276 /// getOrCreateSubprogramDIE - Create new DIE using SP.
277 DIE *getOrCreateSubprogramDIE(DISubprogram SP);
279 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
281 DIE *getOrCreateTypeDIE(const MDNode *N);
283 /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
284 /// for the given DITemplateTypeParameter.
285 DIE *getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP);
287 /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create
288 /// new DIE for the given DITemplateValueParameter.
289 DIE *getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TVP);
291 /// getOrCreateContextDIE - Get context owner's DIE.
292 DIE *getOrCreateContextDIE(DIScope Context);
294 /// createGlobalVariableDIE - create global variable DIE.
295 void createGlobalVariableDIE(const MDNode *N);
297 /// constructContainingTypeDIEs - Construct DIEs for types that contain
299 void constructContainingTypeDIEs();
301 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
302 DIE *constructVariableDIE(DbgVariable *DV, bool isScopeAbstract);
305 /// constructTypeDIE - Construct basic type die from DIBasicType.
306 void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
308 /// constructTypeDIE - Construct derived type die from DIDerivedType.
309 void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
311 /// constructTypeDIE - Construct type DIE from DICompositeType.
312 void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
314 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
315 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
317 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
318 void constructArrayTypeDIE(DIE &Buffer, DICompositeType *CTy);
320 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
321 DIE *constructEnumTypeDIE(DIEnumerator ETy);
323 /// createMemberDIE - Create new member DIE.
324 DIE *createMemberDIE(DIDerivedType DT);
326 /// createStaticMemberDIE - Create new static data member DIE.
327 DIE *createStaticMemberDIE(DIDerivedType DT);
329 /// getLowerBoundDefault - Return the default lower bound for an array. If the
330 /// DWARF version doesn't handle the language, return -1.
331 int64_t getDefaultLowerBound() const;
333 /// getDIEEntry - Returns the debug information entry for the specified
335 DIEEntry *getDIEEntry(const MDNode *N) const {
336 return MDNodeToDIEEntryMap.lookup(N);
339 /// insertDIEEntry - Insert debug information entry into the map.
340 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
341 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
344 // getIndexTyDie - Get an anonymous type for index type.
345 DIE *getIndexTyDie() { return IndexTyDie; }
347 // setIndexTyDie - Set D as anonymous type for index which can be reused
349 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
351 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
352 /// information entry.
353 DIEEntry *createDIEEntry(DIE *Entry);
355 /// resolve - Look in the DwarfDebug map for the MDNode that
356 /// corresponds to the reference.
357 template <typename T> T resolve(DIRef<T> Ref) const {
358 return DD->resolve(Ref);
363 // DIEValueAllocator - All DIEValues are allocated through this allocator.
364 BumpPtrAllocator DIEValueAllocator;
365 DIEInteger *DIEIntegerOne;
368 } // end llvm namespace