Make the DIEValue constructor even more explicit.
[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/MC/MCExpr.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Dwarf.h"
22 #include <vector>
23
24 namespace llvm {
25 class AsmPrinter;
26 class MCSymbol;
27 class MCSymbolRefExpr;
28 class raw_ostream;
29 class DwarfTypeUnit;
30
31 //===--------------------------------------------------------------------===//
32 /// DIEAbbrevData - Dwarf abbreviation data, describes one attribute of a
33 /// Dwarf abbreviation.
34 class DIEAbbrevData {
35   /// Attribute - Dwarf attribute code.
36   ///
37   dwarf::Attribute Attribute;
38
39   /// Form - Dwarf form code.
40   ///
41   dwarf::Form Form;
42
43 public:
44   DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
45
46   // Accessors.
47   dwarf::Attribute getAttribute() const { return Attribute; }
48   dwarf::Form getForm() const { return Form; }
49
50   /// Profile - Used to gather unique data for the abbreviation folding set.
51   ///
52   void Profile(FoldingSetNodeID &ID) const;
53 };
54
55 //===--------------------------------------------------------------------===//
56 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
57 /// information object.
58 class DIEAbbrev : public FoldingSetNode {
59   /// Unique number for node.
60   ///
61   unsigned Number;
62
63   /// Tag - Dwarf tag code.
64   ///
65   dwarf::Tag Tag;
66
67   /// Children - Whether or not this node has children.
68   ///
69   // This cheats a bit in all of the uses since the values in the standard
70   // are 0 and 1 for no children and children respectively.
71   bool Children;
72
73   /// Data - Raw data bytes for abbreviation.
74   ///
75   SmallVector<DIEAbbrevData, 12> Data;
76
77 public:
78   DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C), Data() {}
79
80   // Accessors.
81   dwarf::Tag getTag() const { return Tag; }
82   unsigned getNumber() const { return Number; }
83   bool hasChildren() const { return Children; }
84   const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
85   void setChildrenFlag(bool hasChild) { Children = hasChild; }
86   void setNumber(unsigned N) { Number = N; }
87
88   /// AddAttribute - Adds another set of attribute information to the
89   /// abbreviation.
90   void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
91     Data.push_back(DIEAbbrevData(Attribute, Form));
92   }
93
94   /// Profile - Used to gather unique data for the abbreviation folding set.
95   ///
96   void Profile(FoldingSetNodeID &ID) const;
97
98   /// Emit - Print the abbreviation using the specified asm printer.
99   ///
100   void Emit(AsmPrinter *AP) const;
101
102 #ifndef NDEBUG
103   void print(raw_ostream &O);
104   void dump();
105 #endif
106 };
107
108 //===--------------------------------------------------------------------===//
109 /// DIE - A structured debug information entry.  Has an abbreviation which
110 /// describes its organization.
111 class DIEValue;
112
113 class DIE {
114 protected:
115   /// Offset - Offset in debug info section.
116   ///
117   unsigned Offset;
118
119   /// Size - Size of instance + children.
120   ///
121   unsigned Size;
122
123   /// Abbrev - Buffer for constructing abbreviation.
124   ///
125   DIEAbbrev Abbrev;
126
127   /// Children DIEs.
128   ///
129   std::vector<DIE *> Children;
130
131   DIE *Parent;
132
133   /// Attribute values.
134   ///
135   SmallVector<DIEValue *, 12> Values;
136
137 public:
138   explicit DIE(unsigned Tag)
139       : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
140         Parent(0) {}
141   ~DIE();
142
143   // Accessors.
144   DIEAbbrev &getAbbrev() { return Abbrev; }
145   const DIEAbbrev &getAbbrev() const { return Abbrev; }
146   unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
147   dwarf::Tag getTag() const { return Abbrev.getTag(); }
148   unsigned getOffset() const { return Offset; }
149   unsigned getSize() const { return Size; }
150   const std::vector<DIE *> &getChildren() const { return Children; }
151   const SmallVectorImpl<DIEValue *> &getValues() const { return Values; }
152   DIE *getParent() const { return Parent; }
153   /// Climb up the parent chain to get the compile or type unit DIE this DIE
154   /// belongs to.
155   const DIE *getUnit() const;
156   /// Similar to getUnit, returns null when DIE is not added to an
157   /// owner yet.
158   const DIE *getUnitOrNull() const;
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(dwarf::Attribute Attribute, dwarf::Form 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,
179   /// returns NULL if no such attribute exists.
180   DIEValue *findAttribute(dwarf::Attribute Attribute) const;
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. Some of these roughly correlate
190 /// to DWARF attribute classes.
191 ///
192 class DIEValue {
193   virtual void anchor();
194
195 public:
196   enum Type {
197     isInteger,
198     isString,
199     isExpr,
200     isLabel,
201     isDelta,
202     isEntry,
203     isTypeSignature,
204     isBlock,
205     isLoc
206   };
207
208 protected:
209   /// Ty - Type of data stored in the value.
210   ///
211   Type Ty;
212
213   explicit DIEValue(Type T) : Ty(T) {}
214   virtual ~DIEValue() {}
215
216 public:
217   // Accessors
218   Type getType() const { return Ty; }
219
220   /// EmitValue - Emit value via the Dwarf writer.
221   ///
222   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
223
224   /// SizeOf - Return the size of a value in bytes.
225   ///
226   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
227
228 #ifndef NDEBUG
229   virtual void print(raw_ostream &O) const = 0;
230   void dump() const;
231 #endif
232 };
233
234 //===--------------------------------------------------------------------===//
235 /// DIEInteger - An integer value DIE.
236 ///
237 class DIEInteger : public DIEValue {
238   uint64_t Integer;
239
240 public:
241   explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
242
243   /// BestForm - Choose the best form for integer.
244   ///
245   static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
246     if (IsSigned) {
247       const int64_t SignedInt = Int;
248       if ((char)Int == SignedInt)
249         return dwarf::DW_FORM_data1;
250       if ((short)Int == SignedInt)
251         return dwarf::DW_FORM_data2;
252       if ((int)Int == SignedInt)
253         return dwarf::DW_FORM_data4;
254     } else {
255       if ((unsigned char)Int == Int)
256         return dwarf::DW_FORM_data1;
257       if ((unsigned short)Int == Int)
258         return dwarf::DW_FORM_data2;
259       if ((unsigned int)Int == Int)
260         return dwarf::DW_FORM_data4;
261     }
262     return dwarf::DW_FORM_data8;
263   }
264
265   /// EmitValue - Emit integer of appropriate size.
266   ///
267   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
268
269   uint64_t getValue() const { return Integer; }
270
271   /// SizeOf - Determine size of integer value in bytes.
272   ///
273   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
274
275   // Implement isa/cast/dyncast.
276   static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
277
278 #ifndef NDEBUG
279   virtual void print(raw_ostream &O) const;
280 #endif
281 };
282
283 //===--------------------------------------------------------------------===//
284 /// DIEExpr - An expression DIE.
285 //
286 class DIEExpr : public DIEValue {
287   const MCExpr *Expr;
288
289 public:
290   explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
291
292   /// EmitValue - Emit expression value.
293   ///
294   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
295
296   /// getValue - Get MCExpr.
297   ///
298   const MCExpr *getValue() const { return Expr; }
299
300   /// SizeOf - Determine size of expression value in bytes.
301   ///
302   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
303
304   // Implement isa/cast/dyncast.
305   static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
306
307 #ifndef NDEBUG
308   virtual void print(raw_ostream &O) const;
309 #endif
310 };
311
312 //===--------------------------------------------------------------------===//
313 /// DIELabel - A label DIE.
314 //
315 class DIELabel : public DIEValue {
316   const MCSymbol *Label;
317
318 public:
319   explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
320
321   /// EmitValue - Emit label value.
322   ///
323   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
324
325   /// getValue - Get MCSymbol.
326   ///
327   const MCSymbol *getValue() const { return Label; }
328
329   /// SizeOf - Determine size of label value in bytes.
330   ///
331   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
332
333   // Implement isa/cast/dyncast.
334   static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
335
336 #ifndef NDEBUG
337   virtual void print(raw_ostream &O) const;
338 #endif
339 };
340
341 //===--------------------------------------------------------------------===//
342 /// DIEDelta - A simple label difference DIE.
343 ///
344 class DIEDelta : public DIEValue {
345   const MCSymbol *LabelHi;
346   const MCSymbol *LabelLo;
347
348 public:
349   DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
350       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
351
352   /// EmitValue - Emit delta value.
353   ///
354   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
355
356   /// SizeOf - Determine size of delta value in bytes.
357   ///
358   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
359
360   // Implement isa/cast/dyncast.
361   static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
362
363 #ifndef NDEBUG
364   virtual void print(raw_ostream &O) const;
365 #endif
366 };
367
368 //===--------------------------------------------------------------------===//
369 /// DIEString - A container for string values.
370 ///
371 class DIEString : public DIEValue {
372   const DIEValue *Access;
373   const StringRef Str;
374
375 public:
376   DIEString(const DIEValue *Acc, const StringRef S)
377       : DIEValue(isString), Access(Acc), Str(S) {}
378
379   /// getString - Grab the string out of the object.
380   StringRef getString() const { return Str; }
381
382   /// EmitValue - Emit delta value.
383   ///
384   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
385
386   /// SizeOf - Determine size of delta value in bytes.
387   ///
388   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
389
390   // Implement isa/cast/dyncast.
391   static bool classof(const DIEValue *D) { return D->getType() == isString; }
392
393 #ifndef NDEBUG
394   virtual void print(raw_ostream &O) const;
395 #endif
396 };
397
398 //===--------------------------------------------------------------------===//
399 /// DIEEntry - A pointer to another debug information entry.  An instance of
400 /// this class can also be used as a proxy for a debug information entry not
401 /// yet defined (ie. types.)
402 class DIEEntry : public DIEValue {
403   DIE *const Entry;
404
405 public:
406   explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
407     assert(E && "Cannot construct a DIEEntry with a null DIE");
408   }
409
410   DIE *getEntry() const { return Entry; }
411
412   /// EmitValue - Emit debug information entry offset.
413   ///
414   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
415
416   /// SizeOf - Determine size of debug information entry in bytes.
417   ///
418   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
419     return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
420                                            : sizeof(int32_t);
421   }
422
423   /// Returns size of a ref_addr entry.
424   static unsigned getRefAddrSize(AsmPrinter *AP);
425
426   // Implement isa/cast/dyncast.
427   static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
428
429 #ifndef NDEBUG
430   virtual void print(raw_ostream &O) const;
431 #endif
432 };
433
434 //===--------------------------------------------------------------------===//
435 /// \brief A signature reference to a type unit.
436 class DIETypeSignature : public DIEValue {
437   const DwarfTypeUnit &Unit;
438
439 public:
440   explicit DIETypeSignature(const DwarfTypeUnit &Unit)
441       : DIEValue(isTypeSignature), Unit(Unit) {}
442
443   /// \brief Emit type unit signature.
444   virtual void EmitValue(AsmPrinter *Asm, dwarf::Form Form) const;
445
446   /// Returns size of a ref_sig8 entry.
447   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
448     assert(Form == dwarf::DW_FORM_ref_sig8);
449     return 8;
450   }
451
452   // \brief Implement isa/cast/dyncast.
453   static bool classof(const DIEValue *E) {
454     return E->getType() == isTypeSignature;
455   }
456 #ifndef NDEBUG
457   virtual void print(raw_ostream &O) const;
458   void dump() const;
459 #endif
460 };
461
462 //===--------------------------------------------------------------------===//
463 /// DIELoc - Represents an expression location.
464 //
465 class DIELoc : public DIEValue, public DIE {
466   mutable unsigned Size; // Size in bytes excluding size header.
467 public:
468   DIELoc() : DIEValue(isLoc), DIE(0), Size(0) {}
469
470   /// ComputeSize - Calculate the size of the location expression.
471   ///
472   unsigned ComputeSize(AsmPrinter *AP) const;
473
474   /// BestForm - Choose the best form for data.
475   ///
476   dwarf::Form BestForm(unsigned DwarfVersion) const {
477     if (DwarfVersion > 3)
478       return dwarf::DW_FORM_exprloc;
479     // Pre-DWARF4 location expressions were blocks and not exprloc.
480     if ((unsigned char)Size == Size)
481       return dwarf::DW_FORM_block1;
482     if ((unsigned short)Size == Size)
483       return dwarf::DW_FORM_block2;
484     if ((unsigned int)Size == Size)
485       return dwarf::DW_FORM_block4;
486     return dwarf::DW_FORM_block;
487   }
488
489   /// EmitValue - Emit location data.
490   ///
491   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
492
493   /// SizeOf - Determine size of location data in bytes.
494   ///
495   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
496
497   // Implement isa/cast/dyncast.
498   static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
499
500 #ifndef NDEBUG
501   virtual void print(raw_ostream &O) const;
502 #endif
503 };
504
505 //===--------------------------------------------------------------------===//
506 /// DIEBlock - Represents a block of values.
507 //
508 class DIEBlock : public DIEValue, public DIE {
509   mutable unsigned Size; // Size in bytes excluding size header.
510 public:
511   DIEBlock() : DIEValue(isBlock), DIE(0), Size(0) {}
512
513   /// ComputeSize - Calculate the size of the location expression.
514   ///
515   unsigned ComputeSize(AsmPrinter *AP) const;
516
517   /// BestForm - Choose the best form for data.
518   ///
519   dwarf::Form BestForm() const {
520     if ((unsigned char)Size == Size)
521       return dwarf::DW_FORM_block1;
522     if ((unsigned short)Size == Size)
523       return dwarf::DW_FORM_block2;
524     if ((unsigned int)Size == Size)
525       return dwarf::DW_FORM_block4;
526     return dwarf::DW_FORM_block;
527   }
528
529   /// EmitValue - Emit location data.
530   ///
531   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
532
533   /// SizeOf - Determine size of location data in bytes.
534   ///
535   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
536
537   // Implement isa/cast/dyncast.
538   static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
539
540 #ifndef NDEBUG
541   virtual void print(raw_ostream &O) const;
542 #endif
543 };
544 } // end llvm namespace
545
546 #endif