7d7b351b9b015f04cc44874066500f74f061e75e
[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 #include "llvm/MC/MCSection.h"
26
27 namespace llvm {
28
29 class MachineLocation;
30 class MachineOperand;
31 class ConstantInt;
32 class ConstantFP;
33 class DbgVariable;
34
35 // Data structure to hold a range for range lists.
36 class RangeSpan {
37 public:
38   RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {}
39   const MCSymbol *getStart() const { return Start; }
40   const MCSymbol *getEnd() const { return End; }
41
42 private:
43   const MCSymbol *Start, *End;
44 };
45
46 class RangeSpanList {
47 private:
48   // Index for locating within the debug_range section this particular span.
49   MCSymbol *RangeSym;
50   // List of ranges.
51   SmallVector<RangeSpan, 2> Ranges;
52
53 public:
54   RangeSpanList(MCSymbol *Sym) : RangeSym(Sym) {}
55   MCSymbol *getSym() const { return RangeSym; }
56   const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
57   void addRange(RangeSpan Range) { Ranges.push_back(Range); }
58 };
59
60 //===----------------------------------------------------------------------===//
61 /// Unit - This dwarf writer support class manages information associated
62 /// with a source file.
63 class DwarfUnit {
64 protected:
65   /// UniqueID - a numeric ID unique among all CUs in the module
66   unsigned UniqueID;
67
68   /// Node - MDNode for the compile unit.
69   DICompileUnit Node;
70
71   /// Unit debug information entry.
72   const OwningPtr<DIE> UnitDie;
73
74   /// Offset of the UnitDie from beginning of debug info section.
75   unsigned DebugInfoOffset;
76
77   /// Asm - Target of Dwarf emission.
78   AsmPrinter *Asm;
79
80   // Holders for some common dwarf information.
81   DwarfDebug *DD;
82   DwarfFile *DU;
83
84   /// IndexTyDie - An anonymous type for index type.  Owned by UnitDie.
85   DIE *IndexTyDie;
86
87   /// MDNodeToDieMap - Tracks the mapping of unit level debug information
88   /// variables to debug information entries.
89   DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
90
91   /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information
92   /// descriptors to debug information entries using a DIEEntry proxy.
93   DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
94
95   /// GlobalNames - A map of globally visible named entities for this unit.
96   StringMap<const DIE *> GlobalNames;
97
98   /// GlobalTypes - A map of globally visible types for this unit.
99   StringMap<const DIE *> GlobalTypes;
100
101   /// AccelNames - A map of names for the name accelerator table.
102   StringMap<std::vector<const DIE *> > AccelNames;
103
104   /// AccelObjC - A map of objc spec for the objc accelerator table.
105   StringMap<std::vector<const DIE *> > AccelObjC;
106
107   /// AccelNamespace - A map of names for the namespace accelerator table.
108   StringMap<std::vector<const DIE *> > AccelNamespace;
109
110   /// AccelTypes - A map of names for the type accelerator table.
111   StringMap<std::vector<std::pair<const DIE *, unsigned> > > AccelTypes;
112
113   /// DIEBlocks - A list of all the DIEBlocks in use.
114   std::vector<DIEBlock *> DIEBlocks;
115
116   /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
117   /// need DW_AT_containing_type attribute. This attribute points to a DIE that
118   /// corresponds to the MDNode mapped with the subprogram DIE.
119   DenseMap<DIE *, const MDNode *> ContainingTypeMap;
120
121   // List of ranges for a given compile unit.
122   SmallVector<RangeSpan, 1> CURanges;
123
124   // List of range lists for a given compile unit, separate from the ranges for
125   // the CU itself.
126   SmallVector<RangeSpanList, 1> CURangeLists;
127
128   // DIEValueAllocator - All DIEValues are allocated through this allocator.
129   BumpPtrAllocator DIEValueAllocator;
130
131   // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
132   DIEInteger *DIEIntegerOne;
133
134   /// The section this unit will be emitted in.
135   const MCSection *Section;
136
137   /// A label at the start of the non-dwo section related to this unit.
138   MCSymbol *SectionSym;
139
140   /// The start of the unit within its section.
141   MCSymbol *LabelBegin;
142
143   /// The end of the unit within its section.
144   MCSymbol *LabelEnd;
145
146   /// The label for the start of the range sets for the elements of this unit.
147   MCSymbol *LabelRange;
148
149   DwarfUnit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A,
150             DwarfDebug *DW, DwarfFile *DWU);
151
152 public:
153   virtual ~DwarfUnit();
154
155   /// Pass in the SectionSym even though we could recreate it in every compile
156   /// unit (type units will have actually distinct symbols once they're in
157   /// comdat sections).
158   void initSection(const MCSection *Section, MCSymbol *SectionSym) {
159     assert(!this->Section);
160     this->Section = Section;
161     this->SectionSym = SectionSym;
162     this->LabelBegin =
163         Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
164     this->LabelEnd =
165         Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
166     this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
167   }
168
169   const MCSection *getSection() const {
170     assert(Section);
171     return Section;
172   }
173
174   MCSymbol *getSectionSym() const {
175     assert(Section);
176     return SectionSym;
177   }
178
179   MCSymbol *getLabelBegin() const {
180     assert(Section);
181     return LabelBegin;
182   }
183
184   MCSymbol *getLabelEnd() const {
185     assert(Section);
186     return LabelEnd;
187   }
188
189   MCSymbol *getLabelRange() const {
190     assert(Section);
191     return LabelRange;
192   }
193
194   // Accessors.
195   unsigned getUniqueID() const { return UniqueID; }
196   virtual uint16_t getLanguage() const = 0;
197   DICompileUnit getNode() const { return Node; }
198   DIE *getUnitDie() const { return UnitDie.get(); }
199   const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
200   const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
201
202   const StringMap<std::vector<const DIE *> > &getAccelNames() const {
203     return AccelNames;
204   }
205   const StringMap<std::vector<const DIE *> > &getAccelObjC() const {
206     return AccelObjC;
207   }
208   const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
209     return AccelNamespace;
210   }
211   const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
212   getAccelTypes() const {
213     return AccelTypes;
214   }
215
216   unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
217   void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
218
219   /// hasContent - Return true if this compile unit has something to write out.
220   bool hasContent() const { return !UnitDie->getChildren().empty(); }
221
222   /// addRange - Add an address range to the list of ranges for this unit.
223   void addRange(RangeSpan Range) { CURanges.push_back(Range); }
224
225   /// getRanges - Get the list of ranges for this unit.
226   const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
227   SmallVectorImpl<RangeSpan> &getRanges() { return CURanges; }
228
229   /// addRangeList - Add an address range list to the list of range lists.
230   void addRangeList(RangeSpanList Ranges) { CURangeLists.push_back(Ranges); }
231
232   /// getRangeLists - Get the vector of range lists.
233   const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
234     return CURangeLists;
235   }
236   SmallVectorImpl<RangeSpanList> &getRangeLists() { return CURangeLists; }
237
238   /// getParentContextString - Get a string containing the language specific
239   /// context for a global name.
240   std::string getParentContextString(DIScope Context) const;
241
242   /// addGlobalName - Add a new global entity to the compile unit.
243   ///
244   void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
245
246   /// addAccelName - Add a new name to the name accelerator table.
247   void addAccelName(StringRef Name, const DIE *Die);
248
249   /// addAccelObjC - Add a new name to the ObjC accelerator table.
250   void addAccelObjC(StringRef Name, const DIE *Die);
251
252   /// addAccelNamespace - Add a new name to the namespace accelerator table.
253   void addAccelNamespace(StringRef Name, const DIE *Die);
254
255   /// addAccelType - Add a new type to the type accelerator table.
256   void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
257
258   /// getDIE - Returns the debug information entry map slot for the
259   /// specified debug variable. We delegate the request to DwarfDebug
260   /// when the MDNode can be part of the type system, since DIEs for
261   /// the type system can be shared across CUs and the mappings are
262   /// kept in DwarfDebug.
263   DIE *getDIE(DIDescriptor D) const;
264
265   /// getDIEBlock - Returns a fresh newly allocated DIEBlock.
266   DIEBlock *getDIEBlock() { return new (DIEValueAllocator) DIEBlock(); }
267
268   /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
269   /// when the MDNode can be part of the type system, since DIEs for
270   /// the type system can be shared across CUs and the mappings are
271   /// kept in DwarfDebug.
272   void insertDIE(DIDescriptor Desc, DIE *D);
273
274   /// addDie - Adds or interns the DIE to the compile unit.
275   ///
276   void addDie(DIE *Buffer) { UnitDie->addChild(Buffer); }
277
278   /// addFlag - Add a flag that is true to the DIE.
279   void addFlag(DIE *Die, dwarf::Attribute Attribute);
280
281   /// addUInt - Add an unsigned integer attribute data and value.
282   void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
283                uint64_t Integer);
284
285   void addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer);
286
287   /// addSInt - Add an signed integer attribute data and value.
288   void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
289                int64_t Integer);
290
291   void addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, int64_t Integer);
292
293   /// addString - Add a string attribute data and value.
294   void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
295
296   /// addLocalString - Add a string attribute data and value.
297   void addLocalString(DIE *Die, dwarf::Attribute Attribute,
298                       const StringRef Str);
299
300   /// addExpr - Add a Dwarf expression attribute data and value.
301   void addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr);
302
303   /// addLabel - Add a Dwarf label attribute data and value.
304   void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
305                 const MCSymbol *Label);
306
307   void addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label);
308
309   /// addSectionLabel - Add a Dwarf section label attribute data and value.
310   ///
311   void addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
312                        const MCSymbol *Label);
313
314   /// addSectionOffset - Add an offset into a section attribute data and value.
315   ///
316   void addSectionOffset(DIE *Die, dwarf::Attribute Attribute, uint64_t Integer);
317
318   /// addOpAddress - Add a dwarf op address data and value using the
319   /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
320   void addOpAddress(DIEBlock *Die, const MCSymbol *Label);
321
322   /// addSectionDelta - Add a label delta attribute data and value.
323   void addSectionDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
324                        const MCSymbol *Lo);
325
326   /// addDIEEntry - Add a DIE attribute data and value.
327   void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
328
329   /// addDIEEntry - Add a DIE attribute data and value.
330   void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
331
332   void addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type);
333
334   /// addBlock - Add block data.
335   void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
336
337   /// addSourceLine - Add location information to specified debug information
338   /// entry.
339   void addSourceLine(DIE *Die, DIVariable V);
340   void addSourceLine(DIE *Die, DIGlobalVariable G);
341   void addSourceLine(DIE *Die, DISubprogram SP);
342   void addSourceLine(DIE *Die, DIType Ty);
343   void addSourceLine(DIE *Die, DINameSpace NS);
344   void addSourceLine(DIE *Die, DIObjCProperty Ty);
345
346   /// addAddress - Add an address attribute to a die based on the location
347   /// provided.
348   void addAddress(DIE *Die, dwarf::Attribute Attribute,
349                   const MachineLocation &Location, bool Indirect = false);
350
351   /// addConstantValue - Add constant value entry in variable DIE.
352   void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
353   void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
354   void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
355
356   /// addConstantFPValue - Add constant value entry in variable DIE.
357   void addConstantFPValue(DIE *Die, const MachineOperand &MO);
358   void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
359
360   /// addTemplateParams - Add template parameters in buffer.
361   void addTemplateParams(DIE &Buffer, DIArray TParams);
362
363   /// addRegisterOp - Add register operand.
364   void addRegisterOp(DIEBlock *TheDie, unsigned Reg);
365
366   /// addRegisterOffset - Add register offset.
367   void addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset);
368
369   /// addComplexAddress - Start with the address based on the location provided,
370   /// and generate the DWARF information necessary to find the actual variable
371   /// (navigating the extra location information encoded in the type) based on
372   /// the starting location.  Add the DWARF information to the die.
373   void addComplexAddress(const DbgVariable &DV, DIE *Die,
374                          dwarf::Attribute Attribute,
375                          const MachineLocation &Location);
376
377   // FIXME: Should be reformulated in terms of addComplexAddress.
378   /// addBlockByrefAddress - Start with the address based on the location
379   /// provided, and generate the DWARF information necessary to find the
380   /// actual Block variable (navigating the Block struct) based on the
381   /// starting location.  Add the DWARF information to the die.  Obsolete,
382   /// please use addComplexAddress instead.
383   void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
384                             dwarf::Attribute Attribute,
385                             const MachineLocation &Location);
386
387   /// addVariableAddress - Add DW_AT_location attribute for a
388   /// DbgVariable based on provided MachineLocation.
389   void addVariableAddress(const DbgVariable &DV, DIE *Die,
390                           MachineLocation Location);
391
392   /// addType - Add a new type attribute to the specified entity. This takes
393   /// and attribute parameter because DW_AT_friend attributes are also
394   /// type references.
395   void addType(DIE *Entity, DIType Ty,
396                dwarf::Attribute Attribute = dwarf::DW_AT_type);
397
398   /// getOrCreateNameSpace - Create a DIE for DINameSpace.
399   DIE *getOrCreateNameSpace(DINameSpace NS);
400
401   /// getOrCreateSubprogramDIE - Create new DIE using SP.
402   DIE *getOrCreateSubprogramDIE(DISubprogram SP);
403
404   /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
405   /// given DIType.
406   DIE *getOrCreateTypeDIE(const MDNode *N);
407
408   /// getOrCreateContextDIE - Get context owner's DIE.
409   DIE *createTypeDIE(DICompositeType Ty);
410
411   /// getOrCreateContextDIE - Get context owner's DIE.
412   DIE *getOrCreateContextDIE(DIScope Context);
413
414   /// constructContainingTypeDIEs - Construct DIEs for types that contain
415   /// vtables.
416   void constructContainingTypeDIEs();
417
418   /// constructVariableDIE - Construct a DIE for the given DbgVariable.
419   DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
420
421   /// Create a DIE with the given Tag, add the DIE to its parent, and
422   /// call insertDIE if MD is not null.
423   DIE *createAndAddDIE(unsigned Tag, DIE &Parent,
424                        DIDescriptor N = DIDescriptor());
425
426   /// Compute the size of a header for this unit, not including the initial
427   /// length field.
428   virtual unsigned getHeaderSize() const {
429     return sizeof(int16_t) + // DWARF version number
430            sizeof(int32_t) + // Offset Into Abbrev. Section
431            sizeof(int8_t);   // Pointer Size (in bytes)
432   }
433
434   /// Emit the header for this unit, not including the initial length field.
435   virtual void emitHeader(const MCSection *ASection,
436                           const MCSymbol *ASectionSym) const;
437
438 protected:
439   /// getOrCreateStaticMemberDIE - Create new static data member DIE.
440   DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
441
442 private:
443   /// constructTypeDIE - Construct basic type die from DIBasicType.
444   void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
445
446   /// constructTypeDIE - Construct derived type die from DIDerivedType.
447   void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
448
449   /// constructTypeDIE - Construct type DIE from DICompositeType.
450   void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
451
452   /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
453   void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
454
455   /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
456   void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
457
458   /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
459   void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
460
461   /// constructMemberDIE - Construct member DIE from DIDerivedType.
462   void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
463
464   /// constructTemplateTypeParameterDIE - Construct new DIE for the given
465   /// DITemplateTypeParameter.
466   void constructTemplateTypeParameterDIE(DIE &Buffer,
467                                          DITemplateTypeParameter TP);
468
469   /// constructTemplateValueParameterDIE - Construct new DIE for the given
470   /// DITemplateValueParameter.
471   void constructTemplateValueParameterDIE(DIE &Buffer,
472                                           DITemplateValueParameter TVP);
473
474   /// getLowerBoundDefault - Return the default lower bound for an array. If the
475   /// DWARF version doesn't handle the language, return -1.
476   int64_t getDefaultLowerBound() const;
477
478   /// getDIEEntry - Returns the debug information entry for the specified
479   /// debug variable.
480   DIEEntry *getDIEEntry(const MDNode *N) const {
481     return MDNodeToDIEEntryMap.lookup(N);
482   }
483
484   /// insertDIEEntry - Insert debug information entry into the map.
485   void insertDIEEntry(const MDNode *N, DIEEntry *E) {
486     MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
487   }
488
489   // getIndexTyDie - Get an anonymous type for index type.
490   DIE *getIndexTyDie() { return IndexTyDie; }
491
492   // setIndexTyDie - Set D as anonymous type for index which can be reused
493   // later.
494   void setIndexTyDie(DIE *D) { IndexTyDie = D; }
495
496   /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
497   /// information entry.
498   DIEEntry *createDIEEntry(DIE *Entry);
499
500   /// resolve - Look in the DwarfDebug map for the MDNode that
501   /// corresponds to the reference.
502   template <typename T> T resolve(DIRef<T> Ref) const {
503     return DD->resolve(Ref);
504   }
505
506   /// If this is a named finished type then include it in the list of types for
507   /// the accelerator tables.
508   void updateAcceleratorTables(DIScope Context, DIType Ty, const DIE *TyDIE);
509 };
510
511 class DwarfCompileUnit : public DwarfUnit {
512 public:
513   DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
514                    DwarfDebug *DW, DwarfFile *DWU);
515   virtual ~DwarfCompileUnit() LLVM_OVERRIDE;
516
517   /// createGlobalVariableDIE - create global variable DIE.
518   void createGlobalVariableDIE(DIGlobalVariable GV);
519
520   /// addLabelAddress - Add a dwarf label attribute data and value using
521   /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
522   void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label);
523
524   uint16_t getLanguage() const LLVM_OVERRIDE { return getNode().getLanguage(); }
525 };
526
527 class DwarfTypeUnit : public DwarfUnit {
528 private:
529   uint16_t Language;
530   uint64_t TypeSignature;
531   const DIE *Ty;
532
533 public:
534   DwarfTypeUnit(unsigned UID, DIE *D, uint16_t Language, AsmPrinter *A,
535                 DwarfDebug *DW, DwarfFile *DWU);
536   virtual ~DwarfTypeUnit() LLVM_OVERRIDE;
537
538   void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
539   uint64_t getTypeSignature() const { return TypeSignature; }
540   void setType(const DIE *Ty) { this->Ty = Ty; }
541
542   uint16_t getLanguage() const LLVM_OVERRIDE { return Language; }
543   /// Emit the header for this unit, not including the initial length field.
544   void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym) const
545       LLVM_OVERRIDE;
546   unsigned getHeaderSize() const LLVM_OVERRIDE {
547     return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
548            sizeof(uint32_t);                               // Type DIE Offset
549   }
550   void initSection(const MCSection *Section);
551 };
552 } // end llvm namespace
553 #endif