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 "DwarfLabel.h"
00018 #include "llvm/ADT/FoldingSet.h"
00019 #include "llvm/ADT/SmallVector.h"
00020 #include "llvm/Support/Compiler.h"
00021 #include "llvm/Support/Dwarf.h"
00022 #include "llvm/Support/raw_ostream.h"
00023 #include <iosfwd>
00024 
00025 namespace llvm {
00026   class AsmPrinter;
00027   class Dwarf;
00028   class TargetData;
00029 
00030   //===--------------------------------------------------------------------===//
00031   /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
00032   /// Dwarf abbreviation.
00033   class VISIBILITY_HIDDEN DIEAbbrevData {
00034     /// Attribute - Dwarf attribute code.
00035     ///
00036     unsigned Attribute;
00037 
00038     /// Form - Dwarf form code.
00039     ///
00040     unsigned Form;
00041   public:
00042     DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {}
00043 
00044     // Accessors.
00045     unsigned getAttribute() const { return Attribute; }
00046     unsigned 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 VISIBILITY_HIDDEN DIEAbbrev : public FoldingSetNode {
00057     /// Tag - Dwarf tag code.
00058     ///
00059     unsigned Tag;
00060 
00061     /// Unique number for node.
00062     ///
00063     unsigned Number;
00064 
00065     /// ChildrenFlag - Dwarf children flag.
00066     ///
00067     unsigned ChildrenFlag;
00068 
00069     /// Data - Raw data bytes for abbreviation.
00070     ///
00071     SmallVector<DIEAbbrevData, 8> Data;
00072   public:
00073     DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {}
00074     virtual ~DIEAbbrev() {}
00075 
00076     // Accessors.
00077     unsigned getTag() const { return Tag; }
00078     unsigned getNumber() const { return Number; }
00079     unsigned getChildrenFlag() const { return ChildrenFlag; }
00080     const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
00081     void setTag(unsigned T) { Tag = T; }
00082     void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
00083     void setNumber(unsigned N) { Number = N; }
00084 
00085     /// AddAttribute - Adds another set of attribute information to the
00086     /// abbreviation.
00087     void AddAttribute(unsigned Attribute, unsigned Form) {
00088       Data.push_back(DIEAbbrevData(Attribute, Form));
00089     }
00090 
00091     /// AddFirstAttribute - Adds a set of attribute information to the front
00092     /// of the abbreviation.
00093     void AddFirstAttribute(unsigned Attribute, unsigned Form) {
00094       Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
00095     }
00096 
00097     /// Profile - Used to gather unique data for the abbreviation folding set.
00098     ///
00099     void Profile(FoldingSetNodeID &ID) const;
00100 
00101     /// Emit - Print the abbreviation using the specified asm printer.
00102     ///
00103     void Emit(const AsmPrinter *Asm) const;
00104 
00105 #ifndef NDEBUG
00106     void print(std::ostream *O) {
00107       if (O) print(*O);
00108     }
00109     void print(std::ostream &O);
00110     void dump();
00111 #endif
00112   };
00113 
00114   //===--------------------------------------------------------------------===//
00115   /// DIE - A structured debug information entry.  Has an abbreviation which
00116   /// describes it's organization.
00117   class CompileUnit;
00118   class DIEValue;
00119 
00120   class VISIBILITY_HIDDEN DIE : public FoldingSetNode {
00121   protected:
00122     /// Abbrev - Buffer for constructing abbreviation.
00123     ///
00124     DIEAbbrev Abbrev;
00125 
00126     /// Offset - Offset in debug info section.
00127     ///
00128     unsigned Offset;
00129 
00130     /// Size - Size of instance + children.
00131     ///
00132     unsigned Size;
00133 
00134     /// Children DIEs.
00135     ///
00136     std::vector<DIE *> Children;
00137 
00138     /// Attributes values.
00139     ///
00140     SmallVector<DIEValue*, 32> Values;
00141 
00142     /// Abstract compile unit.
00143     CompileUnit *AbstractCU;
00144     
00145     // Private data for print()
00146     mutable unsigned IndentCount;
00147   public:
00148     explicit DIE(unsigned Tag)
00149       : Abbrev(Tag, dwarf::DW_CHILDREN_no), Offset(0),
00150         Size(0), IndentCount(0) {}
00151     virtual ~DIE();
00152 
00153     // Accessors.
00154     DIEAbbrev &getAbbrev() { return Abbrev; }
00155     unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
00156     unsigned getTag() const { return Abbrev.getTag(); }
00157     unsigned getOffset() const { return Offset; }
00158     unsigned getSize() const { return Size; }
00159     const std::vector<DIE *> &getChildren() const { return Children; }
00160     SmallVector<DIEValue*, 32> &getValues() { return Values; }
00161     CompileUnit *getAbstractCompileUnit() const { return AbstractCU; }
00162 
00163     void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
00164     void setOffset(unsigned O) { Offset = O; }
00165     void setSize(unsigned S) { Size = S; }
00166     void setAbstractCompileUnit(CompileUnit *CU) { AbstractCU = CU; }
00167 
00168     /// AddValue - Add a value and attributes to a DIE.
00169     ///
00170     void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
00171       Abbrev.AddAttribute(Attribute, Form);
00172       Values.push_back(Value);
00173     }
00174 
00175     /// SiblingOffset - Return the offset of the debug information entry's
00176     /// sibling.
00177     unsigned SiblingOffset() const { return Offset + Size; }
00178 
00179     /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
00180     ///
00181     void AddSiblingOffset();
00182 
00183     /// AddChild - Add a child to the DIE.
00184     ///
00185     void AddChild(DIE *Child) {
00186       Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
00187       Children.push_back(Child);
00188     }
00189 
00190     /// Detach - Detaches objects connected to it after copying.
00191     ///
00192     void Detach() {
00193       Children.clear();
00194     }
00195 
00196     /// Profile - Used to gather unique data for the value folding set.
00197     ///
00198     void Profile(FoldingSetNodeID &ID) ;
00199 
00200 #ifndef NDEBUG
00201     void print(std::ostream *O, unsigned IncIndent = 0) {
00202       if (O) print(*O, IncIndent);
00203     }
00204     void print(std::ostream &O, unsigned IncIndent = 0);
00205     void dump();
00206 #endif
00207   };
00208 
00209   //===--------------------------------------------------------------------===//
00210   /// DIEValue - A debug information entry value.
00211   ///
00212   class VISIBILITY_HIDDEN DIEValue : public FoldingSetNode {
00213   public:
00214     enum {
00215       isInteger,
00216       isString,
00217       isLabel,
00218       isAsIsLabel,
00219       isSectionOffset,
00220       isDelta,
00221       isEntry,
00222       isBlock
00223     };
00224   protected:
00225     /// Type - Type of data stored in the value.
00226     ///
00227     unsigned Type;
00228   public:
00229     explicit DIEValue(unsigned T) : Type(T) {}
00230     virtual ~DIEValue() {}
00231 
00232     // Accessors
00233     unsigned getType()  const { return Type; }
00234 
00235     /// EmitValue - Emit value via the Dwarf writer.
00236     ///
00237     virtual void EmitValue(Dwarf *D, unsigned Form) const = 0;
00238 
00239     /// SizeOf - Return the size of a value in bytes.
00240     ///
00241     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const = 0;
00242 
00243     /// Profile - Used to gather unique data for the value folding set.
00244     ///
00245     virtual void Profile(FoldingSetNodeID &ID) = 0;
00246 
00247     // Implement isa/cast/dyncast.
00248     static bool classof(const DIEValue *) { return true; }
00249 
00250 #ifndef NDEBUG
00251     void print(std::ostream *O) {
00252       if (O) print(*O);
00253     }
00254     virtual void print(std::ostream &O) = 0;
00255     void dump();
00256 #endif
00257   };
00258 
00259   //===--------------------------------------------------------------------===//
00260   /// DIEInteger - An integer value DIE.
00261   ///
00262   class VISIBILITY_HIDDEN DIEInteger : public DIEValue {
00263     uint64_t Integer;
00264   public:
00265     explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
00266 
00267     /// BestForm - Choose the best form for integer.
00268     ///
00269     static unsigned BestForm(bool IsSigned, uint64_t Int) {
00270       if (IsSigned) {
00271         if ((char)Int == (signed)Int)   return dwarf::DW_FORM_data1;
00272         if ((short)Int == (signed)Int)  return dwarf::DW_FORM_data2;
00273         if ((int)Int == (signed)Int)    return dwarf::DW_FORM_data4;
00274       } else {
00275         if ((unsigned char)Int == Int)  return dwarf::DW_FORM_data1;
00276         if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
00277         if ((unsigned int)Int == Int)   return dwarf::DW_FORM_data4;
00278       }
00279       return dwarf::DW_FORM_data8;
00280     }
00281 
00282     /// EmitValue - Emit integer of appropriate size.
00283     ///
00284     virtual void EmitValue(Dwarf *D, unsigned Form) const;
00285 
00286     /// SizeOf - Determine size of integer value in bytes.
00287     ///
00288     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
00289 
00290     /// Profile - Used to gather unique data for the value folding set.
00291     ///
00292     static void Profile(FoldingSetNodeID &ID, unsigned Int);
00293     virtual void Profile(FoldingSetNodeID &ID);
00294 
00295     // Implement isa/cast/dyncast.
00296     static bool classof(const DIEInteger *) { return true; }
00297     static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
00298 
00299 #ifndef NDEBUG
00300     virtual void print(std::ostream &O);
00301 #endif
00302   };
00303 
00304   //===--------------------------------------------------------------------===//
00305   /// DIEString - A string value DIE.
00306   ///
00307   class VISIBILITY_HIDDEN DIEString : public DIEValue {
00308     const std::string Str;
00309   public:
00310     explicit DIEString(const std::string &S) : DIEValue(isString), Str(S) {}
00311 
00312     /// EmitValue - Emit string value.
00313     ///
00314     virtual void EmitValue(Dwarf *D, unsigned Form) const;
00315 
00316     /// SizeOf - Determine size of string value in bytes.
00317     ///
00318     virtual unsigned SizeOf(const TargetData *, unsigned /*Form*/) const {
00319       return Str.size() + sizeof(char); // sizeof('\0');
00320     }
00321 
00322     /// Profile - Used to gather unique data for the value folding set.
00323     ///
00324     static void Profile(FoldingSetNodeID &ID, const std::string &Str);
00325     virtual void Profile(FoldingSetNodeID &ID);
00326 
00327     // Implement isa/cast/dyncast.
00328     static bool classof(const DIEString *) { return true; }
00329     static bool classof(const DIEValue *S) { return S->getType() == isString; }
00330 
00331 #ifndef NDEBUG
00332     virtual void print(std::ostream &O);
00333 #endif
00334   };
00335 
00336   //===--------------------------------------------------------------------===//
00337   /// DIEDwarfLabel - A Dwarf internal label expression DIE.
00338   //
00339   class VISIBILITY_HIDDEN DIEDwarfLabel : public DIEValue {
00340     const DWLabel Label;
00341   public:
00342     explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
00343 
00344     /// EmitValue - Emit label value.
00345     ///
00346     virtual void EmitValue(Dwarf *D, unsigned Form) const;
00347 
00348     /// SizeOf - Determine size of label value in bytes.
00349     ///
00350     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
00351 
00352     /// Profile - Used to gather unique data for the value folding set.
00353     ///
00354     static void Profile(FoldingSetNodeID &ID, const DWLabel &Label);
00355     virtual void Profile(FoldingSetNodeID &ID);
00356 
00357     // Implement isa/cast/dyncast.
00358     static bool classof(const DIEDwarfLabel *)  { return true; }
00359     static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
00360 
00361 #ifndef NDEBUG
00362     virtual void print(std::ostream &O);
00363 #endif
00364   };
00365 
00366   //===--------------------------------------------------------------------===//
00367   /// DIEObjectLabel - A label to an object in code or data.
00368   //
00369   class VISIBILITY_HIDDEN DIEObjectLabel : public DIEValue {
00370     const std::string Label;
00371   public:
00372     explicit DIEObjectLabel(const std::string &L)
00373       : DIEValue(isAsIsLabel), Label(L) {}
00374 
00375     /// EmitValue - Emit label value.
00376     ///
00377     virtual void EmitValue(Dwarf *D, unsigned Form) const;
00378 
00379     /// SizeOf - Determine size of label value in bytes.
00380     ///
00381     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
00382 
00383     /// Profile - Used to gather unique data for the value folding set.
00384     ///
00385     static void Profile(FoldingSetNodeID &ID, const std::string &Label);
00386     virtual void Profile(FoldingSetNodeID &ID);
00387 
00388     // Implement isa/cast/dyncast.
00389     static bool classof(const DIEObjectLabel *) { return true; }
00390     static bool classof(const DIEValue *L) {
00391       return L->getType() == isAsIsLabel;
00392     }
00393 
00394 #ifndef NDEBUG
00395     virtual void print(std::ostream &O);
00396 #endif
00397   };
00398 
00399   //===--------------------------------------------------------------------===//
00400   /// DIESectionOffset - A section offset DIE.
00401   ///
00402   class VISIBILITY_HIDDEN DIESectionOffset : public DIEValue {
00403     const DWLabel Label;
00404     const DWLabel Section;
00405     bool IsEH : 1;
00406     bool UseSet : 1;
00407   public:
00408     DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
00409                      bool isEH = false, bool useSet = true)
00410       : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
00411         IsEH(isEH), UseSet(useSet) {}
00412 
00413     /// EmitValue - Emit section offset.
00414     ///
00415     virtual void EmitValue(Dwarf *D, unsigned Form) const;
00416 
00417     /// SizeOf - Determine size of section offset value in bytes.
00418     ///
00419     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
00420 
00421     /// Profile - Used to gather unique data for the value folding set.
00422     ///
00423     static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
00424                         const DWLabel &Section);
00425     virtual void Profile(FoldingSetNodeID &ID);
00426 
00427     // Implement isa/cast/dyncast.
00428     static bool classof(const DIESectionOffset *)  { return true; }
00429     static bool classof(const DIEValue *D) {
00430       return D->getType() == isSectionOffset;
00431     }
00432 
00433 #ifndef NDEBUG
00434     virtual void print(std::ostream &O);
00435 #endif
00436   };
00437 
00438   //===--------------------------------------------------------------------===//
00439   /// DIEDelta - A simple label difference DIE.
00440   ///
00441   class VISIBILITY_HIDDEN DIEDelta : public DIEValue {
00442     const DWLabel LabelHi;
00443     const DWLabel LabelLo;
00444   public:
00445     DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
00446       : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
00447 
00448     /// EmitValue - Emit delta value.
00449     ///
00450     virtual void EmitValue(Dwarf *D, unsigned Form) const;
00451 
00452     /// SizeOf - Determine size of delta value in bytes.
00453     ///
00454     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
00455 
00456     /// Profile - Used to gather unique data for the value folding set.
00457     ///
00458     static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
00459                         const DWLabel &LabelLo);
00460     virtual void Profile(FoldingSetNodeID &ID);
00461 
00462     // Implement isa/cast/dyncast.
00463     static bool classof(const DIEDelta *)  { return true; }
00464     static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
00465 
00466 #ifndef NDEBUG
00467     virtual void print(std::ostream &O);
00468 #endif
00469   };
00470 
00471   //===--------------------------------------------------------------------===//
00472   /// DIEntry - A pointer to another debug information entry.  An instance of
00473   /// this class can also be used as a proxy for a debug information entry not
00474   /// yet defined (ie. types.)
00475   class VISIBILITY_HIDDEN DIEEntry : public DIEValue {
00476     DIE *Entry;
00477   public:
00478     explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
00479 
00480     DIE *getEntry() const { return Entry; }
00481     void setEntry(DIE *E) { Entry = E; }
00482 
00483     /// EmitValue - Emit debug information entry offset.
00484     ///
00485     virtual void EmitValue(Dwarf *D, unsigned Form) const;
00486 
00487     /// SizeOf - Determine size of debug information entry in bytes.
00488     ///
00489     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const {
00490       return sizeof(int32_t);
00491     }
00492 
00493     /// Profile - Used to gather unique data for the value folding set.
00494     ///
00495     static void Profile(FoldingSetNodeID &ID, DIE *Entry);
00496     virtual void Profile(FoldingSetNodeID &ID);
00497 
00498     // Implement isa/cast/dyncast.
00499     static bool classof(const DIEEntry *)  { return true; }
00500     static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
00501 
00502 #ifndef NDEBUG
00503     virtual void print(std::ostream &O);
00504 #endif
00505   };
00506 
00507   //===--------------------------------------------------------------------===//
00508   /// DIEBlock - A block of values.  Primarily used for location expressions.
00509   //
00510   class VISIBILITY_HIDDEN DIEBlock : public DIEValue, public DIE {
00511     unsigned Size;                // Size in bytes excluding size header.
00512   public:
00513     DIEBlock()
00514       : DIEValue(isBlock), DIE(0), Size(0) {}
00515     virtual ~DIEBlock() {}
00516 
00517     /// ComputeSize - calculate the size of the block.
00518     ///
00519     unsigned ComputeSize(const TargetData *TD);
00520 
00521     /// BestForm - Choose the best form for data.
00522     ///
00523     unsigned BestForm() const {
00524       if ((unsigned char)Size == Size)  return dwarf::DW_FORM_block1;
00525       if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
00526       if ((unsigned int)Size == Size)   return dwarf::DW_FORM_block4;
00527       return dwarf::DW_FORM_block;
00528     }
00529 
00530     /// EmitValue - Emit block data.
00531     ///
00532     virtual void EmitValue(Dwarf *D, unsigned Form) const;
00533 
00534     /// SizeOf - Determine size of block data in bytes.
00535     ///
00536     virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const;
00537 
00538     /// Profile - Used to gather unique data for the value folding set.
00539     ///
00540     virtual void Profile(FoldingSetNodeID &ID);
00541 
00542     // Implement isa/cast/dyncast.
00543     static bool classof(const DIEBlock *)  { return true; }
00544     static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
00545 
00546 #ifndef NDEBUG
00547     virtual void print(std::ostream &O);
00548 #endif
00549   };
00550 
00551 } // end llvm namespace
00552 
00553 #endif