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