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