]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
Bring lld (release_39 branch, r279477) to contrib
[FreeBSD/FreeBSD.git] / contrib / llvm / 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 "DwarfDebug.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/DIE.h"
23 #include "llvm/IR/DIBuilder.h"
24 #include "llvm/IR/DebugInfo.h"
25 #include "llvm/MC/MCDwarf.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCSection.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 /// This dwarf writer support class manages information associated with a
67 /// source file.
68 class DwarfUnit {
69 protected:
70   /// MDNode for the compile unit.
71   const DICompileUnit *CUNode;
72
73   // All DIEValues are allocated through this allocator.
74   BumpPtrAllocator DIEValueAllocator;
75
76   /// Unit debug information entry.
77   DIE &UnitDie;
78
79   /// Target of Dwarf emission.
80   AsmPrinter *Asm;
81
82   // Holders for some common dwarf information.
83   DwarfDebug *DD;
84   DwarfFile *DU;
85
86   /// An anonymous type for index type.  Owned by UnitDie.
87   DIE *IndexTyDie;
88
89   /// Tracks the mapping of unit level debug information variables to debug
90   /// information entries.
91   DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
92
93   /// A list of all the DIEBlocks in use.
94   std::vector<DIEBlock *> DIEBlocks;
95
96   /// A list of all the DIELocs in use.
97   std::vector<DIELoc *> DIELocs;
98
99   /// This map is used to keep track of subprogram DIEs that need
100   /// DW_AT_containing_type attribute. This attribute points to a DIE that
101   /// corresponds to the MDNode mapped with the subprogram DIE.
102   DenseMap<DIE *, const DINode *> ContainingTypeMap;
103
104   /// The section this unit will be emitted in.
105   MCSection *Section;
106
107   DwarfUnit(dwarf::Tag, const DICompileUnit *CU, AsmPrinter *A, DwarfDebug *DW,
108             DwarfFile *DWU);
109
110   bool applySubprogramDefinitionAttributes(const DISubprogram *SP, DIE &SPDie);
111
112 public:
113   virtual ~DwarfUnit();
114
115   void initSection(MCSection *Section);
116
117   MCSection *getSection() const {
118     assert(Section);
119     return Section;
120   }
121
122   // Accessors.
123   AsmPrinter* getAsmPrinter() const { return Asm; }
124   uint16_t getLanguage() const { return CUNode->getSourceLanguage(); }
125   const DICompileUnit *getCUNode() const { return CUNode; }
126   DIE &getUnitDie() { return UnitDie; }
127
128   /// Return true if this compile unit has something to write out.
129   bool hasContent() const { return UnitDie.hasChildren(); }
130
131   /// Get string containing language specific context for a global name.
132   ///
133   /// Walks the metadata parent chain in a language specific manner (using the
134   /// compile unit language) and returns it as a string. This is done at the
135   /// metadata level because DIEs may not currently have been added to the
136   /// parent context and walking the DIEs looking for names is more expensive
137   /// than walking the metadata.
138   std::string getParentContextString(const DIScope *Context) const;
139
140   /// Add a new global name to the compile unit.
141   virtual void addGlobalName(StringRef Name, DIE &Die, const DIScope *Context) {
142   }
143
144   /// Add a new global type to the compile unit.
145   virtual void addGlobalType(const DIType *Ty, const DIE &Die,
146                              const DIScope *Context) {}
147
148   /// Returns the DIE map slot for the specified debug variable.
149   ///
150   /// We delegate the request to DwarfDebug when the MDNode can be part of the
151   /// type system, since DIEs for the type system can be shared across CUs and
152   /// the mappings are kept in DwarfDebug.
153   DIE *getDIE(const DINode *D) const;
154
155   /// Returns a fresh newly allocated DIELoc.
156   DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc; }
157
158   /// Insert DIE into the map.
159   ///
160   /// We delegate the request to DwarfDebug when the MDNode can be part of the
161   /// type system, since DIEs for the type system can be shared across CUs and
162   /// the mappings are kept in DwarfDebug.
163   void insertDIE(const DINode *Desc, DIE *D);
164
165   /// Add a flag that is true to the DIE.
166   void addFlag(DIE &Die, dwarf::Attribute Attribute);
167
168   /// Add an unsigned integer attribute data and value.
169   void addUInt(DIEValueList &Die, dwarf::Attribute Attribute,
170                Optional<dwarf::Form> Form, uint64_t Integer);
171
172   void addUInt(DIEValueList &Block, dwarf::Form Form, uint64_t Integer);
173
174   /// Add an signed integer attribute data and value.
175   void addSInt(DIEValueList &Die, dwarf::Attribute Attribute,
176                Optional<dwarf::Form> Form, int64_t Integer);
177
178   void addSInt(DIELoc &Die, Optional<dwarf::Form> Form, int64_t Integer);
179
180   /// Add a string attribute data and value.
181   ///
182   /// We always emit a reference to the string pool instead of immediate
183   /// strings so that DIEs have more predictable sizes. In the case of split
184   /// dwarf we emit an index into another table which gets us the static offset
185   /// into the string table.
186   void addString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
187
188   /// Add a Dwarf label attribute data and value.
189   DIEValueList::value_iterator addLabel(DIEValueList &Die,
190                                         dwarf::Attribute Attribute,
191                                         dwarf::Form Form,
192                                         const MCSymbol *Label);
193
194   void addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label);
195
196   /// Add an offset into a section attribute data and value.
197   void addSectionOffset(DIE &Die, dwarf::Attribute Attribute, uint64_t Integer);
198
199   /// Add a dwarf op address data and value using the form given and an
200   /// op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
201   void addOpAddress(DIELoc &Die, const MCSymbol *Label);
202
203   /// Add a label delta attribute data and value.
204   void addLabelDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
205                      const MCSymbol *Lo);
206
207   /// Add a DIE attribute data and value.
208   void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry);
209
210   /// Add a DIE attribute data and value.
211   void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIEEntry Entry);
212
213   /// Add a type's DW_AT_signature and set the  declaration flag.
214   void addDIETypeSignature(DIE &Die, uint64_t Signature);
215   /// Add an attribute containing the type signature for a unique identifier.
216   void addDIETypeSignature(DIE &Die, dwarf::Attribute Attribute,
217                            StringRef Identifier);
218
219   /// Add block data.
220   void addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Block);
221
222   /// Add block data.
223   void addBlock(DIE &Die, dwarf::Attribute Attribute, DIEBlock *Block);
224
225   /// Add location information to specified debug information entry.
226   void addSourceLine(DIE &Die, unsigned Line, StringRef File,
227                      StringRef Directory);
228   void addSourceLine(DIE &Die, const DILocalVariable *V);
229   void addSourceLine(DIE &Die, const DIGlobalVariable *G);
230   void addSourceLine(DIE &Die, const DISubprogram *SP);
231   void addSourceLine(DIE &Die, const DIType *Ty);
232   void addSourceLine(DIE &Die, const DINamespace *NS);
233   void addSourceLine(DIE &Die, const DIObjCProperty *Ty);
234
235   /// Add constant value entry in variable DIE.
236   void addConstantValue(DIE &Die, const MachineOperand &MO, const DIType *Ty);
237   void addConstantValue(DIE &Die, const ConstantInt *CI, const DIType *Ty);
238   void addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty);
239   void addConstantValue(DIE &Die, const APInt &Val, bool Unsigned);
240   void addConstantValue(DIE &Die, bool Unsigned, uint64_t Val);
241
242   /// Add constant value entry in variable DIE.
243   void addConstantFPValue(DIE &Die, const MachineOperand &MO);
244   void addConstantFPValue(DIE &Die, const ConstantFP *CFP);
245
246   /// Add a linkage name, if it isn't empty.
247   void addLinkageName(DIE &Die, StringRef LinkageName);
248
249   /// Add template parameters in buffer.
250   void addTemplateParams(DIE &Buffer, DINodeArray TParams);
251
252   /// Add register operand.
253   /// \returns false if the register does not exist, e.g., because it was never
254   /// materialized.
255   bool addRegisterOpPiece(DIELoc &TheDie, unsigned Reg,
256                           unsigned SizeInBits = 0, unsigned OffsetInBits = 0);
257
258   /// Add register offset.
259   /// \returns false if the register does not exist, e.g., because it was never
260   /// materialized.
261   bool addRegisterOffset(DIELoc &TheDie, unsigned Reg, int64_t Offset);
262
263   // FIXME: Should be reformulated in terms of addComplexAddress.
264   /// Start with the address based on the location provided, and generate the
265   /// DWARF information necessary to find the actual Block variable (navigating
266   /// the Block struct) based on the starting location.  Add the DWARF
267   /// information to the die.  Obsolete, please use addComplexAddress instead.
268   void addBlockByrefAddress(const DbgVariable &DV, DIE &Die,
269                             dwarf::Attribute Attribute,
270                             const MachineLocation &Location);
271
272   /// Add a new type attribute to the specified entity.
273   ///
274   /// This takes and attribute parameter because DW_AT_friend attributes are
275   /// also type references.
276   void addType(DIE &Entity, const DIType *Ty,
277                dwarf::Attribute Attribute = dwarf::DW_AT_type);
278
279   DIE *getOrCreateNameSpace(const DINamespace *NS);
280   DIE *getOrCreateModule(const DIModule *M);
281   DIE *getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal = false);
282
283   void applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
284                                  bool Minimal = false);
285
286   /// Find existing DIE or create new DIE for the given type.
287   DIE *getOrCreateTypeDIE(const MDNode *N);
288
289   /// Get context owner's DIE.
290   DIE *createTypeDIE(const DICompositeType *Ty);
291
292   /// Get context owner's DIE.
293   DIE *getOrCreateContextDIE(const DIScope *Context);
294
295   /// Construct DIEs for types that contain vtables.
296   void constructContainingTypeDIEs();
297
298   /// Construct function argument DIEs.
299   void constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args);
300
301   /// Create a DIE with the given Tag, add the DIE to its parent, and
302   /// call insertDIE if MD is not null.
303   DIE &createAndAddDIE(unsigned Tag, DIE &Parent, const DINode *N = nullptr);
304
305   /// Compute the size of a header for this unit, not including the initial
306   /// length field.
307   virtual unsigned getHeaderSize() const {
308     return sizeof(int16_t) + // DWARF version number
309            sizeof(int32_t) + // Offset Into Abbrev. Section
310            sizeof(int8_t);   // Pointer Size (in bytes)
311   }
312
313   /// Emit the header for this unit, not including the initial length field.
314   virtual void emitHeader(bool UseOffsets);
315
316   virtual DwarfCompileUnit &getCU() = 0;
317
318   void constructTypeDIE(DIE &Buffer, const DICompositeType *CTy);
319
320 protected:
321   /// Create new static data member DIE.
322   DIE *getOrCreateStaticMemberDIE(const DIDerivedType *DT);
323
324   /// Look up the source ID with the given directory and source file names. If
325   /// none currently exists, create a new ID and insert it in the line table.
326   virtual unsigned getOrCreateSourceID(StringRef File, StringRef Directory) = 0;
327
328   /// Look in the DwarfDebug map for the MDNode that corresponds to the
329   /// reference.
330   template <typename T> T *resolve(TypedDINodeRef<T> Ref) const {
331     return Ref.resolve();
332   }
333
334 private:
335   void constructTypeDIE(DIE &Buffer, const DIBasicType *BTy);
336   void constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy);
337   void constructTypeDIE(DIE &Buffer, const DISubroutineType *DTy);
338   void constructSubrangeDIE(DIE &Buffer, const DISubrange *SR, DIE *IndexTy);
339   void constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy);
340   void constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy);
341   void constructMemberDIE(DIE &Buffer, const DIDerivedType *DT);
342   void constructTemplateTypeParameterDIE(DIE &Buffer,
343                                          const DITemplateTypeParameter *TP);
344   void constructTemplateValueParameterDIE(DIE &Buffer,
345                                           const DITemplateValueParameter *TVP);
346
347   /// Return the default lower bound for an array.
348   ///
349   /// If the DWARF version doesn't handle the language, return -1.
350   int64_t getDefaultLowerBound() const;
351
352   /// Get an anonymous type for index type.
353   DIE *getIndexTyDie();
354
355   /// Set D as anonymous type for index which can be reused later.
356   void setIndexTyDie(DIE *D) { IndexTyDie = D; }
357
358   /// If this is a named finished type then include it in the list of types for
359   /// the accelerator tables.
360   void updateAcceleratorTables(const DIScope *Context, const DIType *Ty,
361                                const DIE &TyDIE);
362
363   virtual bool isDwoUnit() const = 0;
364 };
365
366 class DwarfTypeUnit : public DwarfUnit {
367   uint64_t TypeSignature;
368   const DIE *Ty;
369   DwarfCompileUnit &CU;
370   MCDwarfDwoLineTable *SplitLineTable;
371
372   unsigned getOrCreateSourceID(StringRef File, StringRef Directory) override;
373   bool isDwoUnit() const override;
374
375 public:
376   DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A, DwarfDebug *DW,
377                 DwarfFile *DWU, MCDwarfDwoLineTable *SplitLineTable = nullptr);
378
379   void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
380   void setType(const DIE *Ty) { this->Ty = Ty; }
381
382   /// Emit the header for this unit, not including the initial length field.
383   void emitHeader(bool UseOffsets) override;
384   unsigned getHeaderSize() const override {
385     return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
386            sizeof(uint32_t);                               // Type DIE Offset
387   }
388   DwarfCompileUnit &getCU() override { return CU; }
389 };
390 } // end llvm namespace
391 #endif