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