3876728307de1882a5f744cf176743b8d38c0527
[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 CODEGEN_ASMPRINTER_DIE_H__
15 #define 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   std::vector<std::unique_ptr<DIE>> Children;
128
129   DIE *Parent;
130
131   /// Attribute values.
132   ///
133   SmallVector<DIEValue *, 12> Values;
134
135 protected:
136   DIE()
137       : Offset(0), Size(0), Abbrev((dwarf::Tag)0, dwarf::DW_CHILDREN_no),
138         Parent(0) {}
139
140 public:
141   explicit DIE(dwarf::Tag Tag)
142       : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
143         Parent(0) {}
144
145   // Accessors.
146   DIEAbbrev &getAbbrev() { return Abbrev; }
147   const DIEAbbrev &getAbbrev() const { return Abbrev; }
148   unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
149   dwarf::Tag getTag() const { return Abbrev.getTag(); }
150   unsigned getOffset() const { return Offset; }
151   unsigned getSize() const { return Size; }
152   const std::vector<std::unique_ptr<DIE>> &getChildren() const {
153     return Children;
154   }
155   const SmallVectorImpl<DIEValue *> &getValues() const { return Values; }
156   DIE *getParent() const { return Parent; }
157   /// Climb up the parent chain to get the compile or type unit DIE this DIE
158   /// belongs to.
159   const DIE *getUnit() const;
160   /// Similar to getUnit, returns null when DIE is not added to an
161   /// owner yet.
162   const DIE *getUnitOrNull() const;
163   void setOffset(unsigned O) { Offset = O; }
164   void setSize(unsigned S) { Size = S; }
165
166   /// addValue - Add a value and attributes to a DIE.
167   ///
168   void addValue(dwarf::Attribute Attribute, dwarf::Form Form, DIEValue *Value) {
169     Abbrev.AddAttribute(Attribute, Form);
170     Values.push_back(Value);
171   }
172
173   /// addChild - Add a child to the DIE.
174   ///
175   void addChild(DIE *Child) {
176     assert(!Child->getParent());
177     Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
178     Children.push_back(std::unique_ptr<DIE>(Child));
179     Child->Parent = this;
180   }
181
182   /// findAttribute - Find a value in the DIE with the attribute given,
183   /// returns NULL if no such attribute exists.
184   DIEValue *findAttribute(dwarf::Attribute Attribute) const;
185
186 #ifndef NDEBUG
187   void print(raw_ostream &O, unsigned IndentCount = 0) const;
188   void dump();
189 #endif
190 };
191
192 //===--------------------------------------------------------------------===//
193 /// DIEValue - A debug information entry value. Some of these roughly correlate
194 /// to DWARF attribute classes.
195 ///
196 class DIEValue {
197   virtual void anchor();
198
199 public:
200   enum Type {
201     isInteger,
202     isString,
203     isExpr,
204     isLabel,
205     isDelta,
206     isEntry,
207     isTypeSignature,
208     isBlock,
209     isLoc,
210     isLocList,
211   };
212
213 protected:
214   /// Ty - Type of data stored in the value.
215   ///
216   Type Ty;
217
218   explicit DIEValue(Type T) : Ty(T) {}
219   virtual ~DIEValue() {}
220
221 public:
222   // Accessors
223   Type getType() const { return Ty; }
224
225   /// EmitValue - Emit value via the Dwarf writer.
226   ///
227   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
228
229   /// SizeOf - Return the size of a value in bytes.
230   ///
231   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
232
233 #ifndef NDEBUG
234   virtual void print(raw_ostream &O) const = 0;
235   void dump() const;
236 #endif
237 };
238
239 //===--------------------------------------------------------------------===//
240 /// DIEInteger - An integer value DIE.
241 ///
242 class DIEInteger : public DIEValue {
243   uint64_t Integer;
244
245 public:
246   explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
247
248   /// BestForm - Choose the best form for integer.
249   ///
250   static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
251     if (IsSigned) {
252       const int64_t SignedInt = Int;
253       if ((char)Int == SignedInt)
254         return dwarf::DW_FORM_data1;
255       if ((short)Int == SignedInt)
256         return dwarf::DW_FORM_data2;
257       if ((int)Int == SignedInt)
258         return dwarf::DW_FORM_data4;
259     } else {
260       if ((unsigned char)Int == Int)
261         return dwarf::DW_FORM_data1;
262       if ((unsigned short)Int == Int)
263         return dwarf::DW_FORM_data2;
264       if ((unsigned int)Int == Int)
265         return dwarf::DW_FORM_data4;
266     }
267     return dwarf::DW_FORM_data8;
268   }
269
270   /// EmitValue - Emit integer of appropriate size.
271   ///
272   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
273
274   uint64_t getValue() const { return Integer; }
275
276   /// SizeOf - Determine size of integer value in bytes.
277   ///
278   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
279
280   // Implement isa/cast/dyncast.
281   static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
282
283 #ifndef NDEBUG
284   void print(raw_ostream &O) const override;
285 #endif
286 };
287
288 //===--------------------------------------------------------------------===//
289 /// DIEExpr - An expression DIE.
290 //
291 class DIEExpr : public DIEValue {
292   const MCExpr *Expr;
293
294 public:
295   explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
296
297   /// EmitValue - Emit expression value.
298   ///
299   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
300
301   /// getValue - Get MCExpr.
302   ///
303   const MCExpr *getValue() const { return Expr; }
304
305   /// SizeOf - Determine size of expression value in bytes.
306   ///
307   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
308
309   // Implement isa/cast/dyncast.
310   static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
311
312 #ifndef NDEBUG
313   void print(raw_ostream &O) const override;
314 #endif
315 };
316
317 //===--------------------------------------------------------------------===//
318 /// DIELabel - A label DIE.
319 //
320 class DIELabel : public DIEValue {
321   const MCSymbol *Label;
322
323 public:
324   explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
325
326   /// EmitValue - Emit label value.
327   ///
328   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
329
330   /// getValue - Get MCSymbol.
331   ///
332   const MCSymbol *getValue() const { return Label; }
333
334   /// SizeOf - Determine size of label value in bytes.
335   ///
336   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
337
338   // Implement isa/cast/dyncast.
339   static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
340
341 #ifndef NDEBUG
342   void print(raw_ostream &O) const override;
343 #endif
344 };
345
346 //===--------------------------------------------------------------------===//
347 /// DIEDelta - A simple label difference DIE.
348 ///
349 class DIEDelta : public DIEValue {
350   const MCSymbol *LabelHi;
351   const MCSymbol *LabelLo;
352
353 public:
354   DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
355       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
356
357   /// EmitValue - Emit delta value.
358   ///
359   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
360
361   /// SizeOf - Determine size of delta value in bytes.
362   ///
363   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
364
365   // Implement isa/cast/dyncast.
366   static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
367
368 #ifndef NDEBUG
369   void print(raw_ostream &O) const override;
370 #endif
371 };
372
373 //===--------------------------------------------------------------------===//
374 /// DIEString - A container for string values.
375 ///
376 class DIEString : public DIEValue {
377   const DIEValue *Access;
378   const StringRef Str;
379
380 public:
381   DIEString(const DIEValue *Acc, const StringRef S)
382       : DIEValue(isString), Access(Acc), Str(S) {}
383
384   /// getString - Grab the string out of the object.
385   StringRef getString() const { return Str; }
386
387   /// EmitValue - Emit delta value.
388   ///
389   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
390
391   /// SizeOf - Determine size of delta value in bytes.
392   ///
393   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
394
395   // Implement isa/cast/dyncast.
396   static bool classof(const DIEValue *D) { return D->getType() == isString; }
397
398 #ifndef NDEBUG
399   void print(raw_ostream &O) const override;
400 #endif
401 };
402
403 //===--------------------------------------------------------------------===//
404 /// DIEEntry - A pointer to another debug information entry.  An instance of
405 /// this class can also be used as a proxy for a debug information entry not
406 /// yet defined (ie. types.)
407 class DIEEntry : public DIEValue {
408   DIE *const Entry;
409
410 public:
411   explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
412     assert(E && "Cannot construct a DIEEntry with a null DIE");
413   }
414
415   DIE *getEntry() const { return Entry; }
416
417   /// EmitValue - Emit debug information entry offset.
418   ///
419   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
420
421   /// SizeOf - Determine size of debug information entry in bytes.
422   ///
423    unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override {
424     return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
425                                            : sizeof(int32_t);
426   }
427
428   /// Returns size of a ref_addr entry.
429   static unsigned getRefAddrSize(AsmPrinter *AP);
430
431   // Implement isa/cast/dyncast.
432   static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
433
434 #ifndef NDEBUG
435   void print(raw_ostream &O) const override;
436 #endif
437 };
438
439 //===--------------------------------------------------------------------===//
440 /// \brief A signature reference to a type unit.
441 class DIETypeSignature : public DIEValue {
442   const DwarfTypeUnit &Unit;
443
444 public:
445   explicit DIETypeSignature(const DwarfTypeUnit &Unit)
446       : DIEValue(isTypeSignature), Unit(Unit) {}
447
448   /// \brief Emit type unit signature.
449   void EmitValue(AsmPrinter *Asm, dwarf::Form Form) const override;
450
451   /// Returns size of a ref_sig8 entry.
452   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override {
453     assert(Form == dwarf::DW_FORM_ref_sig8);
454     return 8;
455   }
456
457   // \brief Implement isa/cast/dyncast.
458   static bool classof(const DIEValue *E) {
459     return E->getType() == isTypeSignature;
460   }
461 #ifndef NDEBUG
462   void print(raw_ostream &O) const override;
463   void dump() const;
464 #endif
465 };
466
467 //===--------------------------------------------------------------------===//
468 /// DIELoc - Represents an expression location.
469 //
470 class DIELoc : public DIEValue, public DIE {
471   mutable unsigned Size; // Size in bytes excluding size header.
472 public:
473   DIELoc() : DIEValue(isLoc), Size(0) {}
474
475   /// ComputeSize - Calculate the size of the location expression.
476   ///
477   unsigned ComputeSize(AsmPrinter *AP) const;
478
479   /// BestForm - Choose the best form for data.
480   ///
481   dwarf::Form BestForm(unsigned DwarfVersion) const {
482     if (DwarfVersion > 3)
483       return dwarf::DW_FORM_exprloc;
484     // Pre-DWARF4 location expressions were blocks and not exprloc.
485     if ((unsigned char)Size == Size)
486       return dwarf::DW_FORM_block1;
487     if ((unsigned short)Size == Size)
488       return dwarf::DW_FORM_block2;
489     if ((unsigned int)Size == Size)
490       return dwarf::DW_FORM_block4;
491     return dwarf::DW_FORM_block;
492   }
493
494   /// EmitValue - Emit location data.
495   ///
496   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
497
498   /// SizeOf - Determine size of location data in bytes.
499   ///
500   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
501
502   // Implement isa/cast/dyncast.
503   static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
504
505 #ifndef NDEBUG
506   void print(raw_ostream &O) const override;
507 #endif
508 };
509
510 //===--------------------------------------------------------------------===//
511 /// DIEBlock - Represents a block of values.
512 //
513 class DIEBlock : public DIEValue, public DIE {
514   mutable unsigned Size; // Size in bytes excluding size header.
515 public:
516   DIEBlock() : DIEValue(isBlock), Size(0) {}
517
518   /// ComputeSize - Calculate the size of the location expression.
519   ///
520   unsigned ComputeSize(AsmPrinter *AP) const;
521
522   /// BestForm - Choose the best form for data.
523   ///
524   dwarf::Form BestForm() const {
525     if ((unsigned char)Size == Size)
526       return dwarf::DW_FORM_block1;
527     if ((unsigned short)Size == Size)
528       return dwarf::DW_FORM_block2;
529     if ((unsigned int)Size == Size)
530       return dwarf::DW_FORM_block4;
531     return dwarf::DW_FORM_block;
532   }
533
534   /// EmitValue - Emit location data.
535   ///
536   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
537
538   /// SizeOf - Determine size of location data in bytes.
539   ///
540   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
541
542   // Implement isa/cast/dyncast.
543   static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
544
545 #ifndef NDEBUG
546   void print(raw_ostream &O) const override;
547 #endif
548 };
549
550 //===--------------------------------------------------------------------===//
551 /// DIELocList - Represents a pointer to a location list in the debug_loc
552 /// section.
553 //
554 class DIELocList : public DIEValue {
555   // Index into the .debug_loc vector.
556   size_t Index;
557
558 public:
559   DIELocList(size_t I) : DIEValue(isLocList), Index(I) {}
560
561   /// getValue - Grab the current index out.
562   size_t getValue() const { return Index; }
563
564   /// EmitValue - Emit location data.
565   ///
566   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
567
568   /// SizeOf - Determine size of location data in bytes.
569   ///
570   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
571
572   // Implement isa/cast/dyncast.
573   static bool classof(const DIEValue *E) { return E->getType() == isLocList; }
574
575 #ifndef NDEBUG
576   void print(raw_ostream &O) const override;
577 #endif
578 };
579
580 } // end llvm namespace
581
582 #endif