BitstreamReader.cpp revision 47543a8a66fb9451126f134808b55853aca57e1c
1//===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Bitcode/BitstreamReader.h"
11
12using namespace llvm;
13
14//===----------------------------------------------------------------------===//
15//  BitstreamCursor implementation
16//===----------------------------------------------------------------------===//
17
18void BitstreamCursor::operator=(const BitstreamCursor &RHS) {
19  freeState();
20
21  BitStream = RHS.BitStream;
22  NextChar = RHS.NextChar;
23  CurWord = RHS.CurWord;
24  BitsInCurWord = RHS.BitsInCurWord;
25  CurCodeSize = RHS.CurCodeSize;
26
27  // Copy abbreviations, and bump ref counts.
28  CurAbbrevs = RHS.CurAbbrevs;
29  for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
30       i != e; ++i)
31    CurAbbrevs[i]->addRef();
32
33  // Copy block scope and bump ref counts.
34  BlockScope = RHS.BlockScope;
35  for (unsigned S = 0, e = static_cast<unsigned>(BlockScope.size());
36       S != e; ++S) {
37    std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
38    for (unsigned i = 0, e = static_cast<unsigned>(Abbrevs.size());
39         i != e; ++i)
40      Abbrevs[i]->addRef();
41  }
42}
43
44void BitstreamCursor::freeState() {
45  // Free all the Abbrevs.
46  for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
47       i != e; ++i)
48    CurAbbrevs[i]->dropRef();
49  CurAbbrevs.clear();
50
51  // Free all the Abbrevs in the block scope.
52  for (unsigned S = 0, e = static_cast<unsigned>(BlockScope.size());
53       S != e; ++S) {
54    std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
55    for (unsigned i = 0, e = static_cast<unsigned>(Abbrevs.size());
56         i != e; ++i)
57      Abbrevs[i]->dropRef();
58  }
59  BlockScope.clear();
60}
61
62/// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
63/// the block, and return true if the block has an error.
64bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
65  // Save the current block's state on BlockScope.
66  BlockScope.push_back(Block(CurCodeSize));
67  BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
68
69  // Add the abbrevs specific to this block to the CurAbbrevs list.
70  if (const BitstreamReader::BlockInfo *Info =
71      BitStream->getBlockInfo(BlockID)) {
72    for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size());
73         i != e; ++i) {
74      CurAbbrevs.push_back(Info->Abbrevs[i]);
75      CurAbbrevs.back()->addRef();
76    }
77  }
78
79  // Get the codesize of this block.
80  CurCodeSize = ReadVBR(bitc::CodeLenWidth);
81  SkipToFourByteBoundary();
82  unsigned NumWords = Read(bitc::BlockSizeWidth);
83  if (NumWordsP) *NumWordsP = NumWords;
84
85  // Validate that this block is sane.
86  if (CurCodeSize == 0 || AtEndOfStream())
87    return true;
88
89  return false;
90}
91
92void BitstreamCursor::readAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
93                                             SmallVectorImpl<uint64_t> &Vals) {
94  assert(Op.isLiteral() && "Not a literal");
95  // If the abbrev specifies the literal value to use, use it.
96  Vals.push_back(Op.getLiteralValue());
97}
98
99void BitstreamCursor::readAbbreviatedField(const BitCodeAbbrevOp &Op,
100                                           SmallVectorImpl<uint64_t> &Vals) {
101  assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
102
103  // Decode the value as we are commanded.
104  switch (Op.getEncoding()) {
105  case BitCodeAbbrevOp::Array:
106  case BitCodeAbbrevOp::Blob:
107    assert(0 && "Should not reach here");
108  case BitCodeAbbrevOp::Fixed:
109    Vals.push_back(Read((unsigned)Op.getEncodingData()));
110    break;
111  case BitCodeAbbrevOp::VBR:
112    Vals.push_back(ReadVBR64((unsigned)Op.getEncodingData()));
113    break;
114  case BitCodeAbbrevOp::Char6:
115    Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
116    break;
117  }
118}
119
120void BitstreamCursor::skipAbbreviatedField(const BitCodeAbbrevOp &Op) {
121  assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
122
123  // Decode the value as we are commanded.
124  switch (Op.getEncoding()) {
125  case BitCodeAbbrevOp::Array:
126  case BitCodeAbbrevOp::Blob:
127    assert(0 && "Should not reach here");
128  case BitCodeAbbrevOp::Fixed:
129    (void)Read((unsigned)Op.getEncodingData());
130    break;
131  case BitCodeAbbrevOp::VBR:
132    (void)ReadVBR64((unsigned)Op.getEncodingData());
133    break;
134  case BitCodeAbbrevOp::Char6:
135    (void)Read(6);
136    break;
137  }
138}
139
140
141
142/// skipRecord - Read the current record and discard it.
143void BitstreamCursor::skipRecord(unsigned AbbrevID) {
144  // Skip unabbreviated records by reading past their entries.
145  if (AbbrevID == bitc::UNABBREV_RECORD) {
146    unsigned Code = ReadVBR(6);
147    (void)Code;
148    unsigned NumElts = ReadVBR(6);
149    for (unsigned i = 0; i != NumElts; ++i)
150      (void)ReadVBR64(6);
151    return;
152  }
153
154  const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
155
156  for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
157    const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
158    if (Op.isLiteral())
159      continue;
160
161    if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
162        Op.getEncoding() != BitCodeAbbrevOp::Blob) {
163      skipAbbreviatedField(Op);
164      continue;
165    }
166
167    if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
168      // Array case.  Read the number of elements as a vbr6.
169      unsigned NumElts = ReadVBR(6);
170
171      // Get the element encoding.
172      assert(i+2 == e && "array op not second to last?");
173      const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
174
175      // Read all the elements.
176      for (; NumElts; --NumElts)
177        skipAbbreviatedField(EltEnc);
178      continue;
179    }
180
181    assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
182    // Blob case.  Read the number of bytes as a vbr6.
183    unsigned NumElts = ReadVBR(6);
184    SkipToFourByteBoundary();  // 32-bit alignment
185
186    // Figure out where the end of this blob will be including tail padding.
187    size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
188
189    // If this would read off the end of the bitcode file, just set the
190    // record to empty and return.
191    if (!canSkipToPos(NewEnd/8)) {
192      NextChar = BitStream->getBitcodeBytes().getExtent();
193      break;
194    }
195
196    // Skip over the blob.
197    JumpToBit(NewEnd);
198  }
199}
200
201unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
202                                     SmallVectorImpl<uint64_t> &Vals,
203                                     StringRef *Blob) {
204  if (AbbrevID == bitc::UNABBREV_RECORD) {
205    unsigned Code = ReadVBR(6);
206    unsigned NumElts = ReadVBR(6);
207    for (unsigned i = 0; i != NumElts; ++i)
208      Vals.push_back(ReadVBR64(6));
209    return Code;
210  }
211
212  const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
213
214  for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
215    const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
216    if (Op.isLiteral()) {
217      readAbbreviatedLiteral(Op, Vals);
218      continue;
219    }
220
221    if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
222        Op.getEncoding() != BitCodeAbbrevOp::Blob) {
223      readAbbreviatedField(Op, Vals);
224      continue;
225    }
226
227    if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
228      // Array case.  Read the number of elements as a vbr6.
229      unsigned NumElts = ReadVBR(6);
230
231      // Get the element encoding.
232      assert(i+2 == e && "array op not second to last?");
233      const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
234
235      // Read all the elements.
236      for (; NumElts; --NumElts)
237        readAbbreviatedField(EltEnc, Vals);
238      continue;
239    }
240
241    assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
242    // Blob case.  Read the number of bytes as a vbr6.
243    unsigned NumElts = ReadVBR(6);
244    SkipToFourByteBoundary();  // 32-bit alignment
245
246    // Figure out where the end of this blob will be including tail padding.
247    size_t CurBitPos = GetCurrentBitNo();
248    size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
249
250    // If this would read off the end of the bitcode file, just set the
251    // record to empty and return.
252    if (!canSkipToPos(NewEnd/8)) {
253      Vals.append(NumElts, 0);
254      NextChar = BitStream->getBitcodeBytes().getExtent();
255      break;
256    }
257
258    // Otherwise, read the number of bytes.  If we can return a reference to
259    // the data, do so to avoid copying it.
260    if (Blob) {
261      *Blob =
262        StringRef((const char*)BitStream->getBitcodeBytes().getPointer(
263                                                          CurBitPos/8, NumElts),
264                  NumElts);
265    } else {
266      // FIXME: This is a brutally inefficient way to do this.  Why isn't this
267      // just using getPointer?
268      for (; NumElts; --NumElts)
269        Vals.push_back(Read(8));
270    }
271    // Skip over tail padding.
272    JumpToBit(NewEnd);
273  }
274
275  unsigned Code = (unsigned)Vals[0];
276  Vals.erase(Vals.begin());
277  return Code;
278}
279
280
281void BitstreamCursor::ReadAbbrevRecord() {
282  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
283  unsigned NumOpInfo = ReadVBR(5);
284  for (unsigned i = 0; i != NumOpInfo; ++i) {
285    bool IsLiteral = Read(1) ? true : false;
286    if (IsLiteral) {
287      Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
288      continue;
289    }
290
291    BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
292    if (BitCodeAbbrevOp::hasEncodingData(E))
293      Abbv->Add(BitCodeAbbrevOp(E, ReadVBR64(5)));
294    else
295      Abbv->Add(BitCodeAbbrevOp(E));
296  }
297  CurAbbrevs.push_back(Abbv);
298}
299
300bool BitstreamCursor::ReadBlockInfoBlock() {
301  // If this is the second stream to get to the block info block, skip it.
302  if (BitStream->hasBlockInfoRecords())
303    return SkipBlock();
304
305  if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
306
307  SmallVector<uint64_t, 64> Record;
308  BitstreamReader::BlockInfo *CurBlockInfo = 0;
309
310  // Read all the records for this module.
311  while (1) {
312    BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
313
314    switch (Entry.Kind) {
315    case llvm::BitstreamEntry::SubBlock: // Handled for us already.
316    case llvm::BitstreamEntry::Error:
317      return true;
318    case llvm::BitstreamEntry::EndBlock:
319      return false;
320    case llvm::BitstreamEntry::Record:
321      // The interesting case.
322      break;
323    }
324
325    // Read abbrev records, associate them with CurBID.
326    if (Entry.ID == bitc::DEFINE_ABBREV) {
327      if (!CurBlockInfo) return true;
328      ReadAbbrevRecord();
329
330      // ReadAbbrevRecord installs the abbrev in CurAbbrevs.  Move it to the
331      // appropriate BlockInfo.
332      BitCodeAbbrev *Abbv = CurAbbrevs.back();
333      CurAbbrevs.pop_back();
334      CurBlockInfo->Abbrevs.push_back(Abbv);
335      continue;
336    }
337
338    // Read a record.
339    Record.clear();
340    switch (readRecord(Entry.ID, Record)) {
341      default: break;  // Default behavior, ignore unknown content.
342      case bitc::BLOCKINFO_CODE_SETBID:
343        if (Record.size() < 1) return true;
344        CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
345        break;
346      case bitc::BLOCKINFO_CODE_BLOCKNAME: {
347        if (!CurBlockInfo) return true;
348        if (BitStream->isIgnoringBlockInfoNames()) break;  // Ignore name.
349        std::string Name;
350        for (unsigned i = 0, e = Record.size(); i != e; ++i)
351          Name += (char)Record[i];
352        CurBlockInfo->Name = Name;
353        break;
354      }
355      case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
356        if (!CurBlockInfo) return true;
357        if (BitStream->isIgnoringBlockInfoNames()) break;  // Ignore name.
358        std::string Name;
359        for (unsigned i = 1, e = Record.size(); i != e; ++i)
360          Name += (char)Record[i];
361        CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
362                                                           Name));
363        break;
364      }
365    }
366  }
367}
368
369
370