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"
31 //===--------------------------------------------------------------------===//
32 /// DIEAbbrevData - Dwarf abbreviation data, describes one attribute of a
33 /// Dwarf abbreviation.
35 /// Attribute - Dwarf attribute code.
37 dwarf::Attribute Attribute;
39 /// Form - Dwarf form code.
44 DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
47 dwarf::Attribute getAttribute() const { return Attribute; }
48 dwarf::Form getForm() const { return Form; }
50 /// Profile - Used to gather unique data for the abbreviation folding set.
52 void Profile(FoldingSetNodeID &ID) const;
55 //===--------------------------------------------------------------------===//
56 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
57 /// information object.
58 class DIEAbbrev : public FoldingSetNode {
59 /// Unique number for node.
63 /// Tag - Dwarf tag code.
67 /// Children - Whether or not this node has children.
69 // This cheats a bit in all of the uses since the values in the standard
70 // are 0 and 1 for no children and children respectively.
73 /// Data - Raw data bytes for abbreviation.
75 SmallVector<DIEAbbrevData, 12> Data;
78 DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C), Data() {}
81 dwarf::Tag getTag() const { return Tag; }
82 unsigned getNumber() const { return Number; }
83 bool hasChildren() const { return Children; }
84 const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
85 void setChildrenFlag(bool hasChild) { Children = hasChild; }
86 void setNumber(unsigned N) { Number = N; }
88 /// AddAttribute - Adds another set of attribute information to the
90 void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
91 Data.push_back(DIEAbbrevData(Attribute, Form));
94 /// Profile - Used to gather unique data for the abbreviation folding set.
96 void Profile(FoldingSetNodeID &ID) const;
98 /// Emit - Print the abbreviation using the specified asm printer.
100 void Emit(const AsmPrinter *AP) const;
102 void print(raw_ostream &O);
106 //===--------------------------------------------------------------------===//
107 /// DIEInteger - An integer value DIE.
113 explicit DIEInteger(uint64_t I) : Integer(I) {}
115 /// BestForm - Choose the best form for integer.
117 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
119 const int64_t SignedInt = Int;
120 if ((char)Int == SignedInt)
121 return dwarf::DW_FORM_data1;
122 if ((short)Int == SignedInt)
123 return dwarf::DW_FORM_data2;
124 if ((int)Int == SignedInt)
125 return dwarf::DW_FORM_data4;
127 if ((unsigned char)Int == Int)
128 return dwarf::DW_FORM_data1;
129 if ((unsigned short)Int == Int)
130 return dwarf::DW_FORM_data2;
131 if ((unsigned int)Int == Int)
132 return dwarf::DW_FORM_data4;
134 return dwarf::DW_FORM_data8;
137 uint64_t getValue() const { return Integer; }
138 void setValue(uint64_t Val) { Integer = Val; }
140 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
141 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
143 void print(raw_ostream &O) const;
146 //===--------------------------------------------------------------------===//
147 /// DIEExpr - An expression DIE.
153 explicit DIEExpr(const MCExpr *E) : Expr(E) {}
155 /// getValue - Get MCExpr.
157 const MCExpr *getValue() const { return Expr; }
159 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
160 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
162 void print(raw_ostream &O) const;
165 //===--------------------------------------------------------------------===//
166 /// DIELabel - A label DIE.
169 const MCSymbol *Label;
172 explicit DIELabel(const MCSymbol *L) : Label(L) {}
174 /// getValue - Get MCSymbol.
176 const MCSymbol *getValue() const { return Label; }
178 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
179 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
181 void print(raw_ostream &O) const;
184 //===--------------------------------------------------------------------===//
185 /// DIEDelta - A simple label difference DIE.
188 const MCSymbol *LabelHi;
189 const MCSymbol *LabelLo;
192 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo) : LabelHi(Hi), LabelLo(Lo) {}
194 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
195 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
197 void print(raw_ostream &O) const;
200 //===--------------------------------------------------------------------===//
201 /// DIEString - A container for string values.
204 DwarfStringPoolEntryRef S;
207 DIEString(DwarfStringPoolEntryRef S) : S(S) {}
209 /// getString - Grab the string out of the object.
210 StringRef getString() const { return S.getString(); }
212 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
213 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
215 void print(raw_ostream &O) const;
218 //===--------------------------------------------------------------------===//
219 /// DIEEntry - A pointer to another debug information entry. An instance of
220 /// this class can also be used as a proxy for a debug information entry not
221 /// yet defined (ie. types.)
229 explicit DIEEntry(DIE &E) : Entry(&E) {}
231 DIE &getEntry() const { return *Entry; }
233 /// Returns size of a ref_addr entry.
234 static unsigned getRefAddrSize(const AsmPrinter *AP);
236 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
237 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
238 return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
242 void print(raw_ostream &O) const;
245 //===--------------------------------------------------------------------===//
246 /// DIELocList - Represents a pointer to a location list in the debug_loc
250 // Index into the .debug_loc vector.
254 DIELocList(size_t I) : Index(I) {}
256 /// getValue - Grab the current index out.
257 size_t getValue() const { return Index; }
259 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
260 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
262 void print(raw_ostream &O) const;
265 //===--------------------------------------------------------------------===//
266 /// DIEValue - A debug information entry value. Some of these roughly correlate
267 /// to DWARF attribute classes.
275 #define HANDLE_DIEVALUE(T) is##T,
276 #include "llvm/CodeGen/DIEValue.def"
280 /// Ty - Type of data stored in the value.
283 dwarf::Attribute Attribute = (dwarf::Attribute)0;
284 dwarf::Form Form = (dwarf::Form)0;
286 /// Storage for the value.
288 /// All values that aren't standard layout (or are larger than 8 bytes)
289 /// should be stored by reference instead of by value.
290 typedef AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
291 DIEDelta *, DIEEntry, DIEBlock *, DIELoc *,
294 static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
295 sizeof(ValTy) <= sizeof(void *),
296 "Expected all large types to be stored via pointer");
298 /// Underlying stored value.
301 template <class T> void construct(T V) {
302 static_assert(std::is_standard_layout<T>::value ||
303 std::is_pointer<T>::value,
304 "Expected standard layout or pointer");
305 new (reinterpret_cast<void *>(Val.buffer)) T(V);
308 template <class T> T *get() { return reinterpret_cast<T *>(Val.buffer); }
309 template <class T> const T *get() const {
310 return reinterpret_cast<const T *>(Val.buffer);
312 template <class T> void destruct() { get<T>()->~T(); }
314 /// Destroy the underlying value.
316 /// This should get optimized down to a no-op. We could skip it if we could
317 /// add a static assert on \a std::is_trivially_copyable(), but we currently
318 /// support versions of GCC that don't understand that.
323 #define HANDLE_DIEVALUE_SMALL(T) \
327 #define HANDLE_DIEVALUE_LARGE(T) \
329 destruct<const DIE##T *>();
331 #include "llvm/CodeGen/DIEValue.def"
335 /// Copy the underlying value.
337 /// This should get optimized down to a simple copy. We need to actually
338 /// construct the value, rather than calling memcpy, to satisfy strict
340 void copyVal(const DIEValue &X) {
344 #define HANDLE_DIEVALUE_SMALL(T) \
346 construct<DIE##T>(*X.get<DIE##T>()); \
348 #define HANDLE_DIEVALUE_LARGE(T) \
350 construct<const DIE##T *>(*X.get<const DIE##T *>()); \
352 #include "llvm/CodeGen/DIEValue.def"
357 DIEValue() = default;
358 DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
361 DIEValue &operator=(const DIEValue &X) {
364 Attribute = X.Attribute;
369 ~DIEValue() { destroyVal(); }
371 #define HANDLE_DIEVALUE_SMALL(T) \
372 DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T &V) \
373 : Ty(is##T), Attribute(Attribute), Form(Form) { \
374 construct<DIE##T>(V); \
376 #define HANDLE_DIEVALUE_LARGE(T) \
377 DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T *V) \
378 : Ty(is##T), Attribute(Attribute), Form(Form) { \
379 assert(V && "Expected valid value"); \
380 construct<const DIE##T *>(V); \
382 #include "llvm/CodeGen/DIEValue.def"
385 Type getType() const { return Ty; }
386 dwarf::Attribute getAttribute() const { return Attribute; }
387 dwarf::Form getForm() const { return Form; }
388 explicit operator bool() const { return Ty; }
390 #define HANDLE_DIEVALUE_SMALL(T) \
391 const DIE##T &getDIE##T() const { \
392 assert(getType() == is##T && "Expected " #T); \
393 return *get<DIE##T>(); \
395 #define HANDLE_DIEVALUE_LARGE(T) \
396 const DIE##T &getDIE##T() const { \
397 assert(getType() == is##T && "Expected " #T); \
398 return **get<const DIE##T *>(); \
400 #include "llvm/CodeGen/DIEValue.def"
402 /// EmitValue - Emit value via the Dwarf writer.
404 void EmitValue(const AsmPrinter *AP) const;
406 /// SizeOf - Return the size of a value in bytes.
408 unsigned SizeOf(const AsmPrinter *AP) const;
410 void print(raw_ostream &O) const;
414 struct IntrusiveBackListNode {
415 PointerIntPair<IntrusiveBackListNode *, 1> Next;
416 IntrusiveBackListNode() : Next(this, true) {}
418 IntrusiveBackListNode *getNext() const {
419 return Next.getInt() ? nullptr : Next.getPointer();
423 struct IntrusiveBackListBase {
424 typedef IntrusiveBackListNode Node;
425 Node *Last = nullptr;
427 bool empty() const { return !Last; }
428 void push_back(Node &N) {
429 assert(N.Next.getPointer() == &N && "Expected unlinked node");
430 assert(N.Next.getInt() == true && "Expected unlinked node");
434 Last->Next.setPointerAndInt(&N, false);
440 template <class T> class IntrusiveBackList : IntrusiveBackListBase {
442 using IntrusiveBackListBase::empty;
443 void push_back(T &N) { IntrusiveBackListBase::push_back(N); }
444 T &back() { return *static_cast<T *>(Last); }
445 const T &back() const { return *static_cast<T *>(Last); }
447 class const_iterator;
449 : public iterator_facade_base<iterator, std::forward_iterator_tag, T> {
450 friend class const_iterator;
454 iterator() = default;
455 explicit iterator(T *N) : N(N) {}
457 iterator &operator++() {
462 explicit operator bool() const { return N; }
463 T &operator*() const { return *static_cast<T *>(N); }
465 bool operator==(const iterator &X) const { return N == X.N; }
466 bool operator!=(const iterator &X) const { return N != X.N; }
470 : public iterator_facade_base<const_iterator, std::forward_iterator_tag,
472 const Node *N = nullptr;
475 const_iterator() = default;
476 // Placate MSVC by explicitly scoping 'iterator'.
477 const_iterator(typename IntrusiveBackList<T>::iterator X) : N(X.N) {}
478 explicit const_iterator(const T *N) : N(N) {}
480 const_iterator &operator++() {
485 explicit operator bool() const { return N; }
486 const T &operator*() const { return *static_cast<const T *>(N); }
488 bool operator==(const const_iterator &X) const { return N == X.N; }
489 bool operator!=(const const_iterator &X) const { return N != X.N; }
493 return Last ? iterator(static_cast<T *>(Last->Next.getPointer())) : end();
495 const_iterator begin() const {
496 return const_cast<IntrusiveBackList *>(this)->begin();
498 iterator end() { return iterator(); }
499 const_iterator end() const { return const_iterator(); }
501 static iterator toIterator(T &N) { return iterator(&N); }
502 static const_iterator toIterator(const T &N) { return const_iterator(&N); }
505 /// A list of DIE values.
507 /// This is a singly-linked list, but instead of reversing the order of
508 /// insertion, we keep a pointer to the back of the list so we can push in
511 /// There are two main reasons to choose a linked list over a customized
512 /// vector-like data structure.
514 /// 1. For teardown efficiency, we want DIEs to be BumpPtrAllocated. Using a
515 /// linked list here makes this way easier to accomplish.
516 /// 2. Carrying an extra pointer per \a DIEValue isn't expensive. 45% of DIEs
517 /// have 2 or fewer values, and 90% have 5 or fewer. A vector would be
518 /// over-allocated by 50% on average anyway, the same cost as the
519 /// linked-list node.
521 struct Node : IntrusiveBackListNode {
523 explicit Node(DIEValue V) : V(V) {}
526 typedef IntrusiveBackList<Node> ListTy;
530 class const_value_iterator;
532 : public iterator_adaptor_base<value_iterator, ListTy::iterator,
533 std::forward_iterator_tag, DIEValue> {
534 friend class const_value_iterator;
535 typedef iterator_adaptor_base<value_iterator, ListTy::iterator,
536 std::forward_iterator_tag,
537 DIEValue> iterator_adaptor;
540 value_iterator() = default;
541 explicit value_iterator(ListTy::iterator X) : iterator_adaptor(X) {}
543 explicit operator bool() const { return bool(wrapped()); }
544 DIEValue &operator*() const { return wrapped()->V; }
547 class const_value_iterator : public iterator_adaptor_base<
548 const_value_iterator, ListTy::const_iterator,
549 std::forward_iterator_tag, const DIEValue> {
550 typedef iterator_adaptor_base<const_value_iterator, ListTy::const_iterator,
551 std::forward_iterator_tag,
552 const DIEValue> iterator_adaptor;
555 const_value_iterator() = default;
556 const_value_iterator(DIEValueList::value_iterator X)
557 : iterator_adaptor(X.wrapped()) {}
558 explicit const_value_iterator(ListTy::const_iterator X)
559 : iterator_adaptor(X) {}
561 explicit operator bool() const { return bool(wrapped()); }
562 const DIEValue &operator*() const { return wrapped()->V; }
565 typedef iterator_range<value_iterator> value_range;
566 typedef iterator_range<const_value_iterator> const_value_range;
568 value_iterator addValue(BumpPtrAllocator &Alloc, const DIEValue &V) {
569 List.push_back(*new (Alloc) Node(V));
570 return value_iterator(ListTy::toIterator(List.back()));
573 value_iterator addValue(BumpPtrAllocator &Alloc, dwarf::Attribute Attribute,
574 dwarf::Form Form, T &&Value) {
575 return addValue(Alloc, DIEValue(Attribute, Form, std::forward<T>(Value)));
578 value_range values() {
579 return llvm::make_range(value_iterator(List.begin()),
580 value_iterator(List.end()));
582 const_value_range values() const {
583 return llvm::make_range(const_value_iterator(List.begin()),
584 const_value_iterator(List.end()));
588 //===--------------------------------------------------------------------===//
589 /// DIE - A structured debug information entry. Has an abbreviation which
590 /// describes its organization.
591 class DIE : IntrusiveBackListNode, public DIEValueList {
592 friend class IntrusiveBackList<DIE>;
594 /// Offset - Offset in debug info section.
598 /// Size - Size of instance + children.
602 unsigned AbbrevNumber = ~0u;
604 /// Tag - Dwarf tag code.
606 dwarf::Tag Tag = (dwarf::Tag)0;
609 IntrusiveBackList<DIE> Children;
611 DIE *Parent = nullptr;
614 explicit DIE(dwarf::Tag Tag) : Offset(0), Size(0), Tag(Tag) {}
617 static DIE *get(BumpPtrAllocator &Alloc, dwarf::Tag Tag) {
618 return new (Alloc) DIE(Tag);
622 unsigned getAbbrevNumber() const { return AbbrevNumber; }
623 dwarf::Tag getTag() const { return Tag; }
624 unsigned getOffset() const { return Offset; }
625 unsigned getSize() const { return Size; }
626 bool hasChildren() const { return !Children.empty(); }
628 typedef IntrusiveBackList<DIE>::iterator child_iterator;
629 typedef IntrusiveBackList<DIE>::const_iterator const_child_iterator;
630 typedef iterator_range<child_iterator> child_range;
631 typedef iterator_range<const_child_iterator> const_child_range;
633 child_range children() {
634 return llvm::make_range(Children.begin(), Children.end());
636 const_child_range children() const {
637 return llvm::make_range(Children.begin(), Children.end());
640 DIE *getParent() const { return Parent; }
642 /// Generate the abbreviation for this DIE.
644 /// Calculate the abbreviation for this, which should be uniqued and
645 /// eventually used to call \a setAbbrevNumber().
646 DIEAbbrev generateAbbrev() const;
648 /// Set the abbreviation number for this DIE.
649 void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
651 /// Climb up the parent chain to get the compile or type unit DIE this DIE
653 const DIE *getUnit() const;
654 /// Similar to getUnit, returns null when DIE is not added to an
656 const DIE *getUnitOrNull() const;
657 void setOffset(unsigned O) { Offset = O; }
658 void setSize(unsigned S) { Size = S; }
660 /// Add a child to the DIE.
661 DIE &addChild(DIE *Child) {
662 assert(!Child->getParent() && "Child should be orphaned");
663 Child->Parent = this;
664 Children.push_back(*Child);
665 return Children.back();
668 /// Find a value in the DIE with the attribute given.
670 /// Returns a default-constructed DIEValue (where \a DIEValue::getType()
671 /// gives \a DIEValue::isNone) if no such attribute exists.
672 DIEValue findAttribute(dwarf::Attribute Attribute) const;
674 void print(raw_ostream &O, unsigned IndentCount = 0) const;
678 //===--------------------------------------------------------------------===//
679 /// DIELoc - Represents an expression location.
681 class DIELoc : public DIEValueList {
682 mutable unsigned Size; // Size in bytes excluding size header.
685 DIELoc() : Size(0) {}
687 /// ComputeSize - Calculate the size of the location expression.
689 unsigned ComputeSize(const AsmPrinter *AP) const;
691 /// BestForm - Choose the best form for data.
693 dwarf::Form BestForm(unsigned DwarfVersion) const {
694 if (DwarfVersion > 3)
695 return dwarf::DW_FORM_exprloc;
696 // Pre-DWARF4 location expressions were blocks and not exprloc.
697 if ((unsigned char)Size == Size)
698 return dwarf::DW_FORM_block1;
699 if ((unsigned short)Size == Size)
700 return dwarf::DW_FORM_block2;
701 if ((unsigned int)Size == Size)
702 return dwarf::DW_FORM_block4;
703 return dwarf::DW_FORM_block;
706 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
707 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
709 void print(raw_ostream &O) const;
712 //===--------------------------------------------------------------------===//
713 /// DIEBlock - Represents a block of values.
715 class DIEBlock : public DIEValueList {
716 mutable unsigned Size; // Size in bytes excluding size header.
719 DIEBlock() : Size(0) {}
721 /// ComputeSize - Calculate the size of the location expression.
723 unsigned ComputeSize(const AsmPrinter *AP) const;
725 /// BestForm - Choose the best form for data.
727 dwarf::Form BestForm() const {
728 if ((unsigned char)Size == Size)
729 return dwarf::DW_FORM_block1;
730 if ((unsigned short)Size == Size)
731 return dwarf::DW_FORM_block2;
732 if ((unsigned int)Size == Size)
733 return dwarf::DW_FORM_block4;
734 return dwarf::DW_FORM_block;
737 void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
738 unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
740 void print(raw_ostream &O) const;
743 } // end llvm namespace