blob: 6e6c37e309454158568ace3c6b68819db4bba619 [file] [log] [blame]
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +00001//===-- 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"
13using namespace llvm;
14
15bool 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
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000035void DWARFAbbreviationDeclarationSet::dump(raw_ostream &OS) const {
36 for (unsigned i = 0, e = Decls.size(); i != e; ++i)
37 Decls[i].dump(OS);
38}
39
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000040const DWARFAbbreviationDeclaration*
41DWARFAbbreviationDeclarationSet::getAbbreviationDeclaration(uint32_t abbrCode)
42 const {
43 if (IdxOffset == UINT32_MAX) {
44 DWARFAbbreviationDeclarationCollConstIter pos;
45 DWARFAbbreviationDeclarationCollConstIter end = Decls.end();
46 for (pos = Decls.begin(); pos != end; ++pos) {
47 if (pos->getCode() == abbrCode)
48 return &(*pos);
49 }
50 } else {
51 uint32_t idx = abbrCode - IdxOffset;
52 if (idx < Decls.size())
53 return &Decls[idx];
54 }
55 return NULL;
56}
57
58DWARFDebugAbbrev::DWARFDebugAbbrev() :
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000059 AbbrevCollMap(),
60 PrevAbbrOffsetPos(AbbrevCollMap.end()) {}
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000061
62
63void DWARFDebugAbbrev::parse(DataExtractor data) {
64 uint32_t offset = 0;
65
66 while (data.isValidOffset(offset)) {
67 uint32_t initial_cu_offset = offset;
68 DWARFAbbreviationDeclarationSet abbrevDeclSet;
69
70 if (abbrevDeclSet.extract(data, &offset))
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000071 AbbrevCollMap[initial_cu_offset] = abbrevDeclSet;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000072 else
73 break;
74 }
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000075 PrevAbbrOffsetPos = AbbrevCollMap.end();
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000076}
77
78void DWARFDebugAbbrev::dump(raw_ostream &OS) const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000079 if (AbbrevCollMap.empty()) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000080 OS << "< EMPTY >\n";
81 return;
82 }
83
84 DWARFAbbreviationDeclarationCollMapConstIter pos;
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000085 for (pos = AbbrevCollMap.begin(); pos != AbbrevCollMap.end(); ++pos) {
Benjamin Kramer5eccd362011-11-05 16:01:13 +000086 OS << format("Abbrev table for offset: 0x%8.8" PRIx64 "\n", pos->first);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000087 pos->second.dump(OS);
88 }
89}
90
91const DWARFAbbreviationDeclarationSet*
92DWARFDebugAbbrev::getAbbreviationDeclarationSet(uint64_t cu_abbr_offset) const {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000093 DWARFAbbreviationDeclarationCollMapConstIter end = AbbrevCollMap.end();
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000094 DWARFAbbreviationDeclarationCollMapConstIter pos;
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000095 if (PrevAbbrOffsetPos != end &&
96 PrevAbbrOffsetPos->first == cu_abbr_offset) {
97 return &(PrevAbbrOffsetPos->second);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000098 } else {
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +000099 pos = AbbrevCollMap.find(cu_abbr_offset);
100 PrevAbbrOffsetPos = pos;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000101 }
102
Benjamin Kramer4aa3fea2011-09-13 21:47:32 +0000103 if (pos != AbbrevCollMap.end())
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000104 return &(pos->second);
105 return NULL;
106}