• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
DIEAbbrevData(dwarf::Attribute A,dwarf::Form F)42   DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
43 
44   // Accessors.
getAttribute()45   dwarf::Attribute getAttribute() const { return Attribute; }
getForm()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:
DIEAbbrev(dwarf::Tag T,bool C)76   DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C), Data() {}
77 
78   // Accessors.
getTag()79   dwarf::Tag getTag() const { return Tag; }
getNumber()80   unsigned getNumber() const { return Number; }
hasChildren()81   bool hasChildren() const { return Children; }
getData()82   const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
setChildrenFlag(bool hasChild)83   void setChildrenFlag(bool hasChild) { Children = hasChild; }
setNumber(unsigned N)84   void setNumber(unsigned N) { Number = N; }
85 
86   /// AddAttribute - Adds another set of attribute information to the
87   /// abbreviation.
AddAttribute(dwarf::Attribute Attribute,dwarf::Form Form)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:
DIE()142   DIE()
143       : Offset(0), Size(0), Abbrev((dwarf::Tag)0, dwarf::DW_CHILDREN_no),
144         Parent(nullptr) {}
145 
146 public:
DIE(dwarf::Tag Tag)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.
getAbbrev()152   DIEAbbrev &getAbbrev() { return Abbrev; }
getAbbrev()153   const DIEAbbrev &getAbbrev() const { return Abbrev; }
getAbbrevNumber()154   unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
getTag()155   dwarf::Tag getTag() const { return Abbrev.getTag(); }
getOffset()156   unsigned getOffset() const { return Offset; }
getSize()157   unsigned getSize() const { return Size; }
getChildren()158   const std::vector<std::unique_ptr<DIE>> &getChildren() const {
159     return Children;
160   }
getValues()161   const SmallVectorImpl<DIEValue *> &getValues() const { return Values; }
getParent()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;
setOffset(unsigned O)169   void setOffset(unsigned O) { Offset = O; }
setSize(unsigned S)170   void setSize(unsigned S) { Size = S; }
171 
172   /// addValue - Add a value and attributes to a DIE.
173   ///
addValue(dwarf::Attribute Attribute,dwarf::Form Form,DIEValue * Value)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   ///
addChild(std::unique_ptr<DIE> Child)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   virtual void anchor();
204 
205 public:
206   enum Type {
207     isInteger,
208     isString,
209     isExpr,
210     isLabel,
211     isDelta,
212     isEntry,
213     isTypeSignature,
214     isBlock,
215     isLoc,
216     isLocList,
217   };
218 
219 protected:
220   /// Ty - Type of data stored in the value.
221   ///
222   Type Ty;
223 
DIEValue(Type T)224   explicit DIEValue(Type T) : Ty(T) {}
~DIEValue()225   virtual ~DIEValue() {}
226 
227 public:
228   // Accessors
getType()229   Type getType() const { return Ty; }
230 
231   /// EmitValue - Emit value via the Dwarf writer.
232   ///
233   virtual void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const = 0;
234 
235   /// SizeOf - Return the size of a value in bytes.
236   ///
237   virtual unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const = 0;
238 
239 #ifndef NDEBUG
240   virtual void print(raw_ostream &O) const = 0;
241   void dump() const;
242 #endif
243 };
244 
245 //===--------------------------------------------------------------------===//
246 /// DIEInteger - An integer value DIE.
247 ///
248 class DIEInteger : public DIEValue {
249   uint64_t Integer;
250 
251 public:
DIEInteger(uint64_t I)252   explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
253 
254   /// BestForm - Choose the best form for integer.
255   ///
BestForm(bool IsSigned,uint64_t Int)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   /// EmitValue - Emit integer of appropriate size.
277   ///
278   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
279 
getValue()280   uint64_t getValue() const { return Integer; }
setValue(uint64_t Val)281   void setValue(uint64_t Val) { Integer = Val; }
282 
283   /// SizeOf - Determine size of integer value in bytes.
284   ///
285   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
286 
287   // Implement isa/cast/dyncast.
classof(const DIEValue * I)288   static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
289 
290 #ifndef NDEBUG
291   void print(raw_ostream &O) const override;
292 #endif
293 };
294 
295 //===--------------------------------------------------------------------===//
296 /// DIEExpr - An expression DIE.
297 //
298 class DIEExpr : public DIEValue {
299   const MCExpr *Expr;
300 
301 public:
DIEExpr(const MCExpr * E)302   explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
303 
304   /// EmitValue - Emit expression value.
305   ///
306   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
307 
308   /// getValue - Get MCExpr.
309   ///
getValue()310   const MCExpr *getValue() const { return Expr; }
311 
312   /// SizeOf - Determine size of expression value in bytes.
313   ///
314   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
315 
316   // Implement isa/cast/dyncast.
classof(const DIEValue * E)317   static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
318 
319 #ifndef NDEBUG
320   void print(raw_ostream &O) const override;
321 #endif
322 };
323 
324 //===--------------------------------------------------------------------===//
325 /// DIELabel - A label DIE.
326 //
327 class DIELabel : public DIEValue {
328   const MCSymbol *Label;
329 
330 public:
DIELabel(const MCSymbol * L)331   explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
332 
333   /// EmitValue - Emit label value.
334   ///
335   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
336 
337   /// getValue - Get MCSymbol.
338   ///
getValue()339   const MCSymbol *getValue() const { return Label; }
340 
341   /// SizeOf - Determine size of label value in bytes.
342   ///
343   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
344 
345   // Implement isa/cast/dyncast.
classof(const DIEValue * L)346   static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
347 
348 #ifndef NDEBUG
349   void print(raw_ostream &O) const override;
350 #endif
351 };
352 
353 //===--------------------------------------------------------------------===//
354 /// DIEDelta - A simple label difference DIE.
355 ///
356 class DIEDelta : public DIEValue {
357   const MCSymbol *LabelHi;
358   const MCSymbol *LabelLo;
359 
360 public:
DIEDelta(const MCSymbol * Hi,const MCSymbol * Lo)361   DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
362       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
363 
364   /// EmitValue - Emit delta value.
365   ///
366   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
367 
368   /// SizeOf - Determine size of delta value in bytes.
369   ///
370   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
371 
372   // Implement isa/cast/dyncast.
classof(const DIEValue * D)373   static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
374 
375 #ifndef NDEBUG
376   void print(raw_ostream &O) const override;
377 #endif
378 };
379 
380 //===--------------------------------------------------------------------===//
381 /// DIEString - A container for string values.
382 ///
383 class DIEString : public DIEValue {
384   const DIEValue *Access;
385   StringRef Str;
386 
387 public:
DIEString(const DIEValue * Acc,StringRef S)388   DIEString(const DIEValue *Acc, StringRef S)
389       : DIEValue(isString), Access(Acc), Str(S) {}
390 
391   /// getString - Grab the string out of the object.
getString()392   StringRef getString() const { return Str; }
393 
394   /// EmitValue - Emit delta value.
395   ///
396   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
397 
398   /// SizeOf - Determine size of delta value in bytes.
399   ///
400   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
401 
402   // Implement isa/cast/dyncast.
classof(const DIEValue * D)403   static bool classof(const DIEValue *D) { return D->getType() == isString; }
404 
405 #ifndef NDEBUG
406   void print(raw_ostream &O) const override;
407 #endif
408 };
409 
410 //===--------------------------------------------------------------------===//
411 /// DIEEntry - A pointer to another debug information entry.  An instance of
412 /// this class can also be used as a proxy for a debug information entry not
413 /// yet defined (ie. types.)
414 class DIEEntry : public DIEValue {
415   DIE &Entry;
416 
417 public:
DIEEntry(DIE & E)418   explicit DIEEntry(DIE &E) : DIEValue(isEntry), Entry(E) {
419   }
420 
getEntry()421   DIE &getEntry() const { return Entry; }
422 
423   /// EmitValue - Emit debug information entry offset.
424   ///
425   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
426 
427   /// SizeOf - Determine size of debug information entry in bytes.
428   ///
SizeOf(const AsmPrinter * AP,dwarf::Form Form)429    unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override {
430     return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
431                                            : sizeof(int32_t);
432   }
433 
434   /// Returns size of a ref_addr entry.
435   static unsigned getRefAddrSize(const AsmPrinter *AP);
436 
437   // Implement isa/cast/dyncast.
classof(const DIEValue * E)438   static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
439 
440 #ifndef NDEBUG
441   void print(raw_ostream &O) const override;
442 #endif
443 };
444 
445 //===--------------------------------------------------------------------===//
446 /// \brief A signature reference to a type unit.
447 class DIETypeSignature : public DIEValue {
448   const DwarfTypeUnit &Unit;
449 
450 public:
DIETypeSignature(const DwarfTypeUnit & Unit)451   explicit DIETypeSignature(const DwarfTypeUnit &Unit)
452       : DIEValue(isTypeSignature), Unit(Unit) {}
453 
454   /// \brief Emit type unit signature.
455   void EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const override;
456 
457   /// Returns size of a ref_sig8 entry.
SizeOf(const AsmPrinter * AP,dwarf::Form Form)458   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override {
459     assert(Form == dwarf::DW_FORM_ref_sig8);
460     return 8;
461   }
462 
463   // \brief Implement isa/cast/dyncast.
classof(const DIEValue * E)464   static bool classof(const DIEValue *E) {
465     return E->getType() == isTypeSignature;
466   }
467 #ifndef NDEBUG
468   void print(raw_ostream &O) const override;
469   void dump() const;
470 #endif
471 };
472 
473 //===--------------------------------------------------------------------===//
474 /// DIELoc - Represents an expression location.
475 //
476 class DIELoc : public DIEValue, public DIE {
477   mutable unsigned Size; // Size in bytes excluding size header.
478 public:
DIELoc()479   DIELoc() : DIEValue(isLoc), Size(0) {}
480 
481   /// ComputeSize - Calculate the size of the location expression.
482   ///
483   unsigned ComputeSize(const AsmPrinter *AP) const;
484 
485   /// BestForm - Choose the best form for data.
486   ///
BestForm(unsigned DwarfVersion)487   dwarf::Form BestForm(unsigned DwarfVersion) const {
488     if (DwarfVersion > 3)
489       return dwarf::DW_FORM_exprloc;
490     // Pre-DWARF4 location expressions were blocks and not exprloc.
491     if ((unsigned char)Size == Size)
492       return dwarf::DW_FORM_block1;
493     if ((unsigned short)Size == Size)
494       return dwarf::DW_FORM_block2;
495     if ((unsigned int)Size == Size)
496       return dwarf::DW_FORM_block4;
497     return dwarf::DW_FORM_block;
498   }
499 
500   /// EmitValue - Emit location data.
501   ///
502   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
503 
504   /// SizeOf - Determine size of location data in bytes.
505   ///
506   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
507 
508   // Implement isa/cast/dyncast.
classof(const DIEValue * E)509   static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
510 
511 #ifndef NDEBUG
512   void print(raw_ostream &O) const override;
513 #endif
514 };
515 
516 //===--------------------------------------------------------------------===//
517 /// DIEBlock - Represents a block of values.
518 //
519 class DIEBlock : public DIEValue, public DIE {
520   mutable unsigned Size; // Size in bytes excluding size header.
521 public:
DIEBlock()522   DIEBlock() : DIEValue(isBlock), Size(0) {}
523 
524   /// ComputeSize - Calculate the size of the location expression.
525   ///
526   unsigned ComputeSize(const AsmPrinter *AP) const;
527 
528   /// BestForm - Choose the best form for data.
529   ///
BestForm()530   dwarf::Form BestForm() const {
531     if ((unsigned char)Size == Size)
532       return dwarf::DW_FORM_block1;
533     if ((unsigned short)Size == Size)
534       return dwarf::DW_FORM_block2;
535     if ((unsigned int)Size == Size)
536       return dwarf::DW_FORM_block4;
537     return dwarf::DW_FORM_block;
538   }
539 
540   /// EmitValue - Emit location data.
541   ///
542   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
543 
544   /// SizeOf - Determine size of location data in bytes.
545   ///
546   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
547 
548   // Implement isa/cast/dyncast.
classof(const DIEValue * E)549   static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
550 
551 #ifndef NDEBUG
552   void print(raw_ostream &O) const override;
553 #endif
554 };
555 
556 //===--------------------------------------------------------------------===//
557 /// DIELocList - Represents a pointer to a location list in the debug_loc
558 /// section.
559 //
560 class DIELocList : public DIEValue {
561   // Index into the .debug_loc vector.
562   size_t Index;
563 
564 public:
DIELocList(size_t I)565   DIELocList(size_t I) : DIEValue(isLocList), Index(I) {}
566 
567   /// getValue - Grab the current index out.
getValue()568   size_t getValue() const { return Index; }
569 
570   /// EmitValue - Emit location data.
571   ///
572   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const override;
573 
574   /// SizeOf - Determine size of location data in bytes.
575   ///
576   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const override;
577 
578   // Implement isa/cast/dyncast.
classof(const DIEValue * E)579   static bool classof(const DIEValue *E) { return E->getType() == isLocList; }
580 
581 #ifndef NDEBUG
582   void print(raw_ostream &O) const override;
583 #endif
584 };
585 
586 } // end llvm namespace
587 
588 #endif
589