AsmPrinter: Create a unified .debug_loc stream
[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: 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 {
30 public:
31   struct List {
32     DwarfCompileUnit *CU;
33     MCSymbol *Label;
34     size_t EntryOffset;
35     List(DwarfCompileUnit *CU, MCSymbol *Label, size_t EntryOffset)
36         : CU(CU), Label(Label), EntryOffset(EntryOffset) {}
37   };
38   struct Entry {
39     const MCSymbol *BeginSym;
40     const MCSymbol *EndSym;
41     size_t ByteOffset;
42     size_t CommentOffset;
43     Entry(const MCSymbol *BeginSym, const MCSymbol *EndSym, size_t ByteOffset,
44           size_t CommentOffset)
45         : BeginSym(BeginSym), EndSym(EndSym), ByteOffset(ByteOffset),
46           CommentOffset(CommentOffset) {}
47   };
48
49 private:
50   SmallVector<List, 4> Lists;
51   SmallVector<Entry, 32> Entries;
52   SmallString<256> DWARFBytes;
53   SmallVector<std::string, 32> Comments;
54
55 public:
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; }
59
60   /// \brief Start a new .debug_loc entry list.
61   ///
62   /// Start a new .debug_loc entry list.  Return the new list's index so it can
63   /// be retrieved later via \a getList().
64   ///
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());
69     return LI;
70   }
71
72   /// \brief Start a new .debug_loc entry.
73   ///
74   /// Until the next call, bytes added to the stream will be added to this
75   /// entry.
76   void startEntry(const MCSymbol *BeginSym, const MCSymbol *EndSym) {
77     Entries.emplace_back(BeginSym, EndSym, DWARFBytes.size(), Comments.size());
78   }
79
80   BufferByteStreamer getStreamer() {
81     return BufferByteStreamer(DWARFBytes, Comments);
82   }
83
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));
88   }
89
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));
94   }
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));
99   }
100
101 private:
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();
106   }
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();
111   }
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;
116   }
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;
121   }
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;
126   }
127 };
128 }
129 #endif