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