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