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();
74 return BlockInfoRecords.back();
98 unsigned BitsInCurWord = 0;
101 static const size_t MaxChunkSize =
sizeof(
word_t) * 8;
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;
135 assert(canSkipToPos(ByteNo) &&
"Invalid location");
148 return BitcodeBytes.
data() + ByteNo;
155 assert(!(BitNo % 8) &&
"Expected bit on byte boundary");
156 return getPointerToByte(BitNo / 8, NumBytes);
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;
183 static const unsigned BitsInWord = MaxChunkSize;
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) {}
333 static const size_t MaxChunkSize =
sizeof(
word_t) * 8;
362 AF_DontPopBlockAtEnd = 1,
366 AF_DontAutoprocessAbbrevs = 2
375 unsigned Code = ReadCode();
378 if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd())
387 !(Flags & AF_DontAutoprocessAbbrevs)) {
414 return Read(CurCodeSize);
431 SkipToFourByteBoundary();
436 size_t SkipTo = GetCurrentBitNo() + NumFourBytes*4*8;
437 if (AtEndOfStream() || !canSkipToPos(SkipTo/8))
446 bool EnterSubBlock(
unsigned BlockID,
unsigned *NumWordsP =
nullptr);
449 if (BlockScope.
empty())
return true;
453 SkipToFourByteBoundary();
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();
481 unsigned skipRecord(
unsigned AbbrevID);
489 void ReadAbbrevRecord();
497 ReadBlockInfoBlock(
bool ReadBlockInfoNames =
false);
506 #endif // LLVM_BITCODE_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.
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)
bool canSkipToPos(size_t pos) const
uint64_t ReadVBR64(unsigned NumBits)
void skipToEnd()
Skip to the end of the file.
uint32_t ReadVBR(unsigned NumBits)
static BitstreamEntry getSubBlock(unsigned ID)
ArrayRef< uint8_t > getBitcodeBytes() const
BitstreamEntry advanceSkippingSubblocks(unsigned Flags=0)
This is a convenience function for clients that don't expect any subblocks.
void SkipToFourByteBoundary()
enum llvm::BitstreamEntry::@149 Kind
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.
const BlockInfo * getBlockInfo(unsigned BlockID) const
If there is block info for the specified ID, return it, otherwise return null.
void JumpToBit(uint64_t BitNo)
Reset the stream to the specified bit number.
unsigned getAbbrevIDWidth() const
Return the number of bits used to encode an abbrev #.
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.
When advancing through a bitstream cursor, each advance can discover a few different kinds of entries...
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
BitstreamCursor(ArrayRef< uint8_t > BitcodeBytes)
This class maintains the abbreviations read from a block info block.
DEFINE_ABBREV - Defines an abbrev for the current block.
LLVM_NODISCARD bool empty() const
static BitstreamEntry getEndBlock()
static BitstreamEntry getRecord(unsigned AbbrevID)
SimpleBitstreamCursor(StringRef BitcodeBytes)
static BitstreamEntry getError()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
This represents a position within a bitstream.
BitstreamCursor(StringRef BitcodeBytes)