llvm.org GIT mirror llvm / 48a3969
Updated Deserializer class to provide more information about the current block that is being visited in the bitstream. The client can also now skip blocks before reading them, and query the current abbreviation number as seen from the perspective of the Deserializer. This allows the client to be more interactive in the deserialization process (if they so choose). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43916 91177308-0d34-0410-b5e6-96231b3b80d8 Ted Kremenek 12 years ago
2 changed file(s) with 123 addition(s) and 64 deletion(s). Raw diff Collapse all Expand all
8787
8888 private:
8989 BitstreamReader& Stream;
90 SmallVector10> Record;
90 SmallVector20> Record;
9191 unsigned RecIdx;
9292 BumpPtrAllocator Allocator;
9393 BPNode* FreeList;
9494 MapTy BPatchMap;
95 llvm::SmallVector<uint64_t,5> BlockLocs;
95 llvm::SmallVector<std::pair,5> BlockStack;
96 unsigned AbbrevNo;
97 unsigned RecordCode;
9698
9799 //===----------------------------------------------------------===//
98100 // Public Interface.
230232 RegisterPtr(PtrID,&x);
231233 }
232234
233 Location GetCurrentBlockLocation();
235 Location getCurrentBlockLocation();
236 unsigned getCurrentBlockID();
237 unsigned getAbbrevNo();
238
234239 bool FinishedBlock(Location BlockLoc);
235240
236241 bool AtEnd();
237242 bool inRecord();
243 void SkipBlock();
244
245 unsigned getRecordCode();
238246
239247 private:
240 void ReadRecord();
248 bool AdvanceStream();
249 void ReadRecord();
250
241251 uintptr_t ReadInternalRefPtr();
242252
243253 static inline bool HasFinalPtr(MapTy::value_type& V) {
1919 using namespace llvm;
2020
2121 Deserializer::Deserializer(BitstreamReader& stream)
22 : Stream(stream), RecIdx(0), FreeList(NULL) {
22 : Stream(stream), RecIdx(0), FreeList(NULL), AbbrevNo(0), RecordCode(0) {
2323 }
2424
2525 Deserializer::~Deserializer() {
2626 assert (RecIdx >= Record.size() &&
2727 "Still scanning bitcode record when deserialization completed.");
2828
29 #ifdef NDEBUG
29 #ifdef DEBUG_BACKPATCH
3030 for (MapTy::iterator I=BPatchMap.begin(), E=BPatchMap.end(); I!=E; ++I)
3131 assert (I->first.hasFinalPtr() &&
3232 "Some pointers were not backpatched.");
3939 if (RecIdx >= Record.size()) {
4040 RecIdx = 0;
4141 Record.clear();
42 AbbrevNo = 0;
4243 return false;
4344 }
44 else return true;
45 }
46 else return false;
47 }
48
49 void Deserializer::ReadRecord() {
50 // FIXME: Check if we haven't run off the edge of the stream.
51 // FIXME: Handle abbreviations.
52
53 assert (Record.size() == 0);
54
55 unsigned Code;
56
57 while (true) {
45 else
46 return true;
47 }
48
49 return false;
50 }
51
52 bool Deserializer::AdvanceStream() {
53 assert (!inRecord() &&
54 "Cannot advance stream. Still processing a record.");
55
56 if (AbbrevNo == bitc::ENTER_SUBBLOCK ||
57 AbbrevNo >= bitc::UNABBREV_RECORD)
58 return true;
59
60 while (!Stream.AtEndOfStream()) {
5861
59 if (Stream.AtEndOfStream())
60 return;
61
62 Code = Stream.ReadCode();
63
64 if (Code == bitc::ENTER_SUBBLOCK) {
65 BlockLocs.push_back(Stream.GetCurrentBitNo());
66 unsigned id = Stream.ReadSubBlockID();
67 Stream.EnterSubBlock(id);
68 continue;
69 }
70
71 if (Code == bitc::END_BLOCK) {
72 bool x = Stream.ReadBlockEnd();
73 assert (!x && "Error at block end.");
74 BlockLocs.pop_back();
75 continue;
62 AbbrevNo = Stream.ReadCode();
63
64 switch (AbbrevNo) {
65 case bitc::ENTER_SUBBLOCK: {
66 unsigned id = Stream.ReadSubBlockID();
67 BlockStack.push_back(std::make_pair(Stream.GetCurrentBitNo(),id));
68 break;
69 }
70
71 case bitc::END_BLOCK: {
72 bool x = Stream.ReadBlockEnd();
73 assert (!x && "Error at block end.");
74 BlockStack.pop_back();
75 continue;
76 }
77
78 case bitc::DEFINE_ABBREV:
79 Stream.ReadAbbrevRecord();
80 continue;
81
82 default:
83 break;
7684 }
7785
78 if (Code == bitc::DEFINE_ABBREV) {
79 Stream.ReadAbbrevRecord();
80 continue;
81 }
82
83 break;
84 }
85
86 assert (Record.size() == 0);
87 Stream.ReadRecord(Code,Record);
88 assert (Record.size() > 0 || Stream.AtEndOfStream());
89 }
90
91 Deserializer::Location Deserializer::GetCurrentBlockLocation() {
92 if (!inRecord())
86 return true;
87 }
88
89 return false;
90 }
91
92 void Deserializer::ReadRecord() {
93
94 while (AdvanceStream() && AbbrevNo == bitc::ENTER_SUBBLOCK) {
95 assert (!BlockStack.empty());
96 Stream.EnterSubBlock(BlockStack.back().second);
97 AbbrevNo = 0;
98 }
99
100 if (Stream.AtEndOfStream())
101 return;
102
103 assert (Record.size() == 0);
104 assert (AbbrevNo >= bitc::UNABBREV_RECORD);
105 RecordCode = Stream.ReadRecord(AbbrevNo,Record);
106 assert (Record.size() > 0);
107 }
108
109 void Deserializer::SkipBlock() {
110 assert (!inRecord());
111 assert (AbbrevNo == bitc::ENTER_SUBBLOCK);
112 Stream.SkipBlock();
113 AbbrevNo = 0;
114 }
115
116 Deserializer::Location Deserializer::getCurrentBlockLocation() {
117 if (!inRecord())
118 AdvanceStream();
119
120 return BlockStack.back().first;
121 }
122
123 unsigned Deserializer::getCurrentBlockID() {
124 if (!inRecord())
125 AdvanceStream();
126
127 return BlockStack.back().second;
128 }
129
130 unsigned Deserializer::getRecordCode() {
131 if (!inRecord()) {
132 AdvanceStream();
133 assert (AbbrevNo >= bitc::UNABBREV_RECORD);
93134 ReadRecord();
94
95 assert (!BlockLocs.empty());
96 return BlockLocs.back();
135 }
136
137 return RecordCode;
97138 }
98139
99140 bool Deserializer::FinishedBlock(Location BlockLoc) {
100141 if (!inRecord())
101 ReadRecord();
102
103 for (llvm::SmallVector::reverse_iterator
104 I=BlockLocs.rbegin(), E=BlockLocs.rend(); I!=E; ++I)
105 if (*I == BlockLoc)
106 return false;
142 AdvanceStream();
143
144 for (llvm::SmallVector,5>::reverse_iterator
145 I=BlockStack.rbegin(), E=BlockStack.rend(); I!=E; ++I)
146 if (I->first == BlockLoc)
147 return false;
107148
108149 return true;
150 }
151
152 unsigned Deserializer::getAbbrevNo() {
153 if (!inRecord())
154 AdvanceStream();
155
156 return AbbrevNo;
109157 }
110158
111159 bool Deserializer::AtEnd() {
112160 if (inRecord())
113161 return false;
114162
115 ReadRecord();
116
117 return Stream.AtEndOfStream();
163 if (!AdvanceStream())
164 return true;
165
166 return false;
118167 }
119168
120169 uint64_t Deserializer::ReadInt() {