IndentCount is only used within NDEBUG code.
[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 MCSymbol;
26   class raw_ostream;
27
28   //===--------------------------------------------------------------------===//
29   /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
30   /// Dwarf abbreviation.
31   class DIEAbbrevData {
32     /// Attribute - Dwarf attribute code.
33     ///
34     uint16_t Attribute;
35
36     /// Form - Dwarf form code.
37     ///
38     uint16_t Form;
39   public:
40     DIEAbbrevData(uint16_t A, uint16_t F) : Attribute(A), Form(F) {}
41
42     // Accessors.
43     uint16_t getAttribute() const { return Attribute; }
44     uint16_t getForm()      const { return Form; }
45
46     /// Profile - Used to gather unique data for the abbreviation folding set.
47     ///
48     void Profile(FoldingSetNodeID &ID) const;
49   };
50
51   //===--------------------------------------------------------------------===//
52   /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
53   /// information object.
54   class DIEAbbrev : public FoldingSetNode {
55     /// Tag - Dwarf tag code.
56     ///
57     uint16_t Tag;
58
59     /// ChildrenFlag - Dwarf children flag.
60     ///
61     uint16_t ChildrenFlag;
62
63     /// Unique number for node.
64     ///
65     unsigned Number;
66
67     /// Data - Raw data bytes for abbreviation.
68     ///
69     SmallVector<DIEAbbrevData, 12> Data;
70
71   public:
72     DIEAbbrev(uint16_t T, uint16_t C) : Tag(T), ChildrenFlag(C), Data() {}
73
74     // Accessors.
75     uint16_t getTag() const { return Tag; }
76     unsigned getNumber() const { return Number; }
77     uint16_t getChildrenFlag() const { return ChildrenFlag; }
78     const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
79     void setTag(uint16_t T) { Tag = T; }
80     void setChildrenFlag(uint16_t CF) { ChildrenFlag = CF; }
81     void setNumber(unsigned N) { Number = N; }
82
83     /// AddAttribute - Adds another set of attribute information to the
84     /// abbreviation.
85     void AddAttribute(uint16_t Attribute, uint16_t Form) {
86       Data.push_back(DIEAbbrevData(Attribute, Form));
87     }
88
89     /// AddFirstAttribute - Adds a set of attribute information to the front
90     /// of the abbreviation.
91     void AddFirstAttribute(uint16_t Attribute, uint16_t Form) {
92       Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
93     }
94
95     /// Profile - Used to gather unique data for the abbreviation folding set.
96     ///
97     void Profile(FoldingSetNodeID &ID) const;
98
99     /// Emit - Print the abbreviation using the specified asm printer.
100     ///
101     void Emit(AsmPrinter *AP) const;
102
103 #ifndef NDEBUG
104     void print(raw_ostream &O);
105     void dump();
106 #endif
107   };
108
109   //===--------------------------------------------------------------------===//
110   /// DIE - A structured debug information entry.  Has an abbreviation which
111   /// describes its organization.
112   class DIEValue;
113
114   class DIE {
115   protected:
116     /// Offset - Offset in debug info section.
117     ///
118     unsigned Offset;
119
120     /// Size - Size of instance + children.
121     ///
122     unsigned Size;
123
124     /// Abbrev - Buffer for constructing abbreviation.
125     ///
126     DIEAbbrev Abbrev;
127
128     /// Children DIEs.
129     ///
130     std::vector<DIE *> Children;
131
132     DIE *Parent;
133
134     /// Attribute values.
135     ///
136     SmallVector<DIEValue*, 12> Values;
137
138 #ifndef NDEBUG
139     // Private data for print()
140     mutable unsigned IndentCount;
141 #endif
142   public:
143     explicit DIE(unsigned Tag)
144       : Offset(0), Size(0), Abbrev(Tag, dwarf::DW_CHILDREN_no), Parent(0) {}
145     virtual ~DIE();
146
147     // Accessors.
148     DIEAbbrev &getAbbrev() { return Abbrev; }
149     unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
150     unsigned 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 unit DIE this DIE belongs
157     /// to.
158     DIE *getCompileUnit();
159     void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
160     void setOffset(unsigned O) { Offset = O; }
161     void setSize(unsigned S) { Size = S; }
162
163     /// addValue - Add a value and attributes to a DIE.
164     ///
165     void addValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
166       Abbrev.AddAttribute(Attribute, Form);
167       Values.push_back(Value);
168     }
169
170     /// addChild - Add a child to the DIE.
171     ///
172     void addChild(DIE *Child) {
173       if (Child->getParent()) {
174         assert (Child->getParent() == this && "Unexpected DIE Parent!");
175         return;
176       }
177       Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
178       Children.push_back(Child);
179       Child->Parent = this;
180     }
181
182 #ifndef NDEBUG
183     void print(raw_ostream &O, unsigned IndentCount = 0) const;
184     void dump();
185 #endif
186   };
187
188   //===--------------------------------------------------------------------===//
189   /// DIEValue - A debug information entry value.
190   ///
191   class DIEValue {
192     virtual void anchor();
193   public:
194     enum {
195       isInteger,
196       isString,
197       isLabel,
198       isDelta,
199       isEntry,
200       isBlock
201     };
202   protected:
203     /// Type - Type of data stored in the value.
204     ///
205     unsigned Type;
206   public:
207     explicit DIEValue(unsigned T) : Type(T) {}
208     virtual ~DIEValue() {}
209
210     // Accessors
211     unsigned getType()  const { return Type; }
212
213     /// EmitValue - Emit value via the Dwarf writer.
214     ///
215     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const = 0;
216
217     /// SizeOf - Return the size of a value in bytes.
218     ///
219     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const = 0;
220
221 #ifndef NDEBUG
222     virtual void print(raw_ostream &O) const = 0;
223     void dump() const;
224 #endif
225   };
226
227   //===--------------------------------------------------------------------===//
228   /// DIEInteger - An integer value DIE.
229   ///
230   class DIEInteger : public DIEValue {
231     uint64_t Integer;
232   public:
233     explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
234
235     /// BestForm - Choose the best form for integer.
236     ///
237     static unsigned BestForm(bool IsSigned, uint64_t Int) {
238       if (IsSigned) {
239         const int64_t SignedInt = Int;
240         if ((char)Int == SignedInt)     return dwarf::DW_FORM_data1;
241         if ((short)Int == SignedInt)    return dwarf::DW_FORM_data2;
242         if ((int)Int == SignedInt)      return dwarf::DW_FORM_data4;
243       } else {
244         if ((unsigned char)Int == Int)  return dwarf::DW_FORM_data1;
245         if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
246         if ((unsigned int)Int == Int)   return dwarf::DW_FORM_data4;
247       }
248       return dwarf::DW_FORM_data8;
249     }
250
251     /// EmitValue - Emit integer of appropriate size.
252     ///
253     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
254
255     uint64_t getValue() const { return Integer; }
256
257     /// SizeOf - Determine size of integer value in bytes.
258     ///
259     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
260
261     // Implement isa/cast/dyncast.
262     static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
263
264 #ifndef NDEBUG
265     virtual void print(raw_ostream &O) const;
266 #endif
267   };
268
269   //===--------------------------------------------------------------------===//
270   /// DIELabel - A label expression DIE.
271   //
272   class DIELabel : public DIEValue {
273     const MCSymbol *Label;
274   public:
275     explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
276
277     /// EmitValue - Emit label value.
278     ///
279     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
280
281     /// getValue - Get MCSymbol.
282     ///
283     const MCSymbol *getValue()       const { return Label; }
284
285     /// SizeOf - Determine size of label value in bytes.
286     ///
287     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
288
289     // Implement isa/cast/dyncast.
290     static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
291
292 #ifndef NDEBUG
293     virtual void print(raw_ostream &O) const;
294 #endif
295   };
296
297   //===--------------------------------------------------------------------===//
298   /// DIEDelta - A simple label difference DIE.
299   ///
300   class DIEDelta : public DIEValue {
301     const MCSymbol *LabelHi;
302     const MCSymbol *LabelLo;
303   public:
304     DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
305       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
306
307     /// EmitValue - Emit delta value.
308     ///
309     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
310
311     /// SizeOf - Determine size of delta value in bytes.
312     ///
313     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
314
315     // Implement isa/cast/dyncast.
316     static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
317
318 #ifndef NDEBUG
319     virtual void print(raw_ostream &O) const;
320 #endif
321   };
322
323   //===--------------------------------------------------------------------===//
324   /// DIEEntry - A pointer to another debug information entry.  An instance of
325   /// this class can also be used as a proxy for a debug information entry not
326   /// yet defined (ie. types.)
327   class DIEEntry : public DIEValue {
328     DIE *const Entry;
329   public:
330     explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
331       assert(E && "Cannot construct a DIEEntry with a null DIE");
332     }
333
334     DIE *getEntry() const { return Entry; }
335
336     /// EmitValue - Emit debug information entry offset.
337     ///
338     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
339
340     /// SizeOf - Determine size of debug information entry in bytes.
341     ///
342     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const {
343       return sizeof(int32_t);
344     }
345
346     // Implement isa/cast/dyncast.
347     static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
348
349 #ifndef NDEBUG
350     virtual void print(raw_ostream &O) const;
351 #endif
352   };
353
354   //===--------------------------------------------------------------------===//
355   /// DIEBlock - A block of values.  Primarily used for location expressions.
356   //
357   class DIEBlock : public DIEValue, public DIE {
358     unsigned Size;                // Size in bytes excluding size header.
359   public:
360     DIEBlock()
361       : DIEValue(isBlock), DIE(0), Size(0) {}
362     virtual ~DIEBlock() {}
363
364     /// ComputeSize - calculate the size of the block.
365     ///
366     unsigned ComputeSize(AsmPrinter *AP);
367
368     /// BestForm - Choose the best form for data.
369     ///
370     unsigned BestForm() const {
371       if ((unsigned char)Size == Size)  return dwarf::DW_FORM_block1;
372       if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
373       if ((unsigned int)Size == Size)   return dwarf::DW_FORM_block4;
374       return dwarf::DW_FORM_block;
375     }
376
377     /// EmitValue - Emit block data.
378     ///
379     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
380
381     /// SizeOf - Determine size of block data in bytes.
382     ///
383     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
384
385     // Implement isa/cast/dyncast.
386     static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
387
388 #ifndef NDEBUG
389     virtual void print(raw_ostream &O) const;
390 #endif
391   };
392
393 } // end llvm namespace
394
395 #endif