AsmPrinter: Store abbreviation data directly in DIE and DIEValue
[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 = isNone;
319   dwarf::Attribute Attribute = (dwarf::Attribute)0;
320   dwarf::Form Form = (dwarf::Form)0;
321
322   /// Storage for the value.
323   ///
324   /// All values that aren't standard layout (or are larger than 8 bytes)
325   /// should be stored by reference instead of by value.
326   typedef AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
327                                 DIEDelta *, DIEEntry, DIETypeSignature,
328                                 DIEBlock *, DIELoc *, DIELocList> ValTy;
329   static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
330                     sizeof(ValTy) <= sizeof(void *),
331                 "Expected all large types to be stored via pointer");
332
333   /// Underlying stored value.
334   ValTy Val;
335
336   template <class T> void construct(T V) {
337     static_assert(std::is_standard_layout<T>::value ||
338                       std::is_pointer<T>::value,
339                   "Expected standard layout or pointer");
340     new (reinterpret_cast<void *>(Val.buffer)) T(V);
341   }
342
343   template <class T> T &get() { return *reinterpret_cast<T *>(Val.buffer); }
344   template <class T> const T &get() const {
345     return *reinterpret_cast<const T *>(Val.buffer);
346   }
347   template <class T> void destruct() { get<T>().~T(); }
348
349   /// Destroy the underlying value.
350   ///
351   /// This should get optimized down to a no-op.  We could skip it if we could
352   /// add a static assert on \a std::is_trivially_copyable(), but we currently
353   /// support versions of GCC that don't understand that.
354   void destroyVal() {
355     switch (Ty) {
356     case isNone:
357       return;
358 #define HANDLE_DIEVALUE_SMALL(T)                                               \
359   case is##T:                                                                  \
360     destruct<DIE##T>();
361     return;
362 #define HANDLE_DIEVALUE_LARGE(T)                                               \
363   case is##T:                                                                  \
364     destruct<const DIE##T *>();
365     return;
366 #include "llvm/CodeGen/DIEValue.def"
367     }
368   }
369
370   /// Copy the underlying value.
371   ///
372   /// This should get optimized down to a simple copy.  We need to actually
373   /// construct the value, rather than calling memcpy, to satisfy strict
374   /// aliasing rules.
375   void copyVal(const DIEValue &X) {
376     switch (Ty) {
377     case isNone:
378       return;
379 #define HANDLE_DIEVALUE_SMALL(T)                                               \
380   case is##T:                                                                  \
381     construct<DIE##T>(X.get<DIE##T>());                                        \
382     return;
383 #define HANDLE_DIEVALUE_LARGE(T)                                               \
384   case is##T:                                                                  \
385     construct<const DIE##T *>(X.get<const DIE##T *>());                        \
386     return;
387 #include "llvm/CodeGen/DIEValue.def"
388     }
389   }
390
391 public:
392   DIEValue() = default;
393   DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
394     copyVal(X);
395   }
396   DIEValue &operator=(const DIEValue &X) {
397     destroyVal();
398     Ty = X.Ty;
399     Attribute = X.Attribute;
400     Form = X.Form;
401     copyVal(X);
402     return *this;
403   }
404   ~DIEValue() { destroyVal(); }
405
406 #define HANDLE_DIEVALUE_SMALL(T)                                               \
407   DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T &V)      \
408       : Ty(is##T), Attribute(Attribute), Form(Form) {                          \
409     construct<DIE##T>(V);                                                      \
410   }
411 #define HANDLE_DIEVALUE_LARGE(T)                                               \
412   DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T *V)      \
413       : Ty(is##T), Attribute(Attribute), Form(Form) {                          \
414     assert(V && "Expected valid value");                                       \
415     construct<const DIE##T *>(V);                                              \
416   }
417 #include "llvm/CodeGen/DIEValue.def"
418
419   // Accessors
420   Type getType() const { return Ty; }
421   dwarf::Attribute getAttribute() const { return Attribute; }
422   dwarf::Form getForm() const { return Form; }
423   explicit operator bool() const { return Ty; }
424
425 #define HANDLE_DIEVALUE_SMALL(T)                                               \
426   const DIE##T &getDIE##T() const {                                            \
427     assert(getType() == is##T && "Expected " #T);                              \
428     return get<DIE##T>();                                                      \
429   }
430 #define HANDLE_DIEVALUE_LARGE(T)                                               \
431   const DIE##T &getDIE##T() const {                                            \
432     assert(getType() == is##T && "Expected " #T);                              \
433     return *get<const DIE##T *>();                                             \
434   }
435 #include "llvm/CodeGen/DIEValue.def"
436
437   /// EmitValue - Emit value via the Dwarf writer.
438   ///
439   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
440
441   /// SizeOf - Return the size of a value in bytes.
442   ///
443   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
444
445 #ifndef NDEBUG
446   void print(raw_ostream &O) const;
447   void dump() const;
448 #endif
449 };
450
451 //===--------------------------------------------------------------------===//
452 /// DIE - A structured debug information entry.  Has an abbreviation which
453 /// describes its organization.
454 class DIE {
455 protected:
456   /// Offset - Offset in debug info section.
457   ///
458   unsigned Offset;
459
460   /// Size - Size of instance + children.
461   ///
462   unsigned Size;
463
464   unsigned AbbrevNumber = ~0u;
465
466   /// Tag - Dwarf tag code.
467   ///
468   dwarf::Tag Tag = (dwarf::Tag)0;
469
470   /// Children DIEs.
471   ///
472   // This can't be a vector<DIE> because pointer validity is requirent for the
473   // Parent pointer and DIEEntry.
474   // It can't be a list<DIE> because some clients need pointer validity before
475   // the object has been added to any child list
476   // (eg: DwarfUnit::constructVariableDIE). These aren't insurmountable, but may
477   // be more convoluted than beneficial.
478   std::vector<std::unique_ptr<DIE>> Children;
479
480   DIE *Parent;
481
482   /// Attribute values.
483   ///
484   SmallVector<DIEValue, 12> Values;
485
486 protected:
487   DIE() : Offset(0), Size(0), Parent(nullptr) {}
488
489 public:
490   explicit DIE(dwarf::Tag Tag)
491       : Offset(0), Size(0), Tag(Tag), Parent(nullptr) {}
492
493   // Accessors.
494   unsigned getAbbrevNumber() const { return AbbrevNumber; }
495   dwarf::Tag getTag() const { return Tag; }
496   unsigned getOffset() const { return Offset; }
497   unsigned getSize() const { return Size; }
498   bool hasChildren() const { return !Children.empty(); }
499   const std::vector<std::unique_ptr<DIE>> &getChildren() const {
500     return Children;
501   }
502   const SmallVectorImpl<DIEValue> &getValues() const { return Values; }
503   void setValue(unsigned I, DIEValue New) {
504     assert(I < Values.size());
505     Values[I] = New;
506   }
507   DIE *getParent() const { return Parent; }
508
509   /// Generate the abbreviation for this DIE.
510   ///
511   /// Calculate the abbreviation for this, which should be uniqued and
512   /// eventually used to call \a setAbbrevNumber().
513   DIEAbbrev generateAbbrev() const;
514
515   /// Set the abbreviation number for this DIE.
516   void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
517
518   /// Climb up the parent chain to get the compile or type unit DIE this DIE
519   /// belongs to.
520   const DIE *getUnit() const;
521   /// Similar to getUnit, returns null when DIE is not added to an
522   /// owner yet.
523   const DIE *getUnitOrNull() const;
524   void setOffset(unsigned O) { Offset = O; }
525   void setSize(unsigned S) { Size = S; }
526
527   /// addValue - Add a value and attributes to a DIE.
528   ///
529   void addValue(DIEValue Value) { Values.push_back(Value); }
530   template <class T>
531   void addValue(dwarf::Attribute Attribute, dwarf::Form Form, T &&Value) {
532     Values.emplace_back(Attribute, Form, std::forward<T>(Value));
533   }
534
535   /// addChild - Add a child to the DIE.
536   ///
537   void addChild(std::unique_ptr<DIE> Child) {
538     assert(!Child->getParent());
539     Child->Parent = this;
540     Children.push_back(std::move(Child));
541   }
542
543   /// Find a value in the DIE with the attribute given.
544   ///
545   /// Returns a default-constructed DIEValue (where \a DIEValue::getType()
546   /// gives \a DIEValue::isNone) if no such attribute exists.
547   DIEValue findAttribute(dwarf::Attribute Attribute) const;
548
549 #ifndef NDEBUG
550   void print(raw_ostream &O, unsigned IndentCount = 0) const;
551   void dump();
552 #endif
553 };
554
555 //===--------------------------------------------------------------------===//
556 /// DIELoc - Represents an expression location.
557 //
558 class DIELoc : public DIE {
559   mutable unsigned Size; // Size in bytes excluding size header.
560
561 public:
562   DIELoc() : Size(0) {}
563
564   /// ComputeSize - Calculate the size of the location expression.
565   ///
566   unsigned ComputeSize(const AsmPrinter *AP) const;
567
568   /// BestForm - Choose the best form for data.
569   ///
570   dwarf::Form BestForm(unsigned DwarfVersion) const {
571     if (DwarfVersion > 3)
572       return dwarf::DW_FORM_exprloc;
573     // Pre-DWARF4 location expressions were blocks and not exprloc.
574     if ((unsigned char)Size == Size)
575       return dwarf::DW_FORM_block1;
576     if ((unsigned short)Size == Size)
577       return dwarf::DW_FORM_block2;
578     if ((unsigned int)Size == Size)
579       return dwarf::DW_FORM_block4;
580     return dwarf::DW_FORM_block;
581   }
582
583   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
584   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
585
586 #ifndef NDEBUG
587   void print(raw_ostream &O) const;
588 #endif
589 };
590
591 //===--------------------------------------------------------------------===//
592 /// DIEBlock - Represents a block of values.
593 //
594 class DIEBlock : public DIE {
595   mutable unsigned Size; // Size in bytes excluding size header.
596
597 public:
598   DIEBlock() : Size(0) {}
599
600   /// ComputeSize - Calculate the size of the location expression.
601   ///
602   unsigned ComputeSize(const AsmPrinter *AP) const;
603
604   /// BestForm - Choose the best form for data.
605   ///
606   dwarf::Form BestForm() const {
607     if ((unsigned char)Size == Size)
608       return dwarf::DW_FORM_block1;
609     if ((unsigned short)Size == Size)
610       return dwarf::DW_FORM_block2;
611     if ((unsigned int)Size == Size)
612       return dwarf::DW_FORM_block4;
613     return dwarf::DW_FORM_block;
614   }
615
616   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
617   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
618
619 #ifndef NDEBUG
620   void print(raw_ostream &O) const;
621 #endif
622 };
623
624 } // end llvm namespace
625
626 #endif