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

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

Issue 1122423005: Add (unsupported experimental) feature allowing byte aligned bitcode. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@master
Patch Set: Fix nits. Created 5 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 //===- NaClBitstreamReader.cpp --------------------------------------------===// 1 //===- NaClBitstreamReader.cpp --------------------------------------------===//
2 // NaClBitstreamReader implementation 2 // NaClBitstreamReader implementation
3 // 3 //
4 // The LLVM Compiler Infrastructure 4 // The LLVM Compiler Infrastructure
5 // 5 //
6 // This file is distributed under the University of Illinois Open Source 6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details. 7 // License. See LICENSE.TXT for details.
8 // 8 //
9 //===----------------------------------------------------------------------===// 9 //===----------------------------------------------------------------------===//
10 10
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 136
137 /// skipRecord - Read the current record and discard it. 137 /// skipRecord - Read the current record and discard it.
138 void NaClBitstreamCursor::skipRecord(unsigned AbbrevID) { 138 void NaClBitstreamCursor::skipRecord(unsigned AbbrevID) {
139 // Skip unabbreviated records by reading past their entries. 139 // Skip unabbreviated records by reading past their entries.
140 if (AbbrevID == naclbitc::UNABBREV_RECORD) { 140 if (AbbrevID == naclbitc::UNABBREV_RECORD) {
141 unsigned Code = ReadVBR(6); 141 unsigned Code = ReadVBR(6);
142 (void)Code; 142 (void)Code;
143 unsigned NumElts = ReadVBR(6); 143 unsigned NumElts = ReadVBR(6);
144 for (unsigned i = 0; i != NumElts; ++i) 144 for (unsigned i = 0; i != NumElts; ++i)
145 (void)ReadVBR64(6); 145 (void)ReadVBR64(6);
146 SkipToByteBoundaryIfAligned();
146 return; 147 return;
147 } 148 }
148 149
149 const NaClBitCodeAbbrev *Abbv = getAbbrev(AbbrevID); 150 const NaClBitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
150 151
151 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) { 152 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
152 const NaClBitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); 153 const NaClBitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
153 switch (Op.getEncoding()) { 154 switch (Op.getEncoding()) {
154 default: 155 default:
155 skipAbbreviatedField(Op); 156 skipAbbreviatedField(Op);
156 break; 157 break;
157 case NaClBitCodeAbbrevOp::Literal: 158 case NaClBitCodeAbbrevOp::Literal:
158 break; 159 break;
159 case NaClBitCodeAbbrevOp::Array: { 160 case NaClBitCodeAbbrevOp::Array: {
160 // Array case. Read the number of elements as a vbr6. 161 // Array case. Read the number of elements as a vbr6.
161 unsigned NumElts = ReadVBR(6); 162 unsigned NumElts = ReadVBR(6);
162 163
163 // Get the element encoding. 164 // Get the element encoding.
164 const NaClBitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i); 165 const NaClBitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
165 166
166 // Read all the elements. 167 // Read all the elements.
167 for (; NumElts; --NumElts) 168 for (; NumElts; --NumElts)
168 skipAbbreviatedField(EltEnc); 169 skipAbbreviatedField(EltEnc);
169 break; 170 break;
170 } 171 }
171 } 172 }
172 } 173 }
174 SkipToByteBoundaryIfAligned();
173 } 175 }
174 176
175 bool NaClBitstreamCursor::readRecordAbbrevField( 177 bool NaClBitstreamCursor::readRecordAbbrevField(
176 const NaClBitCodeAbbrevOp &Op, uint64_t &Value) { 178 const NaClBitCodeAbbrevOp &Op, uint64_t &Value) {
177 switch (Op.getEncoding()) { 179 switch (Op.getEncoding()) {
178 case NaClBitCodeAbbrevOp::Literal: 180 case NaClBitCodeAbbrevOp::Literal:
179 Value = Op.getValue(); 181 Value = Op.getValue();
180 break; 182 break;
181 case NaClBitCodeAbbrevOp::Array: 183 case NaClBitCodeAbbrevOp::Array:
182 // Returns number of elements in the array. 184 // Returns number of elements in the array.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 } 225 }
224 } 226 }
225 227
226 unsigned NaClBitstreamCursor::readRecord(unsigned AbbrevID, 228 unsigned NaClBitstreamCursor::readRecord(unsigned AbbrevID,
227 SmallVectorImpl<uint64_t> &Vals) { 229 SmallVectorImpl<uint64_t> &Vals) {
228 if (AbbrevID == naclbitc::UNABBREV_RECORD) { 230 if (AbbrevID == naclbitc::UNABBREV_RECORD) {
229 unsigned Code = ReadVBR(6); 231 unsigned Code = ReadVBR(6);
230 unsigned NumElts = ReadVBR(6); 232 unsigned NumElts = ReadVBR(6);
231 for (unsigned i = 0; i != NumElts; ++i) 233 for (unsigned i = 0; i != NumElts; ++i)
232 Vals.push_back(ReadVBR64(6)); 234 Vals.push_back(ReadVBR64(6));
235 SkipToByteBoundaryIfAligned();
233 return Code; 236 return Code;
234 } 237 }
235 238
236 // Read code. 239 // Read code.
237 const NaClBitCodeAbbrev *Abbv = getAbbrev(AbbrevID); 240 const NaClBitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
238 uint64_t Value; 241 uint64_t Value;
239 unsigned Code; 242 unsigned Code;
240 if (readRecordAbbrevField(Abbv->getOperandInfo(0), Value)) { 243 if (readRecordAbbrevField(Abbv->getOperandInfo(0), Value)) {
241 // Array found, use to read all elements. 244 // Array found, use to read all elements.
242 if (Value == 0) 245 if (Value == 0)
243 ErrHandler->Fatal("No code found for record!"); 246 ErrHandler->Fatal("No code found for record!");
244 const NaClBitCodeAbbrevOp &Op = Abbv->getOperandInfo(1); 247 const NaClBitCodeAbbrevOp &Op = Abbv->getOperandInfo(1);
245 Code = readArrayAbbreviatedField(Op); 248 Code = readArrayAbbreviatedField(Op);
246 readArrayAbbrev(Op, Value - 1, Vals); 249 readArrayAbbrev(Op, Value - 1, Vals);
250 SkipToByteBoundaryIfAligned();
247 return Code; 251 return Code;
248 } 252 }
249 Code = Value; 253 Code = Value;
250 254
251 // Read arguments. 255 // Read arguments.
252 unsigned NumOperands = Abbv->getNumOperandInfos(); 256 unsigned NumOperands = Abbv->getNumOperandInfos();
253 for (unsigned i = 1; i != NumOperands; ++i) { 257 for (unsigned i = 1; i != NumOperands; ++i) {
254 if (readRecordAbbrevField(Abbv->getOperandInfo(i), Value)) { 258 if (readRecordAbbrevField(Abbv->getOperandInfo(i), Value)) {
255 ++i; 259 ++i;
256 readArrayAbbrev(Abbv->getOperandInfo(i), Value, Vals); 260 readArrayAbbrev(Abbv->getOperandInfo(i), Value, Vals);
261 SkipToByteBoundaryIfAligned();
257 return Code; 262 return Code;
258 } 263 }
259 Vals.push_back(Value); 264 Vals.push_back(Value);
260 } 265 }
266 SkipToByteBoundaryIfAligned();
261 return Code; 267 return Code;
262 } 268 }
263 269
264 270
265 NaClBitCodeAbbrevOp::Encoding NaClBitstreamCursor:: 271 NaClBitCodeAbbrevOp::Encoding NaClBitstreamCursor::
266 getEncoding(uint64_t Value) { 272 getEncoding(uint64_t Value) {
267 if (!NaClBitCodeAbbrevOp::isValidEncoding(Value)) { 273 if (!NaClBitCodeAbbrevOp::isValidEncoding(Value)) {
268 std::string Buffer; 274 std::string Buffer;
269 raw_string_ostream StrBuf(Buffer); 275 raw_string_ostream StrBuf(Buffer);
270 StrBuf << "Invalid abbreviation encoding specified in bitcode file: " 276 StrBuf << "Invalid abbreviation encoding specified in bitcode file: "
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 if (!NaClBitCodeAbbrevOp::isValid(E)) { 322 if (!NaClBitCodeAbbrevOp::isValid(E)) {
317 std::string Buffer; 323 std::string Buffer;
318 raw_string_ostream StrBuf(Buffer); 324 raw_string_ostream StrBuf(Buffer);
319 StrBuf << "Invalid abbreviation encoding (" 325 StrBuf << "Invalid abbreviation encoding ("
320 << NaClBitCodeAbbrevOp::getEncodingName(E) << ")"; 326 << NaClBitCodeAbbrevOp::getEncodingName(E) << ")";
321 report_fatal_error(StrBuf.str()); 327 report_fatal_error(StrBuf.str());
322 } 328 }
323 Abbv->Add(NaClBitCodeAbbrevOp(E)); 329 Abbv->Add(NaClBitCodeAbbrevOp(E));
324 } 330 }
325 } 331 }
332 SkipToByteBoundaryIfAligned();
326 if (!Abbv->isValid()) 333 if (!Abbv->isValid())
327 report_fatal_error("Invalid abbreviation specified in bitcode file"); 334 report_fatal_error("Invalid abbreviation specified in bitcode file");
328 CurAbbrevs.push_back(Abbv); 335 CurAbbrevs.push_back(Abbv);
329 if (Listener) { 336 if (Listener) {
330 Listener->ProcessAbbreviation(Abbv, IsLocal); 337 Listener->ProcessAbbreviation(Abbv, IsLocal);
331 // Reset record information of the listener. 338 // Reset record information of the listener.
332 Listener->Values.clear(); 339 Listener->Values.clear();
333 Listener->StartBit = GetCurrentBitNo(); 340 Listener->StartBit = GetCurrentBitNo();
334 } 341 }
335 } 342 }
336 343
337 void NaClBitstreamCursor::SkipAbbrevRecord() { 344 void NaClBitstreamCursor::SkipAbbrevRecord() {
338 unsigned NumOpInfo = ReadVBR(5); 345 unsigned NumOpInfo = ReadVBR(5);
339 for (unsigned i = 0; i != NumOpInfo; ++i) { 346 for (unsigned i = 0; i != NumOpInfo; ++i) {
340 bool IsLiteral = Read(1) ? true : false; 347 bool IsLiteral = Read(1) ? true : false;
341 if (IsLiteral) { 348 if (IsLiteral) {
342 ReadVBR64(8); 349 ReadVBR64(8);
343 continue; 350 continue;
344 } 351 }
345 NaClBitCodeAbbrevOp::Encoding E = getEncoding(Read(3)); 352 NaClBitCodeAbbrevOp::Encoding E = getEncoding(Read(3));
346 if (NaClBitCodeAbbrevOp::hasValue(E)) { 353 if (NaClBitCodeAbbrevOp::hasValue(E)) {
347 ReadVBR64(5); 354 ReadVBR64(5);
348 } 355 }
349 } 356 }
357 SkipToByteBoundaryIfAligned();
350 } 358 }
351 359
352 bool NaClBitstreamCursor::ReadBlockInfoBlock(NaClAbbrevListener *Listener) { 360 bool NaClBitstreamCursor::ReadBlockInfoBlock(NaClAbbrevListener *Listener) {
353 // If this is the second stream to get to the block info block, skip it. 361 // If this is the second stream to get to the block info block, skip it.
354 if (BitStream->hasBlockInfoRecords()) 362 if (BitStream->hasBlockInfoRecords())
355 return SkipBlock(); 363 return SkipBlock();
356 364
357 unsigned NumWords; 365 unsigned NumWords;
358 if (EnterSubBlock(naclbitc::BLOCKINFO_BLOCK_ID, &NumWords)) return true; 366 if (EnterSubBlock(naclbitc::BLOCKINFO_BLOCK_ID, &NumWords)) return true;
359 367
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 if (Record.size() < 1) return true; 410 if (Record.size() < 1) return true;
403 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]); 411 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
404 if (Listener) { 412 if (Listener) {
405 Listener->Values = Record; 413 Listener->Values = Record;
406 Listener->SetBID(); 414 Listener->SetBID();
407 } 415 }
408 break; 416 break;
409 } 417 }
410 } 418 }
411 } 419 }
OLDNEW
« no previous file with comments | « lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp ('k') | lib/Bitcode/NaCl/TestUtils/NaClBitcodeMungeReader.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698