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 // We can't read more than MaxChunkSize at a time
43 if (CurCodeSize > MaxChunkSize)
46 SkipToFourByteBoundary();
47 unsigned NumWords = Read(bitc::BlockSizeWidth);
48 if (NumWordsP) *NumWordsP = NumWords;
50 // Validate that this block is sane.
51 if (CurCodeSize == 0 || AtEndOfStream())
57 static uint64_t readAbbreviatedField(BitstreamCursor &Cursor,
58 const BitCodeAbbrevOp &Op) {
59 assert(!Op.isLiteral() && "Not to be used with literals!");
61 // Decode the value as we are commanded.
62 switch (Op.getEncoding()) {
63 case BitCodeAbbrevOp::Array:
64 case BitCodeAbbrevOp::Blob:
65 llvm_unreachable("Should not reach here");
66 case BitCodeAbbrevOp::Fixed:
67 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
68 return Cursor.Read((unsigned)Op.getEncodingData());
69 case BitCodeAbbrevOp::VBR:
70 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
71 return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
72 case BitCodeAbbrevOp::Char6:
73 return BitCodeAbbrevOp::DecodeChar6(Cursor.Read(6));
75 llvm_unreachable("invalid abbreviation encoding");
78 static void skipAbbreviatedField(BitstreamCursor &Cursor,
79 const BitCodeAbbrevOp &Op) {
80 assert(!Op.isLiteral() && "Not to be used with literals!");
82 // Decode the value as we are commanded.
83 switch (Op.getEncoding()) {
84 case BitCodeAbbrevOp::Array:
85 case BitCodeAbbrevOp::Blob:
86 llvm_unreachable("Should not reach here");
87 case BitCodeAbbrevOp::Fixed:
88 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
89 Cursor.Read((unsigned)Op.getEncodingData());
91 case BitCodeAbbrevOp::VBR:
92 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
93 Cursor.ReadVBR64((unsigned)Op.getEncodingData());
95 case BitCodeAbbrevOp::Char6:
103 /// skipRecord - Read the current record and discard it.
104 void BitstreamCursor::skipRecord(unsigned AbbrevID) {
105 // Skip unabbreviated records by reading past their entries.
106 if (AbbrevID == bitc::UNABBREV_RECORD) {
107 unsigned Code = ReadVBR(6);
109 unsigned NumElts = ReadVBR(6);
110 for (unsigned i = 0; i != NumElts; ++i)
115 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
117 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
118 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
122 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
123 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
124 skipAbbreviatedField(*this, Op);
128 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
129 // Array case. Read the number of elements as a vbr6.
130 unsigned NumElts = ReadVBR(6);
132 // Get the element encoding.
133 assert(i+2 == e && "array op not second to last?");
134 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
136 // Read all the elements.
137 for (; NumElts; --NumElts)
138 skipAbbreviatedField(*this, EltEnc);
142 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
143 // Blob case. Read the number of bytes as a vbr6.
144 unsigned NumElts = ReadVBR(6);
145 SkipToFourByteBoundary(); // 32-bit alignment
147 // Figure out where the end of this blob will be including tail padding.
148 size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
150 // If this would read off the end of the bitcode file, just set the
151 // record to empty and return.
152 if (!canSkipToPos(NewEnd/8)) {
153 NextChar = BitStream->getBitcodeBytes().getExtent();
157 // Skip over the blob.
162 unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
163 SmallVectorImpl<uint64_t> &Vals,
165 if (AbbrevID == bitc::UNABBREV_RECORD) {
166 unsigned Code = ReadVBR(6);
167 unsigned NumElts = ReadVBR(6);
168 for (unsigned i = 0; i != NumElts; ++i)
169 Vals.push_back(ReadVBR64(6));
173 const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
175 // Read the record code first.
176 assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
177 const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
179 if (CodeOp.isLiteral())
180 Code = CodeOp.getLiteralValue();
182 if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
183 CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
184 report_fatal_error("Abbreviation starts with an Array or a Blob");
185 Code = readAbbreviatedField(*this, CodeOp);
188 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
189 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
190 if (Op.isLiteral()) {
191 Vals.push_back(Op.getLiteralValue());
195 if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
196 Op.getEncoding() != BitCodeAbbrevOp::Blob) {
197 Vals.push_back(readAbbreviatedField(*this, Op));
201 if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
202 // Array case. Read the number of elements as a vbr6.
203 unsigned NumElts = ReadVBR(6);
205 // Get the element encoding.
207 report_fatal_error("Array op not second to last");
208 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
209 if (EltEnc.getEncoding() == BitCodeAbbrevOp::Array ||
210 EltEnc.getEncoding() == BitCodeAbbrevOp::Blob)
211 report_fatal_error("Array element type can't be an Array or a Blob");
213 // Read all the elements.
214 for (; NumElts; --NumElts)
215 Vals.push_back(readAbbreviatedField(*this, EltEnc));
219 assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
220 // Blob case. Read the number of bytes as a vbr6.
221 unsigned NumElts = ReadVBR(6);
222 SkipToFourByteBoundary(); // 32-bit alignment
224 // Figure out where the end of this blob will be including tail padding.
225 size_t CurBitPos = GetCurrentBitNo();
226 size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
228 // If this would read off the end of the bitcode file, just set the
229 // record to empty and return.
230 if (!canSkipToPos(NewEnd/8)) {
231 Vals.append(NumElts, 0);
232 NextChar = BitStream->getBitcodeBytes().getExtent();
236 // Otherwise, inform the streamer that we need these bytes in memory.
237 const char *Ptr = (const char*)
238 BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
240 // If we can return a reference to the data, do so to avoid copying it.
242 *Blob = StringRef(Ptr, NumElts);
244 // Otherwise, unpack into Vals with zero extension.
245 for (; NumElts; --NumElts)
246 Vals.push_back((unsigned char)*Ptr++);
248 // Skip over tail padding.
256 void BitstreamCursor::ReadAbbrevRecord() {
257 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
258 unsigned NumOpInfo = ReadVBR(5);
259 for (unsigned i = 0; i != NumOpInfo; ++i) {
260 bool IsLiteral = Read(1);
262 Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
266 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
267 if (BitCodeAbbrevOp::hasEncodingData(E)) {
268 uint64_t Data = ReadVBR64(5);
270 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
271 // and vbr(0) as a literal zero. This is decoded the same way, and avoids
272 // a slow path in Read() to have to handle reading zero bits.
273 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
275 Abbv->Add(BitCodeAbbrevOp(0));
279 if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
282 "Fixed or VBR abbrev record with size > MaxChunkData");
284 Abbv->Add(BitCodeAbbrevOp(E, Data));
286 Abbv->Add(BitCodeAbbrevOp(E));
288 CurAbbrevs.push_back(Abbv);
291 bool BitstreamCursor::ReadBlockInfoBlock() {
292 // If this is the second stream to get to the block info block, skip it.
293 if (BitStream->hasBlockInfoRecords())
296 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
298 SmallVector<uint64_t, 64> Record;
299 BitstreamReader::BlockInfo *CurBlockInfo = nullptr;
301 // Read all the records for this module.
303 BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
305 switch (Entry.Kind) {
306 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
307 case llvm::BitstreamEntry::Error:
309 case llvm::BitstreamEntry::EndBlock:
311 case llvm::BitstreamEntry::Record:
312 // The interesting case.
316 // Read abbrev records, associate them with CurBID.
317 if (Entry.ID == bitc::DEFINE_ABBREV) {
318 if (!CurBlockInfo) return true;
321 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
322 // appropriate BlockInfo.
323 CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
324 CurAbbrevs.pop_back();
330 switch (readRecord(Entry.ID, Record)) {
331 default: break; // Default behavior, ignore unknown content.
332 case bitc::BLOCKINFO_CODE_SETBID:
333 if (Record.size() < 1) return true;
334 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
336 case bitc::BLOCKINFO_CODE_BLOCKNAME: {
337 if (!CurBlockInfo) return true;
338 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
340 for (unsigned i = 0, e = Record.size(); i != e; ++i)
341 Name += (char)Record[i];
342 CurBlockInfo->Name = Name;
345 case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
346 if (!CurBlockInfo) return true;
347 if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
349 for (unsigned i = 1, e = Record.size(); i != e; ++i)
350 Name += (char)Record[i];
351 CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],