Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: lib/Bitcode/NaCl/Reader/NaClBitstreamReader.cpp

Issue 14314016: Copy LLVM bitcode reader to generate a PNaCl wire format reader. (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===// 1 //===- NaClBitstreamReader.cpp --------------------------------------------===//
2 // NaClBitstreamReader implementation
2 // 3 //
3 // The LLVM Compiler Infrastructure 4 // The LLVM Compiler Infrastructure
4 // 5 //
5 // This file is distributed under the University of Illinois Open Source 6 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 7 // License. See LICENSE.TXT for details.
7 // 8 //
8 //===----------------------------------------------------------------------===// 9 //===----------------------------------------------------------------------===//
9 10
10 #include "llvm/Bitcode/BitstreamReader.h" 11 #include "llvm/Bitcode/NaCl/NaClBitstreamReader.h"
11 12
12 using namespace llvm; 13 using namespace llvm;
13 14
14 //===----------------------------------------------------------------------===// 15 //===----------------------------------------------------------------------===//
15 // BitstreamCursor implementation 16 // NaClBitstreamCursor implementation
16 //===----------------------------------------------------------------------===// 17 //===----------------------------------------------------------------------===//
17 18
18 void BitstreamCursor::operator=(const BitstreamCursor &RHS) { 19 void NaClBitstreamCursor::operator=(const NaClBitstreamCursor &RHS) {
19 freeState(); 20 freeState();
20 21
21 BitStream = RHS.BitStream; 22 BitStream = RHS.BitStream;
22 NextChar = RHS.NextChar; 23 NextChar = RHS.NextChar;
23 CurWord = RHS.CurWord; 24 CurWord = RHS.CurWord;
24 BitsInCurWord = RHS.BitsInCurWord; 25 BitsInCurWord = RHS.BitsInCurWord;
25 CurCodeSize = RHS.CurCodeSize; 26 CurCodeSize = RHS.CurCodeSize;
26 27
27 // Copy abbreviations, and bump ref counts. 28 // Copy abbreviations, and bump ref counts.
28 CurAbbrevs = RHS.CurAbbrevs; 29 CurAbbrevs = RHS.CurAbbrevs;
29 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i) 30 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
30 CurAbbrevs[i]->addRef(); 31 CurAbbrevs[i]->addRef();
31 32
32 // Copy block scope and bump ref counts. 33 // Copy block scope and bump ref counts.
33 BlockScope = RHS.BlockScope; 34 BlockScope = RHS.BlockScope;
34 for (size_t S = 0, e = BlockScope.size(); S != e; ++S) { 35 for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
35 std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs; 36 std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
36 for (size_t i = 0, e = Abbrevs.size(); i != e; ++i) 37 for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
37 Abbrevs[i]->addRef(); 38 Abbrevs[i]->addRef();
38 } 39 }
39 } 40 }
40 41
41 void BitstreamCursor::freeState() { 42 void NaClBitstreamCursor::freeState() {
42 // Free all the Abbrevs. 43 // Free all the Abbrevs.
43 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i) 44 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
44 CurAbbrevs[i]->dropRef(); 45 CurAbbrevs[i]->dropRef();
45 CurAbbrevs.clear(); 46 CurAbbrevs.clear();
46 47
47 // Free all the Abbrevs in the block scope. 48 // Free all the Abbrevs in the block scope.
48 for (size_t S = 0, e = BlockScope.size(); S != e; ++S) { 49 for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
49 std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs; 50 std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
50 for (size_t i = 0, e = Abbrevs.size(); i != e; ++i) 51 for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
51 Abbrevs[i]->dropRef(); 52 Abbrevs[i]->dropRef();
52 } 53 }
53 BlockScope.clear(); 54 BlockScope.clear();
54 } 55 }
55 56
56 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter 57 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
57 /// the block, and return true if the block has an error. 58 /// the block, and return true if the block has an error.
58 bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) { 59 bool NaClBitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
59 // Save the current block's state on BlockScope. 60 // Save the current block's state on BlockScope.
60 BlockScope.push_back(Block(CurCodeSize)); 61 BlockScope.push_back(Block(CurCodeSize));
61 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); 62 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
62 63
63 // Add the abbrevs specific to this block to the CurAbbrevs list. 64 // Add the abbrevs specific to this block to the CurAbbrevs list.
64 if (const BitstreamReader::BlockInfo *Info = 65 if (const NaClBitstreamReader::BlockInfo *Info =
65 BitStream->getBlockInfo(BlockID)) { 66 BitStream->getBlockInfo(BlockID)) {
66 for (size_t i = 0, e = Info->Abbrevs.size(); i != e; ++i) { 67 for (size_t i = 0, e = Info->Abbrevs.size(); i != e; ++i) {
67 CurAbbrevs.push_back(Info->Abbrevs[i]); 68 CurAbbrevs.push_back(Info->Abbrevs[i]);
68 CurAbbrevs.back()->addRef(); 69 CurAbbrevs.back()->addRef();
69 } 70 }
70 } 71 }
71 72
72 // Get the codesize of this block. 73 // Get the codesize of this block.
73 CurCodeSize = ReadVBR(bitc::CodeLenWidth); 74 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
74 SkipToFourByteBoundary(); 75 SkipToFourByteBoundary();
75 unsigned NumWords = Read(bitc::BlockSizeWidth); 76 unsigned NumWords = Read(bitc::BlockSizeWidth);
76 if (NumWordsP) *NumWordsP = NumWords; 77 if (NumWordsP) *NumWordsP = NumWords;
77 78
78 // Validate that this block is sane. 79 // Validate that this block is sane.
79 if (CurCodeSize == 0 || AtEndOfStream()) 80 if (CurCodeSize == 0 || AtEndOfStream())
80 return true; 81 return true;
81 82
82 return false; 83 return false;
83 } 84 }
84 85
85 void BitstreamCursor::readAbbreviatedLiteral(const BitCodeAbbrevOp &Op, 86 void NaClBitstreamCursor::readAbbreviatedLiteral(
86 SmallVectorImpl<uint64_t> &Vals) { 87 const BitCodeAbbrevOp &Op,
88 SmallVectorImpl<uint64_t> &Vals) {
87 assert(Op.isLiteral() && "Not a literal"); 89 assert(Op.isLiteral() && "Not a literal");
88 // If the abbrev specifies the literal value to use, use it. 90 // If the abbrev specifies the literal value to use, use it.
89 Vals.push_back(Op.getLiteralValue()); 91 Vals.push_back(Op.getLiteralValue());
90 } 92 }
91 93
92 void BitstreamCursor::readAbbreviatedField(const BitCodeAbbrevOp &Op, 94 void NaClBitstreamCursor::readAbbreviatedField(
93 SmallVectorImpl<uint64_t> &Vals) { 95 const BitCodeAbbrevOp &Op,
96 SmallVectorImpl<uint64_t> &Vals) {
94 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!"); 97 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
95 98
96 // Decode the value as we are commanded. 99 // Decode the value as we are commanded.
97 switch (Op.getEncoding()) { 100 switch (Op.getEncoding()) {
98 case BitCodeAbbrevOp::Array: 101 case BitCodeAbbrevOp::Array:
99 case BitCodeAbbrevOp::Blob: 102 case BitCodeAbbrevOp::Blob:
100 assert(0 && "Should not reach here"); 103 assert(0 && "Should not reach here");
101 case BitCodeAbbrevOp::Fixed: 104 case BitCodeAbbrevOp::Fixed:
102 Vals.push_back(Read((unsigned)Op.getEncodingData())); 105 Vals.push_back(Read((unsigned)Op.getEncodingData()));
103 break; 106 break;
104 case BitCodeAbbrevOp::VBR: 107 case BitCodeAbbrevOp::VBR:
105 Vals.push_back(ReadVBR64((unsigned)Op.getEncodingData())); 108 Vals.push_back(ReadVBR64((unsigned)Op.getEncodingData()));
106 break; 109 break;
107 case BitCodeAbbrevOp::Char6: 110 case BitCodeAbbrevOp::Char6:
108 Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6))); 111 Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
109 break; 112 break;
110 } 113 }
111 } 114 }
112 115
113 void BitstreamCursor::skipAbbreviatedField(const BitCodeAbbrevOp &Op) { 116 void NaClBitstreamCursor::skipAbbreviatedField(const BitCodeAbbrevOp &Op) {
114 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!"); 117 assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
115 118
116 // Decode the value as we are commanded. 119 // Decode the value as we are commanded.
117 switch (Op.getEncoding()) { 120 switch (Op.getEncoding()) {
118 case BitCodeAbbrevOp::Array: 121 case BitCodeAbbrevOp::Array:
119 case BitCodeAbbrevOp::Blob: 122 case BitCodeAbbrevOp::Blob:
120 assert(0 && "Should not reach here"); 123 assert(0 && "Should not reach here");
121 case BitCodeAbbrevOp::Fixed: 124 case BitCodeAbbrevOp::Fixed:
122 (void)Read((unsigned)Op.getEncodingData()); 125 (void)Read((unsigned)Op.getEncodingData());
123 break; 126 break;
124 case BitCodeAbbrevOp::VBR: 127 case BitCodeAbbrevOp::VBR:
125 (void)ReadVBR64((unsigned)Op.getEncodingData()); 128 (void)ReadVBR64((unsigned)Op.getEncodingData());
126 break; 129 break;
127 case BitCodeAbbrevOp::Char6: 130 case BitCodeAbbrevOp::Char6:
128 (void)Read(6); 131 (void)Read(6);
129 break; 132 break;
130 } 133 }
131 } 134 }
132 135
133 136
134 137
135 /// skipRecord - Read the current record and discard it. 138 /// skipRecord - Read the current record and discard it.
136 void BitstreamCursor::skipRecord(unsigned AbbrevID) { 139 void NaClBitstreamCursor::skipRecord(unsigned AbbrevID) {
137 // Skip unabbreviated records by reading past their entries. 140 // Skip unabbreviated records by reading past their entries.
138 if (AbbrevID == bitc::UNABBREV_RECORD) { 141 if (AbbrevID == bitc::UNABBREV_RECORD) {
139 unsigned Code = ReadVBR(6); 142 unsigned Code = ReadVBR(6);
140 (void)Code; 143 (void)Code;
141 unsigned NumElts = ReadVBR(6); 144 unsigned NumElts = ReadVBR(6);
142 for (unsigned i = 0; i != NumElts; ++i) 145 for (unsigned i = 0; i != NumElts; ++i)
143 (void)ReadVBR64(6); 146 (void)ReadVBR64(6);
144 return; 147 return;
145 } 148 }
146 149
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 if (!canSkipToPos(NewEnd/8)) { 187 if (!canSkipToPos(NewEnd/8)) {
185 NextChar = BitStream->getBitcodeBytes().getExtent(); 188 NextChar = BitStream->getBitcodeBytes().getExtent();
186 break; 189 break;
187 } 190 }
188 191
189 // Skip over the blob. 192 // Skip over the blob.
190 JumpToBit(NewEnd); 193 JumpToBit(NewEnd);
191 } 194 }
192 } 195 }
193 196
194 unsigned BitstreamCursor::readRecord(unsigned AbbrevID, 197 unsigned NaClBitstreamCursor::readRecord(unsigned AbbrevID,
195 SmallVectorImpl<uint64_t> &Vals, 198 SmallVectorImpl<uint64_t> &Vals,
196 StringRef *Blob) { 199 StringRef *Blob) {
197 if (AbbrevID == bitc::UNABBREV_RECORD) { 200 if (AbbrevID == bitc::UNABBREV_RECORD) {
198 unsigned Code = ReadVBR(6); 201 unsigned Code = ReadVBR(6);
199 unsigned NumElts = ReadVBR(6); 202 unsigned NumElts = ReadVBR(6);
200 for (unsigned i = 0; i != NumElts; ++i) 203 for (unsigned i = 0; i != NumElts; ++i)
201 Vals.push_back(ReadVBR64(6)); 204 Vals.push_back(ReadVBR64(6));
202 return Code; 205 return Code;
203 } 206 }
204 207
205 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID); 208 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
206 209
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 // Skip over tail padding. 266 // Skip over tail padding.
264 JumpToBit(NewEnd); 267 JumpToBit(NewEnd);
265 } 268 }
266 269
267 unsigned Code = (unsigned)Vals[0]; 270 unsigned Code = (unsigned)Vals[0];
268 Vals.erase(Vals.begin()); 271 Vals.erase(Vals.begin());
269 return Code; 272 return Code;
270 } 273 }
271 274
272 275
273 void BitstreamCursor::ReadAbbrevRecord() { 276 void NaClBitstreamCursor::ReadAbbrevRecord() {
274 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 277 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
275 unsigned NumOpInfo = ReadVBR(5); 278 unsigned NumOpInfo = ReadVBR(5);
276 for (unsigned i = 0; i != NumOpInfo; ++i) { 279 for (unsigned i = 0; i != NumOpInfo; ++i) {
277 bool IsLiteral = Read(1) ? true : false; 280 bool IsLiteral = Read(1) ? true : false;
278 if (IsLiteral) { 281 if (IsLiteral) {
279 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8))); 282 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
280 continue; 283 continue;
281 } 284 }
282 285
283 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3); 286 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
284 if (BitCodeAbbrevOp::hasEncodingData(E)) { 287 if (BitCodeAbbrevOp::hasEncodingData(E)) {
285 unsigned Data = ReadVBR64(5); 288 unsigned Data = ReadVBR64(5);
286 289
287 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits) 290 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
288 // and vbr(0) as a literal zero. This is decoded the same way, and avoids 291 // and vbr(0) as a literal zero. This is decoded the same way, and avoids
289 // a slow path in Read() to have to handle reading zero bits. 292 // a slow path in Read() to have to handle reading zero bits.
290 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) && 293 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
291 Data == 0) { 294 Data == 0) {
292 Abbv->Add(BitCodeAbbrevOp(0)); 295 Abbv->Add(BitCodeAbbrevOp(0));
293 continue; 296 continue;
294 } 297 }
295 298
296 Abbv->Add(BitCodeAbbrevOp(E, Data)); 299 Abbv->Add(BitCodeAbbrevOp(E, Data));
297 } else 300 } else
298 Abbv->Add(BitCodeAbbrevOp(E)); 301 Abbv->Add(BitCodeAbbrevOp(E));
299 } 302 }
300 CurAbbrevs.push_back(Abbv); 303 CurAbbrevs.push_back(Abbv);
301 } 304 }
302 305
303 bool BitstreamCursor::ReadBlockInfoBlock() { 306 bool NaClBitstreamCursor::ReadBlockInfoBlock() {
304 // If this is the second stream to get to the block info block, skip it. 307 // If this is the second stream to get to the block info block, skip it.
305 if (BitStream->hasBlockInfoRecords()) 308 if (BitStream->hasBlockInfoRecords())
306 return SkipBlock(); 309 return SkipBlock();
307 310
308 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true; 311 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
309 312
310 SmallVector<uint64_t, 64> Record; 313 SmallVector<uint64_t, 64> Record;
311 BitstreamReader::BlockInfo *CurBlockInfo = 0; 314 NaClBitstreamReader::BlockInfo *CurBlockInfo = 0;
312 315
313 // Read all the records for this module. 316 // Read all the records for this module.
314 while (1) { 317 while (1) {
315 BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs); 318 NaClBitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrev s);
316 319
317 switch (Entry.Kind) { 320 switch (Entry.Kind) {
318 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 321 case llvm::NaClBitstreamEntry::SubBlock: // Handled for us already.
319 case llvm::BitstreamEntry::Error: 322 case llvm::NaClBitstreamEntry::Error:
320 return true; 323 return true;
321 case llvm::BitstreamEntry::EndBlock: 324 case llvm::NaClBitstreamEntry::EndBlock:
322 return false; 325 return false;
323 case llvm::BitstreamEntry::Record: 326 case llvm::NaClBitstreamEntry::Record:
324 // The interesting case. 327 // The interesting case.
325 break; 328 break;
326 } 329 }
327 330
328 // Read abbrev records, associate them with CurBID. 331 // Read abbrev records, associate them with CurBID.
329 if (Entry.ID == bitc::DEFINE_ABBREV) { 332 if (Entry.ID == bitc::DEFINE_ABBREV) {
330 if (!CurBlockInfo) return true; 333 if (!CurBlockInfo) return true;
331 ReadAbbrevRecord(); 334 ReadAbbrevRecord();
332 335
333 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the 336 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
(...skipping 27 matching lines...) Expand all
361 std::string Name; 364 std::string Name;
362 for (unsigned i = 1, e = Record.size(); i != e; ++i) 365 for (unsigned i = 1, e = Record.size(); i != e; ++i)
363 Name += (char)Record[i]; 366 Name += (char)Record[i];
364 CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0], 367 CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
365 Name)); 368 Name));
366 break; 369 break;
367 } 370 }
368 } 371 }
369 } 372 }
370 } 373 }
371
OLDNEW
« no previous file with comments | « lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp ('k') | lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698