Recommit [NFC] Better encapsulation of llvm::Optional Storage
[llvm-complete.git] / include / llvm / CodeGen / DIE.h
blob9e7167fd88c71ba0045b129ecd46d8c34078145b
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.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
14 #define LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
16 #include "llvm/ADT/FoldingSet.h"
17 #include "llvm/ADT/PointerIntPair.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"
22 #include "llvm/ADT/iterator_range.h"
23 #include "llvm/BinaryFormat/Dwarf.h"
24 #include "llvm/CodeGen/DwarfStringPoolEntry.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>
36 namespace llvm {
38 class AsmPrinter;
39 class DIE;
40 class DIEUnit;
41 class MCExpr;
42 class MCSection;
43 class MCSymbol;
44 class raw_ostream;
46 //===--------------------------------------------------------------------===//
47 /// Dwarf abbreviation data, describes one attribute of a Dwarf abbreviation.
48 class DIEAbbrevData {
49 /// Dwarf attribute code.
50 dwarf::Attribute Attribute;
52 /// Dwarf form code.
53 dwarf::Form Form;
55 /// Dwarf attribute value for DW_FORM_implicit_const
56 int64_t Value = 0;
58 public:
59 DIEAbbrevData(dwarf::Attribute A, dwarf::Form F)
60 : Attribute(A), Form(F) {}
61 DIEAbbrevData(dwarf::Attribute A, int64_t V)
62 : Attribute(A), Form(dwarf::DW_FORM_implicit_const), Value(V) {}
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 /// @}
71 /// Used to gather unique data for the abbreviation folding set.
72 void Profile(FoldingSetNodeID &ID) const;
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;
82 /// Dwarf tag code.
83 dwarf::Tag Tag;
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;
91 /// Raw data bytes for abbreviation.
92 SmallVector<DIEAbbrevData, 12> Data;
94 public:
95 DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C) {}
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 /// @}
107 /// Adds another set of attribute information to the abbreviation.
108 void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
109 Data.push_back(DIEAbbrevData(Attribute, Form));
112 /// Adds attribute with DW_FORM_implicit_const value
113 void AddImplicitConstAttribute(dwarf::Attribute Attribute, int64_t Value) {
114 Data.push_back(DIEAbbrevData(Attribute, Value));
117 /// Used to gather unique data for the abbreviation folding set.
118 void Profile(FoldingSetNodeID &ID) const;
120 /// Print the abbreviation using the specified asm printer.
121 void Emit(const AsmPrinter *AP) const;
123 void print(raw_ostream &O) const;
124 void dump() const;
127 //===--------------------------------------------------------------------===//
128 /// Helps unique DIEAbbrev objects and assigns abbreviation numbers.
130 /// This class will unique the DIE abbreviations for a llvm::DIE object and
131 /// assign a unique abbreviation number to each unique DIEAbbrev object it
132 /// finds. The resulting collection of DIEAbbrev objects can then be emitted
133 /// into the .debug_abbrev section.
134 class DIEAbbrevSet {
135 /// The bump allocator to use when creating DIEAbbrev objects in the uniqued
136 /// storage container.
137 BumpPtrAllocator &Alloc;
138 /// FoldingSet that uniques the abbreviations.
139 FoldingSet<DIEAbbrev> AbbreviationsSet;
140 /// A list of all the unique abbreviations in use.
141 std::vector<DIEAbbrev *> Abbreviations;
143 public:
144 DIEAbbrevSet(BumpPtrAllocator &A) : Alloc(A) {}
145 ~DIEAbbrevSet();
147 /// Generate the abbreviation declaration for a DIE and return a pointer to
148 /// the generated abbreviation.
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);
155 /// Print all abbreviations using the specified asm printer.
156 void Emit(const AsmPrinter *AP, MCSection *Section) const;
159 //===--------------------------------------------------------------------===//
160 /// An integer value DIE.
162 class DIEInteger {
163 uint64_t Integer;
165 public:
166 explicit DIEInteger(uint64_t I) : Integer(I) {}
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;
186 return dwarf::DW_FORM_data8;
189 uint64_t getValue() const { return Integer; }
190 void setValue(uint64_t Val) { Integer = Val; }
192 void EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const;
193 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
195 void print(raw_ostream &O) const;
198 //===--------------------------------------------------------------------===//
199 /// An expression DIE.
200 class DIEExpr {
201 const MCExpr *Expr;
203 public:
204 explicit DIEExpr(const MCExpr *E) : Expr(E) {}
206 /// Get MCExpr.
207 const MCExpr *getValue() const { return Expr; }
209 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
210 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
212 void print(raw_ostream &O) const;
215 //===--------------------------------------------------------------------===//
216 /// A label DIE.
217 class DIELabel {
218 const MCSymbol *Label;
220 public:
221 explicit DIELabel(const MCSymbol *L) : Label(L) {}
223 /// Get MCSymbol.
224 const MCSymbol *getValue() const { return Label; }
226 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
227 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
229 void print(raw_ostream &O) const;
232 //===--------------------------------------------------------------------===//
233 /// A simple label difference DIE.
235 class DIEDelta {
236 const MCSymbol *LabelHi;
237 const MCSymbol *LabelLo;
239 public:
240 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo) : LabelHi(Hi), LabelLo(Lo) {}
242 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
243 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
245 void print(raw_ostream &O) const;
248 //===--------------------------------------------------------------------===//
249 /// A container for string pool string values.
251 /// This class is used with the DW_FORM_strp and DW_FORM_GNU_str_index forms.
252 class DIEString {
253 DwarfStringPoolEntryRef S;
255 public:
256 DIEString(DwarfStringPoolEntryRef S) : S(S) {}
258 /// Grab the string out of the object.
259 StringRef getString() const { return S.getString(); }
261 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
262 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
264 void print(raw_ostream &O) const;
267 //===--------------------------------------------------------------------===//
268 /// A container for inline string values.
270 /// This class is used with the DW_FORM_string form.
271 class DIEInlineString {
272 StringRef S;
274 public:
275 template <typename Allocator>
276 explicit DIEInlineString(StringRef Str, Allocator &A) : S(Str.copy(A)) {}
278 ~DIEInlineString() = default;
280 /// Grab the string out of the object.
281 StringRef getString() const { return S; }
283 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
284 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
286 void print(raw_ostream &O) const;
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;
296 public:
297 DIEEntry() = delete;
298 explicit DIEEntry(DIE &E) : Entry(&E) {}
300 DIE &getEntry() const { return *Entry; }
302 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
303 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
305 void print(raw_ostream &O) const;
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;
315 public:
316 DIELocList(size_t I) : Index(I) {}
318 /// Grab the current index out.
319 size_t getValue() const { return Index; }
321 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
322 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
324 void print(raw_ostream &O) const;
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 {
335 isNone,
336 #define HANDLE_DIEVALUE(T) is##T,
337 #include "llvm/CodeGen/DIEValue.def"
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;
346 /// Storage for the value.
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>;
354 static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
355 sizeof(ValTy) <= sizeof(void *),
356 "Expected all large types to be stored via pointer");
358 /// Underlying stored value.
359 ValTy Val;
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);
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);
372 template <class T> void destruct() { get<T>()->~T(); }
374 /// Destroy the underlying value.
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"
395 /// Copy the underlying value.
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"
416 public:
417 DIEValue() = default;
419 DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
420 copyVal(X);
423 DIEValue &operator=(const DIEValue &X) {
424 destroyVal();
425 Ty = X.Ty;
426 Attribute = X.Attribute;
427 Form = X.Form;
428 copyVal(X);
429 return *this;
432 ~DIEValue() { destroyVal(); }
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); \
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); \
445 #include "llvm/CodeGen/DIEValue.def"
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 /// @}
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>(); \
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 *>(); \
465 #include "llvm/CodeGen/DIEValue.def"
467 /// Emit value via the Dwarf writer.
468 void EmitValue(const AsmPrinter *AP) const;
470 /// Return the size of a value in bytes.
471 unsigned SizeOf(const AsmPrinter *AP) const;
473 void print(raw_ostream &O) const;
474 void dump() const;
477 struct IntrusiveBackListNode {
478 PointerIntPair<IntrusiveBackListNode *, 1> Next;
480 IntrusiveBackListNode() : Next(this, true) {}
482 IntrusiveBackListNode *getNext() const {
483 return Next.getInt() ? nullptr : Next.getPointer();
487 struct IntrusiveBackListBase {
488 using Node = IntrusiveBackListNode;
490 Node *Last = nullptr;
492 bool empty() const { return !Last; }
494 void push_back(Node &N) {
495 assert(N.Next.getPointer() == &N && "Expected unlinked node");
496 assert(N.Next.getInt() == true && "Expected unlinked node");
498 if (Last) {
499 N.Next = Last->Next;
500 Last->Next.setPointerAndInt(&N, false);
502 Last = &N;
506 template <class T> class IntrusiveBackList : IntrusiveBackListBase {
507 public:
508 using IntrusiveBackListBase::empty;
510 void push_back(T &N) { IntrusiveBackListBase::push_back(N); }
511 T &back() { return *static_cast<T *>(Last); }
512 const T &back() const { return *static_cast<T *>(Last); }
514 class const_iterator;
515 class iterator
516 : public iterator_facade_base<iterator, std::forward_iterator_tag, T> {
517 friend class const_iterator;
519 Node *N = nullptr;
521 public:
522 iterator() = default;
523 explicit iterator(T *N) : N(N) {}
525 iterator &operator++() {
526 N = N->getNext();
527 return *this;
530 explicit operator bool() const { return N; }
531 T &operator*() const { return *static_cast<T *>(N); }
533 bool operator==(const iterator &X) const { return N == X.N; }
534 bool operator!=(const iterator &X) const { return N != X.N; }
537 class const_iterator
538 : public iterator_facade_base<const_iterator, std::forward_iterator_tag,
539 const T> {
540 const Node *N = nullptr;
542 public:
543 const_iterator() = default;
544 // Placate MSVC by explicitly scoping 'iterator'.
545 const_iterator(typename IntrusiveBackList<T>::iterator X) : N(X.N) {}
546 explicit const_iterator(const T *N) : N(N) {}
548 const_iterator &operator++() {
549 N = N->getNext();
550 return *this;
553 explicit operator bool() const { return N; }
554 const T &operator*() const { return *static_cast<const T *>(N); }
556 bool operator==(const const_iterator &X) const { return N == X.N; }
557 bool operator!=(const const_iterator &X) const { return N != X.N; }
560 iterator begin() {
561 return Last ? iterator(static_cast<T *>(Last->Next.getPointer())) : end();
563 const_iterator begin() const {
564 return const_cast<IntrusiveBackList *>(this)->begin();
566 iterator end() { return iterator(); }
567 const_iterator end() const { return const_iterator(); }
569 static iterator toIterator(T &N) { return iterator(&N); }
570 static const_iterator toIterator(const T &N) { return const_iterator(&N); }
573 /// A list of DIE values.
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.
579 /// There are two main reasons to choose a linked list over a customized
580 /// vector-like data structure.
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.
588 class DIEValueList {
589 struct Node : IntrusiveBackListNode {
590 DIEValue V;
592 explicit Node(DIEValue V) : V(V) {}
595 using ListTy = IntrusiveBackList<Node>;
597 ListTy List;
599 public:
600 class const_value_iterator;
601 class value_iterator
602 : public iterator_adaptor_base<value_iterator, ListTy::iterator,
603 std::forward_iterator_tag, DIEValue> {
604 friend class const_value_iterator;
606 using iterator_adaptor =
607 iterator_adaptor_base<value_iterator, ListTy::iterator,
608 std::forward_iterator_tag, DIEValue>;
610 public:
611 value_iterator() = default;
612 explicit value_iterator(ListTy::iterator X) : iterator_adaptor(X) {}
614 explicit operator bool() const { return bool(wrapped()); }
615 DIEValue &operator*() const { return wrapped()->V; }
618 class const_value_iterator : public iterator_adaptor_base<
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>;
625 public:
626 const_value_iterator() = default;
627 const_value_iterator(DIEValueList::value_iterator X)
628 : iterator_adaptor(X.wrapped()) {}
629 explicit const_value_iterator(ListTy::const_iterator X)
630 : iterator_adaptor(X) {}
632 explicit operator bool() const { return bool(wrapped()); }
633 const DIEValue &operator*() const { return wrapped()->V; }
636 using value_range = iterator_range<value_iterator>;
637 using const_value_range = iterator_range<const_value_iterator>;
639 value_iterator addValue(BumpPtrAllocator &Alloc, const DIEValue &V) {
640 List.push_back(*new (Alloc) Node(V));
641 return value_iterator(ListTy::toIterator(List.back()));
643 template <class T>
644 value_iterator addValue(BumpPtrAllocator &Alloc, dwarf::Attribute Attribute,
645 dwarf::Form Form, T &&Value) {
646 return addValue(Alloc, DIEValue(Attribute, Form, std::forward<T>(Value)));
649 value_range values() {
650 return make_range(value_iterator(List.begin()), value_iterator(List.end()));
652 const_value_range values() const {
653 return make_range(const_value_iterator(List.begin()),
654 const_value_iterator(List.end()));
658 //===--------------------------------------------------------------------===//
659 /// A structured debug information entry. Has an abbreviation which
660 /// describes its organization.
661 class DIE : IntrusiveBackListNode, public DIEValueList {
662 friend class IntrusiveBackList<DIE>;
663 friend class DIEUnit;
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.
671 dwarf::Tag Tag = (dwarf::Tag)0;
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;
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.
680 PointerUnion<DIE *, DIEUnit *> Owner;
682 explicit DIE(dwarf::Tag Tag) : Tag(Tag) {}
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;
691 static DIE *get(BumpPtrAllocator &Alloc, dwarf::Tag Tag) {
692 return new (Alloc) DIE(Tag);
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; }
704 using child_iterator = IntrusiveBackList<DIE>::iterator;
705 using const_child_iterator = IntrusiveBackList<DIE>::const_iterator;
706 using child_range = iterator_range<child_iterator>;
707 using const_child_range = iterator_range<const_child_iterator>;
709 child_range children() {
710 return make_range(Children.begin(), Children.end());
712 const_child_range children() const {
713 return make_range(Children.begin(), Children.end());
716 DIE *getParent() const;
718 /// Generate the abbreviation for this DIE.
720 /// Calculate the abbreviation for this, which should be uniqued and
721 /// eventually used to call \a setAbbrevNumber().
722 DIEAbbrev generateAbbrev() const;
724 /// Set the abbreviation number for this DIE.
725 void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
727 /// Get the absolute offset within the .debug_info or .debug_types section
728 /// for this DIE.
729 unsigned getDebugSectionOffset() const;
731 /// Compute the offset of this DIE and all its children.
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.
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);
750 /// Climb up the parent chain to get the compile unit or type unit DIE that
751 /// this DIE belongs to.
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;
757 /// Climb up the parent chain to get the compile unit or type unit that this
758 /// DIE belongs to.
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;
764 void setOffset(unsigned O) { Offset = O; }
765 void setSize(unsigned S) { Size = S; }
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();
775 /// Find a value in the DIE with the attribute given.
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;
781 void print(raw_ostream &O, unsigned IndentCount = 0) const;
782 void dump() const;
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.
796 MCSection *Section;
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;
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.
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;
819 virtual const MCSymbol *getCrossSectionRelativeBaseAddress() const {
820 return nullptr;
823 /// Return the section that this DIEUnit will be emitted into.
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; }
837 struct BasicDIEUnit final : DIEUnit {
838 BasicDIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag)
839 : DIEUnit(Version, AddrSize, UnitTag) {}
842 //===--------------------------------------------------------------------===//
843 /// DIELoc - Represents an expression location.
845 class DIELoc : public DIEValueList {
846 mutable unsigned Size = 0; // Size in bytes excluding size header.
848 public:
849 DIELoc() = default;
851 /// ComputeSize - Calculate the size of the location expression.
853 unsigned ComputeSize(const AsmPrinter *AP) const;
855 /// BestForm - Choose the best form for data.
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;
870 void EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const;
871 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
873 void print(raw_ostream &O) const;
876 //===--------------------------------------------------------------------===//
877 /// DIEBlock - Represents a block of values.
879 class DIEBlock : public DIEValueList {
880 mutable unsigned Size = 0; // Size in bytes excluding size header.
882 public:
883 DIEBlock() = default;
885 /// ComputeSize - Calculate the size of the location expression.
887 unsigned ComputeSize(const AsmPrinter *AP) const;
889 /// BestForm - Choose the best form for data.
891 dwarf::Form BestForm() const {
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;
901 void EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const;
902 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
904 void print(raw_ostream &O) const;
907 } // end namespace llvm
909 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H