blob: 4ee277a76741198196346d11a44941efd7b9a339 [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;
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000052
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000053 /// Only verbose textual output needs comments. This will be set to
Pete Coopera05c0822015-05-20 22:51:27 +000054 /// true for that case, and false otherwise.
55 bool GenerateComments;
56
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000057public:
Pete Coopera05c0822015-05-20 22:51:27 +000058 DebugLocStream(bool GenerateComments) : GenerateComments(GenerateComments) { }
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000059 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
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +000063 class ListBuilder;
64 class EntryBuilder;
65
66private:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000067 /// Start a new .debug_loc entry list.
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000068 ///
69 /// Start a new .debug_loc entry list. Return the new list's index so it can
70 /// be retrieved later via \a getList().
71 ///
72 /// Until the next call, \a startEntry() will add entries to this list.
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +000073 size_t startList(DwarfCompileUnit *CU) {
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000074 size_t LI = Lists.size();
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +000075 Lists.emplace_back(CU, Entries.size());
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000076 return LI;
77 }
78
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +000079 /// Finalize a .debug_loc entry list.
80 ///
81 /// If there are no entries in this list, delete it outright. Otherwise,
82 /// create a label with \a Asm.
83 ///
84 /// \return false iff the list is deleted.
85 bool finalizeList(AsmPrinter &Asm);
86
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000087 /// Start a new .debug_loc entry.
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000088 ///
89 /// Until the next call, bytes added to the stream will be added to this
90 /// entry.
91 void startEntry(const MCSymbol *BeginSym, const MCSymbol *EndSym) {
David Blaikie11e0bcf82019-10-02 22:58:02 +000092 Entries.push_back({BeginSym, EndSym, DWARFBytes.size(), Comments.size()});
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000093 }
94
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +000095 /// Finalize a .debug_loc entry, deleting if it's empty.
96 void finalizeEntry();
97
98public:
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000099 BufferByteStreamer getStreamer() {
Pete Coopera05c0822015-05-20 22:51:27 +0000100 return BufferByteStreamer(DWARFBytes, Comments, GenerateComments);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000101 }
102
103 ArrayRef<Entry> getEntries(const List &L) const {
104 size_t LI = getIndex(L);
105 return makeArrayRef(Entries)
106 .slice(Lists[LI].EntryOffset, getNumEntries(LI));
107 }
108
109 ArrayRef<char> getBytes(const Entry &E) const {
110 size_t EI = getIndex(E);
111 return makeArrayRef(DWARFBytes.begin(), DWARFBytes.end())
112 .slice(Entries[EI].ByteOffset, getNumBytes(EI));
113 }
114 ArrayRef<std::string> getComments(const Entry &E) const {
115 size_t EI = getIndex(E);
116 return makeArrayRef(Comments)
117 .slice(Entries[EI].CommentOffset, getNumComments(EI));
118 }
119
120private:
121 size_t getIndex(const List &L) const {
122 assert(&Lists.front() <= &L && &L <= &Lists.back() &&
123 "Expected valid list");
124 return &L - &Lists.front();
125 }
126 size_t getIndex(const Entry &E) const {
127 assert(&Entries.front() <= &E && &E <= &Entries.back() &&
128 "Expected valid entry");
129 return &E - &Entries.front();
130 }
131 size_t getNumEntries(size_t LI) const {
132 if (LI + 1 == Lists.size())
133 return Entries.size() - Lists[LI].EntryOffset;
134 return Lists[LI + 1].EntryOffset - Lists[LI].EntryOffset;
135 }
136 size_t getNumBytes(size_t EI) const {
137 if (EI + 1 == Entries.size())
138 return DWARFBytes.size() - Entries[EI].ByteOffset;
139 return Entries[EI + 1].ByteOffset - Entries[EI].ByteOffset;
140 }
141 size_t getNumComments(size_t EI) const {
142 if (EI + 1 == Entries.size())
143 return Comments.size() - Entries[EI].CommentOffset;
144 return Entries[EI + 1].CommentOffset - Entries[EI].CommentOffset;
145 }
146};
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +0000147
148/// Builder for DebugLocStream lists.
149class DebugLocStream::ListBuilder {
150 DebugLocStream &Locs;
151 AsmPrinter &Asm;
152 DbgVariable &V;
153 const MachineInstr &MI;
154 size_t ListIndex;
155
156public:
157 ListBuilder(DebugLocStream &Locs, DwarfCompileUnit &CU, AsmPrinter &Asm,
158 DbgVariable &V, const MachineInstr &MI)
159 : Locs(Locs), Asm(Asm), V(V), MI(MI), ListIndex(Locs.startList(&CU)) {}
160
161 /// Finalize the list.
162 ///
163 /// If the list is empty, delete it. Otherwise, finalize it by creating a
164 /// temp symbol in \a Asm and setting up the \a DbgVariable.
165 ~ListBuilder();
166
167 DebugLocStream &getLocs() { return Locs; }
168};
169
170/// Builder for DebugLocStream entries.
171class DebugLocStream::EntryBuilder {
172 DebugLocStream &Locs;
173
174public:
175 EntryBuilder(ListBuilder &List, const MCSymbol *Begin, const MCSymbol *End)
176 : Locs(List.getLocs()) {
177 Locs.startEntry(Begin, End);
178 }
179
180 /// Finalize the entry, deleting it if it's empty.
181 ~EntryBuilder() { Locs.finalizeEntry(); }
182
183 BufferByteStreamer getStreamer() { return Locs.getStreamer(); }
184};
185
Alexander Kornienko70bc5f12015-06-19 15:57:42 +0000186} // namespace llvm
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +0000187
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000188#endif