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: Why do we have comments even when it's an object stream?
27 /// FIXME: Do we need all these temp symbols?
28 /// FIXME: Why not output directly to the output stream?
29 class DebugLocStream {
35 List(DwarfCompileUnit *CU, MCSymbol *Label, size_t EntryOffset)
36 : CU(CU), Label(Label), EntryOffset(EntryOffset) {}
39 const MCSymbol *BeginSym;
40 const MCSymbol *EndSym;
43 Entry(const MCSymbol *BeginSym, const MCSymbol *EndSym, size_t ByteOffset,
45 : BeginSym(BeginSym), EndSym(EndSym), ByteOffset(ByteOffset),
46 CommentOffset(CommentOffset) {}
50 SmallVector<List, 4> Lists;
51 SmallVector<Entry, 32> Entries;
52 SmallString<256> DWARFBytes;
53 SmallVector<std::string, 32> Comments;
56 size_t getNumLists() const { return Lists.size(); }
57 const List &getList(size_t LI) const { return Lists[LI]; }
58 ArrayRef<List> getLists() const { return Lists; }
60 /// \brief Start a new .debug_loc entry list.
62 /// Start a new .debug_loc entry list. Return the new list's index so it can
63 /// be retrieved later via \a getList().
65 /// Until the next call, \a startEntry() will add entries to this list.
66 size_t startList(DwarfCompileUnit *CU, MCSymbol *Label) {
67 size_t LI = Lists.size();
68 Lists.emplace_back(CU, Label, Entries.size());
72 /// \brief Start a new .debug_loc entry.
74 /// Until the next call, bytes added to the stream will be added to this
76 void startEntry(const MCSymbol *BeginSym, const MCSymbol *EndSym) {
77 Entries.emplace_back(BeginSym, EndSym, DWARFBytes.size(), Comments.size());
80 BufferByteStreamer getStreamer() {
81 return BufferByteStreamer(DWARFBytes, Comments);
84 ArrayRef<Entry> getEntries(const List &L) const {
85 size_t LI = getIndex(L);
86 return makeArrayRef(Entries)
87 .slice(Lists[LI].EntryOffset, getNumEntries(LI));
90 ArrayRef<char> getBytes(const Entry &E) const {
91 size_t EI = getIndex(E);
92 return makeArrayRef(DWARFBytes.begin(), DWARFBytes.end())
93 .slice(Entries[EI].ByteOffset, getNumBytes(EI));
95 ArrayRef<std::string> getComments(const Entry &E) const {
96 size_t EI = getIndex(E);
97 return makeArrayRef(Comments)
98 .slice(Entries[EI].CommentOffset, getNumComments(EI));
102 size_t getIndex(const List &L) const {
103 assert(&Lists.front() <= &L && &L <= &Lists.back() &&
104 "Expected valid list");
105 return &L - &Lists.front();
107 size_t getIndex(const Entry &E) const {
108 assert(&Entries.front() <= &E && &E <= &Entries.back() &&
109 "Expected valid entry");
110 return &E - &Entries.front();
112 size_t getNumEntries(size_t LI) const {
113 if (LI + 1 == Lists.size())
114 return Entries.size() - Lists[LI].EntryOffset;
115 return Lists[LI + 1].EntryOffset - Lists[LI].EntryOffset;
117 size_t getNumBytes(size_t EI) const {
118 if (EI + 1 == Entries.size())
119 return DWARFBytes.size() - Entries[EI].ByteOffset;
120 return Entries[EI + 1].ByteOffset - Entries[EI].ByteOffset;
122 size_t getNumComments(size_t EI) const {
123 if (EI + 1 == Entries.size())
124 return Comments.size() - Entries[EI].CommentOffset;
125 return Entries[EI + 1].CommentOffset - Entries[EI].CommentOffset;