LLVM 20.0.0git
DebugLocStream.h
Go to the documentation of this file.
1//===--- lib/CodeGen/DebugLocStream.h - DWARF debug_loc stream --*- 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_CODEGEN_ASMPRINTER_DEBUGLOCSTREAM_H
10#define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCSTREAM_H
11
12#include "ByteStreamer.h"
13#include "llvm/ADT/ArrayRef.h"
15
16namespace llvm {
17
18class AsmPrinter;
19class DbgVariable;
20class DwarfCompileUnit;
21class MCSymbol;
22
23/// Byte stream of .debug_loc entries.
24///
25/// Stores a unified stream of .debug_loc entries. There's \a List for each
26/// variable/inlined-at pair, and an \a Entry for each \a DebugLocEntry.
27///
28/// FIXME: Do we need all these temp symbols?
29/// FIXME: Why not output directly to the output stream?
31public:
32 struct List {
34 MCSymbol *Label = nullptr;
38 };
39 struct Entry {
41 const MCSymbol *End;
42 size_t ByteOffset;
44 };
45
46private:
49 SmallString<256> DWARFBytes;
50 std::vector<std::string> Comments;
51 MCSymbol *Sym = nullptr;
52
53 /// Only verbose textual output needs comments. This will be set to
54 /// true for that case, and false otherwise.
55 bool GenerateComments;
56
57public:
58 DebugLocStream(bool GenerateComments) : GenerateComments(GenerateComments) { }
59 size_t getNumLists() const { return Lists.size(); }
60 const List &getList(size_t LI) const { return Lists[LI]; }
61 ArrayRef<List> getLists() const { return Lists; }
62 MCSymbol *getSym() const {
63 return Sym;
64 }
65 void setSym(MCSymbol *Sym) {
66 this->Sym = Sym;
67 }
68
69 class ListBuilder;
70 class EntryBuilder;
71
72private:
73 /// Start a new .debug_loc entry list.
74 ///
75 /// Start a new .debug_loc entry list. Return the new list's index so it can
76 /// be retrieved later via \a getList().
77 ///
78 /// Until the next call, \a startEntry() will add entries to this list.
79 size_t startList(DwarfCompileUnit *CU) {
80 size_t LI = Lists.size();
81 Lists.emplace_back(CU, Entries.size());
82 return LI;
83 }
84
85 /// Finalize a .debug_loc entry list.
86 ///
87 /// If there are no entries in this list, delete it outright. Otherwise,
88 /// create a label with \a Asm.
89 ///
90 /// \return false iff the list is deleted.
91 bool finalizeList(AsmPrinter &Asm);
92
93 /// Start a new .debug_loc entry.
94 ///
95 /// Until the next call, bytes added to the stream will be added to this
96 /// entry.
97 void startEntry(const MCSymbol *BeginSym, const MCSymbol *EndSym) {
98 Entries.push_back({BeginSym, EndSym, DWARFBytes.size(), Comments.size()});
99 }
100
101 /// Finalize a .debug_loc entry, deleting if it's empty.
102 void finalizeEntry();
103
104public:
106 return BufferByteStreamer(DWARFBytes, Comments, GenerateComments);
107 }
108
110 size_t LI = getIndex(L);
111 return ArrayRef(Entries).slice(Lists[LI].EntryOffset, getNumEntries(LI));
112 }
113
115 size_t EI = getIndex(E);
116 return ArrayRef(DWARFBytes.begin(), DWARFBytes.end())
117 .slice(Entries[EI].ByteOffset, getNumBytes(EI));
118 }
120 size_t EI = getIndex(E);
121 return ArrayRef(Comments).slice(Entries[EI].CommentOffset,
122 getNumComments(EI));
123 }
124
125private:
126 size_t getIndex(const List &L) const {
127 assert(&Lists.front() <= &L && &L <= &Lists.back() &&
128 "Expected valid list");
129 return &L - &Lists.front();
130 }
131 size_t getIndex(const Entry &E) const {
132 assert(&Entries.front() <= &E && &E <= &Entries.back() &&
133 "Expected valid entry");
134 return &E - &Entries.front();
135 }
136 size_t getNumEntries(size_t LI) const {
137 if (LI + 1 == Lists.size())
138 return Entries.size() - Lists[LI].EntryOffset;
139 return Lists[LI + 1].EntryOffset - Lists[LI].EntryOffset;
140 }
141 size_t getNumBytes(size_t EI) const {
142 if (EI + 1 == Entries.size())
143 return DWARFBytes.size() - Entries[EI].ByteOffset;
144 return Entries[EI + 1].ByteOffset - Entries[EI].ByteOffset;
145 }
146 size_t getNumComments(size_t EI) const {
147 if (EI + 1 == Entries.size())
148 return Comments.size() - Entries[EI].CommentOffset;
149 return Entries[EI + 1].CommentOffset - Entries[EI].CommentOffset;
150 }
151};
152
153/// Builder for DebugLocStream lists.
155 DebugLocStream &Locs;
156 AsmPrinter &Asm;
157 DbgVariable &V;
158 size_t ListIndex;
159 std::optional<uint8_t> TagOffset;
160
161public:
163 DbgVariable &V)
164 : Locs(Locs), Asm(Asm), V(V), ListIndex(Locs.startList(&CU)),
165 TagOffset(std::nullopt) {}
166
167 void setTagOffset(uint8_t TO) {
168 TagOffset = TO;
169 }
170
171 /// Finalize the list.
172 ///
173 /// If the list is empty, delete it. Otherwise, finalize it by creating a
174 /// temp symbol in \a Asm and setting up the \a DbgVariable.
175 ~ListBuilder();
176
177 DebugLocStream &getLocs() { return Locs; }
178};
179
180/// Builder for DebugLocStream entries.
182 DebugLocStream &Locs;
183
184public:
186 : Locs(List.getLocs()) {
187 Locs.startEntry(Begin, End);
188 }
189
190 /// Finalize the entry, deleting it if it's empty.
191 ~EntryBuilder() { Locs.finalizeEntry(); }
192
194};
195
196} // namespace llvm
197
198#endif
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool End
Definition: ELF_riscv.cpp:480
Symbol * Sym
Definition: ELF_riscv.cpp:479
static const SCEV * getNumBytes(const SCEV *BECount, Type *IntPtr, const SCEV *StoreSizeSCEV, Loop *CurLoop, const DataLayout *DL, ScalarEvolution *SE)
Compute the number of bytes as a SCEV from the backedge taken count.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition: ArrayRef.h:195
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:86
This class is used to track local variable information.
Definition: DwarfDebug.h:214
Builder for DebugLocStream entries.
EntryBuilder(ListBuilder &List, const MCSymbol *Begin, const MCSymbol *End)
~EntryBuilder()
Finalize the entry, deleting it if it's empty.
Builder for DebugLocStream lists.
ListBuilder(DebugLocStream &Locs, DwarfCompileUnit &CU, AsmPrinter &Asm, DbgVariable &V)
Byte stream of .debug_loc entries.
const List & getList(size_t LI) const
ArrayRef< std::string > getComments(const Entry &E) const
BufferByteStreamer getStreamer()
ArrayRef< Entry > getEntries(const List &L) const
ArrayRef< char > getBytes(const Entry &E) const
MCSymbol * getSym() const
DebugLocStream(bool GenerateComments)
size_t getNumLists() const
void setSym(MCSymbol *Sym)
ArrayRef< List > getLists() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
size_t size() const
Definition: SmallVector.h:91
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
List(DwarfCompileUnit *CU, size_t EntryOffset)
DwarfCompileUnit * CU