DIE.h 33 KB

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