2007-04-22 06:22:05 +00:00
|
|
|
//===- BitstreamReader.h - Low-level bitstream reader interface -*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-04-22 06:22:05 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This header defines the BitstreamReader class. This class can be used to
|
|
|
|
// read an arbitrary bitstream, regardless of its contents.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#ifndef LLVM_BITCODE_BITSTREAMREADER_H
|
|
|
|
#define LLVM_BITCODE_BITSTREAMREADER_H
|
2007-04-22 06:22:05 +00:00
|
|
|
|
|
|
|
#include "llvm/Bitcode/BitCodes.h"
|
2012-02-07 10:53:19 +00:00
|
|
|
#include "llvm/Support/Endian.h"
|
2014-11-12 03:55:46 +00:00
|
|
|
#include "llvm/Support/StreamingMemoryObject.h"
|
2009-04-01 18:45:54 +00:00
|
|
|
#include <climits>
|
2009-08-27 06:41:46 +00:00
|
|
|
#include <string>
|
2007-04-29 08:05:07 +00:00
|
|
|
#include <vector>
|
2007-04-22 06:22:05 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
class Deserializer;
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// This class is used to read from an LLVM bitcode stream, maintaining
|
|
|
|
/// information that is global to decoding the entire file. While a file is
|
|
|
|
/// being read, multiple cursors can be independently advanced or skipped around
|
|
|
|
/// within the file. These are represented by the BitstreamCursor class.
|
2007-04-22 06:22:05 +00:00
|
|
|
class BitstreamReader {
|
2009-04-26 20:59:02 +00:00
|
|
|
public:
|
2014-11-06 22:57:10 +00:00
|
|
|
/// This contains information emitted to BLOCKINFO_BLOCK blocks. These
|
|
|
|
/// describe abbreviations that all blocks of the specified ID inherit.
|
2009-04-26 20:59:02 +00:00
|
|
|
struct BlockInfo {
|
|
|
|
unsigned BlockID;
|
2014-09-15 15:44:14 +00:00
|
|
|
std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> Abbrevs;
|
2009-04-26 22:21:57 +00:00
|
|
|
std::string Name;
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2009-04-26 22:21:57 +00:00
|
|
|
std::vector<std::pair<unsigned, std::string> > RecordNames;
|
2009-04-26 20:59:02 +00:00
|
|
|
};
|
|
|
|
private:
|
2014-11-12 03:55:46 +00:00
|
|
|
std::unique_ptr<MemoryObject> BitcodeBytes;
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2009-04-26 20:59:02 +00:00
|
|
|
std::vector<BlockInfo> BlockInfoRecords;
|
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// This is set to true if we don't care about the block/record name
|
|
|
|
/// information in the BlockInfo block. Only llvm-bcanalyzer uses this.
|
2009-04-27 20:04:08 +00:00
|
|
|
bool IgnoreBlockInfoNames;
|
2012-09-17 07:16:40 +00:00
|
|
|
|
|
|
|
BitstreamReader(const BitstreamReader&) LLVM_DELETED_FUNCTION;
|
|
|
|
void operator=(const BitstreamReader&) LLVM_DELETED_FUNCTION;
|
2009-04-26 20:59:02 +00:00
|
|
|
public:
|
2012-02-06 22:30:29 +00:00
|
|
|
BitstreamReader() : IgnoreBlockInfoNames(true) {
|
2009-04-26 20:59:02 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 17:07:55 +00:00
|
|
|
BitstreamReader(const unsigned char *Start, const unsigned char *End)
|
|
|
|
: IgnoreBlockInfoNames(true) {
|
2009-04-26 20:59:02 +00:00
|
|
|
init(Start, End);
|
|
|
|
}
|
|
|
|
|
2014-11-12 03:55:46 +00:00
|
|
|
BitstreamReader(MemoryObject *bytes) : IgnoreBlockInfoNames(true) {
|
2012-02-06 22:30:29 +00:00
|
|
|
BitcodeBytes.reset(bytes);
|
|
|
|
}
|
|
|
|
|
2014-08-30 17:07:55 +00:00
|
|
|
BitstreamReader(BitstreamReader &&Other) {
|
|
|
|
*this = std::move(Other);
|
|
|
|
}
|
|
|
|
|
|
|
|
BitstreamReader &operator=(BitstreamReader &&Other) {
|
|
|
|
BitcodeBytes = std::move(Other.BitcodeBytes);
|
|
|
|
// Explicitly swap block info, so that nothing gets destroyed twice.
|
|
|
|
std::swap(BlockInfoRecords, Other.BlockInfoRecords);
|
|
|
|
IgnoreBlockInfoNames = Other.IgnoreBlockInfoNames;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-04-26 20:59:02 +00:00
|
|
|
void init(const unsigned char *Start, const unsigned char *End) {
|
|
|
|
assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes");
|
2012-02-06 22:30:29 +00:00
|
|
|
BitcodeBytes.reset(getNonStreamedMemoryObject(Start, End));
|
2009-04-26 20:59:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 03:55:46 +00:00
|
|
|
MemoryObject &getBitcodeBytes() { return *BitcodeBytes; }
|
2012-02-06 22:30:29 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// This is called by clients that want block/record name information.
|
2009-04-27 20:04:08 +00:00
|
|
|
void CollectBlockInfoNames() { IgnoreBlockInfoNames = false; }
|
|
|
|
bool isIgnoringBlockInfoNames() { return IgnoreBlockInfoNames; }
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2009-04-26 20:59:02 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Block Manipulation
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// Return true if we've already read and processed the block info block for
|
|
|
|
/// this Bitstream. We only process it for the first cursor that walks over
|
|
|
|
/// it.
|
2009-04-26 21:07:02 +00:00
|
|
|
bool hasBlockInfoRecords() const { return !BlockInfoRecords.empty(); }
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// If there is block info for the specified ID, return it, otherwise return
|
|
|
|
/// null.
|
2009-04-26 22:21:57 +00:00
|
|
|
const BlockInfo *getBlockInfo(unsigned BlockID) const {
|
2009-04-26 20:59:02 +00:00
|
|
|
// Common case, the most recent entry matches BlockID.
|
|
|
|
if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
|
|
|
|
return &BlockInfoRecords.back();
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
|
|
|
|
i != e; ++i)
|
|
|
|
if (BlockInfoRecords[i].BlockID == BlockID)
|
|
|
|
return &BlockInfoRecords[i];
|
2014-04-15 06:32:26 +00:00
|
|
|
return nullptr;
|
2009-04-26 20:59:02 +00:00
|
|
|
}
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2009-04-26 20:59:02 +00:00
|
|
|
BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
|
2009-04-26 22:21:57 +00:00
|
|
|
if (const BlockInfo *BI = getBlockInfo(BlockID))
|
|
|
|
return *const_cast<BlockInfo*>(BI);
|
2009-04-26 20:59:02 +00:00
|
|
|
|
|
|
|
// Otherwise, add a new record.
|
|
|
|
BlockInfoRecords.push_back(BlockInfo());
|
|
|
|
BlockInfoRecords.back().BlockID = BlockID;
|
|
|
|
return BlockInfoRecords.back();
|
|
|
|
}
|
2014-08-30 17:07:55 +00:00
|
|
|
|
|
|
|
/// Takes block info from the other bitstream reader.
|
|
|
|
///
|
|
|
|
/// This is a "take" operation because BlockInfo records are non-trivial, and
|
|
|
|
/// indeed rather expensive.
|
|
|
|
void takeBlockInfo(BitstreamReader &&Other) {
|
|
|
|
assert(!hasBlockInfoRecords());
|
|
|
|
BlockInfoRecords = std::move(Other.BlockInfoRecords);
|
|
|
|
}
|
2013-01-19 21:35:24 +00:00
|
|
|
};
|
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// When advancing through a bitstream cursor, each advance can discover a few
|
|
|
|
/// different kinds of entries:
|
2013-01-19 21:35:24 +00:00
|
|
|
struct BitstreamEntry {
|
|
|
|
enum {
|
2014-11-06 22:57:10 +00:00
|
|
|
Error, // Malformed bitcode was found.
|
|
|
|
EndBlock, // We've reached the end of the current block, (or the end of the
|
|
|
|
// file, which is treated like a series of EndBlock records.
|
|
|
|
SubBlock, // This is the start of a new subblock of a specific ID.
|
|
|
|
Record // This is a record with a specific AbbrevID.
|
2013-01-19 21:35:24 +00:00
|
|
|
} Kind;
|
2013-04-01 02:28:07 +00:00
|
|
|
|
2013-01-19 21:35:24 +00:00
|
|
|
unsigned ID;
|
2009-04-26 20:59:02 +00:00
|
|
|
|
2013-01-19 21:35:24 +00:00
|
|
|
static BitstreamEntry getError() {
|
|
|
|
BitstreamEntry E; E.Kind = Error; return E;
|
|
|
|
}
|
|
|
|
static BitstreamEntry getEndBlock() {
|
|
|
|
BitstreamEntry E; E.Kind = EndBlock; return E;
|
|
|
|
}
|
|
|
|
static BitstreamEntry getSubBlock(unsigned ID) {
|
|
|
|
BitstreamEntry E; E.Kind = SubBlock; E.ID = ID; return E;
|
|
|
|
}
|
|
|
|
static BitstreamEntry getRecord(unsigned AbbrevID) {
|
|
|
|
BitstreamEntry E; E.Kind = Record; E.ID = AbbrevID; return E;
|
|
|
|
}
|
2009-04-26 20:59:02 +00:00
|
|
|
};
|
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// This represents a position within a bitcode file. There may be multiple
|
|
|
|
/// independent cursors reading within one bitstream, each maintaining their own
|
|
|
|
/// local state.
|
2013-01-19 21:35:24 +00:00
|
|
|
///
|
|
|
|
/// Unlike iterators, BitstreamCursors are heavy-weight objects that should not
|
|
|
|
/// be passed by value.
|
2009-04-26 20:59:02 +00:00
|
|
|
class BitstreamCursor {
|
|
|
|
friend class Deserializer;
|
|
|
|
BitstreamReader *BitStream;
|
2012-02-06 22:30:29 +00:00
|
|
|
size_t NextChar;
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2014-11-12 18:37:00 +00:00
|
|
|
// The size of the bicode. 0 if we don't know it yet.
|
|
|
|
size_t Size;
|
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// This is the current data we have pulled from the stream but have not
|
|
|
|
/// returned to the client. This is specifically and intentionally defined to
|
|
|
|
/// follow the word size of the host machine for efficiency. We use word_t in
|
|
|
|
/// places that are aware of this to make it perfectly explicit what is going
|
|
|
|
/// on.
|
2014-11-13 07:23:22 +00:00
|
|
|
typedef size_t word_t;
|
2013-02-09 06:52:14 +00:00
|
|
|
word_t CurWord;
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// This is the number of bits in CurWord that are valid. This is always from
|
2014-11-13 07:23:22 +00:00
|
|
|
/// [0...bits_of(size_t)-1] inclusive.
|
2007-04-22 06:22:05 +00:00
|
|
|
unsigned BitsInCurWord;
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
// This is the declared size of code values used for the current block, in
|
|
|
|
// bits.
|
2007-04-22 06:22:05 +00:00
|
|
|
unsigned CurCodeSize;
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// Abbrevs installed at in this block.
|
2014-09-15 15:44:14 +00:00
|
|
|
std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> CurAbbrevs;
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2007-04-23 18:57:58 +00:00
|
|
|
struct Block {
|
|
|
|
unsigned PrevCodeSize;
|
2014-09-15 15:44:14 +00:00
|
|
|
std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> PrevAbbrevs;
|
2007-04-23 18:57:58 +00:00
|
|
|
explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
|
|
|
|
};
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// This tracks the codesize of parent blocks.
|
2007-04-23 18:57:58 +00:00
|
|
|
SmallVector<Block, 8> BlockScope;
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2013-04-01 02:28:07 +00:00
|
|
|
|
2007-04-22 06:22:05 +00:00
|
|
|
public:
|
2014-11-12 14:48:38 +00:00
|
|
|
BitstreamCursor() { init(nullptr); }
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2014-11-12 14:48:38 +00:00
|
|
|
explicit BitstreamCursor(BitstreamReader &R) { init(&R); }
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2014-11-12 14:48:38 +00:00
|
|
|
void init(BitstreamReader *R) {
|
2009-04-26 20:59:02 +00:00
|
|
|
freeState();
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2014-11-12 14:48:38 +00:00
|
|
|
BitStream = R;
|
2012-02-06 22:30:29 +00:00
|
|
|
NextChar = 0;
|
2014-11-12 18:37:00 +00:00
|
|
|
Size = 0;
|
2007-04-22 06:22:05 +00:00
|
|
|
BitsInCurWord = 0;
|
|
|
|
CurCodeSize = 2;
|
|
|
|
}
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2013-01-19 18:19:39 +00:00
|
|
|
void freeState();
|
2013-04-01 02:28:07 +00:00
|
|
|
|
2012-02-06 22:30:29 +00:00
|
|
|
bool canSkipToPos(size_t pos) const {
|
|
|
|
// pos can be skipped to if it is a valid address or one byte past the end.
|
|
|
|
return pos == 0 || BitStream->getBitcodeBytes().isValidAddress(
|
|
|
|
static_cast<uint64_t>(pos - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AtEndOfStream() {
|
2014-11-12 18:37:00 +00:00
|
|
|
if (BitsInCurWord != 0)
|
|
|
|
return false;
|
2014-11-13 14:37:51 +00:00
|
|
|
if (Size != 0)
|
|
|
|
return Size == NextChar;
|
2014-11-12 18:37:00 +00:00
|
|
|
fillCurWord();
|
|
|
|
return BitsInCurWord == 0;
|
2007-05-06 08:12:09 +00:00
|
|
|
}
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// Return the number of bits used to encode an abbrev #.
|
2013-01-19 21:35:24 +00:00
|
|
|
unsigned getAbbrevIDWidth() const { return CurCodeSize; }
|
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// Return the bit # of the bit we are reading.
|
2007-04-29 19:17:32 +00:00
|
|
|
uint64_t GetCurrentBitNo() const {
|
2012-02-06 22:30:29 +00:00
|
|
|
return NextChar*CHAR_BIT - BitsInCurWord;
|
2007-04-29 19:17:32 +00:00
|
|
|
}
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2009-04-26 22:21:57 +00:00
|
|
|
BitstreamReader *getBitStreamReader() {
|
|
|
|
return BitStream;
|
|
|
|
}
|
|
|
|
const BitstreamReader *getBitStreamReader() const {
|
|
|
|
return BitStream;
|
|
|
|
}
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2013-01-19 23:31:15 +00:00
|
|
|
/// Flags that modify the behavior of advance().
|
|
|
|
enum {
|
2014-11-06 22:57:10 +00:00
|
|
|
/// If this flag is used, the advance() method does not automatically pop
|
|
|
|
/// the block scope when the end of a block is reached.
|
2013-01-20 02:12:39 +00:00
|
|
|
AF_DontPopBlockAtEnd = 1,
|
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// If this flag is used, abbrev entries are returned just like normal
|
|
|
|
/// records.
|
2013-01-20 02:12:39 +00:00
|
|
|
AF_DontAutoprocessAbbrevs = 2
|
2013-01-19 23:31:15 +00:00
|
|
|
};
|
2013-04-01 02:28:07 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// Advance the current bitstream, returning the next entry in the stream.
|
|
|
|
BitstreamEntry advance(unsigned Flags = 0) {
|
2013-01-19 21:35:24 +00:00
|
|
|
while (1) {
|
|
|
|
unsigned Code = ReadCode();
|
|
|
|
if (Code == bitc::END_BLOCK) {
|
2013-01-19 23:31:15 +00:00
|
|
|
// Pop the end of the block unless Flags tells us not to.
|
|
|
|
if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd())
|
2013-01-19 21:35:24 +00:00
|
|
|
return BitstreamEntry::getError();
|
|
|
|
return BitstreamEntry::getEndBlock();
|
|
|
|
}
|
2013-04-01 02:28:07 +00:00
|
|
|
|
2013-01-19 21:35:24 +00:00
|
|
|
if (Code == bitc::ENTER_SUBBLOCK)
|
|
|
|
return BitstreamEntry::getSubBlock(ReadSubBlockID());
|
2013-04-01 02:28:07 +00:00
|
|
|
|
2013-01-20 02:12:39 +00:00
|
|
|
if (Code == bitc::DEFINE_ABBREV &&
|
|
|
|
!(Flags & AF_DontAutoprocessAbbrevs)) {
|
2013-01-19 21:35:24 +00:00
|
|
|
// We read and accumulate abbrev's, the client can't do anything with
|
|
|
|
// them anyway.
|
|
|
|
ReadAbbrevRecord();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return BitstreamEntry::getRecord(Code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// This is a convenience function for clients that don't expect any
|
|
|
|
/// subblocks. This just skips over them automatically.
|
2013-01-19 23:31:15 +00:00
|
|
|
BitstreamEntry advanceSkippingSubblocks(unsigned Flags = 0) {
|
2013-01-19 21:35:24 +00:00
|
|
|
while (1) {
|
|
|
|
// If we found a normal entry, return it.
|
2013-01-19 23:31:15 +00:00
|
|
|
BitstreamEntry Entry = advance(Flags);
|
2013-01-19 21:35:24 +00:00
|
|
|
if (Entry.Kind != BitstreamEntry::SubBlock)
|
|
|
|
return Entry;
|
2013-04-01 02:28:07 +00:00
|
|
|
|
2013-01-19 21:35:24 +00:00
|
|
|
// If we found a sub-block, just skip over it and check the next entry.
|
|
|
|
if (SkipBlock())
|
|
|
|
return BitstreamEntry::getError();
|
|
|
|
}
|
|
|
|
}
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// Reset the stream to the specified bit number.
|
2007-05-01 04:59:06 +00:00
|
|
|
void JumpToBit(uint64_t BitNo) {
|
2013-02-09 06:52:14 +00:00
|
|
|
uintptr_t ByteNo = uintptr_t(BitNo/8) & ~(sizeof(word_t)-1);
|
|
|
|
unsigned WordBitNo = unsigned(BitNo & (sizeof(word_t)*8-1));
|
2012-02-06 22:30:29 +00:00
|
|
|
assert(canSkipToPos(ByteNo) && "Invalid location");
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2007-05-01 04:59:06 +00:00
|
|
|
// Move the cursor to the right word.
|
2012-02-06 22:30:29 +00:00
|
|
|
NextChar = ByteNo;
|
2007-05-01 04:59:06 +00:00
|
|
|
BitsInCurWord = 0;
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2007-05-01 04:59:06 +00:00
|
|
|
// Skip over any bits that are already consumed.
|
2014-11-13 18:44:53 +00:00
|
|
|
if (WordBitNo)
|
|
|
|
Read(WordBitNo);
|
2007-05-01 04:59:06 +00:00
|
|
|
}
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2014-11-12 18:37:00 +00:00
|
|
|
void fillCurWord() {
|
|
|
|
assert(Size == 0 || NextChar < (unsigned)Size);
|
|
|
|
|
|
|
|
// Read the next word from the stream.
|
|
|
|
uint8_t Array[sizeof(word_t)] = {0};
|
|
|
|
|
|
|
|
uint64_t BytesRead =
|
|
|
|
BitStream->getBitcodeBytes().readBytes(Array, sizeof(Array), NextChar);
|
|
|
|
|
|
|
|
// If we run out of data, stop at the end of the stream.
|
|
|
|
if (BytesRead == 0) {
|
|
|
|
Size = NextChar;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-13 14:45:22 +00:00
|
|
|
CurWord =
|
|
|
|
support::endian::read<word_t, support::little, support::unaligned>(
|
|
|
|
Array);
|
2014-11-13 07:23:22 +00:00
|
|
|
NextChar += BytesRead;
|
|
|
|
BitsInCurWord = BytesRead * 8;
|
2014-11-12 18:37:00 +00:00
|
|
|
}
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2014-11-13 18:44:53 +00:00
|
|
|
word_t Read(unsigned NumBits) {
|
2014-11-17 19:26:40 +00:00
|
|
|
static const unsigned BitsInWord = sizeof(word_t) * 8;
|
|
|
|
|
|
|
|
assert(NumBits && NumBits <= BitsInWord &&
|
2014-11-13 18:44:53 +00:00
|
|
|
"Cannot return zero or more than BitsInWord bits!");
|
2013-04-01 02:28:07 +00:00
|
|
|
|
2014-11-13 07:23:22 +00:00
|
|
|
static const unsigned Mask = sizeof(word_t) > 4 ? 0x3f : 0x1f;
|
|
|
|
|
2007-04-22 06:22:05 +00:00
|
|
|
// If the field is fully contained by CurWord, return it quickly.
|
|
|
|
if (BitsInCurWord >= NumBits) {
|
2014-11-17 19:26:40 +00:00
|
|
|
word_t R = CurWord & (~word_t(0) >> (BitsInWord - NumBits));
|
2014-11-12 18:37:00 +00:00
|
|
|
|
|
|
|
// Use a mask to avoid undefined behavior.
|
2014-11-13 07:23:22 +00:00
|
|
|
CurWord >>= (NumBits & Mask);
|
2014-11-12 18:37:00 +00:00
|
|
|
|
2007-04-22 06:22:05 +00:00
|
|
|
BitsInCurWord -= NumBits;
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2014-11-13 18:44:53 +00:00
|
|
|
word_t R = BitsInCurWord ? CurWord : 0;
|
2014-11-12 18:37:00 +00:00
|
|
|
unsigned BitsLeft = NumBits - BitsInCurWord;
|
2013-04-01 02:28:07 +00:00
|
|
|
|
2014-11-12 18:37:00 +00:00
|
|
|
fillCurWord();
|
2013-04-01 02:28:07 +00:00
|
|
|
|
2014-11-12 18:37:00 +00:00
|
|
|
// If we run out of data, stop at the end of the stream.
|
|
|
|
if (BitsLeft > BitsInCurWord)
|
|
|
|
return 0;
|
2013-04-01 02:28:07 +00:00
|
|
|
|
2014-11-17 19:26:40 +00:00
|
|
|
word_t R2 = CurWord & (~word_t(0) >> (BitsInWord - BitsLeft));
|
2013-02-09 06:52:14 +00:00
|
|
|
|
2014-11-12 18:37:00 +00:00
|
|
|
// Use a mask to avoid undefined behavior.
|
2014-11-13 07:23:22 +00:00
|
|
|
CurWord >>= (BitsLeft & Mask);
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2014-11-12 18:37:00 +00:00
|
|
|
BitsInCurWord -= BitsLeft;
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2014-11-13 18:44:53 +00:00
|
|
|
R |= R2 << (NumBits - BitsLeft);
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2007-04-22 06:22:05 +00:00
|
|
|
return R;
|
|
|
|
}
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2007-04-22 06:22:05 +00:00
|
|
|
uint32_t ReadVBR(unsigned NumBits) {
|
|
|
|
uint32_t Piece = Read(NumBits);
|
2007-04-22 15:00:52 +00:00
|
|
|
if ((Piece & (1U << (NumBits-1))) == 0)
|
2007-04-22 06:22:05 +00:00
|
|
|
return Piece;
|
|
|
|
|
|
|
|
uint32_t Result = 0;
|
|
|
|
unsigned NextBit = 0;
|
|
|
|
while (1) {
|
|
|
|
Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
|
|
|
|
|
2007-04-22 15:00:52 +00:00
|
|
|
if ((Piece & (1U << (NumBits-1))) == 0)
|
2007-04-22 06:22:05 +00:00
|
|
|
return Result;
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2007-04-22 06:22:05 +00:00
|
|
|
NextBit += NumBits-1;
|
|
|
|
Piece = Read(NumBits);
|
|
|
|
}
|
|
|
|
}
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
// Read a VBR that may have a value up to 64-bits in size. The chunk size of
|
|
|
|
// the VBR must still be <= 32 bits though.
|
2007-04-22 06:22:05 +00:00
|
|
|
uint64_t ReadVBR64(unsigned NumBits) {
|
2009-07-07 18:39:49 +00:00
|
|
|
uint32_t Piece = Read(NumBits);
|
|
|
|
if ((Piece & (1U << (NumBits-1))) == 0)
|
|
|
|
return uint64_t(Piece);
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2007-04-22 06:22:05 +00:00
|
|
|
uint64_t Result = 0;
|
|
|
|
unsigned NextBit = 0;
|
|
|
|
while (1) {
|
2009-07-07 18:39:49 +00:00
|
|
|
Result |= uint64_t(Piece & ((1U << (NumBits-1))-1)) << NextBit;
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2009-07-07 18:39:49 +00:00
|
|
|
if ((Piece & (1U << (NumBits-1))) == 0)
|
2007-04-22 06:22:05 +00:00
|
|
|
return Result;
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2007-04-22 06:22:05 +00:00
|
|
|
NextBit += NumBits-1;
|
|
|
|
Piece = Read(NumBits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 06:52:14 +00:00
|
|
|
private:
|
2013-01-21 18:04:19 +00:00
|
|
|
void SkipToFourByteBoundary() {
|
2013-02-09 06:52:14 +00:00
|
|
|
// If word_t is 64-bits and if we've read less than 32 bits, just dump
|
|
|
|
// the bits we have up to the next 32-bit boundary.
|
|
|
|
if (sizeof(word_t) > 4 &&
|
2013-02-09 07:37:59 +00:00
|
|
|
BitsInCurWord >= 32) {
|
2013-02-09 06:52:14 +00:00
|
|
|
CurWord >>= BitsInCurWord-32;
|
|
|
|
BitsInCurWord = 32;
|
|
|
|
return;
|
|
|
|
}
|
2013-04-01 02:28:07 +00:00
|
|
|
|
2007-04-22 06:22:05 +00:00
|
|
|
BitsInCurWord = 0;
|
|
|
|
}
|
2013-02-09 06:52:14 +00:00
|
|
|
public:
|
2007-04-22 06:22:05 +00:00
|
|
|
|
|
|
|
unsigned ReadCode() {
|
|
|
|
return Read(CurCodeSize);
|
|
|
|
}
|
|
|
|
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2007-04-22 06:22:05 +00:00
|
|
|
// Block header:
|
|
|
|
// [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
|
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// Having read the ENTER_SUBBLOCK code, read the BlockID for the block.
|
2007-04-22 06:22:05 +00:00
|
|
|
unsigned ReadSubBlockID() {
|
|
|
|
return ReadVBR(bitc::BlockIDWidth);
|
|
|
|
}
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body
|
|
|
|
/// of this block. If the block record is malformed, return true.
|
2007-04-22 06:22:05 +00:00
|
|
|
bool SkipBlock() {
|
|
|
|
// Read and ignore the codelen value. Since we are skipping this block, we
|
|
|
|
// don't care what code widths are used inside of it.
|
|
|
|
ReadVBR(bitc::CodeLenWidth);
|
2013-01-21 18:04:19 +00:00
|
|
|
SkipToFourByteBoundary();
|
2013-02-09 06:52:14 +00:00
|
|
|
unsigned NumFourBytes = Read(bitc::BlockSizeWidth);
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2007-04-22 06:22:05 +00:00
|
|
|
// Check that the block wasn't partially defined, and that the offset isn't
|
|
|
|
// bogus.
|
2013-02-09 06:52:14 +00:00
|
|
|
size_t SkipTo = GetCurrentBitNo() + NumFourBytes*4*8;
|
|
|
|
if (AtEndOfStream() || !canSkipToPos(SkipTo/8))
|
2007-04-22 06:22:05 +00:00
|
|
|
return true;
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2013-02-09 06:52:14 +00:00
|
|
|
JumpToBit(SkipTo);
|
2007-04-22 06:22:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// Having read the ENTER_SUBBLOCK abbrevid, enter the block, and return true
|
|
|
|
/// if the block has an error.
|
2014-04-15 06:32:26 +00:00
|
|
|
bool EnterSubBlock(unsigned BlockID, unsigned *NumWordsP = nullptr);
|
2013-04-01 02:28:07 +00:00
|
|
|
|
2007-04-22 06:22:05 +00:00
|
|
|
bool ReadBlockEnd() {
|
|
|
|
if (BlockScope.empty()) return true;
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2007-04-22 06:22:05 +00:00
|
|
|
// Block tail:
|
|
|
|
// [END_BLOCK, <align4bytes>]
|
2013-01-21 18:04:19 +00:00
|
|
|
SkipToFourByteBoundary();
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2013-01-19 21:35:24 +00:00
|
|
|
popBlockScope();
|
2007-11-10 02:00:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2007-11-10 02:00:38 +00:00
|
|
|
private:
|
2013-01-19 21:35:24 +00:00
|
|
|
|
|
|
|
void popBlockScope() {
|
2007-04-23 18:57:58 +00:00
|
|
|
CurCodeSize = BlockScope.back().PrevCodeSize;
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2014-09-15 15:44:14 +00:00
|
|
|
CurAbbrevs = std::move(BlockScope.back().PrevAbbrevs);
|
2007-04-22 06:22:05 +00:00
|
|
|
BlockScope.pop_back();
|
2009-02-20 23:04:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-20 02:12:39 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
2007-04-22 06:22:05 +00:00
|
|
|
// Record Processing
|
|
|
|
//===--------------------------------------------------------------------===//
|
2009-02-20 23:04:06 +00:00
|
|
|
|
2007-05-04 20:33:47 +00:00
|
|
|
public:
|
2009-04-06 22:43:46 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// Return the abbreviation for the specified AbbrevId.
|
2009-04-06 22:43:46 +00:00
|
|
|
const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
|
|
|
|
unsigned AbbrevNo = AbbrevID-bitc::FIRST_APPLICATION_ABBREV;
|
|
|
|
assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
|
2014-09-15 15:44:14 +00:00
|
|
|
return CurAbbrevs[AbbrevNo].get();
|
2009-04-06 22:43:46 +00:00
|
|
|
}
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2014-11-06 22:57:10 +00:00
|
|
|
/// Read the current record and discard it.
|
2013-01-20 00:00:00 +00:00
|
|
|
void skipRecord(unsigned AbbrevID);
|
2013-04-01 02:28:07 +00:00
|
|
|
|
2013-01-20 01:06:48 +00:00
|
|
|
unsigned readRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals,
|
2014-04-15 06:32:26 +00:00
|
|
|
StringRef *Blob = nullptr);
|
2013-01-20 01:06:48 +00:00
|
|
|
|
2007-04-23 18:57:58 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Abbrev Processing
|
|
|
|
//===--------------------------------------------------------------------===//
|
2013-01-19 18:19:39 +00:00
|
|
|
void ReadAbbrevRecord();
|
2013-04-01 02:28:07 +00:00
|
|
|
|
2013-01-19 18:19:39 +00:00
|
|
|
bool ReadBlockInfoBlock();
|
2007-04-22 06:22:05 +00:00
|
|
|
};
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2007-04-22 06:22:05 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|