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