]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/CodeGen/DIE.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r301441, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / CodeGen / DIE.h
1 //===--- lib/CodeGen/DIE.h - DWARF Info Entries -----------------*- 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 // Data structures for DWARF info entries.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
16
17 #include "llvm/ADT/FoldingSet.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/ADT/PointerUnion.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/iterator.h"
23 #include "llvm/ADT/iterator_range.h"
24 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
25 #include "llvm/Support/AlignOf.h"
26 #include "llvm/Support/Allocator.h"
27 #include "llvm/Support/Dwarf.h"
28 #include <cassert>
29 #include <cstddef>
30 #include <cstdint>
31 #include <iterator>
32 #include <new>
33 #include <type_traits>
34 #include <vector>
35
36 namespace llvm {
37
38 class AsmPrinter;
39 class DIE;
40 class DIEUnit;
41 class MCExpr;
42 class MCSection;
43 class MCSymbol;
44 class raw_ostream;
45
46 //===--------------------------------------------------------------------===//
47 /// Dwarf abbreviation data, describes one attribute of a Dwarf abbreviation.
48 class DIEAbbrevData {
49   /// Dwarf attribute code.
50   dwarf::Attribute Attribute;
51
52   /// Dwarf form code.
53   dwarf::Form Form;
54
55   /// Dwarf attribute value for DW_FORM_implicit_const
56   int64_t Value;
57
58 public:
59   DIEAbbrevData(dwarf::Attribute A, dwarf::Form F)
60       : Attribute(A), Form(F), Value(0) {}
61   DIEAbbrevData(dwarf::Attribute A, int64_t V)
62       : Attribute(A), Form(dwarf::DW_FORM_implicit_const), Value(V) {}
63
64   /// Accessors.
65   /// @{
66   dwarf::Attribute getAttribute() const { return Attribute; }
67   dwarf::Form getForm() const { return Form; }
68   int64_t getValue() const { return Value; }
69   /// @}
70
71   /// Used to gather unique data for the abbreviation folding set.
72   void Profile(FoldingSetNodeID &ID) const;
73 };
74
75 //===--------------------------------------------------------------------===//
76 /// Dwarf abbreviation, describes the organization of a debug information
77 /// object.
78 class DIEAbbrev : public FoldingSetNode {
79   /// Unique number for node.
80   unsigned Number;
81
82   /// Dwarf tag code.
83   dwarf::Tag Tag;
84
85   /// Whether or not this node has children.
86   ///
87   /// This cheats a bit in all of the uses since the values in the standard
88   /// are 0 and 1 for no children and children respectively.
89   bool Children;
90
91   /// Raw data bytes for abbreviation.
92   SmallVector<DIEAbbrevData, 12> Data;
93
94 public:
95   DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C) {}
96
97   /// Accessors.
98   /// @{
99   dwarf::Tag getTag() const { return Tag; }
100   unsigned getNumber() const { return Number; }
101   bool hasChildren() const { return Children; }
102   const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
103   void setChildrenFlag(bool hasChild) { Children = hasChild; }
104   void setNumber(unsigned N) { Number = N; }
105   /// @}
106
107   /// Adds another set of attribute information to the abbreviation.
108   void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
109     Data.push_back(DIEAbbrevData(Attribute, Form));
110   }
111
112   /// Adds attribute with DW_FORM_implicit_const value
113   void AddImplicitConstAttribute(dwarf::Attribute Attribute, int64_t Value) {
114     Data.push_back(DIEAbbrevData(Attribute, Value));
115   }
116
117   /// Used to gather unique data for the abbreviation folding set.
118   void Profile(FoldingSetNodeID &ID) const;
119
120   /// Print the abbreviation using the specified asm printer.
121   void Emit(const AsmPrinter *AP) const;
122
123   void print(raw_ostream &O);
124   void dump();
125 };
126
127 //===--------------------------------------------------------------------===//
128 /// Helps unique DIEAbbrev objects and assigns abbreviation numbers.
129 ///
130 /// This class will unique the DIE abbreviations for a llvm::DIE object and
131 /// assign a unique abbreviation number to each unique DIEAbbrev object it
132 /// finds. The resulting collection of DIEAbbrev objects can then be emitted
133 /// into the .debug_abbrev section.
134 class DIEAbbrevSet {
135   /// The bump allocator to use when creating DIEAbbrev objects in the uniqued
136   /// storage container.
137   BumpPtrAllocator &Alloc;
138   /// \brief FoldingSet that uniques the abbreviations.
139   llvm::FoldingSet<DIEAbbrev> AbbreviationsSet;
140   /// A list of all the unique abbreviations in use.
141   std::vector<DIEAbbrev *> Abbreviations;
142
143 public:
144   DIEAbbrevSet(BumpPtrAllocator &A) : Alloc(A) {}
145   ~DIEAbbrevSet();
146   /// Generate the abbreviation declaration for a DIE and return a pointer to
147   /// the generated abbreviation.
148   ///
149   /// \param Die the debug info entry to generate the abbreviation for.
150   /// \returns A reference to the uniqued abbreviation declaration that is
151   /// owned by this class.
152   DIEAbbrev &uniqueAbbreviation(DIE &Die);
153
154   /// Print all abbreviations using the specified asm printer.
155   void Emit(const AsmPrinter *AP, MCSection *Section) const;
156 };
157
158 //===--------------------------------------------------------------------===//
159 /// An integer value DIE.
160 ///
161 class DIEInteger {
162   uint64_t Integer;
163
164 public:
165   explicit DIEInteger(uint64_t I) : Integer(I) {}
166
167   /// Choose the best form for integer.
168   static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
169     if (IsSigned) {
170       const int64_t SignedInt = Int;
171       if ((char)Int == SignedInt)
172         return dwarf::DW_FORM_data1;
173       if ((short)Int == SignedInt)
174         return dwarf::DW_FORM_data2;
175       if ((int)Int == SignedInt)
176         return dwarf::DW_FORM_data4;
177     } else {
178       if ((unsigned char)Int == Int)
179         return dwarf::DW_FORM_data1;
180       if ((unsigned short)Int == Int)
181         return dwarf::DW_FORM_data2;
182       if ((unsigned int)Int == Int)
183         return dwarf::DW_FORM_data4;
184     }
185     return dwarf::DW_FORM_data8;
186   }
187
188   uint64_t getValue() const { return Integer; }
189   void setValue(uint64_t Val) { Integer = Val; }
190
191   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
192   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
193
194   void print(raw_ostream &O) const;
195 };
196
197 //===--------------------------------------------------------------------===//
198 /// An expression DIE.
199 class DIEExpr {
200   const MCExpr *Expr;
201
202 public:
203   explicit DIEExpr(const MCExpr *E) : Expr(E) {}
204
205   /// Get MCExpr.
206   const MCExpr *getValue() const { return Expr; }
207
208   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
209   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
210
211   void print(raw_ostream &O) const;
212 };
213
214 //===--------------------------------------------------------------------===//
215 /// A label DIE.
216 class DIELabel {
217   const MCSymbol *Label;
218
219 public:
220   explicit DIELabel(const MCSymbol *L) : Label(L) {}
221
222   /// Get MCSymbol.
223   const MCSymbol *getValue() const { return Label; }
224
225   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
226   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
227
228   void print(raw_ostream &O) const;
229 };
230
231 //===--------------------------------------------------------------------===//
232 /// A simple label difference DIE.
233 ///
234 class DIEDelta {
235   const MCSymbol *LabelHi;
236   const MCSymbol *LabelLo;
237
238 public:
239   DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo) : LabelHi(Hi), LabelLo(Lo) {}
240
241   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
242   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
243
244   void print(raw_ostream &O) const;
245 };
246
247 //===--------------------------------------------------------------------===//
248 /// A container for string pool string values.
249 ///
250 /// This class is used with the DW_FORM_strp and DW_FORM_GNU_str_index forms.
251 class DIEString {
252   DwarfStringPoolEntryRef S;
253
254 public:
255   DIEString(DwarfStringPoolEntryRef S) : S(S) {}
256
257   /// Grab the string out of the object.
258   StringRef getString() const { return S.getString(); }
259
260   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
261   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
262
263   void print(raw_ostream &O) const;
264 };
265
266 //===--------------------------------------------------------------------===//
267 /// A container for inline string values.
268 ///
269 /// This class is used with the DW_FORM_string form.
270 class DIEInlineString {
271   StringRef S;
272
273 public:
274   template <typename Allocator>
275   explicit DIEInlineString(StringRef Str, Allocator &A) : S(Str.copy(A)) {}
276
277   ~DIEInlineString() = default;
278
279   /// Grab the string out of the object.
280   StringRef getString() const { return S; }
281
282   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
283   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
284
285   void print(raw_ostream &O) const;
286 };
287
288 //===--------------------------------------------------------------------===//
289 /// A pointer to another debug information entry.  An instance of this class can
290 /// also be used as a proxy for a debug information entry not yet defined
291 /// (ie. types.)
292 class DIE;
293 class DIEEntry {
294   DIE *Entry;
295
296   DIEEntry() = delete;
297
298 public:
299   explicit DIEEntry(DIE &E) : Entry(&E) {}
300
301   DIE &getEntry() const { return *Entry; }
302
303   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
304   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
305
306   void print(raw_ostream &O) const;
307 };
308
309 //===--------------------------------------------------------------------===//
310 /// Represents a pointer to a location list in the debug_loc
311 /// section.
312 class DIELocList {
313   /// Index into the .debug_loc vector.
314   size_t Index;
315
316 public:
317   DIELocList(size_t I) : Index(I) {}
318
319   /// Grab the current index out.
320   size_t getValue() const { return Index; }
321
322   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
323   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
324
325   void print(raw_ostream &O) const;
326 };
327
328 //===--------------------------------------------------------------------===//
329 /// A debug information entry value. Some of these roughly correlate
330 /// to DWARF attribute classes.
331 class DIEBlock;
332 class DIELoc;
333 class DIEValue {
334 public:
335   enum Type {
336     isNone,
337 #define HANDLE_DIEVALUE(T) is##T,
338 #include "llvm/CodeGen/DIEValue.def"
339   };
340
341 private:
342   /// Type of data stored in the value.
343   Type Ty = isNone;
344   dwarf::Attribute Attribute = (dwarf::Attribute)0;
345   dwarf::Form Form = (dwarf::Form)0;
346
347   /// Storage for the value.
348   ///
349   /// All values that aren't standard layout (or are larger than 8 bytes)
350   /// should be stored by reference instead of by value.
351   typedef AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
352                                 DIEDelta *, DIEEntry, DIEBlock *, DIELoc *,
353                                 DIELocList>
354       ValTy;
355   static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
356                     sizeof(ValTy) <= sizeof(void *),
357                 "Expected all large types to be stored via pointer");
358
359   /// Underlying stored value.
360   ValTy Val;
361
362   template <class T> void construct(T V) {
363     static_assert(std::is_standard_layout<T>::value ||
364                       std::is_pointer<T>::value,
365                   "Expected standard layout or pointer");
366     new (reinterpret_cast<void *>(Val.buffer)) T(V);
367   }
368
369   template <class T> T *get() { return reinterpret_cast<T *>(Val.buffer); }
370   template <class T> const T *get() const {
371     return reinterpret_cast<const T *>(Val.buffer);
372   }
373   template <class T> void destruct() { get<T>()->~T(); }
374
375   /// Destroy the underlying value.
376   ///
377   /// This should get optimized down to a no-op.  We could skip it if we could
378   /// add a static assert on \a std::is_trivially_copyable(), but we currently
379   /// support versions of GCC that don't understand that.
380   void destroyVal() {
381     switch (Ty) {
382     case isNone:
383       return;
384 #define HANDLE_DIEVALUE_SMALL(T)                                               \
385   case is##T:                                                                  \
386     destruct<DIE##T>();
387     return;
388 #define HANDLE_DIEVALUE_LARGE(T)                                               \
389   case is##T:                                                                  \
390     destruct<const DIE##T *>();
391     return;
392 #include "llvm/CodeGen/DIEValue.def"
393     }
394   }
395
396   /// Copy the underlying value.
397   ///
398   /// This should get optimized down to a simple copy.  We need to actually
399   /// construct the value, rather than calling memcpy, to satisfy strict
400   /// aliasing rules.
401   void copyVal(const DIEValue &X) {
402     switch (Ty) {
403     case isNone:
404       return;
405 #define HANDLE_DIEVALUE_SMALL(T)                                               \
406   case is##T:                                                                  \
407     construct<DIE##T>(*X.get<DIE##T>());                                       \
408     return;
409 #define HANDLE_DIEVALUE_LARGE(T)                                               \
410   case is##T:                                                                  \
411     construct<const DIE##T *>(*X.get<const DIE##T *>());                       \
412     return;
413 #include "llvm/CodeGen/DIEValue.def"
414     }
415   }
416
417 public:
418   DIEValue() = default;
419
420   DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
421     copyVal(X);
422   }
423
424   DIEValue &operator=(const DIEValue &X) {
425     destroyVal();
426     Ty = X.Ty;
427     Attribute = X.Attribute;
428     Form = X.Form;
429     copyVal(X);
430     return *this;
431   }
432
433   ~DIEValue() { destroyVal(); }
434
435 #define HANDLE_DIEVALUE_SMALL(T)                                               \
436   DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T &V)      \
437       : Ty(is##T), Attribute(Attribute), Form(Form) {                          \
438     construct<DIE##T>(V);                                                      \
439   }
440 #define HANDLE_DIEVALUE_LARGE(T)                                               \
441   DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T *V)      \
442       : Ty(is##T), Attribute(Attribute), Form(Form) {                          \
443     assert(V && "Expected valid value");                                       \
444     construct<const DIE##T *>(V);                                              \
445   }
446 #include "llvm/CodeGen/DIEValue.def"
447
448   /// Accessors.
449   /// @{
450   Type getType() const { return Ty; }
451   dwarf::Attribute getAttribute() const { return Attribute; }
452   dwarf::Form getForm() const { return Form; }
453   explicit operator bool() const { return Ty; }
454   /// @}
455
456 #define HANDLE_DIEVALUE_SMALL(T)                                               \
457   const DIE##T &getDIE##T() const {                                            \
458     assert(getType() == is##T && "Expected " #T);                              \
459     return *get<DIE##T>();                                                     \
460   }
461 #define HANDLE_DIEVALUE_LARGE(T)                                               \
462   const DIE##T &getDIE##T() const {                                            \
463     assert(getType() == is##T && "Expected " #T);                              \
464     return **get<const DIE##T *>();                                            \
465   }
466 #include "llvm/CodeGen/DIEValue.def"
467
468   /// Emit value via the Dwarf writer.
469   void EmitValue(const AsmPrinter *AP) const;
470
471   /// Return the size of a value in bytes.
472   unsigned SizeOf(const AsmPrinter *AP) const;
473
474   void print(raw_ostream &O) const;
475   void dump() const;
476 };
477
478 struct IntrusiveBackListNode {
479   PointerIntPair<IntrusiveBackListNode *, 1> Next;
480
481   IntrusiveBackListNode() : Next(this, true) {}
482
483   IntrusiveBackListNode *getNext() const {
484     return Next.getInt() ? nullptr : Next.getPointer();
485   }
486 };
487
488 struct IntrusiveBackListBase {
489   typedef IntrusiveBackListNode Node;
490   Node *Last = nullptr;
491
492   bool empty() const { return !Last; }
493   void push_back(Node &N) {
494     assert(N.Next.getPointer() == &N && "Expected unlinked node");
495     assert(N.Next.getInt() == true && "Expected unlinked node");
496
497     if (Last) {
498       N.Next = Last->Next;
499       Last->Next.setPointerAndInt(&N, false);
500     }
501     Last = &N;
502   }
503 };
504
505 template <class T> class IntrusiveBackList : IntrusiveBackListBase {
506 public:
507   using IntrusiveBackListBase::empty;
508   void push_back(T &N) { IntrusiveBackListBase::push_back(N); }
509   T &back() { return *static_cast<T *>(Last); }
510   const T &back() const { return *static_cast<T *>(Last); }
511
512   class const_iterator;
513   class iterator
514       : public iterator_facade_base<iterator, std::forward_iterator_tag, T> {
515     friend class const_iterator;
516     Node *N = nullptr;
517
518   public:
519     iterator() = default;
520     explicit iterator(T *N) : N(N) {}
521
522     iterator &operator++() {
523       N = N->getNext();
524       return *this;
525     }
526
527     explicit operator bool() const { return N; }
528     T &operator*() const { return *static_cast<T *>(N); }
529
530     bool operator==(const iterator &X) const { return N == X.N; }
531     bool operator!=(const iterator &X) const { return N != X.N; }
532   };
533
534   class const_iterator
535       : public iterator_facade_base<const_iterator, std::forward_iterator_tag,
536                                     const T> {
537     const Node *N = nullptr;
538
539   public:
540     const_iterator() = default;
541     // Placate MSVC by explicitly scoping 'iterator'.
542     const_iterator(typename IntrusiveBackList<T>::iterator X) : N(X.N) {}
543     explicit const_iterator(const T *N) : N(N) {}
544
545     const_iterator &operator++() {
546       N = N->getNext();
547       return *this;
548     }
549
550     explicit operator bool() const { return N; }
551     const T &operator*() const { return *static_cast<const T *>(N); }
552
553     bool operator==(const const_iterator &X) const { return N == X.N; }
554     bool operator!=(const const_iterator &X) const { return N != X.N; }
555   };
556
557   iterator begin() {
558     return Last ? iterator(static_cast<T *>(Last->Next.getPointer())) : end();
559   }
560   const_iterator begin() const {
561     return const_cast<IntrusiveBackList *>(this)->begin();
562   }
563   iterator end() { return iterator(); }
564   const_iterator end() const { return const_iterator(); }
565
566   static iterator toIterator(T &N) { return iterator(&N); }
567   static const_iterator toIterator(const T &N) { return const_iterator(&N); }
568 };
569
570 /// A list of DIE values.
571 ///
572 /// This is a singly-linked list, but instead of reversing the order of
573 /// insertion, we keep a pointer to the back of the list so we can push in
574 /// order.
575 ///
576 /// There are two main reasons to choose a linked list over a customized
577 /// vector-like data structure.
578 ///
579 ///  1. For teardown efficiency, we want DIEs to be BumpPtrAllocated.  Using a
580 ///     linked list here makes this way easier to accomplish.
581 ///  2. Carrying an extra pointer per \a DIEValue isn't expensive.  45% of DIEs
582 ///     have 2 or fewer values, and 90% have 5 or fewer.  A vector would be
583 ///     over-allocated by 50% on average anyway, the same cost as the
584 ///     linked-list node.
585 class DIEValueList {
586   struct Node : IntrusiveBackListNode {
587     DIEValue V;
588     explicit Node(DIEValue V) : V(V) {}
589   };
590
591   typedef IntrusiveBackList<Node> ListTy;
592   ListTy List;
593
594 public:
595   class const_value_iterator;
596   class value_iterator
597       : public iterator_adaptor_base<value_iterator, ListTy::iterator,
598                                      std::forward_iterator_tag, DIEValue> {
599     friend class const_value_iterator;
600     typedef iterator_adaptor_base<value_iterator, ListTy::iterator,
601                                   std::forward_iterator_tag,
602                                   DIEValue> iterator_adaptor;
603
604   public:
605     value_iterator() = default;
606     explicit value_iterator(ListTy::iterator X) : iterator_adaptor(X) {}
607
608     explicit operator bool() const { return bool(wrapped()); }
609     DIEValue &operator*() const { return wrapped()->V; }
610   };
611
612   class const_value_iterator : public iterator_adaptor_base<
613                                    const_value_iterator, ListTy::const_iterator,
614                                    std::forward_iterator_tag, const DIEValue> {
615     typedef iterator_adaptor_base<const_value_iterator, ListTy::const_iterator,
616                                   std::forward_iterator_tag,
617                                   const DIEValue> iterator_adaptor;
618
619   public:
620     const_value_iterator() = default;
621     const_value_iterator(DIEValueList::value_iterator X)
622         : iterator_adaptor(X.wrapped()) {}
623     explicit const_value_iterator(ListTy::const_iterator X)
624         : iterator_adaptor(X) {}
625
626     explicit operator bool() const { return bool(wrapped()); }
627     const DIEValue &operator*() const { return wrapped()->V; }
628   };
629
630   typedef iterator_range<value_iterator> value_range;
631   typedef iterator_range<const_value_iterator> const_value_range;
632
633   value_iterator addValue(BumpPtrAllocator &Alloc, const DIEValue &V) {
634     List.push_back(*new (Alloc) Node(V));
635     return value_iterator(ListTy::toIterator(List.back()));
636   }
637   template <class T>
638   value_iterator addValue(BumpPtrAllocator &Alloc, dwarf::Attribute Attribute,
639                     dwarf::Form Form, T &&Value) {
640     return addValue(Alloc, DIEValue(Attribute, Form, std::forward<T>(Value)));
641   }
642
643   value_range values() {
644     return make_range(value_iterator(List.begin()), value_iterator(List.end()));
645   }
646   const_value_range values() const {
647     return make_range(const_value_iterator(List.begin()),
648                       const_value_iterator(List.end()));
649   }
650 };
651
652 //===--------------------------------------------------------------------===//
653 /// A structured debug information entry.  Has an abbreviation which
654 /// describes its organization.
655 class DIE : IntrusiveBackListNode, public DIEValueList {
656   friend class IntrusiveBackList<DIE>;
657   friend class DIEUnit;
658
659   /// Dwarf unit relative offset.
660   unsigned Offset;
661   /// Size of instance + children.
662   unsigned Size;
663   unsigned AbbrevNumber = ~0u;
664   /// Dwarf tag code.
665   dwarf::Tag Tag = (dwarf::Tag)0;
666   /// Set to true to force a DIE to emit an abbreviation that says it has
667   /// children even when it doesn't. This is used for unit testing purposes.
668   bool ForceChildren;
669   /// Children DIEs.
670   IntrusiveBackList<DIE> Children;
671
672   /// The owner is either the parent DIE for children of other DIEs, or a
673   /// DIEUnit which contains this DIE as its unit DIE.
674   PointerUnion<DIE *, DIEUnit *> Owner;
675
676   DIE() = delete;
677   explicit DIE(dwarf::Tag Tag) : Offset(0), Size(0), Tag(Tag),
678       ForceChildren(false) {}
679
680 public:
681   static DIE *get(BumpPtrAllocator &Alloc, dwarf::Tag Tag) {
682     return new (Alloc) DIE(Tag);
683   }
684
685   DIE(const DIE &RHS) = delete;
686   DIE(DIE &&RHS) = delete;
687   void operator=(const DIE &RHS) = delete;
688   void operator=(const DIE &&RHS) = delete;
689
690   // Accessors.
691   unsigned getAbbrevNumber() const { return AbbrevNumber; }
692   dwarf::Tag getTag() const { return Tag; }
693   /// Get the compile/type unit relative offset of this DIE.
694   unsigned getOffset() const { return Offset; }
695   unsigned getSize() const { return Size; }
696   bool hasChildren() const { return ForceChildren || !Children.empty(); }
697   void setForceChildren(bool B) { ForceChildren = B; }
698
699   typedef IntrusiveBackList<DIE>::iterator child_iterator;
700   typedef IntrusiveBackList<DIE>::const_iterator const_child_iterator;
701   typedef iterator_range<child_iterator> child_range;
702   typedef iterator_range<const_child_iterator> const_child_range;
703
704   child_range children() {
705     return make_range(Children.begin(), Children.end());
706   }
707   const_child_range children() const {
708     return make_range(Children.begin(), Children.end());
709   }
710
711   DIE *getParent() const;
712
713   /// Generate the abbreviation for this DIE.
714   ///
715   /// Calculate the abbreviation for this, which should be uniqued and
716   /// eventually used to call \a setAbbrevNumber().
717   DIEAbbrev generateAbbrev() const;
718
719   /// Set the abbreviation number for this DIE.
720   void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
721
722   /// Get the absolute offset within the .debug_info or .debug_types section
723   /// for this DIE.
724   unsigned getDebugSectionOffset() const;
725
726   /// Compute the offset of this DIE and all its children.
727   ///
728   /// This function gets called just before we are going to generate the debug
729   /// information and gives each DIE a chance to figure out its CU relative DIE
730   /// offset, unique its abbreviation and fill in the abbreviation code, and
731   /// return the unit offset that points to where the next DIE will be emitted
732   /// within the debug unit section. After this function has been called for all
733   /// DIE objects, the DWARF can be generated since all DIEs will be able to
734   /// properly refer to other DIE objects since all DIEs have calculated their
735   /// offsets.
736   ///
737   /// \param AP AsmPrinter to use when calculating sizes.
738   /// \param AbbrevSet the abbreviation used to unique DIE abbreviations.
739   /// \param CUOffset the compile/type unit relative offset in bytes.
740   /// \returns the offset for the DIE that follows this DIE within the
741   /// current compile/type unit.
742   unsigned computeOffsetsAndAbbrevs(const AsmPrinter *AP,
743                                     DIEAbbrevSet &AbbrevSet, unsigned CUOffset);
744
745   /// Climb up the parent chain to get the compile unit or type unit DIE that
746   /// this DIE belongs to.
747   ///
748   /// \returns the compile or type unit DIE that owns this DIE, or NULL if
749   /// this DIE hasn't been added to a unit DIE.
750   const DIE *getUnitDie() const;
751
752   /// Climb up the parent chain to get the compile unit or type unit that this
753   /// DIE belongs to.
754   ///
755   /// \returns the DIEUnit that represents the compile or type unit that owns
756   /// this DIE, or NULL if this DIE hasn't been added to a unit DIE.
757   const DIEUnit *getUnit() const;
758
759   void setOffset(unsigned O) { Offset = O; }
760   void setSize(unsigned S) { Size = S; }
761
762   /// Add a child to the DIE.
763   DIE &addChild(DIE *Child) {
764     assert(!Child->getParent() && "Child should be orphaned");
765     Child->Owner = this;
766     Children.push_back(*Child);
767     return Children.back();
768   }
769
770   /// Find a value in the DIE with the attribute given.
771   ///
772   /// Returns a default-constructed DIEValue (where \a DIEValue::getType()
773   /// gives \a DIEValue::isNone) if no such attribute exists.
774   DIEValue findAttribute(dwarf::Attribute Attribute) const;
775
776   void print(raw_ostream &O, unsigned IndentCount = 0) const;
777   void dump();
778 };
779
780 //===--------------------------------------------------------------------===//
781 /// Represents a compile or type unit.
782 class DIEUnit {
783   /// The compile unit or type unit DIE. This variable must be an instance of
784   /// DIE so that we can calculate the DIEUnit from any DIE by traversing the
785   /// parent backchain and getting the Unit DIE, and then casting itself to a
786   /// DIEUnit. This allows us to be able to find the DIEUnit for any DIE without
787   /// having to store a pointer to the DIEUnit in each DIE instance.
788   DIE Die;
789   /// The section this unit will be emitted in. This may or may not be set to
790   /// a valid section depending on the client that is emitting DWARF.
791   MCSection *Section;
792   uint64_t Offset; /// .debug_info or .debug_types absolute section offset.
793   uint32_t Length; /// The length in bytes of all of the DIEs in this unit.
794   const uint16_t Version; /// The Dwarf version number for this unit.
795   const uint8_t AddrSize; /// The size in bytes of an address for this unit.
796 protected:
797   ~DIEUnit() = default;
798
799 public:
800   DIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag);
801   DIEUnit(const DIEUnit &RHS) = delete;
802   DIEUnit(DIEUnit &&RHS) = delete;
803   void operator=(const DIEUnit &RHS) = delete;
804   void operator=(const DIEUnit &&RHS) = delete;
805   /// Set the section that this DIEUnit will be emitted into.
806   ///
807   /// This function is used by some clients to set the section. Not all clients
808   /// that emit DWARF use this section variable.
809   void setSection(MCSection *Section) {
810     assert(!this->Section);
811     this->Section = Section;
812   }
813
814   virtual const MCSymbol *getCrossSectionRelativeBaseAddress() const {
815     return nullptr;
816   }
817
818   /// Return the section that this DIEUnit will be emitted into.
819   ///
820   /// \returns Section pointer which can be NULL.
821   MCSection *getSection() const { return Section; }
822   void setDebugSectionOffset(unsigned O) { Offset = O; }
823   unsigned getDebugSectionOffset() const { return Offset; }
824   void setLength(uint64_t L) { Length = L; }
825   uint64_t getLength() const { return Length; }
826   uint16_t getDwarfVersion() const { return Version; }
827   uint16_t getAddressSize() const { return AddrSize; }
828   DIE &getUnitDie() { return Die; }
829   const DIE &getUnitDie() const { return Die; }
830 };
831
832 struct BasicDIEUnit final : DIEUnit {
833   BasicDIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag)
834       : DIEUnit(Version, AddrSize, UnitTag) {}
835 };
836
837 //===--------------------------------------------------------------------===//
838 /// DIELoc - Represents an expression location.
839 //
840 class DIELoc : public DIEValueList {
841   mutable unsigned Size; // Size in bytes excluding size header.
842
843 public:
844   DIELoc() : Size(0) {}
845
846   /// ComputeSize - Calculate the size of the location expression.
847   ///
848   unsigned ComputeSize(const AsmPrinter *AP) const;
849
850   /// BestForm - Choose the best form for data.
851   ///
852   dwarf::Form BestForm(unsigned DwarfVersion) const {
853     if (DwarfVersion > 3)
854       return dwarf::DW_FORM_exprloc;
855     // Pre-DWARF4 location expressions were blocks and not exprloc.
856     if ((unsigned char)Size == Size)
857       return dwarf::DW_FORM_block1;
858     if ((unsigned short)Size == Size)
859       return dwarf::DW_FORM_block2;
860     if ((unsigned int)Size == Size)
861       return dwarf::DW_FORM_block4;
862     return dwarf::DW_FORM_block;
863   }
864
865   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
866   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
867
868   void print(raw_ostream &O) const;
869 };
870
871 //===--------------------------------------------------------------------===//
872 /// DIEBlock - Represents a block of values.
873 //
874 class DIEBlock : public DIEValueList {
875   mutable unsigned Size; // Size in bytes excluding size header.
876
877 public:
878   DIEBlock() : Size(0) {}
879
880   /// ComputeSize - Calculate the size of the location expression.
881   ///
882   unsigned ComputeSize(const AsmPrinter *AP) const;
883
884   /// BestForm - Choose the best form for data.
885   ///
886   dwarf::Form BestForm() const {
887     if ((unsigned char)Size == Size)
888       return dwarf::DW_FORM_block1;
889     if ((unsigned short)Size == Size)
890       return dwarf::DW_FORM_block2;
891     if ((unsigned int)Size == Size)
892       return dwarf::DW_FORM_block4;
893     return dwarf::DW_FORM_block;
894   }
895
896   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
897   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
898
899   void print(raw_ostream &O) const;
900 };
901
902 } // end namespace llvm
903
904 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H