llvm.org GIT mirror llvm / aea2012
implement reading of abbrevs git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36366 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
1 changed file(s) with 91 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
3333 // CurCodeSize - This is the declared size of code values used for the current
3434 // block, in bits.
3535 unsigned CurCodeSize;
36
37 /// CurAbbrevs - Abbrevs installed at in this block.
38 std::vector CurAbbrevs;
39
40 struct Block {
41 unsigned PrevCodeSize;
42 std::vector PrevAbbrevs;
43 explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
44 };
3645
3746 /// BlockScope - This tracks the codesize of parent blocks.
38 SmallVector BlockScope;
39
47 SmallVector BlockScope;
48
4049 public:
4150 BitstreamReader(const unsigned char *Start, const unsigned char *End)
4251 : NextChar(Start), LastChar(End) {
4655 CurCodeSize = 2;
4756 }
4857
58 ~BitstreamReader() {
59 // Abbrevs could still exist if the stream was broken. If so, don't leak
60 // them.
61 for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
62 delete CurAbbrevs[i];
63
64 for (unsigned S = 0, e = BlockScope.size(); S != e; ++S) {
65 std::vector &Abbrevs = BlockScope[S].PrevAbbrevs;
66 for (unsigned i = 0, e = Abbrevs.size(); i != e; ++i)
67 delete Abbrevs[i];
68 }
69 }
70
4971 bool AtEndOfStream() const { return NextChar == LastChar; }
5072
5173 uint32_t Read(unsigned NumBits) {
84106 CurWord = 0;
85107 BitsInCurWord = 32-BitsLeft;
86108 return R;
109 }
110
111 uint64_t Read64(unsigned NumBits) {
112 if (NumBits <= 32) return Read(NumBits);
113
114 uint64_t V = Read(32);
115 return V | (uint64_t)Read(NumBits-32) << 32;
87116 }
88117
89118 uint32_t ReadVBR(unsigned NumBits) {
167196 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, read and enter
168197 /// the block, returning the BlockID of the block we just entered.
169198 bool EnterSubBlock() {
170 BlockScope.push_back(CurCodeSize);
199 BlockScope.push_back(Block(CurCodeSize));
200 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
171201
172202 // Get the codesize of this block.
173203 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
187217 // Block tail:
188218 // [END_BLOCK, ]
189219 SkipToWord();
190 CurCodeSize = BlockScope.back();
220 CurCodeSize = BlockScope.back().PrevCodeSize;
221
222 // Delete abbrevs from popped scope.
223 for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
224 delete CurAbbrevs[i];
225
226 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
191227 BlockScope.pop_back();
192228 return false;
193229 }
205241 return Code;
206242 }
207243
208 assert(0 && "Reading with abbrevs not implemented!");
209 return 0;
210 }
211
244 unsigned AbbrevNo = AbbrevID-bitc::FIRST_ABBREV;
245 assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
246 BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo];
247
248 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
249 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
250 if (Op.isLiteral()) {
251 // If the abbrev specifies the literal value to use, use it.
252 Vals.push_back(Op.getLiteralValue());
253 } else {
254 // Decode the value as we are commanded.
255 switch (Op.getEncoding()) {
256 default: assert(0 && "Unknown encoding!");
257 case BitCodeAbbrevOp::FixedWidth:
258 Vals.push_back(Read(Op.getEncodingData()));
259 break;
260 case BitCodeAbbrevOp::VBR:
261 Vals.push_back(ReadVBR64(Op.getEncodingData()));
262 break;
263 }
264 }
265 }
266
267 unsigned Code = Vals[0];
268 Vals.erase(Vals.begin());
269 return Code;
270 }
271
272 //===--------------------------------------------------------------------===//
273 // Abbrev Processing
274 //===--------------------------------------------------------------------===//
275
276 void ReadAbbrevRecord() {
277 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
278 unsigned NumOpInfo = ReadVBR(5);
279 for (unsigned i = 0; i != NumOpInfo; ++i) {
280 bool IsLiteral = Read(1);
281 if (IsLiteral) {
282 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
283 continue;
284 }
285
286 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
287 if (BitCodeAbbrevOp::hasEncodingData(E)) {
288 Abbv->Add(BitCodeAbbrevOp(E, ReadVBR64(5)));
289 } else {
290 assert(0 && "unimp");
291 }
292 }
293 CurAbbrevs.push_back(Abbv);
294 }
212295 };
213296
214297 } // End llvm namespace