AsmPrinter: Remove the vtable-entry from DIEValue
[oota-llvm.git] / include / llvm / CodeGen / 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 LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
16
17 #include "llvm/ADT/FoldingSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Support/Dwarf.h"
20 #include <vector>
21
22 namespace llvm {
23 class AsmPrinter;
24 class MCExpr;
25 class MCSymbol;
26 class raw_ostream;
27 class DwarfTypeUnit;
28
29 //===--------------------------------------------------------------------===//
30 /// DIEAbbrevData - Dwarf abbreviation data, describes one attribute of a
31 /// Dwarf abbreviation.
32 class DIEAbbrevData {
33   /// Attribute - Dwarf attribute code.
34   ///
35   dwarf::Attribute Attribute;
36
37   /// Form - Dwarf form code.
38   ///
39   dwarf::Form Form;
40
41 public:
42   DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
43
44   // Accessors.
45   dwarf::Attribute getAttribute() const { return Attribute; }
46   dwarf::Form 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 DIEAbbrev : public FoldingSetNode {
57   /// Unique number for node.
58   ///
59   unsigned Number;
60
61   /// Tag - Dwarf tag code.
62   ///
63   dwarf::Tag Tag;
64
65   /// Children - Whether or not this node has children.
66   ///
67   // This cheats a bit in all of the uses since the values in the standard
68   // are 0 and 1 for no children and children respectively.
69   bool Children;
70
71   /// Data - Raw data bytes for abbreviation.
72   ///
73   SmallVector<DIEAbbrevData, 12> Data;
74
75 public:
76   DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C), Data() {}
77
78   // Accessors.
79   dwarf::Tag getTag() const { return Tag; }
80   unsigned getNumber() const { return Number; }
81   bool hasChildren() const { return Children; }
82   const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
83   void setChildrenFlag(bool hasChild) { Children = hasChild; }
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(const 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   // This can't be a vector<DIE> because pointer validity is requirent for the
128   // Parent pointer and DIEEntry.
129   // It can't be a list<DIE> because some clients need pointer validity before
130   // the object has been added to any child list
131   // (eg: DwarfUnit::constructVariableDIE). These aren't insurmountable, but may
132   // be more convoluted than beneficial.
133   std::vector<std::unique_ptr<DIE>> Children;
134
135   DIE *Parent;
136
137   /// Attribute values.
138   ///
139   SmallVector<DIEValue *, 12> Values;
140
141 protected:
142   DIE()
143       : Offset(0), Size(0), Abbrev((dwarf::Tag)0, dwarf::DW_CHILDREN_no),
144         Parent(nullptr) {}
145
146 public:
147   explicit DIE(dwarf::Tag Tag)
148       : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
149         Parent(nullptr) {}
150
151   // Accessors.
152   DIEAbbrev &getAbbrev() { return Abbrev; }
153   const DIEAbbrev &getAbbrev() const { return Abbrev; }
154   unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
155   dwarf::Tag getTag() const { return Abbrev.getTag(); }
156   unsigned getOffset() const { return Offset; }
157   unsigned getSize() const { return Size; }
158   const std::vector<std::unique_ptr<DIE>> &getChildren() const {
159     return Children;
160   }
161   const SmallVectorImpl<DIEValue *> &getValues() const { return Values; }
162   DIE *getParent() const { return Parent; }
163   /// Climb up the parent chain to get the compile or type unit DIE this DIE
164   /// belongs to.
165   const DIE *getUnit() const;
166   /// Similar to getUnit, returns null when DIE is not added to an
167   /// owner yet.
168   const DIE *getUnitOrNull() const;
169   void setOffset(unsigned O) { Offset = O; }
170   void setSize(unsigned S) { Size = S; }
171
172   /// addValue - Add a value and attributes to a DIE.
173   ///
174   void addValue(dwarf::Attribute Attribute, dwarf::Form Form, DIEValue *Value) {
175     Abbrev.AddAttribute(Attribute, Form);
176     Values.push_back(Value);
177   }
178
179   /// addChild - Add a child to the DIE.
180   ///
181   void addChild(std::unique_ptr<DIE> Child) {
182     assert(!Child->getParent());
183     Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
184     Child->Parent = this;
185     Children.push_back(std::move(Child));
186   }
187
188   /// findAttribute - Find a value in the DIE with the attribute given,
189   /// returns NULL if no such attribute exists.
190   DIEValue *findAttribute(dwarf::Attribute Attribute) const;
191
192 #ifndef NDEBUG
193   void print(raw_ostream &O, unsigned IndentCount = 0) const;
194   void dump();
195 #endif
196 };
197
198 //===--------------------------------------------------------------------===//
199 /// DIEValue - A debug information entry value. Some of these roughly correlate
200 /// to DWARF attribute classes.
201 ///
202 class DIEValue {
203 public:
204   enum Type {
205     isInteger,
206     isString,
207     isExpr,
208     isLabel,
209     isDelta,
210     isEntry,
211     isTypeSignature,
212     isBlock,
213     isLoc,
214     isLocList,
215   };
216
217 protected:
218   /// Ty - Type of data stored in the value.
219   ///
220   Type Ty;
221
222   explicit DIEValue(Type T) : Ty(T) {}
223   ~DIEValue() {}
224
225 public:
226   // Accessors
227   Type getType() const { return Ty; }
228
229   /// EmitValue - Emit value via the Dwarf writer.
230   ///
231   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
232
233   /// SizeOf - Return the size of a value in bytes.
234   ///
235   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
236
237 #ifndef NDEBUG
238   void print(raw_ostream &O) const;
239   void dump() const;
240 #endif
241 };
242
243 //===--------------------------------------------------------------------===//
244 /// DIEInteger - An integer value DIE.
245 ///
246 class DIEInteger : public DIEValue {
247   friend DIEValue;
248
249   uint64_t Integer;
250
251 public:
252   explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
253
254   /// BestForm - Choose the best form for integer.
255   ///
256   static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
257     if (IsSigned) {
258       const int64_t SignedInt = Int;
259       if ((char)Int == SignedInt)
260         return dwarf::DW_FORM_data1;
261       if ((short)Int == SignedInt)
262         return dwarf::DW_FORM_data2;
263       if ((int)Int == SignedInt)
264         return dwarf::DW_FORM_data4;
265     } else {
266       if ((unsigned char)Int == Int)
267         return dwarf::DW_FORM_data1;
268       if ((unsigned short)Int == Int)
269         return dwarf::DW_FORM_data2;
270       if ((unsigned int)Int == Int)
271         return dwarf::DW_FORM_data4;
272     }
273     return dwarf::DW_FORM_data8;
274   }
275
276   uint64_t getValue() const { return Integer; }
277   void setValue(uint64_t Val) { Integer = Val; }
278
279   // Implement isa/cast/dyncast.
280   static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
281
282 private:
283   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
284   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
285
286 #ifndef NDEBUG
287   void printImpl(raw_ostream &O) const;
288 #endif
289 };
290
291 //===--------------------------------------------------------------------===//
292 /// DIEExpr - An expression DIE.
293 //
294 class DIEExpr : public DIEValue {
295   friend class DIEValue;
296
297   const MCExpr *Expr;
298
299 public:
300   explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
301
302   /// getValue - Get MCExpr.
303   ///
304   const MCExpr *getValue() const { return Expr; }
305
306   // Implement isa/cast/dyncast.
307   static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
308
309 private:
310   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
311   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
312
313 #ifndef NDEBUG
314   void printImpl(raw_ostream &O) const;
315 #endif
316 };
317
318 //===--------------------------------------------------------------------===//
319 /// DIELabel - A label DIE.
320 //
321 class DIELabel : public DIEValue {
322   friend class DIEValue;
323
324   const MCSymbol *Label;
325
326 public:
327   explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
328
329   /// getValue - Get MCSymbol.
330   ///
331   const MCSymbol *getValue() const { return Label; }
332
333   // Implement isa/cast/dyncast.
334   static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
335
336 private:
337   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
338   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
339
340 #ifndef NDEBUG
341   void printImpl(raw_ostream &O) const;
342 #endif
343 };
344
345 //===--------------------------------------------------------------------===//
346 /// DIEDelta - A simple label difference DIE.
347 ///
348 class DIEDelta : public DIEValue {
349   friend class DIEValue;
350
351   const MCSymbol *LabelHi;
352   const MCSymbol *LabelLo;
353
354 public:
355   DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
356       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
357
358   // Implement isa/cast/dyncast.
359   static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
360
361 private:
362   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
363   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
364
365 #ifndef NDEBUG
366   void printImpl(raw_ostream &O) const;
367 #endif
368 };
369
370 //===--------------------------------------------------------------------===//
371 /// DIEString - A container for string values.
372 ///
373 class DIEString : public DIEValue {
374   friend class DIEValue;
375
376   const DIEValue *Access;
377   StringRef Str;
378
379 public:
380   DIEString(const DIEValue *Acc, StringRef S)
381       : DIEValue(isString), Access(Acc), Str(S) {}
382
383   /// getString - Grab the string out of the object.
384   StringRef getString() const { return Str; }
385
386   // Implement isa/cast/dyncast.
387   static bool classof(const DIEValue *D) { return D->getType() == isString; }
388
389 private:
390   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
391   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
392
393 #ifndef NDEBUG
394   void printImpl(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   friend class DIEValue;
404
405   DIE &Entry;
406
407 public:
408   explicit DIEEntry(DIE &E) : DIEValue(isEntry), Entry(E) {
409   }
410
411   DIE &getEntry() const { return Entry; }
412
413   /// Returns size of a ref_addr entry.
414   static unsigned getRefAddrSize(const AsmPrinter *AP);
415
416   // Implement isa/cast/dyncast.
417   static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
418
419 private:
420   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
421   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
422     return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
423                                            : sizeof(int32_t);
424   }
425
426 #ifndef NDEBUG
427   void printImpl(raw_ostream &O) const;
428 #endif
429 };
430
431 //===--------------------------------------------------------------------===//
432 /// \brief A signature reference to a type unit.
433 class DIETypeSignature : public DIEValue {
434   friend class DIEValue;
435
436   const DwarfTypeUnit &Unit;
437
438 public:
439   explicit DIETypeSignature(const DwarfTypeUnit &Unit)
440       : DIEValue(isTypeSignature), Unit(Unit) {}
441
442   // \brief Implement isa/cast/dyncast.
443   static bool classof(const DIEValue *E) {
444     return E->getType() == isTypeSignature;
445   }
446
447 private:
448   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
449   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
450     assert(Form == dwarf::DW_FORM_ref_sig8);
451     return 8;
452   }
453
454 #ifndef NDEBUG
455   void printImpl(raw_ostream &O) const;
456 #endif
457 };
458
459 //===--------------------------------------------------------------------===//
460 /// DIELoc - Represents an expression location.
461 //
462 class DIELoc : public DIEValue, public DIE {
463   friend class DIEValue;
464
465   mutable unsigned Size; // Size in bytes excluding size header.
466 public:
467   DIELoc() : DIEValue(isLoc), Size(0) {}
468
469   /// ComputeSize - Calculate the size of the location expression.
470   ///
471   unsigned ComputeSize(const AsmPrinter *AP) const;
472
473   /// BestForm - Choose the best form for data.
474   ///
475   dwarf::Form BestForm(unsigned DwarfVersion) const {
476     if (DwarfVersion > 3)
477       return dwarf::DW_FORM_exprloc;
478     // Pre-DWARF4 location expressions were blocks and not exprloc.
479     if ((unsigned char)Size == Size)
480       return dwarf::DW_FORM_block1;
481     if ((unsigned short)Size == Size)
482       return dwarf::DW_FORM_block2;
483     if ((unsigned int)Size == Size)
484       return dwarf::DW_FORM_block4;
485     return dwarf::DW_FORM_block;
486   }
487
488   // Implement isa/cast/dyncast.
489   static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
490
491 private:
492   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
493   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
494
495 #ifndef NDEBUG
496   void printImpl(raw_ostream &O) const;
497 #endif
498 };
499
500 //===--------------------------------------------------------------------===//
501 /// DIEBlock - Represents a block of values.
502 //
503 class DIEBlock : public DIEValue, public DIE {
504   friend class DIEValue;
505
506   mutable unsigned Size; // Size in bytes excluding size header.
507 public:
508   DIEBlock() : DIEValue(isBlock), Size(0) {}
509
510   /// ComputeSize - Calculate the size of the location expression.
511   ///
512   unsigned ComputeSize(const 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   // Implement isa/cast/dyncast.
527   static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
528
529 private:
530   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
531   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
532
533 #ifndef NDEBUG
534   void printImpl(raw_ostream &O) const;
535 #endif
536 };
537
538 //===--------------------------------------------------------------------===//
539 /// DIELocList - Represents a pointer to a location list in the debug_loc
540 /// section.
541 //
542 class DIELocList : public DIEValue {
543   friend class DIEValue;
544
545   // Index into the .debug_loc vector.
546   size_t Index;
547
548 public:
549   DIELocList(size_t I) : DIEValue(isLocList), Index(I) {}
550
551   /// getValue - Grab the current index out.
552   size_t getValue() const { return Index; }
553
554   // Implement isa/cast/dyncast.
555   static bool classof(const DIEValue *E) { return E->getType() == isLocList; }
556
557 private:
558   void EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const;
559   unsigned SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const;
560
561 #ifndef NDEBUG
562   void printImpl(raw_ostream &O) const;
563 #endif
564 };
565
566 } // end llvm namespace
567
568 #endif