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