[Alignment][NFC] Support compile time constants
[llvm-core.git] / include / llvm / CodeGen / DIE.h
blob684f9e40ca5ab525e487d34f7e1198c8ee4467c7
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 DwarfCompileUnit;
42 class MCExpr;
43 class MCSection;
44 class MCSymbol;
45 class raw_ostream;
47 //===--------------------------------------------------------------------===//
48 /// Dwarf abbreviation data, describes one attribute of a Dwarf abbreviation.
49 class DIEAbbrevData {
50 /// Dwarf attribute code.
51 dwarf::Attribute Attribute;
53 /// Dwarf form code.
54 dwarf::Form Form;
56 /// Dwarf attribute value for DW_FORM_implicit_const
57 int64_t Value = 0;
59 public:
60 DIEAbbrevData(dwarf::Attribute A, dwarf::Form F)
61 : Attribute(A), Form(F) {}
62 DIEAbbrevData(dwarf::Attribute A, int64_t V)
63 : Attribute(A), Form(dwarf::DW_FORM_implicit_const), Value(V) {}
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 /// @}
72 /// Used to gather unique data for the abbreviation folding set.
73 void Profile(FoldingSetNodeID &ID) const;
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;
83 /// Dwarf tag code.
84 dwarf::Tag Tag;
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;
92 /// Raw data bytes for abbreviation.
93 SmallVector<DIEAbbrevData, 12> Data;
95 public:
96 DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C) {}
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 /// @}
108 /// Adds another set of attribute information to the abbreviation.
109 void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
110 Data.push_back(DIEAbbrevData(Attribute, Form));
113 /// Adds attribute with DW_FORM_implicit_const value
114 void AddImplicitConstAttribute(dwarf::Attribute Attribute, int64_t Value) {
115 Data.push_back(DIEAbbrevData(Attribute, Value));
118 /// Used to gather unique data for the abbreviation folding set.
119 void Profile(FoldingSetNodeID &ID) const;
121 /// Print the abbreviation using the specified asm printer.
122 void Emit(const AsmPrinter *AP) const;
124 void print(raw_ostream &O) const;
125 void dump() const;
128 //===--------------------------------------------------------------------===//
129 /// Helps unique DIEAbbrev objects and assigns abbreviation numbers.
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.
135 class DIEAbbrevSet {
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;
144 public:
145 DIEAbbrevSet(BumpPtrAllocator &A) : Alloc(A) {}
146 ~DIEAbbrevSet();
148 /// Generate the abbreviation declaration for a DIE and return a pointer to
149 /// the generated abbreviation.
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);
156 /// Print all abbreviations using the specified asm printer.
157 void Emit(const AsmPrinter *AP, MCSection *Section) const;
160 //===--------------------------------------------------------------------===//
161 /// An integer value DIE.
163 class DIEInteger {
164 uint64_t Integer;
166 public:
167 explicit DIEInteger(uint64_t I) : Integer(I) {}
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;
187 return dwarf::DW_FORM_data8;
190 uint64_t getValue() const { return Integer; }
191 void setValue(uint64_t Val) { Integer = Val; }
193 void EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const;
194 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
196 void print(raw_ostream &O) const;
199 //===--------------------------------------------------------------------===//
200 /// An expression DIE.
201 class DIEExpr {
202 const MCExpr *Expr;
204 public:
205 explicit DIEExpr(const MCExpr *E) : Expr(E) {}
207 /// Get MCExpr.
208 const MCExpr *getValue() const { return Expr; }
210 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
211 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
213 void print(raw_ostream &O) const;
216 //===--------------------------------------------------------------------===//
217 /// A label DIE.
218 class DIELabel {
219 const MCSymbol *Label;
221 public:
222 explicit DIELabel(const MCSymbol *L) : Label(L) {}
224 /// Get MCSymbol.
225 const MCSymbol *getValue() const { return Label; }
227 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
228 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
230 void print(raw_ostream &O) const;
233 //===--------------------------------------------------------------------===//
234 /// A BaseTypeRef DIE.
235 class DIEBaseTypeRef {
236 const DwarfCompileUnit *CU;
237 const uint64_t Index;
238 static constexpr unsigned ULEB128PadSize = 4;
240 public:
241 explicit DIEBaseTypeRef(const DwarfCompileUnit *TheCU, uint64_t Idx)
242 : CU(TheCU), Index(Idx) {}
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;
249 void print(raw_ostream &O) const;
252 //===--------------------------------------------------------------------===//
253 /// A simple label difference DIE.
255 class DIEDelta {
256 const MCSymbol *LabelHi;
257 const MCSymbol *LabelLo;
259 public:
260 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo) : LabelHi(Hi), LabelLo(Lo) {}
262 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
263 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
265 void print(raw_ostream &O) const;
268 //===--------------------------------------------------------------------===//
269 /// A container for string pool string values.
271 /// This class is used with the DW_FORM_strp and DW_FORM_GNU_str_index forms.
272 class DIEString {
273 DwarfStringPoolEntryRef S;
275 public:
276 DIEString(DwarfStringPoolEntryRef S) : S(S) {}
278 /// Grab the string out of the object.
279 StringRef getString() const { return S.getString(); }
281 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
282 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
284 void print(raw_ostream &O) const;
287 //===--------------------------------------------------------------------===//
288 /// A container for inline string values.
290 /// This class is used with the DW_FORM_string form.
291 class DIEInlineString {
292 StringRef S;
294 public:
295 template <typename Allocator>
296 explicit DIEInlineString(StringRef Str, Allocator &A) : S(Str.copy(A)) {}
298 ~DIEInlineString() = default;
300 /// Grab the string out of the object.
301 StringRef getString() const { return S; }
303 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
304 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
306 void print(raw_ostream &O) const;
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;
316 public:
317 DIEEntry() = delete;
318 explicit DIEEntry(DIE &E) : Entry(&E) {}
320 DIE &getEntry() const { return *Entry; }
322 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
323 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
325 void print(raw_ostream &O) const;
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;
335 public:
336 DIELocList(size_t I) : Index(I) {}
338 /// Grab the current index out.
339 size_t getValue() const { return Index; }
341 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
342 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
344 void print(raw_ostream &O) const;
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 {
355 isNone,
356 #define HANDLE_DIEVALUE(T) is##T,
357 #include "llvm/CodeGen/DIEValue.def"
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;
366 /// Storage for the value.
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 *>;
374 static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
375 sizeof(ValTy) <= sizeof(void *),
376 "Expected all large types to be stored via pointer");
378 /// Underlying stored value.
379 ValTy Val;
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);
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);
392 template <class T> void destruct() { get<T>()->~T(); }
394 /// Destroy the underlying value.
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"
415 /// Copy the underlying value.
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"
436 public:
437 DIEValue() = default;
439 DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
440 copyVal(X);
443 DIEValue &operator=(const DIEValue &X) {
444 destroyVal();
445 Ty = X.Ty;
446 Attribute = X.Attribute;
447 Form = X.Form;
448 copyVal(X);
449 return *this;
452 ~DIEValue() { destroyVal(); }
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); \
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); \
465 #include "llvm/CodeGen/DIEValue.def"
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 /// @}
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>(); \
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 *>(); \
485 #include "llvm/CodeGen/DIEValue.def"
487 /// Emit value via the Dwarf writer.
488 void EmitValue(const AsmPrinter *AP) const;
490 /// Return the size of a value in bytes.
491 unsigned SizeOf(const AsmPrinter *AP) const;
493 void print(raw_ostream &O) const;
494 void dump() const;
497 struct IntrusiveBackListNode {
498 PointerIntPair<IntrusiveBackListNode *, 1> Next;
500 IntrusiveBackListNode() : Next(this, true) {}
502 IntrusiveBackListNode *getNext() const {
503 return Next.getInt() ? nullptr : Next.getPointer();
507 struct IntrusiveBackListBase {
508 using Node = IntrusiveBackListNode;
510 Node *Last = nullptr;
512 bool empty() const { return !Last; }
514 void push_back(Node &N) {
515 assert(N.Next.getPointer() == &N && "Expected unlinked node");
516 assert(N.Next.getInt() == true && "Expected unlinked node");
518 if (Last) {
519 N.Next = Last->Next;
520 Last->Next.setPointerAndInt(&N, false);
522 Last = &N;
525 void push_front(Node &N) {
526 assert(N.Next.getPointer() == &N && "Expected unlinked node");
527 assert(N.Next.getInt() == true && "Expected unlinked node");
529 if (Last) {
530 N.Next.setPointerAndInt(Last->Next.getPointer(), false);
531 Last->Next.setPointerAndInt(&N, true);
532 } else {
533 Last = &N;
538 template <class T> class IntrusiveBackList : IntrusiveBackListBase {
539 public:
540 using IntrusiveBackListBase::empty;
542 void push_back(T &N) { IntrusiveBackListBase::push_back(N); }
543 void push_front(T &N) { IntrusiveBackListBase::push_front(N); }
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);
549 const T &front() const {
550 return *static_cast<T *>(Last ? Last->Next.getPointer() : nullptr);
553 class const_iterator;
554 class iterator
555 : public iterator_facade_base<iterator, std::forward_iterator_tag, T> {
556 friend class const_iterator;
558 Node *N = nullptr;
560 public:
561 iterator() = default;
562 explicit iterator(T *N) : N(N) {}
564 iterator &operator++() {
565 N = N->getNext();
566 return *this;
569 explicit operator bool() const { return N; }
570 T &operator*() const { return *static_cast<T *>(N); }
572 bool operator==(const iterator &X) const { return N == X.N; }
573 bool operator!=(const iterator &X) const { return N != X.N; }
576 class const_iterator
577 : public iterator_facade_base<const_iterator, std::forward_iterator_tag,
578 const T> {
579 const Node *N = nullptr;
581 public:
582 const_iterator() = default;
583 // Placate MSVC by explicitly scoping 'iterator'.
584 const_iterator(typename IntrusiveBackList<T>::iterator X) : N(X.N) {}
585 explicit const_iterator(const T *N) : N(N) {}
587 const_iterator &operator++() {
588 N = N->getNext();
589 return *this;
592 explicit operator bool() const { return N; }
593 const T &operator*() const { return *static_cast<const T *>(N); }
595 bool operator==(const const_iterator &X) const { return N == X.N; }
596 bool operator!=(const const_iterator &X) const { return N != X.N; }
599 iterator begin() {
600 return Last ? iterator(static_cast<T *>(Last->Next.getPointer())) : end();
602 const_iterator begin() const {
603 return const_cast<IntrusiveBackList *>(this)->begin();
605 iterator end() { return iterator(); }
606 const_iterator end() const { return const_iterator(); }
608 static iterator toIterator(T &N) { return iterator(&N); }
609 static const_iterator toIterator(const T &N) { return const_iterator(&N); }
612 /// A list of DIE values.
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.
618 /// There are two main reasons to choose a linked list over a customized
619 /// vector-like data structure.
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.
627 class DIEValueList {
628 struct Node : IntrusiveBackListNode {
629 DIEValue V;
631 explicit Node(DIEValue V) : V(V) {}
634 using ListTy = IntrusiveBackList<Node>;
636 ListTy List;
638 public:
639 class const_value_iterator;
640 class value_iterator
641 : public iterator_adaptor_base<value_iterator, ListTy::iterator,
642 std::forward_iterator_tag, DIEValue> {
643 friend class const_value_iterator;
645 using iterator_adaptor =
646 iterator_adaptor_base<value_iterator, ListTy::iterator,
647 std::forward_iterator_tag, DIEValue>;
649 public:
650 value_iterator() = default;
651 explicit value_iterator(ListTy::iterator X) : iterator_adaptor(X) {}
653 explicit operator bool() const { return bool(wrapped()); }
654 DIEValue &operator*() const { return wrapped()->V; }
657 class const_value_iterator : public iterator_adaptor_base<
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>;
664 public:
665 const_value_iterator() = default;
666 const_value_iterator(DIEValueList::value_iterator X)
667 : iterator_adaptor(X.wrapped()) {}
668 explicit const_value_iterator(ListTy::const_iterator X)
669 : iterator_adaptor(X) {}
671 explicit operator bool() const { return bool(wrapped()); }
672 const DIEValue &operator*() const { return wrapped()->V; }
675 using value_range = iterator_range<value_iterator>;
676 using const_value_range = iterator_range<const_value_iterator>;
678 value_iterator addValue(BumpPtrAllocator &Alloc, const DIEValue &V) {
679 List.push_back(*new (Alloc) Node(V));
680 return value_iterator(ListTy::toIterator(List.back()));
682 template <class T>
683 value_iterator addValue(BumpPtrAllocator &Alloc, dwarf::Attribute Attribute,
684 dwarf::Form Form, T &&Value) {
685 return addValue(Alloc, DIEValue(Attribute, Form, std::forward<T>(Value)));
688 value_range values() {
689 return make_range(value_iterator(List.begin()), value_iterator(List.end()));
691 const_value_range values() const {
692 return make_range(const_value_iterator(List.begin()),
693 const_value_iterator(List.end()));
697 //===--------------------------------------------------------------------===//
698 /// A structured debug information entry. Has an abbreviation which
699 /// describes its organization.
700 class DIE : IntrusiveBackListNode, public DIEValueList {
701 friend class IntrusiveBackList<DIE>;
702 friend class DIEUnit;
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.
710 dwarf::Tag Tag = (dwarf::Tag)0;
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;
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.
719 PointerUnion<DIE *, DIEUnit *> Owner;
721 explicit DIE(dwarf::Tag Tag) : Tag(Tag) {}
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;
730 static DIE *get(BumpPtrAllocator &Alloc, dwarf::Tag Tag) {
731 return new (Alloc) DIE(Tag);
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; }
743 using child_iterator = IntrusiveBackList<DIE>::iterator;
744 using const_child_iterator = IntrusiveBackList<DIE>::const_iterator;
745 using child_range = iterator_range<child_iterator>;
746 using const_child_range = iterator_range<const_child_iterator>;
748 child_range children() {
749 return make_range(Children.begin(), Children.end());
751 const_child_range children() const {
752 return make_range(Children.begin(), Children.end());
755 DIE *getParent() const;
757 /// Generate the abbreviation for this DIE.
759 /// Calculate the abbreviation for this, which should be uniqued and
760 /// eventually used to call \a setAbbrevNumber().
761 DIEAbbrev generateAbbrev() const;
763 /// Set the abbreviation number for this DIE.
764 void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
766 /// Get the absolute offset within the .debug_info or .debug_types section
767 /// for this DIE.
768 unsigned getDebugSectionOffset() const;
770 /// Compute the offset of this DIE and all its children.
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.
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);
789 /// Climb up the parent chain to get the compile unit or type unit DIE that
790 /// this DIE belongs to.
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;
796 /// Climb up the parent chain to get the compile unit or type unit that this
797 /// DIE belongs to.
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;
803 void setOffset(unsigned O) { Offset = O; }
804 void setSize(unsigned S) { Size = S; }
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();
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();
821 /// Find a value in the DIE with the attribute given.
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;
827 void print(raw_ostream &O, unsigned IndentCount = 0) const;
828 void dump() const;
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.
842 MCSection *Section;
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;
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.
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;
865 virtual const MCSymbol *getCrossSectionRelativeBaseAddress() const {
866 return nullptr;
869 /// Return the section that this DIEUnit will be emitted into.
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; }
883 struct BasicDIEUnit final : DIEUnit {
884 BasicDIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag)
885 : DIEUnit(Version, AddrSize, UnitTag) {}
888 //===--------------------------------------------------------------------===//
889 /// DIELoc - Represents an expression location.
891 class DIELoc : public DIEValueList {
892 mutable unsigned Size = 0; // Size in bytes excluding size header.
894 public:
895 DIELoc() = default;
897 /// ComputeSize - Calculate the size of the location expression.
899 unsigned ComputeSize(const AsmPrinter *AP) const;
901 /// BestForm - Choose the best form for data.
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;
916 void EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const;
917 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
919 void print(raw_ostream &O) const;
922 //===--------------------------------------------------------------------===//
923 /// DIEBlock - Represents a block of values.
925 class DIEBlock : public DIEValueList {
926 mutable unsigned Size = 0; // Size in bytes excluding size header.
928 public:
929 DIEBlock() = default;
931 /// ComputeSize - Calculate the size of the location expression.
933 unsigned ComputeSize(const AsmPrinter *AP) const;
935 /// BestForm - Choose the best form for data.
937 dwarf::Form BestForm() const {
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;
947 void EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const;
948 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
950 void print(raw_ostream &O) const;
953 } // end namespace llvm
955 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H