LLVM  5.0.0
DIE.h
Go to the documentation of this file.
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"
19 #include "llvm/ADT/PointerUnion.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/iterator.h"
26 #include "llvm/Support/AlignOf.h"
27 #include "llvm/Support/Allocator.h"
28 #include <cassert>
29 #include <cstddef>
30 #include <cstdint>
31 #include <iterator>
32 #include <new>
33 #include <type_traits>
34 #include <utility>
35 #include <vector>
36 
37 namespace llvm {
38 
39 class AsmPrinter;
40 class DIE;
41 class DIEUnit;
42 class MCExpr;
43 class MCSection;
44 class MCSymbol;
45 class raw_ostream;
46 
47 //===--------------------------------------------------------------------===//
48 /// Dwarf abbreviation data, describes one attribute of a Dwarf abbreviation.
50  /// Dwarf attribute code.
52 
53  /// Dwarf form code.
54  dwarf::Form Form;
55 
56  /// Dwarf attribute value for DW_FORM_implicit_const
57  int64_t Value = 0;
58 
59 public:
61  : Attribute(A), Form(F) {}
63  : Attribute(A), Form(dwarf::DW_FORM_implicit_const), Value(V) {}
64 
65  /// Accessors.
66  /// @{
68  dwarf::Form getForm() const { return Form; }
69  int64_t getValue() const { return Value; }
70  /// @}
71 
72  /// Used to gather unique data for the abbreviation folding set.
73  void Profile(FoldingSetNodeID &ID) const;
74 };
75 
76 //===--------------------------------------------------------------------===//
77 /// Dwarf abbreviation, describes the organization of a debug information
78 /// object.
79 class DIEAbbrev : public FoldingSetNode {
80  /// Unique number for node.
81  unsigned Number;
82 
83  /// Dwarf tag code.
84  dwarf::Tag Tag;
85 
86  /// Whether or not this node has children.
87  ///
88  /// This cheats a bit in all of the uses since the values in the standard
89  /// are 0 and 1 for no children and children respectively.
90  bool Children;
91 
92  /// Raw data bytes for abbreviation.
94 
95 public:
96  DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C) {}
97 
98  /// Accessors.
99  /// @{
100  dwarf::Tag getTag() const { return Tag; }
101  unsigned getNumber() const { return Number; }
102  bool hasChildren() const { return Children; }
103  const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
104  void setChildrenFlag(bool hasChild) { Children = hasChild; }
105  void setNumber(unsigned N) { Number = N; }
106  /// @}
107 
108  /// Adds another set of attribute information to the abbreviation.
110  Data.push_back(DIEAbbrevData(Attribute, Form));
111  }
112 
113  /// Adds attribute with DW_FORM_implicit_const value
115  Data.push_back(DIEAbbrevData(Attribute, Value));
116  }
117 
118  /// Used to gather unique data for the abbreviation folding set.
119  void Profile(FoldingSetNodeID &ID) const;
120 
121  /// Print the abbreviation using the specified asm printer.
122  void Emit(const AsmPrinter *AP) const;
123 
124  void print(raw_ostream &O) const;
125  void dump() const;
126 };
127 
128 //===--------------------------------------------------------------------===//
129 /// Helps unique DIEAbbrev objects and assigns abbreviation numbers.
130 ///
131 /// This class will unique the DIE abbreviations for a llvm::DIE object and
132 /// assign a unique abbreviation number to each unique DIEAbbrev object it
133 /// finds. The resulting collection of DIEAbbrev objects can then be emitted
134 /// into the .debug_abbrev section.
136  /// The bump allocator to use when creating DIEAbbrev objects in the uniqued
137  /// storage container.
138  BumpPtrAllocator &Alloc;
139  /// \brief FoldingSet that uniques the abbreviations.
140  FoldingSet<DIEAbbrev> AbbreviationsSet;
141  /// A list of all the unique abbreviations in use.
142  std::vector<DIEAbbrev *> Abbreviations;
143 
144 public:
146  ~DIEAbbrevSet();
147 
148  /// Generate the abbreviation declaration for a DIE and return a pointer to
149  /// the generated abbreviation.
150  ///
151  /// \param Die the debug info entry to generate the abbreviation for.
152  /// \returns A reference to the uniqued abbreviation declaration that is
153  /// owned by this class.
155 
156  /// Print all abbreviations using the specified asm printer.
157  void Emit(const AsmPrinter *AP, MCSection *Section) const;
158 };
159 
160 //===--------------------------------------------------------------------===//
161 /// An integer value DIE.
162 ///
163 class DIEInteger {
164  uint64_t Integer;
165 
166 public:
167  explicit DIEInteger(uint64_t I) : Integer(I) {}
168 
169  /// Choose the best form for integer.
170  static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
171  if (IsSigned) {
172  const int64_t SignedInt = Int;
173  if ((char)Int == SignedInt)
174  return dwarf::DW_FORM_data1;
175  if ((short)Int == SignedInt)
176  return dwarf::DW_FORM_data2;
177  if ((int)Int == SignedInt)
178  return dwarf::DW_FORM_data4;
179  } else {
180  if ((unsigned char)Int == Int)
181  return dwarf::DW_FORM_data1;
182  if ((unsigned short)Int == Int)
183  return dwarf::DW_FORM_data2;
184  if ((unsigned int)Int == Int)
185  return dwarf::DW_FORM_data4;
186  }
187  return dwarf::DW_FORM_data8;
188  }
189 
190  uint64_t getValue() const { return Integer; }
191  void setValue(uint64_t Val) { Integer = Val; }
192 
193  void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
194  unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
195 
196  void print(raw_ostream &O) const;
197 };
198 
199 //===--------------------------------------------------------------------===//
200 /// An expression DIE.
201 class DIEExpr {
202  const MCExpr *Expr;
203 
204 public:
205  explicit DIEExpr(const MCExpr *E) : Expr(E) {}
206 
207  /// Get MCExpr.
208  const MCExpr *getValue() const { return Expr; }
209 
210  void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
211  unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
212 
213  void print(raw_ostream &O) const;
214 };
215 
216 //===--------------------------------------------------------------------===//
217 /// A label DIE.
218 class DIELabel {
219  const MCSymbol *Label;
220 
221 public:
222  explicit DIELabel(const MCSymbol *L) : Label(L) {}
223 
224  /// Get MCSymbol.
225  const MCSymbol *getValue() const { return Label; }
226 
227  void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
228  unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
229 
230  void print(raw_ostream &O) const;
231 };
232 
233 //===--------------------------------------------------------------------===//
234 /// A simple label difference DIE.
235 ///
236 class DIEDelta {
237  const MCSymbol *LabelHi;
238  const MCSymbol *LabelLo;
239 
240 public:
241  DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo) : LabelHi(Hi), LabelLo(Lo) {}
242 
243  void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
244  unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
245 
246  void print(raw_ostream &O) const;
247 };
248 
249 //===--------------------------------------------------------------------===//
250 /// A container for string pool string values.
251 ///
252 /// This class is used with the DW_FORM_strp and DW_FORM_GNU_str_index forms.
253 class DIEString {
255 
256 public:
258 
259  /// Grab the string out of the object.
260  StringRef getString() const { return S.getString(); }
261 
262  void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
263  unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
264 
265  void print(raw_ostream &O) const;
266 };
267 
268 //===--------------------------------------------------------------------===//
269 /// A container for inline string values.
270 ///
271 /// This class is used with the DW_FORM_string form.
273  StringRef S;
274 
275 public:
276  template <typename Allocator>
277  explicit DIEInlineString(StringRef Str, Allocator &A) : S(Str.copy(A)) {}
278 
279  ~DIEInlineString() = default;
280 
281  /// Grab the string out of the object.
282  StringRef getString() const { return S; }
283 
284  void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
285  unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
286 
287  void print(raw_ostream &O) const;
288 };
289 
290 //===--------------------------------------------------------------------===//
291 /// A pointer to another debug information entry. An instance of this class can
292 /// also be used as a proxy for a debug information entry not yet defined
293 /// (ie. types.)
294 class DIEEntry {
295  DIE *Entry;
296 
297 public:
298  DIEEntry() = delete;
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 {
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  using ValTy = AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
352  DIEDelta *, DIEEntry, DIEBlock *,
353  DIELoc *, DIELocList>;
354 
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 
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; }
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 
480 
482 
484  return Next.getInt() ? nullptr : Next.getPointer();
485  }
486 };
487 
490 
491  Node *Last = nullptr;
492 
493  bool empty() const { return !Last; }
494 
495  void push_back(Node &N) {
496  assert(N.Next.getPointer() == &N && "Expected unlinked node");
497  assert(N.Next.getInt() == true && "Expected unlinked node");
498 
499  if (Last) {
500  N.Next = Last->Next;
501  Last->Next.setPointerAndInt(&N, false);
502  }
503  Last = &N;
504  }
505 };
506 
507 template <class T> class IntrusiveBackList : IntrusiveBackListBase {
508 public:
510 
512  T &back() { return *static_cast<T *>(Last); }
513  const T &back() const { return *static_cast<T *>(Last); }
514 
515  class const_iterator;
516  class iterator
517  : public iterator_facade_base<iterator, std::forward_iterator_tag, T> {
518  friend class const_iterator;
519 
520  Node *N = nullptr;
521 
522  public:
523  iterator() = default;
524  explicit iterator(T *N) : N(N) {}
525 
527  N = N->getNext();
528  return *this;
529  }
530 
531  explicit operator bool() const { return N; }
532  T &operator*() const { return *static_cast<T *>(N); }
533 
534  bool operator==(const iterator &X) const { return N == X.N; }
535  bool operator!=(const iterator &X) const { return N != X.N; }
536  };
537 
539  : public iterator_facade_base<const_iterator, std::forward_iterator_tag,
540  const T> {
541  const Node *N = nullptr;
542 
543  public:
544  const_iterator() = default;
545  // Placate MSVC by explicitly scoping 'iterator'.
547  explicit const_iterator(const T *N) : N(N) {}
548 
550  N = N->getNext();
551  return *this;
552  }
553 
554  explicit operator bool() const { return N; }
555  const T &operator*() const { return *static_cast<const T *>(N); }
556 
557  bool operator==(const const_iterator &X) const { return N == X.N; }
558  bool operator!=(const const_iterator &X) const { return N != X.N; }
559  };
560 
561  iterator begin() {
562  return Last ? iterator(static_cast<T *>(Last->Next.getPointer())) : end();
563  }
564  const_iterator begin() const {
565  return const_cast<IntrusiveBackList *>(this)->begin();
566  }
567  iterator end() { return iterator(); }
568  const_iterator end() const { return const_iterator(); }
569 
570  static iterator toIterator(T &N) { return iterator(&N); }
571  static const_iterator toIterator(const T &N) { return const_iterator(&N); }
572 };
573 
574 /// A list of DIE values.
575 ///
576 /// This is a singly-linked list, but instead of reversing the order of
577 /// insertion, we keep a pointer to the back of the list so we can push in
578 /// order.
579 ///
580 /// There are two main reasons to choose a linked list over a customized
581 /// vector-like data structure.
582 ///
583 /// 1. For teardown efficiency, we want DIEs to be BumpPtrAllocated. Using a
584 /// linked list here makes this way easier to accomplish.
585 /// 2. Carrying an extra pointer per \a DIEValue isn't expensive. 45% of DIEs
586 /// have 2 or fewer values, and 90% have 5 or fewer. A vector would be
587 /// over-allocated by 50% on average anyway, the same cost as the
588 /// linked-list node.
590  struct Node : IntrusiveBackListNode {
591  DIEValue V;
592 
593  explicit Node(DIEValue V) : V(V) {}
594  };
595 
597 
598  ListTy List;
599 
600 public:
601  class const_value_iterator;
603  : public iterator_adaptor_base<value_iterator, ListTy::iterator,
604  std::forward_iterator_tag, DIEValue> {
605  friend class const_value_iterator;
606 
607  using iterator_adaptor =
609  std::forward_iterator_tag, DIEValue>;
610 
611  public:
612  value_iterator() = default;
614 
615  explicit operator bool() const { return bool(wrapped()); }
616  DIEValue &operator*() const { return wrapped()->V; }
617  };
618 
620  const_value_iterator, ListTy::const_iterator,
621  std::forward_iterator_tag, const DIEValue> {
622  using iterator_adaptor =
624  std::forward_iterator_tag, const DIEValue>;
625 
626  public:
627  const_value_iterator() = default;
629  : iterator_adaptor(X.wrapped()) {}
631  : iterator_adaptor(X) {}
632 
633  explicit operator bool() const { return bool(wrapped()); }
634  const DIEValue &operator*() const { return wrapped()->V; }
635  };
636 
639 
641  List.push_back(*new (Alloc) Node(V));
642  return value_iterator(ListTy::toIterator(List.back()));
643  }
644  template <class T>
646  dwarf::Form Form, T &&Value) {
647  return addValue(Alloc, DIEValue(Attribute, Form, std::forward<T>(Value)));
648  }
649 
651  return make_range(value_iterator(List.begin()), value_iterator(List.end()));
652  }
654  return make_range(const_value_iterator(List.begin()),
655  const_value_iterator(List.end()));
656  }
657 };
658 
659 //===--------------------------------------------------------------------===//
660 /// A structured debug information entry. Has an abbreviation which
661 /// describes its organization.
663  friend class IntrusiveBackList<DIE>;
664  friend class DIEUnit;
665 
666  /// Dwarf unit relative offset.
667  unsigned Offset = 0;
668  /// Size of instance + children.
669  unsigned Size = 0;
670  unsigned AbbrevNumber = ~0u;
671  /// Dwarf tag code.
673  /// Set to true to force a DIE to emit an abbreviation that says it has
674  /// children even when it doesn't. This is used for unit testing purposes.
675  bool ForceChildren = false;
676  /// Children DIEs.
677  IntrusiveBackList<DIE> Children;
678 
679  /// The owner is either the parent DIE for children of other DIEs, or a
680  /// DIEUnit which contains this DIE as its unit DIE.
682 
683  explicit DIE(dwarf::Tag Tag) : Tag(Tag) {}
684 
685 public:
686  DIE() = delete;
687  DIE(const DIE &RHS) = delete;
688  DIE(DIE &&RHS) = delete;
689  DIE &operator=(const DIE &RHS) = delete;
690  DIE &operator=(const DIE &&RHS) = delete;
691 
693  return new (Alloc) DIE(Tag);
694  }
695 
696  // Accessors.
697  unsigned getAbbrevNumber() const { return AbbrevNumber; }
698  dwarf::Tag getTag() const { return Tag; }
699  /// Get the compile/type unit relative offset of this DIE.
700  unsigned getOffset() const { return Offset; }
701  unsigned getSize() const { return Size; }
702  bool hasChildren() const { return ForceChildren || !Children.empty(); }
703  void setForceChildren(bool B) { ForceChildren = B; }
704 
709 
711  return make_range(Children.begin(), Children.end());
712  }
714  return make_range(Children.begin(), Children.end());
715  }
716 
717  DIE *getParent() const;
718 
719  /// Generate the abbreviation for this DIE.
720  ///
721  /// Calculate the abbreviation for this, which should be uniqued and
722  /// eventually used to call \a setAbbrevNumber().
723  DIEAbbrev generateAbbrev() const;
724 
725  /// Set the abbreviation number for this DIE.
726  void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
727 
728  /// Get the absolute offset within the .debug_info or .debug_types section
729  /// for this DIE.
730  unsigned getDebugSectionOffset() const;
731 
732  /// Compute the offset of this DIE and all its children.
733  ///
734  /// This function gets called just before we are going to generate the debug
735  /// information and gives each DIE a chance to figure out its CU relative DIE
736  /// offset, unique its abbreviation and fill in the abbreviation code, and
737  /// return the unit offset that points to where the next DIE will be emitted
738  /// within the debug unit section. After this function has been called for all
739  /// DIE objects, the DWARF can be generated since all DIEs will be able to
740  /// properly refer to other DIE objects since all DIEs have calculated their
741  /// offsets.
742  ///
743  /// \param AP AsmPrinter to use when calculating sizes.
744  /// \param AbbrevSet the abbreviation used to unique DIE abbreviations.
745  /// \param CUOffset the compile/type unit relative offset in bytes.
746  /// \returns the offset for the DIE that follows this DIE within the
747  /// current compile/type unit.
748  unsigned computeOffsetsAndAbbrevs(const AsmPrinter *AP,
749  DIEAbbrevSet &AbbrevSet, unsigned CUOffset);
750 
751  /// Climb up the parent chain to get the compile unit or type unit DIE that
752  /// this DIE belongs to.
753  ///
754  /// \returns the compile or type unit DIE that owns this DIE, or NULL if
755  /// this DIE hasn't been added to a unit DIE.
756  const DIE *getUnitDie() const;
757 
758  /// Climb up the parent chain to get the compile unit or type unit that this
759  /// DIE belongs to.
760  ///
761  /// \returns the DIEUnit that represents the compile or type unit that owns
762  /// this DIE, or NULL if this DIE hasn't been added to a unit DIE.
763  const DIEUnit *getUnit() const;
764 
765  void setOffset(unsigned O) { Offset = O; }
766  void setSize(unsigned S) { Size = S; }
767 
768  /// Add a child to the DIE.
769  DIE &addChild(DIE *Child) {
770  assert(!Child->getParent() && "Child should be orphaned");
771  Child->Owner = this;
772  Children.push_back(*Child);
773  return Children.back();
774  }
775 
776  /// Find a value in the DIE with the attribute given.
777  ///
778  /// Returns a default-constructed DIEValue (where \a DIEValue::getType()
779  /// gives \a DIEValue::isNone) if no such attribute exists.
781 
782  void print(raw_ostream &O, unsigned IndentCount = 0) const;
783  void dump() const;
784 };
785 
786 //===--------------------------------------------------------------------===//
787 /// Represents a compile or type unit.
788 class DIEUnit {
789  /// The compile unit or type unit DIE. This variable must be an instance of
790  /// DIE so that we can calculate the DIEUnit from any DIE by traversing the
791  /// parent backchain and getting the Unit DIE, and then casting itself to a
792  /// DIEUnit. This allows us to be able to find the DIEUnit for any DIE without
793  /// having to store a pointer to the DIEUnit in each DIE instance.
794  DIE Die;
795  /// The section this unit will be emitted in. This may or may not be set to
796  /// a valid section depending on the client that is emitting DWARF.
797  MCSection *Section;
798  uint64_t Offset; /// .debug_info or .debug_types absolute section offset.
799  uint32_t Length; /// The length in bytes of all of the DIEs in this unit.
800  const uint16_t Version; /// The Dwarf version number for this unit.
801  const uint8_t AddrSize; /// The size in bytes of an address for this unit.
802 protected:
803  ~DIEUnit() = default;
804 
805 public:
806  DIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag);
807  DIEUnit(const DIEUnit &RHS) = delete;
808  DIEUnit(DIEUnit &&RHS) = delete;
809  void operator=(const DIEUnit &RHS) = delete;
810  void operator=(const DIEUnit &&RHS) = delete;
811  /// Set the section that this DIEUnit will be emitted into.
812  ///
813  /// This function is used by some clients to set the section. Not all clients
814  /// that emit DWARF use this section variable.
815  void setSection(MCSection *Section) {
816  assert(!this->Section);
817  this->Section = Section;
818  }
819 
821  return nullptr;
822  }
823 
824  /// Return the section that this DIEUnit will be emitted into.
825  ///
826  /// \returns Section pointer which can be NULL.
827  MCSection *getSection() const { return Section; }
828  void setDebugSectionOffset(unsigned O) { Offset = O; }
829  unsigned getDebugSectionOffset() const { return Offset; }
830  void setLength(uint64_t L) { Length = L; }
831  uint64_t getLength() const { return Length; }
832  uint16_t getDwarfVersion() const { return Version; }
833  uint16_t getAddressSize() const { return AddrSize; }
834  DIE &getUnitDie() { return Die; }
835  const DIE &getUnitDie() const { return Die; }
836 };
837 
838 struct BasicDIEUnit final : DIEUnit {
839  BasicDIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag)
840  : DIEUnit(Version, AddrSize, UnitTag) {}
841 };
842 
843 //===--------------------------------------------------------------------===//
844 /// DIELoc - Represents an expression location.
845 //
846 class DIELoc : public DIEValueList {
847  mutable unsigned Size = 0; // Size in bytes excluding size header.
848 
849 public:
850  DIELoc() = default;
851 
852  /// ComputeSize - Calculate the size of the location expression.
853  ///
854  unsigned ComputeSize(const AsmPrinter *AP) const;
855 
856  /// BestForm - Choose the best form for data.
857  ///
859  if (DwarfVersion > 3)
860  return dwarf::DW_FORM_exprloc;
861  // Pre-DWARF4 location expressions were blocks and not exprloc.
862  if ((unsigned char)Size == Size)
863  return dwarf::DW_FORM_block1;
864  if ((unsigned short)Size == Size)
865  return dwarf::DW_FORM_block2;
866  if ((unsigned int)Size == Size)
867  return dwarf::DW_FORM_block4;
868  return dwarf::DW_FORM_block;
869  }
870 
871  void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
872  unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
873 
874  void print(raw_ostream &O) const;
875 };
876 
877 //===--------------------------------------------------------------------===//
878 /// DIEBlock - Represents a block of values.
879 //
880 class DIEBlock : public DIEValueList {
881  mutable unsigned Size = 0; // Size in bytes excluding size header.
882 
883 public:
884  DIEBlock() = default;
885 
886  /// ComputeSize - Calculate the size of the location expression.
887  ///
888  unsigned ComputeSize(const AsmPrinter *AP) const;
889 
890  /// BestForm - Choose the best form for data.
891  ///
893  if ((unsigned char)Size == Size)
894  return dwarf::DW_FORM_block1;
895  if ((unsigned short)Size == Size)
896  return dwarf::DW_FORM_block2;
897  if ((unsigned int)Size == Size)
898  return dwarf::DW_FORM_block4;
899  return dwarf::DW_FORM_block;
900  }
901 
902  void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
903  unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
904 
905  void print(raw_ostream &O) const;
906 };
907 
908 } // end namespace llvm
909 
910 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
MachineLoop * L
const T & back() const
Definition: DIE.h:513
const_iterator begin() const
Definition: DIE.h:564
DIEAbbrev generateAbbrev() const
Generate the abbreviation for this DIE.
Definition: DIE.cpp:185
iterator begin()
Definition: DIE.h:561
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit block data.
Definition: DIE.cpp:773
void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit debug information entry offset.
Definition: DIE.cpp:641
DIEString(DwarfStringPoolEntryRef S)
Definition: DIE.h:257
void setSize(unsigned S)
Definition: DIE.h:766
DIELoc - Represents an expression location.
Definition: DIE.h:846
void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit string value.
Definition: DIE.cpp:566
iterator end()
Definition: DIE.h:567
DIELabel(const MCSymbol *L)
Definition: DIE.h:222
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
IntrusiveBackListNode * getNext() const
Definition: DIE.h:483
DIE & getEntry() const
Definition: DIE.h:301
void EmitValue(const AsmPrinter *AP) const
Emit value via the Dwarf writer.
Definition: DIE.cpp:325
DIEValue(const DIEValue &X)
Definition: DIE.h:420
DIE()=delete
void print(raw_ostream &O) const
Definition: DIE.cpp:483
Attribute
Attributes.
Definition: Dwarf.h:105
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
child_range children()
Definition: DIE.h:710
static const_iterator toIterator(const T &N)
Definition: DIE.h:571
DIEAbbrev(dwarf::Tag T, bool C)
Definition: DIE.h:96
bool hasChildren() const
Definition: DIE.h:102
dwarf::Form BestForm(unsigned DwarfVersion) const
BestForm - Choose the best form for data.
Definition: DIE.h:858
const_value_iterator(DIEValueList::value_iterator X)
Definition: DIE.h:628
~DIEUnit()=default
The size in bytes of an address for this unit.
void print(raw_ostream &O) const
Definition: DIE.cpp:534
void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form)
Adds another set of attribute information to the abbreviation.
Definition: DIE.h:109
DIELoc()=default
Represents a pointer to a location list in the debug_loc section.
Definition: DIE.h:312
value_iterator addValue(BumpPtrAllocator &Alloc, dwarf::Attribute Attribute, dwarf::Form Form, T &&Value)
Definition: DIE.h:645
DIE & operator=(const DIE &RHS)=delete
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
void Profile(FoldingSetNodeID &ID) const
Used to gather unique data for the abbreviation folding set.
Definition: DIE.cpp:57
const DIE * getUnitDie() const
Climb up the parent chain to get the compile unit or type unit DIE that this DIE belongs to...
Definition: DIE.cpp:202
Dwarf abbreviation data, describes one attribute of a Dwarf abbreviation.
Definition: DIE.h:49
void print(raw_ostream &O, unsigned IndentCount=0) const
Definition: DIE.cpp:246
size_t getValue() const
Grab the current index out.
Definition: DIE.h:320
const_child_range children() const
Definition: DIE.h:713
const DIE & getUnitDie() const
Definition: DIE.h:835
DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
Definition: DIE.h:241
void setForceChildren(bool B)
Definition: DIE.h:703
DIEInlineString(StringRef Str, Allocator &A)
Definition: DIE.h:277
dwarf::Form getForm() const
Definition: DIE.h:68
bool empty() const
Definition: DIE.h:493
void AddImplicitConstAttribute(dwarf::Attribute Attribute, int64_t Value)
Adds attribute with DW_FORM_implicit_const value.
Definition: DIE.h:114
String pool entry reference.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const
SizeOf - Determine size of delta value in bytes.
Definition: DIE.cpp:590
bool operator==(const iterator &X) const
Definition: DIE.h:534
const_iterator & operator++()
Definition: DIE.h:549
void dump() const
Definition: DIE.cpp:271
void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit location data.
Definition: DIE.cpp:722
unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:672
#define F(x, y, z)
Definition: MD5.cpp:55
const_iterator(typename IntrusiveBackList< T >::iterator X)
Definition: DIE.h:546
bool operator==(const const_iterator &X) const
Definition: DIE.h:557
void setNumber(unsigned N)
Definition: DIE.h:105
const MCSymbol * getValue() const
Get MCSymbol.
Definition: DIE.h:225
#define T
A list of DIE values.
Definition: DIE.h:589
unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:625
void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit expression value.
Definition: DIE.cpp:494
DIE & addChild(DIE *Child)
Add a child to the DIE.
Definition: DIE.h:769
dwarf::Tag getTag() const
Definition: DIE.h:698
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:68
uint16_t getDwarfVersion() const
Definition: DIE.h:832
void print(raw_ostream &O) const
Definition: DIE.cpp:824
Helps unique DIEAbbrev objects and assigns abbreviation numbers.
Definition: DIE.h:135
void push_back(T &N)
Definition: DIE.h:511
~DIEValue()
Definition: DIE.h:433
~DIEInlineString()=default
void dump() const
Definition: DIE.cpp:363
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:311
unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const
SizeOf - Determine size of label value in bytes.
Definition: DIE.cpp:526
A pointer to another debug information entry.
Definition: DIE.h:294
bool hasChildren() const
Definition: DIE.h:702
unsigned getDebugSectionOffset() const
Get the absolute offset within the .debug_info or .debug_types section for this DIE.
Definition: DIE.cpp:196
void print(raw_ostream &O) const
Definition: DIE.cpp:108
DIEValue()=default
PointerIntPair - This class implements a pair of a pointer and small integer.
static iterator toIterator(T &N)
Definition: DIE.h:570
StringRef getString() const
Grab the string out of the object.
Definition: DIE.h:282
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:138
unsigned getAbbrevNumber() const
Definition: DIE.h:697
CRTP base class for adapting an iterator to a different type.
Definition: iterator.h:208
value_range values()
Definition: DIE.h:650
DIEAbbrevSet(BumpPtrAllocator &A)
Definition: DIE.h:145
DIEValue findAttribute(dwarf::Attribute Attribute) const
Find a value in the DIE with the attribute given.
Definition: DIE.cpp:220
A container for inline string values.
Definition: DIE.h:272
DIE * getParent() const
Definition: DIE.cpp:181
A structured debug information entry.
Definition: DIE.h:662
void print(raw_ostream &O) const
Definition: DIE.cpp:608
#define A
Definition: LargeTest.cpp:12
void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit delta value.
Definition: DIE.cpp:542
void Emit(const AsmPrinter *AP, MCSection *Section) const
Print all abbreviations using the specified asm printer.
Definition: DIE.cpp:169
DIE & getUnitDie()
Definition: DIE.h:834
const_value_range values() const
Definition: DIE.h:653
static dwarf::Form BestForm(bool IsSigned, uint64_t Int)
Choose the best form for integer.
Definition: DIE.h:170
bool operator!=(const iterator &X) const
Definition: DIE.h:535
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:76
unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const
SizeOf - Determine size of integer value in bytes.
Definition: DIE.cpp:425
void push_back(Node &N)
Definition: DIE.h:495
unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const
SizeOf - Determine size of block data in bytes.
Definition: DIE.cpp:788
An expression DIE.
Definition: DIE.h:201
void print(raw_ostream &O) const
Definition: DIE.cpp:799
cl::opt< int > DwarfVersion("dwarf-version", cl::desc("Dwarf version"), cl::init(0))
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
unsigned getSize() const
Definition: DIE.h:701
A label DIE.
Definition: DIE.h:218
DIELocList(size_t I)
Definition: DIE.h:317
void print(raw_ostream &O) const
Definition: DIE.cpp:701
char buffer[Size]
Definition: AlignOf.h:37
int64_t getValue() const
Definition: DIE.h:69
A container for string pool string values.
Definition: DIE.h:253
void operator=(const DIEUnit &RHS)=delete
void setDebugSectionOffset(unsigned O)
Definition: DIE.h:828
FoldingSet - This template class is used to instantiate a specialized implementation of the folding s...
Definition: FoldingSet.h:479
unsigned computeOffsetsAndAbbrevs(const AsmPrinter *AP, DIEAbbrevSet &AbbrevSet, unsigned CUOffset)
Compute the offset of this DIE and all its children.
Definition: DIE.cpp:276
uint64_t getValue() const
Definition: DIE.h:190
void print(raw_ostream &O) const
Definition: DIE.cpp:631
void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit label value.
Definition: DIE.cpp:817
A simple label difference DIE.
Definition: DIE.h:236
const DIEValue & operator*() const
Definition: DIE.h:634
DIEAbbrevData(dwarf::Attribute A, int64_t V)
Definition: DIE.h:62
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
dwarf::Attribute getAttribute() const
Accessors.
Definition: DIE.h:67
Basic Register Allocator
unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const
SizeOf - Determine size of expression value in bytes.
Definition: DIE.cpp:500
void dump() const
Definition: DIE.cpp:131
#define E
Definition: LargeTest.cpp:27
#define B
Definition: LargeTest.cpp:24
uint16_t getAddressSize() const
Definition: DIE.h:833
const DIEUnit * getUnit() const
Climb up the parent chain to get the compile unit or type unit that this DIE belongs to...
Definition: DIE.cpp:213
unsigned getDebugSectionOffset() const
Definition: DIE.h:829
DIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag)
Definition: DIE.cpp:315
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
DIEInteger(uint64_t I)
Definition: DIE.h:167
StringRef getString() const
Grab the string out of the object.
Definition: DIE.h:260
unsigned getOffset() const
Get the compile/type unit relative offset of this DIE.
Definition: DIE.h:700
Represents a compile or type unit.
Definition: DIE.h:788
const size_t N
dwarf::Attribute getAttribute() const
Definition: DIE.h:451
dwarf::Form getForm() const
Definition: DIE.h:452
void Profile(FoldingSetNodeID &ID) const
Used to gather unique data for the abbreviation folding set.
Definition: DIE.cpp:42
uint64_t getLength() const
Definition: DIE.h:831
virtual const MCSymbol * getCrossSectionRelativeBaseAddress() const
Definition: DIE.h:820
bool operator!=(const const_iterator &X) const
Definition: DIE.h:558
A range adaptor for a pair of iterators.
DIEValue & operator*() const
Definition: DIE.h:616
unsigned SizeOf(const AsmPrinter *AP) const
Return the size of a value in bytes.
Definition: DIE.cpp:337
void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:615
unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const
SizeOf - Determine size of location data in bytes.
Definition: DIE.cpp:739
DIEExpr(const MCExpr *E)
Definition: DIE.h:205
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
DIEValue & operator=(const DIEValue &X)
Definition: DIE.h:424
DIEAbbrev & uniqueAbbreviation(DIE &Die)
Generate the abbreviation declaration for a DIE and return a pointer to the generated abbreviation...
Definition: DIE.cpp:145
Basic Alias true
An integer value DIE.
Definition: DIE.h:163
const T & operator*() const
Definition: DIE.h:555
Dwarf abbreviation, describes the organization of a debug information object.
Definition: DIE.h:79
void setSection(MCSection *Section)
Set the section that this DIEUnit will be emitted into.
Definition: DIE.h:815
dwarf::Form BestForm() const
BestForm - Choose the best form for data.
Definition: DIE.h:892
Node - This class is used to maintain the singly linked bucket list in a folding set.
Definition: FoldingSet.h:139
void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit integer of appropriate size.
Definition: DIE.cpp:374
#define C
Definition: LargeTest.cpp:25
void setOffset(unsigned O)
Definition: DIE.h:765
#define I(x, y, z)
Definition: MD5.cpp:58
value_iterator(ListTy::iterator X)
Definition: DIE.h:613
void setLength(uint64_t L)
Definition: DIE.h:830
PointerIntPair< IntrusiveBackListNode *, 1 > Next
Definition: DIE.h:479
DIEBlock()=default
const_value_iterator(ListTy::const_iterator X)
Definition: DIE.h:630
BasicDIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag)
Definition: DIE.h:839
const MCExpr * getValue() const
Get MCExpr.
Definition: DIE.h:208
const SmallVectorImpl< DIEAbbrevData > & getData() const
Definition: DIE.h:103
void setChildrenFlag(bool hasChild)
Definition: DIE.h:104
unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:807
void print(raw_ostream &O) const
Definition: DIE.cpp:556
void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit label value.
Definition: DIE.cpp:516
DIEEntry(DIE &E)
Definition: DIE.h:299
MCSection * getSection() const
Return the section that this DIEUnit will be emitted into.
Definition: DIE.h:827
void print(raw_ostream &O) const
Definition: DIE.cpp:752
Type getType() const
Accessors.
Definition: DIE.h:450
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
dwarf::Tag getTag() const
Accessors.
Definition: DIE.h:100
unsigned ComputeSize(const AsmPrinter *AP) const
ComputeSize - Calculate the size of the location expression.
Definition: DIE.cpp:762
LLVM Value Representation.
Definition: Value.h:73
IntrusiveBackListNode Node
Definition: DIE.h:489
BumpPtrAllocator Alloc
void setAbbrevNumber(unsigned I)
Set the abbreviation number for this DIE.
Definition: DIE.h:726
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
unsigned ComputeSize(const AsmPrinter *AP) const
ComputeSize - Calculate the size of the location expression.
Definition: DIE.cpp:711
const_iterator end() const
Definition: DIE.h:568
void Emit(const AsmPrinter *AP) const
Print the abbreviation using the specified asm printer.
Definition: DIE.cpp:68
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
DIEAbbrevData(dwarf::Attribute A, dwarf::Form F)
Definition: DIE.h:60
DIEBlock - Represents a block of values.
Definition: DIE.h:880
void print(raw_ostream &O) const
Definition: DIE.cpp:508
void print(raw_ostream &O) const
Definition: DIE.cpp:350
DIEEntry()=delete
value_iterator addValue(BumpPtrAllocator &Alloc, const DIEValue &V)
Definition: DIE.h:640
void setValue(uint64_t Val)
Definition: DIE.h:191
unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const
SizeOf - Determine size of delta value in bytes.
Definition: DIE.cpp:548
unsigned getNumber() const
Definition: DIE.h:101
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
Definition: PointerUnion.h:87