Constify the DIEs used for pubname and pubtype tables. Propagate
[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     dwarf::Attribute Attribute;
37
38     /// Form - Dwarf form code.
39     ///
40     dwarf::Form Form;
41   public:
42     DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
43
44     // Accessors.
45     dwarf::Attribute getAttribute() const { return Attribute; }
46     dwarf::Form 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     dwarf::Tag 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(dwarf::Tag T, uint16_t C) : Tag(T), ChildrenFlag(C), Data() {}
75
76     // Accessors.
77     dwarf::Tag 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 setChildrenFlag(uint16_t CF) { ChildrenFlag = CF; }
82     void setNumber(unsigned N) { Number = N; }
83
84     /// AddAttribute - Adds another set of attribute information to the
85     /// abbreviation.
86     void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
87       Data.push_back(DIEAbbrevData(Attribute, Form));
88     }
89
90     /// Profile - Used to gather unique data for the abbreviation folding set.
91     ///
92     void Profile(FoldingSetNodeID &ID) const;
93
94     /// Emit - Print the abbreviation using the specified asm printer.
95     ///
96     void Emit(AsmPrinter *AP) const;
97
98 #ifndef NDEBUG
99     void print(raw_ostream &O);
100     void dump();
101 #endif
102   };
103
104   //===--------------------------------------------------------------------===//
105   /// DIE - A structured debug information entry.  Has an abbreviation which
106   /// describes its organization.
107   class DIEValue;
108
109   class DIE {
110   protected:
111     /// Offset - Offset in debug info section.
112     ///
113     unsigned Offset;
114
115     /// Size - Size of instance + children.
116     ///
117     unsigned Size;
118
119     /// Abbrev - Buffer for constructing abbreviation.
120     ///
121     DIEAbbrev Abbrev;
122
123     /// Children DIEs.
124     ///
125     std::vector<DIE *> Children;
126
127     DIE *Parent;
128
129     /// Attribute values.
130     ///
131     SmallVector<DIEValue*, 12> Values;
132
133   public:
134     explicit DIE(unsigned Tag)
135         : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
136           Parent(0) {}
137     ~DIE();
138
139     // Accessors.
140     DIEAbbrev &getAbbrev() { return Abbrev; }
141     const DIEAbbrev &getAbbrev() const { return Abbrev; }
142     unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
143     dwarf::Tag getTag() const { return Abbrev.getTag(); }
144     unsigned getOffset() const { return Offset; }
145     unsigned getSize() const { return Size; }
146     const std::vector<DIE *> &getChildren() const { return Children; }
147     const SmallVectorImpl<DIEValue*> &getValues() const { return Values; }
148     DIE *getParent() const { return Parent; }
149     /// Climb up the parent chain to get the compile or type unit DIE this DIE
150     /// belongs to.
151     const DIE *getUnit() const;
152     /// Similar to getUnit, returns null when DIE is not added to an
153     /// owner yet.
154     const DIE *getUnitOrNull() const;
155     void setOffset(unsigned O) { Offset = O; }
156     void setSize(unsigned S) { Size = S; }
157
158     /// addValue - Add a value and attributes to a DIE.
159     ///
160     void addValue(dwarf::Attribute Attribute, dwarf::Form Form,
161                   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,
176     /// returns NULL if no such attribute exists.
177     DIEValue *findAttribute(uint16_t Attribute) const;
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, dwarf::Form Form) const = 0;
214
215     /// SizeOf - Return the size of a value in bytes.
216     ///
217     virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form 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 dwarf::Form 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, dwarf::Form 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, dwarf::Form 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, dwarf::Form 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, dwarf::Form 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, dwarf::Form 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, dwarf::Form 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, dwarf::Form Form) const;
336
337     /// SizeOf - Determine size of delta value in bytes.
338     ///
339     virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form 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, dwarf::Form Form) const;
366
367     /// SizeOf - Determine size of delta value in bytes.
368     ///
369     virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form 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, dwarf::Form Form) const;
395
396     /// SizeOf - Determine size of debug information entry in bytes.
397     ///
398     virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form 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() : DIEValue(isBlock), DIE(0), Size(0) {}
421
422     /// ComputeSize - calculate the size of the block.
423     ///
424     unsigned ComputeSize(AsmPrinter *AP);
425
426     /// BestForm - Choose the best form for data.
427     ///
428     dwarf::Form BestForm() const {
429       if ((unsigned char)Size == Size)  return dwarf::DW_FORM_block1;
430       if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
431       if ((unsigned int)Size == Size)   return dwarf::DW_FORM_block4;
432       return dwarf::DW_FORM_block;
433     }
434
435     /// EmitValue - Emit block data.
436     ///
437     virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
438
439     /// SizeOf - Determine size of block data in bytes.
440     ///
441     virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
442
443     // Implement isa/cast/dyncast.
444     static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
445
446 #ifndef NDEBUG
447     virtual void print(raw_ostream &O) const;
448 #endif
449   };
450
451 } // end llvm namespace
452
453 #endif