DebugInfo: Rename DwarfCompileUnit.* to DwarfUnit.* to match their contents.
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfUnit.h
1 //===-- llvm/CodeGen/DwarfUnit.h - Dwarf Compile Unit ---*- C++ -*--===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for writing dwarf compile unit.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
15 #define CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
16
17 #include "DIE.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"
25
26 namespace llvm {
27
28 class MachineLocation;
29 class MachineOperand;
30 class ConstantInt;
31 class ConstantFP;
32 class DbgVariable;
33
34 //===----------------------------------------------------------------------===//
35 /// Unit - This dwarf writer support class manages information associated
36 /// with a source file.
37 class Unit {
38 protected:
39   /// UniqueID - a numeric ID unique among all CUs in the module
40   unsigned UniqueID;
41
42   /// Node - MDNode for the compile unit.
43   DICompileUnit Node;
44
45   /// CUDie - Compile unit debug information entry.
46   const OwningPtr<DIE> CUDie;
47
48   /// Offset of the CUDie from beginning of debug info section.
49   unsigned DebugInfoOffset;
50
51   /// Asm - Target of Dwarf emission.
52   AsmPrinter *Asm;
53
54   // Holders for some common dwarf information.
55   DwarfDebug *DD;
56   DwarfUnits *DU;
57
58   /// IndexTyDie - An anonymous type for index type.  Owned by CUDie.
59   DIE *IndexTyDie;
60
61   /// MDNodeToDieMap - Tracks the mapping of unit level debug information
62   /// variables to debug information entries.
63   DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
64
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;
68
69   /// GlobalNames - A map of globally visible named entities for this unit.
70   StringMap<const DIE *> GlobalNames;
71
72   /// GlobalTypes - A map of globally visible types for this unit.
73   StringMap<const DIE *> GlobalTypes;
74
75   /// AccelNames - A map of names for the name accelerator table.
76   StringMap<std::vector<const DIE *> > AccelNames;
77
78   /// AccelObjC - A map of objc spec for the objc accelerator table.
79   StringMap<std::vector<const DIE *> > AccelObjC;
80
81   /// AccelNamespace - A map of names for the namespace accelerator table.
82   StringMap<std::vector<const DIE *> > AccelNamespace;
83
84   /// AccelTypes - A map of names for the type accelerator table.
85   StringMap<std::vector<std::pair<const DIE *, unsigned> > > AccelTypes;
86
87   /// DIEBlocks - A list of all the DIEBlocks in use.
88   std::vector<DIEBlock *> DIEBlocks;
89
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;
94
95   // DIEValueAllocator - All DIEValues are allocated through this allocator.
96   BumpPtrAllocator DIEValueAllocator;
97
98   // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
99   DIEInteger *DIEIntegerOne;
100
101   Unit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A, DwarfDebug *DW,
102        DwarfUnits *DWU);
103
104 public:
105   virtual ~Unit();
106
107   // Accessors.
108   unsigned getUniqueID() const { return UniqueID; }
109   virtual uint16_t getLanguage() const = 0;
110   DICompileUnit getNode() const { return Node; }
111   DIE *getCUDie() const { return CUDie.get(); }
112   const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
113   const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
114
115   const StringMap<std::vector<const DIE *> > &getAccelNames() const {
116     return AccelNames;
117   }
118   const StringMap<std::vector<const DIE *> > &getAccelObjC() const {
119     return AccelObjC;
120   }
121   const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
122     return AccelNamespace;
123   }
124   const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
125   getAccelTypes() const {
126     return AccelTypes;
127   }
128
129   unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
130   void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
131
132   /// hasContent - Return true if this compile unit has something to write out.
133   bool hasContent() const { return !CUDie->getChildren().empty(); }
134
135   /// getParentContextString - Get a string containing the language specific
136   /// context for a global name.
137   std::string getParentContextString(DIScope Context) const;
138
139   /// addGlobalName - Add a new global entity to the compile unit.
140   ///
141   void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
142
143   /// addAccelName - Add a new name to the name accelerator table.
144   void addAccelName(StringRef Name, const DIE *Die);
145
146   /// addAccelObjC - Add a new name to the ObjC accelerator table.
147   void addAccelObjC(StringRef Name, const DIE *Die);
148
149   /// addAccelNamespace - Add a new name to the namespace accelerator table.
150   void addAccelNamespace(StringRef Name, const DIE *Die);
151
152   /// addAccelType - Add a new type to the type accelerator table.
153   void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
154
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;
161
162   /// getDIEBlock - Returns a fresh newly allocated DIEBlock.
163   DIEBlock *getDIEBlock() { return new (DIEValueAllocator) DIEBlock(); }
164
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);
170
171   /// addDie - Adds or interns the DIE to the compile unit.
172   ///
173   void addDie(DIE *Buffer) { CUDie->addChild(Buffer); }
174
175   /// addFlag - Add a flag that is true to the DIE.
176   void addFlag(DIE *Die, dwarf::Attribute Attribute);
177
178   /// addUInt - Add an unsigned integer attribute data and value.
179   void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
180                uint64_t Integer);
181
182   void addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer);
183
184   /// addSInt - Add an signed integer attribute data and value.
185   void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
186                int64_t Integer);
187
188   void addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, int64_t Integer);
189
190   /// addString - Add a string attribute data and value.
191   void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
192
193   /// addLocalString - Add a string attribute data and value.
194   void addLocalString(DIE *Die, dwarf::Attribute Attribute,
195                       const StringRef Str);
196
197   /// addExpr - Add a Dwarf expression attribute data and value.
198   void addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr);
199
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);
203
204   void addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label);
205
206   /// addSectionLabel - Add a Dwarf section label attribute data and value.
207   ///
208   void addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
209                        const MCSymbol *Label);
210
211   /// addSectionOffset - Add an offset into a section attribute data and value.
212   ///
213   void addSectionOffset(DIE *Die, dwarf::Attribute Attribute, uint64_t Integer);
214
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);
218
219   /// addSectionDelta - Add a label delta attribute data and value.
220   void addSectionDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
221                        const MCSymbol *Lo);
222
223   /// addDIEEntry - Add a DIE attribute data and value.
224   void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
225
226   /// addDIEEntry - Add a DIE attribute data and value.
227   void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
228
229   /// addBlock - Add block data.
230   void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
231
232   /// addSourceLine - Add location information to specified debug information
233   /// entry.
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);
240
241   /// addAddress - Add an address attribute to a die based on the location
242   /// provided.
243   void addAddress(DIE *Die, dwarf::Attribute Attribute,
244                   const MachineLocation &Location, bool Indirect = false);
245
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);
250
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);
254
255   /// addTemplateParams - Add template parameters in buffer.
256   void addTemplateParams(DIE &Buffer, DIArray TParams);
257
258   /// addRegisterOp - Add register operand.
259   void addRegisterOp(DIEBlock *TheDie, unsigned Reg);
260
261   /// addRegisterOffset - Add register offset.
262   void addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset);
263
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);
271
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);
281
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);
286
287   /// addType - Add a new type attribute to the specified entity. This takes
288   /// and attribute parameter because DW_AT_friend attributes are also
289   /// type references.
290   void addType(DIE *Entity, DIType Ty,
291                dwarf::Attribute Attribute = dwarf::DW_AT_type);
292
293   /// getOrCreateNameSpace - Create a DIE for DINameSpace.
294   DIE *getOrCreateNameSpace(DINameSpace NS);
295
296   /// getOrCreateSubprogramDIE - Create new DIE using SP.
297   DIE *getOrCreateSubprogramDIE(DISubprogram SP);
298
299   /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
300   /// given DIType.
301   DIE *getOrCreateTypeDIE(const MDNode *N);
302
303   /// getOrCreateContextDIE - Get context owner's DIE.
304   DIE *createTypeDIE(DICompositeType Ty);
305
306   /// getOrCreateContextDIE - Get context owner's DIE.
307   DIE *getOrCreateContextDIE(DIScope Context);
308
309   /// constructContainingTypeDIEs - Construct DIEs for types that contain
310   /// vtables.
311   void constructContainingTypeDIEs();
312
313   /// constructVariableDIE - Construct a DIE for the given DbgVariable.
314   DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
315
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());
320
321   /// Compute the size of a header for this unit, not including the initial
322   /// length field.
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)
327   }
328
329   /// Emit the header for this unit, not including the initial length field.
330   void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym);
331
332 protected:
333   /// getOrCreateStaticMemberDIE - Create new static data member DIE.
334   DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
335
336 private:
337   /// constructTypeDIE - Construct basic type die from DIBasicType.
338   void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
339
340   /// constructTypeDIE - Construct derived type die from DIDerivedType.
341   void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
342
343   /// constructTypeDIE - Construct type DIE from DICompositeType.
344   void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
345
346   /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
347   void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
348
349   /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
350   void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
351
352   /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
353   void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
354
355   /// constructMemberDIE - Construct member DIE from DIDerivedType.
356   void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
357
358   /// constructTemplateTypeParameterDIE - Construct new DIE for the given
359   /// DITemplateTypeParameter.
360   void constructTemplateTypeParameterDIE(DIE &Buffer,
361                                          DITemplateTypeParameter TP);
362
363   /// constructTemplateValueParameterDIE - Construct new DIE for the given
364   /// DITemplateValueParameter.
365   void constructTemplateValueParameterDIE(DIE &Buffer,
366                                           DITemplateValueParameter TVP);
367
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;
371
372   /// getDIEEntry - Returns the debug information entry for the specified
373   /// debug variable.
374   DIEEntry *getDIEEntry(const MDNode *N) const {
375     return MDNodeToDIEEntryMap.lookup(N);
376   }
377
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));
381   }
382
383   // getIndexTyDie - Get an anonymous type for index type.
384   DIE *getIndexTyDie() { return IndexTyDie; }
385
386   // setIndexTyDie - Set D as anonymous type for index which can be reused
387   // later.
388   void setIndexTyDie(DIE *D) { IndexTyDie = D; }
389
390   /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
391   /// information entry.
392   DIEEntry *createDIEEntry(DIE *Entry);
393
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);
398   }
399
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);
403 };
404
405 class CompileUnit : public Unit {
406 public:
407   CompileUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
408               DwarfDebug *DW, DwarfUnits *DWU);
409
410   /// createGlobalVariableDIE - create global variable DIE.
411   void createGlobalVariableDIE(DIGlobalVariable GV);
412
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);
416
417   uint16_t getLanguage() const { return getNode().getLanguage(); }
418 };
419
420 class TypeUnit : public Unit {
421 private:
422   uint16_t Language;
423
424 public:
425   TypeUnit(unsigned UID, DIE *D, uint16_t Language, AsmPrinter *A,
426            DwarfDebug *DW, DwarfUnits *DWU);
427
428   uint16_t getLanguage() const { return Language; }
429 };
430 } // end llvm namespace
431 #endif