Free DbgScopes in DwarfDebug::endFunction(). Also increased the const-ness of
[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/Compiler.h"
20 #include "llvm/Support/Dwarf.h"
21 #include <vector>
22
23 namespace llvm {
24   class AsmPrinter;
25   class DwarfPrinter;
26   class TargetData;
27   class MCSymbol;
28   class raw_ostream;
29
30   //===--------------------------------------------------------------------===//
31   /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
32   /// Dwarf abbreviation.
33   class DIEAbbrevData {
34     /// Attribute - Dwarf attribute code.
35     ///
36     unsigned Attribute;
37
38     /// Form - Dwarf form code.
39     ///
40     unsigned Form;
41   public:
42     DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
43
44     // Accessors.
45     unsigned getAttribute() const { return Attribute; }
46     unsigned 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     /// Tag - Dwarf tag code.
58     ///
59     unsigned Tag;
60
61     /// Unique number for node.
62     ///
63     unsigned Number;
64
65     /// ChildrenFlag - Dwarf children flag.
66     ///
67     unsigned ChildrenFlag;
68
69     /// Data - Raw data bytes for abbreviation.
70     ///
71     SmallVector<DIEAbbrevData, 8> Data;
72
73   public:
74     DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
75     virtual ~DIEAbbrev() {}
76
77     // Accessors.
78     unsigned getTag() const { return Tag; }
79     unsigned getNumber() const { return Number; }
80     unsigned getChildrenFlag() const { return ChildrenFlag; }
81     const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
82     void setTag(unsigned T) { Tag = T; }
83     void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
84     void setNumber(unsigned N) { Number = N; }
85
86     /// AddAttribute - Adds another set of attribute information to the
87     /// abbreviation.
88     void AddAttribute(unsigned Attribute, unsigned Form) {
89       Data.push_back(DIEAbbrevData(Attribute, Form));
90     }
91
92     /// AddFirstAttribute - Adds a set of attribute information to the front
93     /// of the abbreviation.
94     void AddFirstAttribute(unsigned Attribute, unsigned Form) {
95       Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
96     }
97
98     /// Profile - Used to gather unique data for the abbreviation folding set.
99     ///
100     void Profile(FoldingSetNodeID &ID) const;
101
102     /// Emit - Print the abbreviation using the specified asm printer.
103     ///
104     void Emit(const DwarfPrinter *DP) const;
105
106 #ifndef NDEBUG
107     void print(raw_ostream &O);
108     void dump();
109 #endif
110   };
111
112   //===--------------------------------------------------------------------===//
113   /// DIE - A structured debug information entry.  Has an abbreviation which
114   /// describes it's organization.
115   class DIEValue;
116
117   class DIE {
118   protected:
119     /// Abbrev - Buffer for constructing abbreviation.
120     ///
121     DIEAbbrev Abbrev;
122
123     /// Offset - Offset in debug info section.
124     ///
125     unsigned Offset;
126
127     /// Size - Size of instance + children.
128     ///
129     unsigned Size;
130
131     /// Children DIEs.
132     ///
133     std::vector<DIE *> Children;
134
135     DIE *Parent;
136
137     /// Attributes values.
138     ///
139     SmallVector<DIEValue*, 32> Values;
140
141     // Private data for print()
142     mutable unsigned IndentCount;
143   public:
144     explicit DIE(unsigned Tag)
145       : Abbrev(Tag, dwarf::DW_CHILDREN_no), Offset(0),
146         Size(0), Parent (0), IndentCount(0) {}
147     virtual ~DIE();
148
149     // Accessors.
150     DIEAbbrev &getAbbrev() { return Abbrev; }
151     unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
152     unsigned getTag() const { return Abbrev.getTag(); }
153     unsigned getOffset() const { return Offset; }
154     unsigned getSize() const { return Size; }
155     const std::vector<DIE *> &getChildren() const { return Children; }
156     SmallVector<DIEValue*, 32> &getValues() { return Values; }
157     DIE *getParent() const { return Parent; }
158     void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
159     void setOffset(unsigned O) { Offset = O; }
160     void setSize(unsigned S) { Size = S; }
161     
162     /// addValue - Add a value and attributes to a DIE.
163     ///
164     void addValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
165       Abbrev.AddAttribute(Attribute, Form);
166       Values.push_back(Value);
167     }
168
169     /// SiblingOffset - Return the offset of the debug information entry's
170     /// sibling.
171     unsigned getSiblingOffset() const { return Offset + Size; }
172
173     /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
174     ///
175     void addSiblingOffset();
176
177     /// addChild - Add a child to the DIE.
178     ///
179     void addChild(DIE *Child) {
180       if (Child->getParent()) {
181         assert (Child->getParent() == this && "Unexpected DIE Parent!");
182         return;
183       }
184       Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
185       Children.push_back(Child);
186       Child->Parent = this;
187     }
188
189 #ifndef NDEBUG
190     void print(raw_ostream &O, unsigned IncIndent = 0);
191     void dump();
192 #endif
193   };
194
195   //===--------------------------------------------------------------------===//
196   /// DIEValue - A debug information entry value.
197   ///
198   class DIEValue {
199   public:
200     enum {
201       isInteger,
202       isString,
203       isLabel,
204       isSectionOffset,
205       isDelta,
206       isEntry,
207       isBlock
208     };
209   protected:
210     /// Type - Type of data stored in the value.
211     ///
212     unsigned Type;
213   public:
214     explicit DIEValue(unsigned T) : Type(T) {}
215     virtual ~DIEValue() {}
216
217     // Accessors
218     unsigned getType()  const { return Type; }
219
220     /// EmitValue - Emit value via the Dwarf writer.
221     ///
222     virtual void EmitValue(DwarfPrinter *D, unsigned Form) const = 0;
223
224     /// SizeOf - Return the size of a value in bytes.
225     ///
226     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const = 0;
227
228     // Implement isa/cast/dyncast.
229     static bool classof(const DIEValue *) { return true; }
230
231 #ifndef NDEBUG
232     virtual void print(raw_ostream &O) = 0;
233     void dump();
234 #endif
235   };
236
237   //===--------------------------------------------------------------------===//
238   /// DIEInteger - An integer value DIE.
239   ///
240   class DIEInteger : public DIEValue {
241     uint64_t Integer;
242   public:
243     explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
244
245     /// BestForm - Choose the best form for integer.
246     ///
247     static unsigned BestForm(bool IsSigned, uint64_t Int) {
248       if (IsSigned) {
249         if ((char)Int == (signed)Int)   return dwarf::DW_FORM_data1;
250         if ((short)Int == (signed)Int)  return dwarf::DW_FORM_data2;
251         if ((int)Int == (signed)Int)    return dwarf::DW_FORM_data4;
252       } else {
253         if ((unsigned char)Int == Int)  return dwarf::DW_FORM_data1;
254         if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
255         if ((unsigned int)Int == Int)   return dwarf::DW_FORM_data4;
256       }
257       return dwarf::DW_FORM_data8;
258     }
259
260     /// EmitValue - Emit integer of appropriate size.
261     ///
262     virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
263
264     /// SizeOf - Determine size of integer value in bytes.
265     ///
266     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
267
268
269     // Implement isa/cast/dyncast.
270     static bool classof(const DIEInteger *) { return true; }
271     static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
272
273 #ifndef NDEBUG
274     virtual void print(raw_ostream &O);
275 #endif
276   };
277
278   //===--------------------------------------------------------------------===//
279   /// DIEString - A string value DIE. This DIE keeps string reference only.
280   ///
281   class DIEString : public DIEValue {
282     const StringRef Str;
283   public:
284     explicit DIEString(const StringRef S) : DIEValue(isString), Str(S) {}
285
286     /// EmitValue - Emit string value.
287     ///
288     virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
289
290     /// SizeOf - Determine size of string value in bytes.
291     ///
292     virtual unsigned SizeOf(const TargetData *, unsigned /*Form*/) const {
293       return Str.size() + sizeof(char); // sizeof('\0');
294     }
295
296     // Implement isa/cast/dyncast.
297     static bool classof(const DIEString *) { return true; }
298     static bool classof(const DIEValue *S) { return S->getType() == isString; }
299
300 #ifndef NDEBUG
301     virtual void print(raw_ostream &O);
302 #endif
303   };
304
305   //===--------------------------------------------------------------------===//
306   /// DIELabel - A label expression DIE.
307   //
308   class DIELabel : public DIEValue {
309     const MCSymbol *Label;
310   public:
311     explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
312
313     /// EmitValue - Emit label value.
314     ///
315     virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
316
317     /// SizeOf - Determine size of label value in bytes.
318     ///
319     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
320
321     // Implement isa/cast/dyncast.
322     static bool classof(const DIELabel *)  { return true; }
323     static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
324
325 #ifndef NDEBUG
326     virtual void print(raw_ostream &O);
327 #endif
328   };
329
330   //===--------------------------------------------------------------------===//
331   /// DIESectionOffset - A section offset DIE.
332   ///
333   class DIESectionOffset : public DIEValue {
334     const MCSymbol *Label;
335     const MCSymbol *Section;
336     bool IsEH : 1;
337   public:
338     DIESectionOffset(const MCSymbol *Lab, const MCSymbol *Sec,
339                      bool isEH = false)
340       : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
341         IsEH(isEH) {}
342
343     /// EmitValue - Emit section offset.
344     ///
345     virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
346
347     /// SizeOf - Determine size of section offset value in bytes.
348     ///
349     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
350
351     // Implement isa/cast/dyncast.
352     static bool classof(const DIESectionOffset *)  { return true; }
353     static bool classof(const DIEValue *D) {
354       return D->getType() == isSectionOffset;
355     }
356
357 #ifndef NDEBUG
358     virtual void print(raw_ostream &O);
359 #endif
360   };
361
362   //===--------------------------------------------------------------------===//
363   /// DIEDelta - A simple label difference DIE.
364   ///
365   class DIEDelta : public DIEValue {
366     const MCSymbol *LabelHi;
367     const MCSymbol *LabelLo;
368   public:
369     DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
370       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
371
372     /// EmitValue - Emit delta value.
373     ///
374     virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
375
376     /// SizeOf - Determine size of delta value in bytes.
377     ///
378     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
379
380     // Implement isa/cast/dyncast.
381     static bool classof(const DIEDelta *)  { return true; }
382     static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
383
384 #ifndef NDEBUG
385     virtual void print(raw_ostream &O);
386 #endif
387   };
388
389   //===--------------------------------------------------------------------===//
390   /// DIEntry - A pointer to another debug information entry.  An instance of
391   /// this class can also be used as a proxy for a debug information entry not
392   /// yet defined (ie. types.)
393   class DIEEntry : public DIEValue {
394     DIE *const Entry;
395   public:
396     explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
397
398     DIE *getEntry() const { return Entry; }
399
400     /// EmitValue - Emit debug information entry offset.
401     ///
402     virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
403
404     /// SizeOf - Determine size of debug information entry in bytes.
405     ///
406     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const {
407       return sizeof(int32_t);
408     }
409
410     // Implement isa/cast/dyncast.
411     static bool classof(const DIEEntry *)  { return true; }
412     static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
413
414 #ifndef NDEBUG
415     virtual void print(raw_ostream &O);
416 #endif
417   };
418
419   //===--------------------------------------------------------------------===//
420   /// DIEBlock - A block of values.  Primarily used for location expressions.
421   //
422   class DIEBlock : public DIEValue, public DIE {
423     unsigned Size;                // Size in bytes excluding size header.
424   public:
425     DIEBlock()
426       : DIEValue(isBlock), DIE(0), Size(0) {}
427     virtual ~DIEBlock() {}
428
429     /// ComputeSize - calculate the size of the block.
430     ///
431     unsigned ComputeSize(const TargetData *TD);
432
433     /// BestForm - Choose the best form for data.
434     ///
435     unsigned BestForm() const {
436       if ((unsigned char)Size == Size)  return dwarf::DW_FORM_block1;
437       if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
438       if ((unsigned int)Size == Size)   return dwarf::DW_FORM_block4;
439       return dwarf::DW_FORM_block;
440     }
441
442     /// EmitValue - Emit block data.
443     ///
444     virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
445
446     /// SizeOf - Determine size of block data in bytes.
447     ///
448     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
449
450     // Implement isa/cast/dyncast.
451     static bool classof(const DIEBlock *)  { return true; }
452     static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
453
454 #ifndef NDEBUG
455     virtual void print(raw_ostream &O);
456 #endif
457   };
458
459 } // end llvm namespace
460
461 #endif