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 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, support::little, support::unaligned>(
176 BytesRead = BitcodeBytes.
size() - NextChar;
178 for (
unsigned B = 0;
B != BytesRead; ++
B)
179 CurWord |=
uint64_t(NextCharPtr[
B]) << (
B * 8);
181 NextChar += BytesRead;
182 BitsInCurWord = BytesRead * 8;
187 static const unsigned BitsInWord =
sizeof(
word_t) * 8;
189 assert(NumBits && NumBits <= BitsInWord &&
190 "Cannot return zero or more than BitsInWord bits!");
192 static const unsigned Mask =
sizeof(
word_t) > 4 ? 0x3f : 0x1f;
195 if (BitsInCurWord >= NumBits) {
199 CurWord >>= (NumBits & Mask);
201 BitsInCurWord -= NumBits;
205 word_t R = BitsInCurWord ? CurWord : 0;
206 unsigned BitsLeft = NumBits - BitsInCurWord;
209 return std::move(fillResult);
212 if (BitsLeft > BitsInCurWord)
214 "Unexpected end of file reading %u of %u bits",
215 BitsInCurWord, BitsLeft);
220 CurWord >>= (BitsLeft & Mask);
222 BitsInCurWord -= BitsLeft;
224 R |=
R2 << (NumBits - BitsLeft);
235 assert(NumBits <= 32 && NumBits >= 1 &&
"Invalid NumBits value");
236 const uint32_t MaskBitOrder = (NumBits - 1);
237 const uint32_t Mask = 1UL << MaskBitOrder;
239 if ((Piece & Mask) == 0)
243 unsigned NextBit = 0;
245 Result |= (Piece & (Mask - 1)) << NextBit;
247 if ((Piece & Mask) == 0)
250 NextBit += NumBits-1;
255 MaybeRead =
Read(NumBits);
258 Piece = MaybeRead.
get();
269 assert(NumBits <= 32 && NumBits >= 1 &&
"Invalid NumBits value");
270 const uint32_t MaskBitOrder = (NumBits - 1);
271 const uint32_t Mask = 1UL << MaskBitOrder;
273 if ((Piece & Mask) == 0)
277 unsigned NextBit = 0;
279 Result |=
uint64_t(Piece & (Mask - 1)) << NextBit;
281 if ((Piece & Mask) == 0)
284 NextBit += NumBits-1;
289 MaybeRead =
Read(NumBits);
292 Piece = MaybeRead.
get();
300 BitsInCurWord >= 32) {
301 CurWord >>= BitsInCurWord-32;
319 return Size < BitcodeBytes.
size() * 8;
361 unsigned CurCodeSize = 2;
364 std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs;
367 unsigned PrevCodeSize;
368 std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
370 explicit Block(
unsigned PCS) : PrevCodeSize(PCS) {}
426 unsigned Code = MaybeCode.
get();
439 return MaybeSubBlock.takeError();
447 return std::move(Err);
470 return std::move(Err);
490 return Res.takeError();
496 size_t NumFourBytes = MaybeNum.
get();
503 "can't skip block: already at end of stream");
506 "can't skip to bit %zu from %" PRIu64, SkipTo,
519 if (BlockScope.
empty())
return true;
530 void popBlockScope() {
531 CurCodeSize = BlockScope.
back().PrevCodeSize;
533 CurAbbrevs = std::move(BlockScope.
back().PrevAbbrevs);
545 if (AbbrevNo >= CurAbbrevs.size())
547 std::errc::illegal_byte_sequence,
"Invalid abbrev number");
548 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< word_t > Read(unsigned NumBits)
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.
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()
@ 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< 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)
static BitstreamEntry getEndBlock()
static BitstreamEntry getError()
enum llvm::BitstreamEntry::@184 Kind