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/STLExtras.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
22 #include "llvm/Support/Dwarf.h"
32 //===--------------------------------------------------------------------===//
33 /// DIEAbbrevData - Dwarf abbreviation data, describes one attribute of a
34 /// Dwarf abbreviation.
36 /// Attribute - Dwarf attribute code.
38 dwarf::Attribute Attribute;
40 /// Form - Dwarf form code.
45 DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
48 dwarf::Attribute getAttribute() const { return Attribute; }
49 dwarf::Form getForm() const { return Form; }
51 /// Profile - Used to gather unique data for the abbreviation folding set.
53 void Profile(FoldingSetNodeID &ID) const;
56 //===--------------------------------------------------------------------===//
57 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
58 /// information object.
59 class DIEAbbrev : public FoldingSetNode {
60 /// Unique number for node.
64 /// Tag - Dwarf tag code.
68 /// Children - Whether or not this node has children.
70 // This cheats a bit in all of the uses since the values in the standard
71 // are 0 and 1 for no children and children respectively.
74 /// Data - Raw data bytes for abbreviation.
76 SmallVector<DIEAbbrevData, 12> Data;
79 DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C), Data() {}
82 dwarf::Tag getTag() const { return Tag; }
83 unsigned getNumber() const { return Number; }
84 bool hasChildren() const { return Children; }
85 const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
86 void setChildrenFlag(bool hasChild) { Children = hasChild; }
87 void setNumber(unsigned N) { Number = N; }
89 /// AddAttribute - Adds another set of attribute information to the
91 void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
92 Data.push_back(DIEAbbrevData(Attribute, Form));
95 /// Profile - Used to gather unique data for the abbreviation folding set.
97 void Profile(FoldingSetNodeID &ID) const;
99 /// Emit - Print the abbreviation using the specified asm printer.
101 void Emit(const AsmPrinter *AP) const;
104 void print(raw_ostream &O);
109 //===--------------------------------------------------------------------===//
110 /// DIEInteger - An integer value DIE.
116 explicit DIEInteger(uint64_t I) : Integer(I) {}
118 /// BestForm - Choose the best form for integer.
120 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
122 const int64_t SignedInt = Int;
123 if ((char)Int == SignedInt)
124 return dwarf::DW_FORM_data1;
125 if ((short)Int == SignedInt)
126 return dwarf::DW_FORM_data2;
127 if ((int)Int == SignedInt)
128 return dwarf::DW_FORM_data4;
130 if ((unsigned char)Int == Int)
131 return dwarf::DW_FORM_data1;
132 if ((unsigned short)Int == Int)
133 return dwarf::DW_FORM_data2;
134 if ((unsigned int)Int == Int)
135 return dwarf::DW_FORM_data4;
137 return dwarf::DW_FORM_data8;
140 uint64_t getValue() const { return Integer; }
141 void setValue(uint64_t Val) { Integer = Val; }
143 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
144 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
147 void print(raw_ostream &O) const;
151 //===--------------------------------------------------------------------===//
152 /// DIEExpr - An expression DIE.
158 explicit DIEExpr(const MCExpr *E) : Expr(E) {}
160 /// getValue - Get MCExpr.
162 const MCExpr *getValue() const { return Expr; }
164 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
165 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
168 void print(raw_ostream &O) const;
172 //===--------------------------------------------------------------------===//
173 /// DIELabel - A label DIE.
176 const MCSymbol *Label;
179 explicit DIELabel(const MCSymbol *L) : Label(L) {}
181 /// getValue - Get MCSymbol.
183 const MCSymbol *getValue() const { return Label; }
185 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
186 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
189 void print(raw_ostream &O) const;
193 //===--------------------------------------------------------------------===//
194 /// DIEDelta - A simple label difference DIE.
197 const MCSymbol *LabelHi;
198 const MCSymbol *LabelLo;
201 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo) : LabelHi(Hi), LabelLo(Lo) {}
203 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
204 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
207 void print(raw_ostream &O) const;
211 //===--------------------------------------------------------------------===//
212 /// DIEString - A container for string values.
215 DwarfStringPoolEntryRef S;
218 DIEString(DwarfStringPoolEntryRef S) : S(S) {}
220 /// getString - Grab the string out of the object.
221 StringRef getString() const { return S.getString(); }
223 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
224 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
227 void print(raw_ostream &O) const;
231 //===--------------------------------------------------------------------===//
232 /// DIEEntry - A pointer to another debug information entry. An instance of
233 /// this class can also be used as a proxy for a debug information entry not
234 /// yet defined (ie. types.)
242 explicit DIEEntry(DIE &E) : Entry(&E) {}
244 DIE &getEntry() const { return *Entry; }
246 /// Returns size of a ref_addr entry.
247 static unsigned getRefAddrSize(const AsmPrinter *AP);
249 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
250 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
251 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
256 void print(raw_ostream &O) const;
260 //===--------------------------------------------------------------------===//
261 /// \brief A signature reference to a type unit.
262 class DIETypeSignature {
263 const DwarfTypeUnit *Unit;
265 DIETypeSignature() = delete;
268 explicit DIETypeSignature(const DwarfTypeUnit &Unit) : Unit(&Unit) {}
270 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
271 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
272 assert(Form == dwarf::DW_FORM_ref_sig8);
277 void print(raw_ostream &O) const;
281 //===--------------------------------------------------------------------===//
282 /// DIELocList - Represents a pointer to a location list in the debug_loc
286 // Index into the .debug_loc vector.
290 DIELocList(size_t I) : Index(I) {}
292 /// getValue - Grab the current index out.
293 size_t getValue() const { return Index; }
295 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
296 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
299 void print(raw_ostream &O) const;
303 //===--------------------------------------------------------------------===//
304 /// DIEValue - A debug information entry value. Some of these roughly correlate
305 /// to DWARF attribute classes.
313 #define HANDLE_DIEVALUE(T) is##T,
314 #include "llvm/CodeGen/DIEValue.def"
318 /// Ty - Type of data stored in the value.
321 dwarf::Attribute Attribute = (dwarf::Attribute)0;
322 dwarf::Form Form = (dwarf::Form)0;
324 /// Storage for the value.
326 /// All values that aren't standard layout (or are larger than 8 bytes)
327 /// should be stored by reference instead of by value.
328 typedef AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
329 DIEDelta *, DIEEntry, DIETypeSignature,
330 DIEBlock *, DIELoc *, DIELocList> ValTy;
331 static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
332 sizeof(ValTy) <= sizeof(void *),
333 "Expected all large types to be stored via pointer");
335 /// Underlying stored value.
338 template <class T> void construct(T V) {
339 static_assert(std::is_standard_layout<T>::value ||
340 std::is_pointer<T>::value,
341 "Expected standard layout or pointer");
342 new (reinterpret_cast<void *>(Val.buffer)) T(V);
345 template <class T> T *get() { return reinterpret_cast<T *>(Val.buffer); }
346 template <class T> const T *get() const {
347 return reinterpret_cast<const T *>(Val.buffer);
349 template <class T> void destruct() { get<T>()->~T(); }
351 /// Destroy the underlying value.
353 /// This should get optimized down to a no-op. We could skip it if we could
354 /// add a static assert on \a std::is_trivially_copyable(), but we currently
355 /// support versions of GCC that don't understand that.
360 #define HANDLE_DIEVALUE_SMALL(T) \
364 #define HANDLE_DIEVALUE_LARGE(T) \
366 destruct<const DIE##T *>();
368 #include "llvm/CodeGen/DIEValue.def"
372 /// Copy the underlying value.
374 /// This should get optimized down to a simple copy. We need to actually
375 /// construct the value, rather than calling memcpy, to satisfy strict
377 void copyVal(const DIEValue &X) {
381 #define HANDLE_DIEVALUE_SMALL(T) \
383 construct<DIE##T>(*X.get<DIE##T>()); \
385 #define HANDLE_DIEVALUE_LARGE(T) \
387 construct<const DIE##T *>(*X.get<const DIE##T *>()); \
389 #include "llvm/CodeGen/DIEValue.def"
394 DIEValue() = default;
395 DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
398 DIEValue &operator=(const DIEValue &X) {
401 Attribute = X.Attribute;
406 ~DIEValue() { destroyVal(); }
408 #define HANDLE_DIEVALUE_SMALL(T) \
409 DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T &V) \
410 : Ty(is##T), Attribute(Attribute), Form(Form) { \
411 construct<DIE##T>(V); \
413 #define HANDLE_DIEVALUE_LARGE(T) \
414 DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T *V) \
415 : Ty(is##T), Attribute(Attribute), Form(Form) { \
416 assert(V && "Expected valid value"); \
417 construct<const DIE##T *>(V); \
419 #include "llvm/CodeGen/DIEValue.def"
422 Type getType() const { return Ty; }
423 dwarf::Attribute getAttribute() const { return Attribute; }
424 dwarf::Form getForm() const { return Form; }
425 explicit operator bool() const { return Ty; }
427 #define HANDLE_DIEVALUE_SMALL(T) \
428 const DIE##T &getDIE##T() const { \
429 assert(getType() == is##T && "Expected " #T); \
430 return *get<DIE##T>(); \
432 #define HANDLE_DIEVALUE_LARGE(T) \
433 const DIE##T &getDIE##T() const { \
434 assert(getType() == is##T && "Expected " #T); \
435 return **get<const DIE##T *>(); \
437 #include "llvm/CodeGen/DIEValue.def"
439 /// EmitValue - Emit value via the Dwarf writer.
441 void EmitValue(const AsmPrinter *AP) const;
443 /// SizeOf - Return the size of a value in bytes.
445 unsigned SizeOf(const AsmPrinter *AP) const;
448 void print(raw_ostream &O) const;
453 struct IntrusiveBackListNode {
454 PointerIntPair<IntrusiveBackListNode *, 1> Next;
455 IntrusiveBackListNode() : Next(this, true) {}
457 IntrusiveBackListNode *getNext() const {
458 return Next.getInt() ? nullptr : Next.getPointer();
462 struct IntrusiveBackListBase {
463 typedef IntrusiveBackListNode Node;
464 Node *Last = nullptr;
466 bool empty() const { return !Last; }
467 void push_back(Node &N) {
468 assert(N.Next.getPointer() == &N && "Expected unlinked node");
469 assert(N.Next.getInt() == true && "Expected unlinked node");
473 Last->Next.setPointerAndInt(&N, false);
479 template <class T> class IntrusiveBackList : IntrusiveBackListBase {
481 using IntrusiveBackListBase::empty;
482 void push_back(T &N) { IntrusiveBackListBase::push_back(N); }
483 T &back() { return *static_cast<T *>(Last); }
484 const T &back() const { return *static_cast<T *>(Last); }
486 class const_iterator;
488 : public iterator_facade_base<iterator, std::forward_iterator_tag, T> {
489 friend class const_iterator;
493 iterator() = default;
494 explicit iterator(T *N) : N(N) {}
496 iterator &operator++() {
501 explicit operator bool() const { return N; }
502 T &operator*() const { return *static_cast<T *>(N); }
504 bool operator==(const iterator &X) const { return N == X.N; }
505 bool operator!=(const iterator &X) const { return N != X.N; }
509 : public iterator_facade_base<const_iterator, std::forward_iterator_tag,
511 const Node *N = nullptr;
514 const_iterator() = default;
515 // Placate MSVC by explicitly scoping 'iterator'.
516 const_iterator(typename IntrusiveBackList<T>::iterator X) : N(X.N) {}
517 explicit const_iterator(const T *N) : N(N) {}
519 const_iterator &operator++() {
524 explicit operator bool() const { return N; }
525 const T &operator*() const { return *static_cast<const T *>(N); }
527 bool operator==(const const_iterator &X) const { return N == X.N; }
528 bool operator!=(const const_iterator &X) const { return N != X.N; }
532 return Last ? iterator(static_cast<T *>(Last->Next.getPointer())) : end();
534 const_iterator begin() const {
535 return const_cast<IntrusiveBackList *>(this)->begin();
537 iterator end() { return iterator(); }
538 const_iterator end() const { return const_iterator(); }
540 static iterator toIterator(T &N) { return iterator(&N); }
541 static const_iterator toIterator(const T &N) { return const_iterator(&N); }
544 /// A list of DIE values.
546 /// This is a singly-linked list, but instead of reversing the order of
547 /// insertion, we keep a pointer to the back of the list so we can push in
550 /// There are two main reasons to choose a linked list over a customized
551 /// vector-like data structure.
553 /// 1. For teardown efficiency, we want DIEs to be BumpPtrAllocated. Using a
554 /// linked list here makes this way easier to accomplish.
555 /// 2. Carrying an extra pointer per \a DIEValue isn't expensive. 45% of DIEs
556 /// have 2 or fewer values, and 90% have 5 or fewer. A vector would be
557 /// over-allocated by 50% on average anyway, the same cost as the
558 /// linked-list node.
560 struct Node : IntrusiveBackListNode {
562 explicit Node(DIEValue V) : V(V) {}
565 typedef IntrusiveBackList<Node> ListTy;
569 class const_value_iterator;
571 : public iterator_adaptor_base<value_iterator, ListTy::iterator,
572 std::forward_iterator_tag, DIEValue> {
573 friend class const_value_iterator;
574 typedef iterator_adaptor_base<value_iterator, ListTy::iterator,
575 std::forward_iterator_tag,
576 DIEValue> iterator_adaptor;
579 value_iterator() = default;
580 explicit value_iterator(ListTy::iterator X) : iterator_adaptor(X) {}
582 explicit operator bool() const { return bool(wrapped()); }
583 DIEValue &operator*() const { return wrapped()->V; }
586 class const_value_iterator : public iterator_adaptor_base<
587 const_value_iterator, ListTy::const_iterator,
588 std::forward_iterator_tag, const DIEValue> {
589 typedef iterator_adaptor_base<const_value_iterator, ListTy::const_iterator,
590 std::forward_iterator_tag,
591 const DIEValue> iterator_adaptor;
594 const_value_iterator() = default;
595 const_value_iterator(DIEValueList::value_iterator X)
596 : iterator_adaptor(X.wrapped()) {}
597 explicit const_value_iterator(ListTy::const_iterator X)
598 : iterator_adaptor(X) {}
600 explicit operator bool() const { return bool(wrapped()); }
601 const DIEValue &operator*() const { return wrapped()->V; }
604 typedef iterator_range<value_iterator> value_range;
605 typedef iterator_range<const_value_iterator> const_value_range;
607 value_iterator addValue(BumpPtrAllocator &Alloc, DIEValue V) {
608 List.push_back(*new (Alloc) Node(V));
609 return value_iterator(ListTy::toIterator(List.back()));
612 value_iterator addValue(BumpPtrAllocator &Alloc, dwarf::Attribute Attribute,
613 dwarf::Form Form, T &&Value) {
614 return addValue(Alloc, DIEValue(Attribute, Form, std::forward<T>(Value)));
617 value_range values() {
618 return llvm::make_range(value_iterator(List.begin()),
619 value_iterator(List.end()));
621 const_value_range values() const {
622 return llvm::make_range(const_value_iterator(List.begin()),
623 const_value_iterator(List.end()));
627 //===--------------------------------------------------------------------===//
628 /// DIE - A structured debug information entry. Has an abbreviation which
629 /// describes its organization.
630 class DIE : IntrusiveBackListNode, public DIEValueList {
631 friend class IntrusiveBackList<DIE>;
633 /// Offset - Offset in debug info section.
637 /// Size - Size of instance + children.
641 unsigned AbbrevNumber = ~0u;
643 /// Tag - Dwarf tag code.
645 dwarf::Tag Tag = (dwarf::Tag)0;
648 IntrusiveBackList<DIE> Children;
650 DIE *Parent = nullptr;
653 explicit DIE(dwarf::Tag Tag) : Offset(0), Size(0), Tag(Tag) {}
656 static DIE *get(BumpPtrAllocator &Alloc, dwarf::Tag Tag) {
657 return new (Alloc) DIE(Tag);
661 unsigned getAbbrevNumber() const { return AbbrevNumber; }
662 dwarf::Tag getTag() const { return Tag; }
663 unsigned getOffset() const { return Offset; }
664 unsigned getSize() const { return Size; }
665 bool hasChildren() const { return !Children.empty(); }
667 typedef IntrusiveBackList<DIE>::iterator child_iterator;
668 typedef IntrusiveBackList<DIE>::const_iterator const_child_iterator;
669 typedef iterator_range<child_iterator> child_range;
670 typedef iterator_range<const_child_iterator> const_child_range;
672 child_range children() {
673 return llvm::make_range(Children.begin(), Children.end());
675 const_child_range children() const {
676 return llvm::make_range(Children.begin(), Children.end());
679 DIE *getParent() const { return Parent; }
681 /// Generate the abbreviation for this DIE.
683 /// Calculate the abbreviation for this, which should be uniqued and
684 /// eventually used to call \a setAbbrevNumber().
685 DIEAbbrev generateAbbrev() const;
687 /// Set the abbreviation number for this DIE.
688 void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
690 /// Climb up the parent chain to get the compile or type unit DIE this DIE
692 const DIE *getUnit() const;
693 /// Similar to getUnit, returns null when DIE is not added to an
695 const DIE *getUnitOrNull() const;
696 void setOffset(unsigned O) { Offset = O; }
697 void setSize(unsigned S) { Size = S; }
699 /// Add a child to the DIE.
700 DIE &addChild(DIE *Child) {
701 assert(!Child->getParent() && "Child should be orphaned");
702 Child->Parent = this;
703 Children.push_back(*Child);
704 return Children.back();
707 /// Find a value in the DIE with the attribute given.
709 /// Returns a default-constructed DIEValue (where \a DIEValue::getType()
710 /// gives \a DIEValue::isNone) if no such attribute exists.
711 DIEValue findAttribute(dwarf::Attribute Attribute) const;
714 void print(raw_ostream &O, unsigned IndentCount = 0) const;
719 //===--------------------------------------------------------------------===//
720 /// DIELoc - Represents an expression location.
722 class DIELoc : public DIEValueList {
723 mutable unsigned Size; // Size in bytes excluding size header.
726 DIELoc() : Size(0) {}
728 /// ComputeSize - Calculate the size of the location expression.
730 unsigned ComputeSize(const AsmPrinter *AP) const;
732 /// BestForm - Choose the best form for data.
734 dwarf::Form BestForm(unsigned DwarfVersion) const {
735 if (DwarfVersion > 3)
736 return dwarf::DW_FORM_exprloc;
737 // Pre-DWARF4 location expressions were blocks and not exprloc.
738 if ((unsigned char)Size == Size)
739 return dwarf::DW_FORM_block1;
740 if ((unsigned short)Size == Size)
741 return dwarf::DW_FORM_block2;
742 if ((unsigned int)Size == Size)
743 return dwarf::DW_FORM_block4;
744 return dwarf::DW_FORM_block;
747 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
748 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
751 void print(raw_ostream &O) const;
755 //===--------------------------------------------------------------------===//
756 /// DIEBlock - Represents a block of values.
758 class DIEBlock : public DIEValueList {
759 mutable unsigned Size; // Size in bytes excluding size header.
762 DIEBlock() : Size(0) {}
764 /// ComputeSize - Calculate the size of the location expression.
766 unsigned ComputeSize(const AsmPrinter *AP) const;
768 /// BestForm - Choose the best form for data.
770 dwarf::Form BestForm() const {
771 if ((unsigned char)Size == Size)
772 return dwarf::DW_FORM_block1;
773 if ((unsigned short)Size == Size)
774 return dwarf::DW_FORM_block2;
775 if ((unsigned int)Size == Size)
776 return dwarf::DW_FORM_block4;
777 return dwarf::DW_FORM_block;
780 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
781 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
784 void print(raw_ostream &O) const;
788 } // end llvm namespace