DIE.h revision f8bfa88c4fae4773a01204ea2ba508aa3a58392d
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 DwarfPrinter;
26  class TargetData;
27  class MCSymbol;
28  class raw_ostream;
29
30  //===--------------------------------------------------------------------===//
31  /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
32  /// Dwarf abbreviation.
33  class DIEAbbrevData {
34    /// Attribute - Dwarf attribute code.
35    ///
36    unsigned Attribute;
37
38    /// Form - Dwarf form code.
39    ///
40    unsigned Form;
41  public:
42    DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
43
44    // Accessors.
45    unsigned getAttribute() const { return Attribute; }
46    unsigned 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    unsigned Tag;
60
61    /// Unique number for node.
62    ///
63    unsigned Number;
64
65    /// ChildrenFlag - Dwarf children flag.
66    ///
67    unsigned ChildrenFlag;
68
69    /// Data - Raw data bytes for abbreviation.
70    ///
71    SmallVector<DIEAbbrevData, 8> Data;
72
73  public:
74    DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
75    virtual ~DIEAbbrev() {}
76
77    // Accessors.
78    unsigned getTag() const { return Tag; }
79    unsigned getNumber() const { return Number; }
80    unsigned getChildrenFlag() const { return ChildrenFlag; }
81    const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
82    void setTag(unsigned T) { Tag = T; }
83    void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
84    void setNumber(unsigned N) { Number = N; }
85
86    /// AddAttribute - Adds another set of attribute information to the
87    /// abbreviation.
88    void AddAttribute(unsigned Attribute, unsigned Form) {
89      Data.push_back(DIEAbbrevData(Attribute, Form));
90    }
91
92    /// AddFirstAttribute - Adds a set of attribute information to the front
93    /// of the abbreviation.
94    void AddFirstAttribute(unsigned Attribute, unsigned Form) {
95      Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
96    }
97
98    /// Profile - Used to gather unique data for the abbreviation folding set.
99    ///
100    void Profile(FoldingSetNodeID &ID) const;
101
102    /// Emit - Print the abbreviation using the specified asm printer.
103    ///
104    void Emit(const DwarfPrinter *DP) const;
105
106#ifndef NDEBUG
107    void print(raw_ostream &O);
108    void dump();
109#endif
110  };
111
112  //===--------------------------------------------------------------------===//
113  /// DIE - A structured debug information entry.  Has an abbreviation which
114  /// describes it's organization.
115  class CompileUnit;
116  class DIEValue;
117
118  class DIE {
119  protected:
120    /// Abbrev - Buffer for constructing abbreviation.
121    ///
122    DIEAbbrev Abbrev;
123
124    /// Offset - Offset in debug info section.
125    ///
126    unsigned Offset;
127
128    /// Size - Size of instance + children.
129    ///
130    unsigned Size;
131
132    /// Children DIEs.
133    ///
134    std::vector<DIE *> Children;
135
136    DIE *Parent;
137
138    /// Attributes values.
139    ///
140    SmallVector<DIEValue*, 32> Values;
141
142    // Private data for print()
143    mutable unsigned IndentCount;
144  public:
145    explicit DIE(unsigned Tag)
146      : Abbrev(Tag, dwarf::DW_CHILDREN_no), Offset(0),
147        Size(0), Parent (0), IndentCount(0) {}
148    virtual ~DIE();
149
150    // Accessors.
151    DIEAbbrev &getAbbrev() { return Abbrev; }
152    unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
153    unsigned getTag() const { return Abbrev.getTag(); }
154    unsigned getOffset() const { return Offset; }
155    unsigned getSize() const { return Size; }
156    const std::vector<DIE *> &getChildren() const { return Children; }
157    SmallVector<DIEValue*, 32> &getValues() { return Values; }
158    DIE *getParent() const { return Parent; }
159    void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
160    void setOffset(unsigned O) { Offset = O; }
161    void setSize(unsigned S) { Size = S; }
162    void setParent(DIE *P) { Parent = P; }
163
164    /// addValue - Add a value and attributes to a DIE.
165    ///
166    void addValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
167      Abbrev.AddAttribute(Attribute, Form);
168      Values.push_back(Value);
169    }
170
171    /// SiblingOffset - Return the offset of the debug information entry's
172    /// sibling.
173    unsigned getSiblingOffset() const { return Offset + Size; }
174
175    /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
176    ///
177    void addSiblingOffset();
178
179    /// addChild - Add a child to the DIE.
180    ///
181    void addChild(DIE *Child) {
182      if (Child->getParent()) {
183        assert (Child->getParent() == this && "Unexpected DIE Parent!");
184        return;
185      }
186      Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
187      Children.push_back(Child);
188      Child->setParent(this);
189    }
190
191#ifndef NDEBUG
192    void print(raw_ostream &O, unsigned IncIndent = 0);
193    void dump();
194#endif
195  };
196
197  //===--------------------------------------------------------------------===//
198  /// DIEValue - A debug information entry value.
199  ///
200  class DIEValue {
201  public:
202    enum {
203      isInteger,
204      isString,
205      isLabel,
206      isSectionOffset,
207      isDelta,
208      isEntry,
209      isBlock
210    };
211  protected:
212    /// Type - Type of data stored in the value.
213    ///
214    unsigned Type;
215  public:
216    explicit DIEValue(unsigned T) : Type(T) {}
217    virtual ~DIEValue() {}
218
219    // Accessors
220    unsigned getType()  const { return Type; }
221
222    /// EmitValue - Emit value via the Dwarf writer.
223    ///
224    virtual void EmitValue(DwarfPrinter *D, unsigned Form) const = 0;
225
226    /// SizeOf - Return the size of a value in bytes.
227    ///
228    virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const = 0;
229
230    // Implement isa/cast/dyncast.
231    static bool classof(const DIEValue *) { return true; }
232
233#ifndef NDEBUG
234    virtual void print(raw_ostream &O) = 0;
235    void dump();
236#endif
237  };
238
239  //===--------------------------------------------------------------------===//
240  /// DIEInteger - An integer value DIE.
241  ///
242  class DIEInteger : public DIEValue {
243    uint64_t Integer;
244  public:
245    explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
246
247    /// BestForm - Choose the best form for integer.
248    ///
249    static unsigned BestForm(bool IsSigned, uint64_t Int) {
250      if (IsSigned) {
251        if ((char)Int == (signed)Int)   return dwarf::DW_FORM_data1;
252        if ((short)Int == (signed)Int)  return dwarf::DW_FORM_data2;
253        if ((int)Int == (signed)Int)    return dwarf::DW_FORM_data4;
254      } else {
255        if ((unsigned char)Int == Int)  return dwarf::DW_FORM_data1;
256        if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
257        if ((unsigned int)Int == Int)   return dwarf::DW_FORM_data4;
258      }
259      return dwarf::DW_FORM_data8;
260    }
261
262    /// EmitValue - Emit integer of appropriate size.
263    ///
264    virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
265
266    /// SizeOf - Determine size of integer value in bytes.
267    ///
268    virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
269
270
271    // Implement isa/cast/dyncast.
272    static bool classof(const DIEInteger *) { return true; }
273    static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
274
275#ifndef NDEBUG
276    virtual void print(raw_ostream &O);
277#endif
278  };
279
280  //===--------------------------------------------------------------------===//
281  /// DIEString - A string value DIE. This DIE keeps string reference only.
282  ///
283  class DIEString : public DIEValue {
284    const StringRef Str;
285  public:
286    explicit DIEString(const StringRef S) : DIEValue(isString), Str(S) {}
287
288    /// EmitValue - Emit string value.
289    ///
290    virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
291
292    /// SizeOf - Determine size of string value in bytes.
293    ///
294    virtual unsigned SizeOf(const TargetData *, unsigned /*Form*/) const {
295      return Str.size() + sizeof(char); // sizeof('\0');
296    }
297
298    // Implement isa/cast/dyncast.
299    static bool classof(const DIEString *) { return true; }
300    static bool classof(const DIEValue *S) { return S->getType() == isString; }
301
302#ifndef NDEBUG
303    virtual void print(raw_ostream &O);
304#endif
305  };
306
307  //===--------------------------------------------------------------------===//
308  /// DIELabel - A label expression DIE.
309  //
310  class DIELabel : public DIEValue {
311    const MCSymbol *Label;
312  public:
313    explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
314
315    /// EmitValue - Emit label value.
316    ///
317    virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
318
319    /// SizeOf - Determine size of label value in bytes.
320    ///
321    virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
322
323    // Implement isa/cast/dyncast.
324    static bool classof(const DIELabel *)  { return true; }
325    static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
326
327#ifndef NDEBUG
328    virtual void print(raw_ostream &O);
329#endif
330  };
331
332  //===--------------------------------------------------------------------===//
333  /// DIESectionOffset - A section offset DIE.
334  ///
335  class DIESectionOffset : public DIEValue {
336    const MCSymbol *Label;
337    const MCSymbol *Section;
338    bool IsEH : 1;
339  public:
340    DIESectionOffset(const MCSymbol *Lab, const MCSymbol *Sec,
341                     bool isEH = false)
342      : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
343        IsEH(isEH) {}
344
345    /// EmitValue - Emit section offset.
346    ///
347    virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
348
349    /// SizeOf - Determine size of section offset value in bytes.
350    ///
351    virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
352
353    // Implement isa/cast/dyncast.
354    static bool classof(const DIESectionOffset *)  { return true; }
355    static bool classof(const DIEValue *D) {
356      return D->getType() == isSectionOffset;
357    }
358
359#ifndef NDEBUG
360    virtual void print(raw_ostream &O);
361#endif
362  };
363
364  //===--------------------------------------------------------------------===//
365  /// DIEDelta - A simple label difference DIE.
366  ///
367  class DIEDelta : public DIEValue {
368    const MCSymbol *LabelHi;
369    const MCSymbol *LabelLo;
370  public:
371    DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
372      : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
373
374    /// EmitValue - Emit delta value.
375    ///
376    virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
377
378    /// SizeOf - Determine size of delta value in bytes.
379    ///
380    virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
381
382    // Implement isa/cast/dyncast.
383    static bool classof(const DIEDelta *)  { return true; }
384    static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
385
386#ifndef NDEBUG
387    virtual void print(raw_ostream &O);
388#endif
389  };
390
391  //===--------------------------------------------------------------------===//
392  /// DIEntry - A pointer to another debug information entry.  An instance of
393  /// this class can also be used as a proxy for a debug information entry not
394  /// yet defined (ie. types.)
395  class DIEEntry : public DIEValue {
396    DIE *Entry;
397  public:
398    explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
399
400    DIE *getEntry() const { return Entry; }
401    void setEntry(DIE *E) { Entry = E; }
402
403    /// EmitValue - Emit debug information entry offset.
404    ///
405    virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
406
407    /// SizeOf - Determine size of debug information entry in bytes.
408    ///
409    virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const {
410      return sizeof(int32_t);
411    }
412
413    // Implement isa/cast/dyncast.
414    static bool classof(const DIEEntry *)  { return true; }
415    static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
416
417#ifndef NDEBUG
418    virtual void print(raw_ostream &O);
419#endif
420  };
421
422  //===--------------------------------------------------------------------===//
423  /// DIEBlock - A block of values.  Primarily used for location expressions.
424  //
425  class DIEBlock : public DIEValue, public DIE {
426    unsigned Size;                // Size in bytes excluding size header.
427  public:
428    DIEBlock()
429      : DIEValue(isBlock), DIE(0), Size(0) {}
430    virtual ~DIEBlock() {}
431
432    /// ComputeSize - calculate the size of the block.
433    ///
434    unsigned ComputeSize(const TargetData *TD);
435
436    /// BestForm - Choose the best form for data.
437    ///
438    unsigned BestForm() const {
439      if ((unsigned char)Size == Size)  return dwarf::DW_FORM_block1;
440      if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
441      if ((unsigned int)Size == Size)   return dwarf::DW_FORM_block4;
442      return dwarf::DW_FORM_block;
443    }
444
445    /// EmitValue - Emit block data.
446    ///
447    virtual void EmitValue(DwarfPrinter *D, unsigned Form) const;
448
449    /// SizeOf - Determine size of block data in bytes.
450    ///
451    virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
452
453    // Implement isa/cast/dyncast.
454    static bool classof(const DIEBlock *)  { return true; }
455    static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
456
457#ifndef NDEBUG
458    virtual void print(raw_ostream &O);
459#endif
460  };
461
462} // end llvm namespace
463
464#endif
465