Split out the Dwarf writer stuff into separate files. This is a much more
[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 DIE_H__
15 #define DIE_H__
16
17 #include "DwarfLabel.h"
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Dwarf.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <iosfwd>
24
25 namespace llvm {
26   class AsmPrinter;
27   class Dwarf;
28   class TargetData;
29
30   //===--------------------------------------------------------------------===//
31   /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
32   /// Dwarf abbreviation.
33   class VISIBILITY_HIDDEN DIEAbbrevData {
34     /// Attribute - Dwarf attribute code.
35     ///
36     unsigned Attribute;
37
38     /// Form - Dwarf form code.
39     ///
40     unsigned Form;
41   public:
42     DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
43
44     // Accessors.
45     unsigned getAttribute() const { return Attribute; }
46     unsigned 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 VISIBILITY_HIDDEN DIEAbbrev : public FoldingSetNode {
57     /// Tag - Dwarf tag code.
58     ///
59     unsigned Tag;
60
61     /// Unique number for node.
62     ///
63     unsigned Number;
64
65     /// ChildrenFlag - Dwarf children flag.
66     ///
67     unsigned ChildrenFlag;
68
69     /// Data - Raw data bytes for abbreviation.
70     ///
71     SmallVector<DIEAbbrevData, 8> Data;
72   public:
73     DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
74     virtual ~DIEAbbrev() {}
75
76     // Accessors.
77     unsigned getTag() const { return Tag; }
78     unsigned getNumber() const { return Number; }
79     unsigned getChildrenFlag() const { return ChildrenFlag; }
80     const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
81     void setTag(unsigned T) { Tag = T; }
82     void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
83     void setNumber(unsigned N) { Number = N; }
84
85     /// AddAttribute - Adds another set of attribute information to the
86     /// abbreviation.
87     void AddAttribute(unsigned Attribute, unsigned Form) {
88       Data.push_back(DIEAbbrevData(Attribute, Form));
89     }
90
91     /// AddFirstAttribute - Adds a set of attribute information to the front
92     /// of the abbreviation.
93     void AddFirstAttribute(unsigned Attribute, unsigned Form) {
94       Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
95     }
96
97     /// Profile - Used to gather unique data for the abbreviation folding set.
98     ///
99     void Profile(FoldingSetNodeID &ID) const;
100
101     /// Emit - Print the abbreviation using the specified asm printer.
102     ///
103     void Emit(const AsmPrinter *Asm) const;
104
105 #ifndef NDEBUG
106     void print(std::ostream *O) {
107       if (O) print(*O);
108     }
109     void print(std::ostream &O);
110     void dump();
111 #endif
112   };
113
114   //===--------------------------------------------------------------------===//
115   /// DIE - A structured debug information entry.  Has an abbreviation which
116   /// describes it's organization.
117   class CompileUnit;
118   class DIEValue;
119
120   class VISIBILITY_HIDDEN DIE : public FoldingSetNode {
121   protected:
122     /// Abbrev - Buffer for constructing abbreviation.
123     ///
124     DIEAbbrev Abbrev;
125
126     /// Offset - Offset in debug info section.
127     ///
128     unsigned Offset;
129
130     /// Size - Size of instance + children.
131     ///
132     unsigned Size;
133
134     /// Children DIEs.
135     ///
136     std::vector<DIE *> Children;
137
138     /// Attributes values.
139     ///
140     SmallVector<DIEValue*, 32> Values;
141
142     /// Abstract compile unit.
143     CompileUnit *AbstractCU;
144   public:
145     explicit DIE(unsigned Tag)
146       : Abbrev(Tag, dwarf::DW_CHILDREN_no), Offset(0), Size(0) {}
147     virtual ~DIE();
148
149     // Accessors.
150     DIEAbbrev &getAbbrev() { return Abbrev; }
151     unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
152     unsigned getTag() const { return Abbrev.getTag(); }
153     unsigned getOffset() const { return Offset; }
154     unsigned getSize() const { return Size; }
155     const std::vector<DIE *> &getChildren() const { return Children; }
156     SmallVector<DIEValue*, 32> &getValues() { return Values; }
157     CompileUnit *getAbstractCompileUnit() const { return AbstractCU; }
158
159     void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
160     void setOffset(unsigned O) { Offset = O; }
161     void setSize(unsigned S) { Size = S; }
162     void setAbstractCompileUnit(CompileUnit *CU) { AbstractCU = CU; }
163
164     /// AddValue - Add a value and attributes to a DIE.
165     ///
166     void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
167       Abbrev.AddAttribute(Attribute, Form);
168       Values.push_back(Value);
169     }
170
171     /// SiblingOffset - Return the offset of the debug information entry's
172     /// sibling.
173     unsigned SiblingOffset() const { return Offset + Size; }
174
175     /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
176     ///
177     void AddSiblingOffset();
178
179     /// AddChild - Add a child to the DIE.
180     ///
181     void AddChild(DIE *Child) {
182       Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
183       Children.push_back(Child);
184     }
185
186     /// Detach - Detaches objects connected to it after copying.
187     ///
188     void Detach() {
189       Children.clear();
190     }
191
192     /// Profile - Used to gather unique data for the value folding set.
193     ///
194     void Profile(FoldingSetNodeID &ID) ;
195
196 #ifndef NDEBUG
197     void print(std::ostream *O, unsigned IncIndent = 0) {
198       if (O) print(*O, IncIndent);
199     }
200     void print(std::ostream &O, unsigned IncIndent = 0);
201     void dump();
202 #endif
203   };
204
205   //===--------------------------------------------------------------------===//
206   /// DIEValue - A debug information entry value.
207   ///
208   class VISIBILITY_HIDDEN DIEValue : public FoldingSetNode {
209   public:
210     enum {
211       isInteger,
212       isString,
213       isLabel,
214       isAsIsLabel,
215       isSectionOffset,
216       isDelta,
217       isEntry,
218       isBlock
219     };
220   protected:
221     /// Type - Type of data stored in the value.
222     ///
223     unsigned Type;
224   public:
225     explicit DIEValue(unsigned T) : Type(T) {}
226     virtual ~DIEValue() {}
227
228     // Accessors
229     unsigned getType()  const { return Type; }
230
231     /// EmitValue - Emit value via the Dwarf writer.
232     ///
233     virtual void EmitValue(Dwarf *D, unsigned Form) const = 0;
234
235     /// SizeOf - Return the size of a value in bytes.
236     ///
237     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const = 0;
238
239     /// Profile - Used to gather unique data for the value folding set.
240     ///
241     virtual void Profile(FoldingSetNodeID &ID) = 0;
242
243     // Implement isa/cast/dyncast.
244     static bool classof(const DIEValue *) { return true; }
245
246 #ifndef NDEBUG
247     void print(std::ostream *O) {
248       if (O) print(*O);
249     }
250     virtual void print(std::ostream &O) = 0;
251     void dump();
252 #endif
253   };
254
255   //===--------------------------------------------------------------------===//
256   /// DIEInteger - An integer value DIE.
257   ///
258   class VISIBILITY_HIDDEN DIEInteger : public DIEValue {
259     uint64_t Integer;
260   public:
261     explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
262
263     /// BestForm - Choose the best form for integer.
264     ///
265     static unsigned BestForm(bool IsSigned, uint64_t Int) {
266       if (IsSigned) {
267         if ((char)Int == (signed)Int)   return dwarf::DW_FORM_data1;
268         if ((short)Int == (signed)Int)  return dwarf::DW_FORM_data2;
269         if ((int)Int == (signed)Int)    return dwarf::DW_FORM_data4;
270       } else {
271         if ((unsigned char)Int == Int)  return dwarf::DW_FORM_data1;
272         if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
273         if ((unsigned int)Int == Int)   return dwarf::DW_FORM_data4;
274       }
275       return dwarf::DW_FORM_data8;
276     }
277
278     /// EmitValue - Emit integer of appropriate size.
279     ///
280     virtual void EmitValue(Dwarf *D, unsigned Form) const;
281
282     /// SizeOf - Determine size of integer value in bytes.
283     ///
284     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
285
286     /// Profile - Used to gather unique data for the value folding set.
287     ///
288     static void Profile(FoldingSetNodeID &ID, unsigned Int);
289     virtual void Profile(FoldingSetNodeID &ID);
290
291     // Implement isa/cast/dyncast.
292     static bool classof(const DIEInteger *) { return true; }
293     static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
294
295 #ifndef NDEBUG
296     virtual void print(std::ostream &O);
297 #endif
298   };
299
300   //===--------------------------------------------------------------------===//
301   /// DIEString - A string value DIE.
302   ///
303   class VISIBILITY_HIDDEN DIEString : public DIEValue {
304     const std::string Str;
305   public:
306     explicit DIEString(const std::string &S) : DIEValue(isString), Str(S) {}
307
308     /// EmitValue - Emit string value.
309     ///
310     virtual void EmitValue(Dwarf *D, unsigned Form) const;
311
312     /// SizeOf - Determine size of string value in bytes.
313     ///
314     virtual unsigned SizeOf(const TargetData *, unsigned /*Form*/) const {
315       return Str.size() + sizeof(char); // sizeof('\0');
316     }
317
318     /// Profile - Used to gather unique data for the value folding set.
319     ///
320     static void Profile(FoldingSetNodeID &ID, const std::string &Str);
321     virtual void Profile(FoldingSetNodeID &ID);
322
323     // Implement isa/cast/dyncast.
324     static bool classof(const DIEString *) { return true; }
325     static bool classof(const DIEValue *S) { return S->getType() == isString; }
326
327 #ifndef NDEBUG
328     virtual void print(std::ostream &O);
329 #endif
330   };
331
332   //===--------------------------------------------------------------------===//
333   /// DIEDwarfLabel - A Dwarf internal label expression DIE.
334   //
335   class VISIBILITY_HIDDEN DIEDwarfLabel : public DIEValue {
336     const DWLabel Label;
337   public:
338     explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
339
340     /// EmitValue - Emit label value.
341     ///
342     virtual void EmitValue(Dwarf *D, unsigned Form) const;
343
344     /// SizeOf - Determine size of label value in bytes.
345     ///
346     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
347
348     /// Profile - Used to gather unique data for the value folding set.
349     ///
350     static void Profile(FoldingSetNodeID &ID, const DWLabel &Label);
351     virtual void Profile(FoldingSetNodeID &ID);
352
353     // Implement isa/cast/dyncast.
354     static bool classof(const DIEDwarfLabel *)  { return true; }
355     static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
356
357 #ifndef NDEBUG
358     virtual void print(std::ostream &O);
359 #endif
360   };
361
362   //===--------------------------------------------------------------------===//
363   /// DIEObjectLabel - A label to an object in code or data.
364   //
365   class VISIBILITY_HIDDEN DIEObjectLabel : public DIEValue {
366     const std::string Label;
367   public:
368     explicit DIEObjectLabel(const std::string &L)
369       : DIEValue(isAsIsLabel), Label(L) {}
370
371     /// EmitValue - Emit label value.
372     ///
373     virtual void EmitValue(Dwarf *D, unsigned Form) const;
374
375     /// SizeOf - Determine size of label value in bytes.
376     ///
377     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
378
379     /// Profile - Used to gather unique data for the value folding set.
380     ///
381     static void Profile(FoldingSetNodeID &ID, const std::string &Label);
382     virtual void Profile(FoldingSetNodeID &ID);
383
384     // Implement isa/cast/dyncast.
385     static bool classof(const DIEObjectLabel *) { return true; }
386     static bool classof(const DIEValue *L) {
387       return L->getType() == isAsIsLabel;
388     }
389
390 #ifndef NDEBUG
391     virtual void print(std::ostream &O);
392 #endif
393   };
394
395   //===--------------------------------------------------------------------===//
396   /// DIESectionOffset - A section offset DIE.
397   ///
398   class VISIBILITY_HIDDEN DIESectionOffset : public DIEValue {
399     const DWLabel Label;
400     const DWLabel Section;
401     bool IsEH : 1;
402     bool UseSet : 1;
403   public:
404     DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
405                      bool isEH = false, bool useSet = true)
406       : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
407         IsEH(isEH), UseSet(useSet) {}
408
409     /// EmitValue - Emit section offset.
410     ///
411     virtual void EmitValue(Dwarf *D, unsigned Form) const;
412
413     /// SizeOf - Determine size of section offset value in bytes.
414     ///
415     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
416
417     /// Profile - Used to gather unique data for the value folding set.
418     ///
419     static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
420                         const DWLabel &Section);
421     virtual void Profile(FoldingSetNodeID &ID);
422
423     // Implement isa/cast/dyncast.
424     static bool classof(const DIESectionOffset *)  { return true; }
425     static bool classof(const DIEValue *D) {
426       return D->getType() == isSectionOffset;
427     }
428
429 #ifndef NDEBUG
430     virtual void print(std::ostream &O);
431 #endif
432   };
433
434   //===--------------------------------------------------------------------===//
435   /// DIEDelta - A simple label difference DIE.
436   ///
437   class VISIBILITY_HIDDEN DIEDelta : public DIEValue {
438     const DWLabel LabelHi;
439     const DWLabel LabelLo;
440   public:
441     DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
442       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
443
444     /// EmitValue - Emit delta value.
445     ///
446     virtual void EmitValue(Dwarf *D, unsigned Form) const;
447
448     /// SizeOf - Determine size of delta value in bytes.
449     ///
450     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
451
452     /// Profile - Used to gather unique data for the value folding set.
453     ///
454     static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
455                         const DWLabel &LabelLo);
456     virtual void Profile(FoldingSetNodeID &ID);
457
458     // Implement isa/cast/dyncast.
459     static bool classof(const DIEDelta *)  { return true; }
460     static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
461
462 #ifndef NDEBUG
463     virtual void print(std::ostream &O);
464 #endif
465   };
466
467   //===--------------------------------------------------------------------===//
468   /// DIEntry - A pointer to another debug information entry.  An instance of
469   /// this class can also be used as a proxy for a debug information entry not
470   /// yet defined (ie. types.)
471   class VISIBILITY_HIDDEN DIEEntry : public DIEValue {
472     DIE *Entry;
473   public:
474     explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
475
476     DIE *getEntry() const { return Entry; }
477     void setEntry(DIE *E) { Entry = E; }
478
479     /// EmitValue - Emit debug information entry offset.
480     ///
481     virtual void EmitValue(Dwarf *D, unsigned Form) const;
482
483     /// SizeOf - Determine size of debug information entry in bytes.
484     ///
485     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const {
486       return sizeof(int32_t);
487     }
488
489     /// Profile - Used to gather unique data for the value folding set.
490     ///
491     static void Profile(FoldingSetNodeID &ID, DIE *Entry);
492     virtual void Profile(FoldingSetNodeID &ID);
493
494     // Implement isa/cast/dyncast.
495     static bool classof(const DIEEntry *)  { return true; }
496     static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
497
498 #ifndef NDEBUG
499     virtual void print(std::ostream &O);
500 #endif
501   };
502
503   //===--------------------------------------------------------------------===//
504   /// DIEBlock - A block of values.  Primarily used for location expressions.
505   //
506   class DIEBlock : public DIEValue, public DIE {
507     unsigned Size;                // Size in bytes excluding size header.
508   public:
509     DIEBlock()
510       : DIEValue(isBlock), DIE(0), Size(0) {}
511     virtual ~DIEBlock() {}
512
513     /// ComputeSize - calculate the size of the block.
514     ///
515     unsigned ComputeSize(const TargetData *TD);
516
517     /// BestForm - Choose the best form for data.
518     ///
519     unsigned BestForm() const {
520       if ((unsigned char)Size == Size)  return dwarf::DW_FORM_block1;
521       if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
522       if ((unsigned int)Size == Size)   return dwarf::DW_FORM_block4;
523       return dwarf::DW_FORM_block;
524     }
525
526     /// EmitValue - Emit block data.
527     ///
528     virtual void EmitValue(Dwarf *D, unsigned Form) const;
529
530     /// SizeOf - Determine size of block data in bytes.
531     ///
532     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
533
534     /// Profile - Used to gather unique data for the value folding set.
535     ///
536     virtual void Profile(FoldingSetNodeID &ID);
537
538     // Implement isa/cast/dyncast.
539     static bool classof(const DIEBlock *)  { return true; }
540     static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
541
542 #ifndef NDEBUG
543     virtual void print(std::ostream &O);
544 #endif
545   };
546
547 } // end llvm namespace
548
549 #endif