DIE.h revision db66fb51c47acb2faea916752a90c4baa8f9d0e3
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 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
74    // Accessors.
75    unsigned getTag() const { return Tag; }
76    unsigned getNumber() const { return Number; }
77    unsigned getChildrenFlag() const { return ChildrenFlag; }
78    const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
79    void setTag(unsigned T) { Tag = T; }
80    void setChildrenFlag(unsigned 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(unsigned Attribute, unsigned 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(unsigned Attribute, unsigned 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    /// Abbrev - Buffer for constructing abbreviation.
117    ///
118    DIEAbbrev Abbrev;
119
120    /// Offset - Offset in debug info section.
121    ///
122    unsigned Offset;
123
124    /// Size - Size of instance + children.
125    ///
126    unsigned Size;
127
128    /// Children DIEs.
129    ///
130    std::vector<DIE *> Children;
131
132    DIE *Parent;
133
134    /// Attributes 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      : Abbrev(Tag, dwarf::DW_CHILDREN_no), Offset(0),
143        Size(0), Parent(0), 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    /// SiblingOffset - Return the offset of the debug information entry's
167    /// sibling.
168    unsigned getSiblingOffset() const { return Offset + Size; }
169
170    /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
171    /// The caller is responsible for deleting the return value at or after the
172    /// same time it destroys this DIE.
173    ///
174    DIEValue *addSiblingOffset(BumpPtrAllocator &A);
175
176    /// addChild - Add a child to the DIE.
177    ///
178    void addChild(DIE *Child) {
179      if (Child->getParent()) {
180        assert (Child->getParent() == this && "Unexpected DIE Parent!");
181        return;
182      }
183      Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
184      Children.push_back(Child);
185      Child->Parent = this;
186    }
187
188#ifndef NDEBUG
189    void print(raw_ostream &O, unsigned IncIndent = 0);
190    void dump();
191#endif
192  };
193
194  //===--------------------------------------------------------------------===//
195  /// DIEValue - A debug information entry value.
196  ///
197  class DIEValue {
198  public:
199    enum {
200      isInteger,
201      isString,
202      isLabel,
203      isDelta,
204      isEntry,
205      isBlock
206    };
207  protected:
208    /// Type - Type of data stored in the value.
209    ///
210    unsigned Type;
211  public:
212    explicit DIEValue(unsigned T) : Type(T) {}
213    virtual ~DIEValue() {}
214
215    // Accessors
216    unsigned getType()  const { return Type; }
217
218    /// EmitValue - Emit value via the Dwarf writer.
219    ///
220    virtual void EmitValue(AsmPrinter *AP, unsigned Form) const = 0;
221
222    /// SizeOf - Return the size of a value in bytes.
223    ///
224    virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const = 0;
225
226    // Implement isa/cast/dyncast.
227    static bool classof(const DIEValue *) { return true; }
228
229#ifndef NDEBUG
230    virtual void print(raw_ostream &O) = 0;
231    void dump();
232#endif
233  };
234
235  //===--------------------------------------------------------------------===//
236  /// DIEInteger - An integer value DIE.
237  ///
238  class DIEInteger : public DIEValue {
239    uint64_t Integer;
240  public:
241    explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
242
243    /// BestForm - Choose the best form for integer.
244    ///
245    static unsigned BestForm(bool IsSigned, uint64_t Int) {
246      if (IsSigned) {
247        if ((char)Int == (signed)Int)   return dwarf::DW_FORM_data1;
248        if ((short)Int == (signed)Int)  return dwarf::DW_FORM_data2;
249        if ((int)Int == (signed)Int)    return dwarf::DW_FORM_data4;
250      } else {
251        if ((unsigned char)Int == Int)  return dwarf::DW_FORM_data1;
252        if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
253        if ((unsigned int)Int == Int)   return dwarf::DW_FORM_data4;
254      }
255      return dwarf::DW_FORM_data8;
256    }
257
258    /// EmitValue - Emit integer of appropriate size.
259    ///
260    virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
261
262    uint64_t getValue() const { return Integer; }
263
264    /// SizeOf - Determine size of integer value in bytes.
265    ///
266    virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
267
268    // Implement isa/cast/dyncast.
269    static bool classof(const DIEInteger *) { return true; }
270    static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
271
272#ifndef NDEBUG
273    virtual void print(raw_ostream &O);
274#endif
275  };
276
277  //===--------------------------------------------------------------------===//
278  /// DIEString - A string value DIE. This DIE keeps string reference only.
279  ///
280  class DIEString : public DIEValue {
281    const StringRef Str;
282  public:
283    explicit DIEString(const StringRef S) : DIEValue(isString), Str(S) {}
284
285    /// EmitValue - Emit string value.
286    ///
287    virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
288
289    /// SizeOf - Determine size of string value in bytes.
290    ///
291    virtual unsigned SizeOf(AsmPrinter *AP, unsigned /*Form*/) const {
292      return Str.size() + sizeof(char); // sizeof('\0');
293    }
294
295    // Implement isa/cast/dyncast.
296    static bool classof(const DIEString *) { return true; }
297    static bool classof(const DIEValue *S) { return S->getType() == isString; }
298
299#ifndef NDEBUG
300    virtual void print(raw_ostream &O);
301#endif
302  };
303
304  //===--------------------------------------------------------------------===//
305  /// DIELabel - A label expression DIE.
306  //
307  class DIELabel : public DIEValue {
308    const MCSymbol *Label;
309  public:
310    explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
311
312    /// EmitValue - Emit label value.
313    ///
314    virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
315
316    /// getValue - Get MCSymbol.
317    ///
318    const MCSymbol *getValue()       const { return Label; }
319
320    /// SizeOf - Determine size of label value in bytes.
321    ///
322    virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
323
324    // Implement isa/cast/dyncast.
325    static bool classof(const DIELabel *)  { return true; }
326    static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
327
328#ifndef NDEBUG
329    virtual void print(raw_ostream &O);
330#endif
331  };
332
333  //===--------------------------------------------------------------------===//
334  /// DIEDelta - A simple label difference DIE.
335  ///
336  class DIEDelta : public DIEValue {
337    const MCSymbol *LabelHi;
338    const MCSymbol *LabelLo;
339  public:
340    DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
341      : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
342
343    /// EmitValue - Emit delta value.
344    ///
345    virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
346
347    /// SizeOf - Determine size of delta value in bytes.
348    ///
349    virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
350
351    // Implement isa/cast/dyncast.
352    static bool classof(const DIEDelta *)  { return true; }
353    static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
354
355#ifndef NDEBUG
356    virtual void print(raw_ostream &O);
357#endif
358  };
359
360  //===--------------------------------------------------------------------===//
361  /// DIEEntry - A pointer to another debug information entry.  An instance of
362  /// this class can also be used as a proxy for a debug information entry not
363  /// yet defined (ie. types.)
364  class DIEEntry : public DIEValue {
365    DIE *const Entry;
366  public:
367    explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
368
369    DIE *getEntry() const { return Entry; }
370
371    /// EmitValue - Emit debug information entry offset.
372    ///
373    virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
374
375    /// SizeOf - Determine size of debug information entry in bytes.
376    ///
377    virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const {
378      return sizeof(int32_t);
379    }
380
381    // Implement isa/cast/dyncast.
382    static bool classof(const DIEEntry *)  { return true; }
383    static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
384
385#ifndef NDEBUG
386    virtual void print(raw_ostream &O);
387#endif
388  };
389
390  //===--------------------------------------------------------------------===//
391  /// DIEBlock - A block of values.  Primarily used for location expressions.
392  //
393  class DIEBlock : public DIEValue, public DIE {
394    unsigned Size;                // Size in bytes excluding size header.
395  public:
396    DIEBlock()
397      : DIEValue(isBlock), DIE(0), Size(0) {}
398    virtual ~DIEBlock() {}
399
400    /// ComputeSize - calculate the size of the block.
401    ///
402    unsigned ComputeSize(AsmPrinter *AP);
403
404    /// BestForm - Choose the best form for data.
405    ///
406    unsigned BestForm() const {
407      if ((unsigned char)Size == Size)  return dwarf::DW_FORM_block1;
408      if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
409      if ((unsigned int)Size == Size)   return dwarf::DW_FORM_block4;
410      return dwarf::DW_FORM_block;
411    }
412
413    /// EmitValue - Emit block data.
414    ///
415    virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
416
417    /// SizeOf - Determine size of block data in bytes.
418    ///
419    virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
420
421    // Implement isa/cast/dyncast.
422    static bool classof(const DIEBlock *)  { return true; }
423    static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
424
425#ifndef NDEBUG
426    virtual void print(raw_ostream &O);
427#endif
428  };
429
430} // end llvm namespace
431
432#endif
433