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