LLVM  10.0.0svn
DIE.h
Go to the documentation of this file.
1 //===- lib/CodeGen/DIE.h - DWARF Info Entries -------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Data structures for DWARF info entries.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
14 #define LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
15 
16 #include "llvm/ADT/FoldingSet.h"
18 #include "llvm/ADT/PointerUnion.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/iterator.h"
25 #include "llvm/Support/AlignOf.h"
26 #include "llvm/Support/Allocator.h"
27 #include <cassert>
28 #include <cstddef>
29 #include <cstdint>
30 #include <iterator>
31 #include <new>
32 #include <type_traits>
33 #include <utility>
34 #include <vector>
35 
36 namespace llvm {
37 
38 class AsmPrinter;
39 class DIE;
40 class DIEUnit;
41 class DwarfCompileUnit;
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  /// @{
67  dwarf::Attribute getAttribute() const { return Attribute; }
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.
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  /// 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:
145  DIEAbbrevSet(BumpPtrAllocator &A) : Alloc(A) {}
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.
154  DIEAbbrev &uniqueAbbreviation(DIE &Die);
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 *Asm, 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 BaseTypeRef DIE.
236  const DwarfCompileUnit *CU;
237  const uint64_t Index;
238  static constexpr unsigned ULEB128PadSize = 4;
239 
240 public:
241  explicit DIEBaseTypeRef(const DwarfCompileUnit *TheCU, uint64_t Idx)
242  : CU(TheCU), Index(Idx) {}
243 
244  /// EmitValue - Emit base type reference.
245  void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
246  /// SizeOf - Determine size of the base type reference in bytes.
247  unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
248 
249  void print(raw_ostream &O) const;
250 };
251 
252 //===--------------------------------------------------------------------===//
253 /// A simple label difference DIE.
254 ///
255 class DIEDelta {
256  const MCSymbol *LabelHi;
257  const MCSymbol *LabelLo;
258 
259 public:
260  DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo) : LabelHi(Hi), LabelLo(Lo) {}
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 string pool string values.
270 ///
271 /// This class is used with the DW_FORM_strp and DW_FORM_GNU_str_index forms.
272 class DIEString {
274 
275 public:
277 
278  /// Grab the string out of the object.
279  StringRef getString() const { return S.getString(); }
280 
281  void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
282  unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
283 
284  void print(raw_ostream &O) const;
285 };
286 
287 //===--------------------------------------------------------------------===//
288 /// A container for inline string values.
289 ///
290 /// This class is used with the DW_FORM_string form.
292  StringRef S;
293 
294 public:
295  template <typename Allocator>
296  explicit DIEInlineString(StringRef Str, Allocator &A) : S(Str.copy(A)) {}
297 
298  ~DIEInlineString() = default;
299 
300  /// Grab the string out of the object.
301  StringRef getString() const { return S; }
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 /// A pointer to another debug information entry. An instance of this class can
311 /// also be used as a proxy for a debug information entry not yet defined
312 /// (ie. types.)
313 class DIEEntry {
314  DIE *Entry;
315 
316 public:
317  DIEEntry() = delete;
318  explicit DIEEntry(DIE &E) : Entry(&E) {}
319 
320  DIE &getEntry() const { return *Entry; }
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 /// Represents a pointer to a location list in the debug_loc
330 /// section.
331 class DIELocList {
332  /// Index into the .debug_loc vector.
333  size_t Index;
334 
335 public:
336  DIELocList(size_t I) : Index(I) {}
337 
338  /// Grab the current index out.
339  size_t getValue() const { return Index; }
340 
341  void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
342  unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
343 
344  void print(raw_ostream &O) const;
345 };
346 
347 //===--------------------------------------------------------------------===//
348 /// A debug information entry value. Some of these roughly correlate
349 /// to DWARF attribute classes.
350 class DIEBlock;
351 class DIELoc;
352 class DIEValue {
353 public:
354  enum Type {
356 #define HANDLE_DIEVALUE(T) is##T,
357 #include "llvm/CodeGen/DIEValue.def"
358  };
359 
360 private:
361  /// Type of data stored in the value.
362  Type Ty = isNone;
363  dwarf::Attribute Attribute = (dwarf::Attribute)0;
364  dwarf::Form Form = (dwarf::Form)0;
365 
366  /// Storage for the value.
367  ///
368  /// All values that aren't standard layout (or are larger than 8 bytes)
369  /// should be stored by reference instead of by value.
370  using ValTy = AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
371  DIEDelta *, DIEEntry, DIEBlock *,
372  DIELoc *, DIELocList, DIEBaseTypeRef *>;
373 
374  static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
375  sizeof(ValTy) <= sizeof(void *),
376  "Expected all large types to be stored via pointer");
377 
378  /// Underlying stored value.
379  ValTy Val;
380 
381  template <class T> void construct(T V) {
382  static_assert(std::is_standard_layout<T>::value ||
383  std::is_pointer<T>::value,
384  "Expected standard layout or pointer");
385  new (reinterpret_cast<void *>(Val.buffer)) T(V);
386  }
387 
388  template <class T> T *get() { return reinterpret_cast<T *>(Val.buffer); }
389  template <class T> const T *get() const {
390  return reinterpret_cast<const T *>(Val.buffer);
391  }
392  template <class T> void destruct() { get<T>()->~T(); }
393 
394  /// Destroy the underlying value.
395  ///
396  /// This should get optimized down to a no-op. We could skip it if we could
397  /// add a static assert on \a std::is_trivially_copyable(), but we currently
398  /// support versions of GCC that don't understand that.
399  void destroyVal() {
400  switch (Ty) {
401  case isNone:
402  return;
403 #define HANDLE_DIEVALUE_SMALL(T) \
404  case is##T: \
405  destruct<DIE##T>(); \
406  return;
407 #define HANDLE_DIEVALUE_LARGE(T) \
408  case is##T: \
409  destruct<const DIE##T *>(); \
410  return;
411 #include "llvm/CodeGen/DIEValue.def"
412  }
413  }
414 
415  /// Copy the underlying value.
416  ///
417  /// This should get optimized down to a simple copy. We need to actually
418  /// construct the value, rather than calling memcpy, to satisfy strict
419  /// aliasing rules.
420  void copyVal(const DIEValue &X) {
421  switch (Ty) {
422  case isNone:
423  return;
424 #define HANDLE_DIEVALUE_SMALL(T) \
425  case is##T: \
426  construct<DIE##T>(*X.get<DIE##T>()); \
427  return;
428 #define HANDLE_DIEVALUE_LARGE(T) \
429  case is##T: \
430  construct<const DIE##T *>(*X.get<const DIE##T *>()); \
431  return;
432 #include "llvm/CodeGen/DIEValue.def"
433  }
434  }
435 
436 public:
437  DIEValue() = default;
438 
439  DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
440  copyVal(X);
441  }
442 
444  destroyVal();
445  Ty = X.Ty;
446  Attribute = X.Attribute;
447  Form = X.Form;
448  copyVal(X);
449  return *this;
450  }
451 
452  ~DIEValue() { destroyVal(); }
453 
454 #define HANDLE_DIEVALUE_SMALL(T) \
455  DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T &V) \
456  : Ty(is##T), Attribute(Attribute), Form(Form) { \
457  construct<DIE##T>(V); \
458  }
459 #define HANDLE_DIEVALUE_LARGE(T) \
460  DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T *V) \
461  : Ty(is##T), Attribute(Attribute), Form(Form) { \
462  assert(V && "Expected valid value"); \
463  construct<const DIE##T *>(V); \
464  }
465 #include "llvm/CodeGen/DIEValue.def"
466 
467  /// Accessors.
468  /// @{
469  Type getType() const { return Ty; }
470  dwarf::Attribute getAttribute() const { return Attribute; }
471  dwarf::Form getForm() const { return Form; }
472  explicit operator bool() const { return Ty; }
473  /// @}
474 
475 #define HANDLE_DIEVALUE_SMALL(T) \
476  const DIE##T &getDIE##T() const { \
477  assert(getType() == is##T && "Expected " #T); \
478  return *get<DIE##T>(); \
479  }
480 #define HANDLE_DIEVALUE_LARGE(T) \
481  const DIE##T &getDIE##T() const { \
482  assert(getType() == is##T && "Expected " #T); \
483  return **get<const DIE##T *>(); \
484  }
485 #include "llvm/CodeGen/DIEValue.def"
486 
487  /// Emit value via the Dwarf writer.
488  void EmitValue(const AsmPrinter *AP) const;
489 
490  /// Return the size of a value in bytes.
491  unsigned SizeOf(const AsmPrinter *AP) const;
492 
493  void print(raw_ostream &O) const;
494  void dump() const;
495 };
496 
499 
500  IntrusiveBackListNode() : Next(this, true) {}
501 
503  return Next.getInt() ? nullptr : Next.getPointer();
504  }
505 };
506 
509 
510  Node *Last = nullptr;
511 
512  bool empty() const { return !Last; }
513 
514  void push_back(Node &N) {
515  assert(N.Next.getPointer() == &N && "Expected unlinked node");
516  assert(N.Next.getInt() == true && "Expected unlinked node");
517 
518  if (Last) {
519  N.Next = Last->Next;
520  Last->Next.setPointerAndInt(&N, false);
521  }
522  Last = &N;
523  }
524 
525  void push_front(Node &N) {
526  assert(N.Next.getPointer() == &N && "Expected unlinked node");
527  assert(N.Next.getInt() == true && "Expected unlinked node");
528 
529  if (Last) {
530  N.Next.setPointerAndInt(Last->Next.getPointer(), false);
531  Last->Next.setPointerAndInt(&N, true);
532  } else {
533  Last = &N;
534  }
535  }
536 };
537 
538 template <class T> class IntrusiveBackList : IntrusiveBackListBase {
539 public:
541 
544  T &back() { return *static_cast<T *>(Last); }
545  const T &back() const { return *static_cast<T *>(Last); }
546  T &front() {
547  return *static_cast<T *>(Last ? Last->Next.getPointer() : nullptr);
548  }
549  const T &front() const {
550  return *static_cast<T *>(Last ? Last->Next.getPointer() : nullptr);
551  }
552 
553  class const_iterator;
554  class iterator
555  : public iterator_facade_base<iterator, std::forward_iterator_tag, T> {
556  friend class const_iterator;
557 
558  Node *N = nullptr;
559 
560  public:
561  iterator() = default;
562  explicit iterator(T *N) : N(N) {}
563 
565  N = N->getNext();
566  return *this;
567  }
568 
569  explicit operator bool() const { return N; }
570  T &operator*() const { return *static_cast<T *>(N); }
571 
572  bool operator==(const iterator &X) const { return N == X.N; }
573  bool operator!=(const iterator &X) const { return N != X.N; }
574  };
575 
577  : public iterator_facade_base<const_iterator, std::forward_iterator_tag,
578  const T> {
579  const Node *N = nullptr;
580 
581  public:
582  const_iterator() = default;
583  // Placate MSVC by explicitly scoping 'iterator'.
585  explicit const_iterator(const T *N) : N(N) {}
586 
588  N = N->getNext();
589  return *this;
590  }
591 
592  explicit operator bool() const { return N; }
593  const T &operator*() const { return *static_cast<const T *>(N); }
594 
595  bool operator==(const const_iterator &X) const { return N == X.N; }
596  bool operator!=(const const_iterator &X) const { return N != X.N; }
597  };
598 
599  iterator begin() {
600  return Last ? iterator(static_cast<T *>(Last->Next.getPointer())) : end();
601  }
603  return const_cast<IntrusiveBackList *>(this)->begin();
604  }
605  iterator end() { return iterator(); }
606  const_iterator end() const { return const_iterator(); }
607 
608  static iterator toIterator(T &N) { return iterator(&N); }
609  static const_iterator toIterator(const T &N) { return const_iterator(&N); }
610 };
611 
612 /// A list of DIE values.
613 ///
614 /// This is a singly-linked list, but instead of reversing the order of
615 /// insertion, we keep a pointer to the back of the list so we can push in
616 /// order.
617 ///
618 /// There are two main reasons to choose a linked list over a customized
619 /// vector-like data structure.
620 ///
621 /// 1. For teardown efficiency, we want DIEs to be BumpPtrAllocated. Using a
622 /// linked list here makes this way easier to accomplish.
623 /// 2. Carrying an extra pointer per \a DIEValue isn't expensive. 45% of DIEs
624 /// have 2 or fewer values, and 90% have 5 or fewer. A vector would be
625 /// over-allocated by 50% on average anyway, the same cost as the
626 /// linked-list node.
628  struct Node : IntrusiveBackListNode {
629  DIEValue V;
630 
631  explicit Node(DIEValue V) : V(V) {}
632  };
633 
635 
636  ListTy List;
637 
638 public:
639  class const_value_iterator;
641  : public iterator_adaptor_base<value_iterator, ListTy::iterator,
642  std::forward_iterator_tag, DIEValue> {
643  friend class const_value_iterator;
644 
645  using iterator_adaptor =
646  iterator_adaptor_base<value_iterator, ListTy::iterator,
647  std::forward_iterator_tag, DIEValue>;
648 
649  public:
650  value_iterator() = default;
651  explicit value_iterator(ListTy::iterator X) : iterator_adaptor(X) {}
652 
653  explicit operator bool() const { return bool(wrapped()); }
654  DIEValue &operator*() const { return wrapped()->V; }
655  };
656 
658  const_value_iterator, ListTy::const_iterator,
659  std::forward_iterator_tag, const DIEValue> {
660  using iterator_adaptor =
661  iterator_adaptor_base<const_value_iterator, ListTy::const_iterator,
662  std::forward_iterator_tag, const DIEValue>;
663 
664  public:
665  const_value_iterator() = default;
667  : iterator_adaptor(X.wrapped()) {}
668  explicit const_value_iterator(ListTy::const_iterator X)
669  : iterator_adaptor(X) {}
670 
671  explicit operator bool() const { return bool(wrapped()); }
672  const DIEValue &operator*() const { return wrapped()->V; }
673  };
674 
677 
679  List.push_back(*new (Alloc) Node(V));
680  return value_iterator(ListTy::toIterator(List.back()));
681  }
682  template <class T>
684  dwarf::Form Form, T &&Value) {
685  return addValue(Alloc, DIEValue(Attribute, Form, std::forward<T>(Value)));
686  }
687 
689  return make_range(value_iterator(List.begin()), value_iterator(List.end()));
690  }
692  return make_range(const_value_iterator(List.begin()),
693  const_value_iterator(List.end()));
694  }
695 };
696 
697 //===--------------------------------------------------------------------===//
698 /// A structured debug information entry. Has an abbreviation which
699 /// describes its organization.
701  friend class IntrusiveBackList<DIE>;
702  friend class DIEUnit;
703 
704  /// Dwarf unit relative offset.
705  unsigned Offset = 0;
706  /// Size of instance + children.
707  unsigned Size = 0;
708  unsigned AbbrevNumber = ~0u;
709  /// Dwarf tag code.
711  /// Set to true to force a DIE to emit an abbreviation that says it has
712  /// children even when it doesn't. This is used for unit testing purposes.
713  bool ForceChildren = false;
714  /// Children DIEs.
715  IntrusiveBackList<DIE> Children;
716 
717  /// The owner is either the parent DIE for children of other DIEs, or a
718  /// DIEUnit which contains this DIE as its unit DIE.
720 
721  explicit DIE(dwarf::Tag Tag) : Tag(Tag) {}
722 
723 public:
724  DIE() = delete;
725  DIE(const DIE &RHS) = delete;
726  DIE(DIE &&RHS) = delete;
727  DIE &operator=(const DIE &RHS) = delete;
728  DIE &operator=(const DIE &&RHS) = delete;
729 
730  static DIE *get(BumpPtrAllocator &Alloc, dwarf::Tag Tag) {
731  return new (Alloc) DIE(Tag);
732  }
733 
734  // Accessors.
735  unsigned getAbbrevNumber() const { return AbbrevNumber; }
736  dwarf::Tag getTag() const { return Tag; }
737  /// Get the compile/type unit relative offset of this DIE.
738  unsigned getOffset() const { return Offset; }
739  unsigned getSize() const { return Size; }
740  bool hasChildren() const { return ForceChildren || !Children.empty(); }
741  void setForceChildren(bool B) { ForceChildren = B; }
742 
747 
749  return make_range(Children.begin(), Children.end());
750  }
752  return make_range(Children.begin(), Children.end());
753  }
754 
755  DIE *getParent() const;
756 
757  /// Generate the abbreviation for this DIE.
758  ///
759  /// Calculate the abbreviation for this, which should be uniqued and
760  /// eventually used to call \a setAbbrevNumber().
761  DIEAbbrev generateAbbrev() const;
762 
763  /// Set the abbreviation number for this DIE.
764  void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
765 
766  /// Get the absolute offset within the .debug_info or .debug_types section
767  /// for this DIE.
768  unsigned getDebugSectionOffset() const;
769 
770  /// Compute the offset of this DIE and all its children.
771  ///
772  /// This function gets called just before we are going to generate the debug
773  /// information and gives each DIE a chance to figure out its CU relative DIE
774  /// offset, unique its abbreviation and fill in the abbreviation code, and
775  /// return the unit offset that points to where the next DIE will be emitted
776  /// within the debug unit section. After this function has been called for all
777  /// DIE objects, the DWARF can be generated since all DIEs will be able to
778  /// properly refer to other DIE objects since all DIEs have calculated their
779  /// offsets.
780  ///
781  /// \param AP AsmPrinter to use when calculating sizes.
782  /// \param AbbrevSet the abbreviation used to unique DIE abbreviations.
783  /// \param CUOffset the compile/type unit relative offset in bytes.
784  /// \returns the offset for the DIE that follows this DIE within the
785  /// current compile/type unit.
786  unsigned computeOffsetsAndAbbrevs(const AsmPrinter *AP,
787  DIEAbbrevSet &AbbrevSet, unsigned CUOffset);
788 
789  /// Climb up the parent chain to get the compile unit or type unit DIE that
790  /// this DIE belongs to.
791  ///
792  /// \returns the compile or type unit DIE that owns this DIE, or NULL if
793  /// this DIE hasn't been added to a unit DIE.
794  const DIE *getUnitDie() const;
795 
796  /// Climb up the parent chain to get the compile unit or type unit that this
797  /// DIE belongs to.
798  ///
799  /// \returns the DIEUnit that represents the compile or type unit that owns
800  /// this DIE, or NULL if this DIE hasn't been added to a unit DIE.
801  DIEUnit *getUnit() const;
802 
803  void setOffset(unsigned O) { Offset = O; }
804  void setSize(unsigned S) { Size = S; }
805 
806  /// Add a child to the DIE.
807  DIE &addChild(DIE *Child) {
808  assert(!Child->getParent() && "Child should be orphaned");
809  Child->Owner = this;
810  Children.push_back(*Child);
811  return Children.back();
812  }
813 
814  DIE &addChildFront(DIE *Child) {
815  assert(!Child->getParent() && "Child should be orphaned");
816  Child->Owner = this;
817  Children.push_front(*Child);
818  return Children.front();
819  }
820 
821  /// Find a value in the DIE with the attribute given.
822  ///
823  /// Returns a default-constructed DIEValue (where \a DIEValue::getType()
824  /// gives \a DIEValue::isNone) if no such attribute exists.
825  DIEValue findAttribute(dwarf::Attribute Attribute) const;
826 
827  void print(raw_ostream &O, unsigned IndentCount = 0) const;
828  void dump() const;
829 };
830 
831 //===--------------------------------------------------------------------===//
832 /// Represents a compile or type unit.
833 class DIEUnit {
834  /// The compile unit or type unit DIE. This variable must be an instance of
835  /// DIE so that we can calculate the DIEUnit from any DIE by traversing the
836  /// parent backchain and getting the Unit DIE, and then casting itself to a
837  /// DIEUnit. This allows us to be able to find the DIEUnit for any DIE without
838  /// having to store a pointer to the DIEUnit in each DIE instance.
839  DIE Die;
840  /// The section this unit will be emitted in. This may or may not be set to
841  /// a valid section depending on the client that is emitting DWARF.
843  uint64_t Offset; /// .debug_info or .debug_types absolute section offset.
844  uint32_t Length; /// The length in bytes of all of the DIEs in this unit.
845  const uint16_t Version; /// The Dwarf version number for this unit.
846  const uint8_t AddrSize; /// The size in bytes of an address for this unit.
847 protected:
848  virtual ~DIEUnit() = default;
849 
850 public:
851  DIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag);
852  DIEUnit(const DIEUnit &RHS) = delete;
853  DIEUnit(DIEUnit &&RHS) = delete;
854  void operator=(const DIEUnit &RHS) = delete;
855  void operator=(const DIEUnit &&RHS) = delete;
856  /// Set the section that this DIEUnit will be emitted into.
857  ///
858  /// This function is used by some clients to set the section. Not all clients
859  /// that emit DWARF use this section variable.
860  void setSection(MCSection *Section) {
861  assert(!this->Section);
862  this->Section = Section;
863  }
864 
866  return nullptr;
867  }
868 
869  /// Return the section that this DIEUnit will be emitted into.
870  ///
871  /// \returns Section pointer which can be NULL.
872  MCSection *getSection() const { return Section; }
873  void setDebugSectionOffset(unsigned O) { Offset = O; }
874  unsigned getDebugSectionOffset() const { return Offset; }
875  void setLength(uint64_t L) { Length = L; }
876  uint64_t getLength() const { return Length; }
877  uint16_t getDwarfVersion() const { return Version; }
878  uint16_t getAddressSize() const { return AddrSize; }
879  DIE &getUnitDie() { return Die; }
880  const DIE &getUnitDie() const { return Die; }
881 };
882 
883 struct BasicDIEUnit final : DIEUnit {
884  BasicDIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag)
885  : DIEUnit(Version, AddrSize, UnitTag) {}
886 };
887 
888 //===--------------------------------------------------------------------===//
889 /// DIELoc - Represents an expression location.
890 //
891 class DIELoc : public DIEValueList {
892  mutable unsigned Size = 0; // Size in bytes excluding size header.
893 
894 public:
895  DIELoc() = default;
896 
897  /// ComputeSize - Calculate the size of the location expression.
898  ///
899  unsigned ComputeSize(const AsmPrinter *AP) const;
900 
901  /// BestForm - Choose the best form for data.
902  ///
903  dwarf::Form BestForm(unsigned DwarfVersion) const {
904  if (DwarfVersion > 3)
905  return dwarf::DW_FORM_exprloc;
906  // Pre-DWARF4 location expressions were blocks and not exprloc.
907  if ((unsigned char)Size == Size)
908  return dwarf::DW_FORM_block1;
909  if ((unsigned short)Size == Size)
910  return dwarf::DW_FORM_block2;
911  if ((unsigned int)Size == Size)
912  return dwarf::DW_FORM_block4;
913  return dwarf::DW_FORM_block;
914  }
915 
916  void EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const;
917  unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
918 
919  void print(raw_ostream &O) const;
920 };
921 
922 //===--------------------------------------------------------------------===//
923 /// DIEBlock - Represents a block of values.
924 //
925 class DIEBlock : public DIEValueList {
926  mutable unsigned Size = 0; // Size in bytes excluding size header.
927 
928 public:
929  DIEBlock() = default;
930 
931  /// ComputeSize - Calculate the size of the location expression.
932  ///
933  unsigned ComputeSize(const AsmPrinter *AP) const;
934 
935  /// BestForm - Choose the best form for data.
936  ///
938  if ((unsigned char)Size == Size)
939  return dwarf::DW_FORM_block1;
940  if ((unsigned short)Size == Size)
941  return dwarf::DW_FORM_block2;
942  if ((unsigned int)Size == Size)
943  return dwarf::DW_FORM_block4;
944  return dwarf::DW_FORM_block;
945  }
946 
947  void EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const;
948  unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
949 
950  void print(raw_ostream &O) const;
951 };
952 
953 } // end namespace llvm
954 
955 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
uint16_t getAddressSize() const
Definition: DIE.h:878
unsigned getSize() const
Definition: DIE.h:739
uint64_t CallInst * C
iterator begin()
Definition: DIE.h:599
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
const T & back() const
Definition: DIE.h:545
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
DIEString(DwarfStringPoolEntryRef S)
Definition: DIE.h:276
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
void setSize(unsigned S)
Definition: DIE.h:804
DIELoc - Represents an expression location.
Definition: DIE.h:891
This class represents lattice values for constants.
Definition: AllocatorList.h:23
PointerTy getPointer() const
iterator end()
Definition: DIE.h:605
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:41
const_child_range children() const
Definition: DIE.h:751
DIEValue(const DIEValue &X)
Definition: DIE.h:439
Attribute
Attributes.
Definition: Dwarf.h:102
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
bool operator!=(const const_iterator &X) const
Definition: DIE.h:596
child_range children()
Definition: DIE.h:748
static const_iterator toIterator(const T &N)
Definition: DIE.h:609
bool hasChildren() const
Definition: DIE.h:740
DIEAbbrev(dwarf::Tag T, bool C)
Definition: DIE.h:96
unsigned getDebugSectionOffset() const
Definition: DIE.h:874
const_value_iterator(DIEValueList::value_iterator X)
Definition: DIE.h:666
F(f)
block Block Frequency true
Node(Kind K_, Cache RHSComponentCache_=Cache::No, Cache ArrayCache_=Cache::No, Cache FunctionCache_=Cache::No)
const DIE & getUnitDie() const
Definition: DIE.h:880
void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form)
Adds another set of attribute information to the abbreviation.
Definition: DIE.h:109
Represents a pointer to a location list in the debug_loc section.
Definition: DIE.h:331
value_iterator addValue(BumpPtrAllocator &Alloc, dwarf::Attribute Attribute, dwarf::Form Form, T &&Value)
Definition: DIE.h:683
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
StringRef getString() const
Grab the string out of the object.
Definition: DIE.h:279
int64_t getValue() const
Definition: DIE.h:69
Dwarf abbreviation data, describes one attribute of a Dwarf abbreviation.
Definition: DIE.h:49
DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
Definition: DIE.h:260
size_t getValue() const
Grab the current index out.
Definition: DIE.h:339
void setForceChildren(bool B)
Definition: DIE.h:741
DIEInlineString(StringRef Str, Allocator &A)
Definition: DIE.h:296
DIE & addChildFront(DIE *Child)
Definition: DIE.h:814
const MCExpr * getValue() const
Get MCExpr.
Definition: DIE.h:208
void AddImplicitConstAttribute(dwarf::Attribute Attribute, int64_t Value)
Adds attribute with DW_FORM_implicit_const value.
Definition: DIE.h:114
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
dwarf::Form getForm() const
Definition: DIE.h:68
const_iterator & operator++()
Definition: DIE.h:587
void push_front(T &N)
Definition: DIE.h:543
const_iterator(typename IntrusiveBackList< T >::iterator X)
Definition: DIE.h:584
void setNumber(unsigned N)
Definition: DIE.h:105
IntType getInt() const
A list of DIE values.
Definition: DIE.h:627
bool operator!=(const iterator &X) const
Definition: DIE.h:573
DIE & addChild(DIE *Child)
Add a child to the DIE.
Definition: DIE.h:807
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:67
Helps unique DIEAbbrev objects and assigns abbreviation numbers.
Definition: DIE.h:135
void push_back(T &N)
Definition: DIE.h:542
~DIEValue()
Definition: DIE.h:452
DIE & getEntry() const
Definition: DIE.h:320
uint16_t getDwarfVersion() const
Definition: DIE.h:877
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:305
A pointer to another debug information entry.
Definition: DIE.h:313
virtual const MCSymbol * getCrossSectionRelativeBaseAddress() const
Definition: DIE.h:865
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
PointerIntPair - This class implements a pair of a pointer and small integer.
static iterator toIterator(T &N)
Definition: DIE.h:608
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:140
CRTP base class for adapting an iterator to a different type.
Definition: iterator.h:205
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
value_range values()
Definition: DIE.h:688
bool hasChildren() const
Definition: DIE.h:102
DIEAbbrevSet(BumpPtrAllocator &A)
Definition: DIE.h:145
const DIEValue & operator*() const
Definition: DIE.h:672
A container for inline string values.
Definition: DIE.h:291
A structured debug information entry.
Definition: DIE.h:700
void Profile(FoldingSetNodeID &ID) const
Used to gather unique data for the abbreviation folding set.
Definition: DIE.cpp:42
DIEBaseTypeRef(const DwarfCompileUnit *TheCU, uint64_t Idx)
Definition: DIE.h:241
DIE & getUnitDie()
Definition: DIE.h:879
static dwarf::Form BestForm(bool IsSigned, uint64_t Int)
Choose the best form for integer.
Definition: DIE.h:170
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
void push_back(Node &N)
Definition: DIE.h:514
An expression DIE.
Definition: DIE.h:201
dwarf::Attribute getAttribute() const
Accessors.
Definition: DIE.h:67
A BaseTypeRef DIE.
Definition: DIE.h:235
A label DIE.
Definition: DIE.h:218
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
DIELocList(size_t I)
Definition: DIE.h:336
const T & front() const
Definition: DIE.h:549
const MCSymbol * getValue() const
Get MCSymbol.
Definition: DIE.h:225
DIE * getParent() const
Definition: DIE.cpp:182
bool operator==(const const_iterator &X) const
Definition: DIE.h:595
A container for string pool string values.
Definition: DIE.h:272
void setDebugSectionOffset(unsigned O)
Definition: DIE.h:873
FoldingSet - This template class is used to instantiate a specialized implementation of the folding s...
Definition: FoldingSet.h:473
A simple label difference DIE.
Definition: DIE.h:255
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.
Basic Register Allocator
const_iterator begin() const
Definition: DIE.h:602
dwarf::Tag getTag() const
Accessors.
Definition: DIE.h:100
MCSection * getSection() const
Return the section that this DIEUnit will be emitted into.
Definition: DIE.h:872
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
DIEInteger(uint64_t I)
Definition: DIE.h:167
Represents a compile or type unit.
Definition: DIE.h:833
const_iterator end() const
Definition: DIE.h:606
A range adaptor for a pair of iterators.
This file contains constants used for implementing Dwarf debug support.
dwarf::Form BestForm(unsigned DwarfVersion) const
BestForm - Choose the best form for data.
Definition: DIE.h:903
DIEExpr(const MCExpr *E)
Definition: DIE.h:205
dwarf::Tag getTag() const
Definition: DIE.h:736
String pool entry reference.
uint64_t getValue() const
Definition: DIE.h:190
static constexpr unsigned ULEB128PadSize
Definition: DwarfDebug.cpp:170
DIEValue & operator=(const DIEValue &X)
Definition: DIE.h:443
const_value_range values() const
Definition: DIE.h:691
An integer value DIE.
Definition: DIE.h:163
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:860
DIEValue & operator*() const
Definition: DIE.h:654
Node - This class is used to maintain the singly linked bucket list in a folding set.
Definition: FoldingSet.h:135
dwarf::Form BestForm() const
BestForm - Choose the best form for data.
Definition: DIE.h:937
void setOffset(unsigned O)
Definition: DIE.h:803
const NodeList & List
Definition: RDFGraph.cpp:201
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
value_iterator(ListTy::iterator X)
Definition: DIE.h:651
uint64_t getLength() const
Definition: DIE.h:876
void setLength(uint64_t L)
Definition: DIE.h:875
PointerIntPair< IntrusiveBackListNode *, 1 > Next
Definition: DIE.h:498
const_value_iterator(ListTy::const_iterator X)
Definition: DIE.h:668
BasicDIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag)
Definition: DIE.h:884
uint32_t Size
Definition: Profile.cpp:46
void setChildrenFlag(bool hasChild)
Definition: DIE.h:104
bool operator==(const iterator &X) const
Definition: DIE.h:572
dwarf::Attribute getAttribute() const
Definition: DIE.h:470
DIEEntry(DIE &E)
Definition: DIE.h:318
unsigned getAbbrevNumber() const
Definition: DIE.h:735
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool empty() const
Definition: DIE.h:512
Type getType() const
Accessors.
Definition: DIE.h:469
const SmallVectorImpl< DIEAbbrevData > & getData() const
Definition: DIE.h:103
LLVM Value Representation.
Definition: Value.h:73
unsigned getNumber() const
Definition: DIE.h:101
static const Function * getParent(const Value *V)
void setAbbrevNumber(unsigned I)
Set the abbreviation number for this DIE.
Definition: DIE.h:764
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
unsigned getOffset() const
Get the compile/type unit relative offset of this DIE.
Definition: DIE.h:738
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
DIEAbbrevData(dwarf::Attribute A, dwarf::Form F)
Definition: DIE.h:60
DIEBlock - Represents a block of values.
Definition: DIE.h:925
dwarf::Form getForm() const
Definition: DIE.h:471
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1217
void push_front(Node &N)
Definition: DIE.h:525
const uint64_t Version
Definition: InstrProf.h:984
IntrusiveBackListNode * getNext() const
Definition: DIE.h:502
value_iterator addValue(BumpPtrAllocator &Alloc, const DIEValue &V)
Definition: DIE.h:678
void setValue(uint64_t Val)
Definition: DIE.h:191
StringRef getString() const
Grab the string out of the object.
Definition: DIE.h:301
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:156