3f89aea02f785cff278a010c0d103fbc97892fe3
[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 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(uint16_t Attribute, uint16_t 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(Tag, dwarf::DW_CHILDREN_no), Parent(0) {}
140     virtual ~DIE();
141
142     // Accessors.
143     DIEAbbrev &getAbbrev() { return Abbrev; }
144     unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
145     uint16_t getTag() const { return Abbrev.getTag(); }
146     unsigned getOffset() const { return Offset; }
147     unsigned getSize() const { return Size; }
148     const std::vector<DIE *> &getChildren() const { return Children; }
149     const SmallVectorImpl<DIEValue*> &getValues() const { return Values; }
150     DIE *getParent() const { return Parent; }
151     void setOffset(unsigned O) { Offset = O; }
152     void setSize(unsigned S) { Size = S; }
153
154     /// addValue - Add a value and attributes to a DIE.
155     ///
156     void addValue(uint16_t Attribute, uint16_t Form, DIEValue *Value) {
157       Abbrev.AddAttribute(Attribute, Form);
158       Values.push_back(Value);
159     }
160
161     /// addChild - Add a child to the DIE.
162     ///
163     void addChild(DIE *Child) {
164       assert(!Child->getParent());
165       Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
166       Children.push_back(Child);
167       Child->Parent = this;
168     }
169
170     /// findAttribute - Find a value in the DIE with the attribute given, returns NULL
171     /// if no such attribute exists.
172     DIEValue *findAttribute(uint16_t Attribute);
173
174 #ifndef NDEBUG
175     void print(raw_ostream &O, unsigned IndentCount = 0) const;
176     void dump();
177 #endif
178   };
179
180   //===--------------------------------------------------------------------===//
181   /// DIEValue - A debug information entry value.
182   ///
183   class DIEValue {
184     virtual void anchor();
185   public:
186     enum {
187       isInteger,
188       isString,
189       isExpr,
190       isLabel,
191       isDelta,
192       isEntry,
193       isBlock
194     };
195   protected:
196     /// Type - Type of data stored in the value.
197     ///
198     unsigned Type;
199   public:
200     explicit DIEValue(unsigned T) : Type(T) {}
201     virtual ~DIEValue() {}
202
203     // Accessors
204     unsigned getType()  const { return Type; }
205
206     /// EmitValue - Emit value via the Dwarf writer.
207     ///
208     virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const = 0;
209
210     /// SizeOf - Return the size of a value in bytes.
211     ///
212     virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const = 0;
213
214 #ifndef NDEBUG
215     virtual void print(raw_ostream &O) const = 0;
216     void dump() const;
217 #endif
218   };
219
220   //===--------------------------------------------------------------------===//
221   /// DIEInteger - An integer value DIE.
222   ///
223   class DIEInteger : public DIEValue {
224     uint64_t Integer;
225   public:
226     explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
227
228     /// BestForm - Choose the best form for integer.
229     ///
230     static uint16_t BestForm(bool IsSigned, uint64_t Int) {
231       if (IsSigned) {
232         const int64_t SignedInt = Int;
233         if ((char)Int == SignedInt)     return dwarf::DW_FORM_data1;
234         if ((short)Int == SignedInt)    return dwarf::DW_FORM_data2;
235         if ((int)Int == SignedInt)      return dwarf::DW_FORM_data4;
236       } else {
237         if ((unsigned char)Int == Int)  return dwarf::DW_FORM_data1;
238         if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
239         if ((unsigned int)Int == Int)   return dwarf::DW_FORM_data4;
240       }
241       return dwarf::DW_FORM_data8;
242     }
243
244     /// EmitValue - Emit integer of appropriate size.
245     ///
246     virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
247
248     uint64_t getValue() const { return Integer; }
249
250     /// SizeOf - Determine size of integer value in bytes.
251     ///
252     virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const;
253
254     // Implement isa/cast/dyncast.
255     static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
256
257 #ifndef NDEBUG
258     virtual void print(raw_ostream &O) const;
259 #endif
260   };
261
262   //===--------------------------------------------------------------------===//
263   /// DIEExpr - An expression DIE.
264   //
265   class DIEExpr : public DIEValue {
266     const MCExpr *Expr;
267   public:
268     explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
269
270     /// EmitValue - Emit expression value.
271     ///
272     virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
273
274     /// getValue - Get MCExpr.
275     ///
276     const MCExpr *getValue() const { return Expr; }
277
278     /// SizeOf - Determine size of expression value in bytes.
279     ///
280     virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const;
281
282     // Implement isa/cast/dyncast.
283     static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
284
285 #ifndef NDEBUG
286     virtual void print(raw_ostream &O) const;
287 #endif
288   };
289
290   //===--------------------------------------------------------------------===//
291   /// DIELabel - A label DIE.
292   //
293   class DIELabel : public DIEValue {
294     const MCSymbol *Label;
295   public:
296     explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
297
298     /// EmitValue - Emit label value.
299     ///
300     virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
301
302     /// getValue - Get MCSymbol.
303     ///
304     const MCSymbol *getValue() const { return Label; }
305
306     /// SizeOf - Determine size of label value in bytes.
307     ///
308     virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const;
309
310     // Implement isa/cast/dyncast.
311     static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
312
313 #ifndef NDEBUG
314     virtual void print(raw_ostream &O) const;
315 #endif
316   };
317
318   //===--------------------------------------------------------------------===//
319   /// DIEDelta - A simple label difference DIE.
320   ///
321   class DIEDelta : public DIEValue {
322     const MCSymbol *LabelHi;
323     const MCSymbol *LabelLo;
324   public:
325     DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
326       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
327
328     /// EmitValue - Emit delta value.
329     ///
330     virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
331
332     /// SizeOf - Determine size of delta value in bytes.
333     ///
334     virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const;
335
336     // Implement isa/cast/dyncast.
337     static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
338
339 #ifndef NDEBUG
340     virtual void print(raw_ostream &O) const;
341 #endif
342   };
343
344   //===--------------------------------------------------------------------===//
345   /// DIEString - A container for string values.
346   ///
347   class DIEString : public DIEValue {
348     const DIEValue *Access;
349     const StringRef Str;
350
351   public:
352     DIEString(const DIEValue *Acc, const StringRef S)
353         : DIEValue(isString), Access(Acc), Str(S) {}
354
355     /// getString - Grab the string out of the object.
356     StringRef getString() const { return Str; }
357
358     /// EmitValue - Emit delta value.
359     ///
360     virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
361
362     /// SizeOf - Determine size of delta value in bytes.
363     ///
364     virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const;
365
366     // Implement isa/cast/dyncast.
367     static bool classof(const DIEValue *D) { return D->getType() == isString; }
368
369   #ifndef NDEBUG
370     virtual void print(raw_ostream &O) const;
371   #endif
372   };
373
374   //===--------------------------------------------------------------------===//
375   /// DIEEntry - A pointer to another debug information entry.  An instance of
376   /// this class can also be used as a proxy for a debug information entry not
377   /// yet defined (ie. types.)
378   class DIEEntry : public DIEValue {
379     DIE *const Entry;
380   public:
381     explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
382       assert(E && "Cannot construct a DIEEntry with a null DIE");
383     }
384
385     DIE *getEntry() const { return Entry; }
386
387     /// EmitValue - Emit debug information entry offset.
388     ///
389     virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
390
391     /// SizeOf - Determine size of debug information entry in bytes.
392     ///
393     virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const {
394       return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
395                                              : sizeof(int32_t);
396     }
397
398     /// Returns size of a ref_addr entry.
399     static unsigned getRefAddrSize(AsmPrinter *AP);
400
401     // Implement isa/cast/dyncast.
402     static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
403
404 #ifndef NDEBUG
405     virtual void print(raw_ostream &O) const;
406 #endif
407   };
408
409   //===--------------------------------------------------------------------===//
410   /// DIEBlock - A block of values.  Primarily used for location expressions.
411   //
412   class DIEBlock : public DIEValue, public DIE {
413     unsigned Size;                // Size in bytes excluding size header.
414   public:
415     DIEBlock()
416       : DIEValue(isBlock), DIE(0), Size(0) {}
417     virtual ~DIEBlock() {}
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     uint16_t 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, uint16_t Form) const;
435
436     /// SizeOf - Determine size of block data in bytes.
437     ///
438     virtual unsigned SizeOf(AsmPrinter *AP, uint16_t 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