07f5a8592b478b097f1ac3fe963eb39720578514
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfCompileUnit.h
1 //===-- llvm/CodeGen/DwarfCompileUnit.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 /// CompileUnit - This dwarf writer support class manages information associated
36 /// with a source file.
37 class CompileUnit {
38   /// UniqueID - a numeric ID unique among all CUs in the module
39   ///
40   unsigned UniqueID;
41
42   /// Node - MDNode for the compile unit.
43   DICompileUnit Node;
44
45   /// Language - Language for the translation unit associated with this CU.
46   uint16_t Language;
47
48   /// CUDie - Compile unit debug information entry.
49   ///
50   const OwningPtr<DIE> CUDie;
51
52   /// Asm - Target of Dwarf emission.
53   AsmPrinter *Asm;
54
55   // Holders for some common dwarf information.
56   DwarfDebug *DD;
57   DwarfUnits *DU;
58
59   /// IndexTyDie - An anonymous type for index type.  Owned by CUDie.
60   DIE *IndexTyDie;
61
62   /// MDNodeToDieMap - Tracks the mapping of unit level debug information
63   /// variables to debug information entries.
64   DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
65
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;
69
70   /// GlobalNames - A map of globally visible named entities for this unit.
71   ///
72   StringMap<const DIE *> GlobalNames;
73
74   /// GlobalTypes - A map of globally visible types for this unit.
75   ///
76   StringMap<const DIE *> GlobalTypes;
77
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.
82   ///
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;
87
88   /// DIEBlocks - A list of all the DIEBlocks in use.
89   std::vector<DIEBlock *> DIEBlocks;
90
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;
95
96   // DIEValueAllocator - All DIEValues are allocated through this allocator.
97   BumpPtrAllocator DIEValueAllocator;
98
99   // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
100   DIEInteger *DIEIntegerOne;
101
102 public:
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);
107   ~CompileUnit();
108
109   // Accessors.
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; }
116
117   const StringMap<std::vector<const DIE *> > &getAccelNames() const {
118     return AccelNames;
119   }
120   const StringMap<std::vector<const DIE *> > &getAccelObjC() const {
121     return AccelObjC;
122   }
123   const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
124     return AccelNamespace;
125   }
126   const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
127   getAccelTypes() const {
128     return AccelTypes;
129   }
130
131   unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
132   void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
133
134   /// hasContent - Return true if this compile unit has something to write out.
135   ///
136   bool hasContent() const { return !CUDie->getChildren().empty(); }
137
138   /// getParentContextString - Get a string containing the language specific
139   /// context for a global name.
140   std::string getParentContextString(DIScope Context) const;
141
142   /// addGlobalName - Add a new global entity to the compile unit.
143   ///
144   void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
145
146   /// addGlobalType - Add a new global type to the compile unit.
147   ///
148   void addGlobalType(DIType Ty);
149
150   /// addPubTypes - Add a set of types from the subprogram to the global types.
151   void addPubTypes(DISubprogram SP);
152
153   /// addAccelName - Add a new name to the name accelerator table.
154   void addAccelName(StringRef Name, const DIE *Die);
155
156   /// addAccelObjC - Add a new name to the ObjC accelerator table.
157   void addAccelObjC(StringRef Name, const DIE *Die);
158
159   /// addAccelNamespace - Add a new name to the namespace accelerator table.
160   void addAccelNamespace(StringRef Name, const DIE *Die);
161
162   /// addAccelType - Add a new type to the type accelerator table.
163   void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
164
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;
171
172   DIEBlock *getDIEBlock() { return new (DIEValueAllocator) DIEBlock(); }
173
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);
179
180   /// addDie - Adds or interns the DIE to the compile unit.
181   ///
182   void addDie(DIE *Buffer) { CUDie->addChild(Buffer); }
183
184   /// addFlag - Add a flag that is true to the DIE.
185   void addFlag(DIE *Die, dwarf::Attribute Attribute);
186
187   /// addUInt - Add an unsigned integer attribute data and value.
188   ///
189   void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
190                uint64_t Integer);
191
192   void addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer);
193
194   /// addSInt - Add an signed integer attribute data and value.
195   ///
196   void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
197                int64_t Integer);
198
199   void addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, int64_t Integer);
200
201   /// addString - Add a string attribute data and value.
202   ///
203   void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
204
205   /// addLocalString - Add a string attribute data and value.
206   ///
207   void addLocalString(DIE *Die, dwarf::Attribute Attribute,
208                       const StringRef Str);
209
210   /// addExpr - Add a Dwarf expression attribute data and value.
211   ///
212   void addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr);
213
214   /// addLabel - Add a Dwarf label attribute data and value.
215   ///
216   void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
217                 const MCSymbol *Label);
218
219   void addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label);
220
221   /// addLabelAddress - Add a dwarf label attribute data and value using
222   /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
223   ///
224   void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label);
225
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.
228   ///
229   void addOpAddress(DIEBlock *Die, const MCSymbol *Label);
230
231   /// addDelta - Add a label delta attribute data and value.
232   ///
233   void addDelta(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
234                 const MCSymbol *Hi, const MCSymbol *Lo);
235
236   /// addDIEEntry - Add a DIE attribute data and value.
237   ///
238   void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
239
240   /// addDIEEntry - Add a DIE attribute data and value.
241   ///
242   void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
243
244   /// addBlock - Add block data.
245   ///
246   void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
247
248   /// addSourceLine - Add location information to specified debug information
249   /// entry.
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);
256
257   /// addAddress - Add an address attribute to a die based on the location
258   /// provided.
259   void addAddress(DIE *Die, dwarf::Attribute Attribute,
260                   const MachineLocation &Location, bool Indirect = false);
261
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);
266
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);
270
271   /// addTemplateParams - Add template parameters in buffer.
272   void addTemplateParams(DIE &Buffer, DIArray TParams);
273
274   /// addRegisterOp - Add register operand.
275   void addRegisterOp(DIEBlock *TheDie, unsigned Reg);
276
277   /// addRegisterOffset - Add register offset.
278   void addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset);
279
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.
284   ///
285   void addComplexAddress(const DbgVariable &DV, DIE *Die,
286                          dwarf::Attribute Attribute,
287                          const MachineLocation &Location);
288
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.
295   ///
296   void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
297                             dwarf::Attribute Attribute,
298                             const MachineLocation &Location);
299
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);
304
305   /// addType - Add a new type attribute to the specified entity. This takes
306   /// and attribute parameter because DW_AT_friend attributes are also
307   /// type references.
308   void addType(DIE *Entity, DIType Ty,
309                dwarf::Attribute Attribute = dwarf::DW_AT_type);
310
311   /// getOrCreateNameSpace - Create a DIE for DINameSpace.
312   DIE *getOrCreateNameSpace(DINameSpace NS);
313
314   /// getOrCreateSubprogramDIE - Create new DIE using SP.
315   DIE *getOrCreateSubprogramDIE(DISubprogram SP);
316
317   /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
318   /// given DIType.
319   DIE *getOrCreateTypeDIE(const MDNode *N);
320
321   /// getOrCreateContextDIE - Get context owner's DIE.
322   DIE *createTypeDIE(DICompositeType Ty);
323
324   /// getOrCreateContextDIE - Get context owner's DIE.
325   DIE *getOrCreateContextDIE(DIScope Context);
326
327   /// createGlobalVariableDIE - create global variable DIE.
328   void createGlobalVariableDIE(DIGlobalVariable GV);
329
330   /// constructContainingTypeDIEs - Construct DIEs for types that contain
331   /// vtables.
332   void constructContainingTypeDIEs();
333
334   /// constructVariableDIE - Construct a DIE for the given DbgVariable.
335   DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
336
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());
341
342   /// constructTypeDIEImpl - Construct type DIE that is not a type unit
343   /// reference from a DICompositeType.
344   void constructTypeDIEImpl(DIE &Buffer, DICompositeType CTy);
345
346   /// Compute the size of a header for this unit, not including the initial
347   /// length field.
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)
352   }
353
354   /// Emit the header for this unit, not including the initial length field.
355   void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym);
356
357 private:
358   /// constructTypeDIE - Construct basic type die from DIBasicType.
359   void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
360
361   /// constructTypeDIE - Construct derived type die from DIDerivedType.
362   void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
363
364   /// constructTypeDIE - Construct type DIE from DICompositeType.
365   void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
366
367   /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
368   void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
369
370   /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
371   void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
372
373   /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
374   void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
375
376   /// constructMemberDIE - Construct member DIE from DIDerivedType.
377   void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
378
379   /// constructTemplateTypeParameterDIE - Construct new DIE for the given
380   /// DITemplateTypeParameter.
381   void constructTemplateTypeParameterDIE(DIE &Buffer,
382                                          DITemplateTypeParameter TP);
383
384   /// constructTemplateValueParameterDIE - Construct new DIE for the given
385   /// DITemplateValueParameter.
386   void constructTemplateValueParameterDIE(DIE &Buffer,
387                                           DITemplateValueParameter TVP);
388
389   /// getOrCreateStaticMemberDIE - Create new static data member DIE.
390   DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
391
392   /// Offset of the CUDie from beginning of debug info section.
393   unsigned DebugInfoOffset;
394
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;
398
399   /// getDIEEntry - Returns the debug information entry for the specified
400   /// debug variable.
401   DIEEntry *getDIEEntry(const MDNode *N) const {
402     return MDNodeToDIEEntryMap.lookup(N);
403   }
404
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));
408   }
409
410   // getIndexTyDie - Get an anonymous type for index type.
411   DIE *getIndexTyDie() { return IndexTyDie; }
412
413   // setIndexTyDie - Set D as anonymous type for index which can be reused
414   // later.
415   void setIndexTyDie(DIE *D) { IndexTyDie = D; }
416
417   /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
418   /// information entry.
419   DIEEntry *createDIEEntry(DIE *Entry);
420
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);
425   }
426
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);
430 };
431
432 } // end llvm namespace
433 #endif