Make RangeSpanList take a symbol for the beginning of the range
[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   DwarfUnits *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   Unit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A, DwarfDebug *DW,
131        DwarfUnits *DWU);
132
133 public:
134   virtual ~Unit();
135
136   // Accessors.
137   unsigned getUniqueID() const { return UniqueID; }
138   virtual uint16_t getLanguage() const = 0;
139   DICompileUnit getNode() const { return Node; }
140   DIE *getUnitDie() const { return UnitDie.get(); }
141   const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
142   const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
143
144   const StringMap<std::vector<const DIE *> > &getAccelNames() const {
145     return AccelNames;
146   }
147   const StringMap<std::vector<const DIE *> > &getAccelObjC() const {
148     return AccelObjC;
149   }
150   const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
151     return AccelNamespace;
152   }
153   const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
154   getAccelTypes() const {
155     return AccelTypes;
156   }
157
158   unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
159   void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
160
161   /// hasContent - Return true if this compile unit has something to write out.
162   bool hasContent() const { return !UnitDie->getChildren().empty(); }
163
164   /// addRangeList - Add an address range list to the list of range lists.
165   void addRangeList(RangeSpanList Ranges) { CURangeLists.push_back(Ranges); }
166
167   /// getRangeLists - Get the vector of range lists.
168   const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
169     return CURangeLists;
170   }
171   SmallVectorImpl<RangeSpanList> &getRangeLists() { return CURangeLists; }
172
173   /// getParentContextString - Get a string containing the language specific
174   /// context for a global name.
175   std::string getParentContextString(DIScope Context) const;
176
177   /// addGlobalName - Add a new global entity to the compile unit.
178   ///
179   void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
180
181   /// addAccelName - Add a new name to the name accelerator table.
182   void addAccelName(StringRef Name, const DIE *Die);
183
184   /// addAccelObjC - Add a new name to the ObjC accelerator table.
185   void addAccelObjC(StringRef Name, const DIE *Die);
186
187   /// addAccelNamespace - Add a new name to the namespace accelerator table.
188   void addAccelNamespace(StringRef Name, const DIE *Die);
189
190   /// addAccelType - Add a new type to the type accelerator table.
191   void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
192
193   /// getDIE - Returns the debug information entry map slot for the
194   /// specified debug variable. We delegate the request to DwarfDebug
195   /// when the MDNode can be part of the type system, since DIEs for
196   /// the type system can be shared across CUs and the mappings are
197   /// kept in DwarfDebug.
198   DIE *getDIE(DIDescriptor D) const;
199
200   /// getDIEBlock - Returns a fresh newly allocated DIEBlock.
201   DIEBlock *getDIEBlock() { return new (DIEValueAllocator) DIEBlock(); }
202
203   /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
204   /// when the MDNode can be part of the type system, since DIEs for
205   /// the type system can be shared across CUs and the mappings are
206   /// kept in DwarfDebug.
207   void insertDIE(DIDescriptor Desc, DIE *D);
208
209   /// addDie - Adds or interns the DIE to the compile unit.
210   ///
211   void addDie(DIE *Buffer) { UnitDie->addChild(Buffer); }
212
213   /// addFlag - Add a flag that is true to the DIE.
214   void addFlag(DIE *Die, dwarf::Attribute Attribute);
215
216   /// addUInt - Add an unsigned integer attribute data and value.
217   void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
218                uint64_t Integer);
219
220   void addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer);
221
222   /// addSInt - Add an signed integer attribute data and value.
223   void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
224                int64_t Integer);
225
226   void addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, int64_t Integer);
227
228   /// addString - Add a string attribute data and value.
229   void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
230
231   /// addLocalString - Add a string attribute data and value.
232   void addLocalString(DIE *Die, dwarf::Attribute Attribute,
233                       const StringRef Str);
234
235   /// addExpr - Add a Dwarf expression attribute data and value.
236   void addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr);
237
238   /// addLabel - Add a Dwarf label attribute data and value.
239   void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
240                 const MCSymbol *Label);
241
242   void addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label);
243
244   /// addSectionLabel - Add a Dwarf section label attribute data and value.
245   ///
246   void addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
247                        const MCSymbol *Label);
248
249   /// addSectionOffset - Add an offset into a section attribute data and value.
250   ///
251   void addSectionOffset(DIE *Die, dwarf::Attribute Attribute, uint64_t Integer);
252
253   /// addOpAddress - Add a dwarf op address data and value using the
254   /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
255   void addOpAddress(DIEBlock *Die, const MCSymbol *Label);
256
257   /// addSectionDelta - Add a label delta attribute data and value.
258   void addSectionDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
259                        const MCSymbol *Lo);
260
261   /// addDIEEntry - Add a DIE attribute data and value.
262   void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
263
264   /// addDIEEntry - Add a DIE attribute data and value.
265   void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
266
267   /// addBlock - Add block data.
268   void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
269
270   /// addSourceLine - Add location information to specified debug information
271   /// entry.
272   void addSourceLine(DIE *Die, DIVariable V);
273   void addSourceLine(DIE *Die, DIGlobalVariable G);
274   void addSourceLine(DIE *Die, DISubprogram SP);
275   void addSourceLine(DIE *Die, DIType Ty);
276   void addSourceLine(DIE *Die, DINameSpace NS);
277   void addSourceLine(DIE *Die, DIObjCProperty Ty);
278
279   /// addAddress - Add an address attribute to a die based on the location
280   /// provided.
281   void addAddress(DIE *Die, dwarf::Attribute Attribute,
282                   const MachineLocation &Location, bool Indirect = false);
283
284   /// addConstantValue - Add constant value entry in variable DIE.
285   void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
286   void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
287   void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
288
289   /// addConstantFPValue - Add constant value entry in variable DIE.
290   void addConstantFPValue(DIE *Die, const MachineOperand &MO);
291   void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
292
293   /// addTemplateParams - Add template parameters in buffer.
294   void addTemplateParams(DIE &Buffer, DIArray TParams);
295
296   /// addRegisterOp - Add register operand.
297   void addRegisterOp(DIEBlock *TheDie, unsigned Reg);
298
299   /// addRegisterOffset - Add register offset.
300   void addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset);
301
302   /// addComplexAddress - Start with the address based on the location provided,
303   /// and generate the DWARF information necessary to find the actual variable
304   /// (navigating the extra location information encoded in the type) based on
305   /// the starting location.  Add the DWARF information to the die.
306   void addComplexAddress(const DbgVariable &DV, DIE *Die,
307                          dwarf::Attribute Attribute,
308                          const MachineLocation &Location);
309
310   // FIXME: Should be reformulated in terms of addComplexAddress.
311   /// addBlockByrefAddress - Start with the address based on the location
312   /// provided, and generate the DWARF information necessary to find the
313   /// actual Block variable (navigating the Block struct) based on the
314   /// starting location.  Add the DWARF information to the die.  Obsolete,
315   /// please use addComplexAddress instead.
316   void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
317                             dwarf::Attribute Attribute,
318                             const MachineLocation &Location);
319
320   /// addVariableAddress - Add DW_AT_location attribute for a
321   /// DbgVariable based on provided MachineLocation.
322   void addVariableAddress(const DbgVariable &DV, DIE *Die,
323                           MachineLocation Location);
324
325   /// addType - Add a new type attribute to the specified entity. This takes
326   /// and attribute parameter because DW_AT_friend attributes are also
327   /// type references.
328   void addType(DIE *Entity, DIType Ty,
329                dwarf::Attribute Attribute = dwarf::DW_AT_type);
330
331   /// getOrCreateNameSpace - Create a DIE for DINameSpace.
332   DIE *getOrCreateNameSpace(DINameSpace NS);
333
334   /// getOrCreateSubprogramDIE - Create new DIE using SP.
335   DIE *getOrCreateSubprogramDIE(DISubprogram SP);
336
337   /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
338   /// given DIType.
339   DIE *getOrCreateTypeDIE(const MDNode *N);
340
341   /// getOrCreateContextDIE - Get context owner's DIE.
342   DIE *createTypeDIE(DICompositeType Ty);
343
344   /// getOrCreateContextDIE - Get context owner's DIE.
345   DIE *getOrCreateContextDIE(DIScope Context);
346
347   /// constructContainingTypeDIEs - Construct DIEs for types that contain
348   /// vtables.
349   void constructContainingTypeDIEs();
350
351   /// constructVariableDIE - Construct a DIE for the given DbgVariable.
352   DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
353
354   /// Create a DIE with the given Tag, add the DIE to its parent, and
355   /// call insertDIE if MD is not null.
356   DIE *createAndAddDIE(unsigned Tag, DIE &Parent,
357                        DIDescriptor N = DIDescriptor());
358
359   /// Compute the size of a header for this unit, not including the initial
360   /// length field.
361   unsigned getHeaderSize() const {
362     return sizeof(int16_t) + // DWARF version number
363            sizeof(int32_t) + // Offset Into Abbrev. Section
364            sizeof(int8_t);   // Pointer Size (in bytes)
365   }
366
367   /// Emit the header for this unit, not including the initial length field.
368   void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym);
369
370 protected:
371   /// getOrCreateStaticMemberDIE - Create new static data member DIE.
372   DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
373
374 private:
375   /// constructTypeDIE - Construct basic type die from DIBasicType.
376   void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
377
378   /// constructTypeDIE - Construct derived type die from DIDerivedType.
379   void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
380
381   /// constructTypeDIE - Construct type DIE from DICompositeType.
382   void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
383
384   /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
385   void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
386
387   /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
388   void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
389
390   /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
391   void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
392
393   /// constructMemberDIE - Construct member DIE from DIDerivedType.
394   void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
395
396   /// constructTemplateTypeParameterDIE - Construct new DIE for the given
397   /// DITemplateTypeParameter.
398   void constructTemplateTypeParameterDIE(DIE &Buffer,
399                                          DITemplateTypeParameter TP);
400
401   /// constructTemplateValueParameterDIE - Construct new DIE for the given
402   /// DITemplateValueParameter.
403   void constructTemplateValueParameterDIE(DIE &Buffer,
404                                           DITemplateValueParameter TVP);
405
406   /// getLowerBoundDefault - Return the default lower bound for an array. If the
407   /// DWARF version doesn't handle the language, return -1.
408   int64_t getDefaultLowerBound() const;
409
410   /// getDIEEntry - Returns the debug information entry for the specified
411   /// debug variable.
412   DIEEntry *getDIEEntry(const MDNode *N) const {
413     return MDNodeToDIEEntryMap.lookup(N);
414   }
415
416   /// insertDIEEntry - Insert debug information entry into the map.
417   void insertDIEEntry(const MDNode *N, DIEEntry *E) {
418     MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
419   }
420
421   // getIndexTyDie - Get an anonymous type for index type.
422   DIE *getIndexTyDie() { return IndexTyDie; }
423
424   // setIndexTyDie - Set D as anonymous type for index which can be reused
425   // later.
426   void setIndexTyDie(DIE *D) { IndexTyDie = D; }
427
428   /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
429   /// information entry.
430   DIEEntry *createDIEEntry(DIE *Entry);
431
432   /// resolve - Look in the DwarfDebug map for the MDNode that
433   /// corresponds to the reference.
434   template <typename T> T resolve(DIRef<T> Ref) const {
435     return DD->resolve(Ref);
436   }
437
438   /// If this is a named finished type then include it in the list of types for
439   /// the accelerator tables.
440   void updateAcceleratorTables(DIScope Context, DIType Ty, const DIE *TyDIE);
441 };
442
443 class CompileUnit : public Unit {
444 public:
445   CompileUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
446               DwarfDebug *DW, DwarfUnits *DWU);
447
448   /// createGlobalVariableDIE - create global variable DIE.
449   void createGlobalVariableDIE(DIGlobalVariable GV);
450
451   /// addLabelAddress - Add a dwarf label attribute data and value using
452   /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
453   void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label);
454
455   uint16_t getLanguage() const { return getNode().getLanguage(); }
456 };
457
458 class TypeUnit : public Unit {
459 private:
460   uint16_t Language;
461
462 public:
463   TypeUnit(unsigned UID, DIE *D, uint16_t Language, AsmPrinter *A,
464            DwarfDebug *DW, DwarfUnits *DWU);
465
466   uint16_t getLanguage() const { return Language; }
467 };
468 } // end llvm namespace
469 #endif