1 //===- lib/CodeGen/DIE.h - DWARF Info Entries -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Data structures for DWARF info entries.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
17 #include "llvm/ADT/FoldingSet.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/ADT/PointerUnion.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/iterator.h"
23 #include "llvm/ADT/iterator_range.h"
24 #include "llvm/BinaryFormat/Dwarf.h"
25 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
26 #include "llvm/Support/AlignOf.h"
27 #include "llvm/Support/Allocator.h"
33 #include <type_traits>
47 //===--------------------------------------------------------------------===//
48 /// Dwarf abbreviation data, describes one attribute of a Dwarf abbreviation.
50 /// Dwarf attribute code.
51 dwarf::Attribute Attribute;
56 /// Dwarf attribute value for DW_FORM_implicit_const
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) {}
67 dwarf::Attribute getAttribute() const { return Attribute; }
68 dwarf::Form getForm() const { return Form; }
69 int64_t getValue() const { return Value; }
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
79 class DIEAbbrev : public FoldingSetNode {
80 /// Unique number for node.
86 /// Whether or not this node has children.
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.
92 /// Raw data bytes for abbreviation.
93 SmallVector<DIEAbbrevData, 12> Data;
96 DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C) {}
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; }
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;
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.
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;
145 DIEAbbrevSet(BumpPtrAllocator &A) : Alloc(A) {}
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.
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) {
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;
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.
205 explicit DIEExpr(const MCExpr *E) : Expr(E) {}
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 //===--------------------------------------------------------------------===//
219 const MCSymbol *Label;
222 explicit DIELabel(const MCSymbol *L) : Label(L) {}
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 simple label difference DIE.
237 const MCSymbol *LabelHi;
238 const MCSymbol *LabelLo;
241 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo) : LabelHi(Hi), LabelLo(Lo) {}
243 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
244 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
246 void print(raw_ostream &O) const;
249 //===--------------------------------------------------------------------===//
250 /// A container for string pool string values.
252 /// This class is used with the DW_FORM_strp and DW_FORM_GNU_str_index forms.
254 DwarfStringPoolEntryRef S;
257 DIEString(DwarfStringPoolEntryRef S) : S(S) {}
259 /// Grab the string out of the object.
260 StringRef getString() const { return S.getString(); }
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 inline string values.
271 /// This class is used with the DW_FORM_string form.
272 class DIEInlineString {
276 template <typename Allocator>
277 explicit DIEInlineString(StringRef Str, Allocator &A) : S(Str.copy(A)) {}
279 ~DIEInlineString() = default;
281 /// Grab the string out of the object.
282 StringRef getString() const { return S; }
284 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
285 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
287 void print(raw_ostream &O) const;
290 //===--------------------------------------------------------------------===//
291 /// A pointer to another debug information entry. An instance of this class can
292 /// also be used as a proxy for a debug information entry not yet defined
299 explicit DIEEntry(DIE &E) : Entry(&E) {}
301 DIE &getEntry() const { return *Entry; }
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 /// Represents a pointer to a location list in the debug_loc
313 /// Index into the .debug_loc vector.
317 DIELocList(size_t I) : Index(I) {}
319 /// Grab the current index out.
320 size_t getValue() const { return Index; }
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 /// A debug information entry value. Some of these roughly correlate
330 /// to DWARF attribute classes.
337 #define HANDLE_DIEVALUE(T) is##T,
338 #include "llvm/CodeGen/DIEValue.def"
342 /// Type of data stored in the value.
344 dwarf::Attribute Attribute = (dwarf::Attribute)0;
345 dwarf::Form Form = (dwarf::Form)0;
347 /// Storage for the value.
349 /// All values that aren't standard layout (or are larger than 8 bytes)
350 /// should be stored by reference instead of by value.
351 using ValTy = AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
352 DIEDelta *, DIEEntry, DIEBlock *,
353 DIELoc *, DIELocList>;
355 static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
356 sizeof(ValTy) <= sizeof(void *),
357 "Expected all large types to be stored via pointer");
359 /// Underlying stored value.
362 template <class T> void construct(T V) {
363 static_assert(std::is_standard_layout<T>::value ||
364 std::is_pointer<T>::value,
365 "Expected standard layout or pointer");
366 new (reinterpret_cast<void *>(Val.buffer)) T(V);
369 template <class T> T *get() { return reinterpret_cast<T *>(Val.buffer); }
370 template <class T> const T *get() const {
371 return reinterpret_cast<const T *>(Val.buffer);
373 template <class T> void destruct() { get<T>()->~T(); }
375 /// Destroy the underlying value.
377 /// This should get optimized down to a no-op. We could skip it if we could
378 /// add a static assert on \a std::is_trivially_copyable(), but we currently
379 /// support versions of GCC that don't understand that.
384 #define HANDLE_DIEVALUE_SMALL(T) \
386 destruct<DIE##T>(); \
388 #define HANDLE_DIEVALUE_LARGE(T) \
390 destruct<const DIE##T *>(); \
392 #include "llvm/CodeGen/DIEValue.def"
396 /// Copy the underlying value.
398 /// This should get optimized down to a simple copy. We need to actually
399 /// construct the value, rather than calling memcpy, to satisfy strict
401 void copyVal(const DIEValue &X) {
405 #define HANDLE_DIEVALUE_SMALL(T) \
407 construct<DIE##T>(*X.get<DIE##T>()); \
409 #define HANDLE_DIEVALUE_LARGE(T) \
411 construct<const DIE##T *>(*X.get<const DIE##T *>()); \
413 #include "llvm/CodeGen/DIEValue.def"
418 DIEValue() = default;
420 DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
424 DIEValue &operator=(const DIEValue &X) {
427 Attribute = X.Attribute;
433 ~DIEValue() { destroyVal(); }
435 #define HANDLE_DIEVALUE_SMALL(T) \
436 DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T &V) \
437 : Ty(is##T), Attribute(Attribute), Form(Form) { \
438 construct<DIE##T>(V); \
440 #define HANDLE_DIEVALUE_LARGE(T) \
441 DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T *V) \
442 : Ty(is##T), Attribute(Attribute), Form(Form) { \
443 assert(V && "Expected valid value"); \
444 construct<const DIE##T *>(V); \
446 #include "llvm/CodeGen/DIEValue.def"
450 Type getType() const { return Ty; }
451 dwarf::Attribute getAttribute() const { return Attribute; }
452 dwarf::Form getForm() const { return Form; }
453 explicit operator bool() const { return Ty; }
456 #define HANDLE_DIEVALUE_SMALL(T) \
457 const DIE##T &getDIE##T() const { \
458 assert(getType() == is##T && "Expected " #T); \
459 return *get<DIE##T>(); \
461 #define HANDLE_DIEVALUE_LARGE(T) \
462 const DIE##T &getDIE##T() const { \
463 assert(getType() == is##T && "Expected " #T); \
464 return **get<const DIE##T *>(); \
466 #include "llvm/CodeGen/DIEValue.def"
468 /// Emit value via the Dwarf writer.
469 void EmitValue(const AsmPrinter *AP) const;
471 /// Return the size of a value in bytes.
472 unsigned SizeOf(const AsmPrinter *AP) const;
474 void print(raw_ostream &O) const;
478 struct IntrusiveBackListNode {
479 PointerIntPair<IntrusiveBackListNode *, 1> Next;
481 IntrusiveBackListNode() : Next(this, true) {}
483 IntrusiveBackListNode *getNext() const {
484 return Next.getInt() ? nullptr : Next.getPointer();
488 struct IntrusiveBackListBase {
489 using Node = IntrusiveBackListNode;
491 Node *Last = nullptr;
493 bool empty() const { return !Last; }
495 void push_back(Node &N) {
496 assert(N.Next.getPointer() == &N && "Expected unlinked node");
497 assert(N.Next.getInt() == true && "Expected unlinked node");
501 Last->Next.setPointerAndInt(&N, false);
507 template <class T> class IntrusiveBackList : IntrusiveBackListBase {
509 using IntrusiveBackListBase::empty;
511 void push_back(T &N) { IntrusiveBackListBase::push_back(N); }
512 T &back() { return *static_cast<T *>(Last); }
513 const T &back() const { return *static_cast<T *>(Last); }
515 class const_iterator;
517 : public iterator_facade_base<iterator, std::forward_iterator_tag, T> {
518 friend class const_iterator;
523 iterator() = default;
524 explicit iterator(T *N) : N(N) {}
526 iterator &operator++() {
531 explicit operator bool() const { return N; }
532 T &operator*() const { return *static_cast<T *>(N); }
534 bool operator==(const iterator &X) const { return N == X.N; }
535 bool operator!=(const iterator &X) const { return N != X.N; }
539 : public iterator_facade_base<const_iterator, std::forward_iterator_tag,
541 const Node *N = nullptr;
544 const_iterator() = default;
545 // Placate MSVC by explicitly scoping 'iterator'.
546 const_iterator(typename IntrusiveBackList<T>::iterator X) : N(X.N) {}
547 explicit const_iterator(const T *N) : N(N) {}
549 const_iterator &operator++() {
554 explicit operator bool() const { return N; }
555 const T &operator*() const { return *static_cast<const T *>(N); }
557 bool operator==(const const_iterator &X) const { return N == X.N; }
558 bool operator!=(const const_iterator &X) const { return N != X.N; }
562 return Last ? iterator(static_cast<T *>(Last->Next.getPointer())) : end();
564 const_iterator begin() const {
565 return const_cast<IntrusiveBackList *>(this)->begin();
567 iterator end() { return iterator(); }
568 const_iterator end() const { return const_iterator(); }
570 static iterator toIterator(T &N) { return iterator(&N); }
571 static const_iterator toIterator(const T &N) { return const_iterator(&N); }
574 /// A list of DIE values.
576 /// This is a singly-linked list, but instead of reversing the order of
577 /// insertion, we keep a pointer to the back of the list so we can push in
580 /// There are two main reasons to choose a linked list over a customized
581 /// vector-like data structure.
583 /// 1. For teardown efficiency, we want DIEs to be BumpPtrAllocated. Using a
584 /// linked list here makes this way easier to accomplish.
585 /// 2. Carrying an extra pointer per \a DIEValue isn't expensive. 45% of DIEs
586 /// have 2 or fewer values, and 90% have 5 or fewer. A vector would be
587 /// over-allocated by 50% on average anyway, the same cost as the
588 /// linked-list node.
590 struct Node : IntrusiveBackListNode {
593 explicit Node(DIEValue V) : V(V) {}
596 using ListTy = IntrusiveBackList<Node>;
601 class const_value_iterator;
603 : public iterator_adaptor_base<value_iterator, ListTy::iterator,
604 std::forward_iterator_tag, DIEValue> {
605 friend class const_value_iterator;
607 using iterator_adaptor =
608 iterator_adaptor_base<value_iterator, ListTy::iterator,
609 std::forward_iterator_tag, DIEValue>;
612 value_iterator() = default;
613 explicit value_iterator(ListTy::iterator X) : iterator_adaptor(X) {}
615 explicit operator bool() const { return bool(wrapped()); }
616 DIEValue &operator*() const { return wrapped()->V; }
619 class const_value_iterator : public iterator_adaptor_base<
620 const_value_iterator, ListTy::const_iterator,
621 std::forward_iterator_tag, const DIEValue> {
622 using iterator_adaptor =
623 iterator_adaptor_base<const_value_iterator, ListTy::const_iterator,
624 std::forward_iterator_tag, const DIEValue>;
627 const_value_iterator() = default;
628 const_value_iterator(DIEValueList::value_iterator X)
629 : iterator_adaptor(X.wrapped()) {}
630 explicit const_value_iterator(ListTy::const_iterator X)
631 : iterator_adaptor(X) {}
633 explicit operator bool() const { return bool(wrapped()); }
634 const DIEValue &operator*() const { return wrapped()->V; }
637 using value_range = iterator_range<value_iterator>;
638 using const_value_range = iterator_range<const_value_iterator>;
640 value_iterator addValue(BumpPtrAllocator &Alloc, const DIEValue &V) {
641 List.push_back(*new (Alloc) Node(V));
642 return value_iterator(ListTy::toIterator(List.back()));
645 value_iterator addValue(BumpPtrAllocator &Alloc, dwarf::Attribute Attribute,
646 dwarf::Form Form, T &&Value) {
647 return addValue(Alloc, DIEValue(Attribute, Form, std::forward<T>(Value)));
650 value_range values() {
651 return make_range(value_iterator(List.begin()), value_iterator(List.end()));
653 const_value_range values() const {
654 return make_range(const_value_iterator(List.begin()),
655 const_value_iterator(List.end()));
659 //===--------------------------------------------------------------------===//
660 /// A structured debug information entry. Has an abbreviation which
661 /// describes its organization.
662 class DIE : IntrusiveBackListNode, public DIEValueList {
663 friend class IntrusiveBackList<DIE>;
664 friend class DIEUnit;
666 /// Dwarf unit relative offset.
668 /// Size of instance + children.
670 unsigned AbbrevNumber = ~0u;
672 dwarf::Tag Tag = (dwarf::Tag)0;
673 /// Set to true to force a DIE to emit an abbreviation that says it has
674 /// children even when it doesn't. This is used for unit testing purposes.
675 bool ForceChildren = false;
677 IntrusiveBackList<DIE> Children;
679 /// The owner is either the parent DIE for children of other DIEs, or a
680 /// DIEUnit which contains this DIE as its unit DIE.
681 PointerUnion<DIE *, DIEUnit *> Owner;
683 explicit DIE(dwarf::Tag Tag) : Tag(Tag) {}
687 DIE(const DIE &RHS) = delete;
688 DIE(DIE &&RHS) = delete;
689 DIE &operator=(const DIE &RHS) = delete;
690 DIE &operator=(const DIE &&RHS) = delete;
692 static DIE *get(BumpPtrAllocator &Alloc, dwarf::Tag Tag) {
693 return new (Alloc) DIE(Tag);
697 unsigned getAbbrevNumber() const { return AbbrevNumber; }
698 dwarf::Tag getTag() const { return Tag; }
699 /// Get the compile/type unit relative offset of this DIE.
700 unsigned getOffset() const { return Offset; }
701 unsigned getSize() const { return Size; }
702 bool hasChildren() const { return ForceChildren || !Children.empty(); }
703 void setForceChildren(bool B) { ForceChildren = B; }
705 using child_iterator = IntrusiveBackList<DIE>::iterator;
706 using const_child_iterator = IntrusiveBackList<DIE>::const_iterator;
707 using child_range = iterator_range<child_iterator>;
708 using const_child_range = iterator_range<const_child_iterator>;
710 child_range children() {
711 return make_range(Children.begin(), Children.end());
713 const_child_range children() const {
714 return make_range(Children.begin(), Children.end());
717 DIE *getParent() const;
719 /// Generate the abbreviation for this DIE.
721 /// Calculate the abbreviation for this, which should be uniqued and
722 /// eventually used to call \a setAbbrevNumber().
723 DIEAbbrev generateAbbrev() const;
725 /// Set the abbreviation number for this DIE.
726 void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
728 /// Get the absolute offset within the .debug_info or .debug_types section
730 unsigned getDebugSectionOffset() const;
732 /// Compute the offset of this DIE and all its children.
734 /// This function gets called just before we are going to generate the debug
735 /// information and gives each DIE a chance to figure out its CU relative DIE
736 /// offset, unique its abbreviation and fill in the abbreviation code, and
737 /// return the unit offset that points to where the next DIE will be emitted
738 /// within the debug unit section. After this function has been called for all
739 /// DIE objects, the DWARF can be generated since all DIEs will be able to
740 /// properly refer to other DIE objects since all DIEs have calculated their
743 /// \param AP AsmPrinter to use when calculating sizes.
744 /// \param AbbrevSet the abbreviation used to unique DIE abbreviations.
745 /// \param CUOffset the compile/type unit relative offset in bytes.
746 /// \returns the offset for the DIE that follows this DIE within the
747 /// current compile/type unit.
748 unsigned computeOffsetsAndAbbrevs(const AsmPrinter *AP,
749 DIEAbbrevSet &AbbrevSet, unsigned CUOffset);
751 /// Climb up the parent chain to get the compile unit or type unit DIE that
752 /// this DIE belongs to.
754 /// \returns the compile or type unit DIE that owns this DIE, or NULL if
755 /// this DIE hasn't been added to a unit DIE.
756 const DIE *getUnitDie() const;
758 /// Climb up the parent chain to get the compile unit or type unit that this
761 /// \returns the DIEUnit that represents the compile or type unit that owns
762 /// this DIE, or NULL if this DIE hasn't been added to a unit DIE.
763 const DIEUnit *getUnit() const;
765 void setOffset(unsigned O) { Offset = O; }
766 void setSize(unsigned S) { Size = S; }
768 /// Add a child to the DIE.
769 DIE &addChild(DIE *Child) {
770 assert(!Child->getParent() && "Child should be orphaned");
772 Children.push_back(*Child);
773 return Children.back();
776 /// Find a value in the DIE with the attribute given.
778 /// Returns a default-constructed DIEValue (where \a DIEValue::getType()
779 /// gives \a DIEValue::isNone) if no such attribute exists.
780 DIEValue findAttribute(dwarf::Attribute Attribute) const;
782 void print(raw_ostream &O, unsigned IndentCount = 0) const;
786 //===--------------------------------------------------------------------===//
787 /// Represents a compile or type unit.
789 /// The compile unit or type unit DIE. This variable must be an instance of
790 /// DIE so that we can calculate the DIEUnit from any DIE by traversing the
791 /// parent backchain and getting the Unit DIE, and then casting itself to a
792 /// DIEUnit. This allows us to be able to find the DIEUnit for any DIE without
793 /// having to store a pointer to the DIEUnit in each DIE instance.
795 /// The section this unit will be emitted in. This may or may not be set to
796 /// a valid section depending on the client that is emitting DWARF.
798 uint64_t Offset; /// .debug_info or .debug_types absolute section offset.
799 uint32_t Length; /// The length in bytes of all of the DIEs in this unit.
800 const uint16_t Version; /// The Dwarf version number for this unit.
801 const uint8_t AddrSize; /// The size in bytes of an address for this unit.
803 ~DIEUnit() = default;
806 DIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag);
807 DIEUnit(const DIEUnit &RHS) = delete;
808 DIEUnit(DIEUnit &&RHS) = delete;
809 void operator=(const DIEUnit &RHS) = delete;
810 void operator=(const DIEUnit &&RHS) = delete;
811 /// Set the section that this DIEUnit will be emitted into.
813 /// This function is used by some clients to set the section. Not all clients
814 /// that emit DWARF use this section variable.
815 void setSection(MCSection *Section) {
816 assert(!this->Section);
817 this->Section = Section;
820 virtual const MCSymbol *getCrossSectionRelativeBaseAddress() const {
824 /// Return the section that this DIEUnit will be emitted into.
826 /// \returns Section pointer which can be NULL.
827 MCSection *getSection() const { return Section; }
828 void setDebugSectionOffset(unsigned O) { Offset = O; }
829 unsigned getDebugSectionOffset() const { return Offset; }
830 void setLength(uint64_t L) { Length = L; }
831 uint64_t getLength() const { return Length; }
832 uint16_t getDwarfVersion() const { return Version; }
833 uint16_t getAddressSize() const { return AddrSize; }
834 DIE &getUnitDie() { return Die; }
835 const DIE &getUnitDie() const { return Die; }
838 struct BasicDIEUnit final : DIEUnit {
839 BasicDIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag)
840 : DIEUnit(Version, AddrSize, UnitTag) {}
843 //===--------------------------------------------------------------------===//
844 /// DIELoc - Represents an expression location.
846 class DIELoc : public DIEValueList {
847 mutable unsigned Size = 0; // Size in bytes excluding size header.
852 /// ComputeSize - Calculate the size of the location expression.
854 unsigned ComputeSize(const AsmPrinter *AP) const;
856 /// BestForm - Choose the best form for data.
858 dwarf::Form BestForm(unsigned DwarfVersion) const {
859 if (DwarfVersion > 3)
860 return dwarf::DW_FORM_exprloc;
861 // Pre-DWARF4 location expressions were blocks and not exprloc.
862 if ((unsigned char)Size == Size)
863 return dwarf::DW_FORM_block1;
864 if ((unsigned short)Size == Size)
865 return dwarf::DW_FORM_block2;
866 if ((unsigned int)Size == Size)
867 return dwarf::DW_FORM_block4;
868 return dwarf::DW_FORM_block;
871 void EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const;
872 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
874 void print(raw_ostream &O) const;
877 //===--------------------------------------------------------------------===//
878 /// DIEBlock - Represents a block of values.
880 class DIEBlock : public DIEValueList {
881 mutable unsigned Size = 0; // Size in bytes excluding size header.
884 DIEBlock() = default;
886 /// ComputeSize - Calculate the size of the location expression.
888 unsigned ComputeSize(const AsmPrinter *AP) const;
890 /// BestForm - Choose the best form for data.
892 dwarf::Form BestForm() const {
893 if ((unsigned char)Size == Size)
894 return dwarf::DW_FORM_block1;
895 if ((unsigned short)Size == Size)
896 return dwarf::DW_FORM_block2;
897 if ((unsigned int)Size == Size)
898 return dwarf::DW_FORM_block4;
899 return dwarf::DW_FORM_block;
902 void EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const;
903 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
905 void print(raw_ostream &O) const;
908 } // end namespace llvm
910 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H