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