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