DIE.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
  14. #define LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
  15. #include "llvm/ADT/FoldingSet.h"
  16. #include "llvm/ADT/PointerIntPair.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/CodeGen/DwarfStringPoolEntry.h"
  20. #include "llvm/Support/Dwarf.h"
  21. #include <vector>
  22. namespace llvm {
  23. class AsmPrinter;
  24. class MCExpr;
  25. class MCSymbol;
  26. class raw_ostream;
  27. class DwarfTypeUnit;
  28. //===--------------------------------------------------------------------===//
  29. /// DIEAbbrevData - Dwarf abbreviation data, describes one attribute of a
  30. /// Dwarf abbreviation.
  31. class DIEAbbrevData {
  32. /// Attribute - Dwarf attribute code.
  33. ///
  34. dwarf::Attribute Attribute;
  35. /// Form - Dwarf form code.
  36. ///
  37. dwarf::Form Form;
  38. public:
  39. DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
  40. // Accessors.
  41. dwarf::Attribute getAttribute() const { return Attribute; }
  42. dwarf::Form getForm() const { return Form; }
  43. /// Profile - Used to gather unique data for the abbreviation folding set.
  44. ///
  45. void Profile(FoldingSetNodeID &ID) const;
  46. };
  47. //===--------------------------------------------------------------------===//
  48. /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
  49. /// information object.
  50. class DIEAbbrev : public FoldingSetNode {
  51. /// Unique number for node.
  52. ///
  53. unsigned Number;
  54. /// Tag - Dwarf tag code.
  55. ///
  56. dwarf::Tag Tag;
  57. /// Children - Whether or not this node has children.
  58. ///
  59. // This cheats a bit in all of the uses since the values in the standard
  60. // are 0 and 1 for no children and children respectively.
  61. bool Children;
  62. /// Data - Raw data bytes for abbreviation.
  63. ///
  64. SmallVector<DIEAbbrevData, 12> Data;
  65. public:
  66. DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C), Data() {}
  67. // Accessors.
  68. dwarf::Tag getTag() const { return Tag; }
  69. unsigned getNumber() const { return Number; }
  70. bool hasChildren() const { return Children; }
  71. const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
  72. void setChildrenFlag(bool hasChild) { Children = hasChild; }
  73. void setNumber(unsigned N) { Number = N; }
  74. /// AddAttribute - Adds another set of attribute information to the
  75. /// abbreviation.
  76. void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
  77. Data.push_back(DIEAbbrevData(Attribute, Form));
  78. }
  79. /// Profile - Used to gather unique data for the abbreviation folding set.
  80. ///
  81. void Profile(FoldingSetNodeID &ID) const;
  82. /// Emit - Print the abbreviation using the specified asm printer.
  83. ///
  84. void Emit(const AsmPrinter *AP) const;
  85. #ifndef NDEBUG
  86. void print(raw_ostream &O);
  87. void dump();
  88. #endif
  89. };
  90. //===--------------------------------------------------------------------===//
  91. /// DIEInteger - An integer value DIE.
  92. ///
  93. class DIEInteger {
  94. uint64_t Integer;
  95. public:
  96. explicit DIEInteger(uint64_t I) : Integer(I) {}
  97. /// BestForm - Choose the best form for integer.
  98. ///
  99. static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
  100. if (IsSigned) {
  101. const int64_t SignedInt = Int;
  102. if ((char)Int == SignedInt)
  103. return dwarf::DW_FORM_data1;
  104. if ((short)Int == SignedInt)
  105. return dwarf::DW_FORM_data2;
  106. if ((int)Int == SignedInt)
  107. return dwarf::DW_FORM_data4;
  108. } else {
  109. if ((unsigned char)Int == Int)
  110. return dwarf::DW_FORM_data1;
  111. if ((unsigned short)Int == Int)
  112. return dwarf::DW_FORM_data2;
  113. if ((unsigned int)Int == Int)
  114. return dwarf::DW_FORM_data4;
  115. }
  116. return dwarf::DW_FORM_data8;
  117. }
  118. uint64_t getValue() const { return Integer; }
  119. void setValue(uint64_t Val) { Integer = Val; }
  120. void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
  121. unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
  122. #ifndef NDEBUG
  123. void print(raw_ostream &O) const;
  124. #endif
  125. };
  126. //===--------------------------------------------------------------------===//
  127. /// DIEExpr - An expression DIE.
  128. //
  129. class DIEExpr {
  130. const MCExpr *Expr;
  131. public:
  132. explicit DIEExpr(const MCExpr *E) : Expr(E) {}
  133. /// getValue - Get MCExpr.
  134. ///
  135. const MCExpr *getValue() const { return Expr; }
  136. void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
  137. unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
  138. #ifndef NDEBUG
  139. void print(raw_ostream &O) const;
  140. #endif
  141. };
  142. //===--------------------------------------------------------------------===//
  143. /// DIELabel - A label DIE.
  144. //
  145. class DIELabel {
  146. const MCSymbol *Label;
  147. public:
  148. explicit DIELabel(const MCSymbol *L) : Label(L) {}
  149. /// getValue - Get MCSymbol.
  150. ///
  151. const MCSymbol *getValue() const { return Label; }
  152. void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
  153. unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
  154. #ifndef NDEBUG
  155. void print(raw_ostream &O) const;
  156. #endif
  157. };
  158. //===--------------------------------------------------------------------===//
  159. /// DIEDelta - A simple label difference DIE.
  160. ///
  161. class DIEDelta {
  162. const MCSymbol *LabelHi;
  163. const MCSymbol *LabelLo;
  164. public:
  165. DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo) : LabelHi(Hi), LabelLo(Lo) {}
  166. void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
  167. unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
  168. #ifndef NDEBUG
  169. void print(raw_ostream &O) const;
  170. #endif
  171. };
  172. //===--------------------------------------------------------------------===//
  173. /// DIEString - A container for string values.
  174. ///
  175. class DIEString {
  176. DwarfStringPoolEntryRef S;
  177. public:
  178. DIEString(DwarfStringPoolEntryRef S) : S(S) {}
  179. /// getString - Grab the string out of the object.
  180. StringRef getString() const { return S.getString(); }
  181. void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
  182. unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
  183. #ifndef NDEBUG
  184. void print(raw_ostream &O) const;
  185. #endif
  186. };
  187. //===--------------------------------------------------------------------===//
  188. /// DIEEntry - A pointer to another debug information entry. An instance of
  189. /// this class can also be used as a proxy for a debug information entry not
  190. /// yet defined (ie. types.)
  191. class DIE;
  192. class DIEEntry {
  193. DIE *Entry;
  194. DIEEntry() = delete;
  195. public:
  196. explicit DIEEntry(DIE &E) : Entry(&E) {}
  197. DIE &getEntry() const { return *Entry; }
  198. /// Returns size of a ref_addr entry.
  199. static unsigned getRefAddrSize(const AsmPrinter *AP);
  200. void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
  201. unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  202. return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
  203. : sizeof(int32_t);
  204. }
  205. #ifndef NDEBUG
  206. void print(raw_ostream &O) const;
  207. #endif
  208. };
  209. //===--------------------------------------------------------------------===//
  210. /// \brief A signature reference to a type unit.
  211. class DIETypeSignature {
  212. const DwarfTypeUnit *Unit;
  213. DIETypeSignature() = delete;
  214. public:
  215. explicit DIETypeSignature(const DwarfTypeUnit &Unit) : Unit(&Unit) {}
  216. void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
  217. unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  218. assert(Form == dwarf::DW_FORM_ref_sig8);
  219. return 8;
  220. }
  221. #ifndef NDEBUG
  222. void print(raw_ostream &O) const;
  223. #endif
  224. };
  225. //===--------------------------------------------------------------------===//
  226. /// DIELocList - Represents a pointer to a location list in the debug_loc
  227. /// section.
  228. //
  229. class DIELocList {
  230. // Index into the .debug_loc vector.
  231. size_t Index;
  232. public:
  233. DIELocList(size_t I) : Index(I) {}
  234. /// getValue - Grab the current index out.
  235. size_t getValue() const { return Index; }
  236. void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
  237. unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
  238. #ifndef NDEBUG
  239. void print(raw_ostream &O) const;
  240. #endif
  241. };
  242. //===--------------------------------------------------------------------===//
  243. /// DIEValue - A debug information entry value. Some of these roughly correlate
  244. /// to DWARF attribute classes.
  245. ///
  246. class DIEBlock;
  247. class DIELoc;
  248. class DIEValue {
  249. public:
  250. enum Type {
  251. isNone,
  252. #define HANDLE_DIEVALUE(T) is##T,
  253. #include "llvm/CodeGen/DIEValue.def"
  254. };
  255. private:
  256. /// Ty - Type of data stored in the value.
  257. ///
  258. Type Ty = isNone;
  259. dwarf::Attribute Attribute = (dwarf::Attribute)0;
  260. dwarf::Form Form = (dwarf::Form)0;
  261. /// Storage for the value.
  262. ///
  263. /// All values that aren't standard layout (or are larger than 8 bytes)
  264. /// should be stored by reference instead of by value.
  265. typedef AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
  266. DIEDelta *, DIEEntry, DIETypeSignature,
  267. DIEBlock *, DIELoc *, DIELocList> ValTy;
  268. static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
  269. sizeof(ValTy) <= sizeof(void *),
  270. "Expected all large types to be stored via pointer");
  271. /// Underlying stored value.
  272. ValTy Val;
  273. template <class T> void construct(T V) {
  274. static_assert(std::is_standard_layout<T>::value ||
  275. std::is_pointer<T>::value,
  276. "Expected standard layout or pointer");
  277. new (reinterpret_cast<void *>(Val.buffer)) T(V);
  278. }
  279. template <class T> T *get() { return reinterpret_cast<T *>(Val.buffer); }
  280. template <class T> const T *get() const {
  281. return reinterpret_cast<const T *>(Val.buffer);
  282. }
  283. template <class T> void destruct() { get<T>()->~T(); }
  284. /// Destroy the underlying value.
  285. ///
  286. /// This should get optimized down to a no-op. We could skip it if we could
  287. /// add a static assert on \a std::is_trivially_copyable(), but we currently
  288. /// support versions of GCC that don't understand that.
  289. void destroyVal() {
  290. switch (Ty) {
  291. case isNone:
  292. return;
  293. #define HANDLE_DIEVALUE_SMALL(T) \
  294. case is##T: \
  295. destruct<DIE##T>();
  296. return;
  297. #define HANDLE_DIEVALUE_LARGE(T) \
  298. case is##T: \
  299. destruct<const DIE##T *>();
  300. return;
  301. #include "llvm/CodeGen/DIEValue.def"
  302. }
  303. }
  304. /// Copy the underlying value.
  305. ///
  306. /// This should get optimized down to a simple copy. We need to actually
  307. /// construct the value, rather than calling memcpy, to satisfy strict
  308. /// aliasing rules.
  309. void copyVal(const DIEValue &X) {
  310. switch (Ty) {
  311. case isNone:
  312. return;
  313. #define HANDLE_DIEVALUE_SMALL(T) \
  314. case is##T: \
  315. construct<DIE##T>(*X.get<DIE##T>()); \
  316. return;
  317. #define HANDLE_DIEVALUE_LARGE(T) \
  318. case is##T: \
  319. construct<const DIE##T *>(*X.get<const DIE##T *>()); \
  320. return;
  321. #include "llvm/CodeGen/DIEValue.def"
  322. }
  323. }
  324. public:
  325. DIEValue() = default;
  326. DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
  327. copyVal(X);
  328. }
  329. DIEValue &operator=(const DIEValue &X) {
  330. destroyVal();
  331. Ty = X.Ty;
  332. Attribute = X.Attribute;
  333. Form = X.Form;
  334. copyVal(X);
  335. return *this;
  336. }
  337. ~DIEValue() { destroyVal(); }
  338. #define HANDLE_DIEVALUE_SMALL(T) \
  339. DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T &V) \
  340. : Ty(is##T), Attribute(Attribute), Form(Form) { \
  341. construct<DIE##T>(V); \
  342. }
  343. #define HANDLE_DIEVALUE_LARGE(T) \
  344. DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T *V) \
  345. : Ty(is##T), Attribute(Attribute), Form(Form) { \
  346. assert(V && "Expected valid value"); \
  347. construct<const DIE##T *>(V); \
  348. }
  349. #include "llvm/CodeGen/DIEValue.def"
  350. // Accessors
  351. Type getType() const { return Ty; }
  352. dwarf::Attribute getAttribute() const { return Attribute; }
  353. dwarf::Form getForm() const { return Form; }
  354. explicit operator bool() const { return Ty; }
  355. #define HANDLE_DIEVALUE_SMALL(T) \
  356. const DIE##T &getDIE##T() const { \
  357. assert(getType() == is##T && "Expected " #T); \
  358. return *get<DIE##T>(); \
  359. }
  360. #define HANDLE_DIEVALUE_LARGE(T) \
  361. const DIE##T &getDIE##T() const { \
  362. assert(getType() == is##T && "Expected " #T); \
  363. return **get<const DIE##T *>(); \
  364. }
  365. #include "llvm/CodeGen/DIEValue.def"
  366. /// EmitValue - Emit value via the Dwarf writer.
  367. ///
  368. void EmitValue(const AsmPrinter *AP) const;
  369. /// SizeOf - Return the size of a value in bytes.
  370. ///
  371. unsigned SizeOf(const AsmPrinter *AP) const;
  372. #ifndef NDEBUG
  373. void print(raw_ostream &O) const;
  374. void dump() const;
  375. #endif
  376. };
  377. struct IntrusiveBackListNode {
  378. PointerIntPair<IntrusiveBackListNode *, 1> Next;
  379. IntrusiveBackListNode() : Next(this, true) {}
  380. IntrusiveBackListNode *getNext() const {
  381. return Next.getInt() ? nullptr : Next.getPointer();
  382. }
  383. };
  384. struct IntrusiveBackListBase {
  385. typedef IntrusiveBackListNode Node;
  386. Node *Last = nullptr;
  387. bool empty() const { return !Last; }
  388. void push_back(Node &N) {
  389. assert(N.Next.getPointer() == &N && "Expected unlinked node");
  390. assert(N.Next.getInt() && "Expected unlinked node"); // HLSL Change - was int == bool
  391. if (Last) {
  392. N.Next = Last->Next;
  393. Last->Next.setPointerAndInt(&N, false);
  394. }
  395. Last = &N;
  396. }
  397. };
  398. template <class T> class IntrusiveBackList : IntrusiveBackListBase {
  399. public:
  400. using IntrusiveBackListBase::empty;
  401. void push_back(T &N) { IntrusiveBackListBase::push_back(N); }
  402. T &back() { return *static_cast<T *>(Last); }
  403. const T &back() const { return *static_cast<T *>(Last); }
  404. class const_iterator;
  405. class iterator
  406. : public iterator_facade_base<iterator, std::forward_iterator_tag, T> {
  407. friend class const_iterator;
  408. Node *N = nullptr;
  409. public:
  410. iterator() = default;
  411. explicit iterator(T *N) : N(N) {}
  412. iterator &operator++() {
  413. N = N->getNext();
  414. return *this;
  415. }
  416. explicit operator bool() const { return N; }
  417. T &operator*() const { return *static_cast<T *>(N); }
  418. bool operator==(const iterator &X) const { return N == X.N; }
  419. bool operator!=(const iterator &X) const { return N != X.N; }
  420. };
  421. class const_iterator
  422. : public iterator_facade_base<const_iterator, std::forward_iterator_tag,
  423. const T> {
  424. const Node *N = nullptr;
  425. public:
  426. const_iterator() = default;
  427. // Placate MSVC by explicitly scoping 'iterator'.
  428. const_iterator(typename IntrusiveBackList<T>::iterator X) : N(X.N) {}
  429. explicit const_iterator(const T *N) : N(N) {}
  430. const_iterator &operator++() {
  431. N = N->getNext();
  432. return *this;
  433. }
  434. explicit operator bool() const { return N; }
  435. const T &operator*() const { return *static_cast<const T *>(N); }
  436. bool operator==(const const_iterator &X) const { return N == X.N; }
  437. bool operator!=(const const_iterator &X) const { return N != X.N; }
  438. };
  439. iterator begin() {
  440. return Last ? iterator(static_cast<T *>(Last->Next.getPointer())) : end();
  441. }
  442. const_iterator begin() const {
  443. return const_cast<IntrusiveBackList *>(this)->begin();
  444. }
  445. iterator end() { return iterator(); }
  446. const_iterator end() const { return const_iterator(); }
  447. static iterator toIterator(T &N) { return iterator(&N); }
  448. static const_iterator toIterator(const T &N) { return const_iterator(&N); }
  449. };
  450. /// A list of DIE values.
  451. ///
  452. /// This is a singly-linked list, but instead of reversing the order of
  453. /// insertion, we keep a pointer to the back of the list so we can push in
  454. /// order.
  455. ///
  456. /// There are two main reasons to choose a linked list over a customized
  457. /// vector-like data structure.
  458. ///
  459. /// 1. For teardown efficiency, we want DIEs to be BumpPtrAllocated. Using a
  460. /// linked list here makes this way easier to accomplish.
  461. /// 2. Carrying an extra pointer per \a DIEValue isn't expensive. 45% of DIEs
  462. /// have 2 or fewer values, and 90% have 5 or fewer. A vector would be
  463. /// over-allocated by 50% on average anyway, the same cost as the
  464. /// linked-list node.
  465. class DIEValueList {
  466. struct Node : IntrusiveBackListNode {
  467. DIEValue V;
  468. explicit Node(DIEValue V) : V(V) {}
  469. };
  470. typedef IntrusiveBackList<Node> ListTy;
  471. ListTy List;
  472. public:
  473. bool empty() const { return List.empty(); }
  474. class const_iterator;
  475. class iterator
  476. : public iterator_adaptor_base<iterator, ListTy::iterator,
  477. std::forward_iterator_tag, DIEValue> {
  478. friend class const_iterator;
  479. typedef iterator_adaptor_base<iterator, ListTy::iterator,
  480. std::forward_iterator_tag,
  481. DIEValue> iterator_adaptor;
  482. public:
  483. iterator() = default;
  484. explicit iterator(ListTy::iterator X) : iterator_adaptor(X) {}
  485. explicit operator bool() const { return bool(wrapped()); }
  486. DIEValue &operator*() const { return wrapped()->V; }
  487. };
  488. class const_iterator
  489. : public iterator_adaptor_base<const_iterator, ListTy::const_iterator,
  490. std::forward_iterator_tag,
  491. const DIEValue> {
  492. typedef iterator_adaptor_base<const_iterator, ListTy::const_iterator,
  493. std::forward_iterator_tag,
  494. const DIEValue> iterator_adaptor;
  495. public:
  496. const_iterator() = default;
  497. const_iterator(DIEValueList::iterator X) : iterator_adaptor(X.wrapped()) {}
  498. explicit const_iterator(ListTy::const_iterator X) : iterator_adaptor(X) {}
  499. explicit operator bool() const { return bool(wrapped()); }
  500. const DIEValue &operator*() const { return wrapped()->V; }
  501. };
  502. iterator insert(BumpPtrAllocator &Alloc, DIEValue V) {
  503. List.push_back(*new (Alloc) Node(V));
  504. return iterator(ListTy::toIterator(List.back()));
  505. }
  506. template <class... Ts>
  507. iterator emplace(BumpPtrAllocator &Alloc, Ts &&... Args) {
  508. return insert(Alloc, DIEValue(std::forward<Ts>(Args)...));
  509. }
  510. iterator begin() { return iterator(List.begin()); }
  511. iterator end() { return iterator(List.end()); }
  512. const_iterator begin() const { return const_iterator(List.begin()); }
  513. const_iterator end() const { return const_iterator(List.end()); }
  514. };
  515. //===--------------------------------------------------------------------===//
  516. /// DIE - A structured debug information entry. Has an abbreviation which
  517. /// describes its organization.
  518. class DIE : IntrusiveBackListNode {
  519. friend class IntrusiveBackList<DIE>;
  520. protected:
  521. /// Offset - Offset in debug info section.
  522. ///
  523. unsigned Offset;
  524. /// Size - Size of instance + children.
  525. ///
  526. unsigned Size;
  527. unsigned AbbrevNumber = ~0u;
  528. /// Tag - Dwarf tag code.
  529. ///
  530. dwarf::Tag Tag = (dwarf::Tag)0;
  531. /// Children DIEs.
  532. IntrusiveBackList<DIE> Children;
  533. DIE *Parent = nullptr;
  534. /// Attribute values.
  535. ///
  536. DIEValueList Values;
  537. protected:
  538. DIE() : Offset(0), Size(0) {}
  539. private:
  540. explicit DIE(dwarf::Tag Tag) : Offset(0), Size(0), Tag(Tag) {}
  541. public:
  542. static DIE *get(BumpPtrAllocator &Alloc, dwarf::Tag Tag) {
  543. return new (Alloc) DIE(Tag);
  544. }
  545. // Accessors.
  546. unsigned getAbbrevNumber() const { return AbbrevNumber; }
  547. dwarf::Tag getTag() const { return Tag; }
  548. unsigned getOffset() const { return Offset; }
  549. unsigned getSize() const { return Size; }
  550. bool hasChildren() const { return !Children.empty(); }
  551. typedef IntrusiveBackList<DIE>::iterator child_iterator;
  552. typedef IntrusiveBackList<DIE>::const_iterator const_child_iterator;
  553. typedef iterator_range<child_iterator> child_range;
  554. typedef iterator_range<const_child_iterator> const_child_range;
  555. child_range children() {
  556. return llvm::make_range(Children.begin(), Children.end());
  557. }
  558. const_child_range children() const {
  559. return llvm::make_range(Children.begin(), Children.end());
  560. }
  561. typedef DIEValueList::iterator value_iterator;
  562. typedef iterator_range<value_iterator> value_range;
  563. value_range values() {
  564. return llvm::make_range(Values.begin(), Values.end());
  565. }
  566. typedef DIEValueList::const_iterator const_value_iterator;
  567. typedef iterator_range<const_value_iterator> const_value_range;
  568. const_value_range values() const {
  569. return llvm::make_range(Values.begin(), Values.end());
  570. }
  571. DIE *getParent() const { return Parent; }
  572. /// Generate the abbreviation for this DIE.
  573. ///
  574. /// Calculate the abbreviation for this, which should be uniqued and
  575. /// eventually used to call \a setAbbrevNumber().
  576. DIEAbbrev generateAbbrev() const;
  577. /// Set the abbreviation number for this DIE.
  578. void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
  579. /// Climb up the parent chain to get the compile or type unit DIE this DIE
  580. /// belongs to.
  581. const DIE *getUnit() const;
  582. /// Similar to getUnit, returns null when DIE is not added to an
  583. /// owner yet.
  584. const DIE *getUnitOrNull() const;
  585. void setOffset(unsigned O) { Offset = O; }
  586. void setSize(unsigned S) { Size = S; }
  587. /// addValue - Add a value and attributes to a DIE.
  588. ///
  589. value_iterator addValue(BumpPtrAllocator &Alloc, DIEValue Value) {
  590. return Values.insert(Alloc, Value);
  591. }
  592. template <class T>
  593. value_iterator addValue(BumpPtrAllocator &Alloc, dwarf::Attribute Attribute,
  594. dwarf::Form Form, T &&Value) {
  595. return Values.emplace(Alloc, Attribute, Form, std::forward<T>(Value));
  596. }
  597. /// Add a child to the DIE.
  598. DIE &addChild(DIE *Child) {
  599. assert(!Child->getParent() && "Child should be orphaned");
  600. Child->Parent = this;
  601. Children.push_back(*Child);
  602. return Children.back();
  603. }
  604. /// Find a value in the DIE with the attribute given.
  605. ///
  606. /// Returns a default-constructed DIEValue (where \a DIEValue::getType()
  607. /// gives \a DIEValue::isNone) if no such attribute exists.
  608. DIEValue findAttribute(dwarf::Attribute Attribute) const;
  609. #ifndef NDEBUG
  610. void print(raw_ostream &O, unsigned IndentCount = 0) const;
  611. void dump();
  612. #endif
  613. };
  614. //===--------------------------------------------------------------------===//
  615. /// DIELoc - Represents an expression location.
  616. //
  617. class DIELoc : public DIE {
  618. mutable unsigned Size; // Size in bytes excluding size header.
  619. public:
  620. DIELoc() : Size(0) {}
  621. /// ComputeSize - Calculate the size of the location expression.
  622. ///
  623. unsigned ComputeSize(const AsmPrinter *AP) const;
  624. /// BestForm - Choose the best form for data.
  625. ///
  626. dwarf::Form BestForm(unsigned DwarfVersion) const {
  627. if (DwarfVersion > 3)
  628. return dwarf::DW_FORM_exprloc;
  629. // Pre-DWARF4 location expressions were blocks and not exprloc.
  630. if ((unsigned char)Size == Size)
  631. return dwarf::DW_FORM_block1;
  632. if ((unsigned short)Size == Size)
  633. return dwarf::DW_FORM_block2;
  634. if ((unsigned int)Size == Size)
  635. return dwarf::DW_FORM_block4;
  636. return dwarf::DW_FORM_block;
  637. }
  638. void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
  639. unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
  640. #ifndef NDEBUG
  641. void print(raw_ostream &O) const;
  642. #endif
  643. };
  644. //===--------------------------------------------------------------------===//
  645. /// DIEBlock - Represents a block of values.
  646. //
  647. class DIEBlock : public DIE {
  648. mutable unsigned Size; // Size in bytes excluding size header.
  649. public:
  650. DIEBlock() : Size(0) {}
  651. /// ComputeSize - Calculate the size of the location expression.
  652. ///
  653. unsigned ComputeSize(const AsmPrinter *AP) const;
  654. /// BestForm - Choose the best form for data.
  655. ///
  656. dwarf::Form BestForm() const {
  657. if ((unsigned char)Size == Size)
  658. return dwarf::DW_FORM_block1;
  659. if ((unsigned short)Size == Size)
  660. return dwarf::DW_FORM_block2;
  661. if ((unsigned int)Size == Size)
  662. return dwarf::DW_FORM_block4;
  663. return dwarf::DW_FORM_block;
  664. }
  665. void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
  666. unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
  667. #ifndef NDEBUG
  668. void print(raw_ostream &O) const;
  669. #endif
  670. };
  671. } // end llvm namespace
  672. #endif