blob: 0db86b09d19aad0d61c37babd13b67317a35d99c [file] [log] [blame]
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001//===--- lib/CodeGen/DebugLocStream.h - DWARF debug_loc stream --*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCSTREAM_H
10#define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCSTREAM_H
11
Chandler Carruth6bda14b2017-06-06 11:49:48 +000012#include "ByteStreamer.h"
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000013#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/SmallVector.h"
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000015
16namespace llvm {
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +000017
18class AsmPrinter;
19class DbgVariable;
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000020class DwarfCompileUnit;
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +000021class MachineInstr;
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000022class MCSymbol;
23
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000024/// Byte stream of .debug_loc entries.
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000025///
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///
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000029/// FIXME: Do we need all these temp symbols?
30/// FIXME: Why not output directly to the output stream?
31class DebugLocStream {
32public:
33 struct List {
34 DwarfCompileUnit *CU;
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +000035 MCSymbol *Label = nullptr;
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000036 size_t EntryOffset;
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +000037 List(DwarfCompileUnit *CU, size_t EntryOffset)
38 : CU(CU), EntryOffset(EntryOffset) {}
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000039 };
40 struct Entry {
David Blaikie11e0bcf82019-10-02 22:58:02 +000041 const MCSymbol *Begin;
42 const MCSymbol *End;
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000043 size_t ByteOffset;
44 size_t CommentOffset;
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000045 };
46
47private:
48 SmallVector<List, 4> Lists;
49 SmallVector<Entry, 32> Entries;
50 SmallString<256> DWARFBytes;
David Stenbergd46ac442019-10-15 09:21:09 +000051 std::vector<std::string> Comments;
David Blaikie2941cda2019-10-17 23:02:19 +000052 MCSymbol *Sym;
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000053
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000054 /// Only verbose textual output needs comments. This will be set to
Pete Coopera05c0822015-05-20 22:51:27 +000055 /// true for that case, and false otherwise.
56 bool GenerateComments;
57
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000058public:
Pete Coopera05c0822015-05-20 22:51:27 +000059 DebugLocStream(bool GenerateComments) : GenerateComments(GenerateComments) { }
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000060 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; }
David Blaikie2941cda2019-10-17 23:02:19 +000063 MCSymbol *getSym() const {
64 return Sym;
65 }
66 void setSym(MCSymbol *Sym) {
67 this->Sym = Sym;
68 }
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000069
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +000070 class ListBuilder;
71 class EntryBuilder;
72
73private:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000074 /// Start a new .debug_loc entry list.
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000075 ///
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.
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +000080 size_t startList(DwarfCompileUnit *CU) {
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000081 size_t LI = Lists.size();
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +000082 Lists.emplace_back(CU, Entries.size());
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000083 return LI;
84 }
85
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +000086 /// 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000094 /// Start a new .debug_loc entry.
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000095 ///
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) {
David Blaikie11e0bcf82019-10-02 22:58:02 +000099 Entries.push_back({BeginSym, EndSym, DWARFBytes.size(), Comments.size()});
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000100 }
101
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +0000102 /// Finalize a .debug_loc entry, deleting if it's empty.
103 void finalizeEntry();
104
105public:
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000106 BufferByteStreamer getStreamer() {
Pete Coopera05c0822015-05-20 22:51:27 +0000107 return BufferByteStreamer(DWARFBytes, Comments, GenerateComments);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000108 }
109
110 ArrayRef<Entry> getEntries(const List &L) const {
111 size_t LI = getIndex(L);
112 return makeArrayRef(Entries)
113 .slice(Lists[LI].EntryOffset, getNumEntries(LI));
114 }
115
116 ArrayRef<char> getBytes(const Entry &E) const {
117 size_t EI = getIndex(E);
118 return makeArrayRef(DWARFBytes.begin(), DWARFBytes.end())
119 .slice(Entries[EI].ByteOffset, getNumBytes(EI));
120 }
121 ArrayRef<std::string> getComments(const Entry &E) const {
122 size_t EI = getIndex(E);
123 return makeArrayRef(Comments)
124 .slice(Entries[EI].CommentOffset, getNumComments(EI));
125 }
126
127private:
128 size_t getIndex(const List &L) const {
129 assert(&Lists.front() <= &L && &L <= &Lists.back() &&
130 "Expected valid list");
131 return &L - &Lists.front();
132 }
133 size_t getIndex(const Entry &E) const {
134 assert(&Entries.front() <= &E && &E <= &Entries.back() &&
135 "Expected valid entry");
136 return &E - &Entries.front();
137 }
138 size_t getNumEntries(size_t LI) const {
139 if (LI + 1 == Lists.size())
140 return Entries.size() - Lists[LI].EntryOffset;
141 return Lists[LI + 1].EntryOffset - Lists[LI].EntryOffset;
142 }
143 size_t getNumBytes(size_t EI) const {
144 if (EI + 1 == Entries.size())
145 return DWARFBytes.size() - Entries[EI].ByteOffset;
146 return Entries[EI + 1].ByteOffset - Entries[EI].ByteOffset;
147 }
148 size_t getNumComments(size_t EI) const {
149 if (EI + 1 == Entries.size())
150 return Comments.size() - Entries[EI].CommentOffset;
151 return Entries[EI + 1].CommentOffset - Entries[EI].CommentOffset;
152 }
153};
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +0000154
155/// Builder for DebugLocStream lists.
156class DebugLocStream::ListBuilder {
157 DebugLocStream &Locs;
158 AsmPrinter &Asm;
159 DbgVariable &V;
160 const MachineInstr &MI;
161 size_t ListIndex;
162
163public:
164 ListBuilder(DebugLocStream &Locs, DwarfCompileUnit &CU, AsmPrinter &Asm,
165 DbgVariable &V, const MachineInstr &MI)
166 : Locs(Locs), Asm(Asm), V(V), MI(MI), ListIndex(Locs.startList(&CU)) {}
167
168 /// Finalize the list.
169 ///
170 /// If the list is empty, delete it. Otherwise, finalize it by creating a
171 /// temp symbol in \a Asm and setting up the \a DbgVariable.
172 ~ListBuilder();
173
174 DebugLocStream &getLocs() { return Locs; }
175};
176
177/// Builder for DebugLocStream entries.
178class DebugLocStream::EntryBuilder {
179 DebugLocStream &Locs;
180
181public:
182 EntryBuilder(ListBuilder &List, const MCSymbol *Begin, const MCSymbol *End)
183 : Locs(List.getLocs()) {
184 Locs.startEntry(Begin, End);
185 }
186
187 /// Finalize the entry, deleting it if it's empty.
188 ~EntryBuilder() { Locs.finalizeEntry(); }
189
190 BufferByteStreamer getStreamer() { return Locs.getStreamer(); }
191};
192
Alexander Kornienko70bc5f12015-06-19 15:57:42 +0000193} // namespace llvm
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +0000194
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000195#endif