Enable DebugInfo support for COFF object files.
[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     unsigned Attribute;
35
36     /// Form - Dwarf form code.
37     ///
38     unsigned Form;
39   public:
40     DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
41
42     // Accessors.
43     unsigned getAttribute() const { return Attribute; }
44     unsigned 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     unsigned Tag;
58
59     /// Unique number for node.
60     ///
61     unsigned Number;
62
63     /// ChildrenFlag - Dwarf children flag.
64     ///
65     unsigned ChildrenFlag;
66
67     /// Data - Raw data bytes for abbreviation.
68     ///
69     SmallVector<DIEAbbrevData, 8> Data;
70
71   public:
72     DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
73     virtual ~DIEAbbrev() {}
74
75     // Accessors.
76     unsigned getTag() const { return Tag; }
77     unsigned getNumber() const { return Number; }
78     unsigned getChildrenFlag() const { return ChildrenFlag; }
79     const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
80     void setTag(unsigned T) { Tag = T; }
81     void setChildrenFlag(unsigned 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(unsigned Attribute, unsigned Form) {
87       Data.push_back(DIEAbbrevData(Attribute, Form));
88     }
89
90     /// AddFirstAttribute - Adds a set of attribute information to the front
91     /// of the abbreviation.
92     void AddFirstAttribute(unsigned Attribute, unsigned Form) {
93       Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
94     }
95
96     /// Profile - Used to gather unique data for the abbreviation folding set.
97     ///
98     void Profile(FoldingSetNodeID &ID) const;
99
100     /// Emit - Print the abbreviation using the specified asm printer.
101     ///
102     void Emit(AsmPrinter *AP) const;
103
104 #ifndef NDEBUG
105     void print(raw_ostream &O);
106     void dump();
107 #endif
108   };
109
110   //===--------------------------------------------------------------------===//
111   /// DIE - A structured debug information entry.  Has an abbreviation which
112   /// describes it's organization.
113   class DIEValue;
114
115   class DIE {
116   protected:
117     /// Abbrev - Buffer for constructing abbreviation.
118     ///
119     DIEAbbrev Abbrev;
120
121     /// Offset - Offset in debug info section.
122     ///
123     unsigned Offset;
124
125     /// Size - Size of instance + children.
126     ///
127     unsigned Size;
128
129     /// Children DIEs.
130     ///
131     std::vector<DIE *> Children;
132
133     DIE *Parent;
134
135     /// Attributes values.
136     ///
137     SmallVector<DIEValue*, 32> Values;
138
139     // Private data for print()
140     mutable unsigned IndentCount;
141   public:
142     explicit DIE(unsigned Tag)
143       : Abbrev(Tag, dwarf::DW_CHILDREN_no), Offset(0),
144         Size(0), Parent (0), IndentCount(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 SmallVector<DIEValue*, 32> &getValues() const { return Values; }
155     DIE *getParent() const { return Parent; }
156     void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
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(unsigned Attribute, unsigned Form, DIEValue *Value) {
163       Abbrev.AddAttribute(Attribute, Form);
164       Values.push_back(Value);
165     }
166
167     /// SiblingOffset - Return the offset of the debug information entry's
168     /// sibling.
169     unsigned getSiblingOffset() const { return Offset + Size; }
170
171     /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
172     /// The caller is responsible for deleting the return value at or after the
173     /// same time it destroys this DIE.
174     ///
175     DIEValue *addSiblingOffset(BumpPtrAllocator &A);
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(AsmPrinter *AP, unsigned Form) const = 0;
223
224     /// SizeOf - Return the size of a value in bytes.
225     ///
226     virtual unsigned SizeOf(AsmPrinter *AP, 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(AsmPrinter *AP, unsigned Form) const;
263
264     uint64_t getValue() const { return Integer; }
265
266     /// SizeOf - Determine size of integer value in bytes.
267     ///
268     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
269
270     // Implement isa/cast/dyncast.
271     static bool classof(const DIEInteger *) { return true; }
272     static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
273
274 #ifndef NDEBUG
275     virtual void print(raw_ostream &O);
276 #endif
277   };
278
279   //===--------------------------------------------------------------------===//
280   /// DIEString - A string value DIE. This DIE keeps string reference only.
281   ///
282   class DIEString : public DIEValue {
283     const StringRef Str;
284   public:
285     explicit DIEString(const StringRef S) : DIEValue(isString), Str(S) {}
286
287     /// EmitValue - Emit string value.
288     ///
289     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
290
291     /// SizeOf - Determine size of string value in bytes.
292     ///
293     virtual unsigned SizeOf(AsmPrinter *AP, unsigned /*Form*/) const {
294       return Str.size() + sizeof(char); // sizeof('\0');
295     }
296
297     // Implement isa/cast/dyncast.
298     static bool classof(const DIEString *) { return true; }
299     static bool classof(const DIEValue *S) { return S->getType() == isString; }
300
301 #ifndef NDEBUG
302     virtual void print(raw_ostream &O);
303 #endif
304   };
305
306   //===--------------------------------------------------------------------===//
307   /// DIELabel - A label expression DIE.
308   //
309   class DIELabel : public DIEValue {
310     const MCSymbol *Label;
311   public:
312     explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
313
314     /// EmitValue - Emit label value.
315     ///
316     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
317
318     /// getValue - Get MCSymbol.
319     ///
320     const MCSymbol *getValue()       const { return Label; }
321
322     /// SizeOf - Determine size of label value in bytes.
323     ///
324     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
325
326     // Implement isa/cast/dyncast.
327     static bool classof(const DIELabel *)  { return true; }
328     static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
329
330 #ifndef NDEBUG
331     virtual void print(raw_ostream &O);
332 #endif
333   };
334
335   //===--------------------------------------------------------------------===//
336   /// DIESectionOffset - A section relative label expression DIE.
337   //
338   class DIESectionOffset : public DIEValue {
339     const MCSymbol *Label;
340   public:
341     explicit DIESectionOffset(const MCSymbol *L) : DIEValue(isSectionOffset),
342       Label(L) {}
343
344     /// EmitValue - Emit label value.
345     ///
346     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
347
348     /// getValue - Get MCSymbol.
349     ///
350     const MCSymbol *getValue()       const { return Label; }
351
352     /// SizeOf - Determine size of label value in bytes.
353     ///
354     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
355
356     // Implement isa/cast/dyncast.
357     static bool classof(const DIELabel *)  { return true; }
358     static bool classof(const DIEValue *L) { return L->getType() == isSectionOffset; }
359
360 #ifndef NDEBUG
361     virtual void print(raw_ostream &O);
362 #endif
363   };
364
365   //===--------------------------------------------------------------------===//
366   /// DIEDelta - A simple label difference DIE.
367   ///
368   class DIEDelta : public DIEValue {
369     const MCSymbol *LabelHi;
370     const MCSymbol *LabelLo;
371   public:
372     DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
373       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
374
375     /// EmitValue - Emit delta value.
376     ///
377     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
378
379     /// SizeOf - Determine size of delta value in bytes.
380     ///
381     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
382
383     // Implement isa/cast/dyncast.
384     static bool classof(const DIEDelta *)  { return true; }
385     static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
386
387 #ifndef NDEBUG
388     virtual void print(raw_ostream &O);
389 #endif
390   };
391
392   //===--------------------------------------------------------------------===//
393   /// DIEntry - A pointer to another debug information entry.  An instance of
394   /// this class can also be used as a proxy for a debug information entry not
395   /// yet defined (ie. types.)
396   class DIEEntry : public DIEValue {
397     DIE *const Entry;
398   public:
399     explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
400
401     DIE *getEntry() const { return Entry; }
402
403     /// EmitValue - Emit debug information entry offset.
404     ///
405     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
406
407     /// SizeOf - Determine size of debug information entry in bytes.
408     ///
409     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const {
410       return sizeof(int32_t);
411     }
412
413     // Implement isa/cast/dyncast.
414     static bool classof(const DIEEntry *)  { return true; }
415     static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
416
417 #ifndef NDEBUG
418     virtual void print(raw_ostream &O);
419 #endif
420   };
421
422   //===--------------------------------------------------------------------===//
423   /// DIEBlock - A block of values.  Primarily used for location expressions.
424   //
425   class DIEBlock : public DIEValue, public DIE {
426     unsigned Size;                // Size in bytes excluding size header.
427   public:
428     DIEBlock()
429       : DIEValue(isBlock), DIE(0), Size(0) {}
430     virtual ~DIEBlock() {}
431
432     /// ComputeSize - calculate the size of the block.
433     ///
434     unsigned ComputeSize(AsmPrinter *AP);
435
436     /// BestForm - Choose the best form for data.
437     ///
438     unsigned BestForm() const {
439       if ((unsigned char)Size == Size)  return dwarf::DW_FORM_block1;
440       if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
441       if ((unsigned int)Size == Size)   return dwarf::DW_FORM_block4;
442       return dwarf::DW_FORM_block;
443     }
444
445     /// EmitValue - Emit block data.
446     ///
447     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
448
449     /// SizeOf - Determine size of block data in bytes.
450     ///
451     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
452
453     // Implement isa/cast/dyncast.
454     static bool classof(const DIEBlock *)  { return true; }
455     static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
456
457 #ifndef NDEBUG
458     virtual void print(raw_ostream &O);
459 #endif
460   };
461
462 } // end llvm namespace
463
464 #endif