1 //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Bitcode/BitstreamReader.h"
14 //===----------------------------------------------------------------------===//
15 // BitstreamCursor implementation
16 //===----------------------------------------------------------------------===//
18 void BitstreamCursor::freeState() {
19 // Free all the Abbrevs.
22 // Free all the Abbrevs in the block scope.
26 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
27 /// the block, and return true if the block has an error.
28 bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
29 // Save the current block's state on BlockScope.
30 BlockScope.push_back(Block(CurCodeSize));
31 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
33 // Add the abbrevs specific to this block to the CurAbbrevs list.
34 if (const BitstreamReader::BlockInfo *Info =
35 BitStream->getBlockInfo(BlockID)) {
36 CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
40 // Get the codesize of this block.
41 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
42 SkipToFourByteBoundary();
43 unsigned NumWords = Read(bitc::BlockSizeWidth);
44 if (NumWordsP) *NumWordsP = NumWords;
46 // Validate that this block is sane.
47 if (CurCodeSize == 0 || AtEndOfStream())
53 static uint64_t readAbbreviatedField(BitstreamCursor &Cursor,
54 const BitCodeAbbrevOp &Op) {
55 assert(!Op.isLiteral() && "Not to be used with literals!");
57 // Decode the value as we are commanded.
58 switch (Op.getEncoding()) {
59 case BitCodeAbbrevOp::Array:
60 case BitCodeAbbrevOp::Blob:
61 llvm_unreachable("Should not reach here");
62 case BitCodeAbbrevOp::Fixed:
63 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
64 return Cursor.Read((unsigned)Op.getEncodingData());
65 case BitCodeAbbrevOp::VBR:
66 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
67 return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
68 case BitCodeAbbrevOp::Char6:
69 return BitCodeAbbrevOp::DecodeChar6(Cursor.Read(6));
71 llvm_unreachable("invalid abbreviation encoding");
74 static void skipAbbreviatedField(BitstreamCursor &Cursor,
75 const BitCodeAbbrevOp &Op) {
76 assert(!Op.isLiteral() && "Not to be used with literals!");
78 // Decode the value as we are commanded.
79 switch (Op.getEncoding()) {
80 case BitCodeAbbrevOp::Array:
81 case BitCodeAbbrevOp::Blob:
82 llvm_unreachable("Should not reach here");
83 case BitCodeAbbrevOp::Fixed:
84 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
85 Cursor.Read((unsigned)Op.getEncodingData());
87 case BitCodeAbbrevOp::VBR:
88 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
89 Cursor.ReadVBR64((unsigned)Op.getEncodingData());
91 case BitCodeAbbrevOp::Char6:
99 /// skipRecord - Read the current record and discard it.
100 void BitstreamCursor::skipRecord(unsigned AbbrevID) {
101 // Skip unabbreviated records by reading past their entries.
102 if (AbbrevID == bitc::UNABBREV_RECORD) {
103 unsigned Code = ReadVBR(6);
105 unsigned NumElts = ReadVBR(6);
106 for (unsigned i = 0; i != NumElts; ++i)
111 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
113 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
114 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
118 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
119 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
120 skipAbbreviatedField(*this, Op);
124 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
125 // Array case. Read the number of elements as a vbr6.
126 unsigned NumElts = ReadVBR(6);
128 // Get the element encoding.
129 assert(i+2 == e && "array op not second to last?");
130 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
132 // Read all the elements.
133 for (; NumElts; --NumElts)
134 skipAbbreviatedField(*this, EltEnc);
138 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
139 // Blob case. Read the number of bytes as a vbr6.
140 unsigned NumElts = ReadVBR(6);
141 SkipToFourByteBoundary(); // 32-bit alignment
143 // Figure out where the end of this blob will be including tail padding.
144 size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
146 // If this would read off the end of the bitcode file, just set the
147 // record to empty and return.
148 if (!canSkipToPos(NewEnd/8)) {
149 NextChar = BitStream->getBitcodeBytes().getExtent();
153 // Skip over the blob.
158 unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
159 SmallVectorImpl<uint64_t> &Vals,
161 if (AbbrevID == bitc::UNABBREV_RECORD) {
162 unsigned Code = ReadVBR(6);
163 unsigned NumElts = ReadVBR(6);
164 for (unsigned i = 0; i != NumElts; ++i)
165 Vals.push_back(ReadVBR64(6));
169 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
171 // Read the record code first.
172 assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
173 const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
175 if (CodeOp.isLiteral())
176 Code = CodeOp.getLiteralValue();
178 if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
179 CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
180 report_fatal_error("Abbreviation starts with an Array or a Blob");
181 Code = readAbbreviatedField(*this, CodeOp);
184 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
185 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
186 if (Op.isLiteral()) {
187 Vals.push_back(Op.getLiteralValue());
191 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
192 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
193 Vals.push_back(readAbbreviatedField(*this, Op));
197 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
198 // Array case. Read the number of elements as a vbr6.
199 unsigned NumElts = ReadVBR(6);
201 // Get the element encoding.
203 report_fatal_error("Array op not second to last");
204 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
205 if (EltEnc.getEncoding() == BitCodeAbbrevOp::Array ||
206 EltEnc.getEncoding() == BitCodeAbbrevOp::Blob)
207 report_fatal_error("Array element type can't be an Array or a Blob");
209 // Read all the elements.
210 for (; NumElts; --NumElts)
211 Vals.push_back(readAbbreviatedField(*this, EltEnc));
215 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
216 // Blob case. Read the number of bytes as a vbr6.
217 unsigned NumElts = ReadVBR(6);
218 SkipToFourByteBoundary(); // 32-bit alignment
220 // Figure out where the end of this blob will be including tail padding.
221 size_t CurBitPos = GetCurrentBitNo();
222 size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
224 // If this would read off the end of the bitcode file, just set the
225 // record to empty and return.
226 if (!canSkipToPos(NewEnd/8)) {
227 Vals.append(NumElts, 0);
228 NextChar = BitStream->getBitcodeBytes().getExtent();
232 // Otherwise, inform the streamer that we need these bytes in memory.
233 const char *Ptr = (const char*)
234 BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
236 // If we can return a reference to the data, do so to avoid copying it.
238 *Blob = StringRef(Ptr, NumElts);
240 // Otherwise, unpack into Vals with zero extension.
241 for (; NumElts; --NumElts)
242 Vals.push_back((unsigned char)*Ptr++);
244 // Skip over tail padding.
252 void BitstreamCursor::ReadAbbrevRecord() {
253 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
254 unsigned NumOpInfo = ReadVBR(5);
255 for (unsigned i = 0; i != NumOpInfo; ++i) {
256 bool IsLiteral = Read(1);
258 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
262 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
263 if (BitCodeAbbrevOp::hasEncodingData(E)) {
264 uint64_t Data = ReadVBR64(5);
266 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
267 // and vbr(0) as a literal zero. This is decoded the same way, and avoids
268 // a slow path in Read() to have to handle reading zero bits.
269 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
271 Abbv->Add(BitCodeAbbrevOp(0));
275 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
278 "Fixed or VBR abbrev record with size > MaxChunkData");
280 Abbv->Add(BitCodeAbbrevOp(E, Data));
282 Abbv->Add(BitCodeAbbrevOp(E));
284 CurAbbrevs.push_back(Abbv);
287 bool BitstreamCursor::ReadBlockInfoBlock() {
288 // If this is the second stream to get to the block info block, skip it.
289 if (BitStream->hasBlockInfoRecords())
292 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
294 SmallVector<uint64_t, 64> Record;
295 BitstreamReader::BlockInfo *CurBlockInfo = nullptr;
297 // Read all the records for this module.
299 BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
301 switch (Entry.Kind) {
302 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
303 case llvm::BitstreamEntry::Error:
305 case llvm::BitstreamEntry::EndBlock:
307 case llvm::BitstreamEntry::Record:
308 // The interesting case.
312 // Read abbrev records, associate them with CurBID.
313 if (Entry.ID == bitc::DEFINE_ABBREV) {
314 if (!CurBlockInfo) return true;
317 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
318 // appropriate BlockInfo.
319 CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
320 CurAbbrevs.pop_back();
326 switch (readRecord(Entry.ID, Record)) {
327 default: break; // Default behavior, ignore unknown content.
328 case bitc::BLOCKINFO_CODE_SETBID:
329 if (Record.size() < 1) return true;
330 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
332 case bitc::BLOCKINFO_CODE_BLOCKNAME: {
333 if (!CurBlockInfo) return true;
334 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
336 for (unsigned i = 0, e = Record.size(); i != e; ++i)
337 Name += (char)Record[i];
338 CurBlockInfo->Name = Name;
341 case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
342 if (!CurBlockInfo) return true;
343 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
345 for (unsigned i = 1, e = Record.size(); i != e; ++i)
346 Name += (char)Record[i];
347 CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],