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