Debian Codegraph @

suite/synctools/tablegen/include/llvm/CodeGen/DIE.h

raw symbols SCIP
1 //===- lib/CodeGen/DIE.h - DWARF Info Entries -------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Data structures for DWARF info entries.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_CODEGEN_DIE_H
14 #define LLVM_CODEGEN_DIE_H
15
16 #include "llvm/ADT/FoldingSet.h"
17 #include "llvm/ADT/PointerIntPair.h"
18 #include "llvm/ADT/PointerUnion.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/iterator.h"
22 #include "llvm/ADT/iterator_range.h"
23 #include "llvm/BinaryFormat/Dwarf.h"
24 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
25 #include "llvm/Support/AlignOf.h"
26 #include "llvm/Support/Allocator.h"
27 #include <cassert>
28 #include <cstddef>
29 #include <cstdint>
30 #include <iterator>
31 #include <new>
32 #include <type_traits>
33 #include <utility>
34 #include <vector>
35
36 namespace llvm {
37
38 class AsmPrinter;
39 class DIE;
40 class DIEUnit;
41 class DwarfCompileUnit;
42 class MCExpr;
43 class MCSection;
44 class MCSymbol;
45 class raw_ostream;
46
47 //===--------------------------------------------------------------------===//
48 /// Dwarf abbreviation data, describes one attribute of a Dwarf abbreviation.
49 class DIEAbbrevData {
50 /// Dwarf attribute code.
51 dwarf::Attribute Attribute;
52
53 /// Dwarf form code.
54 dwarf::Form Form;
55
56 /// Dwarf attribute value for DW_FORM_implicit_const
57 int64_t Value = 0;
58
59 public:
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) {}
64
65 /// Accessors.
66 /// @{
67 dwarf::Attribute getAttribute() const { return Attribute; }
68 dwarf::Form getForm() const { return Form; }
69 int64_t getValue() const { return Value; }
70 /// @}
71
72 /// Used to gather unique data for the abbreviation folding set.
73 void Profile(FoldingSetNodeID &ID) const;
74 };
75
76 //===--------------------------------------------------------------------===//
77 /// Dwarf abbreviation, describes the organization of a debug information
78 /// object.
79 class DIEAbbrev : public FoldingSetNode {
80 /// Unique number for node.
81 unsigned Number = 0;
82
83 /// Dwarf tag code.
84 dwarf::Tag Tag;
85
86 /// Whether or not this node has children.
87 ///
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.
90 bool Children;
91
92 /// Raw data bytes for abbreviation.
93 SmallVector<DIEAbbrevData, 12> Data;
94
95 public:
96 DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C) {}
97
98 /// Accessors.
99 /// @{
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; }
106 /// @}
107
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));
111 }
112
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));
116 }
117
118 /// Used to gather unique data for the abbreviation folding set.
119 void Profile(FoldingSetNodeID &ID) const;
120
121 /// Print the abbreviation using the specified asm printer.
122 void Emit(const AsmPrinter *AP) const;
123
124 void print(raw_ostream &O) const;
125 void dump() const;
126 };
127
128 //===--------------------------------------------------------------------===//
129 /// Helps unique DIEAbbrev objects and assigns abbreviation numbers.
130 ///
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.
135 class DIEAbbrevSet {
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;
143
144 public:
145 DIEAbbrevSet(BumpPtrAllocator &A) : Alloc(A) {}
146 ~DIEAbbrevSet();
147
148 /// Generate the abbreviation declaration for a DIE and return a pointer to
149 /// the generated abbreviation.
150 ///
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);
155
156 /// Print all abbreviations using the specified asm printer.
157 void Emit(const AsmPrinter *AP, MCSection *Section) const;
158 };
159
160 //===--------------------------------------------------------------------===//
161 /// An integer value DIE.
162 ///
163 class DIEInteger {
164 uint64_t Integer;
165
166 public:
167 explicit DIEInteger(uint64_t I) : Integer(I) {}
168
169 /// Choose the best form for integer.
170 static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
171 if (IsSigned) {
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;
179 } else {
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;
186 }
187 return dwarf::DW_FORM_data8;
188 }
189
190 uint64_t getValue() const { return Integer; }
191 void setValue(uint64_t Val) { Integer = Val; }
192
193 void emitValue(const AsmPrinter *Asm, dwarf::Form Form) const;
194 unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const;
195
196 void print(raw_ostream &O) const;
197 };
198
199 //===--------------------------------------------------------------------===//
200 /// An expression DIE.
201 class DIEExpr {
202 const MCExpr *Expr;
203
204 public:
205 explicit DIEExpr(const MCExpr *E) : Expr(E) {}
206
207 /// Get MCExpr.
208 const MCExpr *getValue() const { return Expr; }
209
210 void emitValue(const AsmPrinter *AP, dwarf::Form Form) const;
211 unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const;
212
213 void print(raw_ostream &O) const;
214 };
215
216 //===--------------------------------------------------------------------===//
217 /// A label DIE.
218 class DIELabel {
219 const MCSymbol *Label;
220
221 public:
222 explicit DIELabel(const MCSymbol *L) : Label(L) {}
223
224 /// Get MCSymbol.
225 const MCSymbol *getValue() const { return Label; }
226
227 void emitValue(const AsmPrinter *AP, dwarf::Form Form) const;
228 unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const;
229
230 void print(raw_ostream &O) const;
231 };
232
233 //===--------------------------------------------------------------------===//
234 /// A BaseTypeRef DIE.
235 class DIEBaseTypeRef {
236 const DwarfCompileUnit *CU;
237 const uint64_t Index;
238 static constexpr unsigned ULEB128PadSize = 4;
239
240 public:
241 explicit DIEBaseTypeRef(const DwarfCompileUnit *TheCU, uint64_t Idx)
242 : CU(TheCU), Index(Idx) {}
243
244 /// EmitValue - Emit base type reference.
245 void emitValue(const AsmPrinter *AP, dwarf::Form Form) const;
246 /// sizeOf - Determine size of the base type reference in bytes.
247 unsigned sizeOf(const dwarf::FormParams &, dwarf::Form) const;
248
249 void print(raw_ostream &O) const;
250 uint64_t getIndex() const { return Index; }
251 };
252
253 //===--------------------------------------------------------------------===//
254 /// A simple label difference DIE.
255 ///
256 class DIEDelta {
257 const MCSymbol *LabelHi;
258 const MCSymbol *LabelLo;
259
260 public:
261 DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo) : LabelHi(Hi), LabelLo(Lo) {}
262
263 void emitValue(const AsmPrinter *AP, dwarf::Form Form) const;
264 unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const;
265
266 void print(raw_ostream &O) const;
267 };
268
269 //===--------------------------------------------------------------------===//
270 /// A container for string pool string values.
271 ///
272 /// This class is used with the DW_FORM_strp and DW_FORM_GNU_str_index forms.
273 class DIEString {
274 DwarfStringPoolEntryRef S;
275
276 public:
277 DIEString(DwarfStringPoolEntryRef S) : S(S) {}
278
279 /// Grab the string out of the object.
280 StringRef getString() const { return S.getString(); }
281
282 void emitValue(const AsmPrinter *AP, dwarf::Form Form) const;
283 unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const;
284
285 void print(raw_ostream &O) const;
286 };
287
288 //===--------------------------------------------------------------------===//
289 /// A container for inline string values.
290 ///
291 /// This class is used with the DW_FORM_string form.
292 class DIEInlineString {
293 StringRef S;
294
295 public:
296 template <typename Allocator>
297 explicit DIEInlineString(StringRef Str, Allocator &A) : S(Str.copy(A)) {}
298
299 ~DIEInlineString() = default;
300
301 /// Grab the string out of the object.
302 StringRef getString() const { return S; }
303
304 void emitValue(const AsmPrinter *AP, dwarf::Form Form) const;
305 unsigned sizeOf(const dwarf::FormParams &, dwarf::Form) const;
306
307 void print(raw_ostream &O) const;
308 };
309
310 //===--------------------------------------------------------------------===//
311 /// A pointer to another debug information entry. An instance of this class can
312 /// also be used as a proxy for a debug information entry not yet defined
313 /// (ie. types.)
314 class DIEEntry {
315 DIE *Entry;
316
317 public:
318 DIEEntry() = delete;
319 explicit DIEEntry(DIE &E) : Entry(&E) {}
320
321 DIE &getEntry() const { return *Entry; }
322
323 void emitValue(const AsmPrinter *AP, dwarf::Form Form) const;
324 unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const;
325
326 void print(raw_ostream &O) const;
327 };
328
329 //===--------------------------------------------------------------------===//
330 /// Represents a pointer to a location list in the debug_loc
331 /// section.
332 class DIELocList {
333 /// Index into the .debug_loc vector.
334 size_t Index;
335
336 public:
337 DIELocList(size_t I) : Index(I) {}
338
339 /// Grab the current index out.
340 size_t getValue() const { return Index; }
341
342 void emitValue(const AsmPrinter *AP, dwarf::Form Form) const;
343 unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const;
344
345 void print(raw_ostream &O) const;
346 };
347
348 //===--------------------------------------------------------------------===//
349 /// A BaseTypeRef DIE.
350 class DIEAddrOffset {
351 DIEInteger Addr;
352 DIEDelta Offset;
353
354 public:
355 explicit DIEAddrOffset(uint64_t Idx, const MCSymbol *Hi, const MCSymbol *Lo)
356 : Addr(Idx), Offset(Hi, Lo) {}
357
358 void emitValue(const AsmPrinter *AP, dwarf::Form Form) const;
359 unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const;
360
361 void print(raw_ostream &O) const;
362 };
363
364 //===--------------------------------------------------------------------===//
365 /// A debug information entry value. Some of these roughly correlate
366 /// to DWARF attribute classes.
367 class DIEBlock;
368 class DIELoc;
369 class DIEValue {
370 public:
371 enum Type {
372 isNone,
373 #define HANDLE_DIEVALUE(T) is##T,
374 #include "llvm/CodeGen/DIEValue.def"
375 };
376
377 private:
378 /// Type of data stored in the value.
379 Type Ty = isNone;
380 dwarf::Attribute Attribute = (dwarf::Attribute)0;
381 dwarf::Form Form = (dwarf::Form)0;
382
383 /// Storage for the value.
384 ///
385 /// All values that aren't standard layout (or are larger than 8 bytes)
386 /// should be stored by reference instead of by value.
387 using ValTy =
388 AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
389 DIEDelta *, DIEEntry, DIEBlock *, DIELoc *,
390 DIELocList, DIEBaseTypeRef *, DIEAddrOffset *>;
391
392 static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
393 sizeof(ValTy) <= sizeof(void *),
394 "Expected all large types to be stored via pointer");
395
396 /// Underlying stored value.
397 ValTy Val;
398
399 template <class T> void construct(T V) {
400 static_assert(std::is_standard_layout<T>::value ||
401 std::is_pointer<T>::value,
402 "Expected standard layout or pointer");
403 new (reinterpret_cast<void *>(&Val)) T(V);
404 }
405
406 template <class T> T *get() { return reinterpret_cast<T *>(&Val); }
407 template <class T> const T *get() const {
408 return reinterpret_cast<const T *>(&Val);
409 }
410 template <class T> void destruct() { get<T>()->~T(); }
411
412 /// Destroy the underlying value.
413 ///
414 /// This should get optimized down to a no-op. We could skip it if we could
415 /// add a static assert on \a std::is_trivially_copyable(), but we currently
416 /// support versions of GCC that don't understand that.
417 void destroyVal() {
418 switch (Ty) {
419 case isNone:
420 return;
421 #define HANDLE_DIEVALUE_SMALL(T) \
422 case is##T: \
423 destruct<DIE##T>(); \
424 return;
425 #define HANDLE_DIEVALUE_LARGE(T) \
426 case is##T: \
427 destruct<const DIE##T *>(); \
428 return;
429 #include "llvm/CodeGen/DIEValue.def"
430 }
431 }
432
433 /// Copy the underlying value.
434 ///
435 /// This should get optimized down to a simple copy. We need to actually
436 /// construct the value, rather than calling memcpy, to satisfy strict
437 /// aliasing rules.
438 void copyVal(const DIEValue &X) {
439 switch (Ty) {
440 case isNone:
441 return;
442 #define HANDLE_DIEVALUE_SMALL(T) \
443 case is##T: \
444 construct<DIE##T>(*X.get<DIE##T>()); \
445 return;
446 #define HANDLE_DIEVALUE_LARGE(T) \
447 case is##T: \
448 construct<const DIE##T *>(*X.get<const DIE##T *>()); \
449 return;
450 #include "llvm/CodeGen/DIEValue.def"
451 }
452 }
453
454 public:
455 DIEValue() = default;
456
457 DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
458 copyVal(X);
459 }
460
461 DIEValue &operator=(const DIEValue &X) {
462 destroyVal();
463 Ty = X.Ty;
464 Attribute = X.Attribute;
465 Form = X.Form;
466 copyVal(X);
467 return *this;
468 }
469
470 ~DIEValue() { destroyVal(); }
471
472 #define HANDLE_DIEVALUE_SMALL(T) \
473 DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T &V) \
474 : Ty(is##T), Attribute(Attribute), Form(Form) { \
475 construct<DIE##T>(V); \
476 }
477 #define HANDLE_DIEVALUE_LARGE(T) \
478 DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T *V) \
479 : Ty(is##T), Attribute(Attribute), Form(Form) { \
480 assert(V && "Expected valid value"); \
481 construct<const DIE##T *>(V); \
482 }
483 #include "llvm/CodeGen/DIEValue.def"
484
485 /// Accessors.
486 /// @{
487 Type getType() const { return Ty; }
488 dwarf::Attribute getAttribute() const { return Attribute; }
489 dwarf::Form getForm() const { return Form; }
490 explicit operator bool() const { return Ty; }
491 /// @}
492
493 #define HANDLE_DIEVALUE_SMALL(T) \
494 const DIE##T &getDIE##T() const { \
495 assert(getType() == is##T && "Expected " #T); \
496 return *get<DIE##T>(); \
497 }
498 #define HANDLE_DIEVALUE_LARGE(T) \
499 const DIE##T &getDIE##T() const { \
500 assert(getType() == is##T && "Expected " #T); \
501 return **get<const DIE##T *>(); \
502 }
503 #include "llvm/CodeGen/DIEValue.def"
504
505 /// Emit value via the Dwarf writer.
506 void emitValue(const AsmPrinter *AP) const;
507
508 /// Return the size of a value in bytes.
509 unsigned sizeOf(const dwarf::FormParams &FormParams) const;
510
511 void print(raw_ostream &O) const;
512 void dump() const;
513 };
514
515 struct IntrusiveBackListNode {
516 PointerIntPair<IntrusiveBackListNode *, 1> Next;
517
518 IntrusiveBackListNode() : Next(this, true) {}
519
520 IntrusiveBackListNode *getNext() const {
521 return Next.getInt() ? nullptr : Next.getPointer();
522 }
523 };
524
525 struct IntrusiveBackListBase {
526 using Node = IntrusiveBackListNode;
527
528 Node *Last = nullptr;
529
530 bool empty() const { return !Last; }
531
532 void push_back(Node &N) {
533 assert(N.Next.getPointer() == &N && "Expected unlinked node");
534 assert(N.Next.getInt() == true && "Expected unlinked node");
535
536 if (Last) {
537 N.Next = Last->Next;
538 Last->Next.setPointerAndInt(&N, false);
539 }
540 Last = &N;
541 }
542
543 void push_front(Node &N) {
544 assert(N.Next.getPointer() == &N && "Expected unlinked node");
545 assert(N.Next.getInt() == true && "Expected unlinked node");
546
547 if (Last) {
548 N.Next.setPointerAndInt(Last->Next.getPointer(), false);
549 Last->Next.setPointerAndInt(&N, true);
550 } else {
551 Last = &N;
552 }
553 }
554 };
555
556 template <class T> class IntrusiveBackList : IntrusiveBackListBase {
557 public:
558 using IntrusiveBackListBase::empty;
559
560 void push_back(T &N) { IntrusiveBackListBase::push_back(N); }
561 void push_front(T &N) { IntrusiveBackListBase::push_front(N); }
562 T &back() { return *static_cast<T *>(Last); }
563 const T &back() const { return *static_cast<T *>(Last); }
564 T &front() {
565 return *static_cast<T *>(Last ? Last->Next.getPointer() : nullptr);
566 }
567 const T &front() const {
568 return *static_cast<T *>(Last ? Last->Next.getPointer() : nullptr);
569 }
570
571 void takeNodes(IntrusiveBackList<T> &Other) {
572 if (Other.empty())
573 return;
574
575 T *FirstNode = static_cast<T *>(Other.Last->Next.getPointer());
576 T *IterNode = FirstNode;
577 do {
578 // Keep a pointer to the node and increment the iterator.
579 T *TmpNode = IterNode;
580 IterNode = static_cast<T *>(IterNode->Next.getPointer());
581
582 // Unlink the node and push it back to this list.
583 TmpNode->Next.setPointerAndInt(TmpNode, true);
584 push_back(*TmpNode);
585 } while (IterNode != FirstNode);
586
587 Other.Last = nullptr;
588 }
589
590 class const_iterator;
591 class iterator
592 : public iterator_facade_base<iterator, std::forward_iterator_tag, T> {
593 friend class const_iterator;
594
595 Node *N = nullptr;
596
597 public:
598 iterator() = default;
599 explicit iterator(T *N) : N(N) {}
600
601 iterator &operator++() {
602 N = N->getNext();
603 return *this;
604 }
605
606 explicit operator bool() const { return N; }
607 T &operator*() const { return *static_cast<T *>(N); }
608
609 bool operator==(const iterator &X) const { return N == X.N; }
610 };
611
612 class const_iterator
613 : public iterator_facade_base<const_iterator, std::forward_iterator_tag,
614 const T> {
615 const Node *N = nullptr;
616
617 public:
618 const_iterator() = default;
619 // Placate MSVC by explicitly scoping 'iterator'.
620 const_iterator(typename IntrusiveBackList<T>::iterator X) : N(X.N) {}
621 explicit const_iterator(const T *N) : N(N) {}
622
623 const_iterator &operator++() {
624 N = N->getNext();
625 return *this;
626 }
627
628 explicit operator bool() const { return N; }
629 const T &operator*() const { return *static_cast<const T *>(N); }
630
631 bool operator==(const const_iterator &X) const { return N == X.N; }
632 };
633
634 iterator begin() {
635 return Last ? iterator(static_cast<T *>(Last->Next.getPointer())) : end();
636 }
637 const_iterator begin() const {
638 return const_cast<IntrusiveBackList *>(this)->begin();
639 }
640 iterator end() { return iterator(); }
641 const_iterator end() const { return const_iterator(); }
642
643 static iterator toIterator(T &N) { return iterator(&N); }
644 static const_iterator toIterator(const T &N) { return const_iterator(&N); }
645 };
646
647 /// A list of DIE values.
648 ///
649 /// This is a singly-linked list, but instead of reversing the order of
650 /// insertion, we keep a pointer to the back of the list so we can push in
651 /// order.
652 ///
653 /// There are two main reasons to choose a linked list over a customized
654 /// vector-like data structure.
655 ///
656 /// 1. For teardown efficiency, we want DIEs to be BumpPtrAllocated. Using a
657 /// linked list here makes this way easier to accomplish.
658 /// 2. Carrying an extra pointer per \a DIEValue isn't expensive. 45% of DIEs
659 /// have 2 or fewer values, and 90% have 5 or fewer. A vector would be
660 /// over-allocated by 50% on average anyway, the same cost as the
661 /// linked-list node.
662 class DIEValueList {
663 struct Node : IntrusiveBackListNode {
664 DIEValue V;
665
666 explicit Node(DIEValue V) : V(V) {}
667 };
668
669 using ListTy = IntrusiveBackList<Node>;
670
671 ListTy List;
672
673 public:
674 class const_value_iterator;
675 class value_iterator
676 : public iterator_adaptor_base<value_iterator, ListTy::iterator,
677 std::forward_iterator_tag, DIEValue> {
678 friend class const_value_iterator;
679
680 using iterator_adaptor =
681 iterator_adaptor_base<value_iterator, ListTy::iterator,
682 std::forward_iterator_tag, DIEValue>;
683
684 public:
685 value_iterator() = default;
686 explicit value_iterator(ListTy::iterator X) : iterator_adaptor(X) {}
687
688 explicit operator bool() const { return bool(wrapped()); }
689 DIEValue &operator*() const { return wrapped()->V; }
690 };
691
692 class const_value_iterator : public iterator_adaptor_base<
693 const_value_iterator, ListTy::const_iterator,
694 std::forward_iterator_tag, const DIEValue> {
695 using iterator_adaptor =
696 iterator_adaptor_base<const_value_iterator, ListTy::const_iterator,
697 std::forward_iterator_tag, const DIEValue>;
698
699 public:
700 const_value_iterator() = default;
701 const_value_iterator(DIEValueList::value_iterator X)
702 : iterator_adaptor(X.wrapped()) {}
703 explicit const_value_iterator(ListTy::const_iterator X)
704 : iterator_adaptor(X) {}
705
706 explicit operator bool() const { return bool(wrapped()); }
707 const DIEValue &operator*() const { return wrapped()->V; }
708 };
709
710 using value_range = iterator_range<value_iterator>;
711 using const_value_range = iterator_range<const_value_iterator>;
712
713 value_iterator addValue(BumpPtrAllocator &Alloc, const DIEValue &V) {
714 List.push_back(*new (Alloc) Node(V));
715 return value_iterator(ListTy::toIterator(List.back()));
716 }
717 template <class T>
718 value_iterator addValue(BumpPtrAllocator &Alloc, dwarf::Attribute Attribute,
719 dwarf::Form Form, T &&Value) {
720 return addValue(Alloc, DIEValue(Attribute, Form, std::forward<T>(Value)));
721 }
722
723 /// Take ownership of the nodes in \p Other, and append them to the back of
724 /// the list.
725 void takeValues(DIEValueList &Other) { List.takeNodes(Other.List); }
726
727 value_range values() {
728 return make_range(value_iterator(List.begin()), value_iterator(List.end()));
729 }
730 const_value_range values() const {
731 return make_range(const_value_iterator(List.begin()),
732 const_value_iterator(List.end()));
733 }
734 };
735
736 //===--------------------------------------------------------------------===//
737 /// A structured debug information entry. Has an abbreviation which
738 /// describes its organization.
739 class DIE : IntrusiveBackListNode, public DIEValueList {
740 friend class IntrusiveBackList<DIE>;
741 friend class DIEUnit;
742
743 /// Dwarf unit relative offset.
744 unsigned Offset = 0;
745 /// Size of instance + children.
746 unsigned Size = 0;
747 unsigned AbbrevNumber = ~0u;
748 /// Dwarf tag code.
749 dwarf::Tag Tag = (dwarf::Tag)0;
750 /// Set to true to force a DIE to emit an abbreviation that says it has
751 /// children even when it doesn't. This is used for unit testing purposes.
752 bool ForceChildren = false;
753 /// Children DIEs.
754 IntrusiveBackList<DIE> Children;
755
756 /// The owner is either the parent DIE for children of other DIEs, or a
757 /// DIEUnit which contains this DIE as its unit DIE.
758 PointerUnion<DIE *, DIEUnit *> Owner;
759
760 explicit DIE(dwarf::Tag Tag) : Tag(Tag) {}
761
762 public:
763 DIE() = delete;
764 DIE(const DIE &RHS) = delete;
765 DIE(DIE &&RHS) = delete;
766 DIE &operator=(const DIE &RHS) = delete;
767 DIE &operator=(const DIE &&RHS) = delete;
768
769 static DIE *get(BumpPtrAllocator &Alloc, dwarf::Tag Tag) {
770 return new (Alloc) DIE(Tag);
771 }
772
773 // Accessors.
774 unsigned getAbbrevNumber() const { return AbbrevNumber; }
775 dwarf::Tag getTag() const { return Tag; }
776 /// Get the compile/type unit relative offset of this DIE.
777 unsigned getOffset() const {
778 // A real Offset can't be zero because the unit headers are at offset zero.
779 assert(Offset && "Offset being queried before it's been computed.");
780 return Offset;
781 }
782 unsigned getSize() const {
783 // A real Size can't be zero because it includes the non-empty abbrev code.
784 assert(Size && "Size being queried before it's been ocmputed.");
785 return Size;
786 }
787 bool hasChildren() const { return ForceChildren || !Children.empty(); }
788 void setForceChildren(bool B) { ForceChildren = B; }
789
790 using child_iterator = IntrusiveBackList<DIE>::iterator;
791 using const_child_iterator = IntrusiveBackList<DIE>::const_iterator;
792 using child_range = iterator_range<child_iterator>;
793 using const_child_range = iterator_range<const_child_iterator>;
794
795 child_range children() {
796 return make_range(Children.begin(), Children.end());
797 }
798 const_child_range children() const {
799 return make_range(Children.begin(), Children.end());
800 }
801
802 DIE *getParent() const;
803
804 /// Generate the abbreviation for this DIE.
805 ///
806 /// Calculate the abbreviation for this, which should be uniqued and
807 /// eventually used to call \a setAbbrevNumber().
808 DIEAbbrev generateAbbrev() const;
809
810 /// Set the abbreviation number for this DIE.
811 void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
812
813 /// Get the absolute offset within the .debug_info or .debug_types section
814 /// for this DIE.
815 uint64_t getDebugSectionOffset() const;
816
817 /// Compute the offset of this DIE and all its children.
818 ///
819 /// This function gets called just before we are going to generate the debug
820 /// information and gives each DIE a chance to figure out its CU relative DIE
821 /// offset, unique its abbreviation and fill in the abbreviation code, and
822 /// return the unit offset that points to where the next DIE will be emitted
823 /// within the debug unit section. After this function has been called for all
824 /// DIE objects, the DWARF can be generated since all DIEs will be able to
825 /// properly refer to other DIE objects since all DIEs have calculated their
826 /// offsets.
827 ///
828 /// \param FormParams Used when calculating sizes.
829 /// \param AbbrevSet the abbreviation used to unique DIE abbreviations.
830 /// \param CUOffset the compile/type unit relative offset in bytes.
831 /// \returns the offset for the DIE that follows this DIE within the
832 /// current compile/type unit.
833 unsigned computeOffsetsAndAbbrevs(const dwarf::FormParams &FormParams,
834 DIEAbbrevSet &AbbrevSet, unsigned CUOffset);
835
836 /// Climb up the parent chain to get the compile unit or type unit DIE that
837 /// this DIE belongs to.
838 ///
839 /// \returns the compile or type unit DIE that owns this DIE, or NULL if
840 /// this DIE hasn't been added to a unit DIE.
841 const DIE *getUnitDie() const;
842
843 /// Climb up the parent chain to get the compile unit or type unit that this
844 /// DIE belongs to.
845 ///
846 /// \returns the DIEUnit that represents the compile or type unit that owns
847 /// this DIE, or NULL if this DIE hasn't been added to a unit DIE.
848 DIEUnit *getUnit() const;
849
850 void setOffset(unsigned O) { Offset = O; }
851 void setSize(unsigned S) { Size = S; }
852
853 /// Add a child to the DIE.
854 DIE &addChild(DIE *Child) {
855 assert(!Child->getParent() && "Child should be orphaned");
856 Child->Owner = this;
857 Children.push_back(*Child);
858 return Children.back();
859 }
860
861 DIE &addChildFront(DIE *Child) {
862 assert(!Child->getParent() && "Child should be orphaned");
863 Child->Owner = this;
864 Children.push_front(*Child);
865 return Children.front();
866 }
867
868 /// Find a value in the DIE with the attribute given.
869 ///
870 /// Returns a default-constructed DIEValue (where \a DIEValue::getType()
871 /// gives \a DIEValue::isNone) if no such attribute exists.
872 DIEValue findAttribute(dwarf::Attribute Attribute) const;
873
874 void print(raw_ostream &O, unsigned IndentCount = 0) const;
875 void dump() const;
876 };
877
878 //===--------------------------------------------------------------------===//
879 /// Represents a compile or type unit.
880 class DIEUnit {
881 /// The compile unit or type unit DIE. This variable must be an instance of
882 /// DIE so that we can calculate the DIEUnit from any DIE by traversing the
883 /// parent backchain and getting the Unit DIE, and then casting itself to a
884 /// DIEUnit. This allows us to be able to find the DIEUnit for any DIE without
885 /// having to store a pointer to the DIEUnit in each DIE instance.
886 DIE Die;
887 /// The section this unit will be emitted in. This may or may not be set to
888 /// a valid section depending on the client that is emitting DWARF.
889 MCSection *Section = nullptr;
890 uint64_t Offset = 0; /// .debug_info or .debug_types absolute section offset.
891 protected:
892 virtual ~DIEUnit() = default;
893
894 public:
895 explicit DIEUnit(dwarf::Tag UnitTag);
896 DIEUnit(const DIEUnit &RHS) = delete;
897 DIEUnit(DIEUnit &&RHS) = delete;
898 void operator=(const DIEUnit &RHS) = delete;
899 void operator=(const DIEUnit &&RHS) = delete;
900 /// Set the section that this DIEUnit will be emitted into.
901 ///
902 /// This function is used by some clients to set the section. Not all clients
903 /// that emit DWARF use this section variable.
904 void setSection(MCSection *Section) {
905 assert(!this->Section);
906 this->Section = Section;
907 }
908
909 virtual const MCSymbol *getCrossSectionRelativeBaseAddress() const {
910 return nullptr;
911 }
912
913 /// Return the section that this DIEUnit will be emitted into.
914 ///
915 /// \returns Section pointer which can be NULL.
916 MCSection *getSection() const { return Section; }
917 void setDebugSectionOffset(uint64_t O) { Offset = O; }
918 uint64_t getDebugSectionOffset() const { return Offset; }
919 DIE &getUnitDie() { return Die; }
920 const DIE &getUnitDie() const { return Die; }
921 };
922
923 struct BasicDIEUnit final : DIEUnit {
924 explicit BasicDIEUnit(dwarf::Tag UnitTag) : DIEUnit(UnitTag) {}
925 };
926
927 //===--------------------------------------------------------------------===//
928 /// DIELoc - Represents an expression location.
929 //
930 class DIELoc : public DIEValueList {
931 mutable unsigned Size = 0; // Size in bytes excluding size header.
932
933 public:
934 DIELoc() = default;
935
936 /// Calculate the size of the location expression.
937 unsigned computeSize(const dwarf::FormParams &FormParams) const;
938
939 // TODO: move setSize() and Size to DIEValueList.
940 void setSize(unsigned size) { Size = size; }
941
942 /// BestForm - Choose the best form for data.
943 ///
944 dwarf::Form BestForm(unsigned DwarfVersion) const {
945 if (DwarfVersion > 3)
946 return dwarf::DW_FORM_exprloc;
947 // Pre-DWARF4 location expressions were blocks and not exprloc.
948 if ((unsigned char)Size == Size)
949 return dwarf::DW_FORM_block1;
950 if ((unsigned short)Size == Size)
951 return dwarf::DW_FORM_block2;
952 if ((unsigned int)Size == Size)
953 return dwarf::DW_FORM_block4;
954 return dwarf::DW_FORM_block;
955 }
956
957 void emitValue(const AsmPrinter *Asm, dwarf::Form Form) const;
958 unsigned sizeOf(const dwarf::FormParams &, dwarf::Form Form) const;
959
960 void print(raw_ostream &O) const;
961 };
962
963 //===--------------------------------------------------------------------===//
964 /// DIEBlock - Represents a block of values.
965 //
966 class DIEBlock : public DIEValueList {
967 mutable unsigned Size = 0; // Size in bytes excluding size header.
968
969 public:
970 DIEBlock() = default;
971
972 /// Calculate the size of the location expression.
973 unsigned computeSize(const dwarf::FormParams &FormParams) const;
974
975 // TODO: move setSize() and Size to DIEValueList.
976 void setSize(unsigned size) { Size = size; }
977
978 /// BestForm - Choose the best form for data.
979 ///
980 dwarf::Form BestForm() const {
981 if ((unsigned char)Size == Size)
982 return dwarf::DW_FORM_block1;
983 if ((unsigned short)Size == Size)
984 return dwarf::DW_FORM_block2;
985 if ((unsigned int)Size == Size)
986 return dwarf::DW_FORM_block4;
987 return dwarf::DW_FORM_block;
988 }
989
990 void emitValue(const AsmPrinter *Asm, dwarf::Form Form) const;
991 unsigned sizeOf(const dwarf::FormParams &, dwarf::Form Form) const;
992
993 void print(raw_ostream &O) const;
994 };
995
996 } // end namespace llvm
997
998 #endif // LLVM_CODEGEN_DIE_H