15 #ifndef LLVM_BITCODE_BITSTREAMREADER_H
16 #define LLVM_BITCODE_BITSTREAMREADER_H
49 std::vector<BlockInfo> BlockInfoRecords;
54 bool IgnoreBlockInfoNames;
63 IgnoreBlockInfoNames =
true;
68 BitcodeBytes.reset(bytes);
71 void init(
const unsigned char *Start,
const unsigned char *End) {
72 assert(((End-Start) & 3) == 0 &&
"Bitcode stream not a multiple of 4 bytes");
80 while (!BlockInfoRecords.empty()) {
81 BlockInfo &Info = BlockInfoRecords.back();
83 for (
unsigned i = 0, e = static_cast<unsigned>(Info.
Abbrevs.size());
86 BlockInfoRecords.pop_back();
108 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
109 return &BlockInfoRecords.back();
111 for (
unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
113 if (BlockInfoRecords[i].BlockID == BlockID)
114 return &BlockInfoRecords[i];
124 BlockInfoRecords.back().BlockID = BlockID;
125 return BlockInfoRecords.back();
179 typedef uint32_t word_t;
184 unsigned BitsInCurWord;
188 unsigned CurCodeSize;
191 std::vector<BitCodeAbbrev*> CurAbbrevs;
194 unsigned PrevCodeSize;
195 std::vector<BitCodeAbbrev*> PrevAbbrevs;
196 explicit Block(
unsigned PCS) : PrevCodeSize(PCS) {}
200 SmallVector<Block, 8> BlockScope;
242 static_cast<uint64_t>(pos - 1));
246 uint8_t buf[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
252 return BitsInCurWord == 0 &&
isEndPos(NextChar);
260 return NextChar*CHAR_BIT - BitsInCurWord;
326 uintptr_t ByteNo = uintptr_t(BitNo/8) & ~(
sizeof(word_t)-1);
327 unsigned WordBitNo =
unsigned(BitNo & (
sizeof(word_t)*8-1));
337 if (
sizeof(word_t) > 4)
345 uint32_t
Read(
unsigned NumBits) {
346 assert(NumBits && NumBits <= 32 &&
347 "Cannot return zero or more than 32 bits!");
350 if (BitsInCurWord >= NumBits) {
351 uint32_t R = uint32_t(CurWord) & (~0U >> (32-NumBits));
353 BitsInCurWord -= NumBits;
364 uint32_t R = uint32_t(CurWord);
367 uint8_t Array[
sizeof(word_t)] = {0};
374 memcpy(&EndianValue, Array,
sizeof(Array));
376 CurWord = EndianValue;
378 NextChar +=
sizeof(word_t);
381 unsigned BitsLeft = NumBits-BitsInCurWord;
384 R |= uint32_t((CurWord & (word_t(~0ULL) >> (
sizeof(word_t)*8-BitsLeft)))
389 if (BitsLeft !=
sizeof(word_t)*8)
390 CurWord >>= BitsLeft;
393 BitsInCurWord =
sizeof(word_t)*8-BitsLeft;
398 if (NumBits <= 32)
return Read(NumBits);
400 uint64_t V =
Read(32);
401 return V | (uint64_t)
Read(NumBits-32) << 32;
405 uint32_t Piece =
Read(NumBits);
406 if ((Piece & (1U << (NumBits-1))) == 0)
410 unsigned NextBit = 0;
412 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
414 if ((Piece & (1U << (NumBits-1))) == 0)
417 NextBit += NumBits-1;
418 Piece =
Read(NumBits);
425 uint32_t Piece =
Read(NumBits);
426 if ((Piece & (1U << (NumBits-1))) == 0)
427 return uint64_t(Piece);
430 unsigned NextBit = 0;
432 Result |= uint64_t(Piece & ((1U << (NumBits-1))-1)) << NextBit;
434 if ((Piece & (1U << (NumBits-1))) == 0)
437 NextBit += NumBits-1;
438 Piece =
Read(NumBits);
443 void SkipToFourByteBoundary() {
446 if (
sizeof(word_t) > 4 &&
447 BitsInCurWord >= 32) {
448 CurWord >>= BitsInCurWord-32;
459 return Read(CurCodeSize);
479 SkipToFourByteBoundary();
494 bool EnterSubBlock(
unsigned BlockID,
unsigned *NumWordsP = 0);
497 if (BlockScope.
empty())
return true;
501 SkipToFourByteBoundary();
509 void popBlockScope() {
510 CurCodeSize = BlockScope.
back().PrevCodeSize;
513 for (
unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
515 CurAbbrevs[i]->dropRef();
517 BlockScope.
back().PrevAbbrevs.swap(CurAbbrevs);
526 void readAbbreviatedLiteral(
const BitCodeAbbrevOp &Op,
527 SmallVectorImpl<uint64_t> &Vals);
528 void readAbbreviatedField(
const BitCodeAbbrevOp &Op,
529 SmallVectorImpl<uint64_t> &Vals);
530 void skipAbbreviatedField(
const BitCodeAbbrevOp &Op);
537 assert(AbbrevNo < CurAbbrevs.size() &&
"Invalid abbrev #!");
538 return CurAbbrevs[AbbrevNo];
const BitCodeAbbrev * getAbbrev(unsigned AbbrevID)
getAbbrev - Return the abbreviation for the specified AbbrevId.
bool EnterSubBlock(unsigned BlockID, unsigned *NumWordsP=0)
virtual int readBytes(uint64_t address, uint64_t size, uint8_t *buf) const LLVM_OVERRIDE=0
bool hasBlockInfoRecords() const
StreamableMemoryObject & getBitcodeBytes()
BitstreamCursor(BitstreamReader &R)
unsigned readRecord(unsigned AbbrevID, SmallVectorImpl< uint64_t > &Vals, StringRef *Blob=0)
uint64_t GetCurrentBitNo() const
GetCurrentBitNo - Return the bit # of the bit we are reading.
bool canSkipToPos(size_t pos) const
const BlockInfo * getBlockInfo(unsigned BlockID) const
std::vector< std::pair< unsigned, std::string > > RecordNames
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
BlockInfo & getOrCreateBlockInfo(unsigned BlockID)
static BitstreamEntry getSubBlock(unsigned ID)
void operator=(const BitstreamCursor &RHS)
BitstreamEntry advanceSkippingSubblocks(unsigned Flags=0)
BitstreamReader * getBitStreamReader()
uint64_t ReadVBR64(unsigned NumBits)
StreamableMemoryObject * getNonStreamedMemoryObject(const unsigned char *Start, const unsigned char *End)
void init(const unsigned char *Start, const unsigned char *End)
void init(BitstreamReader &R)
std::vector< BitCodeAbbrev * > Abbrevs
virtual bool isObjectEnd(uint64_t address) const =0
uint64_t Read64(unsigned NumBits)
bool isEndPos(size_t pos)
friend class Deserializer
unsigned ReadSubBlockID()
uint32_t ReadVBR(unsigned NumBits)
BitstreamReader(const unsigned char *Start, const unsigned char *End)
void skipRecord(unsigned AbbrevID)
skipRecord - Read the current record and discard it.
void CollectBlockInfoNames()
#define LLVM_DELETED_FUNCTION
virtual bool isValidAddress(uint64_t address) const =0
bool isIgnoringBlockInfoNames()
const BitstreamReader * getBitStreamReader() const
uint32_t Read(unsigned NumBits)
BitstreamCursor(const BitstreamCursor &RHS)
static BitstreamEntry getEndBlock()
static BitstreamEntry getRecord(unsigned AbbrevID)
uint32_t getWord(size_t pos)
void JumpToBit(uint64_t BitNo)
JumpToBit - Reset the stream to the specified bit number.
static BitstreamEntry getError()
BitstreamReader(StreamableMemoryObject *bytes)
BitstreamEntry advance(unsigned Flags=0)
unsigned getAbbrevIDWidth() const
getAbbrevIDWidth - Return the number of bits used to encode an abbrev #.
enum llvm::BitstreamEntry::@27 Kind
bool ReadBlockInfoBlock()