LLVM  9.0.0svn
BitstreamReader.h
Go to the documentation of this file.
1 //===- BitstreamReader.h - Low-level bitstream reader interface -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This header defines the BitstreamReader class. This class can be used to
10 // read an arbitrary bitstream, regardless of its contents.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_BITCODE_BITSTREAMREADER_H
15 #define LLVM_BITCODE_BITSTREAMREADER_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Bitcode/BitCodes.h"
20 #include "llvm/Support/Endian.h"
24 #include <algorithm>
25 #include <cassert>
26 #include <climits>
27 #include <cstddef>
28 #include <cstdint>
29 #include <memory>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 namespace llvm {
35 
36 /// This class maintains the abbreviations read from a block info block.
38 public:
39  /// This contains information emitted to BLOCKINFO_BLOCK blocks. These
40  /// describe abbreviations that all blocks of the specified ID inherit.
41  struct BlockInfo {
42  unsigned BlockID;
43  std::vector<std::shared_ptr<BitCodeAbbrev>> Abbrevs;
44  std::string Name;
45  std::vector<std::pair<unsigned, std::string>> RecordNames;
46  };
47 
48 private:
49  std::vector<BlockInfo> BlockInfoRecords;
50 
51 public:
52  /// If there is block info for the specified ID, return it, otherwise return
53  /// null.
54  const BlockInfo *getBlockInfo(unsigned BlockID) const {
55  // Common case, the most recent entry matches BlockID.
56  if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
57  return &BlockInfoRecords.back();
58 
59  for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
60  i != e; ++i)
61  if (BlockInfoRecords[i].BlockID == BlockID)
62  return &BlockInfoRecords[i];
63  return nullptr;
64  }
65 
67  if (const BlockInfo *BI = getBlockInfo(BlockID))
68  return *const_cast<BlockInfo*>(BI);
69 
70  // Otherwise, add a new record.
71  BlockInfoRecords.emplace_back();
72  BlockInfoRecords.back().BlockID = BlockID;
73  return BlockInfoRecords.back();
74  }
75 };
76 
77 /// This represents a position within a bitstream. There may be multiple
78 /// independent cursors reading within one bitstream, each maintaining their
79 /// own local state.
81  ArrayRef<uint8_t> BitcodeBytes;
82  size_t NextChar = 0;
83 
84 public:
85  /// This is the current data we have pulled from the stream but have not
86  /// returned to the client. This is specifically and intentionally defined to
87  /// follow the word size of the host machine for efficiency. We use word_t in
88  /// places that are aware of this to make it perfectly explicit what is going
89  /// on.
90  using word_t = size_t;
91 
92 private:
93  word_t CurWord = 0;
94 
95  /// This is the number of bits in CurWord that are valid. This is always from
96  /// [0...bits_of(size_t)-1] inclusive.
97  unsigned BitsInCurWord = 0;
98 
99 public:
100  static const size_t MaxChunkSize = sizeof(word_t) * 8;
101 
102  SimpleBitstreamCursor() = default;
104  : BitcodeBytes(BitcodeBytes) {}
105  explicit SimpleBitstreamCursor(StringRef BitcodeBytes)
106  : BitcodeBytes(arrayRefFromStringRef(BitcodeBytes)) {}
107  explicit SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes)
108  : SimpleBitstreamCursor(BitcodeBytes.getBuffer()) {}
109 
110  bool canSkipToPos(size_t pos) const {
111  // pos can be skipped to if it is a valid address or one byte past the end.
112  return pos <= BitcodeBytes.size();
113  }
114 
115  bool AtEndOfStream() {
116  return BitsInCurWord == 0 && BitcodeBytes.size() <= NextChar;
117  }
118 
119  /// Return the bit # of the bit we are reading.
120  uint64_t GetCurrentBitNo() const {
121  return NextChar*CHAR_BIT - BitsInCurWord;
122  }
123 
124  // Return the byte # of the current bit.
125  uint64_t getCurrentByteNo() const { return GetCurrentBitNo() / 8; }
126 
127  ArrayRef<uint8_t> getBitcodeBytes() const { return BitcodeBytes; }
128 
129  /// Reset the stream to the specified bit number.
130  void JumpToBit(uint64_t BitNo) {
131  size_t ByteNo = size_t(BitNo/8) & ~(sizeof(word_t)-1);
132  unsigned WordBitNo = unsigned(BitNo & (sizeof(word_t)*8-1));
133  assert(canSkipToPos(ByteNo) && "Invalid location");
134 
135  // Move the cursor to the right word.
136  NextChar = ByteNo;
137  BitsInCurWord = 0;
138 
139  // Skip over any bits that are already consumed.
140  if (WordBitNo)
141  Read(WordBitNo);
142  }
143 
144  /// Get a pointer into the bitstream at the specified byte offset.
145  const uint8_t *getPointerToByte(uint64_t ByteNo, uint64_t NumBytes) {
146  return BitcodeBytes.data() + ByteNo;
147  }
148 
149  /// Get a pointer into the bitstream at the specified bit offset.
150  ///
151  /// The bit offset must be on a byte boundary.
152  const uint8_t *getPointerToBit(uint64_t BitNo, uint64_t NumBytes) {
153  assert(!(BitNo % 8) && "Expected bit on byte boundary");
154  return getPointerToByte(BitNo / 8, NumBytes);
155  }
156 
157  void fillCurWord() {
158  if (NextChar >= BitcodeBytes.size())
159  report_fatal_error("Unexpected end of file");
160 
161  // Read the next word from the stream.
162  const uint8_t *NextCharPtr = BitcodeBytes.data() + NextChar;
163  unsigned BytesRead;
164  if (BitcodeBytes.size() >= NextChar + sizeof(word_t)) {
165  BytesRead = sizeof(word_t);
166  CurWord =
167  support::endian::read<word_t, support::little, support::unaligned>(
168  NextCharPtr);
169  } else {
170  // Short read.
171  BytesRead = BitcodeBytes.size() - NextChar;
172  CurWord = 0;
173  for (unsigned B = 0; B != BytesRead; ++B)
174  CurWord |= uint64_t(NextCharPtr[B]) << (B * 8);
175  }
176  NextChar += BytesRead;
177  BitsInCurWord = BytesRead * 8;
178  }
179 
180  word_t Read(unsigned NumBits) {
181  static const unsigned BitsInWord = MaxChunkSize;
182 
183  assert(NumBits && NumBits <= BitsInWord &&
184  "Cannot return zero or more than BitsInWord bits!");
185 
186  static const unsigned Mask = sizeof(word_t) > 4 ? 0x3f : 0x1f;
187 
188  // If the field is fully contained by CurWord, return it quickly.
189  if (BitsInCurWord >= NumBits) {
190  word_t R = CurWord & (~word_t(0) >> (BitsInWord - NumBits));
191 
192  // Use a mask to avoid undefined behavior.
193  CurWord >>= (NumBits & Mask);
194 
195  BitsInCurWord -= NumBits;
196  return R;
197  }
198 
199  word_t R = BitsInCurWord ? CurWord : 0;
200  unsigned BitsLeft = NumBits - BitsInCurWord;
201 
202  fillCurWord();
203 
204  // If we run out of data, abort.
205  if (BitsLeft > BitsInCurWord)
206  report_fatal_error("Unexpected end of file");
207 
208  word_t R2 = CurWord & (~word_t(0) >> (BitsInWord - BitsLeft));
209 
210  // Use a mask to avoid undefined behavior.
211  CurWord >>= (BitsLeft & Mask);
212 
213  BitsInCurWord -= BitsLeft;
214 
215  R |= R2 << (NumBits - BitsLeft);
216 
217  return R;
218  }
219 
220  uint32_t ReadVBR(unsigned NumBits) {
221  uint32_t Piece = Read(NumBits);
222  if ((Piece & (1U << (NumBits-1))) == 0)
223  return Piece;
224 
225  uint32_t Result = 0;
226  unsigned NextBit = 0;
227  while (true) {
228  Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
229 
230  if ((Piece & (1U << (NumBits-1))) == 0)
231  return Result;
232 
233  NextBit += NumBits-1;
234  Piece = Read(NumBits);
235  }
236  }
237 
238  // Read a VBR that may have a value up to 64-bits in size. The chunk size of
239  // the VBR must still be <= 32 bits though.
240  uint64_t ReadVBR64(unsigned NumBits) {
241  uint32_t Piece = Read(NumBits);
242  if ((Piece & (1U << (NumBits-1))) == 0)
243  return uint64_t(Piece);
244 
245  uint64_t Result = 0;
246  unsigned NextBit = 0;
247  while (true) {
248  Result |= uint64_t(Piece & ((1U << (NumBits-1))-1)) << NextBit;
249 
250  if ((Piece & (1U << (NumBits-1))) == 0)
251  return Result;
252 
253  NextBit += NumBits-1;
254  Piece = Read(NumBits);
255  }
256  }
257 
259  // If word_t is 64-bits and if we've read less than 32 bits, just dump
260  // the bits we have up to the next 32-bit boundary.
261  if (sizeof(word_t) > 4 &&
262  BitsInCurWord >= 32) {
263  CurWord >>= BitsInCurWord-32;
264  BitsInCurWord = 32;
265  return;
266  }
267 
268  BitsInCurWord = 0;
269  }
270 
271  /// Skip to the end of the file.
272  void skipToEnd() { NextChar = BitcodeBytes.size(); }
273 };
274 
275 /// When advancing through a bitstream cursor, each advance can discover a few
276 /// different kinds of entries:
278  enum {
279  Error, // Malformed bitcode was found.
280  EndBlock, // We've reached the end of the current block, (or the end of the
281  // file, which is treated like a series of EndBlock records.
282  SubBlock, // This is the start of a new subblock of a specific ID.
283  Record // This is a record with a specific AbbrevID.
284  } Kind;
285 
286  unsigned ID;
287 
289  BitstreamEntry E; E.Kind = Error; return E;
290  }
291 
293  BitstreamEntry E; E.Kind = EndBlock; return E;
294  }
295 
296  static BitstreamEntry getSubBlock(unsigned ID) {
297  BitstreamEntry E; E.Kind = SubBlock; E.ID = ID; return E;
298  }
299 
300  static BitstreamEntry getRecord(unsigned AbbrevID) {
301  BitstreamEntry E; E.Kind = Record; E.ID = AbbrevID; return E;
302  }
303 };
304 
305 /// This represents a position within a bitcode file, implemented on top of a
306 /// SimpleBitstreamCursor.
307 ///
308 /// Unlike iterators, BitstreamCursors are heavy-weight objects that should not
309 /// be passed by value.
311  // This is the declared size of code values used for the current block, in
312  // bits.
313  unsigned CurCodeSize = 2;
314 
315  /// Abbrevs installed at in this block.
316  std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs;
317 
318  struct Block {
319  unsigned PrevCodeSize;
320  std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
321 
322  explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
323  };
324 
325  /// This tracks the codesize of parent blocks.
326  SmallVector<Block, 8> BlockScope;
327 
328  BitstreamBlockInfo *BlockInfo = nullptr;
329 
330 public:
331  static const size_t MaxChunkSize = sizeof(word_t) * 8;
332 
333  BitstreamCursor() = default;
334  explicit BitstreamCursor(ArrayRef<uint8_t> BitcodeBytes)
335  : SimpleBitstreamCursor(BitcodeBytes) {}
336  explicit BitstreamCursor(StringRef BitcodeBytes)
337  : SimpleBitstreamCursor(BitcodeBytes) {}
338  explicit BitstreamCursor(MemoryBufferRef BitcodeBytes)
339  : SimpleBitstreamCursor(BitcodeBytes) {}
340 
352 
353  /// Return the number of bits used to encode an abbrev #.
354  unsigned getAbbrevIDWidth() const { return CurCodeSize; }
355 
356  /// Flags that modify the behavior of advance().
357  enum {
358  /// If this flag is used, the advance() method does not automatically pop
359  /// the block scope when the end of a block is reached.
360  AF_DontPopBlockAtEnd = 1,
361 
362  /// If this flag is used, abbrev entries are returned just like normal
363  /// records.
364  AF_DontAutoprocessAbbrevs = 2
365  };
366 
367  /// Advance the current bitstream, returning the next entry in the stream.
368  BitstreamEntry advance(unsigned Flags = 0) {
369  while (true) {
370  if (AtEndOfStream())
371  return BitstreamEntry::getError();
372 
373  unsigned Code = ReadCode();
374  if (Code == bitc::END_BLOCK) {
375  // Pop the end of the block unless Flags tells us not to.
376  if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd())
377  return BitstreamEntry::getError();
379  }
380 
381  if (Code == bitc::ENTER_SUBBLOCK)
382  return BitstreamEntry::getSubBlock(ReadSubBlockID());
383 
384  if (Code == bitc::DEFINE_ABBREV &&
385  !(Flags & AF_DontAutoprocessAbbrevs)) {
386  // We read and accumulate abbrev's, the client can't do anything with
387  // them anyway.
388  ReadAbbrevRecord();
389  continue;
390  }
391 
392  return BitstreamEntry::getRecord(Code);
393  }
394  }
395 
396  /// This is a convenience function for clients that don't expect any
397  /// subblocks. This just skips over them automatically.
399  while (true) {
400  // If we found a normal entry, return it.
401  BitstreamEntry Entry = advance(Flags);
402  if (Entry.Kind != BitstreamEntry::SubBlock)
403  return Entry;
404 
405  // If we found a sub-block, just skip over it and check the next entry.
406  if (SkipBlock())
407  return BitstreamEntry::getError();
408  }
409  }
410 
411  unsigned ReadCode() {
412  return Read(CurCodeSize);
413  }
414 
415  // Block header:
416  // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
417 
418  /// Having read the ENTER_SUBBLOCK code, read the BlockID for the block.
419  unsigned ReadSubBlockID() {
420  return ReadVBR(bitc::BlockIDWidth);
421  }
422 
423  /// Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body
424  /// of this block. If the block record is malformed, return true.
425  bool SkipBlock() {
426  // Read and ignore the codelen value. Since we are skipping this block, we
427  // don't care what code widths are used inside of it.
428  ReadVBR(bitc::CodeLenWidth);
429  SkipToFourByteBoundary();
430  size_t NumFourBytes = Read(bitc::BlockSizeWidth);
431 
432  // Check that the block wasn't partially defined, and that the offset isn't
433  // bogus.
434  size_t SkipTo = GetCurrentBitNo() + NumFourBytes*4*8;
435  if (AtEndOfStream() || !canSkipToPos(SkipTo/8))
436  return true;
437 
438  JumpToBit(SkipTo);
439  return false;
440  }
441 
442  /// Having read the ENTER_SUBBLOCK abbrevid, enter the block, and return true
443  /// if the block has an error.
444  bool EnterSubBlock(unsigned BlockID, unsigned *NumWordsP = nullptr);
445 
446  bool ReadBlockEnd() {
447  if (BlockScope.empty()) return true;
448 
449  // Block tail:
450  // [END_BLOCK, <align4bytes>]
451  SkipToFourByteBoundary();
452 
453  popBlockScope();
454  return false;
455  }
456 
457 private:
458  void popBlockScope() {
459  CurCodeSize = BlockScope.back().PrevCodeSize;
460 
461  CurAbbrevs = std::move(BlockScope.back().PrevAbbrevs);
462  BlockScope.pop_back();
463  }
464 
465  //===--------------------------------------------------------------------===//
466  // Record Processing
467  //===--------------------------------------------------------------------===//
468 
469 public:
470  /// Return the abbreviation for the specified AbbrevId.
471  const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
472  unsigned AbbrevNo = AbbrevID - bitc::FIRST_APPLICATION_ABBREV;
473  if (AbbrevNo >= CurAbbrevs.size())
474  report_fatal_error("Invalid abbrev number");
475  return CurAbbrevs[AbbrevNo].get();
476  }
477 
478  /// Read the current record and discard it, returning the code for the record.
479  unsigned skipRecord(unsigned AbbrevID);
480 
481  unsigned readRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals,
482  StringRef *Blob = nullptr);
483 
484  //===--------------------------------------------------------------------===//
485  // Abbrev Processing
486  //===--------------------------------------------------------------------===//
487  void ReadAbbrevRecord();
488 
489  /// Read and return a block info block from the bitstream. If an error was
490  /// encountered, return None.
491  ///
492  /// \param ReadBlockInfoNames Whether to read block/record name information in
493  /// the BlockInfo block. Only llvm-bcanalyzer uses this.
495  ReadBlockInfoBlock(bool ReadBlockInfoNames = false);
496 
497  /// Set the block info to be used by this BitstreamCursor to interpret
498  /// abbreviated records.
499  void setBlockInfo(BitstreamBlockInfo *BI) { BlockInfo = BI; }
500 };
501 
502 } // end llvm namespace
503 
504 #endif // LLVM_BITCODE_BITSTREAMREADER_H
This contains information emitted to BLOCKINFO_BLOCK blocks.
const BitCodeAbbrev * getAbbrev(unsigned AbbrevID)
Return the abbreviation for the specified AbbrevId.
BlockInfo & getOrCreateBlockInfo(unsigned BlockID)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
uint64_t GetCurrentBitNo() const
Return the bit # of the bit we are reading.
const uint8_t * getPointerToByte(uint64_t ByteNo, uint64_t NumBytes)
Get a pointer into the bitstream at the specified byte offset.
BitCodeAbbrev - This class represents an abbreviation record.
Definition: BitCodes.h:165
const uint8_t * getPointerToBit(uint64_t BitNo, uint64_t NumBytes)
Get a pointer into the bitstream at the specified bit offset.
ArrayRef< uint8_t > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
Definition: StringExtras.h:60
BitstreamCursor(MemoryBufferRef BitcodeBytes)
#define R2(n)
SimpleBitstreamCursor(ArrayRef< uint8_t > BitcodeBytes)
std::vector< std::pair< unsigned, std::string > > RecordNames
void setBlockInfo(BitstreamBlockInfo *BI)
Set the block info to be used by this BitstreamCursor to interpret abbreviated records.
word_t Read(unsigned NumBits)
bool canSkipToPos(size_t pos) const
uint64_t ReadVBR64(unsigned NumBits)
void skipToEnd()
Skip to the end of the file.
uint32_t ReadVBR(unsigned NumBits)
static BitstreamEntry getSubBlock(unsigned ID)
ArrayRef< uint8_t > getBitcodeBytes() const
BitstreamEntry advanceSkippingSubblocks(unsigned Flags=0)
This is a convenience function for clients that don&#39;t expect any subblocks.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
uint64_t getCurrentByteNo() const
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This represents a position within a bitcode file, implemented on top of a SimpleBitstreamCursor.
const BlockInfo * getBlockInfo(unsigned BlockID) const
If there is block info for the specified ID, return it, otherwise return null.
void JumpToBit(uint64_t BitNo)
Reset the stream to the specified bit number.
unsigned getAbbrevIDWidth() const
Return the number of bits used to encode an abbrev #.
bool SkipBlock()
Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body of this block...
const T * data() const
Definition: ArrayRef.h:145
std::vector< std::shared_ptr< BitCodeAbbrev > > Abbrevs
SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes)
unsigned ReadSubBlockID()
Having read the ENTER_SUBBLOCK code, read the BlockID for the block.
When advancing through a bitstream cursor, each advance can discover a few different kinds of entries...
BitstreamCursor(ArrayRef< uint8_t > BitcodeBytes)
This class maintains the abbreviations read from a block info block.
DEFINE_ABBREV - Defines an abbrev for the current block.
Definition: BitCodes.h:52
enum llvm::BitstreamEntry::@153 Kind
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
static BitstreamEntry getEndBlock()
static BitstreamEntry getRecord(unsigned AbbrevID)
SimpleBitstreamCursor(StringRef BitcodeBytes)
static BitstreamEntry getError()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
BitstreamEntry advance(unsigned Flags=0)
Advance the current bitstream, returning the next entry in the stream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
This represents a position within a bitstream.
BitstreamCursor(StringRef BitcodeBytes)