LLVM API Documentation

DIE.h
Go to the documentation of this file.
00001 //===--- lib/CodeGen/DIE.h - DWARF Info Entries -----------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Data structures for DWARF info entries.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
00015 #define LLVM_LIB_CODEGEN_ASMPRINTER_DIE_H
00016 
00017 #include "llvm/ADT/FoldingSet.h"
00018 #include "llvm/ADT/SmallVector.h"
00019 #include "llvm/Support/Dwarf.h"
00020 #include <vector>
00021 
00022 namespace llvm {
00023 class AsmPrinter;
00024 class MCExpr;
00025 class MCSymbol;
00026 class raw_ostream;
00027 class DwarfTypeUnit;
00028 
00029 //===--------------------------------------------------------------------===//
00030 /// DIEAbbrevData - Dwarf abbreviation data, describes one attribute of a
00031 /// Dwarf abbreviation.
00032 class DIEAbbrevData {
00033   /// Attribute - Dwarf attribute code.
00034   ///
00035   dwarf::Attribute Attribute;
00036 
00037   /// Form - Dwarf form code.
00038   ///
00039   dwarf::Form Form;
00040 
00041 public:
00042   DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
00043 
00044   // Accessors.
00045   dwarf::Attribute getAttribute() const { return Attribute; }
00046   dwarf::Form getForm() const { return Form; }
00047 
00048   /// Profile - Used to gather unique data for the abbreviation folding set.
00049   ///
00050   void Profile(FoldingSetNodeID &ID) const;
00051 };
00052 
00053 //===--------------------------------------------------------------------===//
00054 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
00055 /// information object.
00056 class DIEAbbrev : public FoldingSetNode {
00057   /// Unique number for node.
00058   ///
00059   unsigned Number;
00060 
00061   /// Tag - Dwarf tag code.
00062   ///
00063   dwarf::Tag Tag;
00064 
00065   /// Children - Whether or not this node has children.
00066   ///
00067   // This cheats a bit in all of the uses since the values in the standard
00068   // are 0 and 1 for no children and children respectively.
00069   bool Children;
00070 
00071   /// Data - Raw data bytes for abbreviation.
00072   ///
00073   SmallVector<DIEAbbrevData, 12> Data;
00074 
00075 public:
00076   DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C), Data() {}
00077 
00078   // Accessors.
00079   dwarf::Tag getTag() const { return Tag; }
00080   unsigned getNumber() const { return Number; }
00081   bool hasChildren() const { return Children; }
00082   const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
00083   void setChildrenFlag(bool hasChild) { Children = hasChild; }
00084   void setNumber(unsigned N) { Number = N; }
00085 
00086   /// AddAttribute - Adds another set of attribute information to the
00087   /// abbreviation.
00088   void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
00089     Data.push_back(DIEAbbrevData(Attribute, Form));
00090   }
00091 
00092   /// Profile - Used to gather unique data for the abbreviation folding set.
00093   ///
00094   void Profile(FoldingSetNodeID &ID) const;
00095 
00096   /// Emit - Print the abbreviation using the specified asm printer.
00097   ///
00098   void Emit(AsmPrinter *AP) const;
00099 
00100 #ifndef NDEBUG
00101   void print(raw_ostream &O);
00102   void dump();
00103 #endif
00104 };
00105 
00106 //===--------------------------------------------------------------------===//
00107 /// DIE - A structured debug information entry.  Has an abbreviation which
00108 /// describes its organization.
00109 class DIEValue;
00110 
00111 class DIE {
00112 protected:
00113   /// Offset - Offset in debug info section.
00114   ///
00115   unsigned Offset;
00116 
00117   /// Size - Size of instance + children.
00118   ///
00119   unsigned Size;
00120 
00121   /// Abbrev - Buffer for constructing abbreviation.
00122   ///
00123   DIEAbbrev Abbrev;
00124 
00125   /// Children DIEs.
00126   ///
00127   // This can't be a vector<DIE> because pointer validity is requirent for the
00128   // Parent pointer and DIEEntry.
00129   // It can't be a list<DIE> because some clients need pointer validity before
00130   // the object has been added to any child list
00131   // (eg: DwarfUnit::constructVariableDIE). These aren't insurmountable, but may
00132   // be more convoluted than beneficial.
00133   std::vector<std::unique_ptr<DIE>> Children;
00134 
00135   DIE *Parent;
00136 
00137   /// Attribute values.
00138   ///
00139   SmallVector<DIEValue *, 12> Values;
00140 
00141 protected:
00142   DIE()
00143       : Offset(0), Size(0), Abbrev((dwarf::Tag)0, dwarf::DW_CHILDREN_no),
00144         Parent(nullptr) {}
00145 
00146 public:
00147   explicit DIE(dwarf::Tag Tag)
00148       : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
00149         Parent(nullptr) {}
00150 
00151   // Accessors.
00152   DIEAbbrev &getAbbrev() { return Abbrev; }
00153   const DIEAbbrev &getAbbrev() const { return Abbrev; }
00154   unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
00155   dwarf::Tag getTag() const { return Abbrev.getTag(); }
00156   unsigned getOffset() const { return Offset; }
00157   unsigned getSize() const { return Size; }
00158   const std::vector<std::unique_ptr<DIE>> &getChildren() const {
00159     return Children;
00160   }
00161   const SmallVectorImpl<DIEValue *> &getValues() const { return Values; }
00162   DIE *getParent() const { return Parent; }
00163   /// Climb up the parent chain to get the compile or type unit DIE this DIE
00164   /// belongs to.
00165   const DIE *getUnit() const;
00166   /// Similar to getUnit, returns null when DIE is not added to an
00167   /// owner yet.
00168   const DIE *getUnitOrNull() const;
00169   void setOffset(unsigned O) { Offset = O; }
00170   void setSize(unsigned S) { Size = S; }
00171 
00172   /// addValue - Add a value and attributes to a DIE.
00173   ///
00174   void addValue(dwarf::Attribute Attribute, dwarf::Form Form, DIEValue *Value) {
00175     Abbrev.AddAttribute(Attribute, Form);
00176     Values.push_back(Value);
00177   }
00178 
00179   /// addChild - Add a child to the DIE.
00180   ///
00181   void addChild(std::unique_ptr<DIE> Child) {
00182     assert(!Child->getParent());
00183     Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
00184     Child->Parent = this;
00185     Children.push_back(std::move(Child));
00186   }
00187 
00188   /// findAttribute - Find a value in the DIE with the attribute given,
00189   /// returns NULL if no such attribute exists.
00190   DIEValue *findAttribute(dwarf::Attribute Attribute) const;
00191 
00192 #ifndef NDEBUG
00193   void print(raw_ostream &O, unsigned IndentCount = 0) const;
00194   void dump();
00195 #endif
00196 };
00197 
00198 //===--------------------------------------------------------------------===//
00199 /// DIEValue - A debug information entry value. Some of these roughly correlate
00200 /// to DWARF attribute classes.
00201 ///
00202 class DIEValue {
00203   virtual void anchor();
00204 
00205 public:
00206   enum Type {
00207     isInteger,
00208     isString,
00209     isExpr,
00210     isLabel,
00211     isDelta,
00212     isEntry,
00213     isTypeSignature,
00214     isBlock,
00215     isLoc,
00216     isLocList,
00217   };
00218 
00219 protected:
00220   /// Ty - Type of data stored in the value.
00221   ///
00222   Type Ty;
00223 
00224   explicit DIEValue(Type T) : Ty(T) {}
00225   virtual ~DIEValue() {}
00226 
00227 public:
00228   // Accessors
00229   Type getType() const { return Ty; }
00230 
00231   /// EmitValue - Emit value via the Dwarf writer.
00232   ///
00233   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
00234 
00235   /// SizeOf - Return the size of a value in bytes.
00236   ///
00237   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
00238 
00239 #ifndef NDEBUG
00240   virtual void print(raw_ostream &O) const = 0;
00241   void dump() const;
00242 #endif
00243 };
00244 
00245 //===--------------------------------------------------------------------===//
00246 /// DIEInteger - An integer value DIE.
00247 ///
00248 class DIEInteger : public DIEValue {
00249   uint64_t Integer;
00250 
00251 public:
00252   explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
00253 
00254   /// BestForm - Choose the best form for integer.
00255   ///
00256   static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
00257     if (IsSigned) {
00258       const int64_t SignedInt = Int;
00259       if ((char)Int == SignedInt)
00260         return dwarf::DW_FORM_data1;
00261       if ((short)Int == SignedInt)
00262         return dwarf::DW_FORM_data2;
00263       if ((int)Int == SignedInt)
00264         return dwarf::DW_FORM_data4;
00265     } else {
00266       if ((unsigned char)Int == Int)
00267         return dwarf::DW_FORM_data1;
00268       if ((unsigned short)Int == Int)
00269         return dwarf::DW_FORM_data2;
00270       if ((unsigned int)Int == Int)
00271         return dwarf::DW_FORM_data4;
00272     }
00273     return dwarf::DW_FORM_data8;
00274   }
00275 
00276   /// EmitValue - Emit integer of appropriate size.
00277   ///
00278   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00279 
00280   uint64_t getValue() const { return Integer; }
00281 
00282   /// SizeOf - Determine size of integer value in bytes.
00283   ///
00284   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00285 
00286   // Implement isa/cast/dyncast.
00287   static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
00288 
00289 #ifndef NDEBUG
00290   void print(raw_ostream &O) const override;
00291 #endif
00292 };
00293 
00294 //===--------------------------------------------------------------------===//
00295 /// DIEExpr - An expression DIE.
00296 //
00297 class DIEExpr : public DIEValue {
00298   const MCExpr *Expr;
00299 
00300 public:
00301   explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
00302 
00303   /// EmitValue - Emit expression value.
00304   ///
00305   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00306 
00307   /// getValue - Get MCExpr.
00308   ///
00309   const MCExpr *getValue() const { return Expr; }
00310 
00311   /// SizeOf - Determine size of expression value in bytes.
00312   ///
00313   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00314 
00315   // Implement isa/cast/dyncast.
00316   static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
00317 
00318 #ifndef NDEBUG
00319   void print(raw_ostream &O) const override;
00320 #endif
00321 };
00322 
00323 //===--------------------------------------------------------------------===//
00324 /// DIELabel - A label DIE.
00325 //
00326 class DIELabel : public DIEValue {
00327   const MCSymbol *Label;
00328 
00329 public:
00330   explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
00331 
00332   /// EmitValue - Emit label value.
00333   ///
00334   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00335 
00336   /// getValue - Get MCSymbol.
00337   ///
00338   const MCSymbol *getValue() const { return Label; }
00339 
00340   /// SizeOf - Determine size of label value in bytes.
00341   ///
00342   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00343 
00344   // Implement isa/cast/dyncast.
00345   static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
00346 
00347 #ifndef NDEBUG
00348   void print(raw_ostream &O) const override;
00349 #endif
00350 };
00351 
00352 //===--------------------------------------------------------------------===//
00353 /// DIEDelta - A simple label difference DIE.
00354 ///
00355 class DIEDelta : public DIEValue {
00356   const MCSymbol *LabelHi;
00357   const MCSymbol *LabelLo;
00358 
00359 public:
00360   DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
00361       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
00362 
00363   /// EmitValue - Emit delta value.
00364   ///
00365   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00366 
00367   /// SizeOf - Determine size of delta value in bytes.
00368   ///
00369   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00370 
00371   // Implement isa/cast/dyncast.
00372   static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
00373 
00374 #ifndef NDEBUG
00375   void print(raw_ostream &O) const override;
00376 #endif
00377 };
00378 
00379 //===--------------------------------------------------------------------===//
00380 /// DIEString - A container for string values.
00381 ///
00382 class DIEString : public DIEValue {
00383   const DIEValue *Access;
00384   StringRef Str;
00385 
00386 public:
00387   DIEString(const DIEValue *Acc, StringRef S)
00388       : DIEValue(isString), Access(Acc), Str(S) {}
00389 
00390   /// getString - Grab the string out of the object.
00391   StringRef getString() const { return Str; }
00392 
00393   /// EmitValue - Emit delta value.
00394   ///
00395   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00396 
00397   /// SizeOf - Determine size of delta value in bytes.
00398   ///
00399   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00400 
00401   // Implement isa/cast/dyncast.
00402   static bool classof(const DIEValue *D) { return D->getType() == isString; }
00403 
00404 #ifndef NDEBUG
00405   void print(raw_ostream &O) const override;
00406 #endif
00407 };
00408 
00409 //===--------------------------------------------------------------------===//
00410 /// DIEEntry - A pointer to another debug information entry.  An instance of
00411 /// this class can also be used as a proxy for a debug information entry not
00412 /// yet defined (ie. types.)
00413 class DIEEntry : public DIEValue {
00414   DIE &Entry;
00415 
00416 public:
00417   explicit DIEEntry(DIE &E) : DIEValue(isEntry), Entry(E) {
00418   }
00419 
00420   DIE &getEntry() const { return Entry; }
00421 
00422   /// EmitValue - Emit debug information entry offset.
00423   ///
00424   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00425 
00426   /// SizeOf - Determine size of debug information entry in bytes.
00427   ///
00428    unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override {
00429     return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
00430                                            : sizeof(int32_t);
00431   }
00432 
00433   /// Returns size of a ref_addr entry.
00434   static unsigned getRefAddrSize(AsmPrinter *AP);
00435 
00436   // Implement isa/cast/dyncast.
00437   static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
00438 
00439 #ifndef NDEBUG
00440   void print(raw_ostream &O) const override;
00441 #endif
00442 };
00443 
00444 //===--------------------------------------------------------------------===//
00445 /// \brief A signature reference to a type unit.
00446 class DIETypeSignature : public DIEValue {
00447   const DwarfTypeUnit &Unit;
00448 
00449 public:
00450   explicit DIETypeSignature(const DwarfTypeUnit &Unit)
00451       : DIEValue(isTypeSignature), Unit(Unit) {}
00452 
00453   /// \brief Emit type unit signature.
00454   void EmitValue(AsmPrinter *Asm, dwarf::Form Form) const override;
00455 
00456   /// Returns size of a ref_sig8 entry.
00457   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override {
00458     assert(Form == dwarf::DW_FORM_ref_sig8);
00459     return 8;
00460   }
00461 
00462   // \brief Implement isa/cast/dyncast.
00463   static bool classof(const DIEValue *E) {
00464     return E->getType() == isTypeSignature;
00465   }
00466 #ifndef NDEBUG
00467   void print(raw_ostream &O) const override;
00468   void dump() const;
00469 #endif
00470 };
00471 
00472 //===--------------------------------------------------------------------===//
00473 /// DIELoc - Represents an expression location.
00474 //
00475 class DIELoc : public DIEValue, public DIE {
00476   mutable unsigned Size; // Size in bytes excluding size header.
00477 public:
00478   DIELoc() : DIEValue(isLoc), Size(0) {}
00479 
00480   /// ComputeSize - Calculate the size of the location expression.
00481   ///
00482   unsigned ComputeSize(AsmPrinter *AP) const;
00483 
00484   /// BestForm - Choose the best form for data.
00485   ///
00486   dwarf::Form BestForm(unsigned DwarfVersion) const {
00487     if (DwarfVersion > 3)
00488       return dwarf::DW_FORM_exprloc;
00489     // Pre-DWARF4 location expressions were blocks and not exprloc.
00490     if ((unsigned char)Size == Size)
00491       return dwarf::DW_FORM_block1;
00492     if ((unsigned short)Size == Size)
00493       return dwarf::DW_FORM_block2;
00494     if ((unsigned int)Size == Size)
00495       return dwarf::DW_FORM_block4;
00496     return dwarf::DW_FORM_block;
00497   }
00498 
00499   /// EmitValue - Emit location data.
00500   ///
00501   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00502 
00503   /// SizeOf - Determine size of location data in bytes.
00504   ///
00505   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00506 
00507   // Implement isa/cast/dyncast.
00508   static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
00509 
00510 #ifndef NDEBUG
00511   void print(raw_ostream &O) const override;
00512 #endif
00513 };
00514 
00515 //===--------------------------------------------------------------------===//
00516 /// DIEBlock - Represents a block of values.
00517 //
00518 class DIEBlock : public DIEValue, public DIE {
00519   mutable unsigned Size; // Size in bytes excluding size header.
00520 public:
00521   DIEBlock() : DIEValue(isBlock), Size(0) {}
00522 
00523   /// ComputeSize - Calculate the size of the location expression.
00524   ///
00525   unsigned ComputeSize(AsmPrinter *AP) const;
00526 
00527   /// BestForm - Choose the best form for data.
00528   ///
00529   dwarf::Form BestForm() const {
00530     if ((unsigned char)Size == Size)
00531       return dwarf::DW_FORM_block1;
00532     if ((unsigned short)Size == Size)
00533       return dwarf::DW_FORM_block2;
00534     if ((unsigned int)Size == Size)
00535       return dwarf::DW_FORM_block4;
00536     return dwarf::DW_FORM_block;
00537   }
00538 
00539   /// EmitValue - Emit location data.
00540   ///
00541   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00542 
00543   /// SizeOf - Determine size of location data in bytes.
00544   ///
00545   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00546 
00547   // Implement isa/cast/dyncast.
00548   static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
00549 
00550 #ifndef NDEBUG
00551   void print(raw_ostream &O) const override;
00552 #endif
00553 };
00554 
00555 //===--------------------------------------------------------------------===//
00556 /// DIELocList - Represents a pointer to a location list in the debug_loc
00557 /// section.
00558 //
00559 class DIELocList : public DIEValue {
00560   // Index into the .debug_loc vector.
00561   size_t Index;
00562 
00563 public:
00564   DIELocList(size_t I) : DIEValue(isLocList), Index(I) {}
00565 
00566   /// getValue - Grab the current index out.
00567   size_t getValue() const { return Index; }
00568 
00569   /// EmitValue - Emit location data.
00570   ///
00571   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00572 
00573   /// SizeOf - Determine size of location data in bytes.
00574   ///
00575   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00576 
00577   // Implement isa/cast/dyncast.
00578   static bool classof(const DIEValue *E) { return E->getType() == isLocList; }
00579 
00580 #ifndef NDEBUG
00581   void print(raw_ostream &O) const override;
00582 #endif
00583 };
00584 
00585 } // end llvm namespace
00586 
00587 #endif