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