Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 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 | |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 11 | #include "llvm/Support/Format.h" |
| 12 | #include "llvm/Support/raw_ostream.h" |
| 13 | using namespace llvm; |
| 14 | |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 15 | DWARFAbbreviationDeclarationSet::DWARFAbbreviationDeclarationSet() { |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 16 | clear(); |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | void DWARFAbbreviationDeclarationSet::clear() { |
| 20 | Offset = 0; |
| 21 | FirstAbbrCode = 0; |
| 22 | Decls.clear(); |
| 23 | } |
| 24 | |
| 25 | bool DWARFAbbreviationDeclarationSet::extract(DataExtractor Data, |
| 26 | uint32_t *OffsetPtr) { |
| 27 | clear(); |
| 28 | const uint32_t BeginOffset = *OffsetPtr; |
| 29 | Offset = BeginOffset; |
| 30 | DWARFAbbreviationDeclaration AbbrDecl; |
| 31 | uint32_t PrevAbbrCode = 0; |
| 32 | while (AbbrDecl.extract(Data, OffsetPtr)) { |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 33 | if (FirstAbbrCode == 0) { |
| 34 | FirstAbbrCode = AbbrDecl.getCode(); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 35 | } else { |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 36 | if (PrevAbbrCode + 1 != AbbrDecl.getCode()) { |
| 37 | // Codes are not consecutive, can't do O(1) lookups. |
| 38 | FirstAbbrCode = UINT32_MAX; |
| 39 | } |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 40 | } |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 41 | PrevAbbrCode = AbbrDecl.getCode(); |
Benjamin Kramer | c6cc58e | 2014-10-04 16:55:56 +0000 | [diff] [blame] | 42 | Decls.push_back(std::move(AbbrDecl)); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 43 | } |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 44 | return BeginOffset != *OffsetPtr; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 47 | void DWARFAbbreviationDeclarationSet::dump(raw_ostream &OS) const { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 48 | for (const auto &Decl : Decls) |
| 49 | Decl.dump(OS); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 52 | const DWARFAbbreviationDeclaration * |
| 53 | DWARFAbbreviationDeclarationSet::getAbbreviationDeclaration( |
| 54 | uint32_t AbbrCode) const { |
| 55 | if (FirstAbbrCode == UINT32_MAX) { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 56 | for (const auto &Decl : Decls) { |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 57 | if (Decl.getCode() == AbbrCode) |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 58 | return &Decl; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 59 | } |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 60 | return nullptr; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 61 | } |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 62 | if (AbbrCode < FirstAbbrCode || AbbrCode >= FirstAbbrCode + Decls.size()) |
| 63 | return nullptr; |
| 64 | return &Decls[AbbrCode - FirstAbbrCode]; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 67 | DWARFDebugAbbrev::DWARFDebugAbbrev() { |
| 68 | clear(); |
| 69 | } |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 70 | |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 71 | void DWARFDebugAbbrev::clear() { |
| 72 | AbbrDeclSets.clear(); |
| 73 | PrevAbbrOffsetPos = AbbrDeclSets.end(); |
| 74 | } |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 75 | |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 76 | void DWARFDebugAbbrev::extract(DataExtractor Data) { |
| 77 | clear(); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 78 | |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 79 | uint32_t Offset = 0; |
| 80 | DWARFAbbreviationDeclarationSet AbbrDecls; |
| 81 | while (Data.isValidOffset(Offset)) { |
| 82 | uint32_t CUAbbrOffset = Offset; |
| 83 | if (!AbbrDecls.extract(Data, &Offset)) |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 84 | break; |
Benjamin Kramer | c6cc58e | 2014-10-04 16:55:56 +0000 | [diff] [blame] | 85 | AbbrDeclSets[CUAbbrOffset] = std::move(AbbrDecls); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 86 | } |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void DWARFDebugAbbrev::dump(raw_ostream &OS) const { |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 90 | if (AbbrDeclSets.empty()) { |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 91 | OS << "< EMPTY >\n"; |
| 92 | return; |
| 93 | } |
| 94 | |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 95 | for (const auto &I : AbbrDeclSets) { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 96 | OS << format("Abbrev table for offset: 0x%8.8" PRIx64 "\n", I.first); |
| 97 | I.second.dump(OS); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | const DWARFAbbreviationDeclarationSet* |
Alexey Samsonov | 9a5c95a | 2014-04-24 22:41:09 +0000 | [diff] [blame] | 102 | DWARFDebugAbbrev::getAbbreviationDeclarationSet(uint64_t CUAbbrOffset) const { |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 103 | const auto End = AbbrDeclSets.end(); |
Alexey Samsonov | 9a5c95a | 2014-04-24 22:41:09 +0000 | [diff] [blame] | 104 | if (PrevAbbrOffsetPos != End && PrevAbbrOffsetPos->first == CUAbbrOffset) { |
Benjamin Kramer | eaa7433 | 2011-09-13 21:47:32 +0000 | [diff] [blame] | 105 | return &(PrevAbbrOffsetPos->second); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 108 | const auto Pos = AbbrDeclSets.find(CUAbbrOffset); |
Alexey Samsonov | 9a5c95a | 2014-04-24 22:41:09 +0000 | [diff] [blame] | 109 | if (Pos != End) { |
| 110 | PrevAbbrOffsetPos = Pos; |
| 111 | return &(Pos->second); |
| 112 | } |
| 113 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 114 | return nullptr; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 115 | } |