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 CODEGEN_ASMPRINTER_DIE_H__
00015 #define 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   std::vector<std::unique_ptr<DIE>> Children;
00128 
00129   DIE *Parent;
00130 
00131   /// Attribute values.
00132   ///
00133   SmallVector<DIEValue *, 12> Values;
00134 
00135 protected:
00136   DIE()
00137       : Offset(0), Size(0), Abbrev((dwarf::Tag)0, dwarf::DW_CHILDREN_no),
00138         Parent(0) {}
00139 
00140 public:
00141   explicit DIE(dwarf::Tag Tag)
00142       : Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
00143         Parent(0) {}
00144 
00145   // Accessors.
00146   DIEAbbrev &getAbbrev() { return Abbrev; }
00147   const DIEAbbrev &getAbbrev() const { return Abbrev; }
00148   unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
00149   dwarf::Tag getTag() const { return Abbrev.getTag(); }
00150   unsigned getOffset() const { return Offset; }
00151   unsigned getSize() const { return Size; }
00152   const std::vector<std::unique_ptr<DIE>> &getChildren() const {
00153     return Children;
00154   }
00155   const SmallVectorImpl<DIEValue *> &getValues() const { return Values; }
00156   DIE *getParent() const { return Parent; }
00157   /// Climb up the parent chain to get the compile or type unit DIE this DIE
00158   /// belongs to.
00159   const DIE *getUnit() const;
00160   /// Similar to getUnit, returns null when DIE is not added to an
00161   /// owner yet.
00162   const DIE *getUnitOrNull() const;
00163   void setOffset(unsigned O) { Offset = O; }
00164   void setSize(unsigned S) { Size = S; }
00165 
00166   /// addValue - Add a value and attributes to a DIE.
00167   ///
00168   void addValue(dwarf::Attribute Attribute, dwarf::Form Form, DIEValue *Value) {
00169     Abbrev.AddAttribute(Attribute, Form);
00170     Values.push_back(Value);
00171   }
00172 
00173   /// addChild - Add a child to the DIE.
00174   ///
00175   void addChild(DIE *Child) {
00176     assert(!Child->getParent());
00177     Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
00178     Children.push_back(std::unique_ptr<DIE>(Child));
00179     Child->Parent = this;
00180   }
00181 
00182   /// findAttribute - Find a value in the DIE with the attribute given,
00183   /// returns NULL if no such attribute exists.
00184   DIEValue *findAttribute(dwarf::Attribute Attribute) const;
00185 
00186 #ifndef NDEBUG
00187   void print(raw_ostream &O, unsigned IndentCount = 0) const;
00188   void dump();
00189 #endif
00190 };
00191 
00192 //===--------------------------------------------------------------------===//
00193 /// DIEValue - A debug information entry value. Some of these roughly correlate
00194 /// to DWARF attribute classes.
00195 ///
00196 class DIEValue {
00197   virtual void anchor();
00198 
00199 public:
00200   enum Type {
00201     isInteger,
00202     isString,
00203     isExpr,
00204     isLabel,
00205     isDelta,
00206     isEntry,
00207     isTypeSignature,
00208     isBlock,
00209     isLoc,
00210     isLocList,
00211   };
00212 
00213 protected:
00214   /// Ty - Type of data stored in the value.
00215   ///
00216   Type Ty;
00217 
00218   explicit DIEValue(Type T) : Ty(T) {}
00219   virtual ~DIEValue() {}
00220 
00221 public:
00222   // Accessors
00223   Type getType() const { return Ty; }
00224 
00225   /// EmitValue - Emit value via the Dwarf writer.
00226   ///
00227   virtual void EmitValue(AsmPrinter *AP, dwarf::Form Form) const = 0;
00228 
00229   /// SizeOf - Return the size of a value in bytes.
00230   ///
00231   virtual unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const = 0;
00232 
00233 #ifndef NDEBUG
00234   virtual void print(raw_ostream &O) const = 0;
00235   void dump() const;
00236 #endif
00237 };
00238 
00239 //===--------------------------------------------------------------------===//
00240 /// DIEInteger - An integer value DIE.
00241 ///
00242 class DIEInteger : public DIEValue {
00243   uint64_t Integer;
00244 
00245 public:
00246   explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
00247 
00248   /// BestForm - Choose the best form for integer.
00249   ///
00250   static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
00251     if (IsSigned) {
00252       const int64_t SignedInt = Int;
00253       if ((char)Int == SignedInt)
00254         return dwarf::DW_FORM_data1;
00255       if ((short)Int == SignedInt)
00256         return dwarf::DW_FORM_data2;
00257       if ((int)Int == SignedInt)
00258         return dwarf::DW_FORM_data4;
00259     } else {
00260       if ((unsigned char)Int == Int)
00261         return dwarf::DW_FORM_data1;
00262       if ((unsigned short)Int == Int)
00263         return dwarf::DW_FORM_data2;
00264       if ((unsigned int)Int == Int)
00265         return dwarf::DW_FORM_data4;
00266     }
00267     return dwarf::DW_FORM_data8;
00268   }
00269 
00270   /// EmitValue - Emit integer of appropriate size.
00271   ///
00272   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00273 
00274   uint64_t getValue() const { return Integer; }
00275 
00276   /// SizeOf - Determine size of integer value in bytes.
00277   ///
00278   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00279 
00280   // Implement isa/cast/dyncast.
00281   static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
00282 
00283 #ifndef NDEBUG
00284   void print(raw_ostream &O) const override;
00285 #endif
00286 };
00287 
00288 //===--------------------------------------------------------------------===//
00289 /// DIEExpr - An expression DIE.
00290 //
00291 class DIEExpr : public DIEValue {
00292   const MCExpr *Expr;
00293 
00294 public:
00295   explicit DIEExpr(const MCExpr *E) : DIEValue(isExpr), Expr(E) {}
00296 
00297   /// EmitValue - Emit expression value.
00298   ///
00299   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00300 
00301   /// getValue - Get MCExpr.
00302   ///
00303   const MCExpr *getValue() const { return Expr; }
00304 
00305   /// SizeOf - Determine size of expression value in bytes.
00306   ///
00307   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00308 
00309   // Implement isa/cast/dyncast.
00310   static bool classof(const DIEValue *E) { return E->getType() == isExpr; }
00311 
00312 #ifndef NDEBUG
00313   void print(raw_ostream &O) const override;
00314 #endif
00315 };
00316 
00317 //===--------------------------------------------------------------------===//
00318 /// DIELabel - A label DIE.
00319 //
00320 class DIELabel : public DIEValue {
00321   const MCSymbol *Label;
00322 
00323 public:
00324   explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
00325 
00326   /// EmitValue - Emit label value.
00327   ///
00328   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00329 
00330   /// getValue - Get MCSymbol.
00331   ///
00332   const MCSymbol *getValue() const { return Label; }
00333 
00334   /// SizeOf - Determine size of label value in bytes.
00335   ///
00336   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00337 
00338   // Implement isa/cast/dyncast.
00339   static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
00340 
00341 #ifndef NDEBUG
00342   void print(raw_ostream &O) const override;
00343 #endif
00344 };
00345 
00346 //===--------------------------------------------------------------------===//
00347 /// DIEDelta - A simple label difference DIE.
00348 ///
00349 class DIEDelta : public DIEValue {
00350   const MCSymbol *LabelHi;
00351   const MCSymbol *LabelLo;
00352 
00353 public:
00354   DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
00355       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
00356 
00357   /// EmitValue - Emit delta value.
00358   ///
00359   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00360 
00361   /// SizeOf - Determine size of delta value in bytes.
00362   ///
00363   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00364 
00365   // Implement isa/cast/dyncast.
00366   static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
00367 
00368 #ifndef NDEBUG
00369   void print(raw_ostream &O) const override;
00370 #endif
00371 };
00372 
00373 //===--------------------------------------------------------------------===//
00374 /// DIEString - A container for string values.
00375 ///
00376 class DIEString : public DIEValue {
00377   const DIEValue *Access;
00378   const StringRef Str;
00379 
00380 public:
00381   DIEString(const DIEValue *Acc, const StringRef S)
00382       : DIEValue(isString), Access(Acc), Str(S) {}
00383 
00384   /// getString - Grab the string out of the object.
00385   StringRef getString() const { return Str; }
00386 
00387   /// EmitValue - Emit delta value.
00388   ///
00389   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00390 
00391   /// SizeOf - Determine size of delta value in bytes.
00392   ///
00393   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00394 
00395   // Implement isa/cast/dyncast.
00396   static bool classof(const DIEValue *D) { return D->getType() == isString; }
00397 
00398 #ifndef NDEBUG
00399   void print(raw_ostream &O) const override;
00400 #endif
00401 };
00402 
00403 //===--------------------------------------------------------------------===//
00404 /// DIEEntry - A pointer to another debug information entry.  An instance of
00405 /// this class can also be used as a proxy for a debug information entry not
00406 /// yet defined (ie. types.)
00407 class DIEEntry : public DIEValue {
00408   DIE *const Entry;
00409 
00410 public:
00411   explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {
00412     assert(E && "Cannot construct a DIEEntry with a null DIE");
00413   }
00414 
00415   DIE *getEntry() const { return Entry; }
00416 
00417   /// EmitValue - Emit debug information entry offset.
00418   ///
00419   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00420 
00421   /// SizeOf - Determine size of debug information entry in bytes.
00422   ///
00423    unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override {
00424     return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
00425                                            : sizeof(int32_t);
00426   }
00427 
00428   /// Returns size of a ref_addr entry.
00429   static unsigned getRefAddrSize(AsmPrinter *AP);
00430 
00431   // Implement isa/cast/dyncast.
00432   static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
00433 
00434 #ifndef NDEBUG
00435   void print(raw_ostream &O) const override;
00436 #endif
00437 };
00438 
00439 //===--------------------------------------------------------------------===//
00440 /// \brief A signature reference to a type unit.
00441 class DIETypeSignature : public DIEValue {
00442   const DwarfTypeUnit &Unit;
00443 
00444 public:
00445   explicit DIETypeSignature(const DwarfTypeUnit &Unit)
00446       : DIEValue(isTypeSignature), Unit(Unit) {}
00447 
00448   /// \brief Emit type unit signature.
00449   void EmitValue(AsmPrinter *Asm, dwarf::Form Form) const override;
00450 
00451   /// Returns size of a ref_sig8 entry.
00452   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override {
00453     assert(Form == dwarf::DW_FORM_ref_sig8);
00454     return 8;
00455   }
00456 
00457   // \brief Implement isa/cast/dyncast.
00458   static bool classof(const DIEValue *E) {
00459     return E->getType() == isTypeSignature;
00460   }
00461 #ifndef NDEBUG
00462   void print(raw_ostream &O) const override;
00463   void dump() const;
00464 #endif
00465 };
00466 
00467 //===--------------------------------------------------------------------===//
00468 /// DIELoc - Represents an expression location.
00469 //
00470 class DIELoc : public DIEValue, public DIE {
00471   mutable unsigned Size; // Size in bytes excluding size header.
00472 public:
00473   DIELoc() : DIEValue(isLoc), Size(0) {}
00474 
00475   /// ComputeSize - Calculate the size of the location expression.
00476   ///
00477   unsigned ComputeSize(AsmPrinter *AP) const;
00478 
00479   /// BestForm - Choose the best form for data.
00480   ///
00481   dwarf::Form BestForm(unsigned DwarfVersion) const {
00482     if (DwarfVersion > 3)
00483       return dwarf::DW_FORM_exprloc;
00484     // Pre-DWARF4 location expressions were blocks and not exprloc.
00485     if ((unsigned char)Size == Size)
00486       return dwarf::DW_FORM_block1;
00487     if ((unsigned short)Size == Size)
00488       return dwarf::DW_FORM_block2;
00489     if ((unsigned int)Size == Size)
00490       return dwarf::DW_FORM_block4;
00491     return dwarf::DW_FORM_block;
00492   }
00493 
00494   /// EmitValue - Emit location data.
00495   ///
00496   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00497 
00498   /// SizeOf - Determine size of location data in bytes.
00499   ///
00500   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00501 
00502   // Implement isa/cast/dyncast.
00503   static bool classof(const DIEValue *E) { return E->getType() == isLoc; }
00504 
00505 #ifndef NDEBUG
00506   void print(raw_ostream &O) const override;
00507 #endif
00508 };
00509 
00510 //===--------------------------------------------------------------------===//
00511 /// DIEBlock - Represents a block of values.
00512 //
00513 class DIEBlock : public DIEValue, public DIE {
00514   mutable unsigned Size; // Size in bytes excluding size header.
00515 public:
00516   DIEBlock() : DIEValue(isBlock), Size(0) {}
00517 
00518   /// ComputeSize - Calculate the size of the location expression.
00519   ///
00520   unsigned ComputeSize(AsmPrinter *AP) const;
00521 
00522   /// BestForm - Choose the best form for data.
00523   ///
00524   dwarf::Form BestForm() const {
00525     if ((unsigned char)Size == Size)
00526       return dwarf::DW_FORM_block1;
00527     if ((unsigned short)Size == Size)
00528       return dwarf::DW_FORM_block2;
00529     if ((unsigned int)Size == Size)
00530       return dwarf::DW_FORM_block4;
00531     return dwarf::DW_FORM_block;
00532   }
00533 
00534   /// EmitValue - Emit location data.
00535   ///
00536   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00537 
00538   /// SizeOf - Determine size of location data in bytes.
00539   ///
00540   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00541 
00542   // Implement isa/cast/dyncast.
00543   static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
00544 
00545 #ifndef NDEBUG
00546   void print(raw_ostream &O) const override;
00547 #endif
00548 };
00549 
00550 //===--------------------------------------------------------------------===//
00551 /// DIELocList - Represents a pointer to a location list in the debug_loc
00552 /// section.
00553 //
00554 class DIELocList : public DIEValue {
00555   // Index into the .debug_loc vector.
00556   size_t Index;
00557 
00558 public:
00559   DIELocList(size_t I) : DIEValue(isLocList), Index(I) {}
00560 
00561   /// getValue - Grab the current index out.
00562   size_t getValue() const { return Index; }
00563 
00564   /// EmitValue - Emit location data.
00565   ///
00566   void EmitValue(AsmPrinter *AP, dwarf::Form Form) const override;
00567 
00568   /// SizeOf - Determine size of location data in bytes.
00569   ///
00570   unsigned SizeOf(AsmPrinter *AP, dwarf::Form Form) const override;
00571 
00572   // Implement isa/cast/dyncast.
00573   static bool classof(const DIEValue *E) { return E->getType() == isLocList; }
00574 
00575 #ifndef NDEBUG
00576   void print(raw_ostream &O) const override;
00577 #endif
00578 };
00579 
00580 } // end llvm namespace
00581 
00582 #endif