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(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(AsmPrinter *AP, dwarf::Form Form) const = 0;
234 
235   /// SizeOf - Return the size of a value in bytes.
236   ///
237   virtual unsigned SizeOf(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(AsmPrinter *AP, dwarf::Form Form) const override;
279 
getValue()280   uint64_t getValue() const { return Integer; }
281 
282   /// SizeOf - Determine size of integer value in bytes.
283   ///
284   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
285 
286   // Implement isa/cast/dyncast.
classof(const DIEValue * I)287   static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
288 
289 #ifndef NDEBUG
290   void print(raw_ostream &O) const override;
291 #endif
292 };
293 
294 //===--------------------------------------------------------------------===//
295 /// DIEExpr - An expression DIE.
296 //
297 class DIEExpr : public DIEValue {
298   const MCExpr *Expr;
299 
300 public:
DIEExpr(const MCExpr * E)301   explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
302 
303   /// EmitValue - Emit expression value.
304   ///
305   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
306 
307   /// getValue - Get MCExpr.
308   ///
getValue()309   const MCExpr *getValue() const { return Expr; }
310 
311   /// SizeOf - Determine size of expression value in bytes.
312   ///
313   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
314 
315   // Implement isa/cast/dyncast.
classof(const DIEValue * E)316   static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
317 
318 #ifndef NDEBUG
319   void print(raw_ostream &O) const override;
320 #endif
321 };
322 
323 //===--------------------------------------------------------------------===//
324 /// DIELabel - A label DIE.
325 //
326 class DIELabel : public DIEValue {
327   const MCSymbol *Label;
328 
329 public:
DIELabel(const MCSymbol * L)330   explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
331 
332   /// EmitValue - Emit label value.
333   ///
334   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
335 
336   /// getValue - Get MCSymbol.
337   ///
getValue()338   const MCSymbol *getValue() const { return Label; }
339 
340   /// SizeOf - Determine size of label value in bytes.
341   ///
342   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
343 
344   // Implement isa/cast/dyncast.
classof(const DIEValue * L)345   static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
346 
347 #ifndef NDEBUG
348   void print(raw_ostream &O) const override;
349 #endif
350 };
351 
352 //===--------------------------------------------------------------------===//
353 /// DIEDelta - A simple label difference DIE.
354 ///
355 class DIEDelta : public DIEValue {
356   const MCSymbol *LabelHi;
357   const MCSymbol *LabelLo;
358 
359 public:
DIEDelta(const MCSymbol * Hi,const MCSymbol * Lo)360   DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
361       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
362 
363   /// EmitValue - Emit delta value.
364   ///
365   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
366 
367   /// SizeOf - Determine size of delta value in bytes.
368   ///
369   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
370 
371   // Implement isa/cast/dyncast.
classof(const DIEValue * D)372   static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
373 
374 #ifndef NDEBUG
375   void print(raw_ostream &O) const override;
376 #endif
377 };
378 
379 //===--------------------------------------------------------------------===//
380 /// DIEString - A container for string values.
381 ///
382 class DIEString : public DIEValue {
383   const DIEValue *Access;
384   StringRef Str;
385 
386 public:
DIEString(const DIEValue * Acc,StringRef S)387   DIEString(const DIEValue *Acc, StringRef S)
388       : DIEValue(isString), Access(Acc), Str(S) {}
389 
390   /// getString - Grab the string out of the object.
getString()391   StringRef getString() const { return Str; }
392 
393   /// EmitValue - Emit delta value.
394   ///
395   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
396 
397   /// SizeOf - Determine size of delta value in bytes.
398   ///
399   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
400 
401   // Implement isa/cast/dyncast.
classof(const DIEValue * D)402   static bool classof(const DIEValue *D) { return D->getType() == isString; }
403 
404 #ifndef NDEBUG
405   void print(raw_ostream &O) const override;
406 #endif
407 };
408 
409 //===--------------------------------------------------------------------===//
410 /// DIEEntry - A pointer to another debug information entry.  An instance of
411 /// this class can also be used as a proxy for a debug information entry not
412 /// yet defined (ie. types.)
413 class DIEEntry : public DIEValue {
414   DIE &Entry;
415 
416 public:
DIEEntry(DIE & E)417   explicit DIEEntry(DIE &E) : DIEValue(isEntry), Entry(E) {
418   }
419 
getEntry()420   DIE &getEntry() const { return Entry; }
421 
422   /// EmitValue - Emit debug information entry offset.
423   ///
424   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
425 
426   /// SizeOf - Determine size of debug information entry in bytes.
427   ///
SizeOf(AsmPrinter * AP,dwarf::Form Form)428    unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override {
429     return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
430                                            : sizeof(int32_t);
431   }
432 
433   /// Returns size of a ref_addr entry.
434   static unsigned getRefAddrSize(AsmPrinter *AP);
435 
436   // Implement isa/cast/dyncast.
classof(const DIEValue * E)437   static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
438 
439 #ifndef NDEBUG
440   void print(raw_ostream &O) const override;
441 #endif
442 };
443 
444 //===--------------------------------------------------------------------===//
445 /// \brief A signature reference to a type unit.
446 class DIETypeSignature : public DIEValue {
447   const DwarfTypeUnit &Unit;
448 
449 public:
DIETypeSignature(const DwarfTypeUnit & Unit)450   explicit DIETypeSignature(const DwarfTypeUnit &Unit)
451       : DIEValue(isTypeSignature), Unit(Unit) {}
452 
453   /// \brief Emit type unit signature.
454   void EmitValue(AsmPrinter *Asm, dwarf::Form Form) const override;
455 
456   /// Returns size of a ref_sig8 entry.
SizeOf(AsmPrinter * AP,dwarf::Form Form)457   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override {
458     assert(Form == dwarf::DW_FORM_ref_sig8);
459     return 8;
460   }
461 
462   // \brief Implement isa/cast/dyncast.
classof(const DIEValue * E)463   static bool classof(const DIEValue *E) {
464     return E->getType() == isTypeSignature;
465   }
466 #ifndef NDEBUG
467   void print(raw_ostream &O) const override;
468   void dump() const;
469 #endif
470 };
471 
472 //===--------------------------------------------------------------------===//
473 /// DIELoc - Represents an expression location.
474 //
475 class DIELoc : public DIEValue, public DIE {
476   mutable unsigned Size; // Size in bytes excluding size header.
477 public:
DIELoc()478   DIELoc() : DIEValue(isLoc), Size(0) {}
479 
480   /// ComputeSize - Calculate the size of the location expression.
481   ///
482   unsigned ComputeSize(AsmPrinter *AP) const;
483 
484   /// BestForm - Choose the best form for data.
485   ///
BestForm(unsigned DwarfVersion)486   dwarf::Form BestForm(unsigned DwarfVersion) const {
487     if (DwarfVersion > 3)
488       return dwarf::DW_FORM_exprloc;
489     // Pre-DWARF4 location expressions were blocks and not exprloc.
490     if ((unsigned char)Size == Size)
491       return dwarf::DW_FORM_block1;
492     if ((unsigned short)Size == Size)
493       return dwarf::DW_FORM_block2;
494     if ((unsigned int)Size == Size)
495       return dwarf::DW_FORM_block4;
496     return dwarf::DW_FORM_block;
497   }
498 
499   /// EmitValue - Emit location data.
500   ///
501   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
502 
503   /// SizeOf - Determine size of location data in bytes.
504   ///
505   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
506 
507   // Implement isa/cast/dyncast.
classof(const DIEValue * E)508   static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
509 
510 #ifndef NDEBUG
511   void print(raw_ostream &O) const override;
512 #endif
513 };
514 
515 //===--------------------------------------------------------------------===//
516 /// DIEBlock - Represents a block of values.
517 //
518 class DIEBlock : public DIEValue, public DIE {
519   mutable unsigned Size; // Size in bytes excluding size header.
520 public:
DIEBlock()521   DIEBlock() : DIEValue(isBlock), Size(0) {}
522 
523   /// ComputeSize - Calculate the size of the location expression.
524   ///
525   unsigned ComputeSize(AsmPrinter *AP) const;
526 
527   /// BestForm - Choose the best form for data.
528   ///
BestForm()529   dwarf::Form BestForm() const {
530     if ((unsigned char)Size == Size)
531       return dwarf::DW_FORM_block1;
532     if ((unsigned short)Size == Size)
533       return dwarf::DW_FORM_block2;
534     if ((unsigned int)Size == Size)
535       return dwarf::DW_FORM_block4;
536     return dwarf::DW_FORM_block;
537   }
538 
539   /// EmitValue - Emit location data.
540   ///
541   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
542 
543   /// SizeOf - Determine size of location data in bytes.
544   ///
545   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
546 
547   // Implement isa/cast/dyncast.
classof(const DIEValue * E)548   static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
549 
550 #ifndef NDEBUG
551   void print(raw_ostream &O) const override;
552 #endif
553 };
554 
555 //===--------------------------------------------------------------------===//
556 /// DIELocList - Represents a pointer to a location list in the debug_loc
557 /// section.
558 //
559 class DIELocList : public DIEValue {
560   // Index into the .debug_loc vector.
561   size_t Index;
562 
563 public:
DIELocList(size_t I)564   DIELocList(size_t I) : DIEValue(isLocList), Index(I) {}
565 
566   /// getValue - Grab the current index out.
getValue()567   size_t getValue() const { return Index; }
568 
569   /// EmitValue - Emit location data.
570   ///
571   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
572 
573   /// SizeOf - Determine size of location data in bytes.
574   ///
575   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
576 
577   // Implement isa/cast/dyncast.
classof(const DIEValue * E)578   static bool classof(const DIEValue *E) { return E->getType() == isLocList; }
579 
580 #ifndef NDEBUG
581   void print(raw_ostream &O) const override;
582 #endif
583 };
584 
585 } // end llvm namespace
586 
587 #endif
588