68769f05cdfbefc1aaf641636a968ae6249c15cd
[oota-llvm.git] / lib / DebugInfo / DWARFDebugAbbrev.cpp
1 //===-- DWARFDebugAbbrev.cpp ----------------------------------------------===//
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 #include "DWARFDebugAbbrev.h"
11 #include "llvm/Support/Format.h"
12 #include "llvm/Support/raw_ostream.h"
13 using namespace llvm;
14
15 bool DWARFAbbreviationDeclarationSet::extract(DataExtractor data,
16                                               uint32_t* offset_ptr) {
17   const uint32_t beginOffset = *offset_ptr;
18   Offset = beginOffset;
19   clear();
20   DWARFAbbreviationDeclaration abbrevDeclaration;
21   uint32_t prevAbbrAode = 0;
22   while (abbrevDeclaration.extract(data, offset_ptr)) {
23     Decls.push_back(abbrevDeclaration);
24     if (IdxOffset == 0) {
25       IdxOffset = abbrevDeclaration.getCode();
26     } else {
27       if (prevAbbrAode + 1 != abbrevDeclaration.getCode())
28         IdxOffset = UINT32_MAX;// Out of order indexes, we can't do O(1) lookups
29     }
30     prevAbbrAode = abbrevDeclaration.getCode();
31   }
32   return beginOffset != *offset_ptr;
33 }
34
35
36 void DWARFAbbreviationDeclarationSet::dump(raw_ostream &OS) const {
37   for (unsigned i = 0, e = Decls.size(); i != e; ++i)
38     Decls[i].dump(OS);
39 }
40
41
42 const DWARFAbbreviationDeclaration*
43 DWARFAbbreviationDeclarationSet::getAbbreviationDeclaration(uint32_t abbrCode)
44   const {
45   if (IdxOffset == UINT32_MAX) {
46     DWARFAbbreviationDeclarationCollConstIter pos;
47     DWARFAbbreviationDeclarationCollConstIter end = Decls.end();
48     for (pos = Decls.begin(); pos != end; ++pos) {
49       if (pos->getCode() == abbrCode)
50         return &(*pos);
51     }
52   } else {
53     uint32_t idx = abbrCode - IdxOffset;
54     if (idx < Decls.size())
55       return &Decls[idx];
56   }
57   return NULL;
58 }
59
60 DWARFDebugAbbrev::DWARFDebugAbbrev() :
61   m_abbrevCollMap(),
62   m_prev_abbr_offset_pos(m_abbrevCollMap.end()) {}
63
64
65 void DWARFDebugAbbrev::parse(DataExtractor data) {
66   uint32_t offset = 0;
67
68   while (data.isValidOffset(offset)) {
69     uint32_t initial_cu_offset = offset;
70     DWARFAbbreviationDeclarationSet abbrevDeclSet;
71
72     if (abbrevDeclSet.extract(data, &offset))
73       m_abbrevCollMap[initial_cu_offset] = abbrevDeclSet;
74     else
75       break;
76   }
77   m_prev_abbr_offset_pos = m_abbrevCollMap.end();
78 }
79
80 void DWARFDebugAbbrev::dump(raw_ostream &OS) const {
81   if (m_abbrevCollMap.empty()) {
82     OS << "< EMPTY >\n";
83     return;
84   }
85
86   DWARFAbbreviationDeclarationCollMapConstIter pos;
87   for (pos = m_abbrevCollMap.begin(); pos != m_abbrevCollMap.end(); ++pos) {
88     OS << format("Abbrev table for offset: 0x%8.8x\n", pos->first);
89     pos->second.dump(OS);
90   }
91 }
92
93 const DWARFAbbreviationDeclarationSet*
94 DWARFDebugAbbrev::getAbbreviationDeclarationSet(uint64_t cu_abbr_offset) const {
95   DWARFAbbreviationDeclarationCollMapConstIter end = m_abbrevCollMap.end();
96   DWARFAbbreviationDeclarationCollMapConstIter pos;
97   if (m_prev_abbr_offset_pos != end &&
98       m_prev_abbr_offset_pos->first == cu_abbr_offset) {
99     return &(m_prev_abbr_offset_pos->second);
100   } else {
101     pos = m_abbrevCollMap.find(cu_abbr_offset);
102     m_prev_abbr_offset_pos = pos;
103   }
104
105   if (pos != m_abbrevCollMap.end())
106     return &(pos->second);
107   return NULL;
108 }