14#ifndef LLVM_BITSTREAM_BITSTREAMREADER_H
15#define LLVM_BITSTREAM_BITSTREAMREADER_H
46 std::vector<std::shared_ptr<BitCodeAbbrev>>
Abbrevs;
52 std::vector<BlockInfo> BlockInfoRecords;
59 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
60 return &BlockInfoRecords.back();
62 for (
const BlockInfo &BI : BlockInfoRecords)
63 if (BI.BlockID == BlockID)
73 BlockInfoRecords.emplace_back();
74 BlockInfoRecords.back().
BlockID = BlockID;
75 return BlockInfoRecords.back();
99 static constexpr unsigned BitsInWord =
sizeof(
word_t) * 8;
104 : BitcodeBytes(BitcodeBytes),
105 WordLoadEnd(BitcodeBytes.
size() < sizeof(
word_t)
107 : (BitcodeBytes.
size() - sizeof(
word_t) + 1) * 8) {}
115 return pos <= BitcodeBytes.size();
133 "Unexpected end of file jumping to bit %" PRIu64
134 " of %" PRIu64
" bits",
142 return BitcodeBytes.data() + ByteNo;
149 assert(!(BitNo % 8) &&
"Expected bit on byte boundary");
156 word_t loadWordAtBit()
const {
157 assert(BitNo < WordLoadEnd &&
"Word load past the end of the stream");
159 BitcodeBytes.
data() + BitNo / 8) >>
165 assert(NumBits && NumBits <= BitsInWord &&
166 "Cannot return zero or more than BitsInWord bits!");
170 if (NumBits <= BitsInWord - 7 && BitNo < WordLoadEnd) {
171 word_t R = loadWordAtBit() & (
~word_t(0) >> (BitsInWord - NumBits));
189 assert(NumBits <= 32 && NumBits >= 1 &&
"Invalid NumBits value");
190 const uint32_t MaskBitOrder = (NumBits - 1);
191 const uint32_t Mask = 1UL << MaskBitOrder;
193 if ((Piece & Mask) == 0)
197 unsigned NextBit = 0;
199 Result |= (Piece & (Mask - 1)) << NextBit;
201 if ((Piece & Mask) == 0)
204 NextBit += NumBits-1;
209 MaybeRead =
Read(NumBits);
212 Piece = MaybeRead.
get();
223 assert(NumBits <= 32 && NumBits >= 1 &&
"Invalid NumBits value");
224 const uint32_t MaskBitOrder = (NumBits - 1);
225 const uint32_t Mask = 1UL << MaskBitOrder;
227 if ((Piece & Mask) == 0)
231 unsigned NextBit = 0;
233 Result |=
uint64_t(Piece & (Mask - 1)) << NextBit;
235 if ((Piece & Mask) == 0)
238 NextBit += NumBits-1;
243 MaybeRead =
Read(NumBits);
246 Piece = MaybeRead.
get();
267 return Size < BitcodeBytes.size() * 8;
309 unsigned CurCodeSize = 2;
312 std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs;
315 unsigned PrevCodeSize;
316 std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
318 explicit Block(
unsigned PCS) : PrevCodeSize(PCS) {}
373 unsigned Code = MaybeCode.
get();
386 return MaybeSubBlock.takeError();
394 return std::move(Err);
417 return std::move(Err);
437 return Res.takeError();
443 size_t NumFourBytes = MaybeNum.
get();
450 "can't skip block: already at end of stream");
453 "can't skip to bit %zu from %" PRIu64, SkipTo,
466 if (BlockScope.empty())
return true;
477 void popBlockScope() {
478 CurCodeSize = BlockScope.
back().PrevCodeSize;
480 CurAbbrevs = std::move(BlockScope.
back().PrevAbbrevs);
492 if (AbbrevNo >= CurAbbrevs.size())
494 std::errc::illegal_byte_sequence,
"Invalid abbrev number");
495 return CurAbbrevs[AbbrevNo].get();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
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)
BitstreamCursor(StringRef BitcodeBytes)
LLVM_ABI Error ReadAbbrevRecord()
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.
LLVM_ABI Expected< unsigned > readRecord(unsigned AbbrevID, SmallVectorImpl< uint64_t > &Vals, StringRef *Blob=nullptr)
LLVM_ABI Error EnterSubBlock(unsigned BlockID, unsigned *NumWordsP=nullptr)
Having read the ENTER_SUBBLOCK abbrevid, and enter the block.
Error SkipBlock()
Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body of this block.
LLVM_ABI 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
@ 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...
Expected< unsigned > ReadCode()
LLVM_ABI Expected< std::optional< BitstreamBlockInfo > > ReadBlockInfoBlock(bool ReadBlockInfoNames=false)
Read and return a block info block from the bitstream.
Error JumpToBit(uint64_t NewBitNo)
Reset the stream to the specified bit number.
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.
LLVM_ABI Expected< word_t > readSlow(unsigned NumBits)
Read NumBits bits that don't fit in a word load from the current byte, or are near the end of the str...
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
The type of the values returned by Read.
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.
Error JumpToBit(uint64_t NewBitNo)
Reset the stream to the specified bit number.
uint64_t sizeInBits() const
Return the size of the stream in bits.
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.
Represent a constant reference to a string, i.e.
@ DEFINE_ABBREV
DEFINE_ABBREV - Defines an abbrev for the current block.
@ FIRST_APPLICATION_ABBREV
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct an array ref of bytes from a string ref.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
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)
static BitstreamEntry getEndBlock()
static BitstreamEntry getError()