LLVM  mainline
DIE.h
Go to the documentation of this file.
00001 //===--- lib/CodeGen/DIE.h - DWARF Info Entries -----------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Data structures for DWARF info entries.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
00015 #define LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
00016 
00017 #include "llvm/ADT/FoldingSet.h"
00018 #include "llvm/ADT/PointerIntPair.h"
00019 #include "llvm/ADT/STLExtras.h"
00020 #include "llvm/ADT/SmallVector.h"
00021 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
00022 #include "llvm/Support/Dwarf.h"
00023 #include <vector>
00024 
00025 namespace llvm {
00026 class AsmPrinter;
00027 class MCExpr;
00028 class MCSymbol;
00029 class raw_ostream;
00030 class DwarfTypeUnit;
00031 
00032 // AsmStreamerBase - A base abstract interface class defines methods that
00033 // can be implemented to stream objects or can be implemented to
00034 // calculate the size of the streamed objects.
00035 // The derived classes will use an AsmPrinter to implement the methods.
00036 //
00037 // TODO: complete this interface and use it to merge EmitValue and SizeOf
00038 //       methods in the DIE classes below.
00039 class AsmStreamerBase {
00040 protected:
00041   const AsmPrinter *AP;
00042   AsmStreamerBase(const AsmPrinter *AP) : AP(AP) {}
00043 
00044 public:
00045   virtual ~AsmStreamerBase() {}
00046   virtual unsigned emitULEB128(uint64_t Value, const char *Desc = nullptr,
00047                                unsigned PadTo = 0) = 0;
00048   virtual unsigned emitInt8(unsigned char Value) = 0;
00049   virtual unsigned emitBytes(StringRef Data) = 0;
00050 };
00051 
00052 /// EmittingAsmStreamer - Implements AbstractAsmStreamer to stream objects.
00053 /// Notice that the return value is not the actual size of the streamed object.
00054 /// For size calculation use SizeReporterAsmStreamer.
00055 class EmittingAsmStreamer : public AsmStreamerBase {
00056 public:
00057   EmittingAsmStreamer(const AsmPrinter *AP) : AsmStreamerBase(AP) {}
00058   unsigned emitULEB128(uint64_t Value, const char *Desc = nullptr,
00059                        unsigned PadTo = 0) override;
00060   unsigned emitInt8(unsigned char Value) override;
00061   unsigned emitBytes(StringRef Data) override;
00062 };
00063 
00064 /// SizeReporterAsmStreamer - Only reports the size of the streamed objects.
00065 class SizeReporterAsmStreamer : public AsmStreamerBase {
00066 public:
00067   SizeReporterAsmStreamer(const AsmPrinter *AP) : AsmStreamerBase(AP) {}
00068   unsigned emitULEB128(uint64_t Value, const char *Desc = nullptr,
00069                        unsigned PadTo = 0) override;
00070   unsigned emitInt8(unsigned char Value) override;
00071   unsigned emitBytes(StringRef Data) override;
00072 };
00073 
00074 //===--------------------------------------------------------------------===//
00075 /// DIEAbbrevData - Dwarf abbreviation data, describes one attribute of a
00076 /// Dwarf abbreviation.
00077 class DIEAbbrevData {
00078   /// Attribute - Dwarf attribute code.
00079   ///
00080   dwarf::Attribute Attribute;
00081 
00082   /// Form - Dwarf form code.
00083   ///
00084   dwarf::Form Form;
00085 
00086 public:
00087   DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
00088 
00089   // Accessors.
00090   dwarf::Attribute getAttribute() const { return Attribute; }
00091   dwarf::Form getForm() const { return Form; }
00092 
00093   /// Profile - Used to gather unique data for the abbreviation folding set.
00094   ///
00095   void Profile(FoldingSetNodeID &ID) const;
00096 };
00097 
00098 //===--------------------------------------------------------------------===//
00099 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
00100 /// information object.
00101 class DIEAbbrev : public FoldingSetNode {
00102   /// Unique number for node.
00103   ///
00104   unsigned Number;
00105 
00106   /// Tag - Dwarf tag code.
00107   ///
00108   dwarf::Tag Tag;
00109 
00110   /// Children - Whether or not this node has children.
00111   ///
00112   // This cheats a bit in all of the uses since the values in the standard
00113   // are 0 and 1 for no children and children respectively.
00114   bool Children;
00115 
00116   /// Data - Raw data bytes for abbreviation.
00117   ///
00118   SmallVector<DIEAbbrevData, 12> Data;
00119 
00120 public:
00121   DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C), Data() {}
00122 
00123   // Accessors.
00124   dwarf::Tag getTag() const { return Tag; }
00125   unsigned getNumber() const { return Number; }
00126   bool hasChildren() const { return Children; }
00127   const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
00128   void setChildrenFlag(bool hasChild) { Children = hasChild; }
00129   void setNumber(unsigned N) { Number = N; }
00130 
00131   /// AddAttribute - Adds another set of attribute information to the
00132   /// abbreviation.
00133   void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
00134     Data.push_back(DIEAbbrevData(Attribute, Form));
00135   }
00136 
00137   /// Profile - Used to gather unique data for the abbreviation folding set.
00138   ///
00139   void Profile(FoldingSetNodeID &ID) const;
00140 
00141   /// Emit - Print the abbreviation using the specified asm printer.
00142   ///
00143   void Emit(const AsmPrinter *AP) const;
00144 
00145   void print(raw_ostream &O);
00146   void dump();
00147 };
00148 
00149 //===--------------------------------------------------------------------===//
00150 /// DIEInteger - An integer value DIE.
00151 ///
00152 class DIEInteger {
00153   uint64_t Integer;
00154 
00155 public:
00156   explicit DIEInteger(uint64_t I) : Integer(I) {}
00157 
00158   /// BestForm - Choose the best form for integer.
00159   ///
00160   static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
00161     if (IsSigned) {
00162       const int64_t SignedInt = Int;
00163       if ((char)Int == SignedInt)
00164         return dwarf::DW_FORM_data1;
00165       if ((short)Int == SignedInt)
00166         return dwarf::DW_FORM_data2;
00167       if ((int)Int == SignedInt)
00168         return dwarf::DW_FORM_data4;
00169     } else {
00170       if ((unsigned char)Int == Int)
00171         return dwarf::DW_FORM_data1;
00172       if ((unsigned short)Int == Int)
00173         return dwarf::DW_FORM_data2;
00174       if ((unsigned int)Int == Int)
00175         return dwarf::DW_FORM_data4;
00176     }
00177     return dwarf::DW_FORM_data8;
00178   }
00179 
00180   uint64_t getValue() const { return Integer; }
00181   void setValue(uint64_t Val) { Integer = Val; }
00182 
00183   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00184   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00185 
00186   void print(raw_ostream &O) const;
00187 };
00188 
00189 //===--------------------------------------------------------------------===//
00190 /// DIEExpr - An expression DIE.
00191 //
00192 class DIEExpr {
00193   const MCExpr *Expr;
00194 
00195 public:
00196   explicit DIEExpr(const MCExpr *E) : Expr(E) {}
00197 
00198   /// getValue - Get MCExpr.
00199   ///
00200   const MCExpr *getValue() const { return Expr; }
00201 
00202   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00203   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00204 
00205   void print(raw_ostream &O) const;
00206 };
00207 
00208 //===--------------------------------------------------------------------===//
00209 /// DIELabel - A label DIE.
00210 //
00211 class DIELabel {
00212   const MCSymbol *Label;
00213 
00214 public:
00215   explicit DIELabel(const MCSymbol *L) : Label(L) {}
00216 
00217   /// getValue - Get MCSymbol.
00218   ///
00219   const MCSymbol *getValue() const { return Label; }
00220 
00221   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00222   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00223 
00224   void print(raw_ostream &O) const;
00225 };
00226 
00227 //===--------------------------------------------------------------------===//
00228 /// DIEDelta - A simple label difference DIE.
00229 ///
00230 class DIEDelta {
00231   const MCSymbol *LabelHi;
00232   const MCSymbol *LabelLo;
00233 
00234 public:
00235   DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo) : LabelHi(Hi), LabelLo(Lo) {}
00236 
00237   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00238   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00239 
00240   void print(raw_ostream &O) const;
00241 };
00242 
00243 //===--------------------------------------------------------------------===//
00244 /// DIEString - A container for string values.
00245 ///
00246 class DIEString {
00247   DwarfStringPoolEntryRef S;
00248 
00249 public:
00250   DIEString(DwarfStringPoolEntryRef S) : S(S) {}
00251 
00252   /// getString - Grab the string out of the object.
00253   StringRef getString() const { return S.getString(); }
00254 
00255   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00256   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00257 
00258   void print(raw_ostream &O) const;
00259 };
00260 
00261 //===--------------------------------------------------------------------===//
00262 /// DIEEntry - A pointer to another debug information entry.  An instance of
00263 /// this class can also be used as a proxy for a debug information entry not
00264 /// yet defined (ie. types.)
00265 class DIE;
00266 class DIEEntry {
00267   DIE *Entry;
00268 
00269   DIEEntry() = delete;
00270 
00271 public:
00272   explicit DIEEntry(DIE &E) : Entry(&E) {}
00273 
00274   DIE &getEntry() const { return *Entry; }
00275 
00276   /// Returns size of a ref_addr entry.
00277   static unsigned getRefAddrSize(const AsmPrinter *AP);
00278 
00279   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00280   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
00281     return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
00282                                            : sizeof(int32_t);
00283   }
00284 
00285   void print(raw_ostream &O) const;
00286 };
00287 
00288 //===--------------------------------------------------------------------===//
00289 /// \brief A signature reference to a type unit.
00290 class DIETypeSignature {
00291   const DwarfTypeUnit *Unit;
00292 
00293   DIETypeSignature() = delete;
00294 
00295 public:
00296   explicit DIETypeSignature(const DwarfTypeUnit &Unit) : Unit(&Unit) {}
00297 
00298   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00299   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
00300     assert(Form == dwarf::DW_FORM_ref_sig8);
00301     return 8;
00302   }
00303 
00304   void print(raw_ostream &O) const;
00305 };
00306 
00307 //===--------------------------------------------------------------------===//
00308 /// DIELocList - Represents a pointer to a location list in the debug_loc
00309 /// section.
00310 //
00311 class DIELocList {
00312   // Index into the .debug_loc vector.
00313   size_t Index;
00314 
00315 public:
00316   DIELocList(size_t I) : Index(I) {}
00317 
00318   /// getValue - Grab the current index out.
00319   size_t getValue() const { return Index; }
00320 
00321   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00322   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00323 
00324   void print(raw_ostream &O) const;
00325 };
00326 
00327 //===--------------------------------------------------------------------===//
00328 /// DIEValue - A debug information entry value. Some of these roughly correlate
00329 /// to DWARF attribute classes.
00330 ///
00331 class DIEBlock;
00332 class DIELoc;
00333 class DIEValue {
00334 public:
00335   enum Type {
00336     isNone,
00337 #define HANDLE_DIEVALUE(T) is##T,
00338 #include "llvm/CodeGen/DIEValue.def"
00339   };
00340 
00341 private:
00342   /// Ty - Type of data stored in the value.
00343   ///
00344   Type Ty = isNone;
00345   dwarf::Attribute Attribute = (dwarf::Attribute)0;
00346   dwarf::Form Form = (dwarf::Form)0;
00347 
00348   /// Storage for the value.
00349   ///
00350   /// All values that aren't standard layout (or are larger than 8 bytes)
00351   /// should be stored by reference instead of by value.
00352   typedef AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
00353                                 DIEDelta *, DIEEntry, DIETypeSignature,
00354                                 DIEBlock *, DIELoc *, DIELocList> ValTy;
00355   static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
00356                     sizeof(ValTy) <= sizeof(void *),
00357                 "Expected all large types to be stored via pointer");
00358 
00359   /// Underlying stored value.
00360   ValTy Val;
00361 
00362   template <class T> void construct(T V) {
00363     static_assert(std::is_standard_layout<T>::value ||
00364                       std::is_pointer<T>::value,
00365                   "Expected standard layout or pointer");
00366     new (reinterpret_cast<void *>(Val.buffer)) T(V);
00367   }
00368 
00369   template <class T> T *get() { return reinterpret_cast<T *>(Val.buffer); }
00370   template <class T> const T *get() const {
00371     return reinterpret_cast<const T *>(Val.buffer);
00372   }
00373   template <class T> void destruct() { get<T>()->~T(); }
00374 
00375   /// Destroy the underlying value.
00376   ///
00377   /// This should get optimized down to a no-op.  We could skip it if we could
00378   /// add a static assert on \a std::is_trivially_copyable(), but we currently
00379   /// support versions of GCC that don't understand that.
00380   void destroyVal() {
00381     switch (Ty) {
00382     case isNone:
00383       return;
00384 #define HANDLE_DIEVALUE_SMALL(T)                                               \
00385   case is##T:                                                                  \
00386     destruct<DIE##T>();
00387     return;
00388 #define HANDLE_DIEVALUE_LARGE(T)                                               \
00389   case is##T:                                                                  \
00390     destruct<const DIE##T *>();
00391     return;
00392 #include "llvm/CodeGen/DIEValue.def"
00393     }
00394   }
00395 
00396   /// Copy the underlying value.
00397   ///
00398   /// This should get optimized down to a simple copy.  We need to actually
00399   /// construct the value, rather than calling memcpy, to satisfy strict
00400   /// aliasing rules.
00401   void copyVal(const DIEValue &X) {
00402     switch (Ty) {
00403     case isNone:
00404       return;
00405 #define HANDLE_DIEVALUE_SMALL(T)                                               \
00406   case is##T:                                                                  \
00407     construct<DIE##T>(*X.get<DIE##T>());                                       \
00408     return;
00409 #define HANDLE_DIEVALUE_LARGE(T)                                               \
00410   case is##T:                                                                  \
00411     construct<const DIE##T *>(*X.get<const DIE##T *>());                       \
00412     return;
00413 #include "llvm/CodeGen/DIEValue.def"
00414     }
00415   }
00416 
00417 public:
00418   DIEValue() = default;
00419   DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
00420     copyVal(X);
00421   }
00422   DIEValue &operator=(const DIEValue &X) {
00423     destroyVal();
00424     Ty = X.Ty;
00425     Attribute = X.Attribute;
00426     Form = X.Form;
00427     copyVal(X);
00428     return *this;
00429   }
00430   ~DIEValue() { destroyVal(); }
00431 
00432 #define HANDLE_DIEVALUE_SMALL(T)                                               \
00433   DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T &V)      \
00434       : Ty(is##T), Attribute(Attribute), Form(Form) {                          \
00435     construct<DIE##T>(V);                                                      \
00436   }
00437 #define HANDLE_DIEVALUE_LARGE(T)                                               \
00438   DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T *V)      \
00439       : Ty(is##T), Attribute(Attribute), Form(Form) {                          \
00440     assert(V && "Expected valid value");                                       \
00441     construct<const DIE##T *>(V);                                              \
00442   }
00443 #include "llvm/CodeGen/DIEValue.def"
00444 
00445   // Accessors
00446   Type getType() const { return Ty; }
00447   dwarf::Attribute getAttribute() const { return Attribute; }
00448   dwarf::Form getForm() const { return Form; }
00449   explicit operator bool() const { return Ty; }
00450 
00451 #define HANDLE_DIEVALUE_SMALL(T)                                               \
00452   const DIE##T &getDIE##T() const {                                            \
00453     assert(getType() == is##T && "Expected " #T);                              \
00454     return *get<DIE##T>();                                                     \
00455   }
00456 #define HANDLE_DIEVALUE_LARGE(T)                                               \
00457   const DIE##T &getDIE##T() const {                                            \
00458     assert(getType() == is##T && "Expected " #T);                              \
00459     return **get<const DIE##T *>();                                            \
00460   }
00461 #include "llvm/CodeGen/DIEValue.def"
00462 
00463   /// EmitValue - Emit value via the Dwarf writer.
00464   ///
00465   void EmitValue(const AsmPrinter *AP) const;
00466 
00467   /// SizeOf - Return the size of a value in bytes.
00468   ///
00469   unsigned SizeOf(const AsmPrinter *AP) const;
00470 
00471   void print(raw_ostream &O) const;
00472   void dump() const;
00473 };
00474 
00475 struct IntrusiveBackListNode {
00476   PointerIntPair<IntrusiveBackListNode *, 1> Next;
00477   IntrusiveBackListNode() : Next(this, true) {}
00478 
00479   IntrusiveBackListNode *getNext() const {
00480     return Next.getInt() ? nullptr : Next.getPointer();
00481   }
00482 };
00483 
00484 struct IntrusiveBackListBase {
00485   typedef IntrusiveBackListNode Node;
00486   Node *Last = nullptr;
00487 
00488   bool empty() const { return !Last; }
00489   void push_back(Node &N) {
00490     assert(N.Next.getPointer() == &N && "Expected unlinked node");
00491     assert(N.Next.getInt() == true && "Expected unlinked node");
00492 
00493     if (Last) {
00494       N.Next = Last->Next;
00495       Last->Next.setPointerAndInt(&N, false);
00496     }
00497     Last = &N;
00498   }
00499 };
00500 
00501 template <class T> class IntrusiveBackList : IntrusiveBackListBase {
00502 public:
00503   using IntrusiveBackListBase::empty;
00504   void push_back(T &N) { IntrusiveBackListBase::push_back(N); }
00505   T &back() { return *static_cast<T *>(Last); }
00506   const T &back() const { return *static_cast<T *>(Last); }
00507 
00508   class const_iterator;
00509   class iterator
00510       : public iterator_facade_base<iterator, std::forward_iterator_tag, T> {
00511     friend class const_iterator;
00512     Node *N = nullptr;
00513 
00514   public:
00515     iterator() = default;
00516     explicit iterator(T *N) : N(N) {}
00517 
00518     iterator &operator++() {
00519       N = N->getNext();
00520       return *this;
00521     }
00522 
00523     explicit operator bool() const { return N; }
00524     T &operator*() const { return *static_cast<T *>(N); }
00525 
00526     bool operator==(const iterator &X) const { return N == X.N; }
00527     bool operator!=(const iterator &X) const { return N != X.N; }
00528   };
00529 
00530   class const_iterator
00531       : public iterator_facade_base<const_iterator, std::forward_iterator_tag,
00532                                     const T> {
00533     const Node *N = nullptr;
00534 
00535   public:
00536     const_iterator() = default;
00537     // Placate MSVC by explicitly scoping 'iterator'.
00538     const_iterator(typename IntrusiveBackList<T>::iterator X) : N(X.N) {}
00539     explicit const_iterator(const T *N) : N(N) {}
00540 
00541     const_iterator &operator++() {
00542       N = N->getNext();
00543       return *this;
00544     }
00545 
00546     explicit operator bool() const { return N; }
00547     const T &operator*() const { return *static_cast<const T *>(N); }
00548 
00549     bool operator==(const const_iterator &X) const { return N == X.N; }
00550     bool operator!=(const const_iterator &X) const { return N != X.N; }
00551   };
00552 
00553   iterator begin() {
00554     return Last ? iterator(static_cast<T *>(Last->Next.getPointer())) : end();
00555   }
00556   const_iterator begin() const {
00557     return const_cast<IntrusiveBackList *>(this)->begin();
00558   }
00559   iterator end() { return iterator(); }
00560   const_iterator end() const { return const_iterator(); }
00561 
00562   static iterator toIterator(T &N) { return iterator(&N); }
00563   static const_iterator toIterator(const T &N) { return const_iterator(&N); }
00564 };
00565 
00566 /// A list of DIE values.
00567 ///
00568 /// This is a singly-linked list, but instead of reversing the order of
00569 /// insertion, we keep a pointer to the back of the list so we can push in
00570 /// order.
00571 ///
00572 /// There are two main reasons to choose a linked list over a customized
00573 /// vector-like data structure.
00574 ///
00575 ///  1. For teardown efficiency, we want DIEs to be BumpPtrAllocated.  Using a
00576 ///     linked list here makes this way easier to accomplish.
00577 ///  2. Carrying an extra pointer per \a DIEValue isn't expensive.  45% of DIEs
00578 ///     have 2 or fewer values, and 90% have 5 or fewer.  A vector would be
00579 ///     over-allocated by 50% on average anyway, the same cost as the
00580 ///     linked-list node.
00581 class DIEValueList {
00582   struct Node : IntrusiveBackListNode {
00583     DIEValue V;
00584     explicit Node(DIEValue V) : V(V) {}
00585   };
00586 
00587   typedef IntrusiveBackList<Node> ListTy;
00588   ListTy List;
00589 
00590 public:
00591   class const_value_iterator;
00592   class value_iterator
00593       : public iterator_adaptor_base<value_iterator, ListTy::iterator,
00594                                      std::forward_iterator_tag, DIEValue> {
00595     friend class const_value_iterator;
00596     typedef iterator_adaptor_base<value_iterator, ListTy::iterator,
00597                                   std::forward_iterator_tag,
00598                                   DIEValue> iterator_adaptor;
00599 
00600   public:
00601     value_iterator() = default;
00602     explicit value_iterator(ListTy::iterator X) : iterator_adaptor(X) {}
00603 
00604     explicit operator bool() const { return bool(wrapped()); }
00605     DIEValue &operator*() const { return wrapped()->V; }
00606   };
00607 
00608   class const_value_iterator : public iterator_adaptor_base<
00609                                    const_value_iterator, ListTy::const_iterator,
00610                                    std::forward_iterator_tag, const DIEValue> {
00611     typedef iterator_adaptor_base<const_value_iterator, ListTy::const_iterator,
00612                                   std::forward_iterator_tag,
00613                                   const DIEValue> iterator_adaptor;
00614 
00615   public:
00616     const_value_iterator() = default;
00617     const_value_iterator(DIEValueList::value_iterator X)
00618         : iterator_adaptor(X.wrapped()) {}
00619     explicit const_value_iterator(ListTy::const_iterator X)
00620         : iterator_adaptor(X) {}
00621 
00622     explicit operator bool() const { return bool(wrapped()); }
00623     const DIEValue &operator*() const { return wrapped()->V; }
00624   };
00625 
00626   typedef iterator_range<value_iterator> value_range;
00627   typedef iterator_range<const_value_iterator> const_value_range;
00628 
00629   value_iterator addValue(BumpPtrAllocator &Alloc, DIEValue V) {
00630     List.push_back(*new (Alloc) Node(V));
00631     return value_iterator(ListTy::toIterator(List.back()));
00632   }
00633   template <class T>
00634   value_iterator addValue(BumpPtrAllocator &Alloc, dwarf::Attribute Attribute,
00635                     dwarf::Form Form, T &&Value) {
00636     return addValue(Alloc, DIEValue(Attribute, Form, std::forward<T>(Value)));
00637   }
00638 
00639   value_range values() {
00640     return llvm::make_range(value_iterator(List.begin()),
00641                             value_iterator(List.end()));
00642   }
00643   const_value_range values() const {
00644     return llvm::make_range(const_value_iterator(List.begin()),
00645                             const_value_iterator(List.end()));
00646   }
00647 };
00648 
00649 //===--------------------------------------------------------------------===//
00650 /// DIE - A structured debug information entry.  Has an abbreviation which
00651 /// describes its organization.
00652 class DIE : IntrusiveBackListNode, public DIEValueList {
00653   friend class IntrusiveBackList<DIE>;
00654 
00655   /// Offset - Offset in debug info section.
00656   ///
00657   unsigned Offset;
00658 
00659   /// Size - Size of instance + children.
00660   ///
00661   unsigned Size;
00662 
00663   unsigned AbbrevNumber = ~0u;
00664 
00665   /// Tag - Dwarf tag code.
00666   ///
00667   dwarf::Tag Tag = (dwarf::Tag)0;
00668 
00669   /// Children DIEs.
00670   IntrusiveBackList<DIE> Children;
00671 
00672   DIE *Parent = nullptr;
00673 
00674   DIE() = delete;
00675   explicit DIE(dwarf::Tag Tag) : Offset(0), Size(0), Tag(Tag) {}
00676 
00677 public:
00678   static DIE *get(BumpPtrAllocator &Alloc, dwarf::Tag Tag) {
00679     return new (Alloc) DIE(Tag);
00680   }
00681 
00682   // Accessors.
00683   unsigned getAbbrevNumber() const { return AbbrevNumber; }
00684   dwarf::Tag getTag() const { return Tag; }
00685   unsigned getOffset() const { return Offset; }
00686   unsigned getSize() const { return Size; }
00687   bool hasChildren() const { return !Children.empty(); }
00688 
00689   typedef IntrusiveBackList<DIE>::iterator child_iterator;
00690   typedef IntrusiveBackList<DIE>::const_iterator const_child_iterator;
00691   typedef iterator_range<child_iterator> child_range;
00692   typedef iterator_range<const_child_iterator> const_child_range;
00693 
00694   child_range children() {
00695     return llvm::make_range(Children.begin(), Children.end());
00696   }
00697   const_child_range children() const {
00698     return llvm::make_range(Children.begin(), Children.end());
00699   }
00700 
00701   DIE *getParent() const { return Parent; }
00702 
00703   /// Generate the abbreviation for this DIE.
00704   ///
00705   /// Calculate the abbreviation for this, which should be uniqued and
00706   /// eventually used to call \a setAbbrevNumber().
00707   DIEAbbrev generateAbbrev() const;
00708 
00709   /// Set the abbreviation number for this DIE.
00710   void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
00711 
00712   /// Climb up the parent chain to get the compile or type unit DIE this DIE
00713   /// belongs to.
00714   const DIE *getUnit() const;
00715   /// Similar to getUnit, returns null when DIE is not added to an
00716   /// owner yet.
00717   const DIE *getUnitOrNull() const;
00718   void setOffset(unsigned O) { Offset = O; }
00719   void setSize(unsigned S) { Size = S; }
00720 
00721   /// Add a child to the DIE.
00722   DIE &addChild(DIE *Child) {
00723     assert(!Child->getParent() && "Child should be orphaned");
00724     Child->Parent = this;
00725     Children.push_back(*Child);
00726     return Children.back();
00727   }
00728 
00729   /// Find a value in the DIE with the attribute given.
00730   ///
00731   /// Returns a default-constructed DIEValue (where \a DIEValue::getType()
00732   /// gives \a DIEValue::isNone) if no such attribute exists.
00733   DIEValue findAttribute(dwarf::Attribute Attribute) const;
00734 
00735   void print(raw_ostream &O, unsigned IndentCount = 0) const;
00736   void dump();
00737 };
00738 
00739 //===--------------------------------------------------------------------===//
00740 /// DIELoc - Represents an expression location.
00741 //
00742 class DIELoc : public DIEValueList {
00743   mutable unsigned Size; // Size in bytes excluding size header.
00744 
00745 public:
00746   DIELoc() : Size(0) {}
00747 
00748   /// ComputeSize - Calculate the size of the location expression.
00749   ///
00750   unsigned ComputeSize(const AsmPrinter *AP) const;
00751 
00752   /// BestForm - Choose the best form for data.
00753   ///
00754   dwarf::Form BestForm(unsigned DwarfVersion) const {
00755     if (DwarfVersion > 3)
00756       return dwarf::DW_FORM_exprloc;
00757     // Pre-DWARF4 location expressions were blocks and not exprloc.
00758     if ((unsigned char)Size == Size)
00759       return dwarf::DW_FORM_block1;
00760     if ((unsigned short)Size == Size)
00761       return dwarf::DW_FORM_block2;
00762     if ((unsigned int)Size == Size)
00763       return dwarf::DW_FORM_block4;
00764     return dwarf::DW_FORM_block;
00765   }
00766 
00767   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00768   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00769 
00770   void print(raw_ostream &O) const;
00771 };
00772 
00773 //===--------------------------------------------------------------------===//
00774 /// DIEBlock - Represents a block of values.
00775 //
00776 class DIEBlock : public DIEValueList {
00777   mutable unsigned Size; // Size in bytes excluding size header.
00778 
00779 public:
00780   DIEBlock() : Size(0) {}
00781 
00782   /// ComputeSize - Calculate the size of the location expression.
00783   ///
00784   unsigned ComputeSize(const AsmPrinter *AP) const;
00785 
00786   /// BestForm - Choose the best form for data.
00787   ///
00788   dwarf::Form BestForm() const {
00789     if ((unsigned char)Size == Size)
00790       return dwarf::DW_FORM_block1;
00791     if ((unsigned short)Size == Size)
00792       return dwarf::DW_FORM_block2;
00793     if ((unsigned int)Size == Size)
00794       return dwarf::DW_FORM_block4;
00795     return dwarf::DW_FORM_block;
00796   }
00797 
00798   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00799   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00800 
00801   void print(raw_ostream &O) const;
00802 };
00803 
00804 } // end llvm namespace
00805 
00806 #endif