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/CodeGen/DwarfStringPoolEntry.h"
25 #include "llvm/Support/AlignOf.h"
26 #include "llvm/Support/Allocator.h"
27 #include "llvm/Support/Dwarf.h"
33 #include <type_traits>
46 //===--------------------------------------------------------------------===//
47 /// Dwarf abbreviation data, describes one attribute of a Dwarf abbreviation.
49 /// Dwarf attribute code.
50 dwarf::Attribute Attribute;
55 /// Dwarf attribute value for DW_FORM_implicit_const
59 DIEAbbrevData(dwarf::Attribute A, dwarf::Form F)
60 : Attribute(A), Form(F), Value(0) {}
61 DIEAbbrevData(dwarf::Attribute A, int64_t V)
62 : Attribute(A), Form(dwarf::DW_FORM_implicit_const), Value(V) {}
66 dwarf::Attribute getAttribute() const { return Attribute; }
67 dwarf::Form getForm() const { return Form; }
68 int64_t getValue() const { return Value; }
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
78 class DIEAbbrev : public FoldingSetNode {
79 /// Unique number for node.
85 /// Whether or not this node has children.
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.
91 /// Raw data bytes for abbreviation.
92 SmallVector<DIEAbbrevData, 12> Data;
95 DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C) {}
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; }
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);
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.
135 /// The bump allocator to use when creating DIEAbbrev objects in the uniqued
136 /// storage container.
137 BumpPtrAllocator &Alloc;
138 /// \brief FoldingSet that uniques the abbreviations.
139 llvm::FoldingSet<DIEAbbrev> AbbreviationsSet;
140 /// A list of all the unique abbreviations in use.
141 std::vector<DIEAbbrev *> Abbreviations;
144 DIEAbbrevSet(BumpPtrAllocator &A) : Alloc(A) {}
146 /// Generate the abbreviation declaration for a DIE and return a pointer to
147 /// the generated abbreviation.
149 /// \param Die the debug info entry to generate the abbreviation for.
150 /// \returns A reference to the uniqued abbreviation declaration that is
151 /// owned by this class.
152 DIEAbbrev &uniqueAbbreviation(DIE &Die);
154 /// Print all abbreviations using the specified asm printer.
155 void Emit(const AsmPrinter *AP, MCSection *Section) const;
158 //===--------------------------------------------------------------------===//
159 /// An integer value DIE.
165 explicit DIEInteger(uint64_t I) : Integer(I) {}
167 /// Choose the best form for integer.
168 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
170 const int64_t SignedInt = Int;
171 if ((char)Int == SignedInt)
172 return dwarf::DW_FORM_data1;
173 if ((short)Int == SignedInt)
174 return dwarf::DW_FORM_data2;
175 if ((int)Int == SignedInt)
176 return dwarf::DW_FORM_data4;
178 if ((unsigned char)Int == Int)
179 return dwarf::DW_FORM_data1;
180 if ((unsigned short)Int == Int)
181 return dwarf::DW_FORM_data2;
182 if ((unsigned int)Int == Int)
183 return dwarf::DW_FORM_data4;
185 return dwarf::DW_FORM_data8;
188 uint64_t getValue() const { return Integer; }
189 void setValue(uint64_t Val) { Integer = Val; }
191 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
192 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
194 void print(raw_ostream &O) const;
197 //===--------------------------------------------------------------------===//
198 /// An expression DIE.
203 explicit DIEExpr(const MCExpr *E) : Expr(E) {}
206 const MCExpr *getValue() const { return Expr; }
208 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
209 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
211 void print(raw_ostream &O) const;
214 //===--------------------------------------------------------------------===//
217 const MCSymbol *Label;
220 explicit DIELabel(const MCSymbol *L) : Label(L) {}
223 const MCSymbol *getValue() const { return Label; }
225 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
226 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
228 void print(raw_ostream &O) const;
231 //===--------------------------------------------------------------------===//
232 /// A simple label difference DIE.
235 const MCSymbol *LabelHi;
236 const MCSymbol *LabelLo;
239 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo) : LabelHi(Hi), LabelLo(Lo) {}
241 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
242 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
244 void print(raw_ostream &O) const;
247 //===--------------------------------------------------------------------===//
248 /// A container for string pool string values.
250 /// This class is used with the DW_FORM_strp and DW_FORM_GNU_str_index forms.
252 DwarfStringPoolEntryRef S;
255 DIEString(DwarfStringPoolEntryRef S) : S(S) {}
257 /// Grab the string out of the object.
258 StringRef getString() const { return S.getString(); }
260 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
261 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
263 void print(raw_ostream &O) const;
266 //===--------------------------------------------------------------------===//
267 /// A container for inline string values.
269 /// This class is used with the DW_FORM_string form.
270 class DIEInlineString {
274 template <typename Allocator>
275 explicit DIEInlineString(StringRef Str, Allocator &A) : S(Str.copy(A)) {}
277 ~DIEInlineString() = default;
279 /// Grab the string out of the object.
280 StringRef getString() const { return S; }
282 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
283 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
285 void print(raw_ostream &O) const;
288 //===--------------------------------------------------------------------===//
289 /// A pointer to another debug information entry. An instance of this class can
290 /// 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 typedef AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
352 DIEDelta *, DIEEntry, DIEBlock *, DIELoc *,
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) \
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 typedef IntrusiveBackListNode Node;
490 Node *Last = nullptr;
492 bool empty() const { return !Last; }
493 void push_back(Node &N) {
494 assert(N.Next.getPointer() == &N && "Expected unlinked node");
495 assert(N.Next.getInt() == true && "Expected unlinked node");
499 Last->Next.setPointerAndInt(&N, false);
505 template <class T> class IntrusiveBackList : IntrusiveBackListBase {
507 using IntrusiveBackListBase::empty;
508 void push_back(T &N) { IntrusiveBackListBase::push_back(N); }
509 T &back() { return *static_cast<T *>(Last); }
510 const T &back() const { return *static_cast<T *>(Last); }
512 class const_iterator;
514 : public iterator_facade_base<iterator, std::forward_iterator_tag, T> {
515 friend class const_iterator;
519 iterator() = default;
520 explicit iterator(T *N) : N(N) {}
522 iterator &operator++() {
527 explicit operator bool() const { return N; }
528 T &operator*() const { return *static_cast<T *>(N); }
530 bool operator==(const iterator &X) const { return N == X.N; }
531 bool operator!=(const iterator &X) const { return N != X.N; }
535 : public iterator_facade_base<const_iterator, std::forward_iterator_tag,
537 const Node *N = nullptr;
540 const_iterator() = default;
541 // Placate MSVC by explicitly scoping 'iterator'.
542 const_iterator(typename IntrusiveBackList<T>::iterator X) : N(X.N) {}
543 explicit const_iterator(const T *N) : N(N) {}
545 const_iterator &operator++() {
550 explicit operator bool() const { return N; }
551 const T &operator*() const { return *static_cast<const T *>(N); }
553 bool operator==(const const_iterator &X) const { return N == X.N; }
554 bool operator!=(const const_iterator &X) const { return N != X.N; }
558 return Last ? iterator(static_cast<T *>(Last->Next.getPointer())) : end();
560 const_iterator begin() const {
561 return const_cast<IntrusiveBackList *>(this)->begin();
563 iterator end() { return iterator(); }
564 const_iterator end() const { return const_iterator(); }
566 static iterator toIterator(T &N) { return iterator(&N); }
567 static const_iterator toIterator(const T &N) { return const_iterator(&N); }
570 /// A list of DIE values.
572 /// This is a singly-linked list, but instead of reversing the order of
573 /// insertion, we keep a pointer to the back of the list so we can push in
576 /// There are two main reasons to choose a linked list over a customized
577 /// vector-like data structure.
579 /// 1. For teardown efficiency, we want DIEs to be BumpPtrAllocated. Using a
580 /// linked list here makes this way easier to accomplish.
581 /// 2. Carrying an extra pointer per \a DIEValue isn't expensive. 45% of DIEs
582 /// have 2 or fewer values, and 90% have 5 or fewer. A vector would be
583 /// over-allocated by 50% on average anyway, the same cost as the
584 /// linked-list node.
586 struct Node : IntrusiveBackListNode {
588 explicit Node(DIEValue V) : V(V) {}
591 typedef IntrusiveBackList<Node> ListTy;
595 class const_value_iterator;
597 : public iterator_adaptor_base<value_iterator, ListTy::iterator,
598 std::forward_iterator_tag, DIEValue> {
599 friend class const_value_iterator;
600 typedef iterator_adaptor_base<value_iterator, ListTy::iterator,
601 std::forward_iterator_tag,
602 DIEValue> iterator_adaptor;
605 value_iterator() = default;
606 explicit value_iterator(ListTy::iterator X) : iterator_adaptor(X) {}
608 explicit operator bool() const { return bool(wrapped()); }
609 DIEValue &operator*() const { return wrapped()->V; }
612 class const_value_iterator : public iterator_adaptor_base<
613 const_value_iterator, ListTy::const_iterator,
614 std::forward_iterator_tag, const DIEValue> {
615 typedef iterator_adaptor_base<const_value_iterator, ListTy::const_iterator,
616 std::forward_iterator_tag,
617 const DIEValue> iterator_adaptor;
620 const_value_iterator() = default;
621 const_value_iterator(DIEValueList::value_iterator X)
622 : iterator_adaptor(X.wrapped()) {}
623 explicit const_value_iterator(ListTy::const_iterator X)
624 : iterator_adaptor(X) {}
626 explicit operator bool() const { return bool(wrapped()); }
627 const DIEValue &operator*() const { return wrapped()->V; }
630 typedef iterator_range<value_iterator> value_range;
631 typedef iterator_range<const_value_iterator> const_value_range;
633 value_iterator addValue(BumpPtrAllocator &Alloc, const DIEValue &V) {
634 List.push_back(*new (Alloc) Node(V));
635 return value_iterator(ListTy::toIterator(List.back()));
638 value_iterator addValue(BumpPtrAllocator &Alloc, dwarf::Attribute Attribute,
639 dwarf::Form Form, T &&Value) {
640 return addValue(Alloc, DIEValue(Attribute, Form, std::forward<T>(Value)));
643 value_range values() {
644 return make_range(value_iterator(List.begin()), value_iterator(List.end()));
646 const_value_range values() const {
647 return make_range(const_value_iterator(List.begin()),
648 const_value_iterator(List.end()));
652 //===--------------------------------------------------------------------===//
653 /// A structured debug information entry. Has an abbreviation which
654 /// describes its organization.
655 class DIE : IntrusiveBackListNode, public DIEValueList {
656 friend class IntrusiveBackList<DIE>;
657 friend class DIEUnit;
659 /// Dwarf unit relative offset.
661 /// Size of instance + children.
663 unsigned AbbrevNumber = ~0u;
665 dwarf::Tag Tag = (dwarf::Tag)0;
666 /// Set to true to force a DIE to emit an abbreviation that says it has
667 /// children even when it doesn't. This is used for unit testing purposes.
670 IntrusiveBackList<DIE> Children;
672 /// The owner is either the parent DIE for children of other DIEs, or a
673 /// DIEUnit which contains this DIE as its unit DIE.
674 PointerUnion<DIE *, DIEUnit *> Owner;
677 explicit DIE(dwarf::Tag Tag) : Offset(0), Size(0), Tag(Tag),
678 ForceChildren(false) {}
681 static DIE *get(BumpPtrAllocator &Alloc, dwarf::Tag Tag) {
682 return new (Alloc) DIE(Tag);
685 DIE(const DIE &RHS) = delete;
686 DIE(DIE &&RHS) = delete;
687 void operator=(const DIE &RHS) = delete;
688 void operator=(const DIE &&RHS) = delete;
691 unsigned getAbbrevNumber() const { return AbbrevNumber; }
692 dwarf::Tag getTag() const { return Tag; }
693 /// Get the compile/type unit relative offset of this DIE.
694 unsigned getOffset() const { return Offset; }
695 unsigned getSize() const { return Size; }
696 bool hasChildren() const { return ForceChildren || !Children.empty(); }
697 void setForceChildren(bool B) { ForceChildren = B; }
699 typedef IntrusiveBackList<DIE>::iterator child_iterator;
700 typedef IntrusiveBackList<DIE>::const_iterator const_child_iterator;
701 typedef iterator_range<child_iterator> child_range;
702 typedef iterator_range<const_child_iterator> const_child_range;
704 child_range children() {
705 return make_range(Children.begin(), Children.end());
707 const_child_range children() const {
708 return make_range(Children.begin(), Children.end());
711 DIE *getParent() const;
713 /// Generate the abbreviation for this DIE.
715 /// Calculate the abbreviation for this, which should be uniqued and
716 /// eventually used to call \a setAbbrevNumber().
717 DIEAbbrev generateAbbrev() const;
719 /// Set the abbreviation number for this DIE.
720 void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
722 /// Get the absolute offset within the .debug_info or .debug_types section
724 unsigned getDebugSectionOffset() const;
726 /// Compute the offset of this DIE and all its children.
728 /// This function gets called just before we are going to generate the debug
729 /// information and gives each DIE a chance to figure out its CU relative DIE
730 /// offset, unique its abbreviation and fill in the abbreviation code, and
731 /// return the unit offset that points to where the next DIE will be emitted
732 /// within the debug unit section. After this function has been called for all
733 /// DIE objects, the DWARF can be generated since all DIEs will be able to
734 /// properly refer to other DIE objects since all DIEs have calculated their
737 /// \param AP AsmPrinter to use when calculating sizes.
738 /// \param AbbrevSet the abbreviation used to unique DIE abbreviations.
739 /// \param CUOffset the compile/type unit relative offset in bytes.
740 /// \returns the offset for the DIE that follows this DIE within the
741 /// current compile/type unit.
742 unsigned computeOffsetsAndAbbrevs(const AsmPrinter *AP,
743 DIEAbbrevSet &AbbrevSet, unsigned CUOffset);
745 /// Climb up the parent chain to get the compile unit or type unit DIE that
746 /// this DIE belongs to.
748 /// \returns the compile or type unit DIE that owns this DIE, or NULL if
749 /// this DIE hasn't been added to a unit DIE.
750 const DIE *getUnitDie() const;
752 /// Climb up the parent chain to get the compile unit or type unit that this
755 /// \returns the DIEUnit that represents the compile or type unit that owns
756 /// this DIE, or NULL if this DIE hasn't been added to a unit DIE.
757 const DIEUnit *getUnit() const;
759 void setOffset(unsigned O) { Offset = O; }
760 void setSize(unsigned S) { Size = S; }
762 /// Add a child to the DIE.
763 DIE &addChild(DIE *Child) {
764 assert(!Child->getParent() && "Child should be orphaned");
766 Children.push_back(*Child);
767 return Children.back();
770 /// Find a value in the DIE with the attribute given.
772 /// Returns a default-constructed DIEValue (where \a DIEValue::getType()
773 /// gives \a DIEValue::isNone) if no such attribute exists.
774 DIEValue findAttribute(dwarf::Attribute Attribute) const;
776 void print(raw_ostream &O, unsigned IndentCount = 0) const;
780 //===--------------------------------------------------------------------===//
781 /// Represents a compile or type unit.
783 /// The compile unit or type unit DIE. This variable must be an instance of
784 /// DIE so that we can calculate the DIEUnit from any DIE by traversing the
785 /// parent backchain and getting the Unit DIE, and then casting itself to a
786 /// DIEUnit. This allows us to be able to find the DIEUnit for any DIE without
787 /// having to store a pointer to the DIEUnit in each DIE instance.
789 /// The section this unit will be emitted in. This may or may not be set to
790 /// a valid section depending on the client that is emitting DWARF.
792 uint64_t Offset; /// .debug_info or .debug_types absolute section offset.
793 uint32_t Length; /// The length in bytes of all of the DIEs in this unit.
794 const uint16_t Version; /// The Dwarf version number for this unit.
795 const uint8_t AddrSize; /// The size in bytes of an address for this unit.
797 ~DIEUnit() = default;
800 DIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag);
801 DIEUnit(const DIEUnit &RHS) = delete;
802 DIEUnit(DIEUnit &&RHS) = delete;
803 void operator=(const DIEUnit &RHS) = delete;
804 void operator=(const DIEUnit &&RHS) = delete;
805 /// Set the section that this DIEUnit will be emitted into.
807 /// This function is used by some clients to set the section. Not all clients
808 /// that emit DWARF use this section variable.
809 void setSection(MCSection *Section) {
810 assert(!this->Section);
811 this->Section = Section;
814 virtual const MCSymbol *getCrossSectionRelativeBaseAddress() const {
818 /// Return the section that this DIEUnit will be emitted into.
820 /// \returns Section pointer which can be NULL.
821 MCSection *getSection() const { return Section; }
822 void setDebugSectionOffset(unsigned O) { Offset = O; }
823 unsigned getDebugSectionOffset() const { return Offset; }
824 void setLength(uint64_t L) { Length = L; }
825 uint64_t getLength() const { return Length; }
826 uint16_t getDwarfVersion() const { return Version; }
827 uint16_t getAddressSize() const { return AddrSize; }
828 DIE &getUnitDie() { return Die; }
829 const DIE &getUnitDie() const { return Die; }
832 struct BasicDIEUnit final : DIEUnit {
833 BasicDIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag)
834 : DIEUnit(Version, AddrSize, UnitTag) {}
837 //===--------------------------------------------------------------------===//
838 /// DIELoc - Represents an expression location.
840 class DIELoc : public DIEValueList {
841 mutable unsigned Size; // Size in bytes excluding size header.
844 DIELoc() : Size(0) {}
846 /// ComputeSize - Calculate the size of the location expression.
848 unsigned ComputeSize(const AsmPrinter *AP) const;
850 /// BestForm - Choose the best form for data.
852 dwarf::Form BestForm(unsigned DwarfVersion) const {
853 if (DwarfVersion > 3)
854 return dwarf::DW_FORM_exprloc;
855 // Pre-DWARF4 location expressions were blocks and not exprloc.
856 if ((unsigned char)Size == Size)
857 return dwarf::DW_FORM_block1;
858 if ((unsigned short)Size == Size)
859 return dwarf::DW_FORM_block2;
860 if ((unsigned int)Size == Size)
861 return dwarf::DW_FORM_block4;
862 return dwarf::DW_FORM_block;
865 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
866 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
868 void print(raw_ostream &O) const;
871 //===--------------------------------------------------------------------===//
872 /// DIEBlock - Represents a block of values.
874 class DIEBlock : public DIEValueList {
875 mutable unsigned Size; // Size in bytes excluding size header.
878 DIEBlock() : Size(0) {}
880 /// ComputeSize - Calculate the size of the location expression.
882 unsigned ComputeSize(const AsmPrinter *AP) const;
884 /// BestForm - Choose the best form for data.
886 dwarf::Form BestForm() const {
887 if ((unsigned char)Size == Size)
888 return dwarf::DW_FORM_block1;
889 if ((unsigned short)Size == Size)
890 return dwarf::DW_FORM_block2;
891 if ((unsigned int)Size == Size)
892 return dwarf::DW_FORM_block4;
893 return dwarf::DW_FORM_block;
896 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
897 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
899 void print(raw_ostream &O) const;
902 } // end namespace llvm
904 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H