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