Don't generate comments in the DebugLocStream unless required. NFC.
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DebugLocStream.h
1 //===--- lib/CodeGen/DebugLocStream.h - DWARF debug_loc stream --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCSTREAM_H
11 #define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCSTREAM_H
12
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "ByteStreamer.h"
16
17 namespace llvm {
18 class DwarfCompileUnit;
19 class MCSymbol;
20
21 /// \brief Byte stream of .debug_loc entries.
22 ///
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.
25 ///
26 /// FIXME: Do we need all these temp symbols?
27 /// FIXME: Why not output directly to the output stream?
28 class DebugLocStream {
29 public:
30   struct List {
31     DwarfCompileUnit *CU;
32     MCSymbol *Label;
33     size_t EntryOffset;
34     List(DwarfCompileUnit *CU, MCSymbol *Label, size_t EntryOffset)
35         : CU(CU), Label(Label), EntryOffset(EntryOffset) {}
36   };
37   struct Entry {
38     const MCSymbol *BeginSym;
39     const MCSymbol *EndSym;
40     size_t ByteOffset;
41     size_t CommentOffset;
42     Entry(const MCSymbol *BeginSym, const MCSymbol *EndSym, size_t ByteOffset,
43           size_t CommentOffset)
44         : BeginSym(BeginSym), EndSym(EndSym), ByteOffset(ByteOffset),
45           CommentOffset(CommentOffset) {}
46   };
47
48 private:
49   SmallVector<List, 4> Lists;
50   SmallVector<Entry, 32> Entries;
51   SmallString<256> DWARFBytes;
52   SmallVector<std::string, 32> Comments;
53
54   /// \brief Only verbose textual output needs comments.  This will be set to
55   /// true for that case, and false otherwise.
56   bool GenerateComments;
57
58 public:
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
64   /// \brief Start a new .debug_loc entry list.
65   ///
66   /// Start a new .debug_loc entry list.  Return the new list's index so it can
67   /// be retrieved later via \a getList().
68   ///
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());
73     return LI;
74   }
75
76   /// \brief Start a new .debug_loc entry.
77   ///
78   /// Until the next call, bytes added to the stream will be added to this
79   /// entry.
80   void startEntry(const MCSymbol *BeginSym, const MCSymbol *EndSym) {
81     Entries.emplace_back(BeginSym, EndSym, DWARFBytes.size(), Comments.size());
82   }
83
84   BufferByteStreamer getStreamer() {
85     return BufferByteStreamer(DWARFBytes, Comments, GenerateComments);
86   }
87
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));
92   }
93
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));
98   }
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));
103   }
104
105 private:
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();
110   }
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();
115   }
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;
120   }
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;
125   }
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;
130   }
131 };
132 }
133 #endif