Whitespace and 80-col.
[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, 8> 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 SmallVector<DIEAbbrevData, 8> &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 it's 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*, 32> Values;
137
138     // Private data for print()
139     mutable unsigned IndentCount;
140   public:
141     explicit DIE(unsigned Tag)
142       : Offset(0), Size(0), Abbrev(Tag, dwarf::DW_CHILDREN_no), Parent(0),
143         IndentCount(0) {}
144     virtual ~DIE();
145
146     // Accessors.
147     DIEAbbrev &getAbbrev() { return Abbrev; }
148     unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
149     unsigned getTag() const { return Abbrev.getTag(); }
150     unsigned getOffset() const { return Offset; }
151     unsigned getSize() const { return Size; }
152     const std::vector<DIE *> &getChildren() const { return Children; }
153     const SmallVector<DIEValue*, 32> &getValues() const { return Values; }
154     DIE *getParent() const { return Parent; }
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 IncIndent = 0);
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       isLabel,
194       isDelta,
195       isEntry,
196       isBlock
197     };
198   protected:
199     /// Type - Type of data stored in the value.
200     ///
201     unsigned Type;
202   public:
203     explicit DIEValue(unsigned T) : Type(T) {}
204     virtual ~DIEValue() {}
205
206     // Accessors
207     unsigned getType()  const { return Type; }
208
209     /// EmitValue - Emit value via the Dwarf writer.
210     ///
211     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const = 0;
212
213     /// SizeOf - Return the size of a value in bytes.
214     ///
215     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const = 0;
216
217 #ifndef NDEBUG
218     virtual void print(raw_ostream &O) = 0;
219     void dump();
220 #endif
221   };
222
223   //===--------------------------------------------------------------------===//
224   /// DIEInteger - An integer value DIE.
225   ///
226   class DIEInteger : public DIEValue {
227     uint64_t Integer;
228   public:
229     explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
230
231     /// BestForm - Choose the best form for integer.
232     ///
233     static unsigned BestForm(bool IsSigned, uint64_t Int) {
234       if (IsSigned) {
235         if ((char)Int == (signed)Int)   return dwarf::DW_FORM_data1;
236         if ((short)Int == (signed)Int)  return dwarf::DW_FORM_data2;
237         if ((int)Int == (signed)Int)    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, unsigned 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, unsigned 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);
261 #endif
262   };
263
264   //===--------------------------------------------------------------------===//
265   /// DIELabel - A label expression DIE.
266   //
267   class DIELabel : public DIEValue {
268     const MCSymbol *Label;
269   public:
270     explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
271
272     /// EmitValue - Emit label value.
273     ///
274     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
275
276     /// getValue - Get MCSymbol.
277     ///
278     const MCSymbol *getValue()       const { return Label; }
279
280     /// SizeOf - Determine size of label value in bytes.
281     ///
282     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
283
284     // Implement isa/cast/dyncast.
285     static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
286
287 #ifndef NDEBUG
288     virtual void print(raw_ostream &O);
289 #endif
290   };
291
292   //===--------------------------------------------------------------------===//
293   /// DIEDelta - A simple label difference DIE.
294   ///
295   class DIEDelta : public DIEValue {
296     const MCSymbol *LabelHi;
297     const MCSymbol *LabelLo;
298   public:
299     DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
300       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
301
302     /// EmitValue - Emit delta value.
303     ///
304     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
305
306     /// SizeOf - Determine size of delta value in bytes.
307     ///
308     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
309
310     // Implement isa/cast/dyncast.
311     static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
312
313 #ifndef NDEBUG
314     virtual void print(raw_ostream &O);
315 #endif
316   };
317
318   //===--------------------------------------------------------------------===//
319   /// DIEEntry - A pointer to another debug information entry.  An instance of
320   /// this class can also be used as a proxy for a debug information entry not
321   /// yet defined (ie. types.)
322   class DIEEntry : public DIEValue {
323     DIE *const Entry;
324   public:
325     explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
326
327     DIE *getEntry() const { return Entry; }
328
329     /// EmitValue - Emit debug information entry offset.
330     ///
331     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
332
333     /// SizeOf - Determine size of debug information entry in bytes.
334     ///
335     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const {
336       return sizeof(int32_t);
337     }
338
339     // Implement isa/cast/dyncast.
340     static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
341
342 #ifndef NDEBUG
343     virtual void print(raw_ostream &O);
344 #endif
345   };
346
347   //===--------------------------------------------------------------------===//
348   /// DIEBlock - A block of values.  Primarily used for location expressions.
349   //
350   class DIEBlock : public DIEValue, public DIE {
351     unsigned Size;                // Size in bytes excluding size header.
352   public:
353     DIEBlock()
354       : DIEValue(isBlock), DIE(0), Size(0) {}
355     virtual ~DIEBlock() {}
356
357     /// ComputeSize - calculate the size of the block.
358     ///
359     unsigned ComputeSize(AsmPrinter *AP);
360
361     /// BestForm - Choose the best form for data.
362     ///
363     unsigned BestForm() const {
364       if ((unsigned char)Size == Size)  return dwarf::DW_FORM_block1;
365       if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
366       if ((unsigned int)Size == Size)   return dwarf::DW_FORM_block4;
367       return dwarf::DW_FORM_block;
368     }
369
370     /// EmitValue - Emit block data.
371     ///
372     virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
373
374     /// SizeOf - Determine size of block data in bytes.
375     ///
376     virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
377
378     // Implement isa/cast/dyncast.
379     static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
380
381 #ifndef NDEBUG
382     virtual void print(raw_ostream &O);
383 #endif
384   };
385
386 } // end llvm namespace
387
388 #endif