DIE.h revision e7099f6ff48ee99e50aa8347b455255e3a893a12
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
24namespace 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 setTag(uint16_t T) { Tag = T; }
82    void setChildrenFlag(uint16_t CF) { ChildrenFlag = CF; }
83    void setNumber(unsigned N) { Number = N; }
84
85    /// AddAttribute - Adds another set of attribute information to the
86    /// abbreviation.
87    void AddAttribute(uint16_t Attribute, uint16_t Form) {
88      Data.push_back(DIEAbbrevData(Attribute, Form));
89    }
90
91    /// Profile - Used to gather unique data for the abbreviation folding set.
92    ///
93    void Profile(FoldingSetNodeID &ID) const;
94
95    /// Emit - Print the abbreviation using the specified asm printer.
96    ///
97    void Emit(AsmPrinter *AP) const;
98
99#ifndef NDEBUG
100    void print(raw_ostream &O);
101    void dump();
102#endif
103  };
104
105  //===--------------------------------------------------------------------===//
106  /// DIE - A structured debug information entry.  Has an abbreviation which
107  /// describes its organization.
108  class DIEValue;
109
110  class DIE {
111  protected:
112    /// Offset - Offset in debug info section.
113    ///
114    unsigned Offset;
115
116    /// Size - Size of instance + children.
117    ///
118    unsigned Size;
119
120    /// Abbrev - Buffer for constructing abbreviation.
121    ///
122    DIEAbbrev Abbrev;
123
124    /// Children DIEs.
125    ///
126    std::vector<DIE *> Children;
127
128    DIE *Parent;
129
130    /// Attribute values.
131    ///
132    SmallVector<DIEValue*, 12> Values;
133
134#ifndef NDEBUG
135    // Private data for print()
136    mutable unsigned IndentCount;
137#endif
138  public:
139    explicit DIE(unsigned Tag)
140      : Offset(0), Size(0), Abbrev(Tag, dwarf::DW_CHILDREN_no), Parent(0) {}
141    virtual ~DIE();
142
143    // Accessors.
144    DIEAbbrev &getAbbrev() { return Abbrev; }
145    unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
146    uint16_t getTag() const { return Abbrev.getTag(); }
147    unsigned getOffset() const { return Offset; }
148    unsigned getSize() const { return Size; }
149    const std::vector<DIE *> &getChildren() const { return Children; }
150    const SmallVectorImpl<DIEValue*> &getValues() const { return Values; }
151    DIE *getParent() const { return Parent; }
152    void setTag(uint16_t Tag) { Abbrev.setTag(Tag); }
153    void setOffset(unsigned O) { Offset = O; }
154    void setSize(unsigned S) { Size = S; }
155
156    /// addValue - Add a value and attributes to a DIE.
157    ///
158    void addValue(uint16_t Attribute, uint16_t Form, DIEValue *Value) {
159      Abbrev.AddAttribute(Attribute, Form);
160      Values.push_back(Value);
161    }
162
163    /// addChild - Add a child to the DIE.
164    ///
165    void addChild(DIE *Child) {
166      assert(!Child->getParent());
167      Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
168      Children.push_back(Child);
169      Child->Parent = this;
170    }
171
172    /// findAttribute - Find a value in the DIE with the attribute given, returns NULL
173    /// if no such attribute exists.
174    DIEValue *findAttribute(uint16_t Attribute);
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      isExpr,
192      isLabel,
193      isDelta,
194      isEntry,
195      isBlock
196    };
197  protected:
198    /// Type - Type of data stored in the value.
199    ///
200    unsigned Type;
201  public:
202    explicit DIEValue(unsigned T) : Type(T) {}
203    virtual ~DIEValue() {}
204
205    // Accessors
206    unsigned getType()  const { return Type; }
207
208    /// EmitValue - Emit value via the Dwarf writer.
209    ///
210    virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const = 0;
211
212    /// SizeOf - Return the size of a value in bytes.
213    ///
214    virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const = 0;
215
216#ifndef NDEBUG
217    virtual void print(raw_ostream &O) const = 0;
218    void dump() const;
219#endif
220  };
221
222  //===--------------------------------------------------------------------===//
223  /// DIEInteger - An integer value DIE.
224  ///
225  class DIEInteger : public DIEValue {
226    uint64_t Integer;
227  public:
228    explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
229
230    /// BestForm - Choose the best form for integer.
231    ///
232    static uint16_t BestForm(bool IsSigned, uint64_t Int) {
233      if (IsSigned) {
234        const int64_t SignedInt = Int;
235        if ((char)Int == SignedInt)     return dwarf::DW_FORM_data1;
236        if ((short)Int == SignedInt)    return dwarf::DW_FORM_data2;
237        if ((int)Int == SignedInt)      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, uint16_t 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, uint16_t 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) const;
261#endif
262  };
263
264  //===--------------------------------------------------------------------===//
265  /// DIEExpr - An expression DIE.
266  //
267  class DIEExpr : public DIEValue {
268    const MCExpr *Expr;
269  public:
270    explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
271
272    /// EmitValue - Emit expression value.
273    ///
274    virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
275
276    /// getValue - Get MCExpr.
277    ///
278    const MCExpr *getValue() const { return Expr; }
279
280    /// SizeOf - Determine size of expression value in bytes.
281    ///
282    virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const;
283
284    // Implement isa/cast/dyncast.
285    static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
286
287#ifndef NDEBUG
288    virtual void print(raw_ostream &O) const;
289#endif
290  };
291
292  //===--------------------------------------------------------------------===//
293  /// DIELabel - A label DIE.
294  //
295  class DIELabel : public DIEValue {
296    const MCSymbol *Label;
297  public:
298    explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
299
300    /// EmitValue - Emit label value.
301    ///
302    virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
303
304    /// getValue - Get MCSymbol.
305    ///
306    const MCSymbol *getValue() const { return Label; }
307
308    /// SizeOf - Determine size of label value in bytes.
309    ///
310    virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const;
311
312    // Implement isa/cast/dyncast.
313    static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
314
315#ifndef NDEBUG
316    virtual void print(raw_ostream &O) const;
317#endif
318  };
319
320  //===--------------------------------------------------------------------===//
321  /// DIEDelta - A simple label difference DIE.
322  ///
323  class DIEDelta : public DIEValue {
324    const MCSymbol *LabelHi;
325    const MCSymbol *LabelLo;
326  public:
327    DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
328      : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
329
330    /// EmitValue - Emit delta value.
331    ///
332    virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
333
334    /// SizeOf - Determine size of delta value in bytes.
335    ///
336    virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const;
337
338    // Implement isa/cast/dyncast.
339    static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
340
341#ifndef NDEBUG
342    virtual void print(raw_ostream &O) const;
343#endif
344  };
345
346  //===--------------------------------------------------------------------===//
347  /// DIEString - A container for string values.
348  ///
349  class DIEString : public DIEValue {
350    const DIEValue *Access;
351    const StringRef Str;
352
353  public:
354    DIEString(const DIEValue *Acc, const StringRef S)
355        : DIEValue(isString), Access(Acc), Str(S) {}
356
357    /// getString - Grab the string out of the object.
358    StringRef getString() const { return Str; }
359
360    /// EmitValue - Emit delta value.
361    ///
362    virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
363
364    /// SizeOf - Determine size of delta value in bytes.
365    ///
366    virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const;
367
368    // Implement isa/cast/dyncast.
369    static bool classof(const DIEValue *D) { return D->getType() == isString; }
370
371  #ifndef NDEBUG
372    virtual void print(raw_ostream &O) const;
373  #endif
374  };
375
376  //===--------------------------------------------------------------------===//
377  /// DIEEntry - A pointer to another debug information entry.  An instance of
378  /// this class can also be used as a proxy for a debug information entry not
379  /// yet defined (ie. types.)
380  class DIEEntry : public DIEValue {
381    DIE *const Entry;
382  public:
383    explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
384      assert(E && "Cannot construct a DIEEntry with a null DIE");
385    }
386
387    DIE *getEntry() const { return Entry; }
388
389    /// EmitValue - Emit debug information entry offset.
390    ///
391    virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
392
393    /// SizeOf - Determine size of debug information entry in bytes.
394    ///
395    virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const {
396      return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
397                                             : sizeof(int32_t);
398    }
399
400    /// Returns size of a ref_addr entry.
401    static unsigned getRefAddrSize(AsmPrinter *AP);
402
403    // Implement isa/cast/dyncast.
404    static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
405
406#ifndef NDEBUG
407    virtual void print(raw_ostream &O) const;
408#endif
409  };
410
411  //===--------------------------------------------------------------------===//
412  /// DIEBlock - A block of values.  Primarily used for location expressions.
413  //
414  class DIEBlock : public DIEValue, public DIE {
415    unsigned Size;                // Size in bytes excluding size header.
416  public:
417    DIEBlock()
418      : DIEValue(isBlock), DIE(0), Size(0) {}
419    virtual ~DIEBlock() {}
420
421    /// ComputeSize - calculate the size of the block.
422    ///
423    unsigned ComputeSize(AsmPrinter *AP);
424
425    /// BestForm - Choose the best form for data.
426    ///
427    uint16_t BestForm() const {
428      if ((unsigned char)Size == Size)  return dwarf::DW_FORM_block1;
429      if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
430      if ((unsigned int)Size == Size)   return dwarf::DW_FORM_block4;
431      return dwarf::DW_FORM_block;
432    }
433
434    /// EmitValue - Emit block data.
435    ///
436    virtual void EmitValue(AsmPrinter *AP, uint16_t Form) const;
437
438    /// SizeOf - Determine size of block data in bytes.
439    ///
440    virtual unsigned SizeOf(AsmPrinter *AP, uint16_t Form) const;
441
442    // Implement isa/cast/dyncast.
443    static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
444
445#ifndef NDEBUG
446    virtual void print(raw_ostream &O) const;
447#endif
448  };
449
450} // end llvm namespace
451
452#endif
453