LLVM 18.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 MachineInstr;
22class MCSymbol;
23
24/// Byte stream of .debug_loc entries.
25///
26/// Stores a unified stream of .debug_loc entries. There's \a List for each
27/// variable/inlined-at pair, and an \a Entry for each \a DebugLocEntry.
28///
29/// FIXME: Do we need all these temp symbols?
30/// FIXME: Why not output directly to the output stream?
32public:
33 struct List {
35 MCSymbol *Label = nullptr;
39 };
40 struct Entry {
42 const MCSymbol *End;
43 size_t ByteOffset;
45 };
46
47private:
50 SmallString<256> DWARFBytes;
51 std::vector<std::string> Comments;
52 MCSymbol *Sym = nullptr;
53
54 /// Only verbose textual output needs comments. This will be set to
55 /// true for that case, and false otherwise.
56 bool GenerateComments;
57
58public:
59 DebugLocStream(bool GenerateComments) : GenerateComments(GenerateComments) { }
60 size_t getNumLists() const { return Lists.size(); }
61 const List &getList(size_t LI) const { return Lists[LI]; }
62 ArrayRef<List> getLists() const { return Lists; }
63 MCSymbol *getSym() const {
64 return Sym;
65 }
66 void setSym(MCSymbol *Sym) {
67 this->Sym = Sym;
68 }
69
70 class ListBuilder;
71 class EntryBuilder;
72
73private:
74 /// Start a new .debug_loc entry list.
75 ///
76 /// Start a new .debug_loc entry list. Return the new list's index so it can
77 /// be retrieved later via \a getList().
78 ///
79 /// Until the next call, \a startEntry() will add entries to this list.
80 size_t startList(DwarfCompileUnit *CU) {
81 size_t LI = Lists.size();
82 Lists.emplace_back(CU, Entries.size());
83 return LI;
84 }
85
86 /// Finalize a .debug_loc entry list.
87 ///
88 /// If there are no entries in this list, delete it outright. Otherwise,
89 /// create a label with \a Asm.
90 ///
91 /// \return false iff the list is deleted.
92 bool finalizeList(AsmPrinter &Asm);
93
94 /// Start a new .debug_loc entry.
95 ///
96 /// Until the next call, bytes added to the stream will be added to this
97 /// entry.
98 void startEntry(const MCSymbol *BeginSym, const MCSymbol *EndSym) {
99 Entries.push_back({BeginSym, EndSym, DWARFBytes.size(), Comments.size()});
100 }
101
102 /// Finalize a .debug_loc entry, deleting if it's empty.
103 void finalizeEntry();
104
105public:
107 return BufferByteStreamer(DWARFBytes, Comments, GenerateComments);
108 }
109
111 size_t LI = getIndex(L);
112 return ArrayRef(Entries).slice(Lists[LI].EntryOffset, getNumEntries(LI));
113 }
114
116 size_t EI = getIndex(E);
117 return ArrayRef(DWARFBytes.begin(), DWARFBytes.end())
118 .slice(Entries[EI].ByteOffset, getNumBytes(EI));
119 }
121 size_t EI = getIndex(E);
122 return ArrayRef(Comments).slice(Entries[EI].CommentOffset,
123 getNumComments(EI));
124 }
125
126private:
127 size_t getIndex(const List &L) const {
128 assert(&Lists.front() <= &L && &L <= &Lists.back() &&
129 "Expected valid list");
130 return &L - &Lists.front();
131 }
132 size_t getIndex(const Entry &E) const {
133 assert(&Entries.front() <= &E && &E <= &Entries.back() &&
134 "Expected valid entry");
135 return &E - &Entries.front();
136 }
137 size_t getNumEntries(size_t LI) const {
138 if (LI + 1 == Lists.size())
139 return Entries.size() - Lists[LI].EntryOffset;
140 return Lists[LI + 1].EntryOffset - Lists[LI].EntryOffset;
141 }
142 size_t getNumBytes(size_t EI) const {
143 if (EI + 1 == Entries.size())
144 return DWARFBytes.size() - Entries[EI].ByteOffset;
145 return Entries[EI + 1].ByteOffset - Entries[EI].ByteOffset;
146 }
147 size_t getNumComments(size_t EI) const {
148 if (EI + 1 == Entries.size())
149 return Comments.size() - Entries[EI].CommentOffset;
150 return Entries[EI + 1].CommentOffset - Entries[EI].CommentOffset;
151 }
152};
153
154/// Builder for DebugLocStream lists.
156 DebugLocStream &Locs;
157 AsmPrinter &Asm;
158 DbgVariable &V;
159 size_t ListIndex;
160 std::optional<uint8_t> TagOffset;
161
162public:
164 DbgVariable &V)
165 : Locs(Locs), Asm(Asm), V(V), ListIndex(Locs.startList(&CU)),
166 TagOffset(std::nullopt) {}
167
168 void setTagOffset(uint8_t TO) {
169 TagOffset = TO;
170 }
171
172 /// Finalize the list.
173 ///
174 /// If the list is empty, delete it. Otherwise, finalize it by creating a
175 /// temp symbol in \a Asm and setting up the \a DbgVariable.
176 ~ListBuilder();
177
178 DebugLocStream &getLocs() { return Locs; }
179};
180
181/// Builder for DebugLocStream entries.
183 DebugLocStream &Locs;
184
185public:
187 : Locs(List.getLocs()) {
188 Locs.startEntry(Begin, End);
189 }
190
191 /// Finalize the entry, deleting it if it's empty.
192 ~EntryBuilder() { Locs.finalizeEntry(); }
193
195};
196
197} // namespace llvm
198
199#endif
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool End
Definition: ELF_riscv.cpp:478
Symbol * Sym
Definition: ELF_riscv.cpp:477
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:84
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:40
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:941
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
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