LLVM  5.0.0
DWARFAbbreviationDeclaration.h
Go to the documentation of this file.
1 //===- DWARFAbbreviationDeclaration.h ---------------------------*- 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 #ifndef LLVM_DEBUGINFO_DWARFABBREVIATIONDECLARATION_H
11 #define LLVM_DEBUGINFO_DWARFABBREVIATIONDECLARATION_H
12 
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/SmallVector.h"
18 #include <cassert>
19 #include <cstddef>
20 #include <cstdint>
21 
22 namespace llvm {
23 
24 class DWARFFormValue;
25 class DWARFUnit;
26 class raw_ostream;
27 
29 public:
30  struct AttributeSpec {
32  : Attr(A), Form(F), ByteSizeOrValue(V) {}
33 
36 
37  /// The following field is used for ByteSize for non-implicit_const
38  /// attributes and as value for implicit_const ones, indicated by
39  /// Form == DW_FORM_implicit_const.
40  /// The following cases are distinguished:
41  /// * Form != DW_FORM_implicit_const and ByteSizeOrValue has a value:
42  /// ByteSizeOrValue contains the fixed size in bytes
43  /// for the Form in this object.
44  /// * Form != DW_FORM_implicit_const and ByteSizeOrValue is None:
45  /// byte size of Form either varies according to the DWARFUnit
46  /// that it is contained in or the value size varies and must be
47  /// decoded from the debug information in order to determine its size.
48  /// * Form == DW_FORM_implicit_const:
49  /// ByteSizeOrValue contains value for the implicit_const attribute.
51 
52  bool isImplicitConst() const {
53  return Form == dwarf::DW_FORM_implicit_const;
54  }
55 
56  /// Get the fixed byte size of this Form if possible. This function might
57  /// use the DWARFUnit to calculate the size of the Form, like for
58  /// DW_AT_address and DW_AT_ref_addr, so this isn't just an accessor for
59  /// the ByteSize member.
60  Optional<int64_t> getByteSize(const DWARFUnit &U) const;
61  };
63 
65 
66  uint32_t getCode() const { return Code; }
67  uint8_t getCodeByteSize() const { return CodeByteSize; }
68  dwarf::Tag getTag() const { return Tag; }
69  bool hasChildren() const { return HasChildren; }
70 
71  using attr_iterator_range =
73 
75  return attr_iterator_range(AttributeSpecs.begin(), AttributeSpecs.end());
76  }
77 
79  assert(idx < AttributeSpecs.size());
80  return AttributeSpecs[idx].Form;
81  }
82 
83  size_t getNumAttributes() const {
84  return AttributeSpecs.size();
85  }
86 
88  assert(idx < AttributeSpecs.size());
89  return AttributeSpecs[idx].Attr;
90  }
91 
92  /// Get the index of the specified attribute.
93  ///
94  /// Searches the this abbreviation declaration for the index of the specified
95  /// attribute.
96  ///
97  /// \param attr DWARF attribute to search for.
98  /// \returns Optional index of the attribute if found, None otherwise.
100 
101  /// Extract a DWARF form value from a DIE specified by DIE offset.
102  ///
103  /// Extract an attribute value for a DWARFUnit given the DIE offset and the
104  /// attribute.
105  ///
106  /// \param DIEOffset the DIE offset that points to the ULEB128 abbreviation
107  /// code in the .debug_info data.
108  /// \param Attr DWARF attribute to search for.
109  /// \param U the DWARFUnit the contains the DIE.
110  /// \returns Optional DWARF form value if the attribute was extracted.
112  const dwarf::Attribute Attr,
113  const DWARFUnit &U) const;
114 
115  bool extract(DataExtractor Data, uint32_t* OffsetPtr);
116  void dump(raw_ostream &OS) const;
117 
118  // Return an optional byte size of all attribute data in this abbreviation
119  // if a constant byte size can be calculated given a DWARFUnit. This allows
120  // DWARF parsing to be faster as many DWARF DIEs have a fixed byte size.
122 
123 private:
124  void clear();
125 
126  /// A helper structure that can quickly determine the size in bytes of an
127  /// abbreviation declaration.
128  struct FixedSizeInfo {
129  /// The fixed byte size for fixed size forms.
130  uint16_t NumBytes = 0;
131  /// Number of DW_FORM_address forms in this abbrevation declaration.
132  uint8_t NumAddrs = 0;
133  /// Number of DW_FORM_ref_addr forms in this abbrevation declaration.
134  uint8_t NumRefAddrs = 0;
135  /// Number of 4 byte in DWARF32 and 8 byte in DWARF64 forms.
136  uint8_t NumDwarfOffsets = 0;
137 
138  FixedSizeInfo() = default;
139 
140  /// Calculate the fixed size in bytes given a DWARFUnit.
141  ///
142  /// \param U the DWARFUnit to use when determing the byte size.
143  /// \returns the size in bytes for all attribute data in this abbreviation.
144  /// The returned size does not include bytes for the ULEB128 abbreviation
145  /// code
146  size_t getByteSize(const DWARFUnit &U) const;
147  };
148 
149  uint32_t Code;
150  dwarf::Tag Tag;
151  uint8_t CodeByteSize;
152  bool HasChildren;
153  AttributeSpecVector AttributeSpecs;
154  /// If this abbreviation has a fixed byte size then FixedAttributeSize member
155  /// variable below will have a value.
156  Optional<FixedSizeInfo> FixedAttributeSize;
157 };
158 
159 } // end namespace llvm
160 
161 #endif // LLVM_DEBUGINFO_DWARFABBREVIATIONDECLARATION_H
Optional< int64_t > ByteSizeOrValue
The following field is used for ByteSize for non-implicit_const attributes and as value for implicit_...
bool extract(DataExtractor Data, uint32_t *OffsetPtr)
Attribute
Attributes.
Definition: Dwarf.h:105
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
Optional< DWARFFormValue > getAttributeValue(const uint32_t DIEOffset, const dwarf::Attribute Attr, const DWARFUnit &U) const
Extract a DWARF form value from a DIE specified by DIE offset.
Optional< size_t > getFixedAttributesByteSize(const DWARFUnit &U) const
#define F(x, y, z)
Definition: MD5.cpp:55
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
#define A
Definition: LargeTest.cpp:12
dwarf::Attribute getAttrByIndex(uint32_t idx) const
A range adaptor for a pair of iterators.
AttributeSpec(dwarf::Attribute A, dwarf::Form F, Optional< int64_t > V)
Optional< int64_t > getByteSize(const DWARFUnit &U) const
Get the fixed byte size of this Form if possible.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
Optional< uint32_t > findAttributeIndex(dwarf::Attribute attr) const
Get the index of the specified attribute.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
dwarf::Form getFormByIndex(uint32_t idx) const
SmallVector< AttributeSpec, 8 > AttributeSpecVector
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
iterator_range< AttributeSpecVector::const_iterator > attr_iterator_range