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;
103 void print(raw_ostream &O);
107 //===--------------------------------------------------------------------===//
108 /// DIEInteger - An integer value DIE.
114 explicit DIEInteger(uint64_t I) : Integer(I) {}
116 /// BestForm - Choose the best form for integer.
118 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
120 const int64_t SignedInt = Int;
121 if ((char)Int == SignedInt)
122 return dwarf::DW_FORM_data1;
123 if ((short)Int == SignedInt)
124 return dwarf::DW_FORM_data2;
125 if ((int)Int == SignedInt)
126 return dwarf::DW_FORM_data4;
128 if ((unsigned char)Int == Int)
129 return dwarf::DW_FORM_data1;
130 if ((unsigned short)Int == Int)
131 return dwarf::DW_FORM_data2;
132 if ((unsigned int)Int == Int)
133 return dwarf::DW_FORM_data4;
135 return dwarf::DW_FORM_data8;
138 uint64_t getValue() const { return Integer; }
139 void setValue(uint64_t Val) { Integer = Val; }
141 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
142 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
144 void print(raw_ostream &O) const;
147 //===--------------------------------------------------------------------===//
148 /// DIEExpr - An expression DIE.
154 explicit DIEExpr(const MCExpr *E) : Expr(E) {}
156 /// getValue - Get MCExpr.
158 const MCExpr *getValue() const { return Expr; }
160 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
161 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
163 void print(raw_ostream &O) const;
166 //===--------------------------------------------------------------------===//
167 /// DIELabel - A label DIE.
170 const MCSymbol *Label;
173 explicit DIELabel(const MCSymbol *L) : Label(L) {}
175 /// getValue - Get MCSymbol.
177 const MCSymbol *getValue() const { return Label; }
179 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
180 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
182 void print(raw_ostream &O) const;
185 //===--------------------------------------------------------------------===//
186 /// DIEDelta - A simple label difference DIE.
189 const MCSymbol *LabelHi;
190 const MCSymbol *LabelLo;
193 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo) : LabelHi(Hi), LabelLo(Lo) {}
195 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
196 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
198 void print(raw_ostream &O) const;
201 //===--------------------------------------------------------------------===//
202 /// DIEString - A container for string values.
205 DwarfStringPoolEntryRef S;
208 DIEString(DwarfStringPoolEntryRef S) : S(S) {}
210 /// getString - Grab the string out of the object.
211 StringRef getString() const { return S.getString(); }
213 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
214 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
216 void print(raw_ostream &O) const;
219 //===--------------------------------------------------------------------===//
220 /// DIEEntry - A pointer to another debug information entry. An instance of
221 /// this class can also be used as a proxy for a debug information entry not
222 /// yet defined (ie. types.)
230 explicit DIEEntry(DIE &E) : Entry(&E) {}
232 DIE &getEntry() const { return *Entry; }
234 /// Returns size of a ref_addr entry.
235 static unsigned getRefAddrSize(const AsmPrinter *AP);
237 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
238 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
239 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
243 void print(raw_ostream &O) const;
246 //===--------------------------------------------------------------------===//
247 /// \brief A signature reference to a type unit.
248 class DIETypeSignature {
249 const DwarfTypeUnit *Unit;
251 DIETypeSignature() = delete;
254 explicit DIETypeSignature(const DwarfTypeUnit &Unit) : Unit(&Unit) {}
256 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
257 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
258 assert(Form == dwarf::DW_FORM_ref_sig8);
262 void print(raw_ostream &O) const;
265 //===--------------------------------------------------------------------===//
266 /// DIELocList - Represents a pointer to a location list in the debug_loc
270 // Index into the .debug_loc vector.
274 DIELocList(size_t I) : Index(I) {}
276 /// getValue - Grab the current index out.
277 size_t getValue() const { return Index; }
279 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
280 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
282 void print(raw_ostream &O) const;
285 //===--------------------------------------------------------------------===//
286 /// DIEValue - A debug information entry value. Some of these roughly correlate
287 /// to DWARF attribute classes.
295 #define HANDLE_DIEVALUE(T) is##T,
296 #include "llvm/CodeGen/DIEValue.def"
300 /// Ty - Type of data stored in the value.
303 dwarf::Attribute Attribute = (dwarf::Attribute)0;
304 dwarf::Form Form = (dwarf::Form)0;
306 /// Storage for the value.
308 /// All values that aren't standard layout (or are larger than 8 bytes)
309 /// should be stored by reference instead of by value.
310 typedef AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
311 DIEDelta *, DIEEntry, DIETypeSignature,
312 DIEBlock *, DIELoc *, DIELocList> ValTy;
313 static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
314 sizeof(ValTy) <= sizeof(void *),
315 "Expected all large types to be stored via pointer");
317 /// Underlying stored value.
320 template <class T> void construct(T V) {
321 static_assert(std::is_standard_layout<T>::value ||
322 std::is_pointer<T>::value,
323 "Expected standard layout or pointer");
324 new (reinterpret_cast<void *>(Val.buffer)) T(V);
327 template <class T> T *get() { return reinterpret_cast<T *>(Val.buffer); }
328 template <class T> const T *get() const {
329 return reinterpret_cast<const T *>(Val.buffer);
331 template <class T> void destruct() { get<T>()->~T(); }
333 /// Destroy the underlying value.
335 /// This should get optimized down to a no-op. We could skip it if we could
336 /// add a static assert on \a std::is_trivially_copyable(), but we currently
337 /// support versions of GCC that don't understand that.
342 #define HANDLE_DIEVALUE_SMALL(T) \
346 #define HANDLE_DIEVALUE_LARGE(T) \
348 destruct<const DIE##T *>();
350 #include "llvm/CodeGen/DIEValue.def"
354 /// Copy the underlying value.
356 /// This should get optimized down to a simple copy. We need to actually
357 /// construct the value, rather than calling memcpy, to satisfy strict
359 void copyVal(const DIEValue &X) {
363 #define HANDLE_DIEVALUE_SMALL(T) \
365 construct<DIE##T>(*X.get<DIE##T>()); \
367 #define HANDLE_DIEVALUE_LARGE(T) \
369 construct<const DIE##T *>(*X.get<const DIE##T *>()); \
371 #include "llvm/CodeGen/DIEValue.def"
376 DIEValue() = default;
377 DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
380 DIEValue &operator=(const DIEValue &X) {
383 Attribute = X.Attribute;
388 ~DIEValue() { destroyVal(); }
390 #define HANDLE_DIEVALUE_SMALL(T) \
391 DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T &V) \
392 : Ty(is##T), Attribute(Attribute), Form(Form) { \
393 construct<DIE##T>(V); \
395 #define HANDLE_DIEVALUE_LARGE(T) \
396 DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T *V) \
397 : Ty(is##T), Attribute(Attribute), Form(Form) { \
398 assert(V && "Expected valid value"); \
399 construct<const DIE##T *>(V); \
401 #include "llvm/CodeGen/DIEValue.def"
404 Type getType() const { return Ty; }
405 dwarf::Attribute getAttribute() const { return Attribute; }
406 dwarf::Form getForm() const { return Form; }
407 explicit operator bool() const { return Ty; }
409 #define HANDLE_DIEVALUE_SMALL(T) \
410 const DIE##T &getDIE##T() const { \
411 assert(getType() == is##T && "Expected " #T); \
412 return *get<DIE##T>(); \
414 #define HANDLE_DIEVALUE_LARGE(T) \
415 const DIE##T &getDIE##T() const { \
416 assert(getType() == is##T && "Expected " #T); \
417 return **get<const DIE##T *>(); \
419 #include "llvm/CodeGen/DIEValue.def"
421 /// EmitValue - Emit value via the Dwarf writer.
423 void EmitValue(const AsmPrinter *AP) const;
425 /// SizeOf - Return the size of a value in bytes.
427 unsigned SizeOf(const AsmPrinter *AP) const;
429 void print(raw_ostream &O) const;
433 struct IntrusiveBackListNode {
434 PointerIntPair<IntrusiveBackListNode *, 1> Next;
435 IntrusiveBackListNode() : Next(this, true) {}
437 IntrusiveBackListNode *getNext() const {
438 return Next.getInt() ? nullptr : Next.getPointer();
442 struct IntrusiveBackListBase {
443 typedef IntrusiveBackListNode Node;
444 Node *Last = nullptr;
446 bool empty() const { return !Last; }
447 void push_back(Node &N) {
448 assert(N.Next.getPointer() == &N && "Expected unlinked node");
449 assert(N.Next.getInt() == true && "Expected unlinked node");
453 Last->Next.setPointerAndInt(&N, false);
459 template <class T> class IntrusiveBackList : IntrusiveBackListBase {
461 using IntrusiveBackListBase::empty;
462 void push_back(T &N) { IntrusiveBackListBase::push_back(N); }
463 T &back() { return *static_cast<T *>(Last); }
464 const T &back() const { return *static_cast<T *>(Last); }
466 class const_iterator;
468 : public iterator_facade_base<iterator, std::forward_iterator_tag, T> {
469 friend class const_iterator;
473 iterator() = default;
474 explicit iterator(T *N) : N(N) {}
476 iterator &operator++() {
481 explicit operator bool() const { return N; }
482 T &operator*() const { return *static_cast<T *>(N); }
484 bool operator==(const iterator &X) const { return N == X.N; }
485 bool operator!=(const iterator &X) const { return N != X.N; }
489 : public iterator_facade_base<const_iterator, std::forward_iterator_tag,
491 const Node *N = nullptr;
494 const_iterator() = default;
495 // Placate MSVC by explicitly scoping 'iterator'.
496 const_iterator(typename IntrusiveBackList<T>::iterator X) : N(X.N) {}
497 explicit const_iterator(const T *N) : N(N) {}
499 const_iterator &operator++() {
504 explicit operator bool() const { return N; }
505 const T &operator*() const { return *static_cast<const T *>(N); }
507 bool operator==(const const_iterator &X) const { return N == X.N; }
508 bool operator!=(const const_iterator &X) const { return N != X.N; }
512 return Last ? iterator(static_cast<T *>(Last->Next.getPointer())) : end();
514 const_iterator begin() const {
515 return const_cast<IntrusiveBackList *>(this)->begin();
517 iterator end() { return iterator(); }
518 const_iterator end() const { return const_iterator(); }
520 static iterator toIterator(T &N) { return iterator(&N); }
521 static const_iterator toIterator(const T &N) { return const_iterator(&N); }
524 /// A list of DIE values.
526 /// This is a singly-linked list, but instead of reversing the order of
527 /// insertion, we keep a pointer to the back of the list so we can push in
530 /// There are two main reasons to choose a linked list over a customized
531 /// vector-like data structure.
533 /// 1. For teardown efficiency, we want DIEs to be BumpPtrAllocated. Using a
534 /// linked list here makes this way easier to accomplish.
535 /// 2. Carrying an extra pointer per \a DIEValue isn't expensive. 45% of DIEs
536 /// have 2 or fewer values, and 90% have 5 or fewer. A vector would be
537 /// over-allocated by 50% on average anyway, the same cost as the
538 /// linked-list node.
540 struct Node : IntrusiveBackListNode {
542 explicit Node(DIEValue V) : V(V) {}
545 typedef IntrusiveBackList<Node> ListTy;
549 class const_value_iterator;
551 : public iterator_adaptor_base<value_iterator, ListTy::iterator,
552 std::forward_iterator_tag, DIEValue> {
553 friend class const_value_iterator;
554 typedef iterator_adaptor_base<value_iterator, ListTy::iterator,
555 std::forward_iterator_tag,
556 DIEValue> iterator_adaptor;
559 value_iterator() = default;
560 explicit value_iterator(ListTy::iterator X) : iterator_adaptor(X) {}
562 explicit operator bool() const { return bool(wrapped()); }
563 DIEValue &operator*() const { return wrapped()->V; }
566 class const_value_iterator : public iterator_adaptor_base<
567 const_value_iterator, ListTy::const_iterator,
568 std::forward_iterator_tag, const DIEValue> {
569 typedef iterator_adaptor_base<const_value_iterator, ListTy::const_iterator,
570 std::forward_iterator_tag,
571 const DIEValue> iterator_adaptor;
574 const_value_iterator() = default;
575 const_value_iterator(DIEValueList::value_iterator X)
576 : iterator_adaptor(X.wrapped()) {}
577 explicit const_value_iterator(ListTy::const_iterator X)
578 : iterator_adaptor(X) {}
580 explicit operator bool() const { return bool(wrapped()); }
581 const DIEValue &operator*() const { return wrapped()->V; }
584 typedef iterator_range<value_iterator> value_range;
585 typedef iterator_range<const_value_iterator> const_value_range;
587 value_iterator addValue(BumpPtrAllocator &Alloc, DIEValue V) {
588 List.push_back(*new (Alloc) Node(V));
589 return value_iterator(ListTy::toIterator(List.back()));
592 value_iterator addValue(BumpPtrAllocator &Alloc, dwarf::Attribute Attribute,
593 dwarf::Form Form, T &&Value) {
594 return addValue(Alloc, DIEValue(Attribute, Form, std::forward<T>(Value)));
597 value_range values() {
598 return llvm::make_range(value_iterator(List.begin()),
599 value_iterator(List.end()));
601 const_value_range values() const {
602 return llvm::make_range(const_value_iterator(List.begin()),
603 const_value_iterator(List.end()));
607 //===--------------------------------------------------------------------===//
608 /// DIE - A structured debug information entry. Has an abbreviation which
609 /// describes its organization.
610 class DIE : IntrusiveBackListNode, public DIEValueList {
611 friend class IntrusiveBackList<DIE>;
613 /// Offset - Offset in debug info section.
617 /// Size - Size of instance + children.
621 unsigned AbbrevNumber = ~0u;
623 /// Tag - Dwarf tag code.
625 dwarf::Tag Tag = (dwarf::Tag)0;
628 IntrusiveBackList<DIE> Children;
630 DIE *Parent = nullptr;
633 explicit DIE(dwarf::Tag Tag) : Offset(0), Size(0), Tag(Tag) {}
636 static DIE *get(BumpPtrAllocator &Alloc, dwarf::Tag Tag) {
637 return new (Alloc) DIE(Tag);
641 unsigned getAbbrevNumber() const { return AbbrevNumber; }
642 dwarf::Tag getTag() const { return Tag; }
643 unsigned getOffset() const { return Offset; }
644 unsigned getSize() const { return Size; }
645 bool hasChildren() const { return !Children.empty(); }
647 typedef IntrusiveBackList<DIE>::iterator child_iterator;
648 typedef IntrusiveBackList<DIE>::const_iterator const_child_iterator;
649 typedef iterator_range<child_iterator> child_range;
650 typedef iterator_range<const_child_iterator> const_child_range;
652 child_range children() {
653 return llvm::make_range(Children.begin(), Children.end());
655 const_child_range children() const {
656 return llvm::make_range(Children.begin(), Children.end());
659 DIE *getParent() const { return Parent; }
661 /// Generate the abbreviation for this DIE.
663 /// Calculate the abbreviation for this, which should be uniqued and
664 /// eventually used to call \a setAbbrevNumber().
665 DIEAbbrev generateAbbrev() const;
667 /// Set the abbreviation number for this DIE.
668 void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
670 /// Climb up the parent chain to get the compile or type unit DIE this DIE
672 const DIE *getUnit() const;
673 /// Similar to getUnit, returns null when DIE is not added to an
675 const DIE *getUnitOrNull() const;
676 void setOffset(unsigned O) { Offset = O; }
677 void setSize(unsigned S) { Size = S; }
679 /// Add a child to the DIE.
680 DIE &addChild(DIE *Child) {
681 assert(!Child->getParent() && "Child should be orphaned");
682 Child->Parent = this;
683 Children.push_back(*Child);
684 return Children.back();
687 /// Find a value in the DIE with the attribute given.
689 /// Returns a default-constructed DIEValue (where \a DIEValue::getType()
690 /// gives \a DIEValue::isNone) if no such attribute exists.
691 DIEValue findAttribute(dwarf::Attribute Attribute) const;
693 void print(raw_ostream &O, unsigned IndentCount = 0) const;
697 //===--------------------------------------------------------------------===//
698 /// DIELoc - Represents an expression location.
700 class DIELoc : public DIEValueList {
701 mutable unsigned Size; // Size in bytes excluding size header.
704 DIELoc() : Size(0) {}
706 /// ComputeSize - Calculate the size of the location expression.
708 unsigned ComputeSize(const AsmPrinter *AP) const;
710 /// BestForm - Choose the best form for data.
712 dwarf::Form BestForm(unsigned DwarfVersion) const {
713 if (DwarfVersion > 3)
714 return dwarf::DW_FORM_exprloc;
715 // Pre-DWARF4 location expressions were blocks and not exprloc.
716 if ((unsigned char)Size == Size)
717 return dwarf::DW_FORM_block1;
718 if ((unsigned short)Size == Size)
719 return dwarf::DW_FORM_block2;
720 if ((unsigned int)Size == Size)
721 return dwarf::DW_FORM_block4;
722 return dwarf::DW_FORM_block;
725 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
726 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
728 void print(raw_ostream &O) const;
731 //===--------------------------------------------------------------------===//
732 /// DIEBlock - Represents a block of values.
734 class DIEBlock : public DIEValueList {
735 mutable unsigned Size; // Size in bytes excluding size header.
738 DIEBlock() : Size(0) {}
740 /// ComputeSize - Calculate the size of the location expression.
742 unsigned ComputeSize(const AsmPrinter *AP) const;
744 /// BestForm - Choose the best form for data.
746 dwarf::Form BestForm() const {
747 if ((unsigned char)Size == Size)
748 return dwarf::DW_FORM_block1;
749 if ((unsigned short)Size == Size)
750 return dwarf::DW_FORM_block2;
751 if ((unsigned int)Size == Size)
752 return dwarf::DW_FORM_block4;
753 return dwarf::DW_FORM_block;
756 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
757 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
759 void print(raw_ostream &O) const;
762 } // end llvm namespace