1249259Sdim//===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
2249259Sdim//
3249259Sdim//                     The LLVM Compiler Infrastructure
4249259Sdim//
5249259Sdim// This file is distributed under the University of Illinois Open Source
6249259Sdim// License. See LICENSE.TXT for details.
7249259Sdim//
8249259Sdim//===----------------------------------------------------------------------===//
9249259Sdim
10249259Sdim#include "llvm/Bitcode/BitstreamReader.h"
11249259Sdim
12249259Sdimusing namespace llvm;
13249259Sdim
14249259Sdim//===----------------------------------------------------------------------===//
15249259Sdim//  BitstreamCursor implementation
16249259Sdim//===----------------------------------------------------------------------===//
17249259Sdim
18249259Sdimvoid BitstreamCursor::operator=(const BitstreamCursor &RHS) {
19249259Sdim  freeState();
20249259Sdim
21249259Sdim  BitStream = RHS.BitStream;
22249259Sdim  NextChar = RHS.NextChar;
23249259Sdim  CurWord = RHS.CurWord;
24249259Sdim  BitsInCurWord = RHS.BitsInCurWord;
25249259Sdim  CurCodeSize = RHS.CurCodeSize;
26249259Sdim
27249259Sdim  // Copy abbreviations, and bump ref counts.
28249259Sdim  CurAbbrevs = RHS.CurAbbrevs;
29249259Sdim  for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
30249259Sdim    CurAbbrevs[i]->addRef();
31249259Sdim
32249259Sdim  // Copy block scope and bump ref counts.
33249259Sdim  BlockScope = RHS.BlockScope;
34249259Sdim  for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
35249259Sdim    std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
36249259Sdim    for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
37249259Sdim      Abbrevs[i]->addRef();
38249259Sdim  }
39249259Sdim}
40249259Sdim
41249259Sdimvoid BitstreamCursor::freeState() {
42249259Sdim  // Free all the Abbrevs.
43249259Sdim  for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
44249259Sdim    CurAbbrevs[i]->dropRef();
45249259Sdim  CurAbbrevs.clear();
46249259Sdim
47249259Sdim  // Free all the Abbrevs in the block scope.
48249259Sdim  for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
49249259Sdim    std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
50249259Sdim    for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
51249259Sdim      Abbrevs[i]->dropRef();
52249259Sdim  }
53249259Sdim  BlockScope.clear();
54249259Sdim}
55249259Sdim
56249259Sdim/// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
57249259Sdim/// the block, and return true if the block has an error.
58249259Sdimbool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
59249259Sdim  // Save the current block's state on BlockScope.
60249259Sdim  BlockScope.push_back(Block(CurCodeSize));
61249259Sdim  BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
62249259Sdim
63249259Sdim  // Add the abbrevs specific to this block to the CurAbbrevs list.
64249259Sdim  if (const BitstreamReader::BlockInfo *Info =
65249259Sdim      BitStream->getBlockInfo(BlockID)) {
66249259Sdim    for (size_t i = 0, e = Info->Abbrevs.size(); i != e; ++i) {
67249259Sdim      CurAbbrevs.push_back(Info->Abbrevs[i]);
68249259Sdim      CurAbbrevs.back()->addRef();
69249259Sdim    }
70249259Sdim  }
71249259Sdim
72249259Sdim  // Get the codesize of this block.
73249259Sdim  CurCodeSize = ReadVBR(bitc::CodeLenWidth);
74249259Sdim  SkipToFourByteBoundary();
75249259Sdim  unsigned NumWords = Read(bitc::BlockSizeWidth);
76249259Sdim  if (NumWordsP) *NumWordsP = NumWords;
77249259Sdim
78249259Sdim  // Validate that this block is sane.
79249259Sdim  if (CurCodeSize == 0 || AtEndOfStream())
80249259Sdim    return true;
81249259Sdim
82249259Sdim  return false;
83249259Sdim}
84249259Sdim
85249259Sdimvoid BitstreamCursor::readAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
86249259Sdim                                             SmallVectorImpl<uint64_t> &Vals) {
87249259Sdim  assert(Op.isLiteral() && "Not a literal");
88249259Sdim  // If the abbrev specifies the literal value to use, use it.
89249259Sdim  Vals.push_back(Op.getLiteralValue());
90249259Sdim}
91249259Sdim
92249259Sdimvoid BitstreamCursor::readAbbreviatedField(const BitCodeAbbrevOp &Op,
93249259Sdim                                           SmallVectorImpl<uint64_t> &Vals) {
94249259Sdim  assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
95249259Sdim
96249259Sdim  // Decode the value as we are commanded.
97249259Sdim  switch (Op.getEncoding()) {
98249259Sdim  case BitCodeAbbrevOp::Array:
99249259Sdim  case BitCodeAbbrevOp::Blob:
100249259Sdim    assert(0 && "Should not reach here");
101249259Sdim  case BitCodeAbbrevOp::Fixed:
102249259Sdim    Vals.push_back(Read((unsigned)Op.getEncodingData()));
103249259Sdim    break;
104249259Sdim  case BitCodeAbbrevOp::VBR:
105249259Sdim    Vals.push_back(ReadVBR64((unsigned)Op.getEncodingData()));
106249259Sdim    break;
107249259Sdim  case BitCodeAbbrevOp::Char6:
108249259Sdim    Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
109249259Sdim    break;
110249259Sdim  }
111249259Sdim}
112249259Sdim
113249259Sdimvoid BitstreamCursor::skipAbbreviatedField(const BitCodeAbbrevOp &Op) {
114249259Sdim  assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
115249259Sdim
116249259Sdim  // Decode the value as we are commanded.
117249259Sdim  switch (Op.getEncoding()) {
118249259Sdim  case BitCodeAbbrevOp::Array:
119249259Sdim  case BitCodeAbbrevOp::Blob:
120249259Sdim    assert(0 && "Should not reach here");
121249259Sdim  case BitCodeAbbrevOp::Fixed:
122249259Sdim    (void)Read((unsigned)Op.getEncodingData());
123249259Sdim    break;
124249259Sdim  case BitCodeAbbrevOp::VBR:
125249259Sdim    (void)ReadVBR64((unsigned)Op.getEncodingData());
126249259Sdim    break;
127249259Sdim  case BitCodeAbbrevOp::Char6:
128249259Sdim    (void)Read(6);
129249259Sdim    break;
130249259Sdim  }
131249259Sdim}
132249259Sdim
133249259Sdim
134249259Sdim
135249259Sdim/// skipRecord - Read the current record and discard it.
136249259Sdimvoid BitstreamCursor::skipRecord(unsigned AbbrevID) {
137249259Sdim  // Skip unabbreviated records by reading past their entries.
138249259Sdim  if (AbbrevID == bitc::UNABBREV_RECORD) {
139249259Sdim    unsigned Code = ReadVBR(6);
140249259Sdim    (void)Code;
141249259Sdim    unsigned NumElts = ReadVBR(6);
142249259Sdim    for (unsigned i = 0; i != NumElts; ++i)
143249259Sdim      (void)ReadVBR64(6);
144249259Sdim    return;
145249259Sdim  }
146249259Sdim
147249259Sdim  const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
148249259Sdim
149249259Sdim  for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
150249259Sdim    const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
151249259Sdim    if (Op.isLiteral())
152249259Sdim      continue;
153249259Sdim
154249259Sdim    if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
155249259Sdim        Op.getEncoding() != BitCodeAbbrevOp::Blob) {
156249259Sdim      skipAbbreviatedField(Op);
157249259Sdim      continue;
158249259Sdim    }
159249259Sdim
160249259Sdim    if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
161249259Sdim      // Array case.  Read the number of elements as a vbr6.
162249259Sdim      unsigned NumElts = ReadVBR(6);
163249259Sdim
164249259Sdim      // Get the element encoding.
165249259Sdim      assert(i+2 == e && "array op not second to last?");
166249259Sdim      const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
167249259Sdim
168249259Sdim      // Read all the elements.
169249259Sdim      for (; NumElts; --NumElts)
170249259Sdim        skipAbbreviatedField(EltEnc);
171249259Sdim      continue;
172249259Sdim    }
173249259Sdim
174249259Sdim    assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
175249259Sdim    // Blob case.  Read the number of bytes as a vbr6.
176249259Sdim    unsigned NumElts = ReadVBR(6);
177249259Sdim    SkipToFourByteBoundary();  // 32-bit alignment
178249259Sdim
179249259Sdim    // Figure out where the end of this blob will be including tail padding.
180249259Sdim    size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
181249259Sdim
182249259Sdim    // If this would read off the end of the bitcode file, just set the
183249259Sdim    // record to empty and return.
184249259Sdim    if (!canSkipToPos(NewEnd/8)) {
185249259Sdim      NextChar = BitStream->getBitcodeBytes().getExtent();
186249259Sdim      break;
187249259Sdim    }
188249259Sdim
189249259Sdim    // Skip over the blob.
190249259Sdim    JumpToBit(NewEnd);
191249259Sdim  }
192249259Sdim}
193249259Sdim
194249259Sdimunsigned BitstreamCursor::readRecord(unsigned AbbrevID,
195249259Sdim                                     SmallVectorImpl<uint64_t> &Vals,
196249259Sdim                                     StringRef *Blob) {
197249259Sdim  if (AbbrevID == bitc::UNABBREV_RECORD) {
198249259Sdim    unsigned Code = ReadVBR(6);
199249259Sdim    unsigned NumElts = ReadVBR(6);
200249259Sdim    for (unsigned i = 0; i != NumElts; ++i)
201249259Sdim      Vals.push_back(ReadVBR64(6));
202249259Sdim    return Code;
203249259Sdim  }
204249259Sdim
205249259Sdim  const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
206249259Sdim
207263508Sdim  // Read the record code first.
208263508Sdim  assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
209263508Sdim  const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
210263508Sdim  if (CodeOp.isLiteral())
211263508Sdim    readAbbreviatedLiteral(CodeOp, Vals);
212263508Sdim  else
213263508Sdim    readAbbreviatedField(CodeOp, Vals);
214263508Sdim  unsigned Code = (unsigned)Vals.pop_back_val();
215263508Sdim
216263508Sdim  for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
217249259Sdim    const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
218249259Sdim    if (Op.isLiteral()) {
219249259Sdim      readAbbreviatedLiteral(Op, Vals);
220249259Sdim      continue;
221249259Sdim    }
222249259Sdim
223249259Sdim    if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
224249259Sdim        Op.getEncoding() != BitCodeAbbrevOp::Blob) {
225249259Sdim      readAbbreviatedField(Op, Vals);
226249259Sdim      continue;
227249259Sdim    }
228249259Sdim
229249259Sdim    if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
230249259Sdim      // Array case.  Read the number of elements as a vbr6.
231249259Sdim      unsigned NumElts = ReadVBR(6);
232249259Sdim
233249259Sdim      // Get the element encoding.
234249259Sdim      assert(i+2 == e && "array op not second to last?");
235249259Sdim      const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
236249259Sdim
237249259Sdim      // Read all the elements.
238249259Sdim      for (; NumElts; --NumElts)
239249259Sdim        readAbbreviatedField(EltEnc, Vals);
240249259Sdim      continue;
241249259Sdim    }
242249259Sdim
243249259Sdim    assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
244249259Sdim    // Blob case.  Read the number of bytes as a vbr6.
245249259Sdim    unsigned NumElts = ReadVBR(6);
246249259Sdim    SkipToFourByteBoundary();  // 32-bit alignment
247249259Sdim
248249259Sdim    // Figure out where the end of this blob will be including tail padding.
249249259Sdim    size_t CurBitPos = GetCurrentBitNo();
250249259Sdim    size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
251249259Sdim
252249259Sdim    // If this would read off the end of the bitcode file, just set the
253249259Sdim    // record to empty and return.
254249259Sdim    if (!canSkipToPos(NewEnd/8)) {
255249259Sdim      Vals.append(NumElts, 0);
256249259Sdim      NextChar = BitStream->getBitcodeBytes().getExtent();
257249259Sdim      break;
258249259Sdim    }
259249259Sdim
260249259Sdim    // Otherwise, inform the streamer that we need these bytes in memory.
261249259Sdim    const char *Ptr = (const char*)
262249259Sdim      BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
263249259Sdim
264249259Sdim    // If we can return a reference to the data, do so to avoid copying it.
265249259Sdim    if (Blob) {
266249259Sdim      *Blob = StringRef(Ptr, NumElts);
267249259Sdim    } else {
268249259Sdim      // Otherwise, unpack into Vals with zero extension.
269249259Sdim      for (; NumElts; --NumElts)
270249259Sdim        Vals.push_back((unsigned char)*Ptr++);
271249259Sdim    }
272249259Sdim    // Skip over tail padding.
273249259Sdim    JumpToBit(NewEnd);
274249259Sdim  }
275249259Sdim
276249259Sdim  return Code;
277249259Sdim}
278249259Sdim
279249259Sdim
280249259Sdimvoid BitstreamCursor::ReadAbbrevRecord() {
281249259Sdim  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
282249259Sdim  unsigned NumOpInfo = ReadVBR(5);
283249259Sdim  for (unsigned i = 0; i != NumOpInfo; ++i) {
284249259Sdim    bool IsLiteral = Read(1) ? true : false;
285249259Sdim    if (IsLiteral) {
286249259Sdim      Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
287249259Sdim      continue;
288249259Sdim    }
289249259Sdim
290249259Sdim    BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
291249259Sdim    if (BitCodeAbbrevOp::hasEncodingData(E)) {
292249259Sdim      unsigned Data = ReadVBR64(5);
293249259Sdim
294249259Sdim      // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
295249259Sdim      // and vbr(0) as a literal zero.  This is decoded the same way, and avoids
296249259Sdim      // a slow path in Read() to have to handle reading zero bits.
297249259Sdim      if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
298249259Sdim          Data == 0) {
299249259Sdim        Abbv->Add(BitCodeAbbrevOp(0));
300249259Sdim        continue;
301249259Sdim      }
302249259Sdim
303249259Sdim      Abbv->Add(BitCodeAbbrevOp(E, Data));
304249259Sdim    } else
305249259Sdim      Abbv->Add(BitCodeAbbrevOp(E));
306249259Sdim  }
307249259Sdim  CurAbbrevs.push_back(Abbv);
308249259Sdim}
309249259Sdim
310249259Sdimbool BitstreamCursor::ReadBlockInfoBlock() {
311249259Sdim  // If this is the second stream to get to the block info block, skip it.
312249259Sdim  if (BitStream->hasBlockInfoRecords())
313249259Sdim    return SkipBlock();
314249259Sdim
315249259Sdim  if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
316249259Sdim
317249259Sdim  SmallVector<uint64_t, 64> Record;
318249259Sdim  BitstreamReader::BlockInfo *CurBlockInfo = 0;
319249259Sdim
320249259Sdim  // Read all the records for this module.
321249259Sdim  while (1) {
322249259Sdim    BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
323249259Sdim
324249259Sdim    switch (Entry.Kind) {
325249259Sdim    case llvm::BitstreamEntry::SubBlock: // Handled for us already.
326249259Sdim    case llvm::BitstreamEntry::Error:
327249259Sdim      return true;
328249259Sdim    case llvm::BitstreamEntry::EndBlock:
329249259Sdim      return false;
330249259Sdim    case llvm::BitstreamEntry::Record:
331249259Sdim      // The interesting case.
332249259Sdim      break;
333249259Sdim    }
334249259Sdim
335249259Sdim    // Read abbrev records, associate them with CurBID.
336249259Sdim    if (Entry.ID == bitc::DEFINE_ABBREV) {
337249259Sdim      if (!CurBlockInfo) return true;
338249259Sdim      ReadAbbrevRecord();
339249259Sdim
340249259Sdim      // ReadAbbrevRecord installs the abbrev in CurAbbrevs.  Move it to the
341249259Sdim      // appropriate BlockInfo.
342249259Sdim      BitCodeAbbrev *Abbv = CurAbbrevs.back();
343249259Sdim      CurAbbrevs.pop_back();
344249259Sdim      CurBlockInfo->Abbrevs.push_back(Abbv);
345249259Sdim      continue;
346249259Sdim    }
347249259Sdim
348249259Sdim    // Read a record.
349249259Sdim    Record.clear();
350249259Sdim    switch (readRecord(Entry.ID, Record)) {
351249259Sdim      default: break;  // Default behavior, ignore unknown content.
352249259Sdim      case bitc::BLOCKINFO_CODE_SETBID:
353249259Sdim        if (Record.size() < 1) return true;
354249259Sdim        CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
355249259Sdim        break;
356249259Sdim      case bitc::BLOCKINFO_CODE_BLOCKNAME: {
357249259Sdim        if (!CurBlockInfo) return true;
358249259Sdim        if (BitStream->isIgnoringBlockInfoNames()) break;  // Ignore name.
359249259Sdim        std::string Name;
360249259Sdim        for (unsigned i = 0, e = Record.size(); i != e; ++i)
361249259Sdim          Name += (char)Record[i];
362249259Sdim        CurBlockInfo->Name = Name;
363249259Sdim        break;
364249259Sdim      }
365249259Sdim      case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
366249259Sdim        if (!CurBlockInfo) return true;
367249259Sdim        if (BitStream->isIgnoringBlockInfoNames()) break;  // Ignore name.
368249259Sdim        std::string Name;
369249259Sdim        for (unsigned i = 1, e = Record.size(); i != e; ++i)
370249259Sdim          Name += (char)Record[i];
371249259Sdim        CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
372249259Sdim                                                           Name));
373249259Sdim        break;
374249259Sdim      }
375249259Sdim    }
376249259Sdim  }
377249259Sdim}
378249259Sdim
379