Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- NaClBitstreamReader.h -----------------------------------*- C++ -*-===// | 1 //===- NaClBitstreamReader.h -----------------------------------*- C++ -*-===// |
| 2 // Low-level bitstream reader interface | 2 // Low-level bitstream reader interface |
| 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 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 222 private: | 222 private: |
| 223 friend class NaClBitstreamCursor; | 223 friend class NaClBitstreamCursor; |
| 224 | 224 |
| 225 std::unique_ptr<MemoryObject> BitcodeBytes; | 225 std::unique_ptr<MemoryObject> BitcodeBytes; |
| 226 | 226 |
| 227 SharedBlockInfoMap BlockInfoRecords; | 227 SharedBlockInfoMap BlockInfoRecords; |
| 228 | 228 |
| 229 /// \brief Holds the offset of the first byte after the header. | 229 /// \brief Holds the offset of the first byte after the header. |
| 230 size_t InitialAddress; | 230 size_t InitialAddress; |
| 231 | 231 |
| 232 // Holds the number of bytes to add to the bitcode position, when | |
| 233 // reporting errors. Useful when using parallel parses of function | |
| 234 // blocks. | |
| 235 size_t ErrorOffset; | |
|
Jim Stichnoth
2016/04/01 23:52:30
Consider "size_t ErrorOffset = 0;" and then you ca
Karl
2016/04/02 17:16:50
Done.
| |
| 236 | |
| 232 // True if filler should be added to byte align records. | 237 // True if filler should be added to byte align records. |
| 233 bool AlignBitcodeRecords = false; | 238 bool AlignBitcodeRecords = false; |
| 234 NaClBitstreamReader(const NaClBitstreamReader&) = delete; | 239 NaClBitstreamReader(const NaClBitstreamReader&) = delete; |
| 235 void operator=(const NaClBitstreamReader&) = delete; | 240 void operator=(const NaClBitstreamReader&) = delete; |
| 236 | 241 |
| 237 | 242 |
| 238 void initFromHeader(NaClBitcodeHeader &Header) { | 243 void initFromHeader(NaClBitcodeHeader &Header) { |
| 239 InitialAddress = Header.getHeaderSize(); | 244 InitialAddress = Header.getHeaderSize(); |
| 240 AlignBitcodeRecords = Header.getAlignBitcodeRecords(); | 245 AlignBitcodeRecords = Header.getAlignBitcodeRecords(); |
| 241 } | 246 } |
| 242 | 247 |
| 243 public: | 248 public: |
| 244 /// Read stream from sequence of bytes [Start .. End) after parsing | 249 /// Read stream from sequence of bytes [Start .. End) after parsing |
| 245 /// the given bitcode header. | 250 /// the given bitcode header. |
| 246 NaClBitstreamReader(const unsigned char *Start, const unsigned char *End, | 251 NaClBitstreamReader(const unsigned char *Start, const unsigned char *End, |
| 247 NaClBitcodeHeader &Header) | 252 NaClBitcodeHeader &Header) |
| 248 : BitcodeBytes(getNonStreamedMemoryObject(Start, End)), | 253 : BitcodeBytes(getNonStreamedMemoryObject(Start, End)), |
| 249 BlockInfoRecords(BlockInfoRecordsMap::create()) { | 254 BlockInfoRecords(BlockInfoRecordsMap::create()), ErrorOffset(0) { |
| 250 initFromHeader(Header); | 255 initFromHeader(Header); |
| 251 } | 256 } |
| 252 | 257 |
| 253 /// Read stream from Bytes, after parsing the given bitcode header. | 258 /// Read stream from Bytes, after parsing the given bitcode header. |
| 254 NaClBitstreamReader(MemoryObject *Bytes, NaClBitcodeHeader &Header) | 259 NaClBitstreamReader(MemoryObject *Bytes, NaClBitcodeHeader &Header) |
| 255 : BitcodeBytes(Bytes), BlockInfoRecords(BlockInfoRecordsMap::create()) { | 260 : BitcodeBytes(Bytes), BlockInfoRecords(BlockInfoRecordsMap::create()), |
| 256 initFromHeader(Header); | 261 ErrorOffset(0) { initFromHeader(Header); |
| 257 } | 262 } |
| 258 | 263 |
| 259 /// Read stream from bytes, starting at the given initial address. | 264 /// Read stream from bytes, starting at the given initial address. |
| 260 /// Provides simple API for unit testing. | 265 /// Provides simple API for unit testing. |
| 261 NaClBitstreamReader(MemoryObject *Bytes, size_t InitialAddress) | 266 NaClBitstreamReader(MemoryObject *Bytes, size_t InitialAddress) |
| 262 : BitcodeBytes(Bytes), BlockInfoRecords(BlockInfoRecordsMap::create()), | 267 : BitcodeBytes(Bytes), BlockInfoRecords(BlockInfoRecordsMap::create()), |
| 263 InitialAddress(InitialAddress) { | 268 InitialAddress(InitialAddress), |
| 264 } | 269 ErrorOffset(0) {} |
| 265 | 270 |
| 266 /// Read stream from sequence of bytes [Start .. End), using the global | 271 /// Read stream from sequence of bytes [Start .. End), using the global |
| 267 /// abbreviations of the given bitstream reader. Assumes that [Start .. End) | 272 /// abbreviations of the given bitstream reader. Assumes that [Start .. End) |
| 268 /// is copied from Reader's memory object. | 273 /// is copied from Reader's memory object. |
| 269 NaClBitstreamReader(const unsigned char *Start, | 274 NaClBitstreamReader(size_t StartAddress, const unsigned char *Start, |
| 270 const unsigned char *End, NaClBitstreamReader *Reader) | 275 const unsigned char *End, NaClBitstreamReader *Reader) |
| 271 : BitcodeBytes(getNonStreamedMemoryObject(Start, End)), | 276 : BitcodeBytes(getNonStreamedMemoryObject(Start, End)), |
| 272 BlockInfoRecords(Reader->BlockInfoRecords), InitialAddress(0) | 277 BlockInfoRecords(Reader->BlockInfoRecords), InitialAddress(0), |
| 273 { BlockInfoRecords->freeze(); } | 278 ErrorOffset(StartAddress) { BlockInfoRecords->freeze(); } |
| 274 | 279 |
| 275 // Returns the memory object that is being read. | 280 // Returns the memory object that is being read. |
| 276 MemoryObject &getBitcodeBytes() { return *BitcodeBytes; } | 281 MemoryObject &getBitcodeBytes() { return *BitcodeBytes; } |
| 277 | 282 |
| 278 ~NaClBitstreamReader() {} | 283 ~NaClBitstreamReader() {} |
| 279 | 284 |
| 280 /// \brief Returns the initial address (after the header) of the input stream. | 285 /// \brief Returns the initial address (after the header) of the input stream. |
| 281 size_t getInitialAddress() const { | 286 size_t getInitialAddress() const { |
| 282 return InitialAddress; | 287 return InitialAddress; |
| 283 } | 288 } |
| 284 | 289 |
| 290 /// Returns the byte address of the first byte in the bitstream. Used | |
| 291 /// for error reporting. | |
| 292 size_t getErrorOffset() const { return ErrorOffset; } | |
| 293 | |
| 285 //===--------------------------------------------------------------------===// | 294 //===--------------------------------------------------------------------===// |
| 286 // Block Manipulation | 295 // Block Manipulation |
| 287 //===--------------------------------------------------------------------===// | 296 //===--------------------------------------------------------------------===// |
| 288 | 297 |
| 289 BlockInfo *getBlockInfo(unsigned BlockID) { | 298 BlockInfo *getBlockInfo(unsigned BlockID) { |
| 290 return BlockInfoRecords->getBlockInfo(BlockID); | 299 return BlockInfoRecords->getBlockInfo(BlockID); |
| 291 } | 300 } |
| 292 }; | 301 }; |
| 293 | 302 |
| 294 /// When advancing through a bitstream cursor, each advance can discover a few | 303 /// When advancing through a bitstream cursor, each advance can discover a few |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 528 /// Return the number of bits used to encode an abbrev #. | 537 /// Return the number of bits used to encode an abbrev #. |
| 529 unsigned getAbbrevIDWidth() const { | 538 unsigned getAbbrevIDWidth() const { |
| 530 return BlockScope.back().getCodeAbbrev().NumBits; | 539 return BlockScope.back().getCodeAbbrev().NumBits; |
| 531 } | 540 } |
| 532 | 541 |
| 533 /// Return the bit # of the bit we are reading. | 542 /// Return the bit # of the bit we are reading. |
| 534 uint64_t GetCurrentBitNo() const { | 543 uint64_t GetCurrentBitNo() const { |
| 535 return NextChar*CHAR_BIT - BitsInCurWord; | 544 return NextChar*CHAR_BIT - BitsInCurWord; |
| 536 } | 545 } |
| 537 | 546 |
| 547 /// Converts the given position into the corresponding Error position. | |
| 548 uint64_t getErrorBitNo(uint64_t Position) const { | |
| 549 return BitStream->getErrorOffset() * CHAR_BIT + Position; | |
| 550 } | |
| 551 | |
| 552 /// Returns the current bit address for reporting errors. | |
| 553 uint64_t getErrorBitNo() const { | |
| 554 return getErrorBitNo(GetCurrentBitNo()); | |
| 555 } | |
| 556 | |
| 538 NaClBitstreamReader *getBitStreamReader() { | 557 NaClBitstreamReader *getBitStreamReader() { |
| 539 return BitStream; | 558 return BitStream; |
| 540 } | 559 } |
| 541 const NaClBitstreamReader *getBitStreamReader() const { | 560 const NaClBitstreamReader *getBitStreamReader() const { |
| 542 return BitStream; | 561 return BitStream; |
| 543 } | 562 } |
| 544 | 563 |
| 545 /// Returns the current bit address (string) of the bit cursor. | 564 /// Returns the current bit address (string) of the bit cursor. |
| 546 std::string getCurrentBitAddress() const { | 565 std::string getCurrentBitAddress() const { |
| 547 return naclbitc::getBitAddress(GetCurrentBitNo()); | 566 return naclbitc::getBitAddress(GetCurrentBitNo()); |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 886 // Skips over an abbreviation record. Duplicates code of ReadAbbrevRecord, | 905 // Skips over an abbreviation record. Duplicates code of ReadAbbrevRecord, |
| 887 // except that no abbreviation is built. | 906 // except that no abbreviation is built. |
| 888 void SkipAbbrevRecord(); | 907 void SkipAbbrevRecord(); |
| 889 | 908 |
| 890 bool ReadBlockInfoBlock(NaClAbbrevListener *Listener); | 909 bool ReadBlockInfoBlock(NaClAbbrevListener *Listener); |
| 891 }; | 910 }; |
| 892 | 911 |
| 893 } // End llvm namespace | 912 } // End llvm namespace |
| 894 | 913 |
| 895 #endif | 914 #endif |
| OLD | NEW |