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