Benjamin Kramer | 72c0d7f | 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 | |
| 10 | #include "DWARFDebugAbbrev.h" |
| 11 | #include "llvm/Support/Format.h" |
| 12 | #include "llvm/Support/raw_ostream.h" |
| 13 | using namespace llvm; |
| 14 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 15 | DWARFAbbreviationDeclarationSet::DWARFAbbreviationDeclarationSet() { |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 16 | clear(); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [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)) { |
| 33 | Decls.push_back(AbbrDecl); |
| 34 | if (FirstAbbrCode == 0) { |
| 35 | FirstAbbrCode = AbbrDecl.getCode(); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 36 | } else { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 37 | if (PrevAbbrCode + 1 != AbbrDecl.getCode()) { |
| 38 | // Codes are not consecutive, can't do O(1) lookups. |
| 39 | FirstAbbrCode = UINT32_MAX; |
| 40 | } |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 41 | } |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 42 | PrevAbbrCode = AbbrDecl.getCode(); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 43 | } |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 44 | return BeginOffset != *OffsetPtr; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 47 | void DWARFAbbreviationDeclarationSet::dump(raw_ostream &OS) const { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 48 | for (const auto &Decl : Decls) |
| 49 | Decl.dump(OS); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 52 | const DWARFAbbreviationDeclaration * |
| 53 | DWARFAbbreviationDeclarationSet::getAbbreviationDeclaration( |
| 54 | uint32_t AbbrCode) const { |
| 55 | if (FirstAbbrCode == UINT32_MAX) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 56 | for (const auto &Decl : Decls) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 57 | if (Decl.getCode() == AbbrCode) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 58 | return &Decl; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 59 | } |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 60 | return nullptr; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 61 | } |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 62 | if (AbbrCode < FirstAbbrCode || AbbrCode >= FirstAbbrCode + Decls.size()) |
| 63 | return nullptr; |
| 64 | return &Decls[AbbrCode - FirstAbbrCode]; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 67 | DWARFDebugAbbrev::DWARFDebugAbbrev() { |
| 68 | clear(); |
| 69 | } |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 70 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 71 | void DWARFDebugAbbrev::clear() { |
| 72 | AbbrDeclSets.clear(); |
| 73 | PrevAbbrOffsetPos = AbbrDeclSets.end(); |
| 74 | } |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 75 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 76 | void DWARFDebugAbbrev::extract(DataExtractor Data) { |
| 77 | clear(); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 78 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [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 | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 84 | break; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 85 | AbbrDeclSets[CUAbbrOffset] = AbbrDecls; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 86 | } |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void DWARFDebugAbbrev::dump(raw_ostream &OS) const { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 90 | if (AbbrDeclSets.empty()) { |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 91 | OS << "< EMPTY >\n"; |
| 92 | return; |
| 93 | } |
| 94 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 95 | for (const auto &I : AbbrDeclSets) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 96 | OS << format("Abbrev table for offset: 0x%8.8" PRIx64 "\n", I.first); |
| 97 | I.second.dump(OS); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | const DWARFAbbreviationDeclarationSet* |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 102 | DWARFDebugAbbrev::getAbbreviationDeclarationSet(uint64_t CUAbbrOffset) const { |
| 103 | const auto End = AbbrDeclSets.end(); |
| 104 | if (PrevAbbrOffsetPos != End && PrevAbbrOffsetPos->first == CUAbbrOffset) { |
Benjamin Kramer | 4aa3fea | 2011-09-13 21:47:32 +0000 | [diff] [blame] | 105 | return &(PrevAbbrOffsetPos->second); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 108 | const auto Pos = AbbrDeclSets.find(CUAbbrOffset); |
| 109 | if (Pos != End) { |
| 110 | PrevAbbrOffsetPos = Pos; |
| 111 | return &(Pos->second); |
| 112 | } |
| 113 | |
| 114 | return nullptr; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 115 | } |