Expand slightly on comment.
[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. Some of these roughly correlate
188 /// to DWARF attribute classes.
189 ///
190 class DIEValue {
191   virtual void anchor();
192
193 public:
194   enum {
195     isInteger,
196     isString,
197     isExpr,
198     isLabel,
199     isDelta,
200     isEntry,
201     isTypeSignature,
202     isBlock,
203     isLoc
204   };
205
206 protected:
207   /// Type - Type of data stored in the value.
208   ///
209   unsigned Type;
210
211 public:
212   explicit DIEValue(unsigned T) : Type(T) {}
213   virtual ~DIEValue() {}
214
215   // Accessors
216   unsigned getType() const { return Type; }
217
218   /// EmitValue - Emit value via the Dwarf writer.
219   ///
220   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
221
222   /// SizeOf - Return the size of a value in bytes.
223   ///
224   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
225
226 #ifndef NDEBUG
227   virtual void print(raw_ostream &O) const = 0;
228   void dump() const;
229 #endif
230 };
231
232 //===--------------------------------------------------------------------===//
233 /// DIEInteger - An integer value DIE.
234 ///
235 class DIEInteger : public DIEValue {
236   uint64_t Integer;
237
238 public:
239   explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
240
241   /// BestForm - Choose the best form for integer.
242   ///
243   static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
244     if (IsSigned) {
245       const int64_t SignedInt = Int;
246       if ((char)Int == SignedInt)
247         return dwarf::DW_FORM_data1;
248       if ((short)Int == SignedInt)
249         return dwarf::DW_FORM_data2;
250       if ((int)Int == SignedInt)
251         return dwarf::DW_FORM_data4;
252     } else {
253       if ((unsigned char)Int == Int)
254         return dwarf::DW_FORM_data1;
255       if ((unsigned short)Int == Int)
256         return dwarf::DW_FORM_data2;
257       if ((unsigned int)Int == Int)
258         return dwarf::DW_FORM_data4;
259     }
260     return dwarf::DW_FORM_data8;
261   }
262
263   /// EmitValue - Emit integer of appropriate size.
264   ///
265   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
266
267   uint64_t getValue() const { return Integer; }
268
269   /// SizeOf - Determine size of integer value in bytes.
270   ///
271   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
272
273   // Implement isa/cast/dyncast.
274   static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
275
276 #ifndef NDEBUG
277   virtual void print(raw_ostream &O) const;
278 #endif
279 };
280
281 //===--------------------------------------------------------------------===//
282 /// DIEExpr - An expression DIE.
283 //
284 class DIEExpr : public DIEValue {
285   const MCExpr *Expr;
286
287 public:
288   explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
289
290   /// EmitValue - Emit expression value.
291   ///
292   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
293
294   /// getValue - Get MCExpr.
295   ///
296   const MCExpr *getValue() const { return Expr; }
297
298   /// SizeOf - Determine size of expression value in bytes.
299   ///
300   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
301
302   // Implement isa/cast/dyncast.
303   static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
304
305 #ifndef NDEBUG
306   virtual void print(raw_ostream &O) const;
307 #endif
308 };
309
310 //===--------------------------------------------------------------------===//
311 /// DIELabel - A label DIE.
312 //
313 class DIELabel : public DIEValue {
314   const MCSymbol *Label;
315
316 public:
317   explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
318
319   /// EmitValue - Emit label value.
320   ///
321   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
322
323   /// getValue - Get MCSymbol.
324   ///
325   const MCSymbol *getValue() const { return Label; }
326
327   /// SizeOf - Determine size of label value in bytes.
328   ///
329   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
330
331   // Implement isa/cast/dyncast.
332   static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
333
334 #ifndef NDEBUG
335   virtual void print(raw_ostream &O) const;
336 #endif
337 };
338
339 //===--------------------------------------------------------------------===//
340 /// DIEDelta - A simple label difference DIE.
341 ///
342 class DIEDelta : public DIEValue {
343   const MCSymbol *LabelHi;
344   const MCSymbol *LabelLo;
345
346 public:
347   DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
348       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
349
350   /// EmitValue - Emit delta value.
351   ///
352   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
353
354   /// SizeOf - Determine size of delta value in bytes.
355   ///
356   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
357
358   // Implement isa/cast/dyncast.
359   static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
360
361 #ifndef NDEBUG
362   virtual void print(raw_ostream &O) const;
363 #endif
364 };
365
366 //===--------------------------------------------------------------------===//
367 /// DIEString - A container for string values.
368 ///
369 class DIEString : public DIEValue {
370   const DIEValue *Access;
371   const StringRef Str;
372
373 public:
374   DIEString(const DIEValue *Acc, const StringRef S)
375       : DIEValue(isString), Access(Acc), Str(S) {}
376
377   /// getString - Grab the string out of the object.
378   StringRef getString() const { return Str; }
379
380   /// EmitValue - Emit delta value.
381   ///
382   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
383
384   /// SizeOf - Determine size of delta value in bytes.
385   ///
386   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
387
388   // Implement isa/cast/dyncast.
389   static bool classof(const DIEValue *D) { return D->getType() == isString; }
390
391 #ifndef NDEBUG
392   virtual void print(raw_ostream &O) const;
393 #endif
394 };
395
396 //===--------------------------------------------------------------------===//
397 /// DIEEntry - A pointer to another debug information entry.  An instance of
398 /// this class can also be used as a proxy for a debug information entry not
399 /// yet defined (ie. types.)
400 class DIEEntry : public DIEValue {
401   DIE *const Entry;
402
403 public:
404   explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
405     assert(E && "Cannot construct a DIEEntry with a null DIE");
406   }
407
408   DIE *getEntry() const { return Entry; }
409
410   /// EmitValue - Emit debug information entry offset.
411   ///
412   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
413
414   /// SizeOf - Determine size of debug information entry in bytes.
415   ///
416   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
417     return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
418                                            : sizeof(int32_t);
419   }
420
421   /// Returns size of a ref_addr entry.
422   static unsigned getRefAddrSize(AsmPrinter *AP);
423
424   // Implement isa/cast/dyncast.
425   static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
426
427 #ifndef NDEBUG
428   virtual void print(raw_ostream &O) const;
429 #endif
430 };
431
432 //===--------------------------------------------------------------------===//
433 /// \brief A signature reference to a type unit.
434 class DIETypeSignature : public DIEValue {
435   const DwarfTypeUnit &Unit;
436
437 public:
438   explicit DIETypeSignature(const DwarfTypeUnit &Unit)
439       : DIEValue(isTypeSignature), Unit(Unit) {}
440
441   /// \brief Emit type unit signature.
442   virtual void EmitValue(AsmPrinter *Asm, dwarf::Form Form) const;
443
444   /// Returns size of a ref_sig8 entry.
445   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
446     assert(Form == dwarf::DW_FORM_ref_sig8);
447     return 8;
448   }
449
450   // \brief Implement isa/cast/dyncast.
451   static bool classof(const DIEValue *E) {
452     return E->getType() == isTypeSignature;
453   }
454 #ifndef NDEBUG
455   virtual void print(raw_ostream &O) const;
456   void dump() const;
457 #endif
458 };
459
460 //===--------------------------------------------------------------------===//
461 /// DIELoc - Represents an expression location.
462 //
463 class DIELoc : public DIEValue, public DIE {
464   mutable unsigned Size; // Size in bytes excluding size header.
465 public:
466   DIELoc() : DIEValue(isLoc), DIE(0), Size(0) {}
467
468   /// ComputeSize - Calculate the size of the location expression.
469   ///
470   unsigned ComputeSize(AsmPrinter *AP) const;
471
472   /// BestForm - Choose the best form for data.
473   ///
474   dwarf::Form BestForm(unsigned DwarfVersion) const {
475     if (DwarfVersion > 3)
476       return dwarf::DW_FORM_exprloc;
477     // Pre-DWARF4 location expressions were blocks and not exprloc.
478     if ((unsigned char)Size == Size)
479       return dwarf::DW_FORM_block1;
480     if ((unsigned short)Size == Size)
481       return dwarf::DW_FORM_block2;
482     if ((unsigned int)Size == Size)
483       return dwarf::DW_FORM_block4;
484     return dwarf::DW_FORM_block;
485   }
486
487   /// EmitValue - Emit location data.
488   ///
489   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
490
491   /// SizeOf - Determine size of location data in bytes.
492   ///
493   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
494
495   // Implement isa/cast/dyncast.
496   static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
497
498 #ifndef NDEBUG
499   virtual void print(raw_ostream &O) const;
500 #endif
501 };
502
503 //===--------------------------------------------------------------------===//
504 /// DIEBlock - Represents a block of values.
505 //
506 class DIEBlock : public DIEValue, public DIE {
507   mutable unsigned Size; // Size in bytes excluding size header.
508 public:
509   DIEBlock() : DIEValue(isBlock), DIE(0), Size(0) {}
510
511   /// ComputeSize - Calculate the size of the location expression.
512   ///
513   unsigned ComputeSize(AsmPrinter *AP) const;
514
515   /// BestForm - Choose the best form for data.
516   ///
517   dwarf::Form BestForm() const {
518     if ((unsigned char)Size == Size)
519       return dwarf::DW_FORM_block1;
520     if ((unsigned short)Size == Size)
521       return dwarf::DW_FORM_block2;
522     if ((unsigned int)Size == Size)
523       return dwarf::DW_FORM_block4;
524     return dwarf::DW_FORM_block;
525   }
526
527   /// EmitValue - Emit location data.
528   ///
529   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const;
530
531   /// SizeOf - Determine size of location data in bytes.
532   ///
533   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const;
534
535   // Implement isa/cast/dyncast.
536   static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
537
538 #ifndef NDEBUG
539   virtual void print(raw_ostream &O) const;
540 #endif
541 };
542 } // end llvm namespace
543
544 #endif