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