LLVM: include/llvm/Bitstream/BitstreamReader.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14#ifndef LLVM_BITSTREAM_BITSTREAMREADER_H
15#define LLVM_BITSTREAM_BITSTREAMREADER_H
16
24#include
25#include
26#include
27#include
28#include
29#include
30#include
31#include
32#include
33#include
34
35namespace llvm {
36
37
39public:
40
41
44 std::vector<std::shared_ptr> Abbrevs;
46 std::vector<std::pair<unsigned, std::string>> RecordNames;
47 };
48
49private:
50 std::vector BlockInfoRecords;
51
52public:
53
54
56
57 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
58 return &BlockInfoRecords.back();
59
60 for (const BlockInfo &BI : BlockInfoRecords)
61 if (BI.BlockID == BlockID)
62 return &BI;
63 return nullptr;
64 }
65
68 return *const_cast<BlockInfo*>(BI);
69
70
71 BlockInfoRecords.emplace_back();
72 BlockInfoRecords.back().BlockID = BlockID;
73 return BlockInfoRecords.back();
74 }
75};
76
77
78
79
82 size_t NextChar = 0;
83
84public:
85
86
87
88
89
91
92private:
94
95
96
97 unsigned BitsInCurWord = 0;
98
99public:
102 : BitcodeBytes(BitcodeBytes) {}
107
109
110 return pos <= BitcodeBytes.size();
111 }
112
114 return BitsInCurWord == 0 && BitcodeBytes.size() <= NextChar;
115 }
116
117
119 return uint64_t(NextChar)*CHAR_BIT - BitsInCurWord;
120 }
121
122
124
126
127
129 size_t ByteNo = size_t(BitNo/8) & ~(sizeof(word_t)-1);
130 unsigned WordBitNo = unsigned(BitNo & (sizeof(word_t)*8-1));
132
133
134 NextChar = ByteNo;
135 BitsInCurWord = 0;
136
137
138 if (WordBitNo) {
141 else
142 return Res.takeError();
143 }
144
146 }
147
148
150 return BitcodeBytes.data() + ByteNo;
151 }
152
153
154
155
157 assert(!(BitNo % 8) && "Expected bit on byte boundary");
159 }
160
162 if (NextChar >= BitcodeBytes.size())
164 "Unexpected end of file reading %u of %u bytes",
165 NextChar, BitcodeBytes.size());
166
167
168 const uint8_t *NextCharPtr = BitcodeBytes.data() + NextChar;
169 unsigned BytesRead;
170 if (BitcodeBytes.size() >= NextChar + sizeof(word_t)) {
171 BytesRead = sizeof(word_t);
172 CurWord =
174 } else {
175
176 BytesRead = BitcodeBytes.size() - NextChar;
177 CurWord = 0;
178 for (unsigned B = 0; B != BytesRead; ++B)
179 CurWord |= uint64_t(NextCharPtr[B]) << (B * 8);
180 }
181 NextChar += BytesRead;
182 BitsInCurWord = BytesRead * 8;
184 }
185
187 static const unsigned BitsInWord = sizeof(word_t) * 8;
188
189 assert(NumBits && NumBits <= BitsInWord &&
190 "Cannot return zero or more than BitsInWord bits!");
191
192 static const unsigned Mask = sizeof(word_t) > 4 ? 0x3f : 0x1f;
193
194
195 if (BitsInCurWord >= NumBits) {
196 word_t R = CurWord & (~word_t(0) >> (BitsInWord - NumBits));
197
198
199 CurWord >>= (NumBits & Mask);
200
201 BitsInCurWord -= NumBits;
202 return R;
203 }
204
205 word_t R = BitsInCurWord ? CurWord : 0;
206 unsigned BitsLeft = NumBits - BitsInCurWord;
207
209 return std::move(fillResult);
210
211
212 if (BitsLeft > BitsInCurWord)
214 "Unexpected end of file reading %u of %u bits",
215 BitsInCurWord, BitsLeft);
216
217 word_t R2 = CurWord & (~word_t(0) >> (BitsInWord - BitsLeft));
218
219
220 CurWord >>= (BitsLeft & Mask);
221
222 BitsInCurWord -= BitsLeft;
223
224 R |= R2 << (NumBits - BitsLeft);
225
226 return R;
227 }
228
231 if (!MaybeRead)
232 return MaybeRead;
234
235 assert(NumBits <= 32 && NumBits >= 1 && "Invalid NumBits value");
236 const uint32_t MaskBitOrder = (NumBits - 1);
237 const uint32_t Mask = 1UL << MaskBitOrder;
238
239 if ((Piece & Mask) == 0)
240 return Piece;
241
243 unsigned NextBit = 0;
244 while (true) {
245 Result |= (Piece & (Mask - 1)) << NextBit;
246
247 if ((Piece & Mask) == 0)
248 return Result;
249
250 NextBit += NumBits-1;
251 if (NextBit >= 32)
253 "Unterminated VBR");
254
255 MaybeRead = Read(NumBits);
256 if (!MaybeRead)
257 return MaybeRead;
258 Piece = MaybeRead.get();
259 }
260 }
261
262
263
266 if (!MaybeRead)
267 return MaybeRead;
269 assert(NumBits <= 32 && NumBits >= 1 && "Invalid NumBits value");
270 const uint32_t MaskBitOrder = (NumBits - 1);
271 const uint32_t Mask = 1UL << MaskBitOrder;
272
273 if ((Piece & Mask) == 0)
275
277 unsigned NextBit = 0;
278 while (true) {
279 Result |= uint64_t(Piece & (Mask - 1)) << NextBit;
280
281 if ((Piece & Mask) == 0)
282 return Result;
283
284 NextBit += NumBits-1;
285 if (NextBit >= 64)
287 "Unterminated VBR");
288
289 MaybeRead = Read(NumBits);
290 if (!MaybeRead)
291 return MaybeRead;
292 Piece = MaybeRead.get();
293 }
294 }
295
297
298
299 if (sizeof(word_t) > 4 &&
300 BitsInCurWord >= 32) {
301 CurWord >>= BitsInCurWord-32;
302 BitsInCurWord = 32;
303 return;
304 }
305
306 BitsInCurWord = 0;
307 }
308
309
310 size_t SizeInBytes() const { return BitcodeBytes.size(); }
311
312
313 void skipToEnd() { NextChar = BitcodeBytes.size(); }
314
315
317
318
319 return Size < BitcodeBytes.size() * 8;
320 }
321};
322
323
324
352
353
354
355
356
357
359
360
361 unsigned CurCodeSize = 2;
362
363
364 std::vector<std::shared_ptr> CurAbbrevs;
365
366 struct Block {
367 unsigned PrevCodeSize;
368 std::vector<std::shared_ptr> PrevAbbrevs;
369
370 explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
371 };
372
373
375
377
378public:
380
388
402
403
405
406
407 enum {
408
409
411
412
413
415 };
416
417
419 while (true) {
422
424 if (!MaybeCode)
426 unsigned Code = MaybeCode.get();
427
429
433 }
434
438 else
439 return MaybeSubBlock.takeError();
440 }
441
444
445
447 return std::move(Err);
448 continue;
449 }
450
452 }
453 }
454
455
456
458 while (true) {
459
461 if (!MaybeEntry)
462 return MaybeEntry;
464
466 return Entry;
467
468
470 return std::move(Err);
471 }
472 }
473
475
476
477
478
479
481
482
483
485
487 ;
488
489 else
490 return Res.takeError();
491
494 if (!MaybeNum)
496 size_t NumFourBytes = MaybeNum.get();
497
498
499
500 size_t SkipTo = GetCurrentBitNo() + NumFourBytes * 4 * 8;
503 "can't skip block: already at end of stream");
506 "can't skip to bit %zu from %" PRIu64, SkipTo,
508
510 return Res;
511
513 }
514
515
517
519 if (BlockScope.empty()) return true;
520
521
522
524
525 popBlockScope();
526 return false;
527 }
528
529private:
530 void popBlockScope() {
531 CurCodeSize = BlockScope.back().PrevCodeSize;
532
533 CurAbbrevs = std::move(BlockScope.back().PrevAbbrevs);
535 }
536
537
538
539
540
541public:
542
545 if (AbbrevNo >= CurAbbrevs.size())
547 std::errc::illegal_byte_sequence, "Invalid abbrev number");
548 return CurAbbrevs[AbbrevNo].get();
549 }
550
551
553
557
558
559
560
562
563
564
565
566
567
570
571
572
574};
575
576}
577
578#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
This class maintains the abbreviations read from a block info block.
Definition BitstreamReader.h:38
const BlockInfo * getBlockInfo(unsigned BlockID) const
If there is block info for the specified ID, return it, otherwise return null.
Definition BitstreamReader.h:55
BlockInfo & getOrCreateBlockInfo(unsigned BlockID)
Definition BitstreamReader.h:66
Error JumpToBit(uint64_t BitNo)
Reset the stream to the specified bit number.
Definition BitstreamReader.h:128
BitstreamCursor(StringRef BitcodeBytes)
Definition BitstreamReader.h:384
bool AtEndOfStream()
Definition BitstreamReader.h:113
LLVM_ABI Error ReadAbbrevRecord()
uint64_t GetCurrentBitNo() const
Return the bit # of the bit we are reading.
Definition BitstreamReader.h:118
Expected< unsigned > ReadSubBlockID()
Having read the ENTER_SUBBLOCK code, read the BlockID for the block.
Definition BitstreamReader.h:480
static const size_t MaxChunkSize
Definition BitstreamReader.h:379
Expected< BitstreamEntry > advance(unsigned Flags=0)
Advance the current bitstream, returning the next entry in the stream.
Definition BitstreamReader.h:418
BitstreamCursor(MemoryBufferRef BitcodeBytes)
Definition BitstreamReader.h:386
Expected< BitstreamEntry > advanceSkippingSubblocks(unsigned Flags=0)
This is a convenience function for clients that don't expect any subblocks.
Definition BitstreamReader.h:457
Expected< const BitCodeAbbrev * > getAbbrev(unsigned AbbrevID)
Return the abbreviation for the specified AbbrevId.
Definition BitstreamReader.h:543
void setBlockInfo(BitstreamBlockInfo *BI)
Set the block info to be used by this BitstreamCursor to interpret abbreviated records.
Definition BitstreamReader.h:573
LLVM_ABI Expected< unsigned > readRecord(unsigned AbbrevID, SmallVectorImpl< uint64_t > &Vals, StringRef *Blob=nullptr)
LLVM_ABI Error EnterSubBlock(unsigned BlockID, unsigned *NumWordsP=nullptr)
Having read the ENTER_SUBBLOCK abbrevid, and enter the block.
Error SkipBlock()
Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body of this block.
Definition BitstreamReader.h:484
LLVM_ABI Expected< unsigned > skipRecord(unsigned AbbrevID)
Read the current record and discard it, returning the code for the record.
BitstreamCursor(ArrayRef< uint8_t > BitcodeBytes)
Definition BitstreamReader.h:382
bool ReadBlockEnd()
Definition BitstreamReader.h:518
BitstreamCursor()=default
@ AF_DontAutoprocessAbbrevs
If this flag is used, abbrev entries are returned just like normal records.
Definition BitstreamReader.h:414
@ AF_DontPopBlockAtEnd
If this flag is used, the advance() method does not automatically pop the block scope when the end of...
Definition BitstreamReader.h:410
Expected< unsigned > ReadCode()
Definition BitstreamReader.h:474
LLVM_ABI Expected< std::optional< BitstreamBlockInfo > > ReadBlockInfoBlock(bool ReadBlockInfoNames=false)
Read and return a block info block from the bitstream.
Expected< uint32_t > ReadVBR(const unsigned NumBits)
Definition BitstreamReader.h:229
unsigned getAbbrevIDWidth() const
Return the number of bits used to encode an abbrev #.
Definition BitstreamReader.h:404
bool canSkipToPos(size_t pos) const
Definition BitstreamReader.h:108
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
Error JumpToBit(uint64_t BitNo)
Reset the stream to the specified bit number.
Definition BitstreamReader.h:128
bool AtEndOfStream()
Definition BitstreamReader.h:113
const uint8_t * getPointerToBit(uint64_t BitNo, uint64_t NumBytes)
Get a pointer into the bitstream at the specified bit offset.
Definition BitstreamReader.h:156
uint64_t GetCurrentBitNo() const
Return the bit # of the bit we are reading.
Definition BitstreamReader.h:118
Expected< uint64_t > ReadVBR64(const unsigned NumBits)
Definition BitstreamReader.h:264
SimpleBitstreamCursor()=default
size_t SizeInBytes() const
Return the size of the stream in bytes.
Definition BitstreamReader.h:310
ArrayRef< uint8_t > getBitcodeBytes() const
Definition BitstreamReader.h:125
Expected< word_t > Read(unsigned NumBits)
Definition BitstreamReader.h:186
const uint8_t * getPointerToByte(uint64_t ByteNo, uint64_t NumBytes)
Get a pointer into the bitstream at the specified byte offset.
Definition BitstreamReader.h:149
SimpleBitstreamCursor(StringRef BitcodeBytes)
Definition BitstreamReader.h:103
SimpleBitstreamCursor(ArrayRef< uint8_t > BitcodeBytes)
Definition BitstreamReader.h:101
SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes)
Definition BitstreamReader.h:105
size_t word_t
This is the current data we have pulled from the stream but have not returned to the client.
Definition BitstreamReader.h:90
void SkipToFourByteBoundary()
Definition BitstreamReader.h:296
uint64_t getCurrentByteNo() const
Definition BitstreamReader.h:123
bool isSizePlausible(size_t Size) const
Check whether a reservation of Size elements is plausible.
Definition BitstreamReader.h:316
void skipToEnd()
Skip to the end of the file.
Definition BitstreamReader.h:313
Error fillCurWord()
Definition BitstreamReader.h:161
Expected< uint32_t > ReadVBR(const unsigned NumBits)
Definition BitstreamReader.h:229
bool canSkipToPos(size_t pos) const
Definition BitstreamReader.h:108
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
@ DEFINE_ABBREV
DEFINE_ABBREV - Defines an abbrev for the current block.
@ FIRST_APPLICATION_ABBREV
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
This is an optimization pass for GlobalISel generic memory operations.
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
This contains information emitted to BLOCKINFO_BLOCK blocks.
Definition BitstreamReader.h:42
std::vector< std::pair< unsigned, std::string > > RecordNames
Definition BitstreamReader.h:46
std::string Name
Definition BitstreamReader.h:45
unsigned BlockID
Definition BitstreamReader.h:43
std::vector< std::shared_ptr< BitCodeAbbrev > > Abbrevs
Definition BitstreamReader.h:44
When advancing through a bitstream cursor, each advance can discover a few different kinds of entries...
Definition BitstreamReader.h:325
static BitstreamEntry getSubBlock(unsigned ID)
Definition BitstreamReader.h:344
unsigned ID
Definition BitstreamReader.h:334
@ Record
Definition BitstreamReader.h:331
@ EndBlock
Definition BitstreamReader.h:328
@ SubBlock
Definition BitstreamReader.h:330
@ Error
Definition BitstreamReader.h:327
static BitstreamEntry getRecord(unsigned AbbrevID)
Definition BitstreamReader.h:348
static BitstreamEntry getEndBlock()
Definition BitstreamReader.h:340
static BitstreamEntry getError()
Definition BitstreamReader.h:336