1 //===- BitstreamReader.h - Low-level bitstream reader interface -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header defines the BitstreamReader class. This class can be used to
11 // read an arbitrary bitstream, regardless of its contents.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_BITCODE_BITSTREAMREADER_H
16 #define LLVM_BITCODE_BITSTREAMREADER_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/Bitcode/BitCodes.h"
21 #include "llvm/Support/Endian.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/MathExtras.h"
24 #include "llvm/Support/MemoryBuffer.h"
37 /// This class maintains the abbreviations read from a block info block.
38 class BitstreamBlockInfo {
40 /// This contains information emitted to BLOCKINFO_BLOCK blocks. These
41 /// describe abbreviations that all blocks of the specified ID inherit.
44 std::vector<std::shared_ptr<BitCodeAbbrev>> Abbrevs;
46 std::vector<std::pair<unsigned, std::string> > RecordNames;
50 std::vector<BlockInfo> BlockInfoRecords;
53 /// If there is block info for the specified ID, return it, otherwise return
55 const BlockInfo *getBlockInfo(unsigned BlockID) const {
56 // Common case, the most recent entry matches BlockID.
57 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
58 return &BlockInfoRecords.back();
60 for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
62 if (BlockInfoRecords[i].BlockID == BlockID)
63 return &BlockInfoRecords[i];
67 BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
68 if (const BlockInfo *BI = getBlockInfo(BlockID))
69 return *const_cast<BlockInfo*>(BI);
71 // Otherwise, add a new record.
72 BlockInfoRecords.emplace_back();
73 BlockInfoRecords.back().BlockID = BlockID;
74 return BlockInfoRecords.back();
78 /// This represents a position within a bitstream. There may be multiple
79 /// independent cursors reading within one bitstream, each maintaining their
81 class SimpleBitstreamCursor {
82 ArrayRef<uint8_t> BitcodeBytes;
86 /// This is the current data we have pulled from the stream but have not
87 /// returned to the client. This is specifically and intentionally defined to
88 /// follow the word size of the host machine for efficiency. We use word_t in
89 /// places that are aware of this to make it perfectly explicit what is going
91 typedef size_t word_t;
96 /// This is the number of bits in CurWord that are valid. This is always from
97 /// [0...bits_of(size_t)-1] inclusive.
98 unsigned BitsInCurWord = 0;
101 static const size_t MaxChunkSize = sizeof(word_t) * 8;
103 SimpleBitstreamCursor() = default;
104 explicit SimpleBitstreamCursor(ArrayRef<uint8_t> BitcodeBytes)
105 : BitcodeBytes(BitcodeBytes) {}
106 explicit SimpleBitstreamCursor(StringRef BitcodeBytes)
107 : BitcodeBytes(reinterpret_cast<const uint8_t *>(BitcodeBytes.data()),
108 BitcodeBytes.size()) {}
109 explicit SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes)
110 : SimpleBitstreamCursor(BitcodeBytes.getBuffer()) {}
112 bool canSkipToPos(size_t pos) const {
113 // pos can be skipped to if it is a valid address or one byte past the end.
114 return pos <= BitcodeBytes.size();
117 bool AtEndOfStream() {
118 return BitsInCurWord == 0 && BitcodeBytes.size() <= NextChar;
121 /// Return the bit # of the bit we are reading.
122 uint64_t GetCurrentBitNo() const {
123 return NextChar*CHAR_BIT - BitsInCurWord;
126 // Return the byte # of the current bit.
127 uint64_t getCurrentByteNo() const { return GetCurrentBitNo() / 8; }
129 ArrayRef<uint8_t> getBitcodeBytes() const { return BitcodeBytes; }
131 /// Reset the stream to the specified bit number.
132 void JumpToBit(uint64_t BitNo) {
133 size_t ByteNo = size_t(BitNo/8) & ~(sizeof(word_t)-1);
134 unsigned WordBitNo = unsigned(BitNo & (sizeof(word_t)*8-1));
135 assert(canSkipToPos(ByteNo) && "Invalid location");
137 // Move the cursor to the right word.
141 // Skip over any bits that are already consumed.
146 /// Get a pointer into the bitstream at the specified byte offset.
147 const uint8_t *getPointerToByte(uint64_t ByteNo, uint64_t NumBytes) {
148 return BitcodeBytes.data() + ByteNo;
151 /// Get a pointer into the bitstream at the specified bit offset.
153 /// The bit offset must be on a byte boundary.
154 const uint8_t *getPointerToBit(uint64_t BitNo, uint64_t NumBytes) {
155 assert(!(BitNo % 8) && "Expected bit on byte boundary");
156 return getPointerToByte(BitNo / 8, NumBytes);
160 if (NextChar >= BitcodeBytes.size())
161 report_fatal_error("Unexpected end of file");
163 // Read the next word from the stream.
164 const uint8_t *NextCharPtr = BitcodeBytes.data() + NextChar;
166 if (BitcodeBytes.size() >= NextChar + sizeof(word_t)) {
167 BytesRead = sizeof(word_t);
169 support::endian::read<word_t, support::little, support::unaligned>(
173 BytesRead = BitcodeBytes.size() - NextChar;
175 for (unsigned B = 0; B != BytesRead; ++B)
176 CurWord |= uint64_t(NextCharPtr[B]) << (B * 8);
178 NextChar += BytesRead;
179 BitsInCurWord = BytesRead * 8;
182 word_t Read(unsigned NumBits) {
183 static const unsigned BitsInWord = MaxChunkSize;
185 assert(NumBits && NumBits <= BitsInWord &&
186 "Cannot return zero or more than BitsInWord bits!");
188 static const unsigned Mask = sizeof(word_t) > 4 ? 0x3f : 0x1f;
190 // If the field is fully contained by CurWord, return it quickly.
191 if (BitsInCurWord >= NumBits) {
192 word_t R = CurWord & (~word_t(0) >> (BitsInWord - NumBits));
194 // Use a mask to avoid undefined behavior.
195 CurWord >>= (NumBits & Mask);
197 BitsInCurWord -= NumBits;
201 word_t R = BitsInCurWord ? CurWord : 0;
202 unsigned BitsLeft = NumBits - BitsInCurWord;
206 // If we run out of data, abort.
207 if (BitsLeft > BitsInCurWord)
208 report_fatal_error("Unexpected end of file");
210 word_t R2 = CurWord & (~word_t(0) >> (BitsInWord - BitsLeft));
212 // Use a mask to avoid undefined behavior.
213 CurWord >>= (BitsLeft & Mask);
215 BitsInCurWord -= BitsLeft;
217 R |= R2 << (NumBits - BitsLeft);
222 uint32_t ReadVBR(unsigned NumBits) {
223 uint32_t Piece = Read(NumBits);
224 if ((Piece & (1U << (NumBits-1))) == 0)
228 unsigned NextBit = 0;
230 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
232 if ((Piece & (1U << (NumBits-1))) == 0)
235 NextBit += NumBits-1;
236 Piece = Read(NumBits);
240 // Read a VBR that may have a value up to 64-bits in size. The chunk size of
241 // the VBR must still be <= 32 bits though.
242 uint64_t ReadVBR64(unsigned NumBits) {
243 uint32_t Piece = Read(NumBits);
244 if ((Piece & (1U << (NumBits-1))) == 0)
245 return uint64_t(Piece);
248 unsigned NextBit = 0;
250 Result |= uint64_t(Piece & ((1U << (NumBits-1))-1)) << NextBit;
252 if ((Piece & (1U << (NumBits-1))) == 0)
255 NextBit += NumBits-1;
256 Piece = Read(NumBits);
260 void SkipToFourByteBoundary() {
261 // If word_t is 64-bits and if we've read less than 32 bits, just dump
262 // the bits we have up to the next 32-bit boundary.
263 if (sizeof(word_t) > 4 &&
264 BitsInCurWord >= 32) {
265 CurWord >>= BitsInCurWord-32;
273 /// Skip to the end of the file.
274 void skipToEnd() { NextChar = BitcodeBytes.size(); }
277 /// When advancing through a bitstream cursor, each advance can discover a few
278 /// different kinds of entries:
279 struct BitstreamEntry {
281 Error, // Malformed bitcode was found.
282 EndBlock, // We've reached the end of the current block, (or the end of the
283 // file, which is treated like a series of EndBlock records.
284 SubBlock, // This is the start of a new subblock of a specific ID.
285 Record // This is a record with a specific AbbrevID.
290 static BitstreamEntry getError() {
291 BitstreamEntry E; E.Kind = Error; return E;
294 static BitstreamEntry getEndBlock() {
295 BitstreamEntry E; E.Kind = EndBlock; return E;
298 static BitstreamEntry getSubBlock(unsigned ID) {
299 BitstreamEntry E; E.Kind = SubBlock; E.ID = ID; return E;
302 static BitstreamEntry getRecord(unsigned AbbrevID) {
303 BitstreamEntry E; E.Kind = Record; E.ID = AbbrevID; return E;
307 /// This represents a position within a bitcode file, implemented on top of a
308 /// SimpleBitstreamCursor.
310 /// Unlike iterators, BitstreamCursors are heavy-weight objects that should not
311 /// be passed by value.
312 class BitstreamCursor : SimpleBitstreamCursor {
313 // This is the declared size of code values used for the current block, in
315 unsigned CurCodeSize = 2;
317 /// Abbrevs installed at in this block.
318 std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs;
321 unsigned PrevCodeSize;
322 std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
324 explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
327 /// This tracks the codesize of parent blocks.
328 SmallVector<Block, 8> BlockScope;
330 BitstreamBlockInfo *BlockInfo = nullptr;
333 static const size_t MaxChunkSize = sizeof(word_t) * 8;
335 BitstreamCursor() = default;
336 explicit BitstreamCursor(ArrayRef<uint8_t> BitcodeBytes)
337 : SimpleBitstreamCursor(BitcodeBytes) {}
338 explicit BitstreamCursor(StringRef BitcodeBytes)
339 : SimpleBitstreamCursor(BitcodeBytes) {}
340 explicit BitstreamCursor(MemoryBufferRef BitcodeBytes)
341 : SimpleBitstreamCursor(BitcodeBytes) {}
343 using SimpleBitstreamCursor::canSkipToPos;
344 using SimpleBitstreamCursor::AtEndOfStream;
345 using SimpleBitstreamCursor::getBitcodeBytes;
346 using SimpleBitstreamCursor::GetCurrentBitNo;
347 using SimpleBitstreamCursor::getCurrentByteNo;
348 using SimpleBitstreamCursor::getPointerToByte;
349 using SimpleBitstreamCursor::JumpToBit;
350 using SimpleBitstreamCursor::fillCurWord;
351 using SimpleBitstreamCursor::Read;
352 using SimpleBitstreamCursor::ReadVBR;
353 using SimpleBitstreamCursor::ReadVBR64;
355 /// Return the number of bits used to encode an abbrev #.
356 unsigned getAbbrevIDWidth() const { return CurCodeSize; }
358 /// Flags that modify the behavior of advance().
360 /// If this flag is used, the advance() method does not automatically pop
361 /// the block scope when the end of a block is reached.
362 AF_DontPopBlockAtEnd = 1,
364 /// If this flag is used, abbrev entries are returned just like normal
366 AF_DontAutoprocessAbbrevs = 2
369 /// Advance the current bitstream, returning the next entry in the stream.
370 BitstreamEntry advance(unsigned Flags = 0) {
373 return BitstreamEntry::getError();
375 unsigned Code = ReadCode();
376 if (Code == bitc::END_BLOCK) {
377 // Pop the end of the block unless Flags tells us not to.
378 if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd())
379 return BitstreamEntry::getError();
380 return BitstreamEntry::getEndBlock();
383 if (Code == bitc::ENTER_SUBBLOCK)
384 return BitstreamEntry::getSubBlock(ReadSubBlockID());
386 if (Code == bitc::DEFINE_ABBREV &&
387 !(Flags & AF_DontAutoprocessAbbrevs)) {
388 // We read and accumulate abbrev's, the client can't do anything with
394 return BitstreamEntry::getRecord(Code);
398 /// This is a convenience function for clients that don't expect any
399 /// subblocks. This just skips over them automatically.
400 BitstreamEntry advanceSkippingSubblocks(unsigned Flags = 0) {
402 // If we found a normal entry, return it.
403 BitstreamEntry Entry = advance(Flags);
404 if (Entry.Kind != BitstreamEntry::SubBlock)
407 // If we found a sub-block, just skip over it and check the next entry.
409 return BitstreamEntry::getError();
413 unsigned ReadCode() {
414 return Read(CurCodeSize);
418 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
420 /// Having read the ENTER_SUBBLOCK code, read the BlockID for the block.
421 unsigned ReadSubBlockID() {
422 return ReadVBR(bitc::BlockIDWidth);
425 /// Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body
426 /// of this block. If the block record is malformed, return true.
428 // Read and ignore the codelen value. Since we are skipping this block, we
429 // don't care what code widths are used inside of it.
430 ReadVBR(bitc::CodeLenWidth);
431 SkipToFourByteBoundary();
432 unsigned NumFourBytes = Read(bitc::BlockSizeWidth);
434 // Check that the block wasn't partially defined, and that the offset isn't
436 size_t SkipTo = GetCurrentBitNo() + NumFourBytes*4*8;
437 if (AtEndOfStream() || !canSkipToPos(SkipTo/8))
444 /// Having read the ENTER_SUBBLOCK abbrevid, enter the block, and return true
445 /// if the block has an error.
446 bool EnterSubBlock(unsigned BlockID, unsigned *NumWordsP = nullptr);
448 bool ReadBlockEnd() {
449 if (BlockScope.empty()) return true;
452 // [END_BLOCK, <align4bytes>]
453 SkipToFourByteBoundary();
460 void popBlockScope() {
461 CurCodeSize = BlockScope.back().PrevCodeSize;
463 CurAbbrevs = std::move(BlockScope.back().PrevAbbrevs);
464 BlockScope.pop_back();
467 //===--------------------------------------------------------------------===//
469 //===--------------------------------------------------------------------===//
472 /// Return the abbreviation for the specified AbbrevId.
473 const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
474 unsigned AbbrevNo = AbbrevID - bitc::FIRST_APPLICATION_ABBREV;
475 if (AbbrevNo >= CurAbbrevs.size())
476 report_fatal_error("Invalid abbrev number");
477 return CurAbbrevs[AbbrevNo].get();
480 /// Read the current record and discard it, returning the code for the record.
481 unsigned skipRecord(unsigned AbbrevID);
483 unsigned readRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals,
484 StringRef *Blob = nullptr);
486 //===--------------------------------------------------------------------===//
488 //===--------------------------------------------------------------------===//
489 void ReadAbbrevRecord();
491 /// Read and return a block info block from the bitstream. If an error was
492 /// encountered, return None.
494 /// \param ReadBlockInfoNames Whether to read block/record name information in
495 /// the BlockInfo block. Only llvm-bcanalyzer uses this.
496 Optional<BitstreamBlockInfo>
497 ReadBlockInfoBlock(bool ReadBlockInfoNames = false);
499 /// Set the block info to be used by this BitstreamCursor to interpret
500 /// abbreviated records.
501 void setBlockInfo(BitstreamBlockInfo *BI) { BlockInfo = BI; }
504 } // end llvm namespace
506 #endif // LLVM_BITCODE_BITSTREAMREADER_H