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