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