LLVM  mainline
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/CodeGen/DwarfStringPoolEntry.h"
00020 #include "llvm/Support/Dwarf.h"
00021 #include <vector>
00022 
00023 namespace llvm {
00024 class AsmPrinter;
00025 class MCExpr;
00026 class MCSymbol;
00027 class raw_ostream;
00028 class DwarfTypeUnit;
00029 
00030 //===--------------------------------------------------------------------===//
00031 /// DIEAbbrevData - Dwarf abbreviation data, describes one attribute of a
00032 /// Dwarf abbreviation.
00033 class DIEAbbrevData {
00034   /// Attribute - Dwarf attribute code.
00035   ///
00036   dwarf::Attribute Attribute;
00037 
00038   /// Form - Dwarf form code.
00039   ///
00040   dwarf::Form Form;
00041 
00042 public:
00043   DIEAbbrevData(dwarf::Attribute A, dwarf::Form F) : Attribute(A), Form(F) {}
00044 
00045   // Accessors.
00046   dwarf::Attribute getAttribute() const { return Attribute; }
00047   dwarf::Form getForm() const { return Form; }
00048 
00049   /// Profile - Used to gather unique data for the abbreviation folding set.
00050   ///
00051   void Profile(FoldingSetNodeID &ID) const;
00052 };
00053 
00054 //===--------------------------------------------------------------------===//
00055 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
00056 /// information object.
00057 class DIEAbbrev : public FoldingSetNode {
00058   /// Unique number for node.
00059   ///
00060   unsigned Number;
00061 
00062   /// Tag - Dwarf tag code.
00063   ///
00064   dwarf::Tag Tag;
00065 
00066   /// Children - Whether or not this node has children.
00067   ///
00068   // This cheats a bit in all of the uses since the values in the standard
00069   // are 0 and 1 for no children and children respectively.
00070   bool Children;
00071 
00072   /// Data - Raw data bytes for abbreviation.
00073   ///
00074   SmallVector<DIEAbbrevData, 12> Data;
00075 
00076 public:
00077   DIEAbbrev(dwarf::Tag T, bool C) : Tag(T), Children(C), Data() {}
00078 
00079   // Accessors.
00080   dwarf::Tag getTag() const { return Tag; }
00081   unsigned getNumber() const { return Number; }
00082   bool hasChildren() const { return Children; }
00083   const SmallVectorImpl<DIEAbbrevData> &getData() const { return Data; }
00084   void setChildrenFlag(bool hasChild) { Children = hasChild; }
00085   void setNumber(unsigned N) { Number = N; }
00086 
00087   /// AddAttribute - Adds another set of attribute information to the
00088   /// abbreviation.
00089   void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form) {
00090     Data.push_back(DIEAbbrevData(Attribute, Form));
00091   }
00092 
00093   /// Profile - Used to gather unique data for the abbreviation folding set.
00094   ///
00095   void Profile(FoldingSetNodeID &ID) const;
00096 
00097   /// Emit - Print the abbreviation using the specified asm printer.
00098   ///
00099   void Emit(const AsmPrinter *AP) const;
00100 
00101 #ifndef NDEBUG
00102   void print(raw_ostream &O);
00103   void dump();
00104 #endif
00105 };
00106 
00107 //===--------------------------------------------------------------------===//
00108 /// DIEInteger - An integer value DIE.
00109 ///
00110 class DIEInteger {
00111   uint64_t Integer;
00112 
00113 public:
00114   explicit DIEInteger(uint64_t I) : Integer(I) {}
00115 
00116   /// BestForm - Choose the best form for integer.
00117   ///
00118   static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
00119     if (IsSigned) {
00120       const int64_t SignedInt = Int;
00121       if ((char)Int == SignedInt)
00122         return dwarf::DW_FORM_data1;
00123       if ((short)Int == SignedInt)
00124         return dwarf::DW_FORM_data2;
00125       if ((int)Int == SignedInt)
00126         return dwarf::DW_FORM_data4;
00127     } else {
00128       if ((unsigned char)Int == Int)
00129         return dwarf::DW_FORM_data1;
00130       if ((unsigned short)Int == Int)
00131         return dwarf::DW_FORM_data2;
00132       if ((unsigned int)Int == Int)
00133         return dwarf::DW_FORM_data4;
00134     }
00135     return dwarf::DW_FORM_data8;
00136   }
00137 
00138   uint64_t getValue() const { return Integer; }
00139   void setValue(uint64_t Val) { Integer = Val; }
00140 
00141   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00142   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00143 
00144 #ifndef NDEBUG
00145   void print(raw_ostream &O) const;
00146 #endif
00147 };
00148 
00149 //===--------------------------------------------------------------------===//
00150 /// DIEExpr - An expression DIE.
00151 //
00152 class DIEExpr {
00153   const MCExpr *Expr;
00154 
00155 public:
00156   explicit DIEExpr(const MCExpr *E) : Expr(E) {}
00157 
00158   /// getValue - Get MCExpr.
00159   ///
00160   const MCExpr *getValue() const { return Expr; }
00161 
00162   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00163   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00164 
00165 #ifndef NDEBUG
00166   void print(raw_ostream &O) const;
00167 #endif
00168 };
00169 
00170 //===--------------------------------------------------------------------===//
00171 /// DIELabel - A label DIE.
00172 //
00173 class DIELabel {
00174   const MCSymbol *Label;
00175 
00176 public:
00177   explicit DIELabel(const MCSymbol *L) : Label(L) {}
00178 
00179   /// getValue - Get MCSymbol.
00180   ///
00181   const MCSymbol *getValue() const { return Label; }
00182 
00183   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00184   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00185 
00186 #ifndef NDEBUG
00187   void print(raw_ostream &O) const;
00188 #endif
00189 };
00190 
00191 //===--------------------------------------------------------------------===//
00192 /// DIEDelta - A simple label difference DIE.
00193 ///
00194 class DIEDelta {
00195   const MCSymbol *LabelHi;
00196   const MCSymbol *LabelLo;
00197 
00198 public:
00199   DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo) : LabelHi(Hi), LabelLo(Lo) {}
00200 
00201   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00202   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00203 
00204 #ifndef NDEBUG
00205   void print(raw_ostream &O) const;
00206 #endif
00207 };
00208 
00209 //===--------------------------------------------------------------------===//
00210 /// DIEString - A container for string values.
00211 ///
00212 class DIEString {
00213   DwarfStringPoolEntryRef S;
00214 
00215 public:
00216   DIEString(DwarfStringPoolEntryRef S) : S(S) {}
00217 
00218   /// getString - Grab the string out of the object.
00219   StringRef getString() const { return S.getString(); }
00220 
00221   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00222   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00223 
00224 #ifndef NDEBUG
00225   void print(raw_ostream &O) const;
00226 #endif
00227 };
00228 
00229 //===--------------------------------------------------------------------===//
00230 /// DIEEntry - A pointer to another debug information entry.  An instance of
00231 /// this class can also be used as a proxy for a debug information entry not
00232 /// yet defined (ie. types.)
00233 class DIE;
00234 class DIEEntry {
00235   DIE *Entry;
00236 
00237   DIEEntry() = delete;
00238 
00239 public:
00240   explicit DIEEntry(DIE &E) : Entry(&E) {}
00241 
00242   DIE &getEntry() const { return *Entry; }
00243 
00244   /// Returns size of a ref_addr entry.
00245   static unsigned getRefAddrSize(const AsmPrinter *AP);
00246 
00247   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00248   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
00249     return Form == dwarf::DW_FORM_ref_addr ? getRefAddrSize(AP)
00250                                            : sizeof(int32_t);
00251   }
00252 
00253 #ifndef NDEBUG
00254   void print(raw_ostream &O) const;
00255 #endif
00256 };
00257 
00258 //===--------------------------------------------------------------------===//
00259 /// \brief A signature reference to a type unit.
00260 class DIETypeSignature {
00261   const DwarfTypeUnit *Unit;
00262 
00263   DIETypeSignature() = delete;
00264 
00265 public:
00266   explicit DIETypeSignature(const DwarfTypeUnit &Unit) : Unit(&Unit) {}
00267 
00268   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00269   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
00270     assert(Form == dwarf::DW_FORM_ref_sig8);
00271     return 8;
00272   }
00273 
00274 #ifndef NDEBUG
00275   void print(raw_ostream &O) const;
00276 #endif
00277 };
00278 
00279 //===--------------------------------------------------------------------===//
00280 /// DIELocList - Represents a pointer to a location list in the debug_loc
00281 /// section.
00282 //
00283 class DIELocList {
00284   // Index into the .debug_loc vector.
00285   size_t Index;
00286 
00287 public:
00288   DIELocList(size_t I) : Index(I) {}
00289 
00290   /// getValue - Grab the current index out.
00291   size_t getValue() const { return Index; }
00292 
00293   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00294   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00295 
00296 #ifndef NDEBUG
00297   void print(raw_ostream &O) const;
00298 #endif
00299 };
00300 
00301 //===--------------------------------------------------------------------===//
00302 /// DIEValue - A debug information entry value. Some of these roughly correlate
00303 /// to DWARF attribute classes.
00304 ///
00305 class DIEBlock;
00306 class DIELoc;
00307 class DIEValue {
00308 public:
00309   enum Type {
00310     isNone,
00311 #define HANDLE_DIEVALUE(T) is##T,
00312 #include "llvm/CodeGen/DIEValue.def"
00313   };
00314 
00315 private:
00316   /// Ty - Type of data stored in the value.
00317   ///
00318   Type Ty = isNone;
00319   dwarf::Attribute Attribute = (dwarf::Attribute)0;
00320   dwarf::Form Form = (dwarf::Form)0;
00321 
00322   /// Storage for the value.
00323   ///
00324   /// All values that aren't standard layout (or are larger than 8 bytes)
00325   /// should be stored by reference instead of by value.
00326   typedef AlignedCharArrayUnion<DIEInteger, DIEString, DIEExpr, DIELabel,
00327                                 DIEDelta *, DIEEntry, DIETypeSignature,
00328                                 DIEBlock *, DIELoc *, DIELocList> ValTy;
00329   static_assert(sizeof(ValTy) <= sizeof(uint64_t) ||
00330                     sizeof(ValTy) <= sizeof(void *),
00331                 "Expected all large types to be stored via pointer");
00332 
00333   /// Underlying stored value.
00334   ValTy Val;
00335 
00336   template <class T> void construct(T V) {
00337     static_assert(std::is_standard_layout<T>::value ||
00338                       std::is_pointer<T>::value,
00339                   "Expected standard layout or pointer");
00340     new (reinterpret_cast<void *>(Val.buffer)) T(V);
00341   }
00342 
00343   template <class T> T &get() { return *reinterpret_cast<T *>(Val.buffer); }
00344   template <class T> const T &get() const {
00345     return *reinterpret_cast<const T *>(Val.buffer);
00346   }
00347   template <class T> void destruct() { get<T>().~T(); }
00348 
00349   /// Destroy the underlying value.
00350   ///
00351   /// This should get optimized down to a no-op.  We could skip it if we could
00352   /// add a static assert on \a std::is_trivially_copyable(), but we currently
00353   /// support versions of GCC that don't understand that.
00354   void destroyVal() {
00355     switch (Ty) {
00356     case isNone:
00357       return;
00358 #define HANDLE_DIEVALUE_SMALL(T)                                               \
00359   case is##T:                                                                  \
00360     destruct<DIE##T>();
00361     return;
00362 #define HANDLE_DIEVALUE_LARGE(T)                                               \
00363   case is##T:                                                                  \
00364     destruct<const DIE##T *>();
00365     return;
00366 #include "llvm/CodeGen/DIEValue.def"
00367     }
00368   }
00369 
00370   /// Copy the underlying value.
00371   ///
00372   /// This should get optimized down to a simple copy.  We need to actually
00373   /// construct the value, rather than calling memcpy, to satisfy strict
00374   /// aliasing rules.
00375   void copyVal(const DIEValue &X) {
00376     switch (Ty) {
00377     case isNone:
00378       return;
00379 #define HANDLE_DIEVALUE_SMALL(T)                                               \
00380   case is##T:                                                                  \
00381     construct<DIE##T>(X.get<DIE##T>());                                        \
00382     return;
00383 #define HANDLE_DIEVALUE_LARGE(T)                                               \
00384   case is##T:                                                                  \
00385     construct<const DIE##T *>(X.get<const DIE##T *>());                        \
00386     return;
00387 #include "llvm/CodeGen/DIEValue.def"
00388     }
00389   }
00390 
00391 public:
00392   DIEValue() = default;
00393   DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
00394     copyVal(X);
00395   }
00396   DIEValue &operator=(const DIEValue &X) {
00397     destroyVal();
00398     Ty = X.Ty;
00399     Attribute = X.Attribute;
00400     Form = X.Form;
00401     copyVal(X);
00402     return *this;
00403   }
00404   ~DIEValue() { destroyVal(); }
00405 
00406 #define HANDLE_DIEVALUE_SMALL(T)                                               \
00407   DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T &V)      \
00408       : Ty(is##T), Attribute(Attribute), Form(Form) {                          \
00409     construct<DIE##T>(V);                                                      \
00410   }
00411 #define HANDLE_DIEVALUE_LARGE(T)                                               \
00412   DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T *V)      \
00413       : Ty(is##T), Attribute(Attribute), Form(Form) {                          \
00414     assert(V && "Expected valid value");                                       \
00415     construct<const DIE##T *>(V);                                              \
00416   }
00417 #include "llvm/CodeGen/DIEValue.def"
00418 
00419   // Accessors
00420   Type getType() const { return Ty; }
00421   dwarf::Attribute getAttribute() const { return Attribute; }
00422   dwarf::Form getForm() const { return Form; }
00423   explicit operator bool() const { return Ty; }
00424 
00425 #define HANDLE_DIEVALUE_SMALL(T)                                               \
00426   const DIE##T &getDIE##T() const {                                            \
00427     assert(getType() == is##T && "Expected " #T);                              \
00428     return get<DIE##T>();                                                      \
00429   }
00430 #define HANDLE_DIEVALUE_LARGE(T)                                               \
00431   const DIE##T &getDIE##T() const {                                            \
00432     assert(getType() == is##T && "Expected " #T);                              \
00433     return *get<const DIE##T *>();                                             \
00434   }
00435 #include "llvm/CodeGen/DIEValue.def"
00436 
00437   /// EmitValue - Emit value via the Dwarf writer.
00438   ///
00439   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00440 
00441   /// SizeOf - Return the size of a value in bytes.
00442   ///
00443   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00444 
00445 #ifndef NDEBUG
00446   void print(raw_ostream &O) const;
00447   void dump() const;
00448 #endif
00449 };
00450 
00451 //===--------------------------------------------------------------------===//
00452 /// DIE - A structured debug information entry.  Has an abbreviation which
00453 /// describes its organization.
00454 class DIE {
00455 protected:
00456   /// Offset - Offset in debug info section.
00457   ///
00458   unsigned Offset;
00459 
00460   /// Size - Size of instance + children.
00461   ///
00462   unsigned Size;
00463 
00464   unsigned AbbrevNumber = ~0u;
00465 
00466   /// Tag - Dwarf tag code.
00467   ///
00468   dwarf::Tag Tag = (dwarf::Tag)0;
00469 
00470   /// Children DIEs.
00471   ///
00472   // This can't be a vector<DIE> because pointer validity is requirent for the
00473   // Parent pointer and DIEEntry.
00474   // It can't be a list<DIE> because some clients need pointer validity before
00475   // the object has been added to any child list
00476   // (eg: DwarfUnit::constructVariableDIE). These aren't insurmountable, but may
00477   // be more convoluted than beneficial.
00478   std::vector<std::unique_ptr<DIE>> Children;
00479 
00480   DIE *Parent;
00481 
00482   /// Attribute values.
00483   ///
00484   SmallVector<DIEValue, 12> Values;
00485 
00486 protected:
00487   DIE() : Offset(0), Size(0), Parent(nullptr) {}
00488 
00489 public:
00490   explicit DIE(dwarf::Tag Tag)
00491       : Offset(0), Size(0), Tag(Tag), Parent(nullptr) {}
00492 
00493   // Accessors.
00494   unsigned getAbbrevNumber() const { return AbbrevNumber; }
00495   dwarf::Tag getTag() const { return Tag; }
00496   unsigned getOffset() const { return Offset; }
00497   unsigned getSize() const { return Size; }
00498   bool hasChildren() const { return !Children.empty(); }
00499   const std::vector<std::unique_ptr<DIE>> &getChildren() const {
00500     return Children;
00501   }
00502 
00503   typedef SmallVectorImpl<DIEValue>::const_iterator value_iterator;
00504   typedef iterator_range<value_iterator> value_range;
00505 
00506   value_iterator begin_values() const { return Values.begin(); }
00507   value_iterator end_values() const { return Values.end(); }
00508   value_range values() const {
00509     return llvm::make_range(begin_values(), end_values());
00510   }
00511 
00512   void setValue(unsigned I, DIEValue New) {
00513     assert(I < Values.size());
00514     Values[I] = New;
00515   }
00516   DIE *getParent() const { return Parent; }
00517 
00518   /// Generate the abbreviation for this DIE.
00519   ///
00520   /// Calculate the abbreviation for this, which should be uniqued and
00521   /// eventually used to call \a setAbbrevNumber().
00522   DIEAbbrev generateAbbrev() const;
00523 
00524   /// Set the abbreviation number for this DIE.
00525   void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
00526 
00527   /// Climb up the parent chain to get the compile or type unit DIE this DIE
00528   /// belongs to.
00529   const DIE *getUnit() const;
00530   /// Similar to getUnit, returns null when DIE is not added to an
00531   /// owner yet.
00532   const DIE *getUnitOrNull() const;
00533   void setOffset(unsigned O) { Offset = O; }
00534   void setSize(unsigned S) { Size = S; }
00535 
00536   /// addValue - Add a value and attributes to a DIE.
00537   ///
00538   void addValue(DIEValue Value) { Values.push_back(Value); }
00539   template <class T>
00540   void addValue(dwarf::Attribute Attribute, dwarf::Form Form, T &&Value) {
00541     Values.emplace_back(Attribute, Form, std::forward<T>(Value));
00542   }
00543 
00544   /// addChild - Add a child to the DIE.
00545   ///
00546   DIE &addChild(std::unique_ptr<DIE> Child) {
00547     assert(!Child->getParent());
00548     Child->Parent = this;
00549     Children.push_back(std::move(Child));
00550     return *Children.back();
00551   }
00552 
00553   /// Find a value in the DIE with the attribute given.
00554   ///
00555   /// Returns a default-constructed DIEValue (where \a DIEValue::getType()
00556   /// gives \a DIEValue::isNone) if no such attribute exists.
00557   DIEValue findAttribute(dwarf::Attribute Attribute) const;
00558 
00559 #ifndef NDEBUG
00560   void print(raw_ostream &O, unsigned IndentCount = 0) const;
00561   void dump();
00562 #endif
00563 };
00564 
00565 //===--------------------------------------------------------------------===//
00566 /// DIELoc - Represents an expression location.
00567 //
00568 class DIELoc : public DIE {
00569   mutable unsigned Size; // Size in bytes excluding size header.
00570 
00571 public:
00572   DIELoc() : Size(0) {}
00573 
00574   /// ComputeSize - Calculate the size of the location expression.
00575   ///
00576   unsigned ComputeSize(const AsmPrinter *AP) const;
00577 
00578   /// BestForm - Choose the best form for data.
00579   ///
00580   dwarf::Form BestForm(unsigned DwarfVersion) const {
00581     if (DwarfVersion > 3)
00582       return dwarf::DW_FORM_exprloc;
00583     // Pre-DWARF4 location expressions were blocks and not exprloc.
00584     if ((unsigned char)Size == Size)
00585       return dwarf::DW_FORM_block1;
00586     if ((unsigned short)Size == Size)
00587       return dwarf::DW_FORM_block2;
00588     if ((unsigned int)Size == Size)
00589       return dwarf::DW_FORM_block4;
00590     return dwarf::DW_FORM_block;
00591   }
00592 
00593   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00594   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00595 
00596 #ifndef NDEBUG
00597   void print(raw_ostream &O) const;
00598 #endif
00599 };
00600 
00601 //===--------------------------------------------------------------------===//
00602 /// DIEBlock - Represents a block of values.
00603 //
00604 class DIEBlock : public DIE {
00605   mutable unsigned Size; // Size in bytes excluding size header.
00606 
00607 public:
00608   DIEBlock() : Size(0) {}
00609 
00610   /// ComputeSize - Calculate the size of the location expression.
00611   ///
00612   unsigned ComputeSize(const AsmPrinter *AP) const;
00613 
00614   /// BestForm - Choose the best form for data.
00615   ///
00616   dwarf::Form BestForm() const {
00617     if ((unsigned char)Size == Size)
00618       return dwarf::DW_FORM_block1;
00619     if ((unsigned short)Size == Size)
00620       return dwarf::DW_FORM_block2;
00621     if ((unsigned int)Size == Size)
00622       return dwarf::DW_FORM_block4;
00623     return dwarf::DW_FORM_block;
00624   }
00625 
00626   void EmitValue(const AsmPrinter *AP, dwarf::Form Form) const;
00627   unsigned SizeOf(const AsmPrinter *AP, dwarf::Form Form) const;
00628 
00629 #ifndef NDEBUG
00630   void print(raw_ostream &O) const;
00631 #endif
00632 };
00633 
00634 } // end llvm namespace
00635 
00636 #endif