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