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