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 (
unsigned i = 0,
e = static_cast<unsigned>(BlockInfoRecords.size());
61 if (BlockInfoRecords[i].BlockID == BlockID)
62 return &BlockInfoRecords[i];
71 BlockInfoRecords.emplace_back();
73 return BlockInfoRecords.back();
97 unsigned BitsInCurWord = 0;
100 static const constexpr
size_t MaxChunkSize =
sizeof(
word_t) * 8;
104 : BitcodeBytes(BitcodeBytes) {}
112 return pos <= BitcodeBytes.
size();
116 return BitsInCurWord == 0 && BitcodeBytes.
size() <= NextChar;
121 return NextChar*CHAR_BIT - BitsInCurWord;
133 assert(canSkipToPos(ByteNo) &&
"Invalid location");
144 return Res.takeError();
152 return BitcodeBytes.
data() + ByteNo;
159 assert(!(BitNo % 8) &&
"Expected bit on byte boundary");
160 return getPointerToByte(BitNo / 8, NumBytes);
164 if (NextChar >= BitcodeBytes.
size())
166 "Unexpected end of file reading %u of %u bytes",
167 NextChar, BitcodeBytes.
size());
170 const uint8_t *NextCharPtr = BitcodeBytes.
data() + NextChar;
172 if (BitcodeBytes.
size() >= NextChar +
sizeof(
word_t)) {
173 BytesRead =
sizeof(
word_t);
175 support::endian::read<word_t, support::little, support::unaligned>(
179 BytesRead = BitcodeBytes.
size() - NextChar;
181 for (
unsigned B = 0;
B != BytesRead; ++
B)
182 CurWord |= uint64_t(NextCharPtr[
B]) << (B * 8);
184 NextChar += BytesRead;
185 BitsInCurWord = BytesRead * 8;
190 static const unsigned BitsInWord = MaxChunkSize;
192 assert(NumBits && NumBits <= BitsInWord &&
193 "Cannot return zero or more than BitsInWord bits!");
195 static const unsigned Mask =
sizeof(
word_t) > 4 ? 0x3f : 0x1f;
198 if (BitsInCurWord >= NumBits) {
199 word_t R = CurWord & (~
word_t(0) >> (BitsInWord - NumBits));
202 CurWord >>= (NumBits &
Mask);
204 BitsInCurWord -= NumBits;
208 word_t R = BitsInCurWord ? CurWord : 0;
209 unsigned BitsLeft = NumBits - BitsInCurWord;
211 if (
Error fillResult = fillCurWord())
212 return std::move(fillResult);
215 if (BitsLeft > BitsInCurWord)
217 "Unexpected end of file reading %u of %u bits",
218 BitsInCurWord, BitsLeft);
223 CurWord >>= (BitsLeft &
Mask);
225 BitsInCurWord -= BitsLeft;
227 R |= R2 << (NumBits - BitsLeft);
238 if ((Piece & (1U << (NumBits-1))) == 0)
242 unsigned NextBit = 0;
244 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
246 if ((Piece & (1U << (NumBits-1))) == 0)
249 NextBit += NumBits-1;
250 MaybeRead = Read(NumBits);
253 Piece = MaybeRead.
get();
265 if ((Piece & (1U << (NumBits-1))) == 0)
266 return uint64_t(Piece);
269 unsigned NextBit = 0;
271 Result |= uint64_t(Piece & ((1U << (NumBits-1))-1)) << NextBit;
273 if ((Piece & (1U << (NumBits-1))) == 0)
276 NextBit += NumBits-1;
277 MaybeRead = Read(NumBits);
280 Piece = MaybeRead.
get();
288 BitsInCurWord >= 32) {
289 CurWord >>= BitsInCurWord-32;
342 unsigned CurCodeSize = 2;
345 std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs;
348 unsigned PrevCodeSize;
349 std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
351 explicit Block(
unsigned PCS) : PrevCodeSize(PCS) {}
360 static const size_t MaxChunkSize =
sizeof(
word_t) * 8;
391 AF_DontPopBlockAtEnd = 1,
395 AF_DontAutoprocessAbbrevs = 2
407 unsigned Code = MaybeCode.
get();
411 if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd())
420 return MaybeSubBlock.takeError();
424 !(Flags & AF_DontAutoprocessAbbrevs)) {
427 if (
Error Err = ReadAbbrevRecord())
428 return std::move(Err);
450 if (
Error Err = SkipBlock())
451 return std::move(Err);
471 return Res.takeError();
473 SkipToFourByteBoundary();
477 size_t NumFourBytes = MaybeNum.
get();
481 size_t SkipTo = GetCurrentBitNo() + NumFourBytes * 4 * 8;
484 "can't skip block: already at end of stream");
485 if (!canSkipToPos(SkipTo / 8))
487 "can't skip to bit %zu from %" PRIu64, SkipTo,
490 if (
Error Res = JumpToBit(SkipTo))
497 Error EnterSubBlock(
unsigned BlockID,
unsigned *NumWordsP =
nullptr);
500 if (BlockScope.
empty())
return true;
504 SkipToFourByteBoundary();
511 void popBlockScope() {
512 CurCodeSize = BlockScope.
back().PrevCodeSize;
514 CurAbbrevs = std::move(BlockScope.
back().PrevAbbrevs);
526 if (AbbrevNo >= CurAbbrevs.size())
528 return CurAbbrevs[AbbrevNo].get();
541 Error ReadAbbrevRecord();
549 ReadBlockInfoBlock(
bool ReadBlockInfoNames =
false);
558 #endif // LLVM_BITSTREAM_BITSTREAMREADER_H 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.
This class represents lattice values for constants.
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.
const uint8_t * getPointerToBit(uint64_t BitNo, uint64_t NumBytes)
Get a pointer into the bitstream at the specified bit offset.
Expected< uint32_t > ReadVBR(unsigned NumBits)
ArrayRef< uint8_t > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
BitstreamCursor(MemoryBufferRef BitcodeBytes)
Error takeError()
Take ownership of the stored error.
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.
bool canSkipToPos(size_t pos) const
Expected< BitstreamEntry > advance(unsigned Flags=0)
Advance the current bitstream, returning the next entry in the stream.
Error SkipBlock()
Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body of this block...
Tagged union holding either a T or a Error.
void skipToEnd()
Skip to the end of the file.
Expected< unsigned > ReadCode()
static BitstreamEntry getSubBlock(unsigned ID)
ArrayRef< uint8_t > getBitcodeBytes() const
size_t SizeInBytes() const
Return the size of the stream in bytes.
void SkipToFourByteBoundary()
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
uint64_t getCurrentByteNo() const
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This represents a position within a bitcode file, implemented on top of a SimpleBitstreamCursor.
Expected< uint64_t > ReadVBR64(unsigned NumBits)
const BlockInfo * getBlockInfo(unsigned BlockID) const
If there is block info for the specified ID, return it, otherwise return null.
unsigned getAbbrevIDWidth() const
Return the number of bits used to encode an abbrev #.
Expected< unsigned > ReadSubBlockID()
Having read the ENTER_SUBBLOCK code, read the BlockID for the block.
std::vector< std::shared_ptr< BitCodeAbbrev > > Abbrevs
Error JumpToBit(uint64_t BitNo)
Reset the stream to the specified bit number.
SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes)
static ErrorSuccess success()
Create a success value.
When advancing through a bitstream cursor, each advance can discover a few different kinds of entries...
BitstreamCursor(ArrayRef< uint8_t > BitcodeBytes)
This class maintains the abbreviations read from a block info block.
reference get()
Returns a reference to the stored T value.
DEFINE_ABBREV - Defines an abbrev for the current block.
LLVM_NODISCARD bool empty() const
static BitstreamEntry getEndBlock()
static BitstreamEntry getRecord(unsigned AbbrevID)
Expected< BitstreamEntry > advanceSkippingSubblocks(unsigned Flags=0)
This is a convenience function for clients that don't expect any subblocks.
SimpleBitstreamCursor(StringRef BitcodeBytes)
static BitstreamEntry getError()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
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.
StringRef - Represent a constant reference to a string, i.e.
enum llvm::BitstreamEntry::@159 Kind
This represents a position within a bitstream.
Expected< word_t > Read(unsigned NumBits)
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
BitstreamCursor(StringRef BitcodeBytes)