Cleanup: Calls to getDwarfRegNum() may actually fail, if there is
[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 LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_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/StringMap.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/IR/DIBuilder.h"
24 #include "llvm/IR/DebugInfo.h"
25 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCSection.h"
27 #include "llvm/MC/MCDwarf.h"
28
29 namespace llvm {
30
31 class MachineLocation;
32 class MachineOperand;
33 class ConstantInt;
34 class ConstantFP;
35 class DbgVariable;
36 class DwarfCompileUnit;
37
38 // Data structure to hold a range for range lists.
39 class RangeSpan {
40 public:
41   RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {}
42   const MCSymbol *getStart() const { return Start; }
43   const MCSymbol *getEnd() const { return End; }
44   void setEnd(const MCSymbol *E) { End = E; }
45
46 private:
47   const MCSymbol *Start, *End;
48 };
49
50 class RangeSpanList {
51 private:
52   // Index for locating within the debug_range section this particular span.
53   MCSymbol *RangeSym;
54   // List of ranges.
55   SmallVector<RangeSpan, 2> Ranges;
56
57 public:
58   RangeSpanList(MCSymbol *Sym, SmallVector<RangeSpan, 2> Ranges)
59       : RangeSym(Sym), Ranges(std::move(Ranges)) {}
60   MCSymbol *getSym() const { return RangeSym; }
61   const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
62   void addRange(RangeSpan Range) { Ranges.push_back(Range); }
63 };
64
65 //===----------------------------------------------------------------------===//
66 /// Unit - This dwarf writer support class manages information associated
67 /// with a source file.
68 class DwarfUnit {
69 protected:
70   /// UniqueID - a numeric ID unique among all CUs in the module
71   unsigned UniqueID;
72
73   /// Node - MDNode for the compile unit.
74   DICompileUnit CUNode;
75
76   /// Unit debug information entry.
77   DIE UnitDie;
78
79   /// Offset of the UnitDie from beginning of debug info section.
80   unsigned DebugInfoOffset;
81
82   /// Asm - Target of Dwarf emission.
83   AsmPrinter *Asm;
84
85   // Holders for some common dwarf information.
86   DwarfDebug *DD;
87   DwarfFile *DU;
88
89   /// IndexTyDie - An anonymous type for index type.  Owned by UnitDie.
90   DIE *IndexTyDie;
91
92   /// MDNodeToDieMap - Tracks the mapping of unit level debug information
93   /// variables to debug information entries.
94   DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
95
96   /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information
97   /// descriptors to debug information entries using a DIEEntry proxy.
98   DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
99
100   /// DIEBlocks - A list of all the DIEBlocks in use.
101   std::vector<DIEBlock *> DIEBlocks;
102   
103   /// DIELocs - A list of all the DIELocs in use.
104   std::vector<DIELoc *> DIELocs;
105
106   /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
107   /// need DW_AT_containing_type attribute. This attribute points to a DIE that
108   /// corresponds to the MDNode mapped with the subprogram DIE.
109   DenseMap<DIE *, const MDNode *> ContainingTypeMap;
110
111   // DIEValueAllocator - All DIEValues are allocated through this allocator.
112   BumpPtrAllocator DIEValueAllocator;
113
114   // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
115   DIEInteger *DIEIntegerOne;
116
117   /// The section this unit will be emitted in.
118   const MCSection *Section;
119
120   DwarfUnit(unsigned UID, dwarf::Tag, DICompileUnit CU, AsmPrinter *A,
121             DwarfDebug *DW, DwarfFile *DWU);
122
123   void initSection(const MCSection *Section);
124
125   /// Add a string attribute data and value.
126   void addLocalString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
127
128   void addIndexedString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
129
130   bool applySubprogramDefinitionAttributes(DISubprogram SP, DIE &SPDie);
131
132 public:
133   virtual ~DwarfUnit();
134
135   const MCSection *getSection() const {
136     assert(Section);
137     return Section;
138   }
139
140   // Accessors.
141   unsigned getUniqueID() const { return UniqueID; }
142   uint16_t getLanguage() const { return CUNode.getLanguage(); }
143   DICompileUnit getCUNode() const { return CUNode; }
144   DIE &getUnitDie() { return UnitDie; }
145
146   unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
147   void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
148
149   /// hasContent - Return true if this compile unit has something to write out.
150   bool hasContent() const { return !UnitDie.getChildren().empty(); }
151
152   /// getParentContextString - Get a string containing the language specific
153   /// context for a global name.
154   std::string getParentContextString(DIScope Context) const;
155
156   /// Add a new global name to the compile unit.
157   virtual void addGlobalName(StringRef Name, DIE &Die, DIScope Context) {}
158
159   /// Add a new global type to the compile unit.
160   virtual void addGlobalType(DIType Ty, const DIE &Die, DIScope Context) {}
161
162   /// addAccelNamespace - Add a new name to the namespace accelerator table.
163   void addAccelNamespace(StringRef Name, const DIE &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   /// getDIELoc - Returns a fresh newly allocated DIELoc.
173   DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc(); }
174
175   /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
176   /// when the MDNode can be part of the type system, since DIEs for
177   /// the type system can be shared across CUs and the mappings are
178   /// kept in DwarfDebug.
179   void insertDIE(DIDescriptor Desc, DIE *D);
180
181   /// addFlag - Add a flag that is true to the DIE.
182   void addFlag(DIE &Die, dwarf::Attribute Attribute);
183
184   /// addUInt - Add an unsigned integer attribute data and value.
185   void addUInt(DIE &Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
186                uint64_t Integer);
187
188   void addUInt(DIE &Block, dwarf::Form Form, uint64_t Integer);
189
190   /// addSInt - Add an signed integer attribute data and value.
191   void addSInt(DIE &Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
192                int64_t Integer);
193
194   void addSInt(DIELoc &Die, Optional<dwarf::Form> Form, int64_t Integer);
195
196   /// addString - Add a string attribute data and value.
197   void addString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
198
199   /// addLabel - Add a Dwarf label attribute data and value.
200   void addLabel(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form,
201                 const MCSymbol *Label);
202
203   void addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label);
204
205   /// addSectionOffset - Add an offset into a section attribute data and value.
206   ///
207   void addSectionOffset(DIE &Die, dwarf::Attribute Attribute, uint64_t Integer);
208
209   /// addOpAddress - Add a dwarf op address data and value using the
210   /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
211   void addOpAddress(DIELoc &Die, const MCSymbol *Label);
212
213   /// addLabelDelta - Add a label delta attribute data and value.
214   void addLabelDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
215                      const MCSymbol *Lo);
216
217   /// addDIEEntry - Add a DIE attribute data and value.
218   void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry);
219
220   /// addDIEEntry - Add a DIE attribute data and value.
221   void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIEEntry *Entry);
222
223   void addDIETypeSignature(DIE &Die, const DwarfTypeUnit &Type);
224
225   /// addBlock - Add block data.
226   void addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Block);
227
228   /// addBlock - Add block data.
229   void addBlock(DIE &Die, dwarf::Attribute Attribute, DIEBlock *Block);
230
231   /// addSourceLine - Add location information to specified debug information
232   /// entry.
233   void addSourceLine(DIE &Die, unsigned Line, StringRef File,
234                      StringRef Directory);
235   void addSourceLine(DIE &Die, DIVariable V);
236   void addSourceLine(DIE &Die, DIGlobalVariable G);
237   void addSourceLine(DIE &Die, DISubprogram SP);
238   void addSourceLine(DIE &Die, DIType Ty);
239   void addSourceLine(DIE &Die, DINameSpace NS);
240   void addSourceLine(DIE &Die, DIObjCProperty Ty);
241
242   /// addConstantValue - Add constant value entry in variable DIE.
243   void addConstantValue(DIE &Die, const MachineOperand &MO, DIType Ty);
244   void addConstantValue(DIE &Die, const ConstantInt *CI, DIType Ty);
245   void addConstantValue(DIE &Die, const APInt &Val, DIType Ty);
246   void addConstantValue(DIE &Die, const APInt &Val, bool Unsigned);
247   void addConstantValue(DIE &Die, bool Unsigned, uint64_t Val);
248
249   /// addConstantFPValue - Add constant value entry in variable DIE.
250   void addConstantFPValue(DIE &Die, const MachineOperand &MO);
251   void addConstantFPValue(DIE &Die, const ConstantFP *CFP);
252
253   /// addTemplateParams - Add template parameters in buffer.
254   void addTemplateParams(DIE &Buffer, DIArray TParams);
255
256   /// \brief Add register operand.
257   /// \returns false if the register does not exist, e.g., because it was never
258   /// materialized.
259   bool addRegisterOpPiece(DIELoc &TheDie, unsigned Reg,
260                           unsigned SizeInBits = 0, unsigned OffsetInBits = 0);
261
262   /// \brief Add register offset.
263   /// \returns false if the register does not exist, e.g., because it was never
264   /// materialized.
265   bool addRegisterOffset(DIELoc &TheDie, unsigned Reg, int64_t Offset);
266
267   // FIXME: Should be reformulated in terms of addComplexAddress.
268   /// addBlockByrefAddress - Start with the address based on the location
269   /// provided, and generate the DWARF information necessary to find the
270   /// actual Block variable (navigating the Block struct) based on the
271   /// starting location.  Add the DWARF information to the die.  Obsolete,
272   /// please use addComplexAddress instead.
273   void addBlockByrefAddress(const DbgVariable &DV, DIE &Die,
274                             dwarf::Attribute Attribute,
275                             const MachineLocation &Location);
276
277   /// addType - Add a new type attribute to the specified entity. This takes
278   /// and attribute parameter because DW_AT_friend attributes are also
279   /// type references.
280   void addType(DIE &Entity, DIType Ty,
281                dwarf::Attribute Attribute = dwarf::DW_AT_type);
282
283   /// getOrCreateNameSpace - Create a DIE for DINameSpace.
284   DIE *getOrCreateNameSpace(DINameSpace NS);
285
286   /// getOrCreateSubprogramDIE - Create new DIE using SP.
287   DIE *getOrCreateSubprogramDIE(DISubprogram SP, bool Minimal = false);
288
289   void applySubprogramAttributes(DISubprogram SP, DIE &SPDie,
290                                  bool Minimal = false);
291
292   /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
293   /// given DIType.
294   DIE *getOrCreateTypeDIE(const MDNode *N);
295
296   /// getOrCreateContextDIE - Get context owner's DIE.
297   DIE *createTypeDIE(DICompositeType Ty);
298
299   /// getOrCreateContextDIE - Get context owner's DIE.
300   DIE *getOrCreateContextDIE(DIScope Context);
301
302   /// constructContainingTypeDIEs - Construct DIEs for types that contain
303   /// vtables.
304   void constructContainingTypeDIEs();
305
306   /// constructSubprogramArguments - Construct function argument DIEs.
307   void constructSubprogramArguments(DIE &Buffer, DITypeArray Args);
308
309   /// Create a DIE with the given Tag, add the DIE to its parent, and
310   /// call insertDIE if MD is not null.
311   DIE &createAndAddDIE(unsigned Tag, DIE &Parent,
312                        DIDescriptor N = DIDescriptor());
313
314   /// Compute the size of a header for this unit, not including the initial
315   /// length field.
316   virtual unsigned getHeaderSize() const {
317     return sizeof(int16_t) + // DWARF version number
318            sizeof(int32_t) + // Offset Into Abbrev. Section
319            sizeof(int8_t);   // Pointer Size (in bytes)
320   }
321
322   /// Emit the header for this unit, not including the initial length field.
323   virtual void emitHeader(const MCSymbol *ASectionSym) const;
324
325   virtual DwarfCompileUnit &getCU() = 0;
326
327   /// constructTypeDIE - Construct type DIE from DICompositeType.
328   void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
329
330 protected:
331   /// getOrCreateStaticMemberDIE - Create new static data member DIE.
332   DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
333
334   /// Look up the source ID with the given directory and source file names. If
335   /// none currently exists, create a new ID and insert it in the line table.
336   virtual unsigned getOrCreateSourceID(StringRef File, StringRef Directory) = 0;
337
338   /// resolve - Look in the DwarfDebug map for the MDNode that
339   /// corresponds to the reference.
340   template <typename T> T resolve(DIRef<T> Ref) const {
341     return DD->resolve(Ref);
342   }
343
344 private:
345   /// constructTypeDIE - Construct basic type die from DIBasicType.
346   void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
347
348   /// constructTypeDIE - Construct derived type die from DIDerivedType.
349   void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
350
351   /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
352   void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
353
354   /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
355   void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
356
357   /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
358   void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
359
360   /// constructMemberDIE - Construct member DIE from DIDerivedType.
361   void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
362
363   /// constructTemplateTypeParameterDIE - Construct new DIE for the given
364   /// DITemplateTypeParameter.
365   void constructTemplateTypeParameterDIE(DIE &Buffer,
366                                          DITemplateTypeParameter TP);
367
368   /// constructTemplateValueParameterDIE - Construct new DIE for the given
369   /// DITemplateValueParameter.
370   void constructTemplateValueParameterDIE(DIE &Buffer,
371                                           DITemplateValueParameter TVP);
372
373   /// getLowerBoundDefault - Return the default lower bound for an array. If the
374   /// DWARF version doesn't handle the language, return -1.
375   int64_t getDefaultLowerBound() const;
376
377   /// getDIEEntry - Returns the debug information entry for the specified
378   /// debug variable.
379   DIEEntry *getDIEEntry(const MDNode *N) const {
380     return MDNodeToDIEEntryMap.lookup(N);
381   }
382
383   /// insertDIEEntry - Insert debug information entry into the map.
384   void insertDIEEntry(const MDNode *N, DIEEntry *E) {
385     MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
386   }
387
388   // getIndexTyDie - Get an anonymous type for index type.
389   DIE *getIndexTyDie();
390
391   // setIndexTyDie - Set D as anonymous type for index which can be reused
392   // later.
393   void setIndexTyDie(DIE *D) { IndexTyDie = D; }
394
395   /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
396   /// information entry.
397   DIEEntry *createDIEEntry(DIE &Entry);
398
399   /// If this is a named finished type then include it in the list of types for
400   /// the accelerator tables.
401   void updateAcceleratorTables(DIScope Context, DIType Ty, const DIE &TyDIE);
402
403   virtual bool isDwoUnit() const = 0;
404 };
405
406 class DwarfTypeUnit : public DwarfUnit {
407   uint64_t TypeSignature;
408   const DIE *Ty;
409   DwarfCompileUnit &CU;
410   MCDwarfDwoLineTable *SplitLineTable;
411
412   unsigned getOrCreateSourceID(StringRef File, StringRef Directory) override;
413   bool isDwoUnit() const override;
414
415 public:
416   DwarfTypeUnit(unsigned UID, DwarfCompileUnit &CU, AsmPrinter *A,
417                 DwarfDebug *DW, DwarfFile *DWU,
418                 MCDwarfDwoLineTable *SplitLineTable = nullptr);
419
420   void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
421   uint64_t getTypeSignature() const { return TypeSignature; }
422   void setType(const DIE *Ty) { this->Ty = Ty; }
423
424   /// Emit the header for this unit, not including the initial length field.
425   void emitHeader(const MCSymbol *ASectionSym) const override;
426   unsigned getHeaderSize() const override {
427     return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
428            sizeof(uint32_t);                               // Type DIE Offset
429   }
430   using DwarfUnit::initSection;
431   DwarfCompileUnit &getCU() override { return CU; }
432 };
433 } // end llvm namespace
434 #endif