1 //===--- lib/CodeGen/DebugLocStream.h - DWARF debug_loc stream --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCSTREAM_H
11 #define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCSTREAM_H
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "ByteStreamer.h"
18 class DwarfCompileUnit;
21 /// \brief Byte stream of .debug_loc entries.
23 /// Stores a unified stream of .debug_loc entries. There's \a List for each
24 /// variable/inlined-at pair, and an \a Entry for each \a DebugLocEntry.
26 /// FIXME: Do we need all these temp symbols?
27 /// FIXME: Why not output directly to the output stream?
28 class DebugLocStream {
34 List(DwarfCompileUnit *CU, MCSymbol *Label, size_t EntryOffset)
35 : CU(CU), Label(Label), EntryOffset(EntryOffset) {}
38 const MCSymbol *BeginSym;
39 const MCSymbol *EndSym;
42 Entry(const MCSymbol *BeginSym, const MCSymbol *EndSym, size_t ByteOffset,
44 : BeginSym(BeginSym), EndSym(EndSym), ByteOffset(ByteOffset),
45 CommentOffset(CommentOffset) {}
49 SmallVector<List, 4> Lists;
50 SmallVector<Entry, 32> Entries;
51 SmallString<256> DWARFBytes;
52 SmallVector<std::string, 32> Comments;
54 /// \brief Only verbose textual output needs comments. This will be set to
55 /// true for that case, and false otherwise.
56 bool GenerateComments;
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; }
64 /// \brief Start a new .debug_loc entry list.
66 /// Start a new .debug_loc entry list. Return the new list's index so it can
67 /// be retrieved later via \a getList().
69 /// Until the next call, \a startEntry() will add entries to this list.
70 size_t startList(DwarfCompileUnit *CU, MCSymbol *Label) {
71 size_t LI = Lists.size();
72 Lists.emplace_back(CU, Label, Entries.size());
76 /// \brief Start a new .debug_loc entry.
78 /// Until the next call, bytes added to the stream will be added to this
80 void startEntry(const MCSymbol *BeginSym, const MCSymbol *EndSym) {
81 Entries.emplace_back(BeginSym, EndSym, DWARFBytes.size(), Comments.size());
84 BufferByteStreamer getStreamer() {
85 return BufferByteStreamer(DWARFBytes, Comments, GenerateComments);
88 ArrayRef<Entry> getEntries(const List &L) const {
89 size_t LI = getIndex(L);
90 return makeArrayRef(Entries)
91 .slice(Lists[LI].EntryOffset, getNumEntries(LI));
94 ArrayRef<char> getBytes(const Entry &E) const {
95 size_t EI = getIndex(E);
96 return makeArrayRef(DWARFBytes.begin(), DWARFBytes.end())
97 .slice(Entries[EI].ByteOffset, getNumBytes(EI));
99 ArrayRef<std::string> getComments(const Entry &E) const {
100 size_t EI = getIndex(E);
101 return makeArrayRef(Comments)
102 .slice(Entries[EI].CommentOffset, getNumComments(EI));
106 size_t getIndex(const List &L) const {
107 assert(&Lists.front() <= &L && &L <= &Lists.back() &&
108 "Expected valid list");
109 return &L - &Lists.front();
111 size_t getIndex(const Entry &E) const {
112 assert(&Entries.front() <= &E && &E <= &Entries.back() &&
113 "Expected valid entry");
114 return &E - &Entries.front();
116 size_t getNumEntries(size_t LI) const {
117 if (LI + 1 == Lists.size())
118 return Entries.size() - Lists[LI].EntryOffset;
119 return Lists[LI + 1].EntryOffset - Lists[LI].EntryOffset;
121 size_t getNumBytes(size_t EI) const {
122 if (EI + 1 == Entries.size())
123 return DWARFBytes.size() - Entries[EI].ByteOffset;
124 return Entries[EI + 1].ByteOffset - Entries[EI].ByteOffset;
126 size_t getNumComments(size_t EI) const {
127 if (EI + 1 == Entries.size())
128 return Comments.size() - Entries[EI].CommentOffset;
129 return Entries[EI + 1].CommentOffset - Entries[EI].CommentOffset;