15 #ifndef LLVM_BITCODE_BITSTREAMREADER_H
16 #define LLVM_BITCODE_BITSTREAMREADER_H
44 std::vector<std::shared_ptr<BitCodeAbbrev>>
Abbrevs;
50 std::vector<BlockInfo> BlockInfoRecords;
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];
72 BlockInfoRecords.emplace_back();
73 BlockInfoRecords.back().BlockID = BlockID;
74 return BlockInfoRecords.back();
98 unsigned BitsInCurWord = 0;
105 : BitcodeBytes(BitcodeBytes) {}
107 : BitcodeBytes(reinterpret_cast<
const uint8_t *>(BitcodeBytes.data()),
108 BitcodeBytes.size()) {}
114 return pos <= BitcodeBytes.
size();
118 return BitsInCurWord == 0 && BitcodeBytes.
size() <= NextChar;
123 return NextChar*CHAR_BIT - BitsInCurWord;
133 size_t ByteNo = size_t(BitNo/8) & ~(
sizeof(
word_t)-1);
148 return BitcodeBytes.
data() + ByteNo;
155 assert(!(BitNo % 8) &&
"Expected bit on byte boundary");
160 if (NextChar >= BitcodeBytes.
size())
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;
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;
191 if (BitsInCurWord >= NumBits) {
192 word_t R = CurWord & (~
word_t(0) >> (BitsInWord - NumBits));
195 CurWord >>= (NumBits &
Mask);
197 BitsInCurWord -= NumBits;
201 word_t R = BitsInCurWord ? CurWord : 0;
202 unsigned BitsLeft = NumBits - BitsInCurWord;
207 if (BitsLeft > BitsInCurWord)
213 CurWord >>= (BitsLeft &
Mask);
215 BitsInCurWord -= BitsLeft;
217 R |= R2 << (NumBits - BitsLeft);
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);
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);
264 BitsInCurWord >= 32) {
265 CurWord >>= BitsInCurWord-32;
315 unsigned CurCodeSize = 2;
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) {}
414 return Read(CurCodeSize);
446 bool EnterSubBlock(
unsigned BlockID,
unsigned *NumWordsP =
nullptr);
449 if (BlockScope.
empty())
return true;
460 void popBlockScope() {
461 CurCodeSize = BlockScope.
back().PrevCodeSize;
463 CurAbbrevs = std::move(BlockScope.
back().PrevAbbrevs);
475 if (AbbrevNo >= CurAbbrevs.size())
477 return CurAbbrevs[AbbrevNo].get();
506 #endif // LLVM_BITCODE_BITSTREAMREADER_H
If this flag is used, the advance() method does not automatically pop the block scope when the end of...
This contains information emitted to BLOCKINFO_BLOCK blocks.
const BitCodeAbbrev * getAbbrev(unsigned AbbrevID)
Return the abbreviation for the specified AbbrevId.
BlockInfo & getOrCreateBlockInfo(unsigned BlockID)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
uint64_t GetCurrentBitNo() const
Return the bit # of the bit we are reading.
const uint8_t * getPointerToByte(uint64_t ByteNo, uint64_t NumBytes)
Get a pointer into the bitstream at the specified byte offset.
BitCodeAbbrev - This class represents an abbreviation record.
static const size_t MaxChunkSize
const uint8_t * getPointerToBit(uint64_t BitNo, uint64_t NumBytes)
Get a pointer into the bitstream at the specified bit offset.
BitstreamCursor(MemoryBufferRef BitcodeBytes)
SimpleBitstreamCursor(ArrayRef< uint8_t > BitcodeBytes)
std::vector< std::pair< unsigned, std::string > > RecordNames
void setBlockInfo(BitstreamBlockInfo *BI)
Set the block info to be used by this BitstreamCursor to interpret abbreviated records.
word_t Read(unsigned NumBits)
ArrayRef< uint8_t > getBitcodeBytes() const
struct fuzzer::@269 Flags
SimpleBitstreamCursor()=default
uint64_t ReadVBR64(unsigned NumBits)
bool canSkipToPos(size_t pos) const
BitstreamCursor()=default
void skipToEnd()
Skip to the end of the file.
LLVM_NODISCARD bool empty() const
uint32_t ReadVBR(unsigned NumBits)
static BitstreamEntry getSubBlock(unsigned ID)
If this flag is used, abbrev entries are returned just like normal records.
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
BitstreamEntry advanceSkippingSubblocks(unsigned Flags=0)
This is a convenience function for clients that don't expect any subblocks.
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
static const size_t MaxChunkSize
void SkipToFourByteBoundary()
This represents a position within a bitcode file, implemented on top of a SimpleBitstreamCursor.
uint64_t getCurrentByteNo() const
void JumpToBit(uint64_t BitNo)
Reset the stream to the specified bit number.
bool SkipBlock()
Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body of this block...
std::vector< std::shared_ptr< BitCodeAbbrev > > Abbrevs
SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes)
unsigned ReadSubBlockID()
Having read the ENTER_SUBBLOCK code, read the BlockID for the block.
size_t word_t
This is the current data we have pulled from the stream but have not returned to the client...
When advancing through a bitstream cursor, each advance can discover a few different kinds of entries...
BitstreamCursor(ArrayRef< uint8_t > BitcodeBytes)
unsigned readRecord(unsigned AbbrevID, SmallVectorImpl< uint64_t > &Vals, StringRef *Blob=nullptr)
This class maintains the abbreviations read from a block info block.
DEFINE_ABBREV - Defines an abbrev for the current block.
unsigned skipRecord(unsigned AbbrevID)
Read the current record and discard it, returning the code for the record.
Optional< BitstreamBlockInfo > ReadBlockInfoBlock(bool ReadBlockInfoNames=false)
Read and return a block info block from the bitstream.
bool EnterSubBlock(unsigned BlockID, unsigned *NumWordsP=nullptr)
Having read the ENTER_SUBBLOCK abbrevid, enter the block, and return true if the block has an error...
static BitstreamEntry getEndBlock()
enum llvm::BitstreamEntry::@32 Kind
static BitstreamEntry getRecord(unsigned AbbrevID)
SimpleBitstreamCursor(StringRef BitcodeBytes)
static BitstreamEntry getError()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const BlockInfo * getBlockInfo(unsigned BlockID) const
If there is block info for the specified ID, return it, otherwise return null.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
BitstreamEntry advance(unsigned Flags=0)
Advance the current bitstream, returning the next entry in the stream.
StringRef - Represent a constant reference to a string, i.e.
unsigned getAbbrevIDWidth() const
Return the number of bits used to encode an abbrev #.
This represents a position within a bitstream.
BitstreamCursor(StringRef BitcodeBytes)