14#ifndef LLVM_BITSTREAM_BITSTREAMREADER_H
15#define LLVM_BITSTREAM_BITSTREAMREADER_H
43 std::vector<std::shared_ptr<BitCodeAbbrev>>
Abbrevs;
49 std::vector<BlockInfo> BlockInfoRecords;
56 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
57 return &BlockInfoRecords.back();
59 for (
const BlockInfo &BI : BlockInfoRecords)
60 if (BI.BlockID == BlockID)
70 BlockInfoRecords.emplace_back();
71 BlockInfoRecords.back().
BlockID = BlockID;
72 return BlockInfoRecords.back();
96 unsigned BitsInCurWord = 0;
101 : BitcodeBytes(BitcodeBytes) {}
103 : BitcodeBytes(arrayRefFromStringRef(BitcodeBytes)) {}
109 return pos <= BitcodeBytes.
size();
113 return BitsInCurWord == 0 && BitcodeBytes.
size() <= NextChar;
118 return uint64_t(NextChar)*CHAR_BIT - BitsInCurWord;
128 size_t ByteNo = size_t(BitNo/8) & ~(
sizeof(
word_t)-1);
141 return Res.takeError();
149 return BitcodeBytes.
data() + ByteNo;
156 assert(!(BitNo % 8) &&
"Expected bit on byte boundary");
161 if (NextChar >= BitcodeBytes.
size())
163 "Unexpected end of file reading %u of %u bytes",
164 NextChar, BitcodeBytes.
size());
167 const uint8_t *NextCharPtr = BitcodeBytes.
data() + NextChar;
169 if (BitcodeBytes.
size() >= NextChar +
sizeof(
word_t)) {
170 BytesRead =
sizeof(
word_t);
172 support::endian::read<word_t, llvm::endianness::little>(NextCharPtr);
175 BytesRead = BitcodeBytes.
size() - NextChar;
177 for (
unsigned B = 0;
B != BytesRead; ++
B)
178 CurWord |=
uint64_t(NextCharPtr[
B]) << (
B * 8);
180 NextChar += BytesRead;
181 BitsInCurWord = BytesRead * 8;
186 static const unsigned BitsInWord =
sizeof(
word_t) * 8;
188 assert(NumBits && NumBits <= BitsInWord &&
189 "Cannot return zero or more than BitsInWord bits!");
191 static const unsigned Mask =
sizeof(
word_t) > 4 ? 0x3f : 0x1f;
194 if (BitsInCurWord >= NumBits) {
198 CurWord >>= (NumBits & Mask);
200 BitsInCurWord -= NumBits;
204 word_t R = BitsInCurWord ? CurWord : 0;
205 unsigned BitsLeft = NumBits - BitsInCurWord;
208 return std::move(fillResult);
211 if (BitsLeft > BitsInCurWord)
213 "Unexpected end of file reading %u of %u bits",
214 BitsInCurWord, BitsLeft);
219 CurWord >>= (BitsLeft & Mask);
221 BitsInCurWord -= BitsLeft;
223 R |=
R2 << (NumBits - BitsLeft);
234 assert(NumBits <= 32 && NumBits >= 1 &&
"Invalid NumBits value");
235 const uint32_t MaskBitOrder = (NumBits - 1);
236 const uint32_t Mask = 1UL << MaskBitOrder;
238 if ((Piece & Mask) == 0)
242 unsigned NextBit = 0;
244 Result |= (Piece & (Mask - 1)) << NextBit;
246 if ((Piece & Mask) == 0)
249 NextBit += NumBits-1;
254 MaybeRead =
Read(NumBits);
257 Piece = MaybeRead.
get();
268 assert(NumBits <= 32 && NumBits >= 1 &&
"Invalid NumBits value");
269 const uint32_t MaskBitOrder = (NumBits - 1);
270 const uint32_t Mask = 1UL << MaskBitOrder;
272 if ((Piece & Mask) == 0)
276 unsigned NextBit = 0;
278 Result |=
uint64_t(Piece & (Mask - 1)) << NextBit;
280 if ((Piece & Mask) == 0)
283 NextBit += NumBits-1;
288 MaybeRead =
Read(NumBits);
291 Piece = MaybeRead.
get();
299 BitsInCurWord >= 32) {
300 CurWord >>= BitsInCurWord-32;
318 return Size < BitcodeBytes.
size() * 8;
360 unsigned CurCodeSize = 2;
363 std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs;
366 unsigned PrevCodeSize;
367 std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
369 explicit Block(
unsigned PCS) : PrevCodeSize(PCS) {}
425 unsigned Code = MaybeCode.
get();
438 return MaybeSubBlock.takeError();
446 return std::move(Err);
469 return std::move(Err);
489 return Res.takeError();
495 size_t NumFourBytes = MaybeNum.
get();
502 "can't skip block: already at end of stream");
505 "can't skip to bit %zu from %" PRIu64, SkipTo,
518 if (BlockScope.
empty())
return true;
529 void popBlockScope() {
530 CurCodeSize = BlockScope.
back().PrevCodeSize;
532 CurAbbrevs = std::move(BlockScope.
back().PrevAbbrevs);
544 if (AbbrevNo >= CurAbbrevs.size())
546 std::errc::illegal_byte_sequence,
"Invalid abbrev number");
547 return CurAbbrevs[AbbrevNo].get();
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
This class maintains the abbreviations read from a block info block.
const BlockInfo * getBlockInfo(unsigned BlockID) const
If there is block info for the specified ID, return it, otherwise return null.
BlockInfo & getOrCreateBlockInfo(unsigned BlockID)
This represents a position within a bitcode file, implemented on top of a SimpleBitstreamCursor.
Error JumpToBit(uint64_t BitNo)
Reset the stream to the specified bit number.
BitstreamCursor(StringRef BitcodeBytes)
uint64_t GetCurrentBitNo() const
Return the bit # of the bit we are reading.
Expected< unsigned > ReadSubBlockID()
Having read the ENTER_SUBBLOCK code, read the BlockID for the block.
static const size_t MaxChunkSize
Expected< BitstreamEntry > advance(unsigned Flags=0)
Advance the current bitstream, returning the next entry in the stream.
BitstreamCursor(MemoryBufferRef BitcodeBytes)
Expected< BitstreamEntry > advanceSkippingSubblocks(unsigned Flags=0)
This is a convenience function for clients that don't expect any subblocks.
Expected< const BitCodeAbbrev * > getAbbrev(unsigned AbbrevID)
Return the abbreviation for the specified AbbrevId.
void setBlockInfo(BitstreamBlockInfo *BI)
Set the block info to be used by this BitstreamCursor to interpret abbreviated records.
Expected< unsigned > readRecord(unsigned AbbrevID, SmallVectorImpl< uint64_t > &Vals, StringRef *Blob=nullptr)
Error EnterSubBlock(unsigned BlockID, unsigned *NumWordsP=nullptr)
Having read the ENTER_SUBBLOCK abbrevid, and enter the block.
@ AF_DontAutoprocessAbbrevs
If this flag is used, abbrev entries are returned just like normal records.
@ AF_DontPopBlockAtEnd
If this flag is used, the advance() method does not automatically pop the block scope when the end of...
Error SkipBlock()
Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body of this block.
Expected< unsigned > skipRecord(unsigned AbbrevID)
Read the current record and discard it, returning the code for the record.
BitstreamCursor(ArrayRef< uint8_t > BitcodeBytes)
BitstreamCursor()=default
Expected< unsigned > ReadCode()
Expected< std::optional< BitstreamBlockInfo > > ReadBlockInfoBlock(bool ReadBlockInfoNames=false)
Read and return a block info block from the bitstream.
Expected< uint32_t > ReadVBR(const unsigned NumBits)
unsigned getAbbrevIDWidth() const
Return the number of bits used to encode an abbrev #.
bool canSkipToPos(size_t pos) const
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
This represents a position within a bitstream.
Error JumpToBit(uint64_t BitNo)
Reset the stream to the specified bit number.
const uint8_t * getPointerToBit(uint64_t BitNo, uint64_t NumBytes)
Get a pointer into the bitstream at the specified bit offset.
uint64_t GetCurrentBitNo() const
Return the bit # of the bit we are reading.
Expected< uint64_t > ReadVBR64(const unsigned NumBits)
SimpleBitstreamCursor()=default
size_t SizeInBytes() const
Return the size of the stream in bytes.
ArrayRef< uint8_t > getBitcodeBytes() const
Expected< word_t > Read(unsigned NumBits)
const uint8_t * getPointerToByte(uint64_t ByteNo, uint64_t NumBytes)
Get a pointer into the bitstream at the specified byte offset.
SimpleBitstreamCursor(StringRef BitcodeBytes)
SimpleBitstreamCursor(ArrayRef< uint8_t > BitcodeBytes)
SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes)
size_t word_t
This is the current data we have pulled from the stream but have not returned to the client.
void SkipToFourByteBoundary()
uint64_t getCurrentByteNo() const
bool isSizePlausible(size_t Size) const
Check whether a reservation of Size elements is plausible.
void skipToEnd()
Skip to the end of the file.
Expected< uint32_t > ReadVBR(const unsigned NumBits)
bool canSkipToPos(size_t pos) const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
@ DEFINE_ABBREV
DEFINE_ABBREV - Defines an abbrev for the current block.
@ FIRST_APPLICATION_ABBREV
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
This contains information emitted to BLOCKINFO_BLOCK blocks.
std::vector< std::pair< unsigned, std::string > > RecordNames
std::vector< std::shared_ptr< BitCodeAbbrev > > Abbrevs
When advancing through a bitstream cursor, each advance can discover a few different kinds of entries...
static BitstreamEntry getSubBlock(unsigned ID)
static BitstreamEntry getRecord(unsigned AbbrevID)
enum llvm::BitstreamEntry::@196 Kind
static BitstreamEntry getEndBlock()
static BitstreamEntry getError()