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