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