LLVM 18.0.0git
DIEGenerator.h
Go to the documentation of this file.
1//===- DIEGenerator.h -------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIB_DWARFLINKERPARALLEL_DIEGENERATOR_H
10#define LLVM_LIB_DWARFLINKERPARALLEL_DIEGENERATOR_H
11
13#include "DWARFLinkerUnit.h"
14#include "llvm/CodeGen/DIE.h"
15#include "llvm/Support/LEB128.h"
16
17namespace llvm {
18namespace dwarflinker_parallel {
19
20/// This class is a helper to create output DIE tree.
22public:
24 : Allocator(Allocator), CU(CU) {}
25
28
29 /// Creates a DIE of specified tag \p DieTag and \p OutOffset.
30 DIE *createDIE(dwarf::Tag DieTag, uint32_t OutOffset) {
31 OutputDIE = DIE::get(Allocator, DieTag);
32
33 OutputDIE->setOffset(OutOffset);
34
35 return OutputDIE;
36 }
37
38 DIE *getDIE() { return OutputDIE; }
39
40 /// Adds a specified \p Child to the current DIE.
41 void addChild(DIE *Child) {
42 assert(Child != nullptr);
43 assert(OutputDIE != nullptr);
44
45 OutputDIE->addChild(Child);
46 }
47
48 /// Adds specified scalar attribute to the current DIE.
49 std::pair<DIEValue &, size_t> addScalarAttribute(dwarf::Attribute Attr,
50 dwarf::Form AttrForm,
52 return addAttribute(Attr, AttrForm, DIEInteger(Value));
53 }
54
55 /// Adds specified location attribute to the current DIE.
56 std::pair<DIEValue &, size_t> addLocationAttribute(dwarf::Attribute Attr,
57 dwarf::Form AttrForm,
58 ArrayRef<uint8_t> Bytes) {
59 DIELoc *Loc = new (Allocator) DIELoc;
60 for (auto Byte : Bytes)
61 static_cast<DIEValueList *>(Loc)->addValue(
62 Allocator, static_cast<dwarf::Attribute>(0), dwarf::DW_FORM_data1,
63 DIEInteger(Byte));
64 Loc->setSize(Bytes.size());
65
66 return addAttribute(Attr, AttrForm, Loc);
67 }
68
69 /// Adds specified block or exprloc attribute to the current DIE.
70 std::pair<DIEValue &, size_t> addBlockAttribute(dwarf::Attribute Attr,
71 dwarf::Form AttrForm,
72 ArrayRef<uint8_t> Bytes) {
73 // The expression location data might be updated and exceed the original
74 // size. Check whether the new data fits into the original form.
75 assert((AttrForm == dwarf::DW_FORM_block) ||
76 (AttrForm == dwarf::DW_FORM_exprloc) ||
77 (AttrForm == dwarf::DW_FORM_block1 && Bytes.size() <= UINT8_MAX) ||
78 (AttrForm == dwarf::DW_FORM_block2 && Bytes.size() <= UINT16_MAX) ||
79 (AttrForm == dwarf::DW_FORM_block4 && Bytes.size() <= UINT32_MAX));
80
82 for (auto Byte : Bytes)
83 static_cast<DIEValueList *>(Block)->addValue(
84 Allocator, static_cast<dwarf::Attribute>(0), dwarf::DW_FORM_data1,
85 DIEInteger(Byte));
86 Block->setSize(Bytes.size());
87
88 return addAttribute(Attr, AttrForm, Block);
89 }
90
91 /// Adds specified location list attribute to the current DIE.
92 std::pair<DIEValue &, size_t> addLocListAttribute(dwarf::Attribute Attr,
93 dwarf::Form AttrForm,
95 return addAttribute(Attr, AttrForm, DIELocList(Value));
96 }
97
98 /// Adds indexed string attribute.
99 std::pair<DIEValue &, size_t> addIndexedStringAttribute(dwarf::Attribute Attr,
100 dwarf::Form AttrForm,
101 uint64_t Idx) {
102 assert(AttrForm == dwarf::DW_FORM_strx);
103 return addAttribute(Attr, AttrForm, DIEInteger(Idx));
104 }
105
106 /// Adds string attribute with dummy offset to the current DIE.
107 std::pair<DIEValue &, size_t>
109 assert(AttrForm == dwarf::DW_FORM_strp ||
110 AttrForm == dwarf::DW_FORM_line_strp);
111 return addAttribute(Attr, AttrForm, DIEInteger(0xBADDEF));
112 }
113
114 /// Adds inplace string attribute to the current DIE.
115 std::pair<DIEValue &, size_t> addInplaceString(dwarf::Attribute Attr,
118 for (auto Byte : String.bytes())
119 static_cast<DIEValueList *>(Block)->addValue(
120 Allocator, static_cast<dwarf::Attribute>(0), dwarf::DW_FORM_data1,
121 DIEInteger(Byte));
122
123 static_cast<DIEValueList *>(Block)->addValue(
124 Allocator, static_cast<dwarf::Attribute>(0), dwarf::DW_FORM_data1,
125 DIEInteger(0));
126 Block->setSize(String.size() + 1);
127
128 DIEValue &ValueRef =
129 *OutputDIE->addValue(Allocator, Attr, dwarf::DW_FORM_string, Block);
130 return std::pair<DIEValue &, size_t>(ValueRef, String.size() + 1);
131 }
132
133 /// Creates appreviations for the current DIE. Returns value of
134 /// abbreviation number. Updates offsets with the size of abbreviation
135 /// number.
136 size_t finalizeAbbreviations(bool CHILDREN_yes,
137 OffsetsPtrVector *OffsetsList) {
138 // Create abbreviations for output DIE.
139 DIEAbbrev NewAbbrev = OutputDIE->generateAbbrev();
140 if (CHILDREN_yes)
142
143 CU.assignAbbrev(NewAbbrev);
144 OutputDIE->setAbbrevNumber(NewAbbrev.getNumber());
145
146 size_t AbbrevNumberSize = getULEB128Size(OutputDIE->getAbbrevNumber());
147
148 // Add size of abbreviation number to the offsets.
149 if (OffsetsList != nullptr) {
150 for (uint64_t *OffsetPtr : *OffsetsList)
151 *OffsetPtr += AbbrevNumberSize;
152 }
153
154 return AbbrevNumberSize;
155 }
156
157protected:
158 template <typename T>
159 std::pair<DIEValue &, size_t> addAttribute(dwarf::Attribute Attr,
160 dwarf::Form AttrForm, T &&Value) {
161 DIEValue &ValueRef =
162 *OutputDIE->addValue(Allocator, Attr, AttrForm, std::forward<T>(Value));
163 unsigned ValueSize = ValueRef.sizeOf(CU.getFormParams());
164 return std::pair<DIEValue &, size_t>(ValueRef, ValueSize);
165 }
166
167 // Allocator for output DIEs and values.
169
170 // Unit for the output DIE.
172
173 // OutputDIE.
174 DIE *OutputDIE = nullptr;
175};
176
177} // end of namespace dwarflinker_parallel
178} // end namespace llvm
179
180#endif // LLVM_LIB_DWARFLINKERPARALLEL_DIEGENERATOR_H
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
Dwarf abbreviation, describes the organization of a debug information object.
Definition: DIE.h:79
unsigned getNumber() const
Definition: DIE.h:101
void setChildrenFlag(bool hasChild)
Definition: DIE.h:104
DIEBlock - Represents a block of values.
Definition: DIE.h:1046
An integer value DIE.
Definition: DIE.h:168
Represents a pointer to a location list in the debug_loc section.
Definition: DIE.h:337
DIELoc - Represents an expression location.
Definition: DIE.h:1010
void setSize(unsigned size)
Definition: DIE.h:1020
A list of DIE values.
Definition: DIE.h:689
value_iterator addValue(BumpPtrAllocator &Alloc, const DIEValue &V)
Definition: DIE.h:740
unsigned sizeOf(const dwarf::FormParams &FormParams) const
Return the size of a value in bytes.
Definition: DIE.cpp:331
A structured debug information entry.
Definition: DIE.h:819
unsigned getAbbrevNumber() const
Definition: DIE.h:854
DIE & addChild(DIE *Child)
Add a child to the DIE.
Definition: DIE.h:934
DIEAbbrev generateAbbrev() const
Generate the abbreviation for this DIE.
Definition: DIE.cpp:178
static DIE * get(BumpPtrAllocator &Alloc, dwarf::Tag Tag)
Definition: DIE.h:849
void setAbbrevNumber(unsigned I)
Set the abbreviation number for this DIE.
Definition: DIE.h:891
void setOffset(unsigned O)
Definition: DIE.h:930
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
LLVM Value Representation.
Definition: Value.h:74
This class is a helper to create output DIE tree.
Definition: DIEGenerator.h:21
std::pair< DIEValue &, size_t > addInplaceString(dwarf::Attribute Attr, StringRef String)
Adds inplace string attribute to the current DIE.
Definition: DIEGenerator.h:115
std::pair< DIEValue &, size_t > addAttribute(dwarf::Attribute Attr, dwarf::Form AttrForm, T &&Value)
Definition: DIEGenerator.h:159
DIEGenerator(DIE *OutputDIE, BumpPtrAllocator &Allocator, DwarfUnit &CU)
Definition: DIEGenerator.h:26
std::pair< DIEValue &, size_t > addBlockAttribute(dwarf::Attribute Attr, dwarf::Form AttrForm, ArrayRef< uint8_t > Bytes)
Adds specified block or exprloc attribute to the current DIE.
Definition: DIEGenerator.h:70
std::pair< DIEValue &, size_t > addLocationAttribute(dwarf::Attribute Attr, dwarf::Form AttrForm, ArrayRef< uint8_t > Bytes)
Adds specified location attribute to the current DIE.
Definition: DIEGenerator.h:56
DIE * createDIE(dwarf::Tag DieTag, uint32_t OutOffset)
Creates a DIE of specified tag DieTag and OutOffset.
Definition: DIEGenerator.h:30
void addChild(DIE *Child)
Adds a specified Child to the current DIE.
Definition: DIEGenerator.h:41
DIEGenerator(BumpPtrAllocator &Allocator, DwarfUnit &CU)
Definition: DIEGenerator.h:23
size_t finalizeAbbreviations(bool CHILDREN_yes, OffsetsPtrVector *OffsetsList)
Creates appreviations for the current DIE.
Definition: DIEGenerator.h:136
std::pair< DIEValue &, size_t > addStringPlaceholderAttribute(dwarf::Attribute Attr, dwarf::Form AttrForm)
Adds string attribute with dummy offset to the current DIE.
Definition: DIEGenerator.h:108
std::pair< DIEValue &, size_t > addScalarAttribute(dwarf::Attribute Attr, dwarf::Form AttrForm, uint64_t Value)
Adds specified scalar attribute to the current DIE.
Definition: DIEGenerator.h:49
std::pair< DIEValue &, size_t > addIndexedStringAttribute(dwarf::Attribute Attr, dwarf::Form AttrForm, uint64_t Idx)
Adds indexed string attribute.
Definition: DIEGenerator.h:99
std::pair< DIEValue &, size_t > addLocListAttribute(dwarf::Attribute Attr, dwarf::Form AttrForm, uint64_t Value)
Adds specified location list attribute to the current DIE.
Definition: DIEGenerator.h:92
Base class for all Dwarf units(Compile unit/Type table unit).
Attribute
Attributes.
Definition: Dwarf.h:123
@ DW_CHILDREN_yes
Definition: Dwarf.h:521
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
unsigned getULEB128Size(uint64_t Value)
Utility function to get the size of the ULEB128-encoded value.
Definition: LEB128.cpp:19