LLVM 20.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_DWARFLINKER_PARALLEL_DIEGENERATOR_H
10#define LLVM_LIB_DWARFLINKER_PARALLEL_DIEGENERATOR_H
11
13#include "DWARFLinkerUnit.h"
14#include "llvm/CodeGen/DIE.h"
15#include "llvm/Support/LEB128.h"
16
17namespace llvm {
18namespace dwarf_linker {
19namespace parallel {
20
21/// This class is a helper to create output DIE tree.
23public:
25 : Allocator(Allocator), CU(CU) {}
26
29
30 /// Creates a DIE of specified tag \p DieTag and \p OutOffset.
31 DIE *createDIE(dwarf::Tag DieTag, uint32_t OutOffset) {
32 OutputDIE = DIE::get(Allocator, DieTag);
33
34 OutputDIE->setOffset(OutOffset);
35
36 return OutputDIE;
37 }
38
39 DIE *getDIE() { return OutputDIE; }
40
41 /// Adds a specified \p Child to the current DIE.
42 void addChild(DIE *Child) {
43 assert(Child != nullptr);
44 assert(OutputDIE != nullptr);
45
46 OutputDIE->addChild(Child);
47 }
48
49 /// Adds specified scalar attribute to the current DIE.
50 std::pair<DIEValue &, size_t> addScalarAttribute(dwarf::Attribute Attr,
51 dwarf::Form AttrForm,
53 return addAttribute(Attr, AttrForm, DIEInteger(Value));
54 }
55
56 /// Adds specified location attribute to the current DIE.
57 std::pair<DIEValue &, size_t> addLocationAttribute(dwarf::Attribute Attr,
58 dwarf::Form AttrForm,
59 ArrayRef<uint8_t> Bytes) {
60 DIELoc *Loc = new (Allocator) DIELoc;
61 for (auto Byte : Bytes)
62 static_cast<DIEValueList *>(Loc)->addValue(
63 Allocator, static_cast<dwarf::Attribute>(0), dwarf::DW_FORM_data1,
64 DIEInteger(Byte));
65 Loc->setSize(Bytes.size());
66
67 return addAttribute(Attr, AttrForm, Loc);
68 }
69
70 /// Adds specified block or exprloc attribute to the current DIE.
71 std::pair<DIEValue &, size_t> addBlockAttribute(dwarf::Attribute Attr,
72 dwarf::Form AttrForm,
73 ArrayRef<uint8_t> Bytes) {
74 // The expression location data might be updated and exceed the original
75 // size. Check whether the new data fits into the original form.
76 assert((AttrForm == dwarf::DW_FORM_block) ||
77 (AttrForm == dwarf::DW_FORM_exprloc) ||
78 (AttrForm == dwarf::DW_FORM_block1 && Bytes.size() <= UINT8_MAX) ||
79 (AttrForm == dwarf::DW_FORM_block2 && Bytes.size() <= UINT16_MAX) ||
80 (AttrForm == dwarf::DW_FORM_block4 && Bytes.size() <= UINT32_MAX));
81
83 for (auto Byte : Bytes)
84 static_cast<DIEValueList *>(Block)->addValue(
85 Allocator, static_cast<dwarf::Attribute>(0), dwarf::DW_FORM_data1,
86 DIEInteger(Byte));
87 Block->setSize(Bytes.size());
88
89 return addAttribute(Attr, AttrForm, Block);
90 }
91
92 /// Adds specified location list attribute to the current DIE.
93 std::pair<DIEValue &, size_t> addLocListAttribute(dwarf::Attribute Attr,
94 dwarf::Form AttrForm,
96 return addAttribute(Attr, AttrForm, DIELocList(Value));
97 }
98
99 /// Adds indexed string attribute.
100 std::pair<DIEValue &, size_t> addIndexedStringAttribute(dwarf::Attribute Attr,
101 dwarf::Form AttrForm,
102 uint64_t Idx) {
103 assert(AttrForm == dwarf::DW_FORM_strx);
104 return addAttribute(Attr, AttrForm, DIEInteger(Idx));
105 }
106
107 /// Adds string attribute with dummy offset to the current DIE.
108 std::pair<DIEValue &, size_t>
110 assert(AttrForm == dwarf::DW_FORM_strp ||
111 AttrForm == dwarf::DW_FORM_line_strp);
112 return addAttribute(Attr, AttrForm, DIEInteger(0xBADDEF));
113 }
114
115 /// Adds inplace string attribute to the current DIE.
116 std::pair<DIEValue &, size_t> addInplaceString(dwarf::Attribute Attr,
119 for (auto Byte : String.bytes())
120 static_cast<DIEValueList *>(Block)->addValue(
121 Allocator, static_cast<dwarf::Attribute>(0), dwarf::DW_FORM_data1,
122 DIEInteger(Byte));
123
124 static_cast<DIEValueList *>(Block)->addValue(
125 Allocator, static_cast<dwarf::Attribute>(0), dwarf::DW_FORM_data1,
126 DIEInteger(0));
127 Block->setSize(String.size() + 1);
128
129 DIEValue &ValueRef =
130 *OutputDIE->addValue(Allocator, Attr, dwarf::DW_FORM_string, Block);
131 return std::pair<DIEValue &, size_t>(ValueRef, String.size() + 1);
132 }
133
134 /// Creates appreviations for the current DIE. Returns value of
135 /// abbreviation number. Updates offsets with the size of abbreviation
136 /// number.
137 size_t finalizeAbbreviations(bool CHILDREN_yes,
138 OffsetsPtrVector *OffsetsList) {
139 // Create abbreviations for output DIE.
140 DIEAbbrev NewAbbrev = OutputDIE->generateAbbrev();
141 if (CHILDREN_yes)
143
144 CU.assignAbbrev(NewAbbrev);
145 OutputDIE->setAbbrevNumber(NewAbbrev.getNumber());
146
147 size_t AbbrevNumberSize = getULEB128Size(OutputDIE->getAbbrevNumber());
148
149 // Add size of abbreviation number to the offsets.
150 if (OffsetsList != nullptr) {
151 for (uint64_t *OffsetPtr : *OffsetsList)
152 *OffsetPtr += AbbrevNumberSize;
153 }
154
155 return AbbrevNumberSize;
156 }
157
158protected:
159 template <typename T>
160 std::pair<DIEValue &, size_t> addAttribute(dwarf::Attribute Attr,
161 dwarf::Form AttrForm, T &&Value) {
162 DIEValue &ValueRef =
163 *OutputDIE->addValue(Allocator, Attr, AttrForm, std::forward<T>(Value));
164 unsigned ValueSize = ValueRef.sizeOf(CU.getFormParams());
165 return std::pair<DIEValue &, size_t>(ValueRef, ValueSize);
166 }
167
168 // Allocator for output DIEs and values.
170
171 // Unit for the output DIE.
173
174 // OutputDIE.
175 DIE *OutputDIE = nullptr;
176};
177
178} // end of namespace parallel
179} // end of namespace dwarf_linker
180} // end of namespace llvm
181
182#endif // LLVM_LIB_DWARFLINKER_PARALLEL_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:327
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:174
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:22
void addChild(DIE *Child)
Adds a specified Child to the current DIE.
Definition: DIEGenerator.h:42
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:57
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:93
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:109
DIEGenerator(DIE *OutputDIE, BumpPtrAllocator &Allocator, DwarfUnit &CU)
Definition: DIEGenerator.h:27
DIE * createDIE(dwarf::Tag DieTag, uint32_t OutOffset)
Creates a DIE of specified tag DieTag and OutOffset.
Definition: DIEGenerator.h:31
DIEGenerator(BumpPtrAllocator &Allocator, DwarfUnit &CU)
Definition: DIEGenerator.h:24
std::pair< DIEValue &, size_t > addAttribute(dwarf::Attribute Attr, dwarf::Form AttrForm, T &&Value)
Definition: DIEGenerator.h:160
std::pair< DIEValue &, size_t > addIndexedStringAttribute(dwarf::Attribute Attr, dwarf::Form AttrForm, uint64_t Idx)
Adds indexed string attribute.
Definition: DIEGenerator.h:100
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:71
std::pair< DIEValue &, size_t > addInplaceString(dwarf::Attribute Attr, StringRef String)
Adds inplace string attribute to the current DIE.
Definition: DIEGenerator.h:116
size_t finalizeAbbreviations(bool CHILDREN_yes, OffsetsPtrVector *OffsetsList)
Creates appreviations for the current DIE.
Definition: DIEGenerator.h:137
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:50
Base class for all Dwarf units(Compile unit/Type table unit).
Attribute
Attributes.
Definition: Dwarf.h:123
@ DW_CHILDREN_yes
Definition: Dwarf.h:838
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