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